test-blockjob-txn.c 6.6 KB
Newer Older
S
Stefan Hajnoczi 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Blockjob transactions tests
 *
 * Copyright Red Hat, Inc. 2015
 *
 * Authors:
 *  Stefan Hajnoczi    <stefanha@redhat.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.
 */

P
Peter Maydell 已提交
13
#include "qemu/osdep.h"
14
#include "qapi/error.h"
S
Stefan Hajnoczi 已提交
15
#include "qemu/main-loop.h"
16
#include "block/blockjob_int.h"
K
Kevin Wolf 已提交
17
#include "sysemu/block-backend.h"
S
Stefan Hajnoczi 已提交
18 19 20 21 22 23 24 25 26 27 28

typedef struct {
    BlockJob common;
    unsigned int iterations;
    bool use_timer;
    int rc;
    int *result;
} TestBlockJob;

static void test_block_job_complete(BlockJob *job, void *opaque)
{
K
Kevin Wolf 已提交
29
    BlockDriverState *bs = blk_bs(job->blk);
S
Stefan Hajnoczi 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    int rc = (intptr_t)opaque;

    if (block_job_is_cancelled(job)) {
        rc = -ECANCELED;
    }

    block_job_completed(job, rc);
    bdrv_unref(bs);
}

static void coroutine_fn test_block_job_run(void *opaque)
{
    TestBlockJob *s = opaque;
    BlockJob *job = &s->common;

    while (s->iterations--) {
        if (s->use_timer) {
47
            block_job_sleep_ns(job, 0);
S
Stefan Hajnoczi 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        } else {
            block_job_yield(job);
        }

        if (block_job_is_cancelled(job)) {
            break;
        }
    }

    block_job_defer_to_main_loop(job, test_block_job_complete,
                                 (void *)(intptr_t)s->rc);
}

typedef struct {
    TestBlockJob *job;
    int *result;
} TestBlockJobCBData;

static void test_block_job_cb(void *opaque, int ret)
{
    TestBlockJobCBData *data = opaque;
    if (!ret && block_job_is_cancelled(&data->job->common)) {
        ret = -ECANCELED;
    }
    *data->result = ret;
    g_free(data);
}

J
John Snow 已提交
76 77 78 79 80
static const BlockJobDriver test_block_job_driver = {
    .instance_size = sizeof(TestBlockJob),
    .start = test_block_job_run,
};

S
Stefan Hajnoczi 已提交
81 82 83 84 85 86 87 88 89
/* Create a block job that completes with a given return code after a given
 * number of event loop iterations.  The return code is stored in the given
 * result pointer.
 *
 * The event loop iterations can either be handled automatically with a 0 delay
 * timer, or they can be stepped manually by entering the coroutine.
 */
static BlockJob *test_block_job_start(unsigned int iterations,
                                      bool use_timer,
90
                                      int rc, int *result, BlockJobTxn *txn)
S
Stefan Hajnoczi 已提交
91 92 93 94
{
    BlockDriverState *bs;
    TestBlockJob *s;
    TestBlockJobCBData *data;
95 96
    static unsigned counter;
    char job_id[24];
S
Stefan Hajnoczi 已提交
97 98

    data = g_new0(TestBlockJobCBData, 1);
99 100 101 102

    bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
    g_assert_nonnull(bs);

103
    snprintf(job_id, sizeof(job_id), "job%u", counter++);
104
    s = block_job_create(job_id, &test_block_job_driver, txn, bs,
105 106
                         0, BLK_PERM_ALL, 0, BLOCK_JOB_DEFAULT,
                         test_block_job_cb, data, &error_abort);
S
Stefan Hajnoczi 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    s->iterations = iterations;
    s->use_timer = use_timer;
    s->rc = rc;
    s->result = result;
    data->job = s;
    data->result = result;
    return &s->common;
}

static void test_single_job(int expected)
{
    BlockJob *job;
    BlockJobTxn *txn;
    int result = -EINPROGRESS;

    txn = block_job_txn_new();
123
    job = test_block_job_start(1, true, expected, &result, txn);
124
    block_job_start(job);
S
Stefan Hajnoczi 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    if (expected == -ECANCELED) {
        block_job_cancel(job);
    }

    while (result == -EINPROGRESS) {
        aio_poll(qemu_get_aio_context(), true);
    }
    g_assert_cmpint(result, ==, expected);

    block_job_txn_unref(txn);
}

static void test_single_job_success(void)
{
    test_single_job(0);
}

static void test_single_job_failure(void)
{
    test_single_job(-EIO);
}

static void test_single_job_cancel(void)
{
    test_single_job(-ECANCELED);
}

static void test_pair_jobs(int expected1, int expected2)
{
    BlockJob *job1;
    BlockJob *job2;
    BlockJobTxn *txn;
    int result1 = -EINPROGRESS;
    int result2 = -EINPROGRESS;

    txn = block_job_txn_new();
162 163
    job1 = test_block_job_start(1, true, expected1, &result1, txn);
    job2 = test_block_job_start(2, true, expected2, &result2, txn);
164 165
    block_job_start(job1);
    block_job_start(job2);
S
Stefan Hajnoczi 已提交
166

167 168 169 170 171
    /* Release our reference now to trigger as many nice
     * use-after-free bugs as possible.
     */
    block_job_txn_unref(txn);

S
Stefan Hajnoczi 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    if (expected1 == -ECANCELED) {
        block_job_cancel(job1);
    }
    if (expected2 == -ECANCELED) {
        block_job_cancel(job2);
    }

    while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
        aio_poll(qemu_get_aio_context(), true);
    }

    /* Failure or cancellation of one job cancels the other job */
    if (expected1 != 0) {
        expected2 = -ECANCELED;
    } else if (expected2 != 0) {
        expected1 = -ECANCELED;
    }

    g_assert_cmpint(result1, ==, expected1);
    g_assert_cmpint(result2, ==, expected2);
}

static void test_pair_jobs_success(void)
{
    test_pair_jobs(0, 0);
}

static void test_pair_jobs_failure(void)
{
    /* Test both orderings.  The two jobs run for a different number of
     * iterations so the code path is different depending on which job fails
     * first.
     */
    test_pair_jobs(-EIO, 0);
    test_pair_jobs(0, -EIO);
}

static void test_pair_jobs_cancel(void)
{
    test_pair_jobs(-ECANCELED, 0);
    test_pair_jobs(0, -ECANCELED);
}

static void test_pair_jobs_fail_cancel_race(void)
{
    BlockJob *job1;
    BlockJob *job2;
    BlockJobTxn *txn;
    int result1 = -EINPROGRESS;
    int result2 = -EINPROGRESS;

    txn = block_job_txn_new();
224 225
    job1 = test_block_job_start(1, true, -ECANCELED, &result1, txn);
    job2 = test_block_job_start(2, false, 0, &result2, txn);
226 227
    block_job_start(job1);
    block_job_start(job2);
S
Stefan Hajnoczi 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

    block_job_cancel(job1);

    /* Now make job2 finish before the main loop kicks jobs.  This simulates
     * the race between a pending kick and another job completing.
     */
    block_job_enter(job2);
    block_job_enter(job2);

    while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
        aio_poll(qemu_get_aio_context(), true);
    }

    g_assert_cmpint(result1, ==, -ECANCELED);
    g_assert_cmpint(result2, ==, -ECANCELED);

    block_job_txn_unref(txn);
}

int main(int argc, char **argv)
{
    qemu_init_main_loop(&error_abort);
250
    bdrv_init();
S
Stefan Hajnoczi 已提交
251 252 253 254 255 256 257 258 259 260 261

    g_test_init(&argc, &argv, NULL);
    g_test_add_func("/single/success", test_single_job_success);
    g_test_add_func("/single/failure", test_single_job_failure);
    g_test_add_func("/single/cancel", test_single_job_cancel);
    g_test_add_func("/pair/success", test_pair_jobs_success);
    g_test_add_func("/pair/failure", test_pair_jobs_failure);
    g_test_add_func("/pair/cancel", test_pair_jobs_cancel);
    g_test_add_func("/pair/fail-cancel-race", test_pair_jobs_fail_cancel_race);
    return g_test_run();
}