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)
78 static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
79 struct btrfs_qgroup *qgroup, u64 num_bytes,
80 enum btrfs_qgroup_rsv_type type)
82 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
83 qgroup->rsv.values[type] += num_bytes;
86 static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
87 struct btrfs_qgroup *qgroup, u64 num_bytes,
88 enum btrfs_qgroup_rsv_type type)
90 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
91 if (qgroup->rsv.values[type] >= num_bytes) {
92 qgroup->rsv.values[type] -= num_bytes;
95 #ifdef CONFIG_BTRFS_DEBUG
97 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
98 qgroup->qgroupid, qgroup_rsv_type_str(type),
99 qgroup->rsv.values[type], num_bytes);
101 qgroup->rsv.values[type] = 0;
104 static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
105 struct btrfs_qgroup *dest,
106 struct btrfs_qgroup *src)
110 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
111 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
114 static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
115 struct btrfs_qgroup *dest,
116 struct btrfs_qgroup *src)
120 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
121 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
124 static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
127 if (qg->old_refcnt < seq)
128 qg->old_refcnt = seq;
129 qg->old_refcnt += mod;
132 static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
135 if (qg->new_refcnt < seq)
136 qg->new_refcnt = seq;
137 qg->new_refcnt += mod;
140 static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
142 if (qg->old_refcnt < seq)
144 return qg->old_refcnt - seq;
147 static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
149 if (qg->new_refcnt < seq)
151 return qg->new_refcnt - seq;
155 * glue structure to represent the relations between qgroups.
157 struct btrfs_qgroup_list {
158 struct list_head next_group;
159 struct list_head next_member;
160 struct btrfs_qgroup *group;
161 struct btrfs_qgroup *member;
164 static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
166 return (u64)(uintptr_t)qg;
169 static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
171 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
175 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
177 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
179 /* must be called with qgroup_ioctl_lock held */
180 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
183 struct rb_node *n = fs_info->qgroup_tree.rb_node;
184 struct btrfs_qgroup *qgroup;
187 qgroup = rb_entry(n, struct btrfs_qgroup, node);
188 if (qgroup->qgroupid < qgroupid)
190 else if (qgroup->qgroupid > qgroupid)
198 /* must be called with qgroup_lock held */
199 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
202 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
203 struct rb_node *parent = NULL;
204 struct btrfs_qgroup *qgroup;
208 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
210 if (qgroup->qgroupid < qgroupid)
212 else if (qgroup->qgroupid > qgroupid)
218 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
220 return ERR_PTR(-ENOMEM);
222 qgroup->qgroupid = qgroupid;
223 INIT_LIST_HEAD(&qgroup->groups);
224 INIT_LIST_HEAD(&qgroup->members);
225 INIT_LIST_HEAD(&qgroup->dirty);
227 rb_link_node(&qgroup->node, parent, p);
228 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
233 static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
235 struct btrfs_qgroup_list *list;
237 list_del(&qgroup->dirty);
238 while (!list_empty(&qgroup->groups)) {
239 list = list_first_entry(&qgroup->groups,
240 struct btrfs_qgroup_list, next_group);
241 list_del(&list->next_group);
242 list_del(&list->next_member);
246 while (!list_empty(&qgroup->members)) {
247 list = list_first_entry(&qgroup->members,
248 struct btrfs_qgroup_list, next_member);
249 list_del(&list->next_group);
250 list_del(&list->next_member);
256 /* must be called with qgroup_lock held */
257 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
259 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
264 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
265 __del_qgroup_rb(qgroup);
269 /* must be called with qgroup_lock held */
270 static int add_relation_rb(struct btrfs_fs_info *fs_info,
271 u64 memberid, u64 parentid)
273 struct btrfs_qgroup *member;
274 struct btrfs_qgroup *parent;
275 struct btrfs_qgroup_list *list;
277 member = find_qgroup_rb(fs_info, memberid);
278 parent = find_qgroup_rb(fs_info, parentid);
279 if (!member || !parent)
282 list = kzalloc(sizeof(*list), GFP_ATOMIC);
286 list->group = parent;
287 list->member = member;
288 list_add_tail(&list->next_group, &member->groups);
289 list_add_tail(&list->next_member, &parent->members);
294 /* must be called with qgroup_lock held */
295 static int del_relation_rb(struct btrfs_fs_info *fs_info,
296 u64 memberid, u64 parentid)
298 struct btrfs_qgroup *member;
299 struct btrfs_qgroup *parent;
300 struct btrfs_qgroup_list *list;
302 member = find_qgroup_rb(fs_info, memberid);
303 parent = find_qgroup_rb(fs_info, parentid);
304 if (!member || !parent)
307 list_for_each_entry(list, &member->groups, next_group) {
308 if (list->group == parent) {
309 list_del(&list->next_group);
310 list_del(&list->next_member);
318 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
319 int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
322 struct btrfs_qgroup *qgroup;
324 qgroup = find_qgroup_rb(fs_info, qgroupid);
327 if (qgroup->rfer != rfer || qgroup->excl != excl)
334 * The full config is read in one go, only called from open_ctree()
335 * It doesn't use any locking, as at this point we're still single-threaded
337 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
339 struct btrfs_key key;
340 struct btrfs_key found_key;
341 struct btrfs_root *quota_root = fs_info->quota_root;
342 struct btrfs_path *path = NULL;
343 struct extent_buffer *l;
347 u64 rescan_progress = 0;
349 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
352 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
353 if (!fs_info->qgroup_ulist) {
358 path = btrfs_alloc_path();
364 /* default this to quota off, in case no status key is found */
365 fs_info->qgroup_flags = 0;
368 * pass 1: read status, all qgroup infos and limits
373 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
378 struct btrfs_qgroup *qgroup;
380 slot = path->slots[0];
382 btrfs_item_key_to_cpu(l, &found_key, slot);
384 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
385 struct btrfs_qgroup_status_item *ptr;
387 ptr = btrfs_item_ptr(l, slot,
388 struct btrfs_qgroup_status_item);
390 if (btrfs_qgroup_status_version(l, ptr) !=
391 BTRFS_QGROUP_STATUS_VERSION) {
393 "old qgroup version, quota disabled");
396 if (btrfs_qgroup_status_generation(l, ptr) !=
397 fs_info->generation) {
398 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
400 "qgroup generation mismatch, marked as inconsistent");
402 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
404 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
408 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
409 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
412 qgroup = find_qgroup_rb(fs_info, found_key.offset);
413 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
414 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
415 btrfs_err(fs_info, "inconsistent qgroup config");
416 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
419 qgroup = add_qgroup_rb(fs_info, found_key.offset);
420 if (IS_ERR(qgroup)) {
421 ret = PTR_ERR(qgroup);
425 switch (found_key.type) {
426 case BTRFS_QGROUP_INFO_KEY: {
427 struct btrfs_qgroup_info_item *ptr;
429 ptr = btrfs_item_ptr(l, slot,
430 struct btrfs_qgroup_info_item);
431 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
432 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
433 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
434 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
435 /* generation currently unused */
438 case BTRFS_QGROUP_LIMIT_KEY: {
439 struct btrfs_qgroup_limit_item *ptr;
441 ptr = btrfs_item_ptr(l, slot,
442 struct btrfs_qgroup_limit_item);
443 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
444 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
445 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
446 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
447 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
452 ret = btrfs_next_item(quota_root, path);
458 btrfs_release_path(path);
461 * pass 2: read all qgroup relations
464 key.type = BTRFS_QGROUP_RELATION_KEY;
466 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
470 slot = path->slots[0];
472 btrfs_item_key_to_cpu(l, &found_key, slot);
474 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
477 if (found_key.objectid > found_key.offset) {
478 /* parent <- member, not needed to build config */
479 /* FIXME should we omit the key completely? */
483 ret = add_relation_rb(fs_info, found_key.objectid,
485 if (ret == -ENOENT) {
487 "orphan qgroup relation 0x%llx->0x%llx",
488 found_key.objectid, found_key.offset);
489 ret = 0; /* ignore the error */
494 ret = btrfs_next_item(quota_root, path);
501 fs_info->qgroup_flags |= flags;
502 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
503 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
504 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
506 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
507 btrfs_free_path(path);
510 ulist_free(fs_info->qgroup_ulist);
511 fs_info->qgroup_ulist = NULL;
512 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
515 return ret < 0 ? ret : 0;
519 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
520 * first two are in single-threaded paths.And for the third one, we have set
521 * quota_root to be null with qgroup_lock held before, so it is safe to clean
522 * up the in-memory structures without qgroup_lock held.
524 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
527 struct btrfs_qgroup *qgroup;
529 while ((n = rb_first(&fs_info->qgroup_tree))) {
530 qgroup = rb_entry(n, struct btrfs_qgroup, node);
531 rb_erase(n, &fs_info->qgroup_tree);
532 __del_qgroup_rb(qgroup);
535 * we call btrfs_free_qgroup_config() when umounting
536 * filesystem and disabling quota, so we set qgroup_ulist
537 * to be null here to avoid double free.
539 ulist_free(fs_info->qgroup_ulist);
540 fs_info->qgroup_ulist = NULL;
543 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
544 struct btrfs_root *quota_root,
548 struct btrfs_path *path;
549 struct btrfs_key key;
551 path = btrfs_alloc_path();
556 key.type = BTRFS_QGROUP_RELATION_KEY;
559 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
561 btrfs_mark_buffer_dirty(path->nodes[0]);
563 btrfs_free_path(path);
567 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
568 struct btrfs_root *quota_root,
572 struct btrfs_path *path;
573 struct btrfs_key key;
575 path = btrfs_alloc_path();
580 key.type = BTRFS_QGROUP_RELATION_KEY;
583 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
592 ret = btrfs_del_item(trans, quota_root, path);
594 btrfs_free_path(path);
598 static int add_qgroup_item(struct btrfs_trans_handle *trans,
599 struct btrfs_root *quota_root, u64 qgroupid)
602 struct btrfs_path *path;
603 struct btrfs_qgroup_info_item *qgroup_info;
604 struct btrfs_qgroup_limit_item *qgroup_limit;
605 struct extent_buffer *leaf;
606 struct btrfs_key key;
608 if (btrfs_is_testing(quota_root->fs_info))
611 path = btrfs_alloc_path();
616 key.type = BTRFS_QGROUP_INFO_KEY;
617 key.offset = qgroupid;
620 * Avoid a transaction abort by catching -EEXIST here. In that
621 * case, we proceed by re-initializing the existing structure
625 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
626 sizeof(*qgroup_info));
627 if (ret && ret != -EEXIST)
630 leaf = path->nodes[0];
631 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
632 struct btrfs_qgroup_info_item);
633 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
634 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
635 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
636 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
637 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
639 btrfs_mark_buffer_dirty(leaf);
641 btrfs_release_path(path);
643 key.type = BTRFS_QGROUP_LIMIT_KEY;
644 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
645 sizeof(*qgroup_limit));
646 if (ret && ret != -EEXIST)
649 leaf = path->nodes[0];
650 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
651 struct btrfs_qgroup_limit_item);
652 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
653 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
654 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
655 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
656 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
658 btrfs_mark_buffer_dirty(leaf);
662 btrfs_free_path(path);
666 static int del_qgroup_item(struct btrfs_trans_handle *trans,
667 struct btrfs_root *quota_root, u64 qgroupid)
670 struct btrfs_path *path;
671 struct btrfs_key key;
673 path = btrfs_alloc_path();
678 key.type = BTRFS_QGROUP_INFO_KEY;
679 key.offset = qgroupid;
680 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
689 ret = btrfs_del_item(trans, quota_root, path);
693 btrfs_release_path(path);
695 key.type = BTRFS_QGROUP_LIMIT_KEY;
696 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
705 ret = btrfs_del_item(trans, quota_root, path);
708 btrfs_free_path(path);
712 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
713 struct btrfs_root *root,
714 struct btrfs_qgroup *qgroup)
716 struct btrfs_path *path;
717 struct btrfs_key key;
718 struct extent_buffer *l;
719 struct btrfs_qgroup_limit_item *qgroup_limit;
724 key.type = BTRFS_QGROUP_LIMIT_KEY;
725 key.offset = qgroup->qgroupid;
727 path = btrfs_alloc_path();
731 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
739 slot = path->slots[0];
740 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
741 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
742 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
743 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
744 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
745 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
747 btrfs_mark_buffer_dirty(l);
750 btrfs_free_path(path);
754 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
755 struct btrfs_root *root,
756 struct btrfs_qgroup *qgroup)
758 struct btrfs_path *path;
759 struct btrfs_key key;
760 struct extent_buffer *l;
761 struct btrfs_qgroup_info_item *qgroup_info;
765 if (btrfs_is_testing(root->fs_info))
769 key.type = BTRFS_QGROUP_INFO_KEY;
770 key.offset = qgroup->qgroupid;
772 path = btrfs_alloc_path();
776 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
784 slot = path->slots[0];
785 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
786 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
787 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
788 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
789 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
790 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
792 btrfs_mark_buffer_dirty(l);
795 btrfs_free_path(path);
799 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
800 struct btrfs_fs_info *fs_info,
801 struct btrfs_root *root)
803 struct btrfs_path *path;
804 struct btrfs_key key;
805 struct extent_buffer *l;
806 struct btrfs_qgroup_status_item *ptr;
811 key.type = BTRFS_QGROUP_STATUS_KEY;
814 path = btrfs_alloc_path();
818 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
826 slot = path->slots[0];
827 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
828 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
829 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
830 btrfs_set_qgroup_status_rescan(l, ptr,
831 fs_info->qgroup_rescan_progress.objectid);
833 btrfs_mark_buffer_dirty(l);
836 btrfs_free_path(path);
841 * called with qgroup_lock held
843 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
844 struct btrfs_root *root)
846 struct btrfs_path *path;
847 struct btrfs_key key;
848 struct extent_buffer *leaf = NULL;
852 path = btrfs_alloc_path();
856 path->leave_spinning = 1;
863 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
866 leaf = path->nodes[0];
867 nr = btrfs_header_nritems(leaf);
871 * delete the leaf one by one
872 * since the whole tree is going
876 ret = btrfs_del_items(trans, root, path, 0, nr);
880 btrfs_release_path(path);
884 btrfs_free_path(path);
888 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
889 struct btrfs_fs_info *fs_info)
891 struct btrfs_root *quota_root;
892 struct btrfs_root *tree_root = fs_info->tree_root;
893 struct btrfs_path *path = NULL;
894 struct btrfs_qgroup_status_item *ptr;
895 struct extent_buffer *leaf;
896 struct btrfs_key key;
897 struct btrfs_key found_key;
898 struct btrfs_qgroup *qgroup = NULL;
902 mutex_lock(&fs_info->qgroup_ioctl_lock);
903 if (fs_info->quota_root)
906 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
907 if (!fs_info->qgroup_ulist) {
913 * initially create the quota tree
915 quota_root = btrfs_create_tree(trans, fs_info,
916 BTRFS_QUOTA_TREE_OBJECTID);
917 if (IS_ERR(quota_root)) {
918 ret = PTR_ERR(quota_root);
922 path = btrfs_alloc_path();
929 key.type = BTRFS_QGROUP_STATUS_KEY;
932 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
937 leaf = path->nodes[0];
938 ptr = btrfs_item_ptr(leaf, path->slots[0],
939 struct btrfs_qgroup_status_item);
940 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
941 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
942 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
943 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
944 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
945 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
947 btrfs_mark_buffer_dirty(leaf);
950 key.type = BTRFS_ROOT_REF_KEY;
953 btrfs_release_path(path);
954 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
962 slot = path->slots[0];
963 leaf = path->nodes[0];
964 btrfs_item_key_to_cpu(leaf, &found_key, slot);
966 if (found_key.type == BTRFS_ROOT_REF_KEY) {
967 ret = add_qgroup_item(trans, quota_root,
972 qgroup = add_qgroup_rb(fs_info, found_key.offset);
973 if (IS_ERR(qgroup)) {
974 ret = PTR_ERR(qgroup);
978 ret = btrfs_next_item(tree_root, path);
986 btrfs_release_path(path);
987 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
991 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
992 if (IS_ERR(qgroup)) {
993 ret = PTR_ERR(qgroup);
996 spin_lock(&fs_info->qgroup_lock);
997 fs_info->quota_root = quota_root;
998 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
999 spin_unlock(&fs_info->qgroup_lock);
1000 ret = qgroup_rescan_init(fs_info, 0, 1);
1002 qgroup_rescan_zero_tracking(fs_info);
1003 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1004 &fs_info->qgroup_rescan_work);
1008 btrfs_free_path(path);
1011 free_extent_buffer(quota_root->node);
1012 free_extent_buffer(quota_root->commit_root);
1017 ulist_free(fs_info->qgroup_ulist);
1018 fs_info->qgroup_ulist = NULL;
1020 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1024 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
1025 struct btrfs_fs_info *fs_info)
1027 struct btrfs_root *quota_root;
1030 mutex_lock(&fs_info->qgroup_ioctl_lock);
1031 if (!fs_info->quota_root)
1033 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1034 btrfs_qgroup_wait_for_completion(fs_info, false);
1035 spin_lock(&fs_info->qgroup_lock);
1036 quota_root = fs_info->quota_root;
1037 fs_info->quota_root = NULL;
1038 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1039 spin_unlock(&fs_info->qgroup_lock);
1041 btrfs_free_qgroup_config(fs_info);
1043 ret = btrfs_clean_quota_tree(trans, quota_root);
1047 ret = btrfs_del_root(trans, fs_info, "a_root->root_key);
1051 list_del("a_root->dirty_list);
1053 btrfs_tree_lock(quota_root->node);
1054 clean_tree_block(fs_info, quota_root->node);
1055 btrfs_tree_unlock(quota_root->node);
1056 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1058 free_extent_buffer(quota_root->node);
1059 free_extent_buffer(quota_root->commit_root);
1062 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1066 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1067 struct btrfs_qgroup *qgroup)
1069 if (list_empty(&qgroup->dirty))
1070 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1073 static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
1074 struct btrfs_qgroup *qgroup,
1077 #ifdef CONFIG_BTRFS_DEBUG
1078 WARN_ON(qgroup->reserved < num_bytes);
1079 btrfs_debug(fs_info,
1080 "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
1081 qgroup->qgroupid, qgroup->reserved, num_bytes);
1083 qgroup->reserved = 0;
1087 * The easy accounting, we're updating qgroup relationship whose child qgroup
1088 * only has exclusive extents.
1090 * In this case, all exclsuive extents will also be exlusive for parent, so
1091 * excl/rfer just get added/removed.
1093 * So is qgroup reservation space, which should also be added/removed to
1095 * Or when child tries to release reservation space, parent will underflow its
1096 * reservation (for relationship adding case).
1098 * Caller should hold fs_info->qgroup_lock.
1100 static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1101 struct ulist *tmp, u64 ref_root,
1102 struct btrfs_qgroup *src, int sign)
1104 struct btrfs_qgroup *qgroup;
1105 struct btrfs_qgroup_list *glist;
1106 struct ulist_node *unode;
1107 struct ulist_iterator uiter;
1108 u64 num_bytes = src->excl;
1111 qgroup = find_qgroup_rb(fs_info, ref_root);
1115 qgroup->rfer += sign * num_bytes;
1116 qgroup->rfer_cmpr += sign * num_bytes;
1118 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1119 qgroup->excl += sign * num_bytes;
1120 qgroup->excl_cmpr += sign * num_bytes;
1123 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1125 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1127 qgroup_dirty(fs_info, qgroup);
1129 /* Get all of the parent groups that contain this qgroup */
1130 list_for_each_entry(glist, &qgroup->groups, next_group) {
1131 ret = ulist_add(tmp, glist->group->qgroupid,
1132 qgroup_to_aux(glist->group), GFP_ATOMIC);
1137 /* Iterate all of the parents and adjust their reference counts */
1138 ULIST_ITER_INIT(&uiter);
1139 while ((unode = ulist_next(tmp, &uiter))) {
1140 qgroup = unode_aux_to_qgroup(unode);
1141 qgroup->rfer += sign * num_bytes;
1142 qgroup->rfer_cmpr += sign * num_bytes;
1143 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1144 qgroup->excl += sign * num_bytes;
1146 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1148 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1149 qgroup->excl_cmpr += sign * num_bytes;
1150 qgroup_dirty(fs_info, qgroup);
1152 /* Add any parents of the parents */
1153 list_for_each_entry(glist, &qgroup->groups, next_group) {
1154 ret = ulist_add(tmp, glist->group->qgroupid,
1155 qgroup_to_aux(glist->group), GFP_ATOMIC);
1167 * Quick path for updating qgroup with only excl refs.
1169 * In that case, just update all parent will be enough.
1170 * Or we needs to do a full rescan.
1171 * Caller should also hold fs_info->qgroup_lock.
1173 * Return 0 for quick update, return >0 for need to full rescan
1174 * and mark INCONSISTENT flag.
1175 * Return < 0 for other error.
1177 static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1178 struct ulist *tmp, u64 src, u64 dst,
1181 struct btrfs_qgroup *qgroup;
1185 qgroup = find_qgroup_rb(fs_info, src);
1188 if (qgroup->excl == qgroup->rfer) {
1190 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1199 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1203 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1204 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1206 struct btrfs_root *quota_root;
1207 struct btrfs_qgroup *parent;
1208 struct btrfs_qgroup *member;
1209 struct btrfs_qgroup_list *list;
1213 /* Check the level of src and dst first */
1214 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1217 tmp = ulist_alloc(GFP_KERNEL);
1221 mutex_lock(&fs_info->qgroup_ioctl_lock);
1222 quota_root = fs_info->quota_root;
1227 member = find_qgroup_rb(fs_info, src);
1228 parent = find_qgroup_rb(fs_info, dst);
1229 if (!member || !parent) {
1234 /* check if such qgroup relation exist firstly */
1235 list_for_each_entry(list, &member->groups, next_group) {
1236 if (list->group == parent) {
1242 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1246 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1248 del_qgroup_relation_item(trans, quota_root, src, dst);
1252 spin_lock(&fs_info->qgroup_lock);
1253 ret = add_relation_rb(fs_info, src, dst);
1255 spin_unlock(&fs_info->qgroup_lock);
1258 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1259 spin_unlock(&fs_info->qgroup_lock);
1261 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1266 static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
1267 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1269 struct btrfs_root *quota_root;
1270 struct btrfs_qgroup *parent;
1271 struct btrfs_qgroup *member;
1272 struct btrfs_qgroup_list *list;
1277 tmp = ulist_alloc(GFP_KERNEL);
1281 quota_root = fs_info->quota_root;
1287 member = find_qgroup_rb(fs_info, src);
1288 parent = find_qgroup_rb(fs_info, dst);
1289 if (!member || !parent) {
1294 /* check if such qgroup relation exist firstly */
1295 list_for_each_entry(list, &member->groups, next_group) {
1296 if (list->group == parent)
1302 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1303 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1307 spin_lock(&fs_info->qgroup_lock);
1308 del_relation_rb(fs_info, src, dst);
1309 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1310 spin_unlock(&fs_info->qgroup_lock);
1316 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1317 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1321 mutex_lock(&fs_info->qgroup_ioctl_lock);
1322 ret = __del_qgroup_relation(trans, fs_info, src, dst);
1323 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1328 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1329 struct btrfs_fs_info *fs_info, u64 qgroupid)
1331 struct btrfs_root *quota_root;
1332 struct btrfs_qgroup *qgroup;
1335 mutex_lock(&fs_info->qgroup_ioctl_lock);
1336 quota_root = fs_info->quota_root;
1341 qgroup = find_qgroup_rb(fs_info, qgroupid);
1347 ret = add_qgroup_item(trans, quota_root, qgroupid);
1351 spin_lock(&fs_info->qgroup_lock);
1352 qgroup = add_qgroup_rb(fs_info, qgroupid);
1353 spin_unlock(&fs_info->qgroup_lock);
1356 ret = PTR_ERR(qgroup);
1358 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1362 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1363 struct btrfs_fs_info *fs_info, u64 qgroupid)
1365 struct btrfs_root *quota_root;
1366 struct btrfs_qgroup *qgroup;
1367 struct btrfs_qgroup_list *list;
1370 mutex_lock(&fs_info->qgroup_ioctl_lock);
1371 quota_root = fs_info->quota_root;
1377 qgroup = find_qgroup_rb(fs_info, qgroupid);
1382 /* check if there are no children of this qgroup */
1383 if (!list_empty(&qgroup->members)) {
1388 ret = del_qgroup_item(trans, quota_root, qgroupid);
1389 if (ret && ret != -ENOENT)
1392 while (!list_empty(&qgroup->groups)) {
1393 list = list_first_entry(&qgroup->groups,
1394 struct btrfs_qgroup_list, next_group);
1395 ret = __del_qgroup_relation(trans, fs_info,
1397 list->group->qgroupid);
1402 spin_lock(&fs_info->qgroup_lock);
1403 del_qgroup_rb(fs_info, qgroupid);
1404 spin_unlock(&fs_info->qgroup_lock);
1406 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1410 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1411 struct btrfs_fs_info *fs_info, u64 qgroupid,
1412 struct btrfs_qgroup_limit *limit)
1414 struct btrfs_root *quota_root;
1415 struct btrfs_qgroup *qgroup;
1417 /* Sometimes we would want to clear the limit on this qgroup.
1418 * To meet this requirement, we treat the -1 as a special value
1419 * which tell kernel to clear the limit on this qgroup.
1421 const u64 CLEAR_VALUE = -1;
1423 mutex_lock(&fs_info->qgroup_ioctl_lock);
1424 quota_root = fs_info->quota_root;
1430 qgroup = find_qgroup_rb(fs_info, qgroupid);
1436 spin_lock(&fs_info->qgroup_lock);
1437 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1438 if (limit->max_rfer == CLEAR_VALUE) {
1439 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1440 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1441 qgroup->max_rfer = 0;
1443 qgroup->max_rfer = limit->max_rfer;
1446 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1447 if (limit->max_excl == CLEAR_VALUE) {
1448 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1449 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1450 qgroup->max_excl = 0;
1452 qgroup->max_excl = limit->max_excl;
1455 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1456 if (limit->rsv_rfer == CLEAR_VALUE) {
1457 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1458 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1459 qgroup->rsv_rfer = 0;
1461 qgroup->rsv_rfer = limit->rsv_rfer;
1464 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1465 if (limit->rsv_excl == CLEAR_VALUE) {
1466 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1467 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1468 qgroup->rsv_excl = 0;
1470 qgroup->rsv_excl = limit->rsv_excl;
1473 qgroup->lim_flags |= limit->flags;
1475 spin_unlock(&fs_info->qgroup_lock);
1477 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1479 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1480 btrfs_info(fs_info, "unable to update quota limit for %llu",
1485 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1489 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1490 struct btrfs_delayed_ref_root *delayed_refs,
1491 struct btrfs_qgroup_extent_record *record)
1493 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1494 struct rb_node *parent_node = NULL;
1495 struct btrfs_qgroup_extent_record *entry;
1496 u64 bytenr = record->bytenr;
1498 assert_spin_locked(&delayed_refs->lock);
1499 trace_btrfs_qgroup_trace_extent(fs_info, record);
1503 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1505 if (bytenr < entry->bytenr)
1507 else if (bytenr > entry->bytenr)
1508 p = &(*p)->rb_right;
1513 rb_link_node(&record->node, parent_node, p);
1514 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1518 int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1519 struct btrfs_qgroup_extent_record *qrecord)
1521 struct ulist *old_root;
1522 u64 bytenr = qrecord->bytenr;
1525 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
1527 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1529 "error accounting new delayed refs extent (err code: %d), quota inconsistent",
1535 * Here we don't need to get the lock of
1536 * trans->transaction->delayed_refs, since inserted qrecord won't
1537 * be deleted, only qrecord->node may be modified (new qrecord insert)
1539 * So modifying qrecord->old_roots is safe here
1541 qrecord->old_roots = old_root;
1545 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
1546 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1549 struct btrfs_qgroup_extent_record *record;
1550 struct btrfs_delayed_ref_root *delayed_refs;
1553 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1554 || bytenr == 0 || num_bytes == 0)
1556 if (WARN_ON(trans == NULL))
1558 record = kmalloc(sizeof(*record), gfp_flag);
1562 delayed_refs = &trans->transaction->delayed_refs;
1563 record->bytenr = bytenr;
1564 record->num_bytes = num_bytes;
1565 record->old_roots = NULL;
1567 spin_lock(&delayed_refs->lock);
1568 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1569 spin_unlock(&delayed_refs->lock);
1574 return btrfs_qgroup_trace_extent_post(fs_info, record);
1577 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1578 struct btrfs_fs_info *fs_info,
1579 struct extent_buffer *eb)
1581 int nr = btrfs_header_nritems(eb);
1582 int i, extent_type, ret;
1583 struct btrfs_key key;
1584 struct btrfs_file_extent_item *fi;
1585 u64 bytenr, num_bytes;
1587 /* We can be called directly from walk_up_proc() */
1588 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1591 for (i = 0; i < nr; i++) {
1592 btrfs_item_key_to_cpu(eb, &key, i);
1594 if (key.type != BTRFS_EXTENT_DATA_KEY)
1597 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1598 /* filter out non qgroup-accountable extents */
1599 extent_type = btrfs_file_extent_type(eb, fi);
1601 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1604 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1608 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1610 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1611 num_bytes, GFP_NOFS);
1620 * Walk up the tree from the bottom, freeing leaves and any interior
1621 * nodes which have had all slots visited. If a node (leaf or
1622 * interior) is freed, the node above it will have it's slot
1623 * incremented. The root node will never be freed.
1625 * At the end of this function, we should have a path which has all
1626 * slots incremented to the next position for a search. If we need to
1627 * read a new node it will be NULL and the node above it will have the
1628 * correct slot selected for a later read.
1630 * If we increment the root nodes slot counter past the number of
1631 * elements, 1 is returned to signal completion of the search.
1633 static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
1637 struct extent_buffer *eb;
1639 if (root_level == 0)
1642 while (level <= root_level) {
1643 eb = path->nodes[level];
1644 nr = btrfs_header_nritems(eb);
1645 path->slots[level]++;
1646 slot = path->slots[level];
1647 if (slot >= nr || level == 0) {
1649 * Don't free the root - we will detect this
1650 * condition after our loop and return a
1651 * positive value for caller to stop walking the tree.
1653 if (level != root_level) {
1654 btrfs_tree_unlock_rw(eb, path->locks[level]);
1655 path->locks[level] = 0;
1657 free_extent_buffer(eb);
1658 path->nodes[level] = NULL;
1659 path->slots[level] = 0;
1663 * We have a valid slot to walk back down
1664 * from. Stop here so caller can process these
1673 eb = path->nodes[root_level];
1674 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1680 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1681 struct btrfs_root *root,
1682 struct extent_buffer *root_eb,
1683 u64 root_gen, int root_level)
1685 struct btrfs_fs_info *fs_info = root->fs_info;
1688 struct extent_buffer *eb = root_eb;
1689 struct btrfs_path *path = NULL;
1691 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
1692 BUG_ON(root_eb == NULL);
1694 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1697 if (!extent_buffer_uptodate(root_eb)) {
1698 ret = btrfs_read_buffer(root_eb, root_gen);
1703 if (root_level == 0) {
1704 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
1708 path = btrfs_alloc_path();
1713 * Walk down the tree. Missing extent blocks are filled in as
1714 * we go. Metadata is accounted every time we read a new
1717 * When we reach a leaf, we account for file extent items in it,
1718 * walk back up the tree (adjusting slot pointers as we go)
1719 * and restart the search process.
1721 extent_buffer_get(root_eb); /* For path */
1722 path->nodes[root_level] = root_eb;
1723 path->slots[root_level] = 0;
1724 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1727 while (level >= 0) {
1728 if (path->nodes[level] == NULL) {
1734 * We need to get child blockptr/gen from parent before
1737 eb = path->nodes[level + 1];
1738 parent_slot = path->slots[level + 1];
1739 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1740 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1742 eb = read_tree_block(fs_info, child_bytenr, child_gen);
1746 } else if (!extent_buffer_uptodate(eb)) {
1747 free_extent_buffer(eb);
1752 path->nodes[level] = eb;
1753 path->slots[level] = 0;
1755 btrfs_tree_read_lock(eb);
1756 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1757 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1759 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1768 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1769 path->nodes[level]);
1773 /* Nonzero return here means we completed our search */
1774 ret = adjust_slots_upwards(path, root_level);
1778 /* Restart search with new slots */
1787 btrfs_free_path(path);
1792 #define UPDATE_NEW 0
1793 #define UPDATE_OLD 1
1795 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1797 static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1798 struct ulist *roots, struct ulist *tmp,
1799 struct ulist *qgroups, u64 seq, int update_old)
1801 struct ulist_node *unode;
1802 struct ulist_iterator uiter;
1803 struct ulist_node *tmp_unode;
1804 struct ulist_iterator tmp_uiter;
1805 struct btrfs_qgroup *qg;
1810 ULIST_ITER_INIT(&uiter);
1811 while ((unode = ulist_next(roots, &uiter))) {
1812 qg = find_qgroup_rb(fs_info, unode->val);
1817 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
1821 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
1824 ULIST_ITER_INIT(&tmp_uiter);
1825 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1826 struct btrfs_qgroup_list *glist;
1828 qg = unode_aux_to_qgroup(tmp_unode);
1830 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1832 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1833 list_for_each_entry(glist, &qg->groups, next_group) {
1834 ret = ulist_add(qgroups, glist->group->qgroupid,
1835 qgroup_to_aux(glist->group),
1839 ret = ulist_add(tmp, glist->group->qgroupid,
1840 qgroup_to_aux(glist->group),
1851 * Update qgroup rfer/excl counters.
1852 * Rfer update is easy, codes can explain themselves.
1854 * Excl update is tricky, the update is split into 2 part.
1855 * Part 1: Possible exclusive <-> sharing detect:
1857 * -------------------------------------
1859 * -------------------------------------
1861 * -------------------------------------
1864 * A: cur_old_roots < nr_old_roots (not exclusive before)
1865 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1866 * B: cur_new_roots < nr_new_roots (not exclusive now)
1867 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
1870 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1871 * *: Definitely not changed. **: Possible unchanged.
1873 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1875 * To make the logic clear, we first use condition A and B to split
1876 * combination into 4 results.
1878 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1879 * only on variant maybe 0.
1881 * Lastly, check result **, since there are 2 variants maybe 0, split them
1883 * But this time we don't need to consider other things, the codes and logic
1884 * is easy to understand now.
1886 static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1887 struct ulist *qgroups,
1890 u64 num_bytes, u64 seq)
1892 struct ulist_node *unode;
1893 struct ulist_iterator uiter;
1894 struct btrfs_qgroup *qg;
1895 u64 cur_new_count, cur_old_count;
1897 ULIST_ITER_INIT(&uiter);
1898 while ((unode = ulist_next(qgroups, &uiter))) {
1901 qg = unode_aux_to_qgroup(unode);
1902 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1903 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1905 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1906 cur_old_count, cur_new_count);
1908 /* Rfer update part */
1909 if (cur_old_count == 0 && cur_new_count > 0) {
1910 qg->rfer += num_bytes;
1911 qg->rfer_cmpr += num_bytes;
1914 if (cur_old_count > 0 && cur_new_count == 0) {
1915 qg->rfer -= num_bytes;
1916 qg->rfer_cmpr -= num_bytes;
1920 /* Excl update part */
1921 /* Exclusive/none -> shared case */
1922 if (cur_old_count == nr_old_roots &&
1923 cur_new_count < nr_new_roots) {
1924 /* Exclusive -> shared */
1925 if (cur_old_count != 0) {
1926 qg->excl -= num_bytes;
1927 qg->excl_cmpr -= num_bytes;
1932 /* Shared -> exclusive/none case */
1933 if (cur_old_count < nr_old_roots &&
1934 cur_new_count == nr_new_roots) {
1935 /* Shared->exclusive */
1936 if (cur_new_count != 0) {
1937 qg->excl += num_bytes;
1938 qg->excl_cmpr += num_bytes;
1943 /* Exclusive/none -> exclusive/none case */
1944 if (cur_old_count == nr_old_roots &&
1945 cur_new_count == nr_new_roots) {
1946 if (cur_old_count == 0) {
1947 /* None -> exclusive/none */
1949 if (cur_new_count != 0) {
1950 /* None -> exclusive */
1951 qg->excl += num_bytes;
1952 qg->excl_cmpr += num_bytes;
1955 /* None -> none, nothing changed */
1957 /* Exclusive -> exclusive/none */
1959 if (cur_new_count == 0) {
1960 /* Exclusive -> none */
1961 qg->excl -= num_bytes;
1962 qg->excl_cmpr -= num_bytes;
1965 /* Exclusive -> exclusive, nothing changed */
1970 qgroup_dirty(fs_info, qg);
1976 * Check if the @roots potentially is a list of fs tree roots
1978 * Return 0 for definitely not a fs/subvol tree roots ulist
1979 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1982 static int maybe_fs_roots(struct ulist *roots)
1984 struct ulist_node *unode;
1985 struct ulist_iterator uiter;
1987 /* Empty one, still possible for fs roots */
1988 if (!roots || roots->nnodes == 0)
1991 ULIST_ITER_INIT(&uiter);
1992 unode = ulist_next(roots, &uiter);
1997 * If it contains fs tree roots, then it must belong to fs/subvol
1999 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2001 return is_fstree(unode->val);
2005 btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
2006 struct btrfs_fs_info *fs_info,
2007 u64 bytenr, u64 num_bytes,
2008 struct ulist *old_roots, struct ulist *new_roots)
2010 struct ulist *qgroups = NULL;
2011 struct ulist *tmp = NULL;
2013 u64 nr_new_roots = 0;
2014 u64 nr_old_roots = 0;
2017 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2021 if (!maybe_fs_roots(new_roots))
2023 nr_new_roots = new_roots->nnodes;
2026 if (!maybe_fs_roots(old_roots))
2028 nr_old_roots = old_roots->nnodes;
2031 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2032 if (nr_old_roots == 0 && nr_new_roots == 0)
2035 BUG_ON(!fs_info->quota_root);
2037 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
2038 nr_old_roots, nr_new_roots);
2040 qgroups = ulist_alloc(GFP_NOFS);
2045 tmp = ulist_alloc(GFP_NOFS);
2051 mutex_lock(&fs_info->qgroup_rescan_lock);
2052 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2053 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2054 mutex_unlock(&fs_info->qgroup_rescan_lock);
2059 mutex_unlock(&fs_info->qgroup_rescan_lock);
2061 spin_lock(&fs_info->qgroup_lock);
2062 seq = fs_info->qgroup_seq;
2064 /* Update old refcnts using old_roots */
2065 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2070 /* Update new refcnts using new_roots */
2071 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2076 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2080 * Bump qgroup_seq to avoid seq overlap
2082 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2084 spin_unlock(&fs_info->qgroup_lock);
2087 ulist_free(qgroups);
2088 ulist_free(old_roots);
2089 ulist_free(new_roots);
2093 int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2095 struct btrfs_fs_info *fs_info = trans->fs_info;
2096 struct btrfs_qgroup_extent_record *record;
2097 struct btrfs_delayed_ref_root *delayed_refs;
2098 struct ulist *new_roots = NULL;
2099 struct rb_node *node;
2103 delayed_refs = &trans->transaction->delayed_refs;
2104 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2105 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2106 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2109 trace_btrfs_qgroup_account_extents(fs_info, record);
2113 * Old roots should be searched when inserting qgroup
2116 if (WARN_ON(!record->old_roots)) {
2117 /* Search commit root to find old_roots */
2118 ret = btrfs_find_all_roots(NULL, fs_info,
2120 &record->old_roots, false);
2126 * Use SEQ_LAST as time_seq to do special search, which
2127 * doesn't lock tree or delayed_refs and search current
2128 * root. It's safe inside commit_transaction().
2130 ret = btrfs_find_all_roots(trans, fs_info,
2131 record->bytenr, SEQ_LAST, &new_roots, false);
2134 if (qgroup_to_skip) {
2135 ulist_del(new_roots, qgroup_to_skip, 0);
2136 ulist_del(record->old_roots, qgroup_to_skip,
2139 ret = btrfs_qgroup_account_extent(trans, fs_info,
2140 record->bytenr, record->num_bytes,
2141 record->old_roots, new_roots);
2142 record->old_roots = NULL;
2146 ulist_free(record->old_roots);
2147 ulist_free(new_roots);
2149 rb_erase(node, &delayed_refs->dirty_extent_root);
2157 * called from commit_transaction. Writes all changed qgroups to disk.
2159 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2160 struct btrfs_fs_info *fs_info)
2162 struct btrfs_root *quota_root = fs_info->quota_root;
2168 spin_lock(&fs_info->qgroup_lock);
2169 while (!list_empty(&fs_info->dirty_qgroups)) {
2170 struct btrfs_qgroup *qgroup;
2171 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2172 struct btrfs_qgroup, dirty);
2173 list_del_init(&qgroup->dirty);
2174 spin_unlock(&fs_info->qgroup_lock);
2175 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2177 fs_info->qgroup_flags |=
2178 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2179 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2181 fs_info->qgroup_flags |=
2182 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2183 spin_lock(&fs_info->qgroup_lock);
2185 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2186 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2188 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2189 spin_unlock(&fs_info->qgroup_lock);
2191 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2193 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2199 * Copy the accounting information between qgroups. This is necessary
2200 * when a snapshot or a subvolume is created. Throwing an error will
2201 * cause a transaction abort so we take extra care here to only error
2202 * when a readonly fs is a reasonable outcome.
2204 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2205 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2206 struct btrfs_qgroup_inherit *inherit)
2211 struct btrfs_root *quota_root = fs_info->quota_root;
2212 struct btrfs_qgroup *srcgroup;
2213 struct btrfs_qgroup *dstgroup;
2217 mutex_lock(&fs_info->qgroup_ioctl_lock);
2218 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2227 i_qgroups = (u64 *)(inherit + 1);
2228 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2229 2 * inherit->num_excl_copies;
2230 for (i = 0; i < nums; ++i) {
2231 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2234 * Zero out invalid groups so we can ignore
2238 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2246 * create a tracking group for the subvol itself
2248 ret = add_qgroup_item(trans, quota_root, objectid);
2253 struct btrfs_root *srcroot;
2254 struct btrfs_key srckey;
2256 srckey.objectid = srcid;
2257 srckey.type = BTRFS_ROOT_ITEM_KEY;
2258 srckey.offset = (u64)-1;
2259 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2260 if (IS_ERR(srcroot)) {
2261 ret = PTR_ERR(srcroot);
2265 level_size = fs_info->nodesize;
2269 * add qgroup to all inherited groups
2272 i_qgroups = (u64 *)(inherit + 1);
2273 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2274 if (*i_qgroups == 0)
2276 ret = add_qgroup_relation_item(trans, quota_root,
2277 objectid, *i_qgroups);
2278 if (ret && ret != -EEXIST)
2280 ret = add_qgroup_relation_item(trans, quota_root,
2281 *i_qgroups, objectid);
2282 if (ret && ret != -EEXIST)
2289 spin_lock(&fs_info->qgroup_lock);
2291 dstgroup = add_qgroup_rb(fs_info, objectid);
2292 if (IS_ERR(dstgroup)) {
2293 ret = PTR_ERR(dstgroup);
2297 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2298 dstgroup->lim_flags = inherit->lim.flags;
2299 dstgroup->max_rfer = inherit->lim.max_rfer;
2300 dstgroup->max_excl = inherit->lim.max_excl;
2301 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2302 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2304 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2306 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2308 "unable to update quota limit for %llu",
2309 dstgroup->qgroupid);
2315 srcgroup = find_qgroup_rb(fs_info, srcid);
2320 * We call inherit after we clone the root in order to make sure
2321 * our counts don't go crazy, so at this point the only
2322 * difference between the two roots should be the root node.
2324 dstgroup->rfer = srcgroup->rfer;
2325 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2326 dstgroup->excl = level_size;
2327 dstgroup->excl_cmpr = level_size;
2328 srcgroup->excl = level_size;
2329 srcgroup->excl_cmpr = level_size;
2331 /* inherit the limit info */
2332 dstgroup->lim_flags = srcgroup->lim_flags;
2333 dstgroup->max_rfer = srcgroup->max_rfer;
2334 dstgroup->max_excl = srcgroup->max_excl;
2335 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2336 dstgroup->rsv_excl = srcgroup->rsv_excl;
2338 qgroup_dirty(fs_info, dstgroup);
2339 qgroup_dirty(fs_info, srcgroup);
2345 i_qgroups = (u64 *)(inherit + 1);
2346 for (i = 0; i < inherit->num_qgroups; ++i) {
2348 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2355 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
2356 struct btrfs_qgroup *src;
2357 struct btrfs_qgroup *dst;
2359 if (!i_qgroups[0] || !i_qgroups[1])
2362 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2363 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2370 dst->rfer = src->rfer - level_size;
2371 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2373 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2374 struct btrfs_qgroup *src;
2375 struct btrfs_qgroup *dst;
2377 if (!i_qgroups[0] || !i_qgroups[1])
2380 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2381 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2388 dst->excl = src->excl + level_size;
2389 dst->excl_cmpr = src->excl_cmpr + level_size;
2393 spin_unlock(&fs_info->qgroup_lock);
2395 mutex_unlock(&fs_info->qgroup_ioctl_lock);
2399 static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
2401 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2402 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
2405 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2406 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
2412 static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2413 enum btrfs_qgroup_rsv_type type)
2415 struct btrfs_root *quota_root;
2416 struct btrfs_qgroup *qgroup;
2417 struct btrfs_fs_info *fs_info = root->fs_info;
2418 u64 ref_root = root->root_key.objectid;
2421 struct ulist_node *unode;
2422 struct ulist_iterator uiter;
2424 if (!is_fstree(ref_root))
2430 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2431 capable(CAP_SYS_RESOURCE))
2435 spin_lock(&fs_info->qgroup_lock);
2436 quota_root = fs_info->quota_root;
2440 qgroup = find_qgroup_rb(fs_info, ref_root);
2445 * in a first step, we check all affected qgroups if any limits would
2448 ulist_reinit(fs_info->qgroup_ulist);
2449 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2450 (uintptr_t)qgroup, GFP_ATOMIC);
2453 ULIST_ITER_INIT(&uiter);
2454 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2455 struct btrfs_qgroup *qg;
2456 struct btrfs_qgroup_list *glist;
2458 qg = unode_aux_to_qgroup(unode);
2460 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
2462 * Commit the tree and retry, since we may have
2463 * deletions which would free up space.
2465 if (!retried && qgroup_rsv_total(qg) > 0) {
2466 struct btrfs_trans_handle *trans;
2468 spin_unlock(&fs_info->qgroup_lock);
2469 ret = btrfs_start_delalloc_inodes(root, 0);
2472 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
2473 trans = btrfs_join_transaction(root);
2475 return PTR_ERR(trans);
2476 ret = btrfs_commit_transaction(trans);
2486 list_for_each_entry(glist, &qg->groups, next_group) {
2487 ret = ulist_add(fs_info->qgroup_ulist,
2488 glist->group->qgroupid,
2489 (uintptr_t)glist->group, GFP_ATOMIC);
2496 * no limits exceeded, now record the reservation into all qgroups
2498 ULIST_ITER_INIT(&uiter);
2499 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2500 struct btrfs_qgroup *qg;
2502 qg = unode_aux_to_qgroup(unode);
2504 trace_qgroup_update_reserve(fs_info, qg, num_bytes, type);
2505 qgroup_rsv_add(fs_info, qg, num_bytes, type);
2509 spin_unlock(&fs_info->qgroup_lock);
2513 void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2514 u64 ref_root, u64 num_bytes,
2515 enum btrfs_qgroup_rsv_type type)
2517 struct btrfs_root *quota_root;
2518 struct btrfs_qgroup *qgroup;
2519 struct ulist_node *unode;
2520 struct ulist_iterator uiter;
2523 if (!is_fstree(ref_root))
2529 spin_lock(&fs_info->qgroup_lock);
2531 quota_root = fs_info->quota_root;
2535 qgroup = find_qgroup_rb(fs_info, ref_root);
2539 ulist_reinit(fs_info->qgroup_ulist);
2540 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2541 (uintptr_t)qgroup, GFP_ATOMIC);
2544 ULIST_ITER_INIT(&uiter);
2545 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2546 struct btrfs_qgroup *qg;
2547 struct btrfs_qgroup_list *glist;
2549 qg = unode_aux_to_qgroup(unode);
2551 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes, type);
2552 qgroup_rsv_release(fs_info, qg, num_bytes, type);
2554 list_for_each_entry(glist, &qg->groups, next_group) {
2555 ret = ulist_add(fs_info->qgroup_ulist,
2556 glist->group->qgroupid,
2557 (uintptr_t)glist->group, GFP_ATOMIC);
2564 spin_unlock(&fs_info->qgroup_lock);
2568 * returns < 0 on error, 0 when more leafs are to be scanned.
2569 * returns 1 when done.
2572 qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
2573 struct btrfs_trans_handle *trans)
2575 struct btrfs_key found;
2576 struct extent_buffer *scratch_leaf = NULL;
2577 struct ulist *roots = NULL;
2578 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
2583 mutex_lock(&fs_info->qgroup_rescan_lock);
2584 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2585 &fs_info->qgroup_rescan_progress,
2588 btrfs_debug(fs_info,
2589 "current progress key (%llu %u %llu), search_slot ret %d",
2590 fs_info->qgroup_rescan_progress.objectid,
2591 fs_info->qgroup_rescan_progress.type,
2592 fs_info->qgroup_rescan_progress.offset, ret);
2596 * The rescan is about to end, we will not be scanning any
2597 * further blocks. We cannot unset the RESCAN flag here, because
2598 * we want to commit the transaction if everything went well.
2599 * To make the live accounting work in this phase, we set our
2600 * scan progress pointer such that every real extent objectid
2603 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2604 btrfs_release_path(path);
2605 mutex_unlock(&fs_info->qgroup_rescan_lock);
2609 btrfs_item_key_to_cpu(path->nodes[0], &found,
2610 btrfs_header_nritems(path->nodes[0]) - 1);
2611 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2613 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2614 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2615 if (!scratch_leaf) {
2617 mutex_unlock(&fs_info->qgroup_rescan_lock);
2620 extent_buffer_get(scratch_leaf);
2621 btrfs_tree_read_lock(scratch_leaf);
2622 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
2623 slot = path->slots[0];
2624 btrfs_release_path(path);
2625 mutex_unlock(&fs_info->qgroup_rescan_lock);
2627 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2628 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
2629 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2630 found.type != BTRFS_METADATA_ITEM_KEY)
2632 if (found.type == BTRFS_METADATA_ITEM_KEY)
2633 num_bytes = fs_info->nodesize;
2635 num_bytes = found.offset;
2637 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2641 /* For rescan, just pass old_roots as NULL */
2642 ret = btrfs_qgroup_account_extent(trans, fs_info,
2643 found.objectid, num_bytes, NULL, roots);
2649 btrfs_tree_read_unlock_blocking(scratch_leaf);
2650 free_extent_buffer(scratch_leaf);
2652 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2657 static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
2659 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2660 qgroup_rescan_work);
2661 struct btrfs_path *path;
2662 struct btrfs_trans_handle *trans = NULL;
2666 path = btrfs_alloc_path();
2671 while (!err && !btrfs_fs_closing(fs_info)) {
2672 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2673 if (IS_ERR(trans)) {
2674 err = PTR_ERR(trans);
2677 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
2680 err = qgroup_rescan_leaf(fs_info, path, trans);
2683 btrfs_commit_transaction(trans);
2685 btrfs_end_transaction(trans);
2689 btrfs_free_path(path);
2691 mutex_lock(&fs_info->qgroup_rescan_lock);
2692 if (!btrfs_fs_closing(fs_info))
2693 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2696 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2697 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2698 } else if (err < 0) {
2699 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2701 mutex_unlock(&fs_info->qgroup_rescan_lock);
2704 * only update status, since the previous part has already updated the
2707 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2708 if (IS_ERR(trans)) {
2709 err = PTR_ERR(trans);
2711 "fail to start transaction for status update: %d",
2715 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2718 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
2720 btrfs_end_transaction(trans);
2722 if (btrfs_fs_closing(fs_info)) {
2723 btrfs_info(fs_info, "qgroup scan paused");
2724 } else if (err >= 0) {
2725 btrfs_info(fs_info, "qgroup scan completed%s",
2726 err > 0 ? " (inconsistency flag cleared)" : "");
2728 btrfs_err(fs_info, "qgroup scan failed with %d", err);
2732 mutex_lock(&fs_info->qgroup_rescan_lock);
2733 fs_info->qgroup_rescan_running = false;
2734 mutex_unlock(&fs_info->qgroup_rescan_lock);
2735 complete_all(&fs_info->qgroup_rescan_completion);
2739 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2740 * memory required for the rescan context.
2743 qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2749 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2750 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2755 mutex_lock(&fs_info->qgroup_rescan_lock);
2756 spin_lock(&fs_info->qgroup_lock);
2759 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2761 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2765 spin_unlock(&fs_info->qgroup_lock);
2766 mutex_unlock(&fs_info->qgroup_rescan_lock);
2769 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2772 memset(&fs_info->qgroup_rescan_progress, 0,
2773 sizeof(fs_info->qgroup_rescan_progress));
2774 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2775 init_completion(&fs_info->qgroup_rescan_completion);
2776 fs_info->qgroup_rescan_running = true;
2778 spin_unlock(&fs_info->qgroup_lock);
2779 mutex_unlock(&fs_info->qgroup_rescan_lock);
2781 memset(&fs_info->qgroup_rescan_work, 0,
2782 sizeof(fs_info->qgroup_rescan_work));
2783 btrfs_init_work(&fs_info->qgroup_rescan_work,
2784 btrfs_qgroup_rescan_helper,
2785 btrfs_qgroup_rescan_worker, NULL, NULL);
2789 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
2797 qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2800 struct btrfs_qgroup *qgroup;
2802 spin_lock(&fs_info->qgroup_lock);
2803 /* clear all current qgroup tracking information */
2804 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2805 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2807 qgroup->rfer_cmpr = 0;
2809 qgroup->excl_cmpr = 0;
2811 spin_unlock(&fs_info->qgroup_lock);
2815 btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2818 struct btrfs_trans_handle *trans;
2820 ret = qgroup_rescan_init(fs_info, 0, 1);
2825 * We have set the rescan_progress to 0, which means no more
2826 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2827 * However, btrfs_qgroup_account_ref may be right after its call
2828 * to btrfs_find_all_roots, in which case it would still do the
2830 * To solve this, we're committing the transaction, which will
2831 * ensure we run all delayed refs and only after that, we are
2832 * going to clear all tracking information for a clean start.
2835 trans = btrfs_join_transaction(fs_info->fs_root);
2836 if (IS_ERR(trans)) {
2837 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2838 return PTR_ERR(trans);
2840 ret = btrfs_commit_transaction(trans);
2842 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2846 qgroup_rescan_zero_tracking(fs_info);
2848 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2849 &fs_info->qgroup_rescan_work);
2854 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2860 mutex_lock(&fs_info->qgroup_rescan_lock);
2861 spin_lock(&fs_info->qgroup_lock);
2862 running = fs_info->qgroup_rescan_running;
2863 spin_unlock(&fs_info->qgroup_lock);
2864 mutex_unlock(&fs_info->qgroup_rescan_lock);
2870 ret = wait_for_completion_interruptible(
2871 &fs_info->qgroup_rescan_completion);
2873 wait_for_completion(&fs_info->qgroup_rescan_completion);
2879 * this is only called from open_ctree where we're still single threaded, thus
2880 * locking is omitted here.
2883 btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2885 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2886 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2887 &fs_info->qgroup_rescan_work);
2891 * Reserve qgroup space for range [start, start + len).
2893 * This function will either reserve space from related qgroups or doing
2894 * nothing if the range is already reserved.
2896 * Return 0 for successful reserve
2897 * Return <0 for error (including -EQUOT)
2899 * NOTE: this function may sleep for memory allocation.
2900 * if btrfs_qgroup_reserve_data() is called multiple times with
2901 * same @reserved, caller must ensure when error happens it's OK
2902 * to free *ALL* reserved space.
2904 int btrfs_qgroup_reserve_data(struct inode *inode,
2905 struct extent_changeset **reserved_ret, u64 start,
2908 struct btrfs_root *root = BTRFS_I(inode)->root;
2909 struct ulist_node *unode;
2910 struct ulist_iterator uiter;
2911 struct extent_changeset *reserved;
2916 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2917 !is_fstree(root->objectid) || len == 0)
2920 /* @reserved parameter is mandatory for qgroup */
2921 if (WARN_ON(!reserved_ret))
2923 if (!*reserved_ret) {
2924 *reserved_ret = extent_changeset_alloc();
2928 reserved = *reserved_ret;
2929 /* Record already reserved space */
2930 orig_reserved = reserved->bytes_changed;
2931 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
2932 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
2934 /* Newly reserved space */
2935 to_reserve = reserved->bytes_changed - orig_reserved;
2936 trace_btrfs_qgroup_reserve_data(inode, start, len,
2937 to_reserve, QGROUP_RESERVE);
2940 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
2947 /* cleanup *ALL* already reserved ranges */
2948 ULIST_ITER_INIT(&uiter);
2949 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
2950 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
2951 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
2952 extent_changeset_release(reserved);
2956 /* Free ranges specified by @reserved, normally in error path */
2957 static int qgroup_free_reserved_data(struct inode *inode,
2958 struct extent_changeset *reserved, u64 start, u64 len)
2960 struct btrfs_root *root = BTRFS_I(inode)->root;
2961 struct ulist_node *unode;
2962 struct ulist_iterator uiter;
2963 struct extent_changeset changeset;
2967 extent_changeset_init(&changeset);
2968 len = round_up(start + len, root->fs_info->sectorsize);
2969 start = round_down(start, root->fs_info->sectorsize);
2971 ULIST_ITER_INIT(&uiter);
2972 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
2973 u64 range_start = unode->val;
2974 /* unode->aux is the inclusive end */
2975 u64 range_len = unode->aux - range_start + 1;
2979 extent_changeset_release(&changeset);
2981 /* Only free range in range [start, start + len) */
2982 if (range_start >= start + len ||
2983 range_start + range_len <= start)
2985 free_start = max(range_start, start);
2986 free_len = min(start + len, range_start + range_len) -
2989 * TODO: To also modify reserved->ranges_reserved to reflect
2992 * However as long as we free qgroup reserved according to
2993 * EXTENT_QGROUP_RESERVED, we won't double free.
2994 * So not need to rush.
2996 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
2997 free_start, free_start + free_len - 1,
2998 EXTENT_QGROUP_RESERVED, &changeset);
3001 freed += changeset.bytes_changed;
3003 btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed,
3004 BTRFS_QGROUP_RSV_DATA);
3007 extent_changeset_release(&changeset);
3011 static int __btrfs_qgroup_release_data(struct inode *inode,
3012 struct extent_changeset *reserved, u64 start, u64 len,
3015 struct extent_changeset changeset;
3016 int trace_op = QGROUP_RELEASE;
3019 /* In release case, we shouldn't have @reserved */
3020 WARN_ON(!free && reserved);
3021 if (free && reserved)
3022 return qgroup_free_reserved_data(inode, reserved, start, len);
3023 extent_changeset_init(&changeset);
3024 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
3025 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
3030 trace_op = QGROUP_FREE;
3031 trace_btrfs_qgroup_release_data(inode, start, len,
3032 changeset.bytes_changed, trace_op);
3034 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3035 BTRFS_I(inode)->root->objectid,
3036 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3037 ret = changeset.bytes_changed;
3039 extent_changeset_release(&changeset);
3044 * Free a reserved space range from io_tree and related qgroups
3046 * Should be called when a range of pages get invalidated before reaching disk.
3047 * Or for error cleanup case.
3048 * if @reserved is given, only reserved range in [@start, @start + @len) will
3051 * For data written to disk, use btrfs_qgroup_release_data().
3053 * NOTE: This function may sleep for memory allocation.
3055 int btrfs_qgroup_free_data(struct inode *inode,
3056 struct extent_changeset *reserved, u64 start, u64 len)
3058 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
3062 * Release a reserved space range from io_tree only.
3064 * Should be called when a range of pages get written to disk and corresponding
3065 * FILE_EXTENT is inserted into corresponding root.
3067 * Since new qgroup accounting framework will only update qgroup numbers at
3068 * commit_transaction() time, its reserved space shouldn't be freed from
3071 * But we should release the range from io_tree, to allow further write to be
3074 * NOTE: This function may sleep for memory allocation.
3076 int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3078 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
3081 int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3084 struct btrfs_fs_info *fs_info = root->fs_info;
3087 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3088 !is_fstree(root->objectid) || num_bytes == 0)
3091 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3092 trace_qgroup_meta_reserve(root, (s64)num_bytes);
3093 ret = qgroup_reserve(root, num_bytes, enforce, BTRFS_QGROUP_RSV_META);
3096 atomic64_add(num_bytes, &root->qgroup_meta_rsv);
3100 void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
3102 struct btrfs_fs_info *fs_info = root->fs_info;
3105 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3106 !is_fstree(root->objectid))
3109 reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
3112 trace_qgroup_meta_reserve(root, -(s64)reserved);
3113 btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved,
3114 BTRFS_QGROUP_RSV_META);
3117 void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
3119 struct btrfs_fs_info *fs_info = root->fs_info;
3121 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3122 !is_fstree(root->objectid))
3125 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3126 WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
3127 atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
3128 trace_qgroup_meta_reserve(root, -(s64)num_bytes);
3129 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes,
3130 BTRFS_QGROUP_RSV_META);
3134 * Check qgroup reserved space leaking, normally at destroy inode
3137 void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3139 struct extent_changeset changeset;
3140 struct ulist_node *unode;
3141 struct ulist_iterator iter;
3144 extent_changeset_init(&changeset);
3145 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
3146 EXTENT_QGROUP_RESERVED, &changeset);
3149 if (WARN_ON(changeset.bytes_changed)) {
3150 ULIST_ITER_INIT(&iter);
3151 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
3152 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3153 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3154 inode->i_ino, unode->val, unode->aux);
3156 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3157 BTRFS_I(inode)->root->objectid,
3158 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3161 extent_changeset_release(&changeset);