btrfs-progs: image: switch to common message helpers
authorDavid Sterba <dsterba@suse.com>
Thu, 8 Sep 2016 13:57:42 +0000 (15:57 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 3 Oct 2016 09:33:15 +0000 (11:33 +0200)
Signed-off-by: David Sterba <dsterba@suse.com>
btrfs-image.c

index 953d368..516834e 100644 (file)
@@ -354,7 +354,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;
        }
@@ -365,7 +365,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;
@@ -404,8 +404,8 @@ 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_range(94) + 33;
@@ -445,8 +445,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);
@@ -455,7 +454,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);
@@ -500,8 +499,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);
@@ -511,7 +509,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);
@@ -543,7 +541,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;
        }
 
@@ -673,7 +671,7 @@ static void *dump_worker(void *data)
                        async->bufsize = compressBound(async->size);
                        async->buffer = malloc(async->bufsize);
                        if (!async->buffer) {
-                               fprintf(stderr, "Error allocating buffer\n");
+                               error("not enough memory for async buffer");
                                pthread_mutex_lock(&md->mutex);
                                if (!md->error)
                                        md->error = -ENOMEM;
@@ -820,8 +818,7 @@ 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;
        }
 
@@ -836,8 +833,8 @@ static int write_buffers(struct metadump_struct *md, u64 *next)
 
        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 */
@@ -852,10 +849,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);
@@ -869,9 +866,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:
@@ -969,8 +966,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;
@@ -982,8 +981,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);
@@ -1013,8 +1012,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);
        }
@@ -1091,7 +1089,8 @@ static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
 
        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;
        }
 
@@ -1109,8 +1108,7 @@ static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
                        bytenr = btrfs_disk_root_bytenr(eb, ri);
                        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);
@@ -1121,7 +1119,7 @@ static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
                        bytenr = btrfs_node_blockptr(eb, i);
                        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);
@@ -1145,7 +1143,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;
        }
 
@@ -1171,8 +1169,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;
        }
 
@@ -1182,8 +1179,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)
@@ -1209,8 +1205,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;
                }
@@ -1239,7 +1234,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;
@@ -1250,8 +1245,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) {
@@ -1283,8 +1277,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;
                                }
                        }
@@ -1292,8 +1286,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;
                        }
 
@@ -1301,15 +1295,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
@@ -1333,14 +1327,14 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
 
        root = open_ctree(input, 0, 0);
        if (!root) {
-               fprintf(stderr, "Open ctree failed\n");
+               error("open ctree failed");
                return -EIO;
        }
 
        ret = metadump_init(&metadump, root, out, num_threads,
                            compress_level, sanitize);
        if (ret) {
-               fprintf(stderr, "Error initializing metadump %d\n", ret);
+               error("failed to initialize metadump: %d", ret);
                close_ctree(root);
                return ret;
        }
@@ -1348,14 +1342,14 @@ 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 allocating path\n");
+               error("not enough memory to allocate path");
                err = -ENOMEM;
                goto out;
        }
@@ -1394,7 +1388,7 @@ out:
        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);
@@ -1494,8 +1488,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);
@@ -1659,8 +1652,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;
        }
 
@@ -1675,11 +1669,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;
                }
        }
@@ -1699,7 +1693,7 @@ static void *restore_worker(void *data)
        outfd = fileno(mdres->out);
        buffer = malloc(compress_size);
        if (!buffer) {
-               fprintf(stderr, "Error allocating buffer\n");
+               error("not enough memory for restore worker buffer");
                pthread_mutex_lock(&mdres->mutex);
                if (!mdres->error)
                        mdres->error = -ENOMEM;
@@ -1729,8 +1723,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;
@@ -1785,18 +1778,18 @@ static void *restore_worker(void *data)
 
 error:
                                if (ret < 0) {
-                                       fprintf(stderr, "Error writing to device %d\n",
-                                                       errno);
+                                       error("unable to write to device: %s",
+                                                       strerror(errno));
                                        err = errno;
                                } else {
-                                       fprintf(stderr, "Short write\n");
+                                       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);
                        }
                }
@@ -1908,7 +1901,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;
                }
@@ -1945,20 +1938,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 allocating 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;
@@ -1969,7 +1962,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);
@@ -1988,7 +1981,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;
                }
        }
@@ -2037,20 +2030,25 @@ static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
 
        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;
        }
@@ -2078,7 +2076,7 @@ static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
 
                fs_chunk = malloc(sizeof(struct fs_chunk));
                if (!fs_chunk) {
-                       fprintf(stderr, "Error allocating chunk\n");
+                       error("not enough memory to allocate chunk");
                        ret = -ENOMEM;
                        break;
                }
@@ -2137,13 +2135,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 allocating buffer\n");
+               error("not enough memory for buffer");
                free(cluster);
                return -ENOMEM;
        }
@@ -2151,7 +2149,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 allocating tmp buffer\n");
+                       error("not enough memory for buffer");
                        free(cluster);
                        free(buffer);
                        return -ENOMEM;
@@ -2161,7 +2159,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;
                }
@@ -2174,11 +2172,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;
@@ -2186,7 +2188,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;
                }
@@ -2201,8 +2203,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;
                        }
@@ -2210,8 +2212,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;
                                }
@@ -2221,16 +2222,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;
                                }
@@ -2283,7 +2284,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;
@@ -2291,7 +2292,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;
        }
 
@@ -2305,25 +2306,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 allocating 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;
        }
@@ -2340,7 +2342,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;
@@ -2388,7 +2390,7 @@ 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 "
+                       printf("remapping a chunk that had a super "
                                "mirror inside of it, clearing space cache "
                                "so we don't end up with corruption\n");
                        mdres->clear_space_cache = 1;
@@ -2413,14 +2415,13 @@ static int fixup_devices(struct btrfs_fs_info *fs_info,
 
        path = btrfs_alloc_path();
        if (!path) {
-               fprintf(stderr, "Error allocating path\n");
+               error("not enough memory to allocate path");
                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));
+               error("cannot starting transaction %ld", PTR_ERR(trans));
                btrfs_free_path(path);
                return PTR_ERR(trans);
        }
@@ -2439,7 +2440,7 @@ static int fixup_devices(struct btrfs_fs_info *fs_info,
 again:
        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);
        }
 
@@ -2448,8 +2449,7 @@ again:
                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) {
@@ -2473,8 +2473,7 @@ again:
                if (devid != cur_devid) {
                        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);
@@ -2491,7 +2490,7 @@ again:
        btrfs_free_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;
@@ -2514,7 +2513,8 @@ 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;
                }
        }
@@ -2527,7 +2527,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
                                          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;
                }
@@ -2535,7 +2535,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;
        }
@@ -2543,7 +2543,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 initializing mdrestore %d\n", ret);
+               error("failed to intialize metadata restore state: %d", ret);
                goto failed_cluster;
        }
 
@@ -2556,7 +2556,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;
        }
 
@@ -2568,13 +2568,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;
                }
        }
@@ -2589,14 +2589,14 @@ 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;
@@ -2643,7 +2643,7 @@ 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, "ERROR: search key failed\n");
+               error("search key failed: %d", ret);
                ret = -EIO;
                goto out;
        }
@@ -2654,7 +2654,9 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 
        devid = btrfs_device_id(leaf, dev_item);
        if (devid != cur_devid) {
-               printk("ERROR: devid %llu mismatch with %llu\n", devid, cur_devid);
+               error("devid mismatch: %llu != %llu",
+                               (unsigned long long)devid,
+                               (unsigned long long)cur_devid);
                ret = -EIO;
                goto out;
        }
@@ -2670,12 +2672,12 @@ 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, "ERROR: could not open %s\n", other_dev);
+               error("could not open %s: %s", other_dev, strerror(errno));
                ret = -EIO;
                goto out;
        }
@@ -2699,9 +2701,9 @@ 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)
-                       fprintf(stderr, "ERROR: cannot write superblock: %s\n", strerror(ret));
+                       error("cannot write superblock: %s", strerror(ret));
                else
-                       fprintf(stderr, "ERROR: cannot write superblock\n");
+                       error("cannot write superblock");
                ret = -EIO;
                goto out;
        }
@@ -2801,20 +2803,24 @@ int main(int argc, char *argv[])
 
        if (create) {
                if (old_restore) {
-                       fprintf(stderr, "Usage error: create and restore cannot be used at the same time\n");
+                       fprintf(stderr,
+       "Usage error: create and restore cannot be used at the same time\n");
                        usage_error++;
                }
        } else {
                if (walk_trees || sanitize || compress_level) {
-                       fprintf(stderr, "Usage error: use -w, -s, -c options for restore makes no sense\n");
+                       fprintf(stderr,
+       "Usage error: use -w, -s, -c options for restore makes no sense\n");
                        usage_error++;
                }
                if (multi_devices && dev_cnt < 2) {
-                       fprintf(stderr, "Usage error: not enough devices specified for -m option\n");
+                       fprintf(stderr,
+       "Usage error: not enough devices specified for -m option\n");
                        usage_error++;
                }
                if (!multi_devices && dev_cnt != 1) {
-                       fprintf(stderr, "Usage error: accepts only 1 device without -m option\n");
+                       fprintf(stderr,
+       "Usage error: accepts only 1 device without -m option\n");
                        usage_error++;
                }
        }
@@ -2830,7 +2836,7 @@ 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);
                }
        }
@@ -2850,12 +2856,12 @@ int main(int argc, char *argv[])
        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);
@@ -2864,7 +2870,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;
        }
@@ -2879,14 +2885,13 @@ int main(int argc, char *argv[])
                                          OPEN_CTREE_PARTIAL |
                                          OPEN_CTREE_RESTORE);
                if (!info) {
-                       fprintf(stderr, "unable to open %s error = %s\n",
-                               target, strerror(errno));
+                       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;
@@ -2897,7 +2902,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);
@@ -2910,8 +2915,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);
                }
        }
@@ -2925,9 +2929,8 @@ 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));
                }
        }