btrfs-progs: add OPEN_CTREE_INVALIDATE_FST flag
[platform/upstream/btrfs-progs.git] / btrfs-image.c
index 3684a05..066baf9 100644 (file)
@@ -44,6 +44,8 @@
 #define COMPRESS_NONE          0
 #define COMPRESS_ZLIB          1
 
+#define MAX_WORKER_THREADS     (32)
+
 struct meta_cluster_item {
        __le64 bytenr;
        __le32 size;
@@ -68,6 +70,13 @@ struct meta_cluster {
 struct fs_chunk {
        u64 logical;
        u64 physical;
+       /*
+        * physical_dup only store additonal physical for BTRFS_BLOCK_GROUP_DUP
+        * currently restore only support single and DUP
+        * TODO: modify this structure and the function related to this
+        * structure for support RAID*
+        */
+       u64 physical_dup;
        u64 bytes;
        struct rb_node l;
        struct rb_node p;
@@ -88,9 +97,12 @@ struct metadump_struct {
        struct btrfs_root *root;
        FILE *out;
 
-       struct meta_cluster *cluster;
+       union {
+               struct meta_cluster cluster;
+               char meta_cluster_bytes[BLOCK_SIZE];
+       };
 
-       pthread_t *threads;
+       pthread_t threads[MAX_WORKER_THREADS];
        size_t num_threads;
        pthread_mutex_t mutex;
        pthread_cond_t cond;
@@ -123,7 +135,7 @@ struct mdrestore_struct {
        FILE *in;
        FILE *out;
 
-       pthread_t *threads;
+       pthread_t threads[MAX_WORKER_THREADS];
        size_t num_threads;
        pthread_mutex_t mutex;
        pthread_cond_t cond;
@@ -133,7 +145,7 @@ struct mdrestore_struct {
        struct list_head list;
        struct list_head overlapping_chunks;
        size_t num_items;
-       u32 leafsize;
+       u32 nodesize;
        u64 devid;
        u64 alloced_chunks;
        u64 last_physical_offset;
@@ -156,7 +168,7 @@ static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size);
 
 static void csum_block(u8 *buf, size_t len)
 {
-       char result[BTRFS_CRC32_SIZE];
+       u8 result[BTRFS_CRC32_SIZE];
        u32 crc = ~(u32)0;
        crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
        btrfs_csum_final(crc, result);
@@ -188,7 +200,7 @@ static char *generate_garbage(u32 name_len)
                return NULL;
 
        for (i = 0; i < name_len; i++) {
-               char c = rand() % 94 + 33;
+               char c = rand_range(94) + 33;
 
                if (c == '/')
                        c++;
@@ -290,7 +302,8 @@ static struct rb_node *tree_search(struct rb_root *root,
        return NULL;
 }
 
-static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical, u64 *size)
+static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical,
+                              u64 *size, u64 *physical_dup)
 {
        struct fs_chunk *fs_chunk;
        struct rb_node *entry;
@@ -304,7 +317,7 @@ static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical, u64
        entry = tree_search(&mdres->chunk_tree, &search.l, chunk_cmp, 1);
        if (!entry) {
                if (mdres->in != stdin)
-                       printf("Couldn't find a chunk, using logical\n");
+                       warning("cannot find a chunk, using logical");
                return logical;
        }
        fs_chunk = rb_entry(entry, struct fs_chunk, l);
@@ -312,6 +325,14 @@ static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical, u64
                BUG();
        offset = search.logical - fs_chunk->logical;
 
+       if (physical_dup) {
+               /* Only in dup case, physical_dup is not equal to 0 */
+               if (fs_chunk->physical_dup)
+                       *physical_dup = fs_chunk->physical_dup + offset;
+               else
+                       *physical_dup = 0;
+       }
+
        *size = min(*size, fs_chunk->bytes + fs_chunk->logical - logical);
        return fs_chunk->physical + offset;
 }
@@ -338,7 +359,7 @@ static char *find_collision(struct metadump_struct *md, char *name,
 
        val = malloc(sizeof(struct name));
        if (!val) {
-               fprintf(stderr, "Couldn't sanitize name, enomem\n");
+               error("cannot sanitize name, not enough memory");
                free(name);
                return NULL;
        }
@@ -349,7 +370,7 @@ static char *find_collision(struct metadump_struct *md, char *name,
        val->len = name_len;
        val->sub = malloc(name_len);
        if (!val->sub) {
-               fprintf(stderr, "Couldn't sanitize name, enomem\n");
+               error("cannot sanitize name, not enough memory");
                free(val);
                free(name);
                return NULL;
@@ -388,11 +409,11 @@ static char *find_collision(struct metadump_struct *md, char *name,
        }
 
        if (!found) {
-               fprintf(stderr, "Couldn't find a collision for '%.*s', "
-                       "generating normal garbage, it won't match indexes\n",
+               warning(
+"cannot find a hash collision for '%.*s', generating garbage, it won't match indexes",
                        val->len, val->val);
                for (i = 0; i < name_len; i++) {
-                       char c = rand() % 94 + 33;
+                       char c = rand_range(94) + 33;
 
                        if (c == '/')
                                c++;
@@ -429,8 +450,7 @@ static void sanitize_dir_item(struct metadump_struct *md, struct extent_buffer *
                if (md->sanitize_names > 1) {
                        buf = malloc(name_len);
                        if (!buf) {
-                               fprintf(stderr, "Couldn't sanitize name, "
-                                       "enomem\n");
+                               error("cannot sanitize name, not enough memory");
                                return;
                        }
                        read_extent_buffer(eb, buf, name_ptr, name_len);
@@ -439,7 +459,7 @@ static void sanitize_dir_item(struct metadump_struct *md, struct extent_buffer *
                        garbage = generate_garbage(name_len);
                }
                if (!garbage) {
-                       fprintf(stderr, "Couldn't sanitize name, enomem\n");
+                       error("cannot sanitize name, not enough memory");
                        return;
                }
                write_extent_buffer(eb, garbage, name_ptr, name_len);
@@ -484,8 +504,7 @@ static void sanitize_inode_ref(struct metadump_struct *md,
                if (md->sanitize_names > 1) {
                        buf = malloc(len);
                        if (!buf) {
-                               fprintf(stderr, "Couldn't sanitize name, "
-                                       "enomem\n");
+                               error("cannot sanitize name, not enough memory");
                                return;
                        }
                        read_extent_buffer(eb, buf, name_ptr, len);
@@ -495,7 +514,7 @@ static void sanitize_inode_ref(struct metadump_struct *md,
                }
 
                if (!garbage) {
-                       fprintf(stderr, "Couldn't sanitize name, enomem\n");
+                       error("cannot sanitize name, not enough memory");
                        return;
                }
                write_extent_buffer(eb, garbage, name_ptr, len);
@@ -527,7 +546,7 @@ static void sanitize_name(struct metadump_struct *md, u8 *dst,
 
        eb = alloc_dummy_eb(src->start, src->len);
        if (!eb) {
-               fprintf(stderr, "Couldn't sanitize name, no memory\n");
+               error("cannot sanitize name, not enough memory");
                return;
        }
 
@@ -657,7 +676,7 @@ static void *dump_worker(void *data)
                        async->bufsize = compressBound(async->size);
                        async->buffer = malloc(async->bufsize);
                        if (!async->buffer) {
-                               fprintf(stderr, "Error allocing buffer\n");
+                               error("not enough memory for async buffer");
                                pthread_mutex_lock(&md->mutex);
                                if (!md->error)
                                        md->error = -ENOMEM;
@@ -689,7 +708,7 @@ static void meta_cluster_init(struct metadump_struct *md, u64 start)
 
        md->num_items = 0;
        md->num_ready = 0;
-       header = &md->cluster->header;
+       header = &md->cluster.header;
        header->magic = cpu_to_le64(HEADER_MAGIC);
        header->bytenr = cpu_to_le64(start);
        header->nritems = cpu_to_le32(0);
@@ -722,8 +741,6 @@ static void metadump_destroy(struct metadump_struct *md, int num_threads)
                free(name->sub);
                free(name);
        }
-       free(md->threads);
-       free(md->cluster);
 }
 
 static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
@@ -733,39 +750,25 @@ static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
        int i, ret = 0;
 
        memset(md, 0, sizeof(*md));
-       pthread_cond_init(&md->cond, NULL);
-       pthread_mutex_init(&md->mutex, NULL);
        INIT_LIST_HEAD(&md->list);
        INIT_LIST_HEAD(&md->ordered);
        md->root = root;
        md->out = out;
        md->pending_start = (u64)-1;
        md->compress_level = compress_level;
-       md->cluster = calloc(1, BLOCK_SIZE);
        md->sanitize_names = sanitize_names;
        if (sanitize_names > 1)
                crc32c_optimization_init();
 
-       if (!md->cluster) {
-               pthread_cond_destroy(&md->cond);
-               pthread_mutex_destroy(&md->mutex);
-               return -ENOMEM;
-       }
-
+       md->name_tree.rb_node = NULL;
+       md->num_threads = num_threads;
+       pthread_cond_init(&md->cond, NULL);
+       pthread_mutex_init(&md->mutex, NULL);
        meta_cluster_init(md, 0);
+
        if (!num_threads)
                return 0;
 
-       md->name_tree.rb_node = NULL;
-       md->num_threads = num_threads;
-       md->threads = calloc(num_threads, sizeof(pthread_t));
-       if (!md->threads) {
-               free(md->cluster);
-               pthread_cond_destroy(&md->cond);
-               pthread_mutex_destroy(&md->mutex);
-               return -ENOMEM;
-       }
-
        for (i = 0; i < num_threads; i++) {
                ret = pthread_create(md->threads + i, NULL, dump_worker, md);
                if (ret)
@@ -786,7 +789,7 @@ static int write_zero(FILE *out, size_t size)
 
 static int write_buffers(struct metadump_struct *md, u64 *next)
 {
-       struct meta_cluster_header *header = &md->cluster->header;
+       struct meta_cluster_header *header = &md->cluster.header;
        struct meta_cluster_item *item;
        struct async_work *async;
        u64 bytenr = 0;
@@ -810,24 +813,23 @@ static int write_buffers(struct metadump_struct *md, u64 *next)
        }
 
        if (err) {
-               fprintf(stderr, "One of the threads errored out %s\n",
-                               strerror(err));
+               error("one of the threads failed: %s", strerror(-err));
                goto out;
        }
 
        /* setup and write index block */
        list_for_each_entry(async, &md->ordered, ordered) {
-               item = md->cluster->items + nritems;
+               item = &md->cluster.items[nritems];
                item->bytenr = cpu_to_le64(async->start);
                item->size = cpu_to_le32(async->bufsize);
                nritems++;
        }
        header->nritems = cpu_to_le32(nritems);
 
-       ret = fwrite(md->cluster, BLOCK_SIZE, 1, md->out);
+       ret = fwrite(&md->cluster, BLOCK_SIZE, 1, md->out);
        if (ret != 1) {
-               fprintf(stderr, "Error writing out cluster: %d\n", errno);
-               return -EIO;
+               error("unable to write out cluster: %s", strerror(errno));
+               return -errno;
        }
 
        /* write buffers */
@@ -842,10 +844,10 @@ static int write_buffers(struct metadump_struct *md, u64 *next)
                        ret = fwrite(async->buffer, async->bufsize, 1,
                                     md->out);
                if (ret != 1) {
-                       err = -EIO;
+                       error("unable to write out cluster: %s",
+                               strerror(errno));
+                       err = -errno;
                        ret = 0;
-                       fprintf(stderr, "Error writing out cluster: %d\n",
-                               errno);
                }
 
                free(async->buffer);
@@ -859,9 +861,9 @@ static int write_buffers(struct metadump_struct *md, u64 *next)
                bytenr += size;
                ret = write_zero(md->out, size);
                if (ret != 1) {
-                       fprintf(stderr, "Error zeroing out buffer: %d\n",
-                               errno);
-                       err = -EIO;
+                       error("unable to zero out buffer: %s",
+                               strerror(errno));
+                       err = -errno;
                }
        }
 out:
@@ -872,54 +874,34 @@ out:
 static int read_data_extent(struct metadump_struct *md,
                            struct async_work *async)
 {
-       struct btrfs_multi_bio *multi = NULL;
-       struct btrfs_device *device;
+       struct btrfs_root *root = md->root;
        u64 bytes_left = async->size;
        u64 logical = async->start;
        u64 offset = 0;
-       u64 bytenr;
        u64 read_len;
-       ssize_t done;
-       int fd;
+       int num_copies;
+       int cur_mirror;
        int ret;
 
-       while (bytes_left) {
-               read_len = bytes_left;
-               ret = btrfs_map_block(&md->root->fs_info->mapping_tree, READ,
-                                     logical, &read_len, &multi, 0, NULL);
-               if (ret) {
-                       fprintf(stderr, "Couldn't map data block %d\n", ret);
-                       return ret;
-               }
+       num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, logical,
+                                     bytes_left);
 
-               device = multi->stripes[0].dev;
-
-               if (device->fd == 0) {
-                       fprintf(stderr,
-                               "Device we need to read from is not open\n");
-                       free(multi);
-                       return -EIO;
-               }
-               fd = device->fd;
-               bytenr = multi->stripes[0].physical;
-               free(multi);
-
-               read_len = min(read_len, bytes_left);
-               done = pread64(fd, async->buffer+offset, read_len, bytenr);
-               if (done < read_len) {
-                       if (done < 0)
-                               fprintf(stderr, "Error reading extent %d\n",
-                                       errno);
-                       else
-                               fprintf(stderr, "Short read\n");
-                       return -EIO;
+       /* Try our best to read data, just like read_tree_block() */
+       for (cur_mirror = 0; cur_mirror < num_copies; cur_mirror++) {
+               while (bytes_left) {
+                       read_len = bytes_left;
+                       ret = read_extent_data(root,
+                                       (char *)(async->buffer + offset),
+                                       logical, &read_len, cur_mirror);
+                       if (ret < 0)
+                               break;
+                       offset += read_len;
+                       logical += read_len;
+                       bytes_left -= read_len;
                }
-
-               bytes_left -= done;
-               offset += done;
-               logical += done;
        }
-
+       if (bytes_left)
+               return -EIO;
        return 0;
 }
 
@@ -937,7 +919,7 @@ static int flush_pending(struct metadump_struct *md, int done)
        struct async_work *async = NULL;
        struct extent_buffer *eb;
        u64 blocksize = md->root->nodesize;
-       u64 start;
+       u64 start = 0;
        u64 size;
        size_t offset;
        int ret = 0;
@@ -979,8 +961,10 @@ static int flush_pending(struct metadump_struct *md, int done)
                        if (ret < size) {
                                free(async->buffer);
                                free(async);
-                               fprintf(stderr, "Error reading superblock\n");
-                               return -EIO;
+                               error("unable to read superblock at %llu: %s",
+                                               (unsigned long long)start,
+                                               strerror(errno));
+                               return -errno;
                        }
                        size = 0;
                        ret = 0;
@@ -992,8 +976,8 @@ static int flush_pending(struct metadump_struct *md, int done)
                        if (!extent_buffer_uptodate(eb)) {
                                free(async->buffer);
                                free(async);
-                               fprintf(stderr,
-                                       "Error reading metadata block\n");
+                               error("unable to read metadata block %llu",
+                                       (unsigned long long)start);
                                return -EIO;
                        }
                        copy_buffer(md, async->buffer + offset, eb);
@@ -1023,8 +1007,7 @@ static int flush_pending(struct metadump_struct *md, int done)
        if (md->num_items >= ITEMS_PER_CLUSTER || done) {
                ret = write_buffers(md, &start);
                if (ret)
-                       fprintf(stderr, "Error writing buffers %d\n",
-                               errno);
+                       error("unable to write buffers: %s", strerror(-ret));
                else
                        meta_cluster_init(md, start);
        }
@@ -1099,9 +1082,10 @@ static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
        int i = 0;
        int ret;
 
-       ret = add_extent(btrfs_header_bytenr(eb), root->leafsize, metadump, 0);
+       ret = add_extent(btrfs_header_bytenr(eb), root->nodesize, metadump, 0);
        if (ret) {
-               fprintf(stderr, "Error adding metadata block\n");
+               error("unable to add metadata block %llu: %d",
+                               btrfs_header_bytenr(eb), ret);
                return ret;
        }
 
@@ -1117,10 +1101,9 @@ static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
                                continue;
                        ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
                        bytenr = btrfs_disk_root_bytenr(eb, ri);
-                       tmp = read_tree_block(root, bytenr, root->leafsize, 0);
+                       tmp = read_tree_block(root, bytenr, root->nodesize, 0);
                        if (!extent_buffer_uptodate(tmp)) {
-                               fprintf(stderr,
-                                       "Error reading log root block\n");
+                               error("unable to read log root block");
                                return -EIO;
                        }
                        ret = copy_tree_blocks(root, tmp, metadump, 0);
@@ -1129,9 +1112,9 @@ static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
                                return ret;
                } else {
                        bytenr = btrfs_node_blockptr(eb, i);
-                       tmp = read_tree_block(root, bytenr, root->leafsize, 0);
+                       tmp = read_tree_block(root, bytenr, root->nodesize, 0);
                        if (!extent_buffer_uptodate(tmp)) {
-                               fprintf(stderr, "Error reading log block\n");
+                               error("unable to read log root block");
                                return -EIO;
                        }
                        ret = copy_tree_blocks(root, tmp, metadump, root_tree);
@@ -1155,7 +1138,7 @@ static int copy_log_trees(struct btrfs_root *root,
 
        if (!root->fs_info->log_root_tree ||
            !root->fs_info->log_root_tree->node) {
-               fprintf(stderr, "Error copying tree log, it wasn't setup\n");
+               error("unable to copy tree log, it has not been setup");
                return -EIO;
        }
 
@@ -1181,8 +1164,7 @@ static int copy_space_cache(struct btrfs_root *root,
 
        ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
        if (ret < 0) {
-               fprintf(stderr, "Error searching for free space inode %d\n",
-                       ret);
+               error("free space inode not found: %d", ret);
                return ret;
        }
 
@@ -1192,8 +1174,7 @@ static int copy_space_cache(struct btrfs_root *root,
                if (path->slots[0] >= btrfs_header_nritems(leaf)) {
                        ret = btrfs_next_leaf(root, path);
                        if (ret < 0) {
-                               fprintf(stderr, "Error going to next leaf "
-                                       "%d\n", ret);
+                               error("cannot go to next leaf %d", ret);
                                return ret;
                        }
                        if (ret > 0)
@@ -1219,8 +1200,7 @@ static int copy_space_cache(struct btrfs_root *root,
                num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
                ret = add_extent(bytenr, num_bytes, metadump, 1);
                if (ret) {
-                       fprintf(stderr, "Error adding space cache blocks %d\n",
-                               ret);
+                       error("unable to add space cache blocks %d", ret);
                        btrfs_release_path(path);
                        return ret;
                }
@@ -1249,7 +1229,7 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
 
        ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
        if (ret < 0) {
-               fprintf(stderr, "Error searching extent root %d\n", ret);
+               error("extent root not found: %d", ret);
                return ret;
        }
        ret = 0;
@@ -1260,8 +1240,7 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
                if (path->slots[0] >= btrfs_header_nritems(leaf)) {
                        ret = btrfs_next_leaf(extent_root, path);
                        if (ret < 0) {
-                               fprintf(stderr, "Error going to next leaf %d"
-                                       "\n", ret);
+                               error("cannot go to next leaf %d", ret);
                                break;
                        }
                        if (ret > 0) {
@@ -1280,10 +1259,18 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
                }
 
                bytenr = key.objectid;
-               if (key.type == BTRFS_METADATA_ITEM_KEY)
-                       num_bytes = extent_root->leafsize;
-               else
+               if (key.type == BTRFS_METADATA_ITEM_KEY) {
+                       num_bytes = extent_root->nodesize;
+               } else {
                        num_bytes = key.offset;
+               }
+
+               if (num_bytes == 0) {
+                       error("extent length 0 at bytenr %llu key type %d",
+                                       (unsigned long long)bytenr, key.type);
+                       ret = -EIO;
+                       break;
+               }
 
                if (btrfs_item_size_nr(leaf, path->slots[0]) > sizeof(*ei)) {
                        ei = btrfs_item_ptr(leaf, path->slots[0],
@@ -1293,8 +1280,8 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
                                ret = add_extent(bytenr, num_bytes, metadump,
                                                 0);
                                if (ret) {
-                                       fprintf(stderr, "Error adding block "
-                                               "%d\n", ret);
+                                       error("unable to add block %llu: %d",
+                                               (unsigned long long)bytenr, ret);
                                        break;
                                }
                        }
@@ -1302,8 +1289,8 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
                        ret = is_tree_block(extent_root, path, bytenr);
                        if (ret < 0) {
-                               fprintf(stderr, "Error checking tree block "
-                                       "%d\n", ret);
+                               error("failed to check tree block %llu: %d",
+                                       (unsigned long long)bytenr, ret);
                                break;
                        }
 
@@ -1311,15 +1298,15 @@ static int copy_from_extent_tree(struct metadump_struct *metadump,
                                ret = add_extent(bytenr, num_bytes, metadump,
                                                 0);
                                if (ret) {
-                                       fprintf(stderr, "Error adding block "
-                                               "%d\n", ret);
+                                       error("unable to add block %llu: %d",
+                                               (unsigned long long)bytenr, ret);
                                        break;
                                }
                        }
                        ret = 0;
 #else
-                       fprintf(stderr, "Either extent tree corruption or "
-                               "you haven't built with V0 support\n");
+                       error(
+       "either extent tree is corrupted or you haven't built with V0 support");
                        ret = -EIO;
                        break;
 #endif
@@ -1336,23 +1323,21 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
                           int compress_level, int sanitize, int walk_trees)
 {
        struct btrfs_root *root;
-       struct btrfs_path *path = NULL;
+       struct btrfs_path path;
        struct metadump_struct metadump;
        int ret;
        int err = 0;
 
        root = open_ctree(input, 0, 0);
        if (!root) {
-               fprintf(stderr, "Open ctree failed\n");
+               error("open ctree failed");
                return -EIO;
        }
 
-       BUG_ON(root->nodesize != root->leafsize);
-
        ret = metadump_init(&metadump, root, out, num_threads,
                            compress_level, sanitize);
        if (ret) {
-               fprintf(stderr, "Error initing metadump %d\n", ret);
+               error("failed to initialize metadump: %d", ret);
                close_ctree(root);
                return ret;
        }
@@ -1360,17 +1345,12 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
        ret = add_extent(BTRFS_SUPER_INFO_OFFSET, BTRFS_SUPER_INFO_SIZE,
                        &metadump, 0);
        if (ret) {
-               fprintf(stderr, "Error adding metadata %d\n", ret);
+               error("unable to add metadata: %d", ret);
                err = ret;
                goto out;
        }
 
-       path = btrfs_alloc_path();
-       if (!path) {
-               fprintf(stderr, "Out of memory allocing path\n");
-               err = -ENOMEM;
-               goto out;
-       }
+       btrfs_init_path(&path);
 
        if (walk_trees) {
                ret = copy_tree_blocks(root, root->fs_info->chunk_root->node,
@@ -1387,31 +1367,31 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
                        goto out;
                }
        } else {
-               ret = copy_from_extent_tree(&metadump, path);
+               ret = copy_from_extent_tree(&metadump, &path);
                if (ret) {
                        err = ret;
                        goto out;
                }
        }
 
-       ret = copy_log_trees(root, &metadump, path);
+       ret = copy_log_trees(root, &metadump, &path);
        if (ret) {
                err = ret;
                goto out;
        }
 
-       ret = copy_space_cache(root, &metadump, path);
+       ret = copy_space_cache(root, &metadump, &path);
 out:
        ret = flush_pending(&metadump, 1);
        if (ret) {
                if (!err)
                        err = ret;
-               fprintf(stderr, "Error flushing pending %d\n", ret);
+               error("failed to flush pending data: %d", ret);
        }
 
        metadump_destroy(&metadump, num_threads);
 
-       btrfs_free_path(path);
+       btrfs_release_path(&path);
        ret = close_ctree(root);
        return err ? err : ret;
 }
@@ -1479,21 +1459,26 @@ static int update_super(struct mdrestore_struct *mdres, u8 *buffer)
                cur += sizeof(*disk_key);
 
                if (key.type == BTRFS_CHUNK_ITEM_KEY) {
-                       u64 physical, size = 0;
+                       u64 type, physical, physical_dup, size = 0;
 
                        chunk = (struct btrfs_chunk *)ptr;
                        old_num_stripes = btrfs_stack_chunk_num_stripes(chunk);
                        chunk = (struct btrfs_chunk *)write_ptr;
 
                        memmove(write_ptr, ptr, sizeof(*chunk));
-                       btrfs_set_stack_chunk_num_stripes(chunk, 1);
                        btrfs_set_stack_chunk_sub_stripes(chunk, 0);
-                       btrfs_set_stack_chunk_type(chunk,
-                                                  BTRFS_BLOCK_GROUP_SYSTEM);
-                       btrfs_set_stack_stripe_devid(&chunk->stripe,
-                                                    super->dev_item.devid);
+                       type = btrfs_stack_chunk_type(chunk);
+                       if (type & BTRFS_BLOCK_GROUP_DUP) {
+                               new_array_size += sizeof(struct btrfs_stripe);
+                               write_ptr += sizeof(struct btrfs_stripe);
+                       } else {
+                               btrfs_set_stack_chunk_num_stripes(chunk, 1);
+                               btrfs_set_stack_chunk_type(chunk,
+                                               BTRFS_BLOCK_GROUP_SYSTEM);
+                       }
+                       chunk->stripe.devid = super->dev_item.devid;
                        physical = logical_to_physical(mdres, key.offset,
-                                                      &size);
+                                                      &size, &physical_dup);
                        if (size != (u64)-1)
                                btrfs_set_stack_stripe_offset(&chunk->stripe,
                                                              physical);
@@ -1501,8 +1486,7 @@ static int update_super(struct mdrestore_struct *mdres, u8 *buffer)
                               BTRFS_UUID_SIZE);
                        new_array_size += sizeof(*chunk);
                } else {
-                       fprintf(stderr, "Bogus key in the sys chunk array "
-                               "%d\n", key.type);
+                       error("bogus key in the sys array %d", key.type);
                        return -EIO;
                }
                write_ptr += sizeof(*chunk);
@@ -1525,10 +1509,9 @@ static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size)
 {
        struct extent_buffer *eb;
 
-       eb = malloc(sizeof(struct extent_buffer) + size);
+       eb = calloc(1, sizeof(struct extent_buffer) + size);
        if (!eb)
                return NULL;
-       memset(eb, 0, sizeof(struct extent_buffer) + size);
 
        eb->start = bytenr;
        eb->len = size;
@@ -1578,16 +1561,16 @@ static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
        u64 bytenr = async->start;
        int i;
 
-       if (size_left % mdres->leafsize)
+       if (size_left % mdres->nodesize)
                return 0;
 
-       eb = alloc_dummy_eb(bytenr, mdres->leafsize);
+       eb = alloc_dummy_eb(bytenr, mdres->nodesize);
        if (!eb)
                return -ENOMEM;
 
        while (size_left) {
                eb->start = bytenr;
-               memcpy(eb->data, buffer, mdres->leafsize);
+               memcpy(eb->data, buffer, mdres->nodesize);
 
                if (btrfs_header_bytenr(eb) != bytenr)
                        break;
@@ -1603,48 +1586,54 @@ static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
                        goto next;
 
                for (i = 0; i < btrfs_header_nritems(eb); i++) {
-                       struct btrfs_chunk chunk;
+                       struct btrfs_chunk *chunk;
                        struct btrfs_key key;
-                       u64 type, physical, size = (u64)-1;
+                       u64 type, physical, physical_dup, size = (u64)-1;
 
                        btrfs_item_key_to_cpu(eb, &key, i);
                        if (key.type != BTRFS_CHUNK_ITEM_KEY)
                                continue;
-                       truncate_item(eb, i, sizeof(chunk));
-                       read_extent_buffer(eb, &chunk,
-                                          btrfs_item_ptr_offset(eb, i),
-                                          sizeof(chunk));
 
                        size = 0;
                        physical = logical_to_physical(mdres, key.offset,
-                                                      &size);
+                                                      &size, &physical_dup);
+
+                       if (!physical_dup)
+                               truncate_item(eb, i, sizeof(*chunk));
+                       chunk = btrfs_item_ptr(eb, i, struct btrfs_chunk);
+
 
                        /* Zero out the RAID profile */
-                       type = btrfs_stack_chunk_type(&chunk);
+                       type = btrfs_chunk_type(eb, chunk);
                        type &= (BTRFS_BLOCK_GROUP_DATA |
                                 BTRFS_BLOCK_GROUP_SYSTEM |
                                 BTRFS_BLOCK_GROUP_METADATA |
                                 BTRFS_BLOCK_GROUP_DUP);
-                       btrfs_set_stack_chunk_type(&chunk, type);
+                       btrfs_set_chunk_type(eb, chunk, type);
 
-                       btrfs_set_stack_chunk_num_stripes(&chunk, 1);
-                       btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
-                       btrfs_set_stack_stripe_devid(&chunk.stripe, mdres->devid);
+                       if (!physical_dup)
+                               btrfs_set_chunk_num_stripes(eb, chunk, 1);
+                       btrfs_set_chunk_sub_stripes(eb, chunk, 0);
+                       btrfs_set_stripe_devid_nr(eb, chunk, 0, mdres->devid);
                        if (size != (u64)-1)
-                               btrfs_set_stack_stripe_offset(&chunk.stripe,
-                                                             physical);
-                       memcpy(chunk.stripe.dev_uuid, mdres->uuid,
-                              BTRFS_UUID_SIZE);
-                       write_extent_buffer(eb, &chunk,
-                                           btrfs_item_ptr_offset(eb, i),
-                                           sizeof(chunk));
+                               btrfs_set_stripe_offset_nr(eb, chunk, 0,
+                                                          physical);
+                       /* update stripe 2 offset */
+                       if (physical_dup)
+                               btrfs_set_stripe_offset_nr(eb, chunk, 1,
+                                                          physical_dup);
+
+                       write_extent_buffer(eb, mdres->uuid,
+                                       (unsigned long)btrfs_stripe_dev_uuid_nr(
+                                               chunk, 0),
+                                       BTRFS_UUID_SIZE);
                }
                memcpy(buffer, eb->data, eb->len);
                csum_block(buffer, eb->len);
 next:
-               size_left -= mdres->leafsize;
-               buffer += mdres->leafsize;
-               bytenr += mdres->leafsize;
+               size_left -= mdres->nodesize;
+               buffer += mdres->nodesize;
+               bytenr += mdres->nodesize;
        }
 
        free(eb);
@@ -1661,8 +1650,9 @@ static void write_backup_supers(int fd, u8 *buf)
        int ret;
 
        if (fstat(fd, &st)) {
-               fprintf(stderr, "Couldn't stat restore point, won't be able "
-                       "to write backup supers: %d\n", errno);
+               error(
+       "cannot stat restore point, won't be able to write backup supers: %s",
+                       strerror(errno));
                return;
        }
 
@@ -1677,11 +1667,11 @@ static void write_backup_supers(int fd, u8 *buf)
                ret = pwrite64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
                if (ret < BTRFS_SUPER_INFO_SIZE) {
                        if (ret < 0)
-                               fprintf(stderr, "Problem writing out backup "
-                                       "super block %d, err %d\n", i, errno);
+                               error(
+                               "problem writing out backup super block %d: %s",
+                                               i, strerror(errno));
                        else
-                               fprintf(stderr, "Short write writing out "
-                                       "backup super block\n");
+                               error("short write writing out backup super block");
                        break;
                }
        }
@@ -1701,7 +1691,7 @@ static void *restore_worker(void *data)
        outfd = fileno(mdres->out);
        buffer = malloc(compress_size);
        if (!buffer) {
-               fprintf(stderr, "Error allocing buffer\n");
+               error("not enough memory for restore worker buffer");
                pthread_mutex_lock(&mdres->mutex);
                if (!mdres->error)
                        mdres->error = -ENOMEM;
@@ -1710,12 +1700,12 @@ static void *restore_worker(void *data)
        }
 
        while (1) {
-               u64 bytenr;
+               u64 bytenr, physical_dup;
                off_t offset = 0;
                int err = 0;
 
                pthread_mutex_lock(&mdres->mutex);
-               while (!mdres->leafsize || list_empty(&mdres->list)) {
+               while (!mdres->nodesize || list_empty(&mdres->list)) {
                        if (mdres->done) {
                                pthread_mutex_unlock(&mdres->mutex);
                                goto out;
@@ -1731,8 +1721,7 @@ static void *restore_worker(void *data)
                        ret = uncompress(buffer, (unsigned long *)&size,
                                         async->buffer, async->bufsize);
                        if (ret != Z_OK) {
-                               fprintf(stderr, "Error decompressing %d\n",
-                                       ret);
+                               error("decompressiion failed with %d", ret);
                                err = -EIO;
                        }
                        outbuf = buffer;
@@ -1760,34 +1749,45 @@ static void *restore_worker(void *data)
                if (!mdres->fixup_offset) {
                        while (size) {
                                u64 chunk_size = size;
+                               physical_dup = 0;
                                if (!mdres->multi_devices && !mdres->old_restore)
                                        bytenr = logical_to_physical(mdres,
-                                                                    async->start + offset,
-                                                                    &chunk_size);
+                                                    async->start + offset,
+                                                    &chunk_size,
+                                                    &physical_dup);
                                else
                                        bytenr = async->start + offset;
 
                                ret = pwrite64(outfd, outbuf+offset, chunk_size,
                                               bytenr);
-                               if (ret != chunk_size) {
-                                       if (ret < 0) {
-                                               fprintf(stderr, "Error writing to "
-                                                       "device %d\n", errno);
-                                               err = errno;
-                                               break;
-                                       } else {
-                                               fprintf(stderr, "Short write\n");
-                                               err = -EIO;
-                                               break;
-                                       }
-                               }
+                               if (ret != chunk_size)
+                                       goto error;
+
+                               if (physical_dup)
+                                       ret = pwrite64(outfd, outbuf+offset,
+                                                      chunk_size,
+                                                      physical_dup);
+                               if (ret != chunk_size)
+                                       goto error;
+
                                size -= chunk_size;
                                offset += chunk_size;
+                               continue;
+
+error:
+                               if (ret < 0) {
+                                       error("unable to write to device: %s",
+                                                       strerror(errno));
+                                       err = errno;
+                               } else {
+                                       error("short write");
+                                       err = -EIO;
+                               }
                        }
                } else if (async->start != BTRFS_SUPER_INFO_OFFSET) {
                        ret = write_data_to_disk(mdres->info, outbuf, async->start, size, 0);
                        if (ret) {
-                               printk("Error write data\n");
+                               error("failed to write data");
                                exit(1);
                        }
                }
@@ -1834,7 +1834,6 @@ static void mdrestore_destroy(struct mdrestore_struct *mdres, int num_threads)
 
        pthread_cond_destroy(&mdres->cond);
        pthread_mutex_destroy(&mdres->mutex);
-       free(mdres->threads);
 }
 
 static int mdrestore_init(struct mdrestore_struct *mdres,
@@ -1864,14 +1863,14 @@ static int mdrestore_init(struct mdrestore_struct *mdres,
                return 0;
 
        mdres->num_threads = num_threads;
-       mdres->threads = calloc(num_threads, sizeof(pthread_t));
-       if (!mdres->threads)
-               return -ENOMEM;
        for (i = 0; i < num_threads; i++) {
-               ret = pthread_create(mdres->threads + i, NULL, restore_worker,
+               ret = pthread_create(&mdres->threads[i], NULL, restore_worker,
                                     mdres);
-               if (ret)
+               if (ret) {
+                       /* pthread_create returns errno directly */
+                       ret = -ret;
                        break;
+               }
        }
        if (ret)
                mdrestore_destroy(mdres, i + 1);
@@ -1887,7 +1886,7 @@ static int fill_mdres_info(struct mdrestore_struct *mdres,
        int ret;
 
        /* We've already been initialized */
-       if (mdres->leafsize)
+       if (mdres->nodesize)
                return 0;
 
        if (mdres->compress_method == COMPRESS_ZLIB) {
@@ -1899,7 +1898,7 @@ static int fill_mdres_info(struct mdrestore_struct *mdres,
                ret = uncompress(buffer, (unsigned long *)&size,
                                 async->buffer, async->bufsize);
                if (ret != Z_OK) {
-                       fprintf(stderr, "Error decompressing %d\n", ret);
+                       error("decompressiion failed with %d", ret);
                        free(buffer);
                        return -EIO;
                }
@@ -1909,7 +1908,7 @@ static int fill_mdres_info(struct mdrestore_struct *mdres,
        }
 
        super = (struct btrfs_super_block *)outbuf;
-       mdres->leafsize = btrfs_super_leafsize(super);
+       mdres->nodesize = btrfs_super_nodesize(super);
        memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
        memcpy(mdres->uuid, super->dev_item.uuid,
                       BTRFS_UUID_SIZE);
@@ -1936,20 +1935,20 @@ static int add_cluster(struct meta_cluster *cluster,
                item = &cluster->items[i];
                async = calloc(1, sizeof(*async));
                if (!async) {
-                       fprintf(stderr, "Error allocating async\n");
+                       error("not enough memory for async data");
                        return -ENOMEM;
                }
                async->start = le64_to_cpu(item->bytenr);
                async->bufsize = le32_to_cpu(item->size);
                async->buffer = malloc(async->bufsize);
                if (!async->buffer) {
-                       fprintf(stderr, "Error allocing async buffer\n");
+                       error("not enough memory for async buffer");
                        free(async);
                        return -ENOMEM;
                }
                ret = fread(async->buffer, async->bufsize, 1, mdres->in);
                if (ret != 1) {
-                       fprintf(stderr, "Error reading buffer %d\n", errno);
+                       error("unable to read buffer: %s", strerror(errno));
                        free(async->buffer);
                        free(async);
                        return -EIO;
@@ -1960,7 +1959,7 @@ static int add_cluster(struct meta_cluster *cluster,
                if (async->start == BTRFS_SUPER_INFO_OFFSET) {
                        ret = fill_mdres_info(mdres, async);
                        if (ret) {
-                               fprintf(stderr, "Error setting up restore\n");
+                               error("unable to set up restore state");
                                pthread_mutex_unlock(&mdres->mutex);
                                free(async->buffer);
                                free(async);
@@ -1979,7 +1978,7 @@ static int add_cluster(struct meta_cluster *cluster,
                bytenr += size;
                ret = fread(buffer, size, 1, mdres->in);
                if (ret != 1) {
-                       fprintf(stderr, "Error reading in buffer %d\n", errno);
+                       error("failed to read buffer: %s", strerror(errno));
                        return -EIO;
                }
        }
@@ -2015,41 +2014,47 @@ static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
        int ret = 0;
        int i;
 
-       eb = alloc_dummy_eb(bytenr, mdres->leafsize);
+       eb = alloc_dummy_eb(bytenr, mdres->nodesize);
        if (!eb) {
                ret = -ENOMEM;
                goto out;
        }
 
        while (item_bytenr != bytenr) {
-               buffer += mdres->leafsize;
-               item_bytenr += mdres->leafsize;
+               buffer += mdres->nodesize;
+               item_bytenr += mdres->nodesize;
        }
 
-       memcpy(eb->data, buffer, mdres->leafsize);
+       memcpy(eb->data, buffer, mdres->nodesize);
        if (btrfs_header_bytenr(eb) != bytenr) {
-               fprintf(stderr, "Eb bytenr doesn't match found bytenr\n");
+               error("eb bytenr does not match found bytenr: %llu != %llu",
+                               (unsigned long long)btrfs_header_bytenr(eb),
+                               (unsigned long long)bytenr);
                ret = -EIO;
                goto out;
        }
 
        if (memcmp(mdres->fsid, eb->data + offsetof(struct btrfs_header, fsid),
                   BTRFS_FSID_SIZE)) {
-               fprintf(stderr, "Fsid doesn't match\n");
+               error("filesystem UUID of eb %llu does not match",
+                               (unsigned long long)bytenr);
                ret = -EIO;
                goto out;
        }
 
        if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID) {
-               fprintf(stderr, "Does not belong to the chunk tree\n");
+               error("wrong eb %llu owner %llu",
+                               (unsigned long long)bytenr,
+                               (unsigned long long)btrfs_header_owner(eb));
                ret = -EIO;
                goto out;
        }
 
        for (i = 0; i < btrfs_header_nritems(eb); i++) {
-               struct btrfs_chunk chunk;
+               struct btrfs_chunk *chunk;
                struct fs_chunk *fs_chunk;
                struct btrfs_key key;
+               u64 type;
 
                if (btrfs_header_level(eb)) {
                        u64 blockptr = btrfs_node_blockptr(eb, i);
@@ -2068,17 +2073,16 @@ static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
 
                fs_chunk = malloc(sizeof(struct fs_chunk));
                if (!fs_chunk) {
-                       fprintf(stderr, "Erorr allocating chunk\n");
+                       error("not enough memory to allocate chunk");
                        ret = -ENOMEM;
                        break;
                }
                memset(fs_chunk, 0, sizeof(*fs_chunk));
-               read_extent_buffer(eb, &chunk, btrfs_item_ptr_offset(eb, i),
-                                  sizeof(chunk));
+               chunk = btrfs_item_ptr(eb, i, struct btrfs_chunk);
 
                fs_chunk->logical = key.offset;
-               fs_chunk->physical = btrfs_stack_stripe_offset(&chunk.stripe);
-               fs_chunk->bytes = btrfs_stack_chunk_length(&chunk);
+               fs_chunk->physical = btrfs_stripe_offset_nr(eb, chunk, 0);
+               fs_chunk->bytes = btrfs_chunk_length(eb, chunk);
                INIT_LIST_HEAD(&fs_chunk->list);
                if (tree_search(&mdres->physical_tree, &fs_chunk->p,
                                physical_cmp, 1) != NULL)
@@ -2086,11 +2090,25 @@ static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
                else
                        tree_insert(&mdres->physical_tree, &fs_chunk->p,
                                    physical_cmp);
-               if (fs_chunk->physical + fs_chunk->bytes >
+
+               type = btrfs_chunk_type(eb, chunk);
+               if (type & BTRFS_BLOCK_GROUP_DUP) {
+                       fs_chunk->physical_dup =
+                                       btrfs_stripe_offset_nr(eb, chunk, 1);
+               }
+
+               if (fs_chunk->physical_dup + fs_chunk->bytes >
+                   mdres->last_physical_offset)
+                       mdres->last_physical_offset = fs_chunk->physical_dup +
+                               fs_chunk->bytes;
+               else if (fs_chunk->physical + fs_chunk->bytes >
                    mdres->last_physical_offset)
                        mdres->last_physical_offset = fs_chunk->physical +
                                fs_chunk->bytes;
                mdres->alloced_chunks += fs_chunk->bytes;
+               /* in dup case, fs_chunk->bytes should add twice */
+               if (fs_chunk->physical_dup)
+                       mdres->alloced_chunks += fs_chunk->bytes;
                tree_insert(&mdres->chunk_tree, &fs_chunk->l, chunk_cmp);
        }
 out:
@@ -2114,13 +2132,13 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
 
        cluster = malloc(BLOCK_SIZE);
        if (!cluster) {
-               fprintf(stderr, "Error allocating cluster\n");
+               error("not enough memory for cluster");
                return -ENOMEM;
        }
 
        buffer = malloc(max_size);
        if (!buffer) {
-               fprintf(stderr, "Error allocing buffer\n");
+               error("not enough memory for buffer");
                free(cluster);
                return -ENOMEM;
        }
@@ -2128,7 +2146,7 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
        if (mdres->compress_method == COMPRESS_ZLIB) {
                tmp = malloc(max_size);
                if (!tmp) {
-                       fprintf(stderr, "Error allocing tmp buffer\n");
+                       error("not enough memory for buffer");
                        free(cluster);
                        free(buffer);
                        return -ENOMEM;
@@ -2138,7 +2156,7 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
        bytenr = current_cluster;
        while (1) {
                if (fseek(mdres->in, current_cluster, SEEK_SET)) {
-                       fprintf(stderr, "Error seeking: %d\n", errno);
+                       error("seek failed: %s\n", strerror(errno));
                        ret = -EIO;
                        break;
                }
@@ -2151,11 +2169,15 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
                                bytenr = 0;
                                continue;
                        }
-                       printf("ok this is where we screwed up?\n");
+                       error(
+       "unknown state after reading cluster at %llu, probably crrupted data",
+                                       cluster_bytenr);
                        ret = -EIO;
                        break;
                } else if (ret < 0) {
-                       fprintf(stderr, "Error reading image\n");
+                       error("unable to read image at %llu: %s",
+                                       (unsigned long long)cluster_bytenr,
+                                       strerror(errno));
                        break;
                }
                ret = 0;
@@ -2163,7 +2185,7 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
                header = &cluster->header;
                if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
                    le64_to_cpu(header->bytenr) != current_cluster) {
-                       fprintf(stderr, "bad header in metadump image\n");
+                       error("bad header in metadump image");
                        ret = -EIO;
                        break;
                }
@@ -2178,8 +2200,8 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
                        item_bytenr = le64_to_cpu(item->bytenr);
 
                        if (bufsize > max_size) {
-                               fprintf(stderr, "item %u size %u too big\n",
-                                       i, bufsize);
+                               error("item %u too big: %u > %u", i, bufsize,
+                                               max_size);
                                ret = -EIO;
                                break;
                        }
@@ -2187,8 +2209,7 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
                        if (mdres->compress_method == COMPRESS_ZLIB) {
                                ret = fread(tmp, bufsize, 1, mdres->in);
                                if (ret != 1) {
-                                       fprintf(stderr, "Error reading: %d\n",
-                                               errno);
+                                       error("read error: %s", strerror(errno));
                                        ret = -EIO;
                                        break;
                                }
@@ -2198,16 +2219,16 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
                                                 (unsigned long *)&size, tmp,
                                                 bufsize);
                                if (ret != Z_OK) {
-                                       fprintf(stderr, "Error decompressing "
-                                               "%d\n", ret);
+                                       error("decompressiion failed with %d",
+                                                       ret);
                                        ret = -EIO;
                                        break;
                                }
                        } else {
                                ret = fread(buffer, bufsize, 1, mdres->in);
                                if (ret != 1) {
-                                       fprintf(stderr, "Error reading: %d\n",
-                                               errno);
+                                       error("read error: %s",
+                                                       strerror(errno));
                                        ret = -EIO;
                                        break;
                                }
@@ -2260,7 +2281,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
 
        ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
        if (ret <= 0) {
-               fprintf(stderr, "Error reading in cluster: %d\n", errno);
+               error("unable to read cluster: %s", strerror(errno));
                return -EIO;
        }
        ret = 0;
@@ -2268,7 +2289,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
        header = &cluster->header;
        if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
            le64_to_cpu(header->bytenr) != 0) {
-               fprintf(stderr, "bad header in metadump image\n");
+               error("bad header in metadump image");
                return -EIO;
        }
 
@@ -2282,25 +2303,26 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
                        break;
                bytenr += le32_to_cpu(item->size);
                if (fseek(mdres->in, le32_to_cpu(item->size), SEEK_CUR)) {
-                       fprintf(stderr, "Error seeking: %d\n", errno);
+                       error("seek failed: %s\n", strerror(errno));
                        return -EIO;
                }
        }
 
        if (!item || le64_to_cpu(item->bytenr) != BTRFS_SUPER_INFO_OFFSET) {
-               fprintf(stderr, "Huh, didn't find the super?\n");
+               error("did not find superblock at %llu",
+                               le64_to_cpu(item->bytenr));
                return -EINVAL;
        }
 
        buffer = malloc(le32_to_cpu(item->size));
        if (!buffer) {
-               fprintf(stderr, "Error allocing buffer\n");
+               error("not enough memory to allocate buffer");
                return -ENOMEM;
        }
 
        ret = fread(buffer, le32_to_cpu(item->size), 1, mdres->in);
        if (ret != 1) {
-               fprintf(stderr, "Error reading buffer: %d\n", errno);
+               error("unable to read buffer: %s", strerror(errno));
                free(buffer);
                return -EIO;
        }
@@ -2317,7 +2339,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
                ret = uncompress(tmp, (unsigned long *)&size,
                                 buffer, le32_to_cpu(item->size));
                if (ret != Z_OK) {
-                       fprintf(stderr, "Error decompressing %d\n", ret);
+                       error("decompressiion failed with %d", ret);
                        free(buffer);
                        free(tmp);
                        return -EIO;
@@ -2329,7 +2351,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
        pthread_mutex_lock(&mdres->mutex);
        super = (struct btrfs_super_block *)buffer;
        chunk_root_bytenr = btrfs_super_chunk_root(super);
-       mdres->leafsize = btrfs_super_leafsize(super);
+       mdres->nodesize = btrfs_super_nodesize(super);
        memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
        memcpy(mdres->uuid, super->dev_item.uuid,
                       BTRFS_UUID_SIZE);
@@ -2365,9 +2387,8 @@ static void remap_overlapping_chunks(struct mdrestore_struct *mdres)
                list_del_init(&fs_chunk->list);
                if (range_contains_super(fs_chunk->physical,
                                         fs_chunk->bytes)) {
-                       fprintf(stderr, "Remapping a chunk that had a super "
-                               "mirror inside of it, clearing space cache "
-                               "so we don't end up with corruption\n");
+                       warning(
+"remapping a chunk that had a super mirror inside of it, clearing space cache so we don't end up with corruption");
                        mdres->clear_space_cache = 1;
                }
                fs_chunk->physical = mdres->last_physical_offset;
@@ -2381,24 +2402,16 @@ static int fixup_devices(struct btrfs_fs_info *fs_info,
 {
        struct btrfs_trans_handle *trans;
        struct btrfs_dev_item *dev_item;
-       struct btrfs_path *path;
+       struct btrfs_path path;
        struct extent_buffer *leaf;
        struct btrfs_root *root = fs_info->chunk_root;
        struct btrfs_key key;
        u64 devid, cur_devid;
        int ret;
 
-       path = btrfs_alloc_path();
-       if (!path) {
-               fprintf(stderr, "Error alloc'ing path\n");
-               return -ENOMEM;
-       }
-
        trans = btrfs_start_transaction(fs_info->tree_root, 1);
        if (IS_ERR(trans)) {
-               fprintf(stderr, "Error starting transaction %ld\n",
-                       PTR_ERR(trans));
-               btrfs_free_path(path);
+               error("cannot starting transaction %ld", PTR_ERR(trans));
                return PTR_ERR(trans);
        }
 
@@ -2413,48 +2426,48 @@ static int fixup_devices(struct btrfs_fs_info *fs_info,
        key.type = BTRFS_DEV_ITEM_KEY;
        key.offset = 0;
 
+       btrfs_init_path(&path);
+
 again:
-       ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
+       ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
        if (ret < 0) {
-               fprintf(stderr, "search failed %d\n", ret);
+               error("search failed: %d", ret);
                exit(1);
        }
 
        while (1) {
-               leaf = path->nodes[0];
-               if (path->slots[0] >= btrfs_header_nritems(leaf)) {
-                       ret = btrfs_next_leaf(root, path);
+               leaf = path.nodes[0];
+               if (path.slots[0] >= btrfs_header_nritems(leaf)) {
+                       ret = btrfs_next_leaf(root, &path);
                        if (ret < 0) {
-                               fprintf(stderr, "Error going to next leaf "
-                                       "%d\n", ret);
+                               error("cannot go to next leaf %d", ret);
                                exit(1);
                        }
                        if (ret > 0) {
                                ret = 0;
                                break;
                        }
-                       leaf = path->nodes[0];
+                       leaf = path.nodes[0];
                }
 
-               btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+               btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
                if (key.type > BTRFS_DEV_ITEM_KEY)
                        break;
                if (key.type != BTRFS_DEV_ITEM_KEY) {
-                       path->slots[0]++;
+                       path.slots[0]++;
                        continue;
                }
 
-               dev_item = btrfs_item_ptr(leaf, path->slots[0],
+               dev_item = btrfs_item_ptr(leaf, path.slots[0],
                                          struct btrfs_dev_item);
                cur_devid = btrfs_device_id(leaf, dev_item);
                if (devid != cur_devid) {
-                       ret = btrfs_del_item(trans, root, path);
+                       ret = btrfs_del_item(trans, root, &path);
                        if (ret) {
-                               fprintf(stderr, "Error deleting item %d\n",
-                                       ret);
+                               error("cannot delete item: %d", ret);
                                exit(1);
                        }
-                       btrfs_release_path(path);
+                       btrfs_release_path(&path);
                        goto again;
                }
 
@@ -2462,13 +2475,13 @@ again:
                btrfs_set_device_bytes_used(leaf, dev_item,
                                            mdres->alloced_chunks);
                btrfs_mark_buffer_dirty(leaf);
-               path->slots[0]++;
+               path.slots[0]++;
        }
 
-       btrfs_free_path(path);
+       btrfs_release_path(&path);
        ret = btrfs_commit_transaction(trans, fs_info->tree_root);
        if (ret) {
-               fprintf(stderr, "Commit failed %d\n", ret);
+               error("unable to commit transaction: %d", ret);
                return ret;
        }
        return 0;
@@ -2491,20 +2504,20 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
        } else {
                in = fopen(input, "r");
                if (!in) {
-                       perror("unable to open metadump image");
+                       error("unable to open metadump image: %s",
+                                       strerror(errno));
                        return 1;
                }
        }
 
        /* NOTE: open with write mode */
        if (fixup_offset) {
-               BUG_ON(!target);
-               info = open_ctree_fs_info(target, 0, 0,
+               info = open_ctree_fs_info(target, 0, 0, 0,
                                          OPEN_CTREE_WRITES |
                                          OPEN_CTREE_RESTORE |
                                          OPEN_CTREE_PARTIAL);
                if (!info) {
-                       fprintf(stderr, "%s: open ctree failed\n", __func__);
+                       error("open ctree failed");
                        ret = -EIO;
                        goto failed_open;
                }
@@ -2512,7 +2525,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
 
        cluster = malloc(BLOCK_SIZE);
        if (!cluster) {
-               fprintf(stderr, "Error allocating cluster\n");
+               error("not enough memory for cluster");
                ret = -ENOMEM;
                goto failed_info;
        }
@@ -2520,7 +2533,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
        ret = mdrestore_init(&mdrestore, in, out, old_restore, num_threads,
                             fixup_offset, info, multi_devices);
        if (ret) {
-               fprintf(stderr, "Error initing mdrestore %d\n", ret);
+               error("failed to intialize metadata restore state: %d", ret);
                goto failed_cluster;
        }
 
@@ -2533,7 +2546,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
        }
 
        if (in != stdin && fseek(in, 0, SEEK_SET)) {
-               fprintf(stderr, "Error seeking %d\n", errno);
+               error("seek failed: %s\n", strerror(errno));
                goto out;
        }
 
@@ -2545,13 +2558,13 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
                header = &cluster->header;
                if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
                    le64_to_cpu(header->bytenr) != bytenr) {
-                       fprintf(stderr, "bad header in metadump image\n");
+                       error("bad header in metadump image");
                        ret = -EIO;
                        break;
                }
                ret = add_cluster(cluster, &mdrestore, &bytenr);
                if (ret) {
-                       fprintf(stderr, "Error adding cluster\n");
+                       error("failed to add cluster: %d", ret);
                        break;
                }
        }
@@ -2566,15 +2579,16 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
                                          OPEN_CTREE_WRITES |
                                          OPEN_CTREE_NO_DEVICES);
                if (!root) {
-                       fprintf(stderr, "unable to open %s\n", target);
+                       error("open ctree failed in %s", target);
                        ret = -EIO;
                        goto out;
                }
                info = root->fs_info;
 
                if (stat(target, &st)) {
-                       fprintf(stderr, "statting %s failed\n", target);
+                       error("stat %s failed: %s", target, strerror(errno));
                        close_ctree(info->chunk_root);
+                       free(cluster);
                        return 1;
                }
 
@@ -2608,8 +2622,8 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
        char fs_uuid[BTRFS_UUID_SIZE];
        u64 devid, type, io_align, io_width;
        u64 sector_size, total_bytes, bytes_used;
-       char *buf;
-       int fp;
+       char buf[BTRFS_SUPER_INFO_SIZE];
+       int fp = -1;
        int ret;
 
        key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
@@ -2619,8 +2633,9 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
        btrfs_init_path(&path);
        ret = btrfs_search_slot(NULL, info->chunk_root, &key, &path, 0, 0); 
        if (ret) {
-               fprintf(stderr, "search key fails\n");
-               exit(1);
+               error("search key failed: %d", ret);
+               ret = -EIO;
+               goto out;
        }
 
        leaf = path.nodes[0];
@@ -2629,8 +2644,11 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 
        devid = btrfs_device_id(leaf, dev_item);
        if (devid != cur_devid) {
-               printk("devid %llu mismatch with %llu\n", devid, cur_devid);
-               exit(1);
+               error("devid mismatch: %llu != %llu",
+                               (unsigned long long)devid,
+                               (unsigned long long)cur_devid);
+               ret = -EIO;
+               goto out;
        }
 
        type = btrfs_device_type(leaf, dev_item);
@@ -2644,20 +2662,14 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 
        btrfs_release_path(&path);
 
-       printk("update disk super on %s devid=%llu\n", other_dev, devid);
+       printf("update disk super on %s devid=%llu\n", other_dev, devid);
 
        /* update other devices' super block */
        fp = open(other_dev, O_CREAT | O_RDWR, 0600);
        if (fp < 0) {
-               fprintf(stderr, "could not open %s\n", other_dev);
-               exit(1);
-       }
-
-       buf = malloc(BTRFS_SUPER_INFO_SIZE);
-       if (!buf) {
-               ret = -ENOMEM;
-               close(fp);
-               return ret;
+               error("could not open %s: %s", other_dev, strerror(errno));
+               ret = -EIO;
+               goto out;
        }
 
        memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE);
@@ -2678,6 +2690,10 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 
        ret = pwrite64(fp, buf, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET);
        if (ret != BTRFS_SUPER_INFO_SIZE) {
+               if (ret < 0)
+                       error("cannot write superblock: %s", strerror(ret));
+               else
+                       error("cannot write superblock");
                ret = -EIO;
                goto out;
        }
@@ -2685,24 +2701,24 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
        write_backup_supers(fp, (u8 *)buf);
 
 out:
-       free(buf);
-       close(fp);
-       return 0;
+       if (fp != -1)
+               close(fp);
+       return ret;
 }
 
 static void print_usage(int ret)
 {
-       fprintf(stderr, "usage: btrfs-image [options] source target\n");
-       fprintf(stderr, "\t-r      \trestore metadump image\n");
-       fprintf(stderr, "\t-c value\tcompression level (0 ~ 9)\n");
-       fprintf(stderr, "\t-t value\tnumber of threads (1 ~ 32)\n");
-       fprintf(stderr, "\t-o      \tdon't mess with the chunk tree when restoring\n");
-       fprintf(stderr, "\t-s      \tsanitize file names, use once to just use garbage, use twice if you want crc collisions\n");
-       fprintf(stderr, "\t-w      \twalk all trees instead of using extent tree, do this if your extent tree is broken\n");
-       fprintf(stderr, "\t-m      \trestore for multiple devices\n");
-       fprintf(stderr, "\n");
-       fprintf(stderr, "\tIn the dump mode, source is the btrfs device and target is the output file (use '-' for stdout).\n");
-       fprintf(stderr, "\tIn the restore mode, source is the dumped image and target is the btrfs device/file.\n");
+       printf("usage: btrfs-image [options] source target\n");
+       printf("\t-r      \trestore metadump image\n");
+       printf("\t-c value\tcompression level (0 ~ 9)\n");
+       printf("\t-t value\tnumber of threads (1 ~ 32)\n");
+       printf("\t-o      \tdon't mess with the chunk tree when restoring\n");
+       printf("\t-s      \tsanitize file names, use once to just use garbage, use twice if you want crc collisions\n");
+       printf("\t-w      \twalk all trees instead of using extent tree, do this if your extent tree is broken\n");
+       printf("\t-m       \trestore for multiple devices\n");
+       printf("\n");
+       printf("\tIn the dump mode, source is the btrfs device and target is the output file (use '-' for stdout).\n");
+       printf("\tIn the restore mode, source is the dumped image and target is the btrfs device/file.\n");
        exit(ret);
 }
 
@@ -2710,7 +2726,7 @@ int main(int argc, char *argv[])
 {
        char *source;
        char *target;
-       u64 num_threads = 1;
+       u64 num_threads = 0;
        u64 compress_level = 0;
        int create = 1;
        int old_restore = 0;
@@ -2736,13 +2752,20 @@ int main(int argc, char *argv[])
                        break;
                case 't':
                        num_threads = arg_strtou64(optarg);
-                       if (num_threads > 32)
-                               print_usage(1);
+                       if (num_threads > MAX_WORKER_THREADS) {
+                               error("number of threads out of range: %llu > %d",
+                                       (unsigned long long)num_threads,
+                                       MAX_WORKER_THREADS);
+                               return 1;
+                       }
                        break;
                case 'c':
                        compress_level = arg_strtou64(optarg);
-                       if (compress_level > 9)
-                               print_usage(1);
+                       if (compress_level > 9) {
+                               error("compression level out of range: %llu",
+                                       (unsigned long long)compress_level);
+                               return 1;
+                       }
                        break;
                case 'o':
                        old_restore = 1;
@@ -2763,29 +2786,30 @@ int main(int argc, char *argv[])
                }
        }
 
-       argc = argc - optind;
        set_argv0(argv);
-       if (check_argc_min(argc, 2))
+       if (check_argc_min(argc - optind, 2))
                print_usage(1);
 
-       dev_cnt = argc - 1;
+       dev_cnt = argc - optind - 1;
 
        if (create) {
                if (old_restore) {
-                       fprintf(stderr, "Usage error: create and restore cannot be used at the same time\n");
+                       error(
+                       "create and restore cannot be used at the same time");
                        usage_error++;
                }
        } else {
                if (walk_trees || sanitize || compress_level) {
-                       fprintf(stderr, "Usage error: use -w, -s, -c options for restore makes no sense\n");
+                       error(
+                       "useing -w, -s, -c options for restore makes no sense");
                        usage_error++;
                }
                if (multi_devices && dev_cnt < 2) {
-                       fprintf(stderr, "Usage error: not enough devices specified for -m option\n");
+                       error("not enough devices specified for -m option");
                        usage_error++;
                }
                if (!multi_devices && dev_cnt != 1) {
-                       fprintf(stderr, "Usage error: accepts only 1 device without -m option\n");
+                       error("accepts only 1 device without -m option");
                        usage_error++;
                }
        }
@@ -2801,26 +2825,32 @@ int main(int argc, char *argv[])
        } else {
                out = fopen(target, "w+");
                if (!out) {
-                       perror("unable to create target file");
+                       error("unable to create target file %s", target);
                        exit(1);
                }
        }
 
-       if (num_threads == 1 && compress_level > 0) {
-               num_threads = sysconf(_SC_NPROCESSORS_ONLN);
-               if (num_threads <= 0)
-                       num_threads = 1;
+       if (compress_level > 0 || create == 0) {
+               if (num_threads == 0) {
+                       long tmp = sysconf(_SC_NPROCESSORS_ONLN);
+
+                       if (tmp <= 0)
+                               tmp = 1;
+                       num_threads = tmp;
+               }
+       } else {
+               num_threads = 0;
        }
 
        if (create) {
                ret = check_mounted(source);
                if (ret < 0) {
-                       fprintf(stderr, "Could not check mount status: %s\n",
-                               strerror(-ret));
-                       exit(1);
-               } else if (ret)
-                       fprintf(stderr,
-               "WARNING: The device is mounted. Make sure the filesystem is quiescent.\n");
+                       warning("unable to check mount status of: %s",
+                                       strerror(-ret));
+               } else if (ret) {
+                       warning("%s already mounted, results may be inaccurate",
+                                       source);
+               }
 
                ret = create_metadump(source, out, num_threads,
                                      compress_level, sanitize, walk_trees);
@@ -2829,7 +2859,7 @@ int main(int argc, char *argv[])
                                       0, target, multi_devices);
        }
        if (ret) {
-               printk("%s failed (%s)\n", (create) ? "create" : "restore",
+               error("%s failed: %s", (create) ? "create" : "restore",
                       strerror(errno));
                goto out;
        }
@@ -2840,19 +2870,17 @@ int main(int argc, char *argv[])
                u64 total_devs;
                int i;
 
-               info = open_ctree_fs_info(target, 0, 0,
+               info = open_ctree_fs_info(target, 0, 0, 0,
                                          OPEN_CTREE_PARTIAL |
                                          OPEN_CTREE_RESTORE);
                if (!info) {
-                       int e = errno;
-                       fprintf(stderr, "unable to open %s error = %s\n",
-                               target, strerror(e));
+                       error("open ctree failed at %s", target);
                        return 1;
                }
 
                total_devs = btrfs_super_num_devices(info->super_copy);
                if (total_devs != dev_cnt) {
-                       printk("it needs %llu devices but has only %d\n",
+                       error("it needs %llu devices but has only %d",
                                total_devs, dev_cnt);
                        close_ctree(info->chunk_root);
                        goto out;
@@ -2863,7 +2891,7 @@ int main(int argc, char *argv[])
                        ret = update_disk_super_on_device(info,
                                        argv[optind + i], (u64)i);
                        if (ret) {
-                               printk("update disk super failed devid=%d (error=%d)\n",
+                               error("update disk superblock failed devid %d: %d",
                                        i, ret);
                                close_ctree(info->chunk_root);
                                exit(1);
@@ -2876,8 +2904,7 @@ int main(int argc, char *argv[])
                ret = restore_metadump(source, out, 0, num_threads, 1,
                                       target, 1);
                if (ret) {
-                       fprintf(stderr, "fix metadump failed (error=%d)\n",
-                               ret);
+                       error("unable to fixup metadump: %d", ret);
                        exit(1);
                }
        }
@@ -2891,11 +2918,12 @@ out:
 
                        unlink_ret = unlink(target);
                        if (unlink_ret)
-                               fprintf(stderr,
-                                       "unlink output file failed : %s\n",
-                                       strerror(errno));
+                               error("unlink output file %s failed: %s",
+                                               target, strerror(errno));
                }
        }
 
+       btrfs_close_all_devices();
+
        return !!ret;
 }