+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->rfer, 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->excl, 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->max_rfer, 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->max_excl, 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;
+ }
+
+}
+
+static void update_columns_max_len(struct btrfs_qgroup *bq)
+{
+ int i;
+
+ for (i = 0; i < BTRFS_QGROUP_ALL; i++) {
+ if (!btrfs_qgroup_columns[i].need_print)
+ continue;
+ __update_columns_max_len(bq, i);
+ }
+}
+
+static void __filter_and_sort_qgroups(struct qgroup_lookup *all_qgroups,
+ struct qgroup_lookup *sort_tree,
+ struct btrfs_qgroup_filter_set *filter_set,
+ struct btrfs_qgroup_comparer_set *comp_set)
+{
+ struct rb_node *n;
+ struct btrfs_qgroup *entry;
+ int ret;
+
+ qgroup_lookup_init(sort_tree);
+ pre_process_filter_set(all_qgroups, filter_set);
+
+ n = rb_last(&all_qgroups->root);
+ while (n) {
+ entry = rb_entry(n, struct btrfs_qgroup, rb_node);
+
+ ret = filter_qgroup(entry, filter_set);
+ if (ret) {
+ sort_tree_insert(sort_tree, entry, comp_set);
+
+ update_columns_max_len(entry);
+ }
+ n = rb_prev(n);
+ }
+}
+
+static inline void print_status_flag_warning(u64 flags)
+{
+ if (!(flags & BTRFS_QGROUP_STATUS_FLAG_ON))
+ warning("quota disabled, qgroup data may be out of date");
+ else if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
+ warning("rescan is running, qgroup data may be incorrect");
+ else if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
+ warning("qgroup data inconsistent, rescan recommended");
+}
+