btrfs-progs: Add further checks to btrfs replace start command
[platform/upstream/btrfs-progs.git] / btrfs-list.c
index 4e66654..875a89d 100644 (file)
  * Boston, MA 021110-1307, USA.
  */
 
-#define _GNU_SOURCE
-#ifndef __CHECKER__
 #include <sys/ioctl.h>
 #include <sys/mount.h>
-#include "ioctl.h"
-#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include "ctree.h"
 #include "transaction.h"
 #include "utils.h"
+#include "ioctl.h"
 #include <uuid/uuid.h>
 #include "btrfs-list.h"
+#include "rbtree-utils.h"
 
 #define BTRFS_LIST_NFILTERS_INCREASE   (2 * BTRFS_LIST_FILTER_MAX)
 #define BTRFS_LIST_NCOMPS_INCREASE     (2 * BTRFS_LIST_COMP_MAX)
@@ -46,54 +44,7 @@ struct root_lookup {
        struct rb_root root;
 };
 
-/*
- * one of these for each root we find.
- */
-struct root_info {
-       struct rb_node rb_node;
-       struct rb_node sort_node;
-
-       /* this root's id */
-       u64 root_id;
-
-       /* equal the offset of the root's key */
-       u64 root_offset;
-
-       /* flags of the root */
-       u64 flags;
-
-       /* the id of the root that references this one */
-       u64 ref_tree;
-
-       /* the dir id we're in from ref_tree */
-       u64 dir_id;
-
-       u64 top_id;
-
-       /* generation when the root is created or last updated */
-       u64 gen;
-
-       /* creation generation of this root in sec*/
-       u64 ogen;
-
-       /* creation time of this root in sec*/
-       time_t otime;
-
-       u8 uuid[BTRFS_UUID_SIZE];
-       u8 puuid[BTRFS_UUID_SIZE];
-
-       /* path from the subvol we live in to this root, including the
-        * root's name.  This is null until we do the extra lookup ioctl.
-        */
-       char *path;
-
-       /* the name of this root in the directory it lives in */
-       char *name;
-
-       char *full_path;
-};
-
-struct {
+static struct {
        char    *name;
        char    *column_name;
        int     need_print;
@@ -101,12 +52,12 @@ struct {
        {
                .name           = "ID",
                .column_name    = "ID",
-               .need_print     = 1,
+               .need_print     = 0,
        },
        {
                .name           = "gen",
                .column_name    = "Gen",
-               .need_print     = 1,
+               .need_print     = 0,
        },
        {
                .name           = "cgen",
@@ -121,7 +72,7 @@ struct {
        {
                .name           = "top level",
                .column_name    = "Top Level",
-               .need_print     = 1,
+               .need_print     = 0,
        },
        {
                .name           = "otime",
@@ -134,6 +85,11 @@ struct {
                .need_print     = 0,
        },
        {
+               .name           = "received_uuid",
+               .column_name    = "Received UUID",
+               .need_print     = 0,
+       },
+       {
                .name           = "uuid",
                .column_name    = "UUID",
                .need_print     = 0,
@@ -141,7 +97,7 @@ struct {
        {
                .name           = "path",
                .column_name    = "Path",
-               .need_print     = 1,
+               .need_print     = 0,
        },
        {
                .name           = NULL,
@@ -287,9 +243,8 @@ void btrfs_list_free_comparer_set(struct btrfs_list_comparer_set *comp_set)
        free(comp_set);
 }
 
-int btrfs_list_setup_comparer(struct btrfs_list_comparer_set  **comp_set,
-                             enum btrfs_list_comp_enum comparer,
-                             int is_descending)
+static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
+               enum btrfs_list_comp_enum comparer, int is_descending)
 {
        struct btrfs_list_comparer_set *set = *comp_set;
        int size;
@@ -441,7 +396,7 @@ static struct root_info *root_tree_search(struct root_lookup *root_tree,
 static int update_root(struct root_lookup *root_lookup,
                       u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
                       u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
-                      time_t ot, void *uuid, void *puuid)
+                      time_t ot, void *uuid, void *puuid, void *ruuid)
 {
        struct root_info *ri;
 
@@ -449,8 +404,7 @@ static int update_root(struct root_lookup *root_lookup,
        if (!ri || ri->root_id != root_id)
                return -ENOENT;
        if (name && name_len > 0) {
-               if (ri->name)
-                       free(ri->name);
+               free(ri->name);
 
                ri->name = malloc(name_len + 1);
                if (!ri->name) {
@@ -480,6 +434,8 @@ static int update_root(struct root_lookup *root_lookup,
                memcpy(&ri->uuid, uuid, BTRFS_UUID_SIZE);
        if (puuid)
                memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
+       if (ruuid)
+               memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
 
        return 0;
 }
@@ -498,17 +454,19 @@ static int update_root(struct root_lookup *root_lookup,
  * ot: the original time(create time) of the root
  * uuid: uuid of the root
  * puuid: uuid of the root parent if any
+ * ruuid: uuid of the received subvol, if any
  */
 static int add_root(struct root_lookup *root_lookup,
                    u64 root_id, u64 ref_tree, u64 root_offset, u64 flags,
                    u64 dir_id, char *name, int name_len, u64 ogen, u64 gen,
-                   time_t ot, void *uuid, void *puuid)
+                   time_t ot, void *uuid, void *puuid, void *ruuid)
 {
        struct root_info *ri;
        int ret;
 
        ret = update_root(root_lookup, root_id, ref_tree, root_offset, flags,
-                         dir_id, name, name_len, ogen, gen, ot, uuid, puuid);
+                         dir_id, name, name_len, ogen, gen, ot,
+                         uuid, puuid, ruuid);
        if (!ret)
                return 0;
 
@@ -552,6 +510,9 @@ static int add_root(struct root_lookup *root_lookup,
        if (puuid)
                memcpy(&ri->puuid, puuid, BTRFS_UUID_SIZE);
 
+       if (ruuid)
+               memcpy(&ri->ruuid, ruuid, BTRFS_UUID_SIZE);
+
        ret = root_tree_insert(root_lookup, ri);
        if (ret) {
                printf("failed to insert tree %llu\n", (unsigned long long)root_id);
@@ -560,33 +521,20 @@ static int add_root(struct root_lookup *root_lookup,
        return 0;
 }
 
-void __free_root_info(struct root_info *ri)
+static void __free_root_info(struct rb_node *node)
 {
-       if (ri->name)
-               free(ri->name);
-
-       if (ri->path)
-               free(ri->path);
-
-       if (ri->full_path)
-               free(ri->full_path);
+       struct root_info *ri;
 
+       ri = rb_entry(node, struct root_info, rb_node);
+       free(ri->name);
+       free(ri->path);
+       free(ri->full_path);
        free(ri);
 }
 
-void __free_all_subvolumn(struct root_lookup *root_tree)
+static inline void __free_all_subvolumn(struct root_lookup *root_tree)
 {
-       struct root_info *entry;
-       struct rb_node *n;
-
-       n = rb_first(&root_tree->root);
-       while (n) {
-               entry = rb_entry(n, struct root_info, rb_node);
-               rb_erase(n, &root_tree->root);
-               __free_root_info(entry);
-
-               n = rb_first(&root_tree->root);
-       }
+       rb_free_nodes(&root_tree->root, __free_root_info);
 }
 
 /*
@@ -611,15 +559,26 @@ static int resolve_root(struct root_lookup *rl, struct root_info *ri,
        while (1) {
                char *tmp;
                u64 next;
-               int add_len = strlen(found->path);
+               int add_len;
 
-               /* room for / and for null */
-               tmp = malloc(add_len + 2 + len);
-               if (!tmp) {
-                       perror("malloc failed");
-                       exit(1);
+               /*
+                * ref_tree = 0 indicates the subvolumes
+                * has been deleted.
+                */
+               if (!found->ref_tree) {
+                       free(full_path);
+                       return -ENOENT;
                }
+
+               add_len = strlen(found->path);
+
                if (full_path) {
+                       /* room for / and for null */
+                       tmp = malloc(add_len + 2 + len);
+                       if (!tmp) {
+                               perror("malloc failed");
+                               exit(1);
+                       }
                        memcpy(tmp + add_len + 1, full_path, len);
                        tmp[add_len] = '/';
                        memcpy(tmp, found->path, add_len);
@@ -631,27 +590,26 @@ static int resolve_root(struct root_lookup *rl, struct root_info *ri,
                        full_path = strdup(found->path);
                        len = add_len;
                }
+               if (!ri->top_id)
+                       ri->top_id = found->ref_tree;
 
                next = found->ref_tree;
-
-               if (next ==  top_id) {
-                       ri->top_id = top_id;
+               if (next == top_id)
                        break;
-               }
-
-               if (next == BTRFS_FS_TREE_OBJECTID) {
-                       ri->top_id = next;
+               /*
+               * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
+               * we are at the top
+               */
+               if (next == BTRFS_FS_TREE_OBJECTID)
                        break;
-               }
-
                /*
-                * if the ref_tree wasn't in our tree of roots, we're
-                * at the top
-                */
+               * if the ref_tree wasn't in our tree of roots, the
+               * subvolume was deleted.
+               */
                found = root_tree_search(rl, next);
                if (!found) {
-                       ri->top_id = next;
-                       break;
+                       free(full_path);
+                       return -ENOENT;
                }
        }
 
@@ -675,6 +633,9 @@ static int lookup_ino_path(int fd, struct root_info *ri)
        if (ri->path)
                return 0;
 
+       if (!ri->ref_tree)
+               return -ENOENT;
+
        memset(&args, 0, sizeof(args));
        args.treeid = ri->ref_tree;
        args.objectid = ri->dir_id;
@@ -682,6 +643,10 @@ static int lookup_ino_path(int fd, struct root_info *ri)
        ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
        e = errno;
        if (ret) {
+               if (e == ENOENT) {
+                       ri->ref_tree = 0;
+                       return -ENOENT;
+               }
                fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n",
                        (unsigned long long)ri->ref_tree,
                        strerror(e));
@@ -800,7 +765,7 @@ static u64 find_root_gen(int fd)
 
                if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
                        break;
-               if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
+               if (sk->min_objectid != ino_args.treeid)
                        break;
        }
        return max_found;
@@ -852,7 +817,7 @@ static char *__ino_resolve(int fd, u64 dirid)
  * simple string builder, returning a new string with both
  * dirid and name
  */
-char *build_name(char *dirid, char *name)
+static char *build_name(char *dirid, char *name)
 {
        char *full;
        if (!dirid)
@@ -1025,6 +990,7 @@ static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
        time_t t;
        u8 uuid[BTRFS_UUID_SIZE];
        u8 puuid[BTRFS_UUID_SIZE];
+       u8 ruuid[BTRFS_UUID_SIZE];
 
        root_lookup_init(root_lookup);
        memset(&args, 0, sizeof(args));
@@ -1077,27 +1043,29 @@ static int __list_subvol_search(int fd, struct root_lookup *root_lookup)
 
                                add_root(root_lookup, sh.objectid, sh.offset,
                                         0, 0, dir_id, name, name_len, 0, 0, 0,
-                                        NULL, NULL);
+                                        NULL, NULL, NULL);
                        } else if (sh.type == BTRFS_ROOT_ITEM_KEY) {
                                ri = (struct btrfs_root_item *)(args.buf + off);
                                gen = btrfs_root_generation(ri);
                                flags = btrfs_root_flags(ri);
                                if(sh.len >
                                   sizeof(struct btrfs_root_item_v0)) {
-                                       t = ri->otime.sec;
+                                       t = btrfs_stack_timespec_sec(&ri->otime);
                                        ogen = btrfs_root_otransid(ri);
                                        memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
                                        memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
+                                       memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
                                } else {
                                        t = 0;
                                        ogen = 0;
                                        memset(uuid, 0, BTRFS_UUID_SIZE);
                                        memset(puuid, 0, BTRFS_UUID_SIZE);
+                                       memset(ruuid, 0, BTRFS_UUID_SIZE);
                                }
 
                                add_root(root_lookup, sh.objectid, 0,
                                         sh.offset, flags, 0, NULL, 0, ogen,
-                                        gen, t, uuid, puuid);
+                                        gen, t, uuid, puuid, ruuid);
                        }
 
                        off += sh.len;
@@ -1203,6 +1171,16 @@ static int filter_full_path(struct root_info *ri, u64 data)
        return 1;
 }
 
+static int filter_by_parent(struct root_info *ri, u64 data)
+{
+       return !uuid_compare(ri->puuid, (u8 *)(unsigned long)data);
+}
+
+static int filter_deleted(struct root_info *ri, u64 data)
+{
+       return ri->deleted;
+}
+
 static btrfs_list_filter_func all_filter_funcs[] = {
        [BTRFS_LIST_FILTER_ROOTID]              = filter_by_rootid,
        [BTRFS_LIST_FILTER_SNAPSHOT_ONLY]       = filter_snapshot,
@@ -1215,6 +1193,8 @@ static btrfs_list_filter_func all_filter_funcs[] = {
        [BTRFS_LIST_FILTER_CGEN_EQUAL]          = filter_cgen_equal,
        [BTRFS_LIST_FILTER_TOPID_EQUAL]         = filter_topid_equal,
        [BTRFS_LIST_FILTER_FULL_PATH]           = filter_full_path,
+       [BTRFS_LIST_FILTER_BY_PARENT]           = filter_by_parent,
+       [BTRFS_LIST_FILTER_DELETED]             = filter_deleted,
 };
 
 struct btrfs_list_filter_set *btrfs_list_alloc_filter_set(void)
@@ -1269,6 +1249,9 @@ int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
 
        BUG_ON(set->filters[set->nfilters].filter_func);
 
+       if (filter == BTRFS_LIST_FILTER_DELETED)
+               set->only_deleted = 1;
+
        set->filters[set->nfilters].filter_func = all_filter_funcs[filter];
        set->filters[set->nfilters].data = data;
        set->nfilters++;
@@ -1280,9 +1263,15 @@ static int filter_root(struct root_info *ri,
 {
        int i, ret;
 
-       if (!set || !set->nfilters)
+       if (!set)
                return 1;
 
+       if (set->only_deleted && !ri->deleted)
+               return 0;
+
+       if (!set->only_deleted && ri->deleted)
+               return 0;
+
        for (i = 0; i < set->nfilters; i++) {
                if (!set->filters[i].filter_func)
                        break;
@@ -1309,7 +1298,11 @@ static void __filter_and_sort_subvol(struct root_lookup *all_subvols,
        while (n) {
                entry = rb_entry(n, struct root_info, rb_node);
 
-               resolve_root(all_subvols, entry, top_id);
+               ret = resolve_root(all_subvols, entry, top_id);
+               if (ret == -ENOENT) {
+                       entry->full_path = strdup("DELETED");
+                       entry->deleted = 1;
+               }
                ret = filter_root(entry, filter_set);
                if (ret)
                        sort_tree_insert(sort_tree, entry, comp_set);
@@ -1327,7 +1320,7 @@ static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup)
                int ret;
                entry = rb_entry(n, struct root_info, rb_node);
                ret = lookup_ino_path(fd, entry);
-               if(ret < 0)
+               if (ret && ret != -ENOENT)
                        return ret;
                n = rb_next(n);
        }
@@ -1339,7 +1332,7 @@ static void print_subvolume_column(struct root_info *subv,
                                   enum btrfs_list_column_enum column)
 {
        char tstr[256];
-       char uuidparse[37];
+       char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
 
        BUG_ON(column >= BTRFS_LIST_ALL || column < 0);
 
@@ -1360,10 +1353,12 @@ static void print_subvolume_column(struct root_info *subv,
                printf("%llu", subv->top_id);
                break;
        case BTRFS_LIST_OTIME:
-               if (subv->otime)
-                       strftime(tstr, 256, "%Y-%m-%d %X",
-                                localtime(&subv->otime));
-               else
+               if (subv->otime) {
+                       struct tm tm;
+
+                       localtime_r(&subv->otime, &tm);
+                       strftime(tstr, 256, "%Y-%m-%d %X", &tm);
+               } else
                        strcpy(tstr, "-");
                printf("%s", tstr);
                break;
@@ -1381,6 +1376,13 @@ static void print_subvolume_column(struct root_info *subv,
                        uuid_unparse(subv->puuid, uuidparse);
                printf("%s", uuidparse);
                break;
+       case BTRFS_LIST_RUUID:
+               if (uuid_is_null(subv->ruuid))
+                       strcpy(uuidparse, "-");
+               else
+                       uuid_unparse(subv->ruuid, uuidparse);
+               printf("%s", uuidparse);
+               break;
        case BTRFS_LIST_PATH:
                BUG_ON(!subv->full_path);
                printf("%s", subv->full_path);
@@ -1390,6 +1392,22 @@ static void print_subvolume_column(struct root_info *subv,
        }
 }
 
+static void print_single_volume_info_raw(struct root_info *subv, char *raw_prefix)
+{
+       int i;
+
+       for (i = 0; i < BTRFS_LIST_ALL; i++) {
+               if (!btrfs_list_columns[i].need_print)
+                       continue;
+
+               if (raw_prefix)
+                       printf("%s",raw_prefix);
+
+               print_subvolume_column(subv, i);
+       }
+       printf("\n");
+}
+
 static void print_single_volume_info_table(struct root_info *subv)
 {
        int i;
@@ -1426,7 +1444,7 @@ static void print_single_volume_info_default(struct root_info *subv)
        printf("\n");
 }
 
-static void print_all_volume_info_tab_head()
+static void print_all_volume_info_tab_head(void)
 {
        int i;
        int len;
@@ -1456,26 +1474,33 @@ static void print_all_volume_info_tab_head()
 }
 
 static void print_all_volume_info(struct root_lookup *sorted_tree,
-                                 int is_tab_result)
+                                 int layout, char *raw_prefix)
 {
        struct rb_node *n;
        struct root_info *entry;
 
-       if (is_tab_result)
+       if (layout == BTRFS_LIST_LAYOUT_TABLE)
                print_all_volume_info_tab_head();
 
        n = rb_first(&sorted_tree->root);
        while (n) {
                entry = rb_entry(n, struct root_info, sort_node);
-               if (is_tab_result)
-                       print_single_volume_info_table(entry);
-               else
+               switch (layout) {
+               case BTRFS_LIST_LAYOUT_DEFAULT:
                        print_single_volume_info_default(entry);
+                       break;
+               case BTRFS_LIST_LAYOUT_TABLE:
+                       print_single_volume_info_table(entry);
+                       break;
+               case BTRFS_LIST_LAYOUT_RAW:
+                       print_single_volume_info_raw(entry, raw_prefix);
+                       break;
+               }
                n = rb_next(n);
        }
 }
 
-int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
+static int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
 {
        int ret;
 
@@ -1496,12 +1521,17 @@ int btrfs_list_subvols(int fd, struct root_lookup *root_lookup)
 
 int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
                       struct btrfs_list_comparer_set *comp_set,
-                      int is_tab_result, int full_path)
+                      int layout, int full_path, char *raw_prefix)
 {
        struct root_lookup root_lookup;
        struct root_lookup root_sort;
-       int ret;
-       u64 top_id = (full_path ? 0 : btrfs_list_get_path_rootid(fd));
+       int ret = 0;
+       u64 top_id = 0;
+
+       if (full_path)
+               ret = btrfs_list_get_path_rootid(fd, &top_id);
+       if (ret)
+               return ret;
 
        ret = btrfs_list_subvols(fd, &root_lookup);
        if (ret)
@@ -1509,12 +1539,58 @@ int btrfs_list_subvols_print(int fd, struct btrfs_list_filter_set *filter_set,
        __filter_and_sort_subvol(&root_lookup, &root_sort, filter_set,
                                 comp_set, top_id);
 
-       print_all_volume_info(&root_sort, is_tab_result);
+       print_all_volume_info(&root_sort, layout, raw_prefix);
        __free_all_subvolumn(&root_lookup);
 
        return 0;
 }
 
+static char *strdup_or_null(const char *s)
+{
+       if (!s)
+               return NULL;
+       return strdup(s);
+}
+
+int btrfs_get_subvol(int fd, struct root_info *the_ri)
+{
+       int ret, rr;
+       struct root_lookup rl;
+       struct rb_node *rbn;
+       struct root_info *ri;
+       u64 root_id;
+
+       ret = btrfs_list_get_path_rootid(fd, &root_id);
+       if (ret)
+               return ret;
+
+       ret = btrfs_list_subvols(fd, &rl);
+       if (ret)
+               return ret;
+
+       rbn = rb_first(&rl.root);
+       while(rbn) {
+               ri = rb_entry(rbn, struct root_info, rb_node);
+               rr = resolve_root(&rl, ri, root_id);
+               if (rr == -ENOENT) {
+                       ret = -ENOENT;
+                       rbn = rb_next(rbn);
+                       continue;
+               }
+               if (!comp_entry_with_rootid(the_ri, ri, 0)) {
+                       memcpy(the_ri, ri, offsetof(struct root_info, path));
+                       the_ri->path = strdup_or_null(ri->path);
+                       the_ri->name = strdup_or_null(ri->name);
+                       the_ri->full_path = strdup_or_null(ri->full_path);
+                       ret = 0;
+                       break;
+               }
+               rbn = rb_next(rbn);
+       }
+       __free_all_subvolumn(&rl);
+       return ret;
+}
+
 static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
                            struct btrfs_file_extent_item *item,
                            u64 found_gen, u64 *cache_dirid,
@@ -1634,9 +1710,9 @@ int btrfs_list_find_updated_files(int fd, u64 root_id, u64 oldest_gen)
                ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
                e = errno;
                if (ret < 0) {
-                       fprintf(stderr, "ERROR: can't perform the search- %s\n",
+                       fprintf(stderr, "ERROR: can't perform the search - %s\n",
                                strerror(e));
-                       return ret;
+                       break;
                }
                /* the ioctl returns the number of item it found in nr_items */
                if (sk->nr_items == 0)
@@ -1700,7 +1776,11 @@ char *btrfs_list_path_for_root(int fd, u64 root)
        struct rb_node *n;
        char *ret_path = NULL;
        int ret;
-       u64 top_id = btrfs_list_get_path_rootid(fd);
+       u64 top_id;
+
+       ret = btrfs_list_get_path_rootid(fd, &top_id);
+       if (ret)
+               return ERR_PTR(ret);
 
        ret = __list_subvol_search(fd, &root_lookup);
        if (ret < 0)
@@ -1715,7 +1795,11 @@ char *btrfs_list_path_for_root(int fd, u64 root)
                struct root_info *entry;
 
                entry = rb_entry(n, struct root_info, rb_node);
-               resolve_root(&root_lookup, entry, top_id);
+               ret = resolve_root(&root_lookup, entry, top_id);
+               if (ret == -ENOENT && entry->root_id == root) {
+                       ret_path = NULL;
+                       break;
+               }
                if (entry->root_id == root) {
                        ret_path = entry->full_path;
                        entry->full_path = NULL;
@@ -1728,7 +1812,7 @@ char *btrfs_list_path_for_root(int fd, u64 root)
        return ret_path;
 }
 
-int btrfs_list_parse_sort_string(char *optarg,
+int btrfs_list_parse_sort_string(char *opt_arg,
                                 struct btrfs_list_comparer_set **comps)
 {
        int order;
@@ -1737,7 +1821,7 @@ int btrfs_list_parse_sort_string(char *optarg,
        char **ptr_argv;
        int what_to_sort;
 
-       while ((p = strtok(optarg, ",")) != NULL) {
+       while ((p = strtok(opt_arg, ",")) != NULL) {
                flag = 0;
                ptr_argv = all_sort_items;
 
@@ -1773,7 +1857,7 @@ int btrfs_list_parse_sort_string(char *optarg,
                        what_to_sort = btrfs_list_get_sort_item(p);
                        btrfs_list_setup_comparer(comps, what_to_sort, order);
                }
-               optarg = NULL;
+               opt_arg = NULL;
        }
 
        return 0;
@@ -1784,38 +1868,30 @@ int btrfs_list_parse_sort_string(char *optarg,
  *
  * type is the filter object.
  */
-int btrfs_list_parse_filter_string(char *optarg,
+int btrfs_list_parse_filter_string(char *opt_arg,
                                   struct btrfs_list_filter_set **filters,
                                   enum btrfs_list_filter_enum type)
 {
 
        u64 arg;
-       char *ptr_parse_end = NULL;
-       char *ptr_optarg_end = optarg + strlen(optarg);
 
-       switch (*(optarg++)) {
+       switch (*(opt_arg++)) {
        case '+':
-               arg = (u64)strtol(optarg, &ptr_parse_end, 10);
+               arg = arg_strtou64(opt_arg);
                type += 2;
-               if (ptr_parse_end != ptr_optarg_end)
-                       return -1;
 
                btrfs_list_setup_filter(filters, type, arg);
                break;
        case '-':
-               arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
+               arg = arg_strtou64(opt_arg);
                type += 1;
-               if (ptr_parse_end != ptr_optarg_end)
-                       return -1;
 
                btrfs_list_setup_filter(filters, type, arg);
                break;
        default:
-               optarg--;
-               arg = (u64)strtoll(optarg, &ptr_parse_end, 10);
+               opt_arg--;
+               arg = arg_strtou64(opt_arg);
 
-               if (ptr_parse_end != ptr_optarg_end)
-                       return -1;
                btrfs_list_setup_filter(filters, type, arg);
                break;
        }
@@ -1823,7 +1899,7 @@ int btrfs_list_parse_filter_string(char *optarg,
        return 0;
 }
 
-u64 btrfs_list_get_path_rootid(int fd)
+int btrfs_list_get_path_rootid(int fd, u64 *treeid)
 {
        int  ret;
        struct btrfs_ioctl_ino_lookup_args args;
@@ -1834,9 +1910,10 @@ u64 btrfs_list_get_path_rootid(int fd)
        ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
        if (ret < 0) {
                fprintf(stderr,
-                       "ERROR: can't perform the search -%s\n",
+                       "ERROR: can't perform the search - %s\n",
                        strerror(errno));
                return ret;
        }
-       return args.treeid;
+       *treeid = args.treeid;
+       return 0;
 }