2016-07-29 22:31:41 +08:00
|
|
|
/*
|
|
|
|
* Blockjob tests
|
|
|
|
*
|
|
|
|
* Copyright Igalia, S.L. 2016
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alberto Garcia <berto@igalia.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qemu/main-loop.h"
|
2016-10-28 00:07:00 +08:00
|
|
|
#include "block/blockjob_int.h"
|
2016-07-29 22:31:41 +08:00
|
|
|
#include "sysemu/block-backend.h"
|
|
|
|
|
|
|
|
static const BlockJobDriver test_block_job_driver = {
|
2018-04-12 23:29:59 +08:00
|
|
|
.job_driver = {
|
|
|
|
.instance_size = sizeof(BlockJob),
|
2018-04-14 00:50:05 +08:00
|
|
|
.free = block_job_free,
|
2018-04-18 23:10:26 +08:00
|
|
|
.user_resume = block_job_user_resume,
|
2018-04-20 23:00:29 +08:00
|
|
|
.drain = block_job_drain,
|
2018-04-12 23:29:59 +08:00
|
|
|
},
|
2016-07-29 22:31:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void block_job_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:27:46 +08:00
|
|
|
static BlockJob *mk_job(BlockBackend *blk, const char *id,
|
|
|
|
const BlockJobDriver *drv, bool should_succeed,
|
|
|
|
int flags)
|
2016-07-29 22:31:41 +08:00
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
Error *errp = NULL;
|
|
|
|
|
2018-03-10 16:27:46 +08:00
|
|
|
job = block_job_create(id, drv, NULL, blk_bs(blk),
|
|
|
|
0, BLK_PERM_ALL, 0, flags, block_job_cb,
|
2017-01-17 00:18:09 +08:00
|
|
|
NULL, &errp);
|
2016-07-29 22:31:41 +08:00
|
|
|
if (should_succeed) {
|
|
|
|
g_assert_null(errp);
|
|
|
|
g_assert_nonnull(job);
|
|
|
|
if (id) {
|
2018-04-12 23:29:59 +08:00
|
|
|
g_assert_cmpstr(job->job.id, ==, id);
|
2016-07-29 22:31:41 +08:00
|
|
|
} else {
|
2018-04-12 23:29:59 +08:00
|
|
|
g_assert_cmpstr(job->job.id, ==, blk_name(blk));
|
2016-07-29 22:31:41 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_assert_nonnull(errp);
|
|
|
|
g_assert_null(job);
|
|
|
|
error_free(errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:27:46 +08:00
|
|
|
static BlockJob *do_test_id(BlockBackend *blk, const char *id,
|
|
|
|
bool should_succeed)
|
|
|
|
{
|
|
|
|
return mk_job(blk, id, &test_block_job_driver,
|
2018-04-19 23:54:56 +08:00
|
|
|
should_succeed, JOB_DEFAULT);
|
2018-03-10 16:27:46 +08:00
|
|
|
}
|
|
|
|
|
2016-07-29 22:31:41 +08:00
|
|
|
/* This creates a BlockBackend (optionally with a name) with a
|
|
|
|
* BlockDriverState inserted. */
|
|
|
|
static BlockBackend *create_blk(const char *name)
|
|
|
|
{
|
2017-02-09 22:48:04 +08:00
|
|
|
/* No I/O is performed on this device */
|
2017-01-21 00:07:26 +08:00
|
|
|
BlockBackend *blk = blk_new(0, BLK_PERM_ALL);
|
2017-01-17 00:17:38 +08:00
|
|
|
BlockDriverState *bs;
|
|
|
|
|
|
|
|
bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
|
|
|
|
g_assert_nonnull(bs);
|
2016-07-29 22:31:41 +08:00
|
|
|
|
2017-01-14 02:02:32 +08:00
|
|
|
blk_insert_bs(blk, bs, &error_abort);
|
2016-07-29 22:31:41 +08:00
|
|
|
bdrv_unref(bs);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
Error *errp = NULL;
|
|
|
|
monitor_add_blk(blk, name, &errp);
|
|
|
|
g_assert_null(errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This destroys the backend */
|
|
|
|
static void destroy_blk(BlockBackend *blk)
|
|
|
|
{
|
|
|
|
if (blk_name(blk)[0] != '\0') {
|
|
|
|
monitor_remove_blk(blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
blk_remove_bs(blk);
|
|
|
|
blk_unref(blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_job_ids(void)
|
|
|
|
{
|
|
|
|
BlockBackend *blk[3];
|
|
|
|
BlockJob *job[3];
|
|
|
|
|
|
|
|
blk[0] = create_blk(NULL);
|
|
|
|
blk[1] = create_blk("drive1");
|
|
|
|
blk[2] = create_blk("drive2");
|
|
|
|
|
|
|
|
/* No job ID provided and the block backend has no name */
|
|
|
|
job[0] = do_test_id(blk[0], NULL, false);
|
|
|
|
|
|
|
|
/* These are all invalid job IDs */
|
|
|
|
job[0] = do_test_id(blk[0], "0id", false);
|
|
|
|
job[0] = do_test_id(blk[0], "", false);
|
|
|
|
job[0] = do_test_id(blk[0], " ", false);
|
|
|
|
job[0] = do_test_id(blk[0], "123", false);
|
|
|
|
job[0] = do_test_id(blk[0], "_id", false);
|
|
|
|
job[0] = do_test_id(blk[0], "-id", false);
|
|
|
|
job[0] = do_test_id(blk[0], ".id", false);
|
|
|
|
job[0] = do_test_id(blk[0], "#id", false);
|
|
|
|
|
|
|
|
/* This one is valid */
|
|
|
|
job[0] = do_test_id(blk[0], "id0", true);
|
|
|
|
|
|
|
|
/* We cannot have two jobs in the same BDS */
|
|
|
|
do_test_id(blk[0], "id1", false);
|
|
|
|
|
|
|
|
/* Duplicate job IDs are not allowed */
|
|
|
|
job[1] = do_test_id(blk[1], "id0", false);
|
|
|
|
|
|
|
|
/* But once job[0] finishes we can reuse its ID */
|
2018-04-19 23:30:16 +08:00
|
|
|
job_early_fail(&job[0]->job);
|
2016-07-29 22:31:41 +08:00
|
|
|
job[1] = do_test_id(blk[1], "id0", true);
|
|
|
|
|
|
|
|
/* No job ID specified, defaults to the backend name ('drive1') */
|
2018-04-19 23:30:16 +08:00
|
|
|
job_early_fail(&job[1]->job);
|
2016-07-29 22:31:41 +08:00
|
|
|
job[1] = do_test_id(blk[1], NULL, true);
|
|
|
|
|
|
|
|
/* Duplicate job ID */
|
|
|
|
job[2] = do_test_id(blk[2], "drive1", false);
|
|
|
|
|
|
|
|
/* The ID of job[2] would default to 'drive2' but it is already in use */
|
|
|
|
job[0] = do_test_id(blk[0], "drive2", true);
|
|
|
|
job[2] = do_test_id(blk[2], NULL, false);
|
|
|
|
|
|
|
|
/* This one is valid */
|
|
|
|
job[2] = do_test_id(blk[2], "id_2", true);
|
|
|
|
|
2018-04-19 23:30:16 +08:00
|
|
|
job_early_fail(&job[0]->job);
|
|
|
|
job_early_fail(&job[1]->job);
|
|
|
|
job_early_fail(&job[2]->job);
|
2016-07-29 22:31:41 +08:00
|
|
|
|
|
|
|
destroy_blk(blk[0]);
|
|
|
|
destroy_blk(blk[1]);
|
|
|
|
destroy_blk(blk[2]);
|
|
|
|
}
|
|
|
|
|
2018-03-10 16:27:46 +08:00
|
|
|
typedef struct CancelJob {
|
|
|
|
BlockJob common;
|
|
|
|
BlockBackend *blk;
|
|
|
|
bool should_converge;
|
|
|
|
bool should_complete;
|
|
|
|
bool completed;
|
|
|
|
} CancelJob;
|
|
|
|
|
2018-04-17 22:41:17 +08:00
|
|
|
static void cancel_job_completed(Job *job, void *opaque)
|
2018-03-10 16:27:46 +08:00
|
|
|
{
|
|
|
|
CancelJob *s = opaque;
|
|
|
|
s->completed = true;
|
2018-05-24 21:26:10 +08:00
|
|
|
job_completed(job, 0, NULL);
|
2018-03-10 16:27:46 +08:00
|
|
|
}
|
|
|
|
|
2018-04-23 18:24:16 +08:00
|
|
|
static void cancel_job_complete(Job *job, Error **errp)
|
2018-03-10 16:27:46 +08:00
|
|
|
{
|
2018-04-23 18:24:16 +08:00
|
|
|
CancelJob *s = container_of(job, CancelJob, common.job);
|
2018-03-10 16:27:46 +08:00
|
|
|
s->should_complete = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void coroutine_fn cancel_job_start(void *opaque)
|
|
|
|
{
|
|
|
|
CancelJob *s = opaque;
|
|
|
|
|
|
|
|
while (!s->should_complete) {
|
2018-04-17 18:56:07 +08:00
|
|
|
if (job_is_cancelled(&s->common.job)) {
|
2018-03-10 16:27:46 +08:00
|
|
|
goto defer;
|
|
|
|
}
|
|
|
|
|
2018-04-25 21:09:58 +08:00
|
|
|
if (!job_is_ready(&s->common.job) && s->should_converge) {
|
2018-04-25 20:56:09 +08:00
|
|
|
job_transition_to_ready(&s->common.job);
|
2018-03-10 16:27:46 +08:00
|
|
|
}
|
|
|
|
|
2018-04-18 22:32:20 +08:00
|
|
|
job_sleep_ns(&s->common.job, 100000);
|
2018-03-10 16:27:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer:
|
2018-04-17 22:41:17 +08:00
|
|
|
job_defer_to_main_loop(&s->common.job, cancel_job_completed, s);
|
2018-03-10 16:27:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const BlockJobDriver test_cancel_driver = {
|
2018-04-12 23:29:59 +08:00
|
|
|
.job_driver = {
|
|
|
|
.instance_size = sizeof(CancelJob),
|
2018-04-14 00:50:05 +08:00
|
|
|
.free = block_job_free,
|
2018-04-18 23:10:26 +08:00
|
|
|
.user_resume = block_job_user_resume,
|
2018-04-20 23:00:29 +08:00
|
|
|
.drain = block_job_drain,
|
2018-04-13 23:31:02 +08:00
|
|
|
.start = cancel_job_start,
|
2018-04-23 18:24:16 +08:00
|
|
|
.complete = cancel_job_complete,
|
2018-04-12 23:29:59 +08:00
|
|
|
},
|
2018-03-10 16:27:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static CancelJob *create_common(BlockJob **pjob)
|
|
|
|
{
|
|
|
|
BlockBackend *blk;
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
blk = create_blk(NULL);
|
|
|
|
job = mk_job(blk, "Steve", &test_cancel_driver, true,
|
2018-04-19 23:54:56 +08:00
|
|
|
JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
|
2018-04-14 00:50:05 +08:00
|
|
|
job_ref(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_CREATED);
|
2018-03-10 16:27:46 +08:00
|
|
|
s = container_of(job, CancelJob, common);
|
|
|
|
s->blk = blk;
|
|
|
|
|
|
|
|
*pjob = job;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cancel_common(CancelJob *s)
|
|
|
|
{
|
|
|
|
BlockJob *job = &s->common;
|
|
|
|
BlockBackend *blk = s->blk;
|
2018-04-13 23:19:31 +08:00
|
|
|
JobStatus sts = job->job.status;
|
2018-03-10 16:27:46 +08:00
|
|
|
|
2018-04-24 22:13:52 +08:00
|
|
|
job_cancel_sync(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
|
2018-04-24 23:10:12 +08:00
|
|
|
Job *dummy = &job->job;
|
|
|
|
job_dismiss(&dummy, &error_abort);
|
2018-03-10 16:27:46 +08:00
|
|
|
}
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_NULL);
|
2018-04-14 00:50:05 +08:00
|
|
|
job_unref(&job->job);
|
2018-03-10 16:27:46 +08:00
|
|
|
destroy_blk(blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_created(void)
|
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
s = create_common(&job);
|
|
|
|
cancel_common(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_running(void)
|
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
s = create_common(&job);
|
|
|
|
|
2018-04-13 23:31:02 +08:00
|
|
|
job_start(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_RUNNING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
cancel_common(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_paused(void)
|
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
s = create_common(&job);
|
|
|
|
|
2018-04-13 23:31:02 +08:00
|
|
|
job_start(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_RUNNING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
2018-04-18 23:10:26 +08:00
|
|
|
job_user_pause(&job->job, &error_abort);
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_PAUSED);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
cancel_common(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_ready(void)
|
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
s = create_common(&job);
|
|
|
|
|
2018-04-13 23:31:02 +08:00
|
|
|
job_start(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_RUNNING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
s->should_converge = true;
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_READY);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
cancel_common(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_standby(void)
|
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
s = create_common(&job);
|
|
|
|
|
2018-04-13 23:31:02 +08:00
|
|
|
job_start(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_RUNNING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
s->should_converge = true;
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_READY);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
2018-04-18 23:10:26 +08:00
|
|
|
job_user_pause(&job->job, &error_abort);
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_STANDBY);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
cancel_common(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_pending(void)
|
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
s = create_common(&job);
|
|
|
|
|
2018-04-13 23:31:02 +08:00
|
|
|
job_start(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_RUNNING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
s->should_converge = true;
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_READY);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
2018-04-23 18:24:16 +08:00
|
|
|
job_complete(&job->job, &error_abort);
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-03-10 16:27:46 +08:00
|
|
|
while (!s->completed) {
|
|
|
|
aio_poll(qemu_get_aio_context(), true);
|
|
|
|
}
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_PENDING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
cancel_common(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_cancel_concluded(void)
|
|
|
|
{
|
|
|
|
BlockJob *job;
|
|
|
|
CancelJob *s;
|
|
|
|
|
|
|
|
s = create_common(&job);
|
|
|
|
|
2018-04-13 23:31:02 +08:00
|
|
|
job_start(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_RUNNING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
s->should_converge = true;
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_READY);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
2018-04-23 18:24:16 +08:00
|
|
|
job_complete(&job->job, &error_abort);
|
2018-04-24 22:13:52 +08:00
|
|
|
job_enter(&job->job);
|
2018-03-10 16:27:46 +08:00
|
|
|
while (!s->completed) {
|
|
|
|
aio_poll(qemu_get_aio_context(), true);
|
|
|
|
}
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_PENDING);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
2018-04-23 22:06:26 +08:00
|
|
|
job_finalize(&job->job, &error_abort);
|
2018-04-13 23:19:31 +08:00
|
|
|
assert(job->job.status == JOB_STATUS_CONCLUDED);
|
2018-03-10 16:27:46 +08:00
|
|
|
|
|
|
|
cancel_common(s);
|
|
|
|
}
|
|
|
|
|
2016-07-29 22:31:41 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
qemu_init_main_loop(&error_abort);
|
2017-01-17 00:17:38 +08:00
|
|
|
bdrv_init();
|
2016-07-29 22:31:41 +08:00
|
|
|
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
g_test_add_func("/blockjob/ids", test_job_ids);
|
2018-03-10 16:27:46 +08:00
|
|
|
g_test_add_func("/blockjob/cancel/created", test_cancel_created);
|
|
|
|
g_test_add_func("/blockjob/cancel/running", test_cancel_running);
|
|
|
|
g_test_add_func("/blockjob/cancel/paused", test_cancel_paused);
|
|
|
|
g_test_add_func("/blockjob/cancel/ready", test_cancel_ready);
|
|
|
|
g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
|
|
|
|
g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
|
|
|
|
g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
|
2016-07-29 22:31:41 +08:00
|
|
|
return g_test_run();
|
|
|
|
}
|