btrfs-progs: qgroup: show 'none' when we did not limit it on this qgroup
[platform/upstream/btrfs-progs.git] / qgroup.c
1 /*
2  * Copyright (C) 2012 STRATO.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include "qgroup.h"
20 #include <sys/ioctl.h>
21 #include "ctree.h"
22 #include "ioctl.h"
23 #include "utils.h"
24 #include <errno.h>
25
26 #define BTRFS_QGROUP_NFILTERS_INCREASE (2 * BTRFS_QGROUP_FILTER_MAX)
27 #define BTRFS_QGROUP_NCOMPS_INCREASE (2 * BTRFS_QGROUP_COMP_MAX)
28
29 struct qgroup_lookup {
30         struct rb_root root;
31 };
32
33 struct btrfs_qgroup {
34         struct rb_node rb_node;
35         struct rb_node sort_node;
36         /*
37          *all_parent_node is used to
38          *filter a qgroup's all parent
39          */
40         struct rb_node all_parent_node;
41         u64 qgroupid;
42
43         /*
44          * info_item
45          */
46         u64 generation;
47         u64 rfer;       /*referenced*/
48         u64 rfer_cmpr;  /*referenced compressed*/
49         u64 excl;       /*exclusive*/
50         u64 excl_cmpr;  /*exclusive compressed*/
51
52         /*
53          *limit_item
54          */
55         u64 flags;      /*which limits are set*/
56         u64 max_rfer;
57         u64 max_excl;
58         u64 rsv_rfer;
59         u64 rsv_excl;
60
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;
65 };
66
67 /*
68  * glue structure to represent the relations
69  * between qgroups
70  */
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;
76 };
77
78 /*
79  * qgroupid,rfer,excl default to set
80  */
81 static struct {
82         char *name;
83         char *column_name;
84         int need_print;
85         unsigned unit_mode;
86         int max_len;
87 } btrfs_qgroup_columns[] = {
88         {
89                 .name           = "qgroupid",
90                 .column_name    = "Qgroupid",
91                 .need_print     = 1,
92                 .unit_mode      = 0,
93                 .max_len        = 8,
94         },
95         {
96                 .name           = "rfer",
97                 .column_name    = "Rfer",
98                 .need_print     = 1,
99                 .unit_mode      = UNITS_DEFAULT,
100                 .max_len        = 12,
101         },
102         {
103                 .name           = "excl",
104                 .column_name    = "Excl",
105                 .need_print     = 1,
106                 .unit_mode      = UNITS_DEFAULT,
107                 .max_len        = 12,
108         },
109         {       .name           = "max_rfer",
110                 .column_name    = "Max_rfer",
111                 .need_print     = 0,
112                 .unit_mode      = UNITS_DEFAULT,
113                 .max_len        = 12,
114         },
115         {
116                 .name           = "max_excl",
117                 .column_name    = "Max_excl",
118                 .need_print     = 0,
119                 .unit_mode      = UNITS_DEFAULT,
120                 .max_len        = 12,
121         },
122         {
123                 .name           = "parent",
124                 .column_name    = "Parent",
125                 .need_print     = 0,
126                 .unit_mode      = 0,
127                 .max_len        = 7,
128         },
129         {
130                 .name           = "child",
131                 .column_name    = "Child",
132                 .need_print     = 0,
133                 .unit_mode      = 0,
134                 .max_len        = 5,
135         },
136         {
137                 .name           = NULL,
138                 .column_name    = NULL,
139                 .need_print     = 0,
140                 .unit_mode      = 0,
141         },
142 };
143
144 static btrfs_qgroup_filter_func all_filter_funcs[];
145 static btrfs_qgroup_comp_func all_comp_funcs[];
146
147 void btrfs_qgroup_setup_print_column(enum btrfs_qgroup_column_enum column)
148 {
149         int i;
150
151         BUG_ON(column < 0 || column > BTRFS_QGROUP_ALL);
152
153         if (column < BTRFS_QGROUP_ALL) {
154                 btrfs_qgroup_columns[column].need_print = 1;
155                 return;
156         }
157         for (i = 0; i < BTRFS_QGROUP_ALL; i++)
158                 btrfs_qgroup_columns[i].need_print = 1;
159 }
160
161 void btrfs_qgroup_setup_units(unsigned unit_mode)
162 {
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;
167 }
168
169 static int print_parent_column(struct btrfs_qgroup *qgroup)
170 {
171         struct btrfs_qgroup_list *list = NULL;
172         int len = 0;
173
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))
179                         len += printf(",");
180         }
181         if (list_empty(&qgroup->qgroups))
182                 len += printf("---");
183
184         return len;
185 }
186
187 static int print_child_column(struct btrfs_qgroup *qgroup)
188 {
189         struct btrfs_qgroup_list *list = NULL;
190         int len = 0;
191
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))
197                         len += printf(",");
198         }
199         if (list_empty(&qgroup->members))
200                 len += printf("---");
201
202         return len;
203 }
204
205 static void print_qgroup_column_add_blank(enum btrfs_qgroup_column_enum column,
206                                           int len)
207 {
208         len = btrfs_qgroup_columns[column].max_len - len;
209         while (len--)
210                 printf(" ");
211 }
212
213 static void print_qgroup_column(struct btrfs_qgroup *qgroup,
214                                 enum btrfs_qgroup_column_enum column)
215 {
216         BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
217         int len;
218         int unit_mode = btrfs_qgroup_columns[column].unit_mode;
219         int max_len = btrfs_qgroup_columns[column].max_len;
220
221         switch (column) {
222
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);
228                 break;
229         case BTRFS_QGROUP_RFER:
230                 len = printf("%*s", max_len, pretty_size_mode(qgroup->rfer, unit_mode));
231                 break;
232         case BTRFS_QGROUP_EXCL:
233                 len = printf("%*s", max_len, pretty_size_mode(qgroup->excl, unit_mode));
234                 break;
235         case BTRFS_QGROUP_PARENT:
236                 len = print_parent_column(qgroup);
237                 print_qgroup_column_add_blank(BTRFS_QGROUP_PARENT, len);
238                 break;
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));
242                 else
243                         len = printf("%*s", max_len, "none");
244                 break;
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));
248                 else
249                         len = printf("%*s", max_len, "none");
250                 break;
251         case BTRFS_QGROUP_CHILD:
252                 len = print_child_column(qgroup);
253                 print_qgroup_column_add_blank(BTRFS_QGROUP_CHILD, len);
254                 break;
255         default:
256                 break;
257         }
258 }
259
260 static void print_single_qgroup_table(struct btrfs_qgroup *qgroup)
261 {
262         int i;
263
264         for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
265                 if (!btrfs_qgroup_columns[i].need_print)
266                         continue;
267                 print_qgroup_column(qgroup, i);
268
269                 if (i != BTRFS_QGROUP_CHILD)
270                         printf(" ");
271         }
272         printf("\n");
273 }
274
275 static void print_table_head()
276 {
277         int i;
278         int len;
279         int max_len;
280
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)
284                         continue;
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);
288                 else
289                         printf("%*s", max_len, btrfs_qgroup_columns[i].name);
290                 printf(" ");
291         }
292         printf("\n");
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)
296                         continue;
297                 if ((i == BTRFS_QGROUP_QGROUPID) | (i == BTRFS_QGROUP_PARENT) |
298                         (i == BTRFS_QGROUP_CHILD)) {
299                         len = strlen(btrfs_qgroup_columns[i].name);
300                         while (len--)
301                                 printf("-");
302                         len = max_len - strlen(btrfs_qgroup_columns[i].name);
303                         while (len--)
304                                 printf(" ");
305                 } else {
306                         len = max_len - strlen(btrfs_qgroup_columns[i].name);
307                         while (len--)
308                                 printf(" ");
309                         len = strlen(btrfs_qgroup_columns[i].name);
310                         while (len--)
311                                 printf("-");
312                 }
313                 printf(" ");
314         }
315         printf("\n");
316 }
317
318 static void qgroup_lookup_init(struct qgroup_lookup *tree)
319 {
320         tree->root.rb_node = NULL;
321 }
322
323 static int comp_entry_with_qgroupid(struct btrfs_qgroup *entry1,
324                                     struct btrfs_qgroup *entry2,
325                                     int is_descending)
326 {
327
328         int ret;
329
330         if (entry1->qgroupid > entry2->qgroupid)
331                 ret = 1;
332         else if (entry1->qgroupid < entry2->qgroupid)
333                 ret = -1;
334         else
335                 ret = 0;
336
337         return is_descending ? -ret : ret;
338 }
339
340 static int comp_entry_with_rfer(struct btrfs_qgroup *entry1,
341                                 struct btrfs_qgroup *entry2,
342                                 int is_descending)
343 {
344         int ret;
345
346         if (entry1->rfer > entry2->rfer)
347                 ret = 1;
348         else if (entry1->rfer < entry2->rfer)
349                 ret = -1;
350         else
351                 ret = 0;
352
353         return is_descending ? -ret : ret;
354 }
355
356 static int comp_entry_with_excl(struct btrfs_qgroup *entry1,
357                                 struct btrfs_qgroup *entry2,
358                                 int is_descending)
359 {
360         int ret;
361
362         if (entry1->excl > entry2->excl)
363                 ret = 1;
364         else if (entry1->excl < entry2->excl)
365                 ret = -1;
366         else
367                 ret = 0;
368
369         return is_descending ? -ret : ret;
370 }
371
372 static int comp_entry_with_max_rfer(struct btrfs_qgroup *entry1,
373                                     struct btrfs_qgroup *entry2,
374                                     int is_descending)
375 {
376         int ret;
377
378         if (entry1->max_rfer > entry2->max_rfer)
379                 ret = 1;
380         else if (entry1->max_rfer < entry2->max_rfer)
381                 ret = -1;
382         else
383                 ret = 0;
384
385         return is_descending ? -ret : ret;
386 }
387
388 static int comp_entry_with_max_excl(struct btrfs_qgroup *entry1,
389                                     struct btrfs_qgroup *entry2,
390                                     int is_descending)
391 {
392         int ret;
393
394         if (entry1->max_excl > entry2->max_excl)
395                 ret = 1;
396         else if (entry1->max_excl < entry2->max_excl)
397                 ret = -1;
398         else
399                 ret = 0;
400
401         return is_descending ? -ret : ret;
402 }
403
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
410 };
411
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,
419 };
420
421 static int  btrfs_qgroup_get_sort_item(char *sort_name)
422 {
423         int i;
424
425         for (i = 0; i < BTRFS_QGROUP_COMP_MAX; i++) {
426                 if (strcmp(sort_name, all_sort_items[i]) == 0)
427                         return i;
428         }
429         return -1;
430 }
431
432 struct btrfs_qgroup_comparer_set *btrfs_qgroup_alloc_comparer_set(void)
433 {
434         struct btrfs_qgroup_comparer_set *set;
435         int size;
436         size = sizeof(struct btrfs_qgroup_comparer_set) +
437                BTRFS_QGROUP_NCOMPS_INCREASE *
438                sizeof(struct btrfs_qgroup_comparer);
439         set = malloc(size);
440         if (!set) {
441                 fprintf(stderr, "memory allocation failed\n");
442                 exit(1);
443         }
444
445         memset(set, 0, size);
446         set->total = BTRFS_QGROUP_NCOMPS_INCREASE;
447
448         return set;
449 }
450
451 void btrfs_qgroup_free_comparer_set(struct btrfs_qgroup_comparer_set *comp_set)
452 {
453         free(comp_set);
454 }
455
456 int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set  **comp_set,
457                                 enum btrfs_qgroup_comp_enum comparer,
458                                 int is_descending)
459 {
460         struct btrfs_qgroup_comparer_set *set = *comp_set;
461         int size;
462
463         BUG_ON(!set);
464         BUG_ON(comparer >= BTRFS_QGROUP_COMP_MAX);
465         BUG_ON(set->ncomps > set->total);
466
467         if (set->ncomps == set->total) {
468                 size = set->total + BTRFS_QGROUP_NCOMPS_INCREASE;
469                 size = sizeof(*set) +
470                        size * sizeof(struct btrfs_qgroup_comparer);
471                 set = realloc(set, size);
472                 if (!set) {
473                         fprintf(stderr, "memory allocation failed\n");
474                         exit(1);
475                 }
476
477                 memset(&set->comps[set->total], 0,
478                        BTRFS_QGROUP_NCOMPS_INCREASE *
479                        sizeof(struct btrfs_qgroup_comparer));
480                 set->total += BTRFS_QGROUP_NCOMPS_INCREASE;
481                 *comp_set = set;
482         }
483
484         BUG_ON(set->comps[set->ncomps].comp_func);
485
486         set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
487         set->comps[set->ncomps].is_descending = is_descending;
488         set->ncomps++;
489         return 0;
490 }
491
492 static int sort_comp(struct btrfs_qgroup *entry1, struct btrfs_qgroup *entry2,
493                      struct btrfs_qgroup_comparer_set *set)
494 {
495         int qgroupid_compared = 0;
496         int i, ret = 0;
497
498         if (!set || !set->ncomps)
499                 goto comp_qgroupid;
500
501         for (i = 0; i < set->ncomps; i++) {
502                 if (!set->comps[i].comp_func)
503                         break;
504
505                 ret = set->comps[i].comp_func(entry1, entry2,
506                                               set->comps[i].is_descending);
507                 if (ret)
508                         return ret;
509
510                 if (set->comps[i].comp_func == comp_entry_with_qgroupid)
511                         qgroupid_compared = 1;
512         }
513
514         if (!qgroupid_compared) {
515 comp_qgroupid:
516                 ret = comp_entry_with_qgroupid(entry1, entry2, 0);
517         }
518
519         return ret;
520 }
521
522 /*
523  * insert a new root into the tree.  returns the existing root entry
524  * if one is already there.  qgroupid is used
525  * as the key
526  */
527 static int qgroup_tree_insert(struct qgroup_lookup *root_tree,
528                               struct btrfs_qgroup *ins)
529 {
530
531         struct rb_node **p = &root_tree->root.rb_node;
532         struct rb_node *parent = NULL;
533         struct btrfs_qgroup *curr;
534         int ret;
535
536         while (*p) {
537                 parent = *p;
538                 curr = rb_entry(parent, struct btrfs_qgroup, rb_node);
539
540                 ret = comp_entry_with_qgroupid(ins, curr, 0);
541                 if (ret < 0)
542                         p = &(*p)->rb_left;
543                 else if (ret > 0)
544                         p = &(*p)->rb_right;
545                 else
546                         return -EEXIST;
547         }
548         rb_link_node(&ins->rb_node, parent, p);
549         rb_insert_color(&ins->rb_node, &root_tree->root);
550         return 0;
551 }
552
553 /*
554  *find a given qgroupid in the tree. We return the smallest one,
555  *rb_next can be used to move forward looking for more if required
556  */
557 static struct btrfs_qgroup *qgroup_tree_search(struct qgroup_lookup *root_tree,
558                                                u64 qgroupid)
559 {
560         struct rb_node *n = root_tree->root.rb_node;
561         struct btrfs_qgroup *entry;
562         struct btrfs_qgroup tmp;
563         int ret;
564
565         tmp.qgroupid = qgroupid;
566
567         while (n) {
568                 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
569
570                 ret = comp_entry_with_qgroupid(&tmp, entry, 0);
571                 if (ret < 0)
572                         n = n->rb_left;
573                 else if (ret > 0)
574                         n = n->rb_right;
575                 else
576                         return entry;
577
578         }
579         return NULL;
580 }
581
582 static int update_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
583                          u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
584                          u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
585                          u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *pa,
586                          struct btrfs_qgroup *child)
587 {
588         struct btrfs_qgroup *bq;
589         struct btrfs_qgroup_list *list;
590
591         bq = qgroup_tree_search(qgroup_lookup, qgroupid);
592         if (!bq || bq->qgroupid != qgroupid)
593                 return -ENOENT;
594
595         if (generation)
596                 bq->generation = generation;
597         if (rfer)
598                 bq->rfer = rfer;
599         if (rfer_cmpr)
600                 bq->rfer_cmpr = rfer_cmpr;
601         if (excl)
602                 bq->excl = excl;
603         if (excl_cmpr)
604                 bq->excl_cmpr = excl_cmpr;
605         if (flags)
606                 bq->flags = flags;
607         if (max_rfer)
608                 bq->max_rfer = max_rfer;
609         if (max_excl)
610                 bq->max_excl = max_excl;
611         if (rsv_rfer)
612                 bq->rsv_rfer = rsv_rfer;
613         if (pa && child) {
614                 list = malloc(sizeof(*list));
615                 if (!list) {
616                         fprintf(stderr, "memory allocation failed\n");
617                         exit(1);
618                 }
619                 list->qgroup = pa;
620                 list->member = child;
621                 list_add_tail(&list->next_qgroup, &child->qgroups);
622                 list_add_tail(&list->next_member, &pa->members);
623         }
624         return 0;
625 }
626
627 static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
628                       u64 generation, u64 rfer, u64 rfer_cmpr, u64 excl,
629                       u64 excl_cmpr, u64 flags, u64 max_rfer, u64 max_excl,
630                       u64 rsv_rfer, u64 rsv_excl, struct btrfs_qgroup *parent,
631                       struct btrfs_qgroup *child)
632 {
633         struct btrfs_qgroup *bq;
634         struct btrfs_qgroup_list *list;
635         int ret;
636
637         ret = update_qgroup(qgroup_lookup, qgroupid, generation, rfer,
638                             rfer_cmpr, excl, excl_cmpr, flags, max_rfer,
639                             max_excl, rsv_rfer, rsv_excl, parent, child);
640         if (!ret)
641                 return 0;
642
643         bq = malloc(sizeof(*bq));
644         if (!bq) {
645                 printf("memory allocation failed\n");
646                 exit(1);
647         }
648         memset(bq, 0, sizeof(*bq));
649         if (qgroupid) {
650                 bq->qgroupid = qgroupid;
651                 INIT_LIST_HEAD(&bq->qgroups);
652                 INIT_LIST_HEAD(&bq->members);
653         }
654         if (generation)
655                 bq->generation = generation;
656         if (rfer)
657                 bq->rfer = rfer;
658         if (rfer_cmpr)
659                 bq->rfer_cmpr = rfer_cmpr;
660         if (excl)
661                 bq->excl = excl;
662         if (excl_cmpr)
663                 bq->excl_cmpr = excl_cmpr;
664         if (flags)
665                 bq->flags = flags;
666         if (max_rfer)
667                 bq->max_rfer = max_rfer;
668         if (max_excl)
669                 bq->max_excl = max_excl;
670         if (rsv_rfer)
671                 bq->rsv_rfer = rsv_rfer;
672         if (parent && child) {
673                 list = malloc(sizeof(*list));
674                 if (!list) {
675                         fprintf(stderr, "memory allocation failed\n");
676                         exit(1);
677                 }
678                 list->qgroup = parent;
679                 list->member = child;
680                 list_add_tail(&list->next_qgroup, &child->qgroups);
681                 list_add_tail(&list->next_member, &parent->members);
682         }
683         ret = qgroup_tree_insert(qgroup_lookup, bq);
684         if (ret) {
685                 printf("failed to insert tree %llu\n",
686                        bq->qgroupid);
687                 exit(1);
688         }
689         return ret;
690 }
691
692 static void __free_btrfs_qgroup(struct btrfs_qgroup *bq)
693 {
694         struct btrfs_qgroup_list *list;
695         while (!list_empty(&bq->qgroups)) {
696                 list = list_entry((&bq->qgroups)->next,
697                                   struct btrfs_qgroup_list,
698                                   next_qgroup);
699                 list_del(&list->next_qgroup);
700                 list_del(&list->next_member);
701                 free(list);
702         }
703         while (!list_empty(&bq->members)) {
704                 list = list_entry((&bq->members)->next,
705                                   struct btrfs_qgroup_list,
706                                   next_member);
707                 list_del(&list->next_qgroup);
708                 list_del(&list->next_member);
709                 free(list);
710         }
711         free(bq);
712 }
713
714 static void __free_all_qgroups(struct qgroup_lookup *root_tree)
715 {
716         struct btrfs_qgroup *entry;
717         struct rb_node *n;
718
719         n = rb_first(&root_tree->root);
720         while (n) {
721                 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
722                 rb_erase(n, &root_tree->root);
723                 __free_btrfs_qgroup(entry);
724
725                 n = rb_first(&root_tree->root);
726         }
727 }
728
729 static int filter_all_parent_insert(struct qgroup_lookup *sort_tree,
730                                     struct btrfs_qgroup *bq)
731 {
732         struct rb_node **p = &sort_tree->root.rb_node;
733         struct rb_node *parent = NULL;
734         struct btrfs_qgroup *curr;
735         int ret;
736
737         while (*p) {
738                 parent = *p;
739                 curr = rb_entry(parent, struct btrfs_qgroup, all_parent_node);
740
741                 ret = comp_entry_with_qgroupid(bq, curr, 0);
742                 if (ret < 0)
743                         p = &(*p)->rb_left;
744                 else if (ret > 0)
745                         p = &(*p)->rb_right;
746                 else
747                         return -EEXIST;
748         }
749         rb_link_node(&bq->all_parent_node, parent, p);
750         rb_insert_color(&bq->all_parent_node, &sort_tree->root);
751         return 0;
752 }
753
754 static int filter_by_parent(struct btrfs_qgroup *bq, u64 data)
755 {
756         struct btrfs_qgroup *qgroup =
757                 (struct btrfs_qgroup *)(unsigned long)data;
758
759         if (data == 0)
760                 return 0;
761         if (qgroup->qgroupid == bq->qgroupid)
762                 return 1;
763         return 0;
764 }
765
766 static int filter_by_all_parent(struct btrfs_qgroup *bq, u64 data)
767 {
768         struct qgroup_lookup lookup;
769         struct qgroup_lookup *ql = &lookup;
770         struct btrfs_qgroup_list *list;
771         struct rb_node *n;
772         struct btrfs_qgroup *qgroup =
773                          (struct btrfs_qgroup *)(unsigned long)data;
774
775         if (data == 0)
776                 return 0;
777         if (bq->qgroupid == qgroup->qgroupid)
778                 return 1;
779
780         qgroup_lookup_init(ql);
781         filter_all_parent_insert(ql, qgroup);
782         n = rb_first(&ql->root);
783         while (n) {
784                 qgroup = rb_entry(n, struct btrfs_qgroup, all_parent_node);
785                 if (!list_empty(&qgroup->qgroups)) {
786                         list_for_each_entry(list, &qgroup->qgroups,
787                                             next_qgroup) {
788                                 if ((list->qgroup)->qgroupid == bq->qgroupid)
789                                         return 1;
790                                 filter_all_parent_insert(ql, list->qgroup);
791                         }
792                 }
793                 rb_erase(n, &ql->root);
794                 n = rb_first(&ql->root);
795         }
796         return 0;
797 }
798
799 static btrfs_qgroup_filter_func all_filter_funcs[] = {
800         [BTRFS_QGROUP_FILTER_PARENT]            = filter_by_parent,
801         [BTRFS_QGROUP_FILTER_ALL_PARENT]        = filter_by_all_parent,
802 };
803
804 struct btrfs_qgroup_filter_set *btrfs_qgroup_alloc_filter_set(void)
805 {
806         struct btrfs_qgroup_filter_set *set;
807         int size;
808
809         size = sizeof(struct btrfs_qgroup_filter_set) +
810                BTRFS_QGROUP_NFILTERS_INCREASE *
811                sizeof(struct btrfs_qgroup_filter);
812         set = malloc(size);
813         if (!set) {
814                 fprintf(stderr, "memory allocation failed\n");
815                 exit(1);
816         }
817         memset(set, 0, size);
818         set->total = BTRFS_QGROUP_NFILTERS_INCREASE;
819
820         return set;
821 }
822
823 void btrfs_qgroup_free_filter_set(struct btrfs_qgroup_filter_set *filter_set)
824 {
825         free(filter_set);
826 }
827
828 int btrfs_qgroup_setup_filter(struct btrfs_qgroup_filter_set **filter_set,
829                               enum btrfs_qgroup_filter_enum filter, u64 data)
830 {
831         struct btrfs_qgroup_filter_set *set = *filter_set;
832         int size;
833
834         BUG_ON(!set);
835         BUG_ON(filter >= BTRFS_QGROUP_FILTER_MAX);
836         BUG_ON(set->nfilters > set->total);
837
838         if (set->nfilters == set->total) {
839                 size = set->total + BTRFS_QGROUP_NFILTERS_INCREASE;
840                 size = sizeof(*set) + size * sizeof(struct btrfs_qgroup_filter);
841
842                 set = realloc(set, size);
843                 if (!set) {
844                         fprintf(stderr, "memory allocation failed\n");
845                         exit(1);
846                 }
847                 memset(&set->filters[set->total], 0,
848                        BTRFS_QGROUP_NFILTERS_INCREASE *
849                        sizeof(struct btrfs_qgroup_filter));
850                 set->total += BTRFS_QGROUP_NFILTERS_INCREASE;
851                 *filter_set = set;
852         }
853         BUG_ON(set->filters[set->nfilters].filter_func);
854         set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
855         set->filters[set->nfilters].data = data;
856         set->nfilters++;
857         return 0;
858 }
859
860 static int filter_qgroup(struct btrfs_qgroup *bq,
861                          struct btrfs_qgroup_filter_set *set)
862 {
863         int i, ret;
864
865         if (!set || !set->nfilters)
866                 return 1;
867         for (i = 0; i < set->nfilters; i++) {
868                 if (!set->filters[i].filter_func)
869                         break;
870                 ret = set->filters[i].filter_func(bq, set->filters[i].data);
871                 if (!ret)
872                         return 0;
873         }
874         return 1;
875 }
876
877 static void pre_process_filter_set(struct qgroup_lookup *lookup,
878                                    struct btrfs_qgroup_filter_set *set)
879 {
880         int i;
881         struct btrfs_qgroup *qgroup_for_filter = NULL;
882
883         for (i = 0; i < set->nfilters; i++) {
884
885                 if (set->filters[i].filter_func == filter_by_all_parent
886                     || set->filters[i].filter_func == filter_by_parent) {
887                         qgroup_for_filter = qgroup_tree_search(lookup,
888                                             set->filters[i].data);
889                         set->filters[i].data =
890                                  (u64)(unsigned long)qgroup_for_filter;
891                 }
892         }
893 }
894
895 static int sort_tree_insert(struct qgroup_lookup *sort_tree,
896                             struct btrfs_qgroup *bq,
897                             struct btrfs_qgroup_comparer_set *comp_set)
898 {
899         struct rb_node **p = &sort_tree->root.rb_node;
900         struct rb_node *parent = NULL;
901         struct btrfs_qgroup *curr;
902         int ret;
903
904         while (*p) {
905                 parent = *p;
906                 curr = rb_entry(parent, struct btrfs_qgroup, sort_node);
907
908                 ret = sort_comp(bq, curr, comp_set);
909                 if (ret < 0)
910                         p = &(*p)->rb_left;
911                 else if (ret > 0)
912                         p = &(*p)->rb_right;
913                 else
914                         return -EEXIST;
915         }
916         rb_link_node(&bq->sort_node, parent, p);
917         rb_insert_color(&bq->sort_node, &sort_tree->root);
918         return 0;
919 }
920
921 static void __update_columns_max_len(struct btrfs_qgroup *bq,
922                                      enum btrfs_qgroup_column_enum column)
923 {
924         BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
925         struct btrfs_qgroup_list *list = NULL;
926         char tmp[100];
927         int len;
928         unsigned unit_mode = btrfs_qgroup_columns[column].unit_mode;
929
930         switch (column) {
931
932         case BTRFS_QGROUP_QGROUPID:
933                 sprintf(tmp, "%llu/%llu",
934                         btrfs_qgroup_level(bq->qgroupid),
935                         btrfs_qgroup_subvid(bq->qgroupid));
936                 len = strlen(tmp);
937                 if (btrfs_qgroup_columns[column].max_len < len)
938                         btrfs_qgroup_columns[column].max_len = len;
939                 break;
940         case BTRFS_QGROUP_RFER:
941                 len = strlen(pretty_size_mode(bq->rfer, unit_mode));
942                 if (btrfs_qgroup_columns[column].max_len < len)
943                         btrfs_qgroup_columns[column].max_len = len;
944                 break;
945         case BTRFS_QGROUP_EXCL:
946                 len = strlen(pretty_size_mode(bq->excl, unit_mode));
947                 if (btrfs_qgroup_columns[column].max_len < len)
948                         btrfs_qgroup_columns[column].max_len = len;
949                 break;
950         case BTRFS_QGROUP_MAX_RFER:
951                 len = strlen(pretty_size_mode(bq->max_rfer, unit_mode));
952                 if (btrfs_qgroup_columns[column].max_len < len)
953                         btrfs_qgroup_columns[column].max_len = len;
954                 break;
955         case BTRFS_QGROUP_MAX_EXCL:
956                 len = strlen(pretty_size_mode(bq->max_excl, unit_mode));
957                 if (btrfs_qgroup_columns[column].max_len < len)
958                         btrfs_qgroup_columns[column].max_len = len;
959                 break;
960         case BTRFS_QGROUP_PARENT:
961                 len = 0;
962                 list_for_each_entry(list, &bq->qgroups, next_qgroup) {
963                         len += sprintf(tmp, "%llu/%llu",
964                                 btrfs_qgroup_level(list->qgroup->qgroupid),
965                                 btrfs_qgroup_subvid(list->qgroup->qgroupid));
966                         if (!list_is_last(&list->next_qgroup, &bq->qgroups))
967                                 len += 1;
968                 }
969                 if (btrfs_qgroup_columns[column].max_len < len)
970                         btrfs_qgroup_columns[column].max_len = len;
971                 break;
972         case BTRFS_QGROUP_CHILD:
973                 len = 0;
974                 list_for_each_entry(list, &bq->members, next_member) {
975                         len += sprintf(tmp, "%llu/%llu",
976                                 btrfs_qgroup_level(list->member->qgroupid),
977                                 btrfs_qgroup_subvid(list->member->qgroupid));
978                         if (!list_is_last(&list->next_member, &bq->members))
979                                 len += 1;
980                 }
981                 if (btrfs_qgroup_columns[column].max_len < len)
982                         btrfs_qgroup_columns[column].max_len = len;
983                 break;
984         default:
985                 break;
986         }
987
988 }
989
990 static void update_columns_max_len(struct btrfs_qgroup *bq)
991 {
992         int i;
993
994         for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
995                 if (!btrfs_qgroup_columns[i].need_print)
996                         continue;
997                 __update_columns_max_len(bq, i);
998         }
999 }
1000
1001 static void __filter_and_sort_qgroups(struct qgroup_lookup *all_qgroups,
1002                                  struct qgroup_lookup *sort_tree,
1003                                  struct btrfs_qgroup_filter_set *filter_set,
1004                                  struct btrfs_qgroup_comparer_set *comp_set)
1005 {
1006         struct rb_node *n;
1007         struct btrfs_qgroup *entry;
1008         int ret;
1009
1010         qgroup_lookup_init(sort_tree);
1011         pre_process_filter_set(all_qgroups, filter_set);
1012
1013         n = rb_last(&all_qgroups->root);
1014         while (n) {
1015                 entry = rb_entry(n, struct btrfs_qgroup, rb_node);
1016
1017                 ret = filter_qgroup(entry, filter_set);
1018                 if (ret) {
1019                         sort_tree_insert(sort_tree, entry, comp_set);
1020
1021                         update_columns_max_len(entry);
1022                 }
1023                 n = rb_prev(n);
1024         }
1025 }
1026
1027 static inline void print_status_flag_warning(u64 flags)
1028 {
1029         if (!(flags & BTRFS_QGROUP_STATUS_FLAG_ON))
1030                 fprintf(stderr,
1031                 "WARNING: Quota disabled, qgroup data may be out of date\n");
1032         else if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
1033                 fprintf(stderr,
1034                 "WARNING: Rescan is running, qgroup data may be incorrect\n");
1035         else if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
1036                 fprintf(stderr,
1037                 "WARNING: Qgroup data inconsistent, rescan recommended\n");
1038 }
1039
1040 static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
1041 {
1042         int ret;
1043         struct btrfs_ioctl_search_args args;
1044         struct btrfs_ioctl_search_key *sk = &args.key;
1045         struct btrfs_ioctl_search_header *sh;
1046         unsigned long off = 0;
1047         unsigned int i;
1048         int e;
1049         struct btrfs_qgroup_info_item *info;
1050         struct btrfs_qgroup_limit_item *limit;
1051         struct btrfs_qgroup *bq;
1052         struct btrfs_qgroup *bq1;
1053         u64 a1;
1054         u64 a2;
1055         u64 a3;
1056         u64 a4;
1057         u64 a5;
1058
1059         memset(&args, 0, sizeof(args));
1060
1061         sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
1062         sk->max_type = BTRFS_QGROUP_RELATION_KEY;
1063         sk->min_type = BTRFS_QGROUP_STATUS_KEY;
1064         sk->max_objectid = (u64)-1;
1065         sk->max_offset = (u64)-1;
1066         sk->max_transid = (u64)-1;
1067         sk->nr_items = 4096;
1068
1069         qgroup_lookup_init(qgroup_lookup);
1070
1071         while (1) {
1072                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
1073                 e = errno;
1074                 if (ret < 0) {
1075                         fprintf(stderr,
1076                                 "ERROR: can't perform the search - %s\n",
1077                                 strerror(e));
1078                         return ret;
1079                 }
1080                 /* the ioctl returns the number of item it found in nr_items */
1081                 if (sk->nr_items == 0)
1082                         break;
1083
1084                 off = 0;
1085                 /*
1086                  * for each item, pull the key out of the header and then
1087                  * read the root_ref item it contains
1088                  */
1089                 for (i = 0; i < sk->nr_items; i++) {
1090                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
1091                                                                   off);
1092                         off += sizeof(*sh);
1093
1094                         if (sh->type == BTRFS_QGROUP_STATUS_KEY) {
1095                                 struct btrfs_qgroup_status_item *si;
1096                                 u64 flags;
1097
1098                                 si = (struct btrfs_qgroup_status_item *)
1099                                      (args.buf + off);
1100                                 flags = btrfs_stack_qgroup_status_flags(si);
1101                                 print_status_flag_warning(flags);
1102                         } else if (sh->type == BTRFS_QGROUP_INFO_KEY) {
1103                                 info = (struct btrfs_qgroup_info_item *)
1104                                        (args.buf + off);
1105                                 a1 = btrfs_stack_qgroup_info_generation(info);
1106                                 a2 = btrfs_stack_qgroup_info_referenced(info);
1107                                 a3 =
1108                                   btrfs_stack_qgroup_info_referenced_compressed
1109                                   (info);
1110                                 a4 = btrfs_stack_qgroup_info_exclusive(info);
1111                                 a5 =
1112                                   btrfs_stack_qgroup_info_exclusive_compressed
1113                                   (info);
1114                                 add_qgroup(qgroup_lookup, sh->offset, a1, a2,
1115                                            a3, a4, a5, 0, 0, 0, 0, 0, 0, 0);
1116                         } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
1117                                 limit = (struct btrfs_qgroup_limit_item *)
1118                                     (args.buf + off);
1119
1120                                 a1 = btrfs_stack_qgroup_limit_flags(limit);
1121                                 a2 = btrfs_stack_qgroup_limit_max_referenced
1122                                      (limit);
1123                                 a3 = btrfs_stack_qgroup_limit_max_exclusive
1124                                      (limit);
1125                                 a4 = btrfs_stack_qgroup_limit_rsv_referenced
1126                                      (limit);
1127                                 a5 = btrfs_stack_qgroup_limit_rsv_exclusive
1128                                      (limit);
1129                                 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
1130                                            0, 0, 0, a1, a2, a3, a4, a5, 0, 0);
1131                         } else if (sh->type == BTRFS_QGROUP_RELATION_KEY) {
1132                                 if (sh->offset < sh->objectid)
1133                                         goto skip;
1134                                 bq = qgroup_tree_search(qgroup_lookup,
1135                                                         sh->offset);
1136                                 if (!bq)
1137                                         goto skip;
1138                                 bq1 = qgroup_tree_search(qgroup_lookup,
1139                                                          sh->objectid);
1140                                 if (!bq1)
1141                                         goto skip;
1142                                 add_qgroup(qgroup_lookup, sh->offset, 0, 0,
1143                                            0, 0, 0, 0, 0, 0, 0, 0, bq, bq1);
1144                         } else
1145                                 goto done;
1146 skip:
1147                         off += sh->len;
1148
1149                         /*
1150                          * record the mins in sk so we can make sure the
1151                          * next search doesn't repeat this root
1152                          */
1153                         sk->min_type = sh->type;
1154                         sk->min_offset = sh->offset;
1155                         sk->min_objectid = sh->objectid;
1156                 }
1157                 sk->nr_items = 4096;
1158                 /*
1159                  * this iteration is done, step forward one qgroup for the next
1160                  * ioctl
1161                  */
1162                 if (sk->min_offset < (u64)-1)
1163                         sk->min_offset++;
1164                 else
1165                         break;
1166         }
1167
1168 done:
1169         return ret;
1170 }
1171
1172 static void print_all_qgroups(struct qgroup_lookup *qgroup_lookup)
1173 {
1174
1175         struct rb_node *n;
1176         struct btrfs_qgroup *entry;
1177
1178         print_table_head();
1179
1180         n = rb_first(&qgroup_lookup->root);
1181         while (n) {
1182                 entry = rb_entry(n, struct btrfs_qgroup, sort_node);
1183                 print_single_qgroup_table(entry);
1184                 n = rb_next(n);
1185         }
1186 }
1187
1188 int btrfs_show_qgroups(int fd,
1189                        struct btrfs_qgroup_filter_set *filter_set,
1190                        struct btrfs_qgroup_comparer_set *comp_set)
1191 {
1192
1193         struct qgroup_lookup qgroup_lookup;
1194         struct qgroup_lookup sort_tree;
1195         int ret;
1196
1197         ret = __qgroups_search(fd, &qgroup_lookup);
1198         if (ret)
1199                 return ret;
1200         __filter_and_sort_qgroups(&qgroup_lookup, &sort_tree,
1201                                   filter_set, comp_set);
1202         print_all_qgroups(&sort_tree);
1203
1204         __free_all_qgroups(&qgroup_lookup);
1205         btrfs_qgroup_free_filter_set(filter_set);
1206         return ret;
1207 }
1208
1209 u64 btrfs_get_path_rootid(int fd)
1210 {
1211         int  ret;
1212         struct btrfs_ioctl_ino_lookup_args args;
1213
1214         memset(&args, 0, sizeof(args));
1215         args.objectid = BTRFS_FIRST_FREE_OBJECTID;
1216
1217         ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
1218         if (ret < 0) {
1219                 fprintf(stderr,
1220                         "ERROR: can't perform the search - %s\n",
1221                         strerror(errno));
1222                 return ret;
1223         }
1224         return args.treeid;
1225 }
1226
1227 int btrfs_qgroup_parse_sort_string(char *opt_arg,
1228                                    struct btrfs_qgroup_comparer_set **comps)
1229 {
1230         int order;
1231         int flag;
1232         char *p;
1233         char **ptr_argv;
1234         int what_to_sort;
1235
1236         while ((p = strtok(opt_arg, ",")) != NULL) {
1237                 flag = 0;
1238                 ptr_argv = all_sort_items;
1239
1240                 while (*ptr_argv) {
1241                         if (strcmp(*ptr_argv, p) == 0) {
1242                                 flag = 1;
1243                                 break;
1244                         } else {
1245                                 p++;
1246                                 if (strcmp(*ptr_argv, p) == 0) {
1247                                         flag = 1;
1248                                         p--;
1249                                         break;
1250                                 }
1251                                 p--;
1252                         }
1253                         ptr_argv++;
1254                 }
1255
1256                 if (flag == 0)
1257                         return -1;
1258
1259                 else {
1260                         if (*p == '+') {
1261                                 order = 0;
1262                                 p++;
1263                         } else if (*p == '-') {
1264                                 order = 1;
1265                                 p++;
1266                         } else
1267                                 order = 0;
1268
1269                         what_to_sort = btrfs_qgroup_get_sort_item(p);
1270                         if (what_to_sort < 0)
1271                                 return -1;
1272                         btrfs_qgroup_setup_comparer(comps, what_to_sort, order);
1273                 }
1274                 opt_arg = NULL;
1275         }
1276
1277         return 0;
1278 }
1279
1280 int qgroup_inherit_size(struct btrfs_qgroup_inherit *p)
1281 {
1282         return sizeof(*p) + sizeof(p->qgroups[0]) *
1283                             (p->num_qgroups + 2 * p->num_ref_copies +
1284                              2 * p->num_excl_copies);
1285 }
1286
1287 static int
1288 qgroup_inherit_realloc(struct btrfs_qgroup_inherit **inherit, int n, int pos)
1289 {
1290         struct btrfs_qgroup_inherit *out;
1291         int nitems = 0;
1292
1293         if (*inherit) {
1294                 nitems = (*inherit)->num_qgroups +
1295                          (*inherit)->num_ref_copies +
1296                          (*inherit)->num_excl_copies;
1297         }
1298
1299         out = calloc(sizeof(*out) + sizeof(out->qgroups[0]) * (nitems + n), 1);
1300         if (out == NULL) {
1301                 fprintf(stderr, "ERROR: Not enough memory\n");
1302                 return -ENOMEM;
1303         }
1304
1305         if (*inherit) {
1306                 struct btrfs_qgroup_inherit *i = *inherit;
1307                 int s = sizeof(out->qgroups[0]);
1308
1309                 out->num_qgroups = i->num_qgroups;
1310                 out->num_ref_copies = i->num_ref_copies;
1311                 out->num_excl_copies = i->num_excl_copies;
1312                 memcpy(out->qgroups, i->qgroups, pos * s);
1313                 memcpy(out->qgroups + pos + n, i->qgroups + pos,
1314                        (nitems - pos) * s);
1315         }
1316         free(*inherit);
1317         *inherit = out;
1318
1319         return 0;
1320 }
1321
1322 int qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg)
1323 {
1324         int ret;
1325         u64 qgroupid = parse_qgroupid(arg);
1326         int pos = 0;
1327
1328         if (qgroupid == 0) {
1329                 fprintf(stderr, "ERROR: bad qgroup specification\n");
1330                 return -EINVAL;
1331         }
1332
1333         if (*inherit)
1334                 pos = (*inherit)->num_qgroups;
1335         ret = qgroup_inherit_realloc(inherit, 1, pos);
1336         if (ret)
1337                 return ret;
1338
1339         (*inherit)->qgroups[(*inherit)->num_qgroups++] = qgroupid;
1340
1341         return 0;
1342 }
1343
1344 int qgroup_inherit_add_copy(struct btrfs_qgroup_inherit **inherit, char *arg,
1345                             int type)
1346 {
1347         int ret;
1348         u64 qgroup_src;
1349         u64 qgroup_dst;
1350         char *p;
1351         int pos = 0;
1352
1353         p = strchr(arg, ':');
1354         if (!p) {
1355 bad:
1356                 fprintf(stderr, "ERROR: bad copy specification\n");
1357                 return -EINVAL;
1358         }
1359         *p = 0;
1360         qgroup_src = parse_qgroupid(arg);
1361         qgroup_dst = parse_qgroupid(p + 1);
1362         *p = ':';
1363
1364         if (!qgroup_src || !qgroup_dst)
1365                 goto bad;
1366
1367         if (*inherit)
1368                 pos = (*inherit)->num_qgroups +
1369                       (*inherit)->num_ref_copies * 2 * type;
1370
1371         ret = qgroup_inherit_realloc(inherit, 2, pos);
1372         if (ret)
1373                 return ret;
1374
1375         (*inherit)->qgroups[pos++] = qgroup_src;
1376         (*inherit)->qgroups[pos++] = qgroup_dst;
1377
1378         if (!type)
1379                 ++(*inherit)->num_ref_copies;
1380         else
1381                 ++(*inherit)->num_excl_copies;
1382
1383         return 0;
1384 }