9ae3a8
From 8e44095beac9a5c6da5e91339c5cf7746afaa623 Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Thu, 7 Nov 2013 12:53:07 +0100
9ae3a8
Subject: [PATCH 61/87] blockdev: blockdev_init() error conversion
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1383660558-32096-21-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: 55399
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 20/24] blockdev: blockdev_init() error conversion
9ae3a8
Bugzilla: 978402
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
9ae3a8
This gives us meaningful error messages for the blockdev-add QMP
9ae3a8
command.
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
(cherry picked from commit b681072d2005911b79835d2a6af208eba3983a48)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	blockdev.c
9ae3a8
9ae3a8
Conflict caused by do_check_io_limits(), which has been replaced by
9ae3a8
check_throttle_config() in upstream (commit cc0681c4 'block: Enable the
9ae3a8
new throttling code in the block layer.')
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 blockdev.c |   56 +++++++++++++++++++++++++++++++-------------------------
9ae3a8
 1 files changed, 31 insertions(+), 25 deletions(-)
9ae3a8
9ae3a8
diff --git a/blockdev.c b/blockdev.c
9ae3a8
index cbf01eb..a248480 100644
9ae3a8
--- a/blockdev.c
9ae3a8
+++ b/blockdev.c
9ae3a8
@@ -275,7 +275,7 @@ static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
9ae3a8
 }
9ae3a8
 #endif
9ae3a8
 
9ae3a8
-static int parse_block_error_action(const char *buf, bool is_read)
9ae3a8
+static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
9ae3a8
 {
9ae3a8
     if (!strcmp(buf, "ignore")) {
9ae3a8
         return BLOCKDEV_ON_ERROR_IGNORE;
9ae3a8
@@ -286,8 +286,8 @@ static int parse_block_error_action(const char *buf, bool is_read)
9ae3a8
     } else if (!strcmp(buf, "report")) {
9ae3a8
         return BLOCKDEV_ON_ERROR_REPORT;
9ae3a8
     } else {
9ae3a8
-        error_report("'%s' invalid %s error action",
9ae3a8
-                     buf, is_read ? "read" : "write");
9ae3a8
+        error_setg(errp, "'%s' invalid %s error action",
9ae3a8
+                   buf, is_read ? "read" : "write");
9ae3a8
         return -1;
9ae3a8
     }
9ae3a8
 }
9ae3a8
@@ -328,7 +328,8 @@ typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
9ae3a8
 
9ae3a8
 /* Takes the ownership of bs_opts */
9ae3a8
 static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
-                                BlockInterfaceType type)
9ae3a8
+                                BlockInterfaceType type,
9ae3a8
+                                Error **errp)
9ae3a8
 {
9ae3a8
     const char *buf;
9ae3a8
     const char *file = NULL;
9ae3a8
@@ -352,15 +353,13 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
     id = qdict_get_try_str(bs_opts, "id");
9ae3a8
     opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
9ae3a8
     if (error_is_set(&error)) {
9ae3a8
-        qerror_report_err(error);
9ae3a8
-        error_free(error);
9ae3a8
+        error_propagate(errp, error);
9ae3a8
         return NULL;
9ae3a8
     }
9ae3a8
 
9ae3a8
     qemu_opts_absorb_qdict(opts, bs_opts, &error);
9ae3a8
     if (error_is_set(&error)) {
9ae3a8
-        qerror_report_err(error);
9ae3a8
-        error_free(error);
9ae3a8
+        error_propagate(errp, error);
9ae3a8
         return NULL;
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -380,7 +379,7 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
 
9ae3a8
     if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
9ae3a8
         if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
9ae3a8
-            error_report("invalid discard option");
9ae3a8
+            error_setg(errp, "invalid discard option");
9ae3a8
             return NULL;
9ae3a8
         }
9ae3a8
     }
9ae3a8
@@ -403,7 +402,7 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
         } else if (!strcmp(buf, "threads")) {
9ae3a8
             /* this is the default */
9ae3a8
         } else {
9ae3a8
-           error_report("invalid aio option");
9ae3a8
+           error_setg(errp, "invalid aio option");
9ae3a8
            return NULL;
9ae3a8
         }
9ae3a8
     }
9ae3a8
@@ -419,7 +418,7 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
 
9ae3a8
         drv = bdrv_find_whitelisted_format(buf, ro);
9ae3a8
         if (!drv) {
9ae3a8
-            error_report("'%s' invalid format", buf);
9ae3a8
+            error_setg(errp, "'%s' invalid format", buf);
9ae3a8
             return NULL;
9ae3a8
         }
9ae3a8
     }
9ae3a8
@@ -439,20 +438,20 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
         qemu_opt_get_number(opts, "throttling.iops-write", 0);
9ae3a8
 
9ae3a8
     if (!do_check_io_limits(&io_limits, &error)) {
9ae3a8
-        error_report("%s", error_get_pretty(error));
9ae3a8
-        error_free(error);
9ae3a8
+        error_propagate(errp, error);
9ae3a8
         return NULL;
9ae3a8
     }
9ae3a8
 
9ae3a8
     on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
9ae3a8
     if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
9ae3a8
         if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
9ae3a8
-            error_report("werror is not supported by this bus type");
9ae3a8
+            error_setg(errp, "werror is not supported by this bus type");
9ae3a8
             return NULL;
9ae3a8
         }
9ae3a8
 
9ae3a8
-        on_write_error = parse_block_error_action(buf, 0);
9ae3a8
-        if (on_write_error < 0) {
9ae3a8
+        on_write_error = parse_block_error_action(buf, 0, &error);
9ae3a8
+        if (error_is_set(&error)) {
9ae3a8
+            error_propagate(errp, error);
9ae3a8
             return NULL;
9ae3a8
         }
9ae3a8
     }
9ae3a8
@@ -464,8 +463,9 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
             return NULL;
9ae3a8
         }
9ae3a8
 
9ae3a8
-        on_read_error = parse_block_error_action(buf, 1);
9ae3a8
-        if (on_read_error < 0) {
9ae3a8
+        on_read_error = parse_block_error_action(buf, 1, &error);
9ae3a8
+        if (error_is_set(&error)) {
9ae3a8
+            error_propagate(errp, error);
9ae3a8
             return NULL;
9ae3a8
         }
9ae3a8
     }
9ae3a8
@@ -515,8 +515,9 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
9ae3a8
     ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error);
9ae3a8
 
9ae3a8
     if (ret < 0) {
9ae3a8
-        error_report("could not open disk image %s: %s",
9ae3a8
-                     file ?: dinfo->id, error_get_pretty(error));
9ae3a8
+        error_setg(errp, "could not open disk image %s: %s",
9ae3a8
+                   file ?: dinfo->id, error_get_pretty(error));
9ae3a8
+        error_free(error);
9ae3a8
         goto err;
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -852,9 +853,15 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* Actual block device init: Functionality shared with blockdev-add */
9ae3a8
-    dinfo = blockdev_init(bs_opts, type);
9ae3a8
+    dinfo = blockdev_init(bs_opts, type, &local_err);
9ae3a8
     if (dinfo == NULL) {
9ae3a8
+        if (error_is_set(&local_err)) {
9ae3a8
+            qerror_report_err(local_err);
9ae3a8
+            error_free(local_err);
9ae3a8
+        }
9ae3a8
         goto fail;
9ae3a8
+    } else {
9ae3a8
+        assert(!error_is_set(&local_err));
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* Set legacy DriveInfo fields */
9ae3a8
@@ -1745,7 +1752,6 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
9ae3a8
     QmpOutputVisitor *ov = qmp_output_visitor_new();
9ae3a8
     QObject *obj;
9ae3a8
     QDict *qdict;
9ae3a8
-    DriveInfo *dinfo;
9ae3a8
     Error *local_err = NULL;
9ae3a8
 
9ae3a8
     /* Require an ID in the top level */
9ae3a8
@@ -1779,9 +1785,9 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
9ae3a8
 
9ae3a8
     qdict_flatten(qdict);
9ae3a8
 
9ae3a8
-    dinfo = blockdev_init(qdict, IF_NONE);
9ae3a8
-    if (!dinfo) {
9ae3a8
-        error_setg(errp, "Could not open image");
9ae3a8
+    blockdev_init(qdict, IF_NONE, &local_err);
9ae3a8
+    if (error_is_set(&local_err)) {
9ae3a8
+        error_propagate(errp, local_err);
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8