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"
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,
382 if (ret == -ENOENT) {
384 "btrfs: orphan qgroup relation 0x%llx->0x%llx\n",
385 (unsigned long long)found_key.objectid,
386 (unsigned long long)found_key.offset);
387 ret = 0; /* ignore the error */
392 ret = btrfs_next_item(quota_root, path);
399 fs_info->qgroup_flags |= flags;
400 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
401 fs_info->quota_enabled = 0;
402 fs_info->pending_quota_state = 0;
404 btrfs_free_path(path);
406 return ret < 0 ? ret : 0;
410 * This is only called from close_ctree() or open_ctree(), both in single-
411 * treaded paths. Clean up the in-memory structures. No locking needed.
413 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
416 struct btrfs_qgroup *qgroup;
417 struct btrfs_qgroup_list *list;
419 while ((n = rb_first(&fs_info->qgroup_tree))) {
420 qgroup = rb_entry(n, struct btrfs_qgroup, node);
421 rb_erase(n, &fs_info->qgroup_tree);
423 WARN_ON(!list_empty(&qgroup->dirty));
425 while (!list_empty(&qgroup->groups)) {
426 list = list_first_entry(&qgroup->groups,
427 struct btrfs_qgroup_list,
429 list_del(&list->next_group);
430 list_del(&list->next_member);
434 while (!list_empty(&qgroup->members)) {
435 list = list_first_entry(&qgroup->members,
436 struct btrfs_qgroup_list,
438 list_del(&list->next_group);
439 list_del(&list->next_member);
446 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
447 struct btrfs_root *quota_root,
451 struct btrfs_path *path;
452 struct btrfs_key key;
454 path = btrfs_alloc_path();
459 key.type = BTRFS_QGROUP_RELATION_KEY;
462 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
464 btrfs_mark_buffer_dirty(path->nodes[0]);
466 btrfs_free_path(path);
470 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
471 struct btrfs_root *quota_root,
475 struct btrfs_path *path;
476 struct btrfs_key key;
478 path = btrfs_alloc_path();
483 key.type = BTRFS_QGROUP_RELATION_KEY;
486 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
495 ret = btrfs_del_item(trans, quota_root, path);
497 btrfs_free_path(path);
501 static int add_qgroup_item(struct btrfs_trans_handle *trans,
502 struct btrfs_root *quota_root, u64 qgroupid)
505 struct btrfs_path *path;
506 struct btrfs_qgroup_info_item *qgroup_info;
507 struct btrfs_qgroup_limit_item *qgroup_limit;
508 struct extent_buffer *leaf;
509 struct btrfs_key key;
511 path = btrfs_alloc_path();
516 key.type = BTRFS_QGROUP_INFO_KEY;
517 key.offset = qgroupid;
519 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
520 sizeof(*qgroup_info));
524 leaf = path->nodes[0];
525 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
526 struct btrfs_qgroup_info_item);
527 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
528 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
529 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
530 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
531 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
533 btrfs_mark_buffer_dirty(leaf);
535 btrfs_release_path(path);
537 key.type = BTRFS_QGROUP_LIMIT_KEY;
538 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
539 sizeof(*qgroup_limit));
543 leaf = path->nodes[0];
544 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
545 struct btrfs_qgroup_limit_item);
546 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
547 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
548 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
549 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
550 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
552 btrfs_mark_buffer_dirty(leaf);
556 btrfs_free_path(path);
560 static int del_qgroup_item(struct btrfs_trans_handle *trans,
561 struct btrfs_root *quota_root, u64 qgroupid)
564 struct btrfs_path *path;
565 struct btrfs_key key;
567 path = btrfs_alloc_path();
572 key.type = BTRFS_QGROUP_INFO_KEY;
573 key.offset = qgroupid;
574 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
583 ret = btrfs_del_item(trans, quota_root, path);
587 btrfs_release_path(path);
589 key.type = BTRFS_QGROUP_LIMIT_KEY;
590 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
599 ret = btrfs_del_item(trans, quota_root, path);
602 btrfs_free_path(path);
606 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
607 struct btrfs_root *root, u64 qgroupid,
608 u64 flags, u64 max_rfer, u64 max_excl,
609 u64 rsv_rfer, u64 rsv_excl)
611 struct btrfs_path *path;
612 struct btrfs_key key;
613 struct extent_buffer *l;
614 struct btrfs_qgroup_limit_item *qgroup_limit;
619 key.type = BTRFS_QGROUP_LIMIT_KEY;
620 key.offset = qgroupid;
622 path = btrfs_alloc_path();
626 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
634 slot = path->slots[0];
635 qgroup_limit = btrfs_item_ptr(l, path->slots[0],
636 struct btrfs_qgroup_limit_item);
637 btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
638 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
639 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
640 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
641 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
643 btrfs_mark_buffer_dirty(l);
646 btrfs_free_path(path);
650 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
651 struct btrfs_root *root,
652 struct btrfs_qgroup *qgroup)
654 struct btrfs_path *path;
655 struct btrfs_key key;
656 struct extent_buffer *l;
657 struct btrfs_qgroup_info_item *qgroup_info;
662 key.type = BTRFS_QGROUP_INFO_KEY;
663 key.offset = qgroup->qgroupid;
665 path = btrfs_alloc_path();
669 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
677 slot = path->slots[0];
678 qgroup_info = btrfs_item_ptr(l, path->slots[0],
679 struct btrfs_qgroup_info_item);
680 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
681 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
682 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
683 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
684 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
686 btrfs_mark_buffer_dirty(l);
689 btrfs_free_path(path);
693 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
694 struct btrfs_fs_info *fs_info,
695 struct btrfs_root *root)
697 struct btrfs_path *path;
698 struct btrfs_key key;
699 struct extent_buffer *l;
700 struct btrfs_qgroup_status_item *ptr;
705 key.type = BTRFS_QGROUP_STATUS_KEY;
708 path = btrfs_alloc_path();
712 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
720 slot = path->slots[0];
721 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
722 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
723 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
726 btrfs_mark_buffer_dirty(l);
729 btrfs_free_path(path);
734 * called with qgroup_lock held
736 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root)
739 struct btrfs_path *path;
740 struct btrfs_key key;
741 struct extent_buffer *leaf = NULL;
745 path = btrfs_alloc_path();
749 path->leave_spinning = 1;
756 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
759 leaf = path->nodes[0];
760 nr = btrfs_header_nritems(leaf);
764 * delete the leaf one by one
765 * since the whole tree is going
769 ret = btrfs_del_items(trans, root, path, 0, nr);
773 btrfs_release_path(path);
777 root->fs_info->pending_quota_state = 0;
778 btrfs_free_path(path);
782 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
783 struct btrfs_fs_info *fs_info)
785 struct btrfs_root *quota_root;
786 struct btrfs_path *path = NULL;
787 struct btrfs_qgroup_status_item *ptr;
788 struct extent_buffer *leaf;
789 struct btrfs_key key;
792 spin_lock(&fs_info->qgroup_lock);
793 if (fs_info->quota_root) {
794 fs_info->pending_quota_state = 1;
795 spin_unlock(&fs_info->qgroup_lock);
798 spin_unlock(&fs_info->qgroup_lock);
801 * initially create the quota tree
803 quota_root = btrfs_create_tree(trans, fs_info,
804 BTRFS_QUOTA_TREE_OBJECTID);
805 if (IS_ERR(quota_root)) {
806 ret = PTR_ERR(quota_root);
810 path = btrfs_alloc_path();
817 key.type = BTRFS_QGROUP_STATUS_KEY;
820 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
825 leaf = path->nodes[0];
826 ptr = btrfs_item_ptr(leaf, path->slots[0],
827 struct btrfs_qgroup_status_item);
828 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
829 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
830 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
831 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
832 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
833 btrfs_set_qgroup_status_scan(leaf, ptr, 0);
835 btrfs_mark_buffer_dirty(leaf);
837 spin_lock(&fs_info->qgroup_lock);
838 fs_info->quota_root = quota_root;
839 fs_info->pending_quota_state = 1;
840 spin_unlock(&fs_info->qgroup_lock);
842 btrfs_free_path(path);
845 free_extent_buffer(quota_root->node);
846 free_extent_buffer(quota_root->commit_root);
853 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
854 struct btrfs_fs_info *fs_info)
856 struct btrfs_root *tree_root = fs_info->tree_root;
857 struct btrfs_root *quota_root;
860 spin_lock(&fs_info->qgroup_lock);
861 if (!fs_info->quota_root) {
862 spin_unlock(&fs_info->qgroup_lock);
865 fs_info->quota_enabled = 0;
866 fs_info->pending_quota_state = 0;
867 quota_root = fs_info->quota_root;
868 fs_info->quota_root = NULL;
869 btrfs_free_qgroup_config(fs_info);
870 spin_unlock(&fs_info->qgroup_lock);
875 ret = btrfs_clean_quota_tree(trans, quota_root);
879 ret = btrfs_del_root(trans, tree_root, "a_root->root_key);
883 list_del("a_root->dirty_list);
885 btrfs_tree_lock(quota_root->node);
886 clean_tree_block(trans, tree_root, quota_root->node);
887 btrfs_tree_unlock(quota_root->node);
888 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
890 free_extent_buffer(quota_root->node);
891 free_extent_buffer(quota_root->commit_root);
897 int btrfs_quota_rescan(struct btrfs_fs_info *fs_info)
903 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
904 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
906 struct btrfs_root *quota_root;
909 quota_root = fs_info->quota_root;
913 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
917 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
919 del_qgroup_relation_item(trans, quota_root, src, dst);
923 spin_lock(&fs_info->qgroup_lock);
924 ret = add_relation_rb(quota_root->fs_info, src, dst);
925 spin_unlock(&fs_info->qgroup_lock);
930 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
931 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
933 struct btrfs_root *quota_root;
937 quota_root = fs_info->quota_root;
941 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
942 err = del_qgroup_relation_item(trans, quota_root, dst, src);
946 spin_lock(&fs_info->qgroup_lock);
947 del_relation_rb(fs_info, src, dst);
949 spin_unlock(&fs_info->qgroup_lock);
954 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
955 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
957 struct btrfs_root *quota_root;
958 struct btrfs_qgroup *qgroup;
961 quota_root = fs_info->quota_root;
965 ret = add_qgroup_item(trans, quota_root, qgroupid);
967 spin_lock(&fs_info->qgroup_lock);
968 qgroup = add_qgroup_rb(fs_info, qgroupid);
969 spin_unlock(&fs_info->qgroup_lock);
972 ret = PTR_ERR(qgroup);
977 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
978 struct btrfs_fs_info *fs_info, u64 qgroupid)
980 struct btrfs_root *quota_root;
981 struct btrfs_qgroup *qgroup;
984 quota_root = fs_info->quota_root;
988 /* check if there are no relations to this qgroup */
989 spin_lock(&fs_info->qgroup_lock);
990 qgroup = find_qgroup_rb(fs_info, qgroupid);
992 if (!list_empty(&qgroup->groups) || !list_empty(&qgroup->members)) {
993 spin_unlock(&fs_info->qgroup_lock);
997 spin_unlock(&fs_info->qgroup_lock);
999 ret = del_qgroup_item(trans, quota_root, qgroupid);
1001 spin_lock(&fs_info->qgroup_lock);
1002 del_qgroup_rb(quota_root->fs_info, qgroupid);
1003 spin_unlock(&fs_info->qgroup_lock);
1008 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1009 struct btrfs_fs_info *fs_info, u64 qgroupid,
1010 struct btrfs_qgroup_limit *limit)
1012 struct btrfs_root *quota_root = fs_info->quota_root;
1013 struct btrfs_qgroup *qgroup;
1019 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1020 limit->flags, limit->max_rfer,
1021 limit->max_excl, limit->rsv_rfer,
1024 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1025 printk(KERN_INFO "unable to update quota limit for %llu\n",
1026 (unsigned long long)qgroupid);
1029 spin_lock(&fs_info->qgroup_lock);
1031 qgroup = find_qgroup_rb(fs_info, qgroupid);
1036 qgroup->lim_flags = limit->flags;
1037 qgroup->max_rfer = limit->max_rfer;
1038 qgroup->max_excl = limit->max_excl;
1039 qgroup->rsv_rfer = limit->rsv_rfer;
1040 qgroup->rsv_excl = limit->rsv_excl;
1043 spin_unlock(&fs_info->qgroup_lock);
1048 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1049 struct btrfs_qgroup *qgroup)
1051 if (list_empty(&qgroup->dirty))
1052 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1056 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1057 * the modification into a list that's later used by btrfs_end_transaction to
1058 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1060 int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1061 struct btrfs_delayed_ref_node *node,
1062 struct btrfs_delayed_extent_op *extent_op)
1064 struct qgroup_update *u;
1066 BUG_ON(!trans->delayed_ref_elem.seq);
1067 u = kmalloc(sizeof(*u), GFP_NOFS);
1072 u->extent_op = extent_op;
1073 list_add_tail(&u->list, &trans->qgroup_ref_list);
1079 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1080 * from the fs. First, all roots referencing the extent are searched, and
1081 * then the space is accounted accordingly to the different roots. The
1082 * accounting algorithm works in 3 steps documented inline.
1084 int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1085 struct btrfs_fs_info *fs_info,
1086 struct btrfs_delayed_ref_node *node,
1087 struct btrfs_delayed_extent_op *extent_op)
1089 struct btrfs_key ins;
1090 struct btrfs_root *quota_root;
1092 struct btrfs_qgroup *qgroup;
1093 struct ulist_node *unode;
1094 struct ulist *roots = NULL;
1095 struct ulist *tmp = NULL;
1096 struct ulist_iterator uiter;
1101 if (!fs_info->quota_enabled)
1104 BUG_ON(!fs_info->quota_root);
1106 ins.objectid = node->bytenr;
1107 ins.offset = node->num_bytes;
1108 ins.type = BTRFS_EXTENT_ITEM_KEY;
1110 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1111 node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
1112 struct btrfs_delayed_tree_ref *ref;
1113 ref = btrfs_delayed_node_to_tree_ref(node);
1114 ref_root = ref->root;
1115 } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1116 node->type == BTRFS_SHARED_DATA_REF_KEY) {
1117 struct btrfs_delayed_data_ref *ref;
1118 ref = btrfs_delayed_node_to_data_ref(node);
1119 ref_root = ref->root;
1124 if (!is_fstree(ref_root)) {
1126 * non-fs-trees are not being accounted
1131 switch (node->action) {
1132 case BTRFS_ADD_DELAYED_REF:
1133 case BTRFS_ADD_DELAYED_EXTENT:
1136 case BTRFS_DROP_DELAYED_REF:
1139 case BTRFS_UPDATE_DELAYED_HEAD:
1146 * the delayed ref sequence number we pass depends on the direction of
1147 * the operation. for add operations, we pass (node->seq - 1) to skip
1148 * the delayed ref's current sequence number, because we need the state
1149 * of the tree before the add operation. for delete operations, we pass
1150 * (node->seq) to include the delayed ref's current sequence number,
1151 * because we need the state of the tree after the delete operation.
1153 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr,
1154 sgn > 0 ? node->seq - 1 : node->seq, &roots);
1158 spin_lock(&fs_info->qgroup_lock);
1159 quota_root = fs_info->quota_root;
1163 qgroup = find_qgroup_rb(fs_info, ref_root);
1168 * step 1: for each old ref, visit all nodes once and inc refcnt
1170 tmp = ulist_alloc(GFP_ATOMIC);
1175 seq = fs_info->qgroup_seq;
1176 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1178 ULIST_ITER_INIT(&uiter);
1179 while ((unode = ulist_next(roots, &uiter))) {
1180 struct ulist_node *tmp_unode;
1181 struct ulist_iterator tmp_uiter;
1182 struct btrfs_qgroup *qg;
1184 qg = find_qgroup_rb(fs_info, unode->val);
1189 /* XXX id not needed */
1190 ulist_add(tmp, qg->qgroupid, (u64)(uintptr_t)qg, GFP_ATOMIC);
1191 ULIST_ITER_INIT(&tmp_uiter);
1192 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1193 struct btrfs_qgroup_list *glist;
1195 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1196 if (qg->refcnt < seq)
1197 qg->refcnt = seq + 1;
1201 list_for_each_entry(glist, &qg->groups, next_group) {
1202 ulist_add(tmp, glist->group->qgroupid,
1203 (u64)(uintptr_t)glist->group,
1210 * step 2: walk from the new root
1213 ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1214 ULIST_ITER_INIT(&uiter);
1215 while ((unode = ulist_next(tmp, &uiter))) {
1216 struct btrfs_qgroup *qg;
1217 struct btrfs_qgroup_list *glist;
1219 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1220 if (qg->refcnt < seq) {
1221 /* not visited by step 1 */
1222 qg->rfer += sgn * node->num_bytes;
1223 qg->rfer_cmpr += sgn * node->num_bytes;
1224 if (roots->nnodes == 0) {
1225 qg->excl += sgn * node->num_bytes;
1226 qg->excl_cmpr += sgn * node->num_bytes;
1228 qgroup_dirty(fs_info, qg);
1230 WARN_ON(qg->tag >= seq);
1233 list_for_each_entry(glist, &qg->groups, next_group) {
1234 ulist_add(tmp, glist->group->qgroupid,
1235 (uintptr_t)glist->group, GFP_ATOMIC);
1240 * step 3: walk again from old refs
1242 ULIST_ITER_INIT(&uiter);
1243 while ((unode = ulist_next(roots, &uiter))) {
1244 struct btrfs_qgroup *qg;
1245 struct ulist_node *tmp_unode;
1246 struct ulist_iterator tmp_uiter;
1248 qg = find_qgroup_rb(fs_info, unode->val);
1253 ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
1254 ULIST_ITER_INIT(&tmp_uiter);
1255 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1256 struct btrfs_qgroup_list *glist;
1258 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1262 if (qg->refcnt - seq == roots->nnodes) {
1263 qg->excl -= sgn * node->num_bytes;
1264 qg->excl_cmpr -= sgn * node->num_bytes;
1265 qgroup_dirty(fs_info, qg);
1268 list_for_each_entry(glist, &qg->groups, next_group) {
1269 ulist_add(tmp, glist->group->qgroupid,
1270 (uintptr_t)glist->group,
1277 spin_unlock(&fs_info->qgroup_lock);
1286 * called from commit_transaction. Writes all changed qgroups to disk.
1288 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1289 struct btrfs_fs_info *fs_info)
1291 struct btrfs_root *quota_root = fs_info->quota_root;
1297 fs_info->quota_enabled = fs_info->pending_quota_state;
1299 spin_lock(&fs_info->qgroup_lock);
1300 while (!list_empty(&fs_info->dirty_qgroups)) {
1301 struct btrfs_qgroup *qgroup;
1302 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1303 struct btrfs_qgroup, dirty);
1304 list_del_init(&qgroup->dirty);
1305 spin_unlock(&fs_info->qgroup_lock);
1306 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1308 fs_info->qgroup_flags |=
1309 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1310 spin_lock(&fs_info->qgroup_lock);
1312 if (fs_info->quota_enabled)
1313 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1315 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1316 spin_unlock(&fs_info->qgroup_lock);
1318 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1320 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1328 * copy the acounting information between qgroups. This is necessary when a
1329 * snapshot or a subvolume is created
1331 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1332 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1333 struct btrfs_qgroup_inherit *inherit)
1338 struct btrfs_root *quota_root = fs_info->quota_root;
1339 struct btrfs_qgroup *srcgroup;
1340 struct btrfs_qgroup *dstgroup;
1343 if (!fs_info->quota_enabled)
1350 * create a tracking group for the subvol itself
1352 ret = add_qgroup_item(trans, quota_root, objectid);
1356 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
1357 ret = update_qgroup_limit_item(trans, quota_root, objectid,
1359 inherit->lim.max_rfer,
1360 inherit->lim.max_excl,
1361 inherit->lim.rsv_rfer,
1362 inherit->lim.rsv_excl);
1368 struct btrfs_root *srcroot;
1369 struct btrfs_key srckey;
1372 srckey.objectid = srcid;
1373 srckey.type = BTRFS_ROOT_ITEM_KEY;
1374 srckey.offset = (u64)-1;
1375 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1376 if (IS_ERR(srcroot)) {
1377 ret = PTR_ERR(srcroot);
1382 srcroot_level = btrfs_header_level(srcroot->node);
1383 level_size = btrfs_level_size(srcroot, srcroot_level);
1388 * add qgroup to all inherited groups
1391 i_qgroups = (u64 *)(inherit + 1);
1392 for (i = 0; i < inherit->num_qgroups; ++i) {
1393 ret = add_qgroup_relation_item(trans, quota_root,
1394 objectid, *i_qgroups);
1397 ret = add_qgroup_relation_item(trans, quota_root,
1398 *i_qgroups, objectid);
1406 spin_lock(&fs_info->qgroup_lock);
1408 dstgroup = add_qgroup_rb(fs_info, objectid);
1409 if (IS_ERR(dstgroup)) {
1410 ret = PTR_ERR(dstgroup);
1415 srcgroup = find_qgroup_rb(fs_info, srcid);
1418 dstgroup->rfer = srcgroup->rfer - level_size;
1419 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
1420 srcgroup->excl = level_size;
1421 srcgroup->excl_cmpr = level_size;
1422 qgroup_dirty(fs_info, dstgroup);
1423 qgroup_dirty(fs_info, srcgroup);
1429 i_qgroups = (u64 *)(inherit + 1);
1430 for (i = 0; i < inherit->num_qgroups; ++i) {
1431 ret = add_relation_rb(quota_root->fs_info, objectid,
1438 for (i = 0; i < inherit->num_ref_copies; ++i) {
1439 struct btrfs_qgroup *src;
1440 struct btrfs_qgroup *dst;
1442 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1443 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1450 dst->rfer = src->rfer - level_size;
1451 dst->rfer_cmpr = src->rfer_cmpr - level_size;
1454 for (i = 0; i < inherit->num_excl_copies; ++i) {
1455 struct btrfs_qgroup *src;
1456 struct btrfs_qgroup *dst;
1458 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1459 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1466 dst->excl = src->excl + level_size;
1467 dst->excl_cmpr = src->excl_cmpr + level_size;
1472 spin_unlock(&fs_info->qgroup_lock);
1478 * reserve some space for a qgroup and all its parents. The reservation takes
1479 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1480 * accounting. If not enough space is available, EDQUOT is returned.
1481 * We assume that the requested space is new for all qgroups.
1483 int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1485 struct btrfs_root *quota_root;
1486 struct btrfs_qgroup *qgroup;
1487 struct btrfs_fs_info *fs_info = root->fs_info;
1488 u64 ref_root = root->root_key.objectid;
1490 struct ulist *ulist = NULL;
1491 struct ulist_node *unode;
1492 struct ulist_iterator uiter;
1494 if (!is_fstree(ref_root))
1500 spin_lock(&fs_info->qgroup_lock);
1501 quota_root = fs_info->quota_root;
1505 qgroup = find_qgroup_rb(fs_info, ref_root);
1510 * in a first step, we check all affected qgroups if any limits would
1513 ulist = ulist_alloc(GFP_ATOMIC);
1518 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1519 ULIST_ITER_INIT(&uiter);
1520 while ((unode = ulist_next(ulist, &uiter))) {
1521 struct btrfs_qgroup *qg;
1522 struct btrfs_qgroup_list *glist;
1524 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1526 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1527 qg->reserved + qg->rfer + num_bytes >
1531 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1532 qg->reserved + qg->excl + num_bytes >
1536 list_for_each_entry(glist, &qg->groups, next_group) {
1537 ulist_add(ulist, glist->group->qgroupid,
1538 (uintptr_t)glist->group, GFP_ATOMIC);
1545 * no limits exceeded, now record the reservation into all qgroups
1547 ULIST_ITER_INIT(&uiter);
1548 while ((unode = ulist_next(ulist, &uiter))) {
1549 struct btrfs_qgroup *qg;
1551 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1553 qg->reserved += num_bytes;
1557 spin_unlock(&fs_info->qgroup_lock);
1563 void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
1565 struct btrfs_root *quota_root;
1566 struct btrfs_qgroup *qgroup;
1567 struct btrfs_fs_info *fs_info = root->fs_info;
1568 struct ulist *ulist = NULL;
1569 struct ulist_node *unode;
1570 struct ulist_iterator uiter;
1571 u64 ref_root = root->root_key.objectid;
1573 if (!is_fstree(ref_root))
1579 spin_lock(&fs_info->qgroup_lock);
1581 quota_root = fs_info->quota_root;
1585 qgroup = find_qgroup_rb(fs_info, ref_root);
1589 ulist = ulist_alloc(GFP_ATOMIC);
1591 btrfs_std_error(fs_info, -ENOMEM);
1594 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1595 ULIST_ITER_INIT(&uiter);
1596 while ((unode = ulist_next(ulist, &uiter))) {
1597 struct btrfs_qgroup *qg;
1598 struct btrfs_qgroup_list *glist;
1600 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1602 qg->reserved -= num_bytes;
1604 list_for_each_entry(glist, &qg->groups, next_group) {
1605 ulist_add(ulist, glist->group->qgroupid,
1606 (uintptr_t)glist->group, GFP_ATOMIC);
1611 spin_unlock(&fs_info->qgroup_lock);
1615 void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
1617 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
1619 printk(KERN_ERR "btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %llu\n",
1620 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
1621 trans->delayed_ref_elem.seq);