blkdebug.c 19.9 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
/*
 * 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.
 */

P
Peter Maydell 已提交
25
#include "qemu/osdep.h"
26
#include "qapi/error.h"
27
#include "qemu/cutils.h"
28
#include "qemu/config-file.h"
29
#include "block/block_int.h"
30
#include "qemu/module.h"
31 32 33 34
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qint.h"
#include "qapi/qmp/qstring.h"
35
#include "sysemu/qtest.h"
K
Kevin Wolf 已提交
36 37

typedef struct BDRVBlkdebugState {
38
    int state;
39
    int new_state;
40
    int align;
41

42 43 44
    /* For blkdebug_refresh_filename() */
    char *config_file;

45
    QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
46
    QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
47
    QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
K
Kevin Wolf 已提交
48 49
} BDRVBlkdebugState;

K
Kevin Wolf 已提交
50
typedef struct BlkdebugAIOCB {
51
    BlockAIOCB common;
K
Kevin Wolf 已提交
52 53 54
    int ret;
} BlkdebugAIOCB;

55 56 57 58 59 60
typedef struct BlkdebugSuspendedReq {
    Coroutine *co;
    char *tag;
    QLIST_ENTRY(BlkdebugSuspendedReq) next;
} BlkdebugSuspendedReq;

K
Kevin Wolf 已提交
61 62 63
enum {
    ACTION_INJECT_ERROR,
    ACTION_SET_STATE,
64
    ACTION_SUSPEND,
K
Kevin Wolf 已提交
65 66 67
};

typedef struct BlkdebugRule {
68
    BlkdebugEvent event;
K
Kevin Wolf 已提交
69 70 71 72 73 74 75
    int action;
    int state;
    union {
        struct {
            int error;
            int immediately;
            int once;
76
            int64_t offset;
K
Kevin Wolf 已提交
77 78 79 80
        } inject;
        struct {
            int new_state;
        } set_state;
81 82 83
        struct {
            char *tag;
        } suspend;
K
Kevin Wolf 已提交
84 85
    } options;
    QLIST_ENTRY(BlkdebugRule) next;
86
    QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
K
Kevin Wolf 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
} 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,
        },
105 106 107 108
        {
            .name = "sector",
            .type = QEMU_OPT_NUMBER,
        },
K
Kevin Wolf 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
        {
            .name = "once",
            .type = QEMU_OPT_BOOL,
        },
        {
            .name = "immediately",
            .type = QEMU_OPT_BOOL,
        },
        { /* end of list */ }
    },
};

static QemuOptsList set_state_opts = {
    .name = "set-state",
123
    .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
K
Kevin Wolf 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    .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
};

147
static int get_event_by_name(const char *name, BlkdebugEvent *event)
K
Kevin Wolf 已提交
148 149 150
{
    int i;

151
    for (i = 0; i < BLKDBG__MAX; i++) {
152
        if (!strcmp(BlkdebugEvent_lookup[i], name)) {
K
Kevin Wolf 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
            *event = i;
            return 0;
        }
    }

    return -1;
}

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

166
static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
K
Kevin Wolf 已提交
167 168 169 170
{
    struct add_rule_data *d = opaque;
    BDRVBlkdebugState *s = d->s;
    const char* event_name;
171
    BlkdebugEvent event;
K
Kevin Wolf 已提交
172
    struct BlkdebugRule *rule;
173
    int64_t sector;
K
Kevin Wolf 已提交
174 175 176

    /* Find the right event for the rule */
    event_name = qemu_opt_get(opts, "event");
177
    if (!event_name) {
178
        error_setg(errp, "Missing event name for rule");
179 180
        return -1;
    } else if (get_event_by_name(event_name, &event) < 0) {
181
        error_setg(errp, "Invalid event name \"%s\"", event_name);
K
Kevin Wolf 已提交
182 183 184 185
        return -1;
    }

    /* Set attributes common for all actions */
186
    rule = g_malloc0(sizeof(*rule));
K
Kevin Wolf 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199
    *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);
200 201 202
        sector = qemu_opt_get_number(opts, "sector", -1);
        rule->options.inject.offset =
            sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
K
Kevin Wolf 已提交
203 204 205 206 207 208
        break;

    case ACTION_SET_STATE:
        rule->options.set_state.new_state =
            qemu_opt_get_number(opts, "new_state", 0);
        break;
209 210 211 212 213

    case ACTION_SUSPEND:
        rule->options.suspend.tag =
            g_strdup(qemu_opt_get(opts, "tag"));
        break;
K
Kevin Wolf 已提交
214 215 216 217 218 219 220 221
    };

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

    return 0;
}

K
Kevin Wolf 已提交
222 223 224 225 226 227
static void remove_rule(BlkdebugRule *rule)
{
    switch (rule->action) {
    case ACTION_INJECT_ERROR:
    case ACTION_SET_STATE:
        break;
228 229 230
    case ACTION_SUSPEND:
        g_free(rule->options.suspend.tag);
        break;
K
Kevin Wolf 已提交
231 232 233 234 235 236
    }

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

237 238
static int read_config(BDRVBlkdebugState *s, const char *filename,
                       QDict *options, Error **errp)
K
Kevin Wolf 已提交
239
{
M
Max Reitz 已提交
240
    FILE *f = NULL;
K
Kevin Wolf 已提交
241 242
    int ret;
    struct add_rule_data d;
243
    Error *local_err = NULL;
K
Kevin Wolf 已提交
244

M
Max Reitz 已提交
245 246 247 248 249 250
    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 已提交
251

M
Max Reitz 已提交
252 253 254 255 256 257
        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 已提交
258 259
    }

260
    qemu_config_parse_qdict(options, config_groups, &local_err);
261
    if (local_err) {
262 263 264 265 266
        error_propagate(errp, local_err);
        ret = -EINVAL;
        goto fail;
    }

K
Kevin Wolf 已提交
267 268
    d.s = s;
    d.action = ACTION_INJECT_ERROR;
269
    qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
270 271 272 273 274
    if (local_err) {
        error_propagate(errp, local_err);
        ret = -EINVAL;
        goto fail;
    }
K
Kevin Wolf 已提交
275 276

    d.action = ACTION_SET_STATE;
277
    qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
278 279 280 281 282
    if (local_err) {
        error_propagate(errp, local_err);
        ret = -EINVAL;
        goto fail;
    }
K
Kevin Wolf 已提交
283 284 285

    ret = 0;
fail:
286 287
    qemu_opts_reset(&inject_error_opts);
    qemu_opts_reset(&set_state_opts);
M
Max Reitz 已提交
288 289 290
    if (f) {
        fclose(f);
    }
K
Kevin Wolf 已提交
291 292 293 294
    return ret;
}

/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
295 296
static void blkdebug_parse_filename(const char *filename, QDict *options,
                                    Error **errp)
K
Kevin Wolf 已提交
297
{
298
    const char *c;
K
Kevin Wolf 已提交
299

K
Kevin Wolf 已提交
300
    /* Parse the blkdebug: prefix */
301
    if (!strstart(filename, "blkdebug:", &filename)) {
302 303 304
        /* 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));
305
        return;
K
Kevin Wolf 已提交
306 307
    }

308
    /* Parse config file path */
K
Kevin Wolf 已提交
309 310
    c = strchr(filename, ':');
    if (c == NULL) {
311 312
        error_setg(errp, "blkdebug requires both config file and image path");
        return;
K
Kevin Wolf 已提交
313 314
    }

315 316 317 318
    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 已提交
319
    }
320 321

    /* TODO Allow multi-level nesting and set file.filename here */
K
Kevin Wolf 已提交
322
    filename = c + 1;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    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]",
        },
340 341 342 343 344
        {
            .name = "align",
            .type = QEMU_OPT_SIZE,
            .help = "Required alignment in bytes",
        },
345 346 347 348
        { /* end of list */ }
    },
};

M
Max Reitz 已提交
349 350
static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
                         Error **errp)
351 352 353 354
{
    BDRVBlkdebugState *s = bs->opaque;
    QemuOpts *opts;
    Error *local_err = NULL;
355
    uint64_t align;
356 357
    int ret;

358
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
359
    qemu_opts_absorb_qdict(opts, options, &local_err);
360
    if (local_err) {
M
Max Reitz 已提交
361
        error_propagate(errp, local_err);
362
        ret = -EINVAL;
363
        goto out;
364 365
    }

366
    /* Read rules from config file or command line options */
367 368
    s->config_file = g_strdup(qemu_opt_get(opts, "config"));
    ret = read_config(s, s->config_file, options, errp);
M
Max Reitz 已提交
369
    if (ret) {
370
        goto out;
371
    }
K
Kevin Wolf 已提交
372

K
Kevin Wolf 已提交
373
    /* Set initial state */
374
    s->state = 1;
K
Kevin Wolf 已提交
375

376
    /* Open the image file */
K
Kevin Wolf 已提交
377 378 379 380
    bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
                               bs, &child_file, false, &local_err);
    if (local_err) {
        ret = -EINVAL;
M
Max Reitz 已提交
381
        error_propagate(errp, local_err);
382
        goto out;
K
Kevin Wolf 已提交
383 384
    }

385
    /* Set request alignment */
386 387 388 389
    align = qemu_opt_get_size(opts, "align", 0);
    if (align < INT_MAX && is_power_of_2(align)) {
        s->align = align;
    } else if (align) {
390 391
        error_setg(errp, "Invalid alignment");
        ret = -EINVAL;
392
        goto fail_unref;
393 394
    }

395
    ret = 0;
396 397 398
    goto out;

fail_unref:
K
Kevin Wolf 已提交
399
    bdrv_unref_child(bs, bs->file);
400
out:
401 402 403
    if (ret < 0) {
        g_free(s->config_file);
    }
404 405
    qemu_opts_del(opts);
    return ret;
K
Kevin Wolf 已提交
406 407
}

K
Kevin Wolf 已提交
408 409
static void error_callback_bh(void *opaque)
{
410 411
    Coroutine *co = opaque;
    qemu_coroutine_enter(co);
K
Kevin Wolf 已提交
412 413
}

414
static int inject_error(BlockDriverState *bs, BlkdebugRule *rule)
K
Kevin Wolf 已提交
415 416
{
    BDRVBlkdebugState *s = bs->opaque;
417
    int error = rule->options.inject.error;
J
John Snow 已提交
418
    bool immediately = rule->options.inject.immediately;
K
Kevin Wolf 已提交
419

420
    if (rule->options.inject.once) {
J
John Snow 已提交
421 422
        QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
        remove_rule(rule);
K
Kevin Wolf 已提交
423 424
    }

425 426 427 428
    if (!immediately) {
        aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), error_callback_bh,
                                qemu_coroutine_self());
        qemu_coroutine_yield();
K
Kevin Wolf 已提交
429 430
    }

431
    return -error;
K
Kevin Wolf 已提交
432 433
}

434 435 436
static int coroutine_fn
blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
                   QEMUIOVector *qiov, int flags)
K
Kevin Wolf 已提交
437 438
{
    BDRVBlkdebugState *s = bs->opaque;
439 440 441
    BlkdebugRule *rule = NULL;

    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
442 443 444 445 446
        uint64_t inject_offset = rule->options.inject.offset;

        if (inject_offset == -1 ||
            (inject_offset >= offset && inject_offset < offset + bytes))
        {
447 448 449
            break;
        }
    }
K
Kevin Wolf 已提交
450

451
    if (rule && rule->options.inject.error) {
452
        return inject_error(bs, rule);
K
Kevin Wolf 已提交
453 454
    }

455
    return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
K
Kevin Wolf 已提交
456 457
}

458 459 460
static int coroutine_fn
blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
                    QEMUIOVector *qiov, int flags)
K
Kevin Wolf 已提交
461 462
{
    BDRVBlkdebugState *s = bs->opaque;
463 464 465
    BlkdebugRule *rule = NULL;

    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
466 467 468 469 470
        uint64_t inject_offset = rule->options.inject.offset;

        if (inject_offset == -1 ||
            (inject_offset >= offset && inject_offset < offset + bytes))
        {
471 472 473
            break;
        }
    }
K
Kevin Wolf 已提交
474

475
    if (rule && rule->options.inject.error) {
476
        return inject_error(bs, rule);
K
Kevin Wolf 已提交
477 478
    }

479
    return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
K
Kevin Wolf 已提交
480 481
}

482
static int blkdebug_co_flush(BlockDriverState *bs)
483 484 485 486 487
{
    BDRVBlkdebugState *s = bs->opaque;
    BlkdebugRule *rule = NULL;

    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
488
        if (rule->options.inject.offset == -1) {
489 490 491 492 493
            break;
        }
    }

    if (rule && rule->options.inject.error) {
494
        return inject_error(bs, rule);
495 496
    }

497
    return bdrv_co_flush(bs->file->bs);
498 499
}

500

K
Kevin Wolf 已提交
501 502 503
static void blkdebug_close(BlockDriverState *bs)
{
    BDRVBlkdebugState *s = bs->opaque;
K
Kevin Wolf 已提交
504 505 506
    BlkdebugRule *rule, *next;
    int i;

507
    for (i = 0; i < BLKDBG__MAX; i++) {
K
Kevin Wolf 已提交
508
        QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
K
Kevin Wolf 已提交
509
            remove_rule(rule);
K
Kevin Wolf 已提交
510 511
        }
    }
512 513

    g_free(s->config_file);
K
Kevin Wolf 已提交
514 515
}

516 517 518 519 520 521 522 523 524 525 526 527 528
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);

529 530 531
    if (!qtest_enabled()) {
        printf("blkdebug: Suspended request '%s'\n", r.tag);
    }
532
    qemu_coroutine_yield();
533 534 535
    if (!qtest_enabled()) {
        printf("blkdebug: Resuming request '%s'\n", r.tag);
    }
536 537 538 539 540

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

541
static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
542
    bool injected)
K
Kevin Wolf 已提交
543 544 545 546
{
    BDRVBlkdebugState *s = bs->opaque;

    /* Only process rules for the current state */
547
    if (rule->state && rule->state != s->state) {
548
        return injected;
K
Kevin Wolf 已提交
549 550 551 552 553
    }

    /* Take the action */
    switch (rule->action) {
    case ACTION_INJECT_ERROR:
554 555 556 557 558
        if (!injected) {
            QSIMPLEQ_INIT(&s->active_rules);
            injected = true;
        }
        QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
K
Kevin Wolf 已提交
559 560 561
        break;

    case ACTION_SET_STATE:
562
        s->new_state = rule->options.set_state.new_state;
K
Kevin Wolf 已提交
563
        break;
564 565 566 567

    case ACTION_SUSPEND:
        suspend_request(bs, rule);
        break;
K
Kevin Wolf 已提交
568
    }
569
    return injected;
K
Kevin Wolf 已提交
570 571
}

572
static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
K
Kevin Wolf 已提交
573 574
{
    BDRVBlkdebugState *s = bs->opaque;
575
    struct BlkdebugRule *rule, *next;
576
    bool injected;
K
Kevin Wolf 已提交
577

578
    assert((int)event >= 0 && event < BLKDBG__MAX);
K
Kevin Wolf 已提交
579

580
    injected = false;
581
    s->new_state = s->state;
582
    QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
583
        injected = process_rule(bs, rule, injected);
K
Kevin Wolf 已提交
584
    }
585
    s->state = s->new_state;
K
Kevin Wolf 已提交
586 587
}

588 589 590 591 592
static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
                                     const char *tag)
{
    BDRVBlkdebugState *s = bs->opaque;
    struct BlkdebugRule *rule;
593
    BlkdebugEvent blkdebug_event;
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

    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;
616
    BlkdebugSuspendedReq *r, *next;
617

618
    QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
619
        if (!strcmp(r->tag, tag)) {
620
            qemu_coroutine_enter(r->co);
621 622 623 624 625 626
            return 0;
        }
    }
    return -ENOENT;
}

F
Fam Zheng 已提交
627 628 629 630
static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
                                            const char *tag)
{
    BDRVBlkdebugState *s = bs->opaque;
631
    BlkdebugSuspendedReq *r, *r_next;
F
Fam Zheng 已提交
632 633 634
    BlkdebugRule *rule, *next;
    int i, ret = -ENOENT;

635
    for (i = 0; i < BLKDBG__MAX; i++) {
F
Fam Zheng 已提交
636 637 638 639 640 641 642 643
        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;
            }
        }
    }
644
    QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
F
Fam Zheng 已提交
645
        if (!strcmp(r->tag, tag)) {
646
            qemu_coroutine_enter(r->co);
F
Fam Zheng 已提交
647 648 649 650 651
            ret = 0;
        }
    }
    return ret;
}
652 653 654 655 656 657 658 659 660 661 662 663 664 665

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;
}

666 667
static int64_t blkdebug_getlength(BlockDriverState *bs)
{
K
Kevin Wolf 已提交
668
    return bdrv_getlength(bs->file->bs);
669 670
}

K
Kevin Wolf 已提交
671 672
static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
{
K
Kevin Wolf 已提交
673
    return bdrv_truncate(bs->file->bs, offset);
K
Kevin Wolf 已提交
674 675
}

676
static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
677
{
678
    BDRVBlkdebugState *s = bs->opaque;
679
    QDict *opts;
680 681 682
    const QDictEntry *e;
    bool force_json = false;

683
    for (e = qdict_first(options); e; e = qdict_next(options, e)) {
684
        if (strcmp(qdict_entry_key(e), "config") &&
685
            strcmp(qdict_entry_key(e), "x-image"))
686 687 688 689 690
        {
            force_json = true;
            break;
        }
    }
691

K
Kevin Wolf 已提交
692
    if (force_json && !bs->file->bs->full_open_options) {
693 694 695 696 697
        /* The config file cannot be recreated, so creating a plain filename
         * is impossible */
        return;
    }

K
Kevin Wolf 已提交
698
    if (!force_json && bs->file->bs->exact_filename[0]) {
699
        snprintf(bs->exact_filename, sizeof(bs->exact_filename),
700
                 "blkdebug:%s:%s", s->config_file ?: "",
K
Kevin Wolf 已提交
701
                 bs->file->bs->exact_filename);
702 703
    }

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

K
Kevin Wolf 已提交
707 708
    QINCREF(bs->file->bs->full_open_options);
    qdict_put_obj(opts, "image", QOBJECT(bs->file->bs->full_open_options));
709

710 711
    for (e = qdict_first(options); e; e = qdict_next(options, e)) {
        if (strcmp(qdict_entry_key(e), "x-image")) {
712 713
            qobject_incref(qdict_entry_value(e));
            qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
714 715 716 717 718 719
        }
    }

    bs->full_open_options = opts;
}

720 721 722 723 724
static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
{
    BDRVBlkdebugState *s = bs->opaque;

    if (s->align) {
725
        bs->bl.request_alignment = s->align;
726 727 728
    }
}

K
Kevin Wolf 已提交
729 730 731 732 733 734
static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
                                   BlockReopenQueue *queue, Error **errp)
{
    return 0;
}

K
Kevin Wolf 已提交
735
static BlockDriver bdrv_blkdebug = {
736 737 738
    .format_name            = "blkdebug",
    .protocol_name          = "blkdebug",
    .instance_size          = sizeof(BDRVBlkdebugState),
K
Kevin Wolf 已提交
739

740 741 742
    .bdrv_parse_filename    = blkdebug_parse_filename,
    .bdrv_file_open         = blkdebug_open,
    .bdrv_close             = blkdebug_close,
K
Kevin Wolf 已提交
743
    .bdrv_reopen_prepare    = blkdebug_reopen_prepare,
744
    .bdrv_getlength         = blkdebug_getlength,
K
Kevin Wolf 已提交
745
    .bdrv_truncate          = blkdebug_truncate,
746
    .bdrv_refresh_filename  = blkdebug_refresh_filename,
747
    .bdrv_refresh_limits    = blkdebug_refresh_limits,
K
Kevin Wolf 已提交
748

749 750 751
    .bdrv_co_preadv         = blkdebug_co_preadv,
    .bdrv_co_pwritev        = blkdebug_co_pwritev,
    .bdrv_co_flush_to_disk  = blkdebug_co_flush,
K
Kevin Wolf 已提交
752

753 754
    .bdrv_debug_event           = blkdebug_debug_event,
    .bdrv_debug_breakpoint      = blkdebug_debug_breakpoint,
F
Fam Zheng 已提交
755 756
    .bdrv_debug_remove_breakpoint
                                = blkdebug_debug_remove_breakpoint,
757 758
    .bdrv_debug_resume          = blkdebug_debug_resume,
    .bdrv_debug_is_suspended    = blkdebug_debug_is_suspended,
K
Kevin Wolf 已提交
759 760 761 762 763 764 765 766
};

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

block_init(bdrv_blkdebug_init);