From 5407ee85fcaba91c9a0207ffad97a9b8b1c45c8c Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 28 Jul 2016 01:47:40 +0200 Subject: btrfs-progs: refactor and extend btrfs_prepare_device arguments The message about discard is printed unconditionally and does not conform to the --quite option eg. in mkfs. Consolidate the operation flags into one argument and add support for verbosity. Signed-off-by: David Sterba --- cmds-device.c | 5 +++-- cmds-replace.c | 4 ++-- mkfs.c | 14 ++++++++++---- utils.c | 13 +++++++------ utils.h | 8 ++++++-- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cmds-device.c b/cmds-device.c index 59695feb..a939c56f 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -107,8 +107,9 @@ static int cmd_device_add(int argc, char **argv) continue; } - res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count, - 0, discard); + res = btrfs_prepare_device(devfd, argv[i], &dev_block_count, 0, + PREP_DEVICE_ZERO_END | PREP_DEVICE_VERBOSE | + (discard ? PREP_DEVICE_DISCARD : 0)); close(devfd); if (res) { ret++; diff --git a/cmds-replace.c b/cmds-replace.c index 6036e2f9..d1bf057e 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -254,8 +254,8 @@ static int cmd_replace_start(int argc, char **argv) } strncpy((char *)start_args.start.tgtdev_name, dstdev, BTRFS_DEVICE_PATH_NAME_MAX); - ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0, - 0); + ret = btrfs_prepare_device(fddstdev, dstdev, &dstdev_block_count, 0, + PREP_DEVICE_ZERO_END | PREP_DEVICE_VERBOSE); if (ret) goto leave_with_error; diff --git a/mkfs.c b/mkfs.c index 9b9fe4c6..f30f0574 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1636,8 +1636,11 @@ int main(int argc, char **argv) strerror(errno)); exit(1); } - ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, - block_count, discard); + ret = btrfs_prepare_device(fd, file, &dev_block_count, + block_count, + (zero_end ? PREP_DEVICE_ZERO_END : 0) | + (discard ? PREP_DEVICE_DISCARD : 0) | + (verbose ? PREP_DEVICE_VERBOSE : 0)); if (ret) { close(fd); exit(1); @@ -1767,8 +1770,11 @@ int main(int argc, char **argv) close(fd); continue; } - ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, - block_count, discard); + ret = btrfs_prepare_device(fd, file, &dev_block_count, + block_count, + (verbose ? PREP_DEVICE_VERBOSE : 0) | + (zero_end ? PREP_DEVICE_ZERO_END : 0) | + (discard ? PREP_DEVICE_DISCARD : 0)); if (ret) { close(fd); exit(1); diff --git a/utils.c b/utils.c index f73b048c..79bf9466 100644 --- a/utils.c +++ b/utils.c @@ -1682,8 +1682,8 @@ out: return ret; } -int btrfs_prepare_device(int fd, const char *file, int zero_end, - u64 *block_count_ret, u64 max_block_count, int discard) +int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret, + u64 max_block_count, unsigned opflags) { u64 block_count; struct stat st; @@ -1703,15 +1703,16 @@ int btrfs_prepare_device(int fd, const char *file, int zero_end, if (max_block_count) block_count = min(block_count, max_block_count); - if (discard) { + if (opflags & PREP_DEVICE_DISCARD) { /* * We intentionally ignore errors from the discard ioctl. It * is not necessary for the mkfs functionality but just an * optimization. */ if (discard_range(fd, 0, 0) == 0) { - printf("Performing full device TRIM (%s) ...\n", - pretty_size(block_count)); + if (opflags & PREP_DEVICE_VERBOSE) + printf("Performing full device TRIM (%s) ...\n", + pretty_size(block_count)); discard_blocks(fd, 0, block_count); } } @@ -1720,7 +1721,7 @@ int btrfs_prepare_device(int fd, const char *file, int zero_end, for (i = 0 ; !ret && i < BTRFS_SUPER_MIRROR_MAX; i++) ret = zero_dev_clamped(fd, btrfs_sb_offset(i), BTRFS_SUPER_INFO_SIZE, block_count); - if (!ret && zero_end) + if (!ret && (opflags & PREP_DEVICE_ZERO_END)) ret = zero_dev_clamped(fd, block_count - ZERO_DEV_BYTES, ZERO_DEV_BYTES, block_count); diff --git a/utils.h b/utils.h index 98bfb34d..58121ced 100644 --- a/utils.h +++ b/utils.h @@ -147,12 +147,16 @@ struct btrfs_convert_context { void *fs_data; }; +#define PREP_DEVICE_ZERO_END (1U << 0) +#define PREP_DEVICE_DISCARD (1U << 1) +#define PREP_DEVICE_VERBOSE (1U << 2) + int make_btrfs(int fd, struct btrfs_mkfs_config *cfg, struct btrfs_convert_context *cctx); int btrfs_make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 objectid); -int btrfs_prepare_device(int fd, const char *file, int zero_end, - u64 *block_count_ret, u64 max_block_count, int discard); +int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret, + u64 max_block_count, unsigned opflags); int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, struct btrfs_root *root, int fd, char *path, u64 block_count, u32 io_width, u32 io_align, -- cgit v1.2.3