From d6b3fb3db9409f62de3735f967b46fb23e97fda2 Mon Sep 17 00:00:00 2001
From: Luiz Capitulino <lcapitulino@redhat.com>
Date: Wed, 9 Jul 2014 14:31:52 +0200
Subject: [PATCH 204/212] qmp: improve debuggability of BLOCK_IO_ERROR event

Message-id: <1404916312-27953-4-git-send-email-lcapitulino@redhat.com>
Patchwork-id: 59730
O-Subject: [RHEV7.1 qemu-kvm-rhev PATCH 3/3] qmp: improve debuggability of BLOCK_IO_ERROR event
Bugzilla: 1116772
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Eric Blake <eblake@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>

This commit forward ports the following RHEL-7.0 commit to RHEL-7.1:

  commit bfea65d6f229fd65c78ae4daaddf0d0711d0aedd
  Author: Laszlo Ersek <lersek@redhat.com>
  Date:   Thu Nov 21 16:27:19 2013 +0100

  improve debuggability of BLOCK_IO_ERROR / BLOCK_JOB_ERROR (RHEL 6->7 fwd)

I had to redo the work because now events use the QAPI, but it
was straightforward.

There's one significant difference though: this commit does not
extend the BLOCK_JOB_ERROR event as did the RHEL-7.0 commit. The
reason is that the original requirement was to have this only on
the BLOCK_IO_ERROR event. Let's not spread it unless we're asked to.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 block.c                 | 16 ++++++++++++++--
 docs/qmp/qmp-events.txt | 10 +++++++++-
 qapi/block-core.json    | 18 +++++++++++++++++-
 3 files changed, 40 insertions(+), 4 deletions(-)

Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 block.c                 |   16 ++++++++++++++--
 docs/qmp/qmp-events.txt |   10 +++++++++-
 qapi/block-core.json    |   18 +++++++++++++++++-
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/block.c b/block.c
index aab65d9..f1b1cd4 100644
--- a/block.c
+++ b/block.c
@@ -3611,6 +3611,13 @@ static RHEL7BlockErrorReason get_rhel7_error_reason(int error)
     }
 }
 
+static void get_rhel7_error_debug_info(RHEL7BlockErrorDebugInfo *info,
+                                       int error)
+{
+    info->q_errno = error;
+    info->message = strerror(error);
+}
+
 /* This is done by device models because, while the block layer knows
  * about the error, it does not know whether an operation comes from
  * the device or the block layer (from a job, for example).
@@ -3619,9 +3626,14 @@ void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
                        bool is_read, int error)
 {
     RHEL7BlockErrorReason res;
+    RHEL7BlockErrorDebugInfo info;
 
     assert(error >= 0);
     res = get_rhel7_error_reason(error);
+    get_rhel7_error_debug_info(&info, error);
+
+    fprintf(stderr, "block I/O error in device '%s': %s (%d)\n",
+                    bdrv_get_device_name(bs), info.message, (int) info.q_errno);
 
     if (action == BLOCK_ERROR_ACTION_STOP) {
         /* First set the iostatus, so that "info block" returns an iostatus
@@ -3642,13 +3654,13 @@ void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
         qapi_event_send_block_io_error(bdrv_get_device_name(bs),
                                        is_read ? IO_OPERATION_TYPE_READ :
                                        IO_OPERATION_TYPE_WRITE,
-                                       action, res, &error_abort);
+                                       action, res, &info, &error_abort);
         qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
     } else {
         qapi_event_send_block_io_error(bdrv_get_device_name(bs),
                                        is_read ? IO_OPERATION_TYPE_READ :
                                        IO_OPERATION_TYPE_WRITE,
-                                       action, res, &error_abort);
+                                       action, res, &info, &error_abort);
     }
 }
 
diff --git a/docs/qmp/qmp-events.txt b/docs/qmp/qmp-events.txt
index 40c2917..c9dec63 100644
--- a/docs/qmp/qmp-events.txt
+++ b/docs/qmp/qmp-events.txt
@@ -69,6 +69,11 @@ Data:
 	"eperm": errno EPERM
 	"enospc": errno ENOSPC
 	"eother": any other errno (other than EIO, EPERM, ENOSPC)
+- "__com.redhat_debug_info": RHEL7 extension containing debug information for
+                             humans, applications should NOT read any
+                             information from this member (json-object):
+	- "errno": errno value (json-int)
+	- "message": error message returned by strerror() (json-string)
 
 Example:
 
@@ -76,7 +81,10 @@ Example:
     "data": { "device": "ide0-hd1",
               "operation": "write",
               "action": "stop",
-              "__com.redhat_reason": "enospc" },
+              "__com.redhat_reason": "enospc",
+			  "__com.redhat_debug_info": {
+                "message": "No space left on device",
+				"errno": 28 } }
     "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
 
 Note: If action is "stop", a STOP event will eventually follow the
diff --git a/qapi/block-core.json b/qapi/block-core.json
index 404059b..706c5d9 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -1505,6 +1505,17 @@
   'data': [ 'enospc', 'eperm', 'eio', 'eother' ] }
 
 ##
+# @RHEL7BlockErrorDebugInfo
+#
+# Debug information for a block I/O error
+#
+# @message: error message returned by strerror()
+# @errno: errno value
+##
+{ 'type': 'RHEL7BlockErrorDebugInfo',
+  'data': { 'message': 'str', 'errno': 'int' } }
+
+##
 # @BLOCK_IMAGE_CORRUPTED
 #
 # Emitted when a disk image is being marked corrupt
@@ -1542,6 +1553,10 @@
 #
 # @__com.redhat_reason: error reason (RHEL7 vendor extension)
 #
+# @__com.redhat_debug_info: debug information for humans, applications
+#                           should NOT read any information from this
+#                           member (RHEL7 vendor extension)
+#
 # Note: If action is "stop", a STOP event will eventually follow the
 # BLOCK_IO_ERROR event
 #
@@ -1550,7 +1565,8 @@
 { 'event': 'BLOCK_IO_ERROR',
   'data': { 'device': 'str', 'operation': 'IoOperationType',
             'action': 'BlockErrorAction',
-            '__com.redhat_reason': 'RHEL7BlockErrorReason' } }
+            '__com.redhat_reason': 'RHEL7BlockErrorReason',
+            '__com.redhat_debug_info': 'RHEL7BlockErrorDebugInfo' } }
 
 ##
 # @BLOCK_JOB_COMPLETED
-- 
1.7.1