res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
0, &mixed, discard);
+ close(devfd);
if (res) {
- fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
- close(devfd);
ret++;
- continue;
+ goto error_out;
}
- close(devfd);
strncpy_null(ioctl_args.name, argv[i]);
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
}
+error_out:
close_file_or_dir(fdmnt, dirstream);
return !!ret;
}
}
strncpy((char *)start_args.start.tgtdev_name, dstdev,
BTRFS_DEVICE_PATH_NAME_MAX);
- if (btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
- &mixed, 0)) {
- fprintf(stderr, "Error: Failed to prepare device '%s'\n",
- dstdev);
+ ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
+ &mixed, 0);
+ if (ret)
goto leave_with_error;
- }
+
close(fddstdev);
fddstdev = -1;
first_file = file;
ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
block_count, &mixed, discard);
+ if (ret) {
+ close(fd);
+ exit(1);
+ }
if (block_count && block_count > dev_block_count) {
fprintf(stderr, "%s is smaller than requested size\n", file);
exit(1);
}
ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
block_count, &mixed, discard);
+ if (ret) {
+ close(fd);
+ exit(1);
+ }
mixed = old_mixed;
- BUG_ON(ret);
ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
sectorsize, sectorsize, sectorsize);
ret = fstat(fd, &st);
if (ret < 0) {
fprintf(stderr, "unable to stat %s\n", file);
- exit(1);
+ return 1;
}
block_count = btrfs_device_size(fd, &st);
if (block_count == 0) {
fprintf(stderr, "unable to find %s size\n", file);
- exit(1);
+ return 1;
}
if (max_block_count)
block_count = min(block_count, max_block_count);
}
ret = zero_dev_start(fd);
- if (ret) {
- fprintf(stderr, "failed to zero device start %d\n", ret);
- exit(1);
- }
+ if (ret)
+ goto zero_dev_error;
for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
bytenr = btrfs_sb_offset(i);
if (bytenr >= block_count)
break;
- zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
+ ret = zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
+ if (ret)
+ goto zero_dev_error;
}
if (zero_end) {
ret = zero_dev_end(fd, block_count);
- if (ret) {
- fprintf(stderr, "failed to zero device end %d\n", ret);
- exit(1);
- }
+ if (ret)
+ goto zero_dev_error;
}
*block_count_ret = block_count;
+
+zero_dev_error:
+ if (ret < 0) {
+ fprintf(stderr, "ERROR: failed to zero device '%s' - %s\n",
+ file, strerror(-ret));
+ return 1;
+ } else if (ret > 0) {
+ fprintf(stderr, "ERROR: failed to zero device '%s' - %d\n",
+ file, ret);
+ return 1;
+ }
return 0;
}