btrfs-progs: fix error checking in load_device_info
[platform/upstream/btrfs-progs.git] / btrfs-convert.c
index 58468ed..802930c 100644 (file)
@@ -71,10 +71,8 @@ static void *print_copied_inodes(void *p)
 
 static int after_copied_inodes(void *p)
 {
-       struct task_ctx *priv = p;
-
        printf("\n");
-       task_period_stop(priv->info);
+       fflush(stdout);
 
        return 0;
 }
@@ -207,7 +205,8 @@ static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
 }
 
 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
-                              u64 hint_byte, struct btrfs_key *ins)
+                              u64 hint_byte, struct btrfs_key *ins,
+                              int metadata)
 {
        u64 start;
        u64 end;
@@ -246,6 +245,14 @@ static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
                        continue;
                }
 
+               if (metadata) {
+                       BUG_ON(num_bytes != root->nodesize);
+                       if (check_crossing_stripes(start, num_bytes)) {
+                               last = round_down(start + num_bytes,
+                                                 BTRFS_STRIPE_LEN);
+                               continue;
+                       }
+               }
                clear_extent_dirty(&root->fs_info->free_space_cache,
                                   start, start + num_bytes - 1, 0);
 
@@ -286,6 +293,35 @@ static struct btrfs_extent_ops extent_ops = {
        .free_extent = custom_free_extent,
 };
 
+static int convert_insert_dirent(struct btrfs_trans_handle *trans,
+                                struct btrfs_root *root,
+                                const char *name, size_t name_len,
+                                u64 dir, u64 objectid,
+                                u8 file_type, u64 index_cnt,
+                                struct btrfs_inode_item *inode)
+{
+       int ret;
+       u64 inode_size;
+       struct btrfs_key location = {
+               .objectid = objectid,
+               .offset = 0,
+               .type = BTRFS_INODE_ITEM_KEY,
+       };
+
+       ret = btrfs_insert_dir_item(trans, root, name, name_len,
+                                   dir, &location, file_type, index_cnt);
+       if (ret)
+               return ret;
+       ret = btrfs_insert_inode_ref(trans, root, name, name_len,
+                                    objectid, dir, index_cnt);
+       if (ret)
+               return ret;
+       inode_size = btrfs_stack_inode_size(inode) + name_len * 2;
+       btrfs_set_stack_inode_size(inode, inode_size);
+
+       return 0;
+}
+
 struct dir_iterate_data {
        struct btrfs_trans_handle *trans;
        struct btrfs_root *root;
@@ -315,9 +351,7 @@ static int dir_iterate_proc(ext2_ino_t dir, int entry,
        int ret;
        int file_type;
        u64 objectid;
-        u64 inode_size;
        char dotdot[] = "..";
-       struct btrfs_key location;
        struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
        int name_len;
 
@@ -334,33 +368,20 @@ static int dir_iterate_proc(ext2_ino_t dir, int entry,
        if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
                return 0;
 
-       location.objectid = objectid;
-       location.offset = 0;
-       btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
-
        file_type = dirent->name_len >> 8;
        BUG_ON(file_type > EXT2_FT_SYMLINK);
-       ret = btrfs_insert_dir_item(idata->trans, idata->root,
-                                   dirent->name, name_len,
-                                   idata->objectid, &location,
+
+       ret = convert_insert_dirent(idata->trans, idata->root, dirent->name,
+                                   name_len, idata->objectid, objectid,
                                    filetype_conversion_table[file_type],
-                                   idata->index_cnt);
-       if (ret)
-               goto fail;
-       ret = btrfs_insert_inode_ref(idata->trans, idata->root,
-                                    dirent->name, name_len,
-                                    objectid, idata->objectid,
-                                    idata->index_cnt);
-       if (ret)
-               goto fail;
+                                   idata->index_cnt, idata->inode);
+       if (ret < 0) {
+               idata->errcode = ret;
+               return BLOCK_ABORT;
+       }
+
        idata->index_cnt++;
-       inode_size = btrfs_stack_inode_size(idata->inode) +
-                    name_len * 2;
-       btrfs_set_stack_inode_size(idata->inode, inode_size);
        return 0;
-fail:
-       idata->errcode = ret;
-       return BLOCK_ABORT;
 }
 
 static int create_dir_entries(struct btrfs_trans_handle *trans,
@@ -493,7 +514,7 @@ static int record_file_blocks(struct blk_iterate_data *data,
 static int block_iterate_proc(u64 disk_block, u64 file_block,
                              struct blk_iterate_data *idata)
 {
-       int ret;
+       int ret = 0;
        int sb_region;
        int do_barrier;
        struct btrfs_root *root = idata->root;
@@ -535,19 +556,23 @@ static int block_iterate_proc(u64 disk_block, u64 file_block,
                idata->boundary = bytenr / root->sectorsize;
        }
        idata->num_blocks++;
-       return 0;
 fail:
-       idata->errcode = ret;
-       return BLOCK_ABORT;
+       return ret;
 }
 
 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
                                e2_blkcnt_t blockcnt, blk_t ref_block,
                                int ref_offset, void *priv_data)
 {
+       int ret;
        struct blk_iterate_data *idata;
        idata = (struct blk_iterate_data *)priv_data;
-       return block_iterate_proc(*blocknr, blockcnt, idata);
+       ret = block_iterate_proc(*blocknr, blockcnt, idata);
+       if (ret) {
+               idata->errcode = ret;
+               return BLOCK_ABORT;
+       }
+       return 0;
 }
 
 /*
@@ -991,6 +1016,8 @@ static int copy_inode_item(struct btrfs_inode_item *dst,
                           struct ext2_inode *src, u32 blocksize)
 {
        btrfs_set_stack_inode_generation(dst, 1);
+       btrfs_set_stack_inode_sequence(dst, 0);
+       btrfs_set_stack_inode_transid(dst, 1);
        btrfs_set_stack_inode_size(dst, src->i_size);
        btrfs_set_stack_inode_nbytes(dst, 0);
        btrfs_set_stack_inode_block_group(dst, 0);
@@ -1027,6 +1054,8 @@ static int copy_inode_item(struct btrfs_inode_item *dst,
                                new_decode_dev(src->i_block[1]));
                }
        }
+       memset(&dst->reserved, 0, sizeof(dst->reserved));
+
        return 0;
 }
 
@@ -1157,6 +1186,7 @@ static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
        }
        ret = btrfs_commit_transaction(trans, root);
        BUG_ON(ret);
+       ext2fs_close_inode_scan(ext2_scan);
 
        return ret;
 }
@@ -1188,10 +1218,8 @@ static int create_image_file_range(struct btrfs_trans_handle *trans,
                if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
                        continue;
                ret = block_iterate_proc(block, block, &data);
-               if (ret & BLOCK_ABORT) {
-                       ret = data.errcode;
+               if (ret < 0)
                        goto fail;
-               }
        }
        if (data.num_blocks > 0) {
                ret = record_file_blocks(&data, data.first_block,
@@ -1264,7 +1292,7 @@ static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
         * special, we can't rely on relocate_extents_range to relocate it.
         */
        for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
-               ret = custom_alloc_extent(root, sectorsize, 0, &key);
+               ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
                if (ret)
                        goto fail;
                ret = copy_disk_extent(root, key.objectid, last_byte,
@@ -1922,7 +1950,7 @@ static int relocate_one_reference(struct btrfs_trans_handle *trans,
                        ret = get_state_private(reloc_tree, bytenr, &new_pos);
                        BUG_ON(ret);
                } else {
-                       ret = custom_alloc_extent(root, sectorsize, 0, &key);
+                       ret = custom_alloc_extent(root, sectorsize, 0, &key, 0);
                        if (ret)
                                goto fail;
                        new_pos = key.objectid;
@@ -1944,10 +1972,8 @@ static int relocate_one_reference(struct btrfs_trans_handle *trans,
 
                ret = block_iterate_proc(new_pos / sectorsize,
                                         cur_offset / sectorsize, &data);
-               if (ret & BLOCK_ABORT) {
-                       ret = data.errcode;
+               if (ret < 0)
                        goto fail;
-               }
 
                cur_offset += sectorsize;
                bytenr += sectorsize;
@@ -2275,6 +2301,7 @@ static int do_convert(const char *devname, int datacsum, int packing, int noxatt
        struct btrfs_root *image_root;
        struct task_ctx ctx;
        char features_buf[64];
+       struct btrfs_mkfs_config mkfs_cfg;
 
        ret = open_ext2fs(devname, &ext2_fs);
        if (ret) {
@@ -2292,7 +2319,7 @@ static int do_convert(const char *devname, int datacsum, int packing, int noxatt
                fprintf(stderr, "filetype feature is missing\n");
                goto fail;
        }
-       if (btrfs_check_nodesize(nodesize, blocksize))
+       if (btrfs_check_nodesize(nodesize, blocksize, features))
                goto fail;
        blocks_per_node = nodesize / blocksize;
        ret = -blocks_per_node;
@@ -2323,9 +2350,17 @@ static int do_convert(const char *devname, int datacsum, int packing, int noxatt
        printf("\tblocksize: %u\n", blocksize);
        printf("\tnodesize:  %u\n", nodesize);
        printf("\tfeatures:  %s\n", features_buf);
-       ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
-                        NULL, blocks, total_bytes, nodesize,
-                        blocksize, blocksize, features);
+
+       mkfs_cfg.label = ext2_fs->super->s_volume_name;
+       mkfs_cfg.fs_uuid = NULL;
+       memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
+       mkfs_cfg.num_bytes = total_bytes;
+       mkfs_cfg.nodesize = nodesize;
+       mkfs_cfg.sectorsize = blocksize;
+       mkfs_cfg.stripesize = blocksize;
+       mkfs_cfg.features = features;
+
+       ret = make_btrfs(fd, &mkfs_cfg);
        if (ret) {
                fprintf(stderr, "unable to create initial ctree: %s\n",
                        strerror(-ret));
@@ -2398,7 +2433,7 @@ static int do_convert(const char *devname, int datacsum, int packing, int noxatt
                fprintf(stderr, "copy label '%s'\n",
                                root->fs_info->super_copy->label);
        } else if (copylabel == -1) {
-               strncpy(root->fs_info->super_copy->label, fslabel, BTRFS_LABEL_SIZE);
+               strcpy(root->fs_info->super_copy->label, fslabel);
                fprintf(stderr, "set label to '%s'\n", fslabel);
        }
 
@@ -2838,7 +2873,7 @@ int main(int argc, char *argv[])
        int usage_error = 0;
        int progress = 1;
        char *file;
-       char *fslabel = NULL;
+       char fslabel[BTRFS_LABEL_SIZE];
        u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
 
        while(1) {
@@ -2855,6 +2890,7 @@ int main(int argc, char *argv[])
                        { "label", required_argument, NULL, 'l' },
                        { "copy-label", no_argument, NULL, 'L' },
                        { "nodesize", required_argument, NULL, 'N' },
+                       { "help", no_argument, NULL, GETOPT_VAL_HELP},
                        { NULL, 0, NULL, 0 }
                };
                int c = getopt_long(argc, argv, "dinN:rl:LpO:", long_options, NULL);
@@ -2879,13 +2915,13 @@ int main(int argc, char *argv[])
                                break;
                        case 'l':
                                copylabel = -1;
-                               fslabel = strdup(optarg);
-                               if (strlen(fslabel) > BTRFS_LABEL_SIZE) {
+                               if (strlen(optarg) >= BTRFS_LABEL_SIZE) {
                                        fprintf(stderr,
-                                               "warning: label too long, trimmed to %d bytes\n",
-                                               BTRFS_LABEL_SIZE);
-                                       fslabel[BTRFS_LABEL_SIZE] = 0;
+                               "WARNING: label too long, trimmed to %d bytes\n",
+                                               BTRFS_LABEL_SIZE - 1);
                                }
+                               strncpy(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
+                               fslabel[BTRFS_LABEL_SIZE - 1] = 0;
                                break;
                        case 'L':
                                copylabel = 1;
@@ -2927,9 +2963,10 @@ int main(int argc, char *argv[])
                        case GETOPT_VAL_NO_PROGRESS:
                                progress = 0;
                                break;
+                       case GETOPT_VAL_HELP:
                        default:
                                print_usage();
-                               return 1;
+                               return c != GETOPT_VAL_HELP;
                }
        }
        argc = argc - optind;