218e99
From 8d4ddeff8f248b561affa518acc19739b76e2f08 Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Mon, 4 Nov 2013 22:32:11 +0100
218e99
Subject: [PATCH 18/87] block: Error parameter for open functions
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383604354-12743-21-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55320
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 20/43] block: Error parameter for open functions
218e99
Bugzilla: 1026524
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
218e99
BZ: 1026524
218e99
218e99
Add an Error ** parameter to bdrv_open, bdrv_file_open and associated
218e99
functions to allow more specific error messages.
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
(cherry picked from commit 34b5d2c68eb4082c288e70fb99c61af8f7b96fde)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
218e99
Conflicts:
218e99
	block/blkverify.c
218e99
	block/vmdk.c
218e99
	block/vvfat.c
218e99
	blockdev.c
218e99
	hw/block/xen_disk.c
218e99
	qemu-io.c
218e99
218e99
All files conflict because the series "Implement reference count for
218e99
BlockDriverState" has not yet been backported which "replaces"
218e99
bdrv_delete by bdrv_unref.
218e99
218e99
blockdev.c conflicts because the series "block: drive-backup live backup
218e99
command command" has not yet been backported.
218e99
218e99
qemu-io.c conflicts because the series "Make qemu-io commands available
218e99
in the monitor" has not yet been backported (esp. commit 734c3b85c).
218e99
---
218e99
 block.c               | 100 ++++++++++++++++++++++++++++++++------------------
218e99
 block/blkdebug.c      |   4 +-
218e99
 block/blkverify.c     |   8 +++-
218e99
 block/cow.c           |   5 ++-
218e99
 block/mirror.c        |   5 ++-
218e99
 block/qcow.c          |   5 ++-
218e99
 block/qcow2.c         |   4 +-
218e99
 block/qed.c           |   6 ++-
218e99
 block/sheepdog.c      |  10 ++++-
218e99
 block/vmdk.c          |  11 +++++-
218e99
 block/vvfat.c         |   6 ++-
218e99
 blockdev.c            |  26 ++++++-------
218e99
 hw/block/xen_disk.c   |   7 +++-
218e99
 include/block/block.h |   6 +--
218e99
 qemu-img.c            |  21 +++++++----
218e99
 qemu-io.c             |  14 +++++--
218e99
 qemu-nbd.c            |   6 ++-
218e99
 17 files changed, 161 insertions(+), 83 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block.c               |  100 +++++++++++++++++++++++++++++++-----------------
218e99
 block/blkdebug.c      |    4 +-
218e99
 block/blkverify.c     |    8 +++-
218e99
 block/cow.c           |    5 ++-
218e99
 block/mirror.c        |    5 +-
218e99
 block/qcow.c          |    5 ++-
218e99
 block/qcow2.c         |    4 +-
218e99
 block/qed.c           |    6 ++-
218e99
 block/sheepdog.c      |   10 ++++-
218e99
 block/vmdk.c          |   11 ++++-
218e99
 block/vvfat.c         |    6 ++-
218e99
 blockdev.c            |   26 +++++-------
218e99
 hw/block/xen_disk.c   |    7 +++-
218e99
 include/block/block.h |    6 +-
218e99
 qemu-img.c            |   21 +++++++---
218e99
 qemu-io.c             |   14 +++++--
218e99
 qemu-nbd.c            |    6 ++-
218e99
 17 files changed, 161 insertions(+), 83 deletions(-)
218e99
218e99
diff --git a/block.c b/block.c
218e99
index 0cb5ac9..cb99faf 100644
218e99
--- a/block.c
218e99
+++ b/block.c
218e99
@@ -524,7 +524,7 @@ BlockDriver *bdrv_find_protocol(const char *filename,
218e99
 }
218e99
 
218e99
 static int find_image_format(BlockDriverState *bs, const char *filename,
218e99
-                             BlockDriver **pdrv)
218e99
+                             BlockDriver **pdrv, Error **errp)
218e99
 {
218e99
     int score, score_max;
218e99
     BlockDriver *drv1, *drv;
218e99
@@ -535,6 +535,7 @@ static int find_image_format(BlockDriverState *bs, const char *filename,
218e99
     if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
218e99
         drv = bdrv_find_format("raw");
218e99
         if (!drv) {
218e99
+            error_setg(errp, "Could not find raw image format");
218e99
             ret = -ENOENT;
218e99
         }
218e99
         *pdrv = drv;
218e99
@@ -543,6 +544,8 @@ static int find_image_format(BlockDriverState *bs, const char *filename,
218e99
 
218e99
     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
218e99
     if (ret < 0) {
218e99
+        error_setg_errno(errp, -ret, "Could not read image for determining its "
218e99
+                         "format");
218e99
         *pdrv = NULL;
218e99
         return ret;
218e99
     }
218e99
@@ -559,6 +562,8 @@ static int find_image_format(BlockDriverState *bs, const char *filename,
218e99
         }
218e99
     }
218e99
     if (!drv) {
218e99
+        error_setg(errp, "Could not determine image format: No compatible "
218e99
+                   "driver found");
218e99
         ret = -ENOENT;
218e99
     }
218e99
     *pdrv = drv;
218e99
@@ -678,10 +683,11 @@ static int bdrv_open_flags(BlockDriverState *bs, int flags)
218e99
  * Removes all processed options from *options.
218e99
  */
218e99
 static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
218e99
-    QDict *options, int flags, BlockDriver *drv)
218e99
+    QDict *options, int flags, BlockDriver *drv, Error **errp)
218e99
 {
218e99
     int ret, open_flags;
218e99
     const char *filename;
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     assert(drv != NULL);
218e99
     assert(bs->file == NULL);
218e99
@@ -710,6 +716,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
218e99
     bs->read_only = !(open_flags & BDRV_O_RDWR);
218e99
 
218e99
     if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
218e99
+        error_setg(errp, "Driver '%s' is not whitelisted", drv->format_name);
218e99
         return -ENOTSUP;
218e99
     }
218e99
 
218e99
@@ -733,26 +740,33 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
218e99
     if (drv->bdrv_file_open) {
218e99
         assert(file == NULL);
218e99
         assert(drv->bdrv_parse_filename || filename != NULL);
218e99
-        ret = drv->bdrv_file_open(bs, options, open_flags, NULL);
218e99
+        ret = drv->bdrv_file_open(bs, options, open_flags, &local_err);
218e99
     } else {
218e99
         if (file == NULL) {
218e99
-            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a "
218e99
-                          "block driver for the protocol level",
218e99
-                          drv->format_name);
218e99
+            error_setg(errp, "Can't use '%s' as a block driver for the "
218e99
+                       "protocol level", drv->format_name);
218e99
             ret = -EINVAL;
218e99
             goto free_and_fail;
218e99
         }
218e99
         assert(file != NULL);
218e99
         bs->file = file;
218e99
-        ret = drv->bdrv_open(bs, options, open_flags, NULL);
218e99
+        ret = drv->bdrv_open(bs, options, open_flags, &local_err);
218e99
     }
218e99
 
218e99
     if (ret < 0) {
218e99
+        if (error_is_set(&local_err)) {
218e99
+            error_propagate(errp, local_err);
218e99
+        } else if (filename) {
218e99
+            error_setg_errno(errp, -ret, "Could not open '%s'", filename);
218e99
+        } else {
218e99
+            error_setg_errno(errp, -ret, "Could not open image");
218e99
+        }
218e99
         goto free_and_fail;
218e99
     }
218e99
 
218e99
     ret = refresh_total_sectors(bs, bs->total_sectors);
218e99
     if (ret < 0) {
218e99
+        error_setg_errno(errp, -ret, "Could not refresh total sector count");
218e99
         goto free_and_fail;
218e99
     }
218e99
 
218e99
@@ -781,12 +795,13 @@ free_and_fail:
218e99
  * dictionary, it needs to use QINCREF() before calling bdrv_file_open.
218e99
  */
218e99
 int bdrv_file_open(BlockDriverState **pbs, const char *filename,
218e99
-                   QDict *options, int flags)
218e99
+                   QDict *options, int flags, Error **errp)
218e99
 {
218e99
     BlockDriverState *bs;
218e99
     BlockDriver *drv;
218e99
     const char *drvname;
218e99
     bool allow_protocol_prefix = false;
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
 
218e99
     /* NULL means an empty set of options */
218e99
@@ -805,8 +820,8 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
218e99
         qdict_put(options, "filename", qstring_from_str(filename));
218e99
         allow_protocol_prefix = true;
218e99
     } else {
218e99
-        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't specify 'file' and "
218e99
-                      "'filename' options at the same time");
218e99
+        error_setg(errp, "Can't specify 'file' and 'filename' options at the "
218e99
+                   "same time");
218e99
         ret = -EINVAL;
218e99
         goto fail;
218e99
     }
218e99
@@ -815,53 +830,53 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename,
218e99
     drvname = qdict_get_try_str(options, "driver");
218e99
     if (drvname) {
218e99
         drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR));
218e99
+        if (!drv) {
218e99
+            error_setg(errp, "Unknown driver '%s'", drvname);
218e99
+        }
218e99
         qdict_del(options, "driver");
218e99
     } else if (filename) {
218e99
         drv = bdrv_find_protocol(filename, allow_protocol_prefix);
218e99
         if (!drv) {
218e99
-            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Unknown protocol");
218e99
+            error_setg(errp, "Unknown protocol");
218e99
         }
218e99
     } else {
218e99
-        qerror_report(ERROR_CLASS_GENERIC_ERROR,
218e99
-                      "Must specify either driver or file");
218e99
+        error_setg(errp, "Must specify either driver or file");
218e99
         drv = NULL;
218e99
     }
218e99
 
218e99
     if (!drv) {
218e99
+        /* errp has been set already */
218e99
         ret = -ENOENT;
218e99
         goto fail;
218e99
     }
218e99
 
218e99
     /* Parse the filename and open it */
218e99
     if (drv->bdrv_parse_filename && filename) {
218e99
-        Error *local_err = NULL;
218e99
         drv->bdrv_parse_filename(filename, options, &local_err);
218e99
         if (error_is_set(&local_err)) {
218e99
-            qerror_report_err(local_err);
218e99
-            error_free(local_err);
218e99
+            error_propagate(errp, local_err);
218e99
             ret = -EINVAL;
218e99
             goto fail;
218e99
         }
218e99
         qdict_del(options, "filename");
218e99
     } else if (!drv->bdrv_parse_filename && !filename) {
218e99
-        qerror_report(ERROR_CLASS_GENERIC_ERROR,
218e99
-                      "The '%s' block driver requires a file name",
218e99
-                      drv->format_name);
218e99
+        error_setg(errp, "The '%s' block driver requires a file name",
218e99
+                   drv->format_name);
218e99
         ret = -EINVAL;
218e99
         goto fail;
218e99
     }
218e99
 
218e99
-    ret = bdrv_open_common(bs, NULL, options, flags, drv);
218e99
+    ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err);
218e99
     if (ret < 0) {
218e99
+        error_propagate(errp, local_err);
218e99
         goto fail;
218e99
     }
218e99
 
218e99
     /* Check if any unknown options were used */
218e99
     if (qdict_size(options) != 0) {
218e99
         const QDictEntry *entry = qdict_first(options);
218e99
-        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block protocol '%s' doesn't "
218e99
-                      "support the option '%s'",
218e99
-                      drv->format_name, entry->key);
218e99
+        error_setg(errp, "Block protocol '%s' doesn't support the option '%s'",
218e99
+                   drv->format_name, entry->key);
218e99
         ret = -EINVAL;
218e99
         goto fail;
218e99
     }
218e99
@@ -888,11 +903,12 @@ fail:
218e99
  * function (even on failure), so if the caller intends to reuse the dictionary,
218e99
  * it needs to use QINCREF() before calling bdrv_file_open.
218e99
  */
218e99
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)
218e99
+int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
218e99
 {
218e99
     char backing_filename[PATH_MAX];
218e99
     int back_flags, ret;
218e99
     BlockDriver *back_drv = NULL;
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     if (bs->backing_hd != NULL) {
218e99
         QDECREF(options);
218e99
@@ -925,11 +941,12 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)
218e99
 
218e99
     ret = bdrv_open(bs->backing_hd,
218e99
                     *backing_filename ? backing_filename : NULL, options,
218e99
-                    back_flags, back_drv);
218e99
+                    back_flags, back_drv, &local_err);
218e99
     if (ret < 0) {
218e99
         bdrv_delete(bs->backing_hd);
218e99
         bs->backing_hd = NULL;
218e99
         bs->open_flags |= BDRV_O_NO_BACKING;
218e99
+        error_propagate(errp, local_err);
218e99
         return ret;
218e99
     }
218e99
     return 0;
218e99
@@ -963,7 +980,7 @@ static void extract_subqdict(QDict *src, QDict **dst, const char *start)
218e99
  * dictionary, it needs to use QINCREF() before calling bdrv_open.
218e99
  */
218e99
 int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
-              int flags, BlockDriver *drv)
218e99
+              int flags, BlockDriver *drv, Error **errp)
218e99
 {
218e99
     int ret;
218e99
     /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
218e99
@@ -971,6 +988,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
     BlockDriverState *file = NULL;
218e99
     QDict *file_options = NULL;
218e99
     const char *drvname;
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     /* NULL means an empty set of options */
218e99
     if (options == NULL) {
218e99
@@ -989,7 +1007,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
         char backing_filename[PATH_MAX];
218e99
 
218e99
         if (qdict_size(options) != 0) {
218e99
-            error_report("Can't use snapshot=on with driver-specific options");
218e99
+            error_setg(errp, "Can't use snapshot=on with driver-specific options");
218e99
             ret = -EINVAL;
218e99
             goto fail;
218e99
         }
218e99
@@ -1000,7 +1018,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
 
218e99
         /* if there is a backing file, use it */
218e99
         bs1 = bdrv_new("");
218e99
-        ret = bdrv_open(bs1, filename, NULL, 0, drv);
218e99
+        ret = bdrv_open(bs1, filename, NULL, 0, drv, &local_err);
218e99
         if (ret < 0) {
218e99
             bdrv_delete(bs1);
218e99
             goto fail;
218e99
@@ -1011,6 +1029,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
 
218e99
         ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename));
218e99
         if (ret < 0) {
218e99
+            error_setg_errno(errp, -ret, "Could not get temporary filename");
218e99
             goto fail;
218e99
         }
218e99
 
218e99
@@ -1019,6 +1038,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
             snprintf(backing_filename, sizeof(backing_filename),
218e99
                      "%s", filename);
218e99
         } else if (!realpath(filename, backing_filename)) {
218e99
+            error_setg_errno(errp, errno, "Could not resolve path '%s'", filename);
218e99
             ret = -errno;
218e99
             goto fail;
218e99
         }
218e99
@@ -1038,6 +1058,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
         ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options);
218e99
         free_option_parameters(create_options);
218e99
         if (ret < 0) {
218e99
+            error_setg_errno(errp, -ret, "Could not create temporary overlay "
218e99
+                             "'%s'", tmp_filename);
218e99
             goto fail;
218e99
         }
218e99
 
218e99
@@ -1054,7 +1076,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
     extract_subqdict(options, &file_options, "file.");
218e99
 
218e99
     ret = bdrv_file_open(&file, filename, file_options,
218e99
-                         bdrv_open_flags(bs, flags | BDRV_O_UNMAP));
218e99
+                         bdrv_open_flags(bs, flags | BDRV_O_UNMAP), &local_err);
218e99
     if (ret < 0) {
218e99
         goto fail;
218e99
     }
218e99
@@ -1067,7 +1089,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
     }
218e99
 
218e99
     if (!drv) {
218e99
-        ret = find_image_format(file, filename, &drv;;
218e99
+        ret = find_image_format(file, filename, &drv, &local_err);
218e99
     }
218e99
 
218e99
     if (!drv) {
218e99
@@ -1075,7 +1097,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
     }
218e99
 
218e99
     /* Open the image */
218e99
-    ret = bdrv_open_common(bs, file, options, flags, drv);
218e99
+    ret = bdrv_open_common(bs, file, options, flags, drv, &local_err);
218e99
     if (ret < 0) {
218e99
         goto unlink_and_fail;
218e99
     }
218e99
@@ -1090,7 +1112,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
         QDict *backing_options;
218e99
 
218e99
         extract_subqdict(options, &backing_options, "backing.");
218e99
-        ret = bdrv_open_backing_file(bs, backing_options);
218e99
+        ret = bdrv_open_backing_file(bs, backing_options, &local_err);
218e99
         if (ret < 0) {
218e99
             goto close_and_fail;
218e99
         }
218e99
@@ -1099,9 +1121,9 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
     /* Check if any unknown options were used */
218e99
     if (qdict_size(options) != 0) {
218e99
         const QDictEntry *entry = qdict_first(options);
218e99
-        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block format '%s' used by "
218e99
-            "device '%s' doesn't support the option '%s'",
218e99
-            drv->format_name, bs->device_name, entry->key);
218e99
+        error_setg(errp, "Block format '%s' used by device '%s' doesn't "
218e99
+                   "support the option '%s'", drv->format_name, bs->device_name,
218e99
+                   entry->key);
218e99
 
218e99
         ret = -EINVAL;
218e99
         goto close_and_fail;
218e99
@@ -1130,11 +1152,17 @@ fail:
218e99
     QDECREF(bs->options);
218e99
     QDECREF(options);
218e99
     bs->options = NULL;
218e99
+    if (error_is_set(&local_err)) {
218e99
+        error_propagate(errp, local_err);
218e99
+    }
218e99
     return ret;
218e99
 
218e99
 close_and_fail:
218e99
     bdrv_close(bs);
218e99
     QDECREF(options);
218e99
+    if (error_is_set(&local_err)) {
218e99
+        error_propagate(errp, local_err);
218e99
+    }
218e99
     return ret;
218e99
 }
218e99
 
218e99
@@ -4813,7 +4841,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
218e99
             bs = bdrv_new("");
218e99
 
218e99
             ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags,
218e99
-                            backing_drv);
218e99
+                            backing_drv, NULL);
218e99
             if (ret < 0) {
218e99
                 error_setg_errno(errp, -ret, "Could not open '%s'",
218e99
                                  backing_file->value.s);
218e99
diff --git a/block/blkdebug.c b/block/blkdebug.c
218e99
index 114c4c9..eda2810 100644
218e99
--- a/block/blkdebug.c
218e99
+++ b/block/blkdebug.c
218e99
@@ -384,8 +384,10 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
218e99
         goto fail;
218e99
     }
218e99
 
218e99
-    ret = bdrv_file_open(&bs->file, filename, NULL, flags);
218e99
+    ret = bdrv_file_open(&bs->file, filename, NULL, flags, &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         goto fail;
218e99
     }
218e99
 
218e99
diff --git a/block/blkverify.c b/block/blkverify.c
218e99
index 5d716bb..cceb88f 100644
218e99
--- a/block/blkverify.c
218e99
+++ b/block/blkverify.c
218e99
@@ -141,8 +141,10 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
218e99
         goto fail;
218e99
     }
218e99
 
218e99
-    ret = bdrv_file_open(&bs->file, raw, NULL, flags);
218e99
+    ret = bdrv_file_open(&bs->file, raw, NULL, flags, &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         goto fail;
218e99
     }
218e99
 
218e99
@@ -154,8 +156,10 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
218e99
     }
218e99
 
218e99
     s->test_file = bdrv_new("");
218e99
-    ret = bdrv_open(s->test_file, filename, NULL, flags, NULL);
218e99
+    ret = bdrv_open(s->test_file, filename, NULL, flags, NULL, &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         bdrv_delete(s->test_file);
218e99
         s->test_file = NULL;
218e99
         goto fail;
218e99
diff --git a/block/cow.c b/block/cow.c
218e99
index c1d2dc8..8e00f8f 100644
218e99
--- a/block/cow.c
218e99
+++ b/block/cow.c
218e99
@@ -302,6 +302,7 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
218e99
     struct stat st;
218e99
     int64_t image_sectors = 0;
218e99
     const char *image_filename = NULL;
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
     BlockDriverState *cow_bs;
218e99
 
218e99
@@ -320,8 +321,10 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
218e99
         return ret;
218e99
     }
218e99
 
218e99
-    ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR);
218e99
+    ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR, &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         return ret;
218e99
     }
218e99
 
218e99
diff --git a/block/mirror.c b/block/mirror.c
218e99
index dad1e3e..6fa733e 100644
218e99
--- a/block/mirror.c
218e99
+++ b/block/mirror.c
218e99
@@ -505,14 +505,15 @@ static void mirror_iostatus_reset(BlockJob *job)
218e99
 static void mirror_complete(BlockJob *job, Error **errp)
218e99
 {
218e99
     MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
 
218e99
-    ret = bdrv_open_backing_file(s->target, NULL);
218e99
+    ret = bdrv_open_backing_file(s->target, NULL, &local_err);
218e99
     if (ret < 0) {
218e99
         char backing_filename[PATH_MAX];
218e99
         bdrv_get_full_backing_filename(s->target, backing_filename,
218e99
                                        sizeof(backing_filename));
218e99
-        error_setg_file_open(errp, -ret, backing_filename);
218e99
+        error_propagate(errp, local_err);
218e99
         return;
218e99
     }
218e99
     if (!s->synced) {
218e99
diff --git a/block/qcow.c b/block/qcow.c
218e99
index 1a6926f..41a578c 100644
218e99
--- a/block/qcow.c
218e99
+++ b/block/qcow.c
218e99
@@ -668,6 +668,7 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options,
218e99
     int64_t total_size = 0;
218e99
     const char *backing_file = NULL;
218e99
     int flags = 0;
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
     BlockDriverState *qcow_bs;
218e99
 
218e99
@@ -688,8 +689,10 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options,
218e99
         return ret;
218e99
     }
218e99
 
218e99
-    ret = bdrv_file_open(&qcow_bs, filename, NULL, BDRV_O_RDWR);
218e99
+    ret = bdrv_file_open(&qcow_bs, filename, NULL, BDRV_O_RDWR, &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         return ret;
218e99
     }
218e99
 
218e99
diff --git a/block/qcow2.c b/block/qcow2.c
218e99
index b6e50af..a2fca7a 100644
218e99
--- a/block/qcow2.c
218e99
+++ b/block/qcow2.c
218e99
@@ -1369,7 +1369,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
218e99
         return ret;
218e99
     }
218e99
 
218e99
-    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR);
218e99
+    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, NULL);
218e99
     if (ret < 0) {
218e99
         return ret;
218e99
     }
218e99
@@ -1422,7 +1422,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
218e99
     BlockDriver* drv = bdrv_find_format("qcow2");
218e99
     assert(drv != NULL);
218e99
     ret = bdrv_open(bs, filename, NULL,
218e99
-        BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv);
218e99
+        BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, drv, NULL);
218e99
     if (ret < 0) {
218e99
         goto out;
218e99
     }
218e99
diff --git a/block/qed.c b/block/qed.c
218e99
index c56b5e2..fa35fe2 100644
218e99
--- a/block/qed.c
218e99
+++ b/block/qed.c
218e99
@@ -551,6 +551,7 @@ static int qed_create(const char *filename, uint32_t cluster_size,
218e99
     QEDHeader le_header;
218e99
     uint8_t *l1_table = NULL;
218e99
     size_t l1_size = header.cluster_size * header.table_size;
218e99
+    Error *local_err = NULL;
218e99
     int ret = 0;
218e99
     BlockDriverState *bs = NULL;
218e99
 
218e99
@@ -559,8 +560,11 @@ static int qed_create(const char *filename, uint32_t cluster_size,
218e99
         return ret;
218e99
     }
218e99
 
218e99
-    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR | BDRV_O_CACHE_WB);
218e99
+    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR | BDRV_O_CACHE_WB,
218e99
+                         &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         return ret;
218e99
     }
218e99
 
218e99
diff --git a/block/sheepdog.c b/block/sheepdog.c
218e99
index 952d703..e7c4448 100644
218e99
--- a/block/sheepdog.c
218e99
+++ b/block/sheepdog.c
218e99
@@ -1418,10 +1418,13 @@ static int sd_prealloc(const char *filename)
218e99
     uint32_t idx, max_idx;
218e99
     int64_t vdi_size;
218e99
     void *buf = g_malloc0(SD_DATA_OBJ_SIZE);
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
 
218e99
-    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR);
218e99
+    ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         goto out;
218e99
     }
218e99
 
218e99
@@ -1466,6 +1469,7 @@ static int sd_create(const char *filename, QEMUOptionParameter *options,
218e99
     char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
218e99
     uint32_t snapid;
218e99
     bool prealloc = false;
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     s = g_malloc0(sizeof(BDRVSheepdogState));
218e99
 
218e99
@@ -1519,8 +1523,10 @@ static int sd_create(const char *filename, QEMUOptionParameter *options,
218e99
             goto out;
218e99
         }
218e99
 
218e99
-        ret = bdrv_file_open(&bs, backing_file, NULL, 0);
218e99
+        ret = bdrv_file_open(&bs, backing_file, NULL, 0, &local_err);
218e99
         if (ret < 0) {
218e99
+            qerror_report_err(local_err);
218e99
+            error_free(local_err);
218e99
             goto out;
218e99
         }
218e99
 
218e99
diff --git a/block/vmdk.c b/block/vmdk.c
218e99
index fa64f05..213901f 100644
218e99
--- a/block/vmdk.c
218e99
+++ b/block/vmdk.c
218e99
@@ -697,6 +697,7 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
     int64_t flat_offset;
218e99
     char extent_path[PATH_MAX];
218e99
     BlockDriverState *extent_file;
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     while (*p) {
218e99
         /* parse extent line:
218e99
@@ -726,8 +727,11 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
 
218e99
         path_combine(extent_path, sizeof(extent_path),
218e99
                 desc_file_path, fname);
218e99
-        ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags);
218e99
+        ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
218e99
+                             &local_err);
218e99
         if (ret) {
218e99
+            qerror_report_err(local_err);
218e99
+            error_free(local_err);
218e99
             return ret;
218e99
         }
218e99
 
218e99
@@ -1591,6 +1595,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
218e99
         "ddb.geometry.heads = \"%d\"\n"
218e99
         "ddb.geometry.sectors = \"63\"\n"
218e99
         "ddb.adapterType = \"%s\"\n";
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     if (filename_decompose(filename, path, prefix, postfix, PATH_MAX)) {
218e99
         return -EINVAL;
218e99
@@ -1653,8 +1658,10 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
218e99
     }
218e99
     if (backing_file) {
218e99
         BlockDriverState *bs = bdrv_new("");
218e99
-        ret = bdrv_open(bs, backing_file, NULL, 0, NULL);
218e99
+        ret = bdrv_open(bs, backing_file, NULL, 0, NULL, &local_err);
218e99
         if (ret != 0) {
218e99
+            qerror_report_err(local_err);
218e99
+            error_free(local_err);
218e99
             bdrv_delete(bs);
218e99
             return ret;
218e99
         }
218e99
diff --git a/block/vvfat.c b/block/vvfat.c
218e99
index bb3b0b6..a385b6f 100644
218e99
--- a/block/vvfat.c
218e99
+++ b/block/vvfat.c
218e99
@@ -2910,6 +2910,7 @@ static int enable_write_target(BDRVVVFATState *s)
218e99
 {
218e99
     BlockDriver *bdrv_qcow;
218e99
     QEMUOptionParameter *options;
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
     int size = sector2cluster(s, s->sector_count);
218e99
     s->used_clusters = calloc(size, 1);
218e99
@@ -2935,8 +2936,11 @@ static int enable_write_target(BDRVVVFATState *s)
218e99
     s->qcow = bdrv_new("");
218e99
 
218e99
     ret = bdrv_open(s->qcow, s->qcow_filename, NULL,
218e99
-            BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, bdrv_qcow);
218e99
+            BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, bdrv_qcow,
218e99
+            &local_err);
218e99
     if (ret < 0) {
218e99
+        qerror_report_err(local_err);
218e99
+        error_free(local_err);
218e99
         bdrv_delete(s->qcow);
218e99
         goto err;
218e99
     }
218e99
diff --git a/blockdev.c b/blockdev.c
218e99
index a6563fa..2fb6d83 100644
218e99
--- a/blockdev.c
218e99
+++ b/blockdev.c
218e99
@@ -707,17 +707,11 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,
218e99
     }
218e99
 
218e99
     QINCREF(bs_opts);
218e99
-    ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv);
218e99
+    ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error);
218e99
 
218e99
     if (ret < 0) {
218e99
-        if (ret == -EMEDIUMTYPE) {
218e99
-            error_report("could not open disk image %s: not in %s format",
218e99
-                         file ?: dinfo->id, drv ? drv->format_name :
218e99
-                         qdict_get_str(bs_opts, "driver"));
218e99
-        } else {
218e99
-            error_report("could not open disk image %s: %s",
218e99
-                         file ?: dinfo->id, strerror(-ret));
218e99
-        }
218e99
+        error_report("could not open disk image %s: %s",
218e99
+                     file ?: dinfo->id, error_get_pretty(error));
218e99
         goto err;
218e99
     }
218e99
 
218e99
@@ -958,9 +952,9 @@ static void external_snapshot_prepare(BlkTransactionStates *common,
218e99
     /* TODO Inherit bs->options or only take explicit options with an
218e99
      * extended QMP command? */
218e99
     ret = bdrv_open(states->new_bs, new_image_file, NULL,
218e99
-                    flags | BDRV_O_NO_BACKING, drv);
218e99
+                    flags | BDRV_O_NO_BACKING, drv, &local_err);
218e99
     if (ret != 0) {
218e99
-        error_setg_file_open(errp, -ret, new_image_file);
218e99
+        error_propagate(errp, local_err);
218e99
     }
218e99
 }
218e99
 
218e99
@@ -1124,11 +1118,12 @@ static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
218e99
                                     int bdrv_flags, BlockDriver *drv,
218e99
                                     const char *password, Error **errp)
218e99
 {
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
 
218e99
-    ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv);
218e99
+    ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv, &local_err);
218e99
     if (ret < 0) {
218e99
-        error_setg_file_open(errp, -ret, filename);
218e99
+        error_propagate(errp, local_err);
218e99
         return;
218e99
     }
218e99
 
218e99
@@ -1538,10 +1533,11 @@ void qmp_drive_mirror(const char *device, const char *target,
218e99
      * file.
218e99
      */
218e99
     target_bs = bdrv_new("");
218e99
-    ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
218e99
+    ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv,
218e99
+                    &local_err);
218e99
     if (ret < 0) {
218e99
         bdrv_delete(target_bs);
218e99
-        error_setg_file_open(errp, -ret, target);
218e99
+        error_propagate(errp, local_err);
218e99
         return;
218e99
     }
218e99
 
218e99
diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c
218e99
index 247f32f..2d4d263 100644
218e99
--- a/hw/block/xen_disk.c
218e99
+++ b/hw/block/xen_disk.c
218e99
@@ -797,10 +797,15 @@ static int blk_connect(struct XenDevice *xendev)
218e99
         xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
218e99
         blkdev->bs = bdrv_new(blkdev->dev);
218e99
         if (blkdev->bs) {
218e99
+            Error *local_err = NULL;
218e99
             BlockDriver *drv = bdrv_find_whitelisted_format(blkdev->fileproto,
218e99
                                                            readonly);
218e99
             if (bdrv_open(blkdev->bs,
218e99
-                          blkdev->filename, NULL, qflags, drv) != 0) {
218e99
+                          blkdev->filename, NULL, qflags, drv, &local_err) != 0)
218e99
+            {
218e99
+                xen_be_printf(&blkdev->xendev, 0, "error: %s\n",
218e99
+                              error_get_pretty(local_err));
218e99
+                error_free(local_err);
218e99
                 bdrv_delete(blkdev->bs);
218e99
                 blkdev->bs = NULL;
218e99
             }
218e99
diff --git a/include/block/block.h b/include/block/block.h
218e99
index 39770a3..168d8a8 100644
218e99
--- a/include/block/block.h
218e99
+++ b/include/block/block.h
218e99
@@ -162,10 +162,10 @@ void bdrv_delete(BlockDriverState *bs);
218e99
 int bdrv_parse_cache_flags(const char *mode, int *flags);
218e99
 int bdrv_parse_discard_flags(const char *mode, int *flags);
218e99
 int bdrv_file_open(BlockDriverState **pbs, const char *filename,
218e99
-                   QDict *options, int flags);
218e99
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options);
218e99
+                   QDict *options, int flags, Error **errp);
218e99
+int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
218e99
 int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
218e99
-              int flags, BlockDriver *drv);
218e99
+              int flags, BlockDriver *drv, Error **errp);
218e99
 BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
218e99
                                     BlockDriverState *bs, int flags);
218e99
 int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp);
218e99
diff --git a/qemu-img.c b/qemu-img.c
218e99
index 8fcea44..d7d1244 100644
218e99
--- a/qemu-img.c
218e99
+++ b/qemu-img.c
218e99
@@ -264,6 +264,7 @@ static BlockDriverState *bdrv_new_open(const char *filename,
218e99
     BlockDriverState *bs;
218e99
     BlockDriver *drv;
218e99
     char password[256];
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
 
218e99
     bs = bdrv_new("image");
218e99
@@ -278,9 +279,11 @@ static BlockDriverState *bdrv_new_open(const char *filename,
218e99
         drv = NULL;
218e99
     }
218e99
 
218e99
-    ret = bdrv_open(bs, filename, NULL, flags, drv);
218e99
+    ret = bdrv_open(bs, filename, NULL, flags, drv, &local_err);
218e99
     if (ret < 0) {
218e99
-        error_report("Could not open '%s': %s", filename, strerror(-ret));
218e99
+        error_report("Could not open '%s': %s", filename,
218e99
+                     error_get_pretty(local_err));
218e99
+        error_free(local_err);
218e99
         goto fail;
218e99
     }
218e99
 
218e99
@@ -2266,6 +2269,7 @@ static int img_rebase(int argc, char **argv)
218e99
     int unsafe = 0;
218e99
     int progress = 0;
218e99
     bool quiet = false;
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     /* Parse commandline parameters */
218e99
     fmt = NULL;
218e99
@@ -2369,18 +2373,21 @@ static int img_rebase(int argc, char **argv)
218e99
         bs_old_backing = bdrv_new("old_backing");
218e99
         bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
218e99
         ret = bdrv_open(bs_old_backing, backing_name, NULL, BDRV_O_FLAGS,
218e99
-                        old_backing_drv);
218e99
+                        old_backing_drv, &local_err);
218e99
         if (ret) {
218e99
-            error_report("Could not open old backing file '%s'", backing_name);
218e99
+            error_report("Could not open old backing file '%s': %s",
218e99
+                         backing_name, error_get_pretty(local_err));
218e99
+            error_free(local_err);
218e99
             goto out;
218e99
         }
218e99
         if (out_baseimg[0]) {
218e99
             bs_new_backing = bdrv_new("new_backing");
218e99
             ret = bdrv_open(bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS,
218e99
-                        new_backing_drv);
218e99
+                        new_backing_drv, &local_err);
218e99
             if (ret) {
218e99
-                error_report("Could not open new backing file '%s'",
218e99
-                             out_baseimg);
218e99
+                error_report("Could not open new backing file '%s': %s",
218e99
+                             out_baseimg, error_get_pretty(local_err));
218e99
+                error_free(local_err);
218e99
                 goto out;
218e99
             }
218e99
         }
218e99
diff --git a/qemu-io.c b/qemu-io.c
218e99
index bdcce7f..116bb1b 100644
218e99
--- a/qemu-io.c
218e99
+++ b/qemu-io.c
218e99
@@ -1764,21 +1764,27 @@ static const cmdinfo_t close_cmd = {
218e99
 
218e99
 static int openfile(char *name, int flags, int growable)
218e99
 {
218e99
+    Error *local_err = NULL;
218e99
+
218e99
     if (bs) {
218e99
         fprintf(stderr, "file open already, try 'help close'\n");
218e99
         return 1;
218e99
     }
218e99
 
218e99
     if (growable) {
218e99
-        if (bdrv_file_open(&bs, name, NULL, flags)) {
218e99
-            fprintf(stderr, "%s: can't open device %s\n", progname, name);
218e99
+        if (bdrv_file_open(&bs, name, NULL, flags, &local_err)) {
218e99
+            fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
218e99
+                    error_get_pretty(local_err));
218e99
+            error_free(local_err);
218e99
             return 1;
218e99
         }
218e99
     } else {
218e99
         bs = bdrv_new("hda");
218e99
 
218e99
-        if (bdrv_open(bs, name, NULL, flags, NULL) < 0) {
218e99
-            fprintf(stderr, "%s: can't open device %s\n", progname, name);
218e99
+        if (bdrv_open(bs, name, NULL, flags, NULL, &local_err) < 0) {
218e99
+            fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
218e99
+                    error_get_pretty(local_err));
218e99
+            error_free(local_err);
218e99
             bdrv_delete(bs);
218e99
             bs = NULL;
218e99
             return 1;
218e99
diff --git a/qemu-nbd.c b/qemu-nbd.c
218e99
index 9c31d45..c1b395d 100644
218e99
--- a/qemu-nbd.c
218e99
+++ b/qemu-nbd.c
218e99
@@ -354,6 +354,7 @@ int main(int argc, char **argv)
218e99
 #endif
218e99
     pthread_t client_thread;
218e99
     const char *fmt = NULL;
218e99
+    Error *local_err = NULL;
218e99
 
218e99
     /* The client thread uses SIGTERM to interrupt the server.  A signal
218e99
      * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
218e99
@@ -572,10 +573,11 @@ int main(int argc, char **argv)
218e99
 
218e99
     bs = bdrv_new("hda");
218e99
     srcpath = argv[optind];
218e99
-    ret = bdrv_open(bs, srcpath, NULL, flags, drv);
218e99
+    ret = bdrv_open(bs, srcpath, NULL, flags, drv, &local_err);
218e99
     if (ret < 0) {
218e99
         errno = -ret;
218e99
-        err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
218e99
+        err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
218e99
+            error_get_pretty(local_err));
218e99
     }
218e99
 
218e99
     fd_size = bdrv_getlength(bs);
218e99
-- 
218e99
1.7.1
218e99