From 43d589b074370ebc9b340340b5f641b385da9df8 Mon Sep 17 00:00:00 2001 From: Eyal Moscovici Date: Wed, 13 May 2020 16:36:26 +0300 Subject: qemu_img: add cvtnum_full to print error reports All calls to cvtnum check the return value and print the same error message more or less. And so error reporting moved to cvtnum_full to reduce code duplication and provide a single error message. Additionally, cvtnum now wraps cvtnum_full with the existing default range of 0 to MAX_INT64. Acked-by: Mark Kanda Signed-off-by: Eyal Moscovici Message-Id: <20200513133629.18508-2-eyal.moscovici@oracle.com> Reviewed-by: Eric Blake [eblake: fix printf formatting, avoid trailing space, change error wording, reformat commit message] Signed-off-by: Eric Blake --- qemu-img.c | 76 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 42 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 6a4327aaba..5d824fc15f 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -470,19 +470,31 @@ static int add_old_style_options(const char *fmt, QemuOpts *opts, return 0; } -static int64_t cvtnum(const char *s) +static int64_t cvtnum_full(const char *name, const char *value, int64_t min, + int64_t max) { int err; - uint64_t value; - - err = qemu_strtosz(s, NULL, &value); - if (err < 0) { + uint64_t res; + + err = qemu_strtosz(value, NULL, &res); + if (err < 0 && err != -ERANGE) { + error_report("Invalid %s specified. You may use " + "k, M, G, T, P or E suffixes for", name); + error_report("kilobytes, megabytes, gigabytes, terabytes, " + "petabytes and exabytes."); return err; } - if (value > INT64_MAX) { + if (err == -ERANGE || res > max || res < min) { + error_report("Invalid %s specified. Must be between %" PRId64 + " and %" PRId64 ".", name, min, max); return -ERANGE; } - return value; + return res; +} + +static int64_t cvtnum(const char *name, const char *value) +{ + return cvtnum_full(name, value, 0, INT64_MAX); } static int img_create(int argc, char **argv) @@ -572,16 +584,8 @@ static int img_create(int argc, char **argv) if (optind < argc) { int64_t sval; - sval = cvtnum(argv[optind++]); + sval = cvtnum("image size", argv[optind++]); if (sval < 0) { - if (sval == -ERANGE) { - error_report("Image size must be less than 8 EiB!"); - } else { - error_report("Invalid image size specified! You may use k, M, " - "G, T, P or E suffixes for "); - error_report("kilobytes, megabytes, gigabytes, terabytes, " - "petabytes and exabytes."); - } goto fail; } img_size = (uint64_t)sval; @@ -2187,8 +2191,10 @@ static int img_convert(int argc, char **argv) { int64_t sval; - sval = cvtnum(optarg); - if (sval < 0 || !QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || + sval = cvtnum("buffer size for sparse output", optarg); + if (sval < 0) { + goto fail_getopt; + } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { error_report("Invalid buffer size for sparse output specified. " "Valid sizes are multiples of %llu up to %llu. Select " @@ -4291,9 +4297,8 @@ static int img_bench(int argc, char **argv) break; case 'o': { - offset = cvtnum(optarg); + offset = cvtnum("offset", optarg); if (offset < 0) { - error_report("Invalid offset specified"); return 1; } break; @@ -4306,9 +4311,8 @@ static int img_bench(int argc, char **argv) { int64_t sval; - sval = cvtnum(optarg); - if (sval < 0 || sval > INT_MAX) { - error_report("Invalid buffer size specified"); + sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); + if (sval < 0) { return 1; } @@ -4319,9 +4323,8 @@ static int img_bench(int argc, char **argv) { int64_t sval; - sval = cvtnum(optarg); - if (sval < 0 || sval > INT_MAX) { - error_report("Invalid step size specified"); + sval = cvtnum_full("step_size", optarg, 0, INT_MAX); + if (sval < 0) { return 1; } @@ -4491,10 +4494,9 @@ static int img_dd_bs(const char *arg, { int64_t res; - res = cvtnum(arg); + res = cvtnum_full("bs", arg, 1, INT_MAX); - if (res <= 0 || res > INT_MAX) { - error_report("invalid number: '%s'", arg); + if (res < 0) { return 1; } in->bsz = out->bsz = res; @@ -4506,10 +4508,9 @@ static int img_dd_count(const char *arg, struct DdIo *in, struct DdIo *out, struct DdInfo *dd) { - dd->count = cvtnum(arg); + dd->count = cvtnum("count", arg); if (dd->count < 0) { - error_report("invalid number: '%s'", arg); return 1; } @@ -4538,10 +4539,9 @@ static int img_dd_skip(const char *arg, struct DdIo *in, struct DdIo *out, struct DdInfo *dd) { - in->offset = cvtnum(arg); + in->offset = cvtnum("skip", arg); if (in->offset < 0) { - error_report("invalid number: '%s'", arg); return 1; } @@ -4923,16 +4923,8 @@ static int img_measure(int argc, char **argv) { int64_t sval; - sval = cvtnum(optarg); + sval = cvtnum("image size", optarg); if (sval < 0) { - if (sval == -ERANGE) { - error_report("Image size must be less than 8 EiB!"); - } else { - error_report("Invalid image size specified! You may use " - "k, M, G, T, P or E suffixes for "); - error_report("kilobytes, megabytes, gigabytes, terabytes, " - "petabytes and exabytes."); - } goto out; } img_size = (uint64_t)sval; -- cgit v1.2.3 From 8f282e83edd3d1b4ea6e9258f5a4081b490c33cc Mon Sep 17 00:00:00 2001 From: Eyal Moscovici Date: Wed, 13 May 2020 16:36:27 +0300 Subject: qemu-img: validate image length in img_map The code handles this case correctly: we merely skip the loop. However it is probably best to return an explicit error. Reviewed-by: Eric Blake Acked-by: Mark Kanda Signed-off-by: Eyal Moscovici Message-Id: <20200513133629.18508-3-eyal.moscovici@oracle.com> [eblake: commit message tweak] Signed-off-by: Eric Blake --- qemu-img.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 5d824fc15f..c88f412333 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3092,6 +3092,11 @@ static int img_map(int argc, char **argv) } length = blk_getlength(blk); + if (length < 0) { + error_report("Failed to get size for '%s'", filename); + return 1; + } + while (curr.start + curr.length < length) { int64_t offset = curr.start + curr.length; int64_t n; -- cgit v1.2.3 From e46c0b18cfd02195a0d527ca73f3ed9f3ce5eacb Mon Sep 17 00:00:00 2001 From: Eyal Moscovici Date: Wed, 13 May 2020 16:36:28 +0300 Subject: qemu-img: refactor dump_map_entry JSON format output Previously dump_map_entry identified whether we need to start a new JSON array based on whether start address == 0. In this refactor we remove this assumption as in following patches we will allow map to start from an arbitrary position. Reviewed-by: Eric Blake Acked-by: Mark Kanda Signed-off-by: Eyal Moscovici Message-Id: <20200513133629.18508-4-eyal.moscovici@oracle.com> Signed-off-by: Eric Blake --- qemu-img.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index c88f412333..4aa9414aba 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -2902,9 +2902,8 @@ static int dump_map_entry(OutputFormat output_format, MapEntry *e, } break; case OFORMAT_JSON: - printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," + printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", - (e->start == 0 ? "[" : ",\n"), e->start, e->length, e->depth, e->zero ? "true" : "false", e->data ? "true" : "false"); @@ -2913,8 +2912,8 @@ static int dump_map_entry(OutputFormat output_format, MapEntry *e, } putchar('}'); - if (!next) { - printf("]\n"); + if (next) { + puts(","); } break; } @@ -3089,6 +3088,8 @@ static int img_map(int argc, char **argv) if (output_format == OFORMAT_HUMAN) { printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); + } else if (output_format == OFORMAT_JSON) { + putchar('['); } length = blk_getlength(blk); @@ -3125,6 +3126,9 @@ static int img_map(int argc, char **argv) } ret = dump_map_entry(output_format, &curr, NULL); + if (output_format == OFORMAT_JSON) { + puts("]"); + } out: blk_unref(blk); -- cgit v1.2.3 From c0469496b32910a6a092d5b614efbf4088b13a29 Mon Sep 17 00:00:00 2001 From: Eyal Moscovici Date: Wed, 13 May 2020 16:36:29 +0300 Subject: qemu-img: Add --start-offset and --max-length to map The mapping operation of large disks especially ones stored over a long chain of QCOW2 files can take a long time to finish. Additionally when mapping fails there was no way recover by restarting the mapping from the failed location. The new options, --start-offset and --max-length allows the user to divide these type of map operations into shorter independent tasks. Reviewed-by: Eric Blake Acked-by: Mark Kanda Co-developed-by: Yoav Elnekave Signed-off-by: Yoav Elnekave Signed-off-by: Eyal Moscovici Message-Id: <20200513133629.18508-5-eyal.moscovici@oracle.com> Signed-off-by: Eric Blake --- qemu-img.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index 4aa9414aba..947bf8b34b 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3009,6 +3009,8 @@ static int img_map(int argc, char **argv) int ret = 0; bool image_opts = false; bool force_share = false; + int64_t start_offset = 0; + int64_t max_length = -1; fmt = NULL; output = NULL; @@ -3021,9 +3023,11 @@ static int img_map(int argc, char **argv) {"object", required_argument, 0, OPTION_OBJECT}, {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, {"force-share", no_argument, 0, 'U'}, + {"start-offset", required_argument, 0, 's'}, + {"max-length", required_argument, 0, 'l'}, {0, 0, 0, 0} }; - c = getopt_long(argc, argv, ":f:hU", + c = getopt_long(argc, argv, ":f:s:l:hU", long_options, &option_index); if (c == -1) { break; @@ -3047,6 +3051,18 @@ static int img_map(int argc, char **argv) case OPTION_OUTPUT: output = optarg; break; + case 's': + start_offset = cvtnum("start offset", optarg); + if (start_offset < 0) { + return 1; + } + break; + case 'l': + max_length = cvtnum("max length", optarg); + if (max_length < 0) { + return 1; + } + break; case OPTION_OBJECT: { QemuOpts *opts; opts = qemu_opts_parse_noisily(&qemu_object_opts, @@ -3097,7 +3113,11 @@ static int img_map(int argc, char **argv) error_report("Failed to get size for '%s'", filename); return 1; } + if (max_length != -1) { + length = MIN(start_offset + max_length, length); + } + curr.start = start_offset; while (curr.start + curr.length < length) { int64_t offset = curr.start + curr.length; int64_t n; -- cgit v1.2.3