btrfs-progs: tests: convert misc/011-delete-missing-device to loopdevs
[platform/upstream/btrfs-progs.git] / mkfs / main.c
index 70a2052..f495258 100644 (file)
@@ -75,6 +75,7 @@ static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
        int ret;
 
        trans = btrfs_start_transaction(root, 1);
+       BUG_ON(IS_ERR(trans));
        bytes_used = btrfs_super_bytes_used(fs_info->super_copy);
 
        root->fs_info->system_allocs = 1;
@@ -719,7 +720,7 @@ again:
         * keep our extent size at 1MB max, this makes it easier to work inside
         * the tiny block groups created during mkfs
         */
-       cur_bytes = min(total_bytes, 1024ULL * 1024);
+       cur_bytes = min(total_bytes, (u64)SZ_1M);
        ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
                                   &key, 1);
        if (ret)
@@ -992,7 +993,7 @@ static int create_chunks(struct btrfs_trans_handle *trans,
        u64 chunk_size;
        u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
        u64 data_type = BTRFS_BLOCK_GROUP_DATA;
-       u64 minimum_data_chunk_size = 8 * 1024 * 1024;
+       u64 minimum_data_chunk_size = SZ_8M;
        u64 i;
        int ret;
 
@@ -1047,6 +1048,7 @@ static int make_image(const char *source_dir, struct btrfs_root *root)
        INIT_LIST_HEAD(&dir_head.list);
 
        trans = btrfs_start_transaction(root, 1);
+       BUG_ON(IS_ERR(trans));
        ret = traverse_directory(trans, root, source_dir, &dir_head);
        if (ret) {
                error("unable to traverse directory %s: %d", source_dir, ret);
@@ -1095,9 +1097,9 @@ static u64 size_sourcedir(const char *dir_name, u64 sectorsize,
        u64 dir_size = 0;
        u64 total_size = 0;
        int ret;
-       u64 default_chunk_size = 8 * 1024 * 1024;       /* 8MB */
-       u64 allocated_meta_size = 8 * 1024 * 1024;      /* 8MB */
-       u64 allocated_total_size = 20 * 1024 * 1024;    /* 20MB */
+       u64 default_chunk_size = SZ_8M;
+       u64 allocated_meta_size = SZ_8M;
+       u64 allocated_total_size = 20 * SZ_1M;  /* 20MB */
        u64 num_of_meta_chunks = 0;
        u64 num_of_data_chunks = 0;
        u64 num_of_allocated_meta_chunks =
@@ -1325,6 +1327,7 @@ static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
 
        btrfs_init_path(&path);
        trans = btrfs_start_transaction(root, 1);
+       BUG_ON(IS_ERR(trans));
 
        key.objectid = 0;
        key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
@@ -1409,7 +1412,7 @@ int main(int argc, char **argv)
        u32 sectorsize = 4096;
        u32 stripesize = 4096;
        int zero_end = 1;
-       int fd;
+       int fd = -1;
        int ret;
        int i;
        int mixed = 0;
@@ -1495,12 +1498,12 @@ int main(int argc, char **argv)
                                        error("unrecognized filesystem feature '%s'",
                                                        tmp);
                                        free(orig);
-                                       exit(1);
+                                       goto error;
                                }
                                free(orig);
                                if (features & BTRFS_FEATURE_LIST_ALL) {
                                        btrfs_list_all_fs_features(0);
-                                       exit(0);
+                                       goto success;
                                }
                                break;
                                }
@@ -1514,8 +1517,7 @@ int main(int argc, char **argv)
                        case 'V':
                                printf("mkfs.btrfs, part of %s\n",
                                                PACKAGE_STRING);
-                               exit(0);
-                               break;
+                               goto success;
                        case 'r':
                                source_dir = optarg;
                                source_dir_set = 1;
@@ -1550,7 +1552,7 @@ int main(int argc, char **argv)
 
        if (source_dir_set && dev_cnt > 1) {
                error("the option -r is limited to a single device");
-               exit(1);
+               goto error;
        }
 
        if (*fs_uuid) {
@@ -1558,11 +1560,11 @@ int main(int argc, char **argv)
 
                if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
                        error("could not parse UUID: %s", fs_uuid);
-                       exit(1);
+                       goto error;
                }
                if (!test_uuid_unique(fs_uuid)) {
                        error("non-unique UUID: %s", fs_uuid);
-                       exit(1);
+                       goto error;
                }
        }
 
@@ -1570,7 +1572,7 @@ int main(int argc, char **argv)
                file = argv[optind++];
                if (is_block_device(file) == 1)
                        if (test_dev_for_mkfs(file, force_overwrite))
-                               exit(1);
+                               goto error;
        }
 
        optind = saved_optind;
@@ -1605,7 +1607,7 @@ int main(int argc, char **argv)
                        if (metadata_profile != data_profile) {
                                error(
        "with mixed block groups data and metadata profiles must be the same");
-                               exit(1);
+                               goto error;
                        }
                }
 
@@ -1627,12 +1629,12 @@ int main(int argc, char **argv)
 
        if (btrfs_check_nodesize(nodesize, sectorsize,
                                 features))
-               exit(1);
+               goto error;
 
        if (sectorsize < sizeof(struct btrfs_super_block)) {
                error("sectorsize smaller than superblock: %u < %zu",
                                sectorsize, sizeof(struct btrfs_super_block));
-               exit(1);
+               goto error;
        }
 
        /* Check device/block_count after the nodesize is determined */
@@ -1641,7 +1643,7 @@ int main(int argc, char **argv)
                        block_count);
                error("minimum size for btrfs filesystem is %llu",
                        btrfs_min_dev_size(nodesize));
-               exit(1);
+               goto error;
        }
        for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
                char *path;
@@ -1651,20 +1653,20 @@ int main(int argc, char **argv)
                if (ret < 0) {
                        error("failed to check size for %s: %s",
                                path, strerror(-ret));
-                       exit (1);
+                       goto error;
                }
                if (ret > 0) {
                        error("'%s' is too small to make a usable filesystem",
                                path);
                        error("minimum size for each btrfs device is %llu",
                                btrfs_min_dev_size(nodesize));
-                       exit(1);
+                       goto error;
                }
        }
        ret = test_num_disk_vs_raid(metadata_profile, data_profile,
                        dev_cnt, mixed, ssd);
        if (ret)
-               exit(1);
+               goto error;
 
        dev_cnt--;
 
@@ -1677,7 +1679,7 @@ int main(int argc, char **argv)
                fd = open(file, O_RDWR);
                if (fd < 0) {
                        error("unable to open %s: %s", file, strerror(errno));
-                       exit(1);
+                       goto error;
                }
                ret = btrfs_prepare_device(fd, file, &dev_block_count,
                                block_count,
@@ -1685,22 +1687,21 @@ int main(int argc, char **argv)
                                (discard ? PREP_DEVICE_DISCARD : 0) |
                                (verbose ? PREP_DEVICE_VERBOSE : 0));
                if (ret) {
-                       close(fd);
-                       exit(1);
+                       goto error;
                }
                if (block_count && block_count > dev_block_count) {
                        error("%s is smaller than requested size, expected %llu, found %llu",
                                        file,
                                        (unsigned long long)block_count,
                                        (unsigned long long)dev_block_count);
-                       exit(1);
+                       goto error;
                }
        } else {
                fd = open(file, O_CREAT | O_RDWR,
                                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
                if (fd < 0) {
                        error("unable to open %s: %s", file, strerror(errno));
-                       exit(1);
+                       goto error;
                }
 
                source_dir_size = size_sourcedir(source_dir, sectorsize,
@@ -1710,7 +1711,7 @@ int main(int argc, char **argv)
                ret = zero_output_file(fd, block_count);
                if (ret) {
                        error("unable to zero the output file");
-                       exit(1);
+                       goto error;
                }
                /* our "device" is the new image file */
                dev_block_count = block_count;
@@ -1720,7 +1721,7 @@ int main(int argc, char **argv)
        if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
                error("device is too small to make filesystem, must be at least %llu",
                                (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
-               exit(1);
+               goto error;
        }
 
        if (group_profile_max_safe_loss(metadata_profile) <
@@ -1739,41 +1740,42 @@ int main(int argc, char **argv)
        ret = make_btrfs(fd, &mkfs_cfg);
        if (ret) {
                error("error during mkfs: %s", strerror(-ret));
-               exit(1);
+               goto error;
        }
 
        fs_info = open_ctree_fs_info(file, 0, 0, 0,
                        OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
        if (!fs_info) {
                error("open ctree failed");
-               close(fd);
-               exit(1);
+               goto error;
        }
+       close(fd);
+       fd = -1;
        root = fs_info->fs_root;
        fs_info->alloc_start = alloc_start;
 
        ret = create_metadata_block_groups(root, mixed, &allocation);
        if (ret) {
                error("failed to create default block groups: %d", ret);
-               exit(1);
+               goto error;
        }
 
        trans = btrfs_start_transaction(root, 1);
-       if (!trans) {
+       if (IS_ERR(trans)) {
                error("failed to start transaction");
-               exit(1);
+               goto error;
        }
 
        ret = create_data_block_groups(trans, root, mixed, &allocation);
        if (ret) {
                error("failed to create default data block groups: %d", ret);
-               exit(1);
+               goto error;
        }
 
        ret = make_root_dir(trans, root);
        if (ret) {
                error("failed to setup the root directory: %d", ret);
-               exit(1);
+               goto error;
        }
 
        ret = btrfs_commit_transaction(trans, root);
@@ -1783,9 +1785,9 @@ int main(int argc, char **argv)
        }
 
        trans = btrfs_start_transaction(root, 1);
-       if (!trans) {
+       if (IS_ERR(trans)) {
                error("failed to start transaction");
-               exit(1);
+               goto error;
        }
 
        if (dev_cnt == 0)
@@ -1802,7 +1804,7 @@ int main(int argc, char **argv)
                fd = open(file, O_RDWR);
                if (fd < 0) {
                        error("unable to open %s: %s", file, strerror(errno));
-                       exit(1);
+                       goto error;
                }
                ret = btrfs_device_already_in_root(root, fd,
                                                   BTRFS_SUPER_INFO_OFFSET);
@@ -1818,8 +1820,7 @@ int main(int argc, char **argv)
                                (zero_end ? PREP_DEVICE_ZERO_END : 0) |
                                (discard ? PREP_DEVICE_DISCARD : 0));
                if (ret) {
-                       close(fd);
-                       exit(1);
+                       goto error;
                }
 
                ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
@@ -1862,6 +1863,7 @@ raid_groups:
 
        if (source_dir_set) {
                trans = btrfs_start_transaction(root, 1);
+               BUG_ON(IS_ERR(trans));
                ret = create_chunks(trans, root,
                                    num_of_meta_chunks, size_of_data,
                                    &allocation);
@@ -1943,4 +1945,12 @@ out:
        free(label);
 
        return !!ret;
+error:
+       if (fd > 0)
+               close(fd);
+
+       free(label);
+       exit(1);
+success:
+       exit(0);
 }