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