Browse Source

jobs: canonize Error object

Jobs presently use both an Error object in the case of the create job,
and char strings in the case of generic errors elsewhere.

Unify the two paths as just j->err, and remove the extra argument from
job_completed. The integer error code for job_completed is kept for now,
to be removed shortly in a separate patch.

Signed-off-by: John Snow <jsnow@redhat.com>
Message-id: 2018083001.19765-3-jsnow@redhat.com
[mreitz: Dropped a superfluous g_strdup()]
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
tags/v3.1.0-rc0
John Snow 9 months ago
parent
commit
3d1f8b07a4
11 changed files with 26 additions and 30 deletions
  1. 1
    1
      block/backup.c
  2. 1
    1
      block/commit.c
  3. 2
    3
      block/create.c
  4. 1
    1
      block/mirror.c
  5. 1
    1
      block/stream.c
  6. 8
    6
      include/qemu/job.h
  7. 3
    2
      job-qmp.c
  8. 6
    12
      job.c
  9. 1
    1
      tests/test-bdrv-drain.c
  10. 1
    1
      tests/test-blockjob-txn.c
  11. 1
    1
      tests/test-blockjob.c

+ 1
- 1
block/backup.c View File

@@ -388,7 +388,7 @@ static void backup_complete(Job *job, void *opaque)
388 388
 {
389 389
     BackupCompleteData *data = opaque;
390 390
 
391
-    job_completed(job, data->ret, NULL);
391
+    job_completed(job, data->ret);
392 392
     g_free(data);
393 393
 }
394 394
 

+ 1
- 1
block/commit.c View File

@@ -117,7 +117,7 @@ static void commit_complete(Job *job, void *opaque)
117 117
      * bdrv_set_backing_hd() to fail. */
118 118
     block_job_remove_all_bdrv(bjob);
119 119
 
120
-    job_completed(job, ret, NULL);
120
+    job_completed(job, ret);
121 121
     g_free(data);
122 122
 
123 123
     /* If bdrv_drop_intermediate() didn't already do that, remove the commit

+ 2
- 3
block/create.c View File

@@ -35,14 +35,13 @@ typedef struct BlockdevCreateJob {
35 35
     BlockDriver *drv;
36 36
     BlockdevCreateOptions *opts;
37 37
     int ret;
38
-    Error *err;
39 38
 } BlockdevCreateJob;
40 39
 
41 40
 static void blockdev_create_complete(Job *job, void *opaque)
42 41
 {
43 42
     BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
44 43
 
45
-    job_completed(job, s->ret, s->err);
44
+    job_completed(job, s->ret);
46 45
 }
47 46
 
48 47
 static int coroutine_fn blockdev_create_run(Job *job, Error **errp)
@@ -50,7 +49,7 @@ static int coroutine_fn blockdev_create_run(Job *job, Error **errp)
50 49
     BlockdevCreateJob *s = container_of(job, BlockdevCreateJob, common);
51 50
 
52 51
     job_progress_set_remaining(&s->common, 1);
53
-    s->ret = s->drv->bdrv_co_create(s->opts, &s->err);
52
+    s->ret = s->drv->bdrv_co_create(s->opts, errp);
54 53
     job_progress_update(&s->common, 1);
55 54
 
56 55
     qapi_free_BlockdevCreateOptions(s->opts);

+ 1
- 1
block/mirror.c View File

@@ -710,7 +710,7 @@ static void mirror_exit(Job *job, void *opaque)
710 710
     blk_insert_bs(bjob->blk, mirror_top_bs, &error_abort);
711 711
 
712 712
     bs_opaque->job = NULL;
713
-    job_completed(job, data->ret, NULL);
713
+    job_completed(job, data->ret);
714 714
 
715 715
     g_free(data);
716 716
     bdrv_drained_end(src);

+ 1
- 1
block/stream.c View File

@@ -93,7 +93,7 @@ out:
93 93
     }
94 94
 
95 95
     g_free(s->backing_file_str);
96
-    job_completed(job, data->ret, NULL);
96
+    job_completed(job, data->ret);
97 97
     g_free(data);
98 98
 }
99 99
 

+ 8
- 6
include/qemu/job.h View File

@@ -124,12 +124,16 @@ typedef struct Job {
124 124
     /** Estimated progress_current value at the completion of the job */
125 125
     int64_t progress_total;
126 126
 
127
-    /** Error string for a failed job (NULL if, and only if, job->ret == 0) */
128
-    char *error;
129
-
130 127
     /** ret code passed to job_completed. */
131 128
     int ret;
132 129
 
130
+    /**
131
+     * Error object for a failed job.
132
+     * If job->ret is nonzero and an error object was not set, it will be set
133
+     * to strerror(-job->ret) during job_completed.
134
+     */
135
+    Error *err;
136
+
133 137
     /** The completion function that will be called when the job completes.  */
134 138
     BlockCompletionFunc *cb;
135 139
 
@@ -484,15 +488,13 @@ void job_transition_to_ready(Job *job);
484 488
 /**
485 489
  * @job: The job being completed.
486 490
  * @ret: The status code.
487
- * @error: The error message for a failing job (only with @ret < 0). If @ret is
488
- *         negative, but NULL is given for @error, strerror() is used.
489 491
  *
490 492
  * Marks @job as completed. If @ret is non-zero, the job transaction it is part
491 493
  * of is aborted. If @ret is zero, the job moves into the WAITING state. If it
492 494
  * is the last job to complete in its transaction, all jobs in the transaction
493 495
  * move from WAITING to PENDING.
494 496
  */
495
-void job_completed(Job *job, int ret, Error *error);
497
+void job_completed(Job *job, int ret);
496 498
 
497 499
 /** Asynchronously complete the specified @job. */
498 500
 void job_complete(Job *job, Error **errp);

+ 3
- 2
job-qmp.c View File

@@ -146,8 +146,9 @@ static JobInfo *job_query_single(Job *job, Error **errp)
146 146
         .status             = job->status,
147 147
         .current_progress   = job->progress_current,
148 148
         .total_progress     = job->progress_total,
149
-        .has_error          = !!job->error,
150
-        .error              = g_strdup(job->error),
149
+        .has_error          = !!job->err,
150
+        .error              = job->err ? \
151
+                              g_strdup(error_get_pretty(job->err)) : NULL,
151 152
     };
152 153
 
153 154
     return info;

+ 6
- 12
job.c View File

@@ -369,7 +369,7 @@ void job_unref(Job *job)
369 369
 
370 370
         QLIST_REMOVE(job, job_list);
371 371
 
372
-        g_free(job->error);
372
+        error_free(job->err);
373 373
         g_free(job->id);
374 374
         g_free(job);
375 375
     }
@@ -546,7 +546,7 @@ static void coroutine_fn job_co_entry(void *opaque)
546 546
 
547 547
     assert(job && job->driver && job->driver->run);
548 548
     job_pause_point(job);
549
-    job->ret = job->driver->run(job, NULL);
549
+    job->ret = job->driver->run(job, &job->err);
550 550
 }
551 551
 
552 552
 
@@ -666,8 +666,8 @@ static void job_update_rc(Job *job)
666 666
         job->ret = -ECANCELED;
667 667
     }
668 668
     if (job->ret) {
669
-        if (!job->error) {
670
-            job->error = g_strdup(strerror(-job->ret));
669
+        if (!job->err) {
670
+            error_setg(&job->err, "%s", strerror(-job->ret));
671 671
         }
672 672
         job_state_transition(job, JOB_STATUS_ABORTING);
673 673
     }
@@ -865,17 +865,11 @@ static void job_completed_txn_success(Job *job)
865 865
     }
866 866
 }
867 867
 
868
-void job_completed(Job *job, int ret, Error *error)
868
+void job_completed(Job *job, int ret)
869 869
 {
870 870
     assert(job && job->txn && !job_is_completed(job));
871 871
 
872 872
     job->ret = ret;
873
-    if (error) {
874
-        assert(job->ret < 0);
875
-        job->error = g_strdup(error_get_pretty(error));
876
-        error_free(error);
877
-    }
878
-
879 873
     job_update_rc(job);
880 874
     trace_job_completed(job, ret, job->ret);
881 875
     if (job->ret) {
@@ -893,7 +887,7 @@ void job_cancel(Job *job, bool force)
893 887
     }
894 888
     job_cancel_async(job, force);
895 889
     if (!job_started(job)) {
896
-        job_completed(job, -ECANCELED, NULL);
890
+        job_completed(job, -ECANCELED);
897 891
     } else if (job->deferred_to_main_loop) {
898 892
         job_completed_txn_abort(job);
899 893
     } else {

+ 1
- 1
tests/test-bdrv-drain.c View File

@@ -754,7 +754,7 @@ typedef struct TestBlockJob {
754 754
 
755 755
 static void test_job_completed(Job *job, void *opaque)
756 756
 {
757
-    job_completed(job, 0, NULL);
757
+    job_completed(job, 0);
758 758
 }
759 759
 
760 760
 static int coroutine_fn test_job_run(Job *job, Error **errp)

+ 1
- 1
tests/test-blockjob-txn.c View File

@@ -34,7 +34,7 @@ static void test_block_job_complete(Job *job, void *opaque)
34 34
         rc = -ECANCELED;
35 35
     }
36 36
 
37
-    job_completed(job, rc, NULL);
37
+    job_completed(job, rc);
38 38
     bdrv_unref(bs);
39 39
 }
40 40
 

+ 1
- 1
tests/test-blockjob.c View File

@@ -167,7 +167,7 @@ static void cancel_job_completed(Job *job, void *opaque)
167 167
 {
168 168
     CancelJob *s = opaque;
169 169
     s->completed = true;
170
-    job_completed(job, 0, NULL);
170
+    job_completed(job, 0);
171 171
 }
172 172
 
173 173
 static void cancel_job_complete(Job *job, Error **errp)

Loading…
Cancel
Save