btrfs-progs: fix scrub error return from pthread_mutex_lock
[platform/upstream/btrfs-progs.git] / btrfs-list.c
index a166c8b..a748d5e 100644 (file)
@@ -54,12 +54,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",
@@ -74,7 +74,7 @@ struct {
        {
                .name           = "top level",
                .column_name    = "Top Level",
-               .need_print     = 1,
+               .need_print     = 0,
        },
        {
                .name           = "otime",
@@ -94,7 +94,7 @@ struct {
        {
                .name           = "path",
                .column_name    = "Path",
-               .need_print     = 1,
+               .need_print     = 0,
        },
        {
                .name           = NULL,
@@ -564,7 +564,18 @@ 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;
+
+               /*
+                * ref_tree = 0 indicates the subvolumes
+                * has been deleted.
+                */
+               if (!found->ref_tree) {
+                       free(full_path);
+                       return -ENOENT;
+               }
+
+               add_len = strlen(found->path);
 
                /* room for / and for null */
                tmp = malloc(add_len + 2 + len);
@@ -587,24 +598,28 @@ static int resolve_root(struct root_lookup *rl, struct root_info *ri,
 
                next = found->ref_tree;
 
-               if (next ==  top_id) {
+               if (next == top_id) {
                        ri->top_id = top_id;
                        break;
                }
 
+               /*
+               * if the ref_tree = BTRFS_FS_TREE_OBJECTID,
+               * we are at the top
+               */
                if (next == BTRFS_FS_TREE_OBJECTID) {
                        ri->top_id = next;
                        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;
                }
        }
 
@@ -628,6 +643,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;
@@ -635,6 +653,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));
@@ -1268,10 +1290,13 @@ 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)
+                       goto skip;
                ret = filter_root(entry, filter_set);
                if (ret)
                        sort_tree_insert(sort_tree, entry, comp_set);
+skip:
                n = rb_prev(n);
        }
 }
@@ -1286,7 +1311,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);
        }
@@ -1349,6 +1374,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;
@@ -1415,21 +1456,28 @@ 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);
        }
 }
@@ -1455,12 +1503,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)
@@ -1468,41 +1521,49 @@ 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;
 }
 
+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 = -1;
+       int ret, rr;
        struct root_lookup rl;
        struct rb_node *rbn;
        struct root_info *ri;
-       u64 root_id = btrfs_list_get_path_rootid(fd);
+       u64 root_id;
 
-       if (btrfs_list_subvols(fd, &rl))
-               return 1;
+       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);
-               resolve_root(&rl, ri, root_id);
+               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));
-                       if (ri->path)
-                               the_ri->path = strdup(ri->path);
-                       else
-                               the_ri->path = NULL;
-                       if (ri->name)
-                               the_ri->name = strdup(ri->name);
-                       else
-                               the_ri->name = NULL;
-                       if (ri->full_path)
-                               the_ri->full_path = strdup(ri->full_path);
-                       else
-                               the_ri->name = NULL;
+                       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;
                }
@@ -1697,7 +1758,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)
@@ -1712,7 +1777,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;
@@ -1820,7 +1889,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;
@@ -1835,5 +1904,6 @@ u64 btrfs_list_get_path_rootid(int fd)
                        strerror(errno));
                return ret;
        }
-       return args.treeid;
+       *treeid = args.treeid;
+       return 0;
 }