123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898 |
- /*
- * Block tests for iothreads
- *
- * Copyright (c) 2018 Kevin Wolf <kwolf@redhat.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- #include "qemu/osdep.h"
- #include "block/block.h"
- #include "block/block_int-global-state.h"
- #include "block/blockjob_int.h"
- #include "sysemu/block-backend.h"
- #include "qapi/error.h"
- #include "qapi/qmp/qdict.h"
- #include "qemu/main-loop.h"
- #include "iothread.h"
- static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
- int64_t offset, int64_t bytes,
- QEMUIOVector *qiov,
- BdrvRequestFlags flags)
- {
- return 0;
- }
- static int coroutine_fn bdrv_test_co_pwritev(BlockDriverState *bs,
- int64_t offset, int64_t bytes,
- QEMUIOVector *qiov,
- BdrvRequestFlags flags)
- {
- return 0;
- }
- static int coroutine_fn bdrv_test_co_pdiscard(BlockDriverState *bs,
- int64_t offset, int64_t bytes)
- {
- return 0;
- }
- static int coroutine_fn
- bdrv_test_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
- PreallocMode prealloc, BdrvRequestFlags flags,
- Error **errp)
- {
- return 0;
- }
- static int coroutine_fn bdrv_test_co_block_status(BlockDriverState *bs,
- bool want_zero,
- int64_t offset, int64_t count,
- int64_t *pnum, int64_t *map,
- BlockDriverState **file)
- {
- *pnum = count;
- return 0;
- }
- static BlockDriver bdrv_test = {
- .format_name = "test",
- .instance_size = 1,
- .bdrv_co_preadv = bdrv_test_co_preadv,
- .bdrv_co_pwritev = bdrv_test_co_pwritev,
- .bdrv_co_pdiscard = bdrv_test_co_pdiscard,
- .bdrv_co_truncate = bdrv_test_co_truncate,
- .bdrv_co_block_status = bdrv_test_co_block_status,
- };
- static void test_sync_op_pread(BdrvChild *c)
- {
- uint8_t buf[512];
- int ret;
- /* Success */
- ret = bdrv_pread(c, 0, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = bdrv_pread(c, -2, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_pwrite(BdrvChild *c)
- {
- uint8_t buf[512] = { 0 };
- int ret;
- /* Success */
- ret = bdrv_pwrite(c, 0, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = bdrv_pwrite(c, -2, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_pread(BlockBackend *blk)
- {
- uint8_t buf[512];
- int ret;
- /* Success */
- ret = blk_pread(blk, 0, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_pread(blk, -2, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_pwrite(BlockBackend *blk)
- {
- uint8_t buf[512] = { 0 };
- int ret;
- /* Success */
- ret = blk_pwrite(blk, 0, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_pwrite(blk, -2, sizeof(buf), buf, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_preadv(BlockBackend *blk)
- {
- uint8_t buf[512];
- QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, sizeof(buf));
- int ret;
- /* Success */
- ret = blk_preadv(blk, 0, sizeof(buf), &qiov, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_preadv(blk, -2, sizeof(buf), &qiov, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_pwritev(BlockBackend *blk)
- {
- uint8_t buf[512] = { 0 };
- QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, sizeof(buf));
- int ret;
- /* Success */
- ret = blk_pwritev(blk, 0, sizeof(buf), &qiov, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_pwritev(blk, -2, sizeof(buf), &qiov, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_preadv_part(BlockBackend *blk)
- {
- uint8_t buf[512];
- QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, sizeof(buf));
- int ret;
- /* Success */
- ret = blk_preadv_part(blk, 0, sizeof(buf), &qiov, 0, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_preadv_part(blk, -2, sizeof(buf), &qiov, 0, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_pwritev_part(BlockBackend *blk)
- {
- uint8_t buf[512] = { 0 };
- QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, sizeof(buf));
- int ret;
- /* Success */
- ret = blk_pwritev_part(blk, 0, sizeof(buf), &qiov, 0, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_pwritev_part(blk, -2, sizeof(buf), &qiov, 0, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_pwrite_compressed(BlockBackend *blk)
- {
- uint8_t buf[512] = { 0 };
- int ret;
- /* Late error: Not supported */
- ret = blk_pwrite_compressed(blk, 0, sizeof(buf), buf);
- g_assert_cmpint(ret, ==, -ENOTSUP);
- /* Early error: Negative offset */
- ret = blk_pwrite_compressed(blk, -2, sizeof(buf), buf);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_pwrite_zeroes(BlockBackend *blk)
- {
- int ret;
- /* Success */
- ret = blk_pwrite_zeroes(blk, 0, 512, 0);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_pwrite_zeroes(blk, -2, 512, 0);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_load_vmstate(BdrvChild *c)
- {
- uint8_t buf[512];
- int ret;
- /* Error: Driver does not support snapshots */
- ret = bdrv_load_vmstate(c->bs, buf, 0, sizeof(buf));
- g_assert_cmpint(ret, ==, -ENOTSUP);
- }
- static void test_sync_op_save_vmstate(BdrvChild *c)
- {
- uint8_t buf[512] = { 0 };
- int ret;
- /* Error: Driver does not support snapshots */
- ret = bdrv_save_vmstate(c->bs, buf, 0, sizeof(buf));
- g_assert_cmpint(ret, ==, -ENOTSUP);
- }
- static void test_sync_op_pdiscard(BdrvChild *c)
- {
- int ret;
- /* Normal success path */
- c->bs->open_flags |= BDRV_O_UNMAP;
- ret = bdrv_pdiscard(c, 0, 512);
- g_assert_cmpint(ret, ==, 0);
- /* Early success: UNMAP not supported */
- c->bs->open_flags &= ~BDRV_O_UNMAP;
- ret = bdrv_pdiscard(c, 0, 512);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = bdrv_pdiscard(c, -2, 512);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_blk_pdiscard(BlockBackend *blk)
- {
- int ret;
- /* Early success: UNMAP not supported */
- ret = blk_pdiscard(blk, 0, 512);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_pdiscard(blk, -2, 512);
- g_assert_cmpint(ret, ==, -EIO);
- }
- static void test_sync_op_truncate(BdrvChild *c)
- {
- int ret;
- /* Normal success path */
- ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, 0, NULL);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = bdrv_truncate(c, -2, false, PREALLOC_MODE_OFF, 0, NULL);
- g_assert_cmpint(ret, ==, -EINVAL);
- /* Error: Read-only image */
- c->bs->open_flags &= ~BDRV_O_RDWR;
- ret = bdrv_truncate(c, 65536, false, PREALLOC_MODE_OFF, 0, NULL);
- g_assert_cmpint(ret, ==, -EACCES);
- c->bs->open_flags |= BDRV_O_RDWR;
- }
- static void test_sync_op_blk_truncate(BlockBackend *blk)
- {
- int ret;
- /* Normal success path */
- ret = blk_truncate(blk, 65536, false, PREALLOC_MODE_OFF, 0, NULL);
- g_assert_cmpint(ret, ==, 0);
- /* Early error: Negative offset */
- ret = blk_truncate(blk, -2, false, PREALLOC_MODE_OFF, 0, NULL);
- g_assert_cmpint(ret, ==, -EINVAL);
- }
- /* Disable TSA to make bdrv_test.bdrv_co_block_status writable */
- static void TSA_NO_TSA test_sync_op_block_status(BdrvChild *c)
- {
- int ret;
- int64_t n;
- /* Normal success path */
- ret = bdrv_is_allocated(c->bs, 0, 65536, &n);
- g_assert_cmpint(ret, ==, 0);
- /* Early success: No driver support */
- bdrv_test.bdrv_co_block_status = NULL;
- ret = bdrv_is_allocated(c->bs, 0, 65536, &n);
- g_assert_cmpint(ret, ==, 1);
- /* Early success: bytes = 0 */
- ret = bdrv_is_allocated(c->bs, 0, 0, &n);
- g_assert_cmpint(ret, ==, 0);
- /* Early success: Offset > image size*/
- ret = bdrv_is_allocated(c->bs, 0x1000000, 0x1000000, &n);
- g_assert_cmpint(ret, ==, 0);
- }
- static void test_sync_op_flush(BdrvChild *c)
- {
- int ret;
- /* Normal success path */
- ret = bdrv_flush(c->bs);
- g_assert_cmpint(ret, ==, 0);
- /* Early success: Read-only image */
- c->bs->open_flags &= ~BDRV_O_RDWR;
- ret = bdrv_flush(c->bs);
- g_assert_cmpint(ret, ==, 0);
- c->bs->open_flags |= BDRV_O_RDWR;
- }
- static void test_sync_op_blk_flush(BlockBackend *blk)
- {
- BlockDriverState *bs = blk_bs(blk);
- int ret;
- /* Normal success path */
- ret = blk_flush(blk);
- g_assert_cmpint(ret, ==, 0);
- /* Early success: Read-only image */
- bs->open_flags &= ~BDRV_O_RDWR;
- ret = blk_flush(blk);
- g_assert_cmpint(ret, ==, 0);
- bs->open_flags |= BDRV_O_RDWR;
- }
- static void test_sync_op_check(BdrvChild *c)
- {
- BdrvCheckResult result;
- int ret;
- /* Error: Driver does not implement check */
- ret = bdrv_check(c->bs, &result, 0);
- g_assert_cmpint(ret, ==, -ENOTSUP);
- }
- static void test_sync_op_activate(BdrvChild *c)
- {
- GLOBAL_STATE_CODE();
- GRAPH_RDLOCK_GUARD_MAINLOOP();
- /* Early success: Image is not inactive */
- bdrv_activate(c->bs, NULL);
- }
- typedef struct SyncOpTest {
- const char *name;
- void (*fn)(BdrvChild *c);
- void (*blkfn)(BlockBackend *blk);
- } SyncOpTest;
- const SyncOpTest sync_op_tests[] = {
- {
- .name = "/sync-op/pread",
- .fn = test_sync_op_pread,
- .blkfn = test_sync_op_blk_pread,
- }, {
- .name = "/sync-op/pwrite",
- .fn = test_sync_op_pwrite,
- .blkfn = test_sync_op_blk_pwrite,
- }, {
- .name = "/sync-op/preadv",
- .fn = NULL,
- .blkfn = test_sync_op_blk_preadv,
- }, {
- .name = "/sync-op/pwritev",
- .fn = NULL,
- .blkfn = test_sync_op_blk_pwritev,
- }, {
- .name = "/sync-op/preadv_part",
- .fn = NULL,
- .blkfn = test_sync_op_blk_preadv_part,
- }, {
- .name = "/sync-op/pwritev_part",
- .fn = NULL,
- .blkfn = test_sync_op_blk_pwritev_part,
- }, {
- .name = "/sync-op/pwrite_compressed",
- .fn = NULL,
- .blkfn = test_sync_op_blk_pwrite_compressed,
- }, {
- .name = "/sync-op/pwrite_zeroes",
- .fn = NULL,
- .blkfn = test_sync_op_blk_pwrite_zeroes,
- }, {
- .name = "/sync-op/load_vmstate",
- .fn = test_sync_op_load_vmstate,
- }, {
- .name = "/sync-op/save_vmstate",
- .fn = test_sync_op_save_vmstate,
- }, {
- .name = "/sync-op/pdiscard",
- .fn = test_sync_op_pdiscard,
- .blkfn = test_sync_op_blk_pdiscard,
- }, {
- .name = "/sync-op/truncate",
- .fn = test_sync_op_truncate,
- .blkfn = test_sync_op_blk_truncate,
- }, {
- .name = "/sync-op/block_status",
- .fn = test_sync_op_block_status,
- }, {
- .name = "/sync-op/flush",
- .fn = test_sync_op_flush,
- .blkfn = test_sync_op_blk_flush,
- }, {
- .name = "/sync-op/check",
- .fn = test_sync_op_check,
- }, {
- .name = "/sync-op/activate",
- .fn = test_sync_op_activate,
- },
- };
- /* Test synchronous operations that run in a different iothread, so we have to
- * poll for the coroutine there to return. */
- static void test_sync_op(const void *opaque)
- {
- const SyncOpTest *t = opaque;
- IOThread *iothread = iothread_new();
- AioContext *ctx = iothread_get_aio_context(iothread);
- BlockBackend *blk;
- BlockDriverState *bs;
- BdrvChild *c;
- GLOBAL_STATE_CODE();
- blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
- bs = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
- bs->total_sectors = 65536 / BDRV_SECTOR_SIZE;
- blk_insert_bs(blk, bs, &error_abort);
- bdrv_graph_rdlock_main_loop();
- c = QLIST_FIRST(&bs->parents);
- bdrv_graph_rdunlock_main_loop();
- blk_set_aio_context(blk, ctx, &error_abort);
- if (t->fn) {
- t->fn(c);
- }
- if (t->blkfn) {
- t->blkfn(blk);
- }
- blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
- bdrv_unref(bs);
- blk_unref(blk);
- }
- typedef struct TestBlockJob {
- BlockJob common;
- bool should_complete;
- int n;
- } TestBlockJob;
- static int test_job_prepare(Job *job)
- {
- g_assert(qemu_get_current_aio_context() == qemu_get_aio_context());
- return 0;
- }
- static int coroutine_fn test_job_run(Job *job, Error **errp)
- {
- TestBlockJob *s = container_of(job, TestBlockJob, common.job);
- job_transition_to_ready(&s->common.job);
- while (!s->should_complete) {
- s->n++;
- g_assert(qemu_get_current_aio_context() == job->aio_context);
- /* Avoid job_sleep_ns() because it marks the job as !busy. We want to
- * emulate some actual activity (probably some I/O) here so that the
- * drain involved in AioContext switches has to wait for this activity
- * to stop. */
- qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 1000000);
- job_pause_point(&s->common.job);
- }
- g_assert(qemu_get_current_aio_context() == job->aio_context);
- return 0;
- }
- static void test_job_complete(Job *job, Error **errp)
- {
- TestBlockJob *s = container_of(job, TestBlockJob, common.job);
- s->should_complete = true;
- }
- BlockJobDriver test_job_driver = {
- .job_driver = {
- .instance_size = sizeof(TestBlockJob),
- .free = block_job_free,
- .user_resume = block_job_user_resume,
- .run = test_job_run,
- .complete = test_job_complete,
- .prepare = test_job_prepare,
- },
- };
- static void test_attach_blockjob(void)
- {
- IOThread *iothread = iothread_new();
- AioContext *ctx = iothread_get_aio_context(iothread);
- BlockBackend *blk;
- BlockDriverState *bs;
- TestBlockJob *tjob;
- blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL, BLK_PERM_ALL);
- bs = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
- blk_insert_bs(blk, bs, &error_abort);
- tjob = block_job_create("job0", &test_job_driver, NULL, bs,
- 0, BLK_PERM_ALL,
- 0, 0, NULL, NULL, &error_abort);
- job_start(&tjob->common.job);
- while (tjob->n == 0) {
- aio_poll(qemu_get_aio_context(), false);
- }
- blk_set_aio_context(blk, ctx, &error_abort);
- tjob->n = 0;
- while (tjob->n == 0) {
- aio_poll(qemu_get_aio_context(), false);
- }
- blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
- tjob->n = 0;
- while (tjob->n == 0) {
- aio_poll(qemu_get_aio_context(), false);
- }
- blk_set_aio_context(blk, ctx, &error_abort);
- tjob->n = 0;
- while (tjob->n == 0) {
- aio_poll(qemu_get_aio_context(), false);
- }
- WITH_JOB_LOCK_GUARD() {
- job_complete_sync_locked(&tjob->common.job, &error_abort);
- }
- blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
- bdrv_unref(bs);
- blk_unref(blk);
- }
- /*
- * Test that changing the AioContext for one node in a tree (here through blk)
- * changes all other nodes as well:
- *
- * blk
- * |
- * | bs_verify [blkverify]
- * | / \
- * | / \
- * bs_a [bdrv_test] bs_b [bdrv_test]
- *
- */
- static void test_propagate_basic(void)
- {
- IOThread *iothread = iothread_new();
- AioContext *ctx = iothread_get_aio_context(iothread);
- AioContext *main_ctx;
- BlockBackend *blk;
- BlockDriverState *bs_a, *bs_b, *bs_verify;
- QDict *options;
- /*
- * Create bs_a and its BlockBackend. We cannot take the RESIZE
- * permission because blkverify will not share it on the test
- * image.
- */
- blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL & ~BLK_PERM_RESIZE,
- BLK_PERM_ALL);
- bs_a = bdrv_new_open_driver(&bdrv_test, "bs_a", BDRV_O_RDWR, &error_abort);
- blk_insert_bs(blk, bs_a, &error_abort);
- /* Create bs_b */
- bs_b = bdrv_new_open_driver(&bdrv_test, "bs_b", BDRV_O_RDWR, &error_abort);
- /* Create blkverify filter that references both bs_a and bs_b */
- options = qdict_new();
- qdict_put_str(options, "driver", "blkverify");
- qdict_put_str(options, "test", "bs_a");
- qdict_put_str(options, "raw", "bs_b");
- bs_verify = bdrv_open(NULL, NULL, options, BDRV_O_RDWR, &error_abort);
- /* Switch the AioContext */
- blk_set_aio_context(blk, ctx, &error_abort);
- g_assert(blk_get_aio_context(blk) == ctx);
- g_assert(bdrv_get_aio_context(bs_a) == ctx);
- g_assert(bdrv_get_aio_context(bs_verify) == ctx);
- g_assert(bdrv_get_aio_context(bs_b) == ctx);
- /* Switch the AioContext back */
- main_ctx = qemu_get_aio_context();
- blk_set_aio_context(blk, main_ctx, &error_abort);
- g_assert(blk_get_aio_context(blk) == main_ctx);
- g_assert(bdrv_get_aio_context(bs_a) == main_ctx);
- g_assert(bdrv_get_aio_context(bs_verify) == main_ctx);
- g_assert(bdrv_get_aio_context(bs_b) == main_ctx);
- bdrv_unref(bs_verify);
- bdrv_unref(bs_b);
- bdrv_unref(bs_a);
- blk_unref(blk);
- }
- /*
- * Test that diamonds in the graph don't lead to endless recursion:
- *
- * blk
- * |
- * bs_verify [blkverify]
- * / \
- * / \
- * bs_b [raw] bs_c[raw]
- * \ /
- * \ /
- * bs_a [bdrv_test]
- */
- static void test_propagate_diamond(void)
- {
- IOThread *iothread = iothread_new();
- AioContext *ctx = iothread_get_aio_context(iothread);
- AioContext *main_ctx;
- BlockBackend *blk;
- BlockDriverState *bs_a, *bs_b, *bs_c, *bs_verify;
- QDict *options;
- /* Create bs_a */
- bs_a = bdrv_new_open_driver(&bdrv_test, "bs_a", BDRV_O_RDWR, &error_abort);
- /* Create bs_b and bc_c */
- options = qdict_new();
- qdict_put_str(options, "driver", "raw");
- qdict_put_str(options, "file", "bs_a");
- qdict_put_str(options, "node-name", "bs_b");
- bs_b = bdrv_open(NULL, NULL, options, BDRV_O_RDWR, &error_abort);
- options = qdict_new();
- qdict_put_str(options, "driver", "raw");
- qdict_put_str(options, "file", "bs_a");
- qdict_put_str(options, "node-name", "bs_c");
- bs_c = bdrv_open(NULL, NULL, options, BDRV_O_RDWR, &error_abort);
- /* Create blkverify filter that references both bs_b and bs_c */
- options = qdict_new();
- qdict_put_str(options, "driver", "blkverify");
- qdict_put_str(options, "test", "bs_b");
- qdict_put_str(options, "raw", "bs_c");
- bs_verify = bdrv_open(NULL, NULL, options, BDRV_O_RDWR, &error_abort);
- /*
- * Do not take the RESIZE permission: This would require the same
- * from bs_c and thus from bs_a; however, blkverify will not share
- * it on bs_b, and thus it will not be available for bs_a.
- */
- blk = blk_new(qemu_get_aio_context(), BLK_PERM_ALL & ~BLK_PERM_RESIZE,
- BLK_PERM_ALL);
- blk_insert_bs(blk, bs_verify, &error_abort);
- /* Switch the AioContext */
- blk_set_aio_context(blk, ctx, &error_abort);
- g_assert(blk_get_aio_context(blk) == ctx);
- g_assert(bdrv_get_aio_context(bs_verify) == ctx);
- g_assert(bdrv_get_aio_context(bs_a) == ctx);
- g_assert(bdrv_get_aio_context(bs_b) == ctx);
- g_assert(bdrv_get_aio_context(bs_c) == ctx);
- /* Switch the AioContext back */
- main_ctx = qemu_get_aio_context();
- blk_set_aio_context(blk, main_ctx, &error_abort);
- g_assert(blk_get_aio_context(blk) == main_ctx);
- g_assert(bdrv_get_aio_context(bs_verify) == main_ctx);
- g_assert(bdrv_get_aio_context(bs_a) == main_ctx);
- g_assert(bdrv_get_aio_context(bs_b) == main_ctx);
- g_assert(bdrv_get_aio_context(bs_c) == main_ctx);
- blk_unref(blk);
- bdrv_unref(bs_verify);
- bdrv_unref(bs_c);
- bdrv_unref(bs_b);
- bdrv_unref(bs_a);
- }
- static void test_propagate_mirror(void)
- {
- IOThread *iothread = iothread_new();
- AioContext *ctx = iothread_get_aio_context(iothread);
- AioContext *main_ctx = qemu_get_aio_context();
- BlockDriverState *src, *target, *filter;
- BlockBackend *blk;
- Job *job;
- Error *local_err = NULL;
- /* Create src and target*/
- src = bdrv_new_open_driver(&bdrv_test, "src", BDRV_O_RDWR, &error_abort);
- target = bdrv_new_open_driver(&bdrv_test, "target", BDRV_O_RDWR,
- &error_abort);
- /* Start a mirror job */
- mirror_start("job0", src, target, NULL, JOB_DEFAULT, 0, 0, 0,
- MIRROR_SYNC_MODE_NONE, MIRROR_OPEN_BACKING_CHAIN, false,
- BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
- false, "filter_node", MIRROR_COPY_MODE_BACKGROUND,
- &error_abort);
- WITH_JOB_LOCK_GUARD() {
- job = job_get_locked("job0");
- }
- filter = bdrv_find_node("filter_node");
- /* Change the AioContext of src */
- bdrv_try_change_aio_context(src, ctx, NULL, &error_abort);
- g_assert(bdrv_get_aio_context(src) == ctx);
- g_assert(bdrv_get_aio_context(target) == ctx);
- g_assert(bdrv_get_aio_context(filter) == ctx);
- g_assert(job->aio_context == ctx);
- /* Change the AioContext of target */
- bdrv_try_change_aio_context(target, main_ctx, NULL, &error_abort);
- g_assert(bdrv_get_aio_context(src) == main_ctx);
- g_assert(bdrv_get_aio_context(target) == main_ctx);
- g_assert(bdrv_get_aio_context(filter) == main_ctx);
- /* With a BlockBackend on src, changing target must fail */
- blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
- blk_insert_bs(blk, src, &error_abort);
- bdrv_try_change_aio_context(target, ctx, NULL, &local_err);
- error_free_or_abort(&local_err);
- g_assert(blk_get_aio_context(blk) == main_ctx);
- g_assert(bdrv_get_aio_context(src) == main_ctx);
- g_assert(bdrv_get_aio_context(target) == main_ctx);
- g_assert(bdrv_get_aio_context(filter) == main_ctx);
- /* ...unless we explicitly allow it */
- blk_set_allow_aio_context_change(blk, true);
- bdrv_try_change_aio_context(target, ctx, NULL, &error_abort);
- g_assert(blk_get_aio_context(blk) == ctx);
- g_assert(bdrv_get_aio_context(src) == ctx);
- g_assert(bdrv_get_aio_context(target) == ctx);
- g_assert(bdrv_get_aio_context(filter) == ctx);
- job_cancel_sync_all();
- blk_set_aio_context(blk, main_ctx, &error_abort);
- bdrv_try_change_aio_context(target, main_ctx, NULL, &error_abort);
- blk_unref(blk);
- bdrv_unref(src);
- bdrv_unref(target);
- }
- static void test_attach_second_node(void)
- {
- IOThread *iothread = iothread_new();
- AioContext *ctx = iothread_get_aio_context(iothread);
- AioContext *main_ctx = qemu_get_aio_context();
- BlockBackend *blk;
- BlockDriverState *bs, *filter;
- QDict *options;
- blk = blk_new(ctx, BLK_PERM_ALL, BLK_PERM_ALL);
- bs = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
- blk_insert_bs(blk, bs, &error_abort);
- options = qdict_new();
- qdict_put_str(options, "driver", "raw");
- qdict_put_str(options, "file", "base");
- filter = bdrv_open(NULL, NULL, options, BDRV_O_RDWR, &error_abort);
- g_assert(blk_get_aio_context(blk) == ctx);
- g_assert(bdrv_get_aio_context(bs) == ctx);
- g_assert(bdrv_get_aio_context(filter) == ctx);
- blk_set_aio_context(blk, main_ctx, &error_abort);
- g_assert(blk_get_aio_context(blk) == main_ctx);
- g_assert(bdrv_get_aio_context(bs) == main_ctx);
- g_assert(bdrv_get_aio_context(filter) == main_ctx);
- bdrv_unref(filter);
- bdrv_unref(bs);
- blk_unref(blk);
- }
- static void test_attach_preserve_blk_ctx(void)
- {
- IOThread *iothread = iothread_new();
- AioContext *ctx = iothread_get_aio_context(iothread);
- BlockBackend *blk;
- BlockDriverState *bs;
- blk = blk_new(ctx, BLK_PERM_ALL, BLK_PERM_ALL);
- bs = bdrv_new_open_driver(&bdrv_test, "base", BDRV_O_RDWR, &error_abort);
- bs->total_sectors = 65536 / BDRV_SECTOR_SIZE;
- /* Add node to BlockBackend that has an iothread context assigned */
- blk_insert_bs(blk, bs, &error_abort);
- g_assert(blk_get_aio_context(blk) == ctx);
- g_assert(bdrv_get_aio_context(bs) == ctx);
- /* Remove the node again */
- blk_remove_bs(blk);
- g_assert(blk_get_aio_context(blk) == ctx);
- g_assert(bdrv_get_aio_context(bs) == qemu_get_aio_context());
- /* Re-attach the node */
- blk_insert_bs(blk, bs, &error_abort);
- g_assert(blk_get_aio_context(blk) == ctx);
- g_assert(bdrv_get_aio_context(bs) == ctx);
- blk_set_aio_context(blk, qemu_get_aio_context(), &error_abort);
- bdrv_unref(bs);
- blk_unref(blk);
- }
- int main(int argc, char **argv)
- {
- int i;
- bdrv_init();
- qemu_init_main_loop(&error_abort);
- g_test_init(&argc, &argv, NULL);
- for (i = 0; i < ARRAY_SIZE(sync_op_tests); i++) {
- const SyncOpTest *t = &sync_op_tests[i];
- g_test_add_data_func(t->name, t, test_sync_op);
- }
- g_test_add_func("/attach/blockjob", test_attach_blockjob);
- g_test_add_func("/attach/second_node", test_attach_second_node);
- g_test_add_func("/attach/preserve_blk_ctx", test_attach_preserve_blk_ctx);
- g_test_add_func("/propagate/basic", test_propagate_basic);
- g_test_add_func("/propagate/diamond", test_propagate_diamond);
- g_test_add_func("/propagate/mirror", test_propagate_mirror);
- return g_test_run();
- }
|