btrfs-progs: typo review of strings and comments
[platform/upstream/btrfs-progs.git] / btrfs-image.c
index f48d81a..8a1b799 100644 (file)
@@ -133,7 +133,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;
@@ -657,7 +657,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");
+                               fprintf(stderr, "Error allocating buffer\n");
                                pthread_mutex_lock(&md->mutex);
                                if (!md->error)
                                        md->error = -ENOMEM;
@@ -733,39 +733,33 @@ 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);
+       md->cluster = calloc(1, BLOCK_SIZE);
+       if (!md->cluster)
+               return -ENOMEM;
+       md->threads = calloc(num_threads, sizeof(pthread_t));
+       if (!md->threads) {
+               free(md->cluster);
+               return -ENOMEM;
+       }
        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)
@@ -1079,7 +1073,7 @@ 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");
                return ret;
@@ -1097,7 +1091,7 @@ 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");
@@ -1109,7 +1103,7 @@ 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");
                                return -EIO;
@@ -1261,7 +1255,7 @@ 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;
+                       num_bytes = extent_root->nodesize;
                else
                        num_bytes = key.offset;
 
@@ -1327,12 +1321,10 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
                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);
+               fprintf(stderr, "Error initializing metadump %d\n", ret);
                close_ctree(root);
                return ret;
        }
@@ -1347,7 +1339,7 @@ static int create_metadump(const char *input, FILE *out, int num_threads,
 
        path = btrfs_alloc_path();
        if (!path) {
-               fprintf(stderr, "Out of memory allocing path\n");
+               fprintf(stderr, "Out of memory allocating path\n");
                err = -ENOMEM;
                goto out;
        }
@@ -1470,8 +1462,7 @@ static int update_super(struct mdrestore_struct *mdres, u8 *buffer)
                        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);
+                       chunk->stripe.devid = super->dev_item.devid;
                        physical = logical_to_physical(mdres, key.offset,
                                                       &size);
                        if (size != (u64)-1)
@@ -1505,10 +1496,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;
@@ -1558,16 +1548,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;
@@ -1622,9 +1612,9 @@ static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
                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);
@@ -1681,7 +1671,7 @@ static void *restore_worker(void *data)
        outfd = fileno(mdres->out);
        buffer = malloc(compress_size);
        if (!buffer) {
-               fprintf(stderr, "Error allocing buffer\n");
+               fprintf(stderr, "Error allocating buffer\n");
                pthread_mutex_lock(&mdres->mutex);
                if (!mdres->error)
                        mdres->error = -ENOMEM;
@@ -1695,7 +1685,7 @@ static void *restore_worker(void *data)
                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;
@@ -1867,7 +1857,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) {
@@ -1889,7 +1879,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);
@@ -1923,7 +1913,7 @@ static int add_cluster(struct meta_cluster *cluster,
                async->bufsize = le32_to_cpu(item->size);
                async->buffer = malloc(async->bufsize);
                if (!async->buffer) {
-                       fprintf(stderr, "Error allocing async buffer\n");
+                       fprintf(stderr, "Error allocating async buffer\n");
                        free(async);
                        return -ENOMEM;
                }
@@ -1995,18 +1985,18 @@ 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");
                ret = -EIO;
@@ -2048,7 +2038,7 @@ 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");
+                       fprintf(stderr, "Error allocating chunk\n");
                        ret = -ENOMEM;
                        break;
                }
@@ -2100,7 +2090,7 @@ static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
 
        buffer = malloc(max_size);
        if (!buffer) {
-               fprintf(stderr, "Error allocing buffer\n");
+               fprintf(stderr, "Error allocating buffer\n");
                free(cluster);
                return -ENOMEM;
        }
@@ -2108,7 +2098,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");
+                       fprintf(stderr, "Error allocating tmp buffer\n");
                        free(cluster);
                        free(buffer);
                        return -ENOMEM;
@@ -2274,7 +2264,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
 
        buffer = malloc(le32_to_cpu(item->size));
        if (!buffer) {
-               fprintf(stderr, "Error allocing buffer\n");
+               fprintf(stderr, "Error allocating buffer\n");
                return -ENOMEM;
        }
 
@@ -2309,7 +2299,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);
@@ -2370,7 +2360,7 @@ static int fixup_devices(struct btrfs_fs_info *fs_info,
 
        path = btrfs_alloc_path();
        if (!path) {
-               fprintf(stderr, "Error alloc'ing path\n");
+               fprintf(stderr, "Error allocating path\n");
                return -ENOMEM;
        }
 
@@ -2479,7 +2469,7 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
        /* 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);
@@ -2500,7 +2490,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);
+               fprintf(stderr, "Error initializing mdrestore %d\n", ret);
                goto failed_cluster;
        }
 
@@ -2588,8 +2578,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;
@@ -2599,8 +2589,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);
+               fprintf(stderr, "ERROR: search key failed\n");
+               ret = -EIO;
+               goto out;
        }
 
        leaf = path.nodes[0];
@@ -2609,8 +2600,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("devid %llu mismatch with %llu\n", devid, cur_devid);
-               exit(1);
+               printk("ERROR: devid %llu mismatch with %llu\n", devid, cur_devid);
+               ret = -EIO;
+               goto out;
        }
 
        type = btrfs_device_type(leaf, dev_item);
@@ -2629,15 +2621,9 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
        /* 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;
+               fprintf(stderr, "ERROR: could not open %s\n", other_dev);
+               ret = -EIO;
+               goto out;
        }
 
        memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE);
@@ -2658,6 +2644,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)
+                       fprintf(stderr, "ERROR: cannot write superblock: %s\n", strerror(ret));
+               else
+                       fprintf(stderr, "ERROR: cannot write superblock\n");
                ret = -EIO;
                goto out;
        }
@@ -2665,9 +2655,9 @@ 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)
@@ -2690,7 +2680,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;
@@ -2743,12 +2733,11 @@ 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) {
@@ -2786,10 +2775,16 @@ int main(int argc, char *argv[])
                }
        }
 
-       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) {
@@ -2820,13 +2815,12 @@ 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));
+                               target, strerror(errno));
                        return 1;
                }
 
@@ -2877,5 +2871,7 @@ out:
                }
        }
 
+       btrfs_close_all_devices();
+
        return !!ret;
 }