return 0;
}
+static int64_t cvtnum(const char *s)
+{
+ int err;
+ uint64_t value;
+
+ err = qemu_strtosz(s, NULL, &value);
+ if (err < 0) {
+ return err;
+ }
+ if (value > INT64_MAX) {
+ return -ERANGE;
+ }
+ return value;
+}
+
static int img_create(int argc, char **argv)
{
int c;
/* Get image size, if specified */
if (optind < argc) {
int64_t sval;
- char *end;
- sval = qemu_strtosz_suffix(argv[optind++], &end,
- QEMU_STRTOSZ_DEFSUFFIX_B);
- if (sval < 0 || *end) {
+
+ sval = cvtnum(argv[optind++]);
+ if (sval < 0) {
if (sval == -ERANGE) {
error_report("Image size must be less than 8 EiB!");
} else {
if (base) {
base_bs = bdrv_find_backing_image(bs, base);
if (!base_bs) {
- error_setg(&local_err, QERR_BASE_NOT_FOUND, base);
+ error_setg(&local_err,
+ "Did not find '%s' in the backing chain of '%s'",
+ base, filename);
goto done;
}
} else {
aio_context = bdrv_get_aio_context(bs);
aio_context_acquire(aio_context);
- commit_active_start("commit", bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT,
- common_block_job_cb, &cbi, &local_err, false);
+ commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0,
+ BLOCKDEV_ON_ERROR_REPORT, common_block_job_cb, &cbi,
+ &local_err, false);
aio_context_release(aio_context);
if (local_err) {
goto done;
case 'S':
{
int64_t sval;
- char *end;
- sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
- if (sval < 0 || *end) {
+
+ sval = cvtnum(optarg);
+ if (sval < 0) {
error_report("Invalid minimum zero buffer size for sparse output specified");
ret = -1;
goto fail_getopt;
}
if (sn_opts) {
- ret = bdrv_snapshot_load_tmp(bs[0],
- qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
- qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
- &local_err);
+ bdrv_snapshot_load_tmp(bs[0],
+ qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
+ qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
+ &local_err);
} else if (snapshot_name != NULL) {
if (bs_n > 1) {
error_report("No support for concatenating multiple snapshot");
create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
- if (options) {
- qemu_opts_do_parse(opts, options, NULL, &err);
- if (err) {
- error_report_err(err);
- ret = -1;
- goto out;
- }
+ qemu_opts_do_parse(opts, options, NULL, &err);
+ if (err) {
+ error_report_err(err);
+ ret = -1;
+ goto out;
}
/* In case the driver does not call amend_status_cb() */
}
while (b->n > b->in_flight && b->in_flight < b->nrreq) {
+ int64_t offset = b->offset;
+ /* blk_aio_* might look for completed I/Os and kick bench_cb
+ * again, so make sure this operation is counted by in_flight
+ * and b->offset is ready for the next submission.
+ */
+ b->in_flight++;
+ b->offset += b->step;
+ b->offset %= b->image_size;
if (b->write) {
- acb = blk_aio_pwritev(b->blk, b->offset, b->qiov, 0,
- bench_cb, b);
+ acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b);
} else {
- acb = blk_aio_preadv(b->blk, b->offset, b->qiov, 0,
- bench_cb, b);
+ acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b);
}
if (!acb) {
error_report("Failed to issue request");
exit(EXIT_FAILURE);
}
- b->in_flight++;
- b->offset += b->step;
- b->offset %= b->image_size;
}
}
break;
case 'c':
{
- char *end;
- errno = 0;
- count = strtoul(optarg, &end, 0);
- if (errno || *end || count > INT_MAX) {
+ unsigned long res;
+
+ if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
error_report("Invalid request count specified");
return 1;
}
+ count = res;
break;
}
case 'd':
{
- char *end;
- errno = 0;
- depth = strtoul(optarg, &end, 0);
- if (errno || *end || depth > INT_MAX) {
+ unsigned long res;
+
+ if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
error_report("Invalid queue depth specified");
return 1;
}
+ depth = res;
break;
}
case 'f':
break;
case 'o':
{
- char *end;
- errno = 0;
- offset = qemu_strtosz_suffix(optarg, &end,
- QEMU_STRTOSZ_DEFSUFFIX_B);
- if (offset < 0|| *end) {
+ offset = cvtnum(optarg);
+ if (offset < 0) {
error_report("Invalid offset specified");
return 1;
}
case 's':
{
int64_t sval;
- char *end;
- sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
- if (sval < 0 || sval > INT_MAX || *end) {
+ sval = cvtnum(optarg);
+ if (sval < 0 || sval > INT_MAX) {
error_report("Invalid buffer size specified");
return 1;
}
case 'S':
{
int64_t sval;
- char *end;
- sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
- if (sval < 0 || sval > INT_MAX || *end) {
+ sval = cvtnum(optarg);
+ if (sval < 0 || sval > INT_MAX) {
error_report("Invalid step size specified");
return 1;
}
break;
case OPTION_PATTERN:
{
- char *end;
- errno = 0;
- pattern = strtoul(optarg, &end, 0);
- if (errno || *end || pattern > 0xff) {
+ unsigned long res;
+
+ if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) {
error_report("Invalid pattern byte specified");
return 1;
}
+ pattern = res;
break;
}
case OPTION_FLUSH_INTERVAL:
{
- char *end;
- errno = 0;
- flush_interval = strtoul(optarg, &end, 0);
- if (errno || *end || flush_interval > INT_MAX) {
+ unsigned long res;
+
+ if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) {
error_report("Invalid flush interval specified");
return 1;
}
+ flush_interval = res;
break;
}
case OPTION_NO_DRAIN:
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- char *end;
int64_t res;
- res = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
+ res = cvtnum(arg);
- if (res <= 0 || res > INT_MAX || *end) {
+ if (res <= 0 || res > INT_MAX) {
error_report("invalid number: '%s'", arg);
return 1;
}
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- char *end;
-
- dd->count = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
+ dd->count = cvtnum(arg);
- if (dd->count < 0 || *end) {
+ if (dd->count < 0) {
error_report("invalid number: '%s'", arg);
return 1;
}
struct DdIo *in, struct DdIo *out,
struct DdInfo *dd)
{
- char *end;
-
- in->offset = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
+ in->offset = cvtnum(arg);
- if (in->offset < 0 || *end) {
+ if (in->offset < 0) {
error_report("invalid number: '%s'", arg);
return 1;
}