05bba0
From f262d114ddfe11bad7b2f109cb965873f132f74e Mon Sep 17 00:00:00 2001
05bba0
From: Max Reitz <mreitz@redhat.com>
05bba0
Date: Sat, 13 Jun 2015 16:22:02 +0200
05bba0
Subject: [PATCH 08/42] qcow2: Add qcow2_signal_corruption()
05bba0
MIME-Version: 1.0
05bba0
Content-Type: text/plain; charset=UTF-8
05bba0
Content-Transfer-Encoding: 8bit
05bba0
05bba0
Message-id: <1434212556-3927-9-git-send-email-mreitz@redhat.com>
05bba0
Patchwork-id: 66027
05bba0
O-Subject: [RHEL-7.2 qemu-kvm PATCH 08/42] qcow2: Add qcow2_signal_corruption()
05bba0
Bugzilla: 1129893
05bba0
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
05bba0
RH-Acked-by: Fam Zheng <famz@redhat.com>
05bba0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
05bba0
BZ: 1129893
05bba0
05bba0
Add a helper function for easily marking an image corrupt (on fatal
05bba0
corruptions) while outputting an informative message to stderr and via
05bba0
QAPI.
05bba0
05bba0
Signed-off-by: Max Reitz <mreitz@redhat.com>
05bba0
Reviewed-by: Eric Blake <eblake@redhat.com>
05bba0
Reviewed-by: BenoƮt Canet <benoit.canet@nodalink.com>
05bba0
Message-id: 1409926039-29044-3-git-send-email-mreitz@redhat.com
05bba0
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
(cherry picked from commit 85186ebdac7e183242deaa55d5049988de832be1)
05bba0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
05bba0
Conflicts:
05bba0
	block/qcow2.c
05bba0
05bba0
No qapi_event_send_*() downstream.
05bba0
05bba0
Signed-off-by: Max Reitz <mreitz@redhat.com>
05bba0
---
05bba0
 block/qcow2.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
05bba0
 block/qcow2.h |  5 +++++
05bba0
 2 files changed, 67 insertions(+)
05bba0
05bba0
diff --git a/block/qcow2.c b/block/qcow2.c
05bba0
index 6026f8a..be7e8e8 100644
05bba0
--- a/block/qcow2.c
05bba0
+++ b/block/qcow2.c
05bba0
@@ -31,6 +31,8 @@
05bba0
 #include "qapi/qmp/qerror.h"
05bba0
 #include "qapi/qmp/qbool.h"
05bba0
 #include "qapi/util.h"
05bba0
+#include "qapi/qmp/types.h"
05bba0
+#include "monitor/monitor.h"
05bba0
 #include "trace.h"
05bba0
 
05bba0
 /*
05bba0
@@ -2329,6 +2331,66 @@ static int qcow2_amend_options(BlockDriverState *bs,
05bba0
     return 0;
05bba0
 }
05bba0
 
05bba0
+/*
05bba0
+ * If offset or size are negative, respectively, they will not be included in
05bba0
+ * the BLOCK_IMAGE_CORRUPTED event emitted.
05bba0
+ * fatal will be ignored for read-only BDS; corruptions found there will always
05bba0
+ * be considered non-fatal.
05bba0
+ */
05bba0
+void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
05bba0
+                             int64_t size, const char *message_format, ...)
05bba0
+{
05bba0
+    BDRVQcowState *s = bs->opaque;
05bba0
+    char *message;
05bba0
+    QObject *data;
05bba0
+    va_list ap;
05bba0
+
05bba0
+    fatal = fatal && !bs->read_only;
05bba0
+
05bba0
+    if (s->signaled_corruption &&
05bba0
+        (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
05bba0
+    {
05bba0
+        return;
05bba0
+    }
05bba0
+
05bba0
+    va_start(ap, message_format);
05bba0
+    message = g_strdup_vprintf(message_format, ap);
05bba0
+    va_end(ap);
05bba0
+
05bba0
+    if (fatal) {
05bba0
+        fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
05bba0
+                "corruption events will be suppressed\n", message);
05bba0
+    } else {
05bba0
+        fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
05bba0
+                "corruption events will be suppressed\n", message);
05bba0
+    }
05bba0
+
05bba0
+    assert((offset >= 0) == (size >= 0));
05bba0
+
05bba0
+    if (offset >= 0) {
05bba0
+        data = qobject_from_jsonf("{ 'device': %s, 'msg': %s, 'offset': %"
05bba0
+                                  PRId64 ", 'size': %" PRId64 ", 'fatal': %s }",
05bba0
+                                  bdrv_get_device_name(bs), message,
05bba0
+                                  offset, size, fatal ? "true" : "false");
05bba0
+    } else {
05bba0
+        data = qobject_from_jsonf("{ 'device': %s, 'msg': %s, 'fatal': %s }",
05bba0
+                                  bdrv_get_device_name(bs), message,
05bba0
+                                  fatal ? "true" : "false");
05bba0
+    }
05bba0
+
05bba0
+    monitor_protocol_event(QEVENT_BLOCK_IMAGE_CORRUPTED, data);
05bba0
+    qobject_decref(data);
05bba0
+
05bba0
+    g_free(message);
05bba0
+
05bba0
+    if (fatal) {
05bba0
+        qcow2_mark_corrupt(bs);
05bba0
+        bs->drv = NULL; /* make BDS unusable */
05bba0
+    }
05bba0
+
05bba0
+    s->signaled_corruption = true;
05bba0
+}
05bba0
+
05bba0
 static QEMUOptionParameter qcow2_create_options[] = {
05bba0
     {
05bba0
         .name = BLOCK_OPT_SIZE,
05bba0
diff --git a/block/qcow2.h b/block/qcow2.h
05bba0
index e958ab4..2138462 100644
05bba0
--- a/block/qcow2.h
05bba0
+++ b/block/qcow2.h
05bba0
@@ -252,6 +252,7 @@ typedef struct BDRVQcowState {
05bba0
     bool discard_passthrough[QCOW2_DISCARD_MAX];
05bba0
 
05bba0
     int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
05bba0
+    bool signaled_corruption;
05bba0
 
05bba0
     uint64_t incompatible_features;
05bba0
     uint64_t compatible_features;
05bba0
@@ -468,6 +469,10 @@ int qcow2_mark_corrupt(BlockDriverState *bs);
05bba0
 int qcow2_mark_consistent(BlockDriverState *bs);
05bba0
 int qcow2_update_header(BlockDriverState *bs);
05bba0
 
05bba0
+void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
05bba0
+                             int64_t size, const char *message_format, ...)
05bba0
+                             GCC_FMT_ATTR(5, 6);
05bba0
+
05bba0
 /* qcow2-refcount.c functions */
05bba0
 int qcow2_refcount_init(BlockDriverState *bs);
05bba0
 void qcow2_refcount_close(BlockDriverState *bs);
05bba0
-- 
05bba0
1.8.3.1
05bba0