Btrfs-progs: make btrfs-map-logical handle error gracefully
[platform/upstream/btrfs-progs.git] / btrfs-map-logical.c
index a109c6a..fce6504 100644 (file)
@@ -41,7 +41,6 @@ struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr,
                                     u32 blocksize, int copy)
 {
        int ret;
-       int dev_nr;
        struct extent_buffer *eb;
        u64 length;
        struct btrfs_multi_bio *multi = NULL;
@@ -53,24 +52,31 @@ struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr,
        if (!eb)
                return NULL;
 
-       dev_nr = 0;
        length = blocksize;
        while (1) {
                ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
-                                     eb->start, &length, &multi, mirror_num);
-               BUG_ON(ret);
+                                     eb->start, &length, &multi,
+                                     mirror_num, NULL);
+               if (ret) {
+                       fprintf(info_file,
+                               "Error: fails to map mirror%d logical %llu: %s\n",
+                               mirror_num, (unsigned long long)eb->start,
+                               strerror(-ret));
+                       free_extent_buffer(eb);
+                       return NULL;
+               }
                device = multi->stripes[0].dev;
                eb->fd = device->fd;
                device->total_ios++;
                eb->dev_bytenr = multi->stripes[0].physical;
 
                fprintf(info_file, "mirror %d logical %Lu physical %Lu "
-                       "device %s\n", mirror_num, bytenr, eb->dev_bytenr,
-                       device->name);
+                       "device %s\n", mirror_num, (unsigned long long)bytenr,
+                       (unsigned long long)eb->dev_bytenr, device->name);
                kfree(multi);
 
                if (!copy || mirror_num == copy)
-                       ret = read_extent_from_disk(eb);
+                       ret = read_extent_from_disk(eb, 0, eb->len);
 
                num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
                                              eb->start, eb->len);
@@ -86,11 +92,11 @@ struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr,
 
 static void print_usage(void)
 {
-       fprintf(stderr, "usage: btrfs-map-logical [options] mount_point\n");
+       fprintf(stderr, "usage: btrfs-map-logical [options] device\n");
        fprintf(stderr, "\t-l Logical extent to map\n");
        fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
        fprintf(stderr, "\t-o Output file to hold the extent\n");
-       fprintf(stderr, "\t-s Number of bytes to read\n");
+       fprintf(stderr, "\t-b Number of bytes to read\n");
        exit(1);
 }
 
@@ -98,7 +104,7 @@ static struct option long_options[] = {
        /* { "byte-count", 1, NULL, 'b' }, */
        { "logical", 1, NULL, 'l' },
        { "copy", 1, NULL, 'c' },
-       { "output", 1, NULL, 'c' },
+       { "output", 1, NULL, 'o' },
        { "bytes", 1, NULL, 'b' },
        { 0, 0, 0, 0}
 };
@@ -175,6 +181,7 @@ int main(int ac, char **av)
                exit(1);
        }
 
+       info_file = stdout;
        if (output_file) {
                if (strcmp(output_file, "-") == 0) {
                        out_fd = 1;