Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Block protocol for I/O error injection |
| 3 | * |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 4 | * Copyright (C) 2016-2017 Red Hat, Inc. |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 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 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 27 | #include "qapi/error.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 28 | #include "qemu/cutils.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 29 | #include "qemu/config-file.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 30 | #include "block/block_int.h" |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 31 | #include "block/qdict.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 32 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 33 | #include "qemu/option.h" |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 34 | #include "qapi/qapi-visit-block-core.h" |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 35 | #include "qapi/qmp/qdict.h" |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 36 | #include "qapi/qmp/qlist.h" |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 37 | #include "qapi/qmp/qstring.h" |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 38 | #include "qapi/qobject-input-visitor.h" |
Michael S. Tsirkin | 2087352 | 2015-11-30 13:44:44 +0200 | [diff] [blame] | 39 | #include "sysemu/qtest.h" |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 40 | |
| 41 | typedef struct BDRVBlkdebugState { |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 42 | int state; |
Paolo Bonzini | 8f96b5b | 2012-09-28 17:23:00 +0200 | [diff] [blame] | 43 | int new_state; |
Eric Blake | 3dc834f | 2017-04-29 14:14:17 -0500 | [diff] [blame] | 44 | uint64_t align; |
Eric Blake | 430b26a | 2017-04-29 14:14:18 -0500 | [diff] [blame] | 45 | uint64_t max_transfer; |
| 46 | uint64_t opt_write_zero; |
| 47 | uint64_t max_write_zero; |
| 48 | uint64_t opt_discard; |
| 49 | uint64_t max_discard; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 50 | |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 51 | uint64_t take_child_perms; |
| 52 | uint64_t unshare_child_perms; |
| 53 | |
Max Reitz | 036990d | 2016-08-15 15:29:25 +0200 | [diff] [blame] | 54 | /* For blkdebug_refresh_filename() */ |
| 55 | char *config_file; |
| 56 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 57 | QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX]; |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 58 | QSIMPLEQ_HEAD(, BlkdebugRule) active_rules; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 59 | QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs; |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 60 | } BDRVBlkdebugState; |
| 61 | |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 62 | typedef struct BlkdebugAIOCB { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 63 | BlockAIOCB common; |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 64 | int ret; |
| 65 | } BlkdebugAIOCB; |
| 66 | |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 67 | typedef struct BlkdebugSuspendedReq { |
| 68 | Coroutine *co; |
| 69 | char *tag; |
| 70 | QLIST_ENTRY(BlkdebugSuspendedReq) next; |
| 71 | } BlkdebugSuspendedReq; |
| 72 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 73 | enum { |
| 74 | ACTION_INJECT_ERROR, |
| 75 | ACTION_SET_STATE, |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 76 | ACTION_SUSPEND, |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | typedef struct BlkdebugRule { |
Eric Blake | a31939e | 2015-11-18 01:52:54 -0700 | [diff] [blame] | 80 | BlkdebugEvent event; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 81 | int action; |
| 82 | int state; |
| 83 | union { |
| 84 | struct { |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 85 | uint64_t iotype_mask; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 86 | int error; |
| 87 | int immediately; |
| 88 | int once; |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 89 | int64_t offset; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 90 | } inject; |
| 91 | struct { |
| 92 | int new_state; |
| 93 | } set_state; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 94 | struct { |
| 95 | char *tag; |
| 96 | } suspend; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 97 | } options; |
| 98 | QLIST_ENTRY(BlkdebugRule) next; |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 99 | QSIMPLEQ_ENTRY(BlkdebugRule) active_next; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 100 | } BlkdebugRule; |
| 101 | |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 102 | QEMU_BUILD_BUG_MSG(BLKDEBUG_IO_TYPE__MAX > 64, |
| 103 | "BlkdebugIOType mask does not fit into an uint64_t"); |
| 104 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 105 | static QemuOptsList inject_error_opts = { |
| 106 | .name = "inject-error", |
| 107 | .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head), |
| 108 | .desc = { |
| 109 | { |
| 110 | .name = "event", |
| 111 | .type = QEMU_OPT_STRING, |
| 112 | }, |
| 113 | { |
| 114 | .name = "state", |
| 115 | .type = QEMU_OPT_NUMBER, |
| 116 | }, |
| 117 | { |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 118 | .name = "iotype", |
| 119 | .type = QEMU_OPT_STRING, |
| 120 | }, |
| 121 | { |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 122 | .name = "errno", |
| 123 | .type = QEMU_OPT_NUMBER, |
| 124 | }, |
| 125 | { |
Paolo Bonzini | e4780db | 2012-06-06 08:10:43 +0200 | [diff] [blame] | 126 | .name = "sector", |
| 127 | .type = QEMU_OPT_NUMBER, |
| 128 | }, |
| 129 | { |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 130 | .name = "once", |
| 131 | .type = QEMU_OPT_BOOL, |
| 132 | }, |
| 133 | { |
| 134 | .name = "immediately", |
| 135 | .type = QEMU_OPT_BOOL, |
| 136 | }, |
| 137 | { /* end of list */ } |
| 138 | }, |
| 139 | }; |
| 140 | |
| 141 | static QemuOptsList set_state_opts = { |
| 142 | .name = "set-state", |
Kevin Wolf | 327cdad | 2010-06-30 17:40:42 +0200 | [diff] [blame] | 143 | .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head), |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 144 | .desc = { |
| 145 | { |
| 146 | .name = "event", |
| 147 | .type = QEMU_OPT_STRING, |
| 148 | }, |
| 149 | { |
| 150 | .name = "state", |
| 151 | .type = QEMU_OPT_NUMBER, |
| 152 | }, |
| 153 | { |
| 154 | .name = "new_state", |
| 155 | .type = QEMU_OPT_NUMBER, |
| 156 | }, |
| 157 | { /* end of list */ } |
| 158 | }, |
| 159 | }; |
| 160 | |
| 161 | static QemuOptsList *config_groups[] = { |
| 162 | &inject_error_opts, |
| 163 | &set_state_opts, |
| 164 | NULL |
| 165 | }; |
| 166 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 167 | struct add_rule_data { |
| 168 | BDRVBlkdebugState *s; |
| 169 | int action; |
| 170 | }; |
| 171 | |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 172 | static int add_rule(void *opaque, QemuOpts *opts, Error **errp) |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 173 | { |
| 174 | struct add_rule_data *d = opaque; |
| 175 | BDRVBlkdebugState *s = d->s; |
shiliyang | 5f14f31 | 2020-10-30 11:35:12 +0800 | [diff] [blame] | 176 | const char *event_name; |
Marc-André Lureau | f9509d1 | 2017-08-24 10:46:02 +0200 | [diff] [blame] | 177 | int event; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 178 | struct BlkdebugRule *rule; |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 179 | int64_t sector; |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 180 | BlkdebugIOType iotype; |
| 181 | Error *local_error = NULL; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 182 | |
| 183 | /* Find the right event for the rule */ |
| 184 | event_name = qemu_opt_get(opts, "event"); |
Stefan Hajnoczi | d4362d6 | 2014-09-20 09:55:52 +0100 | [diff] [blame] | 185 | if (!event_name) { |
Markus Armbruster | 8809cfc | 2015-03-13 13:38:42 +0100 | [diff] [blame] | 186 | error_setg(errp, "Missing event name for rule"); |
Stefan Hajnoczi | d4362d6 | 2014-09-20 09:55:52 +0100 | [diff] [blame] | 187 | return -1; |
Marc-André Lureau | f9509d1 | 2017-08-24 10:46:02 +0200 | [diff] [blame] | 188 | } |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 189 | event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp); |
Marc-André Lureau | f9509d1 | 2017-08-24 10:46:02 +0200 | [diff] [blame] | 190 | if (event < 0) { |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | /* Set attributes common for all actions */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 195 | rule = g_malloc0(sizeof(*rule)); |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 196 | *rule = (struct BlkdebugRule) { |
| 197 | .event = event, |
| 198 | .action = d->action, |
| 199 | .state = qemu_opt_get_number(opts, "state", 0), |
| 200 | }; |
| 201 | |
| 202 | /* Parse action-specific options */ |
| 203 | switch (d->action) { |
| 204 | case ACTION_INJECT_ERROR: |
| 205 | rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO); |
| 206 | rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); |
| 207 | rule->options.inject.immediately = |
| 208 | qemu_opt_get_bool(opts, "immediately", 0); |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 209 | sector = qemu_opt_get_number(opts, "sector", -1); |
| 210 | rule->options.inject.offset = |
| 211 | sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE; |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 212 | |
| 213 | iotype = qapi_enum_parse(&BlkdebugIOType_lookup, |
| 214 | qemu_opt_get(opts, "iotype"), |
| 215 | BLKDEBUG_IO_TYPE__MAX, &local_error); |
| 216 | if (local_error) { |
| 217 | error_propagate(errp, local_error); |
Elena Afanasova | 5b4c95d | 2020-10-09 12:09:59 -0700 | [diff] [blame] | 218 | g_free(rule); |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 219 | return -1; |
| 220 | } |
| 221 | if (iotype != BLKDEBUG_IO_TYPE__MAX) { |
| 222 | rule->options.inject.iotype_mask = (1ull << iotype); |
| 223 | } else { |
| 224 | /* Apply the default */ |
| 225 | rule->options.inject.iotype_mask = |
| 226 | (1ull << BLKDEBUG_IO_TYPE_READ) |
| 227 | | (1ull << BLKDEBUG_IO_TYPE_WRITE) |
| 228 | | (1ull << BLKDEBUG_IO_TYPE_WRITE_ZEROES) |
| 229 | | (1ull << BLKDEBUG_IO_TYPE_DISCARD) |
| 230 | | (1ull << BLKDEBUG_IO_TYPE_FLUSH); |
| 231 | } |
| 232 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 233 | break; |
| 234 | |
| 235 | case ACTION_SET_STATE: |
| 236 | rule->options.set_state.new_state = |
| 237 | qemu_opt_get_number(opts, "new_state", 0); |
| 238 | break; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 239 | |
| 240 | case ACTION_SUSPEND: |
| 241 | rule->options.suspend.tag = |
| 242 | g_strdup(qemu_opt_get(opts, "tag")); |
| 243 | break; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | /* Add the rule */ |
| 247 | QLIST_INSERT_HEAD(&s->rules[event], rule, next); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Kevin Wolf | 9e35542 | 2012-12-06 14:32:56 +0100 | [diff] [blame] | 252 | static void remove_rule(BlkdebugRule *rule) |
| 253 | { |
| 254 | switch (rule->action) { |
| 255 | case ACTION_INJECT_ERROR: |
| 256 | case ACTION_SET_STATE: |
| 257 | break; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 258 | case ACTION_SUSPEND: |
| 259 | g_free(rule->options.suspend.tag); |
| 260 | break; |
Kevin Wolf | 9e35542 | 2012-12-06 14:32:56 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | QLIST_REMOVE(rule, next); |
| 264 | g_free(rule); |
| 265 | } |
| 266 | |
Max Reitz | 89f2b21 | 2013-12-20 19:28:07 +0100 | [diff] [blame] | 267 | static int read_config(BDRVBlkdebugState *s, const char *filename, |
| 268 | QDict *options, Error **errp) |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 269 | { |
Max Reitz | 85a040e | 2013-12-20 19:28:06 +0100 | [diff] [blame] | 270 | FILE *f = NULL; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 271 | int ret; |
| 272 | struct add_rule_data d; |
Max Reitz | 89f2b21 | 2013-12-20 19:28:07 +0100 | [diff] [blame] | 273 | Error *local_err = NULL; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 274 | |
Max Reitz | 85a040e | 2013-12-20 19:28:06 +0100 | [diff] [blame] | 275 | if (filename) { |
| 276 | f = fopen(filename, "r"); |
| 277 | if (f == NULL) { |
| 278 | error_setg_errno(errp, errno, "Could not read blkdebug config file"); |
| 279 | return -errno; |
| 280 | } |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 281 | |
Max Reitz | 85a040e | 2013-12-20 19:28:06 +0100 | [diff] [blame] | 282 | ret = qemu_config_parse(f, config_groups, filename); |
| 283 | if (ret < 0) { |
| 284 | error_setg(errp, "Could not parse blkdebug config file"); |
Max Reitz | 85a040e | 2013-12-20 19:28:06 +0100 | [diff] [blame] | 285 | goto fail; |
| 286 | } |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Max Reitz | 89f2b21 | 2013-12-20 19:28:07 +0100 | [diff] [blame] | 289 | qemu_config_parse_qdict(options, config_groups, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 290 | if (local_err) { |
Max Reitz | 89f2b21 | 2013-12-20 19:28:07 +0100 | [diff] [blame] | 291 | error_propagate(errp, local_err); |
| 292 | ret = -EINVAL; |
| 293 | goto fail; |
| 294 | } |
| 295 | |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 296 | d.s = s; |
| 297 | d.action = ACTION_INJECT_ERROR; |
Markus Armbruster | 8809cfc | 2015-03-13 13:38:42 +0100 | [diff] [blame] | 298 | qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err); |
Stefan Hajnoczi | d4362d6 | 2014-09-20 09:55:52 +0100 | [diff] [blame] | 299 | if (local_err) { |
| 300 | error_propagate(errp, local_err); |
| 301 | ret = -EINVAL; |
| 302 | goto fail; |
| 303 | } |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 304 | |
| 305 | d.action = ACTION_SET_STATE; |
Markus Armbruster | 8809cfc | 2015-03-13 13:38:42 +0100 | [diff] [blame] | 306 | qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err); |
Stefan Hajnoczi | d4362d6 | 2014-09-20 09:55:52 +0100 | [diff] [blame] | 307 | if (local_err) { |
| 308 | error_propagate(errp, local_err); |
| 309 | ret = -EINVAL; |
| 310 | goto fail; |
| 311 | } |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 312 | |
| 313 | ret = 0; |
| 314 | fail: |
Kevin Wolf | 698f0d5 | 2010-06-30 17:42:23 +0200 | [diff] [blame] | 315 | qemu_opts_reset(&inject_error_opts); |
| 316 | qemu_opts_reset(&set_state_opts); |
Max Reitz | 85a040e | 2013-12-20 19:28:06 +0100 | [diff] [blame] | 317 | if (f) { |
| 318 | fclose(f); |
| 319 | } |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */ |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 324 | static void blkdebug_parse_filename(const char *filename, QDict *options, |
| 325 | Error **errp) |
| 326 | { |
| 327 | const char *c; |
| 328 | |
| 329 | /* Parse the blkdebug: prefix */ |
| 330 | if (!strstart(filename, "blkdebug:", &filename)) { |
Max Reitz | d4881b9 | 2013-12-20 19:28:02 +0100 | [diff] [blame] | 331 | /* There was no prefix; therefore, all options have to be already |
| 332 | present in the QDict (except for the filename) */ |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 333 | qdict_put_str(options, "x-image", filename); |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
| 337 | /* Parse config file path */ |
| 338 | c = strchr(filename, ':'); |
| 339 | if (c == NULL) { |
| 340 | error_setg(errp, "blkdebug requires both config file and image path"); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if (c != filename) { |
| 345 | QString *config_path; |
Markus Armbruster | ba891d6 | 2018-07-27 08:22:04 +0200 | [diff] [blame] | 346 | config_path = qstring_from_substr(filename, 0, c - filename); |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 347 | qdict_put(options, "config", config_path); |
| 348 | } |
| 349 | |
| 350 | /* TODO Allow multi-level nesting and set file.filename here */ |
| 351 | filename = c + 1; |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 352 | qdict_put_str(options, "x-image", filename); |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 355 | static int blkdebug_parse_perm_list(uint64_t *dest, QDict *options, |
| 356 | const char *prefix, Error **errp) |
| 357 | { |
| 358 | int ret = 0; |
| 359 | QDict *subqdict = NULL; |
| 360 | QObject *crumpled_subqdict = NULL; |
| 361 | Visitor *v = NULL; |
| 362 | BlockPermissionList *perm_list = NULL, *element; |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 363 | |
| 364 | *dest = 0; |
| 365 | |
| 366 | qdict_extract_subqdict(options, &subqdict, prefix); |
| 367 | if (!qdict_size(subqdict)) { |
| 368 | goto out; |
| 369 | } |
| 370 | |
| 371 | crumpled_subqdict = qdict_crumple(subqdict, errp); |
| 372 | if (!crumpled_subqdict) { |
| 373 | ret = -EINVAL; |
| 374 | goto out; |
| 375 | } |
| 376 | |
| 377 | v = qobject_input_visitor_new(crumpled_subqdict); |
Markus Armbruster | af175e8 | 2020-07-07 18:06:03 +0200 | [diff] [blame] | 378 | if (!visit_type_BlockPermissionList(v, NULL, &perm_list, errp)) { |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 379 | ret = -EINVAL; |
| 380 | goto out; |
| 381 | } |
| 382 | |
| 383 | for (element = perm_list; element; element = element->next) { |
| 384 | *dest |= bdrv_qapi_perm_to_blk_perm(element->value); |
| 385 | } |
| 386 | |
| 387 | out: |
| 388 | qapi_free_BlockPermissionList(perm_list); |
| 389 | visit_free(v); |
| 390 | qobject_unref(subqdict); |
| 391 | qobject_unref(crumpled_subqdict); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | static int blkdebug_parse_perms(BDRVBlkdebugState *s, QDict *options, |
| 396 | Error **errp) |
| 397 | { |
| 398 | int ret; |
| 399 | |
| 400 | ret = blkdebug_parse_perm_list(&s->take_child_perms, options, |
| 401 | "take-child-perms.", errp); |
| 402 | if (ret < 0) { |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | ret = blkdebug_parse_perm_list(&s->unshare_child_perms, options, |
| 407 | "unshare-child-perms.", errp); |
| 408 | if (ret < 0) { |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 415 | static QemuOptsList runtime_opts = { |
| 416 | .name = "blkdebug", |
| 417 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 418 | .desc = { |
| 419 | { |
| 420 | .name = "config", |
| 421 | .type = QEMU_OPT_STRING, |
| 422 | .help = "Path to the configuration file", |
| 423 | }, |
| 424 | { |
| 425 | .name = "x-image", |
| 426 | .type = QEMU_OPT_STRING, |
| 427 | .help = "[internal use only, will be removed]", |
| 428 | }, |
Kevin Wolf | b35ee7f | 2014-01-14 13:44:35 +0100 | [diff] [blame] | 429 | { |
| 430 | .name = "align", |
| 431 | .type = QEMU_OPT_SIZE, |
| 432 | .help = "Required alignment in bytes", |
| 433 | }, |
Eric Blake | 430b26a | 2017-04-29 14:14:18 -0500 | [diff] [blame] | 434 | { |
| 435 | .name = "max-transfer", |
| 436 | .type = QEMU_OPT_SIZE, |
| 437 | .help = "Maximum transfer size in bytes", |
| 438 | }, |
| 439 | { |
| 440 | .name = "opt-write-zero", |
| 441 | .type = QEMU_OPT_SIZE, |
| 442 | .help = "Optimum write zero alignment in bytes", |
| 443 | }, |
| 444 | { |
| 445 | .name = "max-write-zero", |
| 446 | .type = QEMU_OPT_SIZE, |
| 447 | .help = "Maximum write zero size in bytes", |
| 448 | }, |
| 449 | { |
| 450 | .name = "opt-discard", |
| 451 | .type = QEMU_OPT_SIZE, |
| 452 | .help = "Optimum discard alignment in bytes", |
| 453 | }, |
| 454 | { |
| 455 | .name = "max-discard", |
| 456 | .type = QEMU_OPT_SIZE, |
| 457 | .help = "Maximum discard size in bytes", |
| 458 | }, |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 459 | { /* end of list */ } |
| 460 | }, |
| 461 | }; |
| 462 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 463 | static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, |
| 464 | Error **errp) |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 465 | { |
| 466 | BDRVBlkdebugState *s = bs->opaque; |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 467 | QemuOpts *opts; |
| 468 | Error *local_err = NULL; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 469 | int ret; |
Eric Blake | 430b26a | 2017-04-29 14:14:18 -0500 | [diff] [blame] | 470 | uint64_t align; |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 471 | |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 472 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Markus Armbruster | af175e8 | 2020-07-07 18:06:03 +0200 | [diff] [blame] | 473 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 474 | ret = -EINVAL; |
Kevin Wolf | eaf944a | 2014-02-08 09:53:22 +0100 | [diff] [blame] | 475 | goto out; |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 476 | } |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 477 | |
Max Reitz | 89f2b21 | 2013-12-20 19:28:07 +0100 | [diff] [blame] | 478 | /* Read rules from config file or command line options */ |
Max Reitz | 036990d | 2016-08-15 15:29:25 +0200 | [diff] [blame] | 479 | s->config_file = g_strdup(qemu_opt_get(opts, "config")); |
| 480 | ret = read_config(s, s->config_file, options, errp); |
Max Reitz | 85a040e | 2013-12-20 19:28:06 +0100 | [diff] [blame] | 481 | if (ret) { |
Kevin Wolf | eaf944a | 2014-02-08 09:53:22 +0100 | [diff] [blame] | 482 | goto out; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Kevin Wolf | 8db520c | 2010-06-30 17:43:40 +0200 | [diff] [blame] | 485 | /* Set initial state */ |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 486 | s->state = 1; |
Kevin Wolf | 8db520c | 2010-06-30 17:43:40 +0200 | [diff] [blame] | 487 | |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 488 | /* Parse permissions modifiers before opening the image file */ |
| 489 | ret = blkdebug_parse_perms(s, options, errp); |
| 490 | if (ret < 0) { |
| 491 | goto out; |
| 492 | } |
| 493 | |
Fam Zheng | 6b826af | 2015-10-16 18:46:04 +0800 | [diff] [blame] | 494 | /* Open the image file */ |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 495 | bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image", |
Max Reitz | b3af2af | 2020-05-13 13:05:36 +0200 | [diff] [blame] | 496 | bs, &child_of_bds, |
| 497 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, |
| 498 | false, &local_err); |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 499 | if (local_err) { |
| 500 | ret = -EINVAL; |
Max Reitz | 10ffa72 | 2013-10-10 15:44:03 +0200 | [diff] [blame] | 501 | error_propagate(errp, local_err); |
Kevin Wolf | eaf944a | 2014-02-08 09:53:22 +0100 | [diff] [blame] | 502 | goto out; |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Max Reitz | 228345b | 2018-04-21 15:29:26 +0200 | [diff] [blame] | 505 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED | |
| 506 | (BDRV_REQ_FUA & bs->file->bs->supported_write_flags); |
| 507 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED | |
Kevin Wolf | 80f5c33 | 2019-03-22 13:42:39 +0100 | [diff] [blame] | 508 | ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) & |
Max Reitz | 228345b | 2018-04-21 15:29:26 +0200 | [diff] [blame] | 509 | bs->file->bs->supported_zero_flags); |
Eric Blake | 3dc834f | 2017-04-29 14:14:17 -0500 | [diff] [blame] | 510 | ret = -EINVAL; |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 511 | |
Eric Blake | 430b26a | 2017-04-29 14:14:18 -0500 | [diff] [blame] | 512 | /* Set alignment overrides */ |
Eric Blake | 3dc834f | 2017-04-29 14:14:17 -0500 | [diff] [blame] | 513 | s->align = qemu_opt_get_size(opts, "align", 0); |
| 514 | if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) { |
| 515 | error_setg(errp, "Cannot meet constraints with align %" PRIu64, |
| 516 | s->align); |
Max Reitz | de23489 | 2017-04-13 17:43:34 +0200 | [diff] [blame] | 517 | goto out; |
Kevin Wolf | b35ee7f | 2014-01-14 13:44:35 +0100 | [diff] [blame] | 518 | } |
Eric Blake | 430b26a | 2017-04-29 14:14:18 -0500 | [diff] [blame] | 519 | align = MAX(s->align, bs->file->bs->bl.request_alignment); |
| 520 | |
| 521 | s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0); |
| 522 | if (s->max_transfer && |
| 523 | (s->max_transfer >= INT_MAX || |
| 524 | !QEMU_IS_ALIGNED(s->max_transfer, align))) { |
| 525 | error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64, |
| 526 | s->max_transfer); |
| 527 | goto out; |
| 528 | } |
| 529 | |
| 530 | s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0); |
| 531 | if (s->opt_write_zero && |
| 532 | (s->opt_write_zero >= INT_MAX || |
| 533 | !QEMU_IS_ALIGNED(s->opt_write_zero, align))) { |
| 534 | error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64, |
| 535 | s->opt_write_zero); |
| 536 | goto out; |
| 537 | } |
| 538 | |
| 539 | s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0); |
| 540 | if (s->max_write_zero && |
| 541 | (s->max_write_zero >= INT_MAX || |
| 542 | !QEMU_IS_ALIGNED(s->max_write_zero, |
| 543 | MAX(s->opt_write_zero, align)))) { |
| 544 | error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64, |
| 545 | s->max_write_zero); |
| 546 | goto out; |
| 547 | } |
| 548 | |
| 549 | s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0); |
| 550 | if (s->opt_discard && |
| 551 | (s->opt_discard >= INT_MAX || |
| 552 | !QEMU_IS_ALIGNED(s->opt_discard, align))) { |
| 553 | error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64, |
| 554 | s->opt_discard); |
| 555 | goto out; |
| 556 | } |
| 557 | |
| 558 | s->max_discard = qemu_opt_get_size(opts, "max-discard", 0); |
| 559 | if (s->max_discard && |
| 560 | (s->max_discard >= INT_MAX || |
| 561 | !QEMU_IS_ALIGNED(s->max_discard, |
| 562 | MAX(s->opt_discard, align)))) { |
| 563 | error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64, |
| 564 | s->max_discard); |
| 565 | goto out; |
| 566 | } |
Kevin Wolf | b35ee7f | 2014-01-14 13:44:35 +0100 | [diff] [blame] | 567 | |
Max Reitz | f8cec15 | 2019-05-07 22:35:05 +0200 | [diff] [blame] | 568 | bdrv_debug_event(bs, BLKDBG_NONE); |
| 569 | |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 570 | ret = 0; |
Kevin Wolf | eaf944a | 2014-02-08 09:53:22 +0100 | [diff] [blame] | 571 | out: |
Max Reitz | 036990d | 2016-08-15 15:29:25 +0200 | [diff] [blame] | 572 | if (ret < 0) { |
| 573 | g_free(s->config_file); |
| 574 | } |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 575 | qemu_opts_del(opts); |
| 576 | return ret; |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 579 | static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
| 580 | BlkdebugIOType iotype) |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 581 | { |
| 582 | BDRVBlkdebugState *s = bs->opaque; |
Eric Blake | d157ed5 | 2017-04-29 14:14:15 -0500 | [diff] [blame] | 583 | BlkdebugRule *rule = NULL; |
| 584 | int error; |
| 585 | bool immediately; |
| 586 | |
| 587 | QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) { |
| 588 | uint64_t inject_offset = rule->options.inject.offset; |
| 589 | |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 590 | if ((inject_offset == -1 || |
| 591 | (bytes && inject_offset >= offset && |
| 592 | inject_offset < offset + bytes)) && |
| 593 | (rule->options.inject.iotype_mask & (1ull << iotype))) |
Eric Blake | d157ed5 | 2017-04-29 14:14:15 -0500 | [diff] [blame] | 594 | { |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (!rule || !rule->options.inject.error) { |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | immediately = rule->options.inject.immediately; |
| 604 | error = rule->options.inject.error; |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 605 | |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 606 | if (rule->options.inject.once) { |
John Snow | a069e2f | 2015-02-06 16:26:17 -0500 | [diff] [blame] | 607 | QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next); |
| 608 | remove_rule(rule); |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 611 | if (!immediately) { |
Paolo Bonzini | e5c67ab | 2017-02-13 14:52:26 +0100 | [diff] [blame] | 612 | aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self()); |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 613 | qemu_coroutine_yield(); |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 616 | return -error; |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 619 | static int coroutine_fn |
| 620 | blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
| 621 | QEMUIOVector *qiov, int flags) |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 622 | { |
Eric Blake | d157ed5 | 2017-04-29 14:14:15 -0500 | [diff] [blame] | 623 | int err; |
Paolo Bonzini | e4780db | 2012-06-06 08:10:43 +0200 | [diff] [blame] | 624 | |
Eric Blake | e0ef439 | 2017-04-29 14:14:14 -0500 | [diff] [blame] | 625 | /* Sanity check block layer guarantees */ |
| 626 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); |
| 627 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); |
| 628 | if (bs->bl.max_transfer) { |
| 629 | assert(bytes <= bs->bl.max_transfer); |
| 630 | } |
| 631 | |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 632 | err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_READ); |
Eric Blake | d157ed5 | 2017-04-29 14:14:15 -0500 | [diff] [blame] | 633 | if (err) { |
| 634 | return err; |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 637 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 640 | static int coroutine_fn |
| 641 | blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
| 642 | QEMUIOVector *qiov, int flags) |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 643 | { |
Eric Blake | d157ed5 | 2017-04-29 14:14:15 -0500 | [diff] [blame] | 644 | int err; |
Paolo Bonzini | e4780db | 2012-06-06 08:10:43 +0200 | [diff] [blame] | 645 | |
Eric Blake | e0ef439 | 2017-04-29 14:14:14 -0500 | [diff] [blame] | 646 | /* Sanity check block layer guarantees */ |
| 647 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); |
| 648 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); |
| 649 | if (bs->bl.max_transfer) { |
| 650 | assert(bytes <= bs->bl.max_transfer); |
| 651 | } |
| 652 | |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 653 | err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE); |
Eric Blake | d157ed5 | 2017-04-29 14:14:15 -0500 | [diff] [blame] | 654 | if (err) { |
| 655 | return err; |
Kevin Wolf | b9f66d9 | 2010-02-19 16:24:35 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 658 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 659 | } |
| 660 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 661 | static int blkdebug_co_flush(BlockDriverState *bs) |
Paolo Bonzini | 9e52c53 | 2014-08-04 17:11:02 -0400 | [diff] [blame] | 662 | { |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 663 | int err = rule_check(bs, 0, 0, BLKDEBUG_IO_TYPE_FLUSH); |
Paolo Bonzini | 9e52c53 | 2014-08-04 17:11:02 -0400 | [diff] [blame] | 664 | |
Eric Blake | d157ed5 | 2017-04-29 14:14:15 -0500 | [diff] [blame] | 665 | if (err) { |
| 666 | return err; |
Paolo Bonzini | 9e52c53 | 2014-08-04 17:11:02 -0400 | [diff] [blame] | 667 | } |
| 668 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 669 | return bdrv_co_flush(bs->file->bs); |
Paolo Bonzini | 9e52c53 | 2014-08-04 17:11:02 -0400 | [diff] [blame] | 670 | } |
| 671 | |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 672 | static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs, |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 673 | int64_t offset, int bytes, |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 674 | BdrvRequestFlags flags) |
| 675 | { |
| 676 | uint32_t align = MAX(bs->bl.request_alignment, |
| 677 | bs->bl.pwrite_zeroes_alignment); |
| 678 | int err; |
| 679 | |
| 680 | /* Only pass through requests that are larger than requested |
| 681 | * preferred alignment (so that we test the fallback to writes on |
| 682 | * unaligned portions), and check that the block layer never hands |
| 683 | * us anything unaligned that crosses an alignment boundary. */ |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 684 | if (bytes < align) { |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 685 | assert(QEMU_IS_ALIGNED(offset, align) || |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 686 | QEMU_IS_ALIGNED(offset + bytes, align) || |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 687 | DIV_ROUND_UP(offset, align) == |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 688 | DIV_ROUND_UP(offset + bytes, align)); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 689 | return -ENOTSUP; |
| 690 | } |
| 691 | assert(QEMU_IS_ALIGNED(offset, align)); |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 692 | assert(QEMU_IS_ALIGNED(bytes, align)); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 693 | if (bs->bl.max_pwrite_zeroes) { |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 694 | assert(bytes <= bs->bl.max_pwrite_zeroes); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 695 | } |
| 696 | |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 697 | err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE_ZEROES); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 698 | if (err) { |
| 699 | return err; |
| 700 | } |
| 701 | |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 702 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs, |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 706 | int64_t offset, int bytes) |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 707 | { |
| 708 | uint32_t align = bs->bl.pdiscard_alignment; |
| 709 | int err; |
| 710 | |
| 711 | /* Only pass through requests that are larger than requested |
| 712 | * minimum alignment, and ensure that unaligned requests do not |
| 713 | * cross optimum discard boundaries. */ |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 714 | if (bytes < bs->bl.request_alignment) { |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 715 | assert(QEMU_IS_ALIGNED(offset, align) || |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 716 | QEMU_IS_ALIGNED(offset + bytes, align) || |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 717 | DIV_ROUND_UP(offset, align) == |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 718 | DIV_ROUND_UP(offset + bytes, align)); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 719 | return -ENOTSUP; |
| 720 | } |
| 721 | assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 722 | assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); |
| 723 | if (align && bytes >= align) { |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 724 | assert(QEMU_IS_ALIGNED(offset, align)); |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 725 | assert(QEMU_IS_ALIGNED(bytes, align)); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 726 | } |
| 727 | if (bs->bl.max_pdiscard) { |
Manos Pitsidianakis | f5a5ca7 | 2017-06-09 13:18:08 +0300 | [diff] [blame] | 728 | assert(bytes <= bs->bl.max_pdiscard); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 729 | } |
| 730 | |
Max Reitz | 16789db | 2019-05-07 22:35:04 +0200 | [diff] [blame] | 731 | err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_DISCARD); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 732 | if (err) { |
| 733 | return err; |
| 734 | } |
| 735 | |
Fam Zheng | 0b9fd3f | 2018-07-10 14:31:17 +0800 | [diff] [blame] | 736 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 737 | } |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 738 | |
Eric Blake | 3e4d0e7 | 2018-02-13 14:26:43 -0600 | [diff] [blame] | 739 | static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs, |
| 740 | bool want_zero, |
| 741 | int64_t offset, |
| 742 | int64_t bytes, |
| 743 | int64_t *pnum, |
| 744 | int64_t *map, |
| 745 | BlockDriverState **file) |
Eric Blake | efa6e2e | 2017-10-11 22:47:17 -0500 | [diff] [blame] | 746 | { |
Max Reitz | 1adb0b5 | 2019-05-07 22:35:06 +0200 | [diff] [blame] | 747 | int err; |
| 748 | |
Eric Blake | 3e4d0e7 | 2018-02-13 14:26:43 -0600 | [diff] [blame] | 749 | assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment)); |
Max Reitz | 1adb0b5 | 2019-05-07 22:35:06 +0200 | [diff] [blame] | 750 | |
| 751 | err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_BLOCK_STATUS); |
| 752 | if (err) { |
| 753 | return err; |
| 754 | } |
| 755 | |
Max Reitz | 549ec0d | 2019-02-13 18:05:23 +0100 | [diff] [blame] | 756 | assert(bs->file && bs->file->bs); |
| 757 | *pnum = bytes; |
| 758 | *map = offset; |
| 759 | *file = bs->file->bs; |
| 760 | return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID; |
Eric Blake | efa6e2e | 2017-10-11 22:47:17 -0500 | [diff] [blame] | 761 | } |
| 762 | |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 763 | static void blkdebug_close(BlockDriverState *bs) |
| 764 | { |
| 765 | BDRVBlkdebugState *s = bs->opaque; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 766 | BlkdebugRule *rule, *next; |
| 767 | int i; |
| 768 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 769 | for (i = 0; i < BLKDBG__MAX; i++) { |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 770 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
Kevin Wolf | 9e35542 | 2012-12-06 14:32:56 +0100 | [diff] [blame] | 771 | remove_rule(rule); |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 772 | } |
| 773 | } |
Max Reitz | 036990d | 2016-08-15 15:29:25 +0200 | [diff] [blame] | 774 | |
| 775 | g_free(s->config_file); |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 778 | static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule) |
| 779 | { |
| 780 | BDRVBlkdebugState *s = bs->opaque; |
| 781 | BlkdebugSuspendedReq r; |
| 782 | |
| 783 | r = (BlkdebugSuspendedReq) { |
| 784 | .co = qemu_coroutine_self(), |
| 785 | .tag = g_strdup(rule->options.suspend.tag), |
| 786 | }; |
| 787 | |
| 788 | remove_rule(rule); |
| 789 | QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next); |
| 790 | |
Michael S. Tsirkin | 2087352 | 2015-11-30 13:44:44 +0200 | [diff] [blame] | 791 | if (!qtest_enabled()) { |
| 792 | printf("blkdebug: Suspended request '%s'\n", r.tag); |
| 793 | } |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 794 | qemu_coroutine_yield(); |
Michael S. Tsirkin | 2087352 | 2015-11-30 13:44:44 +0200 | [diff] [blame] | 795 | if (!qtest_enabled()) { |
| 796 | printf("blkdebug: Resuming request '%s'\n", r.tag); |
| 797 | } |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 798 | |
| 799 | QLIST_REMOVE(&r, next); |
| 800 | g_free(r.tag); |
| 801 | } |
| 802 | |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 803 | static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, |
Paolo Bonzini | 8f96b5b | 2012-09-28 17:23:00 +0200 | [diff] [blame] | 804 | bool injected) |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 805 | { |
| 806 | BDRVBlkdebugState *s = bs->opaque; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 807 | |
| 808 | /* Only process rules for the current state */ |
Paolo Bonzini | 8f96b5b | 2012-09-28 17:23:00 +0200 | [diff] [blame] | 809 | if (rule->state && rule->state != s->state) { |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 810 | return injected; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /* Take the action */ |
| 814 | switch (rule->action) { |
| 815 | case ACTION_INJECT_ERROR: |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 816 | if (!injected) { |
| 817 | QSIMPLEQ_INIT(&s->active_rules); |
| 818 | injected = true; |
| 819 | } |
| 820 | QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next); |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 821 | break; |
| 822 | |
| 823 | case ACTION_SET_STATE: |
Paolo Bonzini | 8f96b5b | 2012-09-28 17:23:00 +0200 | [diff] [blame] | 824 | s->new_state = rule->options.set_state.new_state; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 825 | break; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 826 | |
| 827 | case ACTION_SUSPEND: |
| 828 | suspend_request(bs, rule); |
| 829 | break; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 830 | } |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 831 | return injected; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Eric Blake | a31939e | 2015-11-18 01:52:54 -0700 | [diff] [blame] | 834 | static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event) |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 835 | { |
| 836 | BDRVBlkdebugState *s = bs->opaque; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 837 | struct BlkdebugRule *rule, *next; |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 838 | bool injected; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 839 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 840 | assert((int)event >= 0 && event < BLKDBG__MAX); |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 841 | |
Paolo Bonzini | 571cd43 | 2012-06-06 08:10:42 +0200 | [diff] [blame] | 842 | injected = false; |
Paolo Bonzini | 8f96b5b | 2012-09-28 17:23:00 +0200 | [diff] [blame] | 843 | s->new_state = s->state; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 844 | QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) { |
Paolo Bonzini | 8f96b5b | 2012-09-28 17:23:00 +0200 | [diff] [blame] | 845 | injected = process_rule(bs, rule, injected); |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 846 | } |
Paolo Bonzini | 8f96b5b | 2012-09-28 17:23:00 +0200 | [diff] [blame] | 847 | s->state = s->new_state; |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 850 | static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event, |
| 851 | const char *tag) |
| 852 | { |
| 853 | BDRVBlkdebugState *s = bs->opaque; |
| 854 | struct BlkdebugRule *rule; |
Marc-André Lureau | f9509d1 | 2017-08-24 10:46:02 +0200 | [diff] [blame] | 855 | int blkdebug_event; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 856 | |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 857 | blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL); |
Marc-André Lureau | f9509d1 | 2017-08-24 10:46:02 +0200 | [diff] [blame] | 858 | if (blkdebug_event < 0) { |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 859 | return -ENOENT; |
| 860 | } |
| 861 | |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 862 | rule = g_malloc(sizeof(*rule)); |
| 863 | *rule = (struct BlkdebugRule) { |
| 864 | .event = blkdebug_event, |
| 865 | .action = ACTION_SUSPEND, |
| 866 | .state = 0, |
| 867 | .options.suspend.tag = g_strdup(tag), |
| 868 | }; |
| 869 | |
| 870 | QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag) |
| 876 | { |
| 877 | BDRVBlkdebugState *s = bs->opaque; |
Fam Zheng | c547e56 | 2013-12-13 15:25:12 +0800 | [diff] [blame] | 878 | BlkdebugSuspendedReq *r, *next; |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 879 | |
Fam Zheng | c547e56 | 2013-12-13 15:25:12 +0800 | [diff] [blame] | 880 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) { |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 881 | if (!strcmp(r->tag, tag)) { |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 882 | qemu_coroutine_enter(r->co); |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 883 | return 0; |
| 884 | } |
| 885 | } |
| 886 | return -ENOENT; |
| 887 | } |
| 888 | |
Fam Zheng | 4cc70e9 | 2013-11-20 10:01:54 +0800 | [diff] [blame] | 889 | static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs, |
| 890 | const char *tag) |
| 891 | { |
| 892 | BDRVBlkdebugState *s = bs->opaque; |
Fam Zheng | c547e56 | 2013-12-13 15:25:12 +0800 | [diff] [blame] | 893 | BlkdebugSuspendedReq *r, *r_next; |
Fam Zheng | 4cc70e9 | 2013-11-20 10:01:54 +0800 | [diff] [blame] | 894 | BlkdebugRule *rule, *next; |
| 895 | int i, ret = -ENOENT; |
| 896 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 897 | for (i = 0; i < BLKDBG__MAX; i++) { |
Fam Zheng | 4cc70e9 | 2013-11-20 10:01:54 +0800 | [diff] [blame] | 898 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { |
| 899 | if (rule->action == ACTION_SUSPEND && |
| 900 | !strcmp(rule->options.suspend.tag, tag)) { |
| 901 | remove_rule(rule); |
| 902 | ret = 0; |
| 903 | } |
| 904 | } |
| 905 | } |
Fam Zheng | c547e56 | 2013-12-13 15:25:12 +0800 | [diff] [blame] | 906 | QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) { |
Fam Zheng | 4cc70e9 | 2013-11-20 10:01:54 +0800 | [diff] [blame] | 907 | if (!strcmp(r->tag, tag)) { |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 908 | qemu_coroutine_enter(r->co); |
Fam Zheng | 4cc70e9 | 2013-11-20 10:01:54 +0800 | [diff] [blame] | 909 | ret = 0; |
| 910 | } |
| 911 | } |
| 912 | return ret; |
| 913 | } |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 914 | |
| 915 | static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag) |
| 916 | { |
| 917 | BDRVBlkdebugState *s = bs->opaque; |
| 918 | BlkdebugSuspendedReq *r; |
| 919 | |
| 920 | QLIST_FOREACH(r, &s->suspended_reqs, next) { |
| 921 | if (!strcmp(r->tag, tag)) { |
| 922 | return true; |
| 923 | } |
| 924 | } |
| 925 | return false; |
| 926 | } |
| 927 | |
Paolo Bonzini | e130225 | 2012-06-06 08:10:41 +0200 | [diff] [blame] | 928 | static int64_t blkdebug_getlength(BlockDriverState *bs) |
| 929 | { |
Kevin Wolf | 9a4f4c3 | 2015-06-16 14:19:22 +0200 | [diff] [blame] | 930 | return bdrv_getlength(bs->file->bs); |
Paolo Bonzini | e130225 | 2012-06-06 08:10:41 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Max Reitz | 998b3a1 | 2019-02-01 20:29:28 +0100 | [diff] [blame] | 933 | static void blkdebug_refresh_filename(BlockDriverState *bs) |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 934 | { |
Max Reitz | 036990d | 2016-08-15 15:29:25 +0200 | [diff] [blame] | 935 | BDRVBlkdebugState *s = bs->opaque; |
Max Reitz | 8779441 | 2014-11-11 10:23:44 +0100 | [diff] [blame] | 936 | const QDictEntry *e; |
Max Reitz | 998b3a1 | 2019-02-01 20:29:28 +0100 | [diff] [blame] | 937 | int ret; |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 938 | |
Max Reitz | 998b3a1 | 2019-02-01 20:29:28 +0100 | [diff] [blame] | 939 | if (!bs->file->bs->exact_filename[0]) { |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 940 | return; |
| 941 | } |
| 942 | |
Max Reitz | 998b3a1 | 2019-02-01 20:29:28 +0100 | [diff] [blame] | 943 | for (e = qdict_first(bs->full_open_options); e; |
| 944 | e = qdict_next(bs->full_open_options, e)) |
| 945 | { |
| 946 | /* Real child options are under "image", but "x-image" may |
| 947 | * contain a filename */ |
| 948 | if (strcmp(qdict_entry_key(e), "config") && |
| 949 | strcmp(qdict_entry_key(e), "image") && |
| 950 | strcmp(qdict_entry_key(e), "x-image") && |
| 951 | strcmp(qdict_entry_key(e), "driver")) |
| 952 | { |
| 953 | return; |
Max Reitz | de81d72 | 2017-06-13 19:20:05 +0200 | [diff] [blame] | 954 | } |
Max Reitz | 8779441 | 2014-11-11 10:23:44 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Max Reitz | 998b3a1 | 2019-02-01 20:29:28 +0100 | [diff] [blame] | 957 | ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
| 958 | "blkdebug:%s:%s", |
| 959 | s->config_file ?: "", bs->file->bs->exact_filename); |
| 960 | if (ret >= sizeof(bs->exact_filename)) { |
| 961 | /* An overflow makes the filename unusable, so do not report any */ |
| 962 | bs->exact_filename[0] = 0; |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 963 | } |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Eric Blake | 835db3e | 2016-06-23 16:37:13 -0600 | [diff] [blame] | 966 | static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp) |
| 967 | { |
| 968 | BDRVBlkdebugState *s = bs->opaque; |
| 969 | |
| 970 | if (s->align) { |
Eric Blake | a5b8dd2 | 2016-06-23 16:37:24 -0600 | [diff] [blame] | 971 | bs->bl.request_alignment = s->align; |
Eric Blake | 835db3e | 2016-06-23 16:37:13 -0600 | [diff] [blame] | 972 | } |
Eric Blake | 430b26a | 2017-04-29 14:14:18 -0500 | [diff] [blame] | 973 | if (s->max_transfer) { |
| 974 | bs->bl.max_transfer = s->max_transfer; |
| 975 | } |
| 976 | if (s->opt_write_zero) { |
| 977 | bs->bl.pwrite_zeroes_alignment = s->opt_write_zero; |
| 978 | } |
| 979 | if (s->max_write_zero) { |
| 980 | bs->bl.max_pwrite_zeroes = s->max_write_zero; |
| 981 | } |
| 982 | if (s->opt_discard) { |
| 983 | bs->bl.pdiscard_alignment = s->opt_discard; |
| 984 | } |
| 985 | if (s->max_discard) { |
| 986 | bs->bl.max_pdiscard = s->max_discard; |
| 987 | } |
Eric Blake | 835db3e | 2016-06-23 16:37:13 -0600 | [diff] [blame] | 988 | } |
| 989 | |
Kevin Wolf | c5e8bfb | 2015-10-29 15:22:27 +0100 | [diff] [blame] | 990 | static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state, |
| 991 | BlockReopenQueue *queue, Error **errp) |
| 992 | { |
| 993 | return 0; |
| 994 | } |
| 995 | |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 996 | static void blkdebug_child_perm(BlockDriverState *bs, BdrvChild *c, |
Max Reitz | bf8e925 | 2020-05-13 13:05:16 +0200 | [diff] [blame] | 997 | BdrvChildRole role, |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 998 | BlockReopenQueue *reopen_queue, |
| 999 | uint64_t perm, uint64_t shared, |
| 1000 | uint64_t *nperm, uint64_t *nshared) |
| 1001 | { |
| 1002 | BDRVBlkdebugState *s = bs->opaque; |
| 1003 | |
Max Reitz | e5d8a40 | 2020-05-13 13:05:44 +0200 | [diff] [blame] | 1004 | bdrv_default_perms(bs, c, role, reopen_queue, |
Max Reitz | 69dca43 | 2020-05-13 13:05:39 +0200 | [diff] [blame] | 1005 | perm, shared, nperm, nshared); |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 1006 | |
| 1007 | *nperm |= s->take_child_perms; |
| 1008 | *nshared &= ~s->unshare_child_perms; |
| 1009 | } |
| 1010 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1011 | static const char *const blkdebug_strong_runtime_opts[] = { |
| 1012 | "config", |
| 1013 | "inject-error.", |
| 1014 | "set-state.", |
| 1015 | "align", |
| 1016 | "max-transfer", |
| 1017 | "opt-write-zero", |
| 1018 | "max-write-zero", |
| 1019 | "opt-discard", |
| 1020 | "max-discard", |
| 1021 | |
| 1022 | NULL |
| 1023 | }; |
| 1024 | |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 1025 | static BlockDriver bdrv_blkdebug = { |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 1026 | .format_name = "blkdebug", |
| 1027 | .protocol_name = "blkdebug", |
| 1028 | .instance_size = sizeof(BDRVBlkdebugState), |
Manos Pitsidianakis | d8e12cd | 2017-07-13 18:30:27 +0300 | [diff] [blame] | 1029 | .is_filter = true, |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 1030 | |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 1031 | .bdrv_parse_filename = blkdebug_parse_filename, |
| 1032 | .bdrv_file_open = blkdebug_open, |
| 1033 | .bdrv_close = blkdebug_close, |
Kevin Wolf | c5e8bfb | 2015-10-29 15:22:27 +0100 | [diff] [blame] | 1034 | .bdrv_reopen_prepare = blkdebug_reopen_prepare, |
Max Reitz | 69c6449 | 2019-11-08 13:34:53 +0100 | [diff] [blame] | 1035 | .bdrv_child_perm = blkdebug_child_perm, |
Kevin Wolf | d7010df | 2016-12-15 12:28:58 +0100 | [diff] [blame] | 1036 | |
Kevin Wolf | f468121 | 2013-04-10 13:37:33 +0200 | [diff] [blame] | 1037 | .bdrv_getlength = blkdebug_getlength, |
Max Reitz | 2c31b04 | 2014-07-18 20:24:57 +0200 | [diff] [blame] | 1038 | .bdrv_refresh_filename = blkdebug_refresh_filename, |
Eric Blake | 835db3e | 2016-06-23 16:37:13 -0600 | [diff] [blame] | 1039 | .bdrv_refresh_limits = blkdebug_refresh_limits, |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 1040 | |
Kevin Wolf | 7c3a998 | 2016-11-04 21:13:45 +0100 | [diff] [blame] | 1041 | .bdrv_co_preadv = blkdebug_co_preadv, |
| 1042 | .bdrv_co_pwritev = blkdebug_co_pwritev, |
| 1043 | .bdrv_co_flush_to_disk = blkdebug_co_flush, |
Eric Blake | 63188c2 | 2017-04-29 14:14:16 -0500 | [diff] [blame] | 1044 | .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes, |
| 1045 | .bdrv_co_pdiscard = blkdebug_co_pdiscard, |
Eric Blake | 3e4d0e7 | 2018-02-13 14:26:43 -0600 | [diff] [blame] | 1046 | .bdrv_co_block_status = blkdebug_co_block_status, |
Kevin Wolf | 8b9b0cc | 2010-03-15 17:27:00 +0100 | [diff] [blame] | 1047 | |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 1048 | .bdrv_debug_event = blkdebug_debug_event, |
| 1049 | .bdrv_debug_breakpoint = blkdebug_debug_breakpoint, |
Fam Zheng | 4cc70e9 | 2013-11-20 10:01:54 +0800 | [diff] [blame] | 1050 | .bdrv_debug_remove_breakpoint |
| 1051 | = blkdebug_debug_remove_breakpoint, |
Kevin Wolf | 3c90c65 | 2012-12-06 14:32:57 +0100 | [diff] [blame] | 1052 | .bdrv_debug_resume = blkdebug_debug_resume, |
| 1053 | .bdrv_debug_is_suspended = blkdebug_debug_is_suspended, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1054 | |
| 1055 | .strong_runtime_opts = blkdebug_strong_runtime_opts, |
Kevin Wolf | 6a14372 | 2010-02-18 17:48:12 +0100 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | static void bdrv_blkdebug_init(void) |
| 1059 | { |
| 1060 | bdrv_register(&bdrv_blkdebug); |
| 1061 | } |
| 1062 | |
| 1063 | block_init(bdrv_blkdebug_init); |