Blame SOURCES/kvm-job-Add-error-message-for-failing-jobs.patch

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