2 * Copyright (C) 2012 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.
20 #include <sys/ioctl.h>
26 #define BTRFS_QGROUP_NFILTERS_INCREASE (2 * BTRFS_QGROUP_FILTER_MAX)
27 #define BTRFS_QGROUP_NCOMPS_INCREASE (2 * BTRFS_QGROUP_COMP_MAX)
29 struct qgroup_lookup {
34 struct rb_node rb_node;
35 struct rb_node sort_node;
37 *all_parent_node is used to
38 *filter a qgroup's all parent
40 struct rb_node all_parent_node;
47 u64 rfer; /*referenced*/
48 u64 rfer_cmpr; /*referenced compressed*/
49 u64 excl; /*exclusive*/
50 u64 excl_cmpr; /*exclusive compressed*/
55 u64 flags; /*which limits are set*/
61 /*qgroups this group is member of*/
62 struct list_head qgroups;
63 /*qgroups that are members of this group*/
64 struct list_head members;
68 * glue structure to represent the relations
71 struct btrfs_qgroup_list {
72 struct list_head next_qgroup;
73 struct list_head next_member;
74 struct btrfs_qgroup *qgroup;
75 struct btrfs_qgroup *member;
79 * qgroupid,rfer,excl default to set
87 } btrfs_qgroup_columns[] = {
90 .column_name = "Qgroupid",
97 .column_name = "Rfer",
99 .unit_mode = UNITS_DEFAULT,
104 .column_name = "Excl",
106 .unit_mode = UNITS_DEFAULT,
109 { .name = "max_rfer",
110 .column_name = "Max_rfer",
112 .unit_mode = UNITS_DEFAULT,
117 .column_name = "Max_excl",
119 .unit_mode = UNITS_DEFAULT,
124 .column_name = "Parent",
131 .column_name = "Child",
144 static btrfs_qgroup_filter_func all_filter_funcs[];
145 static btrfs_qgroup_comp_func all_comp_funcs[];
147 void btrfs_qgroup_setup_print_column(enum btrfs_qgroup_column_enum column)
151 BUG_ON(column < 0 || column > BTRFS_QGROUP_ALL);
153 if (column < BTRFS_QGROUP_ALL) {
154 btrfs_qgroup_columns[column].need_print = 1;
157 for (i = 0; i < BTRFS_QGROUP_ALL; i++)
158 btrfs_qgroup_columns[i].need_print = 1;
161 void btrfs_qgroup_setup_units(unsigned unit_mode)
163 btrfs_qgroup_columns[BTRFS_QGROUP_RFER].unit_mode = unit_mode;
164 btrfs_qgroup_columns[BTRFS_QGROUP_EXCL].unit_mode = unit_mode;
165 btrfs_qgroup_columns[BTRFS_QGROUP_MAX_RFER].unit_mode = unit_mode;
166 btrfs_qgroup_columns[BTRFS_QGROUP_MAX_EXCL].unit_mode = unit_mode;
169 static int print_parent_column(struct btrfs_qgroup *qgroup)
171 struct btrfs_qgroup_list *list = NULL;
174 list_for_each_entry(list, &qgroup->qgroups, next_qgroup) {
175 len += printf("%llu/%llu",
176 btrfs_qgroup_level(list->qgroup->qgroupid),
177 btrfs_qgroup_subvid(list->qgroup->qgroupid));
178 if (!list_is_last(&list->next_qgroup, &qgroup->qgroups))
181 if (list_empty(&qgroup->qgroups))
182 len += printf("---");
187 static int print_child_column(struct btrfs_qgroup *qgroup)
189 struct btrfs_qgroup_list *list = NULL;
192 list_for_each_entry(list, &qgroup->members, next_member) {
193 len += printf("%llu/%llu",
194 btrfs_qgroup_level(list->member->qgroupid),
195 btrfs_qgroup_subvid(list->member->qgroupid));
196 if (!list_is_last(&list->next_member, &qgroup->members))
199 if (list_empty(&qgroup->members))
200 len += printf("---");
205 static void print_qgroup_column_add_blank(enum btrfs_qgroup_column_enum column,
208 len = btrfs_qgroup_columns[column].max_len - len;
213 static void print_qgroup_column(struct btrfs_qgroup *qgroup,
214 enum btrfs_qgroup_column_enum column)
216 BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
218 int unit_mode = btrfs_qgroup_columns[column].unit_mode;
219 int max_len = btrfs_qgroup_columns[column].max_len;
223 case BTRFS_QGROUP_QGROUPID:
224 len = printf("%llu/%llu",
225 btrfs_qgroup_level(qgroup->qgroupid),
226 btrfs_qgroup_subvid(qgroup->qgroupid));
227 print_qgroup_column_add_blank(BTRFS_QGROUP_QGROUPID, len);
229 case BTRFS_QGROUP_RFER:
230 len = printf("%*s", max_len, pretty_size_mode(qgroup->rfer, unit_mode));
232 case BTRFS_QGROUP_EXCL:
233 len = printf("%*s", max_len, pretty_size_mode(qgroup->excl, unit_mode));
235 case BTRFS_QGROUP_PARENT:
236 len = print_parent_column(qgroup);
237 print_qgroup_column_add_blank(BTRFS_QGROUP_PARENT, len);
239 case BTRFS_QGROUP_MAX_RFER:
240 if (qgroup->flags & BTRFS_QGROUP_LIMIT_MAX_RFER)
241 len = printf("%*s", max_len, pretty_size_mode(qgroup->max_rfer, unit_mode));
243 len = printf("%*s", max_len, "none");
245 case BTRFS_QGROUP_MAX_EXCL:
246 if (qgroup->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
247 len = printf("%*s", max_len, pretty_size_mode(qgroup->max_excl, unit_mode));
249 len = printf("%*s", max_len, "none");
251 case BTRFS_QGROUP_CHILD:
252 len = print_child_column(qgroup);
253 print_qgroup_column_add_blank(BTRFS_QGROUP_CHILD, len);
260 static void print_single_qgroup_table(struct btrfs_qgroup *qgroup)
264 for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
265 if (!btrfs_qgroup_columns[i].need_print)
267 print_qgroup_column(qgroup, i);
269 if (i != BTRFS_QGROUP_CHILD)
275 static void print_table_head()
281 for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
282 max_len = btrfs_qgroup_columns[i].max_len;
283 if (!btrfs_qgroup_columns[i].need_print)
285 if ((i == BTRFS_QGROUP_QGROUPID) | (i == BTRFS_QGROUP_PARENT) |
286 (i == BTRFS_QGROUP_CHILD))
287 printf("%-*s", max_len, btrfs_qgroup_columns[i].name);
289 printf("%*s", max_len, btrfs_qgroup_columns[i].name);
293 for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
294 max_len = btrfs_qgroup_columns[i].max_len;
295 if (!btrfs_qgroup_columns[i].need_print)
297 if ((i == BTRFS_QGROUP_QGROUPID) | (i == BTRFS_QGROUP_PARENT) |
298 (i == BTRFS_QGROUP_CHILD)) {
299 len = strlen(btrfs_qgroup_columns[i].name);
302 len = max_len - strlen(btrfs_qgroup_columns[i].name);
306 len = max_len - strlen(btrfs_qgroup_columns[i].name);
309 len = strlen(btrfs_qgroup_columns[i].name);
318 static void qgroup_lookup_init(struct qgroup_lookup *tree)
320 tree->root.rb_node = NULL;
323 static int comp_entry_with_qgroupid(struct btrfs_qgroup *entry1,
324 struct btrfs_qgroup *entry2,
330 if (entry1->qgroupid > entry2->qgroupid)
332 else if (entry1->qgroupid < entry2->qgroupid)
337 return is_descending ? -ret : ret;
340 static int comp_entry_with_rfer(struct btrfs_qgroup *entry1,
341 struct btrfs_qgroup *entry2,
346 if (entry1->rfer > entry2->rfer)
348 else if (entry1->rfer < entry2->rfer)
353 return is_descending ? -ret : ret;
356 static int comp_entry_with_excl(struct btrfs_qgroup *entry1,
357 struct btrfs_qgroup *entry2,
362 if (entry1->excl > entry2->excl)
364 else if (entry1->excl < entry2->excl)
369 return is_descending ? -ret : ret;
372 static int comp_entry_with_max_rfer(struct btrfs_qgroup *entry1,
373 struct btrfs_qgroup *entry2,
378 if (entry1->max_rfer > entry2->max_rfer)
380 else if (entry1->max_rfer < entry2->max_rfer)
385 return is_descending ? -ret : ret;
388 static int comp_entry_with_max_excl(struct btrfs_qgroup *entry1,
389 struct btrfs_qgroup *entry2,
394 if (entry1->max_excl > entry2->max_excl)
396 else if (entry1->max_excl < entry2->max_excl)
401 return is_descending ? -ret : ret;
404 static btrfs_qgroup_comp_func all_comp_funcs[] = {
405 [BTRFS_QGROUP_COMP_QGROUPID] = comp_entry_with_qgroupid,
406 [BTRFS_QGROUP_COMP_RFER] = comp_entry_with_rfer,
407 [BTRFS_QGROUP_COMP_EXCL] = comp_entry_with_excl,
408 [BTRFS_QGROUP_COMP_MAX_RFER] = comp_entry_with_max_rfer,
409 [BTRFS_QGROUP_COMP_MAX_EXCL] = comp_entry_with_max_excl
412 static char *all_sort_items[] = {
413 [BTRFS_QGROUP_COMP_QGROUPID] = "qgroupid",
414 [BTRFS_QGROUP_COMP_RFER] = "rfer",
415 [BTRFS_QGROUP_COMP_EXCL] = "excl",
416 [BTRFS_QGROUP_COMP_MAX_RFER] = "max_rfer",
417 [BTRFS_QGROUP_COMP_MAX_EXCL] = "max_excl",
418 [BTRFS_QGROUP_COMP_MAX] = NULL,
421 static int btrfs_qgroup_get_sort_item(char *sort_name)
425 for (i = 0; i < BTRFS_QGROUP_COMP_MAX; i++) {
426 if (strcmp(sort_name, all_sort_items[i]) == 0)
432 struct btrfs_qgroup_comparer_set *btrfs_qgroup_alloc_comparer_set(void)
434 struct btrfs_qgroup_comparer_set *set;
436 size = sizeof(struct btrfs_qgroup_comparer_set) +
437 BTRFS_QGROUP_NCOMPS_INCREASE *
438 sizeof(struct btrfs_qgroup_comparer);
441 fprintf(stderr, "memory allocation failed\n");
445 memset(set, 0, size);
446 set->total = BTRFS_QGROUP_NCOMPS_INCREASE;
451 void btrfs_qgroup_free_comparer_set(struct btrfs_qgroup_comparer_set *comp_set)
456 int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set **comp_set,
457 enum btrfs_qgroup_comp_enum comparer,
460 struct btrfs_qgroup_comparer_set *set = *comp_set;
464 BUG_ON(comparer >= BTRFS_QGROUP_COMP_MAX);
465 BUG_ON(set->ncomps > set->total);
467 if (set->ncomps == set->total) {
470 size = set->total + BTRFS_QGROUP_NCOMPS_INCREASE;
471 size = sizeof(*set) +
472 size * sizeof(struct btrfs_qgroup_comparer);
474 set = realloc(set, size);
476 fprintf(stderr, "memory allocation failed\n");
481 memset(&set->comps[set->total], 0,
482 BTRFS_QGROUP_NCOMPS_INCREASE *
483 sizeof(struct btrfs_qgroup_comparer));
484 set->total += BTRFS_QGROUP_NCOMPS_INCREASE;
488 BUG_ON(set->comps[set->ncomps].comp_func);
490 set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
491 set->comps[set->ncomps].is_descending = is_descending;
496 static int sort_comp(struct btrfs_qgroup *entry1, struct btrfs_qgroup *entry2,
497 struct btrfs_qgroup_comparer_set *set)
499 int qgroupid_compared = 0;
502 if (!set || !set->ncomps)
505 for (i = 0; i < set->ncomps; i++) {
506 if (!set->comps[i].comp_func)
509 ret = set->comps[i].comp_func(entry1, entry2,
510 set->comps[i].is_descending);
514 if (set->comps[i].comp_func == comp_entry_with_qgroupid)
515 qgroupid_compared = 1;
518 if (!qgroupid_compared) {
520 ret = comp_entry_with_qgroupid(entry1, entry2, 0);
527 * insert a new root into the tree. returns the existing root entry
528 * if one is already there. qgroupid is used
531 static int qgroup_tree_insert(struct qgroup_lookup *root_tree,
532 struct btrfs_qgroup *ins)
535 struct rb_node **p = &root_tree->root.rb_node;
536 struct rb_node *parent = NULL;
537 struct btrfs_qgroup *curr;
542 curr = rb_entry(parent, struct btrfs_qgroup, rb_node);
544 ret = comp_entry_with_qgroupid(ins, curr, 0);
552 rb_link_node(&ins->rb_node, parent, p);
553 rb_insert_color(&ins->rb_node, &root_tree->root);
558 *find a given qgroupid in the tree. We return the smallest one,
559 *rb_next can be used to move forward looking for more if required
561 static struct btrfs_qgroup *qgroup_tree_search(struct qgroup_lookup *root_tree,
564 struct rb_node *n = root_tree->root.rb_node;
565 struct btrfs_qgroup *entry;
566 struct btrfs_qgroup tmp;
569 tmp.qgroupid = qgroupid;
572 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
574 ret = comp_entry_with_qgroupid(&tmp, entry, 0);
586 static int update_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
587 u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
588 u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
589 u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *pa,
590 struct btrfs_qgroup *child)
592 struct btrfs_qgroup *bq;
593 struct btrfs_qgroup_list *list;
595 bq = qgroup_tree_search(qgroup_lookup, qgroupid);
596 if (!bq || bq->qgroupid != qgroupid)
600 bq->generation = generation;
604 bq->rfer_cmpr = rfer_cmpr;
608 bq->excl_cmpr = excl_cmpr;
612 bq->max_rfer = max_rfer;
614 bq->max_excl = max_excl;
616 bq->rsv_rfer = rsv_rfer;
618 list = malloc(sizeof(*list));
620 fprintf(stderr, "memory allocation failed\n");
624 list->member = child;
625 list_add_tail(&list->next_qgroup, &child->qgroups);
626 list_add_tail(&list->next_member, &pa->members);
631 static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
632 u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
633 u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
634 u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *parent,
635 struct btrfs_qgroup *child)
637 struct btrfs_qgroup *bq;
638 struct btrfs_qgroup_list *list;
641 ret = update_qgroup(qgroup_lookup, qgroupid, generation, rfer,
642 rfer_cmpr, excl, excl_cmpr, flags, max_rfer,
643 max_excl, rsv_rfer, rsv_excl, parent, child);
647 bq = malloc(sizeof(*bq));
649 printf("memory allocation failed\n");
652 memset(bq, 0, sizeof(*bq));
654 bq->qgroupid = qgroupid;
655 INIT_LIST_HEAD(&bq->qgroups);
656 INIT_LIST_HEAD(&bq->members);
659 bq->generation = generation;
663 bq->rfer_cmpr = rfer_cmpr;
667 bq->excl_cmpr = excl_cmpr;
671 bq->max_rfer = max_rfer;
673 bq->max_excl = max_excl;
675 bq->rsv_rfer = rsv_rfer;
676 if (parent && child) {
677 list = malloc(sizeof(*list));
679 fprintf(stderr, "memory allocation failed\n");
682 list->qgroup = parent;
683 list->member = child;
684 list_add_tail(&list->next_qgroup, &child->qgroups);
685 list_add_tail(&list->next_member, &parent->members);
687 ret = qgroup_tree_insert(qgroup_lookup, bq);
689 printf("failed to insert tree %llu\n",
696 static void __free_btrfs_qgroup(struct btrfs_qgroup *bq)
698 struct btrfs_qgroup_list *list;
699 while (!list_empty(&bq->qgroups)) {
700 list = list_entry((&bq->qgroups)->next,
701 struct btrfs_qgroup_list,
703 list_del(&list->next_qgroup);
704 list_del(&list->next_member);
707 while (!list_empty(&bq->members)) {
708 list = list_entry((&bq->members)->next,
709 struct btrfs_qgroup_list,
711 list_del(&list->next_qgroup);
712 list_del(&list->next_member);
718 static void __free_all_qgroups(struct qgroup_lookup *root_tree)
720 struct btrfs_qgroup *entry;
723 n = rb_first(&root_tree->root);
725 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
726 rb_erase(n, &root_tree->root);
727 __free_btrfs_qgroup(entry);
729 n = rb_first(&root_tree->root);
733 static int filter_all_parent_insert(struct qgroup_lookup *sort_tree,
734 struct btrfs_qgroup *bq)
736 struct rb_node **p = &sort_tree->root.rb_node;
737 struct rb_node *parent = NULL;
738 struct btrfs_qgroup *curr;
743 curr = rb_entry(parent, struct btrfs_qgroup, all_parent_node);
745 ret = comp_entry_with_qgroupid(bq, curr, 0);
753 rb_link_node(&bq->all_parent_node, parent, p);
754 rb_insert_color(&bq->all_parent_node, &sort_tree->root);
758 static int filter_by_parent(struct btrfs_qgroup *bq, u64 data)
760 struct btrfs_qgroup *qgroup =
761 (struct btrfs_qgroup *)(unsigned long)data;
765 if (qgroup->qgroupid == bq->qgroupid)
770 static int filter_by_all_parent(struct btrfs_qgroup *bq, u64 data)
772 struct qgroup_lookup lookup;
773 struct qgroup_lookup *ql = &lookup;
774 struct btrfs_qgroup_list *list;
776 struct btrfs_qgroup *qgroup =
777 (struct btrfs_qgroup *)(unsigned long)data;
781 if (bq->qgroupid == qgroup->qgroupid)
784 qgroup_lookup_init(ql);
785 filter_all_parent_insert(ql, qgroup);
786 n = rb_first(&ql->root);
788 qgroup = rb_entry(n, struct btrfs_qgroup, all_parent_node);
789 if (!list_empty(&qgroup->qgroups)) {
790 list_for_each_entry(list, &qgroup->qgroups,
792 if ((list->qgroup)->qgroupid == bq->qgroupid)
794 filter_all_parent_insert(ql, list->qgroup);
797 rb_erase(n, &ql->root);
798 n = rb_first(&ql->root);
803 static btrfs_qgroup_filter_func all_filter_funcs[] = {
804 [BTRFS_QGROUP_FILTER_PARENT] = filter_by_parent,
805 [BTRFS_QGROUP_FILTER_ALL_PARENT] = filter_by_all_parent,
808 struct btrfs_qgroup_filter_set *btrfs_qgroup_alloc_filter_set(void)
810 struct btrfs_qgroup_filter_set *set;
813 size = sizeof(struct btrfs_qgroup_filter_set) +
814 BTRFS_QGROUP_NFILTERS_INCREASE *
815 sizeof(struct btrfs_qgroup_filter);
818 fprintf(stderr, "memory allocation failed\n");
821 memset(set, 0, size);
822 set->total = BTRFS_QGROUP_NFILTERS_INCREASE;
827 void btrfs_qgroup_free_filter_set(struct btrfs_qgroup_filter_set *filter_set)
832 int btrfs_qgroup_setup_filter(struct btrfs_qgroup_filter_set **filter_set,
833 enum btrfs_qgroup_filter_enum filter, u64 data)
835 struct btrfs_qgroup_filter_set *set = *filter_set;
839 BUG_ON(filter >= BTRFS_QGROUP_FILTER_MAX);
840 BUG_ON(set->nfilters > set->total);
842 if (set->nfilters == set->total) {
845 size = set->total + BTRFS_QGROUP_NFILTERS_INCREASE;
846 size = sizeof(*set) + size * sizeof(struct btrfs_qgroup_filter);
849 set = realloc(set, size);
851 fprintf(stderr, "memory allocation failed\n");
855 memset(&set->filters[set->total], 0,
856 BTRFS_QGROUP_NFILTERS_INCREASE *
857 sizeof(struct btrfs_qgroup_filter));
858 set->total += BTRFS_QGROUP_NFILTERS_INCREASE;
861 BUG_ON(set->filters[set->nfilters].filter_func);
862 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
863 set->filters[set->nfilters].data = data;
868 static int filter_qgroup(struct btrfs_qgroup *bq,
869 struct btrfs_qgroup_filter_set *set)
873 if (!set || !set->nfilters)
875 for (i = 0; i < set->nfilters; i++) {
876 if (!set->filters[i].filter_func)
878 ret = set->filters[i].filter_func(bq, set->filters[i].data);
885 static void pre_process_filter_set(struct qgroup_lookup *lookup,
886 struct btrfs_qgroup_filter_set *set)
889 struct btrfs_qgroup *qgroup_for_filter = NULL;
891 for (i = 0; i < set->nfilters; i++) {
893 if (set->filters[i].filter_func == filter_by_all_parent
894 || set->filters[i].filter_func == filter_by_parent) {
895 qgroup_for_filter = qgroup_tree_search(lookup,
896 set->filters[i].data);
897 set->filters[i].data =
898 (u64)(unsigned long)qgroup_for_filter;
903 static int sort_tree_insert(struct qgroup_lookup *sort_tree,
904 struct btrfs_qgroup *bq,
905 struct btrfs_qgroup_comparer_set *comp_set)
907 struct rb_node **p = &sort_tree->root.rb_node;
908 struct rb_node *parent = NULL;
909 struct btrfs_qgroup *curr;
914 curr = rb_entry(parent, struct btrfs_qgroup, sort_node);
916 ret = sort_comp(bq, curr, comp_set);
924 rb_link_node(&bq->sort_node, parent, p);
925 rb_insert_color(&bq->sort_node, &sort_tree->root);
929 static void __update_columns_max_len(struct btrfs_qgroup *bq,
930 enum btrfs_qgroup_column_enum column)
932 BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
933 struct btrfs_qgroup_list *list = NULL;
936 unsigned unit_mode = btrfs_qgroup_columns[column].unit_mode;
940 case BTRFS_QGROUP_QGROUPID:
941 sprintf(tmp, "%llu/%llu",
942 btrfs_qgroup_level(bq->qgroupid),
943 btrfs_qgroup_subvid(bq->qgroupid));
945 if (btrfs_qgroup_columns[column].max_len < len)
946 btrfs_qgroup_columns[column].max_len = len;
948 case BTRFS_QGROUP_RFER:
949 len = strlen(pretty_size_mode(bq->rfer, unit_mode));
950 if (btrfs_qgroup_columns[column].max_len < len)
951 btrfs_qgroup_columns[column].max_len = len;
953 case BTRFS_QGROUP_EXCL:
954 len = strlen(pretty_size_mode(bq->excl, unit_mode));
955 if (btrfs_qgroup_columns[column].max_len < len)
956 btrfs_qgroup_columns[column].max_len = len;
958 case BTRFS_QGROUP_MAX_RFER:
959 len = strlen(pretty_size_mode(bq->max_rfer, unit_mode));
960 if (btrfs_qgroup_columns[column].max_len < len)
961 btrfs_qgroup_columns[column].max_len = len;
963 case BTRFS_QGROUP_MAX_EXCL:
964 len = strlen(pretty_size_mode(bq->max_excl, unit_mode));
965 if (btrfs_qgroup_columns[column].max_len < len)
966 btrfs_qgroup_columns[column].max_len = len;
968 case BTRFS_QGROUP_PARENT:
970 list_for_each_entry(list, &bq->qgroups, next_qgroup) {
971 len += sprintf(tmp, "%llu/%llu",
972 btrfs_qgroup_level(list->qgroup->qgroupid),
973 btrfs_qgroup_subvid(list->qgroup->qgroupid));
974 if (!list_is_last(&list->next_qgroup, &bq->qgroups))
977 if (btrfs_qgroup_columns[column].max_len < len)
978 btrfs_qgroup_columns[column].max_len = len;
980 case BTRFS_QGROUP_CHILD:
982 list_for_each_entry(list, &bq->members, next_member) {
983 len += sprintf(tmp, "%llu/%llu",
984 btrfs_qgroup_level(list->member->qgroupid),
985 btrfs_qgroup_subvid(list->member->qgroupid));
986 if (!list_is_last(&list->next_member, &bq->members))
989 if (btrfs_qgroup_columns[column].max_len < len)
990 btrfs_qgroup_columns[column].max_len = len;
998 static void update_columns_max_len(struct btrfs_qgroup *bq)
1002 for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
1003 if (!btrfs_qgroup_columns[i].need_print)
1005 __update_columns_max_len(bq, i);
1009 static void __filter_and_sort_qgroups(struct qgroup_lookup *all_qgroups,
1010 struct qgroup_lookup *sort_tree,
1011 struct btrfs_qgroup_filter_set *filter_set,
1012 struct btrfs_qgroup_comparer_set *comp_set)
1015 struct btrfs_qgroup *entry;
1018 qgroup_lookup_init(sort_tree);
1019 pre_process_filter_set(all_qgroups, filter_set);
1021 n = rb_last(&all_qgroups->root);
1023 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
1025 ret = filter_qgroup(entry, filter_set);
1027 sort_tree_insert(sort_tree, entry, comp_set);
1029 update_columns_max_len(entry);
1035 static inline void print_status_flag_warning(u64 flags)
1037 if (!(flags & BTRFS_QGROUP_STATUS_FLAG_ON))
1039 "WARNING: Quota disabled, qgroup data may be out of date\n");
1040 else if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
1042 "WARNING: Rescan is running, qgroup data may be incorrect\n");
1043 else if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
1045 "WARNING: Qgroup data inconsistent, rescan recommended\n");
1048 static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
1051 struct btrfs_ioctl_search_args args;
1052 struct btrfs_ioctl_search_key *sk = &args.key;
1053 struct btrfs_ioctl_search_header *sh;
1054 unsigned long off = 0;
1057 struct btrfs_qgroup_info_item *info;
1058 struct btrfs_qgroup_limit_item *limit;
1059 struct btrfs_qgroup *bq;
1060 struct btrfs_qgroup *bq1;
1067 memset(&args, 0, sizeof(args));
1069 sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
1070 sk->max_type = BTRFS_QGROUP_RELATION_KEY;
1071 sk->min_type = BTRFS_QGROUP_STATUS_KEY;
1072 sk->max_objectid = (u64)-1;
1073 sk->max_offset = (u64)-1;
1074 sk->max_transid = (u64)-1;
1075 sk->nr_items = 4096;
1077 qgroup_lookup_init(qgroup_lookup);
1080 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1084 "ERROR: can't perform the search - %s\n",
1088 /* the ioctl returns the number of item it found in nr_items */
1089 if (sk->nr_items == 0)
1094 * for each item, pull the key out of the header and then
1095 * read the root_ref item it contains
1097 for (i = 0; i < sk->nr_items; i++) {
1098 sh = (struct btrfs_ioctl_search_header *)(args.buf +
1102 if (sh->type == BTRFS_QGROUP_STATUS_KEY) {
1103 struct btrfs_qgroup_status_item *si;
1106 si = (struct btrfs_qgroup_status_item *)
1108 flags = btrfs_stack_qgroup_status_flags(si);
1109 print_status_flag_warning(flags);
1110 } else if (sh->type == BTRFS_QGROUP_INFO_KEY) {
1111 info = (struct btrfs_qgroup_info_item *)
1113 a1 = btrfs_stack_qgroup_info_generation(info);
1114 a2 = btrfs_stack_qgroup_info_referenced(info);
1116 btrfs_stack_qgroup_info_referenced_compressed
1118 a4 = btrfs_stack_qgroup_info_exclusive(info);
1120 btrfs_stack_qgroup_info_exclusive_compressed
1122 add_qgroup(qgroup_lookup, sh->offset, a1, a2,
1123 a3, a4, a5, 0, 0, 0, 0, 0, 0, 0);
1124 } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
1125 limit = (struct btrfs_qgroup_limit_item *)
1128 a1 = btrfs_stack_qgroup_limit_flags(limit);
1129 a2 = btrfs_stack_qgroup_limit_max_referenced
1131 a3 = btrfs_stack_qgroup_limit_max_exclusive
1133 a4 = btrfs_stack_qgroup_limit_rsv_referenced
1135 a5 = btrfs_stack_qgroup_limit_rsv_exclusive
1137 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
1138 0, 0, 0, a1, a2, a3, a4, a5, 0, 0);
1139 } else if (sh->type == BTRFS_QGROUP_RELATION_KEY) {
1140 if (sh->offset < sh->objectid)
1142 bq = qgroup_tree_search(qgroup_lookup,
1146 bq1 = qgroup_tree_search(qgroup_lookup,
1150 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
1151 0, 0, 0, 0, 0, 0, 0, 0, bq, bq1);
1158 * record the mins in sk so we can make sure the
1159 * next search doesn't repeat this root
1161 sk->min_type = sh->type;
1162 sk->min_offset = sh->offset;
1163 sk->min_objectid = sh->objectid;
1165 sk->nr_items = 4096;
1167 * this iteration is done, step forward one qgroup for the next
1170 if (sk->min_offset < (u64)-1)
1180 static void print_all_qgroups(struct qgroup_lookup *qgroup_lookup)
1184 struct btrfs_qgroup *entry;
1188 n = rb_first(&qgroup_lookup->root);
1190 entry = rb_entry(n, struct btrfs_qgroup, sort_node);
1191 print_single_qgroup_table(entry);
1196 int btrfs_show_qgroups(int fd,
1197 struct btrfs_qgroup_filter_set *filter_set,
1198 struct btrfs_qgroup_comparer_set *comp_set)
1201 struct qgroup_lookup qgroup_lookup;
1202 struct qgroup_lookup sort_tree;
1205 ret = __qgroups_search(fd, &qgroup_lookup);
1208 __filter_and_sort_qgroups(&qgroup_lookup, &sort_tree,
1209 filter_set, comp_set);
1210 print_all_qgroups(&sort_tree);
1212 __free_all_qgroups(&qgroup_lookup);
1213 btrfs_qgroup_free_filter_set(filter_set);
1217 u64 btrfs_get_path_rootid(int fd)
1220 struct btrfs_ioctl_ino_lookup_args args;
1222 memset(&args, 0, sizeof(args));
1223 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1225 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1228 "ERROR: can't perform the search - %s\n",
1235 int btrfs_qgroup_parse_sort_string(char *opt_arg,
1236 struct btrfs_qgroup_comparer_set **comps)
1244 while ((p = strtok(opt_arg, ",")) != NULL) {
1246 ptr_argv = all_sort_items;
1249 if (strcmp(*ptr_argv, p) == 0) {
1254 if (strcmp(*ptr_argv, p) == 0) {
1271 } else if (*p == '-') {
1277 what_to_sort = btrfs_qgroup_get_sort_item(p);
1278 if (what_to_sort < 0)
1280 btrfs_qgroup_setup_comparer(comps, what_to_sort, order);
1288 int qgroup_inherit_size(struct btrfs_qgroup_inherit *p)
1290 return sizeof(*p) + sizeof(p->qgroups[0]) *
1291 (p->num_qgroups + 2 * p->num_ref_copies +
1292 2 * p->num_excl_copies);
1296 qgroup_inherit_realloc(struct btrfs_qgroup_inherit **inherit, int n, int pos)
1298 struct btrfs_qgroup_inherit *out;
1302 nitems = (*inherit)->num_qgroups +
1303 (*inherit)->num_ref_copies +
1304 (*inherit)->num_excl_copies;
1307 out = calloc(sizeof(*out) + sizeof(out->qgroups[0]) * (nitems + n), 1);
1309 fprintf(stderr, "ERROR: Not enough memory\n");
1314 struct btrfs_qgroup_inherit *i = *inherit;
1315 int s = sizeof(out->qgroups[0]);
1317 out->num_qgroups = i->num_qgroups;
1318 out->num_ref_copies = i->num_ref_copies;
1319 out->num_excl_copies = i->num_excl_copies;
1320 memcpy(out->qgroups, i->qgroups, pos * s);
1321 memcpy(out->qgroups + pos + n, i->qgroups + pos,
1322 (nitems - pos) * s);
1330 int qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg)
1333 u64 qgroupid = parse_qgroupid(arg);
1336 if (qgroupid == 0) {
1337 fprintf(stderr, "ERROR: bad qgroup specification\n");
1342 pos = (*inherit)->num_qgroups;
1343 ret = qgroup_inherit_realloc(inherit, 1, pos);
1347 (*inherit)->qgroups[(*inherit)->num_qgroups++] = qgroupid;
1352 int qgroup_inherit_add_copy(struct btrfs_qgroup_inherit **inherit, char *arg,
1361 p = strchr(arg, ':');
1364 fprintf(stderr, "ERROR: bad copy specification\n");
1368 qgroup_src = parse_qgroupid(arg);
1369 qgroup_dst = parse_qgroupid(p + 1);
1372 if (!qgroup_src || !qgroup_dst)
1376 pos = (*inherit)->num_qgroups +
1377 (*inherit)->num_ref_copies * 2 * type;
1379 ret = qgroup_inherit_realloc(inherit, 2, pos);
1383 (*inherit)->qgroups[pos++] = qgroup_src;
1384 (*inherit)->qgroups[pos++] = qgroup_dst;
1387 ++(*inherit)->num_ref_copies;
1389 ++(*inherit)->num_excl_copies;