]> git.proxmox.com Git - mirror_qemu.git/blame - block/blkdebug.c
blkdebug: Allow usage without config file
[mirror_qemu.git] / block / blkdebug.c
CommitLineData
6a143727
KW
1/*
2 * Block protocol for I/O error injection
3 *
4 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu-common.h"
26#include "block_int.h"
27#include "module.h"
28
29typedef struct BDRVBlkdebugState {
571cd43e 30 int state;
8f96b5be 31 int new_state;
571cd43e
PB
32 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
33 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
6a143727
KW
34} BDRVBlkdebugState;
35
b9f66d96
KW
36typedef struct BlkdebugAIOCB {
37 BlockDriverAIOCB common;
38 QEMUBH *bh;
39 int ret;
40} BlkdebugAIOCB;
41
42static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);
43
d7331bed 44static const AIOCBInfo blkdebug_aiocb_info = {
b9f66d96
KW
45 .aiocb_size = sizeof(BlkdebugAIOCB),
46 .cancel = blkdebug_aio_cancel,
47};
48
8b9b0cc2
KW
49enum {
50 ACTION_INJECT_ERROR,
51 ACTION_SET_STATE,
52};
53
54typedef struct BlkdebugRule {
55 BlkDebugEvent event;
56 int action;
57 int state;
58 union {
59 struct {
60 int error;
61 int immediately;
62 int once;
e4780db4 63 int64_t sector;
8b9b0cc2
KW
64 } inject;
65 struct {
66 int new_state;
67 } set_state;
68 } options;
69 QLIST_ENTRY(BlkdebugRule) next;
571cd43e 70 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
8b9b0cc2
KW
71} BlkdebugRule;
72
73static QemuOptsList inject_error_opts = {
74 .name = "inject-error",
75 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
76 .desc = {
77 {
78 .name = "event",
79 .type = QEMU_OPT_STRING,
80 },
81 {
82 .name = "state",
83 .type = QEMU_OPT_NUMBER,
84 },
85 {
86 .name = "errno",
87 .type = QEMU_OPT_NUMBER,
88 },
e4780db4
PB
89 {
90 .name = "sector",
91 .type = QEMU_OPT_NUMBER,
92 },
8b9b0cc2
KW
93 {
94 .name = "once",
95 .type = QEMU_OPT_BOOL,
96 },
97 {
98 .name = "immediately",
99 .type = QEMU_OPT_BOOL,
100 },
101 { /* end of list */ }
102 },
103};
104
105static QemuOptsList set_state_opts = {
106 .name = "set-state",
327cdad4 107 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
8b9b0cc2
KW
108 .desc = {
109 {
110 .name = "event",
111 .type = QEMU_OPT_STRING,
112 },
113 {
114 .name = "state",
115 .type = QEMU_OPT_NUMBER,
116 },
117 {
118 .name = "new_state",
119 .type = QEMU_OPT_NUMBER,
120 },
121 { /* end of list */ }
122 },
123};
124
125static QemuOptsList *config_groups[] = {
126 &inject_error_opts,
127 &set_state_opts,
128 NULL
129};
130
131static const char *event_names[BLKDBG_EVENT_MAX] = {
8252278a
KW
132 [BLKDBG_L1_UPDATE] = "l1_update",
133 [BLKDBG_L1_GROW_ALLOC_TABLE] = "l1_grow.alloc_table",
134 [BLKDBG_L1_GROW_WRITE_TABLE] = "l1_grow.write_table",
135 [BLKDBG_L1_GROW_ACTIVATE_TABLE] = "l1_grow.activate_table",
136
137 [BLKDBG_L2_LOAD] = "l2_load",
138 [BLKDBG_L2_UPDATE] = "l2_update",
139 [BLKDBG_L2_UPDATE_COMPRESSED] = "l2_update_compressed",
140 [BLKDBG_L2_ALLOC_COW_READ] = "l2_alloc.cow_read",
141 [BLKDBG_L2_ALLOC_WRITE] = "l2_alloc.write",
142
8252278a 143 [BLKDBG_READ_AIO] = "read_aio",
8252278a
KW
144 [BLKDBG_READ_BACKING_AIO] = "read_backing_aio",
145 [BLKDBG_READ_COMPRESSED] = "read_compressed",
146
147 [BLKDBG_WRITE_AIO] = "write_aio",
148 [BLKDBG_WRITE_COMPRESSED] = "write_compressed",
149
150 [BLKDBG_VMSTATE_LOAD] = "vmstate_load",
151 [BLKDBG_VMSTATE_SAVE] = "vmstate_save",
152
153 [BLKDBG_COW_READ] = "cow_read",
154 [BLKDBG_COW_WRITE] = "cow_write",
155
156 [BLKDBG_REFTABLE_LOAD] = "reftable_load",
157 [BLKDBG_REFTABLE_GROW] = "reftable_grow",
158
159 [BLKDBG_REFBLOCK_LOAD] = "refblock_load",
160 [BLKDBG_REFBLOCK_UPDATE] = "refblock_update",
161 [BLKDBG_REFBLOCK_UPDATE_PART] = "refblock_update_part",
162 [BLKDBG_REFBLOCK_ALLOC] = "refblock_alloc",
163 [BLKDBG_REFBLOCK_ALLOC_HOOKUP] = "refblock_alloc.hookup",
164 [BLKDBG_REFBLOCK_ALLOC_WRITE] = "refblock_alloc.write",
165 [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS] = "refblock_alloc.write_blocks",
166 [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE] = "refblock_alloc.write_table",
167 [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE] = "refblock_alloc.switch_table",
168
169 [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc",
170 [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes",
171 [BLKDBG_CLUSTER_FREE] = "cluster_free",
8b9b0cc2
KW
172};
173
174static int get_event_by_name(const char *name, BlkDebugEvent *event)
175{
176 int i;
177
178 for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
179 if (!strcmp(event_names[i], name)) {
180 *event = i;
181 return 0;
182 }
183 }
184
185 return -1;
186}
187
188struct add_rule_data {
189 BDRVBlkdebugState *s;
190 int action;
191};
192
193static int add_rule(QemuOpts *opts, void *opaque)
194{
195 struct add_rule_data *d = opaque;
196 BDRVBlkdebugState *s = d->s;
197 const char* event_name;
198 BlkDebugEvent event;
199 struct BlkdebugRule *rule;
200
201 /* Find the right event for the rule */
202 event_name = qemu_opt_get(opts, "event");
203 if (!event_name || get_event_by_name(event_name, &event) < 0) {
204 return -1;
205 }
206
207 /* Set attributes common for all actions */
7267c094 208 rule = g_malloc0(sizeof(*rule));
8b9b0cc2
KW
209 *rule = (struct BlkdebugRule) {
210 .event = event,
211 .action = d->action,
212 .state = qemu_opt_get_number(opts, "state", 0),
213 };
214
215 /* Parse action-specific options */
216 switch (d->action) {
217 case ACTION_INJECT_ERROR:
218 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
219 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
220 rule->options.inject.immediately =
221 qemu_opt_get_bool(opts, "immediately", 0);
e4780db4 222 rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
8b9b0cc2
KW
223 break;
224
225 case ACTION_SET_STATE:
226 rule->options.set_state.new_state =
227 qemu_opt_get_number(opts, "new_state", 0);
228 break;
229 };
230
231 /* Add the rule */
232 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
233
234 return 0;
235}
236
237static int read_config(BDRVBlkdebugState *s, const char *filename)
238{
239 FILE *f;
240 int ret;
241 struct add_rule_data d;
242
312a2ba0
KW
243 /* Allow usage without config file */
244 if (!*filename) {
245 return 0;
246 }
247
8b9b0cc2
KW
248 f = fopen(filename, "r");
249 if (f == NULL) {
250 return -errno;
251 }
252
253 ret = qemu_config_parse(f, config_groups, filename);
254 if (ret < 0) {
255 goto fail;
256 }
257
258 d.s = s;
259 d.action = ACTION_INJECT_ERROR;
260 qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
261
262 d.action = ACTION_SET_STATE;
263 qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);
264
265 ret = 0;
266fail:
698f0d52
KW
267 qemu_opts_reset(&inject_error_opts);
268 qemu_opts_reset(&set_state_opts);
8b9b0cc2
KW
269 fclose(f);
270 return ret;
271}
272
273/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
6a143727
KW
274static int blkdebug_open(BlockDriverState *bs, const char *filename, int flags)
275{
276 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
277 int ret;
278 char *config, *c;
6a143727 279
8b9b0cc2 280 /* Parse the blkdebug: prefix */
6a143727
KW
281 if (strncmp(filename, "blkdebug:", strlen("blkdebug:"))) {
282 return -EINVAL;
283 }
284 filename += strlen("blkdebug:");
285
8b9b0cc2
KW
286 /* Read rules from config file */
287 c = strchr(filename, ':');
288 if (c == NULL) {
289 return -EINVAL;
290 }
291
031380d8 292 config = g_strdup(filename);
8b9b0cc2
KW
293 config[c - filename] = '\0';
294 ret = read_config(s, config);
031380d8 295 g_free(config);
8b9b0cc2
KW
296 if (ret < 0) {
297 return ret;
298 }
299 filename = c + 1;
300
8db520ce 301 /* Set initial state */
571cd43e 302 s->state = 1;
8db520ce 303
8b9b0cc2 304 /* Open the backing file */
66f82cee 305 ret = bdrv_file_open(&bs->file, filename, flags);
8b9b0cc2
KW
306 if (ret < 0) {
307 return ret;
308 }
309
310 return 0;
6a143727
KW
311}
312
b9f66d96
KW
313static void error_callback_bh(void *opaque)
314{
315 struct BlkdebugAIOCB *acb = opaque;
316 qemu_bh_delete(acb->bh);
317 acb->common.cb(acb->common.opaque, acb->ret);
318 qemu_aio_release(acb);
319}
320
321static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
322{
b666d239 323 BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
b9f66d96
KW
324 qemu_aio_release(acb);
325}
326
327static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
571cd43e 328 BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
b9f66d96
KW
329{
330 BDRVBlkdebugState *s = bs->opaque;
571cd43e 331 int error = rule->options.inject.error;
b9f66d96
KW
332 struct BlkdebugAIOCB *acb;
333 QEMUBH *bh;
334
571cd43e
PB
335 if (rule->options.inject.once) {
336 QSIMPLEQ_INIT(&s->active_rules);
b9f66d96
KW
337 }
338
571cd43e 339 if (rule->options.inject.immediately) {
b9f66d96
KW
340 return NULL;
341 }
342
d7331bed 343 acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
b9f66d96
KW
344 acb->ret = -error;
345
346 bh = qemu_bh_new(error_callback_bh, acb);
347 acb->bh = bh;
348 qemu_bh_schedule(bh);
349
b666d239 350 return &acb->common;
b9f66d96
KW
351}
352
6a143727
KW
353static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
354 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
355 BlockDriverCompletionFunc *cb, void *opaque)
356{
357 BDRVBlkdebugState *s = bs->opaque;
e4780db4
PB
358 BlkdebugRule *rule = NULL;
359
360 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
361 if (rule->options.inject.sector == -1 ||
362 (rule->options.inject.sector >= sector_num &&
363 rule->options.inject.sector < sector_num + nb_sectors)) {
364 break;
365 }
366 }
b9f66d96 367
571cd43e
PB
368 if (rule && rule->options.inject.error) {
369 return inject_error(bs, cb, opaque, rule);
b9f66d96
KW
370 }
371
368e8dd1 372 return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
6a143727
KW
373}
374
375static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
376 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
377 BlockDriverCompletionFunc *cb, void *opaque)
378{
379 BDRVBlkdebugState *s = bs->opaque;
e4780db4
PB
380 BlkdebugRule *rule = NULL;
381
382 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
383 if (rule->options.inject.sector == -1 ||
384 (rule->options.inject.sector >= sector_num &&
385 rule->options.inject.sector < sector_num + nb_sectors)) {
386 break;
387 }
388 }
b9f66d96 389
571cd43e
PB
390 if (rule && rule->options.inject.error) {
391 return inject_error(bs, cb, opaque, rule);
b9f66d96
KW
392 }
393
368e8dd1 394 return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
6a143727
KW
395}
396
397static void blkdebug_close(BlockDriverState *bs)
398{
399 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
400 BlkdebugRule *rule, *next;
401 int i;
402
403 for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
404 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
405 QLIST_REMOVE(rule, next);
7267c094 406 g_free(rule);
8b9b0cc2
KW
407 }
408 }
6a143727
KW
409}
410
571cd43e 411static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
8f96b5be 412 bool injected)
8b9b0cc2
KW
413{
414 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
415
416 /* Only process rules for the current state */
8f96b5be 417 if (rule->state && rule->state != s->state) {
571cd43e 418 return injected;
8b9b0cc2
KW
419 }
420
421 /* Take the action */
422 switch (rule->action) {
423 case ACTION_INJECT_ERROR:
571cd43e
PB
424 if (!injected) {
425 QSIMPLEQ_INIT(&s->active_rules);
426 injected = true;
427 }
428 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
8b9b0cc2
KW
429 break;
430
431 case ACTION_SET_STATE:
8f96b5be 432 s->new_state = rule->options.set_state.new_state;
8b9b0cc2
KW
433 break;
434 }
571cd43e 435 return injected;
8b9b0cc2
KW
436}
437
438static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
439{
440 BDRVBlkdebugState *s = bs->opaque;
441 struct BlkdebugRule *rule;
571cd43e 442 bool injected;
8b9b0cc2 443
95ee3914 444 assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
8b9b0cc2 445
571cd43e 446 injected = false;
8f96b5be 447 s->new_state = s->state;
8b9b0cc2 448 QLIST_FOREACH(rule, &s->rules[event], next) {
8f96b5be 449 injected = process_rule(bs, rule, injected);
8b9b0cc2 450 }
8f96b5be 451 s->state = s->new_state;
8b9b0cc2
KW
452}
453
e1302255
PB
454static int64_t blkdebug_getlength(BlockDriverState *bs)
455{
456 return bdrv_getlength(bs->file);
457}
458
6a143727
KW
459static BlockDriver bdrv_blkdebug = {
460 .format_name = "blkdebug",
461 .protocol_name = "blkdebug",
462
463 .instance_size = sizeof(BDRVBlkdebugState),
464
66f82cee 465 .bdrv_file_open = blkdebug_open,
6a143727 466 .bdrv_close = blkdebug_close,
e1302255 467 .bdrv_getlength = blkdebug_getlength,
6a143727
KW
468
469 .bdrv_aio_readv = blkdebug_aio_readv,
470 .bdrv_aio_writev = blkdebug_aio_writev,
8b9b0cc2
KW
471
472 .bdrv_debug_event = blkdebug_debug_event,
6a143727
KW
473};
474
475static void bdrv_blkdebug_init(void)
476{
477 bdrv_register(&bdrv_blkdebug);
478}
479
480block_init(bdrv_blkdebug_init);