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>
24 #define BTRFS_QGROUP_NFILTERS_INCREASE (2 * BTRFS_QGROUP_FILTER_MAX)
26 struct qgroup_lookup {
31 struct rb_node rb_node;
32 struct rb_node sort_node;
34 *all_parent_node is used to
35 *filter a qgroup's all parent
37 struct rb_node all_parent_node;
44 u64 rfer; /*referenced*/
45 u64 rfer_cmpr; /*referenced compressed*/
46 u64 excl; /*exclusive*/
47 u64 excl_cmpr; /*exclusive compressed*/
52 u64 flags; /*which limits are set*/
58 /*qgroups this group is member of*/
59 struct list_head qgroups;
60 /*qgroups that are members of this group*/
61 struct list_head members;
65 * glue structure to represent the relations
68 struct btrfs_qgroup_list {
69 struct list_head next_qgroup;
70 struct list_head next_member;
71 struct btrfs_qgroup *qgroup;
72 struct btrfs_qgroup *member;
76 * qgroupid,rfer,excl default to set
82 } btrfs_qgroup_columns[] = {
85 .column_name = "Qgroupid",
90 .column_name = "Rfer",
95 .column_name = "Excl",
99 .column_name = "Max_rfer",
104 .column_name = "Max_excl",
109 .column_name = "Parent",
114 .column_name = "Child",
124 static btrfs_qgroup_filter_func all_filter_funcs[];
126 void btrfs_qgroup_setup_print_column(enum btrfs_qgroup_column_enum column)
130 BUG_ON(column < 0 || column > BTRFS_QGROUP_ALL);
132 if (column < BTRFS_QGROUP_ALL) {
133 btrfs_qgroup_columns[column].need_print = 1;
136 for (i = 0; i < BTRFS_QGROUP_ALL; i++)
137 btrfs_qgroup_columns[i].need_print = 1;
140 static void print_parent_column(struct btrfs_qgroup *qgroup)
142 struct btrfs_qgroup_list *list = NULL;
144 list_for_each_entry(list, &qgroup->qgroups, next_qgroup) {
145 printf("%llu/%llu", (list->qgroup)->qgroupid >> 48,
146 ((1ll << 48) - 1) & (list->qgroup)->qgroupid);
147 if (!list_is_last(&list->next_qgroup, &qgroup->qgroups))
150 if (list_empty(&qgroup->qgroups))
154 static void print_child_column(struct btrfs_qgroup *qgroup)
156 struct btrfs_qgroup_list *list = NULL;
158 list_for_each_entry(list, &qgroup->members, next_member) {
159 printf("%llu/%llu", (list->member)->qgroupid >> 48,
160 ((1ll << 48) - 1) & (list->member)->qgroupid);
161 if (!list_is_last(&list->next_member, &qgroup->members))
164 if (list_empty(&qgroup->members))
168 static void print_qgroup_column(struct btrfs_qgroup *qgroup,
169 enum btrfs_qgroup_column_enum column)
171 BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
175 case BTRFS_QGROUP_QGROUPID:
176 printf("%llu/%llu", qgroup->qgroupid >> 48,
177 ((1ll << 48) - 1) & qgroup->qgroupid);
179 case BTRFS_QGROUP_RFER:
180 printf("%lld", qgroup->rfer);
182 case BTRFS_QGROUP_EXCL:
183 printf("%lld", qgroup->excl);
185 case BTRFS_QGROUP_PARENT:
186 print_parent_column(qgroup);
188 case BTRFS_QGROUP_MAX_RFER:
189 printf("%llu", qgroup->max_rfer);
191 case BTRFS_QGROUP_MAX_EXCL:
192 printf("%llu", qgroup->max_excl);
194 case BTRFS_QGROUP_CHILD:
195 print_child_column(qgroup);
202 static void print_single_qgroup_default(struct btrfs_qgroup *qgroup)
206 for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
207 if (!btrfs_qgroup_columns[i].need_print)
209 print_qgroup_column(qgroup, i);
211 if (i != BTRFS_QGROUP_ALL - 1)
217 static void qgroup_lookup_init(struct qgroup_lookup *tree)
219 tree->root.rb_node = NULL;
222 static int comp_entry_with_qgroupid(struct btrfs_qgroup *entry1,
223 struct btrfs_qgroup *entry2,
229 if (entry1->qgroupid > entry2->qgroupid)
231 else if (entry1->qgroupid < entry2->qgroupid)
236 return is_descending ? -ret : ret;
240 * insert a new root into the tree. returns the existing root entry
241 * if one is already there. qgroupid is used
244 static int qgroup_tree_insert(struct qgroup_lookup *root_tree,
245 struct btrfs_qgroup *ins)
248 struct rb_node **p = &root_tree->root.rb_node;
249 struct rb_node *parent = NULL;
250 struct btrfs_qgroup *curr;
255 curr = rb_entry(parent, struct btrfs_qgroup, rb_node);
257 ret = comp_entry_with_qgroupid(ins, curr, 0);
265 rb_link_node(&ins->rb_node, parent, p);
266 rb_insert_color(&ins->rb_node, &root_tree->root);
271 *find a given qgroupid in the tree. We return the smallest one,
272 *rb_next can be used to move forward looking for more if required
274 static struct btrfs_qgroup *qgroup_tree_search(struct qgroup_lookup *root_tree,
277 struct rb_node *n = root_tree->root.rb_node;
278 struct btrfs_qgroup *entry;
279 struct btrfs_qgroup tmp;
282 tmp.qgroupid = qgroupid;
285 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
287 ret = comp_entry_with_qgroupid(&tmp, entry, 0);
299 static int update_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
300 u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
301 u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
302 u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *pa,
303 struct btrfs_qgroup *child)
305 struct btrfs_qgroup *bq;
306 struct btrfs_qgroup_list *list;
308 bq = qgroup_tree_search(qgroup_lookup, qgroupid);
309 if (!bq || bq->qgroupid != qgroupid)
313 bq->generation = generation;
317 bq->rfer_cmpr = rfer_cmpr;
321 bq->excl_cmpr = excl_cmpr;
325 bq->max_rfer = max_rfer;
327 bq->max_excl = max_excl;
329 bq->rsv_rfer = rsv_rfer;
331 list = malloc(sizeof(*list));
333 fprintf(stderr, "memory allocation failed\n");
337 list->member = child;
338 list_add_tail(&list->next_qgroup, &child->qgroups);
339 list_add_tail(&list->next_member, &pa->members);
344 static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
345 u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
346 u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
347 u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *parent,
348 struct btrfs_qgroup *child)
350 struct btrfs_qgroup *bq;
351 struct btrfs_qgroup_list *list;
354 ret = update_qgroup(qgroup_lookup, qgroupid, generation, rfer,
355 rfer_cmpr, excl, excl_cmpr, flags, max_rfer,
356 max_excl, rsv_rfer, rsv_excl, parent, child);
360 bq = malloc(sizeof(*bq));
362 printf("memory allocation failed\n");
365 memset(bq, 0, sizeof(*bq));
367 bq->qgroupid = qgroupid;
368 INIT_LIST_HEAD(&bq->qgroups);
369 INIT_LIST_HEAD(&bq->members);
372 bq->generation = generation;
376 bq->rfer_cmpr = rfer_cmpr;
380 bq->excl_cmpr = excl_cmpr;
384 bq->max_rfer = max_rfer;
386 bq->max_excl = max_excl;
388 bq->rsv_rfer = rsv_rfer;
389 if (parent && child) {
390 list = malloc(sizeof(*list));
392 fprintf(stderr, "memory allocation failed\n");
395 list->qgroup = parent;
396 list->member = child;
397 list_add_tail(&list->next_qgroup, &child->qgroups);
398 list_add_tail(&list->next_member, &parent->members);
400 ret = qgroup_tree_insert(qgroup_lookup, bq);
402 printf("failed to insert tree %llu\n",
409 void __free_btrfs_qgroup(struct btrfs_qgroup *bq)
411 struct btrfs_qgroup_list *list;
412 while (!list_empty(&bq->qgroups)) {
413 list = list_entry((&bq->qgroups)->next,
414 struct btrfs_qgroup_list,
416 list_del(&list->next_qgroup);
417 list_del(&list->next_member);
420 while (!list_empty(&bq->members)) {
421 list = list_entry((&bq->members)->next,
422 struct btrfs_qgroup_list,
424 list_del(&list->next_qgroup);
425 list_del(&list->next_member);
431 void __free_all_qgroups(struct qgroup_lookup *root_tree)
433 struct btrfs_qgroup *entry;
436 n = rb_first(&root_tree->root);
438 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
439 rb_erase(n, &root_tree->root);
440 __free_btrfs_qgroup(entry);
442 n = rb_first(&root_tree->root);
446 static int filter_all_parent_insert(struct qgroup_lookup *sort_tree,
447 struct btrfs_qgroup *bq)
449 struct rb_node **p = &sort_tree->root.rb_node;
450 struct rb_node *parent = NULL;
451 struct btrfs_qgroup *curr;
456 curr = rb_entry(parent, struct btrfs_qgroup, all_parent_node);
458 ret = comp_entry_with_qgroupid(bq, curr, 0);
466 rb_link_node(&bq->all_parent_node, parent, p);
467 rb_insert_color(&bq->all_parent_node, &sort_tree->root);
471 static int filter_by_all_parent(struct btrfs_qgroup *bq, u64 data)
473 struct qgroup_lookup lookup;
474 struct qgroup_lookup *ql = &lookup;
475 struct btrfs_qgroup_list *list;
477 struct btrfs_qgroup *qgroup =
478 (struct btrfs_qgroup *)(unsigned long)data;
482 if (bq->qgroupid == qgroup->qgroupid)
485 qgroup_lookup_init(ql);
486 filter_all_parent_insert(ql, qgroup);
487 n = rb_first(&ql->root);
489 qgroup = rb_entry(n, struct btrfs_qgroup, all_parent_node);
490 if (!list_empty(&qgroup->qgroups)) {
491 list_for_each_entry(list, &qgroup->qgroups,
493 if ((list->qgroup)->qgroupid == bq->qgroupid)
495 filter_all_parent_insert(ql, list->qgroup);
498 rb_erase(n, &ql->root);
499 n = rb_first(&ql->root);
504 static btrfs_qgroup_filter_func all_filter_funcs[] = {
505 [BTRFS_QGROUP_FILTER_ALL_PARENT] = filter_by_all_parent,
508 struct btrfs_qgroup_filter_set *btrfs_qgroup_alloc_filter_set(void)
510 struct btrfs_qgroup_filter_set *set;
513 size = sizeof(struct btrfs_qgroup_filter_set) +
514 BTRFS_QGROUP_NFILTERS_INCREASE *
515 sizeof(struct btrfs_qgroup_filter);
518 fprintf(stderr, "memory allocation failed\n");
521 memset(set, 0, size);
522 set->total = BTRFS_QGROUP_NFILTERS_INCREASE;
527 void btrfs_qgroup_free_filter_set(struct btrfs_qgroup_filter_set *filter_set)
532 int btrfs_qgroup_setup_filter(struct btrfs_qgroup_filter_set **filter_set,
533 enum btrfs_qgroup_filter_enum filter, u64 data)
535 struct btrfs_qgroup_filter_set *set = *filter_set;
539 BUG_ON(filter >= BTRFS_QGROUP_FILTER_MAX);
540 BUG_ON(set->nfilters > set->total);
542 if (set->nfilters == set->total) {
543 size = set->total + BTRFS_QGROUP_NFILTERS_INCREASE;
544 size = sizeof(*set) + size * sizeof(struct btrfs_qgroup_filter);
546 set = realloc(set, size);
548 fprintf(stderr, "memory allocation failed\n");
551 memset(&set->filters[set->total], 0,
552 BTRFS_QGROUP_NFILTERS_INCREASE *
553 sizeof(struct btrfs_qgroup_filter));
554 set->total += BTRFS_QGROUP_NFILTERS_INCREASE;
557 BUG_ON(set->filters[set->nfilters].filter_func);
558 set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
559 set->filters[set->nfilters].data = data;
564 static int filter_qgroup(struct btrfs_qgroup *bq,
565 struct btrfs_qgroup_filter_set *set)
569 if (!set || !set->nfilters)
571 for (i = 0; i < set->nfilters; i++) {
572 if (!set->filters[i].filter_func)
574 ret = set->filters[i].filter_func(bq, set->filters[i].data);
581 static void pre_process_filter_set(struct qgroup_lookup *lookup,
582 struct btrfs_qgroup_filter_set *set)
585 struct btrfs_qgroup *qgroup_for_filter = NULL;
587 for (i = 0; i < set->nfilters; i++) {
589 if (set->filters[i].filter_func == filter_by_all_parent) {
590 qgroup_for_filter = qgroup_tree_search(lookup,
591 set->filters[i].data);
592 set->filters[i].data =
593 (u64)(unsigned long)qgroup_for_filter;
598 static int sort_tree_insert(struct qgroup_lookup *sort_tree,
599 struct btrfs_qgroup *bq)
601 struct rb_node **p = &sort_tree->root.rb_node;
602 struct rb_node *parent = NULL;
603 struct btrfs_qgroup *curr;
608 curr = rb_entry(parent, struct btrfs_qgroup, sort_node);
610 ret = comp_entry_with_qgroupid(bq, curr, 0);
618 rb_link_node(&bq->sort_node, parent, p);
619 rb_insert_color(&bq->sort_node, &sort_tree->root);
623 static void __filter_all_qgroups(struct qgroup_lookup *all_qgroups,
624 struct qgroup_lookup *sort_tree,
625 struct btrfs_qgroup_filter_set *filter_set)
628 struct btrfs_qgroup *entry;
631 qgroup_lookup_init(sort_tree);
632 pre_process_filter_set(all_qgroups, filter_set);
634 n = rb_last(&all_qgroups->root);
636 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
638 ret = filter_qgroup(entry, filter_set);
640 sort_tree_insert(sort_tree, entry);
645 static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
648 struct btrfs_ioctl_search_args args;
649 struct btrfs_ioctl_search_key *sk = &args.key;
650 struct btrfs_ioctl_search_header *sh;
651 unsigned long off = 0;
654 struct btrfs_qgroup_info_item *info;
655 struct btrfs_qgroup_limit_item *limit;
656 struct btrfs_qgroup *bq;
657 struct btrfs_qgroup *bq1;
664 memset(&args, 0, sizeof(args));
666 sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
667 sk->max_type = BTRFS_QGROUP_RELATION_KEY;
668 sk->min_type = BTRFS_QGROUP_INFO_KEY;
669 sk->max_objectid = (u64)-1;
670 sk->max_offset = (u64)-1;
671 sk->max_transid = (u64)-1;
674 qgroup_lookup_init(qgroup_lookup);
677 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
681 "ERROR: can't perform the search - %s\n",
685 /* the ioctl returns the number of item it found in nr_items */
686 if (sk->nr_items == 0)
691 * for each item, pull the key out of the header and then
692 * read the root_ref item it contains
694 for (i = 0; i < sk->nr_items; i++) {
695 sh = (struct btrfs_ioctl_search_header *)(args.buf +
699 if (sh->type == BTRFS_QGROUP_INFO_KEY) {
700 info = (struct btrfs_qgroup_info_item *)
702 a1 = btrfs_stack_qgroup_info_generation(info);
703 a2 = btrfs_stack_qgroup_info_referenced(info);
705 btrfs_stack_qgroup_info_referenced_compressed
707 a4 = btrfs_stack_qgroup_info_exclusive(info);
709 btrfs_stack_qgroup_info_exclusive_compressed
711 add_qgroup(qgroup_lookup, sh->offset, a1, a2,
712 a3, a4, a5, 0, 0, 0, 0, 0, 0, 0);
713 } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
714 limit = (struct btrfs_qgroup_limit_item *)
717 a1 = btrfs_stack_qgroup_limit_flags(limit);
718 a2 = btrfs_stack_qgroup_limit_max_referenced
720 a3 = btrfs_stack_qgroup_limit_max_exclusive
722 a4 = btrfs_stack_qgroup_limit_rsv_referenced
724 a5 = btrfs_stack_qgroup_limit_rsv_exclusive
726 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
727 0, 0, 0, a1, a2, a3, a4, a5, 0, 0);
728 } else if (sh->type == BTRFS_QGROUP_RELATION_KEY) {
729 if (sh->offset < sh->objectid)
731 bq = qgroup_tree_search(qgroup_lookup,
735 bq1 = qgroup_tree_search(qgroup_lookup,
739 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
740 0, 0, 0, 0, 0, 0, 0, 0, bq, bq1);
747 * record the mins in sk so we can make sure the
748 * next search doesn't repeat this root
750 sk->min_type = sh->type;
751 sk->min_offset = sh->offset;
752 sk->min_objectid = sh->objectid;
756 * this iteration is done, step forward one qgroup for the next
759 if (sk->min_offset < (u64)-1)
769 static void print_all_qgroups(struct qgroup_lookup *qgroup_lookup)
773 struct btrfs_qgroup *entry;
775 n = rb_first(&qgroup_lookup->root);
777 entry = rb_entry(n, struct btrfs_qgroup, sort_node);
778 print_single_qgroup_default(entry);
783 int btrfs_show_qgroups(int fd,
784 struct btrfs_qgroup_filter_set *filter_set)
787 struct qgroup_lookup qgroup_lookup;
788 struct qgroup_lookup sort_tree;
791 ret = __qgroups_search(fd, &qgroup_lookup);
794 __filter_all_qgroups(&qgroup_lookup, &sort_tree,
796 print_all_qgroups(&sort_tree);
798 __free_all_qgroups(&qgroup_lookup);
799 btrfs_qgroup_free_filter_set(filter_set);
803 u64 btrfs_get_path_rootid(int fd)
806 struct btrfs_ioctl_ino_lookup_args args;
808 memset(&args, 0, sizeof(args));
809 args.objectid = BTRFS_FIRST_FREE_OBJECTID;
811 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
814 "ERROR: can't perform the search -%s\n",
821 u64 parse_qgroupid(char *p)
823 char *s = strchr(p, '/');
824 char *ptr_src_end = p + strlen(p);
825 char *ptr_parse_end = NULL;
830 id = strtoull(p, &ptr_parse_end, 10);
831 if (ptr_parse_end != ptr_src_end)
835 level = strtoull(p, &ptr_parse_end, 10);
836 if (ptr_parse_end != s)
839 id = strtoull(s+1, &ptr_parse_end, 10);
840 if (ptr_parse_end != ptr_src_end)
843 return (level << 48) | id;
845 fprintf(stderr, "ERROR:invalid qgroupid\n");
849 int qgroup_inherit_size(struct btrfs_qgroup_inherit *p)
851 return sizeof(*p) + sizeof(p->qgroups[0]) *
852 (p->num_qgroups + 2 * p->num_ref_copies +
853 2 * p->num_excl_copies);
857 qgroup_inherit_realloc(struct btrfs_qgroup_inherit **inherit, int n, int pos)
859 struct btrfs_qgroup_inherit *out;
863 nitems = (*inherit)->num_qgroups +
864 (*inherit)->num_ref_copies +
865 (*inherit)->num_excl_copies;
868 out = calloc(sizeof(*out) + sizeof(out->qgroups[0]) * (nitems + n), 1);
870 fprintf(stderr, "ERROR: Not enough memory\n");
875 struct btrfs_qgroup_inherit *i = *inherit;
876 int s = sizeof(out->qgroups[0]);
878 out->num_qgroups = i->num_qgroups;
879 out->num_ref_copies = i->num_ref_copies;
880 out->num_excl_copies = i->num_excl_copies;
881 memcpy(out->qgroups, i->qgroups, pos * s);
882 memcpy(out->qgroups + pos + n, i->qgroups + pos,
891 int qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg)
894 u64 qgroupid = parse_qgroupid(arg);
898 fprintf(stderr, "ERROR: bad qgroup specification\n");
903 pos = (*inherit)->num_qgroups;
904 ret = qgroup_inherit_realloc(inherit, 1, pos);
908 (*inherit)->qgroups[(*inherit)->num_qgroups++] = qgroupid;
913 int qgroup_inherit_add_copy(struct btrfs_qgroup_inherit **inherit, char *arg,
922 p = strchr(arg, ':');
925 fprintf(stderr, "ERROR: bad copy specification\n");
929 qgroup_src = parse_qgroupid(arg);
930 qgroup_dst = parse_qgroupid(p + 1);
933 if (!qgroup_src || !qgroup_dst)
937 pos = (*inherit)->num_qgroups +
938 (*inherit)->num_ref_copies * 2 * type;
940 ret = qgroup_inherit_realloc(inherit, 2, pos);
944 (*inherit)->qgroups[pos++] = qgroup_src;
945 (*inherit)->qgroups[pos++] = qgroup_dst;
948 ++(*inherit)->num_ref_copies;
950 ++(*inherit)->num_excl_copies;