Hubert Kario
2012-May-01 12:41 UTC
[PATCH v2 4/5] better error handling in btrfs_prepare_device()
btrfs_prepare_device did abort the whole application on any error, even when there were other tasks queued that could succeed, now it returns non zero value on error. Add more descriptive error messages: print failing device name and cause of error. Signed-off-by: Hubert Kario <kario@wit.edu.pl> diff --git a/mkfs.c b/mkfs.c index 7d1165f..9a58f67 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1310,6 +1310,10 @@ int main(int ac, char **av) } first_file = file; ret = btrfs_prepare_device(fd, file, &dev_block_count, &mixed); + if (ret) { + fprintf(stderr, "Unable to init device %s\n", file); + exit(1); + } if (block_count == 0) block_count = dev_block_count; } else { diff --git a/utils.c b/utils.c index e2c72ad..139ba8a 100644 --- a/utils.c +++ b/utils.c @@ -546,13 +546,13 @@ int btrfs_prepare_device(int fd, char *file, u64 *block_count_ret, int *mixed) ret = fstat(fd, &st); if (ret < 0) { fprintf(stderr, "unable to stat %s\n", file); - exit(1); + return 1; } block_count = device_size(fd, &st); if (block_count == 0) { fprintf(stderr, "unable to find %s size\n", file); - exit(1); + return 1; } if (mixed && block_count < 1024 * 1024 * 1024 && !(*mixed)) { @@ -568,21 +568,30 @@ int btrfs_prepare_device(int fd, char *file, u64 *block_count_ret, int *mixed) ret = zero_dev_start(fd); if (ret) { - fprintf(stderr, "failed to zero device start %d\n", ret); - exit(1); + fprintf(stderr, "failed to zero device %s start: %s\n", file, + strerror(-ret)); + return 1; } for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); - if (bytenr >= block_count) + /* don''t zero the superblock if it''s on device end boundary, + * it will be zeroed by zero_dev_end() anyway */ + if (bytenr + BTRFS_SUPER_INFO_SIZE >= block_count) break; - zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE); + ret = zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE); + if (ret) { + fprintf(stderr, "failed to zero superblock no. %i " + "(at %lli) on device %s: %s\n", i, bytenr, + file, strerror(-ret)); + return 1; + } } ret = zero_dev_end(fd, block_count); if (ret) { - fprintf(stderr, "failed to zero device end %d\n", ret); - exit(1); + fprintf(stderr, "failed to zero device %s end: %s\n", file, strerror(-ret)); + return 1; } if (block_count_ret) -- 1.7.10 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Signed-off-by: Hubert Kario <kario@wit.edu.pl> diff --git a/cmds-device.c b/cmds-device.c index a28752f..b1e70f9 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -261,8 +261,6 @@ static int cmd_zero_dev(int argc, char **argv) int arg_processed; int ret = 0; int n; - u64 device_len; - int mixed_mode_needed = 1; /* keep btrfs_prepare_device() quiet */ if( argc < 2 ) { usage(cmd_zero_dev_usage); @@ -278,8 +276,7 @@ static int cmd_zero_dev(int argc, char **argv) continue; } - n = btrfs_prepare_device(fd, file, &device_len, - &mixed_mode_needed); + n = btrfs_prepare_device(fd, file, NULL, NULL); if (n) { fprintf(stderr, "Error when zeroing out %s\n", file); ret |= n; -- 1.7.10 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html