blkdebug.c 12.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 25 26 27 28 29
/*
 * 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"
#include "block_int.h"
#include "module.h"

typedef struct BDRVBlkdebugState {
30 31 32
    int state;
    QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
    QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
K
Kevin Wolf 已提交
33 34
} BDRVBlkdebugState;

K
Kevin Wolf 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
typedef struct BlkdebugAIOCB {
    BlockDriverAIOCB common;
    QEMUBH *bh;
    int ret;
} BlkdebugAIOCB;

static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);

static AIOPool blkdebug_aio_pool = {
    .aiocb_size = sizeof(BlkdebugAIOCB),
    .cancel     = blkdebug_aio_cancel,
};

K
Kevin Wolf 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
enum {
    ACTION_INJECT_ERROR,
    ACTION_SET_STATE,
};

typedef struct BlkdebugRule {
    BlkDebugEvent event;
    int action;
    int state;
    union {
        struct {
            int error;
            int immediately;
            int once;
62
            int64_t sector;
K
Kevin Wolf 已提交
63 64 65 66 67 68
        } inject;
        struct {
            int new_state;
        } set_state;
    } options;
    QLIST_ENTRY(BlkdebugRule) next;
69
    QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
K
Kevin Wolf 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
} 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,
        },
88 89 90 91
        {
            .name = "sector",
            .type = QEMU_OPT_NUMBER,
        },
K
Kevin Wolf 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
        {
            .name = "once",
            .type = QEMU_OPT_BOOL,
        },
        {
            .name = "immediately",
            .type = QEMU_OPT_BOOL,
        },
        { /* end of list */ }
    },
};

static QemuOptsList set_state_opts = {
    .name = "set-state",
106
    .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
K
Kevin Wolf 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    .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 已提交
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 162 163 164 165 166 167 168 169 170
    [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",

    [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",
K
Kevin Wolf 已提交
171 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
};

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 */
207
    rule = g_malloc0(sizeof(*rule));
K
Kevin Wolf 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220
    *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);
221
        rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
K
Kevin Wolf 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        break;

    case ACTION_SET_STATE:
        rule->options.set_state.new_state =
            qemu_opt_get_number(opts, "new_state", 0);
        break;
    };

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

    return 0;
}

static int read_config(BDRVBlkdebugState *s, const char *filename)
{
    FILE *f;
    int ret;
    struct add_rule_data d;

    f = fopen(filename, "r");
    if (f == NULL) {
        return -errno;
    }

    ret = qemu_config_parse(f, config_groups, filename);
    if (ret < 0) {
        goto fail;
    }

    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:
261 262
    qemu_opts_reset(&inject_error_opts);
    qemu_opts_reset(&set_state_opts);
K
Kevin Wolf 已提交
263 264 265 266 267
    fclose(f);
    return ret;
}

/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
K
Kevin Wolf 已提交
268 269 270
static int blkdebug_open(BlockDriverState *bs, const char *filename, int flags)
{
    BDRVBlkdebugState *s = bs->opaque;
K
Kevin Wolf 已提交
271 272
    int ret;
    char *config, *c;
K
Kevin Wolf 已提交
273

K
Kevin Wolf 已提交
274
    /* Parse the blkdebug: prefix */
K
Kevin Wolf 已提交
275 276 277 278 279
    if (strncmp(filename, "blkdebug:", strlen("blkdebug:"))) {
        return -EINVAL;
    }
    filename += strlen("blkdebug:");

K
Kevin Wolf 已提交
280 281 282 283 284 285
    /* Read rules from config file */
    c = strchr(filename, ':');
    if (c == NULL) {
        return -EINVAL;
    }

286
    config = g_strdup(filename);
K
Kevin Wolf 已提交
287 288
    config[c - filename] = '\0';
    ret = read_config(s, config);
289
    g_free(config);
K
Kevin Wolf 已提交
290 291 292 293 294
    if (ret < 0) {
        return ret;
    }
    filename = c + 1;

K
Kevin Wolf 已提交
295
    /* Set initial state */
296
    s->state = 1;
K
Kevin Wolf 已提交
297

K
Kevin Wolf 已提交
298
    /* Open the backing file */
299
    ret = bdrv_file_open(&bs->file, filename, flags);
K
Kevin Wolf 已提交
300 301 302 303 304
    if (ret < 0) {
        return ret;
    }

    return 0;
K
Kevin Wolf 已提交
305 306
}

K
Kevin Wolf 已提交
307 308 309 310 311 312 313 314 315 316
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)
{
317
    BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
K
Kevin Wolf 已提交
318 319 320 321
    qemu_aio_release(acb);
}

static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
322
    BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
K
Kevin Wolf 已提交
323 324
{
    BDRVBlkdebugState *s = bs->opaque;
325
    int error = rule->options.inject.error;
K
Kevin Wolf 已提交
326 327 328
    struct BlkdebugAIOCB *acb;
    QEMUBH *bh;

329 330
    if (rule->options.inject.once) {
        QSIMPLEQ_INIT(&s->active_rules);
K
Kevin Wolf 已提交
331 332
    }

333
    if (rule->options.inject.immediately) {
K
Kevin Wolf 已提交
334 335 336 337 338 339 340 341 342 343
        return NULL;
    }

    acb = qemu_aio_get(&blkdebug_aio_pool, bs, cb, opaque);
    acb->ret = -error;

    bh = qemu_bh_new(error_callback_bh, acb);
    acb->bh = bh;
    qemu_bh_schedule(bh);

344
    return &acb->common;
K
Kevin Wolf 已提交
345 346
}

K
Kevin Wolf 已提交
347 348 349 350 351
static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
    BlockDriverCompletionFunc *cb, void *opaque)
{
    BDRVBlkdebugState *s = bs->opaque;
352 353 354 355 356 357 358 359 360
    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 已提交
361

362 363
    if (rule && rule->options.inject.error) {
        return inject_error(bs, cb, opaque, rule);
K
Kevin Wolf 已提交
364 365
    }

P
Paolo Bonzini 已提交
366
    return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
K
Kevin Wolf 已提交
367 368 369 370 371 372 373
}

static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
    BlockDriverCompletionFunc *cb, void *opaque)
{
    BDRVBlkdebugState *s = bs->opaque;
374 375 376 377 378 379 380 381 382
    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 已提交
383

384 385
    if (rule && rule->options.inject.error) {
        return inject_error(bs, cb, opaque, rule);
K
Kevin Wolf 已提交
386 387
    }

P
Paolo Bonzini 已提交
388
    return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
K
Kevin Wolf 已提交
389 390 391 392 393
}

static void blkdebug_close(BlockDriverState *bs)
{
    BDRVBlkdebugState *s = bs->opaque;
K
Kevin Wolf 已提交
394 395 396 397 398 399
    BlkdebugRule *rule, *next;
    int i;

    for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
        QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
            QLIST_REMOVE(rule, next);
400
            g_free(rule);
K
Kevin Wolf 已提交
401 402
        }
    }
K
Kevin Wolf 已提交
403 404
}

405 406
static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
    int old_state, bool injected)
K
Kevin Wolf 已提交
407 408 409 410
{
    BDRVBlkdebugState *s = bs->opaque;

    /* Only process rules for the current state */
411 412
    if (rule->state && rule->state != old_state) {
        return injected;
K
Kevin Wolf 已提交
413 414 415 416 417
    }

    /* Take the action */
    switch (rule->action) {
    case ACTION_INJECT_ERROR:
418 419 420 421 422
        if (!injected) {
            QSIMPLEQ_INIT(&s->active_rules);
            injected = true;
        }
        QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
K
Kevin Wolf 已提交
423 424 425
        break;

    case ACTION_SET_STATE:
426
        s->state = rule->options.set_state.new_state;
K
Kevin Wolf 已提交
427 428
        break;
    }
429
    return injected;
K
Kevin Wolf 已提交
430 431 432 433 434 435
}

static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
{
    BDRVBlkdebugState *s = bs->opaque;
    struct BlkdebugRule *rule;
436 437
    int old_state = s->state;
    bool injected;
K
Kevin Wolf 已提交
438

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

441
    injected = false;
K
Kevin Wolf 已提交
442
    QLIST_FOREACH(rule, &s->rules[event], next) {
443
        injected = process_rule(bs, rule, old_state, injected);
K
Kevin Wolf 已提交
444 445 446
    }
}

447 448 449 450 451
static int64_t blkdebug_getlength(BlockDriverState *bs)
{
    return bdrv_getlength(bs->file);
}

K
Kevin Wolf 已提交
452 453 454 455 456 457
static BlockDriver bdrv_blkdebug = {
    .format_name        = "blkdebug",
    .protocol_name      = "blkdebug",

    .instance_size      = sizeof(BDRVBlkdebugState),

458
    .bdrv_file_open     = blkdebug_open,
K
Kevin Wolf 已提交
459
    .bdrv_close         = blkdebug_close,
460
    .bdrv_getlength     = blkdebug_getlength,
K
Kevin Wolf 已提交
461 462 463

    .bdrv_aio_readv     = blkdebug_aio_readv,
    .bdrv_aio_writev    = blkdebug_aio_writev,
K
Kevin Wolf 已提交
464 465

    .bdrv_debug_event   = blkdebug_debug_event,
K
Kevin Wolf 已提交
466 467 468 469 470 471 472 473
};

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

block_init(bdrv_blkdebug_init);