+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->generation = btrfs_stack_qgroup_info_generation(info);
+ bq->rfer = btrfs_stack_qgroup_info_referenced(info);
+ bq->rfer_cmpr = btrfs_stack_qgroup_info_referenced_compressed(info);
+ bq->excl = btrfs_stack_qgroup_info_exclusive(info);
+ bq->excl_cmpr = 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->flags = btrfs_stack_qgroup_limit_flags(limit);
+ bq->max_rfer = btrfs_stack_qgroup_limit_max_referenced(limit);
+ bq->max_excl = btrfs_stack_qgroup_limit_max_exclusive(limit);
+ bq->rsv_rfer = btrfs_stack_qgroup_limit_rsv_referenced(limit);
+ bq->rsv_excl = 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->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");
+}
+
+static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
+{
+ int ret;
+ struct btrfs_ioctl_search_args args;
+ struct btrfs_ioctl_search_key *sk = &args.key;
+ struct btrfs_ioctl_search_header *sh;
+ unsigned long off = 0;
+ unsigned int i;
+ struct btrfs_qgroup_status_item *si;
+ struct btrfs_qgroup_info_item *info;
+ struct btrfs_qgroup_limit_item *limit;
+ u64 flags;
+ u64 qgroupid;
+ u64 qgroupid1;
+
+ memset(&args, 0, sizeof(args));
+
+ sk->tree_id = BTRFS_QUOTA_TREE_OBJECTID;
+ sk->max_type = BTRFS_QGROUP_RELATION_KEY;
+ sk->min_type = BTRFS_QGROUP_STATUS_KEY;
+ sk->max_objectid = (u64)-1;
+ sk->max_offset = (u64)-1;
+ sk->max_transid = (u64)-1;
+ sk->nr_items = 4096;
+
+ qgroup_lookup_init(qgroup_lookup);
+
+ while (1) {
+ ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
+ if (ret < 0) {
+ if (errno == ENOENT) {
+ error("can't list qgroups: quotas not enabled");
+ ret = -ENOTTY;
+ } else {
+ error("can't list qgroups: %s",
+ strerror(errno));
+ ret = -errno;
+ }
+
+ break;
+ }
+
+ /* the ioctl returns the number of item it found in nr_items */
+ if (sk->nr_items == 0)
+ break;
+
+ off = 0;
+ /*
+ * for each item, pull the key out of the header and then
+ * read the root_ref item it contains
+ */
+ for (i = 0; i < sk->nr_items; i++) {
+ sh = (struct btrfs_ioctl_search_header *)(args.buf +
+ off);
+ off += sizeof(*sh);
+
+ switch (btrfs_search_header_type(sh)) {
+ case BTRFS_QGROUP_STATUS_KEY:
+ si = (struct btrfs_qgroup_status_item *)
+ (args.buf + off);
+ flags = btrfs_stack_qgroup_status_flags(si);
+
+ print_status_flag_warning(flags);
+ break;
+ case BTRFS_QGROUP_INFO_KEY:
+ qgroupid = btrfs_search_header_offset(sh);
+ info = (struct btrfs_qgroup_info_item *)
+ (args.buf + off);
+
+ ret = update_qgroup_info(qgroup_lookup,
+ qgroupid, info);
+ break;
+ case BTRFS_QGROUP_LIMIT_KEY:
+ qgroupid = btrfs_search_header_offset(sh);
+ limit = (struct btrfs_qgroup_limit_item *)
+ (args.buf + off);
+
+ ret = update_qgroup_limit(qgroup_lookup,
+ qgroupid, limit);
+ break;
+ case BTRFS_QGROUP_RELATION_KEY:
+ qgroupid = btrfs_search_header_offset(sh);
+ qgroupid1 = btrfs_search_header_objectid(sh);
+
+ if (qgroupid < qgroupid1)
+ break;
+
+ ret = update_qgroup_relation(qgroup_lookup,
+ qgroupid, qgroupid1);
+ break;
+ default:
+ return ret;
+ }
+
+ if (ret)
+ return ret;
+
+ off += btrfs_search_header_len(sh);
+
+ /*
+ * record the mins in sk so we can make sure the
+ * next search doesn't repeat this root
+ */
+ sk->min_type = btrfs_search_header_type(sh);
+ sk->min_offset = btrfs_search_header_offset(sh);
+ sk->min_objectid = btrfs_search_header_objectid(sh);
+ }
+ sk->nr_items = 4096;
+ /*
+ * this iteration is done, step forward one qgroup for the next
+ * ioctl
+ */
+ if (sk->min_offset < (u64)-1)
+ sk->min_offset++;
+ else
+ break;
+ }
+
+ return ret;
+}
+
+static void print_all_qgroups(struct qgroup_lookup *qgroup_lookup)
+{
+
+ struct rb_node *n;
+ struct btrfs_qgroup *entry;
+
+ print_table_head();
+
+ n = rb_first(&qgroup_lookup->root);
+ while (n) {
+ entry = rb_entry(n, struct btrfs_qgroup, sort_node);
+ print_single_qgroup_table(entry);
+ n = rb_next(n);
+ }
+}
+
+int btrfs_show_qgroups(int fd,
+ struct btrfs_qgroup_filter_set *filter_set,
+ struct btrfs_qgroup_comparer_set *comp_set)
+{
+
+ struct qgroup_lookup qgroup_lookup;
+ struct qgroup_lookup sort_tree;
+ int ret;
+
+ ret = __qgroups_search(fd, &qgroup_lookup);
+ if (ret)
+ return ret;
+ __filter_and_sort_qgroups(&qgroup_lookup, &sort_tree,
+ filter_set, comp_set);
+ print_all_qgroups(&sort_tree);
+
+ __free_all_qgroups(&qgroup_lookup);
+ return ret;
+}
+
+int btrfs_qgroup_parse_sort_string(const char *opt_arg,
+ struct btrfs_qgroup_comparer_set **comps)
+{
+ int order;
+ int flag;
+ char *p;
+ char **ptr_argv;
+ int what_to_sort;
+ char *opt_tmp;
+ int ret = 0;
+
+ opt_tmp = strdup(opt_arg);
+ if (!opt_tmp)
+ return -ENOMEM;
+
+ p = strtok(opt_tmp, ",");
+ while (p) {
+ flag = 0;
+ ptr_argv = all_sort_items;
+
+ while (*ptr_argv) {
+ if (strcmp(*ptr_argv, p) == 0) {
+ flag = 1;
+ break;
+ } else {
+ p++;
+ if (strcmp(*ptr_argv, p) == 0) {
+ flag = 1;
+ p--;
+ break;
+ }
+ p--;
+ }
+ ptr_argv++;
+ }
+
+ if (flag == 0) {
+ ret = -1;
+ goto out;
+ } else {
+ if (*p == '+') {
+ order = 0;
+ p++;
+ } else if (*p == '-') {
+ order = 1;
+ p++;
+ } else
+ order = 0;
+
+ what_to_sort = btrfs_qgroup_get_sort_item(p);
+ if (what_to_sort < 0) {
+ ret = -1;
+ goto out;
+ }
+ btrfs_qgroup_setup_comparer(comps, what_to_sort, order);
+ }
+ p = strtok(NULL, ",");
+ }