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>
28 #include "transaction.h"
36 * - subvol delete -> delete when ref goes to 0? delete limits also?
41 * - copy also limits on subvol creation
43 * - caches fuer ulists
44 * - performance benchmarks
45 * - check all ioctl parameters
49 * one struct for each qgroup, organized in fs_info->qgroup_tree.
57 u64 rfer
; /* referenced */
58 u64 rfer_cmpr
; /* referenced compressed */
59 u64 excl
; /* exclusive */
60 u64 excl_cmpr
; /* exclusive compressed */
65 u64 lim_flags
; /* which limits are set */
72 * reservation tracking
79 struct list_head groups
; /* groups this group is member of */
80 struct list_head members
; /* groups that are members of this group */
81 struct list_head dirty
; /* dirty groups */
82 struct rb_node node
; /* tree of qgroups */
85 * temp variables for accounting operations
92 * glue structure to represent the relations between qgroups.
94 struct btrfs_qgroup_list
{
95 struct list_head next_group
;
96 struct list_head next_member
;
97 struct btrfs_qgroup
*group
;
98 struct btrfs_qgroup
*member
;
101 /* must be called with qgroup_lock held */
102 static struct btrfs_qgroup
*find_qgroup_rb(struct btrfs_fs_info
*fs_info
,
105 struct rb_node
*n
= fs_info
->qgroup_tree
.rb_node
;
106 struct btrfs_qgroup
*qgroup
;
109 qgroup
= rb_entry(n
, struct btrfs_qgroup
, node
);
110 if (qgroup
->qgroupid
< qgroupid
)
112 else if (qgroup
->qgroupid
> qgroupid
)
120 /* must be called with qgroup_lock held */
121 static struct btrfs_qgroup
*add_qgroup_rb(struct btrfs_fs_info
*fs_info
,
124 struct rb_node
**p
= &fs_info
->qgroup_tree
.rb_node
;
125 struct rb_node
*parent
= NULL
;
126 struct btrfs_qgroup
*qgroup
;
130 qgroup
= rb_entry(parent
, struct btrfs_qgroup
, node
);
132 if (qgroup
->qgroupid
< qgroupid
)
134 else if (qgroup
->qgroupid
> qgroupid
)
140 qgroup
= kzalloc(sizeof(*qgroup
), GFP_ATOMIC
);
142 return ERR_PTR(-ENOMEM
);
144 qgroup
->qgroupid
= qgroupid
;
145 INIT_LIST_HEAD(&qgroup
->groups
);
146 INIT_LIST_HEAD(&qgroup
->members
);
147 INIT_LIST_HEAD(&qgroup
->dirty
);
149 rb_link_node(&qgroup
->node
, parent
, p
);
150 rb_insert_color(&qgroup
->node
, &fs_info
->qgroup_tree
);
155 /* must be called with qgroup_lock held */
156 static int del_qgroup_rb(struct btrfs_fs_info
*fs_info
, u64 qgroupid
)
158 struct btrfs_qgroup
*qgroup
= find_qgroup_rb(fs_info
, qgroupid
);
159 struct btrfs_qgroup_list
*list
;
164 rb_erase(&qgroup
->node
, &fs_info
->qgroup_tree
);
165 list_del(&qgroup
->dirty
);
167 while (!list_empty(&qgroup
->groups
)) {
168 list
= list_first_entry(&qgroup
->groups
,
169 struct btrfs_qgroup_list
, next_group
);
170 list_del(&list
->next_group
);
171 list_del(&list
->next_member
);
175 while (!list_empty(&qgroup
->members
)) {
176 list
= list_first_entry(&qgroup
->members
,
177 struct btrfs_qgroup_list
, next_member
);
178 list_del(&list
->next_group
);
179 list_del(&list
->next_member
);
187 /* must be called with qgroup_lock held */
188 static int add_relation_rb(struct btrfs_fs_info
*fs_info
,
189 u64 memberid
, u64 parentid
)
191 struct btrfs_qgroup
*member
;
192 struct btrfs_qgroup
*parent
;
193 struct btrfs_qgroup_list
*list
;
195 member
= find_qgroup_rb(fs_info
, memberid
);
196 parent
= find_qgroup_rb(fs_info
, parentid
);
197 if (!member
|| !parent
)
200 list
= kzalloc(sizeof(*list
), GFP_ATOMIC
);
204 list
->group
= parent
;
205 list
->member
= member
;
206 list_add_tail(&list
->next_group
, &member
->groups
);
207 list_add_tail(&list
->next_member
, &parent
->members
);
212 /* must be called with qgroup_lock held */
213 static int del_relation_rb(struct btrfs_fs_info
*fs_info
,
214 u64 memberid
, u64 parentid
)
216 struct btrfs_qgroup
*member
;
217 struct btrfs_qgroup
*parent
;
218 struct btrfs_qgroup_list
*list
;
220 member
= find_qgroup_rb(fs_info
, memberid
);
221 parent
= find_qgroup_rb(fs_info
, parentid
);
222 if (!member
|| !parent
)
225 list_for_each_entry(list
, &member
->groups
, next_group
) {
226 if (list
->group
== parent
) {
227 list_del(&list
->next_group
);
228 list_del(&list
->next_member
);
237 * The full config is read in one go, only called from open_ctree()
238 * It doesn't use any locking, as at this point we're still single-threaded
240 int btrfs_read_qgroup_config(struct btrfs_fs_info
*fs_info
)
242 struct btrfs_key key
;
243 struct btrfs_key found_key
;
244 struct btrfs_root
*quota_root
= fs_info
->quota_root
;
245 struct btrfs_path
*path
= NULL
;
246 struct extent_buffer
*l
;
251 if (!fs_info
->quota_enabled
)
254 path
= btrfs_alloc_path();
260 /* default this to quota off, in case no status key is found */
261 fs_info
->qgroup_flags
= 0;
264 * pass 1: read status, all qgroup infos and limits
269 ret
= btrfs_search_slot_for_read(quota_root
, &key
, path
, 1, 1);
274 struct btrfs_qgroup
*qgroup
;
276 slot
= path
->slots
[0];
278 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
280 if (found_key
.type
== BTRFS_QGROUP_STATUS_KEY
) {
281 struct btrfs_qgroup_status_item
*ptr
;
283 ptr
= btrfs_item_ptr(l
, slot
,
284 struct btrfs_qgroup_status_item
);
286 if (btrfs_qgroup_status_version(l
, ptr
) !=
287 BTRFS_QGROUP_STATUS_VERSION
) {
289 "btrfs: old qgroup version, quota disabled\n");
292 if (btrfs_qgroup_status_generation(l
, ptr
) !=
293 fs_info
->generation
) {
294 flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
296 "btrfs: qgroup generation mismatch, "
297 "marked as inconsistent\n");
299 fs_info
->qgroup_flags
= btrfs_qgroup_status_flags(l
,
301 /* FIXME read scan element */
305 if (found_key
.type
!= BTRFS_QGROUP_INFO_KEY
&&
306 found_key
.type
!= BTRFS_QGROUP_LIMIT_KEY
)
309 qgroup
= find_qgroup_rb(fs_info
, found_key
.offset
);
310 if ((qgroup
&& found_key
.type
== BTRFS_QGROUP_INFO_KEY
) ||
311 (!qgroup
&& found_key
.type
== BTRFS_QGROUP_LIMIT_KEY
)) {
312 printk(KERN_ERR
"btrfs: inconsitent qgroup config\n");
313 flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
316 qgroup
= add_qgroup_rb(fs_info
, found_key
.offset
);
317 if (IS_ERR(qgroup
)) {
318 ret
= PTR_ERR(qgroup
);
322 switch (found_key
.type
) {
323 case BTRFS_QGROUP_INFO_KEY
: {
324 struct btrfs_qgroup_info_item
*ptr
;
326 ptr
= btrfs_item_ptr(l
, slot
,
327 struct btrfs_qgroup_info_item
);
328 qgroup
->rfer
= btrfs_qgroup_info_rfer(l
, ptr
);
329 qgroup
->rfer_cmpr
= btrfs_qgroup_info_rfer_cmpr(l
, ptr
);
330 qgroup
->excl
= btrfs_qgroup_info_excl(l
, ptr
);
331 qgroup
->excl_cmpr
= btrfs_qgroup_info_excl_cmpr(l
, ptr
);
332 /* generation currently unused */
335 case BTRFS_QGROUP_LIMIT_KEY
: {
336 struct btrfs_qgroup_limit_item
*ptr
;
338 ptr
= btrfs_item_ptr(l
, slot
,
339 struct btrfs_qgroup_limit_item
);
340 qgroup
->lim_flags
= btrfs_qgroup_limit_flags(l
, ptr
);
341 qgroup
->max_rfer
= btrfs_qgroup_limit_max_rfer(l
, ptr
);
342 qgroup
->max_excl
= btrfs_qgroup_limit_max_excl(l
, ptr
);
343 qgroup
->rsv_rfer
= btrfs_qgroup_limit_rsv_rfer(l
, ptr
);
344 qgroup
->rsv_excl
= btrfs_qgroup_limit_rsv_excl(l
, ptr
);
349 ret
= btrfs_next_item(quota_root
, path
);
355 btrfs_release_path(path
);
358 * pass 2: read all qgroup relations
361 key
.type
= BTRFS_QGROUP_RELATION_KEY
;
363 ret
= btrfs_search_slot_for_read(quota_root
, &key
, path
, 1, 0);
367 slot
= path
->slots
[0];
369 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
371 if (found_key
.type
!= BTRFS_QGROUP_RELATION_KEY
)
374 if (found_key
.objectid
> found_key
.offset
) {
375 /* parent <- member, not needed to build config */
376 /* FIXME should we omit the key completely? */
380 ret
= add_relation_rb(fs_info
, found_key
.objectid
,
385 ret
= btrfs_next_item(quota_root
, path
);
392 fs_info
->qgroup_flags
|= flags
;
393 if (!(fs_info
->qgroup_flags
& BTRFS_QGROUP_STATUS_FLAG_ON
)) {
394 fs_info
->quota_enabled
= 0;
395 fs_info
->pending_quota_state
= 0;
397 btrfs_free_path(path
);
399 return ret
< 0 ? ret
: 0;
403 * This is only called from close_ctree() or open_ctree(), both in single-
404 * treaded paths. Clean up the in-memory structures. No locking needed.
406 void btrfs_free_qgroup_config(struct btrfs_fs_info
*fs_info
)
409 struct btrfs_qgroup
*qgroup
;
410 struct btrfs_qgroup_list
*list
;
412 while ((n
= rb_first(&fs_info
->qgroup_tree
))) {
413 qgroup
= rb_entry(n
, struct btrfs_qgroup
, node
);
414 rb_erase(n
, &fs_info
->qgroup_tree
);
416 WARN_ON(!list_empty(&qgroup
->dirty
));
418 while (!list_empty(&qgroup
->groups
)) {
419 list
= list_first_entry(&qgroup
->groups
,
420 struct btrfs_qgroup_list
,
422 list_del(&list
->next_group
);
423 list_del(&list
->next_member
);
427 while (!list_empty(&qgroup
->members
)) {
428 list
= list_first_entry(&qgroup
->members
,
429 struct btrfs_qgroup_list
,
431 list_del(&list
->next_group
);
432 list_del(&list
->next_member
);
439 static int add_qgroup_relation_item(struct btrfs_trans_handle
*trans
,
440 struct btrfs_root
*quota_root
,
444 struct btrfs_path
*path
;
445 struct btrfs_key key
;
447 path
= btrfs_alloc_path();
452 key
.type
= BTRFS_QGROUP_RELATION_KEY
;
455 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
, 0);
457 btrfs_mark_buffer_dirty(path
->nodes
[0]);
459 btrfs_free_path(path
);
463 static int del_qgroup_relation_item(struct btrfs_trans_handle
*trans
,
464 struct btrfs_root
*quota_root
,
468 struct btrfs_path
*path
;
469 struct btrfs_key key
;
471 path
= btrfs_alloc_path();
476 key
.type
= BTRFS_QGROUP_RELATION_KEY
;
479 ret
= btrfs_search_slot(trans
, quota_root
, &key
, path
, -1, 1);
488 ret
= btrfs_del_item(trans
, quota_root
, path
);
490 btrfs_free_path(path
);
494 static int add_qgroup_item(struct btrfs_trans_handle
*trans
,
495 struct btrfs_root
*quota_root
, u64 qgroupid
)
498 struct btrfs_path
*path
;
499 struct btrfs_qgroup_info_item
*qgroup_info
;
500 struct btrfs_qgroup_limit_item
*qgroup_limit
;
501 struct extent_buffer
*leaf
;
502 struct btrfs_key key
;
504 path
= btrfs_alloc_path();
509 key
.type
= BTRFS_QGROUP_INFO_KEY
;
510 key
.offset
= qgroupid
;
512 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
,
513 sizeof(*qgroup_info
));
517 leaf
= path
->nodes
[0];
518 qgroup_info
= btrfs_item_ptr(leaf
, path
->slots
[0],
519 struct btrfs_qgroup_info_item
);
520 btrfs_set_qgroup_info_generation(leaf
, qgroup_info
, trans
->transid
);
521 btrfs_set_qgroup_info_rfer(leaf
, qgroup_info
, 0);
522 btrfs_set_qgroup_info_rfer_cmpr(leaf
, qgroup_info
, 0);
523 btrfs_set_qgroup_info_excl(leaf
, qgroup_info
, 0);
524 btrfs_set_qgroup_info_excl_cmpr(leaf
, qgroup_info
, 0);
526 btrfs_mark_buffer_dirty(leaf
);
528 btrfs_release_path(path
);
530 key
.type
= BTRFS_QGROUP_LIMIT_KEY
;
531 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
,
532 sizeof(*qgroup_limit
));
536 leaf
= path
->nodes
[0];
537 qgroup_limit
= btrfs_item_ptr(leaf
, path
->slots
[0],
538 struct btrfs_qgroup_limit_item
);
539 btrfs_set_qgroup_limit_flags(leaf
, qgroup_limit
, 0);
540 btrfs_set_qgroup_limit_max_rfer(leaf
, qgroup_limit
, 0);
541 btrfs_set_qgroup_limit_max_excl(leaf
, qgroup_limit
, 0);
542 btrfs_set_qgroup_limit_rsv_rfer(leaf
, qgroup_limit
, 0);
543 btrfs_set_qgroup_limit_rsv_excl(leaf
, qgroup_limit
, 0);
545 btrfs_mark_buffer_dirty(leaf
);
549 btrfs_free_path(path
);
553 static int del_qgroup_item(struct btrfs_trans_handle
*trans
,
554 struct btrfs_root
*quota_root
, u64 qgroupid
)
557 struct btrfs_path
*path
;
558 struct btrfs_key key
;
560 path
= btrfs_alloc_path();
565 key
.type
= BTRFS_QGROUP_INFO_KEY
;
566 key
.offset
= qgroupid
;
567 ret
= btrfs_search_slot(trans
, quota_root
, &key
, path
, -1, 1);
576 ret
= btrfs_del_item(trans
, quota_root
, path
);
580 btrfs_release_path(path
);
582 key
.type
= BTRFS_QGROUP_LIMIT_KEY
;
583 ret
= btrfs_search_slot(trans
, quota_root
, &key
, path
, -1, 1);
592 ret
= btrfs_del_item(trans
, quota_root
, path
);
595 btrfs_free_path(path
);
599 static int update_qgroup_limit_item(struct btrfs_trans_handle
*trans
,
600 struct btrfs_root
*root
, u64 qgroupid
,
601 u64 flags
, u64 max_rfer
, u64 max_excl
,
602 u64 rsv_rfer
, u64 rsv_excl
)
604 struct btrfs_path
*path
;
605 struct btrfs_key key
;
606 struct extent_buffer
*l
;
607 struct btrfs_qgroup_limit_item
*qgroup_limit
;
612 key
.type
= BTRFS_QGROUP_LIMIT_KEY
;
613 key
.offset
= qgroupid
;
615 path
= btrfs_alloc_path();
617 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
625 slot
= path
->slots
[0];
626 qgroup_limit
= btrfs_item_ptr(l
, path
->slots
[0],
627 struct btrfs_qgroup_limit_item
);
628 btrfs_set_qgroup_limit_flags(l
, qgroup_limit
, flags
);
629 btrfs_set_qgroup_limit_max_rfer(l
, qgroup_limit
, max_rfer
);
630 btrfs_set_qgroup_limit_max_excl(l
, qgroup_limit
, max_excl
);
631 btrfs_set_qgroup_limit_rsv_rfer(l
, qgroup_limit
, rsv_rfer
);
632 btrfs_set_qgroup_limit_rsv_excl(l
, qgroup_limit
, rsv_excl
);
634 btrfs_mark_buffer_dirty(l
);
637 btrfs_free_path(path
);
641 static int update_qgroup_info_item(struct btrfs_trans_handle
*trans
,
642 struct btrfs_root
*root
,
643 struct btrfs_qgroup
*qgroup
)
645 struct btrfs_path
*path
;
646 struct btrfs_key key
;
647 struct extent_buffer
*l
;
648 struct btrfs_qgroup_info_item
*qgroup_info
;
653 key
.type
= BTRFS_QGROUP_INFO_KEY
;
654 key
.offset
= qgroup
->qgroupid
;
656 path
= btrfs_alloc_path();
658 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
666 slot
= path
->slots
[0];
667 qgroup_info
= btrfs_item_ptr(l
, path
->slots
[0],
668 struct btrfs_qgroup_info_item
);
669 btrfs_set_qgroup_info_generation(l
, qgroup_info
, trans
->transid
);
670 btrfs_set_qgroup_info_rfer(l
, qgroup_info
, qgroup
->rfer
);
671 btrfs_set_qgroup_info_rfer_cmpr(l
, qgroup_info
, qgroup
->rfer_cmpr
);
672 btrfs_set_qgroup_info_excl(l
, qgroup_info
, qgroup
->excl
);
673 btrfs_set_qgroup_info_excl_cmpr(l
, qgroup_info
, qgroup
->excl_cmpr
);
675 btrfs_mark_buffer_dirty(l
);
678 btrfs_free_path(path
);
682 static int update_qgroup_status_item(struct btrfs_trans_handle
*trans
,
683 struct btrfs_fs_info
*fs_info
,
684 struct btrfs_root
*root
)
686 struct btrfs_path
*path
;
687 struct btrfs_key key
;
688 struct extent_buffer
*l
;
689 struct btrfs_qgroup_status_item
*ptr
;
694 key
.type
= BTRFS_QGROUP_STATUS_KEY
;
697 path
= btrfs_alloc_path();
699 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
707 slot
= path
->slots
[0];
708 ptr
= btrfs_item_ptr(l
, slot
, struct btrfs_qgroup_status_item
);
709 btrfs_set_qgroup_status_flags(l
, ptr
, fs_info
->qgroup_flags
);
710 btrfs_set_qgroup_status_generation(l
, ptr
, trans
->transid
);
713 btrfs_mark_buffer_dirty(l
);
716 btrfs_free_path(path
);
721 * called with qgroup_lock held
723 static int btrfs_clean_quota_tree(struct btrfs_trans_handle
*trans
,
724 struct btrfs_root
*root
)
726 struct btrfs_path
*path
;
727 struct btrfs_key key
;
733 path
= btrfs_alloc_path();
742 path
->leave_spinning
= 1;
743 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
745 if (path
->slots
[0] == 0)
748 } else if (ret
< 0) {
752 ret
= btrfs_del_item(trans
, root
, path
);
755 btrfs_release_path(path
);
759 root
->fs_info
->pending_quota_state
= 0;
760 btrfs_free_path(path
);
764 int btrfs_quota_enable(struct btrfs_trans_handle
*trans
,
765 struct btrfs_fs_info
*fs_info
)
767 struct btrfs_root
*quota_root
;
768 struct btrfs_path
*path
= NULL
;
769 struct btrfs_qgroup_status_item
*ptr
;
770 struct extent_buffer
*leaf
;
771 struct btrfs_key key
;
774 spin_lock(&fs_info
->qgroup_lock
);
775 if (fs_info
->quota_root
) {
776 fs_info
->pending_quota_state
= 1;
777 spin_unlock(&fs_info
->qgroup_lock
);
780 spin_unlock(&fs_info
->qgroup_lock
);
783 * initially create the quota tree
785 quota_root
= btrfs_create_tree(trans
, fs_info
,
786 BTRFS_QUOTA_TREE_OBJECTID
);
787 if (IS_ERR(quota_root
)) {
788 ret
= PTR_ERR(quota_root
);
792 path
= btrfs_alloc_path();
797 key
.type
= BTRFS_QGROUP_STATUS_KEY
;
800 ret
= btrfs_insert_empty_item(trans
, quota_root
, path
, &key
,
805 leaf
= path
->nodes
[0];
806 ptr
= btrfs_item_ptr(leaf
, path
->slots
[0],
807 struct btrfs_qgroup_status_item
);
808 btrfs_set_qgroup_status_generation(leaf
, ptr
, trans
->transid
);
809 btrfs_set_qgroup_status_version(leaf
, ptr
, BTRFS_QGROUP_STATUS_VERSION
);
810 fs_info
->qgroup_flags
= BTRFS_QGROUP_STATUS_FLAG_ON
|
811 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
812 btrfs_set_qgroup_status_flags(leaf
, ptr
, fs_info
->qgroup_flags
);
813 btrfs_set_qgroup_status_scan(leaf
, ptr
, 0);
815 btrfs_mark_buffer_dirty(leaf
);
817 spin_lock(&fs_info
->qgroup_lock
);
818 fs_info
->quota_root
= quota_root
;
819 fs_info
->pending_quota_state
= 1;
820 spin_unlock(&fs_info
->qgroup_lock
);
822 btrfs_free_path(path
);
826 int btrfs_quota_disable(struct btrfs_trans_handle
*trans
,
827 struct btrfs_fs_info
*fs_info
)
829 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
830 struct btrfs_root
*quota_root
;
833 spin_lock(&fs_info
->qgroup_lock
);
834 fs_info
->quota_enabled
= 0;
835 fs_info
->pending_quota_state
= 0;
836 quota_root
= fs_info
->quota_root
;
837 fs_info
->quota_root
= NULL
;
838 btrfs_free_qgroup_config(fs_info
);
839 spin_unlock(&fs_info
->qgroup_lock
);
844 ret
= btrfs_clean_quota_tree(trans
, quota_root
);
848 ret
= btrfs_del_root(trans
, tree_root
, "a_root
->root_key
);
852 list_del("a_root
->dirty_list
);
854 btrfs_tree_lock(quota_root
->node
);
855 clean_tree_block(trans
, tree_root
, quota_root
->node
);
856 btrfs_tree_unlock(quota_root
->node
);
857 btrfs_free_tree_block(trans
, quota_root
, quota_root
->node
, 0, 1);
859 free_extent_buffer(quota_root
->node
);
860 free_extent_buffer(quota_root
->commit_root
);
866 int btrfs_quota_rescan(struct btrfs_fs_info
*fs_info
)
872 int btrfs_add_qgroup_relation(struct btrfs_trans_handle
*trans
,
873 struct btrfs_fs_info
*fs_info
, u64 src
, u64 dst
)
875 struct btrfs_root
*quota_root
;
878 quota_root
= fs_info
->quota_root
;
882 ret
= add_qgroup_relation_item(trans
, quota_root
, src
, dst
);
886 ret
= add_qgroup_relation_item(trans
, quota_root
, dst
, src
);
888 del_qgroup_relation_item(trans
, quota_root
, src
, dst
);
892 spin_lock(&fs_info
->qgroup_lock
);
893 ret
= add_relation_rb(quota_root
->fs_info
, src
, dst
);
894 spin_unlock(&fs_info
->qgroup_lock
);
899 int btrfs_del_qgroup_relation(struct btrfs_trans_handle
*trans
,
900 struct btrfs_fs_info
*fs_info
, u64 src
, u64 dst
)
902 struct btrfs_root
*quota_root
;
906 quota_root
= fs_info
->quota_root
;
910 ret
= del_qgroup_relation_item(trans
, quota_root
, src
, dst
);
911 err
= del_qgroup_relation_item(trans
, quota_root
, dst
, src
);
915 spin_lock(&fs_info
->qgroup_lock
);
916 del_relation_rb(fs_info
, src
, dst
);
918 spin_unlock(&fs_info
->qgroup_lock
);
923 int btrfs_create_qgroup(struct btrfs_trans_handle
*trans
,
924 struct btrfs_fs_info
*fs_info
, u64 qgroupid
, char *name
)
926 struct btrfs_root
*quota_root
;
927 struct btrfs_qgroup
*qgroup
;
930 quota_root
= fs_info
->quota_root
;
934 ret
= add_qgroup_item(trans
, quota_root
, qgroupid
);
936 spin_lock(&fs_info
->qgroup_lock
);
937 qgroup
= add_qgroup_rb(fs_info
, qgroupid
);
938 spin_unlock(&fs_info
->qgroup_lock
);
941 ret
= PTR_ERR(qgroup
);
946 int btrfs_remove_qgroup(struct btrfs_trans_handle
*trans
,
947 struct btrfs_fs_info
*fs_info
, u64 qgroupid
)
949 struct btrfs_root
*quota_root
;
952 quota_root
= fs_info
->quota_root
;
956 ret
= del_qgroup_item(trans
, quota_root
, qgroupid
);
958 spin_lock(&fs_info
->qgroup_lock
);
959 del_qgroup_rb(quota_root
->fs_info
, qgroupid
);
961 spin_unlock(&fs_info
->qgroup_lock
);
966 int btrfs_limit_qgroup(struct btrfs_trans_handle
*trans
,
967 struct btrfs_fs_info
*fs_info
, u64 qgroupid
,
968 struct btrfs_qgroup_limit
*limit
)
970 struct btrfs_root
*quota_root
= fs_info
->quota_root
;
971 struct btrfs_qgroup
*qgroup
;
977 ret
= update_qgroup_limit_item(trans
, quota_root
, qgroupid
,
978 limit
->flags
, limit
->max_rfer
,
979 limit
->max_excl
, limit
->rsv_rfer
,
982 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
983 printk(KERN_INFO
"unable to update quota limit for %llu\n",
984 (unsigned long long)qgroupid
);
987 spin_lock(&fs_info
->qgroup_lock
);
989 qgroup
= find_qgroup_rb(fs_info
, qgroupid
);
994 qgroup
->lim_flags
= limit
->flags
;
995 qgroup
->max_rfer
= limit
->max_rfer
;
996 qgroup
->max_excl
= limit
->max_excl
;
997 qgroup
->rsv_rfer
= limit
->rsv_rfer
;
998 qgroup
->rsv_excl
= limit
->rsv_excl
;
1001 spin_unlock(&fs_info
->qgroup_lock
);
1006 static void qgroup_dirty(struct btrfs_fs_info
*fs_info
,
1007 struct btrfs_qgroup
*qgroup
)
1009 if (list_empty(&qgroup
->dirty
))
1010 list_add(&qgroup
->dirty
, &fs_info
->dirty_qgroups
);
1014 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1015 * the modification into a list that's later used by btrfs_end_transaction to
1016 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1018 int btrfs_qgroup_record_ref(struct btrfs_trans_handle
*trans
,
1019 struct btrfs_delayed_ref_node
*node
,
1020 struct btrfs_delayed_extent_op
*extent_op
)
1022 struct qgroup_update
*u
;
1024 BUG_ON(!trans
->delayed_ref_elem
.seq
);
1025 u
= kmalloc(sizeof(*u
), GFP_NOFS
);
1030 u
->extent_op
= extent_op
;
1031 list_add_tail(&u
->list
, &trans
->qgroup_ref_list
);
1037 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1038 * from the fs. First, all roots referencing the extent are searched, and
1039 * then the space is accounted accordingly to the different roots. The
1040 * accounting algorithm works in 3 steps documented inline.
1042 int btrfs_qgroup_account_ref(struct btrfs_trans_handle
*trans
,
1043 struct btrfs_fs_info
*fs_info
,
1044 struct btrfs_delayed_ref_node
*node
,
1045 struct btrfs_delayed_extent_op
*extent_op
)
1047 struct btrfs_key ins
;
1048 struct btrfs_root
*quota_root
;
1050 struct btrfs_qgroup
*qgroup
;
1051 struct ulist_node
*unode
;
1052 struct ulist
*roots
= NULL
;
1053 struct ulist
*tmp
= NULL
;
1054 struct ulist_iterator uiter
;
1059 if (!fs_info
->quota_enabled
)
1062 BUG_ON(!fs_info
->quota_root
);
1064 ins
.objectid
= node
->bytenr
;
1065 ins
.offset
= node
->num_bytes
;
1066 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1068 if (node
->type
== BTRFS_TREE_BLOCK_REF_KEY
||
1069 node
->type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
1070 struct btrfs_delayed_tree_ref
*ref
;
1071 ref
= btrfs_delayed_node_to_tree_ref(node
);
1072 ref_root
= ref
->root
;
1073 } else if (node
->type
== BTRFS_EXTENT_DATA_REF_KEY
||
1074 node
->type
== BTRFS_SHARED_DATA_REF_KEY
) {
1075 struct btrfs_delayed_data_ref
*ref
;
1076 ref
= btrfs_delayed_node_to_data_ref(node
);
1077 ref_root
= ref
->root
;
1082 if (!is_fstree(ref_root
)) {
1084 * non-fs-trees are not being accounted
1089 switch (node
->action
) {
1090 case BTRFS_ADD_DELAYED_REF
:
1091 case BTRFS_ADD_DELAYED_EXTENT
:
1094 case BTRFS_DROP_DELAYED_REF
:
1097 case BTRFS_UPDATE_DELAYED_HEAD
:
1104 * the delayed ref sequence number we pass depends on the direction of
1105 * the operation. for add operations, we pass (node->seq - 1) to skip
1106 * the delayed ref's current sequence number, because we need the state
1107 * of the tree before the add operation. for delete operations, we pass
1108 * (node->seq) to include the delayed ref's current sequence number,
1109 * because we need the state of the tree after the delete operation.
1111 ret
= btrfs_find_all_roots(trans
, fs_info
, node
->bytenr
,
1112 sgn
> 0 ? node
->seq
- 1 : node
->seq
, &roots
);
1116 spin_lock(&fs_info
->qgroup_lock
);
1117 quota_root
= fs_info
->quota_root
;
1121 qgroup
= find_qgroup_rb(fs_info
, ref_root
);
1126 * step 1: for each old ref, visit all nodes once and inc refcnt
1128 tmp
= ulist_alloc(GFP_ATOMIC
);
1133 seq
= fs_info
->qgroup_seq
;
1134 fs_info
->qgroup_seq
+= roots
->nnodes
+ 1; /* max refcnt */
1136 ULIST_ITER_INIT(&uiter
);
1137 while ((unode
= ulist_next(roots
, &uiter
))) {
1138 struct ulist_node
*tmp_unode
;
1139 struct ulist_iterator tmp_uiter
;
1140 struct btrfs_qgroup
*qg
;
1142 qg
= find_qgroup_rb(fs_info
, unode
->val
);
1147 /* XXX id not needed */
1148 ulist_add(tmp
, qg
->qgroupid
, (u64
)(uintptr_t)qg
, GFP_ATOMIC
);
1149 ULIST_ITER_INIT(&tmp_uiter
);
1150 while ((tmp_unode
= ulist_next(tmp
, &tmp_uiter
))) {
1151 struct btrfs_qgroup_list
*glist
;
1153 qg
= (struct btrfs_qgroup
*)(uintptr_t)tmp_unode
->aux
;
1154 if (qg
->refcnt
< seq
)
1155 qg
->refcnt
= seq
+ 1;
1159 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
1160 ulist_add(tmp
, glist
->group
->qgroupid
,
1161 (u64
)(uintptr_t)glist
->group
,
1168 * step 2: walk from the new root
1171 ulist_add(tmp
, qgroup
->qgroupid
, (uintptr_t)qgroup
, GFP_ATOMIC
);
1172 ULIST_ITER_INIT(&uiter
);
1173 while ((unode
= ulist_next(tmp
, &uiter
))) {
1174 struct btrfs_qgroup
*qg
;
1175 struct btrfs_qgroup_list
*glist
;
1177 qg
= (struct btrfs_qgroup
*)(uintptr_t)unode
->aux
;
1178 if (qg
->refcnt
< seq
) {
1179 /* not visited by step 1 */
1180 qg
->rfer
+= sgn
* node
->num_bytes
;
1181 qg
->rfer_cmpr
+= sgn
* node
->num_bytes
;
1182 if (roots
->nnodes
== 0) {
1183 qg
->excl
+= sgn
* node
->num_bytes
;
1184 qg
->excl_cmpr
+= sgn
* node
->num_bytes
;
1186 qgroup_dirty(fs_info
, qg
);
1188 WARN_ON(qg
->tag
>= seq
);
1191 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
1192 ulist_add(tmp
, glist
->group
->qgroupid
,
1193 (uintptr_t)glist
->group
, GFP_ATOMIC
);
1198 * step 3: walk again from old refs
1200 ULIST_ITER_INIT(&uiter
);
1201 while ((unode
= ulist_next(roots
, &uiter
))) {
1202 struct btrfs_qgroup
*qg
;
1203 struct ulist_node
*tmp_unode
;
1204 struct ulist_iterator tmp_uiter
;
1206 qg
= find_qgroup_rb(fs_info
, unode
->val
);
1211 ulist_add(tmp
, qg
->qgroupid
, (uintptr_t)qg
, GFP_ATOMIC
);
1212 ULIST_ITER_INIT(&tmp_uiter
);
1213 while ((tmp_unode
= ulist_next(tmp
, &tmp_uiter
))) {
1214 struct btrfs_qgroup_list
*glist
;
1216 qg
= (struct btrfs_qgroup
*)(uintptr_t)tmp_unode
->aux
;
1220 if (qg
->refcnt
- seq
== roots
->nnodes
) {
1221 qg
->excl
-= sgn
* node
->num_bytes
;
1222 qg
->excl_cmpr
-= sgn
* node
->num_bytes
;
1223 qgroup_dirty(fs_info
, qg
);
1226 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
1227 ulist_add(tmp
, glist
->group
->qgroupid
,
1228 (uintptr_t)glist
->group
,
1235 spin_unlock(&fs_info
->qgroup_lock
);
1244 * called from commit_transaction. Writes all changed qgroups to disk.
1246 int btrfs_run_qgroups(struct btrfs_trans_handle
*trans
,
1247 struct btrfs_fs_info
*fs_info
)
1249 struct btrfs_root
*quota_root
= fs_info
->quota_root
;
1255 fs_info
->quota_enabled
= fs_info
->pending_quota_state
;
1257 spin_lock(&fs_info
->qgroup_lock
);
1258 while (!list_empty(&fs_info
->dirty_qgroups
)) {
1259 struct btrfs_qgroup
*qgroup
;
1260 qgroup
= list_first_entry(&fs_info
->dirty_qgroups
,
1261 struct btrfs_qgroup
, dirty
);
1262 list_del_init(&qgroup
->dirty
);
1263 spin_unlock(&fs_info
->qgroup_lock
);
1264 ret
= update_qgroup_info_item(trans
, quota_root
, qgroup
);
1266 fs_info
->qgroup_flags
|=
1267 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
1268 spin_lock(&fs_info
->qgroup_lock
);
1270 if (fs_info
->quota_enabled
)
1271 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_ON
;
1273 fs_info
->qgroup_flags
&= ~BTRFS_QGROUP_STATUS_FLAG_ON
;
1274 spin_unlock(&fs_info
->qgroup_lock
);
1276 ret
= update_qgroup_status_item(trans
, fs_info
, quota_root
);
1278 fs_info
->qgroup_flags
|= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT
;
1286 * copy the acounting information between qgroups. This is necessary when a
1287 * snapshot or a subvolume is created
1289 int btrfs_qgroup_inherit(struct btrfs_trans_handle
*trans
,
1290 struct btrfs_fs_info
*fs_info
, u64 srcid
, u64 objectid
,
1291 struct btrfs_qgroup_inherit
*inherit
)
1296 struct btrfs_root
*quota_root
= fs_info
->quota_root
;
1297 struct btrfs_qgroup
*srcgroup
;
1298 struct btrfs_qgroup
*dstgroup
;
1301 if (!fs_info
->quota_enabled
)
1308 * create a tracking group for the subvol itself
1310 ret
= add_qgroup_item(trans
, quota_root
, objectid
);
1314 if (inherit
&& inherit
->flags
& BTRFS_QGROUP_INHERIT_SET_LIMITS
) {
1315 ret
= update_qgroup_limit_item(trans
, quota_root
, objectid
,
1317 inherit
->lim
.max_rfer
,
1318 inherit
->lim
.max_excl
,
1319 inherit
->lim
.rsv_rfer
,
1320 inherit
->lim
.rsv_excl
);
1326 struct btrfs_root
*srcroot
;
1327 struct btrfs_key srckey
;
1330 srckey
.objectid
= srcid
;
1331 srckey
.type
= BTRFS_ROOT_ITEM_KEY
;
1332 srckey
.offset
= (u64
)-1;
1333 srcroot
= btrfs_read_fs_root_no_name(fs_info
, &srckey
);
1334 if (IS_ERR(srcroot
)) {
1335 ret
= PTR_ERR(srcroot
);
1340 srcroot_level
= btrfs_header_level(srcroot
->node
);
1341 level_size
= btrfs_level_size(srcroot
, srcroot_level
);
1346 * add qgroup to all inherited groups
1349 i_qgroups
= (u64
*)(inherit
+ 1);
1350 for (i
= 0; i
< inherit
->num_qgroups
; ++i
) {
1351 ret
= add_qgroup_relation_item(trans
, quota_root
,
1352 objectid
, *i_qgroups
);
1355 ret
= add_qgroup_relation_item(trans
, quota_root
,
1356 *i_qgroups
, objectid
);
1364 spin_lock(&fs_info
->qgroup_lock
);
1366 dstgroup
= add_qgroup_rb(fs_info
, objectid
);
1367 if (IS_ERR(dstgroup
)) {
1368 ret
= PTR_ERR(dstgroup
);
1373 srcgroup
= find_qgroup_rb(fs_info
, srcid
);
1376 dstgroup
->rfer
= srcgroup
->rfer
- level_size
;
1377 dstgroup
->rfer_cmpr
= srcgroup
->rfer_cmpr
- level_size
;
1378 srcgroup
->excl
= level_size
;
1379 srcgroup
->excl_cmpr
= level_size
;
1380 qgroup_dirty(fs_info
, dstgroup
);
1381 qgroup_dirty(fs_info
, srcgroup
);
1387 i_qgroups
= (u64
*)(inherit
+ 1);
1388 for (i
= 0; i
< inherit
->num_qgroups
; ++i
) {
1389 ret
= add_relation_rb(quota_root
->fs_info
, objectid
,
1396 for (i
= 0; i
< inherit
->num_ref_copies
; ++i
) {
1397 struct btrfs_qgroup
*src
;
1398 struct btrfs_qgroup
*dst
;
1400 src
= find_qgroup_rb(fs_info
, i_qgroups
[0]);
1401 dst
= find_qgroup_rb(fs_info
, i_qgroups
[1]);
1408 dst
->rfer
= src
->rfer
- level_size
;
1409 dst
->rfer_cmpr
= src
->rfer_cmpr
- level_size
;
1412 for (i
= 0; i
< inherit
->num_excl_copies
; ++i
) {
1413 struct btrfs_qgroup
*src
;
1414 struct btrfs_qgroup
*dst
;
1416 src
= find_qgroup_rb(fs_info
, i_qgroups
[0]);
1417 dst
= find_qgroup_rb(fs_info
, i_qgroups
[1]);
1424 dst
->excl
= src
->excl
+ level_size
;
1425 dst
->excl_cmpr
= src
->excl_cmpr
+ level_size
;
1430 spin_unlock(&fs_info
->qgroup_lock
);
1436 * reserve some space for a qgroup and all its parents. The reservation takes
1437 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1438 * accounting. If not enough space is available, EDQUOT is returned.
1439 * We assume that the requested space is new for all qgroups.
1441 int btrfs_qgroup_reserve(struct btrfs_root
*root
, u64 num_bytes
)
1443 struct btrfs_root
*quota_root
;
1444 struct btrfs_qgroup
*qgroup
;
1445 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1446 u64 ref_root
= root
->root_key
.objectid
;
1448 struct ulist
*ulist
= NULL
;
1449 struct ulist_node
*unode
;
1450 struct ulist_iterator uiter
;
1452 if (!is_fstree(ref_root
))
1458 spin_lock(&fs_info
->qgroup_lock
);
1459 quota_root
= fs_info
->quota_root
;
1463 qgroup
= find_qgroup_rb(fs_info
, ref_root
);
1468 * in a first step, we check all affected qgroups if any limits would
1471 ulist
= ulist_alloc(GFP_ATOMIC
);
1476 ulist_add(ulist
, qgroup
->qgroupid
, (uintptr_t)qgroup
, GFP_ATOMIC
);
1477 ULIST_ITER_INIT(&uiter
);
1478 while ((unode
= ulist_next(ulist
, &uiter
))) {
1479 struct btrfs_qgroup
*qg
;
1480 struct btrfs_qgroup_list
*glist
;
1482 qg
= (struct btrfs_qgroup
*)(uintptr_t)unode
->aux
;
1484 if ((qg
->lim_flags
& BTRFS_QGROUP_LIMIT_MAX_RFER
) &&
1485 qg
->reserved
+ qg
->rfer
+ num_bytes
>
1489 if ((qg
->lim_flags
& BTRFS_QGROUP_LIMIT_MAX_EXCL
) &&
1490 qg
->reserved
+ qg
->excl
+ num_bytes
>
1494 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
1495 ulist_add(ulist
, glist
->group
->qgroupid
,
1496 (uintptr_t)glist
->group
, GFP_ATOMIC
);
1503 * no limits exceeded, now record the reservation into all qgroups
1505 ULIST_ITER_INIT(&uiter
);
1506 while ((unode
= ulist_next(ulist
, &uiter
))) {
1507 struct btrfs_qgroup
*qg
;
1509 qg
= (struct btrfs_qgroup
*)(uintptr_t)unode
->aux
;
1511 qg
->reserved
+= num_bytes
;
1515 spin_unlock(&fs_info
->qgroup_lock
);
1521 void btrfs_qgroup_free(struct btrfs_root
*root
, u64 num_bytes
)
1523 struct btrfs_root
*quota_root
;
1524 struct btrfs_qgroup
*qgroup
;
1525 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1526 struct ulist
*ulist
= NULL
;
1527 struct ulist_node
*unode
;
1528 struct ulist_iterator uiter
;
1529 u64 ref_root
= root
->root_key
.objectid
;
1531 if (!is_fstree(ref_root
))
1537 spin_lock(&fs_info
->qgroup_lock
);
1539 quota_root
= fs_info
->quota_root
;
1543 qgroup
= find_qgroup_rb(fs_info
, ref_root
);
1547 ulist
= ulist_alloc(GFP_ATOMIC
);
1549 btrfs_std_error(fs_info
, -ENOMEM
);
1552 ulist_add(ulist
, qgroup
->qgroupid
, (uintptr_t)qgroup
, GFP_ATOMIC
);
1553 ULIST_ITER_INIT(&uiter
);
1554 while ((unode
= ulist_next(ulist
, &uiter
))) {
1555 struct btrfs_qgroup
*qg
;
1556 struct btrfs_qgroup_list
*glist
;
1558 qg
= (struct btrfs_qgroup
*)(uintptr_t)unode
->aux
;
1560 qg
->reserved
-= num_bytes
;
1562 list_for_each_entry(glist
, &qg
->groups
, next_group
) {
1563 ulist_add(ulist
, glist
->group
->qgroupid
,
1564 (uintptr_t)glist
->group
, GFP_ATOMIC
);
1569 spin_unlock(&fs_info
->qgroup_lock
);
1573 void assert_qgroups_uptodate(struct btrfs_trans_handle
*trans
)
1575 if (list_empty(&trans
->qgroup_ref_list
) && !trans
->delayed_ref_elem
.seq
)
1577 printk(KERN_ERR
"btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %llu\n",
1578 trans
, list_empty(&trans
->qgroup_ref_list
) ? "" : " not",
1579 trans
->delayed_ref_elem
.seq
);