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