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
50 static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup
*qg
, u64 seq
,
53 if (qg
->old_refcnt
< seq
)
55 qg
->old_refcnt
+= mod
;
58 static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup
*qg
, u64 seq
,
61 if (qg
->new_refcnt
< seq
)
63 qg
->new_refcnt
+= mod
;
66 static inline u64
btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup
*qg
, u64 seq
)
68 if (qg
->old_refcnt
< seq
)
70 return qg
->old_refcnt
- seq
;
73 static inline u64
btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup
*qg
, u64 seq
)
75 if (qg
->new_refcnt
< seq
)
77 return qg
->new_refcnt
- seq
;
81 * glue structure to represent the relations between qgroups.
83 struct btrfs_qgroup_list
{
84 struct list_head next_group
;
85 struct list_head next_member
;
86 struct btrfs_qgroup
*group
;
87 struct btrfs_qgroup
*member
;
90 static inline u64
qgroup_to_aux(struct btrfs_qgroup
*qg
)
92 return (u64
)(uintptr_t)qg
;
95 static inline struct btrfs_qgroup
* unode_aux_to_qgroup(struct ulist_node
*n
)
97 return (struct btrfs_qgroup
*)(uintptr_t)n
->aux
;
101 qgroup_rescan_init(struct btrfs_fs_info
*fs_info
, u64 progress_objectid
,
103 static void qgroup_rescan_zero_tracking(struct btrfs_fs_info
*fs_info
);
105 /* must be called with qgroup_ioctl_lock held */
106 static struct btrfs_qgroup
*find_qgroup_rb(struct btrfs_fs_info
*fs_info
,
109 struct rb_node
*n
= fs_info
->qgroup_tree
.rb_node
;
110 struct btrfs_qgroup
*qgroup
;
113 qgroup
= rb_entry(n
, struct btrfs_qgroup
, node
);
114 if (qgroup
->qgroupid
< qgroupid
)
116 else if (qgroup
->qgroupid
> qgroupid
)
124 /* must be called with qgroup_lock held */
125 static struct btrfs_qgroup
*add_qgroup_rb(struct btrfs_fs_info
*fs_info
,
128 struct rb_node
**p
= &fs_info
->qgroup_tree
.rb_node
;
129 struct rb_node
*parent
= NULL
;
130 struct btrfs_qgroup
*qgroup
;
134 qgroup
= rb_entry(parent
, struct btrfs_qgroup
, node
);
136 if (qgroup
->qgroupid
< qgroupid
)
138 else if (qgroup
->qgroupid
> qgroupid
)
144 qgroup
= kzalloc(sizeof(*qgroup
), GFP_ATOMIC
);
146 return ERR_PTR(-ENOMEM
);
148 qgroup
->qgroupid
= qgroupid
;
149 INIT_LIST_HEAD(&qgroup
->groups
);
150 INIT_LIST_HEAD(&qgroup
->members
);
151 INIT_LIST_HEAD(&qgroup
->dirty
);
153 rb_link_node(&qgroup
->node
, parent
, p
);
154 rb_insert_color(&qgroup
->node
, &fs_info
->qgroup_tree
);
159 static void __del_qgroup_rb(struct btrfs_qgroup
*qgroup
)
161 struct btrfs_qgroup_list
*list
;
163 list_del(&qgroup
->dirty
);
164 while (!list_empty(&qgroup
->groups
)) {
165 list
= list_first_entry(&qgroup
->groups
,
166 struct btrfs_qgroup_list
, next_group
);
167 list_del(&list
->next_group
);
168 list_del(&list
->next_member
);
172 while (!list_empty(&qgroup
->members
)) {
173 list
= list_first_entry(&qgroup
->members
,
174 struct btrfs_qgroup_list
, next_member
);
175 list_del(&list
->next_group
);
176 list_del(&list
->next_member
);
182 /* must be called with qgroup_lock held */
183 static int del_qgroup_rb(struct btrfs_fs_info
*fs_info
, u64 qgroupid
)
185 struct btrfs_qgroup
*qgroup
= find_qgroup_rb(fs_info
, qgroupid
);
190 rb_erase(&qgroup
->node
, &fs_info
->qgroup_tree
);
191 __del_qgroup_rb(qgroup
);
195 /* must be called with qgroup_lock held */
196 static int add_relation_rb(struct btrfs_fs_info
*fs_info
,
197 u64 memberid
, u64 parentid
)
199 struct btrfs_qgroup
*member
;
200 struct btrfs_qgroup
*parent
;
201 struct btrfs_qgroup_list
*list
;
203 member
= find_qgroup_rb(fs_info
, memberid
);
204 parent
= find_qgroup_rb(fs_info
, parentid
);
205 if (!member
|| !parent
)
208 list
= kzalloc(sizeof(*list
), GFP_ATOMIC
);
212 list
->group
= parent
;
213 list
->member
= member
;
214 list_add_tail(&list
->next_group
, &member
->groups
);
215 list_add_tail(&list
->next_member
, &parent
->members
);
220 /* must be called with qgroup_lock held */
221 static int del_relation_rb(struct btrfs_fs_info
*fs_info
,
222 u64 memberid
, u64 parentid
)
224 struct btrfs_qgroup
*member
;
225 struct btrfs_qgroup
*parent
;
226 struct btrfs_qgroup_list
*list
;
228 member
= find_qgroup_rb(fs_info
, memberid
);
229 parent
= find_qgroup_rb(fs_info
, parentid
);
230 if (!member
|| !parent
)
233 list_for_each_entry(list
, &member
->groups
, next_group
) {
234 if (list
->group
== parent
) {
235 list_del(&list
->next_group
);
236 list_del(&list
->next_member
);
244 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
245 int btrfs_verify_qgroup_counts(struct btrfs_fs_info
*fs_info
, u64 qgroupid
,
248 struct btrfs_qgroup
*qgroup
;
250 qgroup
= find_qgroup_rb(fs_info
, qgroupid
);
253 if (qgroup
->rfer
!= rfer
|| qgroup
->excl
!= excl
)
260 * The full config is read in one go, only called from open_ctree()
261 * It doesn't use any locking, as at this point we're still single-threaded
263 int btrfs_read_qgroup_config(struct btrfs_fs_info
*fs_info
)
265 struct btrfs_key key
;
266 struct btrfs_key found_key
;
267 struct btrfs_root
*quota_root
= fs_info
->quota_root
;
268 struct btrfs_path
*path
= NULL
;
269 struct extent_buffer
*l
;
273 u64 rescan_progress
= 0;
275 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
278 fs_info
->qgroup_ulist
= ulist_alloc(GFP_KERNEL
);
279 if (!fs_info
->qgroup_ulist
) {
284 path
= btrfs_alloc_path();
290 /* default this to quota off, in case no status key is found */
291 fs_info
->qgroup_flags
= 0;
294 * pass 1: read status, all qgroup infos and limits
299 ret
= btrfs_search_slot_for_read(quota_root
, &key
, path
, 1, 1);
304 struct btrfs_qgroup
*qgroup
;
306 slot
= path
->slots
[0];
308 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
310 if (found_key
.type
== BTRFS_QGROUP_STATUS_KEY
) {
311 struct btrfs_qgroup_status_item
*ptr
;
313 ptr
= btrfs_item_ptr(l
, slot
,
314 struct btrfs_qgroup_status_item
);
316 if (btrfs_qgroup_status_version(l
, ptr
) !=
317 BTRFS_QGROUP_STATUS_VERSION
) {
319 "old qgroup version, quota disabled");
322 if (btrfs_qgroup_status_generation(l
, ptr
) !=
323 fs_info
->generation
) {
324 flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
326 "qgroup generation mismatch, marked as inconsistent");
328 fs_info
->qgroup_flags
= btrfs_qgroup_status_flags(l
,
330 rescan_progress
= btrfs_qgroup_status_rescan(l
, ptr
);
334 if (found_key
.type
!= BTRFS_QGROUP_INFO_KEY
&&
335 found_key
.type
!= BTRFS_QGROUP_LIMIT_KEY
)
338 qgroup
= find_qgroup_rb(fs_info
, found_key
.offset
);
339 if ((qgroup
&& found_key
.type
== BTRFS_QGROUP_INFO_KEY
) ||
340 (!qgroup
&& found_key
.type
== BTRFS_QGROUP_LIMIT_KEY
)) {
341 btrfs_err(fs_info
, "inconsistent qgroup config");
342 flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
345 qgroup
= add_qgroup_rb(fs_info
, found_key
.offset
);
346 if (IS_ERR(qgroup
)) {
347 ret
= PTR_ERR(qgroup
);
351 switch (found_key
.type
) {
352 case BTRFS_QGROUP_INFO_KEY
: {
353 struct btrfs_qgroup_info_item
*ptr
;
355 ptr
= btrfs_item_ptr(l
, slot
,
356 struct btrfs_qgroup_info_item
);
357 qgroup
->rfer
= btrfs_qgroup_info_rfer(l
, ptr
);
358 qgroup
->rfer_cmpr
= btrfs_qgroup_info_rfer_cmpr(l
, ptr
);
359 qgroup
->excl
= btrfs_qgroup_info_excl(l
, ptr
);
360 qgroup
->excl_cmpr
= btrfs_qgroup_info_excl_cmpr(l
, ptr
);
361 /* generation currently unused */
364 case BTRFS_QGROUP_LIMIT_KEY
: {
365 struct btrfs_qgroup_limit_item
*ptr
;
367 ptr
= btrfs_item_ptr(l
, slot
,
368 struct btrfs_qgroup_limit_item
);
369 qgroup
->lim_flags
= btrfs_qgroup_limit_flags(l
, ptr
);
370 qgroup
->max_rfer
= btrfs_qgroup_limit_max_rfer(l
, ptr
);
371 qgroup
->max_excl
= btrfs_qgroup_limit_max_excl(l
, ptr
);
372 qgroup
->rsv_rfer
= btrfs_qgroup_limit_rsv_rfer(l
, ptr
);
373 qgroup
->rsv_excl
= btrfs_qgroup_limit_rsv_excl(l
, ptr
);
378 ret
= btrfs_next_item(quota_root
, path
);
384 btrfs_release_path(path
);
387 * pass 2: read all qgroup relations
390 key
.type
= BTRFS_QGROUP_RELATION_KEY
;
392 ret
= btrfs_search_slot_for_read(quota_root
, &key
, path
, 1, 0);
396 slot
= path
->slots
[0];
398 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
400 if (found_key
.type
!= BTRFS_QGROUP_RELATION_KEY
)
403 if (found_key
.objectid
> found_key
.offset
) {
404 /* parent <- member, not needed to build config */
405 /* FIXME should we omit the key completely? */
409 ret
= add_relation_rb(fs_info
, found_key
.objectid
,
411 if (ret
== -ENOENT
) {
413 "orphan qgroup relation 0x%llx->0x%llx",
414 found_key
.objectid
, found_key
.offset
);
415 ret
= 0; /* ignore the error */
420 ret
= btrfs_next_item(quota_root
, path
);
427 fs_info
->qgroup_flags
|= flags
;
428 if (!(fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_ON
))
429 clear_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
);
430 else if (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
&&
432 ret
= qgroup_rescan_init(fs_info
, rescan_progress
, 0);
433 btrfs_free_path(path
);
436 ulist_free(fs_info
->qgroup_ulist
);
437 fs_info
->qgroup_ulist
= NULL
;
438 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN
;
441 return ret
< 0 ? ret
: 0;
445 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
446 * first two are in single-threaded paths.And for the third one, we have set
447 * quota_root to be null with qgroup_lock held before, so it is safe to clean
448 * up the in-memory structures without qgroup_lock held.
450 void btrfs_free_qgroup_config(struct btrfs_fs_info
*fs_info
)
453 struct btrfs_qgroup
*qgroup
;
455 while ((n
= rb_first(&fs_info
->qgroup_tree
))) {
456 qgroup
= rb_entry(n
, struct btrfs_qgroup
, node
);
457 rb_erase(n
, &fs_info
->qgroup_tree
);
458 __del_qgroup_rb(qgroup
);
461 * we call btrfs_free_qgroup_config() when umounting
462 * filesystem and disabling quota, so we set qgroup_ulist
463 * to be null here to avoid double free.
465 ulist_free(fs_info
->qgroup_ulist
);
466 fs_info
->qgroup_ulist
= NULL
;
469 static int add_qgroup_relation_item(struct btrfs_trans_handle
*trans
,
470 struct btrfs_root
*quota_root
,
474 struct btrfs_path
*path
;
475 struct btrfs_key key
;
477 path
= btrfs_alloc_path();
482 key
.type
= BTRFS_QGROUP_RELATION_KEY
;
485 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
, 0);
487 btrfs_mark_buffer_dirty(path
->nodes
[0]);
489 btrfs_free_path(path
);
493 static int del_qgroup_relation_item(struct btrfs_trans_handle
*trans
,
494 struct btrfs_root
*quota_root
,
498 struct btrfs_path
*path
;
499 struct btrfs_key key
;
501 path
= btrfs_alloc_path();
506 key
.type
= BTRFS_QGROUP_RELATION_KEY
;
509 ret
= btrfs_search_slot(trans
, quota_root
, &key
, path
, -1, 1);
518 ret
= btrfs_del_item(trans
, quota_root
, path
);
520 btrfs_free_path(path
);
524 static int add_qgroup_item(struct btrfs_trans_handle
*trans
,
525 struct btrfs_root
*quota_root
, u64 qgroupid
)
528 struct btrfs_path
*path
;
529 struct btrfs_qgroup_info_item
*qgroup_info
;
530 struct btrfs_qgroup_limit_item
*qgroup_limit
;
531 struct extent_buffer
*leaf
;
532 struct btrfs_key key
;
534 if (btrfs_is_testing(quota_root
->fs_info
))
537 path
= btrfs_alloc_path();
542 key
.type
= BTRFS_QGROUP_INFO_KEY
;
543 key
.offset
= qgroupid
;
546 * Avoid a transaction abort by catching -EEXIST here. In that
547 * case, we proceed by re-initializing the existing structure
551 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
,
552 sizeof(*qgroup_info
));
553 if (ret
&& ret
!= -EEXIST
)
556 leaf
= path
->nodes
[0];
557 qgroup_info
= btrfs_item_ptr(leaf
, path
->slots
[0],
558 struct btrfs_qgroup_info_item
);
559 btrfs_set_qgroup_info_generation(leaf
, qgroup_info
, trans
->transid
);
560 btrfs_set_qgroup_info_rfer(leaf
, qgroup_info
, 0);
561 btrfs_set_qgroup_info_rfer_cmpr(leaf
, qgroup_info
, 0);
562 btrfs_set_qgroup_info_excl(leaf
, qgroup_info
, 0);
563 btrfs_set_qgroup_info_excl_cmpr(leaf
, qgroup_info
, 0);
565 btrfs_mark_buffer_dirty(leaf
);
567 btrfs_release_path(path
);
569 key
.type
= BTRFS_QGROUP_LIMIT_KEY
;
570 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
,
571 sizeof(*qgroup_limit
));
572 if (ret
&& ret
!= -EEXIST
)
575 leaf
= path
->nodes
[0];
576 qgroup_limit
= btrfs_item_ptr(leaf
, path
->slots
[0],
577 struct btrfs_qgroup_limit_item
);
578 btrfs_set_qgroup_limit_flags(leaf
, qgroup_limit
, 0);
579 btrfs_set_qgroup_limit_max_rfer(leaf
, qgroup_limit
, 0);
580 btrfs_set_qgroup_limit_max_excl(leaf
, qgroup_limit
, 0);
581 btrfs_set_qgroup_limit_rsv_rfer(leaf
, qgroup_limit
, 0);
582 btrfs_set_qgroup_limit_rsv_excl(leaf
, qgroup_limit
, 0);
584 btrfs_mark_buffer_dirty(leaf
);
588 btrfs_free_path(path
);
592 static int del_qgroup_item(struct btrfs_trans_handle
*trans
,
593 struct btrfs_root
*quota_root
, u64 qgroupid
)
596 struct btrfs_path
*path
;
597 struct btrfs_key key
;
599 path
= btrfs_alloc_path();
604 key
.type
= BTRFS_QGROUP_INFO_KEY
;
605 key
.offset
= qgroupid
;
606 ret
= btrfs_search_slot(trans
, quota_root
, &key
, path
, -1, 1);
615 ret
= btrfs_del_item(trans
, quota_root
, path
);
619 btrfs_release_path(path
);
621 key
.type
= BTRFS_QGROUP_LIMIT_KEY
;
622 ret
= btrfs_search_slot(trans
, quota_root
, &key
, path
, -1, 1);
631 ret
= btrfs_del_item(trans
, quota_root
, path
);
634 btrfs_free_path(path
);
638 static int update_qgroup_limit_item(struct btrfs_trans_handle
*trans
,
639 struct btrfs_root
*root
,
640 struct btrfs_qgroup
*qgroup
)
642 struct btrfs_path
*path
;
643 struct btrfs_key key
;
644 struct extent_buffer
*l
;
645 struct btrfs_qgroup_limit_item
*qgroup_limit
;
650 key
.type
= BTRFS_QGROUP_LIMIT_KEY
;
651 key
.offset
= qgroup
->qgroupid
;
653 path
= btrfs_alloc_path();
657 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
665 slot
= path
->slots
[0];
666 qgroup_limit
= btrfs_item_ptr(l
, slot
, struct btrfs_qgroup_limit_item
);
667 btrfs_set_qgroup_limit_flags(l
, qgroup_limit
, qgroup
->lim_flags
);
668 btrfs_set_qgroup_limit_max_rfer(l
, qgroup_limit
, qgroup
->max_rfer
);
669 btrfs_set_qgroup_limit_max_excl(l
, qgroup_limit
, qgroup
->max_excl
);
670 btrfs_set_qgroup_limit_rsv_rfer(l
, qgroup_limit
, qgroup
->rsv_rfer
);
671 btrfs_set_qgroup_limit_rsv_excl(l
, qgroup_limit
, qgroup
->rsv_excl
);
673 btrfs_mark_buffer_dirty(l
);
676 btrfs_free_path(path
);
680 static int update_qgroup_info_item(struct btrfs_trans_handle
*trans
,
681 struct btrfs_root
*root
,
682 struct btrfs_qgroup
*qgroup
)
684 struct btrfs_path
*path
;
685 struct btrfs_key key
;
686 struct extent_buffer
*l
;
687 struct btrfs_qgroup_info_item
*qgroup_info
;
691 if (btrfs_is_testing(root
->fs_info
))
695 key
.type
= BTRFS_QGROUP_INFO_KEY
;
696 key
.offset
= qgroup
->qgroupid
;
698 path
= btrfs_alloc_path();
702 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
710 slot
= path
->slots
[0];
711 qgroup_info
= btrfs_item_ptr(l
, slot
, struct btrfs_qgroup_info_item
);
712 btrfs_set_qgroup_info_generation(l
, qgroup_info
, trans
->transid
);
713 btrfs_set_qgroup_info_rfer(l
, qgroup_info
, qgroup
->rfer
);
714 btrfs_set_qgroup_info_rfer_cmpr(l
, qgroup_info
, qgroup
->rfer_cmpr
);
715 btrfs_set_qgroup_info_excl(l
, qgroup_info
, qgroup
->excl
);
716 btrfs_set_qgroup_info_excl_cmpr(l
, qgroup_info
, qgroup
->excl_cmpr
);
718 btrfs_mark_buffer_dirty(l
);
721 btrfs_free_path(path
);
725 static int update_qgroup_status_item(struct btrfs_trans_handle
*trans
,
726 struct btrfs_fs_info
*fs_info
,
727 struct btrfs_root
*root
)
729 struct btrfs_path
*path
;
730 struct btrfs_key key
;
731 struct extent_buffer
*l
;
732 struct btrfs_qgroup_status_item
*ptr
;
737 key
.type
= BTRFS_QGROUP_STATUS_KEY
;
740 path
= btrfs_alloc_path();
744 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
752 slot
= path
->slots
[0];
753 ptr
= btrfs_item_ptr(l
, slot
, struct btrfs_qgroup_status_item
);
754 btrfs_set_qgroup_status_flags(l
, ptr
, fs_info
->qgroup_flags
);
755 btrfs_set_qgroup_status_generation(l
, ptr
, trans
->transid
);
756 btrfs_set_qgroup_status_rescan(l
, ptr
,
757 fs_info
->qgroup_rescan_progress
.objectid
);
759 btrfs_mark_buffer_dirty(l
);
762 btrfs_free_path(path
);
767 * called with qgroup_lock held
769 static int btrfs_clean_quota_tree(struct btrfs_trans_handle
*trans
,
770 struct btrfs_root
*root
)
772 struct btrfs_path
*path
;
773 struct btrfs_key key
;
774 struct extent_buffer
*leaf
= NULL
;
778 path
= btrfs_alloc_path();
782 path
->leave_spinning
= 1;
789 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
792 leaf
= path
->nodes
[0];
793 nr
= btrfs_header_nritems(leaf
);
797 * delete the leaf one by one
798 * since the whole tree is going
802 ret
= btrfs_del_items(trans
, root
, path
, 0, nr
);
806 btrfs_release_path(path
);
810 btrfs_free_path(path
);
814 int btrfs_quota_enable(struct btrfs_trans_handle
*trans
,
815 struct btrfs_fs_info
*fs_info
)
817 struct btrfs_root
*quota_root
;
818 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
819 struct btrfs_path
*path
= NULL
;
820 struct btrfs_qgroup_status_item
*ptr
;
821 struct extent_buffer
*leaf
;
822 struct btrfs_key key
;
823 struct btrfs_key found_key
;
824 struct btrfs_qgroup
*qgroup
= NULL
;
828 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
829 if (fs_info
->quota_root
) {
830 set_bit(BTRFS_FS_QUOTA_ENABLING
, &fs_info
->flags
);
834 fs_info
->qgroup_ulist
= ulist_alloc(GFP_KERNEL
);
835 if (!fs_info
->qgroup_ulist
) {
841 * initially create the quota tree
843 quota_root
= btrfs_create_tree(trans
, fs_info
,
844 BTRFS_QUOTA_TREE_OBJECTID
);
845 if (IS_ERR(quota_root
)) {
846 ret
= PTR_ERR(quota_root
);
850 path
= btrfs_alloc_path();
857 key
.type
= BTRFS_QGROUP_STATUS_KEY
;
860 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
,
865 leaf
= path
->nodes
[0];
866 ptr
= btrfs_item_ptr(leaf
, path
->slots
[0],
867 struct btrfs_qgroup_status_item
);
868 btrfs_set_qgroup_status_generation(leaf
, ptr
, trans
->transid
);
869 btrfs_set_qgroup_status_version(leaf
, ptr
, BTRFS_QGROUP_STATUS_VERSION
);
870 fs_info
->qgroup_flags
= BTRFS_QGROUP_STATUS_FLAG_ON
|
871 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
872 btrfs_set_qgroup_status_flags(leaf
, ptr
, fs_info
->qgroup_flags
);
873 btrfs_set_qgroup_status_rescan(leaf
, ptr
, 0);
875 btrfs_mark_buffer_dirty(leaf
);
878 key
.type
= BTRFS_ROOT_REF_KEY
;
881 btrfs_release_path(path
);
882 ret
= btrfs_search_slot_for_read(tree_root
, &key
, path
, 1, 0);
890 slot
= path
->slots
[0];
891 leaf
= path
->nodes
[0];
892 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
894 if (found_key
.type
== BTRFS_ROOT_REF_KEY
) {
895 ret
= add_qgroup_item(trans
, quota_root
,
900 qgroup
= add_qgroup_rb(fs_info
, found_key
.offset
);
901 if (IS_ERR(qgroup
)) {
902 ret
= PTR_ERR(qgroup
);
906 ret
= btrfs_next_item(tree_root
, path
);
914 btrfs_release_path(path
);
915 ret
= add_qgroup_item(trans
, quota_root
, BTRFS_FS_TREE_OBJECTID
);
919 qgroup
= add_qgroup_rb(fs_info
, BTRFS_FS_TREE_OBJECTID
);
920 if (IS_ERR(qgroup
)) {
921 ret
= PTR_ERR(qgroup
);
924 spin_lock(&fs_info
->qgroup_lock
);
925 fs_info
->quota_root
= quota_root
;
926 set_bit(BTRFS_FS_QUOTA_ENABLING
, &fs_info
->flags
);
927 spin_unlock(&fs_info
->qgroup_lock
);
929 btrfs_free_path(path
);
932 free_extent_buffer(quota_root
->node
);
933 free_extent_buffer(quota_root
->commit_root
);
938 ulist_free(fs_info
->qgroup_ulist
);
939 fs_info
->qgroup_ulist
= NULL
;
941 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
945 int btrfs_quota_disable(struct btrfs_trans_handle
*trans
,
946 struct btrfs_fs_info
*fs_info
)
948 struct btrfs_root
*quota_root
;
951 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
952 if (!fs_info
->quota_root
)
954 clear_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
);
955 btrfs_qgroup_wait_for_completion(fs_info
, false);
956 spin_lock(&fs_info
->qgroup_lock
);
957 quota_root
= fs_info
->quota_root
;
958 fs_info
->quota_root
= NULL
;
959 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_ON
;
960 spin_unlock(&fs_info
->qgroup_lock
);
962 btrfs_free_qgroup_config(fs_info
);
964 ret
= btrfs_clean_quota_tree(trans
, quota_root
);
968 ret
= btrfs_del_root(trans
, fs_info
, "a_root
->root_key
);
972 list_del("a_root
->dirty_list
);
974 btrfs_tree_lock(quota_root
->node
);
975 clean_tree_block(fs_info
, quota_root
->node
);
976 btrfs_tree_unlock(quota_root
->node
);
977 btrfs_free_tree_block(trans
, quota_root
, quota_root
->node
, 0, 1);
979 free_extent_buffer(quota_root
->node
);
980 free_extent_buffer(quota_root
->commit_root
);
983 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
987 static void qgroup_dirty(struct btrfs_fs_info
*fs_info
,
988 struct btrfs_qgroup
*qgroup
)
990 if (list_empty(&qgroup
->dirty
))
991 list_add(&qgroup
->dirty
, &fs_info
->dirty_qgroups
);
994 static void report_reserved_underflow(struct btrfs_fs_info
*fs_info
,
995 struct btrfs_qgroup
*qgroup
,
998 #ifdef CONFIG_BTRFS_DEBUG
999 WARN_ON(qgroup
->reserved
< num_bytes
);
1000 btrfs_debug(fs_info
,
1001 "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
1002 qgroup
->qgroupid
, qgroup
->reserved
, num_bytes
);
1004 qgroup
->reserved
= 0;
1007 * The easy accounting, if we are adding/removing the only ref for an extent
1008 * then this qgroup and all of the parent qgroups get their reference and
1009 * exclusive counts adjusted.
1011 * Caller should hold fs_info->qgroup_lock.
1013 static int __qgroup_excl_accounting(struct btrfs_fs_info
*fs_info
,
1014 struct ulist
*tmp
, u64 ref_root
,
1015 u64 num_bytes
, int sign
)
1017 struct btrfs_qgroup
*qgroup
;
1018 struct btrfs_qgroup_list
*glist
;
1019 struct ulist_node
*unode
;
1020 struct ulist_iterator uiter
;
1023 qgroup
= find_qgroup_rb(fs_info
, ref_root
);
1027 qgroup
->rfer
+= sign
* num_bytes
;
1028 qgroup
->rfer_cmpr
+= sign
* num_bytes
;
1030 WARN_ON(sign
< 0 && qgroup
->excl
< num_bytes
);
1031 qgroup
->excl
+= sign
* num_bytes
;
1032 qgroup
->excl_cmpr
+= sign
* num_bytes
;
1034 trace_qgroup_update_reserve(fs_info
, qgroup
, -(s64
)num_bytes
);
1035 if (qgroup
->reserved
< num_bytes
)
1036 report_reserved_underflow(fs_info
, qgroup
, num_bytes
);
1038 qgroup
->reserved
-= num_bytes
;
1041 qgroup_dirty(fs_info
, qgroup
);
1043 /* Get all of the parent groups that contain this qgroup */
1044 list_for_each_entry(glist
, &qgroup
->groups
, next_group
) {
1045 ret
= ulist_add(tmp
, glist
->group
->qgroupid
,
1046 qgroup_to_aux(glist
->group
), GFP_ATOMIC
);
1051 /* Iterate all of the parents and adjust their reference counts */
1052 ULIST_ITER_INIT(&uiter
);
1053 while ((unode
= ulist_next(tmp
, &uiter
))) {
1054 qgroup
= unode_aux_to_qgroup(unode
);
1055 qgroup
->rfer
+= sign
* num_bytes
;
1056 qgroup
->rfer_cmpr
+= sign
* num_bytes
;
1057 WARN_ON(sign
< 0 && qgroup
->excl
< num_bytes
);
1058 qgroup
->excl
+= sign
* num_bytes
;
1060 trace_qgroup_update_reserve(fs_info
, qgroup
,
1062 if (qgroup
->reserved
< num_bytes
)
1063 report_reserved_underflow(fs_info
, qgroup
,
1066 qgroup
->reserved
-= num_bytes
;
1068 qgroup
->excl_cmpr
+= sign
* num_bytes
;
1069 qgroup_dirty(fs_info
, qgroup
);
1071 /* Add any parents of the parents */
1072 list_for_each_entry(glist
, &qgroup
->groups
, next_group
) {
1073 ret
= ulist_add(tmp
, glist
->group
->qgroupid
,
1074 qgroup_to_aux(glist
->group
), GFP_ATOMIC
);
1086 * Quick path for updating qgroup with only excl refs.
1088 * In that case, just update all parent will be enough.
1089 * Or we needs to do a full rescan.
1090 * Caller should also hold fs_info->qgroup_lock.
1092 * Return 0 for quick update, return >0 for need to full rescan
1093 * and mark INCONSISTENT flag.
1094 * Return < 0 for other error.
1096 static int quick_update_accounting(struct btrfs_fs_info
*fs_info
,
1097 struct ulist
*tmp
, u64 src
, u64 dst
,
1100 struct btrfs_qgroup
*qgroup
;
1104 qgroup
= find_qgroup_rb(fs_info
, src
);
1107 if (qgroup
->excl
== qgroup
->rfer
) {
1109 err
= __qgroup_excl_accounting(fs_info
, tmp
, dst
,
1110 qgroup
->excl
, sign
);
1118 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
1122 int btrfs_add_qgroup_relation(struct btrfs_trans_handle
*trans
,
1123 struct btrfs_fs_info
*fs_info
, u64 src
, u64 dst
)
1125 struct btrfs_root
*quota_root
;
1126 struct btrfs_qgroup
*parent
;
1127 struct btrfs_qgroup
*member
;
1128 struct btrfs_qgroup_list
*list
;
1132 /* Check the level of src and dst first */
1133 if (btrfs_qgroup_level(src
) >= btrfs_qgroup_level(dst
))
1136 tmp
= ulist_alloc(GFP_KERNEL
);
1140 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
1141 quota_root
= fs_info
->quota_root
;
1146 member
= find_qgroup_rb(fs_info
, src
);
1147 parent
= find_qgroup_rb(fs_info
, dst
);
1148 if (!member
|| !parent
) {
1153 /* check if such qgroup relation exist firstly */
1154 list_for_each_entry(list
, &member
->groups
, next_group
) {
1155 if (list
->group
== parent
) {
1161 ret
= add_qgroup_relation_item(trans
, quota_root
, src
, dst
);
1165 ret
= add_qgroup_relation_item(trans
, quota_root
, dst
, src
);
1167 del_qgroup_relation_item(trans
, quota_root
, src
, dst
);
1171 spin_lock(&fs_info
->qgroup_lock
);
1172 ret
= add_relation_rb(fs_info
, src
, dst
);
1174 spin_unlock(&fs_info
->qgroup_lock
);
1177 ret
= quick_update_accounting(fs_info
, tmp
, src
, dst
, 1);
1178 spin_unlock(&fs_info
->qgroup_lock
);
1180 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
1185 static int __del_qgroup_relation(struct btrfs_trans_handle
*trans
,
1186 struct btrfs_fs_info
*fs_info
, u64 src
, u64 dst
)
1188 struct btrfs_root
*quota_root
;
1189 struct btrfs_qgroup
*parent
;
1190 struct btrfs_qgroup
*member
;
1191 struct btrfs_qgroup_list
*list
;
1196 tmp
= ulist_alloc(GFP_KERNEL
);
1200 quota_root
= fs_info
->quota_root
;
1206 member
= find_qgroup_rb(fs_info
, src
);
1207 parent
= find_qgroup_rb(fs_info
, dst
);
1208 if (!member
|| !parent
) {
1213 /* check if such qgroup relation exist firstly */
1214 list_for_each_entry(list
, &member
->groups
, next_group
) {
1215 if (list
->group
== parent
)
1221 ret
= del_qgroup_relation_item(trans
, quota_root
, src
, dst
);
1222 err
= del_qgroup_relation_item(trans
, quota_root
, dst
, src
);
1226 spin_lock(&fs_info
->qgroup_lock
);
1227 del_relation_rb(fs_info
, src
, dst
);
1228 ret
= quick_update_accounting(fs_info
, tmp
, src
, dst
, -1);
1229 spin_unlock(&fs_info
->qgroup_lock
);
1235 int btrfs_del_qgroup_relation(struct btrfs_trans_handle
*trans
,
1236 struct btrfs_fs_info
*fs_info
, u64 src
, u64 dst
)
1240 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
1241 ret
= __del_qgroup_relation(trans
, fs_info
, src
, dst
);
1242 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
1247 int btrfs_create_qgroup(struct btrfs_trans_handle
*trans
,
1248 struct btrfs_fs_info
*fs_info
, u64 qgroupid
)
1250 struct btrfs_root
*quota_root
;
1251 struct btrfs_qgroup
*qgroup
;
1254 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
1255 quota_root
= fs_info
->quota_root
;
1260 qgroup
= find_qgroup_rb(fs_info
, qgroupid
);
1266 ret
= add_qgroup_item(trans
, quota_root
, qgroupid
);
1270 spin_lock(&fs_info
->qgroup_lock
);
1271 qgroup
= add_qgroup_rb(fs_info
, qgroupid
);
1272 spin_unlock(&fs_info
->qgroup_lock
);
1275 ret
= PTR_ERR(qgroup
);
1277 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
1281 int btrfs_remove_qgroup(struct btrfs_trans_handle
*trans
,
1282 struct btrfs_fs_info
*fs_info
, u64 qgroupid
)
1284 struct btrfs_root
*quota_root
;
1285 struct btrfs_qgroup
*qgroup
;
1286 struct btrfs_qgroup_list
*list
;
1289 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
1290 quota_root
= fs_info
->quota_root
;
1296 qgroup
= find_qgroup_rb(fs_info
, qgroupid
);
1301 /* check if there are no children of this qgroup */
1302 if (!list_empty(&qgroup
->members
)) {
1307 ret
= del_qgroup_item(trans
, quota_root
, qgroupid
);
1308 if (ret
&& ret
!= -ENOENT
)
1311 while (!list_empty(&qgroup
->groups
)) {
1312 list
= list_first_entry(&qgroup
->groups
,
1313 struct btrfs_qgroup_list
, next_group
);
1314 ret
= __del_qgroup_relation(trans
, fs_info
,
1316 list
->group
->qgroupid
);
1321 spin_lock(&fs_info
->qgroup_lock
);
1322 del_qgroup_rb(fs_info
, qgroupid
);
1323 spin_unlock(&fs_info
->qgroup_lock
);
1325 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
1329 int btrfs_limit_qgroup(struct btrfs_trans_handle
*trans
,
1330 struct btrfs_fs_info
*fs_info
, u64 qgroupid
,
1331 struct btrfs_qgroup_limit
*limit
)
1333 struct btrfs_root
*quota_root
;
1334 struct btrfs_qgroup
*qgroup
;
1336 /* Sometimes we would want to clear the limit on this qgroup.
1337 * To meet this requirement, we treat the -1 as a special value
1338 * which tell kernel to clear the limit on this qgroup.
1340 const u64 CLEAR_VALUE
= -1;
1342 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
1343 quota_root
= fs_info
->quota_root
;
1349 qgroup
= find_qgroup_rb(fs_info
, qgroupid
);
1355 spin_lock(&fs_info
->qgroup_lock
);
1356 if (limit
->flags
& BTRFS_QGROUP_LIMIT_MAX_RFER
) {
1357 if (limit
->max_rfer
== CLEAR_VALUE
) {
1358 qgroup
->lim_flags
&= ~BTRFS_QGROUP_LIMIT_MAX_RFER
;
1359 limit
->flags
&= ~BTRFS_QGROUP_LIMIT_MAX_RFER
;
1360 qgroup
->max_rfer
= 0;
1362 qgroup
->max_rfer
= limit
->max_rfer
;
1365 if (limit
->flags
& BTRFS_QGROUP_LIMIT_MAX_EXCL
) {
1366 if (limit
->max_excl
== CLEAR_VALUE
) {
1367 qgroup
->lim_flags
&= ~BTRFS_QGROUP_LIMIT_MAX_EXCL
;
1368 limit
->flags
&= ~BTRFS_QGROUP_LIMIT_MAX_EXCL
;
1369 qgroup
->max_excl
= 0;
1371 qgroup
->max_excl
= limit
->max_excl
;
1374 if (limit
->flags
& BTRFS_QGROUP_LIMIT_RSV_RFER
) {
1375 if (limit
->rsv_rfer
== CLEAR_VALUE
) {
1376 qgroup
->lim_flags
&= ~BTRFS_QGROUP_LIMIT_RSV_RFER
;
1377 limit
->flags
&= ~BTRFS_QGROUP_LIMIT_RSV_RFER
;
1378 qgroup
->rsv_rfer
= 0;
1380 qgroup
->rsv_rfer
= limit
->rsv_rfer
;
1383 if (limit
->flags
& BTRFS_QGROUP_LIMIT_RSV_EXCL
) {
1384 if (limit
->rsv_excl
== CLEAR_VALUE
) {
1385 qgroup
->lim_flags
&= ~BTRFS_QGROUP_LIMIT_RSV_EXCL
;
1386 limit
->flags
&= ~BTRFS_QGROUP_LIMIT_RSV_EXCL
;
1387 qgroup
->rsv_excl
= 0;
1389 qgroup
->rsv_excl
= limit
->rsv_excl
;
1392 qgroup
->lim_flags
|= limit
->flags
;
1394 spin_unlock(&fs_info
->qgroup_lock
);
1396 ret
= update_qgroup_limit_item(trans
, quota_root
, qgroup
);
1398 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
1399 btrfs_info(fs_info
, "unable to update quota limit for %llu",
1404 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
1408 int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info
*fs_info
,
1409 struct btrfs_delayed_ref_root
*delayed_refs
,
1410 struct btrfs_qgroup_extent_record
*record
)
1412 struct rb_node
**p
= &delayed_refs
->dirty_extent_root
.rb_node
;
1413 struct rb_node
*parent_node
= NULL
;
1414 struct btrfs_qgroup_extent_record
*entry
;
1415 u64 bytenr
= record
->bytenr
;
1417 assert_spin_locked(&delayed_refs
->lock
);
1418 trace_btrfs_qgroup_trace_extent(fs_info
, record
);
1422 entry
= rb_entry(parent_node
, struct btrfs_qgroup_extent_record
,
1424 if (bytenr
< entry
->bytenr
)
1426 else if (bytenr
> entry
->bytenr
)
1427 p
= &(*p
)->rb_right
;
1432 rb_link_node(&record
->node
, parent_node
, p
);
1433 rb_insert_color(&record
->node
, &delayed_refs
->dirty_extent_root
);
1437 int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info
*fs_info
,
1438 struct btrfs_qgroup_extent_record
*qrecord
)
1440 struct ulist
*old_root
;
1441 u64 bytenr
= qrecord
->bytenr
;
1444 ret
= btrfs_find_all_roots(NULL
, fs_info
, bytenr
, 0, &old_root
, false);
1449 * Here we don't need to get the lock of
1450 * trans->transaction->delayed_refs, since inserted qrecord won't
1451 * be deleted, only qrecord->node may be modified (new qrecord insert)
1453 * So modifying qrecord->old_roots is safe here
1455 qrecord
->old_roots
= old_root
;
1459 int btrfs_qgroup_trace_extent(struct btrfs_trans_handle
*trans
,
1460 struct btrfs_fs_info
*fs_info
, u64 bytenr
, u64 num_bytes
,
1463 struct btrfs_qgroup_extent_record
*record
;
1464 struct btrfs_delayed_ref_root
*delayed_refs
;
1467 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
)
1468 || bytenr
== 0 || num_bytes
== 0)
1470 if (WARN_ON(trans
== NULL
))
1472 record
= kmalloc(sizeof(*record
), gfp_flag
);
1476 delayed_refs
= &trans
->transaction
->delayed_refs
;
1477 record
->bytenr
= bytenr
;
1478 record
->num_bytes
= num_bytes
;
1479 record
->old_roots
= NULL
;
1481 spin_lock(&delayed_refs
->lock
);
1482 ret
= btrfs_qgroup_trace_extent_nolock(fs_info
, delayed_refs
, record
);
1483 spin_unlock(&delayed_refs
->lock
);
1488 return btrfs_qgroup_trace_extent_post(fs_info
, record
);
1491 int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle
*trans
,
1492 struct btrfs_fs_info
*fs_info
,
1493 struct extent_buffer
*eb
)
1495 int nr
= btrfs_header_nritems(eb
);
1496 int i
, extent_type
, ret
;
1497 struct btrfs_key key
;
1498 struct btrfs_file_extent_item
*fi
;
1499 u64 bytenr
, num_bytes
;
1501 /* We can be called directly from walk_up_proc() */
1502 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
1505 for (i
= 0; i
< nr
; i
++) {
1506 btrfs_item_key_to_cpu(eb
, &key
, i
);
1508 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1511 fi
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
1512 /* filter out non qgroup-accountable extents */
1513 extent_type
= btrfs_file_extent_type(eb
, fi
);
1515 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
)
1518 bytenr
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1522 num_bytes
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
1524 ret
= btrfs_qgroup_trace_extent(trans
, fs_info
, bytenr
,
1525 num_bytes
, GFP_NOFS
);
1534 * Walk up the tree from the bottom, freeing leaves and any interior
1535 * nodes which have had all slots visited. If a node (leaf or
1536 * interior) is freed, the node above it will have it's slot
1537 * incremented. The root node will never be freed.
1539 * At the end of this function, we should have a path which has all
1540 * slots incremented to the next position for a search. If we need to
1541 * read a new node it will be NULL and the node above it will have the
1542 * correct slot selected for a later read.
1544 * If we increment the root nodes slot counter past the number of
1545 * elements, 1 is returned to signal completion of the search.
1547 static int adjust_slots_upwards(struct btrfs_path
*path
, int root_level
)
1551 struct extent_buffer
*eb
;
1553 if (root_level
== 0)
1556 while (level
<= root_level
) {
1557 eb
= path
->nodes
[level
];
1558 nr
= btrfs_header_nritems(eb
);
1559 path
->slots
[level
]++;
1560 slot
= path
->slots
[level
];
1561 if (slot
>= nr
|| level
== 0) {
1563 * Don't free the root - we will detect this
1564 * condition after our loop and return a
1565 * positive value for caller to stop walking the tree.
1567 if (level
!= root_level
) {
1568 btrfs_tree_unlock_rw(eb
, path
->locks
[level
]);
1569 path
->locks
[level
] = 0;
1571 free_extent_buffer(eb
);
1572 path
->nodes
[level
] = NULL
;
1573 path
->slots
[level
] = 0;
1577 * We have a valid slot to walk back down
1578 * from. Stop here so caller can process these
1587 eb
= path
->nodes
[root_level
];
1588 if (path
->slots
[root_level
] >= btrfs_header_nritems(eb
))
1594 int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle
*trans
,
1595 struct btrfs_root
*root
,
1596 struct extent_buffer
*root_eb
,
1597 u64 root_gen
, int root_level
)
1599 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1602 struct extent_buffer
*eb
= root_eb
;
1603 struct btrfs_path
*path
= NULL
;
1605 BUG_ON(root_level
< 0 || root_level
>= BTRFS_MAX_LEVEL
);
1606 BUG_ON(root_eb
== NULL
);
1608 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
1611 if (!extent_buffer_uptodate(root_eb
)) {
1612 ret
= btrfs_read_buffer(root_eb
, root_gen
);
1617 if (root_level
== 0) {
1618 ret
= btrfs_qgroup_trace_leaf_items(trans
, fs_info
, root_eb
);
1622 path
= btrfs_alloc_path();
1627 * Walk down the tree. Missing extent blocks are filled in as
1628 * we go. Metadata is accounted every time we read a new
1631 * When we reach a leaf, we account for file extent items in it,
1632 * walk back up the tree (adjusting slot pointers as we go)
1633 * and restart the search process.
1635 extent_buffer_get(root_eb
); /* For path */
1636 path
->nodes
[root_level
] = root_eb
;
1637 path
->slots
[root_level
] = 0;
1638 path
->locks
[root_level
] = 0; /* so release_path doesn't try to unlock */
1641 while (level
>= 0) {
1642 if (path
->nodes
[level
] == NULL
) {
1648 * We need to get child blockptr/gen from parent before
1651 eb
= path
->nodes
[level
+ 1];
1652 parent_slot
= path
->slots
[level
+ 1];
1653 child_bytenr
= btrfs_node_blockptr(eb
, parent_slot
);
1654 child_gen
= btrfs_node_ptr_generation(eb
, parent_slot
);
1656 eb
= read_tree_block(fs_info
, child_bytenr
, child_gen
);
1660 } else if (!extent_buffer_uptodate(eb
)) {
1661 free_extent_buffer(eb
);
1666 path
->nodes
[level
] = eb
;
1667 path
->slots
[level
] = 0;
1669 btrfs_tree_read_lock(eb
);
1670 btrfs_set_lock_blocking_rw(eb
, BTRFS_READ_LOCK
);
1671 path
->locks
[level
] = BTRFS_READ_LOCK_BLOCKING
;
1673 ret
= btrfs_qgroup_trace_extent(trans
, fs_info
,
1682 ret
= btrfs_qgroup_trace_leaf_items(trans
,fs_info
,
1683 path
->nodes
[level
]);
1687 /* Nonzero return here means we completed our search */
1688 ret
= adjust_slots_upwards(path
, root_level
);
1692 /* Restart search with new slots */
1701 btrfs_free_path(path
);
1706 #define UPDATE_NEW 0
1707 #define UPDATE_OLD 1
1709 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1711 static int qgroup_update_refcnt(struct btrfs_fs_info
*fs_info
,
1712 struct ulist
*roots
, struct ulist
*tmp
,
1713 struct ulist
*qgroups
, u64 seq
, int update_old
)
1715 struct ulist_node
*unode
;
1716 struct ulist_iterator uiter
;
1717 struct ulist_node
*tmp_unode
;
1718 struct ulist_iterator tmp_uiter
;
1719 struct btrfs_qgroup
*qg
;
1724 ULIST_ITER_INIT(&uiter
);
1725 while ((unode
= ulist_next(roots
, &uiter
))) {
1726 qg
= find_qgroup_rb(fs_info
, unode
->val
);
1731 ret
= ulist_add(qgroups
, qg
->qgroupid
, qgroup_to_aux(qg
),
1735 ret
= ulist_add(tmp
, qg
->qgroupid
, qgroup_to_aux(qg
), GFP_ATOMIC
);
1738 ULIST_ITER_INIT(&tmp_uiter
);
1739 while ((tmp_unode
= ulist_next(tmp
, &tmp_uiter
))) {
1740 struct btrfs_qgroup_list
*glist
;
1742 qg
= unode_aux_to_qgroup(tmp_unode
);
1744 btrfs_qgroup_update_old_refcnt(qg
, seq
, 1);
1746 btrfs_qgroup_update_new_refcnt(qg
, seq
, 1);
1747 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
1748 ret
= ulist_add(qgroups
, glist
->group
->qgroupid
,
1749 qgroup_to_aux(glist
->group
),
1753 ret
= ulist_add(tmp
, glist
->group
->qgroupid
,
1754 qgroup_to_aux(glist
->group
),
1765 * Update qgroup rfer/excl counters.
1766 * Rfer update is easy, codes can explain themselves.
1768 * Excl update is tricky, the update is split into 2 part.
1769 * Part 1: Possible exclusive <-> sharing detect:
1771 * -------------------------------------
1773 * -------------------------------------
1775 * -------------------------------------
1778 * A: cur_old_roots < nr_old_roots (not exclusive before)
1779 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1780 * B: cur_new_roots < nr_new_roots (not exclusive now)
1781 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
1784 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1785 * *: Definitely not changed. **: Possible unchanged.
1787 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1789 * To make the logic clear, we first use condition A and B to split
1790 * combination into 4 results.
1792 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1793 * only on variant maybe 0.
1795 * Lastly, check result **, since there are 2 variants maybe 0, split them
1797 * But this time we don't need to consider other things, the codes and logic
1798 * is easy to understand now.
1800 static int qgroup_update_counters(struct btrfs_fs_info
*fs_info
,
1801 struct ulist
*qgroups
,
1804 u64 num_bytes
, u64 seq
)
1806 struct ulist_node
*unode
;
1807 struct ulist_iterator uiter
;
1808 struct btrfs_qgroup
*qg
;
1809 u64 cur_new_count
, cur_old_count
;
1811 ULIST_ITER_INIT(&uiter
);
1812 while ((unode
= ulist_next(qgroups
, &uiter
))) {
1815 qg
= unode_aux_to_qgroup(unode
);
1816 cur_old_count
= btrfs_qgroup_get_old_refcnt(qg
, seq
);
1817 cur_new_count
= btrfs_qgroup_get_new_refcnt(qg
, seq
);
1819 trace_qgroup_update_counters(fs_info
, qg
->qgroupid
,
1820 cur_old_count
, cur_new_count
);
1822 /* Rfer update part */
1823 if (cur_old_count
== 0 && cur_new_count
> 0) {
1824 qg
->rfer
+= num_bytes
;
1825 qg
->rfer_cmpr
+= num_bytes
;
1828 if (cur_old_count
> 0 && cur_new_count
== 0) {
1829 qg
->rfer
-= num_bytes
;
1830 qg
->rfer_cmpr
-= num_bytes
;
1834 /* Excl update part */
1835 /* Exclusive/none -> shared case */
1836 if (cur_old_count
== nr_old_roots
&&
1837 cur_new_count
< nr_new_roots
) {
1838 /* Exclusive -> shared */
1839 if (cur_old_count
!= 0) {
1840 qg
->excl
-= num_bytes
;
1841 qg
->excl_cmpr
-= num_bytes
;
1846 /* Shared -> exclusive/none case */
1847 if (cur_old_count
< nr_old_roots
&&
1848 cur_new_count
== nr_new_roots
) {
1849 /* Shared->exclusive */
1850 if (cur_new_count
!= 0) {
1851 qg
->excl
+= num_bytes
;
1852 qg
->excl_cmpr
+= num_bytes
;
1857 /* Exclusive/none -> exclusive/none case */
1858 if (cur_old_count
== nr_old_roots
&&
1859 cur_new_count
== nr_new_roots
) {
1860 if (cur_old_count
== 0) {
1861 /* None -> exclusive/none */
1863 if (cur_new_count
!= 0) {
1864 /* None -> exclusive */
1865 qg
->excl
+= num_bytes
;
1866 qg
->excl_cmpr
+= num_bytes
;
1869 /* None -> none, nothing changed */
1871 /* Exclusive -> exclusive/none */
1873 if (cur_new_count
== 0) {
1874 /* Exclusive -> none */
1875 qg
->excl
-= num_bytes
;
1876 qg
->excl_cmpr
-= num_bytes
;
1879 /* Exclusive -> exclusive, nothing changed */
1884 qgroup_dirty(fs_info
, qg
);
1890 * Check if the @roots potentially is a list of fs tree roots
1892 * Return 0 for definitely not a fs/subvol tree roots ulist
1893 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1896 static int maybe_fs_roots(struct ulist
*roots
)
1898 struct ulist_node
*unode
;
1899 struct ulist_iterator uiter
;
1901 /* Empty one, still possible for fs roots */
1902 if (!roots
|| roots
->nnodes
== 0)
1905 ULIST_ITER_INIT(&uiter
);
1906 unode
= ulist_next(roots
, &uiter
);
1911 * If it contains fs tree roots, then it must belong to fs/subvol
1913 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
1915 return is_fstree(unode
->val
);
1919 btrfs_qgroup_account_extent(struct btrfs_trans_handle
*trans
,
1920 struct btrfs_fs_info
*fs_info
,
1921 u64 bytenr
, u64 num_bytes
,
1922 struct ulist
*old_roots
, struct ulist
*new_roots
)
1924 struct ulist
*qgroups
= NULL
;
1925 struct ulist
*tmp
= NULL
;
1927 u64 nr_new_roots
= 0;
1928 u64 nr_old_roots
= 0;
1931 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
1935 if (!maybe_fs_roots(new_roots
))
1937 nr_new_roots
= new_roots
->nnodes
;
1940 if (!maybe_fs_roots(old_roots
))
1942 nr_old_roots
= old_roots
->nnodes
;
1945 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
1946 if (nr_old_roots
== 0 && nr_new_roots
== 0)
1949 BUG_ON(!fs_info
->quota_root
);
1951 trace_btrfs_qgroup_account_extent(fs_info
, bytenr
, num_bytes
,
1952 nr_old_roots
, nr_new_roots
);
1954 qgroups
= ulist_alloc(GFP_NOFS
);
1959 tmp
= ulist_alloc(GFP_NOFS
);
1965 mutex_lock(&fs_info
->qgroup_rescan_lock
);
1966 if (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) {
1967 if (fs_info
->qgroup_rescan_progress
.objectid
<= bytenr
) {
1968 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
1973 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
1975 spin_lock(&fs_info
->qgroup_lock
);
1976 seq
= fs_info
->qgroup_seq
;
1978 /* Update old refcnts using old_roots */
1979 ret
= qgroup_update_refcnt(fs_info
, old_roots
, tmp
, qgroups
, seq
,
1984 /* Update new refcnts using new_roots */
1985 ret
= qgroup_update_refcnt(fs_info
, new_roots
, tmp
, qgroups
, seq
,
1990 qgroup_update_counters(fs_info
, qgroups
, nr_old_roots
, nr_new_roots
,
1994 * Bump qgroup_seq to avoid seq overlap
1996 fs_info
->qgroup_seq
+= max(nr_old_roots
, nr_new_roots
) + 1;
1998 spin_unlock(&fs_info
->qgroup_lock
);
2001 ulist_free(qgroups
);
2002 ulist_free(old_roots
);
2003 ulist_free(new_roots
);
2007 int btrfs_qgroup_account_extents(struct btrfs_trans_handle
*trans
,
2008 struct btrfs_fs_info
*fs_info
)
2010 struct btrfs_qgroup_extent_record
*record
;
2011 struct btrfs_delayed_ref_root
*delayed_refs
;
2012 struct ulist
*new_roots
= NULL
;
2013 struct rb_node
*node
;
2017 delayed_refs
= &trans
->transaction
->delayed_refs
;
2018 qgroup_to_skip
= delayed_refs
->qgroup_to_skip
;
2019 while ((node
= rb_first(&delayed_refs
->dirty_extent_root
))) {
2020 record
= rb_entry(node
, struct btrfs_qgroup_extent_record
,
2023 trace_btrfs_qgroup_account_extents(fs_info
, record
);
2027 * Old roots should be searched when inserting qgroup
2030 if (WARN_ON(!record
->old_roots
)) {
2031 /* Search commit root to find old_roots */
2032 ret
= btrfs_find_all_roots(NULL
, fs_info
,
2034 &record
->old_roots
, false);
2040 * Use SEQ_LAST as time_seq to do special search, which
2041 * doesn't lock tree or delayed_refs and search current
2042 * root. It's safe inside commit_transaction().
2044 ret
= btrfs_find_all_roots(trans
, fs_info
,
2045 record
->bytenr
, SEQ_LAST
, &new_roots
, false);
2048 if (qgroup_to_skip
) {
2049 ulist_del(new_roots
, qgroup_to_skip
, 0);
2050 ulist_del(record
->old_roots
, qgroup_to_skip
,
2053 ret
= btrfs_qgroup_account_extent(trans
, fs_info
,
2054 record
->bytenr
, record
->num_bytes
,
2055 record
->old_roots
, new_roots
);
2056 record
->old_roots
= NULL
;
2060 ulist_free(record
->old_roots
);
2061 ulist_free(new_roots
);
2063 rb_erase(node
, &delayed_refs
->dirty_extent_root
);
2071 * called from commit_transaction. Writes all changed qgroups to disk.
2073 int btrfs_run_qgroups(struct btrfs_trans_handle
*trans
,
2074 struct btrfs_fs_info
*fs_info
)
2076 struct btrfs_root
*quota_root
= fs_info
->quota_root
;
2078 int start_rescan_worker
= 0;
2083 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
) &&
2084 test_bit(BTRFS_FS_QUOTA_ENABLING
, &fs_info
->flags
))
2085 start_rescan_worker
= 1;
2087 if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING
, &fs_info
->flags
))
2088 set_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
);
2090 spin_lock(&fs_info
->qgroup_lock
);
2091 while (!list_empty(&fs_info
->dirty_qgroups
)) {
2092 struct btrfs_qgroup
*qgroup
;
2093 qgroup
= list_first_entry(&fs_info
->dirty_qgroups
,
2094 struct btrfs_qgroup
, dirty
);
2095 list_del_init(&qgroup
->dirty
);
2096 spin_unlock(&fs_info
->qgroup_lock
);
2097 ret
= update_qgroup_info_item(trans
, quota_root
, qgroup
);
2099 fs_info
->qgroup_flags
|=
2100 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
2101 ret
= update_qgroup_limit_item(trans
, quota_root
, qgroup
);
2103 fs_info
->qgroup_flags
|=
2104 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
2105 spin_lock(&fs_info
->qgroup_lock
);
2107 if (test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
2108 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_ON
;
2110 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_ON
;
2111 spin_unlock(&fs_info
->qgroup_lock
);
2113 ret
= update_qgroup_status_item(trans
, fs_info
, quota_root
);
2115 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
2117 if (!ret
&& start_rescan_worker
) {
2118 ret
= qgroup_rescan_init(fs_info
, 0, 1);
2120 qgroup_rescan_zero_tracking(fs_info
);
2121 btrfs_queue_work(fs_info
->qgroup_rescan_workers
,
2122 &fs_info
->qgroup_rescan_work
);
2133 * Copy the accounting information between qgroups. This is necessary
2134 * when a snapshot or a subvolume is created. Throwing an error will
2135 * cause a transaction abort so we take extra care here to only error
2136 * when a readonly fs is a reasonable outcome.
2138 int btrfs_qgroup_inherit(struct btrfs_trans_handle
*trans
,
2139 struct btrfs_fs_info
*fs_info
, u64 srcid
, u64 objectid
,
2140 struct btrfs_qgroup_inherit
*inherit
)
2145 struct btrfs_root
*quota_root
= fs_info
->quota_root
;
2146 struct btrfs_qgroup
*srcgroup
;
2147 struct btrfs_qgroup
*dstgroup
;
2151 mutex_lock(&fs_info
->qgroup_ioctl_lock
);
2152 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
))
2161 i_qgroups
= (u64
*)(inherit
+ 1);
2162 nums
= inherit
->num_qgroups
+ 2 * inherit
->num_ref_copies
+
2163 2 * inherit
->num_excl_copies
;
2164 for (i
= 0; i
< nums
; ++i
) {
2165 srcgroup
= find_qgroup_rb(fs_info
, *i_qgroups
);
2168 * Zero out invalid groups so we can ignore
2172 ((srcgroup
->qgroupid
>> 48) <= (objectid
>> 48)))
2180 * create a tracking group for the subvol itself
2182 ret
= add_qgroup_item(trans
, quota_root
, objectid
);
2187 struct btrfs_root
*srcroot
;
2188 struct btrfs_key srckey
;
2190 srckey
.objectid
= srcid
;
2191 srckey
.type
= BTRFS_ROOT_ITEM_KEY
;
2192 srckey
.offset
= (u64
)-1;
2193 srcroot
= btrfs_read_fs_root_no_name(fs_info
, &srckey
);
2194 if (IS_ERR(srcroot
)) {
2195 ret
= PTR_ERR(srcroot
);
2199 level_size
= fs_info
->nodesize
;
2203 * add qgroup to all inherited groups
2206 i_qgroups
= (u64
*)(inherit
+ 1);
2207 for (i
= 0; i
< inherit
->num_qgroups
; ++i
, ++i_qgroups
) {
2208 if (*i_qgroups
== 0)
2210 ret
= add_qgroup_relation_item(trans
, quota_root
,
2211 objectid
, *i_qgroups
);
2212 if (ret
&& ret
!= -EEXIST
)
2214 ret
= add_qgroup_relation_item(trans
, quota_root
,
2215 *i_qgroups
, objectid
);
2216 if (ret
&& ret
!= -EEXIST
)
2223 spin_lock(&fs_info
->qgroup_lock
);
2225 dstgroup
= add_qgroup_rb(fs_info
, objectid
);
2226 if (IS_ERR(dstgroup
)) {
2227 ret
= PTR_ERR(dstgroup
);
2231 if (inherit
&& inherit
->flags
& BTRFS_QGROUP_INHERIT_SET_LIMITS
) {
2232 dstgroup
->lim_flags
= inherit
->lim
.flags
;
2233 dstgroup
->max_rfer
= inherit
->lim
.max_rfer
;
2234 dstgroup
->max_excl
= inherit
->lim
.max_excl
;
2235 dstgroup
->rsv_rfer
= inherit
->lim
.rsv_rfer
;
2236 dstgroup
->rsv_excl
= inherit
->lim
.rsv_excl
;
2238 ret
= update_qgroup_limit_item(trans
, quota_root
, dstgroup
);
2240 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
2242 "unable to update quota limit for %llu",
2243 dstgroup
->qgroupid
);
2249 srcgroup
= find_qgroup_rb(fs_info
, srcid
);
2254 * We call inherit after we clone the root in order to make sure
2255 * our counts don't go crazy, so at this point the only
2256 * difference between the two roots should be the root node.
2258 dstgroup
->rfer
= srcgroup
->rfer
;
2259 dstgroup
->rfer_cmpr
= srcgroup
->rfer_cmpr
;
2260 dstgroup
->excl
= level_size
;
2261 dstgroup
->excl_cmpr
= level_size
;
2262 srcgroup
->excl
= level_size
;
2263 srcgroup
->excl_cmpr
= level_size
;
2265 /* inherit the limit info */
2266 dstgroup
->lim_flags
= srcgroup
->lim_flags
;
2267 dstgroup
->max_rfer
= srcgroup
->max_rfer
;
2268 dstgroup
->max_excl
= srcgroup
->max_excl
;
2269 dstgroup
->rsv_rfer
= srcgroup
->rsv_rfer
;
2270 dstgroup
->rsv_excl
= srcgroup
->rsv_excl
;
2272 qgroup_dirty(fs_info
, dstgroup
);
2273 qgroup_dirty(fs_info
, srcgroup
);
2279 i_qgroups
= (u64
*)(inherit
+ 1);
2280 for (i
= 0; i
< inherit
->num_qgroups
; ++i
) {
2282 ret
= add_relation_rb(fs_info
, objectid
, *i_qgroups
);
2289 for (i
= 0; i
< inherit
->num_ref_copies
; ++i
, i_qgroups
+= 2) {
2290 struct btrfs_qgroup
*src
;
2291 struct btrfs_qgroup
*dst
;
2293 if (!i_qgroups
[0] || !i_qgroups
[1])
2296 src
= find_qgroup_rb(fs_info
, i_qgroups
[0]);
2297 dst
= find_qgroup_rb(fs_info
, i_qgroups
[1]);
2304 dst
->rfer
= src
->rfer
- level_size
;
2305 dst
->rfer_cmpr
= src
->rfer_cmpr
- level_size
;
2307 for (i
= 0; i
< inherit
->num_excl_copies
; ++i
, i_qgroups
+= 2) {
2308 struct btrfs_qgroup
*src
;
2309 struct btrfs_qgroup
*dst
;
2311 if (!i_qgroups
[0] || !i_qgroups
[1])
2314 src
= find_qgroup_rb(fs_info
, i_qgroups
[0]);
2315 dst
= find_qgroup_rb(fs_info
, i_qgroups
[1]);
2322 dst
->excl
= src
->excl
+ level_size
;
2323 dst
->excl_cmpr
= src
->excl_cmpr
+ level_size
;
2327 spin_unlock(&fs_info
->qgroup_lock
);
2329 mutex_unlock(&fs_info
->qgroup_ioctl_lock
);
2333 static bool qgroup_check_limits(const struct btrfs_qgroup
*qg
, u64 num_bytes
)
2335 if ((qg
->lim_flags
& BTRFS_QGROUP_LIMIT_MAX_RFER
) &&
2336 qg
->reserved
+ (s64
)qg
->rfer
+ num_bytes
> qg
->max_rfer
)
2339 if ((qg
->lim_flags
& BTRFS_QGROUP_LIMIT_MAX_EXCL
) &&
2340 qg
->reserved
+ (s64
)qg
->excl
+ num_bytes
> qg
->max_excl
)
2346 static int qgroup_reserve(struct btrfs_root
*root
, u64 num_bytes
, bool enforce
)
2348 struct btrfs_root
*quota_root
;
2349 struct btrfs_qgroup
*qgroup
;
2350 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2351 u64 ref_root
= root
->root_key
.objectid
;
2354 struct ulist_node
*unode
;
2355 struct ulist_iterator uiter
;
2357 if (!is_fstree(ref_root
))
2363 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE
, &fs_info
->flags
) &&
2364 capable(CAP_SYS_RESOURCE
))
2368 spin_lock(&fs_info
->qgroup_lock
);
2369 quota_root
= fs_info
->quota_root
;
2373 qgroup
= find_qgroup_rb(fs_info
, ref_root
);
2378 * in a first step, we check all affected qgroups if any limits would
2381 ulist_reinit(fs_info
->qgroup_ulist
);
2382 ret
= ulist_add(fs_info
->qgroup_ulist
, qgroup
->qgroupid
,
2383 (uintptr_t)qgroup
, GFP_ATOMIC
);
2386 ULIST_ITER_INIT(&uiter
);
2387 while ((unode
= ulist_next(fs_info
->qgroup_ulist
, &uiter
))) {
2388 struct btrfs_qgroup
*qg
;
2389 struct btrfs_qgroup_list
*glist
;
2391 qg
= unode_aux_to_qgroup(unode
);
2393 if (enforce
&& !qgroup_check_limits(qg
, num_bytes
)) {
2395 * Commit the tree and retry, since we may have
2396 * deletions which would free up space.
2398 if (!retried
&& qg
->reserved
> 0) {
2399 struct btrfs_trans_handle
*trans
;
2401 spin_unlock(&fs_info
->qgroup_lock
);
2402 ret
= btrfs_start_delalloc_inodes(root
, 0);
2405 btrfs_wait_ordered_extents(root
, U64_MAX
, 0, (u64
)-1);
2406 trans
= btrfs_join_transaction(root
);
2408 return PTR_ERR(trans
);
2409 ret
= btrfs_commit_transaction(trans
);
2419 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
2420 ret
= ulist_add(fs_info
->qgroup_ulist
,
2421 glist
->group
->qgroupid
,
2422 (uintptr_t)glist
->group
, GFP_ATOMIC
);
2429 * no limits exceeded, now record the reservation into all qgroups
2431 ULIST_ITER_INIT(&uiter
);
2432 while ((unode
= ulist_next(fs_info
->qgroup_ulist
, &uiter
))) {
2433 struct btrfs_qgroup
*qg
;
2435 qg
= unode_aux_to_qgroup(unode
);
2437 trace_qgroup_update_reserve(fs_info
, qg
, num_bytes
);
2438 qg
->reserved
+= num_bytes
;
2442 spin_unlock(&fs_info
->qgroup_lock
);
2446 void btrfs_qgroup_free_refroot(struct btrfs_fs_info
*fs_info
,
2447 u64 ref_root
, u64 num_bytes
)
2449 struct btrfs_root
*quota_root
;
2450 struct btrfs_qgroup
*qgroup
;
2451 struct ulist_node
*unode
;
2452 struct ulist_iterator uiter
;
2455 if (!is_fstree(ref_root
))
2461 spin_lock(&fs_info
->qgroup_lock
);
2463 quota_root
= fs_info
->quota_root
;
2467 qgroup
= find_qgroup_rb(fs_info
, ref_root
);
2471 ulist_reinit(fs_info
->qgroup_ulist
);
2472 ret
= ulist_add(fs_info
->qgroup_ulist
, qgroup
->qgroupid
,
2473 (uintptr_t)qgroup
, GFP_ATOMIC
);
2476 ULIST_ITER_INIT(&uiter
);
2477 while ((unode
= ulist_next(fs_info
->qgroup_ulist
, &uiter
))) {
2478 struct btrfs_qgroup
*qg
;
2479 struct btrfs_qgroup_list
*glist
;
2481 qg
= unode_aux_to_qgroup(unode
);
2483 trace_qgroup_update_reserve(fs_info
, qg
, -(s64
)num_bytes
);
2484 if (qg
->reserved
< num_bytes
)
2485 report_reserved_underflow(fs_info
, qg
, num_bytes
);
2487 qg
->reserved
-= num_bytes
;
2489 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
2490 ret
= ulist_add(fs_info
->qgroup_ulist
,
2491 glist
->group
->qgroupid
,
2492 (uintptr_t)glist
->group
, GFP_ATOMIC
);
2499 spin_unlock(&fs_info
->qgroup_lock
);
2503 * returns < 0 on error, 0 when more leafs are to be scanned.
2504 * returns 1 when done.
2507 qgroup_rescan_leaf(struct btrfs_fs_info
*fs_info
, struct btrfs_path
*path
,
2508 struct btrfs_trans_handle
*trans
)
2510 struct btrfs_key found
;
2511 struct extent_buffer
*scratch_leaf
= NULL
;
2512 struct ulist
*roots
= NULL
;
2513 struct seq_list tree_mod_seq_elem
= SEQ_LIST_INIT(tree_mod_seq_elem
);
2518 mutex_lock(&fs_info
->qgroup_rescan_lock
);
2519 ret
= btrfs_search_slot_for_read(fs_info
->extent_root
,
2520 &fs_info
->qgroup_rescan_progress
,
2523 btrfs_debug(fs_info
,
2524 "current progress key (%llu %u %llu), search_slot ret %d",
2525 fs_info
->qgroup_rescan_progress
.objectid
,
2526 fs_info
->qgroup_rescan_progress
.type
,
2527 fs_info
->qgroup_rescan_progress
.offset
, ret
);
2531 * The rescan is about to end, we will not be scanning any
2532 * further blocks. We cannot unset the RESCAN flag here, because
2533 * we want to commit the transaction if everything went well.
2534 * To make the live accounting work in this phase, we set our
2535 * scan progress pointer such that every real extent objectid
2538 fs_info
->qgroup_rescan_progress
.objectid
= (u64
)-1;
2539 btrfs_release_path(path
);
2540 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2544 btrfs_item_key_to_cpu(path
->nodes
[0], &found
,
2545 btrfs_header_nritems(path
->nodes
[0]) - 1);
2546 fs_info
->qgroup_rescan_progress
.objectid
= found
.objectid
+ 1;
2548 btrfs_get_tree_mod_seq(fs_info
, &tree_mod_seq_elem
);
2549 scratch_leaf
= btrfs_clone_extent_buffer(path
->nodes
[0]);
2550 if (!scratch_leaf
) {
2552 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2555 extent_buffer_get(scratch_leaf
);
2556 btrfs_tree_read_lock(scratch_leaf
);
2557 btrfs_set_lock_blocking_rw(scratch_leaf
, BTRFS_READ_LOCK
);
2558 slot
= path
->slots
[0];
2559 btrfs_release_path(path
);
2560 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2562 for (; slot
< btrfs_header_nritems(scratch_leaf
); ++slot
) {
2563 btrfs_item_key_to_cpu(scratch_leaf
, &found
, slot
);
2564 if (found
.type
!= BTRFS_EXTENT_ITEM_KEY
&&
2565 found
.type
!= BTRFS_METADATA_ITEM_KEY
)
2567 if (found
.type
== BTRFS_METADATA_ITEM_KEY
)
2568 num_bytes
= fs_info
->nodesize
;
2570 num_bytes
= found
.offset
;
2572 ret
= btrfs_find_all_roots(NULL
, fs_info
, found
.objectid
, 0,
2576 /* For rescan, just pass old_roots as NULL */
2577 ret
= btrfs_qgroup_account_extent(trans
, fs_info
,
2578 found
.objectid
, num_bytes
, NULL
, roots
);
2584 btrfs_tree_read_unlock_blocking(scratch_leaf
);
2585 free_extent_buffer(scratch_leaf
);
2587 btrfs_put_tree_mod_seq(fs_info
, &tree_mod_seq_elem
);
2592 static void btrfs_qgroup_rescan_worker(struct btrfs_work
*work
)
2594 struct btrfs_fs_info
*fs_info
= container_of(work
, struct btrfs_fs_info
,
2595 qgroup_rescan_work
);
2596 struct btrfs_path
*path
;
2597 struct btrfs_trans_handle
*trans
= NULL
;
2601 path
= btrfs_alloc_path();
2606 while (!err
&& !btrfs_fs_closing(fs_info
)) {
2607 trans
= btrfs_start_transaction(fs_info
->fs_root
, 0);
2608 if (IS_ERR(trans
)) {
2609 err
= PTR_ERR(trans
);
2612 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
)) {
2615 err
= qgroup_rescan_leaf(fs_info
, path
, trans
);
2618 btrfs_commit_transaction(trans
);
2620 btrfs_end_transaction(trans
);
2624 btrfs_free_path(path
);
2626 mutex_lock(&fs_info
->qgroup_rescan_lock
);
2627 if (!btrfs_fs_closing(fs_info
))
2628 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN
;
2631 fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
) {
2632 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
2633 } else if (err
< 0) {
2634 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
2636 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2639 * only update status, since the previous part has already updated the
2642 trans
= btrfs_start_transaction(fs_info
->quota_root
, 1);
2643 if (IS_ERR(trans
)) {
2644 err
= PTR_ERR(trans
);
2646 "fail to start transaction for status update: %d",
2650 ret
= update_qgroup_status_item(trans
, fs_info
, fs_info
->quota_root
);
2653 btrfs_err(fs_info
, "fail to update qgroup status: %d", err
);
2655 btrfs_end_transaction(trans
);
2657 if (btrfs_fs_closing(fs_info
)) {
2658 btrfs_info(fs_info
, "qgroup scan paused");
2659 } else if (err
>= 0) {
2660 btrfs_info(fs_info
, "qgroup scan completed%s",
2661 err
> 0 ? " (inconsistency flag cleared)" : "");
2663 btrfs_err(fs_info
, "qgroup scan failed with %d", err
);
2667 mutex_lock(&fs_info
->qgroup_rescan_lock
);
2668 fs_info
->qgroup_rescan_running
= false;
2669 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2670 complete_all(&fs_info
->qgroup_rescan_completion
);
2674 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2675 * memory required for the rescan context.
2678 qgroup_rescan_init(struct btrfs_fs_info
*fs_info
, u64 progress_objectid
,
2684 (!(fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
) ||
2685 !(fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_ON
))) {
2690 mutex_lock(&fs_info
->qgroup_rescan_lock
);
2691 spin_lock(&fs_info
->qgroup_lock
);
2694 if (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
)
2696 else if (!(fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_ON
))
2700 spin_unlock(&fs_info
->qgroup_lock
);
2701 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2704 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_RESCAN
;
2707 memset(&fs_info
->qgroup_rescan_progress
, 0,
2708 sizeof(fs_info
->qgroup_rescan_progress
));
2709 fs_info
->qgroup_rescan_progress
.objectid
= progress_objectid
;
2710 init_completion(&fs_info
->qgroup_rescan_completion
);
2711 fs_info
->qgroup_rescan_running
= true;
2713 spin_unlock(&fs_info
->qgroup_lock
);
2714 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2716 memset(&fs_info
->qgroup_rescan_work
, 0,
2717 sizeof(fs_info
->qgroup_rescan_work
));
2718 btrfs_init_work(&fs_info
->qgroup_rescan_work
,
2719 btrfs_qgroup_rescan_helper
,
2720 btrfs_qgroup_rescan_worker
, NULL
, NULL
);
2724 btrfs_info(fs_info
, "qgroup_rescan_init failed with %d", ret
);
2732 qgroup_rescan_zero_tracking(struct btrfs_fs_info
*fs_info
)
2735 struct btrfs_qgroup
*qgroup
;
2737 spin_lock(&fs_info
->qgroup_lock
);
2738 /* clear all current qgroup tracking information */
2739 for (n
= rb_first(&fs_info
->qgroup_tree
); n
; n
= rb_next(n
)) {
2740 qgroup
= rb_entry(n
, struct btrfs_qgroup
, node
);
2742 qgroup
->rfer_cmpr
= 0;
2744 qgroup
->excl_cmpr
= 0;
2746 spin_unlock(&fs_info
->qgroup_lock
);
2750 btrfs_qgroup_rescan(struct btrfs_fs_info
*fs_info
)
2753 struct btrfs_trans_handle
*trans
;
2755 ret
= qgroup_rescan_init(fs_info
, 0, 1);
2760 * We have set the rescan_progress to 0, which means no more
2761 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2762 * However, btrfs_qgroup_account_ref may be right after its call
2763 * to btrfs_find_all_roots, in which case it would still do the
2765 * To solve this, we're committing the transaction, which will
2766 * ensure we run all delayed refs and only after that, we are
2767 * going to clear all tracking information for a clean start.
2770 trans
= btrfs_join_transaction(fs_info
->fs_root
);
2771 if (IS_ERR(trans
)) {
2772 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN
;
2773 return PTR_ERR(trans
);
2775 ret
= btrfs_commit_transaction(trans
);
2777 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN
;
2781 qgroup_rescan_zero_tracking(fs_info
);
2783 btrfs_queue_work(fs_info
->qgroup_rescan_workers
,
2784 &fs_info
->qgroup_rescan_work
);
2789 int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info
*fs_info
,
2795 mutex_lock(&fs_info
->qgroup_rescan_lock
);
2796 spin_lock(&fs_info
->qgroup_lock
);
2797 running
= fs_info
->qgroup_rescan_running
;
2798 spin_unlock(&fs_info
->qgroup_lock
);
2799 mutex_unlock(&fs_info
->qgroup_rescan_lock
);
2805 ret
= wait_for_completion_interruptible(
2806 &fs_info
->qgroup_rescan_completion
);
2808 wait_for_completion(&fs_info
->qgroup_rescan_completion
);
2814 * this is only called from open_ctree where we're still single threaded, thus
2815 * locking is omitted here.
2818 btrfs_qgroup_rescan_resume(struct btrfs_fs_info
*fs_info
)
2820 if (fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_RESCAN
)
2821 btrfs_queue_work(fs_info
->qgroup_rescan_workers
,
2822 &fs_info
->qgroup_rescan_work
);
2826 * Reserve qgroup space for range [start, start + len).
2828 * This function will either reserve space from related qgroups or doing
2829 * nothing if the range is already reserved.
2831 * Return 0 for successful reserve
2832 * Return <0 for error (including -EQUOT)
2834 * NOTE: this function may sleep for memory allocation.
2835 * if btrfs_qgroup_reserve_data() is called multiple times with
2836 * same @reserved, caller must ensure when error happens it's OK
2837 * to free *ALL* reserved space.
2839 int btrfs_qgroup_reserve_data(struct inode
*inode
,
2840 struct extent_changeset
**reserved_ret
, u64 start
,
2843 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2844 struct ulist_node
*unode
;
2845 struct ulist_iterator uiter
;
2846 struct extent_changeset
*reserved
;
2851 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &root
->fs_info
->flags
) ||
2852 !is_fstree(root
->objectid
) || len
== 0)
2855 /* @reserved parameter is mandatory for qgroup */
2856 if (WARN_ON(!reserved_ret
))
2858 if (!*reserved_ret
) {
2859 *reserved_ret
= extent_changeset_alloc();
2863 reserved
= *reserved_ret
;
2864 /* Record already reserved space */
2865 orig_reserved
= reserved
->bytes_changed
;
2866 ret
= set_record_extent_bits(&BTRFS_I(inode
)->io_tree
, start
,
2867 start
+ len
-1, EXTENT_QGROUP_RESERVED
, reserved
);
2869 /* Newly reserved space */
2870 to_reserve
= reserved
->bytes_changed
- orig_reserved
;
2871 trace_btrfs_qgroup_reserve_data(inode
, start
, len
,
2872 to_reserve
, QGROUP_RESERVE
);
2875 ret
= qgroup_reserve(root
, to_reserve
, true);
2882 /* cleanup *ALL* already reserved ranges */
2883 ULIST_ITER_INIT(&uiter
);
2884 while ((unode
= ulist_next(&reserved
->range_changed
, &uiter
)))
2885 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, unode
->val
,
2886 unode
->aux
, EXTENT_QGROUP_RESERVED
, 0, 0, NULL
,
2888 extent_changeset_release(reserved
);
2892 /* Free ranges specified by @reserved, normally in error path */
2893 static int qgroup_free_reserved_data(struct inode
*inode
,
2894 struct extent_changeset
*reserved
, u64 start
, u64 len
)
2896 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2897 struct ulist_node
*unode
;
2898 struct ulist_iterator uiter
;
2899 struct extent_changeset changeset
;
2903 extent_changeset_init(&changeset
);
2904 len
= round_up(start
+ len
, root
->fs_info
->sectorsize
);
2905 start
= round_down(start
, root
->fs_info
->sectorsize
);
2907 ULIST_ITER_INIT(&uiter
);
2908 while ((unode
= ulist_next(&reserved
->range_changed
, &uiter
))) {
2909 u64 range_start
= unode
->val
;
2910 /* unode->aux is the inclusive end */
2911 u64 range_len
= unode
->aux
- range_start
+ 1;
2915 extent_changeset_release(&changeset
);
2917 /* Only free range in range [start, start + len) */
2918 if (range_start
>= start
+ len
||
2919 range_start
+ range_len
<= start
)
2921 free_start
= max(range_start
, start
);
2922 free_len
= min(start
+ len
, range_start
+ range_len
) -
2925 * TODO: To also modify reserved->ranges_reserved to reflect
2928 * However as long as we free qgroup reserved according to
2929 * EXTENT_QGROUP_RESERVED, we won't double free.
2930 * So not need to rush.
2932 ret
= clear_record_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
2933 free_start
, free_start
+ free_len
- 1,
2934 EXTENT_QGROUP_RESERVED
, &changeset
);
2937 freed
+= changeset
.bytes_changed
;
2939 btrfs_qgroup_free_refroot(root
->fs_info
, root
->objectid
, freed
);
2942 extent_changeset_release(&changeset
);
2946 static int __btrfs_qgroup_release_data(struct inode
*inode
,
2947 struct extent_changeset
*reserved
, u64 start
, u64 len
,
2950 struct extent_changeset changeset
;
2951 int trace_op
= QGROUP_RELEASE
;
2954 /* In release case, we shouldn't have @reserved */
2955 WARN_ON(!free
&& reserved
);
2956 if (free
&& reserved
)
2957 return qgroup_free_reserved_data(inode
, reserved
, start
, len
);
2958 extent_changeset_init(&changeset
);
2959 ret
= clear_record_extent_bits(&BTRFS_I(inode
)->io_tree
, start
,
2960 start
+ len
-1, EXTENT_QGROUP_RESERVED
, &changeset
);
2965 trace_op
= QGROUP_FREE
;
2966 trace_btrfs_qgroup_release_data(inode
, start
, len
,
2967 changeset
.bytes_changed
, trace_op
);
2969 btrfs_qgroup_free_refroot(BTRFS_I(inode
)->root
->fs_info
,
2970 BTRFS_I(inode
)->root
->objectid
,
2971 changeset
.bytes_changed
);
2972 ret
= changeset
.bytes_changed
;
2974 extent_changeset_release(&changeset
);
2979 * Free a reserved space range from io_tree and related qgroups
2981 * Should be called when a range of pages get invalidated before reaching disk.
2982 * Or for error cleanup case.
2983 * if @reserved is given, only reserved range in [@start, @start + @len) will
2986 * For data written to disk, use btrfs_qgroup_release_data().
2988 * NOTE: This function may sleep for memory allocation.
2990 int btrfs_qgroup_free_data(struct inode
*inode
,
2991 struct extent_changeset
*reserved
, u64 start
, u64 len
)
2993 return __btrfs_qgroup_release_data(inode
, reserved
, start
, len
, 1);
2997 * Release a reserved space range from io_tree only.
2999 * Should be called when a range of pages get written to disk and corresponding
3000 * FILE_EXTENT is inserted into corresponding root.
3002 * Since new qgroup accounting framework will only update qgroup numbers at
3003 * commit_transaction() time, its reserved space shouldn't be freed from
3006 * But we should release the range from io_tree, to allow further write to be
3009 * NOTE: This function may sleep for memory allocation.
3011 int btrfs_qgroup_release_data(struct inode
*inode
, u64 start
, u64 len
)
3013 return __btrfs_qgroup_release_data(inode
, NULL
, start
, len
, 0);
3016 int btrfs_qgroup_reserve_meta(struct btrfs_root
*root
, int num_bytes
,
3019 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3022 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
) ||
3023 !is_fstree(root
->objectid
) || num_bytes
== 0)
3026 BUG_ON(num_bytes
!= round_down(num_bytes
, fs_info
->nodesize
));
3027 trace_qgroup_meta_reserve(root
, (s64
)num_bytes
);
3028 ret
= qgroup_reserve(root
, num_bytes
, enforce
);
3031 atomic64_add(num_bytes
, &root
->qgroup_meta_rsv
);
3035 void btrfs_qgroup_free_meta_all(struct btrfs_root
*root
)
3037 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3040 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
) ||
3041 !is_fstree(root
->objectid
))
3044 reserved
= atomic64_xchg(&root
->qgroup_meta_rsv
, 0);
3047 trace_qgroup_meta_reserve(root
, -(s64
)reserved
);
3048 btrfs_qgroup_free_refroot(fs_info
, root
->objectid
, reserved
);
3051 void btrfs_qgroup_free_meta(struct btrfs_root
*root
, int num_bytes
)
3053 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3055 if (!test_bit(BTRFS_FS_QUOTA_ENABLED
, &fs_info
->flags
) ||
3056 !is_fstree(root
->objectid
))
3059 BUG_ON(num_bytes
!= round_down(num_bytes
, fs_info
->nodesize
));
3060 WARN_ON(atomic64_read(&root
->qgroup_meta_rsv
) < num_bytes
);
3061 atomic64_sub(num_bytes
, &root
->qgroup_meta_rsv
);
3062 trace_qgroup_meta_reserve(root
, -(s64
)num_bytes
);
3063 btrfs_qgroup_free_refroot(fs_info
, root
->objectid
, num_bytes
);
3067 * Check qgroup reserved space leaking, normally at destroy inode
3070 void btrfs_qgroup_check_reserved_leak(struct inode
*inode
)
3072 struct extent_changeset changeset
;
3073 struct ulist_node
*unode
;
3074 struct ulist_iterator iter
;
3077 extent_changeset_init(&changeset
);
3078 ret
= clear_record_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, (u64
)-1,
3079 EXTENT_QGROUP_RESERVED
, &changeset
);
3082 if (WARN_ON(changeset
.bytes_changed
)) {
3083 ULIST_ITER_INIT(&iter
);
3084 while ((unode
= ulist_next(&changeset
.range_changed
, &iter
))) {
3085 btrfs_warn(BTRFS_I(inode
)->root
->fs_info
,
3086 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3087 inode
->i_ino
, unode
->val
, unode
->aux
);
3089 btrfs_qgroup_free_refroot(BTRFS_I(inode
)->root
->fs_info
,
3090 BTRFS_I(inode
)->root
->objectid
,
3091 changeset
.bytes_changed
);
3094 extent_changeset_release(&changeset
);