qemu/tests/test-blockjob-txn.c
<<
>>
Prefs
   1/*
   2 * Blockjob transactions tests
   3 *
   4 * Copyright Red Hat, Inc. 2015
   5 *
   6 * Authors:
   7 *  Stefan Hajnoczi    <stefanha@redhat.com>
   8 *
   9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  10 * See the COPYING.LIB file in the top-level directory.
  11 */
  12
  13#include "qemu/osdep.h"
  14#include "qapi/error.h"
  15#include "qemu/main-loop.h"
  16#include "block/blockjob_int.h"
  17#include "sysemu/block-backend.h"
  18
  19typedef struct {
  20    BlockJob common;
  21    unsigned int iterations;
  22    bool use_timer;
  23    int rc;
  24    int *result;
  25} TestBlockJob;
  26
  27static void test_block_job_complete(BlockJob *job, void *opaque)
  28{
  29    BlockDriverState *bs = blk_bs(job->blk);
  30    int rc = (intptr_t)opaque;
  31
  32    if (block_job_is_cancelled(job)) {
  33        rc = -ECANCELED;
  34    }
  35
  36    block_job_completed(job, rc);
  37    bdrv_unref(bs);
  38}
  39
  40static void coroutine_fn test_block_job_run(void *opaque)
  41{
  42    TestBlockJob *s = opaque;
  43    BlockJob *job = &s->common;
  44
  45    while (s->iterations--) {
  46        if (s->use_timer) {
  47            block_job_sleep_ns(job, 0);
  48        } else {
  49            block_job_yield(job);
  50        }
  51
  52        if (block_job_is_cancelled(job)) {
  53            break;
  54        }
  55    }
  56
  57    block_job_defer_to_main_loop(job, test_block_job_complete,
  58                                 (void *)(intptr_t)s->rc);
  59}
  60
  61typedef struct {
  62    TestBlockJob *job;
  63    int *result;
  64} TestBlockJobCBData;
  65
  66static void test_block_job_cb(void *opaque, int ret)
  67{
  68    TestBlockJobCBData *data = opaque;
  69    if (!ret && block_job_is_cancelled(&data->job->common)) {
  70        ret = -ECANCELED;
  71    }
  72    *data->result = ret;
  73    g_free(data);
  74}
  75
  76static const BlockJobDriver test_block_job_driver = {
  77    .instance_size = sizeof(TestBlockJob),
  78    .start = test_block_job_run,
  79};
  80
  81/* Create a block job that completes with a given return code after a given
  82 * number of event loop iterations.  The return code is stored in the given
  83 * result pointer.
  84 *
  85 * The event loop iterations can either be handled automatically with a 0 delay
  86 * timer, or they can be stepped manually by entering the coroutine.
  87 */
  88static BlockJob *test_block_job_start(unsigned int iterations,
  89                                      bool use_timer,
  90                                      int rc, int *result, BlockJobTxn *txn)
  91{
  92    BlockDriverState *bs;
  93    TestBlockJob *s;
  94    TestBlockJobCBData *data;
  95    static unsigned counter;
  96    char job_id[24];
  97
  98    data = g_new0(TestBlockJobCBData, 1);
  99
 100    bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
 101    g_assert_nonnull(bs);
 102
 103    snprintf(job_id, sizeof(job_id), "job%u", counter++);
 104    s = block_job_create(job_id, &test_block_job_driver, txn, bs,
 105                         0, BLK_PERM_ALL, 0, BLOCK_JOB_DEFAULT,
 106                         test_block_job_cb, data, &error_abort);
 107    s->iterations = iterations;
 108    s->use_timer = use_timer;
 109    s->rc = rc;
 110    s->result = result;
 111    data->job = s;
 112    data->result = result;
 113    return &s->common;
 114}
 115
 116static void test_single_job(int expected)
 117{
 118    BlockJob *job;
 119    BlockJobTxn *txn;
 120    int result = -EINPROGRESS;
 121
 122    txn = block_job_txn_new();
 123    job = test_block_job_start(1, true, expected, &result, txn);
 124    block_job_start(job);
 125
 126    if (expected == -ECANCELED) {
 127        block_job_cancel(job, false);
 128    }
 129
 130    while (result == -EINPROGRESS) {
 131        aio_poll(qemu_get_aio_context(), true);
 132    }
 133    g_assert_cmpint(result, ==, expected);
 134
 135    block_job_txn_unref(txn);
 136}
 137
 138static void test_single_job_success(void)
 139{
 140    test_single_job(0);
 141}
 142
 143static void test_single_job_failure(void)
 144{
 145    test_single_job(-EIO);
 146}
 147
 148static void test_single_job_cancel(void)
 149{
 150    test_single_job(-ECANCELED);
 151}
 152
 153static void test_pair_jobs(int expected1, int expected2)
 154{
 155    BlockJob *job1;
 156    BlockJob *job2;
 157    BlockJobTxn *txn;
 158    int result1 = -EINPROGRESS;
 159    int result2 = -EINPROGRESS;
 160
 161    txn = block_job_txn_new();
 162    job1 = test_block_job_start(1, true, expected1, &result1, txn);
 163    job2 = test_block_job_start(2, true, expected2, &result2, txn);
 164    block_job_start(job1);
 165    block_job_start(job2);
 166
 167    /* Release our reference now to trigger as many nice
 168     * use-after-free bugs as possible.
 169     */
 170    block_job_txn_unref(txn);
 171
 172    if (expected1 == -ECANCELED) {
 173        block_job_cancel(job1, false);
 174    }
 175    if (expected2 == -ECANCELED) {
 176        block_job_cancel(job2, false);
 177    }
 178
 179    while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
 180        aio_poll(qemu_get_aio_context(), true);
 181    }
 182
 183    /* Failure or cancellation of one job cancels the other job */
 184    if (expected1 != 0) {
 185        expected2 = -ECANCELED;
 186    } else if (expected2 != 0) {
 187        expected1 = -ECANCELED;
 188    }
 189
 190    g_assert_cmpint(result1, ==, expected1);
 191    g_assert_cmpint(result2, ==, expected2);
 192}
 193
 194static void test_pair_jobs_success(void)
 195{
 196    test_pair_jobs(0, 0);
 197}
 198
 199static void test_pair_jobs_failure(void)
 200{
 201    /* Test both orderings.  The two jobs run for a different number of
 202     * iterations so the code path is different depending on which job fails
 203     * first.
 204     */
 205    test_pair_jobs(-EIO, 0);
 206    test_pair_jobs(0, -EIO);
 207}
 208
 209static void test_pair_jobs_cancel(void)
 210{
 211    test_pair_jobs(-ECANCELED, 0);
 212    test_pair_jobs(0, -ECANCELED);
 213}
 214
 215static void test_pair_jobs_fail_cancel_race(void)
 216{
 217    BlockJob *job1;
 218    BlockJob *job2;
 219    BlockJobTxn *txn;
 220    int result1 = -EINPROGRESS;
 221    int result2 = -EINPROGRESS;
 222
 223    txn = block_job_txn_new();
 224    job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn);
 225    job2 = test_block_job_start(2, false, 0, &result2, txn);
 226    block_job_start(job1);
 227    block_job_start(job2);
 228
 229    block_job_cancel(job1, false);
 230
 231    /* Now make job2 finish before the main loop kicks jobs.  This simulates
 232     * the race between a pending kick and another job completing.
 233     */
 234    block_job_enter(job2);
 235    block_job_enter(job2);
 236
 237    while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
 238        aio_poll(qemu_get_aio_context(), true);
 239    }
 240
 241    g_assert_cmpint(result1, ==, -ECANCELED);
 242    g_assert_cmpint(result2, ==, -ECANCELED);
 243
 244    block_job_txn_unref(txn);
 245}
 246
 247int main(int argc, char **argv)
 248{
 249    qemu_init_main_loop(&error_abort);
 250    bdrv_init();
 251
 252    g_test_init(&argc, &argv, NULL);
 253    g_test_add_func("/single/success", test_single_job_success);
 254    g_test_add_func("/single/failure", test_single_job_failure);
 255    g_test_add_func("/single/cancel", test_single_job_cancel);
 256    g_test_add_func("/pair/success", test_pair_jobs_success);
 257    g_test_add_func("/pair/failure", test_pair_jobs_failure);
 258    g_test_add_func("/pair/cancel", test_pair_jobs_cancel);
 259    g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race);
 260    return g_test_run();
 261}
 262