forked from kdave/btrfs-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgroup.c
1460 lines (1255 loc) · 33.5 KB
/
qgroup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012 STRATO. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include "qgroup.h"
#include <sys/ioctl.h>
#include "kernel-shared/ctree.h"
#include "ioctl.h"
#include "common/utils.h"
#include "common/units.h"
#include <errno.h>
#define BTRFS_QGROUP_NFILTERS_INCREASE (2 * BTRFS_QGROUP_FILTER_MAX)
#define BTRFS_QGROUP_NCOMPS_INCREASE (2 * BTRFS_QGROUP_COMP_MAX)
struct qgroup_lookup {
struct rb_root root;
};
struct btrfs_qgroup {
struct rb_node rb_node;
struct rb_node sort_node;
/*
*all_parent_node is used to
*filter a qgroup's all parent
*/
struct rb_node all_parent_node;
u64 qgroupid;
/*
* info_item
*/
struct btrfs_qgroup_info info;
/*
*limit_item
*/
struct btrfs_qgroup_limit limit;
/*qgroups this group is member of*/
struct list_head qgroups;
/*qgroups that are members of this group*/
struct list_head members;
};
/*
* glue structure to represent the relations
* between qgroups
*/
struct btrfs_qgroup_list {
struct list_head next_qgroup;
struct list_head next_member;
struct btrfs_qgroup *qgroup;
struct btrfs_qgroup *member;
};
/*
* qgroupid,rfer,excl default to set
*/
static struct {
char *name;
char *column_name;
int need_print;
unsigned unit_mode;
int max_len;
} btrfs_qgroup_columns[] = {
{
.name = "qgroupid",
.column_name = "Qgroupid",
.need_print = 1,
.unit_mode = 0,
.max_len = 8,
},
{
.name = "rfer",
.column_name = "Rfer",
.need_print = 1,
.unit_mode = UNITS_DEFAULT,
.max_len = 12,
},
{
.name = "excl",
.column_name = "Excl",
.need_print = 1,
.unit_mode = UNITS_DEFAULT,
.max_len = 12,
},
{ .name = "max_rfer",
.column_name = "Max_rfer",
.need_print = 0,
.unit_mode = UNITS_DEFAULT,
.max_len = 12,
},
{
.name = "max_excl",
.column_name = "Max_excl",
.need_print = 0,
.unit_mode = UNITS_DEFAULT,
.max_len = 12,
},
{
.name = "parent",
.column_name = "Parent",
.need_print = 0,
.unit_mode = 0,
.max_len = 7,
},
{
.name = "child",
.column_name = "Child",
.need_print = 0,
.unit_mode = 0,
.max_len = 5,
},
{
.name = NULL,
.column_name = NULL,
.need_print = 0,
.unit_mode = 0,
},
};
static btrfs_qgroup_filter_func all_filter_funcs[];
static btrfs_qgroup_comp_func all_comp_funcs[];
void btrfs_qgroup_setup_print_column(enum btrfs_qgroup_column_enum column)
{
int i;
ASSERT(0 <= column && column <= BTRFS_QGROUP_ALL);
if (column < BTRFS_QGROUP_ALL) {
btrfs_qgroup_columns[column].need_print = 1;
return;
}
for (i = 0; i < BTRFS_QGROUP_ALL; i++)
btrfs_qgroup_columns[i].need_print = 1;
}
void btrfs_qgroup_setup_units(unsigned unit_mode)
{
btrfs_qgroup_columns[BTRFS_QGROUP_RFER].unit_mode = unit_mode;
btrfs_qgroup_columns[BTRFS_QGROUP_EXCL].unit_mode = unit_mode;
btrfs_qgroup_columns[BTRFS_QGROUP_MAX_RFER].unit_mode = unit_mode;
btrfs_qgroup_columns[BTRFS_QGROUP_MAX_EXCL].unit_mode = unit_mode;
}
static int print_parent_column(struct btrfs_qgroup *qgroup)
{
struct btrfs_qgroup_list *list = NULL;
int len = 0;
list_for_each_entry(list, &qgroup->qgroups, next_qgroup) {
len += printf("%llu/%llu",
btrfs_qgroup_level(list->qgroup->qgroupid),
btrfs_qgroup_subvid(list->qgroup->qgroupid));
if (!list_is_last(&list->next_qgroup, &qgroup->qgroups))
len += printf(",");
}
if (list_empty(&qgroup->qgroups))
len += printf("---");
return len;
}
static int print_child_column(struct btrfs_qgroup *qgroup)
{
struct btrfs_qgroup_list *list = NULL;
int len = 0;
list_for_each_entry(list, &qgroup->members, next_member) {
len += printf("%llu/%llu",
btrfs_qgroup_level(list->member->qgroupid),
btrfs_qgroup_subvid(list->member->qgroupid));
if (!list_is_last(&list->next_member, &qgroup->members))
len += printf(",");
}
if (list_empty(&qgroup->members))
len += printf("---");
return len;
}
static int print_u64(u64 value, int unit_mode, int max_len)
{
return printf("%*s", max_len, pretty_size_mode(value, unit_mode));
}
static void print_qgroup_column_add_blank(enum btrfs_qgroup_column_enum column,
int len)
{
len = btrfs_qgroup_columns[column].max_len - len;
while (len--)
printf(" ");
}
static void print_qgroup_column(struct btrfs_qgroup *qgroup,
enum btrfs_qgroup_column_enum column)
{
int len;
int unit_mode = btrfs_qgroup_columns[column].unit_mode;
int max_len = btrfs_qgroup_columns[column].max_len;
ASSERT(0 <= column && column < BTRFS_QGROUP_ALL);
switch (column) {
case BTRFS_QGROUP_QGROUPID:
len = printf("%llu/%llu",
btrfs_qgroup_level(qgroup->qgroupid),
btrfs_qgroup_subvid(qgroup->qgroupid));
print_qgroup_column_add_blank(BTRFS_QGROUP_QGROUPID, len);
break;
case BTRFS_QGROUP_RFER:
len = print_u64(qgroup->info.referenced, unit_mode, max_len);
break;
case BTRFS_QGROUP_EXCL:
len = print_u64(qgroup->info.exclusive, unit_mode, max_len);
break;
case BTRFS_QGROUP_PARENT:
len = print_parent_column(qgroup);
print_qgroup_column_add_blank(BTRFS_QGROUP_PARENT, len);
break;
case BTRFS_QGROUP_MAX_RFER:
if (qgroup->limit.flags & BTRFS_QGROUP_LIMIT_MAX_RFER)
len = print_u64(qgroup->limit.max_referenced,
unit_mode, max_len);
else
len = printf("%*s", max_len, "none");
break;
case BTRFS_QGROUP_MAX_EXCL:
if (qgroup->limit.flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
len = print_u64(qgroup->limit.max_exclusive,
unit_mode, max_len);
else
len = printf("%*s", max_len, "none");
break;
case BTRFS_QGROUP_CHILD:
len = print_child_column(qgroup);
print_qgroup_column_add_blank(BTRFS_QGROUP_CHILD, len);
break;
default:
break;
}
}
static void print_single_qgroup_table(struct btrfs_qgroup *qgroup)
{
int i;
for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
if (!btrfs_qgroup_columns[i].need_print)
continue;
print_qgroup_column(qgroup, i);
if (i != BTRFS_QGROUP_ALL - 1)
printf(" ");
}
printf("\n");
}
static void print_table_head(void)
{
int i;
int len;
int max_len;
for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
max_len = btrfs_qgroup_columns[i].max_len;
if (!btrfs_qgroup_columns[i].need_print)
continue;
if ((i == BTRFS_QGROUP_QGROUPID) | (i == BTRFS_QGROUP_PARENT) |
(i == BTRFS_QGROUP_CHILD))
printf("%-*s", max_len, btrfs_qgroup_columns[i].name);
else
printf("%*s", max_len, btrfs_qgroup_columns[i].name);
printf(" ");
}
printf("\n");
for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
max_len = btrfs_qgroup_columns[i].max_len;
if (!btrfs_qgroup_columns[i].need_print)
continue;
if ((i == BTRFS_QGROUP_QGROUPID) | (i == BTRFS_QGROUP_PARENT) |
(i == BTRFS_QGROUP_CHILD)) {
len = strlen(btrfs_qgroup_columns[i].name);
while (len--)
printf("-");
len = max_len - strlen(btrfs_qgroup_columns[i].name);
while (len--)
printf(" ");
} else {
len = max_len - strlen(btrfs_qgroup_columns[i].name);
while (len--)
printf(" ");
len = strlen(btrfs_qgroup_columns[i].name);
while (len--)
printf("-");
}
printf(" ");
}
printf("\n");
}
static void qgroup_lookup_init(struct qgroup_lookup *tree)
{
tree->root.rb_node = NULL;
}
static int comp_entry_with_qgroupid(struct btrfs_qgroup *entry1,
struct btrfs_qgroup *entry2,
int is_descending)
{
int ret;
if (entry1->qgroupid > entry2->qgroupid)
ret = 1;
else if (entry1->qgroupid < entry2->qgroupid)
ret = -1;
else
ret = 0;
return is_descending ? -ret : ret;
}
static int comp_entry_with_rfer(struct btrfs_qgroup *entry1,
struct btrfs_qgroup *entry2,
int is_descending)
{
int ret;
if (entry1->info.referenced > entry2->info.referenced)
ret = 1;
else if (entry1->info.referenced < entry2->info.referenced)
ret = -1;
else
ret = 0;
return is_descending ? -ret : ret;
}
static int comp_entry_with_excl(struct btrfs_qgroup *entry1,
struct btrfs_qgroup *entry2,
int is_descending)
{
int ret;
if (entry1->info.exclusive > entry2->info.exclusive)
ret = 1;
else if (entry1->info.exclusive < entry2->info.exclusive)
ret = -1;
else
ret = 0;
return is_descending ? -ret : ret;
}
static int comp_entry_with_max_rfer(struct btrfs_qgroup *entry1,
struct btrfs_qgroup *entry2,
int is_descending)
{
int ret;
if (entry1->limit.max_referenced > entry2->limit.max_referenced)
ret = 1;
else if (entry1->limit.max_referenced < entry2->limit.max_referenced)
ret = -1;
else
ret = 0;
return is_descending ? -ret : ret;
}
static int comp_entry_with_max_excl(struct btrfs_qgroup *entry1,
struct btrfs_qgroup *entry2,
int is_descending)
{
int ret;
if (entry1->limit.max_exclusive > entry2->limit.max_exclusive)
ret = 1;
else if (entry1->limit.max_exclusive < entry2->limit.max_exclusive)
ret = -1;
else
ret = 0;
return is_descending ? -ret : ret;
}
static btrfs_qgroup_comp_func all_comp_funcs[] = {
[BTRFS_QGROUP_COMP_QGROUPID] = comp_entry_with_qgroupid,
[BTRFS_QGROUP_COMP_RFER] = comp_entry_with_rfer,
[BTRFS_QGROUP_COMP_EXCL] = comp_entry_with_excl,
[BTRFS_QGROUP_COMP_MAX_RFER] = comp_entry_with_max_rfer,
[BTRFS_QGROUP_COMP_MAX_EXCL] = comp_entry_with_max_excl
};
static char *all_sort_items[] = {
[BTRFS_QGROUP_COMP_QGROUPID] = "qgroupid",
[BTRFS_QGROUP_COMP_RFER] = "rfer",
[BTRFS_QGROUP_COMP_EXCL] = "excl",
[BTRFS_QGROUP_COMP_MAX_RFER] = "max_rfer",
[BTRFS_QGROUP_COMP_MAX_EXCL] = "max_excl",
[BTRFS_QGROUP_COMP_MAX] = NULL,
};
static int btrfs_qgroup_get_sort_item(char *sort_name)
{
int i;
for (i = 0; i < BTRFS_QGROUP_COMP_MAX; i++) {
if (strcmp(sort_name, all_sort_items[i]) == 0)
return i;
}
return -1;
}
struct btrfs_qgroup_comparer_set *btrfs_qgroup_alloc_comparer_set(void)
{
struct btrfs_qgroup_comparer_set *set;
int size;
size = sizeof(struct btrfs_qgroup_comparer_set) +
BTRFS_QGROUP_NCOMPS_INCREASE *
sizeof(struct btrfs_qgroup_comparer);
set = calloc(1, size);
if (!set) {
error("memory allocation failed");
exit(1);
}
set->total = BTRFS_QGROUP_NCOMPS_INCREASE;
return set;
}
int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set **comp_set,
enum btrfs_qgroup_comp_enum comparer,
int is_descending)
{
struct btrfs_qgroup_comparer_set *set = *comp_set;
int size;
ASSERT(set != NULL);
ASSERT(comparer < BTRFS_QGROUP_COMP_MAX);
ASSERT(set->ncomps <= set->total);
if (set->ncomps == set->total) {
void *tmp;
size = set->total + BTRFS_QGROUP_NCOMPS_INCREASE;
size = sizeof(*set) +
size * sizeof(struct btrfs_qgroup_comparer);
tmp = set;
set = realloc(set, size);
if (!set) {
free(tmp);
return -ENOMEM;
}
memset(&set->comps[set->total], 0,
BTRFS_QGROUP_NCOMPS_INCREASE *
sizeof(struct btrfs_qgroup_comparer));
set->total += BTRFS_QGROUP_NCOMPS_INCREASE;
*comp_set = set;
}
ASSERT(set->comps[set->ncomps].comp_func == NULL);
set->comps[set->ncomps].comp_func = all_comp_funcs[comparer];
set->comps[set->ncomps].is_descending = is_descending;
set->ncomps++;
return 0;
}
static int sort_comp(struct btrfs_qgroup *entry1, struct btrfs_qgroup *entry2,
struct btrfs_qgroup_comparer_set *set)
{
int qgroupid_compared = 0;
int i, ret = 0;
if (!set || !set->ncomps)
goto comp_qgroupid;
for (i = 0; i < set->ncomps; i++) {
if (!set->comps[i].comp_func)
break;
ret = set->comps[i].comp_func(entry1, entry2,
set->comps[i].is_descending);
if (ret)
return ret;
if (set->comps[i].comp_func == comp_entry_with_qgroupid)
qgroupid_compared = 1;
}
if (!qgroupid_compared) {
comp_qgroupid:
ret = comp_entry_with_qgroupid(entry1, entry2, 0);
}
return ret;
}
/*
* insert a new root into the tree. returns the existing root entry
* if one is already there. qgroupid is used
* as the key
*/
static int qgroup_tree_insert(struct qgroup_lookup *root_tree,
struct btrfs_qgroup *ins)
{
struct rb_node **p = &root_tree->root.rb_node;
struct rb_node *parent = NULL;
struct btrfs_qgroup *curr;
int ret;
while (*p) {
parent = *p;
curr = rb_entry(parent, struct btrfs_qgroup, rb_node);
ret = comp_entry_with_qgroupid(ins, curr, 0);
if (ret < 0)
p = &(*p)->rb_left;
else if (ret > 0)
p = &(*p)->rb_right;
else
return -EEXIST;
}
rb_link_node(&ins->rb_node, parent, p);
rb_insert_color(&ins->rb_node, &root_tree->root);
return 0;
}
/*
*find a given qgroupid in the tree. We return the smallest one,
*rb_next can be used to move forward looking for more if required
*/
static struct btrfs_qgroup *qgroup_tree_search(struct qgroup_lookup *root_tree,
u64 qgroupid)
{
struct rb_node *n = root_tree->root.rb_node;
struct btrfs_qgroup *entry;
struct btrfs_qgroup tmp;
int ret;
tmp.qgroupid = qgroupid;
while (n) {
entry = rb_entry(n, struct btrfs_qgroup, rb_node);
ret = comp_entry_with_qgroupid(&tmp, entry, 0);
if (ret < 0)
n = n->rb_left;
else if (ret > 0)
n = n->rb_right;
else
return entry;
}
return NULL;
}
/*
* Lookup or insert btrfs_qgroup into qgroup_lookup.
*
* Search a btrfs_qgroup with @qgroupid from the @qgroup_lookup. If not found,
* initialize a btrfs_qgroup with the given qgroupid and insert it to the
* @qgroup_lookup.
*
* Return the pointer to the btrfs_qgroup if found or if inserted successfully.
* Return ERR_PTR if any error occurred.
*/
static struct btrfs_qgroup *get_or_add_qgroup(
struct qgroup_lookup *qgroup_lookup, u64 qgroupid)
{
struct btrfs_qgroup *bq;
int ret;
bq = qgroup_tree_search(qgroup_lookup, qgroupid);
if (bq)
return bq;
bq = calloc(1, sizeof(*bq));
if (!bq) {
error("memory allocation failed");
return ERR_PTR(-ENOMEM);
}
bq->qgroupid = qgroupid;
INIT_LIST_HEAD(&bq->qgroups);
INIT_LIST_HEAD(&bq->members);
ret = qgroup_tree_insert(qgroup_lookup, bq);
if (ret) {
errno = -ret;
error("failed to insert %llu into tree: %m",
(unsigned long long)bq->qgroupid);
free(bq);
return ERR_PTR(ret);
}
return bq;
}
static int update_qgroup_info(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
struct btrfs_qgroup_info_item *info)
{
struct btrfs_qgroup *bq;
bq = get_or_add_qgroup(qgroup_lookup, qgroupid);
if (IS_ERR_OR_NULL(bq))
return PTR_ERR(bq);
bq->info.generation = btrfs_stack_qgroup_info_generation(info);
bq->info.referenced = btrfs_stack_qgroup_info_referenced(info);
bq->info.referenced_compressed =
btrfs_stack_qgroup_info_referenced_compressed(info);
bq->info.exclusive = btrfs_stack_qgroup_info_exclusive(info);
bq->info.exclusive_compressed =
btrfs_stack_qgroup_info_exclusive_compressed(info);
return 0;
}
static int update_qgroup_limit(struct qgroup_lookup *qgroup_lookup,
u64 qgroupid,
struct btrfs_qgroup_limit_item *limit)
{
struct btrfs_qgroup *bq;
bq = get_or_add_qgroup(qgroup_lookup, qgroupid);
if (IS_ERR_OR_NULL(bq))
return PTR_ERR(bq);
bq->limit.flags = btrfs_stack_qgroup_limit_flags(limit);
bq->limit.max_referenced =
btrfs_stack_qgroup_limit_max_referenced(limit);
bq->limit.max_exclusive =
btrfs_stack_qgroup_limit_max_exclusive(limit);
bq->limit.rsv_referenced =
btrfs_stack_qgroup_limit_rsv_referenced(limit);
bq->limit.rsv_exclusive = btrfs_stack_qgroup_limit_rsv_exclusive(limit);
return 0;
}
static int update_qgroup_relation(struct qgroup_lookup *qgroup_lookup,
u64 child_id, u64 parent_id)
{
struct btrfs_qgroup *child;
struct btrfs_qgroup *parent;
struct btrfs_qgroup_list *list;
child = qgroup_tree_search(qgroup_lookup, child_id);
if (!child) {
error("cannot find the qgroup %llu/%llu",
btrfs_qgroup_level(child_id),
btrfs_qgroup_subvid(child_id));
return -ENOENT;
}
parent = qgroup_tree_search(qgroup_lookup, parent_id);
if (!parent) {
error("cannot find the qgroup %llu/%llu",
btrfs_qgroup_level(parent_id),
btrfs_qgroup_subvid(parent_id));
return -ENOENT;
}
list = malloc(sizeof(*list));
if (!list) {
error("memory allocation failed");
return -ENOMEM;
}
list->qgroup = parent;
list->member = child;
list_add_tail(&list->next_qgroup, &child->qgroups);
list_add_tail(&list->next_member, &parent->members);
return 0;
}
static void __free_btrfs_qgroup(struct btrfs_qgroup *bq)
{
struct btrfs_qgroup_list *list;
while (!list_empty(&bq->qgroups)) {
list = list_entry((&bq->qgroups)->next,
struct btrfs_qgroup_list,
next_qgroup);
list_del(&list->next_qgroup);
list_del(&list->next_member);
free(list);
}
while (!list_empty(&bq->members)) {
list = list_entry((&bq->members)->next,
struct btrfs_qgroup_list,
next_member);
list_del(&list->next_qgroup);
list_del(&list->next_member);
free(list);
}
free(bq);
}
static void __free_all_qgroups(struct qgroup_lookup *root_tree)
{
struct btrfs_qgroup *entry;
struct rb_node *n;
n = rb_first(&root_tree->root);
while (n) {
entry = rb_entry(n, struct btrfs_qgroup, rb_node);
rb_erase(n, &root_tree->root);
__free_btrfs_qgroup(entry);
n = rb_first(&root_tree->root);
}
}
static int filter_all_parent_insert(struct qgroup_lookup *sort_tree,
struct btrfs_qgroup *bq)
{
struct rb_node **p = &sort_tree->root.rb_node;
struct rb_node *parent = NULL;
struct btrfs_qgroup *curr;
int ret;
while (*p) {
parent = *p;
curr = rb_entry(parent, struct btrfs_qgroup, all_parent_node);
ret = comp_entry_with_qgroupid(bq, curr, 0);
if (ret < 0)
p = &(*p)->rb_left;
else if (ret > 0)
p = &(*p)->rb_right;
else
return -EEXIST;
}
rb_link_node(&bq->all_parent_node, parent, p);
rb_insert_color(&bq->all_parent_node, &sort_tree->root);
return 0;
}
static int filter_by_parent(struct btrfs_qgroup *bq, u64 data)
{
struct btrfs_qgroup *qgroup =
(struct btrfs_qgroup *)(unsigned long)data;
if (data == 0)
return 0;
if (qgroup->qgroupid == bq->qgroupid)
return 1;
return 0;
}
static int filter_by_all_parent(struct btrfs_qgroup *bq, u64 data)
{
struct qgroup_lookup lookup;
struct qgroup_lookup *ql = &lookup;
struct btrfs_qgroup_list *list;
struct rb_node *n;
struct btrfs_qgroup *qgroup =
(struct btrfs_qgroup *)(unsigned long)data;
if (data == 0)
return 0;
if (bq->qgroupid == qgroup->qgroupid)
return 1;
qgroup_lookup_init(ql);
filter_all_parent_insert(ql, qgroup);
n = rb_first(&ql->root);
while (n) {
qgroup = rb_entry(n, struct btrfs_qgroup, all_parent_node);
if (!list_empty(&qgroup->qgroups)) {
list_for_each_entry(list, &qgroup->qgroups,
next_qgroup) {
if ((list->qgroup)->qgroupid == bq->qgroupid)
return 1;
filter_all_parent_insert(ql, list->qgroup);
}
}
rb_erase(n, &ql->root);
n = rb_first(&ql->root);
}
return 0;
}
static btrfs_qgroup_filter_func all_filter_funcs[] = {
[BTRFS_QGROUP_FILTER_PARENT] = filter_by_parent,
[BTRFS_QGROUP_FILTER_ALL_PARENT] = filter_by_all_parent,
};
struct btrfs_qgroup_filter_set *btrfs_qgroup_alloc_filter_set(void)
{
struct btrfs_qgroup_filter_set *set;
int size;
size = sizeof(struct btrfs_qgroup_filter_set) +
BTRFS_QGROUP_NFILTERS_INCREASE *
sizeof(struct btrfs_qgroup_filter);
set = calloc(1, size);
if (!set) {
error("memory allocation failed");
exit(1);
}
set->total = BTRFS_QGROUP_NFILTERS_INCREASE;
return set;
}
int btrfs_qgroup_setup_filter(struct btrfs_qgroup_filter_set **filter_set,
enum btrfs_qgroup_filter_enum filter, u64 data)
{
struct btrfs_qgroup_filter_set *set = *filter_set;
int size;
ASSERT(set != NULL);
ASSERT(filter < BTRFS_QGROUP_FILTER_MAX);
ASSERT(set->nfilters <= set->total);
if (set->nfilters == set->total) {
void *tmp;
size = set->total + BTRFS_QGROUP_NFILTERS_INCREASE;
size = sizeof(*set) + size * sizeof(struct btrfs_qgroup_filter);
tmp = set;
set = realloc(set, size);
if (!set) {
error("memory allocation failed");
free(tmp);
exit(1);
}
memset(&set->filters[set->total], 0,
BTRFS_QGROUP_NFILTERS_INCREASE *
sizeof(struct btrfs_qgroup_filter));
set->total += BTRFS_QGROUP_NFILTERS_INCREASE;
*filter_set = set;
}
ASSERT(set->filters[set->nfilters].filter_func == NULL);
set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
set->filters[set->nfilters].data = data;
set->nfilters++;
return 0;
}
static int filter_qgroup(struct btrfs_qgroup *bq,
struct btrfs_qgroup_filter_set *set)
{
int i, ret;
if (!set || !set->nfilters)
return 1;
for (i = 0; i < set->nfilters; i++) {
if (!set->filters[i].filter_func)
break;
ret = set->filters[i].filter_func(bq, set->filters[i].data);
if (!ret)
return 0;
}
return 1;
}
static void pre_process_filter_set(struct qgroup_lookup *lookup,
struct btrfs_qgroup_filter_set *set)
{
int i;
struct btrfs_qgroup *qgroup_for_filter = NULL;
for (i = 0; i < set->nfilters; i++) {
if (set->filters[i].filter_func == filter_by_all_parent
|| set->filters[i].filter_func == filter_by_parent) {
qgroup_for_filter = qgroup_tree_search(lookup,
set->filters[i].data);
set->filters[i].data =
(u64)(unsigned long)qgroup_for_filter;
}
}
}
static int sort_tree_insert(struct qgroup_lookup *sort_tree,
struct btrfs_qgroup *bq,
struct btrfs_qgroup_comparer_set *comp_set)
{
struct rb_node **p = &sort_tree->root.rb_node;
struct rb_node *parent = NULL;
struct btrfs_qgroup *curr;
int ret;
while (*p) {
parent = *p;
curr = rb_entry(parent, struct btrfs_qgroup, sort_node);
ret = sort_comp(bq, curr, comp_set);
if (ret < 0)
p = &(*p)->rb_left;
else if (ret > 0)
p = &(*p)->rb_right;
else
return -EEXIST;
}
rb_link_node(&bq->sort_node, parent, p);
rb_insert_color(&bq->sort_node, &sort_tree->root);
return 0;
}
static void __update_columns_max_len(struct btrfs_qgroup *bq,
enum btrfs_qgroup_column_enum column)
{
struct btrfs_qgroup_list *list = NULL;
char tmp[100];
int len;
unsigned unit_mode = btrfs_qgroup_columns[column].unit_mode;
ASSERT(0 <= column && column < BTRFS_QGROUP_ALL);
switch (column) {
case BTRFS_QGROUP_QGROUPID:
sprintf(tmp, "%llu/%llu",
btrfs_qgroup_level(bq->qgroupid),
btrfs_qgroup_subvid(bq->qgroupid));
len = strlen(tmp);
if (btrfs_qgroup_columns[column].max_len < len)
btrfs_qgroup_columns[column].max_len = len;
break;
case BTRFS_QGROUP_RFER:
len = strlen(pretty_size_mode(bq->info.referenced, unit_mode));
if (btrfs_qgroup_columns[column].max_len < len)
btrfs_qgroup_columns[column].max_len = len;
break;
case BTRFS_QGROUP_EXCL:
len = strlen(pretty_size_mode(bq->info.exclusive, unit_mode));
if (btrfs_qgroup_columns[column].max_len < len)
btrfs_qgroup_columns[column].max_len = len;
break;
case BTRFS_QGROUP_MAX_RFER:
len = strlen(pretty_size_mode(bq->limit.max_referenced,
unit_mode));
if (btrfs_qgroup_columns[column].max_len < len)
btrfs_qgroup_columns[column].max_len = len;
break;
case BTRFS_QGROUP_MAX_EXCL:
len = strlen(pretty_size_mode(bq->limit.max_exclusive,
unit_mode));
if (btrfs_qgroup_columns[column].max_len < len)
btrfs_qgroup_columns[column].max_len = len;
break;
case BTRFS_QGROUP_PARENT:
len = 0;
list_for_each_entry(list, &bq->qgroups, next_qgroup) {
len += sprintf(tmp, "%llu/%llu",
btrfs_qgroup_level(list->qgroup->qgroupid),
btrfs_qgroup_subvid(list->qgroup->qgroupid));
if (!list_is_last(&list->next_qgroup, &bq->qgroups))
len += 1;
}
if (btrfs_qgroup_columns[column].max_len < len)
btrfs_qgroup_columns[column].max_len = len;
break;
case BTRFS_QGROUP_CHILD:
len = 0;
list_for_each_entry(list, &bq->members, next_member) {
len += sprintf(tmp, "%llu/%llu",
btrfs_qgroup_level(list->member->qgroupid),
btrfs_qgroup_subvid(list->member->qgroupid));
if (!list_is_last(&list->next_member, &bq->members))
len += 1;
}
if (btrfs_qgroup_columns[column].max_len < len)
btrfs_qgroup_columns[column].max_len = len;
break;
default:
break;
}
}