xref: /qemu/blockjob.c (revision 76d554e2)
12f0c9fe6SPaolo Bonzini /*
22f0c9fe6SPaolo Bonzini  * QEMU System Emulator block driver
32f0c9fe6SPaolo Bonzini  *
42f0c9fe6SPaolo Bonzini  * Copyright (c) 2011 IBM Corp.
52f0c9fe6SPaolo Bonzini  * Copyright (c) 2012 Red Hat, Inc.
62f0c9fe6SPaolo Bonzini  *
72f0c9fe6SPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
82f0c9fe6SPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
92f0c9fe6SPaolo Bonzini  * in the Software without restriction, including without limitation the rights
102f0c9fe6SPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
112f0c9fe6SPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
122f0c9fe6SPaolo Bonzini  * furnished to do so, subject to the following conditions:
132f0c9fe6SPaolo Bonzini  *
142f0c9fe6SPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
152f0c9fe6SPaolo Bonzini  * all copies or substantial portions of the Software.
162f0c9fe6SPaolo Bonzini  *
172f0c9fe6SPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
182f0c9fe6SPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
192f0c9fe6SPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
202f0c9fe6SPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
212f0c9fe6SPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
222f0c9fe6SPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
232f0c9fe6SPaolo Bonzini  * THE SOFTWARE.
242f0c9fe6SPaolo Bonzini  */
252f0c9fe6SPaolo Bonzini 
26d38ea87aSPeter Maydell #include "qemu/osdep.h"
272f0c9fe6SPaolo Bonzini #include "qemu-common.h"
28737e150eSPaolo Bonzini #include "block/block.h"
29c87621eaSJohn Snow #include "block/blockjob_int.h"
30737e150eSPaolo Bonzini #include "block/block_int.h"
31373340b2SMax Reitz #include "sysemu/block-backend.h"
32cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h"
337b1b5d19SPaolo Bonzini #include "qapi/qmp/qjson.h"
3410817bf0SDaniel P. Berrange #include "qemu/coroutine.h"
357f0317cfSAlberto Garcia #include "qemu/id.h"
362f0c9fe6SPaolo Bonzini #include "qmp-commands.h"
371de7afc9SPaolo Bonzini #include "qemu/timer.h"
385a2d2cbdSWenchao Xia #include "qapi-event.h"
392f0c9fe6SPaolo Bonzini 
408254b6d9SJohn Snow static void block_job_event_cancelled(BlockJob *job);
418254b6d9SJohn Snow static void block_job_event_completed(BlockJob *job, const char *msg);
428254b6d9SJohn Snow 
43c55a832fSFam Zheng /* Transactional group of block jobs */
44c55a832fSFam Zheng struct BlockJobTxn {
45c55a832fSFam Zheng 
46c55a832fSFam Zheng     /* Is this txn being cancelled? */
47c55a832fSFam Zheng     bool aborting;
48c55a832fSFam Zheng 
49c55a832fSFam Zheng     /* List of jobs */
50c55a832fSFam Zheng     QLIST_HEAD(, BlockJob) jobs;
51c55a832fSFam Zheng 
52c55a832fSFam Zheng     /* Reference count */
53c55a832fSFam Zheng     int refcnt;
54c55a832fSFam Zheng };
55c55a832fSFam Zheng 
56a7112795SAlberto Garcia static QLIST_HEAD(, BlockJob) block_jobs = QLIST_HEAD_INITIALIZER(block_jobs);
57a7112795SAlberto Garcia 
58*76d554e2SKevin Wolf static char *child_job_get_parent_desc(BdrvChild *c)
59*76d554e2SKevin Wolf {
60*76d554e2SKevin Wolf     BlockJob *job = c->opaque;
61*76d554e2SKevin Wolf     return g_strdup_printf("%s job '%s'",
62*76d554e2SKevin Wolf                            BlockJobType_lookup[job->driver->job_type],
63*76d554e2SKevin Wolf                            job->id);
64*76d554e2SKevin Wolf }
65*76d554e2SKevin Wolf 
66*76d554e2SKevin Wolf static const BdrvChildRole child_job = {
67*76d554e2SKevin Wolf     .get_parent_desc    = child_job_get_parent_desc,
68*76d554e2SKevin Wolf     .stay_at_node       = true,
69*76d554e2SKevin Wolf };
70*76d554e2SKevin Wolf 
71a7112795SAlberto Garcia BlockJob *block_job_next(BlockJob *job)
72a7112795SAlberto Garcia {
73a7112795SAlberto Garcia     if (!job) {
74a7112795SAlberto Garcia         return QLIST_FIRST(&block_jobs);
75a7112795SAlberto Garcia     }
76a7112795SAlberto Garcia     return QLIST_NEXT(job, job_list);
77a7112795SAlberto Garcia }
78a7112795SAlberto Garcia 
79ffb1f10cSAlberto Garcia BlockJob *block_job_get(const char *id)
80ffb1f10cSAlberto Garcia {
81ffb1f10cSAlberto Garcia     BlockJob *job;
82ffb1f10cSAlberto Garcia 
83ffb1f10cSAlberto Garcia     QLIST_FOREACH(job, &block_jobs, job_list) {
84559b935fSJohn Snow         if (job->id && !strcmp(id, job->id)) {
85ffb1f10cSAlberto Garcia             return job;
86ffb1f10cSAlberto Garcia         }
87ffb1f10cSAlberto Garcia     }
88ffb1f10cSAlberto Garcia 
89ffb1f10cSAlberto Garcia     return NULL;
90ffb1f10cSAlberto Garcia }
91ffb1f10cSAlberto Garcia 
92463e0be1SStefan Hajnoczi static void block_job_attached_aio_context(AioContext *new_context,
93463e0be1SStefan Hajnoczi                                            void *opaque)
94463e0be1SStefan Hajnoczi {
95463e0be1SStefan Hajnoczi     BlockJob *job = opaque;
96463e0be1SStefan Hajnoczi 
97463e0be1SStefan Hajnoczi     if (job->driver->attached_aio_context) {
98463e0be1SStefan Hajnoczi         job->driver->attached_aio_context(job, new_context);
99463e0be1SStefan Hajnoczi     }
100463e0be1SStefan Hajnoczi 
101463e0be1SStefan Hajnoczi     block_job_resume(job);
102463e0be1SStefan Hajnoczi }
103463e0be1SStefan Hajnoczi 
104bae8196dSPaolo Bonzini static void block_job_drain(BlockJob *job)
105bae8196dSPaolo Bonzini {
106bae8196dSPaolo Bonzini     /* If job is !job->busy this kicks it into the next pause point. */
107bae8196dSPaolo Bonzini     block_job_enter(job);
108bae8196dSPaolo Bonzini 
109bae8196dSPaolo Bonzini     blk_drain(job->blk);
110bae8196dSPaolo Bonzini     if (job->driver->drain) {
111bae8196dSPaolo Bonzini         job->driver->drain(job);
112bae8196dSPaolo Bonzini     }
113bae8196dSPaolo Bonzini }
114bae8196dSPaolo Bonzini 
115463e0be1SStefan Hajnoczi static void block_job_detach_aio_context(void *opaque)
116463e0be1SStefan Hajnoczi {
117463e0be1SStefan Hajnoczi     BlockJob *job = opaque;
118463e0be1SStefan Hajnoczi 
119463e0be1SStefan Hajnoczi     /* In case the job terminates during aio_poll()... */
120463e0be1SStefan Hajnoczi     block_job_ref(job);
121463e0be1SStefan Hajnoczi 
122463e0be1SStefan Hajnoczi     block_job_pause(job);
123463e0be1SStefan Hajnoczi 
124463e0be1SStefan Hajnoczi     while (!job->paused && !job->completed) {
125bae8196dSPaolo Bonzini         block_job_drain(job);
126463e0be1SStefan Hajnoczi     }
127463e0be1SStefan Hajnoczi 
128463e0be1SStefan Hajnoczi     block_job_unref(job);
129463e0be1SStefan Hajnoczi }
130463e0be1SStefan Hajnoczi 
131*76d554e2SKevin Wolf int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
132*76d554e2SKevin Wolf                        uint64_t perm, uint64_t shared_perm, Error **errp)
13323d402d4SAlberto Garcia {
134*76d554e2SKevin Wolf     BdrvChild *c;
135*76d554e2SKevin Wolf 
136*76d554e2SKevin Wolf     c = bdrv_root_attach_child(bs, name, &child_job, perm, shared_perm,
137*76d554e2SKevin Wolf                                job, errp);
138*76d554e2SKevin Wolf     if (c == NULL) {
139*76d554e2SKevin Wolf         return -EPERM;
140*76d554e2SKevin Wolf     }
141*76d554e2SKevin Wolf 
142*76d554e2SKevin Wolf     job->nodes = g_slist_prepend(job->nodes, c);
14323d402d4SAlberto Garcia     bdrv_ref(bs);
14423d402d4SAlberto Garcia     bdrv_op_block_all(bs, job->blocker);
145*76d554e2SKevin Wolf 
146*76d554e2SKevin Wolf     return 0;
14723d402d4SAlberto Garcia }
14823d402d4SAlberto Garcia 
1497f0317cfSAlberto Garcia void *block_job_create(const char *job_id, const BlockJobDriver *driver,
150c6cc12bfSKevin Wolf                        BlockDriverState *bs, uint64_t perm,
151c6cc12bfSKevin Wolf                        uint64_t shared_perm, int64_t speed, int flags,
1527f0317cfSAlberto Garcia                        BlockCompletionFunc *cb, void *opaque, Error **errp)
1532f0c9fe6SPaolo Bonzini {
154b6d2e599SKevin Wolf     BlockBackend *blk;
1552f0c9fe6SPaolo Bonzini     BlockJob *job;
156d7086422SKevin Wolf     int ret;
1572f0c9fe6SPaolo Bonzini 
158628ff683SFam Zheng     if (bs->job) {
159c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
1602f0c9fe6SPaolo Bonzini         return NULL;
1612f0c9fe6SPaolo Bonzini     }
162b6d2e599SKevin Wolf 
163f81e0b45SJohn Snow     if (job_id == NULL && !(flags & BLOCK_JOB_INTERNAL)) {
1647f0317cfSAlberto Garcia         job_id = bdrv_get_device_name(bs);
1651562047cSKevin Wolf         if (!*job_id) {
1661562047cSKevin Wolf             error_setg(errp, "An explicit job ID is required for this node");
1671562047cSKevin Wolf             return NULL;
1681562047cSKevin Wolf         }
1697f0317cfSAlberto Garcia     }
1707f0317cfSAlberto Garcia 
171f81e0b45SJohn Snow     if (job_id) {
172f81e0b45SJohn Snow         if (flags & BLOCK_JOB_INTERNAL) {
173f81e0b45SJohn Snow             error_setg(errp, "Cannot specify job ID for internal block job");
174f81e0b45SJohn Snow             return NULL;
175f81e0b45SJohn Snow         }
176f81e0b45SJohn Snow 
1777f0317cfSAlberto Garcia         if (!id_wellformed(job_id)) {
1787f0317cfSAlberto Garcia             error_setg(errp, "Invalid job ID '%s'", job_id);
1797f0317cfSAlberto Garcia             return NULL;
1807f0317cfSAlberto Garcia         }
1817f0317cfSAlberto Garcia 
1827f0317cfSAlberto Garcia         if (block_job_get(job_id)) {
1837f0317cfSAlberto Garcia             error_setg(errp, "Job ID '%s' already in use", job_id);
1847f0317cfSAlberto Garcia             return NULL;
1857f0317cfSAlberto Garcia         }
186f81e0b45SJohn Snow     }
1877f0317cfSAlberto Garcia 
188c6cc12bfSKevin Wolf     blk = blk_new(perm, shared_perm);
189d7086422SKevin Wolf     ret = blk_insert_bs(blk, bs, errp);
190d7086422SKevin Wolf     if (ret < 0) {
191d7086422SKevin Wolf         blk_unref(blk);
192d7086422SKevin Wolf         return NULL;
193d7086422SKevin Wolf     }
194b6d2e599SKevin Wolf 
1953fc4b10aSFam Zheng     job = g_malloc0(driver->instance_size);
1963718d8abSFam Zheng     error_setg(&job->blocker, "block device is in use by block job: %s",
1973718d8abSFam Zheng                BlockJobType_lookup[driver->job_type]);
198*76d554e2SKevin Wolf     block_job_add_bdrv(job, "main node", bs, 0, BLK_PERM_ALL, &error_abort);
199b112a65cSStefan Hajnoczi     bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker);
2003718d8abSFam Zheng 
2013fc4b10aSFam Zheng     job->driver        = driver;
2027f0317cfSAlberto Garcia     job->id            = g_strdup(job_id);
203b6d2e599SKevin Wolf     job->blk           = blk;
2042f0c9fe6SPaolo Bonzini     job->cb            = cb;
2052f0c9fe6SPaolo Bonzini     job->opaque        = opaque;
2065ccac6f1SJohn Snow     job->busy          = false;
2075ccac6f1SJohn Snow     job->paused        = true;
2085ccac6f1SJohn Snow     job->pause_count   = 1;
20918930ba3SFam Zheng     job->refcnt        = 1;
2102f0c9fe6SPaolo Bonzini     bs->job = job;
2112f0c9fe6SPaolo Bonzini 
212a7112795SAlberto Garcia     QLIST_INSERT_HEAD(&block_jobs, job, job_list);
213a7112795SAlberto Garcia 
214463e0be1SStefan Hajnoczi     blk_add_aio_context_notifier(blk, block_job_attached_aio_context,
215463e0be1SStefan Hajnoczi                                  block_job_detach_aio_context, job);
216463e0be1SStefan Hajnoczi 
2172f0c9fe6SPaolo Bonzini     /* Only set speed when necessary to avoid NotSupported error */
2182f0c9fe6SPaolo Bonzini     if (speed != 0) {
2192f0c9fe6SPaolo Bonzini         Error *local_err = NULL;
2202f0c9fe6SPaolo Bonzini 
2212f0c9fe6SPaolo Bonzini         block_job_set_speed(job, speed, &local_err);
22284d18f06SMarkus Armbruster         if (local_err) {
22318930ba3SFam Zheng             block_job_unref(job);
2242f0c9fe6SPaolo Bonzini             error_propagate(errp, local_err);
2252f0c9fe6SPaolo Bonzini             return NULL;
2262f0c9fe6SPaolo Bonzini         }
2272f0c9fe6SPaolo Bonzini     }
2282f0c9fe6SPaolo Bonzini     return job;
2292f0c9fe6SPaolo Bonzini }
2302f0c9fe6SPaolo Bonzini 
231559b935fSJohn Snow bool block_job_is_internal(BlockJob *job)
232559b935fSJohn Snow {
233559b935fSJohn Snow     return (job->id == NULL);
234559b935fSJohn Snow }
235559b935fSJohn Snow 
2365ccac6f1SJohn Snow static bool block_job_started(BlockJob *job)
2375ccac6f1SJohn Snow {
2385ccac6f1SJohn Snow     return job->co;
2395ccac6f1SJohn Snow }
2405ccac6f1SJohn Snow 
2415ccac6f1SJohn Snow void block_job_start(BlockJob *job)
2425ccac6f1SJohn Snow {
2435ccac6f1SJohn Snow     assert(job && !block_job_started(job) && job->paused &&
2445ccac6f1SJohn Snow            !job->busy && job->driver->start);
2455ccac6f1SJohn Snow     job->co = qemu_coroutine_create(job->driver->start, job);
2465ccac6f1SJohn Snow     if (--job->pause_count == 0) {
2475ccac6f1SJohn Snow         job->paused = false;
2485ccac6f1SJohn Snow         job->busy = true;
2495ccac6f1SJohn Snow         qemu_coroutine_enter(job->co);
2505ccac6f1SJohn Snow     }
2515ccac6f1SJohn Snow }
2525ccac6f1SJohn Snow 
25318930ba3SFam Zheng void block_job_ref(BlockJob *job)
25497031164STing Wang {
25518930ba3SFam Zheng     ++job->refcnt;
25618930ba3SFam Zheng }
25797031164STing Wang 
25818930ba3SFam Zheng void block_job_unref(BlockJob *job)
25918930ba3SFam Zheng {
26018930ba3SFam Zheng     if (--job->refcnt == 0) {
26123d402d4SAlberto Garcia         GSList *l;
262b6d2e599SKevin Wolf         BlockDriverState *bs = blk_bs(job->blk);
263b6d2e599SKevin Wolf         bs->job = NULL;
26423d402d4SAlberto Garcia         for (l = job->nodes; l; l = l->next) {
265*76d554e2SKevin Wolf             BdrvChild *c = l->data;
266*76d554e2SKevin Wolf             bdrv_op_unblock_all(c->bs, job->blocker);
267*76d554e2SKevin Wolf             bdrv_root_unref_child(c);
26823d402d4SAlberto Garcia         }
26923d402d4SAlberto Garcia         g_slist_free(job->nodes);
270463e0be1SStefan Hajnoczi         blk_remove_aio_context_notifier(job->blk,
271463e0be1SStefan Hajnoczi                                         block_job_attached_aio_context,
272463e0be1SStefan Hajnoczi                                         block_job_detach_aio_context, job);
273b6d2e599SKevin Wolf         blk_unref(job->blk);
27497031164STing Wang         error_free(job->blocker);
2758ccb9569SKevin Wolf         g_free(job->id);
276a7112795SAlberto Garcia         QLIST_REMOVE(job, job_list);
27797031164STing Wang         g_free(job);
27897031164STing Wang     }
27918930ba3SFam Zheng }
28097031164STing Wang 
281c55a832fSFam Zheng static void block_job_completed_single(BlockJob *job)
282c55a832fSFam Zheng {
283c55a832fSFam Zheng     if (!job->ret) {
284c55a832fSFam Zheng         if (job->driver->commit) {
285c55a832fSFam Zheng             job->driver->commit(job);
286c55a832fSFam Zheng         }
287c55a832fSFam Zheng     } else {
288c55a832fSFam Zheng         if (job->driver->abort) {
289c55a832fSFam Zheng             job->driver->abort(job);
290c55a832fSFam Zheng         }
291c55a832fSFam Zheng     }
292e8a40bf7SJohn Snow     if (job->driver->clean) {
293e8a40bf7SJohn Snow         job->driver->clean(job);
294e8a40bf7SJohn Snow     }
2958254b6d9SJohn Snow 
2968254b6d9SJohn Snow     if (job->cb) {
297c55a832fSFam Zheng         job->cb(job->opaque, job->ret);
2988254b6d9SJohn Snow     }
2995ccac6f1SJohn Snow 
3005ccac6f1SJohn Snow     /* Emit events only if we actually started */
3015ccac6f1SJohn Snow     if (block_job_started(job)) {
3028254b6d9SJohn Snow         if (block_job_is_cancelled(job)) {
3038254b6d9SJohn Snow             block_job_event_cancelled(job);
3048254b6d9SJohn Snow         } else {
3058254b6d9SJohn Snow             const char *msg = NULL;
3068254b6d9SJohn Snow             if (job->ret < 0) {
3078254b6d9SJohn Snow                 msg = strerror(-job->ret);
3088254b6d9SJohn Snow             }
3098254b6d9SJohn Snow             block_job_event_completed(job, msg);
3108254b6d9SJohn Snow         }
3115ccac6f1SJohn Snow     }
3128254b6d9SJohn Snow 
313c55a832fSFam Zheng     if (job->txn) {
3141e93b9fbSVladimir Sementsov-Ogievskiy         QLIST_REMOVE(job, txn_list);
315c55a832fSFam Zheng         block_job_txn_unref(job->txn);
316c55a832fSFam Zheng     }
317c55a832fSFam Zheng     block_job_unref(job);
318c55a832fSFam Zheng }
319c55a832fSFam Zheng 
320c55a832fSFam Zheng static void block_job_completed_txn_abort(BlockJob *job)
321c55a832fSFam Zheng {
322c55a832fSFam Zheng     AioContext *ctx;
323c55a832fSFam Zheng     BlockJobTxn *txn = job->txn;
324c55a832fSFam Zheng     BlockJob *other_job, *next;
325c55a832fSFam Zheng 
326c55a832fSFam Zheng     if (txn->aborting) {
327c55a832fSFam Zheng         /*
328c55a832fSFam Zheng          * We are cancelled by another job, which will handle everything.
329c55a832fSFam Zheng          */
330c55a832fSFam Zheng         return;
331c55a832fSFam Zheng     }
332c55a832fSFam Zheng     txn->aborting = true;
333c55a832fSFam Zheng     /* We are the first failed job. Cancel other jobs. */
334c55a832fSFam Zheng     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
335b6d2e599SKevin Wolf         ctx = blk_get_aio_context(other_job->blk);
336c55a832fSFam Zheng         aio_context_acquire(ctx);
337c55a832fSFam Zheng     }
338c55a832fSFam Zheng     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
339c55a832fSFam Zheng         if (other_job == job || other_job->completed) {
340c55a832fSFam Zheng             /* Other jobs are "effectively" cancelled by us, set the status for
341c55a832fSFam Zheng              * them; this job, however, may or may not be cancelled, depending
342c55a832fSFam Zheng              * on the caller, so leave it. */
343c55a832fSFam Zheng             if (other_job != job) {
344c55a832fSFam Zheng                 other_job->cancelled = true;
345c55a832fSFam Zheng             }
346c55a832fSFam Zheng             continue;
347c55a832fSFam Zheng         }
348c55a832fSFam Zheng         block_job_cancel_sync(other_job);
349c55a832fSFam Zheng         assert(other_job->completed);
350c55a832fSFam Zheng     }
351c55a832fSFam Zheng     QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
352b6d2e599SKevin Wolf         ctx = blk_get_aio_context(other_job->blk);
353c55a832fSFam Zheng         block_job_completed_single(other_job);
354c55a832fSFam Zheng         aio_context_release(ctx);
355c55a832fSFam Zheng     }
356c55a832fSFam Zheng }
357c55a832fSFam Zheng 
358c55a832fSFam Zheng static void block_job_completed_txn_success(BlockJob *job)
359c55a832fSFam Zheng {
360c55a832fSFam Zheng     AioContext *ctx;
361c55a832fSFam Zheng     BlockJobTxn *txn = job->txn;
362c55a832fSFam Zheng     BlockJob *other_job, *next;
363c55a832fSFam Zheng     /*
364c55a832fSFam Zheng      * Successful completion, see if there are other running jobs in this
365c55a832fSFam Zheng      * txn.
366c55a832fSFam Zheng      */
367c55a832fSFam Zheng     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
368c55a832fSFam Zheng         if (!other_job->completed) {
369c55a832fSFam Zheng             return;
370c55a832fSFam Zheng         }
371c55a832fSFam Zheng     }
372c55a832fSFam Zheng     /* We are the last completed job, commit the transaction. */
373c55a832fSFam Zheng     QLIST_FOREACH_SAFE(other_job, &txn->jobs, txn_list, next) {
374b6d2e599SKevin Wolf         ctx = blk_get_aio_context(other_job->blk);
375c55a832fSFam Zheng         aio_context_acquire(ctx);
376c55a832fSFam Zheng         assert(other_job->ret == 0);
377c55a832fSFam Zheng         block_job_completed_single(other_job);
378c55a832fSFam Zheng         aio_context_release(ctx);
379c55a832fSFam Zheng     }
380c55a832fSFam Zheng }
381c55a832fSFam Zheng 
38265f46322SPaolo Bonzini void block_job_completed(BlockJob *job, int ret)
3832f0c9fe6SPaolo Bonzini {
384b6d2e599SKevin Wolf     assert(blk_bs(job->blk)->job == job);
385a689dbf2SFam Zheng     assert(!job->completed);
386a689dbf2SFam Zheng     job->completed = true;
387a689dbf2SFam Zheng     job->ret = ret;
388c55a832fSFam Zheng     if (!job->txn) {
389c55a832fSFam Zheng         block_job_completed_single(job);
390c55a832fSFam Zheng     } else if (ret < 0 || block_job_is_cancelled(job)) {
391c55a832fSFam Zheng         block_job_completed_txn_abort(job);
392c55a832fSFam Zheng     } else {
393c55a832fSFam Zheng         block_job_completed_txn_success(job);
394c55a832fSFam Zheng     }
3952f0c9fe6SPaolo Bonzini }
3962f0c9fe6SPaolo Bonzini 
3972f0c9fe6SPaolo Bonzini void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
3982f0c9fe6SPaolo Bonzini {
3992f0c9fe6SPaolo Bonzini     Error *local_err = NULL;
4002f0c9fe6SPaolo Bonzini 
4013fc4b10aSFam Zheng     if (!job->driver->set_speed) {
402c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_UNSUPPORTED);
4032f0c9fe6SPaolo Bonzini         return;
4042f0c9fe6SPaolo Bonzini     }
4053fc4b10aSFam Zheng     job->driver->set_speed(job, speed, &local_err);
40684d18f06SMarkus Armbruster     if (local_err) {
4072f0c9fe6SPaolo Bonzini         error_propagate(errp, local_err);
4082f0c9fe6SPaolo Bonzini         return;
4092f0c9fe6SPaolo Bonzini     }
4102f0c9fe6SPaolo Bonzini 
4112f0c9fe6SPaolo Bonzini     job->speed = speed;
4122f0c9fe6SPaolo Bonzini }
4132f0c9fe6SPaolo Bonzini 
414aeae883bSPaolo Bonzini void block_job_complete(BlockJob *job, Error **errp)
415aeae883bSPaolo Bonzini {
416559b935fSJohn Snow     /* Should not be reachable via external interface for internal jobs */
417559b935fSJohn Snow     assert(job->id);
4185ccac6f1SJohn Snow     if (job->pause_count || job->cancelled ||
4195ccac6f1SJohn Snow         !block_job_started(job) || !job->driver->complete) {
4209df229c3SAlberto Garcia         error_setg(errp, "The active block job '%s' cannot be completed",
4219df229c3SAlberto Garcia                    job->id);
422aeae883bSPaolo Bonzini         return;
423aeae883bSPaolo Bonzini     }
424aeae883bSPaolo Bonzini 
4253fc4b10aSFam Zheng     job->driver->complete(job, errp);
426aeae883bSPaolo Bonzini }
427aeae883bSPaolo Bonzini 
4288acc72a4SPaolo Bonzini void block_job_pause(BlockJob *job)
4292f0c9fe6SPaolo Bonzini {
430751ebd76SFam Zheng     job->pause_count++;
4318acc72a4SPaolo Bonzini }
4328acc72a4SPaolo Bonzini 
4330df4ba58SJohn Snow void block_job_user_pause(BlockJob *job)
4340df4ba58SJohn Snow {
4350df4ba58SJohn Snow     job->user_paused = true;
4360df4ba58SJohn Snow     block_job_pause(job);
4370df4ba58SJohn Snow }
4380df4ba58SJohn Snow 
439a7f3b7ffSStefan Hajnoczi static bool block_job_should_pause(BlockJob *job)
4408acc72a4SPaolo Bonzini {
441751ebd76SFam Zheng     return job->pause_count > 0;
4428acc72a4SPaolo Bonzini }
4438acc72a4SPaolo Bonzini 
4440df4ba58SJohn Snow bool block_job_user_paused(BlockJob *job)
4450df4ba58SJohn Snow {
4460df4ba58SJohn Snow     return job ? job->user_paused : 0;
4470df4ba58SJohn Snow }
4480df4ba58SJohn Snow 
449fc9c0a9cSStefan Hajnoczi void coroutine_fn block_job_pause_point(BlockJob *job)
450fc9c0a9cSStefan Hajnoczi {
4515ccac6f1SJohn Snow     assert(job && block_job_started(job));
4525ccac6f1SJohn Snow 
453fc9c0a9cSStefan Hajnoczi     if (!block_job_should_pause(job)) {
454fc9c0a9cSStefan Hajnoczi         return;
455fc9c0a9cSStefan Hajnoczi     }
456fc9c0a9cSStefan Hajnoczi     if (block_job_is_cancelled(job)) {
457fc9c0a9cSStefan Hajnoczi         return;
458fc9c0a9cSStefan Hajnoczi     }
459fc9c0a9cSStefan Hajnoczi 
460fc9c0a9cSStefan Hajnoczi     if (job->driver->pause) {
461fc9c0a9cSStefan Hajnoczi         job->driver->pause(job);
462fc9c0a9cSStefan Hajnoczi     }
463fc9c0a9cSStefan Hajnoczi 
464fc9c0a9cSStefan Hajnoczi     if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
465fc9c0a9cSStefan Hajnoczi         job->paused = true;
466fc9c0a9cSStefan Hajnoczi         job->busy = false;
467fc9c0a9cSStefan Hajnoczi         qemu_coroutine_yield(); /* wait for block_job_resume() */
468fc9c0a9cSStefan Hajnoczi         job->busy = true;
469fc9c0a9cSStefan Hajnoczi         job->paused = false;
470fc9c0a9cSStefan Hajnoczi     }
471fc9c0a9cSStefan Hajnoczi 
472fc9c0a9cSStefan Hajnoczi     if (job->driver->resume) {
473fc9c0a9cSStefan Hajnoczi         job->driver->resume(job);
474fc9c0a9cSStefan Hajnoczi     }
475fc9c0a9cSStefan Hajnoczi }
476fc9c0a9cSStefan Hajnoczi 
4778acc72a4SPaolo Bonzini void block_job_resume(BlockJob *job)
4788acc72a4SPaolo Bonzini {
479751ebd76SFam Zheng     assert(job->pause_count > 0);
480751ebd76SFam Zheng     job->pause_count--;
481751ebd76SFam Zheng     if (job->pause_count) {
482751ebd76SFam Zheng         return;
483751ebd76SFam Zheng     }
484751ebd76SFam Zheng     block_job_enter(job);
485751ebd76SFam Zheng }
486751ebd76SFam Zheng 
4870df4ba58SJohn Snow void block_job_user_resume(BlockJob *job)
4880df4ba58SJohn Snow {
4890df4ba58SJohn Snow     if (job && job->user_paused && job->pause_count > 0) {
4900df4ba58SJohn Snow         job->user_paused = false;
4910df4ba58SJohn Snow         block_job_resume(job);
4920df4ba58SJohn Snow     }
4930df4ba58SJohn Snow }
4940df4ba58SJohn Snow 
495751ebd76SFam Zheng void block_job_enter(BlockJob *job)
496751ebd76SFam Zheng {
4972f0c9fe6SPaolo Bonzini     if (job->co && !job->busy) {
4980b8b8753SPaolo Bonzini         qemu_coroutine_enter(job->co);
4992f0c9fe6SPaolo Bonzini     }
5002f0c9fe6SPaolo Bonzini }
5012f0c9fe6SPaolo Bonzini 
5028acc72a4SPaolo Bonzini void block_job_cancel(BlockJob *job)
5038acc72a4SPaolo Bonzini {
5045ccac6f1SJohn Snow     if (block_job_started(job)) {
5058acc72a4SPaolo Bonzini         job->cancelled = true;
50617bd51f9SStefan Hajnoczi         block_job_iostatus_reset(job);
507751ebd76SFam Zheng         block_job_enter(job);
5085ccac6f1SJohn Snow     } else {
5095ccac6f1SJohn Snow         block_job_completed(job, -ECANCELED);
5105ccac6f1SJohn Snow     }
5118acc72a4SPaolo Bonzini }
5128acc72a4SPaolo Bonzini 
5132f0c9fe6SPaolo Bonzini bool block_job_is_cancelled(BlockJob *job)
5142f0c9fe6SPaolo Bonzini {
5152f0c9fe6SPaolo Bonzini     return job->cancelled;
5162f0c9fe6SPaolo Bonzini }
5172f0c9fe6SPaolo Bonzini 
51832c81a4aSPaolo Bonzini void block_job_iostatus_reset(BlockJob *job)
51932c81a4aSPaolo Bonzini {
52032c81a4aSPaolo Bonzini     job->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
5213fc4b10aSFam Zheng     if (job->driver->iostatus_reset) {
5223fc4b10aSFam Zheng         job->driver->iostatus_reset(job);
5233bd293c3SPaolo Bonzini     }
52432c81a4aSPaolo Bonzini }
52532c81a4aSPaolo Bonzini 
526345f9e1bSMax Reitz static int block_job_finish_sync(BlockJob *job,
527345f9e1bSMax Reitz                                  void (*finish)(BlockJob *, Error **errp),
528345f9e1bSMax Reitz                                  Error **errp)
5292f0c9fe6SPaolo Bonzini {
530345f9e1bSMax Reitz     Error *local_err = NULL;
53194db6d2dSFam Zheng     int ret;
5322f0c9fe6SPaolo Bonzini 
533b6d2e599SKevin Wolf     assert(blk_bs(job->blk)->job == job);
5342f0c9fe6SPaolo Bonzini 
53594db6d2dSFam Zheng     block_job_ref(job);
536bae8196dSPaolo Bonzini 
537345f9e1bSMax Reitz     finish(job, &local_err);
538345f9e1bSMax Reitz     if (local_err) {
539345f9e1bSMax Reitz         error_propagate(errp, local_err);
54094db6d2dSFam Zheng         block_job_unref(job);
541345f9e1bSMax Reitz         return -EBUSY;
542345f9e1bSMax Reitz     }
543bae8196dSPaolo Bonzini     /* block_job_drain calls block_job_enter, and it should be enough to
544bae8196dSPaolo Bonzini      * induce progress until the job completes or moves to the main thread.
545bae8196dSPaolo Bonzini     */
546bae8196dSPaolo Bonzini     while (!job->deferred_to_main_loop && !job->completed) {
547bae8196dSPaolo Bonzini         block_job_drain(job);
548bae8196dSPaolo Bonzini     }
54994db6d2dSFam Zheng     while (!job->completed) {
550bae8196dSPaolo Bonzini         aio_poll(qemu_get_aio_context(), true);
5512f0c9fe6SPaolo Bonzini     }
55294db6d2dSFam Zheng     ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
55394db6d2dSFam Zheng     block_job_unref(job);
55494db6d2dSFam Zheng     return ret;
5552f0c9fe6SPaolo Bonzini }
5562f0c9fe6SPaolo Bonzini 
557345f9e1bSMax Reitz /* A wrapper around block_job_cancel() taking an Error ** parameter so it may be
558345f9e1bSMax Reitz  * used with block_job_finish_sync() without the need for (rather nasty)
559345f9e1bSMax Reitz  * function pointer casts there. */
560345f9e1bSMax Reitz static void block_job_cancel_err(BlockJob *job, Error **errp)
561345f9e1bSMax Reitz {
562345f9e1bSMax Reitz     block_job_cancel(job);
563345f9e1bSMax Reitz }
564345f9e1bSMax Reitz 
565345f9e1bSMax Reitz int block_job_cancel_sync(BlockJob *job)
566345f9e1bSMax Reitz {
567345f9e1bSMax Reitz     return block_job_finish_sync(job, &block_job_cancel_err, NULL);
568345f9e1bSMax Reitz }
569345f9e1bSMax Reitz 
570a1a2af07SKevin Wolf void block_job_cancel_sync_all(void)
571a1a2af07SKevin Wolf {
572a1a2af07SKevin Wolf     BlockJob *job;
573a1a2af07SKevin Wolf     AioContext *aio_context;
574a1a2af07SKevin Wolf 
575a1a2af07SKevin Wolf     while ((job = QLIST_FIRST(&block_jobs))) {
576b6d2e599SKevin Wolf         aio_context = blk_get_aio_context(job->blk);
577a1a2af07SKevin Wolf         aio_context_acquire(aio_context);
578a1a2af07SKevin Wolf         block_job_cancel_sync(job);
579a1a2af07SKevin Wolf         aio_context_release(aio_context);
580a1a2af07SKevin Wolf     }
581a1a2af07SKevin Wolf }
582a1a2af07SKevin Wolf 
583345f9e1bSMax Reitz int block_job_complete_sync(BlockJob *job, Error **errp)
584345f9e1bSMax Reitz {
585345f9e1bSMax Reitz     return block_job_finish_sync(job, &block_job_complete, errp);
586345f9e1bSMax Reitz }
587345f9e1bSMax Reitz 
5887483d1e5SAlex Bligh void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
5892f0c9fe6SPaolo Bonzini {
5908acc72a4SPaolo Bonzini     assert(job->busy);
5918acc72a4SPaolo Bonzini 
5922f0c9fe6SPaolo Bonzini     /* Check cancellation *before* setting busy = false, too!  */
5938acc72a4SPaolo Bonzini     if (block_job_is_cancelled(job)) {
5948acc72a4SPaolo Bonzini         return;
5952f0c9fe6SPaolo Bonzini     }
5968acc72a4SPaolo Bonzini 
5978acc72a4SPaolo Bonzini     job->busy = false;
598a7f3b7ffSStefan Hajnoczi     if (!block_job_should_pause(job)) {
5990824afdaSAlberto Garcia         co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
6000824afdaSAlberto Garcia     }
6018acc72a4SPaolo Bonzini     job->busy = true;
602fc9c0a9cSStefan Hajnoczi 
603fc9c0a9cSStefan Hajnoczi     block_job_pause_point(job);
6042f0c9fe6SPaolo Bonzini }
60530e628b7SPaolo Bonzini 
606dc71ce45SFam Zheng void block_job_yield(BlockJob *job)
607dc71ce45SFam Zheng {
608dc71ce45SFam Zheng     assert(job->busy);
609dc71ce45SFam Zheng 
610dc71ce45SFam Zheng     /* Check cancellation *before* setting busy = false, too!  */
611dc71ce45SFam Zheng     if (block_job_is_cancelled(job)) {
612dc71ce45SFam Zheng         return;
613dc71ce45SFam Zheng     }
614dc71ce45SFam Zheng 
615dc71ce45SFam Zheng     job->busy = false;
616fc9c0a9cSStefan Hajnoczi     if (!block_job_should_pause(job)) {
617dc71ce45SFam Zheng         qemu_coroutine_yield();
618fc9c0a9cSStefan Hajnoczi     }
619dc71ce45SFam Zheng     job->busy = true;
620fc9c0a9cSStefan Hajnoczi 
621fc9c0a9cSStefan Hajnoczi     block_job_pause_point(job);
622dc71ce45SFam Zheng }
623dc71ce45SFam Zheng 
624559b935fSJohn Snow BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
62530e628b7SPaolo Bonzini {
626559b935fSJohn Snow     BlockJobInfo *info;
627559b935fSJohn Snow 
628559b935fSJohn Snow     if (block_job_is_internal(job)) {
629559b935fSJohn Snow         error_setg(errp, "Cannot query QEMU internal jobs");
630559b935fSJohn Snow         return NULL;
631559b935fSJohn Snow     }
632559b935fSJohn Snow     info = g_new0(BlockJobInfo, 1);
63379e14bf7SFam Zheng     info->type      = g_strdup(BlockJobType_lookup[job->driver->job_type]);
6348ccb9569SKevin Wolf     info->device    = g_strdup(job->id);
63530e628b7SPaolo Bonzini     info->len       = job->len;
6368d65883fSPaolo Bonzini     info->busy      = job->busy;
637751ebd76SFam Zheng     info->paused    = job->pause_count > 0;
63830e628b7SPaolo Bonzini     info->offset    = job->offset;
63930e628b7SPaolo Bonzini     info->speed     = job->speed;
64032c81a4aSPaolo Bonzini     info->io_status = job->iostatus;
641ef6dbf1eSMax Reitz     info->ready     = job->ready;
64230e628b7SPaolo Bonzini     return info;
64330e628b7SPaolo Bonzini }
64432c81a4aSPaolo Bonzini 
64532c81a4aSPaolo Bonzini static void block_job_iostatus_set_err(BlockJob *job, int error)
64632c81a4aSPaolo Bonzini {
64732c81a4aSPaolo Bonzini     if (job->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
64832c81a4aSPaolo Bonzini         job->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
64932c81a4aSPaolo Bonzini                                           BLOCK_DEVICE_IO_STATUS_FAILED;
65032c81a4aSPaolo Bonzini     }
65132c81a4aSPaolo Bonzini }
65232c81a4aSPaolo Bonzini 
6538254b6d9SJohn Snow static void block_job_event_cancelled(BlockJob *job)
654a66a2a36SPaolo Bonzini {
655559b935fSJohn Snow     if (block_job_is_internal(job)) {
656559b935fSJohn Snow         return;
657559b935fSJohn Snow     }
658559b935fSJohn Snow 
659bcada37bSWenchao Xia     qapi_event_send_block_job_cancelled(job->driver->job_type,
6608ccb9569SKevin Wolf                                         job->id,
661a66a2a36SPaolo Bonzini                                         job->len,
662a66a2a36SPaolo Bonzini                                         job->offset,
663bcada37bSWenchao Xia                                         job->speed,
664bcada37bSWenchao Xia                                         &error_abort);
665a66a2a36SPaolo Bonzini }
666a66a2a36SPaolo Bonzini 
6678254b6d9SJohn Snow static void block_job_event_completed(BlockJob *job, const char *msg)
668a66a2a36SPaolo Bonzini {
669559b935fSJohn Snow     if (block_job_is_internal(job)) {
670559b935fSJohn Snow         return;
671559b935fSJohn Snow     }
672559b935fSJohn Snow 
673bcada37bSWenchao Xia     qapi_event_send_block_job_completed(job->driver->job_type,
6748ccb9569SKevin Wolf                                         job->id,
675bcada37bSWenchao Xia                                         job->len,
676bcada37bSWenchao Xia                                         job->offset,
677bcada37bSWenchao Xia                                         job->speed,
678bcada37bSWenchao Xia                                         !!msg,
679bcada37bSWenchao Xia                                         msg,
680bcada37bSWenchao Xia                                         &error_abort);
681bcada37bSWenchao Xia }
682bcada37bSWenchao Xia 
683bcada37bSWenchao Xia void block_job_event_ready(BlockJob *job)
684bcada37bSWenchao Xia {
685ef6dbf1eSMax Reitz     job->ready = true;
686ef6dbf1eSMax Reitz 
687559b935fSJohn Snow     if (block_job_is_internal(job)) {
688559b935fSJohn Snow         return;
689559b935fSJohn Snow     }
690559b935fSJohn Snow 
691518848a2SMarkus Armbruster     qapi_event_send_block_job_ready(job->driver->job_type,
6928ccb9569SKevin Wolf                                     job->id,
693518848a2SMarkus Armbruster                                     job->len,
694518848a2SMarkus Armbruster                                     job->offset,
695518848a2SMarkus Armbruster                                     job->speed, &error_abort);
696a66a2a36SPaolo Bonzini }
697a66a2a36SPaolo Bonzini 
69881e254dcSKevin Wolf BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
69932c81a4aSPaolo Bonzini                                         int is_read, int error)
70032c81a4aSPaolo Bonzini {
70132c81a4aSPaolo Bonzini     BlockErrorAction action;
70232c81a4aSPaolo Bonzini 
70332c81a4aSPaolo Bonzini     switch (on_err) {
70432c81a4aSPaolo Bonzini     case BLOCKDEV_ON_ERROR_ENOSPC:
7058c398252SKevin Wolf     case BLOCKDEV_ON_ERROR_AUTO:
706a589569fSWenchao Xia         action = (error == ENOSPC) ?
707a589569fSWenchao Xia                  BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT;
70832c81a4aSPaolo Bonzini         break;
70932c81a4aSPaolo Bonzini     case BLOCKDEV_ON_ERROR_STOP:
710a589569fSWenchao Xia         action = BLOCK_ERROR_ACTION_STOP;
71132c81a4aSPaolo Bonzini         break;
71232c81a4aSPaolo Bonzini     case BLOCKDEV_ON_ERROR_REPORT:
713a589569fSWenchao Xia         action = BLOCK_ERROR_ACTION_REPORT;
71432c81a4aSPaolo Bonzini         break;
71532c81a4aSPaolo Bonzini     case BLOCKDEV_ON_ERROR_IGNORE:
716a589569fSWenchao Xia         action = BLOCK_ERROR_ACTION_IGNORE;
71732c81a4aSPaolo Bonzini         break;
71832c81a4aSPaolo Bonzini     default:
71932c81a4aSPaolo Bonzini         abort();
72032c81a4aSPaolo Bonzini     }
721559b935fSJohn Snow     if (!block_job_is_internal(job)) {
7228ccb9569SKevin Wolf         qapi_event_send_block_job_error(job->id,
7235a2d2cbdSWenchao Xia                                         is_read ? IO_OPERATION_TYPE_READ :
7245a2d2cbdSWenchao Xia                                         IO_OPERATION_TYPE_WRITE,
7255a2d2cbdSWenchao Xia                                         action, &error_abort);
726559b935fSJohn Snow     }
727a589569fSWenchao Xia     if (action == BLOCK_ERROR_ACTION_STOP) {
728751ebd76SFam Zheng         /* make the pause user visible, which will be resumed from QMP. */
7290df4ba58SJohn Snow         block_job_user_pause(job);
73032c81a4aSPaolo Bonzini         block_job_iostatus_set_err(job, error);
73132c81a4aSPaolo Bonzini     }
73232c81a4aSPaolo Bonzini     return action;
73332c81a4aSPaolo Bonzini }
734dec7d421SStefan Hajnoczi 
735dec7d421SStefan Hajnoczi typedef struct {
736dec7d421SStefan Hajnoczi     BlockJob *job;
737dec7d421SStefan Hajnoczi     AioContext *aio_context;
738dec7d421SStefan Hajnoczi     BlockJobDeferToMainLoopFn *fn;
739dec7d421SStefan Hajnoczi     void *opaque;
740dec7d421SStefan Hajnoczi } BlockJobDeferToMainLoopData;
741dec7d421SStefan Hajnoczi 
742dec7d421SStefan Hajnoczi static void block_job_defer_to_main_loop_bh(void *opaque)
743dec7d421SStefan Hajnoczi {
744dec7d421SStefan Hajnoczi     BlockJobDeferToMainLoopData *data = opaque;
745dec7d421SStefan Hajnoczi     AioContext *aio_context;
746dec7d421SStefan Hajnoczi 
747dec7d421SStefan Hajnoczi     /* Prevent race with block_job_defer_to_main_loop() */
748dec7d421SStefan Hajnoczi     aio_context_acquire(data->aio_context);
749dec7d421SStefan Hajnoczi 
750dec7d421SStefan Hajnoczi     /* Fetch BDS AioContext again, in case it has changed */
751b6d2e599SKevin Wolf     aio_context = blk_get_aio_context(data->job->blk);
752dec7d421SStefan Hajnoczi     aio_context_acquire(aio_context);
753dec7d421SStefan Hajnoczi 
754794f0141SFam Zheng     data->job->deferred_to_main_loop = false;
755dec7d421SStefan Hajnoczi     data->fn(data->job, data->opaque);
756dec7d421SStefan Hajnoczi 
757dec7d421SStefan Hajnoczi     aio_context_release(aio_context);
758dec7d421SStefan Hajnoczi 
759dec7d421SStefan Hajnoczi     aio_context_release(data->aio_context);
760dec7d421SStefan Hajnoczi 
761dec7d421SStefan Hajnoczi     g_free(data);
762dec7d421SStefan Hajnoczi }
763dec7d421SStefan Hajnoczi 
764dec7d421SStefan Hajnoczi void block_job_defer_to_main_loop(BlockJob *job,
765dec7d421SStefan Hajnoczi                                   BlockJobDeferToMainLoopFn *fn,
766dec7d421SStefan Hajnoczi                                   void *opaque)
767dec7d421SStefan Hajnoczi {
768dec7d421SStefan Hajnoczi     BlockJobDeferToMainLoopData *data = g_malloc(sizeof(*data));
769dec7d421SStefan Hajnoczi     data->job = job;
770b6d2e599SKevin Wolf     data->aio_context = blk_get_aio_context(job->blk);
771dec7d421SStefan Hajnoczi     data->fn = fn;
772dec7d421SStefan Hajnoczi     data->opaque = opaque;
773794f0141SFam Zheng     job->deferred_to_main_loop = true;
774dec7d421SStefan Hajnoczi 
775fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(qemu_get_aio_context(),
776fffb6e12SPaolo Bonzini                             block_job_defer_to_main_loop_bh, data);
777dec7d421SStefan Hajnoczi }
778c55a832fSFam Zheng 
779c55a832fSFam Zheng BlockJobTxn *block_job_txn_new(void)
780c55a832fSFam Zheng {
781c55a832fSFam Zheng     BlockJobTxn *txn = g_new0(BlockJobTxn, 1);
782c55a832fSFam Zheng     QLIST_INIT(&txn->jobs);
783c55a832fSFam Zheng     txn->refcnt = 1;
784c55a832fSFam Zheng     return txn;
785c55a832fSFam Zheng }
786c55a832fSFam Zheng 
787c55a832fSFam Zheng static void block_job_txn_ref(BlockJobTxn *txn)
788c55a832fSFam Zheng {
789c55a832fSFam Zheng     txn->refcnt++;
790c55a832fSFam Zheng }
791c55a832fSFam Zheng 
792c55a832fSFam Zheng void block_job_txn_unref(BlockJobTxn *txn)
793c55a832fSFam Zheng {
794c55a832fSFam Zheng     if (txn && --txn->refcnt == 0) {
795c55a832fSFam Zheng         g_free(txn);
796c55a832fSFam Zheng     }
797c55a832fSFam Zheng }
798c55a832fSFam Zheng 
799c55a832fSFam Zheng void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job)
800c55a832fSFam Zheng {
801c55a832fSFam Zheng     if (!txn) {
802c55a832fSFam Zheng         return;
803c55a832fSFam Zheng     }
804c55a832fSFam Zheng 
805c55a832fSFam Zheng     assert(!job->txn);
806c55a832fSFam Zheng     job->txn = txn;
807c55a832fSFam Zheng 
808c55a832fSFam Zheng     QLIST_INSERT_HEAD(&txn->jobs, job, txn_list);
809c55a832fSFam Zheng     block_job_txn_ref(txn);
810c55a832fSFam Zheng }
811