}
bool job_is_cancelled(Job *job)
+{
+ /* force_cancel may be true only if cancelled is true, too */
+ assert(job->cancelled || !job->force_cancel);
+ return job->force_cancel;
+}
+
+bool job_cancel_requested(Job *job)
{
return job->cancelled;
}
job->cb = cb;
job->opaque = opaque;
+ progress_init(&job->progress);
+
notifier_list_init(&job->on_finalize_cancelled);
notifier_list_init(&job->on_finalize_completed);
notifier_list_init(&job->on_pending);
notifier_list_init(&job->on_ready);
+ notifier_list_init(&job->on_idle);
job_state_transition(job, JOB_STATUS_CREATED);
aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
QLIST_REMOVE(job, job_list);
+ progress_destroy(&job->progress);
error_free(job->err);
g_free(job->id);
g_free(job);
static void job_cancel_async(Job *job, bool force)
{
if (job->driver->cancel) {
- job->driver->cancel(job, force);
+ force = job->driver->cancel(job, force);
+ } else {
+ /* No .cancel() means the job will behave as if force-cancelled */
+ force = true;
}
+
if (job->user_paused) {
/* Do not call job_enter here, the caller will handle it. */
if (job->driver->user_resume) {
assert(job->pause_count > 0);
job->pause_count--;
}
- job->cancelled = true;
- /* To prevent 'force == false' overriding a previous 'force == true' */
- job->force_cancel |= force;
+
+ /*
+ * Ignore soft cancel requests after the job is already done
+ * (We will still invoke job->driver->cancel() above, but if the
+ * job driver supports soft cancelling and the job is done, that
+ * should be a no-op, too. We still call it so it can override
+ * @force.)
+ */
+ if (force || !job->deferred_to_main_loop) {
+ job->cancelled = true;
+ /* To prevent 'force == false' overriding a previous 'force == true' */
+ job->force_cancel |= force;
+ }
}
static void job_completed_txn_abort(Job *job)
{
- AioContext *outer_ctx = job->aio_context;
AioContext *ctx;
JobTxn *txn = job->txn;
Job *other_job;
txn->aborting = true;
job_txn_ref(txn);
- /* We can only hold the single job's AioContext lock while calling
+ /*
+ * We can only hold the single job's AioContext lock while calling
* job_finalize_single() because the finalization callbacks can involve
- * calls of AIO_WAIT_WHILE(), which could deadlock otherwise. */
- aio_context_release(outer_ctx);
+ * calls of AIO_WAIT_WHILE(), which could deadlock otherwise.
+ * Note that the job's AioContext may change when it is finalized.
+ */
+ job_ref(job);
+ aio_context_release(job->aio_context);
/* Other jobs are effectively cancelled by us, set the status for
* them; this job, however, may or may not be cancelled, depending
if (other_job != job) {
ctx = other_job->aio_context;
aio_context_acquire(ctx);
- job_cancel_async(other_job, false);
+ /*
+ * This is a transaction: If one job failed, no result will matter.
+ * Therefore, pass force=true to terminate all other jobs as quickly
+ * as possible.
+ */
+ job_cancel_async(other_job, true);
aio_context_release(ctx);
}
}
while (!QLIST_EMPTY(&txn->jobs)) {
other_job = QLIST_FIRST(&txn->jobs);
+ /*
+ * The job's AioContext may change, so store it in @ctx so we
+ * release the same context that we have acquired before.
+ */
ctx = other_job->aio_context;
aio_context_acquire(ctx);
if (!job_is_completed(other_job)) {
- assert(job_is_cancelled(other_job));
+ assert(job_cancel_requested(other_job));
job_finish_sync(other_job, NULL, NULL);
}
job_finalize_single(other_job);
aio_context_release(ctx);
}
- aio_context_acquire(outer_ctx);
+ /*
+ * Use job_ref()/job_unref() so we can read the AioContext here
+ * even if the job went away during job_finalize_single().
+ */
+ aio_context_acquire(job->aio_context);
+ job_unref(job);
job_txn_unref(txn);
}
if (!job_started(job)) {
job_completed(job);
} else if (job->deferred_to_main_loop) {
- job_completed_txn_abort(job);
+ /*
+ * job_cancel_async() ignores soft-cancel requests for jobs
+ * that are already done (i.e. deferred to the main loop). We
+ * have to check again whether the job is really cancelled.
+ * (job_cancel_requested() and job_is_cancelled() are equivalent
+ * here, because job_cancel_async() will make soft-cancel
+ * requests no-ops when deferred_to_main_loop is true. We
+ * choose to call job_is_cancelled() to show that we invoke
+ * job_completed_txn_abort() only for force-cancelled jobs.)
+ */
+ if (job_is_cancelled(job)) {
+ job_completed_txn_abort(job);
+ }
} else {
job_enter(job);
}
job_cancel(job, false);
}
-int job_cancel_sync(Job *job)
+/**
+ * Same as job_cancel_err(), but force-cancel.
+ */
+static void job_force_cancel_err(Job *job, Error **errp)
+{
+ job_cancel(job, true);
+}
+
+int job_cancel_sync(Job *job, bool force)
{
- return job_finish_sync(job, &job_cancel_err, NULL);
+ if (force) {
+ return job_finish_sync(job, &job_force_cancel_err, NULL);
+ } else {
+ return job_finish_sync(job, &job_cancel_err, NULL);
+ }
}
void job_cancel_sync_all(void)
while ((job = job_next(NULL))) {
aio_context = job->aio_context;
aio_context_acquire(aio_context);
- job_cancel_sync(job);
+ job_cancel_sync(job, true);
aio_context_release(aio_context);
}
}
if (job_apply_verb(job, JOB_VERB_COMPLETE, errp)) {
return;
}
- if (job_is_cancelled(job) || !job->driver->complete) {
+ if (job_cancel_requested(job) || !job->driver->complete) {
error_setg(errp, "The active block job '%s' cannot be completed",
job->id);
return;