Anand Jain
2013-Dec-16 12:33 UTC
[PATCH 1/3] btrfs_progs: don''t replicate the stripe_len defines
a clean up patch, the BTRFS_STRIPE_LEN is been duplicated across btrfs_progs, the kernel defines it in volume.h so do the same for progs. Signed-off-by: Anand Jain <anand.jain@oracle.com> --- btrfs-convert.c | 19 +++++++++---------- chunk-recover.c | 1 - cmds-chunk.c | 1 - volumes.h | 2 ++ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index ae10eed..65fe707 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -43,7 +43,6 @@ #include <ext2fs/ext2_ext_attr.h> #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO) -#define STRIPE_LEN (64 * 1024) #define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID /* @@ -134,11 +133,11 @@ static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs) for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); - bytenr &= ~((u64)STRIPE_LEN - 1); + bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1); if (bytenr >= blocksize * ext2_fs->super->s_blocks_count) break; clear_extent_dirty(&root->fs_info->free_space_cache, bytenr, - bytenr + STRIPE_LEN - 1, 0); + bytenr + BTRFS_STRIPE_LEN - 1, 0); } clear_extent_dirty(&root->fs_info->free_space_cache, @@ -207,9 +206,9 @@ static int intersect_with_sb(u64 bytenr, u64 num_bytes) for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { offset = btrfs_sb_offset(i); - offset &= ~((u64)STRIPE_LEN - 1); + offset &= ~((u64)BTRFS_STRIPE_LEN - 1); - if (bytenr < offset + STRIPE_LEN && + if (bytenr < offset + BTRFS_STRIPE_LEN && bytenr + num_bytes > offset) return 1; } @@ -450,8 +449,8 @@ static int block_iterate_proc(ext2_filsys ext2_fs, } if (sb_region) { - bytenr += STRIPE_LEN - 1; - bytenr &= ~((u64)STRIPE_LEN - 1); + bytenr += BTRFS_STRIPE_LEN - 1; + bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1); } else { cache = btrfs_lookup_block_group(root->fs_info, bytenr); BUG_ON(!cache); @@ -1523,7 +1522,7 @@ static int create_chunk_mapping(struct btrfs_trans_handle *trans, btrfs_set_stack_chunk_length(&chunk, cache->key.offset); btrfs_set_stack_chunk_owner(&chunk, extent_root->root_key.objectid); - btrfs_set_stack_chunk_stripe_len(&chunk, STRIPE_LEN); + btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(&chunk, cache->flags); btrfs_set_stack_chunk_io_align(&chunk, device->io_align); btrfs_set_stack_chunk_io_width(&chunk, device->io_width); @@ -2098,10 +2097,10 @@ static int cleanup_sys_chunk(struct btrfs_root *fs_root, } for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { offset = btrfs_sb_offset(i); - offset &= ~((u64)STRIPE_LEN - 1); + offset &= ~((u64)BTRFS_STRIPE_LEN - 1); ret = relocate_extents_range(fs_root, ext2_root, - offset, offset + STRIPE_LEN); + offset, offset + BTRFS_STRIPE_LEN); if (ret) goto fail; } diff --git a/chunk-recover.c b/chunk-recover.c index bcde39e..b072ba6 100644 --- a/chunk-recover.c +++ b/chunk-recover.c @@ -41,7 +41,6 @@ #include "btrfsck.h" #include "commands.h" -#define BTRFS_STRIPE_LEN (64 * 1024) #define BTRFS_NUM_MIRRORS 2 struct recover_control { diff --git a/cmds-chunk.c b/cmds-chunk.c index 4d7fce0..348229c 100644 --- a/cmds-chunk.c +++ b/cmds-chunk.c @@ -42,7 +42,6 @@ #include "commands.h" #define BTRFS_CHUNK_TREE_REBUILD_ABORTED -7500 -#define BTRFS_STRIPE_LEN (64 * 1024) #define BTRFS_NUM_MIRRORS 2 struct recover_control { diff --git a/volumes.h b/volumes.h index 2802cb0..b1ff3d0 100644 --- a/volumes.h +++ b/volumes.h @@ -19,6 +19,8 @@ #ifndef __BTRFS_VOLUMES_ #define __BTRFS_VOLUMES_ +#define BTRFS_STRIPE_LEN (64 * 1024) + struct btrfs_device { struct list_head dev_list; struct btrfs_root *dev_root; -- 1.8.1.164.g2d0029e -- 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: Anand Jain <anand.jain@oracle.com> --- btrfs-convert.c | 2 +- btrfs-image.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index 65fe707..df20c15 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -1715,7 +1715,7 @@ static int prepare_system_chunk_sb(struct btrfs_super_block *super) btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super)); btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID); - btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024); + btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM); btrfs_set_stack_chunk_io_align(chunk, sectorsize); btrfs_set_stack_chunk_io_width(chunk, sectorsize); diff --git a/btrfs-image.c b/btrfs-image.c index 7bcfc06..1b2831a 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -1350,7 +1350,7 @@ static void update_super_old(u8 *buffer) btrfs_set_stack_chunk_length(chunk, (u64)-1); btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID); - btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024); + btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM); btrfs_set_stack_chunk_io_align(chunk, sectorsize); btrfs_set_stack_chunk_io_width(chunk, sectorsize); -- 1.8.1.164.g2d0029e -- 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
Anand Jain
2013-Dec-16 12:33 UTC
[PATCH 3/3] btrfs_progs: handle error in the btrfs_prepare_device
this patch will handle the strerror reporting of the error instead of printing errno, and also replaced the BUG_ON with the error handling Signed-off-by: Anand Jain <anand.jain@oracle.com> --- cmds-device.c | 7 +++---- cmds-replace.c | 10 ++++------ mkfs.c | 9 ++++++++- utils.c | 30 +++++++++++++++++++----------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/cmds-device.c b/cmds-device.c index bc4a8dc..ada0bcd 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -111,13 +111,11 @@ static int cmd_add_dev(int argc, char **argv) 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); @@ -130,6 +128,7 @@ static int cmd_add_dev(int argc, char **argv) } +error_out: close_file_or_dir(fdmnt, dirstream); return !!ret; } diff --git a/cmds-replace.c b/cmds-replace.c index d9b0940..8160107 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -276,13 +276,11 @@ static int cmd_start_replace(int argc, char **argv) } 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); - goto leave_with_error; - } + ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0, + &mixed, 0); close(fddstdev); + if (ret) + goto leave_with_error; fddstdev = -1; dev_replace_handle_sigint(fdmnt); diff --git a/mkfs.c b/mkfs.c index 33369f9..18df087 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1446,6 +1446,10 @@ int main(int ac, char **av) 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); @@ -1553,8 +1557,11 @@ int main(int ac, char **av) } 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); diff --git a/utils.c b/utils.c index f499023..85b967b 100644 --- a/utils.c +++ b/utils.c @@ -581,13 +581,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, 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); @@ -612,26 +612,34 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, } 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) { + ret < 0 ? + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %s\n", + file, strerror(-ret)) : + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %d\n", + file, ret); + return 1; + } return 0; } -- 1.8.1.164.g2d0029e -- 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
Wang Shilong
2013-Dec-16 12:38 UTC
Re: [PATCH 3/3] btrfs_progs: handle error in the btrfs_prepare_device
Hello Anand, s/btrfs_progs/btrfs-progs? more comments below…> this patch will handle the strerror reporting of the error instead of > printing errno, and also replaced the BUG_ON with the error handling > > Signed-off-by: Anand Jain <anand.jain@oracle.com> > --- > cmds-device.c | 7 +++---- > cmds-replace.c | 10 ++++------ > mkfs.c | 9 ++++++++- > utils.c | 30 +++++++++++++++++++----------- > 4 files changed, 34 insertions(+), 22 deletions(-) > > diff --git a/cmds-device.c b/cmds-device.c > index bc4a8dc..ada0bcd 100644 > --- a/cmds-device.c > +++ b/cmds-device.c > @@ -111,13 +111,11 @@ static int cmd_add_dev(int argc, char **argv) > > 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); > @@ -130,6 +128,7 @@ static int cmd_add_dev(int argc, char **argv) > > } > > +error_out: > close_file_or_dir(fdmnt, dirstream); > return !!ret; > } > diff --git a/cmds-replace.c b/cmds-replace.c > index d9b0940..8160107 100644 > --- a/cmds-replace.c > +++ b/cmds-replace.c > @@ -276,13 +276,11 @@ static int cmd_start_replace(int argc, char **argv) > } > 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); > - goto leave_with_error; > - } > + ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0, > + &mixed, 0); > close(fddstdev); > + if (ret) > + goto leave_with_error; > fddstdev = -1; > > dev_replace_handle_sigint(fdmnt); > diff --git a/mkfs.c b/mkfs.c > index 33369f9..18df087 100644 > --- a/mkfs.c > +++ b/mkfs.c > @@ -1446,6 +1446,10 @@ int main(int ac, char **av) > 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); > @@ -1553,8 +1557,11 @@ int main(int ac, char **av) > } > 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); > diff --git a/utils.c b/utils.c > index f499023..85b967b 100644 > --- a/utils.c > +++ b/utils.c > @@ -581,13 +581,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, > 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); > @@ -612,26 +612,34 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, > } > > 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) { > + ret < 0 ? > + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %s\n", > + file, strerror(-ret)) : > + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %d\n", > + file, ret);Why we output message twice here…. Thanks, Wang> + return 1; > + } > return 0; > } > > -- > 1.8.1.164.g2d0029e > > -- > 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-- 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
David Sterba
2013-Dec-16 13:59 UTC
Re: [PATCH 2/3] btrfs_progs: use stripe_len define here
Thanks, good cleanup, FYI I''ll fold in the remaining cases from volumes.c david -- 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
Anand Jain
2013-Dec-17 02:33 UTC
[PATCH v2 1/3] btrfs-progs: don''t replicate the stripe_len defines
a clean up patch, the BTRFS_STRIPE_LEN is been duplicated across btrfs-progs, the kernel defines it in volume.h so do the same for progs. Signed-off-by: Anand Jain <anand.jain@oracle.com> --- v2: commit update --- btrfs-convert.c | 19 +++++++++---------- chunk-recover.c | 1 - cmds-chunk.c | 1 - volumes.h | 2 ++ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index ae10eed..65fe707 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -43,7 +43,6 @@ #include <ext2fs/ext2_ext_attr.h> #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO) -#define STRIPE_LEN (64 * 1024) #define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID /* @@ -134,11 +133,11 @@ static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs) for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); - bytenr &= ~((u64)STRIPE_LEN - 1); + bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1); if (bytenr >= blocksize * ext2_fs->super->s_blocks_count) break; clear_extent_dirty(&root->fs_info->free_space_cache, bytenr, - bytenr + STRIPE_LEN - 1, 0); + bytenr + BTRFS_STRIPE_LEN - 1, 0); } clear_extent_dirty(&root->fs_info->free_space_cache, @@ -207,9 +206,9 @@ static int intersect_with_sb(u64 bytenr, u64 num_bytes) for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { offset = btrfs_sb_offset(i); - offset &= ~((u64)STRIPE_LEN - 1); + offset &= ~((u64)BTRFS_STRIPE_LEN - 1); - if (bytenr < offset + STRIPE_LEN && + if (bytenr < offset + BTRFS_STRIPE_LEN && bytenr + num_bytes > offset) return 1; } @@ -450,8 +449,8 @@ static int block_iterate_proc(ext2_filsys ext2_fs, } if (sb_region) { - bytenr += STRIPE_LEN - 1; - bytenr &= ~((u64)STRIPE_LEN - 1); + bytenr += BTRFS_STRIPE_LEN - 1; + bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1); } else { cache = btrfs_lookup_block_group(root->fs_info, bytenr); BUG_ON(!cache); @@ -1523,7 +1522,7 @@ static int create_chunk_mapping(struct btrfs_trans_handle *trans, btrfs_set_stack_chunk_length(&chunk, cache->key.offset); btrfs_set_stack_chunk_owner(&chunk, extent_root->root_key.objectid); - btrfs_set_stack_chunk_stripe_len(&chunk, STRIPE_LEN); + btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(&chunk, cache->flags); btrfs_set_stack_chunk_io_align(&chunk, device->io_align); btrfs_set_stack_chunk_io_width(&chunk, device->io_width); @@ -2098,10 +2097,10 @@ static int cleanup_sys_chunk(struct btrfs_root *fs_root, } for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { offset = btrfs_sb_offset(i); - offset &= ~((u64)STRIPE_LEN - 1); + offset &= ~((u64)BTRFS_STRIPE_LEN - 1); ret = relocate_extents_range(fs_root, ext2_root, - offset, offset + STRIPE_LEN); + offset, offset + BTRFS_STRIPE_LEN); if (ret) goto fail; } diff --git a/chunk-recover.c b/chunk-recover.c index bcde39e..b072ba6 100644 --- a/chunk-recover.c +++ b/chunk-recover.c @@ -41,7 +41,6 @@ #include "btrfsck.h" #include "commands.h" -#define BTRFS_STRIPE_LEN (64 * 1024) #define BTRFS_NUM_MIRRORS 2 struct recover_control { diff --git a/cmds-chunk.c b/cmds-chunk.c index 4d7fce0..348229c 100644 --- a/cmds-chunk.c +++ b/cmds-chunk.c @@ -42,7 +42,6 @@ #include "commands.h" #define BTRFS_CHUNK_TREE_REBUILD_ABORTED -7500 -#define BTRFS_STRIPE_LEN (64 * 1024) #define BTRFS_NUM_MIRRORS 2 struct recover_control { diff --git a/volumes.h b/volumes.h index 2802cb0..b1ff3d0 100644 --- a/volumes.h +++ b/volumes.h @@ -19,6 +19,8 @@ #ifndef __BTRFS_VOLUMES_ #define __BTRFS_VOLUMES_ +#define BTRFS_STRIPE_LEN (64 * 1024) + struct btrfs_device { struct list_head dev_list; struct btrfs_root *dev_root; -- 1.8.1.164.g2d0029e -- 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: Anand Jain <anand.jain@oracle.com> --- v2: commit update --- btrfs-convert.c | 2 +- btrfs-image.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index 65fe707..df20c15 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -1715,7 +1715,7 @@ static int prepare_system_chunk_sb(struct btrfs_super_block *super) btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super)); btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID); - btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024); + btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM); btrfs_set_stack_chunk_io_align(chunk, sectorsize); btrfs_set_stack_chunk_io_width(chunk, sectorsize); diff --git a/btrfs-image.c b/btrfs-image.c index 7bcfc06..1b2831a 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -1350,7 +1350,7 @@ static void update_super_old(u8 *buffer) btrfs_set_stack_chunk_length(chunk, (u64)-1); btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID); - btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024); + btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM); btrfs_set_stack_chunk_io_align(chunk, sectorsize); btrfs_set_stack_chunk_io_width(chunk, sectorsize); -- 1.8.1.164.g2d0029e -- 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
Anand Jain
2013-Dec-17 02:33 UTC
[PATCH v2 3/3] btrfs-progs: handle error in the btrfs_prepare_device
this patch will handle the strerror reporting of the error instead of printing errno, and also replaced the BUG_ON with the error handling Signed-off-by: Anand Jain <anand.jain@oracle.com> --- v2: commit update --- cmds-device.c | 7 +++---- cmds-replace.c | 10 ++++------ mkfs.c | 9 ++++++++- utils.c | 30 +++++++++++++++++++----------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/cmds-device.c b/cmds-device.c index bc4a8dc..ada0bcd 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -111,13 +111,11 @@ static int cmd_add_dev(int argc, char **argv) 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); @@ -130,6 +128,7 @@ static int cmd_add_dev(int argc, char **argv) } +error_out: close_file_or_dir(fdmnt, dirstream); return !!ret; } diff --git a/cmds-replace.c b/cmds-replace.c index d9b0940..8160107 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -276,13 +276,11 @@ static int cmd_start_replace(int argc, char **argv) } 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); - goto leave_with_error; - } + ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0, + &mixed, 0); close(fddstdev); + if (ret) + goto leave_with_error; fddstdev = -1; dev_replace_handle_sigint(fdmnt); diff --git a/mkfs.c b/mkfs.c index 33369f9..18df087 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1446,6 +1446,10 @@ int main(int ac, char **av) 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); @@ -1553,8 +1557,11 @@ int main(int ac, char **av) } 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); diff --git a/utils.c b/utils.c index f499023..85b967b 100644 --- a/utils.c +++ b/utils.c @@ -581,13 +581,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, 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); @@ -612,26 +612,34 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, } 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) { + ret < 0 ? + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %s\n", + file, strerror(-ret)) : + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %d\n", + file, ret); + return 1; + } return 0; } -- 1.8.1.164.g2d0029e -- 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
Anand Jain
2013-Dec-17 02:40 UTC
Re: [PATCH 3/3] btrfs_progs: handle error in the btrfs_prepare_device
Thanks Wang.> s/btrfs_progs/btrfs-progs?yeah. updated my script.>> + >> +zero_dev_error: >> + if (ret) { >> + ret < 0 ? >> + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %s\n", >> + file, strerror(-ret)) : >> + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %d\n", >> + file, ret); > > Why we output message twice here….Not really. there is "ret < 0 ?" -Anand -- 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
Stefan Behrens
2013-Dec-17 08:01 UTC
Re: [PATCH v2 3/3] btrfs-progs: handle error in the btrfs_prepare_device
On Tue, 17 Dec 2013 10:33:36 +0800, Anand Jain wrote:> this patch will handle the strerror reporting of the error instead of > printing errno, and also replaced the BUG_ON with the error handling > > Signed-off-by: Anand Jain <anand.jain@oracle.com> > --- > v2: commit update > --- > cmds-device.c | 7 +++---- > cmds-replace.c | 10 ++++------ > mkfs.c | 9 ++++++++- > utils.c | 30 +++++++++++++++++++----------- > 4 files changed, 34 insertions(+), 22 deletions(-) >[...]> diff --git a/cmds-replace.c b/cmds-replace.c > index d9b0940..8160107 100644 > --- a/cmds-replace.c > +++ b/cmds-replace.c > @@ -276,13 +276,11 @@ static int cmd_start_replace(int argc, char **argv) > } > 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); > - goto leave_with_error; > - } > + ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0, > + &mixed, 0); > close(fddstdev); > + if (ret) > + goto leave_with_error; > fddstdev = -1;You change the code to call close(fddstdev) twice. [...]> +zero_dev_error: > + if (ret) { > + ret < 0 ? > + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %s\n", > + file, strerror(-ret)) : > + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %d\n", > + file, ret);This is not funny. -- 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
Anand Jain
2013-Dec-17 08:37 UTC
Re: [PATCH v2 3/3] btrfs-progs: handle error in the btrfs_prepare_device
>> + ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0, >> + &mixed, 0); >> close(fddstdev); >> + if (ret) >> + goto leave_with_error;>> fddstdev = -1;yeah moved this 3 lines up. thanks.> You change the code to call close(fddstdev) twice. > > [...] >> +zero_dev_error: >> + if (ret) { >> + ret < 0 ? >> + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %s\n", >> + file, strerror(-ret)) : >> + fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %d\n", >> + file, ret); > > This is not funny.hmm. I am not sure what you mean ? Thanks, Anand -- 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
Anand Jain
2013-Dec-17 11:16 UTC
[PATCH v2 1/3] btrfs-progs: don''t replicate the stripe_len defines
a clean up patch, the BTRFS_STRIPE_LEN is been duplicated across btrfs-progs, the kernel defines it in volume.h so do the same for progs. Signed-off-by: Anand Jain <anand.jain@oracle.com> --- v2: commit update btrfs-convert.c | 19 +++++++++---------- chunk-recover.c | 1 - cmds-chunk.c | 1 - volumes.h | 2 ++ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index ae10eed..65fe707 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -43,7 +43,6 @@ #include <ext2fs/ext2_ext_attr.h> #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO) -#define STRIPE_LEN (64 * 1024) #define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID /* @@ -134,11 +133,11 @@ static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs) for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { bytenr = btrfs_sb_offset(i); - bytenr &= ~((u64)STRIPE_LEN - 1); + bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1); if (bytenr >= blocksize * ext2_fs->super->s_blocks_count) break; clear_extent_dirty(&root->fs_info->free_space_cache, bytenr, - bytenr + STRIPE_LEN - 1, 0); + bytenr + BTRFS_STRIPE_LEN - 1, 0); } clear_extent_dirty(&root->fs_info->free_space_cache, @@ -207,9 +206,9 @@ static int intersect_with_sb(u64 bytenr, u64 num_bytes) for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { offset = btrfs_sb_offset(i); - offset &= ~((u64)STRIPE_LEN - 1); + offset &= ~((u64)BTRFS_STRIPE_LEN - 1); - if (bytenr < offset + STRIPE_LEN && + if (bytenr < offset + BTRFS_STRIPE_LEN && bytenr + num_bytes > offset) return 1; } @@ -450,8 +449,8 @@ static int block_iterate_proc(ext2_filsys ext2_fs, } if (sb_region) { - bytenr += STRIPE_LEN - 1; - bytenr &= ~((u64)STRIPE_LEN - 1); + bytenr += BTRFS_STRIPE_LEN - 1; + bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1); } else { cache = btrfs_lookup_block_group(root->fs_info, bytenr); BUG_ON(!cache); @@ -1523,7 +1522,7 @@ static int create_chunk_mapping(struct btrfs_trans_handle *trans, btrfs_set_stack_chunk_length(&chunk, cache->key.offset); btrfs_set_stack_chunk_owner(&chunk, extent_root->root_key.objectid); - btrfs_set_stack_chunk_stripe_len(&chunk, STRIPE_LEN); + btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(&chunk, cache->flags); btrfs_set_stack_chunk_io_align(&chunk, device->io_align); btrfs_set_stack_chunk_io_width(&chunk, device->io_width); @@ -2098,10 +2097,10 @@ static int cleanup_sys_chunk(struct btrfs_root *fs_root, } for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { offset = btrfs_sb_offset(i); - offset &= ~((u64)STRIPE_LEN - 1); + offset &= ~((u64)BTRFS_STRIPE_LEN - 1); ret = relocate_extents_range(fs_root, ext2_root, - offset, offset + STRIPE_LEN); + offset, offset + BTRFS_STRIPE_LEN); if (ret) goto fail; } diff --git a/chunk-recover.c b/chunk-recover.c index bcde39e..b072ba6 100644 --- a/chunk-recover.c +++ b/chunk-recover.c @@ -41,7 +41,6 @@ #include "btrfsck.h" #include "commands.h" -#define BTRFS_STRIPE_LEN (64 * 1024) #define BTRFS_NUM_MIRRORS 2 struct recover_control { diff --git a/cmds-chunk.c b/cmds-chunk.c index 4d7fce0..348229c 100644 --- a/cmds-chunk.c +++ b/cmds-chunk.c @@ -42,7 +42,6 @@ #include "commands.h" #define BTRFS_CHUNK_TREE_REBUILD_ABORTED -7500 -#define BTRFS_STRIPE_LEN (64 * 1024) #define BTRFS_NUM_MIRRORS 2 struct recover_control { diff --git a/volumes.h b/volumes.h index 2802cb0..b1ff3d0 100644 --- a/volumes.h +++ b/volumes.h @@ -19,6 +19,8 @@ #ifndef __BTRFS_VOLUMES_ #define __BTRFS_VOLUMES_ +#define BTRFS_STRIPE_LEN (64 * 1024) + struct btrfs_device { struct list_head dev_list; struct btrfs_root *dev_root; -- 1.7.1 -- 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: Anand Jain <anand.jain@oracle.com> --- v3: volume.c needs BTRFS_STRIPE_LEN as well v2: commit update btrfs-convert.c | 2 +- btrfs-image.c | 2 +- volumes.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index 65fe707..df20c15 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -1715,7 +1715,7 @@ static int prepare_system_chunk_sb(struct btrfs_super_block *super) btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super)); btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID); - btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024); + btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM); btrfs_set_stack_chunk_io_align(chunk, sectorsize); btrfs_set_stack_chunk_io_width(chunk, sectorsize); diff --git a/btrfs-image.c b/btrfs-image.c index 7bcfc06..1b2831a 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -1350,7 +1350,7 @@ static void update_super_old(u8 *buffer) btrfs_set_stack_chunk_length(chunk, (u64)-1); btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID); - btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024); + btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN); btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM); btrfs_set_stack_chunk_io_align(chunk, sectorsize); btrfs_set_stack_chunk_io_width(chunk, sectorsize); diff --git a/volumes.c b/volumes.c index c38da6c..7a9c955 100644 --- a/volumes.c +++ b/volumes.c @@ -773,7 +773,7 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, int looped = 0; int ret; int index; - int stripe_len = 64 * 1024; + int stripe_len = BTRFS_STRIPE_LEN; struct btrfs_key key; u64 offset; -- 1.7.1 -- 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
Anand Jain
2013-Dec-17 11:16 UTC
[PATCH v3 3/3] btrfs-progs: handle error in the btrfs_prepare_device
this patch will handle the strerror reporting of the error instead of printing errno, and also replaced the BUG_ON with the error handling Signed-off-by: Anand Jain <anand.jain@oracle.com> --- v3: fix per Stefan review, update error message v2: commit update cmds-device.c | 7 +++---- cmds-replace.c | 9 ++++----- mkfs.c | 9 ++++++++- utils.c | 30 +++++++++++++++++++----------- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/cmds-device.c b/cmds-device.c index bc4a8dc..ada0bcd 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -111,13 +111,11 @@ static int cmd_add_dev(int argc, char **argv) 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); @@ -130,6 +128,7 @@ static int cmd_add_dev(int argc, char **argv) } +error_out: close_file_or_dir(fdmnt, dirstream); return !!ret; } diff --git a/cmds-replace.c b/cmds-replace.c index d9b0940..c683d6c 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -276,12 +276,11 @@ static int cmd_start_replace(int argc, char **argv) } 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; diff --git a/mkfs.c b/mkfs.c index 33369f9..18df087 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1446,6 +1446,10 @@ int main(int ac, char **av) 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); @@ -1553,8 +1557,11 @@ int main(int ac, char **av) } 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); diff --git a/utils.c b/utils.c index f499023..03947bd 100644 --- a/utils.c +++ b/utils.c @@ -581,13 +581,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, 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); @@ -612,26 +612,34 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, } 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) { + ret < 0 ? + fprintf(stderr, "ERROR: failed to zero device ''%s'' - %s\n", + file, strerror(-ret)) : + fprintf(stderr, "ERROR: failed to zero device ''%s'' - %d\n", + file, ret); + return 1; + } return 0; } -- 1.7.1 -- 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
David Sterba
2013-Dec-17 14:18 UTC
Re: [PATCH v2 3/3] btrfs-progs: handle error in the btrfs_prepare_device
On Tue, Dec 17, 2013 at 04:37:35PM +0800, Anand Jain wrote:> >>+zero_dev_error: > >>+ if (ret) { > >>+ ret < 0 ? > >>+ fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %s\n", > >>+ file, strerror(-ret)) : > >>+ fprintf(stderr, "ERROR: failed to zero device start ''%s'' - %d\n", > >>+ file, ret); > > > >This is not funny. > > hmm. I am not sure what you mean ?It''s rather obfuscated, though it''s a valid C. No need to minimize the source line count but the time to understand them. -- 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
Anand Jain
2013-Dec-18 04:07 UTC
[PATCH v4 3/3] btrfs-progs: handle error in the btrfs_prepare_device
this patch will handle the strerror reporting of the error instead of printing errno, and also replaced the BUG_ON with the error handling Signed-off-by: Anand Jain <anand.jain@oracle.com> --- v4: replaced ? statement with proper if statement v3: fix per Stefan review, update error message v2: commit update cmds-device.c | 7 +++---- cmds-replace.c | 9 ++++----- mkfs.c | 9 ++++++++- utils.c | 31 ++++++++++++++++++++----------- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/cmds-device.c b/cmds-device.c index bc4a8dc..ada0bcd 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -111,13 +111,11 @@ static int cmd_add_dev(int argc, char **argv) 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); @@ -130,6 +128,7 @@ static int cmd_add_dev(int argc, char **argv) } +error_out: close_file_or_dir(fdmnt, dirstream); return !!ret; } diff --git a/cmds-replace.c b/cmds-replace.c index d9b0940..c683d6c 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -276,12 +276,11 @@ static int cmd_start_replace(int argc, char **argv) } 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; diff --git a/mkfs.c b/mkfs.c index 33369f9..18df087 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1446,6 +1446,10 @@ int main(int ac, char **av) 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); @@ -1553,8 +1557,11 @@ int main(int ac, char **av) } 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); diff --git a/utils.c b/utils.c index f499023..f37083a 100644 --- a/utils.c +++ b/utils.c @@ -581,13 +581,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, 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); @@ -612,26 +612,35 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, } 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; } -- 1.7.1 -- 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