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"
39 * - subvol delete -> delete when ref goes to 0? delete limits also?
43 * - copy also limits on subvol creation
45 * - caches fuer ulists
46 * - performance benchmarks
47 * - check all ioctl parameters
51 * Helpers to access qgroup reservation
53 * Callers should ensure the lock context and type are valid
56 static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
61 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
62 ret += qgroup->rsv.values[i];
67 #ifdef CONFIG_BTRFS_DEBUG
68 static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
70 if (type == BTRFS_QGROUP_RSV_DATA)
72 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
73 return "meta_pertrans";
74 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
75 return "meta_prealloc";
80 static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
81 struct btrfs_qgroup *qgroup, u64 num_bytes,
82 enum btrfs_qgroup_rsv_type type)
84 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
85 qgroup->rsv.values[type] += num_bytes;
88 static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
89 struct btrfs_qgroup *qgroup, u64 num_bytes,
90 enum btrfs_qgroup_rsv_type type)
92 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
93 if (qgroup->rsv.values[type] >= num_bytes) {
94 qgroup->rsv.values[type] -= num_bytes;
97 #ifdef CONFIG_BTRFS_DEBUG
99 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
100 qgroup->qgroupid, qgroup_rsv_type_str(type),
101 qgroup->rsv.values[type], num_bytes);
103 qgroup->rsv.values[type] = 0;
106 static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
107 struct btrfs_qgroup *dest,
108 struct btrfs_qgroup *src)
112 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
113 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
116 static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
117 struct btrfs_qgroup *dest,
118 struct btrfs_qgroup *src)
122 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
123 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
126 static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
129 if (qg->old_refcnt < seq)
130 qg->old_refcnt = seq;
131 qg->old_refcnt += mod;
134 static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
137 if (qg->new_refcnt < seq)
138 qg->new_refcnt = seq;
139 qg->new_refcnt += mod;
142 static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
144 if (qg->old_refcnt < seq)
146 return qg->old_refcnt - seq;
149 static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
151 if (qg->new_refcnt < seq)
153 return qg->new_refcnt - seq;
157 * glue structure to represent the relations between qgroups.
159 struct btrfs_qgroup_list {
160 struct list_head next_group;
161 struct list_head next_member;
162 struct btrfs_qgroup *group;
163 struct btrfs_qgroup *member;
166 static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
168 return (u64)(uintptr_t)qg;
171 static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
173 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
177 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
179 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
181 /* must be called with qgroup_ioctl_lock held */
182 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
185 struct rb_node *n = fs_info->qgroup_tree.rb_node;
186 struct btrfs_qgroup *qgroup;
189 qgroup = rb_entry(n, struct btrfs_qgroup, node);
190 if (qgroup->qgroupid < qgroupid)
192 else if (qgroup->qgroupid > qgroupid)
200 /* must be called with qgroup_lock held */
201 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
204 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
205 struct rb_node *parent = NULL;
206 struct btrfs_qgroup *qgroup;
210 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
212 if (qgroup->qgroupid < qgroupid)
214 else if (qgroup->qgroupid > qgroupid)
220 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
222 return ERR_PTR(-ENOMEM);
224 qgroup->qgroupid = qgroupid;
225 INIT_LIST_HEAD(&qgroup->groups);
226 INIT_LIST_HEAD(&qgroup->members);
227 INIT_LIST_HEAD(&qgroup->dirty);
229 rb_link_node(&qgroup->node, parent, p);
230 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
235 static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
237 struct btrfs_qgroup_list *list;
239 list_del(&qgroup->dirty);
240 while (!list_empty(&qgroup->groups)) {
241 list = list_first_entry(&qgroup->groups,
242 struct btrfs_qgroup_list, next_group);
243 list_del(&list->next_group);
244 list_del(&list->next_member);
248 while (!list_empty(&qgroup->members)) {
249 list = list_first_entry(&qgroup->members,
250 struct btrfs_qgroup_list, next_member);
251 list_del(&list->next_group);
252 list_del(&list->next_member);
258 /* must be called with qgroup_lock held */
259 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
261 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
266 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
267 __del_qgroup_rb(qgroup);
271 /* must be called with qgroup_lock held */
272 static int add_relation_rb(struct btrfs_fs_info *fs_info,
273 u64 memberid, u64 parentid)
275 struct btrfs_qgroup *member;
276 struct btrfs_qgroup *parent;
277 struct btrfs_qgroup_list *list;
279 member = find_qgroup_rb(fs_info, memberid);
280 parent = find_qgroup_rb(fs_info, parentid);
281 if (!member || !parent)
284 list = kzalloc(sizeof(*list), GFP_ATOMIC);
288 list->group = parent;
289 list->member = member;
290 list_add_tail(&list->next_group, &member->groups);
291 list_add_tail(&list->next_member, &parent->members);
296 /* must be called with qgroup_lock held */
297 static int del_relation_rb(struct btrfs_fs_info *fs_info,
298 u64 memberid, u64 parentid)
300 struct btrfs_qgroup *member;
301 struct btrfs_qgroup *parent;
302 struct btrfs_qgroup_list *list;
304 member = find_qgroup_rb(fs_info, memberid);
305 parent = find_qgroup_rb(fs_info, parentid);
306 if (!member || !parent)
309 list_for_each_entry(list, &member->groups, next_group) {
310 if (list->group == parent) {
311 list_del(&list->next_group);
312 list_del(&list->next_member);
320 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
321 int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
324 struct btrfs_qgroup *qgroup;
326 qgroup = find_qgroup_rb(fs_info, qgroupid);
329 if (qgroup->rfer != rfer || qgroup->excl != excl)
336 * The full config is read in one go, only called from open_ctree()
337 * It doesn't use any locking, as at this point we're still single-threaded
339 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
341 struct btrfs_key key;
342 struct btrfs_key found_key;
343 struct btrfs_root *quota_root = fs_info->quota_root;
344 struct btrfs_path *path = NULL;
345 struct extent_buffer *l;
349 u64 rescan_progress = 0;
351 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
354 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
355 if (!fs_info->qgroup_ulist) {
360 path = btrfs_alloc_path();
366 /* default this to quota off, in case no status key is found */
367 fs_info->qgroup_flags = 0;
370 * pass 1: read status, all qgroup infos and limits
375 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
380 struct btrfs_qgroup *qgroup;
382 slot = path->slots[0];
384 btrfs_item_key_to_cpu(l, &found_key, slot);
386 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
387 struct btrfs_qgroup_status_item *ptr;
389 ptr = btrfs_item_ptr(l, slot,
390 struct btrfs_qgroup_status_item);
392 if (btrfs_qgroup_status_version(l, ptr) !=
393 BTRFS_QGROUP_STATUS_VERSION) {
395 "old qgroup version, quota disabled");
398 if (btrfs_qgroup_status_generation(l, ptr) !=
399 fs_info->generation) {
400 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
402 "qgroup generation mismatch, marked as inconsistent");
404 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
406 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
410 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
411 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
414 qgroup = find_qgroup_rb(fs_info, found_key.offset);
415 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
416 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
417 btrfs_err(fs_info, "inconsistent qgroup config");
418 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
421 qgroup = add_qgroup_rb(fs_info, found_key.offset);
422 if (IS_ERR(qgroup)) {
423 ret = PTR_ERR(qgroup);
427 switch (found_key.type) {
428 case BTRFS_QGROUP_INFO_KEY: {
429 struct btrfs_qgroup_info_item *ptr;
431 ptr = btrfs_item_ptr(l, slot,
432 struct btrfs_qgroup_info_item);
433 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
434 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
435 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
436 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
437 /* generation currently unused */
440 case BTRFS_QGROUP_LIMIT_KEY: {
441 struct btrfs_qgroup_limit_item *ptr;
443 ptr = btrfs_item_ptr(l, slot,
444 struct btrfs_qgroup_limit_item);
445 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
446 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
447 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
448 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
449 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
454 ret = btrfs_next_item(quota_root, path);
460 btrfs_release_path(path);
463 * pass 2: read all qgroup relations
466 key.type = BTRFS_QGROUP_RELATION_KEY;
468 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
472 slot = path->slots[0];
474 btrfs_item_key_to_cpu(l, &found_key, slot);
476 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
479 if (found_key.objectid > found_key.offset) {
480 /* parent <- member, not needed to build config */
481 /* FIXME should we omit the key completely? */
485 ret = add_relation_rb(fs_info, found_key.objectid,
487 if (ret == -ENOENT) {
489 "orphan qgroup relation 0x%llx->0x%llx",
490 found_key.objectid, found_key.offset);
491 ret = 0; /* ignore the error */
496 ret = btrfs_next_item(quota_root, path);
503 fs_info->qgroup_flags |= flags;
504 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
505 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
506 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
508 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
509 btrfs_free_path(path);
512 ulist_free(fs_info->qgroup_ulist);
513 fs_info->qgroup_ulist = NULL;
514 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
517 return ret < 0 ? ret : 0;
521 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
522 * first two are in single-threaded paths.And for the third one, we have set
523 * quota_root to be null with qgroup_lock held before, so it is safe to clean
524 * up the in-memory structures without qgroup_lock held.
526 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
529 struct btrfs_qgroup *qgroup;
531 while ((n = rb_first(&fs_info->qgroup_tree))) {
532 qgroup = rb_entry(n, struct btrfs_qgroup, node);
533 rb_erase(n, &fs_info->qgroup_tree);
534 __del_qgroup_rb(qgroup);
537 * we call btrfs_free_qgroup_config() when umounting
538 * filesystem and disabling quota, so we set qgroup_ulist
539 * to be null here to avoid double free.
541 ulist_free(fs_info->qgroup_ulist);
542 fs_info->qgroup_ulist = NULL;
545 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
546 struct btrfs_root *quota_root,
550 struct btrfs_path *path;
551 struct btrfs_key key;
553 path = btrfs_alloc_path();
558 key.type = BTRFS_QGROUP_RELATION_KEY;
561 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
563 btrfs_mark_buffer_dirty(path->nodes[0]);
565 btrfs_free_path(path);
569 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
570 struct btrfs_root *quota_root,
574 struct btrfs_path *path;
575 struct btrfs_key key;
577 path = btrfs_alloc_path();
582 key.type = BTRFS_QGROUP_RELATION_KEY;
585 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
594 ret = btrfs_del_item(trans, quota_root, path);
596 btrfs_free_path(path);
600 static int add_qgroup_item(struct btrfs_trans_handle *trans,
601 struct btrfs_root *quota_root, u64 qgroupid)
604 struct btrfs_path *path;
605 struct btrfs_qgroup_info_item *qgroup_info;
606 struct btrfs_qgroup_limit_item *qgroup_limit;
607 struct extent_buffer *leaf;
608 struct btrfs_key key;
610 if (btrfs_is_testing(quota_root->fs_info))
613 path = btrfs_alloc_path();
618 key.type = BTRFS_QGROUP_INFO_KEY;
619 key.offset = qgroupid;
622 * Avoid a transaction abort by catching -EEXIST here. In that
623 * case, we proceed by re-initializing the existing structure
627 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
628 sizeof(*qgroup_info));
629 if (ret && ret != -EEXIST)
632 leaf = path->nodes[0];
633 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
634 struct btrfs_qgroup_info_item);
635 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
636 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
637 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
638 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
639 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
641 btrfs_mark_buffer_dirty(leaf);
643 btrfs_release_path(path);
645 key.type = BTRFS_QGROUP_LIMIT_KEY;
646 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
647 sizeof(*qgroup_limit));
648 if (ret && ret != -EEXIST)
651 leaf = path->nodes[0];
652 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
653 struct btrfs_qgroup_limit_item);
654 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
655 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
656 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
657 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
658 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
660 btrfs_mark_buffer_dirty(leaf);
664 btrfs_free_path(path);
668 static int del_qgroup_item(struct btrfs_trans_handle *trans,
669 struct btrfs_root *quota_root, u64 qgroupid)
672 struct btrfs_path *path;
673 struct btrfs_key key;
675 path = btrfs_alloc_path();
680 key.type = BTRFS_QGROUP_INFO_KEY;
681 key.offset = qgroupid;
682 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
691 ret = btrfs_del_item(trans, quota_root, path);
695 btrfs_release_path(path);
697 key.type = BTRFS_QGROUP_LIMIT_KEY;
698 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
707 ret = btrfs_del_item(trans, quota_root, path);
710 btrfs_free_path(path);
714 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
715 struct btrfs_root *root,
716 struct btrfs_qgroup *qgroup)
718 struct btrfs_path *path;
719 struct btrfs_key key;
720 struct extent_buffer *l;
721 struct btrfs_qgroup_limit_item *qgroup_limit;
726 key.type = BTRFS_QGROUP_LIMIT_KEY;
727 key.offset = qgroup->qgroupid;
729 path = btrfs_alloc_path();
733 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
741 slot = path->slots[0];
742 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
743 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
744 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
745 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
746 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
747 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
749 btrfs_mark_buffer_dirty(l);
752 btrfs_free_path(path);
756 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
757 struct btrfs_root *root,
758 struct btrfs_qgroup *qgroup)
760 struct btrfs_path *path;
761 struct btrfs_key key;
762 struct extent_buffer *l;
763 struct btrfs_qgroup_info_item *qgroup_info;
767 if (btrfs_is_testing(root->fs_info))
771 key.type = BTRFS_QGROUP_INFO_KEY;
772 key.offset = qgroup->qgroupid;
774 path = btrfs_alloc_path();
778 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
786 slot = path->slots[0];
787 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
788 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
789 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
790 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
791 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
792 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
794 btrfs_mark_buffer_dirty(l);
797 btrfs_free_path(path);
801 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
802 struct btrfs_fs_info *fs_info,
803 struct btrfs_root *root)
805 struct btrfs_path *path;
806 struct btrfs_key key;
807 struct extent_buffer *l;
808 struct btrfs_qgroup_status_item *ptr;
813 key.type = BTRFS_QGROUP_STATUS_KEY;
816 path = btrfs_alloc_path();
820 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
828 slot = path->slots[0];
829 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
830 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
831 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
832 btrfs_set_qgroup_status_rescan(l, ptr,
833 fs_info->qgroup_rescan_progress.objectid);
835 btrfs_mark_buffer_dirty(l);
838 btrfs_free_path(path);
843 * called with qgroup_lock held
845 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
846 struct btrfs_root *root)
848 struct btrfs_path *path;
849 struct btrfs_key key;
850 struct extent_buffer *leaf = NULL;
854 path = btrfs_alloc_path();
858 path->leave_spinning = 1;
865 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
868 leaf = path->nodes[0];
869 nr = btrfs_header_nritems(leaf);
873 * delete the leaf one by one
874 * since the whole tree is going
878 ret = btrfs_del_items(trans, root, path, 0, nr);
882 btrfs_release_path(path);
886 btrfs_free_path(path);
890 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
891 struct btrfs_fs_info *fs_info)
893 struct btrfs_root *quota_root;
894 struct btrfs_root *tree_root = fs_info->tree_root;
895 struct btrfs_path *path = NULL;
896 struct btrfs_qgroup_status_item *ptr;
897 struct extent_buffer *leaf;
898 struct btrfs_key key;
899 struct btrfs_key found_key;
900 struct btrfs_qgroup *qgroup = NULL;
904 mutex_lock(&fs_info->qgroup_ioctl_lock);
905 if (fs_info->quota_root)
908 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
909 if (!fs_info->qgroup_ulist) {
915 * initially create the quota tree
917 quota_root = btrfs_create_tree(trans, fs_info,
918 BTRFS_QUOTA_TREE_OBJECTID);
919 if (IS_ERR(quota_root)) {
920 ret = PTR_ERR(quota_root);
924 path = btrfs_alloc_path();
931 key.type = BTRFS_QGROUP_STATUS_KEY;
934 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
939 leaf = path->nodes[0];
940 ptr = btrfs_item_ptr(leaf, path->slots[0],
941 struct btrfs_qgroup_status_item);
942 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
943 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
944 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
945 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
946 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
947 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
949 btrfs_mark_buffer_dirty(leaf);
952 key.type = BTRFS_ROOT_REF_KEY;
955 btrfs_release_path(path);
956 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
964 slot = path->slots[0];
965 leaf = path->nodes[0];
966 btrfs_item_key_to_cpu(leaf, &found_key, slot);
968 if (found_key.type == BTRFS_ROOT_REF_KEY) {
969 ret = add_qgroup_item(trans, quota_root,
974 qgroup = add_qgroup_rb(fs_info, found_key.offset);
975 if (IS_ERR(qgroup)) {
976 ret = PTR_ERR(qgroup);
980 ret = btrfs_next_item(tree_root, path);
988 btrfs_release_path(path);
989 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
993 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
994 if (IS_ERR(qgroup)) {
995 ret = PTR_ERR(qgroup);
998 spin_lock(&fs_info->qgroup_lock);
999 fs_info->quota_root = quota_root;
1000 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1001 spin_unlock(&fs_info->qgroup_lock);
1002 ret = qgroup_rescan_init(fs_info, 0, 1);
1004 qgroup_rescan_zero_tracking(fs_info);
1005 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1006 &fs_info->qgroup_rescan_work);
1010 btrfs_free_path(path);
1013 free_extent_buffer(quota_root->node);
1014 free_extent_buffer(quota_root->commit_root);
1019 ulist_free(fs_info->qgroup_ulist);
1020 fs_info->qgroup_ulist = NULL;
1022 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1026 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
1027 struct btrfs_fs_info *fs_info)
1029 struct btrfs_root *quota_root;
1032 mutex_lock(&fs_info->qgroup_ioctl_lock);
1033 if (!fs_info->quota_root)
1035 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1036 btrfs_qgroup_wait_for_completion(fs_info, false);
1037 spin_lock(&fs_info->qgroup_lock);
1038 quota_root = fs_info->quota_root;
1039 fs_info->quota_root = NULL;
1040 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1041 spin_unlock(&fs_info->qgroup_lock);
1043 btrfs_free_qgroup_config(fs_info);
1045 ret = btrfs_clean_quota_tree(trans, quota_root);
1049 ret = btrfs_del_root(trans, fs_info, "a_root->root_key);
1053 list_del("a_root->dirty_list);
1055 btrfs_tree_lock(quota_root->node);
1056 clean_tree_block(fs_info, quota_root->node);
1057 btrfs_tree_unlock(quota_root->node);
1058 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1060 free_extent_buffer(quota_root->node);
1061 free_extent_buffer(quota_root->commit_root);
1064 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1068 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1069 struct btrfs_qgroup *qgroup)
1071 if (list_empty(&qgroup->dirty))
1072 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1076 * The easy accounting, we're updating qgroup relationship whose child qgroup
1077 * only has exclusive extents.
1079 * In this case, all exclsuive extents will also be exlusive for parent, so
1080 * excl/rfer just get added/removed.
1082 * So is qgroup reservation space, which should also be added/removed to
1084 * Or when child tries to release reservation space, parent will underflow its
1085 * reservation (for relationship adding case).
1087 * Caller should hold fs_info->qgroup_lock.
1089 static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1090 struct ulist *tmp, u64 ref_root,
1091 struct btrfs_qgroup *src, int sign)
1093 struct btrfs_qgroup *qgroup;
1094 struct btrfs_qgroup_list *glist;
1095 struct ulist_node *unode;
1096 struct ulist_iterator uiter;
1097 u64 num_bytes = src->excl;
1100 qgroup = find_qgroup_rb(fs_info, ref_root);
1104 qgroup->rfer += sign * num_bytes;
1105 qgroup->rfer_cmpr += sign * num_bytes;
1107 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1108 qgroup->excl += sign * num_bytes;
1109 qgroup->excl_cmpr += sign * num_bytes;
1112 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1114 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1116 qgroup_dirty(fs_info, qgroup);
1118 /* Get all of the parent groups that contain this qgroup */
1119 list_for_each_entry(glist, &qgroup->groups, next_group) {
1120 ret = ulist_add(tmp, glist->group->qgroupid,
1121 qgroup_to_aux(glist->group), GFP_ATOMIC);
1126 /* Iterate all of the parents and adjust their reference counts */
1127 ULIST_ITER_INIT(&uiter);
1128 while ((unode = ulist_next(tmp, &uiter))) {
1129 qgroup = unode_aux_to_qgroup(unode);
1130 qgroup->rfer += sign * num_bytes;
1131 qgroup->rfer_cmpr += sign * num_bytes;
1132 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1133 qgroup->excl += sign * num_bytes;
1135 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1137 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1138 qgroup->excl_cmpr += sign * num_bytes;
1139 qgroup_dirty(fs_info, qgroup);
1141 /* Add any parents of the parents */
1142 list_for_each_entry(glist, &qgroup->groups, next_group) {
1143 ret = ulist_add(tmp, glist->group->qgroupid,
1144 qgroup_to_aux(glist->group), GFP_ATOMIC);
1156 * Quick path for updating qgroup with only excl refs.
1158 * In that case, just update all parent will be enough.
1159 * Or we needs to do a full rescan.
1160 * Caller should also hold fs_info->qgroup_lock.
1162 * Return 0 for quick update, return >0 for need to full rescan
1163 * and mark INCONSISTENT flag.
1164 * Return < 0 for other error.
1166 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1167 struct ulist *tmp, u64 src, u64 dst,
1170 struct btrfs_qgroup *qgroup;
1174 qgroup = find_qgroup_rb(fs_info, src);
1177 if (qgroup->excl == qgroup->rfer) {
1179 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1188 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1192 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1193 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1195 struct btrfs_root *quota_root;
1196 struct btrfs_qgroup *parent;
1197 struct btrfs_qgroup *member;
1198 struct btrfs_qgroup_list *list;
1202 /* Check the level of src and dst first */
1203 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1206 tmp = ulist_alloc(GFP_KERNEL);
1210 mutex_lock(&fs_info->qgroup_ioctl_lock);
1211 quota_root = fs_info->quota_root;
1216 member = find_qgroup_rb(fs_info, src);
1217 parent = find_qgroup_rb(fs_info, dst);
1218 if (!member || !parent) {
1223 /* check if such qgroup relation exist firstly */
1224 list_for_each_entry(list, &member->groups, next_group) {
1225 if (list->group == parent) {
1231 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1235 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1237 del_qgroup_relation_item(trans, quota_root, src, dst);
1241 spin_lock(&fs_info->qgroup_lock);
1242 ret = add_relation_rb(fs_info, src, dst);
1244 spin_unlock(&fs_info->qgroup_lock);
1247 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1248 spin_unlock(&fs_info->qgroup_lock);
1250 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1255 static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
1256 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1258 struct btrfs_root *quota_root;
1259 struct btrfs_qgroup *parent;
1260 struct btrfs_qgroup *member;
1261 struct btrfs_qgroup_list *list;
1266 tmp = ulist_alloc(GFP_KERNEL);
1270 quota_root = fs_info->quota_root;
1276 member = find_qgroup_rb(fs_info, src);
1277 parent = find_qgroup_rb(fs_info, dst);
1278 if (!member || !parent) {
1283 /* check if such qgroup relation exist firstly */
1284 list_for_each_entry(list, &member->groups, next_group) {
1285 if (list->group == parent)
1291 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1292 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1296 spin_lock(&fs_info->qgroup_lock);
1297 del_relation_rb(fs_info, src, dst);
1298 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1299 spin_unlock(&fs_info->qgroup_lock);
1305 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1306 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1310 mutex_lock(&fs_info->qgroup_ioctl_lock);
1311 ret = __del_qgroup_relation(trans, fs_info, src, dst);
1312 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1317 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1318 struct btrfs_fs_info *fs_info, u64 qgroupid)
1320 struct btrfs_root *quota_root;
1321 struct btrfs_qgroup *qgroup;
1324 mutex_lock(&fs_info->qgroup_ioctl_lock);
1325 quota_root = fs_info->quota_root;
1330 qgroup = find_qgroup_rb(fs_info, qgroupid);
1336 ret = add_qgroup_item(trans, quota_root, qgroupid);
1340 spin_lock(&fs_info->qgroup_lock);
1341 qgroup = add_qgroup_rb(fs_info, qgroupid);
1342 spin_unlock(&fs_info->qgroup_lock);
1345 ret = PTR_ERR(qgroup);
1347 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1351 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1352 struct btrfs_fs_info *fs_info, u64 qgroupid)
1354 struct btrfs_root *quota_root;
1355 struct btrfs_qgroup *qgroup;
1356 struct btrfs_qgroup_list *list;
1359 mutex_lock(&fs_info->qgroup_ioctl_lock);
1360 quota_root = fs_info->quota_root;
1366 qgroup = find_qgroup_rb(fs_info, qgroupid);
1371 /* check if there are no children of this qgroup */
1372 if (!list_empty(&qgroup->members)) {
1377 ret = del_qgroup_item(trans, quota_root, qgroupid);
1378 if (ret && ret != -ENOENT)
1381 while (!list_empty(&qgroup->groups)) {
1382 list = list_first_entry(&qgroup->groups,
1383 struct btrfs_qgroup_list, next_group);
1384 ret = __del_qgroup_relation(trans, fs_info,
1386 list->group->qgroupid);
1391 spin_lock(&fs_info->qgroup_lock);
1392 del_qgroup_rb(fs_info, qgroupid);
1393 spin_unlock(&fs_info->qgroup_lock);
1395 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1399 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1400 struct btrfs_fs_info *fs_info, u64 qgroupid,
1401 struct btrfs_qgroup_limit *limit)
1403 struct btrfs_root *quota_root;
1404 struct btrfs_qgroup *qgroup;
1406 /* Sometimes we would want to clear the limit on this qgroup.
1407 * To meet this requirement, we treat the -1 as a special value
1408 * which tell kernel to clear the limit on this qgroup.
1410 const u64 CLEAR_VALUE = -1;
1412 mutex_lock(&fs_info->qgroup_ioctl_lock);
1413 quota_root = fs_info->quota_root;
1419 qgroup = find_qgroup_rb(fs_info, qgroupid);
1425 spin_lock(&fs_info->qgroup_lock);
1426 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1427 if (limit->max_rfer == CLEAR_VALUE) {
1428 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1429 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1430 qgroup->max_rfer = 0;
1432 qgroup->max_rfer = limit->max_rfer;
1435 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1436 if (limit->max_excl == CLEAR_VALUE) {
1437 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1438 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1439 qgroup->max_excl = 0;
1441 qgroup->max_excl = limit->max_excl;
1444 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1445 if (limit->rsv_rfer == CLEAR_VALUE) {
1446 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1447 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1448 qgroup->rsv_rfer = 0;
1450 qgroup->rsv_rfer = limit->rsv_rfer;
1453 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1454 if (limit->rsv_excl == CLEAR_VALUE) {
1455 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1456 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1457 qgroup->rsv_excl = 0;
1459 qgroup->rsv_excl = limit->rsv_excl;
1462 qgroup->lim_flags |= limit->flags;
1464 spin_unlock(&fs_info->qgroup_lock);
1466 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1468 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1469 btrfs_info(fs_info, "unable to update quota limit for %llu",
1474 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1478 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1479 struct btrfs_delayed_ref_root *delayed_refs,
1480 struct btrfs_qgroup_extent_record *record)
1482 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1483 struct rb_node *parent_node = NULL;
1484 struct btrfs_qgroup_extent_record *entry;
1485 u64 bytenr = record->bytenr;
1487 lockdep_assert_held(&delayed_refs->lock);
1488 trace_btrfs_qgroup_trace_extent(fs_info, record);
1492 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1494 if (bytenr < entry->bytenr)
1496 else if (bytenr > entry->bytenr)
1497 p = &(*p)->rb_right;
1502 rb_link_node(&record->node, parent_node, p);
1503 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1507 int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1508 struct btrfs_qgroup_extent_record *qrecord)
1510 struct ulist *old_root;
1511 u64 bytenr = qrecord->bytenr;
1514 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
1516 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1518 "error accounting new delayed refs extent (err code: %d), quota inconsistent",
1524 * Here we don't need to get the lock of
1525 * trans->transaction->delayed_refs, since inserted qrecord won't
1526 * be deleted, only qrecord->node may be modified (new qrecord insert)
1528 * So modifying qrecord->old_roots is safe here
1530 qrecord->old_roots = old_root;
1534 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
1535 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1538 struct btrfs_qgroup_extent_record *record;
1539 struct btrfs_delayed_ref_root *delayed_refs;
1542 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1543 || bytenr == 0 || num_bytes == 0)
1545 if (WARN_ON(trans == NULL))
1547 record = kmalloc(sizeof(*record), gfp_flag);
1551 delayed_refs = &trans->transaction->delayed_refs;
1552 record->bytenr = bytenr;
1553 record->num_bytes = num_bytes;
1554 record->old_roots = NULL;
1556 spin_lock(&delayed_refs->lock);
1557 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1558 spin_unlock(&delayed_refs->lock);
1563 return btrfs_qgroup_trace_extent_post(fs_info, record);
1566 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1567 struct btrfs_fs_info *fs_info,
1568 struct extent_buffer *eb)
1570 int nr = btrfs_header_nritems(eb);
1571 int i, extent_type, ret;
1572 struct btrfs_key key;
1573 struct btrfs_file_extent_item *fi;
1574 u64 bytenr, num_bytes;
1576 /* We can be called directly from walk_up_proc() */
1577 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1580 for (i = 0; i < nr; i++) {
1581 btrfs_item_key_to_cpu(eb, &key, i);
1583 if (key.type != BTRFS_EXTENT_DATA_KEY)
1586 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1587 /* filter out non qgroup-accountable extents */
1588 extent_type = btrfs_file_extent_type(eb, fi);
1590 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1593 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1597 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1599 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1600 num_bytes, GFP_NOFS);
1609 * Walk up the tree from the bottom, freeing leaves and any interior
1610 * nodes which have had all slots visited. If a node (leaf or
1611 * interior) is freed, the node above it will have it's slot
1612 * incremented. The root node will never be freed.
1614 * At the end of this function, we should have a path which has all
1615 * slots incremented to the next position for a search. If we need to
1616 * read a new node it will be NULL and the node above it will have the
1617 * correct slot selected for a later read.
1619 * If we increment the root nodes slot counter past the number of
1620 * elements, 1 is returned to signal completion of the search.
1622 static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
1626 struct extent_buffer *eb;
1628 if (root_level == 0)
1631 while (level <= root_level) {
1632 eb = path->nodes[level];
1633 nr = btrfs_header_nritems(eb);
1634 path->slots[level]++;
1635 slot = path->slots[level];
1636 if (slot >= nr || level == 0) {
1638 * Don't free the root - we will detect this
1639 * condition after our loop and return a
1640 * positive value for caller to stop walking the tree.
1642 if (level != root_level) {
1643 btrfs_tree_unlock_rw(eb, path->locks[level]);
1644 path->locks[level] = 0;
1646 free_extent_buffer(eb);
1647 path->nodes[level] = NULL;
1648 path->slots[level] = 0;
1652 * We have a valid slot to walk back down
1653 * from. Stop here so caller can process these
1662 eb = path->nodes[root_level];
1663 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1669 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1670 struct btrfs_root *root,
1671 struct extent_buffer *root_eb,
1672 u64 root_gen, int root_level)
1674 struct btrfs_fs_info *fs_info = root->fs_info;
1677 struct extent_buffer *eb = root_eb;
1678 struct btrfs_path *path = NULL;
1680 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
1681 BUG_ON(root_eb == NULL);
1683 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1686 if (!extent_buffer_uptodate(root_eb)) {
1687 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
1692 if (root_level == 0) {
1693 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
1697 path = btrfs_alloc_path();
1702 * Walk down the tree. Missing extent blocks are filled in as
1703 * we go. Metadata is accounted every time we read a new
1706 * When we reach a leaf, we account for file extent items in it,
1707 * walk back up the tree (adjusting slot pointers as we go)
1708 * and restart the search process.
1710 extent_buffer_get(root_eb); /* For path */
1711 path->nodes[root_level] = root_eb;
1712 path->slots[root_level] = 0;
1713 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1716 while (level >= 0) {
1717 if (path->nodes[level] == NULL) {
1718 struct btrfs_key first_key;
1724 * We need to get child blockptr/gen from parent before
1727 eb = path->nodes[level + 1];
1728 parent_slot = path->slots[level + 1];
1729 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1730 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1731 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
1733 eb = read_tree_block(fs_info, child_bytenr, child_gen,
1738 } else if (!extent_buffer_uptodate(eb)) {
1739 free_extent_buffer(eb);
1744 path->nodes[level] = eb;
1745 path->slots[level] = 0;
1747 btrfs_tree_read_lock(eb);
1748 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1749 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1751 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1760 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1761 path->nodes[level]);
1765 /* Nonzero return here means we completed our search */
1766 ret = adjust_slots_upwards(path, root_level);
1770 /* Restart search with new slots */
1779 btrfs_free_path(path);
1784 #define UPDATE_NEW 0
1785 #define UPDATE_OLD 1
1787 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1789 static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1790 struct ulist *roots, struct ulist *tmp,
1791 struct ulist *qgroups, u64 seq, int update_old)
1793 struct ulist_node *unode;
1794 struct ulist_iterator uiter;
1795 struct ulist_node *tmp_unode;
1796 struct ulist_iterator tmp_uiter;
1797 struct btrfs_qgroup *qg;
1802 ULIST_ITER_INIT(&uiter);
1803 while ((unode = ulist_next(roots, &uiter))) {
1804 qg = find_qgroup_rb(fs_info, unode->val);
1809 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
1813 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
1816 ULIST_ITER_INIT(&tmp_uiter);
1817 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1818 struct btrfs_qgroup_list *glist;
1820 qg = unode_aux_to_qgroup(tmp_unode);
1822 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1824 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1825 list_for_each_entry(glist, &qg->groups, next_group) {
1826 ret = ulist_add(qgroups, glist->group->qgroupid,
1827 qgroup_to_aux(glist->group),
1831 ret = ulist_add(tmp, glist->group->qgroupid,
1832 qgroup_to_aux(glist->group),
1843 * Update qgroup rfer/excl counters.
1844 * Rfer update is easy, codes can explain themselves.
1846 * Excl update is tricky, the update is split into 2 part.
1847 * Part 1: Possible exclusive <-> sharing detect:
1849 * -------------------------------------
1851 * -------------------------------------
1853 * -------------------------------------
1856 * A: cur_old_roots < nr_old_roots (not exclusive before)
1857 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1858 * B: cur_new_roots < nr_new_roots (not exclusive now)
1859 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
1862 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1863 * *: Definitely not changed. **: Possible unchanged.
1865 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1867 * To make the logic clear, we first use condition A and B to split
1868 * combination into 4 results.
1870 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1871 * only on variant maybe 0.
1873 * Lastly, check result **, since there are 2 variants maybe 0, split them
1875 * But this time we don't need to consider other things, the codes and logic
1876 * is easy to understand now.
1878 static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1879 struct ulist *qgroups,
1882 u64 num_bytes, u64 seq)
1884 struct ulist_node *unode;
1885 struct ulist_iterator uiter;
1886 struct btrfs_qgroup *qg;
1887 u64 cur_new_count, cur_old_count;
1889 ULIST_ITER_INIT(&uiter);
1890 while ((unode = ulist_next(qgroups, &uiter))) {
1893 qg = unode_aux_to_qgroup(unode);
1894 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1895 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1897 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1898 cur_old_count, cur_new_count);
1900 /* Rfer update part */
1901 if (cur_old_count == 0 && cur_new_count > 0) {
1902 qg->rfer += num_bytes;
1903 qg->rfer_cmpr += num_bytes;
1906 if (cur_old_count > 0 && cur_new_count == 0) {
1907 qg->rfer -= num_bytes;
1908 qg->rfer_cmpr -= num_bytes;
1912 /* Excl update part */
1913 /* Exclusive/none -> shared case */
1914 if (cur_old_count == nr_old_roots &&
1915 cur_new_count < nr_new_roots) {
1916 /* Exclusive -> shared */
1917 if (cur_old_count != 0) {
1918 qg->excl -= num_bytes;
1919 qg->excl_cmpr -= num_bytes;
1924 /* Shared -> exclusive/none case */
1925 if (cur_old_count < nr_old_roots &&
1926 cur_new_count == nr_new_roots) {
1927 /* Shared->exclusive */
1928 if (cur_new_count != 0) {
1929 qg->excl += num_bytes;
1930 qg->excl_cmpr += num_bytes;
1935 /* Exclusive/none -> exclusive/none case */
1936 if (cur_old_count == nr_old_roots &&
1937 cur_new_count == nr_new_roots) {
1938 if (cur_old_count == 0) {
1939 /* None -> exclusive/none */
1941 if (cur_new_count != 0) {
1942 /* None -> exclusive */
1943 qg->excl += num_bytes;
1944 qg->excl_cmpr += num_bytes;
1947 /* None -> none, nothing changed */
1949 /* Exclusive -> exclusive/none */
1951 if (cur_new_count == 0) {
1952 /* Exclusive -> none */
1953 qg->excl -= num_bytes;
1954 qg->excl_cmpr -= num_bytes;
1957 /* Exclusive -> exclusive, nothing changed */
1962 qgroup_dirty(fs_info, qg);
1968 * Check if the @roots potentially is a list of fs tree roots
1970 * Return 0 for definitely not a fs/subvol tree roots ulist
1971 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1974 static int maybe_fs_roots(struct ulist *roots)
1976 struct ulist_node *unode;
1977 struct ulist_iterator uiter;
1979 /* Empty one, still possible for fs roots */
1980 if (!roots || roots->nnodes == 0)
1983 ULIST_ITER_INIT(&uiter);
1984 unode = ulist_next(roots, &uiter);
1989 * If it contains fs tree roots, then it must belong to fs/subvol
1991 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
1993 return is_fstree(unode->val);
1997 btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1998 struct btrfs_fs_info *fs_info,
1999 u64 bytenr, u64 num_bytes,
2000 struct ulist *old_roots, struct ulist *new_roots)
2002 struct ulist *qgroups = NULL;
2003 struct ulist *tmp = NULL;
2005 u64 nr_new_roots = 0;
2006 u64 nr_old_roots = 0;
2009 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2013 if (!maybe_fs_roots(new_roots))
2015 nr_new_roots = new_roots->nnodes;
2018 if (!maybe_fs_roots(old_roots))
2020 nr_old_roots = old_roots->nnodes;
2023 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2024 if (nr_old_roots == 0 && nr_new_roots == 0)
2027 BUG_ON(!fs_info->quota_root);
2029 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
2030 nr_old_roots, nr_new_roots);
2032 qgroups = ulist_alloc(GFP_NOFS);
2037 tmp = ulist_alloc(GFP_NOFS);
2043 mutex_lock(&fs_info->qgroup_rescan_lock);
2044 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2045 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2046 mutex_unlock(&fs_info->qgroup_rescan_lock);
2051 mutex_unlock(&fs_info->qgroup_rescan_lock);
2053 spin_lock(&fs_info->qgroup_lock);
2054 seq = fs_info->qgroup_seq;
2056 /* Update old refcnts using old_roots */
2057 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2062 /* Update new refcnts using new_roots */
2063 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2068 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2072 * Bump qgroup_seq to avoid seq overlap
2074 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2076 spin_unlock(&fs_info->qgroup_lock);
2079 ulist_free(qgroups);
2080 ulist_free(old_roots);
2081 ulist_free(new_roots);
2085 int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2087 struct btrfs_fs_info *fs_info = trans->fs_info;
2088 struct btrfs_qgroup_extent_record *record;
2089 struct btrfs_delayed_ref_root *delayed_refs;
2090 struct ulist *new_roots = NULL;
2091 struct rb_node *node;
2095 delayed_refs = &trans->transaction->delayed_refs;
2096 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2097 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2098 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2101 trace_btrfs_qgroup_account_extents(fs_info, record);
2105 * Old roots should be searched when inserting qgroup
2108 if (WARN_ON(!record->old_roots)) {
2109 /* Search commit root to find old_roots */
2110 ret = btrfs_find_all_roots(NULL, fs_info,
2112 &record->old_roots, false);
2118 * Use SEQ_LAST as time_seq to do special search, which
2119 * doesn't lock tree or delayed_refs and search current
2120 * root. It's safe inside commit_transaction().
2122 ret = btrfs_find_all_roots(trans, fs_info,
2123 record->bytenr, SEQ_LAST, &new_roots, false);
2126 if (qgroup_to_skip) {
2127 ulist_del(new_roots, qgroup_to_skip, 0);
2128 ulist_del(record->old_roots, qgroup_to_skip,
2131 ret = btrfs_qgroup_account_extent(trans, fs_info,
2132 record->bytenr, record->num_bytes,
2133 record->old_roots, new_roots);
2134 record->old_roots = NULL;
2138 ulist_free(record->old_roots);
2139 ulist_free(new_roots);
2141 rb_erase(node, &delayed_refs->dirty_extent_root);
2149 * called from commit_transaction. Writes all changed qgroups to disk.
2151 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2152 struct btrfs_fs_info *fs_info)
2154 struct btrfs_root *quota_root = fs_info->quota_root;
2160 spin_lock(&fs_info->qgroup_lock);
2161 while (!list_empty(&fs_info->dirty_qgroups)) {
2162 struct btrfs_qgroup *qgroup;
2163 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2164 struct btrfs_qgroup, dirty);
2165 list_del_init(&qgroup->dirty);
2166 spin_unlock(&fs_info->qgroup_lock);
2167 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2169 fs_info->qgroup_flags |=
2170 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2171 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2173 fs_info->qgroup_flags |=
2174 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2175 spin_lock(&fs_info->qgroup_lock);
2177 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2178 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2180 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2181 spin_unlock(&fs_info->qgroup_lock);
2183 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2185 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2191 * Copy the accounting information between qgroups. This is necessary
2192 * when a snapshot or a subvolume is created. Throwing an error will
2193 * cause a transaction abort so we take extra care here to only error
2194 * when a readonly fs is a reasonable outcome.
2196 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2197 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2198 struct btrfs_qgroup_inherit *inherit)
2203 struct btrfs_root *quota_root = fs_info->quota_root;
2204 struct btrfs_qgroup *srcgroup;
2205 struct btrfs_qgroup *dstgroup;
2209 mutex_lock(&fs_info->qgroup_ioctl_lock);
2210 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2219 i_qgroups = (u64 *)(inherit + 1);
2220 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2221 2 * inherit->num_excl_copies;
2222 for (i = 0; i < nums; ++i) {
2223 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2226 * Zero out invalid groups so we can ignore
2230 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2238 * create a tracking group for the subvol itself
2240 ret = add_qgroup_item(trans, quota_root, objectid);
2245 struct btrfs_root *srcroot;
2246 struct btrfs_key srckey;
2248 srckey.objectid = srcid;
2249 srckey.type = BTRFS_ROOT_ITEM_KEY;
2250 srckey.offset = (u64)-1;
2251 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2252 if (IS_ERR(srcroot)) {
2253 ret = PTR_ERR(srcroot);
2257 level_size = fs_info->nodesize;
2261 * add qgroup to all inherited groups
2264 i_qgroups = (u64 *)(inherit + 1);
2265 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2266 if (*i_qgroups == 0)
2268 ret = add_qgroup_relation_item(trans, quota_root,
2269 objectid, *i_qgroups);
2270 if (ret && ret != -EEXIST)
2272 ret = add_qgroup_relation_item(trans, quota_root,
2273 *i_qgroups, objectid);
2274 if (ret && ret != -EEXIST)
2281 spin_lock(&fs_info->qgroup_lock);
2283 dstgroup = add_qgroup_rb(fs_info, objectid);
2284 if (IS_ERR(dstgroup)) {
2285 ret = PTR_ERR(dstgroup);
2289 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2290 dstgroup->lim_flags = inherit->lim.flags;
2291 dstgroup->max_rfer = inherit->lim.max_rfer;
2292 dstgroup->max_excl = inherit->lim.max_excl;
2293 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2294 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2296 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2298 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2300 "unable to update quota limit for %llu",
2301 dstgroup->qgroupid);
2307 srcgroup = find_qgroup_rb(fs_info, srcid);
2312 * We call inherit after we clone the root in order to make sure
2313 * our counts don't go crazy, so at this point the only
2314 * difference between the two roots should be the root node.
2316 dstgroup->rfer = srcgroup->rfer;
2317 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2318 dstgroup->excl = level_size;
2319 dstgroup->excl_cmpr = level_size;
2320 srcgroup->excl = level_size;
2321 srcgroup->excl_cmpr = level_size;
2323 /* inherit the limit info */
2324 dstgroup->lim_flags = srcgroup->lim_flags;
2325 dstgroup->max_rfer = srcgroup->max_rfer;
2326 dstgroup->max_excl = srcgroup->max_excl;
2327 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2328 dstgroup->rsv_excl = srcgroup->rsv_excl;
2330 qgroup_dirty(fs_info, dstgroup);
2331 qgroup_dirty(fs_info, srcgroup);
2337 i_qgroups = (u64 *)(inherit + 1);
2338 for (i = 0; i < inherit->num_qgroups; ++i) {
2340 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2347 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
2348 struct btrfs_qgroup *src;
2349 struct btrfs_qgroup *dst;
2351 if (!i_qgroups[0] || !i_qgroups[1])
2354 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2355 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2362 dst->rfer = src->rfer - level_size;
2363 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2365 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2366 struct btrfs_qgroup *src;
2367 struct btrfs_qgroup *dst;
2369 if (!i_qgroups[0] || !i_qgroups[1])
2372 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2373 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2380 dst->excl = src->excl + level_size;
2381 dst->excl_cmpr = src->excl_cmpr + level_size;
2385 spin_unlock(&fs_info->qgroup_lock);
2387 mutex_unlock(&fs_info->qgroup_ioctl_lock);
2391 static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
2393 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2394 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
2397 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2398 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
2404 static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2405 enum btrfs_qgroup_rsv_type type)
2407 struct btrfs_root *quota_root;
2408 struct btrfs_qgroup *qgroup;
2409 struct btrfs_fs_info *fs_info = root->fs_info;
2410 u64 ref_root = root->root_key.objectid;
2412 struct ulist_node *unode;
2413 struct ulist_iterator uiter;
2415 if (!is_fstree(ref_root))
2421 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2422 capable(CAP_SYS_RESOURCE))
2425 spin_lock(&fs_info->qgroup_lock);
2426 quota_root = fs_info->quota_root;
2430 qgroup = find_qgroup_rb(fs_info, ref_root);
2435 * in a first step, we check all affected qgroups if any limits would
2438 ulist_reinit(fs_info->qgroup_ulist);
2439 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2440 qgroup_to_aux(qgroup), GFP_ATOMIC);
2443 ULIST_ITER_INIT(&uiter);
2444 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2445 struct btrfs_qgroup *qg;
2446 struct btrfs_qgroup_list *glist;
2448 qg = unode_aux_to_qgroup(unode);
2450 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
2455 list_for_each_entry(glist, &qg->groups, next_group) {
2456 ret = ulist_add(fs_info->qgroup_ulist,
2457 glist->group->qgroupid,
2458 qgroup_to_aux(glist->group), GFP_ATOMIC);
2465 * no limits exceeded, now record the reservation into all qgroups
2467 ULIST_ITER_INIT(&uiter);
2468 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2469 struct btrfs_qgroup *qg;
2471 qg = unode_aux_to_qgroup(unode);
2473 trace_qgroup_update_reserve(fs_info, qg, num_bytes, type);
2474 qgroup_rsv_add(fs_info, qg, num_bytes, type);
2478 spin_unlock(&fs_info->qgroup_lock);
2483 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
2486 * Will handle all higher level qgroup too.
2488 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
2489 * This special case is only used for META_PERTRANS type.
2491 void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2492 u64 ref_root, u64 num_bytes,
2493 enum btrfs_qgroup_rsv_type type)
2495 struct btrfs_root *quota_root;
2496 struct btrfs_qgroup *qgroup;
2497 struct ulist_node *unode;
2498 struct ulist_iterator uiter;
2501 if (!is_fstree(ref_root))
2507 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
2508 WARN(1, "%s: Invalid type to free", __func__);
2511 spin_lock(&fs_info->qgroup_lock);
2513 quota_root = fs_info->quota_root;
2517 qgroup = find_qgroup_rb(fs_info, ref_root);
2521 if (num_bytes == (u64)-1)
2523 * We're freeing all pertrans rsv, get reserved value from
2524 * level 0 qgroup as real num_bytes to free.
2526 num_bytes = qgroup->rsv.values[type];
2528 ulist_reinit(fs_info->qgroup_ulist);
2529 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2530 qgroup_to_aux(qgroup), GFP_ATOMIC);
2533 ULIST_ITER_INIT(&uiter);
2534 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2535 struct btrfs_qgroup *qg;
2536 struct btrfs_qgroup_list *glist;
2538 qg = unode_aux_to_qgroup(unode);
2540 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes, type);
2541 qgroup_rsv_release(fs_info, qg, num_bytes, type);
2543 list_for_each_entry(glist, &qg->groups, next_group) {
2544 ret = ulist_add(fs_info->qgroup_ulist,
2545 glist->group->qgroupid,
2546 qgroup_to_aux(glist->group), GFP_ATOMIC);
2553 spin_unlock(&fs_info->qgroup_lock);
2557 * returns < 0 on error, 0 when more leafs are to be scanned.
2558 * returns 1 when done.
2561 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
2562 struct btrfs_trans_handle *trans)
2564 struct btrfs_key found;
2565 struct extent_buffer *scratch_leaf = NULL;
2566 struct ulist *roots = NULL;
2567 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
2572 mutex_lock(&fs_info->qgroup_rescan_lock);
2573 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2574 &fs_info->qgroup_rescan_progress,
2577 btrfs_debug(fs_info,
2578 "current progress key (%llu %u %llu), search_slot ret %d",
2579 fs_info->qgroup_rescan_progress.objectid,
2580 fs_info->qgroup_rescan_progress.type,
2581 fs_info->qgroup_rescan_progress.offset, ret);
2585 * The rescan is about to end, we will not be scanning any
2586 * further blocks. We cannot unset the RESCAN flag here, because
2587 * we want to commit the transaction if everything went well.
2588 * To make the live accounting work in this phase, we set our
2589 * scan progress pointer such that every real extent objectid
2592 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2593 btrfs_release_path(path);
2594 mutex_unlock(&fs_info->qgroup_rescan_lock);
2598 btrfs_item_key_to_cpu(path->nodes[0], &found,
2599 btrfs_header_nritems(path->nodes[0]) - 1);
2600 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2602 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2603 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2604 if (!scratch_leaf) {
2606 mutex_unlock(&fs_info->qgroup_rescan_lock);
2609 extent_buffer_get(scratch_leaf);
2610 btrfs_tree_read_lock(scratch_leaf);
2611 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
2612 slot = path->slots[0];
2613 btrfs_release_path(path);
2614 mutex_unlock(&fs_info->qgroup_rescan_lock);
2616 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2617 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
2618 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2619 found.type != BTRFS_METADATA_ITEM_KEY)
2621 if (found.type == BTRFS_METADATA_ITEM_KEY)
2622 num_bytes = fs_info->nodesize;
2624 num_bytes = found.offset;
2626 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2630 /* For rescan, just pass old_roots as NULL */
2631 ret = btrfs_qgroup_account_extent(trans, fs_info,
2632 found.objectid, num_bytes, NULL, roots);
2638 btrfs_tree_read_unlock_blocking(scratch_leaf);
2639 free_extent_buffer(scratch_leaf);
2641 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2646 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
2648 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2649 qgroup_rescan_work);
2650 struct btrfs_path *path;
2651 struct btrfs_trans_handle *trans = NULL;
2655 path = btrfs_alloc_path();
2660 while (!err && !btrfs_fs_closing(fs_info)) {
2661 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2662 if (IS_ERR(trans)) {
2663 err = PTR_ERR(trans);
2666 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
2669 err = qgroup_rescan_leaf(fs_info, path, trans);
2672 btrfs_commit_transaction(trans);
2674 btrfs_end_transaction(trans);
2678 btrfs_free_path(path);
2680 mutex_lock(&fs_info->qgroup_rescan_lock);
2681 if (!btrfs_fs_closing(fs_info))
2682 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2685 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2686 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2687 } else if (err < 0) {
2688 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2690 mutex_unlock(&fs_info->qgroup_rescan_lock);
2693 * only update status, since the previous part has already updated the
2696 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2697 if (IS_ERR(trans)) {
2698 err = PTR_ERR(trans);
2700 "fail to start transaction for status update: %d",
2704 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2707 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
2709 btrfs_end_transaction(trans);
2711 if (btrfs_fs_closing(fs_info)) {
2712 btrfs_info(fs_info, "qgroup scan paused");
2713 } else if (err >= 0) {
2714 btrfs_info(fs_info, "qgroup scan completed%s",
2715 err > 0 ? " (inconsistency flag cleared)" : "");
2717 btrfs_err(fs_info, "qgroup scan failed with %d", err);
2721 mutex_lock(&fs_info->qgroup_rescan_lock);
2722 fs_info->qgroup_rescan_running = false;
2723 mutex_unlock(&fs_info->qgroup_rescan_lock);
2724 complete_all(&fs_info->qgroup_rescan_completion);
2728 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2729 * memory required for the rescan context.
2732 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2738 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2739 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2744 mutex_lock(&fs_info->qgroup_rescan_lock);
2745 spin_lock(&fs_info->qgroup_lock);
2748 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2750 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2754 spin_unlock(&fs_info->qgroup_lock);
2755 mutex_unlock(&fs_info->qgroup_rescan_lock);
2758 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2761 memset(&fs_info->qgroup_rescan_progress, 0,
2762 sizeof(fs_info->qgroup_rescan_progress));
2763 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2764 init_completion(&fs_info->qgroup_rescan_completion);
2765 fs_info->qgroup_rescan_running = true;
2767 spin_unlock(&fs_info->qgroup_lock);
2768 mutex_unlock(&fs_info->qgroup_rescan_lock);
2770 memset(&fs_info->qgroup_rescan_work, 0,
2771 sizeof(fs_info->qgroup_rescan_work));
2772 btrfs_init_work(&fs_info->qgroup_rescan_work,
2773 btrfs_qgroup_rescan_helper,
2774 btrfs_qgroup_rescan_worker, NULL, NULL);
2778 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
2786 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2789 struct btrfs_qgroup *qgroup;
2791 spin_lock(&fs_info->qgroup_lock);
2792 /* clear all current qgroup tracking information */
2793 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2794 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2796 qgroup->rfer_cmpr = 0;
2798 qgroup->excl_cmpr = 0;
2800 spin_unlock(&fs_info->qgroup_lock);
2804 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2807 struct btrfs_trans_handle *trans;
2809 ret = qgroup_rescan_init(fs_info, 0, 1);
2814 * We have set the rescan_progress to 0, which means no more
2815 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2816 * However, btrfs_qgroup_account_ref may be right after its call
2817 * to btrfs_find_all_roots, in which case it would still do the
2819 * To solve this, we're committing the transaction, which will
2820 * ensure we run all delayed refs and only after that, we are
2821 * going to clear all tracking information for a clean start.
2824 trans = btrfs_join_transaction(fs_info->fs_root);
2825 if (IS_ERR(trans)) {
2826 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2827 return PTR_ERR(trans);
2829 ret = btrfs_commit_transaction(trans);
2831 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2835 qgroup_rescan_zero_tracking(fs_info);
2837 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2838 &fs_info->qgroup_rescan_work);
2843 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2849 mutex_lock(&fs_info->qgroup_rescan_lock);
2850 spin_lock(&fs_info->qgroup_lock);
2851 running = fs_info->qgroup_rescan_running;
2852 spin_unlock(&fs_info->qgroup_lock);
2853 mutex_unlock(&fs_info->qgroup_rescan_lock);
2859 ret = wait_for_completion_interruptible(
2860 &fs_info->qgroup_rescan_completion);
2862 wait_for_completion(&fs_info->qgroup_rescan_completion);
2868 * this is only called from open_ctree where we're still single threaded, thus
2869 * locking is omitted here.
2872 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2874 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2875 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2876 &fs_info->qgroup_rescan_work);
2880 * Reserve qgroup space for range [start, start + len).
2882 * This function will either reserve space from related qgroups or doing
2883 * nothing if the range is already reserved.
2885 * Return 0 for successful reserve
2886 * Return <0 for error (including -EQUOT)
2888 * NOTE: this function may sleep for memory allocation.
2889 * if btrfs_qgroup_reserve_data() is called multiple times with
2890 * same @reserved, caller must ensure when error happens it's OK
2891 * to free *ALL* reserved space.
2893 int btrfs_qgroup_reserve_data(struct inode *inode,
2894 struct extent_changeset **reserved_ret, u64 start,
2897 struct btrfs_root *root = BTRFS_I(inode)->root;
2898 struct ulist_node *unode;
2899 struct ulist_iterator uiter;
2900 struct extent_changeset *reserved;
2905 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2906 !is_fstree(root->objectid) || len == 0)
2909 /* @reserved parameter is mandatory for qgroup */
2910 if (WARN_ON(!reserved_ret))
2912 if (!*reserved_ret) {
2913 *reserved_ret = extent_changeset_alloc();
2917 reserved = *reserved_ret;
2918 /* Record already reserved space */
2919 orig_reserved = reserved->bytes_changed;
2920 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
2921 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
2923 /* Newly reserved space */
2924 to_reserve = reserved->bytes_changed - orig_reserved;
2925 trace_btrfs_qgroup_reserve_data(inode, start, len,
2926 to_reserve, QGROUP_RESERVE);
2929 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
2936 /* cleanup *ALL* already reserved ranges */
2937 ULIST_ITER_INIT(&uiter);
2938 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
2939 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
2940 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
2941 extent_changeset_release(reserved);
2945 /* Free ranges specified by @reserved, normally in error path */
2946 static int qgroup_free_reserved_data(struct inode *inode,
2947 struct extent_changeset *reserved, u64 start, u64 len)
2949 struct btrfs_root *root = BTRFS_I(inode)->root;
2950 struct ulist_node *unode;
2951 struct ulist_iterator uiter;
2952 struct extent_changeset changeset;
2956 extent_changeset_init(&changeset);
2957 len = round_up(start + len, root->fs_info->sectorsize);
2958 start = round_down(start, root->fs_info->sectorsize);
2960 ULIST_ITER_INIT(&uiter);
2961 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
2962 u64 range_start = unode->val;
2963 /* unode->aux is the inclusive end */
2964 u64 range_len = unode->aux - range_start + 1;
2968 extent_changeset_release(&changeset);
2970 /* Only free range in range [start, start + len) */
2971 if (range_start >= start + len ||
2972 range_start + range_len <= start)
2974 free_start = max(range_start, start);
2975 free_len = min(start + len, range_start + range_len) -
2978 * TODO: To also modify reserved->ranges_reserved to reflect
2981 * However as long as we free qgroup reserved according to
2982 * EXTENT_QGROUP_RESERVED, we won't double free.
2983 * So not need to rush.
2985 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
2986 free_start, free_start + free_len - 1,
2987 EXTENT_QGROUP_RESERVED, &changeset);
2990 freed += changeset.bytes_changed;
2992 btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed,
2993 BTRFS_QGROUP_RSV_DATA);
2996 extent_changeset_release(&changeset);
3000 static int __btrfs_qgroup_release_data(struct inode *inode,
3001 struct extent_changeset *reserved, u64 start, u64 len,
3004 struct extent_changeset changeset;
3005 int trace_op = QGROUP_RELEASE;
3008 /* In release case, we shouldn't have @reserved */
3009 WARN_ON(!free && reserved);
3010 if (free && reserved)
3011 return qgroup_free_reserved_data(inode, reserved, start, len);
3012 extent_changeset_init(&changeset);
3013 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
3014 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
3019 trace_op = QGROUP_FREE;
3020 trace_btrfs_qgroup_release_data(inode, start, len,
3021 changeset.bytes_changed, trace_op);
3023 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3024 BTRFS_I(inode)->root->objectid,
3025 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3026 ret = changeset.bytes_changed;
3028 extent_changeset_release(&changeset);
3033 * Free a reserved space range from io_tree and related qgroups
3035 * Should be called when a range of pages get invalidated before reaching disk.
3036 * Or for error cleanup case.
3037 * if @reserved is given, only reserved range in [@start, @start + @len) will
3040 * For data written to disk, use btrfs_qgroup_release_data().
3042 * NOTE: This function may sleep for memory allocation.
3044 int btrfs_qgroup_free_data(struct inode *inode,
3045 struct extent_changeset *reserved, u64 start, u64 len)
3047 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
3051 * Release a reserved space range from io_tree only.
3053 * Should be called when a range of pages get written to disk and corresponding
3054 * FILE_EXTENT is inserted into corresponding root.
3056 * Since new qgroup accounting framework will only update qgroup numbers at
3057 * commit_transaction() time, its reserved space shouldn't be freed from
3060 * But we should release the range from io_tree, to allow further write to be
3063 * NOTE: This function may sleep for memory allocation.
3065 int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3067 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
3070 static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3071 enum btrfs_qgroup_rsv_type type)
3073 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3074 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3079 spin_lock(&root->qgroup_meta_rsv_lock);
3080 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3081 root->qgroup_meta_rsv_prealloc += num_bytes;
3083 root->qgroup_meta_rsv_pertrans += num_bytes;
3084 spin_unlock(&root->qgroup_meta_rsv_lock);
3087 static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3088 enum btrfs_qgroup_rsv_type type)
3090 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3091 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3096 spin_lock(&root->qgroup_meta_rsv_lock);
3097 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3098 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3100 root->qgroup_meta_rsv_prealloc -= num_bytes;
3102 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3104 root->qgroup_meta_rsv_pertrans -= num_bytes;
3106 spin_unlock(&root->qgroup_meta_rsv_lock);
3110 int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3111 enum btrfs_qgroup_rsv_type type, bool enforce)
3113 struct btrfs_fs_info *fs_info = root->fs_info;
3116 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3117 !is_fstree(root->objectid) || num_bytes == 0)
3120 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3121 trace_qgroup_meta_reserve(root, type, (s64)num_bytes);
3122 ret = qgroup_reserve(root, num_bytes, enforce, type);
3126 * Record what we have reserved into root.
3128 * To avoid quota disabled->enabled underflow.
3129 * In that case, we may try to free space we haven't reserved
3130 * (since quota was disabled), so record what we reserved into root.
3131 * And ensure later release won't underflow this number.
3133 add_root_meta_rsv(root, num_bytes, type);
3137 void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
3139 struct btrfs_fs_info *fs_info = root->fs_info;
3141 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3142 !is_fstree(root->objectid))
3145 /* TODO: Update trace point to handle such free */
3146 trace_qgroup_meta_free_all_pertrans(root);
3147 /* Special value -1 means to free all reserved space */
3148 btrfs_qgroup_free_refroot(fs_info, root->objectid, (u64)-1,
3149 BTRFS_QGROUP_RSV_META_PERTRANS);
3152 void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3153 enum btrfs_qgroup_rsv_type type)
3155 struct btrfs_fs_info *fs_info = root->fs_info;
3157 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3158 !is_fstree(root->objectid))
3162 * reservation for META_PREALLOC can happen before quota is enabled,
3163 * which can lead to underflow.
3164 * Here ensure we will only free what we really have reserved.
3166 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
3167 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3168 trace_qgroup_meta_reserve(root, type, -(s64)num_bytes);
3169 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes, type);
3172 static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3175 struct btrfs_root *quota_root = fs_info->quota_root;
3176 struct btrfs_qgroup *qgroup;
3177 struct ulist_node *unode;
3178 struct ulist_iterator uiter;
3186 spin_lock(&fs_info->qgroup_lock);
3187 qgroup = find_qgroup_rb(fs_info, ref_root);
3190 ulist_reinit(fs_info->qgroup_ulist);
3191 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3192 qgroup_to_aux(qgroup), GFP_ATOMIC);
3195 ULIST_ITER_INIT(&uiter);
3196 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3197 struct btrfs_qgroup *qg;
3198 struct btrfs_qgroup_list *glist;
3200 qg = unode_aux_to_qgroup(unode);
3202 qgroup_rsv_release(fs_info, qg, num_bytes,
3203 BTRFS_QGROUP_RSV_META_PREALLOC);
3204 qgroup_rsv_add(fs_info, qg, num_bytes,
3205 BTRFS_QGROUP_RSV_META_PERTRANS);
3206 list_for_each_entry(glist, &qg->groups, next_group) {
3207 ret = ulist_add(fs_info->qgroup_ulist,
3208 glist->group->qgroupid,
3209 qgroup_to_aux(glist->group), GFP_ATOMIC);
3215 spin_unlock(&fs_info->qgroup_lock);
3218 void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
3220 struct btrfs_fs_info *fs_info = root->fs_info;
3222 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3223 !is_fstree(root->objectid))
3225 /* Same as btrfs_qgroup_free_meta_prealloc() */
3226 num_bytes = sub_root_meta_rsv(root, num_bytes,
3227 BTRFS_QGROUP_RSV_META_PREALLOC);
3228 trace_qgroup_meta_convert(root, num_bytes);
3229 qgroup_convert_meta(fs_info, root->objectid, num_bytes);
3233 * Check qgroup reserved space leaking, normally at destroy inode
3236 void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3238 struct extent_changeset changeset;
3239 struct ulist_node *unode;
3240 struct ulist_iterator iter;
3243 extent_changeset_init(&changeset);
3244 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
3245 EXTENT_QGROUP_RESERVED, &changeset);
3248 if (WARN_ON(changeset.bytes_changed)) {
3249 ULIST_ITER_INIT(&iter);
3250 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
3251 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3252 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3253 inode->i_ino, unode->val, unode->aux);
3255 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3256 BTRFS_I(inode)->root->objectid,
3257 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3260 extent_changeset_release(&changeset);