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