This moves reference counting from BlockJob to Job.
In order to keep calling the BlockJob cleanup code when the job is
deleted via job_unref(), introduce a new JobDriver.free callback. Every
block job must use block_job_free() for this callback, this is asserted
in block_job_create().
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
.job_driver = {
.instance_size = sizeof(BackupBlockJob),
.job_type = JOB_TYPE_BACKUP,
+ .free = block_job_free,
},
.start = backup_run,
.commit = backup_commit,
.job_driver = {
.instance_size = sizeof(CommitBlockJob),
.job_type = JOB_TYPE_COMMIT,
+ .free = block_job_free,
},
.start = commit_run,
};
.job_driver = {
.instance_size = sizeof(MirrorBlockJob),
.job_type = JOB_TYPE_MIRROR,
+ .free = block_job_free,
},
.start = mirror_run,
.complete = mirror_complete,
.job_driver = {
.instance_size = sizeof(MirrorBlockJob),
.job_type = JOB_TYPE_COMMIT,
+ .free = block_job_free,
},
.start = mirror_run,
.complete = mirror_complete,
.job_driver = {
.instance_size = sizeof(StreamBlockJob),
.job_type = JOB_TYPE_STREAM,
+ .free = block_job_free,
},
.start = stream_run,
};
block_job_enter_cond(job, block_job_timer_not_pending);
}
-void block_job_ref(BlockJob *job)
-{
- ++job->refcnt;
-}
-
static void block_job_attached_aio_context(AioContext *new_context,
void *opaque);
static void block_job_detach_aio_context(void *opaque);
-void block_job_unref(BlockJob *job)
+void block_job_free(Job *job)
{
- if (--job->refcnt == 0) {
- assert(job->job.status == JOB_STATUS_NULL);
- assert(!job->txn);
- BlockDriverState *bs = blk_bs(job->blk);
- bs->job = NULL;
- block_job_remove_all_bdrv(job);
- blk_remove_aio_context_notifier(job->blk,
- block_job_attached_aio_context,
- block_job_detach_aio_context, job);
- blk_unref(job->blk);
- error_free(job->blocker);
- assert(!timer_pending(&job->sleep_timer));
- job_delete(&job->job);
- }
+ BlockJob *bjob = container_of(job, BlockJob, job);
+ BlockDriverState *bs = blk_bs(bjob->blk);
+
+ assert(!bjob->txn);
+
+ bs->job = NULL;
+ block_job_remove_all_bdrv(bjob);
+ blk_remove_aio_context_notifier(bjob->blk,
+ block_job_attached_aio_context,
+ block_job_detach_aio_context, bjob);
+ blk_unref(bjob->blk);
+ error_free(bjob->blocker);
+ assert(!timer_pending(&bjob->sleep_timer));
}
static void block_job_attached_aio_context(AioContext *new_context,
BlockJob *job = opaque;
/* In case the job terminates during aio_poll()... */
- block_job_ref(job);
+ job_ref(&job->job);
block_job_pause(job);
block_job_drain(job);
}
- block_job_unref(job);
+ job_unref(&job->job);
}
static char *child_job_get_parent_desc(BdrvChild *c)
job->deferred_to_main_loop = true;
block_job_txn_del_job(job);
job_state_transition(&job->job, JOB_STATUS_NULL);
- block_job_unref(job);
+ job_unref(&job->job);
}
static void block_job_do_dismiss(BlockJob *job)
assert(blk_bs(job->blk)->job == job);
- block_job_ref(job);
+ job_ref(&job->job);
if (finish) {
finish(job, &local_err);
}
if (local_err) {
error_propagate(errp, local_err);
- block_job_unref(job);
+ job_unref(&job->job);
return -EBUSY;
}
/* block_job_drain calls block_job_enter, and it should be enough to
aio_poll(qemu_get_aio_context(), true);
}
ret = (job->cancelled && job->ret == 0) ? -ECANCELED : job->ret;
- block_job_unref(job);
+ job_unref(&job->job);
return ret;
}
}
assert(is_block_job(&job->job));
+ assert(job->job.driver->free == &block_job_free);
job->driver = driver;
job->blk = blk;
job->busy = false;
job->paused = true;
job->pause_count = 1;
- job->refcnt = 1;
job->auto_finalize = !(flags & BLOCK_JOB_MANUAL_FINALIZE);
job->auto_dismiss = !(flags & BLOCK_JOB_MANUAL_DISMISS);
aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
/** The opaque value that is passed to the completion function. */
void *opaque;
- /** Reference count of the block job */
- int refcnt;
-
/** True when job has reported completion by calling block_job_completed. */
bool completed;
*/
BlockJobTxn *block_job_txn_new(void);
-/**
- * block_job_ref:
- *
- * Add a reference to BlockJob refcnt, it will be decreased with
- * block_job_unref, and then be freed if it comes to be the last
- * reference.
- */
-void block_job_ref(BlockJob *job);
-
-/**
- * block_job_unref:
- *
- * Release a reference that was previously acquired with block_job_ref
- * or block_job_create. If it's the last reference to the object, it will be
- * freed.
- */
-void block_job_unref(BlockJob *job);
-
/**
* block_job_txn_unref:
*
uint64_t shared_perm, int64_t speed, int flags,
BlockCompletionFunc *cb, void *opaque, Error **errp);
+/**
+ * block_job_free:
+ * Callback to be used for JobDriver.free in all block jobs. Frees block job
+ * specific resources in @job.
+ */
+void block_job_free(Job *job);
+
/**
* block_job_sleep_ns:
* @job: The job that calls the function.
/** The type of this job. */
const JobDriver *driver;
+ /** Reference count of the block job */
+ int refcnt;
+
/** Current state; See @JobStatus for details. */
JobStatus status;
/** Enum describing the operation */
JobType job_type;
+
+ /** Called when the job is freed */
+ void (*free)(Job *job);
};
*/
void *job_create(const char *job_id, const JobDriver *driver, Error **errp);
-/** Frees the @job object. */
-void job_delete(Job *job);
+/**
+ * Add a reference to Job refcnt, it will be decreased with job_unref, and then
+ * be freed if it comes to be the last reference.
+ */
+void job_ref(Job *job);
+
+/**
+ * Release a reference that was previously acquired with job_ref() or
+ * job_create(). If it's the last reference to the object, it will be freed.
+ */
+void job_unref(Job *job);
/** Returns the JobType of a given Job. */
JobType job_type(const Job *job);
job = g_malloc0(driver->instance_size);
job->driver = driver;
job->id = g_strdup(job_id);
+ job->refcnt = 1;
job_state_transition(job, JOB_STATUS_CREATED);
return job;
}
-void job_delete(Job *job)
+void job_ref(Job *job)
{
- QLIST_REMOVE(job, job_list);
+ ++job->refcnt;
+}
+
+void job_unref(Job *job)
+{
+ if (--job->refcnt == 0) {
+ assert(job->status == JOB_STATUS_NULL);
- g_free(job->id);
- g_free(job);
+ if (job->driver->free) {
+ job->driver->free(job);
+ }
+
+ QLIST_REMOVE(job, job_list);
+
+ g_free(job->id);
+ g_free(job);
+ }
}
int ret = 0;
aio_context_acquire(aio_context);
- block_job_ref(job);
+ job_ref(&job->job);
do {
aio_poll(aio_context, true);
qemu_progress_print(job->len ?
} else {
ret = job->ret;
}
- block_job_unref(job);
+ job_unref(&job->job);
aio_context_release(aio_context);
/* publish completion progress only when success */
BlockJobDriver test_job_driver = {
.job_driver = {
.instance_size = sizeof(TestBlockJob),
+ .free = block_job_free,
},
.start = test_job_start,
.complete = test_job_complete,
static const BlockJobDriver test_block_job_driver = {
.job_driver = {
.instance_size = sizeof(TestBlockJob),
+ .free = block_job_free,
},
.start = test_block_job_run,
};
static const BlockJobDriver test_block_job_driver = {
.job_driver = {
.instance_size = sizeof(BlockJob),
+ .free = block_job_free,
},
};
static const BlockJobDriver test_cancel_driver = {
.job_driver = {
.instance_size = sizeof(CancelJob),
+ .free = block_job_free,
},
.start = cancel_job_start,
.complete = cancel_job_complete,
blk = create_blk(NULL);
job = mk_job(blk, "Steve", &test_cancel_driver, true,
BLOCK_JOB_MANUAL_FINALIZE | BLOCK_JOB_MANUAL_DISMISS);
- block_job_ref(job);
+ job_ref(&job->job);
assert(job->job.status == JOB_STATUS_CREATED);
s = container_of(job, CancelJob, common);
s->blk = blk;
block_job_dismiss(&dummy, &error_abort);
}
assert(job->job.status == JOB_STATUS_NULL);
- block_job_unref(job);
+ job_unref(&job->job);
destroy_blk(blk);
}