btrfs-progs: handle errors from btrfs_alloc_path
[platform/upstream/btrfs-progs.git] / btrfs-convert.c
index 9e04805..5175f1f 100644 (file)
@@ -2325,7 +2325,6 @@ static int do_convert(const char *devname, int datacsum, int packing,
 {
        int ret;
        int fd = -1;
-       int is_btrfs = 0;
        u32 blocksize;
        u64 total_bytes;
        struct btrfs_root *root;
@@ -2374,10 +2373,8 @@ static int do_convert(const char *devname, int datacsum, int packing,
        mkfs_cfg.stripesize = blocksize;
        mkfs_cfg.features = features;
        /* New convert need these space */
-       mkfs_cfg.fs_uuid = malloc(BTRFS_UUID_UNPARSED_SIZE);
-       mkfs_cfg.chunk_uuid = malloc(BTRFS_UUID_UNPARSED_SIZE);
-       *(mkfs_cfg.fs_uuid) = '\0';
-       *(mkfs_cfg.chunk_uuid) = '\0';
+       memset(mkfs_cfg.chunk_uuid, 0, BTRFS_UUID_UNPARSED_SIZE);
+       memset(mkfs_cfg.fs_uuid, 0, BTRFS_UUID_UNPARSED_SIZE);
 
        ret = make_btrfs(fd, &mkfs_cfg, &cctx);
        if (ret) {
@@ -2473,7 +2470,6 @@ static int do_convert(const char *devname, int datacsum, int packing,
                error("unable to migrate super block: %d", ret);
                goto fail;
        }
-       is_btrfs = 1;
 
        root = open_ctree_fd(fd, devname, 0,
                        OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
@@ -2491,11 +2487,8 @@ fail:
        clean_convert_context(&cctx);
        if (fd != -1)
                close(fd);
-       if (is_btrfs)
-               warning(
-"an error occurred during chunk mapping fixup, filesystem mountable but not finalized");
-       else
-               error("conversion aborted");
+       warning(
+"an error occurred during conversion, filesystem is partially created but not finalized and not mountable");
        return -1;
 }
 
@@ -2660,24 +2653,24 @@ static int do_rollback(const char *devname)
 
        fd = open(devname, O_RDWR);
        if (fd < 0) {
-               fprintf(stderr, "unable to open %s\n", devname);
+               error("unable to open %s: %s", devname, strerror(errno));
                goto fail;
        }
        root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
        if (!root) {
-               fprintf(stderr, "unable to open ctree\n");
+               error("unable to open ctree");
                goto fail;
        }
        ret = may_rollback(root);
        if (ret < 0) {
-               fprintf(stderr, "unable to do rollback\n");
+               error("unable to do rollback: %d", ret);
                goto fail;
        }
 
        sectorsize = root->sectorsize;
        buf = malloc(sectorsize);
        if (!buf) {
-               fprintf(stderr, "unable to allocate memory\n");
+               error("unable to allocate memory");
                goto fail;
        }
 
@@ -2690,12 +2683,10 @@ static int do_rollback(const char *devname)
                                0);
        btrfs_release_path(&path);
        if (ret > 0) {
-               fprintf(stderr,
-               "ERROR: unable to convert ext2 image subvolume, is it deleted?\n");
+               error("unable to convert ext2 image subvolume, is it deleted?");
                goto fail;
        } else if (ret < 0) {
-               fprintf(stderr,
-                       "ERROR: unable to open ext2_saved, id=%llu: %s\n",
+               error("unable to open ext2_saved, id %llu: %s",
                        (unsigned long long)key.objectid, strerror(-ret));
                goto fail;
        }
@@ -2705,8 +2696,8 @@ static int do_rollback(const char *devname)
        key.offset = (u64)-1;
        image_root = btrfs_read_fs_root(root->fs_info, &key);
        if (!image_root || IS_ERR(image_root)) {
-               fprintf(stderr, "unable to open subvol %llu\n",
-                       (unsigned long long)key.objectid);
+               error("unable to open subvolume %llu: %ld",
+                       (unsigned long long)key.objectid, PTR_ERR(image_root));
                goto fail;
        }
 
@@ -2715,7 +2706,7 @@ static int do_rollback(const char *devname)
        dir = btrfs_lookup_dir_item(NULL, image_root, &path,
                                   root_dir, name, strlen(name), 0);
        if (!dir || IS_ERR(dir)) {
-               fprintf(stderr, "unable to find file %s\n", name);
+               error("unable to find file %s: %ld", name, PTR_ERR(dir));
                goto fail;
        }
        leaf = path.nodes[0];
@@ -2726,7 +2717,7 @@ static int do_rollback(const char *devname)
 
        ret = btrfs_lookup_inode(NULL, image_root, &path, &key, 0);
        if (ret) {
-               fprintf(stderr, "unable to find inode item\n");
+               error("unable to find inode item: %d", ret);
                goto fail;
        }
        leaf = path.nodes[0];
@@ -2739,7 +2730,7 @@ static int do_rollback(const char *devname)
        btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
        ret = btrfs_search_slot(NULL, image_root, &key, &path, 0, 0);
        if (ret != 0) {
-               fprintf(stderr, "unable to find first file extent\n");
+               error("unable to find first file extent: %d", ret);
                btrfs_release_path(&path);
                goto fail;
        }
@@ -2802,8 +2793,10 @@ next_extent:
        btrfs_release_path(&path);
 
        if (offset < total_bytes) {
-               fprintf(stderr, "unable to build extent mapping\n");
-               fprintf(stderr, "converted filesystem after balance is unable to rollback\n");
+               error("unable to build extent mapping (offset %llu, total_bytes %llu)",
+                               (unsigned long long)offset,
+                               (unsigned long long)total_bytes);
+               error("converted filesystem after balance is unable to rollback");
                goto fail;
        }
 
@@ -2811,7 +2804,7 @@ next_extent:
        first_free &= ~((u64)sectorsize - 1);
        /* backup for extent #0 should exist */
        if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
-               fprintf(stderr, "no backup for the first extent\n");
+               error("no backup for the first extent");
                goto fail;
        }
        /* force no allocation from system block group */
@@ -2852,13 +2845,16 @@ next_extent:
        }
        /* only extent #0 left in system block group? */
        if (num_bytes > first_free) {
-               fprintf(stderr, "unable to empty system block group\n");
+               error(
+       "unable to empty system block group (num_bytes %llu, first_free %llu",
+                               (unsigned long long)num_bytes,
+                               (unsigned long long)first_free);
                goto fail;
        }
        /* create a system chunk that maps the whole device */
        ret = prepare_system_chunk_sb(root->fs_info->super_copy);
        if (ret) {
-               fprintf(stderr, "unable to update system chunk\n");
+               error("unable to update system chunk: %d", ret);
                goto fail;
        }
 
@@ -2867,7 +2863,7 @@ next_extent:
 
        ret = close_ctree(root);
        if (ret) {
-               fprintf(stderr, "error during close_ctree %d\n", ret);
+               error("close_ctree failed: %d", ret);
                goto fail;
        }
 
@@ -2879,9 +2875,8 @@ next_extent:
                        break;
                ret = pwrite(fd, buf, sectorsize, bytenr);
                if (ret != sectorsize) {
-                       fprintf(stderr,
-                               "error during zeroing superblock %d: %d\n",
-                               i, ret);
+                       error("zeroing superblock mirror %d failed: %d",
+                                       i, ret);
                        goto fail;
                }
        }
@@ -2907,13 +2902,15 @@ next_extent:
                        }
                        ret = pread(fd, buf, sectorsize, bytenr);
                        if (ret < 0) {
-                               fprintf(stderr, "error during pread %d\n", ret);
+                               error("reading superblock at %llu failed: %d",
+                                               (unsigned long long)bytenr, ret);
                                goto fail;
                        }
                        BUG_ON(ret != sectorsize);
                        ret = pwrite(fd, buf, sectorsize, start);
                        if (ret < 0) {
-                               fprintf(stderr, "error during pwrite %d\n", ret);
+                               error("writing superblock at %llu failed: %d",
+                                               (unsigned long long)start, ret);
                                goto fail;
                        }
                        BUG_ON(ret != sectorsize);
@@ -2924,8 +2921,8 @@ next_sector:
        }
 
        ret = fsync(fd);
-       if (ret) {
-               fprintf(stderr, "error during fsync %d\n", ret);
+       if (ret < 0) {
+               error("fsync failed: %s", strerror(errno));
                goto fail;
        }
        /*
@@ -2933,33 +2930,35 @@ next_sector:
         */
        ret = pread(fd, buf, sectorsize, sb_bytenr);
        if (ret < 0) {
-               fprintf(stderr, "error during pread %d\n", ret);
+               error("reading primary superblock failed: %s",
+                               strerror(errno));
                goto fail;
        }
        BUG_ON(ret != sectorsize);
        ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
        if (ret < 0) {
-               fprintf(stderr, "error during pwrite %d\n", ret);
+               error("writing primary superblock failed: %s",
+                               strerror(errno));
                goto fail;
        }
        BUG_ON(ret != sectorsize);
        ret = fsync(fd);
-       if (ret) {
-               fprintf(stderr, "error during fsync %d\n", ret);
+       if (ret < 0) {
+               error("fsync failed: %s", strerror(errno));
                goto fail;
        }
 
        close(fd);
        free(buf);
        extent_io_tree_cleanup(&io_tree);
-       printf("rollback complete.\n");
+       printf("rollback complete\n");
        return 0;
 
 fail:
        if (fd != -1)
                close(fd);
        free(buf);
-       fprintf(stderr, "rollback aborted.\n");
+       error("rollback aborted");
        return -1;
 }