Outside blockjob.c, block_job_unref is only used when a block job fails
to start, and block_job_ref is not used at all. The reference counting
thus is pretty well hidden. Introduce a separate function to be used
by block jobs; because block_job_ref and block_job_unref now become
static, move them earlier in blockjob.c.
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Jeff Cody <jcody@redhat.com>
Message-id:
20170508141310.8674-4-pbonzini@redhat.com
Signed-off-by: Jeff Cody <jcody@redhat.com>
}
if (job) {
backup_clean(&job->common);
- block_job_unref(&job->common);
+ block_job_early_fail(&job->common);
}
return NULL;
if (commit_top_bs) {
bdrv_set_backing_hd(overlay_bs, top, &error_abort);
}
- block_job_unref(&s->common);
+ block_job_early_fail(&s->common);
}
g_free(s->replaces);
blk_unref(s->target);
- block_job_unref(&s->common);
+ block_job_early_fail(&s->common);
}
bdrv_child_try_set_perm(mirror_top_bs->backing, 0, BLK_PERM_ALL,
return NULL;
}
+static 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);
+
+static void block_job_unref(BlockJob *job)
+{
+ if (--job->refcnt == 0) {
+ 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);
+ g_free(job->id);
+ QLIST_REMOVE(job, job_list);
+ g_free(job);
+ }
+}
+
static void block_job_attached_aio_context(AioContext *new_context,
void *opaque)
{
bdrv_coroutine_enter(blk_bs(job->blk), job->co);
}
-void block_job_ref(BlockJob *job)
-{
- ++job->refcnt;
-}
-
-void block_job_unref(BlockJob *job)
+void block_job_early_fail(BlockJob *job)
{
- if (--job->refcnt == 0) {
- 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);
- g_free(job->id);
- QLIST_REMOVE(job, job_list);
- g_free(job);
- }
+ block_job_unref(job);
}
static void block_job_completed_single(BlockJob *job)
void block_job_yield(BlockJob *job);
/**
- * block_job_ref:
+ * block_job_early_fail:
* @bs: The block device.
*
- * Grab a reference to the block job. Should be paired with block_job_unref.
+ * The block job could not be started, free it.
*/
-void block_job_ref(BlockJob *job);
-
-/**
- * block_job_unref:
- * @bs: The block device.
- *
- * Release reference to the block job and release resources if it is the last
- * reference.
- */
-void block_job_unref(BlockJob *job);
+void block_job_early_fail(BlockJob *job);
/**
* block_job_completed:
job[1] = do_test_id(blk[1], "id0", false);
/* But once job[0] finishes we can reuse its ID */
- block_job_unref(job[0]);
+ block_job_early_fail(job[0]);
job[1] = do_test_id(blk[1], "id0", true);
/* No job ID specified, defaults to the backend name ('drive1') */
- block_job_unref(job[1]);
+ block_job_early_fail(job[1]);
job[1] = do_test_id(blk[1], NULL, true);
/* Duplicate job ID */
/* This one is valid */
job[2] = do_test_id(blk[2], "id_2", true);
- block_job_unref(job[0]);
- block_job_unref(job[1]);
- block_job_unref(job[2]);
+ block_job_early_fail(job[0]);
+ block_job_early_fail(job[1]);
+ block_job_early_fail(job[2]);
destroy_blk(blk[0]);
destroy_blk(blk[1]);