218e99
From 3b0640a90526abf547f13c2bd6b1bb98aec947bd Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Mon, 4 Nov 2013 22:32:20 +0100
218e99
Subject: [PATCH 27/87] block/raw-posix: Employ error parameter
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383604354-12743-30-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55329
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 29/43] block/raw-posix: Employ error parameter
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
Make use of the error parameter in the opening and creating functions in
218e99
block/raw-posix.c.
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit e428e439df4d92ac42cb913a1dd19b86155eae86)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
---
218e99
 block/raw-posix.c          | 72 ++++++++++++++++++++++++++++++++++++----------
218e99
 tests/qemu-iotests/051.out |  2 +-
218e99
 2 files changed, 58 insertions(+), 16 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block/raw-posix.c          |   72 ++++++++++++++++++++++++++++++++++---------
218e99
 tests/qemu-iotests/051.out |    2 +-
218e99
 2 files changed, 58 insertions(+), 16 deletions(-)
218e99
218e99
diff --git a/block/raw-posix.c b/block/raw-posix.c
218e99
index 249bffb..74b15da 100644
218e99
--- a/block/raw-posix.c
218e99
+++ b/block/raw-posix.c
218e99
@@ -276,7 +276,7 @@ static QemuOptsList raw_runtime_opts = {
218e99
 };
218e99
 
218e99
 static int raw_open_common(BlockDriverState *bs, QDict *options,
218e99
-                           int bdrv_flags, int open_flags)
218e99
+                           int bdrv_flags, int open_flags, Error **errp)
218e99
 {
218e99
     BDRVRawState *s = bs->opaque;
218e99
     QemuOpts *opts;
218e99
@@ -287,8 +287,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
218e99
     opts = qemu_opts_create_nofail(&raw_runtime_opts);
218e99
     qemu_opts_absorb_qdict(opts, 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
@@ -297,6 +296,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
218e99
 
218e99
     ret = raw_normalize_devicepath(&filename);
218e99
     if (ret != 0) {
218e99
+        error_setg_errno(errp, -ret, "Could not normalize device path");
218e99
         goto fail;
218e99
     }
218e99
 
218e99
@@ -310,6 +310,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
218e99
         if (ret == -EROFS) {
218e99
             ret = -EACCES;
218e99
         }
218e99
+        error_setg_errno(errp, -ret, "Could not open file");
218e99
         goto fail;
218e99
     }
218e99
     s->fd = fd;
218e99
@@ -318,6 +319,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
218e99
     if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
218e99
         qemu_close(fd);
218e99
         ret = -errno;
218e99
+        error_setg_errno(errp, -ret, "Could not set AIO state");
218e99
         goto fail;
218e99
     }
218e99
 #endif
218e99
@@ -339,9 +341,15 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
218e99
                     Error **errp)
218e99
 {
218e99
     BDRVRawState *s = bs->opaque;
218e99
+    Error *local_err = NULL;
218e99
+    int ret;
218e99
 
218e99
     s->type = FTYPE_FILE;
218e99
-    return raw_open_common(bs, options, flags, 0);
218e99
+    ret = raw_open_common(bs, options, flags, 0, &local_err);
218e99
+    if (error_is_set(&local_err)) {
218e99
+        error_propagate(errp, local_err);
218e99
+    }
218e99
+    return ret;
218e99
 }
218e99
 
218e99
 static int raw_reopen_prepare(BDRVReopenState *state,
218e99
@@ -366,6 +374,7 @@ static int raw_reopen_prepare(BDRVReopenState *state,
218e99
      * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
218e99
      * won't override aio_ctx if aio_ctx is non-NULL */
218e99
     if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
218e99
+        error_setg(errp, "Could not set AIO state");
218e99
         return -1;
218e99
     }
218e99
 #endif
218e99
@@ -417,6 +426,7 @@ static int raw_reopen_prepare(BDRVReopenState *state,
218e99
         assert(!(raw_s->open_flags & O_CREAT));
218e99
         raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
218e99
         if (raw_s->fd == -1) {
218e99
+            error_setg_errno(errp, errno, "Could not reopen file");
218e99
             ret = -1;
218e99
         }
218e99
     }
218e99
@@ -1060,12 +1070,15 @@ static int raw_create(const char *filename, QEMUOptionParameter *options,
218e99
                    0644);
218e99
     if (fd < 0) {
218e99
         result = -errno;
218e99
+        error_setg_errno(errp, -result, "Could not create file");
218e99
     } else {
218e99
         if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
218e99
             result = -errno;
218e99
+            error_setg_errno(errp, -result, "Could not resize file");
218e99
         }
218e99
         if (qemu_close(fd) != 0) {
218e99
             result = -errno;
218e99
+            error_setg_errno(errp, -result, "Could not close the new file");
218e99
         }
218e99
     }
218e99
     return result;
218e99
@@ -1336,6 +1349,7 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
218e99
                      Error **errp)
218e99
 {
218e99
     BDRVRawState *s = bs->opaque;
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
     const char *filename = qdict_get_str(options, "filename");
218e99
 
218e99
@@ -1379,8 +1393,11 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
218e99
     }
218e99
 #endif
218e99
 
218e99
-    ret = raw_open_common(bs, options, flags, 0);
218e99
+    ret = raw_open_common(bs, options, flags, 0, &local_err);
218e99
     if (ret < 0) {
218e99
+        if (error_is_set(&local_err)) {
218e99
+            error_propagate(errp, local_err);
218e99
+        }
218e99
         return ret;
218e99
     }
218e99
 
218e99
@@ -1388,6 +1405,7 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
218e99
         ret = check_hdev_writable(s);
218e99
         if (ret < 0) {
218e99
             raw_close(bs);
218e99
+            error_setg_errno(errp, -ret, "The device is not writable");
218e99
             return ret;
218e99
         }
218e99
     }
218e99
@@ -1523,15 +1541,23 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options,
218e99
     }
218e99
 
218e99
     fd = qemu_open(filename, O_WRONLY | O_BINARY);
218e99
-    if (fd < 0)
218e99
-        return -errno;
218e99
+    if (fd < 0) {
218e99
+        ret = -errno;
218e99
+        error_setg_errno(errp, -ret, "Could not open device");
218e99
+        return ret;
218e99
+    }
218e99
 
218e99
-    if (fstat(fd, &stat_buf) < 0)
218e99
+    if (fstat(fd, &stat_buf) < 0) {
218e99
         ret = -errno;
218e99
-    else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
218e99
+        error_setg_errno(errp, -ret, "Could not stat device");
218e99
+    } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) {
218e99
+        error_setg(errp,
218e99
+                   "The given file is neither a block nor a character device");
218e99
         ret = -ENODEV;
218e99
-    else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
218e99
+    } else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) {
218e99
+        error_setg(errp, "Device is too small");
218e99
         ret = -ENOSPC;
218e99
+    }
218e99
 
218e99
     qemu_close(fd);
218e99
     return ret;
218e99
@@ -1578,14 +1604,19 @@ static int floppy_open(BlockDriverState *bs, QDict *options, int flags,
218e99
                        Error **errp)
218e99
 {
218e99
     BDRVRawState *s = bs->opaque;
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
 
218e99
     s->type = FTYPE_FD;
218e99
 
218e99
     /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
218e99
-    ret = raw_open_common(bs, options, flags, O_NONBLOCK);
218e99
-    if (ret)
218e99
+    ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
218e99
+    if (ret) {
218e99
+        if (error_is_set(&local_err)) {
218e99
+            error_propagate(errp, local_err);
218e99
+        }
218e99
         return ret;
218e99
+    }
218e99
 
218e99
     /* close fd so that we can reopen it as needed */
218e99
     qemu_close(s->fd);
218e99
@@ -1701,11 +1732,17 @@ static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
218e99
                       Error **errp)
218e99
 {
218e99
     BDRVRawState *s = bs->opaque;
218e99
+    Error *local_err = NULL;
218e99
+    int ret;
218e99
 
218e99
     s->type = FTYPE_CD;
218e99
 
218e99
     /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
218e99
-    return raw_open_common(bs, options, flags, O_NONBLOCK);
218e99
+    ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
218e99
+    if (error_is_set(&local_err)) {
218e99
+        error_propagate(errp, local_err);
218e99
+    }
218e99
+    return ret;
218e99
 }
218e99
 
218e99
 static int cdrom_probe_device(const char *filename)
218e99
@@ -1809,13 +1846,18 @@ static BlockDriver bdrv_host_cdrom = {
218e99
 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags)
218e99
 {
218e99
     BDRVRawState *s = bs->opaque;
218e99
+    Error *local_err = NULL;
218e99
     int ret;
218e99
 
218e99
     s->type = FTYPE_CD;
218e99
 
218e99
-    ret = raw_open_common(bs, options, flags, 0);
218e99
-    if (ret)
218e99
+    ret = raw_open_common(bs, options, flags, 0, &local_err);
218e99
+    if (ret) {
218e99
+        if (error_is_set(&local_err)) {
218e99
+            error_propagate(errp, local_err);
218e99
+        }
218e99
         return ret;
218e99
+    }
218e99
 
218e99
     /* make sure the door isn't locked at this time */
218e99
     ioctl(s->fd, CDIOCALLOW);
218e99
diff --git a/tests/qemu-iotests/051.out b/tests/qemu-iotests/051.out
218e99
index fe0b347..54a6b3a 100644
218e99
--- a/tests/qemu-iotests/051.out
218e99
+++ b/tests/qemu-iotests/051.out
218e99
@@ -166,6 +166,6 @@ Testing: -drive file=foo:bar
218e99
 QEMU_PROG: -drive file=foo:bar: could not open disk image foo:bar: Unknown protocol
218e99
 
218e99
 Testing: -drive file.filename=foo:bar
218e99
-QEMU_PROG: -drive file.filename=foo:bar: could not open disk image ide0-hd0: Could not open 'foo:bar': No such file or directory
218e99
+QEMU_PROG: -drive file.filename=foo:bar: could not open disk image ide0-hd0: Could not open file: No such file or directory
218e99
 
218e99
 *** done
218e99
-- 
218e99
1.7.1
218e99