thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ae23c9
From a04691e941048c14853d40cbb2a174e4e9b473a5 Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Tue, 26 Jun 2018 09:48:39 +0200
ae23c9
Subject: [PATCH 131/268] job: Add error message for failing jobs
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20180626094856.6924-57-kwolf@redhat.com>
ae23c9
Patchwork-id: 81110
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 56/73] job: Add error message for failing jobs
ae23c9
Bugzilla: 1513543
ae23c9
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
ae23c9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ae23c9
RH-Acked-by: Fam Zheng <famz@redhat.com>
ae23c9
ae23c9
So far we relied on job->ret and strerror() to produce an error message
ae23c9
for failed jobs. Not surprisingly, this tends to result in completely
ae23c9
useless messages.
ae23c9
ae23c9
This adds a Job.error field that can contain an error string for a
ae23c9
failing job, and a parameter to job_completed() that sets the field. As
ae23c9
a default, if NULL is passed, we continue to use strerror(job->ret).
ae23c9
ae23c9
All existing callers are changed to pass NULL. They can be improved in
ae23c9
separate patches.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: Max Reitz <mreitz@redhat.com>
ae23c9
Reviewed-by: Jeff Cody <jcody@redhat.com>
ae23c9
(cherry picked from commit 1266c9b9f5fa05877b979eece5963a2bd99c3bfd)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 block/backup.c            |  2 +-
ae23c9
 block/commit.c            |  2 +-
ae23c9
 block/mirror.c            |  2 +-
ae23c9
 block/stream.c            |  2 +-
ae23c9
 include/qemu/job.h        |  7 ++++++-
ae23c9
 job-qmp.c                 |  9 ++-------
ae23c9
 job.c                     | 16 ++++++++++++++--
ae23c9
 tests/test-bdrv-drain.c   |  2 +-
ae23c9
 tests/test-blockjob-txn.c |  2 +-
ae23c9
 tests/test-blockjob.c     |  2 +-
ae23c9
 10 files changed, 29 insertions(+), 17 deletions(-)
ae23c9
ae23c9
diff --git a/block/backup.c b/block/backup.c
ae23c9
index 4e228e9..5661435 100644
ae23c9
--- a/block/backup.c
ae23c9
+++ b/block/backup.c
ae23c9
@@ -321,7 +321,7 @@ static void backup_complete(Job *job, void *opaque)
ae23c9
 {
ae23c9
     BackupCompleteData *data = opaque;
ae23c9
 
ae23c9
-    job_completed(job, data->ret);
ae23c9
+    job_completed(job, data->ret, NULL);
ae23c9
     g_free(data);
ae23c9
 }
ae23c9
 
ae23c9
diff --git a/block/commit.c b/block/commit.c
ae23c9
index 6206661..e1814d9 100644
ae23c9
--- a/block/commit.c
ae23c9
+++ b/block/commit.c
ae23c9
@@ -117,7 +117,7 @@ static void commit_complete(Job *job, void *opaque)
ae23c9
      * bdrv_set_backing_hd() to fail. */
ae23c9
     block_job_remove_all_bdrv(bjob);
ae23c9
 
ae23c9
-    job_completed(job, ret);
ae23c9
+    job_completed(job, ret, NULL);
ae23c9
     g_free(data);
ae23c9
 
ae23c9
     /* If bdrv_drop_intermediate() didn't already do that, remove the commit
ae23c9
diff --git a/block/mirror.c b/block/mirror.c
ae23c9
index dcb66ec..435268b 100644
ae23c9
--- a/block/mirror.c
ae23c9
+++ b/block/mirror.c
ae23c9
@@ -581,7 +581,7 @@ static void mirror_exit(Job *job, void *opaque)
ae23c9
     blk_set_perm(bjob->blk, 0, BLK_PERM_ALL, &error_abort);
ae23c9
     blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort);
ae23c9
 
ae23c9
-    job_completed(job, data->ret);
ae23c9
+    job_completed(job, data->ret, NULL);
ae23c9
 
ae23c9
     g_free(data);
ae23c9
     bdrv_drained_end(src);
ae23c9
diff --git a/block/stream.c b/block/stream.c
ae23c9
index a5d6e0c..9264b68 100644
ae23c9
--- a/block/stream.c
ae23c9
+++ b/block/stream.c
ae23c9
@@ -93,7 +93,7 @@ out:
ae23c9
     }
ae23c9
 
ae23c9
     g_free(s->backing_file_str);
ae23c9
-    job_completed(job, data->ret);
ae23c9
+    job_completed(job, data->ret, NULL);
ae23c9
     g_free(data);
ae23c9
 }
ae23c9
 
ae23c9
diff --git a/include/qemu/job.h b/include/qemu/job.h
ae23c9
index 8c8badf..1d82053 100644
ae23c9
--- a/include/qemu/job.h
ae23c9
+++ b/include/qemu/job.h
ae23c9
@@ -124,6 +124,9 @@ typedef struct Job {
ae23c9
     /** Estimated progress_current value at the completion of the job */
ae23c9
     int64_t progress_total;
ae23c9
 
ae23c9
+    /** Error string for a failed job (NULL if, and only if, job->ret == 0) */
ae23c9
+    char *error;
ae23c9
+
ae23c9
     /** ret code passed to job_completed. */
ae23c9
     int ret;
ae23c9
 
ae23c9
@@ -466,13 +469,15 @@ void job_transition_to_ready(Job *job);
ae23c9
 /**
ae23c9
  * @job: The job being completed.
ae23c9
  * @ret: The status code.
ae23c9
+ * @error: The error message for a failing job (only with @ret < 0). If @ret is
ae23c9
+ *         negative, but NULL is given for @error, strerror() is used.
ae23c9
  *
ae23c9
  * Marks @job as completed. If @ret is non-zero, the job transaction it is part
ae23c9
  * of is aborted. If @ret is zero, the job moves into the WAITING state. If it
ae23c9
  * is the last job to complete in its transaction, all jobs in the transaction
ae23c9
  * move from WAITING to PENDING.
ae23c9
  */
ae23c9
-void job_completed(Job *job, int ret);
ae23c9
+void job_completed(Job *job, int ret, Error *error);
ae23c9
 
ae23c9
 /** Asynchronously complete the specified @job. */
ae23c9
 void job_complete(Job *job, Error **errp);
ae23c9
diff --git a/job-qmp.c b/job-qmp.c
ae23c9
index 7f38f63..410775d 100644
ae23c9
--- a/job-qmp.c
ae23c9
+++ b/job-qmp.c
ae23c9
@@ -136,14 +136,9 @@ void qmp_job_dismiss(const char *id, Error **errp)
ae23c9
 static JobInfo *job_query_single(Job *job, Error **errp)
ae23c9
 {
ae23c9
     JobInfo *info;
ae23c9
-    const char *errmsg = NULL;
ae23c9
 
ae23c9
     assert(!job_is_internal(job));
ae23c9
 
ae23c9
-    if (job->ret < 0) {
ae23c9
-        errmsg = strerror(-job->ret);
ae23c9
-    }
ae23c9
-
ae23c9
     info = g_new(JobInfo, 1);
ae23c9
     *info = (JobInfo) {
ae23c9
         .id                 = g_strdup(job->id),
ae23c9
@@ -151,8 +146,8 @@ static JobInfo *job_query_single(Job *job, Error **errp)
ae23c9
         .status             = job->status,
ae23c9
         .current_progress   = job->progress_current,
ae23c9
         .total_progress     = job->progress_total,
ae23c9
-        .has_error          = !!errmsg,
ae23c9
-        .error              = g_strdup(errmsg),
ae23c9
+        .has_error          = !!job->error,
ae23c9
+        .error              = g_strdup(job->error),
ae23c9
     };
ae23c9
 
ae23c9
     return info;
ae23c9
diff --git a/job.c b/job.c
ae23c9
index f026661..84e1402 100644
ae23c9
--- a/job.c
ae23c9
+++ b/job.c
ae23c9
@@ -369,6 +369,7 @@ void job_unref(Job *job)
ae23c9
 
ae23c9
         QLIST_REMOVE(job, job_list);
ae23c9
 
ae23c9
+        g_free(job->error);
ae23c9
         g_free(job->id);
ae23c9
         g_free(job);
ae23c9
     }
ae23c9
@@ -660,6 +661,9 @@ static void job_update_rc(Job *job)
ae23c9
         job->ret = -ECANCELED;
ae23c9
     }
ae23c9
     if (job->ret) {
ae23c9
+        if (!job->error) {
ae23c9
+            job->error = g_strdup(strerror(-job->ret));
ae23c9
+        }
ae23c9
         job_state_transition(job, JOB_STATUS_ABORTING);
ae23c9
     }
ae23c9
 }
ae23c9
@@ -782,6 +786,7 @@ static int job_prepare(Job *job)
ae23c9
 {
ae23c9
     if (job->ret == 0 && job->driver->prepare) {
ae23c9
         job->ret = job->driver->prepare(job);
ae23c9
+        job_update_rc(job);
ae23c9
     }
ae23c9
     return job->ret;
ae23c9
 }
ae23c9
@@ -855,10 +860,17 @@ static void job_completed_txn_success(Job *job)
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
-void job_completed(Job *job, int ret)
ae23c9
+void job_completed(Job *job, int ret, Error *error)
ae23c9
 {
ae23c9
     assert(job && job->txn && !job_is_completed(job));
ae23c9
+
ae23c9
     job->ret = ret;
ae23c9
+    if (error) {
ae23c9
+        assert(job->ret < 0);
ae23c9
+        job->error = g_strdup(error_get_pretty(error));
ae23c9
+        error_free(error);
ae23c9
+    }
ae23c9
+
ae23c9
     job_update_rc(job);
ae23c9
     trace_job_completed(job, ret, job->ret);
ae23c9
     if (job->ret) {
ae23c9
@@ -876,7 +888,7 @@ void job_cancel(Job *job, bool force)
ae23c9
     }
ae23c9
     job_cancel_async(job, force);
ae23c9
     if (!job_started(job)) {
ae23c9
-        job_completed(job, -ECANCELED);
ae23c9
+        job_completed(job, -ECANCELED, NULL);
ae23c9
     } else if (job->deferred_to_main_loop) {
ae23c9
         job_completed_txn_abort(job);
ae23c9
     } else {
ae23c9
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
ae23c9
index 2cba63b..a11c4cf 100644
ae23c9
--- a/tests/test-bdrv-drain.c
ae23c9
+++ b/tests/test-bdrv-drain.c
ae23c9
@@ -498,7 +498,7 @@ typedef struct TestBlockJob {
ae23c9
 
ae23c9
 static void test_job_completed(Job *job, void *opaque)
ae23c9
 {
ae23c9
-    job_completed(job, 0);
ae23c9
+    job_completed(job, 0, NULL);
ae23c9
 }
ae23c9
 
ae23c9
 static void coroutine_fn test_job_start(void *opaque)
ae23c9
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
ae23c9
index fce8366..58d9b87 100644
ae23c9
--- a/tests/test-blockjob-txn.c
ae23c9
+++ b/tests/test-blockjob-txn.c
ae23c9
@@ -34,7 +34,7 @@ static void test_block_job_complete(Job *job, void *opaque)
ae23c9
         rc = -ECANCELED;
ae23c9
     }
ae23c9
 
ae23c9
-    job_completed(job, rc);
ae23c9
+    job_completed(job, rc, NULL);
ae23c9
     bdrv_unref(bs);
ae23c9
 }
ae23c9
 
ae23c9
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
ae23c9
index e408d52..cb42f06 100644
ae23c9
--- a/tests/test-blockjob.c
ae23c9
+++ b/tests/test-blockjob.c
ae23c9
@@ -167,7 +167,7 @@ static void cancel_job_completed(Job *job, void *opaque)
ae23c9
 {
ae23c9
     CancelJob *s = opaque;
ae23c9
     s->completed = true;
ae23c9
-    job_completed(job, 0);
ae23c9
+    job_completed(job, 0, NULL);
ae23c9
 }
ae23c9
 
ae23c9
 static void cancel_job_complete(Job *job, Error **errp)
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9