blkdebug.c 24.3 KB
Newer Older
K
Kevin Wolf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Block protocol for I/O error injection
 *
 * Copyright (c) 2010 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-common.h"
26
#include "qemu/config-file.h"
27
#include "block/block_int.h"
28
#include "qemu/module.h"
29 30 31 32
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qint.h"
#include "qapi/qmp/qstring.h"
K
Kevin Wolf 已提交
33 34

typedef struct BDRVBlkdebugState {
35
    int state;
36
    int new_state;
37

38 39
    QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
    QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
40
    QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
K
Kevin Wolf 已提交
41 42
} BDRVBlkdebugState;

K
Kevin Wolf 已提交
43 44 45 46 47 48
typedef struct BlkdebugAIOCB {
    BlockDriverAIOCB common;
    QEMUBH *bh;
    int ret;
} BlkdebugAIOCB;

49 50 51 52 53 54
typedef struct BlkdebugSuspendedReq {
    Coroutine *co;
    char *tag;
    QLIST_ENTRY(BlkdebugSuspendedReq) next;
} BlkdebugSuspendedReq;

K
Kevin Wolf 已提交
55 56
static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);

S
Stefan Hajnoczi 已提交
57
static const AIOCBInfo blkdebug_aiocb_info = {
K
Kevin Wolf 已提交
58 59 60 61
    .aiocb_size = sizeof(BlkdebugAIOCB),
    .cancel     = blkdebug_aio_cancel,
};

K
Kevin Wolf 已提交
62 63 64
enum {
    ACTION_INJECT_ERROR,
    ACTION_SET_STATE,
65
    ACTION_SUSPEND,
K
Kevin Wolf 已提交
66 67 68 69 70 71 72 73 74 75 76
};

typedef struct BlkdebugRule {
    BlkDebugEvent event;
    int action;
    int state;
    union {
        struct {
            int error;
            int immediately;
            int once;
77
            int64_t sector;
K
Kevin Wolf 已提交
78 79 80 81
        } inject;
        struct {
            int new_state;
        } set_state;
82 83 84
        struct {
            char *tag;
        } suspend;
K
Kevin Wolf 已提交
85 86
    } options;
    QLIST_ENTRY(BlkdebugRule) next;
87
    QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
K
Kevin Wolf 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
} BlkdebugRule;

static QemuOptsList inject_error_opts = {
    .name = "inject-error",
    .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
    .desc = {
        {
            .name = "event",
            .type = QEMU_OPT_STRING,
        },
        {
            .name = "state",
            .type = QEMU_OPT_NUMBER,
        },
        {
            .name = "errno",
            .type = QEMU_OPT_NUMBER,
        },
106 107 108 109
        {
            .name = "sector",
            .type = QEMU_OPT_NUMBER,
        },
K
Kevin Wolf 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123
        {
            .name = "once",
            .type = QEMU_OPT_BOOL,
        },
        {
            .name = "immediately",
            .type = QEMU_OPT_BOOL,
        },
        { /* end of list */ }
    },
};

static QemuOptsList set_state_opts = {
    .name = "set-state",
124
    .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
K
Kevin Wolf 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    .desc = {
        {
            .name = "event",
            .type = QEMU_OPT_STRING,
        },
        {
            .name = "state",
            .type = QEMU_OPT_NUMBER,
        },
        {
            .name = "new_state",
            .type = QEMU_OPT_NUMBER,
        },
        { /* end of list */ }
    },
};

static QemuOptsList *config_groups[] = {
    &inject_error_opts,
    &set_state_opts,
    NULL
};

static const char *event_names[BLKDBG_EVENT_MAX] = {
K
Kevin Wolf 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    [BLKDBG_L1_UPDATE]                      = "l1_update",
    [BLKDBG_L1_GROW_ALLOC_TABLE]            = "l1_grow.alloc_table",
    [BLKDBG_L1_GROW_WRITE_TABLE]            = "l1_grow.write_table",
    [BLKDBG_L1_GROW_ACTIVATE_TABLE]         = "l1_grow.activate_table",

    [BLKDBG_L2_LOAD]                        = "l2_load",
    [BLKDBG_L2_UPDATE]                      = "l2_update",
    [BLKDBG_L2_UPDATE_COMPRESSED]           = "l2_update_compressed",
    [BLKDBG_L2_ALLOC_COW_READ]              = "l2_alloc.cow_read",
    [BLKDBG_L2_ALLOC_WRITE]                 = "l2_alloc.write",

    [BLKDBG_READ_AIO]                       = "read_aio",
    [BLKDBG_READ_BACKING_AIO]               = "read_backing_aio",
    [BLKDBG_READ_COMPRESSED]                = "read_compressed",

    [BLKDBG_WRITE_AIO]                      = "write_aio",
    [BLKDBG_WRITE_COMPRESSED]               = "write_compressed",

    [BLKDBG_VMSTATE_LOAD]                   = "vmstate_load",
    [BLKDBG_VMSTATE_SAVE]                   = "vmstate_save",

    [BLKDBG_COW_READ]                       = "cow_read",
    [BLKDBG_COW_WRITE]                      = "cow_write",

    [BLKDBG_REFTABLE_LOAD]                  = "reftable_load",
    [BLKDBG_REFTABLE_GROW]                  = "reftable_grow",
175
    [BLKDBG_REFTABLE_UPDATE]                = "reftable_update",
K
Kevin Wolf 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189

    [BLKDBG_REFBLOCK_LOAD]                  = "refblock_load",
    [BLKDBG_REFBLOCK_UPDATE]                = "refblock_update",
    [BLKDBG_REFBLOCK_UPDATE_PART]           = "refblock_update_part",
    [BLKDBG_REFBLOCK_ALLOC]                 = "refblock_alloc",
    [BLKDBG_REFBLOCK_ALLOC_HOOKUP]          = "refblock_alloc.hookup",
    [BLKDBG_REFBLOCK_ALLOC_WRITE]           = "refblock_alloc.write",
    [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS]    = "refblock_alloc.write_blocks",
    [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE]     = "refblock_alloc.write_table",
    [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE]    = "refblock_alloc.switch_table",

    [BLKDBG_CLUSTER_ALLOC]                  = "cluster_alloc",
    [BLKDBG_CLUSTER_ALLOC_BYTES]            = "cluster_alloc_bytes",
    [BLKDBG_CLUSTER_FREE]                   = "cluster_free",
190 191 192

    [BLKDBG_FLUSH_TO_OS]                    = "flush_to_os",
    [BLKDBG_FLUSH_TO_DISK]                  = "flush_to_disk",
193 194 195 196 197 198 199 200

    [BLKDBG_PWRITEV_RMW_HEAD]               = "pwritev_rmw.head",
    [BLKDBG_PWRITEV_RMW_AFTER_HEAD]         = "pwritev_rmw.after_head",
    [BLKDBG_PWRITEV_RMW_TAIL]               = "pwritev_rmw.tail",
    [BLKDBG_PWRITEV_RMW_AFTER_TAIL]         = "pwritev_rmw.after_tail",
    [BLKDBG_PWRITEV]                        = "pwritev",
    [BLKDBG_PWRITEV_ZERO]                   = "pwritev_zero",
    [BLKDBG_PWRITEV_DONE]                   = "pwritev_done",
K
Kevin Wolf 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
};

static int get_event_by_name(const char *name, BlkDebugEvent *event)
{
    int i;

    for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
        if (!strcmp(event_names[i], name)) {
            *event = i;
            return 0;
        }
    }

    return -1;
}

struct add_rule_data {
    BDRVBlkdebugState *s;
    int action;
};

static int add_rule(QemuOpts *opts, void *opaque)
{
    struct add_rule_data *d = opaque;
    BDRVBlkdebugState *s = d->s;
    const char* event_name;
    BlkDebugEvent event;
    struct BlkdebugRule *rule;

    /* Find the right event for the rule */
    event_name = qemu_opt_get(opts, "event");
    if (!event_name || get_event_by_name(event_name, &event) < 0) {
        return -1;
    }

    /* Set attributes common for all actions */
237
    rule = g_malloc0(sizeof(*rule));
K
Kevin Wolf 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250
    *rule = (struct BlkdebugRule) {
        .event  = event,
        .action = d->action,
        .state  = qemu_opt_get_number(opts, "state", 0),
    };

    /* Parse action-specific options */
    switch (d->action) {
    case ACTION_INJECT_ERROR:
        rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
        rule->options.inject.once  = qemu_opt_get_bool(opts, "once", 0);
        rule->options.inject.immediately =
            qemu_opt_get_bool(opts, "immediately", 0);
251
        rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
K
Kevin Wolf 已提交
252 253 254 255 256 257
        break;

    case ACTION_SET_STATE:
        rule->options.set_state.new_state =
            qemu_opt_get_number(opts, "new_state", 0);
        break;
258 259 260 261 262

    case ACTION_SUSPEND:
        rule->options.suspend.tag =
            g_strdup(qemu_opt_get(opts, "tag"));
        break;
K
Kevin Wolf 已提交
263 264 265 266 267 268 269 270
    };

    /* Add the rule */
    QLIST_INSERT_HEAD(&s->rules[event], rule, next);

    return 0;
}

K
Kevin Wolf 已提交
271 272 273 274 275 276
static void remove_rule(BlkdebugRule *rule)
{
    switch (rule->action) {
    case ACTION_INJECT_ERROR:
    case ACTION_SET_STATE:
        break;
277 278 279
    case ACTION_SUSPEND:
        g_free(rule->options.suspend.tag);
        break;
K
Kevin Wolf 已提交
280 281 282 283 284 285
    }

    QLIST_REMOVE(rule, next);
    g_free(rule);
}

286 287
static int read_config(BDRVBlkdebugState *s, const char *filename,
                       QDict *options, Error **errp)
K
Kevin Wolf 已提交
288
{
M
Max Reitz 已提交
289
    FILE *f = NULL;
K
Kevin Wolf 已提交
290 291
    int ret;
    struct add_rule_data d;
292
    Error *local_err = NULL;
K
Kevin Wolf 已提交
293

M
Max Reitz 已提交
294 295 296 297 298 299
    if (filename) {
        f = fopen(filename, "r");
        if (f == NULL) {
            error_setg_errno(errp, errno, "Could not read blkdebug config file");
            return -errno;
        }
K
Kevin Wolf 已提交
300

M
Max Reitz 已提交
301 302 303 304 305 306
        ret = qemu_config_parse(f, config_groups, filename);
        if (ret < 0) {
            error_setg(errp, "Could not parse blkdebug config file");
            ret = -EINVAL;
            goto fail;
        }
K
Kevin Wolf 已提交
307 308
    }

309
    qemu_config_parse_qdict(options, config_groups, &local_err);
310
    if (local_err) {
311 312 313 314 315
        error_propagate(errp, local_err);
        ret = -EINVAL;
        goto fail;
    }

K
Kevin Wolf 已提交
316 317 318 319 320 321 322 323 324
    d.s = s;
    d.action = ACTION_INJECT_ERROR;
    qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);

    d.action = ACTION_SET_STATE;
    qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);

    ret = 0;
fail:
325 326
    qemu_opts_reset(&inject_error_opts);
    qemu_opts_reset(&set_state_opts);
M
Max Reitz 已提交
327 328 329
    if (f) {
        fclose(f);
    }
K
Kevin Wolf 已提交
330 331 332 333
    return ret;
}

/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
334 335
static void blkdebug_parse_filename(const char *filename, QDict *options,
                                    Error **errp)
K
Kevin Wolf 已提交
336
{
337
    const char *c;
K
Kevin Wolf 已提交
338

K
Kevin Wolf 已提交
339
    /* Parse the blkdebug: prefix */
340
    if (!strstart(filename, "blkdebug:", &filename)) {
341 342 343
        /* There was no prefix; therefore, all options have to be already
           present in the QDict (except for the filename) */
        qdict_put(options, "x-image", qstring_from_str(filename));
344
        return;
K
Kevin Wolf 已提交
345 346
    }

347
    /* Parse config file path */
K
Kevin Wolf 已提交
348 349
    c = strchr(filename, ':');
    if (c == NULL) {
350 351
        error_setg(errp, "blkdebug requires both config file and image path");
        return;
K
Kevin Wolf 已提交
352 353
    }

354 355 356 357
    if (c != filename) {
        QString *config_path;
        config_path = qstring_from_substr(filename, 0, c - filename - 1);
        qdict_put(options, "config", config_path);
K
Kevin Wolf 已提交
358
    }
359 360

    /* TODO Allow multi-level nesting and set file.filename here */
K
Kevin Wolf 已提交
361
    filename = c + 1;
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    qdict_put(options, "x-image", qstring_from_str(filename));
}

static QemuOptsList runtime_opts = {
    .name = "blkdebug",
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
    .desc = {
        {
            .name = "config",
            .type = QEMU_OPT_STRING,
            .help = "Path to the configuration file",
        },
        {
            .name = "x-image",
            .type = QEMU_OPT_STRING,
            .help = "[internal use only, will be removed]",
        },
379 380 381 382 383
        {
            .name = "align",
            .type = QEMU_OPT_SIZE,
            .help = "Required alignment in bytes",
        },
384 385 386 387
        { /* end of list */ }
    },
};

M
Max Reitz 已提交
388 389
static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
                         Error **errp)
390 391 392 393
{
    BDRVBlkdebugState *s = bs->opaque;
    QemuOpts *opts;
    Error *local_err = NULL;
394
    const char *config;
395
    uint64_t align;
396 397
    int ret;

398
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
399
    qemu_opts_absorb_qdict(opts, options, &local_err);
400
    if (local_err) {
M
Max Reitz 已提交
401
        error_propagate(errp, local_err);
402
        ret = -EINVAL;
403
        goto out;
404 405
    }

406
    /* Read rules from config file or command line options */
407
    config = qemu_opt_get(opts, "config");
408
    ret = read_config(s, config, options, errp);
M
Max Reitz 已提交
409
    if (ret) {
410
        goto out;
411
    }
K
Kevin Wolf 已提交
412

K
Kevin Wolf 已提交
413
    /* Set initial state */
414
    s->state = 1;
K
Kevin Wolf 已提交
415

K
Kevin Wolf 已提交
416
    /* Open the backing file */
417
    assert(bs->file == NULL);
418
    ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-image"), options, "image",
419
                          flags | BDRV_O_PROTOCOL, false, &local_err);
K
Kevin Wolf 已提交
420
    if (ret < 0) {
M
Max Reitz 已提交
421
        error_propagate(errp, local_err);
422
        goto out;
K
Kevin Wolf 已提交
423 424
    }

425 426 427 428 429 430 431
    /* Set request alignment */
    align = qemu_opt_get_size(opts, "align", bs->request_alignment);
    if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
        bs->request_alignment = align;
    } else {
        error_setg(errp, "Invalid alignment");
        ret = -EINVAL;
432
        goto fail_unref;
433 434
    }

435
    ret = 0;
436 437 438 439 440
    goto out;

fail_unref:
    bdrv_unref(bs->file);
out:
441 442
    qemu_opts_del(opts);
    return ret;
K
Kevin Wolf 已提交
443 444
}

K
Kevin Wolf 已提交
445 446 447 448 449 450 451 452 453 454
static void error_callback_bh(void *opaque)
{
    struct BlkdebugAIOCB *acb = opaque;
    qemu_bh_delete(acb->bh);
    acb->common.cb(acb->common.opaque, acb->ret);
    qemu_aio_release(acb);
}

static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
{
455
    BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
456 457 458 459
    if (acb->bh) {
        qemu_bh_delete(acb->bh);
        acb->bh = NULL;
    }
K
Kevin Wolf 已提交
460 461 462 463
    qemu_aio_release(acb);
}

static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
464
    BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
K
Kevin Wolf 已提交
465 466
{
    BDRVBlkdebugState *s = bs->opaque;
467
    int error = rule->options.inject.error;
K
Kevin Wolf 已提交
468 469 470
    struct BlkdebugAIOCB *acb;
    QEMUBH *bh;

471 472
    if (rule->options.inject.once) {
        QSIMPLEQ_INIT(&s->active_rules);
K
Kevin Wolf 已提交
473 474
    }

475
    if (rule->options.inject.immediately) {
K
Kevin Wolf 已提交
476 477 478
        return NULL;
    }

S
Stefan Hajnoczi 已提交
479
    acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
K
Kevin Wolf 已提交
480 481
    acb->ret = -error;

482
    bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
K
Kevin Wolf 已提交
483 484 485
    acb->bh = bh;
    qemu_bh_schedule(bh);

486
    return &acb->common;
K
Kevin Wolf 已提交
487 488
}

K
Kevin Wolf 已提交
489 490 491 492 493
static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
    BlockDriverCompletionFunc *cb, void *opaque)
{
    BDRVBlkdebugState *s = bs->opaque;
494 495 496 497 498 499 500 501 502
    BlkdebugRule *rule = NULL;

    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
        if (rule->options.inject.sector == -1 ||
            (rule->options.inject.sector >= sector_num &&
             rule->options.inject.sector < sector_num + nb_sectors)) {
            break;
        }
    }
K
Kevin Wolf 已提交
503

504 505
    if (rule && rule->options.inject.error) {
        return inject_error(bs, cb, opaque, rule);
K
Kevin Wolf 已提交
506 507
    }

P
Paolo Bonzini 已提交
508
    return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
K
Kevin Wolf 已提交
509 510 511 512 513 514 515
}

static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
    BlockDriverCompletionFunc *cb, void *opaque)
{
    BDRVBlkdebugState *s = bs->opaque;
516 517 518 519 520 521 522 523 524
    BlkdebugRule *rule = NULL;

    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
        if (rule->options.inject.sector == -1 ||
            (rule->options.inject.sector >= sector_num &&
             rule->options.inject.sector < sector_num + nb_sectors)) {
            break;
        }
    }
K
Kevin Wolf 已提交
525

526 527
    if (rule && rule->options.inject.error) {
        return inject_error(bs, cb, opaque, rule);
K
Kevin Wolf 已提交
528 529
    }

P
Paolo Bonzini 已提交
530
    return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
K
Kevin Wolf 已提交
531 532
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
static BlockDriverAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
    BlockDriverCompletionFunc *cb, void *opaque)
{
    BDRVBlkdebugState *s = bs->opaque;
    BlkdebugRule *rule = NULL;

    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
        if (rule->options.inject.sector == -1) {
            break;
        }
    }

    if (rule && rule->options.inject.error) {
        return inject_error(bs, cb, opaque, rule);
    }

    return bdrv_aio_flush(bs->file, cb, opaque);
}

552

K
Kevin Wolf 已提交
553 554 555
static void blkdebug_close(BlockDriverState *bs)
{
    BDRVBlkdebugState *s = bs->opaque;
K
Kevin Wolf 已提交
556 557 558 559 560
    BlkdebugRule *rule, *next;
    int i;

    for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
        QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
K
Kevin Wolf 已提交
561
            remove_rule(rule);
K
Kevin Wolf 已提交
562 563
        }
    }
K
Kevin Wolf 已提交
564 565
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
{
    BDRVBlkdebugState *s = bs->opaque;
    BlkdebugSuspendedReq r;

    r = (BlkdebugSuspendedReq) {
        .co         = qemu_coroutine_self(),
        .tag        = g_strdup(rule->options.suspend.tag),
    };

    remove_rule(rule);
    QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);

    printf("blkdebug: Suspended request '%s'\n", r.tag);
    qemu_coroutine_yield();
    printf("blkdebug: Resuming request '%s'\n", r.tag);

    QLIST_REMOVE(&r, next);
    g_free(r.tag);
}

587
static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
588
    bool injected)
K
Kevin Wolf 已提交
589 590 591 592
{
    BDRVBlkdebugState *s = bs->opaque;

    /* Only process rules for the current state */
593
    if (rule->state && rule->state != s->state) {
594
        return injected;
K
Kevin Wolf 已提交
595 596 597 598 599
    }

    /* Take the action */
    switch (rule->action) {
    case ACTION_INJECT_ERROR:
600 601 602 603 604
        if (!injected) {
            QSIMPLEQ_INIT(&s->active_rules);
            injected = true;
        }
        QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
K
Kevin Wolf 已提交
605 606 607
        break;

    case ACTION_SET_STATE:
608
        s->new_state = rule->options.set_state.new_state;
K
Kevin Wolf 已提交
609
        break;
610 611 612 613

    case ACTION_SUSPEND:
        suspend_request(bs, rule);
        break;
K
Kevin Wolf 已提交
614
    }
615
    return injected;
K
Kevin Wolf 已提交
616 617 618 619 620
}

static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
{
    BDRVBlkdebugState *s = bs->opaque;
621
    struct BlkdebugRule *rule, *next;
622
    bool injected;
K
Kevin Wolf 已提交
623

B
Blue Swirl 已提交
624
    assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
K
Kevin Wolf 已提交
625

626
    injected = false;
627
    s->new_state = s->state;
628
    QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
629
        injected = process_rule(bs, rule, injected);
K
Kevin Wolf 已提交
630
    }
631
    s->state = s->new_state;
K
Kevin Wolf 已提交
632 633
}

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
                                     const char *tag)
{
    BDRVBlkdebugState *s = bs->opaque;
    struct BlkdebugRule *rule;
    BlkDebugEvent blkdebug_event;

    if (get_event_by_name(event, &blkdebug_event) < 0) {
        return -ENOENT;
    }


    rule = g_malloc(sizeof(*rule));
    *rule = (struct BlkdebugRule) {
        .event  = blkdebug_event,
        .action = ACTION_SUSPEND,
        .state  = 0,
        .options.suspend.tag = g_strdup(tag),
    };

    QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);

    return 0;
}

static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
{
    BDRVBlkdebugState *s = bs->opaque;
662
    BlkdebugSuspendedReq *r, *next;
663

664
    QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
665 666 667 668 669 670 671 672
        if (!strcmp(r->tag, tag)) {
            qemu_coroutine_enter(r->co, NULL);
            return 0;
        }
    }
    return -ENOENT;
}

F
Fam Zheng 已提交
673 674 675 676
static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
                                            const char *tag)
{
    BDRVBlkdebugState *s = bs->opaque;
677
    BlkdebugSuspendedReq *r, *r_next;
F
Fam Zheng 已提交
678 679 680 681 682 683 684 685 686 687 688 689
    BlkdebugRule *rule, *next;
    int i, ret = -ENOENT;

    for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
        QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
            if (rule->action == ACTION_SUSPEND &&
                !strcmp(rule->options.suspend.tag, tag)) {
                remove_rule(rule);
                ret = 0;
            }
        }
    }
690
    QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
F
Fam Zheng 已提交
691 692 693 694 695 696 697
        if (!strcmp(r->tag, tag)) {
            qemu_coroutine_enter(r->co, NULL);
            ret = 0;
        }
    }
    return ret;
}
698 699 700 701 702 703 704 705 706 707 708 709 710 711

static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
{
    BDRVBlkdebugState *s = bs->opaque;
    BlkdebugSuspendedReq *r;

    QLIST_FOREACH(r, &s->suspended_reqs, next) {
        if (!strcmp(r->tag, tag)) {
            return true;
        }
    }
    return false;
}

712 713 714 715 716
static int64_t blkdebug_getlength(BlockDriverState *bs)
{
    return bdrv_getlength(bs->file);
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
static void blkdebug_refresh_filename(BlockDriverState *bs)
{
    BDRVBlkdebugState *s = bs->opaque;
    struct BlkdebugRule *rule;
    QDict *opts;
    QList *inject_error_list = NULL, *set_state_list = NULL;
    QList *suspend_list = NULL;
    int event;

    if (!bs->file->full_open_options) {
        /* The config file cannot be recreated, so creating a plain filename
         * is impossible */
        return;
    }

    opts = qdict_new();
    qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));

    QINCREF(bs->file->full_open_options);
    qdict_put_obj(opts, "image", QOBJECT(bs->file->full_open_options));

    for (event = 0; event < BLKDBG_EVENT_MAX; event++) {
        QLIST_FOREACH(rule, &s->rules[event], next) {
            if (rule->action == ACTION_INJECT_ERROR) {
                QDict *inject_error = qdict_new();

                qdict_put_obj(inject_error, "event", QOBJECT(qstring_from_str(
                              BlkdebugEvent_lookup[rule->event])));
                qdict_put_obj(inject_error, "state",
                              QOBJECT(qint_from_int(rule->state)));
                qdict_put_obj(inject_error, "errno", QOBJECT(qint_from_int(
                              rule->options.inject.error)));
                qdict_put_obj(inject_error, "sector", QOBJECT(qint_from_int(
                              rule->options.inject.sector)));
                qdict_put_obj(inject_error, "once", QOBJECT(qbool_from_int(
                              rule->options.inject.once)));
                qdict_put_obj(inject_error, "immediately",
                              QOBJECT(qbool_from_int(
                              rule->options.inject.immediately)));

                if (!inject_error_list) {
                    inject_error_list = qlist_new();
                }

                qlist_append_obj(inject_error_list, QOBJECT(inject_error));
            } else if (rule->action == ACTION_SET_STATE) {
                QDict *set_state = qdict_new();

                qdict_put_obj(set_state, "event", QOBJECT(qstring_from_str(
                              BlkdebugEvent_lookup[rule->event])));
                qdict_put_obj(set_state, "state",
                              QOBJECT(qint_from_int(rule->state)));
                qdict_put_obj(set_state, "new_state", QOBJECT(qint_from_int(
                              rule->options.set_state.new_state)));

                if (!set_state_list) {
                    set_state_list = qlist_new();
                }

                qlist_append_obj(set_state_list, QOBJECT(set_state));
            } else if (rule->action == ACTION_SUSPEND) {
                QDict *suspend = qdict_new();

                qdict_put_obj(suspend, "event", QOBJECT(qstring_from_str(
                              BlkdebugEvent_lookup[rule->event])));
                qdict_put_obj(suspend, "state",
                              QOBJECT(qint_from_int(rule->state)));
                qdict_put_obj(suspend, "tag", QOBJECT(qstring_from_str(
                              rule->options.suspend.tag)));

                if (!suspend_list) {
                    suspend_list = qlist_new();
                }

                qlist_append_obj(suspend_list, QOBJECT(suspend));
            }
        }
    }

    if (inject_error_list) {
        qdict_put_obj(opts, "inject-error", QOBJECT(inject_error_list));
    }
    if (set_state_list) {
        qdict_put_obj(opts, "set-state", QOBJECT(set_state_list));
    }
    if (suspend_list) {
        qdict_put_obj(opts, "suspend", QOBJECT(suspend_list));
    }

    bs->full_open_options = opts;
}

K
Kevin Wolf 已提交
809
static BlockDriver bdrv_blkdebug = {
810 811 812
    .format_name            = "blkdebug",
    .protocol_name          = "blkdebug",
    .instance_size          = sizeof(BDRVBlkdebugState),
K
Kevin Wolf 已提交
813

814 815 816 817
    .bdrv_parse_filename    = blkdebug_parse_filename,
    .bdrv_file_open         = blkdebug_open,
    .bdrv_close             = blkdebug_close,
    .bdrv_getlength         = blkdebug_getlength,
818
    .bdrv_refresh_filename  = blkdebug_refresh_filename,
K
Kevin Wolf 已提交
819

820 821
    .bdrv_aio_readv         = blkdebug_aio_readv,
    .bdrv_aio_writev        = blkdebug_aio_writev,
822
    .bdrv_aio_flush         = blkdebug_aio_flush,
K
Kevin Wolf 已提交
823

824 825
    .bdrv_debug_event           = blkdebug_debug_event,
    .bdrv_debug_breakpoint      = blkdebug_debug_breakpoint,
F
Fam Zheng 已提交
826 827
    .bdrv_debug_remove_breakpoint
                                = blkdebug_debug_remove_breakpoint,
828 829
    .bdrv_debug_resume          = blkdebug_debug_resume,
    .bdrv_debug_is_suspended    = blkdebug_debug_is_suspended,
K
Kevin Wolf 已提交
830 831 832 833 834 835 836 837
};

static void bdrv_blkdebug_init(void)
{
    bdrv_register(&bdrv_blkdebug);
}

block_init(bdrv_blkdebug_init);