]> Git Repo - qemu.git/blame - block/blkdebug.c
qcow2: move qcow2_store_persistent_dirty_bitmaps() before cache flushing
[qemu.git] / block / blkdebug.c
CommitLineData
6a143727
KW
1/*
2 * Block protocol for I/O error injection
3 *
63188c24 4 * Copyright (C) 2016-2017 Red Hat, Inc.
6a143727
KW
5 * Copyright (c) 2010 Kevin Wolf <[email protected]>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
80c71a24 26#include "qemu/osdep.h"
da34e65c 27#include "qapi/error.h"
f348b6d1 28#include "qemu/cutils.h"
1de7afc9 29#include "qemu/config-file.h"
737e150e 30#include "block/block_int.h"
1de7afc9 31#include "qemu/module.h"
2c31b04c
HR
32#include "qapi/qmp/qbool.h"
33#include "qapi/qmp/qdict.h"
2c31b04c 34#include "qapi/qmp/qstring.h"
20873526 35#include "sysemu/qtest.h"
6a143727
KW
36
37typedef struct BDRVBlkdebugState {
571cd43e 38 int state;
8f96b5be 39 int new_state;
3dc834f8 40 uint64_t align;
430b26a8
EB
41 uint64_t max_transfer;
42 uint64_t opt_write_zero;
43 uint64_t max_write_zero;
44 uint64_t opt_discard;
45 uint64_t max_discard;
3c90c65d 46
036990d7
HR
47 /* For blkdebug_refresh_filename() */
48 char *config_file;
49
7fb1cf16 50 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
571cd43e 51 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
3c90c65d 52 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
6a143727
KW
53} BDRVBlkdebugState;
54
b9f66d96 55typedef struct BlkdebugAIOCB {
7c84b1b8 56 BlockAIOCB common;
b9f66d96
KW
57 int ret;
58} BlkdebugAIOCB;
59
3c90c65d
KW
60typedef struct BlkdebugSuspendedReq {
61 Coroutine *co;
62 char *tag;
63 QLIST_ENTRY(BlkdebugSuspendedReq) next;
64} BlkdebugSuspendedReq;
65
8b9b0cc2
KW
66enum {
67 ACTION_INJECT_ERROR,
68 ACTION_SET_STATE,
3c90c65d 69 ACTION_SUSPEND,
8b9b0cc2
KW
70};
71
72typedef struct BlkdebugRule {
a31939e6 73 BlkdebugEvent event;
8b9b0cc2
KW
74 int action;
75 int state;
76 union {
77 struct {
78 int error;
79 int immediately;
80 int once;
7c3a9985 81 int64_t offset;
8b9b0cc2
KW
82 } inject;
83 struct {
84 int new_state;
85 } set_state;
3c90c65d
KW
86 struct {
87 char *tag;
88 } suspend;
8b9b0cc2
KW
89 } options;
90 QLIST_ENTRY(BlkdebugRule) next;
571cd43e 91 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
8b9b0cc2
KW
92} BlkdebugRule;
93
94static QemuOptsList inject_error_opts = {
95 .name = "inject-error",
96 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
97 .desc = {
98 {
99 .name = "event",
100 .type = QEMU_OPT_STRING,
101 },
102 {
103 .name = "state",
104 .type = QEMU_OPT_NUMBER,
105 },
106 {
107 .name = "errno",
108 .type = QEMU_OPT_NUMBER,
109 },
e4780db4
PB
110 {
111 .name = "sector",
112 .type = QEMU_OPT_NUMBER,
113 },
8b9b0cc2
KW
114 {
115 .name = "once",
116 .type = QEMU_OPT_BOOL,
117 },
118 {
119 .name = "immediately",
120 .type = QEMU_OPT_BOOL,
121 },
122 { /* end of list */ }
123 },
124};
125
126static QemuOptsList set_state_opts = {
127 .name = "set-state",
327cdad4 128 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
8b9b0cc2
KW
129 .desc = {
130 {
131 .name = "event",
132 .type = QEMU_OPT_STRING,
133 },
134 {
135 .name = "state",
136 .type = QEMU_OPT_NUMBER,
137 },
138 {
139 .name = "new_state",
140 .type = QEMU_OPT_NUMBER,
141 },
142 { /* end of list */ }
143 },
144};
145
146static QemuOptsList *config_groups[] = {
147 &inject_error_opts,
148 &set_state_opts,
149 NULL
150};
151
8b9b0cc2
KW
152struct add_rule_data {
153 BDRVBlkdebugState *s;
154 int action;
155};
156
28d0de7a 157static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
8b9b0cc2
KW
158{
159 struct add_rule_data *d = opaque;
160 BDRVBlkdebugState *s = d->s;
161 const char* event_name;
f9509d15 162 int event;
8b9b0cc2 163 struct BlkdebugRule *rule;
7c3a9985 164 int64_t sector;
8b9b0cc2
KW
165
166 /* Find the right event for the rule */
167 event_name = qemu_opt_get(opts, "event");
d4362d64 168 if (!event_name) {
8809cfc3 169 error_setg(errp, "Missing event name for rule");
d4362d64 170 return -1;
f9509d15 171 }
f7abe0ec 172 event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp);
f9509d15 173 if (event < 0) {
8b9b0cc2
KW
174 return -1;
175 }
176
177 /* Set attributes common for all actions */
7267c094 178 rule = g_malloc0(sizeof(*rule));
8b9b0cc2
KW
179 *rule = (struct BlkdebugRule) {
180 .event = event,
181 .action = d->action,
182 .state = qemu_opt_get_number(opts, "state", 0),
183 };
184
185 /* Parse action-specific options */
186 switch (d->action) {
187 case ACTION_INJECT_ERROR:
188 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
189 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
190 rule->options.inject.immediately =
191 qemu_opt_get_bool(opts, "immediately", 0);
7c3a9985
KW
192 sector = qemu_opt_get_number(opts, "sector", -1);
193 rule->options.inject.offset =
194 sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
8b9b0cc2
KW
195 break;
196
197 case ACTION_SET_STATE:
198 rule->options.set_state.new_state =
199 qemu_opt_get_number(opts, "new_state", 0);
200 break;
3c90c65d
KW
201
202 case ACTION_SUSPEND:
203 rule->options.suspend.tag =
204 g_strdup(qemu_opt_get(opts, "tag"));
205 break;
8b9b0cc2
KW
206 };
207
208 /* Add the rule */
209 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
210
211 return 0;
212}
213
9e35542b
KW
214static void remove_rule(BlkdebugRule *rule)
215{
216 switch (rule->action) {
217 case ACTION_INJECT_ERROR:
218 case ACTION_SET_STATE:
219 break;
3c90c65d
KW
220 case ACTION_SUSPEND:
221 g_free(rule->options.suspend.tag);
222 break;
9e35542b
KW
223 }
224
225 QLIST_REMOVE(rule, next);
226 g_free(rule);
227}
228
89f2b21e
HR
229static int read_config(BDRVBlkdebugState *s, const char *filename,
230 QDict *options, Error **errp)
8b9b0cc2 231{
85a040e5 232 FILE *f = NULL;
8b9b0cc2
KW
233 int ret;
234 struct add_rule_data d;
89f2b21e 235 Error *local_err = NULL;
8b9b0cc2 236
85a040e5
HR
237 if (filename) {
238 f = fopen(filename, "r");
239 if (f == NULL) {
240 error_setg_errno(errp, errno, "Could not read blkdebug config file");
241 return -errno;
242 }
8b9b0cc2 243
85a040e5
HR
244 ret = qemu_config_parse(f, config_groups, filename);
245 if (ret < 0) {
246 error_setg(errp, "Could not parse blkdebug config file");
247 ret = -EINVAL;
248 goto fail;
249 }
8b9b0cc2
KW
250 }
251
89f2b21e 252 qemu_config_parse_qdict(options, config_groups, &local_err);
84d18f06 253 if (local_err) {
89f2b21e
HR
254 error_propagate(errp, local_err);
255 ret = -EINVAL;
256 goto fail;
257 }
258
8b9b0cc2
KW
259 d.s = s;
260 d.action = ACTION_INJECT_ERROR;
8809cfc3 261 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
d4362d64
SH
262 if (local_err) {
263 error_propagate(errp, local_err);
264 ret = -EINVAL;
265 goto fail;
266 }
8b9b0cc2
KW
267
268 d.action = ACTION_SET_STATE;
8809cfc3 269 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
d4362d64
SH
270 if (local_err) {
271 error_propagate(errp, local_err);
272 ret = -EINVAL;
273 goto fail;
274 }
8b9b0cc2
KW
275
276 ret = 0;
277fail:
698f0d52
KW
278 qemu_opts_reset(&inject_error_opts);
279 qemu_opts_reset(&set_state_opts);
85a040e5
HR
280 if (f) {
281 fclose(f);
282 }
8b9b0cc2
KW
283 return ret;
284}
285
286/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
f4681212
KW
287static void blkdebug_parse_filename(const char *filename, QDict *options,
288 Error **errp)
6a143727 289{
f4681212 290 const char *c;
6a143727 291
8b9b0cc2 292 /* Parse the blkdebug: prefix */
f4681212 293 if (!strstart(filename, "blkdebug:", &filename)) {
d4881b9b
HR
294 /* There was no prefix; therefore, all options have to be already
295 present in the QDict (except for the filename) */
46f5ac20 296 qdict_put_str(options, "x-image", filename);
f4681212 297 return;
6a143727 298 }
6a143727 299
f4681212 300 /* Parse config file path */
8b9b0cc2
KW
301 c = strchr(filename, ':');
302 if (c == NULL) {
f4681212
KW
303 error_setg(errp, "blkdebug requires both config file and image path");
304 return;
8b9b0cc2
KW
305 }
306
f4681212
KW
307 if (c != filename) {
308 QString *config_path;
309 config_path = qstring_from_substr(filename, 0, c - filename - 1);
310 qdict_put(options, "config", config_path);
8b9b0cc2 311 }
f4681212
KW
312
313 /* TODO Allow multi-level nesting and set file.filename here */
8b9b0cc2 314 filename = c + 1;
46f5ac20 315 qdict_put_str(options, "x-image", filename);
f4681212
KW
316}
317
318static QemuOptsList runtime_opts = {
319 .name = "blkdebug",
320 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
321 .desc = {
322 {
323 .name = "config",
324 .type = QEMU_OPT_STRING,
325 .help = "Path to the configuration file",
326 },
327 {
328 .name = "x-image",
329 .type = QEMU_OPT_STRING,
330 .help = "[internal use only, will be removed]",
331 },
b35ee7fb
KW
332 {
333 .name = "align",
334 .type = QEMU_OPT_SIZE,
335 .help = "Required alignment in bytes",
336 },
430b26a8
EB
337 {
338 .name = "max-transfer",
339 .type = QEMU_OPT_SIZE,
340 .help = "Maximum transfer size in bytes",
341 },
342 {
343 .name = "opt-write-zero",
344 .type = QEMU_OPT_SIZE,
345 .help = "Optimum write zero alignment in bytes",
346 },
347 {
348 .name = "max-write-zero",
349 .type = QEMU_OPT_SIZE,
350 .help = "Maximum write zero size in bytes",
351 },
352 {
353 .name = "opt-discard",
354 .type = QEMU_OPT_SIZE,
355 .help = "Optimum discard alignment in bytes",
356 },
357 {
358 .name = "max-discard",
359 .type = QEMU_OPT_SIZE,
360 .help = "Maximum discard size in bytes",
361 },
f4681212
KW
362 { /* end of list */ }
363 },
364};
365
015a1036
HR
366static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
367 Error **errp)
f4681212
KW
368{
369 BDRVBlkdebugState *s = bs->opaque;
370 QemuOpts *opts;
371 Error *local_err = NULL;
f4681212 372 int ret;
430b26a8 373 uint64_t align;
f4681212 374
87ea75d5 375 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
f4681212 376 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 377 if (local_err) {
10ffa72f 378 error_propagate(errp, local_err);
f4681212 379 ret = -EINVAL;
eaf944a4 380 goto out;
f4681212
KW
381 }
382
89f2b21e 383 /* Read rules from config file or command line options */
036990d7
HR
384 s->config_file = g_strdup(qemu_opt_get(opts, "config"));
385 ret = read_config(s, s->config_file, options, errp);
85a040e5 386 if (ret) {
eaf944a4 387 goto out;
f4681212 388 }
8b9b0cc2 389
8db520ce 390 /* Set initial state */
571cd43e 391 s->state = 1;
8db520ce 392
6b826af7 393 /* Open the image file */
9a4f4c31
KW
394 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
395 bs, &child_file, false, &local_err);
396 if (local_err) {
397 ret = -EINVAL;
10ffa72f 398 error_propagate(errp, local_err);
eaf944a4 399 goto out;
8b9b0cc2
KW
400 }
401
63188c24
EB
402 bs->supported_write_flags = BDRV_REQ_FUA &
403 bs->file->bs->supported_write_flags;
404 bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
405 bs->file->bs->supported_zero_flags;
3dc834f8 406 ret = -EINVAL;
63188c24 407
430b26a8 408 /* Set alignment overrides */
3dc834f8
EB
409 s->align = qemu_opt_get_size(opts, "align", 0);
410 if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
411 error_setg(errp, "Cannot meet constraints with align %" PRIu64,
412 s->align);
de234897 413 goto out;
b35ee7fb 414 }
430b26a8
EB
415 align = MAX(s->align, bs->file->bs->bl.request_alignment);
416
417 s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
418 if (s->max_transfer &&
419 (s->max_transfer >= INT_MAX ||
420 !QEMU_IS_ALIGNED(s->max_transfer, align))) {
421 error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
422 s->max_transfer);
423 goto out;
424 }
425
426 s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
427 if (s->opt_write_zero &&
428 (s->opt_write_zero >= INT_MAX ||
429 !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
430 error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
431 s->opt_write_zero);
432 goto out;
433 }
434
435 s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
436 if (s->max_write_zero &&
437 (s->max_write_zero >= INT_MAX ||
438 !QEMU_IS_ALIGNED(s->max_write_zero,
439 MAX(s->opt_write_zero, align)))) {
440 error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
441 s->max_write_zero);
442 goto out;
443 }
444
445 s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
446 if (s->opt_discard &&
447 (s->opt_discard >= INT_MAX ||
448 !QEMU_IS_ALIGNED(s->opt_discard, align))) {
449 error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
450 s->opt_discard);
451 goto out;
452 }
453
454 s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
455 if (s->max_discard &&
456 (s->max_discard >= INT_MAX ||
457 !QEMU_IS_ALIGNED(s->max_discard,
458 MAX(s->opt_discard, align)))) {
459 error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
460 s->max_discard);
461 goto out;
462 }
b35ee7fb 463
f4681212 464 ret = 0;
eaf944a4 465out:
036990d7
HR
466 if (ret < 0) {
467 g_free(s->config_file);
468 }
f4681212
KW
469 qemu_opts_del(opts);
470 return ret;
6a143727
KW
471}
472
d157ed5f 473static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes)
b9f66d96
KW
474{
475 BDRVBlkdebugState *s = bs->opaque;
d157ed5f
EB
476 BlkdebugRule *rule = NULL;
477 int error;
478 bool immediately;
479
480 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
481 uint64_t inject_offset = rule->options.inject.offset;
482
483 if (inject_offset == -1 ||
484 (bytes && inject_offset >= offset &&
485 inject_offset < offset + bytes))
486 {
487 break;
488 }
489 }
490
491 if (!rule || !rule->options.inject.error) {
492 return 0;
493 }
494
495 immediately = rule->options.inject.immediately;
496 error = rule->options.inject.error;
b9f66d96 497
571cd43e 498 if (rule->options.inject.once) {
a069e2f1
JS
499 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
500 remove_rule(rule);
b9f66d96
KW
501 }
502
7c3a9985 503 if (!immediately) {
e5c67ab5 504 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
7c3a9985 505 qemu_coroutine_yield();
b9f66d96
KW
506 }
507
7c3a9985 508 return -error;
b9f66d96
KW
509}
510
7c3a9985
KW
511static int coroutine_fn
512blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
513 QEMUIOVector *qiov, int flags)
6a143727 514{
d157ed5f 515 int err;
e4780db4 516
e0ef4395
EB
517 /* Sanity check block layer guarantees */
518 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
519 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
520 if (bs->bl.max_transfer) {
521 assert(bytes <= bs->bl.max_transfer);
522 }
523
d157ed5f
EB
524 err = rule_check(bs, offset, bytes);
525 if (err) {
526 return err;
b9f66d96
KW
527 }
528
7c3a9985 529 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
6a143727
KW
530}
531
7c3a9985
KW
532static int coroutine_fn
533blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
534 QEMUIOVector *qiov, int flags)
6a143727 535{
d157ed5f 536 int err;
e4780db4 537
e0ef4395
EB
538 /* Sanity check block layer guarantees */
539 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
540 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
541 if (bs->bl.max_transfer) {
542 assert(bytes <= bs->bl.max_transfer);
543 }
544
d157ed5f
EB
545 err = rule_check(bs, offset, bytes);
546 if (err) {
547 return err;
b9f66d96
KW
548 }
549
7c3a9985 550 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
6a143727
KW
551}
552
7c3a9985 553static int blkdebug_co_flush(BlockDriverState *bs)
9e52c53b 554{
d157ed5f 555 int err = rule_check(bs, 0, 0);
9e52c53b 556
d157ed5f
EB
557 if (err) {
558 return err;
9e52c53b
PB
559 }
560
7c3a9985 561 return bdrv_co_flush(bs->file->bs);
9e52c53b
PB
562}
563
63188c24 564static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
f5a5ca79 565 int64_t offset, int bytes,
63188c24
EB
566 BdrvRequestFlags flags)
567{
568 uint32_t align = MAX(bs->bl.request_alignment,
569 bs->bl.pwrite_zeroes_alignment);
570 int err;
571
572 /* Only pass through requests that are larger than requested
573 * preferred alignment (so that we test the fallback to writes on
574 * unaligned portions), and check that the block layer never hands
575 * us anything unaligned that crosses an alignment boundary. */
f5a5ca79 576 if (bytes < align) {
63188c24 577 assert(QEMU_IS_ALIGNED(offset, align) ||
f5a5ca79 578 QEMU_IS_ALIGNED(offset + bytes, align) ||
63188c24 579 DIV_ROUND_UP(offset, align) ==
f5a5ca79 580 DIV_ROUND_UP(offset + bytes, align));
63188c24
EB
581 return -ENOTSUP;
582 }
583 assert(QEMU_IS_ALIGNED(offset, align));
f5a5ca79 584 assert(QEMU_IS_ALIGNED(bytes, align));
63188c24 585 if (bs->bl.max_pwrite_zeroes) {
f5a5ca79 586 assert(bytes <= bs->bl.max_pwrite_zeroes);
63188c24
EB
587 }
588
f5a5ca79 589 err = rule_check(bs, offset, bytes);
63188c24
EB
590 if (err) {
591 return err;
592 }
593
f5a5ca79 594 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
63188c24
EB
595}
596
597static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
f5a5ca79 598 int64_t offset, int bytes)
63188c24
EB
599{
600 uint32_t align = bs->bl.pdiscard_alignment;
601 int err;
602
603 /* Only pass through requests that are larger than requested
604 * minimum alignment, and ensure that unaligned requests do not
605 * cross optimum discard boundaries. */
f5a5ca79 606 if (bytes < bs->bl.request_alignment) {
63188c24 607 assert(QEMU_IS_ALIGNED(offset, align) ||
f5a5ca79 608 QEMU_IS_ALIGNED(offset + bytes, align) ||
63188c24 609 DIV_ROUND_UP(offset, align) ==
f5a5ca79 610 DIV_ROUND_UP(offset + bytes, align));
63188c24
EB
611 return -ENOTSUP;
612 }
613 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
f5a5ca79
MP
614 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
615 if (align && bytes >= align) {
63188c24 616 assert(QEMU_IS_ALIGNED(offset, align));
f5a5ca79 617 assert(QEMU_IS_ALIGNED(bytes, align));
63188c24
EB
618 }
619 if (bs->bl.max_pdiscard) {
f5a5ca79 620 assert(bytes <= bs->bl.max_pdiscard);
63188c24
EB
621 }
622
f5a5ca79 623 err = rule_check(bs, offset, bytes);
63188c24
EB
624 if (err) {
625 return err;
626 }
627
f5a5ca79 628 return bdrv_co_pdiscard(bs->file->bs, offset, bytes);
63188c24 629}
3c90c65d 630
6a143727
KW
631static void blkdebug_close(BlockDriverState *bs)
632{
633 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
634 BlkdebugRule *rule, *next;
635 int i;
636
7fb1cf16 637 for (i = 0; i < BLKDBG__MAX; i++) {
8b9b0cc2 638 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
9e35542b 639 remove_rule(rule);
8b9b0cc2
KW
640 }
641 }
036990d7
HR
642
643 g_free(s->config_file);
6a143727
KW
644}
645
3c90c65d
KW
646static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
647{
648 BDRVBlkdebugState *s = bs->opaque;
649 BlkdebugSuspendedReq r;
650
651 r = (BlkdebugSuspendedReq) {
652 .co = qemu_coroutine_self(),
653 .tag = g_strdup(rule->options.suspend.tag),
654 };
655
656 remove_rule(rule);
657 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
658
20873526
MT
659 if (!qtest_enabled()) {
660 printf("blkdebug: Suspended request '%s'\n", r.tag);
661 }
3c90c65d 662 qemu_coroutine_yield();
20873526
MT
663 if (!qtest_enabled()) {
664 printf("blkdebug: Resuming request '%s'\n", r.tag);
665 }
3c90c65d
KW
666
667 QLIST_REMOVE(&r, next);
668 g_free(r.tag);
669}
670
571cd43e 671static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
8f96b5be 672 bool injected)
8b9b0cc2
KW
673{
674 BDRVBlkdebugState *s = bs->opaque;
8b9b0cc2
KW
675
676 /* Only process rules for the current state */
8f96b5be 677 if (rule->state && rule->state != s->state) {
571cd43e 678 return injected;
8b9b0cc2
KW
679 }
680
681 /* Take the action */
682 switch (rule->action) {
683 case ACTION_INJECT_ERROR:
571cd43e
PB
684 if (!injected) {
685 QSIMPLEQ_INIT(&s->active_rules);
686 injected = true;
687 }
688 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
8b9b0cc2
KW
689 break;
690
691 case ACTION_SET_STATE:
8f96b5be 692 s->new_state = rule->options.set_state.new_state;
8b9b0cc2 693 break;
3c90c65d
KW
694
695 case ACTION_SUSPEND:
696 suspend_request(bs, rule);
697 break;
8b9b0cc2 698 }
571cd43e 699 return injected;
8b9b0cc2
KW
700}
701
a31939e6 702static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
8b9b0cc2
KW
703{
704 BDRVBlkdebugState *s = bs->opaque;
3c90c65d 705 struct BlkdebugRule *rule, *next;
571cd43e 706 bool injected;
8b9b0cc2 707
7fb1cf16 708 assert((int)event >= 0 && event < BLKDBG__MAX);
8b9b0cc2 709
571cd43e 710 injected = false;
8f96b5be 711 s->new_state = s->state;
3c90c65d 712 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
8f96b5be 713 injected = process_rule(bs, rule, injected);
8b9b0cc2 714 }
8f96b5be 715 s->state = s->new_state;
8b9b0cc2
KW
716}
717
3c90c65d
KW
718static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
719 const char *tag)
720{
721 BDRVBlkdebugState *s = bs->opaque;
722 struct BlkdebugRule *rule;
f9509d15 723 int blkdebug_event;
3c90c65d 724
f7abe0ec 725 blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
f9509d15 726 if (blkdebug_event < 0) {
3c90c65d
KW
727 return -ENOENT;
728 }
729
3c90c65d
KW
730 rule = g_malloc(sizeof(*rule));
731 *rule = (struct BlkdebugRule) {
732 .event = blkdebug_event,
733 .action = ACTION_SUSPEND,
734 .state = 0,
735 .options.suspend.tag = g_strdup(tag),
736 };
737
738 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
739
740 return 0;
741}
742
743static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
744{
745 BDRVBlkdebugState *s = bs->opaque;
c547e564 746 BlkdebugSuspendedReq *r, *next;
3c90c65d 747
c547e564 748 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
3c90c65d 749 if (!strcmp(r->tag, tag)) {
0b8b8753 750 qemu_coroutine_enter(r->co);
3c90c65d
KW
751 return 0;
752 }
753 }
754 return -ENOENT;
755}
756
4cc70e93
FZ
757static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
758 const char *tag)
759{
760 BDRVBlkdebugState *s = bs->opaque;
c547e564 761 BlkdebugSuspendedReq *r, *r_next;
4cc70e93
FZ
762 BlkdebugRule *rule, *next;
763 int i, ret = -ENOENT;
764
7fb1cf16 765 for (i = 0; i < BLKDBG__MAX; i++) {
4cc70e93
FZ
766 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
767 if (rule->action == ACTION_SUSPEND &&
768 !strcmp(rule->options.suspend.tag, tag)) {
769 remove_rule(rule);
770 ret = 0;
771 }
772 }
773 }
c547e564 774 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
4cc70e93 775 if (!strcmp(r->tag, tag)) {
0b8b8753 776 qemu_coroutine_enter(r->co);
4cc70e93
FZ
777 ret = 0;
778 }
779 }
780 return ret;
781}
3c90c65d
KW
782
783static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
784{
785 BDRVBlkdebugState *s = bs->opaque;
786 BlkdebugSuspendedReq *r;
787
788 QLIST_FOREACH(r, &s->suspended_reqs, next) {
789 if (!strcmp(r->tag, tag)) {
790 return true;
791 }
792 }
793 return false;
794}
795
e1302255
PB
796static int64_t blkdebug_getlength(BlockDriverState *bs)
797{
9a4f4c31 798 return bdrv_getlength(bs->file->bs);
e1302255
PB
799}
800
4cdd01d3 801static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
2c31b04c 802{
036990d7 803 BDRVBlkdebugState *s = bs->opaque;
2c31b04c 804 QDict *opts;
8779441b
HR
805 const QDictEntry *e;
806 bool force_json = false;
807
4cdd01d3 808 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
8779441b 809 if (strcmp(qdict_entry_key(e), "config") &&
4cdd01d3 810 strcmp(qdict_entry_key(e), "x-image"))
8779441b
HR
811 {
812 force_json = true;
813 break;
814 }
815 }
2c31b04c 816
9a4f4c31 817 if (force_json && !bs->file->bs->full_open_options) {
2c31b04c
HR
818 /* The config file cannot be recreated, so creating a plain filename
819 * is impossible */
820 return;
821 }
822
9a4f4c31 823 if (!force_json && bs->file->bs->exact_filename[0]) {
de81d72d
HR
824 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
825 "blkdebug:%s:%s", s->config_file ?: "",
826 bs->file->bs->exact_filename);
827 if (ret >= sizeof(bs->exact_filename)) {
828 /* An overflow makes the filename unusable, so do not report any */
829 bs->exact_filename[0] = 0;
830 }
8779441b
HR
831 }
832
2c31b04c 833 opts = qdict_new();
46f5ac20 834 qdict_put_str(opts, "driver", "blkdebug");
2c31b04c 835
9a4f4c31 836 QINCREF(bs->file->bs->full_open_options);
de6e7951 837 qdict_put(opts, "image", bs->file->bs->full_open_options);
2c31b04c 838
4cdd01d3
KW
839 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
840 if (strcmp(qdict_entry_key(e), "x-image")) {
8779441b
HR
841 qobject_incref(qdict_entry_value(e));
842 qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
2c31b04c
HR
843 }
844 }
845
2c31b04c
HR
846 bs->full_open_options = opts;
847}
848
835db3ee
EB
849static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
850{
851 BDRVBlkdebugState *s = bs->opaque;
852
853 if (s->align) {
a5b8dd2c 854 bs->bl.request_alignment = s->align;
835db3ee 855 }
430b26a8
EB
856 if (s->max_transfer) {
857 bs->bl.max_transfer = s->max_transfer;
858 }
859 if (s->opt_write_zero) {
860 bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
861 }
862 if (s->max_write_zero) {
863 bs->bl.max_pwrite_zeroes = s->max_write_zero;
864 }
865 if (s->opt_discard) {
866 bs->bl.pdiscard_alignment = s->opt_discard;
867 }
868 if (s->max_discard) {
869 bs->bl.max_pdiscard = s->max_discard;
870 }
835db3ee
EB
871}
872
c5e8bfb7
KW
873static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
874 BlockReopenQueue *queue, Error **errp)
875{
876 return 0;
877}
878
6a143727 879static BlockDriver bdrv_blkdebug = {
f4681212
KW
880 .format_name = "blkdebug",
881 .protocol_name = "blkdebug",
882 .instance_size = sizeof(BDRVBlkdebugState),
d8e12cd3 883 .is_filter = true,
6a143727 884
f4681212
KW
885 .bdrv_parse_filename = blkdebug_parse_filename,
886 .bdrv_file_open = blkdebug_open,
887 .bdrv_close = blkdebug_close,
c5e8bfb7 888 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
d7010dfb
KW
889 .bdrv_child_perm = bdrv_filter_default_perms,
890
f4681212 891 .bdrv_getlength = blkdebug_getlength,
2c31b04c 892 .bdrv_refresh_filename = blkdebug_refresh_filename,
835db3ee 893 .bdrv_refresh_limits = blkdebug_refresh_limits,
6a143727 894
7c3a9985
KW
895 .bdrv_co_preadv = blkdebug_co_preadv,
896 .bdrv_co_pwritev = blkdebug_co_pwritev,
897 .bdrv_co_flush_to_disk = blkdebug_co_flush,
63188c24
EB
898 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
899 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
f7cc69b3 900 .bdrv_co_get_block_status = bdrv_co_get_block_status_from_file,
8b9b0cc2 901
3c90c65d
KW
902 .bdrv_debug_event = blkdebug_debug_event,
903 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
4cc70e93
FZ
904 .bdrv_debug_remove_breakpoint
905 = blkdebug_debug_remove_breakpoint,
3c90c65d
KW
906 .bdrv_debug_resume = blkdebug_debug_resume,
907 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
6a143727
KW
908};
909
910static void bdrv_blkdebug_init(void)
911{
912 bdrv_register(&bdrv_blkdebug);
913}
914
915block_init(bdrv_blkdebug_init);
This page took 0.550365 seconds and 4 git commands to generate.