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 struct qgroup_lookup {
29 struct rb_node rb_node;
36 u64 rfer; /*referenced*/
37 u64 rfer_cmpr; /*referenced compressed*/
38 u64 excl; /*exclusive*/
39 u64 excl_cmpr; /*exclusive compressed*/
44 u64 flags; /*which limits are set*/
50 /*qgroups this group is member of*/
51 struct list_head qgroups;
52 /*qgroups that are members of this group*/
53 struct list_head members;
57 * glue structure to represent the relations
60 struct btrfs_qgroup_list {
61 struct list_head next_qgroup;
62 struct list_head next_member;
63 struct btrfs_qgroup *qgroup;
64 struct btrfs_qgroup *member;
68 * qgroupid,rfer,excl default to set
74 } btrfs_qgroup_columns[] = {
77 .column_name = "Qgroupid",
82 .column_name = "Rfer",
87 .column_name = "Excl",
97 void btrfs_qgroup_setup_print_column(enum btrfs_qgroup_column_enum column)
101 BUG_ON(column < 0 || column > BTRFS_QGROUP_ALL);
103 if (column < BTRFS_QGROUP_ALL) {
104 btrfs_qgroup_columns[column].need_print = 1;
107 for (i = 0; i < BTRFS_QGROUP_ALL; i++)
108 btrfs_qgroup_columns[i].need_print = 1;
111 static void print_qgroup_column(struct btrfs_qgroup *qgroup,
112 enum btrfs_qgroup_column_enum column)
114 BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
118 case BTRFS_QGROUP_QGROUPID:
119 printf("%llu/%llu", qgroup->qgroupid >> 48,
120 ((1ll << 48) - 1) & qgroup->qgroupid);
122 case BTRFS_QGROUP_RFER:
123 printf("%lld", qgroup->rfer);
125 case BTRFS_QGROUP_EXCL:
126 printf("%lld", qgroup->excl);
133 static void print_single_qgroup_default(struct btrfs_qgroup *qgroup)
137 for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
138 if (!btrfs_qgroup_columns[i].need_print)
140 print_qgroup_column(qgroup, i);
142 if (i != BTRFS_QGROUP_ALL - 1)
148 static void qgroup_lookup_init(struct qgroup_lookup *tree)
150 tree->root.rb_node = NULL;
153 static int comp_entry_with_qgroupid(struct btrfs_qgroup *entry1,
154 struct btrfs_qgroup *entry2,
160 if (entry1->qgroupid > entry2->qgroupid)
162 else if (entry1->qgroupid < entry2->qgroupid)
167 return is_descending ? -ret : ret;
171 * insert a new root into the tree. returns the existing root entry
172 * if one is already there. qgroupid is used
175 static int qgroup_tree_insert(struct qgroup_lookup *root_tree,
176 struct btrfs_qgroup *ins)
179 struct rb_node **p = &root_tree->root.rb_node;
180 struct rb_node *parent = NULL;
181 struct btrfs_qgroup *curr;
186 curr = rb_entry(parent, struct btrfs_qgroup, rb_node);
188 ret = comp_entry_with_qgroupid(ins, curr, 0);
196 rb_link_node(&ins->rb_node, parent, p);
197 rb_insert_color(&ins->rb_node, &root_tree->root);
202 *find a given qgroupid in the tree. We return the smallest one,
203 *rb_next can be used to move forward looking for more if required
205 static struct btrfs_qgroup *qgroup_tree_search(struct qgroup_lookup *root_tree,
208 struct rb_node *n = root_tree->root.rb_node;
209 struct btrfs_qgroup *entry;
210 struct btrfs_qgroup tmp;
213 tmp.qgroupid = qgroupid;
216 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
218 ret = comp_entry_with_qgroupid(&tmp, entry, 0);
230 static int update_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
231 u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
232 u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
233 u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *pa,
234 struct btrfs_qgroup *child)
236 struct btrfs_qgroup *bq;
237 struct btrfs_qgroup_list *list;
239 bq = qgroup_tree_search(qgroup_lookup, qgroupid);
240 if (!bq || bq->qgroupid != qgroupid)
244 bq->generation = generation;
248 bq->rfer_cmpr = rfer_cmpr;
252 bq->excl_cmpr = excl_cmpr;
256 bq->max_rfer = max_rfer;
258 bq->max_excl = max_excl;
260 bq->rsv_rfer = rsv_rfer;
262 list = malloc(sizeof(*list));
264 fprintf(stderr, "memory allocation failed\n");
268 list->member = child;
269 list_add_tail(&list->next_qgroup, &child->qgroups);
270 list_add_tail(&list->next_member, &pa->members);
275 static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
276 u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
277 u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
278 u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *parent,
279 struct btrfs_qgroup *child)
281 struct btrfs_qgroup *bq;
282 struct btrfs_qgroup_list *list;
285 ret = update_qgroup(qgroup_lookup, qgroupid, generation, rfer,
286 rfer_cmpr, excl, excl_cmpr, flags, max_rfer,
287 max_excl, rsv_rfer, rsv_excl, parent, child);
291 bq = malloc(sizeof(*bq));
293 printf("memory allocation failed\n");
296 memset(bq, 0, sizeof(*bq));
298 bq->qgroupid = qgroupid;
299 INIT_LIST_HEAD(&bq->qgroups);
300 INIT_LIST_HEAD(&bq->members);
303 bq->generation = generation;
307 bq->rfer_cmpr = rfer_cmpr;
311 bq->excl_cmpr = excl_cmpr;
315 bq->max_rfer = max_rfer;
317 bq->max_excl = max_excl;
319 bq->rsv_rfer = rsv_rfer;
320 if (parent && child) {
321 list = malloc(sizeof(*list));
323 fprintf(stderr, "memory allocation failed\n");
326 list->qgroup = parent;
327 list->member = child;
328 list_add_tail(&list->next_qgroup, &child->qgroups);
329 list_add_tail(&list->next_member, &parent->members);
331 ret = qgroup_tree_insert(qgroup_lookup, bq);
333 printf("failed to insert tree %llu\n",
340 void __free_btrfs_qgroup(struct btrfs_qgroup *bq)
342 struct btrfs_qgroup_list *list;
343 while (!list_empty(&bq->qgroups)) {
344 list = list_entry((&bq->qgroups)->next,
345 struct btrfs_qgroup_list,
347 list_del(&list->next_qgroup);
348 list_del(&list->next_member);
351 while (!list_empty(&bq->members)) {
352 list = list_entry((&bq->members)->next,
353 struct btrfs_qgroup_list,
355 list_del(&list->next_qgroup);
356 list_del(&list->next_member);
362 void __free_all_qgroups(struct qgroup_lookup *root_tree)
364 struct btrfs_qgroup *entry;
367 n = rb_first(&root_tree->root);
369 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
370 rb_erase(n, &root_tree->root);
371 __free_btrfs_qgroup(entry);
373 n = rb_first(&root_tree->root);
377 static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
380 struct btrfs_ioctl_search_args args;
381 struct btrfs_ioctl_search_key *sk = &args.key;
382 struct btrfs_ioctl_search_header *sh;
383 unsigned long off = 0;
386 struct btrfs_qgroup_info_item *info;
387 struct btrfs_qgroup_limit_item *limit;
388 struct btrfs_qgroup *bq;
389 struct btrfs_qgroup *bq1;
396 memset(&args, 0, sizeof(args));
398 sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
399 sk->max_type = BTRFS_QGROUP_RELATION_KEY;
400 sk->min_type = BTRFS_QGROUP_INFO_KEY;
401 sk->max_objectid = (u64)-1;
402 sk->max_offset = (u64)-1;
403 sk->max_transid = (u64)-1;
406 qgroup_lookup_init(qgroup_lookup);
409 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
413 "ERROR: can't perform the search - %s\n",
417 /* the ioctl returns the number of item it found in nr_items */
418 if (sk->nr_items == 0)
423 * for each item, pull the key out of the header and then
424 * read the root_ref item it contains
426 for (i = 0; i < sk->nr_items; i++) {
427 sh = (struct btrfs_ioctl_search_header *)(args.buf +
431 if (sh->type == BTRFS_QGROUP_INFO_KEY) {
432 info = (struct btrfs_qgroup_info_item *)
434 a1 = btrfs_stack_qgroup_info_generation(info);
435 a2 = btrfs_stack_qgroup_info_referenced(info);
437 btrfs_stack_qgroup_info_referenced_compressed
439 a4 = btrfs_stack_qgroup_info_exclusive(info);
441 btrfs_stack_qgroup_info_exclusive_compressed
443 add_qgroup(qgroup_lookup, sh->offset, a1, a2,
444 a3, a4, a5, 0, 0, 0, 0, 0, 0, 0);
445 } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
446 limit = (struct btrfs_qgroup_limit_item *)
449 a1 = btrfs_stack_qgroup_limit_flags(limit);
450 a2 = btrfs_stack_qgroup_limit_max_referenced
452 a3 = btrfs_stack_qgroup_limit_max_exclusive
454 a4 = btrfs_stack_qgroup_limit_rsv_referenced
456 a5 = btrfs_stack_qgroup_limit_rsv_exclusive
458 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
459 0, 0, 0, a1, a2, a3, a4, a5, 0, 0);
460 } else if (sh->type == BTRFS_QGROUP_RELATION_KEY) {
461 if (sh->offset < sh->objectid)
463 bq = qgroup_tree_search(qgroup_lookup,
467 bq1 = qgroup_tree_search(qgroup_lookup,
471 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
472 0, 0, 0, 0, 0, 0, 0, 0, bq, bq1);
479 * record the mins in sk so we can make sure the
480 * next search doesn't repeat this root
482 sk->min_type = sh->type;
483 sk->min_offset = sh->offset;
484 sk->min_objectid = sh->objectid;
488 * this iteration is done, step forward one qgroup for the next
491 if (sk->min_offset < (u64)-1)
501 static void print_all_qgroups(struct qgroup_lookup *qgroup_lookup)
505 struct btrfs_qgroup *entry;
507 n = rb_first(&qgroup_lookup->root);
509 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
510 print_single_qgroup_default(entry);
515 int btrfs_show_qgroups(int fd)
518 struct qgroup_lookup qgroup_lookup;
521 ret = __qgroups_search(fd, &qgroup_lookup);
525 print_all_qgroups(&qgroup_lookup);
526 __free_all_qgroups(&qgroup_lookup);
531 u64 parse_qgroupid(char *p)
533 char *s = strchr(p, '/');
534 char *ptr_src_end = p + strlen(p);
535 char *ptr_parse_end = NULL;
540 id = strtoull(p, &ptr_parse_end, 10);
541 if (ptr_parse_end != ptr_src_end)
545 level = strtoull(p, &ptr_parse_end, 10);
546 if (ptr_parse_end != s)
549 id = strtoull(s+1, &ptr_parse_end, 10);
550 if (ptr_parse_end != ptr_src_end)
553 return (level << 48) | id;
555 fprintf(stderr, "ERROR:invalid qgroupid\n");
559 int qgroup_inherit_size(struct btrfs_qgroup_inherit *p)
561 return sizeof(*p) + sizeof(p->qgroups[0]) *
562 (p->num_qgroups + 2 * p->num_ref_copies +
563 2 * p->num_excl_copies);
567 qgroup_inherit_realloc(struct btrfs_qgroup_inherit **inherit, int n, int pos)
569 struct btrfs_qgroup_inherit *out;
573 nitems = (*inherit)->num_qgroups +
574 (*inherit)->num_ref_copies +
575 (*inherit)->num_excl_copies;
578 out = calloc(sizeof(*out) + sizeof(out->qgroups[0]) * (nitems + n), 1);
580 fprintf(stderr, "ERROR: Not enough memory\n");
585 struct btrfs_qgroup_inherit *i = *inherit;
586 int s = sizeof(out->qgroups[0]);
588 out->num_qgroups = i->num_qgroups;
589 out->num_ref_copies = i->num_ref_copies;
590 out->num_excl_copies = i->num_excl_copies;
591 memcpy(out->qgroups, i->qgroups, pos * s);
592 memcpy(out->qgroups + pos + n, i->qgroups + pos,
601 int qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg)
604 u64 qgroupid = parse_qgroupid(arg);
608 fprintf(stderr, "ERROR: bad qgroup specification\n");
613 pos = (*inherit)->num_qgroups;
614 ret = qgroup_inherit_realloc(inherit, 1, pos);
618 (*inherit)->qgroups[(*inherit)->num_qgroups++] = qgroupid;
623 int qgroup_inherit_add_copy(struct btrfs_qgroup_inherit **inherit, char *arg,
632 p = strchr(arg, ':');
635 fprintf(stderr, "ERROR: bad copy specification\n");
639 qgroup_src = parse_qgroupid(arg);
640 qgroup_dst = parse_qgroupid(p + 1);
643 if (!qgroup_src || !qgroup_dst)
647 pos = (*inherit)->num_qgroups +
648 (*inherit)->num_ref_copies * 2 * type;
650 ret = qgroup_inherit_realloc(inherit, 2, pos);
654 (*inherit)->qgroups[pos++] = qgroup_src;
655 (*inherit)->qgroups[pos++] = qgroup_dst;
658 ++(*inherit)->num_ref_copies;
660 ++(*inherit)->num_excl_copies;