Skip to content

Commit b681072

Browse files
committed
blockdev: blockdev_init() error conversion
This gives us meaningful error messages for the blockdev-add QMP command. Signed-off-by: Kevin Wolf <[email protected]> Reviewed-by: Max Reitz <[email protected]> Reviewed-by: Eric Blake <[email protected]>
1 parent 0ebd24e commit b681072

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

blockdev.c

+31-25
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
272272
qemu_bh_schedule(s->bh);
273273
}
274274

275-
static int parse_block_error_action(const char *buf, bool is_read)
275+
static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
276276
{
277277
if (!strcmp(buf, "ignore")) {
278278
return BLOCKDEV_ON_ERROR_IGNORE;
@@ -283,8 +283,8 @@ static int parse_block_error_action(const char *buf, bool is_read)
283283
} else if (!strcmp(buf, "report")) {
284284
return BLOCKDEV_ON_ERROR_REPORT;
285285
} else {
286-
error_report("'%s' invalid %s error action",
287-
buf, is_read ? "read" : "write");
286+
error_setg(errp, "'%s' invalid %s error action",
287+
buf, is_read ? "read" : "write");
288288
return -1;
289289
}
290290
}
@@ -309,7 +309,8 @@ typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
309309

310310
/* Takes the ownership of bs_opts */
311311
static DriveInfo *blockdev_init(QDict *bs_opts,
312-
BlockInterfaceType type)
312+
BlockInterfaceType type,
313+
Error **errp)
313314
{
314315
const char *buf;
315316
const char *file = NULL;
@@ -333,15 +334,13 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
333334
id = qdict_get_try_str(bs_opts, "id");
334335
opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
335336
if (error_is_set(&error)) {
336-
qerror_report_err(error);
337-
error_free(error);
337+
error_propagate(errp, error);
338338
return NULL;
339339
}
340340

341341
qemu_opts_absorb_qdict(opts, bs_opts, &error);
342342
if (error_is_set(&error)) {
343-
qerror_report_err(error);
344-
error_free(error);
343+
error_propagate(errp, error);
345344
return NULL;
346345
}
347346

@@ -361,7 +360,7 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
361360

362361
if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
363362
if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
364-
error_report("invalid discard option");
363+
error_setg(errp, "invalid discard option");
365364
return NULL;
366365
}
367366
}
@@ -383,7 +382,7 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
383382
} else if (!strcmp(buf, "threads")) {
384383
/* this is the default */
385384
} else {
386-
error_report("invalid aio option");
385+
error_setg(errp, "invalid aio option");
387386
return NULL;
388387
}
389388
}
@@ -399,7 +398,7 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
399398

400399
drv = bdrv_find_format(buf);
401400
if (!drv) {
402-
error_report("'%s' invalid format", buf);
401+
error_setg(errp, "'%s' invalid format", buf);
403402
return NULL;
404403
}
405404
}
@@ -435,20 +434,20 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
435434
cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
436435

437436
if (!check_throttle_config(&cfg, &error)) {
438-
error_report("%s", error_get_pretty(error));
439-
error_free(error);
437+
error_propagate(errp, error);
440438
return NULL;
441439
}
442440

443441
on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
444442
if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
445443
if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
446-
error_report("werror is not supported by this bus type");
444+
error_setg(errp, "werror is not supported by this bus type");
447445
return NULL;
448446
}
449447

450-
on_write_error = parse_block_error_action(buf, 0);
451-
if (on_write_error < 0) {
448+
on_write_error = parse_block_error_action(buf, 0, &error);
449+
if (error_is_set(&error)) {
450+
error_propagate(errp, error);
452451
return NULL;
453452
}
454453
}
@@ -460,8 +459,9 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
460459
return NULL;
461460
}
462461

463-
on_read_error = parse_block_error_action(buf, 1);
464-
if (on_read_error < 0) {
462+
on_read_error = parse_block_error_action(buf, 1, &error);
463+
if (error_is_set(&error)) {
464+
error_propagate(errp, error);
465465
return NULL;
466466
}
467467
}
@@ -514,8 +514,9 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
514514
ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error);
515515

516516
if (ret < 0) {
517-
error_report("could not open disk image %s: %s",
518-
file ?: dinfo->id, error_get_pretty(error));
517+
error_setg(errp, "could not open disk image %s: %s",
518+
file ?: dinfo->id, error_get_pretty(error));
519+
error_free(error);
519520
goto err;
520521
}
521522

@@ -862,9 +863,15 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
862863
}
863864

864865
/* Actual block device init: Functionality shared with blockdev-add */
865-
dinfo = blockdev_init(bs_opts, type);
866+
dinfo = blockdev_init(bs_opts, type, &local_err);
866867
if (dinfo == NULL) {
868+
if (error_is_set(&local_err)) {
869+
qerror_report_err(local_err);
870+
error_free(local_err);
871+
}
867872
goto fail;
873+
} else {
874+
assert(!error_is_set(&local_err));
868875
}
869876

870877
/* Set legacy DriveInfo fields */
@@ -2155,7 +2162,6 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
21552162
QmpOutputVisitor *ov = qmp_output_visitor_new();
21562163
QObject *obj;
21572164
QDict *qdict;
2158-
DriveInfo *dinfo;
21592165
Error *local_err = NULL;
21602166

21612167
/* Require an ID in the top level */
@@ -2189,9 +2195,9 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
21892195

21902196
qdict_flatten(qdict);
21912197

2192-
dinfo = blockdev_init(qdict, IF_NONE);
2193-
if (!dinfo) {
2194-
error_setg(errp, "Could not open image");
2198+
blockdev_init(qdict, IF_NONE, &local_err);
2199+
if (error_is_set(&local_err)) {
2200+
error_propagate(errp, local_err);
21952201
goto fail;
21962202
}
21972203

0 commit comments

Comments
 (0)