btrfs-progs: image: pass rb_root to find_collisions
[platform/upstream/btrfs-progs.git] / qgroup.c
index a672ac0..fffdbb1 100644 (file)
--- a/qgroup.c
+++ b/qgroup.c
@@ -148,7 +148,7 @@ void btrfs_qgroup_setup_print_column(enum btrfs_qgroup_column_enum column)
 {
        int i;
 
-       BUG_ON(column < 0 || column > BTRFS_QGROUP_ALL);
+       ASSERT(0 <= column && column <= BTRFS_QGROUP_ALL);
 
        if (column < BTRFS_QGROUP_ALL) {
                btrfs_qgroup_columns[column].need_print = 1;
@@ -213,11 +213,12 @@ static void print_qgroup_column_add_blank(enum btrfs_qgroup_column_enum column,
 static void print_qgroup_column(struct btrfs_qgroup *qgroup,
                                enum btrfs_qgroup_column_enum column)
 {
-       BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
        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:
@@ -438,7 +439,7 @@ struct btrfs_qgroup_comparer_set *btrfs_qgroup_alloc_comparer_set(void)
               sizeof(struct btrfs_qgroup_comparer);
        set = calloc(1, size);
        if (!set) {
-               fprintf(stderr, "memory allocation failed\n");
+               error("memory allocation failed");
                exit(1);
        }
 
@@ -447,11 +448,6 @@ struct btrfs_qgroup_comparer_set *btrfs_qgroup_alloc_comparer_set(void)
        return set;
 }
 
-void btrfs_qgroup_free_comparer_set(struct btrfs_qgroup_comparer_set *comp_set)
-{
-       free(comp_set);
-}
-
 int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set  **comp_set,
                                enum btrfs_qgroup_comp_enum comparer,
                                int is_descending)
@@ -459,9 +455,9 @@ int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set  **comp_set,
        struct btrfs_qgroup_comparer_set *set = *comp_set;
        int size;
 
-       BUG_ON(!set);
-       BUG_ON(comparer >= BTRFS_QGROUP_COMP_MAX);
-       BUG_ON(set->ncomps > set->total);
+       ASSERT(set != NULL);
+       ASSERT(comparer < BTRFS_QGROUP_COMP_MAX);
+       ASSERT(set->ncomps <= set->total);
 
        if (set->ncomps == set->total) {
                void *tmp;
@@ -472,7 +468,7 @@ int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set  **comp_set,
                tmp = set;
                set = realloc(set, size);
                if (!set) {
-                       fprintf(stderr, "memory allocation failed\n");
+                       error("memory allocation failed");
                        free(tmp);
                        exit(1);
                }
@@ -484,7 +480,7 @@ int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set  **comp_set,
                *comp_set = set;
        }
 
-       BUG_ON(set->comps[set->ncomps].comp_func);
+       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;
@@ -616,7 +612,7 @@ static int update_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
        if (pa && child) {
                list = malloc(sizeof(*list));
                if (!list) {
-                       fprintf(stderr, "memory allocation failed\n");
+                       error("memory allocation failed");
                        exit(1);
                }
                list->qgroup = pa;
@@ -645,7 +641,7 @@ static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
 
        bq = calloc(1, sizeof(*bq));
        if (!bq) {
-               printf("memory allocation failed\n");
+               error("memory allocation failed");
                exit(1);
        }
        if (qgroupid) {
@@ -674,7 +670,7 @@ static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
        if (parent && child) {
                list = malloc(sizeof(*list));
                if (!list) {
-                       fprintf(stderr, "memory allocation failed\n");
+                       error("memory allocation failed");
                        exit(1);
                }
                list->qgroup = parent;
@@ -684,8 +680,8 @@ static int add_qgroup(struct qgroup_lookup *qgroup_lookup, u64 qgroupid,
        }
        ret = qgroup_tree_insert(qgroup_lookup, bq);
        if (ret) {
-               printf("failed to insert tree %llu\n",
-                      bq->qgroupid);
+               error("failed to insert %llu into tree: %s",
+                      (unsigned long long)bq->qgroupid, strerror(-ret));
                exit(1);
        }
        return ret;
@@ -813,7 +809,7 @@ struct btrfs_qgroup_filter_set *btrfs_qgroup_alloc_filter_set(void)
               sizeof(struct btrfs_qgroup_filter);
        set = calloc(1, size);
        if (!set) {
-               fprintf(stderr, "memory allocation failed\n");
+               error("memory allocation failed");
                exit(1);
        }
        set->total = BTRFS_QGROUP_NFILTERS_INCREASE;
@@ -821,20 +817,15 @@ struct btrfs_qgroup_filter_set *btrfs_qgroup_alloc_filter_set(void)
        return set;
 }
 
-void btrfs_qgroup_free_filter_set(struct btrfs_qgroup_filter_set *filter_set)
-{
-       free(filter_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;
 
-       BUG_ON(!set);
-       BUG_ON(filter >= BTRFS_QGROUP_FILTER_MAX);
-       BUG_ON(set->nfilters > set->total);
+       ASSERT(set != NULL);
+       ASSERT(filter < BTRFS_QGROUP_FILTER_MAX);
+       ASSERT(set->nfilters <= set->total);
 
        if (set->nfilters == set->total) {
                void *tmp;
@@ -845,7 +836,7 @@ int btrfs_qgroup_setup_filter(struct btrfs_qgroup_filter_set **filter_set,
                tmp = set;
                set = realloc(set, size);
                if (!set) {
-                       fprintf(stderr, "memory allocation failed\n");
+                       error("memory allocation failed");
                        free(tmp);
                        exit(1);
                }
@@ -855,7 +846,8 @@ int btrfs_qgroup_setup_filter(struct btrfs_qgroup_filter_set **filter_set,
                set->total += BTRFS_QGROUP_NFILTERS_INCREASE;
                *filter_set = set;
        }
-       BUG_ON(set->filters[set->nfilters].filter_func);
+
+       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++;
@@ -926,12 +918,13 @@ static int sort_tree_insert(struct qgroup_lookup *sort_tree,
 static void __update_columns_max_len(struct btrfs_qgroup *bq,
                                     enum btrfs_qgroup_column_enum column)
 {
-       BUG_ON(column >= BTRFS_QGROUP_ALL || column < 0);
        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:
@@ -1032,14 +1025,11 @@ static void __filter_and_sort_qgroups(struct qgroup_lookup *all_qgroups,
 static inline void print_status_flag_warning(u64 flags)
 {
        if (!(flags & BTRFS_QGROUP_STATUS_FLAG_ON))
-               fprintf(stderr,
-               "WARNING: Quota disabled, qgroup data may be out of date\n");
+               warning("quota disabled, qgroup data may be out of date");
        else if (flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
-               fprintf(stderr,
-               "WARNING: Rescan is running, qgroup data may be incorrect\n");
+               warning("rescan is running, qgroup data may be incorrect");
        else if (flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)
-               fprintf(stderr,
-               "WARNING: Qgroup data inconsistent, rescan recommended\n");
+               warning("qgroup data inconsistent, rescan recommended");
 }
 
 static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
@@ -1074,12 +1064,9 @@ static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
 
        while (1) {
                ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
-               if (ret < 0) {
-                       fprintf(stderr,
-                               "ERROR: can't perform the search - %s\n",
-                               strerror(errno));
-                       return ret;
-               }
+               if (ret < 0)
+                       return -errno;
+
                /* the ioctl returns the number of item it found in nr_items */
                if (sk->nr_items == 0)
                        break;
@@ -1094,7 +1081,8 @@ static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
                                                                  off);
                        off += sizeof(*sh);
 
-                       if (sh->type == BTRFS_QGROUP_STATUS_KEY) {
+                       if (btrfs_search_header_type(sh)
+                           == BTRFS_QGROUP_STATUS_KEY) {
                                struct btrfs_qgroup_status_item *si;
                                u64 flags;
 
@@ -1102,7 +1090,8 @@ static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
                                     (args.buf + off);
                                flags = btrfs_stack_qgroup_status_flags(si);
                                print_status_flag_warning(flags);
-                       } else if (sh->type == BTRFS_QGROUP_INFO_KEY) {
+                       } else if (btrfs_search_header_type(sh)
+                                  == BTRFS_QGROUP_INFO_KEY) {
                                info = (struct btrfs_qgroup_info_item *)
                                       (args.buf + off);
                                a1 = btrfs_stack_qgroup_info_generation(info);
@@ -1114,10 +1103,12 @@ static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
                                a5 =
                                  btrfs_stack_qgroup_info_exclusive_compressed
                                  (info);
-                               add_qgroup(qgroup_lookup, sh->offset, a1, a2,
-                                          a3, a4, a5, 0, 0, 0, 0, 0,
-                                          NULL, NULL);
-                       } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
+                               add_qgroup(qgroup_lookup,
+                                       btrfs_search_header_offset(sh), a1,
+                                       a2, a3, a4, a5, 0, 0, 0, 0, 0, NULL,
+                                       NULL);
+                       } else if (btrfs_search_header_type(sh)
+                                  == BTRFS_QGROUP_LIMIT_KEY) {
                                limit = (struct btrfs_qgroup_limit_item *)
                                    (args.buf + off);
 
@@ -1130,34 +1121,38 @@ static int __qgroups_search(int fd, struct qgroup_lookup *qgroup_lookup)
                                     (limit);
                                a5 = btrfs_stack_qgroup_limit_rsv_exclusive
                                     (limit);
-                               add_qgroup(qgroup_lookup, sh->offset, 0, 0,
-                                          0, 0, 0, a1, a2, a3, a4, a5,
+                               add_qgroup(qgroup_lookup,
+                                          btrfs_search_header_offset(sh), 0,
+                                          0, 0, 0, 0, a1, a2, a3, a4, a5,
                                           NULL, NULL);
-                       } else if (sh->type == BTRFS_QGROUP_RELATION_KEY) {
-                               if (sh->offset < sh->objectid)
+                       } else if (btrfs_search_header_type(sh)
+                                  == BTRFS_QGROUP_RELATION_KEY) {
+                               if (btrfs_search_header_offset(sh)
+                                   < btrfs_search_header_objectid(sh))
                                        goto skip;
                                bq = qgroup_tree_search(qgroup_lookup,
-                                                       sh->offset);
+                                       btrfs_search_header_offset(sh));
                                if (!bq)
                                        goto skip;
                                bq1 = qgroup_tree_search(qgroup_lookup,
-                                                        sh->objectid);
+                                        btrfs_search_header_objectid(sh));
                                if (!bq1)
                                        goto skip;
-                               add_qgroup(qgroup_lookup, sh->offset, 0, 0,
-                                          0, 0, 0, 0, 0, 0, 0, 0, bq, bq1);
+                               add_qgroup(qgroup_lookup,
+                                          btrfs_search_header_offset(sh), 0,
+                                          0, 0, 0, 0, 0, 0, 0, 0, 0, bq, bq1);
                        } else
                                goto done;
 skip:
-                       off += sh->len;
+                       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 = sh->type;
-                       sk->min_offset = sh->offset;
-                       sk->min_objectid = sh->objectid;
+                       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;
                /*
@@ -1207,30 +1202,12 @@ int btrfs_show_qgroups(int fd,
        print_all_qgroups(&sort_tree);
 
        __free_all_qgroups(&qgroup_lookup);
-       btrfs_qgroup_free_filter_set(filter_set);
-       btrfs_qgroup_free_comparer_set(comp_set);
+       free(filter_set);
+       free(comp_set);
        return ret;
 }
 
-u64 btrfs_get_path_rootid(int fd)
-{
-       int  ret;
-       struct btrfs_ioctl_ino_lookup_args args;
-
-       memset(&args, 0, sizeof(args));
-       args.objectid = BTRFS_FIRST_FREE_OBJECTID;
-
-       ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
-       if (ret < 0) {
-               fprintf(stderr,
-                       "ERROR: can't perform the search - %s\n",
-                       strerror(errno));
-               return ret;
-       }
-       return args.treeid;
-}
-
-int btrfs_qgroup_parse_sort_string(char *opt_arg,
+int btrfs_qgroup_parse_sort_string(const char *opt_arg,
                                   struct btrfs_qgroup_comparer_set **comps)
 {
        int order;
@@ -1238,8 +1215,14 @@ int btrfs_qgroup_parse_sort_string(char *opt_arg,
        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;
 
-       while ((p = strtok(opt_arg, ",")) != NULL) {
+       while ((p = strtok(opt_tmp, ",")) != NULL) {
                flag = 0;
                ptr_argv = all_sort_items;
 
@@ -1259,10 +1242,10 @@ int btrfs_qgroup_parse_sort_string(char *opt_arg,
                        ptr_argv++;
                }
 
-               if (flag == 0)
-                       return -1;
-
-               else {
+               if (flag == 0) {
+                       ret = -1;
+                       goto out;
+               else {
                        if (*p == '+') {
                                order = 0;
                                p++;
@@ -1273,14 +1256,19 @@ int btrfs_qgroup_parse_sort_string(char *opt_arg,
                                order = 0;
 
                        what_to_sort = btrfs_qgroup_get_sort_item(p);
-                       if (what_to_sort < 0)
-                               return -1;
+                       if (what_to_sort < 0) {
+                               ret = -1;
+                               goto out;
+                       }
                        btrfs_qgroup_setup_comparer(comps, what_to_sort, order);
                }
-               opt_arg = NULL;
+               free(opt_tmp);
+               opt_tmp = NULL;
        }
 
-       return 0;
+out:
+       free(opt_tmp);
+       return ret;
 }
 
 int qgroup_inherit_size(struct btrfs_qgroup_inherit *p)
@@ -1304,7 +1292,7 @@ qgroup_inherit_realloc(struct btrfs_qgroup_inherit **inherit, int n, int pos)
 
        out = calloc(sizeof(*out) + sizeof(out->qgroups[0]) * (nitems + n), 1);
        if (out == NULL) {
-               fprintf(stderr, "ERROR: Not enough memory\n");
+               error("not enough memory");
                return -ENOMEM;
        }
 
@@ -1332,7 +1320,7 @@ int qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg)
        int pos = 0;
 
        if (qgroupid == 0) {
-               fprintf(stderr, "ERROR: bad qgroup specification\n");
+               error("invalid qgroup specification, qgroupid must not 0");
                return -EINVAL;
        }
 
@@ -1359,7 +1347,7 @@ int qgroup_inherit_add_copy(struct btrfs_qgroup_inherit **inherit, char *arg,
        p = strchr(arg, ':');
        if (!p) {
 bad:
-               fprintf(stderr, "ERROR: bad copy specification\n");
+               error("invalid copy specification, missing separator :");
                return -EINVAL;
        }
        *p = 0;