9ae3a8
From ef9c4188157e2349ff68710982d9922f4a335b82 Mon Sep 17 00:00:00 2001
9ae3a8
From: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Date: Mon, 13 Jan 2014 20:52:04 +0100
9ae3a8
Subject: [PATCH 12/16] block: vhdx - improve error message, and .bdrv_check implementation
9ae3a8
9ae3a8
RH-Author: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Message-id: <f13406a07edd6ca0f7c7c5a9f87c0376dc4c1663.1389634483.git.jcody@redhat.com>
9ae3a8
Patchwork-id: 56643
9ae3a8
O-Subject: [RHEL7 qemu-kvm PATCH] block: vhdx - improve error message, and .bdrv_check implementation
9ae3a8
Bugzilla: 1035001
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
9ae3a8
If there is a dirty log file to be replayed in a VHDX image, it is
9ae3a8
replayed in .vhdx_open().  However, if the file is opened read-only,
9ae3a8
then a somewhat cryptic error message results.
9ae3a8
9ae3a8
This adds a more helpful error message for the user.  If an image file
9ae3a8
contains a log to be replayed, and is opened read-only, the user is
9ae3a8
instructed to run 'qemu-img check -r all' on the image file.
9ae3a8
9ae3a8
Running qemu-img check -r all will cause the image file to be opened
9ae3a8
r/w, which will replay the log file.  If a log file replay is detected,
9ae3a8
this is flagged, and bdrv_check will increase the corruptions_fixed
9ae3a8
count for the image.
9ae3a8
9ae3a8
[Fixed typo in error message that was pointed out by Eric Blake
9ae3a8
<eblake@redhat.com>.
9ae3a8
--Stefan]
9ae3a8
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit 7e30e6a6746b417c7e0dbc9af009560fbb63f336)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	block/vhdx.c
9ae3a8
9ae3a8
RHEL7 Notes:  Conflict due to different order of members in the
9ae3a8
              BlockDriver struct.  Resolved so that the order matches
9ae3a8
              what is upstream.
9ae3a8
9ae3a8
BZ 1035001
9ae3a8
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=6861970
9ae3a8
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
---
9ae3a8
 block/vhdx-log.c | 13 ++++++++++++-
9ae3a8
 block/vhdx.c     | 25 ++++++++++++++++++++++---
9ae3a8
 block/vhdx.h     |  5 ++++-
9ae3a8
 3 files changed, 38 insertions(+), 5 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/vhdx-log.c |   13 ++++++++++++-
9ae3a8
 block/vhdx.c     |   25 ++++++++++++++++++++++---
9ae3a8
 block/vhdx.h     |    5 ++++-
9ae3a8
 3 files changed, 38 insertions(+), 5 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/vhdx-log.c b/block/vhdx-log.c
9ae3a8
index ee5583c..8c9ae0d 100644
9ae3a8
--- a/block/vhdx-log.c
9ae3a8
+++ b/block/vhdx-log.c
9ae3a8
@@ -706,7 +706,8 @@ exit:
9ae3a8
  *
9ae3a8
  * If read-only, we must replay the log in RAM (or refuse to open
9ae3a8
  * a dirty VHDX file read-only) */
9ae3a8
-int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed)
9ae3a8
+int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed,
9ae3a8
+                   Error **errp)
9ae3a8
 {
9ae3a8
     int ret = 0;
9ae3a8
     VHDXHeader *hdr;
9ae3a8
@@ -761,6 +762,16 @@ int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed)
9ae3a8
     }
9ae3a8
 
9ae3a8
     if (logs.valid) {
9ae3a8
+        if (bs->read_only) {
9ae3a8
+            ret = -EPERM;
9ae3a8
+            error_setg_errno(errp, EPERM,
9ae3a8
+                             "VHDX image file '%s' opened read-only, but "
9ae3a8
+                             "contains a log that needs to be replayed.  To "
9ae3a8
+                             "replay the log, execute:\n qemu-img check -r "
9ae3a8
+                             "all '%s'",
9ae3a8
+                             bs->filename, bs->filename);
9ae3a8
+            goto exit;
9ae3a8
+        }
9ae3a8
         /* now flush the log */
9ae3a8
         ret = vhdx_log_flush(bs, s, &logs);
9ae3a8
         if (ret < 0) {
9ae3a8
diff --git a/block/vhdx.c b/block/vhdx.c
9ae3a8
index 8e3b371..8b1a5c9 100644
9ae3a8
--- a/block/vhdx.c
9ae3a8
+++ b/block/vhdx.c
9ae3a8
@@ -878,7 +878,6 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
     int ret = 0;
9ae3a8
     uint32_t i;
9ae3a8
     uint64_t signature;
9ae3a8
-    bool log_flushed = false;
9ae3a8
 
9ae3a8
 
9ae3a8
     s->bat = NULL;
9ae3a8
@@ -907,7 +906,7 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    ret = vhdx_parse_log(bs, s, &log_flushed);
9ae3a8
+    ret = vhdx_parse_log(bs, s, &s->log_replayed_on_open, errp);
9ae3a8
     if (ret < 0) {
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
@@ -1854,6 +1853,24 @@ exit:
9ae3a8
     return ret;
9ae3a8
 }
9ae3a8
 
9ae3a8
+/* If opened r/w, the VHDX driver will automatically replay the log,
9ae3a8
+ * if one is present, inside the vhdx_open() call.
9ae3a8
+ *
9ae3a8
+ * If qemu-img check -r all is called, the image is automatically opened
9ae3a8
+ * r/w and any log has already been replayed, so there is nothing (currently)
9ae3a8
+ * for us to do here
9ae3a8
+ */
9ae3a8
+static int vhdx_check(BlockDriverState *bs, BdrvCheckResult *result,
9ae3a8
+                       BdrvCheckMode fix)
9ae3a8
+{
9ae3a8
+    BDRVVHDXState *s = bs->opaque;
9ae3a8
+
9ae3a8
+    if (s->log_replayed_on_open) {
9ae3a8
+        result->corruptions_fixed++;
9ae3a8
+    }
9ae3a8
+    return 0;
9ae3a8
+}
9ae3a8
+
9ae3a8
 static QEMUOptionParameter vhdx_create_options[] = {
9ae3a8
     {
9ae3a8
         .name = BLOCK_OPT_SIZE,
9ae3a8
@@ -1896,8 +1913,10 @@ static BlockDriver bdrv_vhdx = {
9ae3a8
     .bdrv_reopen_prepare    = vhdx_reopen_prepare,
9ae3a8
     .bdrv_co_readv          = vhdx_co_readv,
9ae3a8
     .bdrv_co_writev         = vhdx_co_writev,
9ae3a8
-    .bdrv_get_info          = vhdx_get_info,
9ae3a8
     .bdrv_create            = vhdx_create,
9ae3a8
+    .bdrv_get_info          = vhdx_get_info,
9ae3a8
+    .bdrv_check             = vhdx_check,
9ae3a8
+
9ae3a8
     .create_options         = vhdx_create_options,
9ae3a8
 };
9ae3a8
 
9ae3a8
diff --git a/block/vhdx.h b/block/vhdx.h
9ae3a8
index 365eca0..469e95c 100644
9ae3a8
--- a/block/vhdx.h
9ae3a8
+++ b/block/vhdx.h
9ae3a8
@@ -394,6 +394,8 @@ typedef struct BDRVVHDXState {
9ae3a8
 
9ae3a8
     Error *migration_blocker;
9ae3a8
 
9ae3a8
+    bool log_replayed_on_open;
9ae3a8
+
9ae3a8
     QLIST_HEAD(VHDXRegionHead, VHDXRegionEntry) regions;
9ae3a8
 } BDRVVHDXState;
9ae3a8
 
9ae3a8
@@ -408,7 +410,8 @@ uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
9ae3a8
 
9ae3a8
 bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset);
9ae3a8
 
9ae3a8
-int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed);
9ae3a8
+int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed,
9ae3a8
+                   Error **errp);
9ae3a8
 
9ae3a8
 int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
9ae3a8
                              void *data, uint32_t length, uint64_t offset);
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8