+static int comp_entry_with_rfer(struct btrfs_qgroup *entry1,
+ struct btrfs_qgroup *entry2,
+ int is_descending)
+{
+ int ret;
+
+ if (entry1->rfer > entry2->rfer)
+ ret = 1;
+ else if (entry1->rfer < entry2->rfer)
+ 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->excl > entry2->excl)
+ ret = 1;
+ else if (entry1->excl < entry2->excl)
+ 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->max_rfer > entry2->max_rfer)
+ ret = 1;
+ else if (entry1->max_rfer < entry2->max_rfer)
+ 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->max_excl > entry2->max_excl)
+ ret = 1;
+ else if (entry1->max_excl < entry2->max_excl)
+ 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) {
+ error("memory allocation failed");
+ free(tmp);
+ exit(1);
+ }
+
+ 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;
+}
+