]>
Commit | Line | Data |
---|---|---|
666daa68 MA |
1 | /* |
2 | * QEMU host block devices | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
7 | * later. See the COPYING file in the top-level directory. | |
3618a094 MA |
8 | * |
9 | * This file incorporates work covered by the following copyright and | |
10 | * permission notice: | |
11 | * | |
12 | * Copyright (c) 2003-2008 Fabrice Bellard | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
666daa68 MA |
31 | */ |
32 | ||
d38ea87a | 33 | #include "qemu/osdep.h" |
26f54e9a | 34 | #include "sysemu/block-backend.h" |
9c17d615 | 35 | #include "sysemu/blockdev.h" |
0d09e41a | 36 | #include "hw/block/block.h" |
737e150e | 37 | #include "block/blockjob.h" |
609f45ea | 38 | #include "block/qdict.h" |
76f4afb4 | 39 | #include "block/throttle-groups.h" |
83c9089e | 40 | #include "monitor/monitor.h" |
d49b6836 | 41 | #include "qemu/error-report.h" |
1de7afc9 | 42 | #include "qemu/option.h" |
cdcd4361 | 43 | #include "qemu/qemu-print.h" |
1de7afc9 | 44 | #include "qemu/config-file.h" |
9af23989 MA |
45 | #include "qapi/qapi-commands-block.h" |
46 | #include "qapi/qapi-commands-transaction.h" | |
47 | #include "qapi/qapi-visit-block-core.h" | |
452fcdbc | 48 | #include "qapi/qmp/qdict.h" |
15280c36 | 49 | #include "qapi/qmp/qnum.h" |
6b673957 | 50 | #include "qapi/qmp/qstring.h" |
e688df6b | 51 | #include "qapi/error.h" |
cc7a8ea7 | 52 | #include "qapi/qmp/qerror.h" |
47e6b297 | 53 | #include "qapi/qmp/qlist.h" |
b3db211f | 54 | #include "qapi/qobject-output-visitor.h" |
9c17d615 | 55 | #include "sysemu/sysemu.h" |
ca00bbb1 | 56 | #include "sysemu/iothread.h" |
737e150e | 57 | #include "block/block_int.h" |
0ab8ed18 | 58 | #include "block/trace.h" |
54d31236 | 59 | #include "sysemu/runstate.h" |
b39847a5 | 60 | #include "sysemu/replay.h" |
f348b6d1 VB |
61 | #include "qemu/cutils.h" |
62 | #include "qemu/help_option.h" | |
db725815 | 63 | #include "qemu/main-loop.h" |
a2a7862c | 64 | #include "qemu/throttle-options.h" |
666daa68 | 65 | |
89802d5a | 66 | QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = |
9c4218e9 HR |
67 | QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); |
68 | ||
89802d5a ML |
69 | void bdrv_set_monitor_owned(BlockDriverState *bs) |
70 | { | |
71 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); | |
72 | } | |
73 | ||
1960966d MA |
74 | static const char *const if_name[IF_COUNT] = { |
75 | [IF_NONE] = "none", | |
76 | [IF_IDE] = "ide", | |
77 | [IF_SCSI] = "scsi", | |
78 | [IF_FLOPPY] = "floppy", | |
79 | [IF_PFLASH] = "pflash", | |
80 | [IF_MTD] = "mtd", | |
81 | [IF_SD] = "sd", | |
82 | [IF_VIRTIO] = "virtio", | |
83 | [IF_XEN] = "xen", | |
84 | }; | |
85 | ||
21dff8cf | 86 | static int if_max_devs[IF_COUNT] = { |
27d6bf40 MA |
87 | /* |
88 | * Do not change these numbers! They govern how drive option | |
89 | * index maps to unit and bus. That mapping is ABI. | |
90 | * | |
547cb157 | 91 | * All controllers used to implement if=T drives need to support |
27d6bf40 MA |
92 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. |
93 | * Otherwise, some index values map to "impossible" bus, unit | |
94 | * values. | |
95 | * | |
96 | * For instance, if you change [IF_SCSI] to 255, -drive | |
97 | * if=scsi,index=12 no longer means bus=1,unit=5, but | |
98 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), | |
99 | * the drive can't be set up. Regression. | |
100 | */ | |
101 | [IF_IDE] = 2, | |
102 | [IF_SCSI] = 7, | |
1960966d MA |
103 | }; |
104 | ||
21dff8cf JS |
105 | /** |
106 | * Boards may call this to offer board-by-board overrides | |
107 | * of the default, global values. | |
108 | */ | |
109 | void override_max_devs(BlockInterfaceType type, int max_devs) | |
110 | { | |
18e46a03 | 111 | BlockBackend *blk; |
21dff8cf JS |
112 | DriveInfo *dinfo; |
113 | ||
114 | if (max_devs <= 0) { | |
115 | return; | |
116 | } | |
117 | ||
18e46a03 MA |
118 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
119 | dinfo = blk_legacy_dinfo(blk); | |
21dff8cf JS |
120 | if (dinfo->type == type) { |
121 | fprintf(stderr, "Cannot override units-per-bus property of" | |
122 | " the %s interface, because a drive of that type has" | |
123 | " already been added.\n", if_name[type]); | |
124 | g_assert_not_reached(); | |
125 | } | |
126 | } | |
127 | ||
128 | if_max_devs[type] = max_devs; | |
129 | } | |
130 | ||
14bafc54 MA |
131 | /* |
132 | * We automatically delete the drive when a device using it gets | |
133 | * unplugged. Questionable feature, but we can't just drop it. | |
134 | * Device models call blockdev_mark_auto_del() to schedule the | |
135 | * automatic deletion, and generic qdev code calls blockdev_auto_del() | |
136 | * when deletion is actually safe. | |
137 | */ | |
4be74634 | 138 | void blockdev_mark_auto_del(BlockBackend *blk) |
14bafc54 | 139 | { |
18e46a03 | 140 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
8164102f | 141 | BlockJob *job; |
14bafc54 | 142 | |
26f8b3a8 | 143 | if (!dinfo) { |
2d246f01 KW |
144 | return; |
145 | } | |
146 | ||
8164102f VSO |
147 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { |
148 | if (block_job_has_bdrv(job, blk_bs(blk))) { | |
149 | AioContext *aio_context = job->job.aio_context; | |
150 | aio_context_acquire(aio_context); | |
91fddb0d | 151 | |
8164102f | 152 | job_cancel(&job->job, false); |
91fddb0d | 153 | |
8164102f VSO |
154 | aio_context_release(aio_context); |
155 | } | |
5433c24f | 156 | } |
91fddb0d | 157 | |
26f8b3a8 | 158 | dinfo->auto_del = 1; |
14bafc54 MA |
159 | } |
160 | ||
4be74634 | 161 | void blockdev_auto_del(BlockBackend *blk) |
14bafc54 | 162 | { |
18e46a03 | 163 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
14bafc54 | 164 | |
0fc0f1fa | 165 | if (dinfo && dinfo->auto_del) { |
efaa7c4e | 166 | monitor_remove_blk(blk); |
b9fe8a7a | 167 | blk_unref(blk); |
14bafc54 MA |
168 | } |
169 | } | |
170 | ||
d8f94e1b JS |
171 | /** |
172 | * Returns the current mapping of how many units per bus | |
173 | * a particular interface can support. | |
174 | * | |
175 | * A positive integer indicates n units per bus. | |
176 | * 0 implies the mapping has not been established. | |
177 | * -1 indicates an invalid BlockInterfaceType was given. | |
178 | */ | |
179 | int drive_get_max_devs(BlockInterfaceType type) | |
180 | { | |
181 | if (type >= IF_IDE && type < IF_COUNT) { | |
182 | return if_max_devs[type]; | |
183 | } | |
184 | ||
185 | return -1; | |
186 | } | |
187 | ||
505a7fb1 MA |
188 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
189 | { | |
190 | int max_devs = if_max_devs[type]; | |
191 | return max_devs ? index / max_devs : 0; | |
192 | } | |
193 | ||
194 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
195 | { | |
196 | int max_devs = if_max_devs[type]; | |
197 | return max_devs ? index % max_devs : index; | |
198 | } | |
199 | ||
2292ddae MA |
200 | QemuOpts *drive_def(const char *optstr) |
201 | { | |
70b94331 | 202 | return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); |
2292ddae MA |
203 | } |
204 | ||
205 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 206 | const char *optstr) |
666daa68 | 207 | { |
666daa68 MA |
208 | QemuOpts *opts; |
209 | ||
2292ddae | 210 | opts = drive_def(optstr); |
666daa68 MA |
211 | if (!opts) { |
212 | return NULL; | |
213 | } | |
2292ddae | 214 | if (type != IF_DEFAULT) { |
f43e47db | 215 | qemu_opt_set(opts, "if", if_name[type], &error_abort); |
2292ddae MA |
216 | } |
217 | if (index >= 0) { | |
a8b18f8f | 218 | qemu_opt_set_number(opts, "index", index, &error_abort); |
2292ddae | 219 | } |
666daa68 | 220 | if (file) |
f43e47db | 221 | qemu_opt_set(opts, "file", file, &error_abort); |
666daa68 MA |
222 | return opts; |
223 | } | |
224 | ||
225 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
226 | { | |
18e46a03 | 227 | BlockBackend *blk; |
666daa68 MA |
228 | DriveInfo *dinfo; |
229 | ||
18e46a03 MA |
230 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
231 | dinfo = blk_legacy_dinfo(blk); | |
232 | if (dinfo && dinfo->type == type | |
233 | && dinfo->bus == bus && dinfo->unit == unit) { | |
666daa68 | 234 | return dinfo; |
18e46a03 | 235 | } |
666daa68 MA |
236 | } |
237 | ||
238 | return NULL; | |
239 | } | |
240 | ||
fe9f70a1 MA |
241 | /* |
242 | * Check board claimed all -drive that are meant to be claimed. | |
243 | * Fatal error if any remain unclaimed. | |
244 | */ | |
720b8dc0 | 245 | void drive_check_orphaned(void) |
a66c9dc7 | 246 | { |
18e46a03 | 247 | BlockBackend *blk; |
a66c9dc7 | 248 | DriveInfo *dinfo; |
664cc623 | 249 | Location loc; |
720b8dc0 | 250 | bool orphans = false; |
a66c9dc7 | 251 | |
18e46a03 MA |
252 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
253 | dinfo = blk_legacy_dinfo(blk); | |
fe9f70a1 MA |
254 | /* |
255 | * Ignore default drives, because we create certain default | |
256 | * drives unconditionally, then leave them unclaimed. Not the | |
257 | * users fault. | |
258 | * Ignore IF_VIRTIO, because it gets desugared into -device, | |
259 | * so we can leave failing to -device. | |
260 | * Ignore IF_NONE, because leaving unclaimed IF_NONE remains | |
261 | * available for device_add is a feature. | |
262 | */ | |
263 | if (dinfo->is_default || dinfo->type == IF_VIRTIO | |
264 | || dinfo->type == IF_NONE) { | |
a1b40bda MA |
265 | continue; |
266 | } | |
267 | if (!blk_get_attached_dev(blk)) { | |
664cc623 MA |
268 | loc_push_none(&loc); |
269 | qemu_opts_loc_restore(dinfo->opts); | |
720b8dc0 | 270 | error_report("machine type does not support" |
664cc623 MA |
271 | " if=%s,bus=%d,unit=%d", |
272 | if_name[dinfo->type], dinfo->bus, dinfo->unit); | |
273 | loc_pop(&loc); | |
720b8dc0 | 274 | orphans = true; |
a66c9dc7 JS |
275 | } |
276 | } | |
277 | ||
720b8dc0 MA |
278 | if (orphans) { |
279 | exit(1); | |
280 | } | |
a66c9dc7 JS |
281 | } |
282 | ||
f1bd51ac MA |
283 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
284 | { | |
285 | return drive_get(type, | |
286 | drive_index_to_bus_id(type, index), | |
287 | drive_index_to_unit_id(type, index)); | |
288 | } | |
289 | ||
666daa68 MA |
290 | int drive_get_max_bus(BlockInterfaceType type) |
291 | { | |
292 | int max_bus; | |
18e46a03 | 293 | BlockBackend *blk; |
666daa68 MA |
294 | DriveInfo *dinfo; |
295 | ||
296 | max_bus = -1; | |
18e46a03 MA |
297 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
298 | dinfo = blk_legacy_dinfo(blk); | |
299 | if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { | |
666daa68 | 300 | max_bus = dinfo->bus; |
18e46a03 | 301 | } |
666daa68 MA |
302 | } |
303 | return max_bus; | |
304 | } | |
305 | ||
666daa68 MA |
306 | static void bdrv_format_print(void *opaque, const char *name) |
307 | { | |
cdcd4361 | 308 | qemu_printf(" %s", name); |
666daa68 MA |
309 | } |
310 | ||
aa398a5c SH |
311 | typedef struct { |
312 | QEMUBH *bh; | |
fa510ebf FZ |
313 | BlockDriverState *bs; |
314 | } BDRVPutRefBH; | |
aa398a5c | 315 | |
b681072d | 316 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) |
666daa68 MA |
317 | { |
318 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 319 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 320 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 321 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 322 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 323 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 324 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 325 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 326 | } else { |
b681072d KW |
327 | error_setg(errp, "'%s' invalid %s error action", |
328 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
329 | return -1; |
330 | } | |
331 | } | |
332 | ||
40119eff AG |
333 | static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, |
334 | Error **errp) | |
335 | { | |
336 | const QListEntry *entry; | |
337 | for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { | |
338 | switch (qobject_type(entry->value)) { | |
339 | ||
340 | case QTYPE_QSTRING: { | |
341 | unsigned long long length; | |
7dc847eb HR |
342 | const char *str = qstring_get_str(qobject_to(QString, |
343 | entry->value)); | |
40119eff AG |
344 | if (parse_uint_full(str, &length, 10) == 0 && |
345 | length > 0 && length <= UINT_MAX) { | |
346 | block_acct_add_interval(stats, (unsigned) length); | |
347 | } else { | |
348 | error_setg(errp, "Invalid interval length: %s", str); | |
349 | return false; | |
350 | } | |
351 | break; | |
352 | } | |
353 | ||
01b2ffce | 354 | case QTYPE_QNUM: { |
7dc847eb | 355 | int64_t length = qnum_get_int(qobject_to(QNum, entry->value)); |
01b2ffce | 356 | |
40119eff AG |
357 | if (length > 0 && length <= UINT_MAX) { |
358 | block_acct_add_interval(stats, (unsigned) length); | |
359 | } else { | |
360 | error_setg(errp, "Invalid interval length: %" PRId64, length); | |
361 | return false; | |
362 | } | |
363 | break; | |
364 | } | |
365 | ||
366 | default: | |
367 | error_setg(errp, "The specification of stats-intervals is invalid"); | |
368 | return false; | |
369 | } | |
370 | } | |
371 | return true; | |
372 | } | |
373 | ||
33cb7dc8 KW |
374 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
375 | ||
fbf8175e HR |
376 | /* All parameters but @opts are optional and may be set to NULL. */ |
377 | static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, | |
378 | const char **throttling_group, ThrottleConfig *throttle_cfg, | |
379 | BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) | |
380 | { | |
fbf8175e HR |
381 | Error *local_error = NULL; |
382 | const char *aio; | |
383 | ||
384 | if (bdrv_flags) { | |
fbf8175e HR |
385 | if (qemu_opt_get_bool(opts, "copy-on-read", false)) { |
386 | *bdrv_flags |= BDRV_O_COPY_ON_READ; | |
387 | } | |
388 | ||
fbf8175e | 389 | if ((aio = qemu_opt_get(opts, "aio")) != NULL) { |
f80f2673 AM |
390 | if (bdrv_parse_aio(aio, bdrv_flags) < 0) { |
391 | error_setg(errp, "invalid aio option"); | |
392 | return; | |
fbf8175e HR |
393 | } |
394 | } | |
395 | } | |
396 | ||
397 | /* disk I/O throttling */ | |
398 | if (throttling_group) { | |
399 | *throttling_group = qemu_opt_get(opts, "throttling.group"); | |
400 | } | |
401 | ||
402 | if (throttle_cfg) { | |
1588ab5d | 403 | throttle_config_init(throttle_cfg); |
fbf8175e HR |
404 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = |
405 | qemu_opt_get_number(opts, "throttling.bps-total", 0); | |
406 | throttle_cfg->buckets[THROTTLE_BPS_READ].avg = | |
407 | qemu_opt_get_number(opts, "throttling.bps-read", 0); | |
408 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = | |
409 | qemu_opt_get_number(opts, "throttling.bps-write", 0); | |
410 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = | |
411 | qemu_opt_get_number(opts, "throttling.iops-total", 0); | |
412 | throttle_cfg->buckets[THROTTLE_OPS_READ].avg = | |
413 | qemu_opt_get_number(opts, "throttling.iops-read", 0); | |
414 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = | |
415 | qemu_opt_get_number(opts, "throttling.iops-write", 0); | |
416 | ||
417 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = | |
418 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
419 | throttle_cfg->buckets[THROTTLE_BPS_READ].max = | |
420 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
421 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = | |
422 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
423 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = | |
424 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
425 | throttle_cfg->buckets[THROTTLE_OPS_READ].max = | |
426 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
427 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = | |
428 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
429 | ||
8a0fc18d AG |
430 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = |
431 | qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); | |
432 | throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = | |
433 | qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); | |
434 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = | |
435 | qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); | |
436 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = | |
437 | qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); | |
438 | throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = | |
439 | qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); | |
440 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = | |
441 | qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); | |
442 | ||
fbf8175e HR |
443 | throttle_cfg->op_size = |
444 | qemu_opt_get_number(opts, "throttling.iops-size", 0); | |
445 | ||
d5851089 | 446 | if (!throttle_is_valid(throttle_cfg, errp)) { |
fbf8175e HR |
447 | return; |
448 | } | |
449 | } | |
450 | ||
451 | if (detect_zeroes) { | |
452 | *detect_zeroes = | |
f7abe0ec | 453 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, |
fbf8175e | 454 | qemu_opt_get(opts, "detect-zeroes"), |
fbf8175e HR |
455 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
456 | &local_error); | |
457 | if (local_error) { | |
458 | error_propagate(errp, local_error); | |
459 | return; | |
460 | } | |
fbf8175e HR |
461 | } |
462 | } | |
463 | ||
f298d071 | 464 | /* Takes the ownership of bs_opts */ |
18e46a03 MA |
465 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
466 | Error **errp) | |
666daa68 MA |
467 | { |
468 | const char *buf; | |
666daa68 MA |
469 | int bdrv_flags = 0; |
470 | int on_read_error, on_write_error; | |
362e9299 | 471 | bool account_invalid, account_failed; |
f87a0e29 | 472 | bool writethrough, read_only; |
26f54e9a | 473 | BlockBackend *blk; |
a0f1eab1 | 474 | BlockDriverState *bs; |
cc0681c4 | 475 | ThrottleConfig cfg; |
666daa68 | 476 | int snapshot = 0; |
c546194f | 477 | Error *error = NULL; |
0006383e | 478 | QemuOpts *opts; |
40119eff AG |
479 | QDict *interval_dict = NULL; |
480 | QList *interval_list = NULL; | |
0006383e | 481 | const char *id; |
fbf8175e HR |
482 | BlockdevDetectZeroesOptions detect_zeroes = |
483 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; | |
484 | const char *throttling_group = NULL; | |
666daa68 | 485 | |
f298d071 KW |
486 | /* Check common options by copying from bs_opts to opts, all other options |
487 | * stay in bs_opts for processing by bdrv_open(). */ | |
488 | id = qdict_get_try_str(bs_opts, "id"); | |
c6ecec43 MA |
489 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, errp); |
490 | if (!opts) { | |
6376f952 | 491 | goto err_no_opts; |
0006383e KW |
492 | } |
493 | ||
668f62ec | 494 | if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) { |
ec9c10d2 | 495 | goto early_err; |
0006383e KW |
496 | } |
497 | ||
498 | if (id) { | |
499 | qdict_del(bs_opts, "id"); | |
500 | } | |
501 | ||
666daa68 | 502 | /* extract parameters */ |
666daa68 | 503 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
666daa68 | 504 | |
362e9299 AG |
505 | account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); |
506 | account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); | |
507 | ||
e4b24b49 KW |
508 | writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); |
509 | ||
ff356ee4 AG |
510 | id = qemu_opts_id(opts); |
511 | ||
40119eff AG |
512 | qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); |
513 | qdict_array_split(interval_dict, &interval_list); | |
514 | ||
515 | if (qdict_size(interval_dict) != 0) { | |
516 | error_setg(errp, "Invalid option stats-intervals.%s", | |
517 | qdict_first(interval_dict)->key); | |
518 | goto early_err; | |
519 | } | |
2be5506f | 520 | |
fbf8175e HR |
521 | extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, |
522 | &detect_zeroes, &error); | |
523 | if (error) { | |
524 | error_propagate(errp, error); | |
525 | goto early_err; | |
666daa68 | 526 | } |
666daa68 MA |
527 | |
528 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 | 529 | if (is_help_option(buf)) { |
cdcd4361 | 530 | qemu_printf("Supported formats:"); |
9ac404c5 | 531 | bdrv_iterate_format(bdrv_format_print, NULL, false); |
cdcd4361 | 532 | qemu_printf("\nSupported formats (read-only):"); |
9ac404c5 | 533 | bdrv_iterate_format(bdrv_format_print, NULL, true); |
cdcd4361 | 534 | qemu_printf("\n"); |
ec9c10d2 | 535 | goto early_err; |
666daa68 | 536 | } |
74fe54f2 | 537 | |
e4342ce5 HR |
538 | if (qdict_haskey(bs_opts, "driver")) { |
539 | error_setg(errp, "Cannot specify both 'driver' and 'format'"); | |
ec9c10d2 | 540 | goto early_err; |
6db5f5d6 | 541 | } |
46f5ac20 | 542 | qdict_put_str(bs_opts, "driver", buf); |
666daa68 MA |
543 | } |
544 | ||
92aa5c6d | 545 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 546 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
b681072d | 547 | on_write_error = parse_block_error_action(buf, 0, &error); |
84d18f06 | 548 | if (error) { |
b681072d | 549 | error_propagate(errp, error); |
ec9c10d2 | 550 | goto early_err; |
666daa68 MA |
551 | } |
552 | } | |
553 | ||
92aa5c6d | 554 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 555 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
b681072d | 556 | on_read_error = parse_block_error_action(buf, 1, &error); |
84d18f06 | 557 | if (error) { |
b681072d | 558 | error_propagate(errp, error); |
ec9c10d2 | 559 | goto early_err; |
666daa68 MA |
560 | } |
561 | } | |
562 | ||
5ec18f8c | 563 | if (snapshot) { |
91a097e7 | 564 | bdrv_flags |= BDRV_O_SNAPSHOT; |
5ec18f8c HR |
565 | } |
566 | ||
f87a0e29 AG |
567 | read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); |
568 | ||
326642bc | 569 | /* init */ |
39c4ae94 | 570 | if ((!file || !*file) && !qdict_size(bs_opts)) { |
5ec18f8c HR |
571 | BlockBackendRootState *blk_rs; |
572 | ||
d861ab3a | 573 | blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
5ec18f8c | 574 | blk_rs = blk_get_root_state(blk); |
260242a8 | 575 | blk_rs->open_flags = bdrv_flags | (read_only ? 0 : BDRV_O_RDWR); |
5ec18f8c HR |
576 | blk_rs->detect_zeroes = detect_zeroes; |
577 | ||
cb3e7f08 | 578 | qobject_unref(bs_opts); |
e4342ce5 HR |
579 | } else { |
580 | if (file && !*file) { | |
c2ad1b0c | 581 | file = NULL; |
c2ad1b0c | 582 | } |
666daa68 | 583 | |
91a097e7 KW |
584 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
585 | * with other callers) rather than what we want as the real defaults. | |
586 | * Apply the defaults here instead. */ | |
91a097e7 KW |
587 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
588 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
f87a0e29 AG |
589 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, |
590 | read_only ? "on" : "off"); | |
9384a444 | 591 | qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on"); |
72e775c7 | 592 | assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); |
91a097e7 | 593 | |
12d5ee3a KW |
594 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
595 | bdrv_flags |= BDRV_O_INACTIVE; | |
596 | } | |
597 | ||
efaa7c4e | 598 | blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); |
e4342ce5 HR |
599 | if (!blk) { |
600 | goto err_no_bs_opts; | |
601 | } | |
602 | bs = blk_bs(blk); | |
ed9d4205 | 603 | |
5ec18f8c | 604 | bs->detect_zeroes = detect_zeroes; |
666daa68 | 605 | |
9caa6f3d | 606 | block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); |
2be5506f | 607 | |
40119eff AG |
608 | if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { |
609 | blk_unref(blk); | |
610 | blk = NULL; | |
611 | goto err_no_bs_opts; | |
2be5506f | 612 | } |
666daa68 MA |
613 | } |
614 | ||
7ca7f0f6 KW |
615 | /* disk I/O throttling */ |
616 | if (throttle_enabled(&cfg)) { | |
617 | if (!throttling_group) { | |
ff356ee4 | 618 | throttling_group = id; |
7ca7f0f6 KW |
619 | } |
620 | blk_io_limits_enable(blk, throttling_group); | |
621 | blk_set_io_limits(blk, &cfg); | |
622 | } | |
623 | ||
e4b24b49 | 624 | blk_set_enable_write_cache(blk, !writethrough); |
5ec18f8c | 625 | blk_set_on_error(blk, on_read_error, on_write_error); |
0006383e | 626 | |
ff356ee4 | 627 | if (!monitor_add_blk(blk, id, errp)) { |
efaa7c4e HR |
628 | blk_unref(blk); |
629 | blk = NULL; | |
630 | goto err_no_bs_opts; | |
631 | } | |
632 | ||
e4342ce5 | 633 | err_no_bs_opts: |
0006383e | 634 | qemu_opts_del(opts); |
cb3e7f08 MAL |
635 | qobject_unref(interval_dict); |
636 | qobject_unref(interval_list); | |
18e46a03 | 637 | return blk; |
a9ae2bff | 638 | |
ec9c10d2 | 639 | early_err: |
ec9c10d2 | 640 | qemu_opts_del(opts); |
cb3e7f08 MAL |
641 | qobject_unref(interval_dict); |
642 | qobject_unref(interval_list); | |
6376f952 | 643 | err_no_opts: |
cb3e7f08 | 644 | qobject_unref(bs_opts); |
a9ae2bff | 645 | return NULL; |
666daa68 MA |
646 | } |
647 | ||
bd745e23 | 648 | /* Takes the ownership of bs_opts */ |
89802d5a | 649 | BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) |
bd745e23 | 650 | { |
bd745e23 HR |
651 | int bdrv_flags = 0; |
652 | ||
a81d6164 KW |
653 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
654 | * with other callers) rather than what we want as the real defaults. | |
655 | * Apply the defaults here instead. */ | |
a81d6164 KW |
656 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
657 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
f87a0e29 | 658 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); |
a81d6164 | 659 | |
12d5ee3a KW |
660 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
661 | bdrv_flags |= BDRV_O_INACTIVE; | |
662 | } | |
663 | ||
74e1ae7c | 664 | return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); |
bd745e23 HR |
665 | } |
666 | ||
9c4218e9 HR |
667 | void blockdev_close_all_bdrv_states(void) |
668 | { | |
669 | BlockDriverState *bs, *next_bs; | |
670 | ||
671 | QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { | |
672 | AioContext *ctx = bdrv_get_aio_context(bs); | |
673 | ||
674 | aio_context_acquire(ctx); | |
675 | bdrv_unref(bs); | |
676 | aio_context_release(ctx); | |
677 | } | |
678 | } | |
679 | ||
262b4e8f HR |
680 | /* Iterates over the list of monitor-owned BlockDriverStates */ |
681 | BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) | |
682 | { | |
683 | return bs ? QTAILQ_NEXT(bs, monitor_list) | |
684 | : QTAILQ_FIRST(&monitor_bdrv_states); | |
685 | } | |
686 | ||
c75d7f71 | 687 | static bool qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
5abbf0ee | 688 | Error **errp) |
57975222 KW |
689 | { |
690 | const char *value; | |
691 | ||
692 | value = qemu_opt_get(opts, from); | |
693 | if (value) { | |
5abbf0ee KW |
694 | if (qemu_opt_find(opts, to)) { |
695 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " | |
696 | "same time", to, from); | |
c75d7f71 | 697 | return false; |
5abbf0ee | 698 | } |
20d6cd47 JL |
699 | } |
700 | ||
701 | /* rename all items in opts */ | |
702 | while ((value = qemu_opt_get(opts, from))) { | |
f43e47db | 703 | qemu_opt_set(opts, to, value, &error_abort); |
57975222 KW |
704 | qemu_opt_unset(opts, from); |
705 | } | |
c75d7f71 | 706 | return true; |
57975222 KW |
707 | } |
708 | ||
33cb7dc8 KW |
709 | QemuOptsList qemu_legacy_drive_opts = { |
710 | .name = "drive", | |
711 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
712 | .desc = { | |
713 | { | |
87a899c5 KW |
714 | .name = "bus", |
715 | .type = QEMU_OPT_NUMBER, | |
716 | .help = "bus number", | |
717 | },{ | |
718 | .name = "unit", | |
719 | .type = QEMU_OPT_NUMBER, | |
720 | .help = "unit number (i.e. lun for scsi)", | |
721 | },{ | |
722 | .name = "index", | |
723 | .type = QEMU_OPT_NUMBER, | |
724 | .help = "index number", | |
725 | },{ | |
33cb7dc8 KW |
726 | .name = "media", |
727 | .type = QEMU_OPT_STRING, | |
728 | .help = "media type (disk, cdrom)", | |
593d464b KW |
729 | },{ |
730 | .name = "if", | |
731 | .type = QEMU_OPT_STRING, | |
732 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
d095b465 HR |
733 | },{ |
734 | .name = "file", | |
735 | .type = QEMU_OPT_STRING, | |
736 | .help = "file name", | |
33cb7dc8 | 737 | }, |
0ebd24e0 KW |
738 | |
739 | /* Options that are passed on, but have special semantics with -drive */ | |
740 | { | |
4e200cf8 | 741 | .name = BDRV_OPT_READ_ONLY, |
0ebd24e0 KW |
742 | .type = QEMU_OPT_BOOL, |
743 | .help = "open drive file as read-only", | |
ee13ed1c KW |
744 | },{ |
745 | .name = "rerror", | |
746 | .type = QEMU_OPT_STRING, | |
747 | .help = "read error action", | |
748 | },{ | |
749 | .name = "werror", | |
750 | .type = QEMU_OPT_STRING, | |
751 | .help = "write error action", | |
0ebd24e0 KW |
752 | },{ |
753 | .name = "copy-on-read", | |
754 | .type = QEMU_OPT_BOOL, | |
755 | .help = "copy read data from backing file into image file", | |
756 | }, | |
757 | ||
33cb7dc8 KW |
758 | { /* end of list */ } |
759 | }, | |
760 | }; | |
761 | ||
c4f26c9f MA |
762 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type, |
763 | Error **errp) | |
57975222 | 764 | { |
29c4e2b5 | 765 | const char *value; |
18e46a03 | 766 | BlockBackend *blk; |
33cb7dc8 | 767 | DriveInfo *dinfo = NULL; |
f298d071 | 768 | QDict *bs_opts; |
33cb7dc8 KW |
769 | QemuOpts *legacy_opts; |
770 | DriveMediaType media = MEDIA_DISK; | |
593d464b | 771 | BlockInterfaceType type; |
87a899c5 | 772 | int max_devs, bus_id, unit_id, index; |
ee13ed1c | 773 | const char *werror, *rerror; |
a7fdbcf0 FZ |
774 | bool read_only = false; |
775 | bool copy_on_read; | |
d095b465 | 776 | const char *filename; |
247147fb | 777 | int i; |
29c4e2b5 | 778 | |
57975222 | 779 | /* Change legacy command line options into QMP ones */ |
247147fb KW |
780 | static const struct { |
781 | const char *from; | |
782 | const char *to; | |
783 | } opt_renames[] = { | |
784 | { "iops", "throttling.iops-total" }, | |
785 | { "iops_rd", "throttling.iops-read" }, | |
786 | { "iops_wr", "throttling.iops-write" }, | |
57975222 | 787 | |
247147fb KW |
788 | { "bps", "throttling.bps-total" }, |
789 | { "bps_rd", "throttling.bps-read" }, | |
790 | { "bps_wr", "throttling.bps-write" }, | |
57975222 | 791 | |
247147fb KW |
792 | { "iops_max", "throttling.iops-total-max" }, |
793 | { "iops_rd_max", "throttling.iops-read-max" }, | |
794 | { "iops_wr_max", "throttling.iops-write-max" }, | |
3e9fab69 | 795 | |
247147fb KW |
796 | { "bps_max", "throttling.bps-total-max" }, |
797 | { "bps_rd_max", "throttling.bps-read-max" }, | |
798 | { "bps_wr_max", "throttling.bps-write-max" }, | |
3e9fab69 | 799 | |
247147fb | 800 | { "iops_size", "throttling.iops-size" }, |
2024c1df | 801 | |
76f4afb4 AG |
802 | { "group", "throttling.group" }, |
803 | ||
4e200cf8 | 804 | { "readonly", BDRV_OPT_READ_ONLY }, |
247147fb KW |
805 | }; |
806 | ||
807 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
235e59cf | 808 | if (!qemu_opt_rename(all_opts, opt_renames[i].from, |
668f62ec | 809 | opt_renames[i].to, errp)) { |
5abbf0ee KW |
810 | return NULL; |
811 | } | |
247147fb | 812 | } |
0f227a94 | 813 | |
29c4e2b5 KW |
814 | value = qemu_opt_get(all_opts, "cache"); |
815 | if (value) { | |
816 | int flags = 0; | |
04feb4a5 | 817 | bool writethrough; |
29c4e2b5 | 818 | |
04feb4a5 | 819 | if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { |
c4f26c9f | 820 | error_setg(errp, "invalid cache option"); |
29c4e2b5 KW |
821 | return NULL; |
822 | } | |
823 | ||
824 | /* Specific options take precedence */ | |
54861b92 KW |
825 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { |
826 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, | |
04feb4a5 | 827 | !writethrough, &error_abort); |
29c4e2b5 | 828 | } |
54861b92 KW |
829 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { |
830 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, | |
cccb7967 | 831 | !!(flags & BDRV_O_NOCACHE), &error_abort); |
29c4e2b5 | 832 | } |
54861b92 KW |
833 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { |
834 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, | |
cccb7967 | 835 | !!(flags & BDRV_O_NO_FLUSH), &error_abort); |
29c4e2b5 KW |
836 | } |
837 | qemu_opt_unset(all_opts, "cache"); | |
838 | } | |
839 | ||
f298d071 KW |
840 | /* Get a QDict for processing the options */ |
841 | bs_opts = qdict_new(); | |
842 | qemu_opts_to_qdict(all_opts, bs_opts); | |
843 | ||
87ea75d5 PC |
844 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
845 | &error_abort); | |
af175e85 | 846 | if (!qemu_opts_absorb_qdict(legacy_opts, bs_opts, errp)) { |
33cb7dc8 KW |
847 | goto fail; |
848 | } | |
849 | ||
850 | /* Media type */ | |
851 | value = qemu_opt_get(legacy_opts, "media"); | |
852 | if (value) { | |
853 | if (!strcmp(value, "disk")) { | |
854 | media = MEDIA_DISK; | |
855 | } else if (!strcmp(value, "cdrom")) { | |
856 | media = MEDIA_CDROM; | |
a7fdbcf0 | 857 | read_only = true; |
33cb7dc8 | 858 | } else { |
c4f26c9f | 859 | error_setg(errp, "'%s' invalid media", value); |
33cb7dc8 KW |
860 | goto fail; |
861 | } | |
862 | } | |
863 | ||
0ebd24e0 | 864 | /* copy-on-read is disabled with a warning for read-only devices */ |
4e200cf8 | 865 | read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); |
0ebd24e0 KW |
866 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
867 | ||
868 | if (read_only && copy_on_read) { | |
3dc6f869 | 869 | warn_report("disabling copy-on-read on read-only drive"); |
0ebd24e0 KW |
870 | copy_on_read = false; |
871 | } | |
872 | ||
46f5ac20 EB |
873 | qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); |
874 | qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); | |
0ebd24e0 | 875 | |
593d464b KW |
876 | /* Controller type */ |
877 | value = qemu_opt_get(legacy_opts, "if"); | |
878 | if (value) { | |
879 | for (type = 0; | |
880 | type < IF_COUNT && strcmp(value, if_name[type]); | |
881 | type++) { | |
882 | } | |
883 | if (type == IF_COUNT) { | |
c4f26c9f | 884 | error_setg(errp, "unsupported bus type '%s'", value); |
593d464b KW |
885 | goto fail; |
886 | } | |
887 | } else { | |
888 | type = block_default_type; | |
889 | } | |
890 | ||
87a899c5 KW |
891 | /* Device address specified by bus/unit or index. |
892 | * If none was specified, try to find the first free one. */ | |
893 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); | |
894 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); | |
895 | index = qemu_opt_get_number(legacy_opts, "index", -1); | |
896 | ||
897 | max_devs = if_max_devs[type]; | |
898 | ||
899 | if (index != -1) { | |
900 | if (bus_id != 0 || unit_id != -1) { | |
c4f26c9f | 901 | error_setg(errp, "index cannot be used with bus and unit"); |
87a899c5 KW |
902 | goto fail; |
903 | } | |
904 | bus_id = drive_index_to_bus_id(type, index); | |
905 | unit_id = drive_index_to_unit_id(type, index); | |
906 | } | |
907 | ||
908 | if (unit_id == -1) { | |
909 | unit_id = 0; | |
910 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
911 | unit_id++; | |
912 | if (max_devs && unit_id >= max_devs) { | |
913 | unit_id -= max_devs; | |
914 | bus_id++; | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
919 | if (max_devs && unit_id >= max_devs) { | |
c4f26c9f | 920 | error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1); |
87a899c5 KW |
921 | goto fail; |
922 | } | |
923 | ||
924 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
c4f26c9f MA |
925 | error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists", |
926 | bus_id, unit_id, index); | |
87a899c5 KW |
927 | goto fail; |
928 | } | |
929 | ||
930 | /* no id supplied -> create one */ | |
931 | if (qemu_opts_id(all_opts) == NULL) { | |
932 | char *new_id; | |
933 | const char *mediastr = ""; | |
934 | if (type == IF_IDE || type == IF_SCSI) { | |
935 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
936 | } | |
937 | if (max_devs) { | |
938 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, | |
939 | mediastr, unit_id); | |
940 | } else { | |
941 | new_id = g_strdup_printf("%s%s%i", if_name[type], | |
942 | mediastr, unit_id); | |
943 | } | |
46f5ac20 | 944 | qdict_put_str(bs_opts, "id", new_id); |
87a899c5 KW |
945 | g_free(new_id); |
946 | } | |
947 | ||
394c7d4d | 948 | /* Add virtio block device */ |
394c7d4d KW |
949 | if (type == IF_VIRTIO) { |
950 | QemuOpts *devopts; | |
87ea75d5 PC |
951 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
952 | &error_abort); | |
9db1d3a2 | 953 | qemu_opt_set(devopts, "driver", "virtio-blk", &error_abort); |
f43e47db MA |
954 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), |
955 | &error_abort); | |
394c7d4d KW |
956 | } |
957 | ||
d095b465 HR |
958 | filename = qemu_opt_get(legacy_opts, "file"); |
959 | ||
ee13ed1c KW |
960 | /* Check werror/rerror compatibility with if=... */ |
961 | werror = qemu_opt_get(legacy_opts, "werror"); | |
962 | if (werror != NULL) { | |
963 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && | |
964 | type != IF_NONE) { | |
c4f26c9f | 965 | error_setg(errp, "werror is not supported by this bus type"); |
ee13ed1c KW |
966 | goto fail; |
967 | } | |
46f5ac20 | 968 | qdict_put_str(bs_opts, "werror", werror); |
ee13ed1c KW |
969 | } |
970 | ||
971 | rerror = qemu_opt_get(legacy_opts, "rerror"); | |
972 | if (rerror != NULL) { | |
973 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && | |
974 | type != IF_NONE) { | |
c4f26c9f | 975 | error_setg(errp, "rerror is not supported by this bus type"); |
ee13ed1c KW |
976 | goto fail; |
977 | } | |
46f5ac20 | 978 | qdict_put_str(bs_opts, "rerror", rerror); |
ee13ed1c KW |
979 | } |
980 | ||
2d246f01 | 981 | /* Actual block device init: Functionality shared with blockdev-add */ |
992861fb | 982 | blk = blockdev_init(filename, bs_opts, errp); |
3cb0e25c | 983 | bs_opts = NULL; |
18e46a03 | 984 | if (!blk) { |
2d246f01 KW |
985 | goto fail; |
986 | } | |
987 | ||
26f8b3a8 MA |
988 | /* Create legacy DriveInfo */ |
989 | dinfo = g_malloc0(sizeof(*dinfo)); | |
f298d071 | 990 | dinfo->opts = all_opts; |
2d246f01 | 991 | |
ee13ed1c | 992 | dinfo->type = type; |
87a899c5 KW |
993 | dinfo->bus = bus_id; |
994 | dinfo->unit = unit_id; | |
bcf83158 | 995 | |
26f8b3a8 MA |
996 | blk_set_legacy_dinfo(blk, dinfo); |
997 | ||
e34ef046 KW |
998 | switch(type) { |
999 | case IF_IDE: | |
1000 | case IF_SCSI: | |
1001 | case IF_XEN: | |
1002 | case IF_NONE: | |
1003 | dinfo->media_cd = media == MEDIA_CDROM; | |
1004 | break; | |
1005 | default: | |
1006 | break; | |
1007 | } | |
1008 | ||
2d246f01 | 1009 | fail: |
33cb7dc8 | 1010 | qemu_opts_del(legacy_opts); |
cb3e7f08 | 1011 | qobject_unref(bs_opts); |
2d246f01 | 1012 | return dinfo; |
57975222 KW |
1013 | } |
1014 | ||
b6c1bae5 KW |
1015 | static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) |
1016 | { | |
1017 | BlockDriverState *bs; | |
1018 | ||
1019 | bs = bdrv_lookup_bs(name, name, errp); | |
1020 | if (bs == NULL) { | |
1021 | return NULL; | |
1022 | } | |
1023 | ||
1024 | if (!bdrv_is_root_node(bs)) { | |
1025 | error_setg(errp, "Need a root block node"); | |
1026 | return NULL; | |
1027 | } | |
1028 | ||
1029 | if (!bdrv_is_inserted(bs)) { | |
1030 | error_setg(errp, "Device has no medium"); | |
1031 | return NULL; | |
1032 | } | |
1033 | ||
1034 | return bs; | |
1035 | } | |
1036 | ||
10f75907 | 1037 | static void blockdev_do_action(TransactionAction *action, Error **errp) |
6cc2a415 | 1038 | { |
c8a83e85 | 1039 | TransactionActionList list; |
6cc2a415 | 1040 | |
10f75907 | 1041 | list.value = action; |
6cc2a415 | 1042 | list.next = NULL; |
94d16a64 | 1043 | qmp_transaction(&list, false, NULL, errp); |
6cc2a415 PB |
1044 | } |
1045 | ||
0901f67e BC |
1046 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
1047 | bool has_node_name, const char *node_name, | |
1048 | const char *snapshot_file, | |
1049 | bool has_snapshot_node_name, | |
1050 | const char *snapshot_node_name, | |
6106e249 | 1051 | bool has_format, const char *format, |
0901f67e | 1052 | bool has_mode, NewImageMode mode, Error **errp) |
f8882568 | 1053 | { |
a911e6ae | 1054 | BlockdevSnapshotSync snapshot = { |
0901f67e | 1055 | .has_device = has_device, |
6cc2a415 | 1056 | .device = (char *) device, |
0901f67e BC |
1057 | .has_node_name = has_node_name, |
1058 | .node_name = (char *) node_name, | |
6cc2a415 | 1059 | .snapshot_file = (char *) snapshot_file, |
0901f67e BC |
1060 | .has_snapshot_node_name = has_snapshot_node_name, |
1061 | .snapshot_node_name = (char *) snapshot_node_name, | |
6cc2a415 PB |
1062 | .has_format = has_format, |
1063 | .format = (char *) format, | |
1064 | .has_mode = has_mode, | |
1065 | .mode = mode, | |
1066 | }; | |
10f75907 EB |
1067 | TransactionAction action = { |
1068 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, | |
32bafa8f | 1069 | .u.blockdev_snapshot_sync.data = &snapshot, |
10f75907 EB |
1070 | }; |
1071 | blockdev_do_action(&action, errp); | |
f8882568 JS |
1072 | } |
1073 | ||
43de7e2d AG |
1074 | void qmp_blockdev_snapshot(const char *node, const char *overlay, |
1075 | Error **errp) | |
1076 | { | |
1077 | BlockdevSnapshot snapshot_data = { | |
1078 | .node = (char *) node, | |
1079 | .overlay = (char *) overlay | |
1080 | }; | |
10f75907 EB |
1081 | TransactionAction action = { |
1082 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, | |
32bafa8f | 1083 | .u.blockdev_snapshot.data = &snapshot_data, |
10f75907 EB |
1084 | }; |
1085 | blockdev_do_action(&action, errp); | |
43de7e2d AG |
1086 | } |
1087 | ||
f323bc9e WX |
1088 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
1089 | const char *name, | |
1090 | Error **errp) | |
1091 | { | |
1092 | BlockdevSnapshotInternal snapshot = { | |
1093 | .device = (char *) device, | |
1094 | .name = (char *) name | |
1095 | }; | |
10f75907 EB |
1096 | TransactionAction action = { |
1097 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
32bafa8f | 1098 | .u.blockdev_snapshot_internal_sync.data = &snapshot, |
10f75907 EB |
1099 | }; |
1100 | blockdev_do_action(&action, errp); | |
f323bc9e WX |
1101 | } |
1102 | ||
44e3e053 WX |
1103 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
1104 | bool has_id, | |
1105 | const char *id, | |
1106 | bool has_name, | |
1107 | const char *name, | |
1108 | Error **errp) | |
1109 | { | |
a0e8544c | 1110 | BlockDriverState *bs; |
4ef3982a | 1111 | AioContext *aio_context; |
44e3e053 WX |
1112 | QEMUSnapshotInfo sn; |
1113 | Error *local_err = NULL; | |
1114 | SnapshotInfo *info = NULL; | |
1115 | int ret; | |
1116 | ||
2dfb4c03 KW |
1117 | bs = qmp_get_root_bs(device, errp); |
1118 | if (!bs) { | |
44e3e053 WX |
1119 | return NULL; |
1120 | } | |
2dfb4c03 | 1121 | aio_context = bdrv_get_aio_context(bs); |
5433c24f | 1122 | aio_context_acquire(aio_context); |
44e3e053 WX |
1123 | |
1124 | if (!has_id) { | |
1125 | id = NULL; | |
1126 | } | |
1127 | ||
1128 | if (!has_name) { | |
1129 | name = NULL; | |
1130 | } | |
1131 | ||
1132 | if (!id && !name) { | |
1133 | error_setg(errp, "Name or id must be provided"); | |
5433c24f | 1134 | goto out_aio_context; |
44e3e053 WX |
1135 | } |
1136 | ||
0b928854 SH |
1137 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { |
1138 | goto out_aio_context; | |
1139 | } | |
1140 | ||
44e3e053 | 1141 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); |
84d18f06 | 1142 | if (local_err) { |
44e3e053 | 1143 | error_propagate(errp, local_err); |
4ef3982a | 1144 | goto out_aio_context; |
44e3e053 WX |
1145 | } |
1146 | if (!ret) { | |
1147 | error_setg(errp, | |
1148 | "Snapshot with id '%s' and name '%s' does not exist on " | |
1149 | "device '%s'", | |
1150 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
4ef3982a | 1151 | goto out_aio_context; |
44e3e053 WX |
1152 | } |
1153 | ||
1154 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
84d18f06 | 1155 | if (local_err) { |
44e3e053 | 1156 | error_propagate(errp, local_err); |
4ef3982a | 1157 | goto out_aio_context; |
44e3e053 WX |
1158 | } |
1159 | ||
4ef3982a SH |
1160 | aio_context_release(aio_context); |
1161 | ||
5839e53b | 1162 | info = g_new0(SnapshotInfo, 1); |
44e3e053 WX |
1163 | info->id = g_strdup(sn.id_str); |
1164 | info->name = g_strdup(sn.name); | |
1165 | info->date_nsec = sn.date_nsec; | |
1166 | info->date_sec = sn.date_sec; | |
1167 | info->vm_state_size = sn.vm_state_size; | |
1168 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
1169 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
b39847a5 PD |
1170 | if (sn.icount != -1ULL) { |
1171 | info->icount = sn.icount; | |
1172 | info->has_icount = true; | |
1173 | } | |
44e3e053 WX |
1174 | |
1175 | return info; | |
4ef3982a SH |
1176 | |
1177 | out_aio_context: | |
1178 | aio_context_release(aio_context); | |
1179 | return NULL; | |
44e3e053 | 1180 | } |
8802d1fd | 1181 | |
b756b9ce | 1182 | /* New and old BlockDriverState structs for atomic group operations */ |
ba0c86a3 | 1183 | |
50f43f0f | 1184 | typedef struct BlkActionState BlkActionState; |
ba0c86a3 | 1185 | |
50f43f0f JS |
1186 | /** |
1187 | * BlkActionOps: | |
1188 | * Table of operations that define an Action. | |
1189 | * | |
1190 | * @instance_size: Size of state struct, in bytes. | |
1191 | * @prepare: Prepare the work, must NOT be NULL. | |
1192 | * @commit: Commit the changes, can be NULL. | |
1193 | * @abort: Abort the changes on fail, can be NULL. | |
1194 | * @clean: Clean up resources after all transaction actions have called | |
1195 | * commit() or abort(). Can be NULL. | |
1196 | * | |
1197 | * Only prepare() may fail. In a single transaction, only one of commit() or | |
1198 | * abort() will be called. clean() will always be called if it is present. | |
1199 | */ | |
1200 | typedef struct BlkActionOps { | |
ba0c86a3 | 1201 | size_t instance_size; |
50f43f0f JS |
1202 | void (*prepare)(BlkActionState *common, Error **errp); |
1203 | void (*commit)(BlkActionState *common); | |
1204 | void (*abort)(BlkActionState *common); | |
1205 | void (*clean)(BlkActionState *common); | |
1206 | } BlkActionOps; | |
ba0c86a3 | 1207 | |
50f43f0f JS |
1208 | /** |
1209 | * BlkActionState: | |
1210 | * Describes one Action's state within a Transaction. | |
1211 | * | |
1212 | * @action: QAPI-defined enum identifying which Action to perform. | |
1213 | * @ops: Table of ActionOps this Action can perform. | |
94d16a64 | 1214 | * @block_job_txn: Transaction which this action belongs to. |
50f43f0f JS |
1215 | * @entry: List membership for all Actions in this Transaction. |
1216 | * | |
1217 | * This structure must be arranged as first member in a subclassed type, | |
1218 | * assuming that the compiler will also arrange it to the same offsets as the | |
1219 | * base class. | |
ba0c86a3 | 1220 | */ |
50f43f0f | 1221 | struct BlkActionState { |
c8a83e85 | 1222 | TransactionAction *action; |
50f43f0f | 1223 | const BlkActionOps *ops; |
62c9e416 | 1224 | JobTxn *block_job_txn; |
94d16a64 | 1225 | TransactionProperties *txn_props; |
f4de0f8c | 1226 | QTAILQ_ENTRY(BlkActionState) entry; |
ba0c86a3 WX |
1227 | }; |
1228 | ||
bbe86010 WX |
1229 | /* internal snapshot private data */ |
1230 | typedef struct InternalSnapshotState { | |
50f43f0f | 1231 | BlkActionState common; |
bbe86010 WX |
1232 | BlockDriverState *bs; |
1233 | QEMUSnapshotInfo sn; | |
507306cc | 1234 | bool created; |
bbe86010 WX |
1235 | } InternalSnapshotState; |
1236 | ||
94d16a64 JS |
1237 | |
1238 | static int action_check_completion_mode(BlkActionState *s, Error **errp) | |
1239 | { | |
1240 | if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
1241 | error_setg(errp, | |
1242 | "Action '%s' does not support Transaction property " | |
1243 | "completion-mode = %s", | |
977c736f MA |
1244 | TransactionActionKind_str(s->action->type), |
1245 | ActionCompletionMode_str(s->txn_props->completion_mode)); | |
94d16a64 JS |
1246 | return -1; |
1247 | } | |
1248 | return 0; | |
1249 | } | |
1250 | ||
50f43f0f | 1251 | static void internal_snapshot_prepare(BlkActionState *common, |
bbe86010 WX |
1252 | Error **errp) |
1253 | { | |
f70edf99 | 1254 | Error *local_err = NULL; |
bbe86010 WX |
1255 | const char *device; |
1256 | const char *name; | |
1257 | BlockDriverState *bs; | |
1258 | QEMUSnapshotInfo old_sn, *sn; | |
1259 | bool ret; | |
1260 | qemu_timeval tv; | |
1261 | BlockdevSnapshotInternal *internal; | |
1262 | InternalSnapshotState *state; | |
a36e458c | 1263 | AioContext *aio_context; |
bbe86010 WX |
1264 | int ret1; |
1265 | ||
6a8f9661 | 1266 | g_assert(common->action->type == |
bbe86010 | 1267 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); |
32bafa8f | 1268 | internal = common->action->u.blockdev_snapshot_internal_sync.data; |
bbe86010 WX |
1269 | state = DO_UPCAST(InternalSnapshotState, common, common); |
1270 | ||
1271 | /* 1. parse input */ | |
1272 | device = internal->device; | |
1273 | name = internal->name; | |
1274 | ||
1275 | /* 2. check for validation */ | |
94d16a64 JS |
1276 | if (action_check_completion_mode(common, errp) < 0) { |
1277 | return; | |
1278 | } | |
1279 | ||
75dfd402 KW |
1280 | bs = qmp_get_root_bs(device, errp); |
1281 | if (!bs) { | |
bbe86010 WX |
1282 | return; |
1283 | } | |
1284 | ||
a36e458c SH |
1285 | aio_context = bdrv_get_aio_context(bs); |
1286 | aio_context_acquire(aio_context); | |
5d6e96ef | 1287 | |
507306cc | 1288 | state->bs = bs; |
a36e458c SH |
1289 | |
1290 | /* Paired with .clean() */ | |
507306cc FZ |
1291 | bdrv_drained_begin(bs); |
1292 | ||
3dc7ca3c | 1293 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { |
a36e458c | 1294 | goto out; |
3dc7ca3c SH |
1295 | } |
1296 | ||
bbe86010 | 1297 | if (bdrv_is_read_only(bs)) { |
81e5f78a | 1298 | error_setg(errp, "Device '%s' is read only", device); |
a36e458c | 1299 | goto out; |
bbe86010 WX |
1300 | } |
1301 | ||
1302 | if (!bdrv_can_snapshot(bs)) { | |
81e5f78a AG |
1303 | error_setg(errp, "Block format '%s' used by device '%s' " |
1304 | "does not support internal snapshots", | |
1305 | bs->drv->format_name, device); | |
a36e458c | 1306 | goto out; |
bbe86010 WX |
1307 | } |
1308 | ||
1309 | if (!strlen(name)) { | |
1310 | error_setg(errp, "Name is empty"); | |
a36e458c | 1311 | goto out; |
bbe86010 WX |
1312 | } |
1313 | ||
1314 | /* check whether a snapshot with name exist */ | |
f70edf99 MA |
1315 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
1316 | &local_err); | |
1317 | if (local_err) { | |
1318 | error_propagate(errp, local_err); | |
a36e458c | 1319 | goto out; |
bbe86010 WX |
1320 | } else if (ret) { |
1321 | error_setg(errp, | |
1322 | "Snapshot with name '%s' already exists on device '%s'", | |
1323 | name, device); | |
a36e458c | 1324 | goto out; |
bbe86010 WX |
1325 | } |
1326 | ||
1327 | /* 3. take the snapshot */ | |
1328 | sn = &state->sn; | |
1329 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1330 | qemu_gettimeofday(&tv); | |
1331 | sn->date_sec = tv.tv_sec; | |
1332 | sn->date_nsec = tv.tv_usec * 1000; | |
1333 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
b39847a5 PD |
1334 | if (replay_mode != REPLAY_MODE_NONE) { |
1335 | sn->icount = replay_get_current_icount(); | |
1336 | } else { | |
1337 | sn->icount = -1ULL; | |
1338 | } | |
bbe86010 WX |
1339 | |
1340 | ret1 = bdrv_snapshot_create(bs, sn); | |
1341 | if (ret1 < 0) { | |
1342 | error_setg_errno(errp, -ret1, | |
1343 | "Failed to create snapshot '%s' on device '%s'", | |
1344 | name, device); | |
a36e458c | 1345 | goto out; |
bbe86010 WX |
1346 | } |
1347 | ||
1348 | /* 4. succeed, mark a snapshot is created */ | |
507306cc | 1349 | state->created = true; |
a36e458c SH |
1350 | |
1351 | out: | |
1352 | aio_context_release(aio_context); | |
bbe86010 WX |
1353 | } |
1354 | ||
50f43f0f | 1355 | static void internal_snapshot_abort(BlkActionState *common) |
bbe86010 WX |
1356 | { |
1357 | InternalSnapshotState *state = | |
1358 | DO_UPCAST(InternalSnapshotState, common, common); | |
1359 | BlockDriverState *bs = state->bs; | |
1360 | QEMUSnapshotInfo *sn = &state->sn; | |
a36e458c | 1361 | AioContext *aio_context; |
bbe86010 WX |
1362 | Error *local_error = NULL; |
1363 | ||
507306cc | 1364 | if (!state->created) { |
bbe86010 WX |
1365 | return; |
1366 | } | |
1367 | ||
a36e458c SH |
1368 | aio_context = bdrv_get_aio_context(state->bs); |
1369 | aio_context_acquire(aio_context); | |
1370 | ||
bbe86010 | 1371 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { |
c29b77f9 MA |
1372 | error_reportf_err(local_error, |
1373 | "Failed to delete snapshot with id '%s' and " | |
1374 | "name '%s' on device '%s' in abort: ", | |
1375 | sn->id_str, sn->name, | |
1376 | bdrv_get_device_name(bs)); | |
bbe86010 | 1377 | } |
a36e458c SH |
1378 | |
1379 | aio_context_release(aio_context); | |
bbe86010 WX |
1380 | } |
1381 | ||
50f43f0f | 1382 | static void internal_snapshot_clean(BlkActionState *common) |
5d6e96ef SH |
1383 | { |
1384 | InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, | |
1385 | common, common); | |
a36e458c | 1386 | AioContext *aio_context; |
5d6e96ef | 1387 | |
a36e458c SH |
1388 | if (!state->bs) { |
1389 | return; | |
5d6e96ef | 1390 | } |
a36e458c SH |
1391 | |
1392 | aio_context = bdrv_get_aio_context(state->bs); | |
1393 | aio_context_acquire(aio_context); | |
1394 | ||
1395 | bdrv_drained_end(state->bs); | |
1396 | ||
1397 | aio_context_release(aio_context); | |
5d6e96ef SH |
1398 | } |
1399 | ||
ba0c86a3 | 1400 | /* external snapshot private data */ |
ba5d6ab6 | 1401 | typedef struct ExternalSnapshotState { |
50f43f0f | 1402 | BlkActionState common; |
8802d1fd JC |
1403 | BlockDriverState *old_bs; |
1404 | BlockDriverState *new_bs; | |
067acf28 | 1405 | bool overlay_appended; |
ba5d6ab6 | 1406 | } ExternalSnapshotState; |
8802d1fd | 1407 | |
50f43f0f | 1408 | static void external_snapshot_prepare(BlkActionState *common, |
9b9877ee WX |
1409 | Error **errp) |
1410 | { | |
934aee14 | 1411 | int ret; |
5b363937 | 1412 | int flags = 0; |
43de7e2d | 1413 | QDict *options = NULL; |
9b9877ee | 1414 | Error *local_err = NULL; |
43de7e2d | 1415 | /* Device and node name of the image to generate the snapshot from */ |
e2a31e87 | 1416 | const char *device; |
0901f67e | 1417 | const char *node_name; |
43de7e2d AG |
1418 | /* Reference to the new image (for 'blockdev-snapshot') */ |
1419 | const char *snapshot_ref; | |
1420 | /* File name of the new image (for 'blockdev-snapshot-sync') */ | |
e2a31e87 | 1421 | const char *new_image_file; |
ba5d6ab6 SH |
1422 | ExternalSnapshotState *state = |
1423 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1424 | TransactionAction *action = common->action; |
2d24b60b | 1425 | AioContext *aio_context; |
d29d3d1f | 1426 | uint64_t perm, shared; |
9b9877ee | 1427 | |
43de7e2d AG |
1428 | /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar |
1429 | * purpose but a different set of parameters */ | |
1430 | switch (action->type) { | |
1431 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: | |
1432 | { | |
32bafa8f | 1433 | BlockdevSnapshot *s = action->u.blockdev_snapshot.data; |
43de7e2d AG |
1434 | device = s->node; |
1435 | node_name = s->node; | |
1436 | new_image_file = NULL; | |
1437 | snapshot_ref = s->overlay; | |
1438 | } | |
1439 | break; | |
1440 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: | |
1441 | { | |
32bafa8f | 1442 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
43de7e2d AG |
1443 | device = s->has_device ? s->device : NULL; |
1444 | node_name = s->has_node_name ? s->node_name : NULL; | |
1445 | new_image_file = s->snapshot_file; | |
1446 | snapshot_ref = NULL; | |
1447 | } | |
1448 | break; | |
1449 | default: | |
1450 | g_assert_not_reached(); | |
e2a31e87 WX |
1451 | } |
1452 | ||
1453 | /* start processing */ | |
94d16a64 JS |
1454 | if (action_check_completion_mode(common, errp) < 0) { |
1455 | return; | |
1456 | } | |
1457 | ||
43de7e2d AG |
1458 | state->old_bs = bdrv_lookup_bs(device, node_name, errp); |
1459 | if (!state->old_bs) { | |
9b9877ee WX |
1460 | return; |
1461 | } | |
1462 | ||
2d24b60b SH |
1463 | aio_context = bdrv_get_aio_context(state->old_bs); |
1464 | aio_context_acquire(aio_context); | |
1465 | ||
1466 | /* Paired with .clean() */ | |
da763e83 | 1467 | bdrv_drained_begin(state->old_bs); |
5d6e96ef | 1468 | |
ba5d6ab6 | 1469 | if (!bdrv_is_inserted(state->old_bs)) { |
c6bd8c70 | 1470 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
2d24b60b | 1471 | goto out; |
9b9877ee WX |
1472 | } |
1473 | ||
3718d8ab FZ |
1474 | if (bdrv_op_is_blocked(state->old_bs, |
1475 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
2d24b60b | 1476 | goto out; |
9b9877ee WX |
1477 | } |
1478 | ||
ba5d6ab6 SH |
1479 | if (!bdrv_is_read_only(state->old_bs)) { |
1480 | if (bdrv_flush(state->old_bs)) { | |
c6bd8c70 | 1481 | error_setg(errp, QERR_IO_ERROR); |
2d24b60b | 1482 | goto out; |
9b9877ee WX |
1483 | } |
1484 | } | |
1485 | ||
43de7e2d | 1486 | if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { |
32bafa8f | 1487 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
43de7e2d AG |
1488 | const char *format = s->has_format ? s->format : "qcow2"; |
1489 | enum NewImageMode mode; | |
1490 | const char *snapshot_node_name = | |
1491 | s->has_snapshot_node_name ? s->snapshot_node_name : NULL; | |
9b9877ee | 1492 | |
43de7e2d | 1493 | if (node_name && !snapshot_node_name) { |
ef2e38a1 | 1494 | error_setg(errp, "New overlay node-name missing"); |
2d24b60b | 1495 | goto out; |
9b9877ee | 1496 | } |
9b9877ee | 1497 | |
43de7e2d AG |
1498 | if (snapshot_node_name && |
1499 | bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { | |
ef2e38a1 | 1500 | error_setg(errp, "New overlay node-name already in use"); |
2d24b60b | 1501 | goto out; |
43de7e2d AG |
1502 | } |
1503 | ||
1504 | flags = state->old_bs->open_flags; | |
2a32c6e8 JS |
1505 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ); |
1506 | flags |= BDRV_O_NO_BACKING; | |
43de7e2d AG |
1507 | |
1508 | /* create new image w/backing file */ | |
1509 | mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1510 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
f86b8b58 KW |
1511 | int64_t size = bdrv_getlength(state->old_bs); |
1512 | if (size < 0) { | |
1513 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
2d24b60b | 1514 | goto out; |
f86b8b58 | 1515 | } |
f30c66ba | 1516 | bdrv_refresh_filename(state->old_bs); |
43de7e2d AG |
1517 | bdrv_img_create(new_image_file, format, |
1518 | state->old_bs->filename, | |
1519 | state->old_bs->drv->format_name, | |
9217283d | 1520 | NULL, size, flags, false, &local_err); |
43de7e2d AG |
1521 | if (local_err) { |
1522 | error_propagate(errp, local_err); | |
2d24b60b | 1523 | goto out; |
43de7e2d AG |
1524 | } |
1525 | } | |
1526 | ||
1527 | options = qdict_new(); | |
d52e1a0e | 1528 | if (snapshot_node_name) { |
46f5ac20 | 1529 | qdict_put_str(options, "node-name", snapshot_node_name); |
43de7e2d | 1530 | } |
46f5ac20 | 1531 | qdict_put_str(options, "driver", format); |
0901f67e BC |
1532 | } |
1533 | ||
5b363937 HR |
1534 | state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, |
1535 | errp); | |
f67503e5 | 1536 | /* We will manually add the backing_hd field to the bs later */ |
5b363937 | 1537 | if (!state->new_bs) { |
2d24b60b | 1538 | goto out; |
43de7e2d AG |
1539 | } |
1540 | ||
d29d3d1f KW |
1541 | /* |
1542 | * Allow attaching a backing file to an overlay that's already in use only | |
1543 | * if the parents don't assume that they are already seeing a valid image. | |
1544 | * (Specifically, allow it as a mirror target, which is write-only access.) | |
1545 | */ | |
1546 | bdrv_get_cumulative_perm(state->new_bs, &perm, &shared); | |
1547 | if (perm & BLK_PERM_CONSISTENT_READ) { | |
a2bb6f8c | 1548 | error_setg(errp, "The overlay is already in use"); |
2d24b60b | 1549 | goto out; |
43de7e2d AG |
1550 | } |
1551 | ||
7cc734a9 HR |
1552 | if (state->new_bs->drv->is_filter) { |
1553 | error_setg(errp, "Filters cannot be used as overlays"); | |
1554 | goto out; | |
1555 | } | |
1556 | ||
1557 | if (bdrv_cow_child(state->new_bs)) { | |
a2bb6f8c | 1558 | error_setg(errp, "The overlay already has a backing image"); |
2d24b60b | 1559 | goto out; |
08b24cfe AG |
1560 | } |
1561 | ||
1562 | if (!state->new_bs->drv->supports_backing) { | |
a2bb6f8c | 1563 | error_setg(errp, "The overlay does not support backing images"); |
2d24b60b | 1564 | goto out; |
b2c2832c KW |
1565 | } |
1566 | ||
934aee14 VSO |
1567 | ret = bdrv_append(state->new_bs, state->old_bs, errp); |
1568 | if (ret < 0) { | |
2d24b60b | 1569 | goto out; |
9b9877ee | 1570 | } |
067acf28 | 1571 | state->overlay_appended = true; |
2d24b60b SH |
1572 | |
1573 | out: | |
1574 | aio_context_release(aio_context); | |
9b9877ee WX |
1575 | } |
1576 | ||
50f43f0f | 1577 | static void external_snapshot_commit(BlkActionState *common) |
3b0047e8 | 1578 | { |
ba5d6ab6 SH |
1579 | ExternalSnapshotState *state = |
1580 | DO_UPCAST(ExternalSnapshotState, common, common); | |
2d24b60b SH |
1581 | AioContext *aio_context; |
1582 | ||
1583 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1584 | aio_context_acquire(aio_context); | |
ba0c86a3 | 1585 | |
3b0047e8 WX |
1586 | /* We don't need (or want) to use the transactional |
1587 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1588 | * don't want to abort all of them if one of them fails the reopen */ | |
d73415a3 | 1589 | if (!qatomic_read(&state->old_bs->copy_on_read)) { |
1b57774f | 1590 | bdrv_reopen_set_read_only(state->old_bs, true, NULL); |
4c844983 | 1591 | } |
2d24b60b SH |
1592 | |
1593 | aio_context_release(aio_context); | |
3b0047e8 WX |
1594 | } |
1595 | ||
50f43f0f | 1596 | static void external_snapshot_abort(BlkActionState *common) |
96b86bf7 | 1597 | { |
ba5d6ab6 SH |
1598 | ExternalSnapshotState *state = |
1599 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1600 | if (state->new_bs) { | |
067acf28 | 1601 | if (state->overlay_appended) { |
2d24b60b | 1602 | AioContext *aio_context; |
377410f6 SL |
1603 | AioContext *tmp_context; |
1604 | int ret; | |
2d24b60b SH |
1605 | |
1606 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1607 | aio_context_acquire(aio_context); | |
1608 | ||
719fc28c JC |
1609 | bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() |
1610 | close state->old_bs; we need it */ | |
1611 | bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); | |
377410f6 SL |
1612 | |
1613 | /* | |
1614 | * The call to bdrv_set_backing_hd() above returns state->old_bs to | |
1615 | * the main AioContext. As we're still going to be using it, return | |
1616 | * it to the AioContext it was before. | |
1617 | */ | |
1618 | tmp_context = bdrv_get_aio_context(state->old_bs); | |
1619 | if (aio_context != tmp_context) { | |
1620 | aio_context_release(aio_context); | |
1621 | aio_context_acquire(tmp_context); | |
1622 | ||
1623 | ret = bdrv_try_set_aio_context(state->old_bs, | |
1624 | aio_context, NULL); | |
1625 | assert(ret == 0); | |
1626 | ||
1627 | aio_context_release(tmp_context); | |
1628 | aio_context_acquire(aio_context); | |
1629 | } | |
1630 | ||
5fe31c25 | 1631 | bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); |
719fc28c | 1632 | bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ |
2d24b60b SH |
1633 | |
1634 | aio_context_release(aio_context); | |
b2c2832c | 1635 | } |
96b86bf7 | 1636 | } |
da763e83 FZ |
1637 | } |
1638 | ||
50f43f0f | 1639 | static void external_snapshot_clean(BlkActionState *common) |
da763e83 FZ |
1640 | { |
1641 | ExternalSnapshotState *state = | |
1642 | DO_UPCAST(ExternalSnapshotState, common, common); | |
2d24b60b SH |
1643 | AioContext *aio_context; |
1644 | ||
1645 | if (!state->old_bs) { | |
1646 | return; | |
5d6e96ef | 1647 | } |
2d24b60b SH |
1648 | |
1649 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1650 | aio_context_acquire(aio_context); | |
1651 | ||
1652 | bdrv_drained_end(state->old_bs); | |
1653 | bdrv_unref(state->new_bs); | |
1654 | ||
1655 | aio_context_release(aio_context); | |
96b86bf7 WX |
1656 | } |
1657 | ||
3037f364 | 1658 | typedef struct DriveBackupState { |
50f43f0f | 1659 | BlkActionState common; |
3037f364 SH |
1660 | BlockDriverState *bs; |
1661 | BlockJob *job; | |
1662 | } DriveBackupState; | |
1663 | ||
2288ccfa SL |
1664 | static BlockJob *do_backup_common(BackupCommon *backup, |
1665 | BlockDriverState *bs, | |
1666 | BlockDriverState *target_bs, | |
1667 | AioContext *aio_context, | |
1668 | JobTxn *txn, Error **errp); | |
78f51fde | 1669 | |
50f43f0f | 1670 | static void drive_backup_prepare(BlkActionState *common, Error **errp) |
3037f364 SH |
1671 | { |
1672 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1673 | DriveBackup *backup; | |
2288ccfa SL |
1674 | BlockDriverState *bs; |
1675 | BlockDriverState *target_bs; | |
1676 | BlockDriverState *source = NULL; | |
66d56054 | 1677 | AioContext *aio_context; |
3ea67e08 | 1678 | AioContext *old_context; |
2288ccfa | 1679 | QDict *options; |
3037f364 | 1680 | Error *local_err = NULL; |
2288ccfa SL |
1681 | int flags; |
1682 | int64_t size; | |
1683 | bool set_backing_hd = false; | |
3ea67e08 | 1684 | int ret; |
3037f364 | 1685 | |
6a8f9661 | 1686 | assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); |
32bafa8f | 1687 | backup = common->action->u.drive_backup.data; |
3037f364 | 1688 | |
2288ccfa SL |
1689 | if (!backup->has_mode) { |
1690 | backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1691 | } | |
1692 | ||
85c9d133 | 1693 | bs = bdrv_lookup_bs(backup->device, backup->device, errp); |
b7e4fa22 | 1694 | if (!bs) { |
1fdd4b7b FZ |
1695 | return; |
1696 | } | |
1697 | ||
2288ccfa SL |
1698 | if (!bs->drv) { |
1699 | error_setg(errp, "Device has no medium"); | |
1700 | return; | |
1701 | } | |
1702 | ||
66d56054 SH |
1703 | aio_context = bdrv_get_aio_context(bs); |
1704 | aio_context_acquire(aio_context); | |
1705 | ||
2842ff2d | 1706 | state->bs = bs; |
66d56054 | 1707 | /* Paired with .clean() */ |
b7e4fa22 | 1708 | bdrv_drained_begin(bs); |
66d56054 | 1709 | |
2288ccfa SL |
1710 | if (!backup->has_format) { |
1711 | backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? | |
1712 | NULL : (char *) bs->drv->format_name; | |
1713 | } | |
1714 | ||
1715 | /* Early check to avoid creating target */ | |
1716 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { | |
1717 | goto out; | |
1718 | } | |
1719 | ||
1720 | flags = bs->open_flags | BDRV_O_RDWR; | |
1721 | ||
1722 | /* | |
1723 | * See if we have a backing HD we can use to create our new image | |
1724 | * on top of. | |
1725 | */ | |
1726 | if (backup->sync == MIRROR_SYNC_MODE_TOP) { | |
2b088c60 HR |
1727 | /* |
1728 | * Backup will not replace the source by the target, so none | |
1729 | * of the filters skipped here will be removed (in contrast to | |
1730 | * mirror). Therefore, we can skip all of them when looking | |
1731 | * for the first COW relationship. | |
1732 | */ | |
1733 | source = bdrv_cow_bs(bdrv_skip_filters(bs)); | |
2288ccfa SL |
1734 | if (!source) { |
1735 | backup->sync = MIRROR_SYNC_MODE_FULL; | |
1736 | } | |
1737 | } | |
1738 | if (backup->sync == MIRROR_SYNC_MODE_NONE) { | |
1739 | source = bs; | |
1740 | flags |= BDRV_O_NO_BACKING; | |
1741 | set_backing_hd = true; | |
1742 | } | |
1743 | ||
1744 | size = bdrv_getlength(bs); | |
1745 | if (size < 0) { | |
1746 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
1747 | goto out; | |
1748 | } | |
1749 | ||
1750 | if (backup->mode != NEW_IMAGE_MODE_EXISTING) { | |
1751 | assert(backup->format); | |
1752 | if (source) { | |
2b088c60 HR |
1753 | /* Implicit filters should not appear in the filename */ |
1754 | BlockDriverState *explicit_backing = | |
1755 | bdrv_skip_implicit_filters(source); | |
1756 | ||
1757 | bdrv_refresh_filename(explicit_backing); | |
1758 | bdrv_img_create(backup->target, backup->format, | |
1759 | explicit_backing->filename, | |
1760 | explicit_backing->drv->format_name, NULL, | |
2288ccfa SL |
1761 | size, flags, false, &local_err); |
1762 | } else { | |
1763 | bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, | |
1764 | size, flags, false, &local_err); | |
1765 | } | |
1766 | } | |
5d6e96ef | 1767 | |
84d18f06 | 1768 | if (local_err) { |
3037f364 | 1769 | error_propagate(errp, local_err); |
66d56054 | 1770 | goto out; |
3037f364 | 1771 | } |
66d56054 | 1772 | |
2288ccfa SL |
1773 | options = qdict_new(); |
1774 | qdict_put_str(options, "discard", "unmap"); | |
1775 | qdict_put_str(options, "detect-zeroes", "unmap"); | |
1776 | if (backup->format) { | |
1777 | qdict_put_str(options, "driver", backup->format); | |
1778 | } | |
1779 | ||
1780 | target_bs = bdrv_open(backup->target, NULL, options, flags, errp); | |
1781 | if (!target_bs) { | |
1782 | goto out; | |
1783 | } | |
1784 | ||
3ea67e08 SL |
1785 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ |
1786 | old_context = bdrv_get_aio_context(target_bs); | |
1787 | aio_context_release(aio_context); | |
1788 | aio_context_acquire(old_context); | |
1789 | ||
1790 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); | |
1791 | if (ret < 0) { | |
1792 | bdrv_unref(target_bs); | |
1793 | aio_context_release(old_context); | |
1794 | return; | |
1795 | } | |
1796 | ||
1797 | aio_context_release(old_context); | |
1798 | aio_context_acquire(aio_context); | |
1799 | ||
2288ccfa | 1800 | if (set_backing_hd) { |
5a11a1ca | 1801 | if (bdrv_set_backing_hd(target_bs, source, errp) < 0) { |
2288ccfa SL |
1802 | goto unref; |
1803 | } | |
1804 | } | |
1805 | ||
2288ccfa SL |
1806 | state->job = do_backup_common(qapi_DriveBackup_base(backup), |
1807 | bs, target_bs, aio_context, | |
1808 | common->block_job_txn, errp); | |
1809 | ||
1810 | unref: | |
1811 | bdrv_unref(target_bs); | |
66d56054 SH |
1812 | out: |
1813 | aio_context_release(aio_context); | |
111049a4 | 1814 | } |
3037f364 | 1815 | |
111049a4 JS |
1816 | static void drive_backup_commit(BlkActionState *common) |
1817 | { | |
1818 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
66d56054 SH |
1819 | AioContext *aio_context; |
1820 | ||
1821 | aio_context = bdrv_get_aio_context(state->bs); | |
1822 | aio_context_acquire(aio_context); | |
1823 | ||
111049a4 | 1824 | assert(state->job); |
da01ff7f | 1825 | job_start(&state->job->job); |
66d56054 SH |
1826 | |
1827 | aio_context_release(aio_context); | |
3037f364 SH |
1828 | } |
1829 | ||
50f43f0f | 1830 | static void drive_backup_abort(BlkActionState *common) |
3037f364 SH |
1831 | { |
1832 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
3037f364 | 1833 | |
111049a4 | 1834 | if (state->job) { |
66d56054 SH |
1835 | AioContext *aio_context; |
1836 | ||
1837 | aio_context = bdrv_get_aio_context(state->bs); | |
1838 | aio_context_acquire(aio_context); | |
1839 | ||
4cfb3f05 | 1840 | job_cancel_sync(&state->job->job, true); |
66d56054 SH |
1841 | |
1842 | aio_context_release(aio_context); | |
3037f364 SH |
1843 | } |
1844 | } | |
1845 | ||
50f43f0f | 1846 | static void drive_backup_clean(BlkActionState *common) |
5d6e96ef SH |
1847 | { |
1848 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
66d56054 | 1849 | AioContext *aio_context; |
5d6e96ef | 1850 | |
66d56054 SH |
1851 | if (!state->bs) { |
1852 | return; | |
5d6e96ef | 1853 | } |
66d56054 SH |
1854 | |
1855 | aio_context = bdrv_get_aio_context(state->bs); | |
1856 | aio_context_acquire(aio_context); | |
1857 | ||
1858 | bdrv_drained_end(state->bs); | |
1859 | ||
1860 | aio_context_release(aio_context); | |
5d6e96ef SH |
1861 | } |
1862 | ||
bd8baecd | 1863 | typedef struct BlockdevBackupState { |
50f43f0f | 1864 | BlkActionState common; |
bd8baecd FZ |
1865 | BlockDriverState *bs; |
1866 | BlockJob *job; | |
bd8baecd FZ |
1867 | } BlockdevBackupState; |
1868 | ||
50f43f0f | 1869 | static void blockdev_backup_prepare(BlkActionState *common, Error **errp) |
bd8baecd FZ |
1870 | { |
1871 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1872 | BlockdevBackup *backup; | |
5b7bfe51 SL |
1873 | BlockDriverState *bs; |
1874 | BlockDriverState *target_bs; | |
edd5adee | 1875 | AioContext *aio_context; |
3ea67e08 SL |
1876 | AioContext *old_context; |
1877 | int ret; | |
bd8baecd | 1878 | |
6a8f9661 | 1879 | assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); |
32bafa8f | 1880 | backup = common->action->u.blockdev_backup.data; |
bd8baecd | 1881 | |
930fe17f | 1882 | bs = bdrv_lookup_bs(backup->device, backup->device, errp); |
cef34eeb | 1883 | if (!bs) { |
ff52bf36 FZ |
1884 | return; |
1885 | } | |
1886 | ||
5b7bfe51 SL |
1887 | target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); |
1888 | if (!target_bs) { | |
bd8baecd FZ |
1889 | return; |
1890 | } | |
1891 | ||
3ea67e08 | 1892 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ |
edd5adee | 1893 | aio_context = bdrv_get_aio_context(bs); |
3ea67e08 SL |
1894 | old_context = bdrv_get_aio_context(target_bs); |
1895 | aio_context_acquire(old_context); | |
1896 | ||
1897 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); | |
1898 | if (ret < 0) { | |
1899 | aio_context_release(old_context); | |
1900 | return; | |
1901 | } | |
1902 | ||
1903 | aio_context_release(old_context); | |
edd5adee | 1904 | aio_context_acquire(aio_context); |
cef34eeb | 1905 | state->bs = bs; |
edd5adee SH |
1906 | |
1907 | /* Paired with .clean() */ | |
ff52bf36 | 1908 | bdrv_drained_begin(state->bs); |
bd8baecd | 1909 | |
5b7bfe51 SL |
1910 | state->job = do_backup_common(qapi_BlockdevBackup_base(backup), |
1911 | bs, target_bs, aio_context, | |
1912 | common->block_job_txn, errp); | |
edd5adee | 1913 | |
edd5adee | 1914 | aio_context_release(aio_context); |
111049a4 | 1915 | } |
bd8baecd | 1916 | |
111049a4 JS |
1917 | static void blockdev_backup_commit(BlkActionState *common) |
1918 | { | |
1919 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
edd5adee SH |
1920 | AioContext *aio_context; |
1921 | ||
1922 | aio_context = bdrv_get_aio_context(state->bs); | |
1923 | aio_context_acquire(aio_context); | |
1924 | ||
111049a4 | 1925 | assert(state->job); |
da01ff7f | 1926 | job_start(&state->job->job); |
edd5adee SH |
1927 | |
1928 | aio_context_release(aio_context); | |
bd8baecd FZ |
1929 | } |
1930 | ||
50f43f0f | 1931 | static void blockdev_backup_abort(BlkActionState *common) |
bd8baecd FZ |
1932 | { |
1933 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
bd8baecd | 1934 | |
111049a4 | 1935 | if (state->job) { |
edd5adee SH |
1936 | AioContext *aio_context; |
1937 | ||
1938 | aio_context = bdrv_get_aio_context(state->bs); | |
1939 | aio_context_acquire(aio_context); | |
1940 | ||
4cfb3f05 | 1941 | job_cancel_sync(&state->job->job, true); |
edd5adee SH |
1942 | |
1943 | aio_context_release(aio_context); | |
bd8baecd FZ |
1944 | } |
1945 | } | |
1946 | ||
50f43f0f | 1947 | static void blockdev_backup_clean(BlkActionState *common) |
bd8baecd FZ |
1948 | { |
1949 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
edd5adee | 1950 | AioContext *aio_context; |
bd8baecd | 1951 | |
edd5adee SH |
1952 | if (!state->bs) { |
1953 | return; | |
bd8baecd | 1954 | } |
edd5adee SH |
1955 | |
1956 | aio_context = bdrv_get_aio_context(state->bs); | |
1957 | aio_context_acquire(aio_context); | |
1958 | ||
1959 | bdrv_drained_end(state->bs); | |
1960 | ||
1961 | aio_context_release(aio_context); | |
bd8baecd FZ |
1962 | } |
1963 | ||
df9a681d | 1964 | typedef struct BlockDirtyBitmapState { |
50f43f0f | 1965 | BlkActionState common; |
df9a681d FZ |
1966 | BdrvDirtyBitmap *bitmap; |
1967 | BlockDriverState *bs; | |
df9a681d FZ |
1968 | HBitmap *backup; |
1969 | bool prepared; | |
c6490447 | 1970 | bool was_enabled; |
df9a681d FZ |
1971 | } BlockDirtyBitmapState; |
1972 | ||
50f43f0f | 1973 | static void block_dirty_bitmap_add_prepare(BlkActionState *common, |
df9a681d FZ |
1974 | Error **errp) |
1975 | { | |
1976 | Error *local_err = NULL; | |
1977 | BlockDirtyBitmapAdd *action; | |
1978 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
1979 | common, common); | |
1980 | ||
94d16a64 JS |
1981 | if (action_check_completion_mode(common, errp) < 0) { |
1982 | return; | |
1983 | } | |
1984 | ||
32bafa8f | 1985 | action = common->action->u.block_dirty_bitmap_add.data; |
df9a681d FZ |
1986 | /* AIO context taken and released within qmp_block_dirty_bitmap_add */ |
1987 | qmp_block_dirty_bitmap_add(action->node, action->name, | |
1988 | action->has_granularity, action->granularity, | |
fd5ae4cc | 1989 | action->has_persistent, action->persistent, |
0e2b7f09 | 1990 | action->has_disabled, action->disabled, |
df9a681d FZ |
1991 | &local_err); |
1992 | ||
1993 | if (!local_err) { | |
1994 | state->prepared = true; | |
1995 | } else { | |
1996 | error_propagate(errp, local_err); | |
1997 | } | |
1998 | } | |
1999 | ||
50f43f0f | 2000 | static void block_dirty_bitmap_add_abort(BlkActionState *common) |
df9a681d FZ |
2001 | { |
2002 | BlockDirtyBitmapAdd *action; | |
2003 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2004 | common, common); | |
2005 | ||
32bafa8f | 2006 | action = common->action->u.block_dirty_bitmap_add.data; |
df9a681d FZ |
2007 | /* Should not be able to fail: IF the bitmap was added via .prepare(), |
2008 | * then the node reference and bitmap name must have been valid. | |
2009 | */ | |
2010 | if (state->prepared) { | |
2011 | qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); | |
2012 | } | |
2013 | } | |
2014 | ||
50f43f0f | 2015 | static void block_dirty_bitmap_clear_prepare(BlkActionState *common, |
df9a681d FZ |
2016 | Error **errp) |
2017 | { | |
2018 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2019 | common, common); | |
2020 | BlockDirtyBitmap *action; | |
2021 | ||
94d16a64 JS |
2022 | if (action_check_completion_mode(common, errp) < 0) { |
2023 | return; | |
2024 | } | |
2025 | ||
32bafa8f | 2026 | action = common->action->u.block_dirty_bitmap_clear.data; |
df9a681d FZ |
2027 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
2028 | action->name, | |
2029 | &state->bs, | |
df9a681d FZ |
2030 | errp); |
2031 | if (!state->bitmap) { | |
2032 | return; | |
2033 | } | |
2034 | ||
3ae96d66 | 2035 | if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) { |
d6883bc9 | 2036 | return; |
df9a681d FZ |
2037 | } |
2038 | ||
2039 | bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); | |
df9a681d FZ |
2040 | } |
2041 | ||
5c4cf8b2 | 2042 | static void block_dirty_bitmap_restore(BlkActionState *common) |
df9a681d FZ |
2043 | { |
2044 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2045 | common, common); | |
2046 | ||
184dd9c4 | 2047 | if (state->backup) { |
56bd6624 | 2048 | bdrv_restore_dirty_bitmap(state->bitmap, state->backup); |
184dd9c4 | 2049 | } |
df9a681d FZ |
2050 | } |
2051 | ||
5c4cf8b2 | 2052 | static void block_dirty_bitmap_free_backup(BlkActionState *common) |
df9a681d FZ |
2053 | { |
2054 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2055 | common, common); | |
2056 | ||
2057 | hbitmap_free(state->backup); | |
2058 | } | |
2059 | ||
c6490447 VSO |
2060 | static void block_dirty_bitmap_enable_prepare(BlkActionState *common, |
2061 | Error **errp) | |
2062 | { | |
2063 | BlockDirtyBitmap *action; | |
2064 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2065 | common, common); | |
2066 | ||
2067 | if (action_check_completion_mode(common, errp) < 0) { | |
2068 | return; | |
2069 | } | |
2070 | ||
0e2b7f09 | 2071 | action = common->action->u.block_dirty_bitmap_enable.data; |
c6490447 VSO |
2072 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
2073 | action->name, | |
2074 | NULL, | |
2075 | errp); | |
2076 | if (!state->bitmap) { | |
2077 | return; | |
2078 | } | |
2079 | ||
3ae96d66 | 2080 | if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
b053bb55 JS |
2081 | return; |
2082 | } | |
2083 | ||
c6490447 VSO |
2084 | state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); |
2085 | bdrv_enable_dirty_bitmap(state->bitmap); | |
2086 | } | |
2087 | ||
2088 | static void block_dirty_bitmap_enable_abort(BlkActionState *common) | |
2089 | { | |
2090 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2091 | common, common); | |
2092 | ||
2093 | if (!state->was_enabled) { | |
2094 | bdrv_disable_dirty_bitmap(state->bitmap); | |
2095 | } | |
2096 | } | |
2097 | ||
2098 | static void block_dirty_bitmap_disable_prepare(BlkActionState *common, | |
2099 | Error **errp) | |
2100 | { | |
2101 | BlockDirtyBitmap *action; | |
2102 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2103 | common, common); | |
2104 | ||
2105 | if (action_check_completion_mode(common, errp) < 0) { | |
2106 | return; | |
2107 | } | |
2108 | ||
0e2b7f09 | 2109 | action = common->action->u.block_dirty_bitmap_disable.data; |
c6490447 VSO |
2110 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
2111 | action->name, | |
2112 | NULL, | |
2113 | errp); | |
2114 | if (!state->bitmap) { | |
2115 | return; | |
2116 | } | |
2117 | ||
3ae96d66 | 2118 | if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
b053bb55 JS |
2119 | return; |
2120 | } | |
2121 | ||
c6490447 VSO |
2122 | state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); |
2123 | bdrv_disable_dirty_bitmap(state->bitmap); | |
2124 | } | |
2125 | ||
2126 | static void block_dirty_bitmap_disable_abort(BlkActionState *common) | |
2127 | { | |
2128 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2129 | common, common); | |
2130 | ||
2131 | if (state->was_enabled) { | |
2132 | bdrv_enable_dirty_bitmap(state->bitmap); | |
2133 | } | |
2134 | } | |
2135 | ||
6fd2e407 VSO |
2136 | static void block_dirty_bitmap_merge_prepare(BlkActionState *common, |
2137 | Error **errp) | |
2138 | { | |
2139 | BlockDirtyBitmapMerge *action; | |
2140 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2141 | common, common); | |
6fd2e407 VSO |
2142 | |
2143 | if (action_check_completion_mode(common, errp) < 0) { | |
2144 | return; | |
2145 | } | |
2146 | ||
0e2b7f09 | 2147 | action = common->action->u.block_dirty_bitmap_merge.data; |
6fd2e407 | 2148 | |
c6996cf9 EB |
2149 | state->bitmap = block_dirty_bitmap_merge(action->node, action->target, |
2150 | action->bitmaps, &state->backup, | |
2151 | errp); | |
6fd2e407 VSO |
2152 | } |
2153 | ||
c4e4b0fa JS |
2154 | static void block_dirty_bitmap_remove_prepare(BlkActionState *common, |
2155 | Error **errp) | |
2156 | { | |
2157 | BlockDirtyBitmap *action; | |
2158 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2159 | common, common); | |
2160 | ||
2161 | if (action_check_completion_mode(common, errp) < 0) { | |
2162 | return; | |
2163 | } | |
2164 | ||
2165 | action = common->action->u.block_dirty_bitmap_remove.data; | |
2166 | ||
c6996cf9 EB |
2167 | state->bitmap = block_dirty_bitmap_remove(action->node, action->name, |
2168 | false, &state->bs, errp); | |
c4e4b0fa JS |
2169 | if (state->bitmap) { |
2170 | bdrv_dirty_bitmap_skip_store(state->bitmap, true); | |
2171 | bdrv_dirty_bitmap_set_busy(state->bitmap, true); | |
2172 | } | |
2173 | } | |
2174 | ||
2175 | static void block_dirty_bitmap_remove_abort(BlkActionState *common) | |
2176 | { | |
2177 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2178 | common, common); | |
2179 | ||
2180 | if (state->bitmap) { | |
2181 | bdrv_dirty_bitmap_skip_store(state->bitmap, false); | |
2182 | bdrv_dirty_bitmap_set_busy(state->bitmap, false); | |
2183 | } | |
2184 | } | |
2185 | ||
2186 | static void block_dirty_bitmap_remove_commit(BlkActionState *common) | |
2187 | { | |
2188 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2189 | common, common); | |
2190 | ||
2191 | bdrv_dirty_bitmap_set_busy(state->bitmap, false); | |
5deb6cbd | 2192 | bdrv_release_dirty_bitmap(state->bitmap); |
c4e4b0fa JS |
2193 | } |
2194 | ||
50f43f0f | 2195 | static void abort_prepare(BlkActionState *common, Error **errp) |
78b18b78 SH |
2196 | { |
2197 | error_setg(errp, "Transaction aborted using Abort action"); | |
2198 | } | |
2199 | ||
50f43f0f | 2200 | static void abort_commit(BlkActionState *common) |
78b18b78 | 2201 | { |
dfc6f865 | 2202 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
2203 | } |
2204 | ||
50f43f0f | 2205 | static const BlkActionOps actions[] = { |
43de7e2d AG |
2206 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { |
2207 | .instance_size = sizeof(ExternalSnapshotState), | |
2208 | .prepare = external_snapshot_prepare, | |
2209 | .commit = external_snapshot_commit, | |
2210 | .abort = external_snapshot_abort, | |
4ad6f3db | 2211 | .clean = external_snapshot_clean, |
43de7e2d | 2212 | }, |
c8a83e85 | 2213 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 2214 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
2215 | .prepare = external_snapshot_prepare, |
2216 | .commit = external_snapshot_commit, | |
2217 | .abort = external_snapshot_abort, | |
da763e83 | 2218 | .clean = external_snapshot_clean, |
ba0c86a3 | 2219 | }, |
3037f364 SH |
2220 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
2221 | .instance_size = sizeof(DriveBackupState), | |
2222 | .prepare = drive_backup_prepare, | |
111049a4 | 2223 | .commit = drive_backup_commit, |
3037f364 | 2224 | .abort = drive_backup_abort, |
5d6e96ef | 2225 | .clean = drive_backup_clean, |
3037f364 | 2226 | }, |
bd8baecd FZ |
2227 | [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { |
2228 | .instance_size = sizeof(BlockdevBackupState), | |
2229 | .prepare = blockdev_backup_prepare, | |
111049a4 | 2230 | .commit = blockdev_backup_commit, |
bd8baecd FZ |
2231 | .abort = blockdev_backup_abort, |
2232 | .clean = blockdev_backup_clean, | |
2233 | }, | |
78b18b78 | 2234 | [TRANSACTION_ACTION_KIND_ABORT] = { |
50f43f0f | 2235 | .instance_size = sizeof(BlkActionState), |
78b18b78 SH |
2236 | .prepare = abort_prepare, |
2237 | .commit = abort_commit, | |
2238 | }, | |
bbe86010 WX |
2239 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
2240 | .instance_size = sizeof(InternalSnapshotState), | |
2241 | .prepare = internal_snapshot_prepare, | |
2242 | .abort = internal_snapshot_abort, | |
5d6e96ef | 2243 | .clean = internal_snapshot_clean, |
bbe86010 | 2244 | }, |
df9a681d FZ |
2245 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { |
2246 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2247 | .prepare = block_dirty_bitmap_add_prepare, | |
2248 | .abort = block_dirty_bitmap_add_abort, | |
2249 | }, | |
2250 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { | |
2251 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2252 | .prepare = block_dirty_bitmap_clear_prepare, | |
5c4cf8b2 VSO |
2253 | .commit = block_dirty_bitmap_free_backup, |
2254 | .abort = block_dirty_bitmap_restore, | |
c6490447 | 2255 | }, |
0e2b7f09 | 2256 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = { |
c6490447 VSO |
2257 | .instance_size = sizeof(BlockDirtyBitmapState), |
2258 | .prepare = block_dirty_bitmap_enable_prepare, | |
2259 | .abort = block_dirty_bitmap_enable_abort, | |
2260 | }, | |
0e2b7f09 | 2261 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = { |
c6490447 VSO |
2262 | .instance_size = sizeof(BlockDirtyBitmapState), |
2263 | .prepare = block_dirty_bitmap_disable_prepare, | |
2264 | .abort = block_dirty_bitmap_disable_abort, | |
66da04dd | 2265 | }, |
0e2b7f09 | 2266 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = { |
6fd2e407 VSO |
2267 | .instance_size = sizeof(BlockDirtyBitmapState), |
2268 | .prepare = block_dirty_bitmap_merge_prepare, | |
2269 | .commit = block_dirty_bitmap_free_backup, | |
2270 | .abort = block_dirty_bitmap_restore, | |
2271 | }, | |
c4e4b0fa JS |
2272 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = { |
2273 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2274 | .prepare = block_dirty_bitmap_remove_prepare, | |
2275 | .commit = block_dirty_bitmap_remove_commit, | |
2276 | .abort = block_dirty_bitmap_remove_abort, | |
2277 | }, | |
66da04dd JS |
2278 | /* Where are transactions for MIRROR, COMMIT and STREAM? |
2279 | * Although these blockjobs use transaction callbacks like the backup job, | |
2280 | * these jobs do not necessarily adhere to transaction semantics. | |
2281 | * These jobs may not fully undo all of their actions on abort, nor do they | |
2282 | * necessarily work in transactions with more than one job in them. | |
2283 | */ | |
ba0c86a3 WX |
2284 | }; |
2285 | ||
94d16a64 JS |
2286 | /** |
2287 | * Allocate a TransactionProperties structure if necessary, and fill | |
2288 | * that structure with desired defaults if they are unset. | |
2289 | */ | |
2290 | static TransactionProperties *get_transaction_properties( | |
2291 | TransactionProperties *props) | |
2292 | { | |
2293 | if (!props) { | |
2294 | props = g_new0(TransactionProperties, 1); | |
2295 | } | |
2296 | ||
2297 | if (!props->has_completion_mode) { | |
2298 | props->has_completion_mode = true; | |
2299 | props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; | |
2300 | } | |
2301 | ||
2302 | return props; | |
2303 | } | |
2304 | ||
8802d1fd | 2305 | /* |
b756b9ce SH |
2306 | * 'Atomic' group operations. The operations are performed as a set, and if |
2307 | * any fail then we roll back all operations in the group. | |
8802d1fd | 2308 | */ |
94d16a64 JS |
2309 | void qmp_transaction(TransactionActionList *dev_list, |
2310 | bool has_props, | |
2311 | struct TransactionProperties *props, | |
2312 | Error **errp) | |
8802d1fd | 2313 | { |
c8a83e85 | 2314 | TransactionActionList *dev_entry = dev_list; |
62c9e416 | 2315 | JobTxn *block_job_txn = NULL; |
50f43f0f | 2316 | BlkActionState *state, *next; |
43e17041 | 2317 | Error *local_err = NULL; |
8802d1fd | 2318 | |
f4de0f8c JS |
2319 | QTAILQ_HEAD(, BlkActionState) snap_bdrv_states; |
2320 | QTAILQ_INIT(&snap_bdrv_states); | |
8802d1fd | 2321 | |
94d16a64 | 2322 | /* Does this transaction get canceled as a group on failure? |
62c9e416 | 2323 | * If not, we don't really need to make a JobTxn. |
94d16a64 JS |
2324 | */ |
2325 | props = get_transaction_properties(props); | |
2326 | if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
7eaa8fb5 | 2327 | block_job_txn = job_txn_new(); |
94d16a64 JS |
2328 | } |
2329 | ||
b756b9ce | 2330 | /* drain all i/o before any operations */ |
8802d1fd JC |
2331 | bdrv_drain_all(); |
2332 | ||
b756b9ce | 2333 | /* We don't do anything in this loop that commits us to the operations */ |
8802d1fd | 2334 | while (NULL != dev_entry) { |
c8a83e85 | 2335 | TransactionAction *dev_info = NULL; |
50f43f0f | 2336 | const BlkActionOps *ops; |
52e7c241 | 2337 | |
8802d1fd JC |
2338 | dev_info = dev_entry->value; |
2339 | dev_entry = dev_entry->next; | |
2340 | ||
6a8f9661 | 2341 | assert(dev_info->type < ARRAY_SIZE(actions)); |
ba0c86a3 | 2342 | |
6a8f9661 | 2343 | ops = &actions[dev_info->type]; |
aa3fe714 HR |
2344 | assert(ops->instance_size > 0); |
2345 | ||
ba5d6ab6 SH |
2346 | state = g_malloc0(ops->instance_size); |
2347 | state->ops = ops; | |
2348 | state->action = dev_info; | |
94d16a64 JS |
2349 | state->block_job_txn = block_job_txn; |
2350 | state->txn_props = props; | |
f4de0f8c | 2351 | QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry); |
8802d1fd | 2352 | |
ba5d6ab6 | 2353 | state->ops->prepare(state, &local_err); |
84d18f06 | 2354 | if (local_err) { |
ba0c86a3 WX |
2355 | error_propagate(errp, local_err); |
2356 | goto delete_and_fail; | |
8802d1fd | 2357 | } |
8802d1fd JC |
2358 | } |
2359 | ||
f4de0f8c | 2360 | QTAILQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
2361 | if (state->ops->commit) { |
2362 | state->ops->commit(state); | |
2363 | } | |
8802d1fd JC |
2364 | } |
2365 | ||
2366 | /* success */ | |
2367 | goto exit; | |
2368 | ||
2369 | delete_and_fail: | |
b756b9ce | 2370 | /* failure, and it is all-or-none; roll back all operations */ |
f4de0f8c | 2371 | QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) { |
ba5d6ab6 SH |
2372 | if (state->ops->abort) { |
2373 | state->ops->abort(state); | |
ba0c86a3 | 2374 | } |
8802d1fd JC |
2375 | } |
2376 | exit: | |
f4de0f8c | 2377 | QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
ba5d6ab6 SH |
2378 | if (state->ops->clean) { |
2379 | state->ops->clean(state); | |
ba0c86a3 | 2380 | } |
ba5d6ab6 | 2381 | g_free(state); |
8802d1fd | 2382 | } |
94d16a64 JS |
2383 | if (!has_props) { |
2384 | qapi_free_TransactionProperties(props); | |
2385 | } | |
7eaa8fb5 | 2386 | job_txn_unref(block_job_txn); |
8802d1fd JC |
2387 | } |
2388 | ||
a3b52535 VSO |
2389 | BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, |
2390 | const char *name, | |
2391 | Error **errp) | |
2392 | { | |
2393 | BdrvDirtyBitmap *bitmap; | |
2394 | BlockDriverState *bs; | |
2395 | BlockDirtyBitmapSha256 *ret = NULL; | |
2396 | char *sha256; | |
2397 | ||
2398 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2399 | if (!bitmap || !bs) { | |
2400 | return NULL; | |
2401 | } | |
2402 | ||
2403 | sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); | |
2404 | if (sha256 == NULL) { | |
2405 | return NULL; | |
2406 | } | |
2407 | ||
2408 | ret = g_new(BlockDirtyBitmapSha256, 1); | |
2409 | ret->sha256 = sha256; | |
2410 | ||
2411 | return ret; | |
2412 | } | |
2413 | ||
eb94b81a KW |
2414 | void coroutine_fn qmp_block_resize(bool has_device, const char *device, |
2415 | bool has_node_name, const char *node_name, | |
2416 | int64_t size, Error **errp) | |
6d4a2b3a | 2417 | { |
3b1dbd11 | 2418 | Error *local_err = NULL; |
d9dbf25f | 2419 | BlockBackend *blk; |
6d4a2b3a | 2420 | BlockDriverState *bs; |
eb94b81a | 2421 | AioContext *old_ctx; |
6d4a2b3a | 2422 | |
3b1dbd11 BC |
2423 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
2424 | has_node_name ? node_name : NULL, | |
2425 | &local_err); | |
84d18f06 | 2426 | if (local_err) { |
3b1dbd11 BC |
2427 | error_propagate(errp, local_err); |
2428 | return; | |
2429 | } | |
2430 | ||
6d4a2b3a | 2431 | if (size < 0) { |
c6bd8c70 | 2432 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
d9dbf25f | 2433 | return; |
6d4a2b3a CH |
2434 | } |
2435 | ||
9c75e168 | 2436 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
c6bd8c70 | 2437 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
d9dbf25f | 2438 | return; |
9c75e168 JC |
2439 | } |
2440 | ||
a3aeeab5 EB |
2441 | blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp); |
2442 | if (!blk) { | |
d9dbf25f | 2443 | return; |
d7086422 | 2444 | } |
7dad9ee6 | 2445 | |
8089eab2 | 2446 | bdrv_co_lock(bs); |
698bdfa0 | 2447 | bdrv_drained_begin(bs); |
8089eab2 KW |
2448 | bdrv_co_unlock(bs); |
2449 | ||
eb94b81a | 2450 | old_ctx = bdrv_co_enter(bs); |
a3aeeab5 | 2451 | blk_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp); |
eb94b81a | 2452 | bdrv_co_leave(bs, old_ctx); |
927e0e76 | 2453 | |
eb94b81a | 2454 | bdrv_co_lock(bs); |
8089eab2 | 2455 | bdrv_drained_end(bs); |
7dad9ee6 | 2456 | blk_unref(blk); |
eb94b81a | 2457 | bdrv_co_unlock(bs); |
6d4a2b3a | 2458 | } |
12bd451f | 2459 | |
2323322e | 2460 | void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, |
13d8cc51 | 2461 | bool has_base, const char *base, |
312fe09c | 2462 | bool has_base_node, const char *base_node, |
13d8cc51 | 2463 | bool has_backing_file, const char *backing_file, |
7f4a396d | 2464 | bool has_bottom, const char *bottom, |
13d8cc51 | 2465 | bool has_speed, int64_t speed, |
1d809098 | 2466 | bool has_on_error, BlockdevOnError on_error, |
880747a8 | 2467 | bool has_filter_node_name, const char *filter_node_name, |
241ca1ab JS |
2468 | bool has_auto_finalize, bool auto_finalize, |
2469 | bool has_auto_dismiss, bool auto_dismiss, | |
1d809098 | 2470 | Error **errp) |
12bd451f | 2471 | { |
7f4a396d | 2472 | BlockDriverState *bs, *iter, *iter_end; |
c8c3080f | 2473 | BlockDriverState *base_bs = NULL; |
7f4a396d | 2474 | BlockDriverState *bottom_bs = NULL; |
f3e69beb | 2475 | AioContext *aio_context; |
fd7f8c65 | 2476 | Error *local_err = NULL; |
cf6320df | 2477 | int job_flags = JOB_DEFAULT; |
12bd451f | 2478 | |
7f4a396d VSO |
2479 | if (has_base && has_base_node) { |
2480 | error_setg(errp, "'base' and 'base-node' cannot be specified " | |
2481 | "at the same time"); | |
2482 | return; | |
2483 | } | |
2484 | ||
2485 | if (has_base && has_bottom) { | |
2486 | error_setg(errp, "'base' and 'bottom' cannot be specified " | |
2487 | "at the same time"); | |
2488 | return; | |
2489 | } | |
2490 | ||
2491 | if (has_bottom && has_base_node) { | |
2492 | error_setg(errp, "'bottom' and 'base-node' cannot be specified " | |
2493 | "at the same time"); | |
2494 | return; | |
2495 | } | |
2496 | ||
1d809098 PB |
2497 | if (!has_on_error) { |
2498 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2499 | } | |
2500 | ||
554b6147 | 2501 | bs = bdrv_lookup_bs(device, device, errp); |
b6c1bae5 | 2502 | if (!bs) { |
12bd451f SH |
2503 | return; |
2504 | } | |
2505 | ||
b6c1bae5 | 2506 | aio_context = bdrv_get_aio_context(bs); |
f3e69beb SH |
2507 | aio_context_acquire(aio_context); |
2508 | ||
13d8cc51 | 2509 | if (has_base) { |
c8c3080f MT |
2510 | base_bs = bdrv_find_backing_image(bs, base); |
2511 | if (base_bs == NULL) { | |
b0d7be2a | 2512 | error_setg(errp, "Can't find '%s' in the backing chain", base); |
f3e69beb | 2513 | goto out; |
c8c3080f | 2514 | } |
f3e69beb | 2515 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
12bd451f SH |
2516 | } |
2517 | ||
312fe09c AG |
2518 | if (has_base_node) { |
2519 | base_bs = bdrv_lookup_bs(NULL, base_node, errp); | |
2520 | if (!base_bs) { | |
2521 | goto out; | |
2522 | } | |
2523 | if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { | |
2524 | error_setg(errp, "Node '%s' is not a backing image of '%s'", | |
2525 | base_node, device); | |
2526 | goto out; | |
2527 | } | |
2528 | assert(bdrv_get_aio_context(base_bs) == aio_context); | |
f30c66ba | 2529 | bdrv_refresh_filename(base_bs); |
312fe09c AG |
2530 | } |
2531 | ||
7f4a396d VSO |
2532 | if (has_bottom) { |
2533 | bottom_bs = bdrv_lookup_bs(NULL, bottom, errp); | |
2534 | if (!bottom_bs) { | |
2535 | goto out; | |
2536 | } | |
2537 | if (!bottom_bs->drv) { | |
2538 | error_setg(errp, "Node '%s' is not open", bottom); | |
2539 | goto out; | |
2540 | } | |
2541 | if (bottom_bs->drv->is_filter) { | |
2542 | error_setg(errp, "Node '%s' is a filter, use a non-filter node " | |
2543 | "as 'bottom'", bottom); | |
2544 | goto out; | |
2545 | } | |
2546 | if (!bdrv_chain_contains(bs, bottom_bs)) { | |
2547 | error_setg(errp, "Node '%s' is not in a chain starting from '%s'", | |
2548 | bottom, device); | |
2549 | goto out; | |
2550 | } | |
2551 | assert(bdrv_get_aio_context(bottom_bs) == aio_context); | |
2552 | } | |
2553 | ||
2554 | /* | |
2555 | * Check for op blockers in the whole chain between bs and base (or bottom) | |
2556 | */ | |
2557 | iter_end = has_bottom ? bdrv_filter_or_cow_bs(bottom_bs) : base_bs; | |
2558 | for (iter = bs; iter && iter != iter_end; | |
67acfd21 HR |
2559 | iter = bdrv_filter_or_cow_bs(iter)) |
2560 | { | |
554b6147 AG |
2561 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { |
2562 | goto out; | |
2563 | } | |
2564 | } | |
2565 | ||
13d8cc51 JC |
2566 | /* if we are streaming the entire chain, the result will have no backing |
2567 | * file, and specifying one is therefore an error */ | |
2568 | if (base_bs == NULL && has_backing_file) { | |
2569 | error_setg(errp, "backing file specified, but streaming the " | |
2570 | "entire chain"); | |
f3e69beb | 2571 | goto out; |
13d8cc51 JC |
2572 | } |
2573 | ||
241ca1ab JS |
2574 | if (has_auto_finalize && !auto_finalize) { |
2575 | job_flags |= JOB_MANUAL_FINALIZE; | |
2576 | } | |
2577 | if (has_auto_dismiss && !auto_dismiss) { | |
2578 | job_flags |= JOB_MANUAL_DISMISS; | |
2579 | } | |
2580 | ||
000e5a1c | 2581 | stream_start(has_job_id ? job_id : NULL, bs, base_bs, backing_file, |
7f4a396d | 2582 | bottom_bs, job_flags, has_speed ? speed : 0, on_error, |
880747a8 | 2583 | filter_node_name, &local_err); |
84d18f06 | 2584 | if (local_err) { |
fd7f8c65 | 2585 | error_propagate(errp, local_err); |
f3e69beb | 2586 | goto out; |
12bd451f SH |
2587 | } |
2588 | ||
b23c580c | 2589 | trace_qmp_block_stream(bs); |
f3e69beb SH |
2590 | |
2591 | out: | |
2592 | aio_context_release(aio_context); | |
12bd451f | 2593 | } |
2d47c6e9 | 2594 | |
fd62c609 | 2595 | void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, |
3c605f40 | 2596 | bool has_base_node, const char *base_node, |
7676e2c5 | 2597 | bool has_base, const char *base, |
3c605f40 | 2598 | bool has_top_node, const char *top_node, |
7676e2c5 | 2599 | bool has_top, const char *top, |
54e26900 | 2600 | bool has_backing_file, const char *backing_file, |
ed61fc10 | 2601 | bool has_speed, int64_t speed, |
8faad1c7 | 2602 | bool has_on_error, BlockdevOnError on_error, |
0db832f4 | 2603 | bool has_filter_node_name, const char *filter_node_name, |
96fbf534 JS |
2604 | bool has_auto_finalize, bool auto_finalize, |
2605 | bool has_auto_dismiss, bool auto_dismiss, | |
ed61fc10 JC |
2606 | Error **errp) |
2607 | { | |
2608 | BlockDriverState *bs; | |
058223a6 | 2609 | BlockDriverState *iter; |
ed61fc10 | 2610 | BlockDriverState *base_bs, *top_bs; |
9e85cd5c | 2611 | AioContext *aio_context; |
ed61fc10 | 2612 | Error *local_err = NULL; |
5360782d | 2613 | int job_flags = JOB_DEFAULT; |
05ea385a | 2614 | uint64_t top_perm, top_shared; |
ed61fc10 | 2615 | |
54504663 HR |
2616 | if (!has_speed) { |
2617 | speed = 0; | |
2618 | } | |
8faad1c7 KW |
2619 | if (!has_on_error) { |
2620 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2621 | } | |
0db832f4 KW |
2622 | if (!has_filter_node_name) { |
2623 | filter_node_name = NULL; | |
2624 | } | |
96fbf534 JS |
2625 | if (has_auto_finalize && !auto_finalize) { |
2626 | job_flags |= JOB_MANUAL_FINALIZE; | |
2627 | } | |
2628 | if (has_auto_dismiss && !auto_dismiss) { | |
2629 | job_flags |= JOB_MANUAL_DISMISS; | |
2630 | } | |
54504663 | 2631 | |
7676e2c5 JC |
2632 | /* Important Note: |
2633 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
2634 | * live commit feature versions; for this to work, we must make sure to | |
2635 | * perform the device lookup before any generic errors that may occur in a | |
2636 | * scenario in which all optional arguments are omitted. */ | |
1d13b167 KW |
2637 | bs = qmp_get_root_bs(device, &local_err); |
2638 | if (!bs) { | |
2639 | bs = bdrv_lookup_bs(device, device, NULL); | |
2640 | if (!bs) { | |
2641 | error_free(local_err); | |
2642 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
2643 | "Device '%s' not found", device); | |
2644 | } else { | |
2645 | error_propagate(errp, local_err); | |
2646 | } | |
ed61fc10 | 2647 | return; |
628ff683 FZ |
2648 | } |
2649 | ||
1d13b167 | 2650 | aio_context = bdrv_get_aio_context(bs); |
9e85cd5c SH |
2651 | aio_context_acquire(aio_context); |
2652 | ||
bb00021d | 2653 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { |
9e85cd5c | 2654 | goto out; |
ed61fc10 | 2655 | } |
ed61fc10 JC |
2656 | |
2657 | /* default top_bs is the active layer */ | |
2658 | top_bs = bs; | |
2659 | ||
3c605f40 KW |
2660 | if (has_top_node && has_top) { |
2661 | error_setg(errp, "'top-node' and 'top' are mutually exclusive"); | |
2662 | goto out; | |
2663 | } else if (has_top_node) { | |
2664 | top_bs = bdrv_lookup_bs(NULL, top_node, errp); | |
2665 | if (top_bs == NULL) { | |
2666 | goto out; | |
2667 | } | |
2668 | if (!bdrv_chain_contains(bs, top_bs)) { | |
2669 | error_setg(errp, "'%s' is not in this backing file chain", | |
2670 | top_node); | |
2671 | goto out; | |
2672 | } | |
2673 | } else if (has_top && top) { | |
f30c66ba HR |
2674 | /* This strcmp() is just a shortcut, there is no need to |
2675 | * refresh @bs's filename. If it mismatches, | |
2676 | * bdrv_find_backing_image() will do the refresh and may still | |
2677 | * return @bs. */ | |
ed61fc10 JC |
2678 | if (strcmp(bs->filename, top) != 0) { |
2679 | top_bs = bdrv_find_backing_image(bs, top); | |
2680 | } | |
2681 | } | |
2682 | ||
2683 | if (top_bs == NULL) { | |
2684 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
9e85cd5c | 2685 | goto out; |
ed61fc10 JC |
2686 | } |
2687 | ||
9e85cd5c SH |
2688 | assert(bdrv_get_aio_context(top_bs) == aio_context); |
2689 | ||
3c605f40 KW |
2690 | if (has_base_node && has_base) { |
2691 | error_setg(errp, "'base-node' and 'base' are mutually exclusive"); | |
2692 | goto out; | |
2693 | } else if (has_base_node) { | |
2694 | base_bs = bdrv_lookup_bs(NULL, base_node, errp); | |
2695 | if (base_bs == NULL) { | |
2696 | goto out; | |
2697 | } | |
2698 | if (!bdrv_chain_contains(top_bs, base_bs)) { | |
2699 | error_setg(errp, "'%s' is not in this backing file chain", | |
2700 | base_node); | |
2701 | goto out; | |
2702 | } | |
2703 | } else if (has_base && base) { | |
d5208c45 | 2704 | base_bs = bdrv_find_backing_image(top_bs, base); |
b0d7be2a MA |
2705 | if (base_bs == NULL) { |
2706 | error_setg(errp, "Can't find '%s' in the backing chain", base); | |
2707 | goto out; | |
2708 | } | |
d5208c45 JC |
2709 | } else { |
2710 | base_bs = bdrv_find_base(top_bs); | |
b0d7be2a MA |
2711 | if (base_bs == NULL) { |
2712 | error_setg(errp, "There is no backimg image"); | |
2713 | goto out; | |
2714 | } | |
d5208c45 JC |
2715 | } |
2716 | ||
9e85cd5c SH |
2717 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
2718 | ||
9a71b9de HR |
2719 | for (iter = top_bs; iter != bdrv_filter_or_cow_bs(base_bs); |
2720 | iter = bdrv_filter_or_cow_bs(iter)) | |
2721 | { | |
058223a6 AG |
2722 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { |
2723 | goto out; | |
2724 | } | |
bb00021d FZ |
2725 | } |
2726 | ||
7676e2c5 JC |
2727 | /* Do not allow attempts to commit an image into itself */ |
2728 | if (top_bs == base_bs) { | |
2729 | error_setg(errp, "cannot commit an image into itself"); | |
9e85cd5c | 2730 | goto out; |
7676e2c5 JC |
2731 | } |
2732 | ||
05ea385a HR |
2733 | /* |
2734 | * Active commit is required if and only if someone has taken a | |
2735 | * WRITE permission on the top node. Historically, we have always | |
2736 | * used active commit for top nodes, so continue that practice | |
2737 | * lest we possibly break clients that rely on this behavior, e.g. | |
2738 | * to later attach this node to a writing parent. | |
2739 | * (Active commit is never really wrong.) | |
2740 | */ | |
2741 | bdrv_get_cumulative_perm(top_bs, &top_perm, &top_shared); | |
2742 | if (top_perm & BLK_PERM_WRITE || | |
2743 | bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs)) | |
2744 | { | |
54e26900 | 2745 | if (has_backing_file) { |
05ea385a HR |
2746 | if (bdrv_skip_filters(top_bs) == bdrv_skip_filters(bs)) { |
2747 | error_setg(errp, "'backing-file' specified," | |
2748 | " but 'top' is the active layer"); | |
2749 | } else { | |
2750 | error_setg(errp, "'backing-file' specified, but 'top' has a " | |
2751 | "writer on it"); | |
2752 | } | |
9e85cd5c | 2753 | goto out; |
54e26900 | 2754 | } |
05ea385a HR |
2755 | if (!has_job_id) { |
2756 | /* | |
2757 | * Emulate here what block_job_create() does, because it | |
2758 | * is possible that @bs != @top_bs (the block job should | |
2759 | * be named after @bs, even if @top_bs is the actual | |
2760 | * source) | |
2761 | */ | |
2762 | job_id = bdrv_get_device_name(bs); | |
2763 | } | |
2764 | commit_active_start(job_id, top_bs, base_bs, job_flags, speed, on_error, | |
78bbd910 | 2765 | filter_node_name, NULL, NULL, false, &local_err); |
20a63d2c | 2766 | } else { |
058223a6 AG |
2767 | BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); |
2768 | if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { | |
2769 | goto out; | |
2770 | } | |
5360782d JS |
2771 | commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags, |
2772 | speed, on_error, has_backing_file ? backing_file : NULL, | |
0db832f4 | 2773 | filter_node_name, &local_err); |
20a63d2c | 2774 | } |
ed61fc10 JC |
2775 | if (local_err != NULL) { |
2776 | error_propagate(errp, local_err); | |
9e85cd5c | 2777 | goto out; |
ed61fc10 | 2778 | } |
9e85cd5c SH |
2779 | |
2780 | out: | |
2781 | aio_context_release(aio_context); | |
ed61fc10 JC |
2782 | } |
2783 | ||
7b0b870b JS |
2784 | /* Common QMP interface for drive-backup and blockdev-backup */ |
2785 | static BlockJob *do_backup_common(BackupCommon *backup, | |
2786 | BlockDriverState *bs, | |
2787 | BlockDriverState *target_bs, | |
2788 | AioContext *aio_context, | |
2789 | JobTxn *txn, Error **errp) | |
99a9addf | 2790 | { |
111049a4 | 2791 | BlockJob *job = NULL; |
d58d8453 | 2792 | BdrvDirtyBitmap *bmap = NULL; |
6a30f663 | 2793 | BackupPerf perf = { .max_workers = 64 }; |
7b0b870b | 2794 | int job_flags = JOB_DEFAULT; |
99a9addf | 2795 | |
81206a89 PB |
2796 | if (!backup->has_speed) { |
2797 | backup->speed = 0; | |
99a9addf | 2798 | } |
81206a89 PB |
2799 | if (!backup->has_on_source_error) { |
2800 | backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
99a9addf | 2801 | } |
81206a89 PB |
2802 | if (!backup->has_on_target_error) { |
2803 | backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
99a9addf | 2804 | } |
81206a89 PB |
2805 | if (!backup->has_job_id) { |
2806 | backup->job_id = NULL; | |
99a9addf | 2807 | } |
b40dacdc JS |
2808 | if (!backup->has_auto_finalize) { |
2809 | backup->auto_finalize = true; | |
2810 | } | |
2811 | if (!backup->has_auto_dismiss) { | |
2812 | backup->auto_dismiss = true; | |
2813 | } | |
13b9414b PB |
2814 | if (!backup->has_compress) { |
2815 | backup->compress = false; | |
2816 | } | |
99a9addf | 2817 | |
86c6a3b6 VSO |
2818 | if (backup->x_perf) { |
2819 | if (backup->x_perf->has_use_copy_range) { | |
2820 | perf.use_copy_range = backup->x_perf->use_copy_range; | |
2821 | } | |
2c59fd83 VSO |
2822 | if (backup->x_perf->has_max_workers) { |
2823 | perf.max_workers = backup->x_perf->max_workers; | |
2824 | } | |
2825 | if (backup->x_perf->has_max_chunk) { | |
2826 | perf.max_chunk = backup->x_perf->max_chunk; | |
2827 | } | |
86c6a3b6 VSO |
2828 | } |
2829 | ||
a6c9365a JS |
2830 | if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) || |
2831 | (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) { | |
2832 | /* done before desugaring 'incremental' to print the right message */ | |
2833 | if (!backup->has_bitmap) { | |
2834 | error_setg(errp, "must provide a valid bitmap name for " | |
2835 | "'%s' sync mode", MirrorSyncMode_str(backup->sync)); | |
2836 | return NULL; | |
2837 | } | |
2838 | } | |
2839 | ||
c8b56501 JS |
2840 | if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) { |
2841 | if (backup->has_bitmap_mode && | |
2842 | backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) { | |
2843 | error_setg(errp, "Bitmap sync mode must be '%s' " | |
2844 | "when using sync mode '%s'", | |
2845 | BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS), | |
2846 | MirrorSyncMode_str(backup->sync)); | |
2847 | return NULL; | |
2848 | } | |
2849 | backup->has_bitmap_mode = true; | |
2850 | backup->sync = MIRROR_SYNC_MODE_BITMAP; | |
2851 | backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS; | |
2852 | } | |
2853 | ||
7b0b870b JS |
2854 | if (backup->has_bitmap) { |
2855 | bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); | |
2856 | if (!bmap) { | |
2857 | error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); | |
2858 | return NULL; | |
2859 | } | |
c8b56501 JS |
2860 | if (!backup->has_bitmap_mode) { |
2861 | error_setg(errp, "Bitmap sync mode must be given " | |
2862 | "when providing a bitmap"); | |
2863 | return NULL; | |
2864 | } | |
b30ffbef | 2865 | if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
7b0b870b JS |
2866 | return NULL; |
2867 | } | |
1a2b8b40 JS |
2868 | |
2869 | /* This does not produce a useful bitmap artifact: */ | |
2870 | if (backup->sync == MIRROR_SYNC_MODE_NONE) { | |
2871 | error_setg(errp, "sync mode '%s' does not produce meaningful bitmap" | |
2872 | " outputs", MirrorSyncMode_str(backup->sync)); | |
2873 | return NULL; | |
2874 | } | |
2875 | ||
2876 | /* If the bitmap isn't used for input or output, this is useless: */ | |
2877 | if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER && | |
2878 | backup->sync != MIRROR_SYNC_MODE_BITMAP) { | |
2879 | error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect" | |
2880 | " when combined with sync mode '%s'", | |
2881 | BitmapSyncMode_str(backup->bitmap_mode), | |
2882 | MirrorSyncMode_str(backup->sync)); | |
2883 | return NULL; | |
2884 | } | |
2885 | } | |
2886 | ||
2887 | if (!backup->has_bitmap && backup->has_bitmap_mode) { | |
2888 | error_setg(errp, "Cannot specify bitmap sync mode without a bitmap"); | |
2889 | return NULL; | |
7b0b870b JS |
2890 | } |
2891 | ||
2892 | if (!backup->auto_finalize) { | |
2893 | job_flags |= JOB_MANUAL_FINALIZE; | |
2894 | } | |
2895 | if (!backup->auto_dismiss) { | |
2896 | job_flags |= JOB_MANUAL_DISMISS; | |
2897 | } | |
2898 | ||
2899 | job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, | |
c8b56501 JS |
2900 | backup->sync, bmap, backup->bitmap_mode, |
2901 | backup->compress, | |
00e30f05 | 2902 | backup->filter_node_name, |
86c6a3b6 | 2903 | &perf, |
c8b56501 JS |
2904 | backup->on_source_error, |
2905 | backup->on_target_error, | |
7b0b870b JS |
2906 | job_flags, NULL, NULL, txn, errp); |
2907 | return job; | |
2908 | } | |
2909 | ||
2288ccfa | 2910 | void qmp_drive_backup(DriveBackup *backup, Error **errp) |
78f51fde | 2911 | { |
2288ccfa SL |
2912 | TransactionAction action = { |
2913 | .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP, | |
2914 | .u.drive_backup.data = backup, | |
2915 | }; | |
2916 | blockdev_do_action(&action, errp); | |
78f51fde JS |
2917 | } |
2918 | ||
facda544 PK |
2919 | BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat, |
2920 | bool flat, | |
2921 | Error **errp) | |
c13163fb | 2922 | { |
facda544 PK |
2923 | bool return_flat = has_flat && flat; |
2924 | ||
2925 | return bdrv_named_nodes_list(return_flat, errp); | |
c13163fb BC |
2926 | } |
2927 | ||
5d3b4e99 VSO |
2928 | XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp) |
2929 | { | |
2930 | return bdrv_get_xdbg_block_graph(errp); | |
2931 | } | |
2932 | ||
5b7bfe51 | 2933 | void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp) |
c29c1dd3 | 2934 | { |
5b7bfe51 SL |
2935 | TransactionAction action = { |
2936 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP, | |
2937 | .u.blockdev_backup.data = backup, | |
2938 | }; | |
2939 | blockdev_do_action(&action, errp); | |
78f51fde JS |
2940 | } |
2941 | ||
4193cdd7 FZ |
2942 | /* Parameter check and block job starting for drive mirroring. |
2943 | * Caller should hold @device and @target's aio context (must be the same). | |
2944 | **/ | |
71aa9867 | 2945 | static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, |
4193cdd7 FZ |
2946 | BlockDriverState *target, |
2947 | bool has_replaces, const char *replaces, | |
2948 | enum MirrorSyncMode sync, | |
274fccee | 2949 | BlockMirrorBackingMode backing_mode, |
cdf3bc93 | 2950 | bool zero_target, |
4193cdd7 FZ |
2951 | bool has_speed, int64_t speed, |
2952 | bool has_granularity, uint32_t granularity, | |
2953 | bool has_buf_size, int64_t buf_size, | |
2954 | bool has_on_source_error, | |
2955 | BlockdevOnError on_source_error, | |
2956 | bool has_on_target_error, | |
2957 | BlockdevOnError on_target_error, | |
2958 | bool has_unmap, bool unmap, | |
6cdbceb1 KW |
2959 | bool has_filter_node_name, |
2960 | const char *filter_node_name, | |
481debaa | 2961 | bool has_copy_mode, MirrorCopyMode copy_mode, |
a6b58ade JS |
2962 | bool has_auto_finalize, bool auto_finalize, |
2963 | bool has_auto_dismiss, bool auto_dismiss, | |
4193cdd7 | 2964 | Error **errp) |
d9b902db | 2965 | { |
3f072a7f | 2966 | BlockDriverState *unfiltered_bs; |
a1999b33 | 2967 | int job_flags = JOB_DEFAULT; |
d9b902db PB |
2968 | |
2969 | if (!has_speed) { | |
2970 | speed = 0; | |
2971 | } | |
b952b558 PB |
2972 | if (!has_on_source_error) { |
2973 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2974 | } | |
2975 | if (!has_on_target_error) { | |
2976 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2977 | } | |
eee13dfe PB |
2978 | if (!has_granularity) { |
2979 | granularity = 0; | |
2980 | } | |
08e4ed6c | 2981 | if (!has_buf_size) { |
48ac0a4d | 2982 | buf_size = 0; |
08e4ed6c | 2983 | } |
0fc9f8ea FZ |
2984 | if (!has_unmap) { |
2985 | unmap = true; | |
2986 | } | |
6cdbceb1 KW |
2987 | if (!has_filter_node_name) { |
2988 | filter_node_name = NULL; | |
2989 | } | |
481debaa HR |
2990 | if (!has_copy_mode) { |
2991 | copy_mode = MIRROR_COPY_MODE_BACKGROUND; | |
2992 | } | |
a6b58ade JS |
2993 | if (has_auto_finalize && !auto_finalize) { |
2994 | job_flags |= JOB_MANUAL_FINALIZE; | |
2995 | } | |
2996 | if (has_auto_dismiss && !auto_dismiss) { | |
2997 | job_flags |= JOB_MANUAL_DISMISS; | |
2998 | } | |
08e4ed6c | 2999 | |
eee13dfe | 3000 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
c6bd8c70 MA |
3001 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
3002 | "a value in range [512B, 64MB]"); | |
eee13dfe PB |
3003 | return; |
3004 | } | |
3005 | if (granularity & (granularity - 1)) { | |
c6bd8c70 | 3006 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
6cc0667d | 3007 | "a power of 2"); |
eee13dfe PB |
3008 | return; |
3009 | } | |
d9b902db | 3010 | |
4193cdd7 FZ |
3011 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { |
3012 | return; | |
3013 | } | |
e40e5027 FZ |
3014 | if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { |
3015 | return; | |
3016 | } | |
4193cdd7 | 3017 | |
3f072a7f | 3018 | if (!bdrv_backing_chain_next(bs) && sync == MIRROR_SYNC_MODE_TOP) { |
4193cdd7 FZ |
3019 | sync = MIRROR_SYNC_MODE_FULL; |
3020 | } | |
3021 | ||
3f072a7f HR |
3022 | if (!has_replaces) { |
3023 | /* We want to mirror from @bs, but keep implicit filters on top */ | |
3024 | unfiltered_bs = bdrv_skip_implicit_filters(bs); | |
3025 | if (unfiltered_bs != bs) { | |
3026 | replaces = unfiltered_bs->node_name; | |
3027 | has_replaces = true; | |
3028 | } | |
3029 | } | |
3030 | ||
74ce9e46 HR |
3031 | if (has_replaces) { |
3032 | BlockDriverState *to_replace_bs; | |
3033 | AioContext *replace_aio_context; | |
3034 | int64_t bs_size, replace_size; | |
3035 | ||
3036 | bs_size = bdrv_getlength(bs); | |
3037 | if (bs_size < 0) { | |
3038 | error_setg_errno(errp, -bs_size, "Failed to query device's size"); | |
3039 | return; | |
3040 | } | |
3041 | ||
3042 | to_replace_bs = check_to_replace_node(bs, replaces, errp); | |
3043 | if (!to_replace_bs) { | |
3044 | return; | |
3045 | } | |
3046 | ||
3047 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); | |
3048 | aio_context_acquire(replace_aio_context); | |
3049 | replace_size = bdrv_getlength(to_replace_bs); | |
3050 | aio_context_release(replace_aio_context); | |
3051 | ||
3052 | if (replace_size < 0) { | |
3053 | error_setg_errno(errp, -replace_size, | |
3054 | "Failed to query the replacement node's size"); | |
3055 | return; | |
3056 | } | |
3057 | if (bs_size != replace_size) { | |
3058 | error_setg(errp, "cannot replace image with a mirror image of " | |
3059 | "different size"); | |
3060 | return; | |
3061 | } | |
3062 | } | |
3063 | ||
4193cdd7 FZ |
3064 | /* pass the node name to replace to mirror start since it's loose coupling |
3065 | * and will allow to check whether the node still exist at mirror completion | |
3066 | */ | |
71aa9867 | 3067 | mirror_start(job_id, bs, target, |
a1999b33 | 3068 | has_replaces ? replaces : NULL, job_flags, |
cdf3bc93 | 3069 | speed, granularity, buf_size, sync, backing_mode, zero_target, |
6cdbceb1 | 3070 | on_source_error, on_target_error, unmap, filter_node_name, |
481debaa | 3071 | copy_mode, errp); |
4193cdd7 FZ |
3072 | } |
3073 | ||
faecd40a | 3074 | void qmp_drive_mirror(DriveMirror *arg, Error **errp) |
4193cdd7 FZ |
3075 | { |
3076 | BlockDriverState *bs; | |
3f072a7f | 3077 | BlockDriverState *target_backing_bs, *target_bs; |
4193cdd7 | 3078 | AioContext *aio_context; |
3ea67e08 | 3079 | AioContext *old_context; |
274fccee | 3080 | BlockMirrorBackingMode backing_mode; |
4193cdd7 FZ |
3081 | Error *local_err = NULL; |
3082 | QDict *options = NULL; | |
3083 | int flags; | |
3084 | int64_t size; | |
faecd40a | 3085 | const char *format = arg->format; |
cdf3bc93 | 3086 | bool zero_target; |
8ed7d42f | 3087 | int ret; |
4193cdd7 | 3088 | |
0524e93a KW |
3089 | bs = qmp_get_root_bs(arg->device, errp); |
3090 | if (!bs) { | |
d9b902db PB |
3091 | return; |
3092 | } | |
3093 | ||
cb2af917 PB |
3094 | /* Early check to avoid creating target */ |
3095 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { | |
3096 | return; | |
3097 | } | |
3098 | ||
0524e93a | 3099 | aio_context = bdrv_get_aio_context(bs); |
5a7e7a0b SH |
3100 | aio_context_acquire(aio_context); |
3101 | ||
faecd40a EB |
3102 | if (!arg->has_mode) { |
3103 | arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
4193cdd7 | 3104 | } |
d9b902db | 3105 | |
faecd40a EB |
3106 | if (!arg->has_format) { |
3107 | format = (arg->mode == NEW_IMAGE_MODE_EXISTING | |
3108 | ? NULL : bs->drv->format_name); | |
d9b902db | 3109 | } |
d9b902db | 3110 | |
61de4c68 | 3111 | flags = bs->open_flags | BDRV_O_RDWR; |
3f072a7f HR |
3112 | target_backing_bs = bdrv_cow_bs(bdrv_skip_filters(bs)); |
3113 | if (!target_backing_bs && arg->sync == MIRROR_SYNC_MODE_TOP) { | |
faecd40a | 3114 | arg->sync = MIRROR_SYNC_MODE_FULL; |
d9b902db | 3115 | } |
faecd40a | 3116 | if (arg->sync == MIRROR_SYNC_MODE_NONE) { |
3f072a7f | 3117 | target_backing_bs = bs; |
117e0c82 | 3118 | } |
d9b902db | 3119 | |
ac3c5d83 SH |
3120 | size = bdrv_getlength(bs); |
3121 | if (size < 0) { | |
3122 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
5a7e7a0b | 3123 | goto out; |
ac3c5d83 SH |
3124 | } |
3125 | ||
faecd40a | 3126 | if (arg->has_replaces) { |
faecd40a | 3127 | if (!arg->has_node_name) { |
09158f00 BC |
3128 | error_setg(errp, "a node-name must be provided when replacing a" |
3129 | " named node of the graph"); | |
5a7e7a0b | 3130 | goto out; |
09158f00 | 3131 | } |
09158f00 BC |
3132 | } |
3133 | ||
faecd40a | 3134 | if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { |
274fccee HR |
3135 | backing_mode = MIRROR_SOURCE_BACKING_CHAIN; |
3136 | } else { | |
3137 | backing_mode = MIRROR_OPEN_BACKING_CHAIN; | |
3138 | } | |
3139 | ||
2a32c6e8 JS |
3140 | /* Don't open backing image in create() */ |
3141 | flags |= BDRV_O_NO_BACKING; | |
3142 | ||
3f072a7f | 3143 | if ((arg->sync == MIRROR_SYNC_MODE_FULL || !target_backing_bs) |
faecd40a | 3144 | && arg->mode != NEW_IMAGE_MODE_EXISTING) |
14526864 | 3145 | { |
d9b902db | 3146 | /* create new image w/o backing file */ |
e6641719 | 3147 | assert(format); |
faecd40a | 3148 | bdrv_img_create(arg->target, format, |
9217283d | 3149 | NULL, NULL, NULL, size, flags, false, &local_err); |
d9b902db | 3150 | } else { |
3f072a7f HR |
3151 | /* Implicit filters should not appear in the filename */ |
3152 | BlockDriverState *explicit_backing = | |
3153 | bdrv_skip_implicit_filters(target_backing_bs); | |
3154 | ||
faecd40a | 3155 | switch (arg->mode) { |
d9b902db | 3156 | case NEW_IMAGE_MODE_EXISTING: |
d9b902db PB |
3157 | break; |
3158 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
3159 | /* create new image with backing file */ | |
3f072a7f | 3160 | bdrv_refresh_filename(explicit_backing); |
faecd40a | 3161 | bdrv_img_create(arg->target, format, |
3f072a7f HR |
3162 | explicit_backing->filename, |
3163 | explicit_backing->drv->format_name, | |
9217283d | 3164 | NULL, size, flags, false, &local_err); |
d9b902db PB |
3165 | break; |
3166 | default: | |
3167 | abort(); | |
3168 | } | |
3169 | } | |
3170 | ||
84d18f06 | 3171 | if (local_err) { |
cf8f2426 | 3172 | error_propagate(errp, local_err); |
5a7e7a0b | 3173 | goto out; |
d9b902db PB |
3174 | } |
3175 | ||
e6641719 | 3176 | options = qdict_new(); |
faecd40a | 3177 | if (arg->has_node_name) { |
46f5ac20 | 3178 | qdict_put_str(options, "node-name", arg->node_name); |
4c828dc6 | 3179 | } |
e6641719 | 3180 | if (format) { |
46f5ac20 | 3181 | qdict_put_str(options, "driver", format); |
e6641719 | 3182 | } |
4c828dc6 | 3183 | |
b812f671 PB |
3184 | /* Mirroring takes care of copy-on-write using the source's backing |
3185 | * file. | |
3186 | */ | |
2a32c6e8 | 3187 | target_bs = bdrv_open(arg->target, NULL, options, flags, errp); |
5b363937 | 3188 | if (!target_bs) { |
5a7e7a0b | 3189 | goto out; |
d9b902db PB |
3190 | } |
3191 | ||
cdf3bc93 HR |
3192 | zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL && |
3193 | (arg->mode == NEW_IMAGE_MODE_EXISTING || | |
3194 | !bdrv_has_zero_init(target_bs))); | |
3195 | ||
3ea67e08 SL |
3196 | |
3197 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ | |
3198 | old_context = bdrv_get_aio_context(target_bs); | |
3199 | aio_context_release(aio_context); | |
3200 | aio_context_acquire(old_context); | |
3201 | ||
8ed7d42f KW |
3202 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); |
3203 | if (ret < 0) { | |
3204 | bdrv_unref(target_bs); | |
3ea67e08 SL |
3205 | aio_context_release(old_context); |
3206 | return; | |
8ed7d42f | 3207 | } |
5a7e7a0b | 3208 | |
3ea67e08 SL |
3209 | aio_context_release(old_context); |
3210 | aio_context_acquire(aio_context); | |
3211 | ||
faecd40a EB |
3212 | blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, |
3213 | arg->has_replaces, arg->replaces, arg->sync, | |
cdf3bc93 HR |
3214 | backing_mode, zero_target, |
3215 | arg->has_speed, arg->speed, | |
faecd40a EB |
3216 | arg->has_granularity, arg->granularity, |
3217 | arg->has_buf_size, arg->buf_size, | |
3218 | arg->has_on_source_error, arg->on_source_error, | |
3219 | arg->has_on_target_error, arg->on_target_error, | |
3220 | arg->has_unmap, arg->unmap, | |
6cdbceb1 | 3221 | false, NULL, |
481debaa | 3222 | arg->has_copy_mode, arg->copy_mode, |
a6b58ade JS |
3223 | arg->has_auto_finalize, arg->auto_finalize, |
3224 | arg->has_auto_dismiss, arg->auto_dismiss, | |
992861fb | 3225 | errp); |
e253f4b8 | 3226 | bdrv_unref(target_bs); |
5a7e7a0b SH |
3227 | out: |
3228 | aio_context_release(aio_context); | |
d9b902db PB |
3229 | } |
3230 | ||
71aa9867 AG |
3231 | void qmp_blockdev_mirror(bool has_job_id, const char *job_id, |
3232 | const char *device, const char *target, | |
df92562e FZ |
3233 | bool has_replaces, const char *replaces, |
3234 | MirrorSyncMode sync, | |
3235 | bool has_speed, int64_t speed, | |
3236 | bool has_granularity, uint32_t granularity, | |
3237 | bool has_buf_size, int64_t buf_size, | |
3238 | bool has_on_source_error, | |
3239 | BlockdevOnError on_source_error, | |
3240 | bool has_on_target_error, | |
3241 | BlockdevOnError on_target_error, | |
6cdbceb1 KW |
3242 | bool has_filter_node_name, |
3243 | const char *filter_node_name, | |
481debaa | 3244 | bool has_copy_mode, MirrorCopyMode copy_mode, |
a6b58ade JS |
3245 | bool has_auto_finalize, bool auto_finalize, |
3246 | bool has_auto_dismiss, bool auto_dismiss, | |
df92562e FZ |
3247 | Error **errp) |
3248 | { | |
3249 | BlockDriverState *bs; | |
df92562e FZ |
3250 | BlockDriverState *target_bs; |
3251 | AioContext *aio_context; | |
3ea67e08 | 3252 | AioContext *old_context; |
274fccee | 3253 | BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; |
cdf3bc93 | 3254 | bool zero_target; |
8ed7d42f | 3255 | int ret; |
df92562e | 3256 | |
07eec652 | 3257 | bs = qmp_get_root_bs(device, errp); |
df92562e | 3258 | if (!bs) { |
df92562e FZ |
3259 | return; |
3260 | } | |
3261 | ||
3262 | target_bs = bdrv_lookup_bs(target, target, errp); | |
3263 | if (!target_bs) { | |
3264 | return; | |
3265 | } | |
3266 | ||
cdf3bc93 HR |
3267 | zero_target = (sync == MIRROR_SYNC_MODE_FULL); |
3268 | ||
3ea67e08 SL |
3269 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ |
3270 | old_context = bdrv_get_aio_context(target_bs); | |
df92562e | 3271 | aio_context = bdrv_get_aio_context(bs); |
3ea67e08 | 3272 | aio_context_acquire(old_context); |
df92562e | 3273 | |
8ed7d42f | 3274 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); |
3ea67e08 SL |
3275 | |
3276 | aio_context_release(old_context); | |
3277 | aio_context_acquire(aio_context); | |
3278 | ||
8ed7d42f KW |
3279 | if (ret < 0) { |
3280 | goto out; | |
3281 | } | |
df92562e | 3282 | |
71aa9867 | 3283 | blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, |
274fccee | 3284 | has_replaces, replaces, sync, backing_mode, |
cdf3bc93 | 3285 | zero_target, has_speed, speed, |
df92562e FZ |
3286 | has_granularity, granularity, |
3287 | has_buf_size, buf_size, | |
3288 | has_on_source_error, on_source_error, | |
3289 | has_on_target_error, on_target_error, | |
3290 | true, true, | |
6cdbceb1 | 3291 | has_filter_node_name, filter_node_name, |
481debaa | 3292 | has_copy_mode, copy_mode, |
a6b58ade JS |
3293 | has_auto_finalize, auto_finalize, |
3294 | has_auto_dismiss, auto_dismiss, | |
992861fb | 3295 | errp); |
8ed7d42f | 3296 | out: |
df92562e FZ |
3297 | aio_context_release(aio_context); |
3298 | } | |
3299 | ||
3ddf3efe AG |
3300 | /* Get a block job using its ID and acquire its AioContext */ |
3301 | static BlockJob *find_block_job(const char *id, AioContext **aio_context, | |
24d6bffe | 3302 | Error **errp) |
2d47c6e9 | 3303 | { |
3ddf3efe | 3304 | BlockJob *job; |
2d47c6e9 | 3305 | |
3ddf3efe | 3306 | assert(id != NULL); |
5433c24f | 3307 | |
3ddf3efe | 3308 | *aio_context = NULL; |
3d948cdf | 3309 | |
3ddf3efe | 3310 | job = block_job_get(id); |
5433c24f | 3311 | |
3ddf3efe AG |
3312 | if (!job) { |
3313 | error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, | |
3314 | "Block job '%s' not found", id); | |
3315 | return NULL; | |
2d47c6e9 | 3316 | } |
3d948cdf | 3317 | |
3ddf3efe AG |
3318 | *aio_context = blk_get_aio_context(job->blk); |
3319 | aio_context_acquire(*aio_context); | |
3d948cdf | 3320 | |
3ddf3efe | 3321 | return job; |
2d47c6e9 SH |
3322 | } |
3323 | ||
882ec7ce | 3324 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 | 3325 | { |
3d948cdf | 3326 | AioContext *aio_context; |
24d6bffe | 3327 | BlockJob *job = find_block_job(device, &aio_context, errp); |
2d47c6e9 SH |
3328 | |
3329 | if (!job) { | |
2d47c6e9 SH |
3330 | return; |
3331 | } | |
3332 | ||
882ec7ce | 3333 | block_job_set_speed(job, speed, errp); |
3d948cdf | 3334 | aio_context_release(aio_context); |
2d47c6e9 | 3335 | } |
370521a1 | 3336 | |
6e37fb81 PB |
3337 | void qmp_block_job_cancel(const char *device, |
3338 | bool has_force, bool force, Error **errp) | |
370521a1 | 3339 | { |
3d948cdf | 3340 | AioContext *aio_context; |
24d6bffe | 3341 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3342 | |
370521a1 | 3343 | if (!job) { |
370521a1 SH |
3344 | return; |
3345 | } | |
3d948cdf SH |
3346 | |
3347 | if (!has_force) { | |
3348 | force = false; | |
3349 | } | |
3350 | ||
b15de828 | 3351 | if (job_user_paused(&job->job) && !force) { |
f231b88d CR |
3352 | error_setg(errp, "The block job for device '%s' is currently paused", |
3353 | device); | |
3d948cdf | 3354 | goto out; |
8acc72a4 | 3355 | } |
370521a1 SH |
3356 | |
3357 | trace_qmp_block_job_cancel(job); | |
3d70ff53 | 3358 | job_user_cancel(&job->job, force, errp); |
3d948cdf SH |
3359 | out: |
3360 | aio_context_release(aio_context); | |
370521a1 | 3361 | } |
fb5458cd | 3362 | |
6e37fb81 PB |
3363 | void qmp_block_job_pause(const char *device, Error **errp) |
3364 | { | |
3d948cdf | 3365 | AioContext *aio_context; |
24d6bffe | 3366 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3367 | |
0ec4dfb8 | 3368 | if (!job) { |
6e37fb81 PB |
3369 | return; |
3370 | } | |
3371 | ||
3372 | trace_qmp_block_job_pause(job); | |
b15de828 | 3373 | job_user_pause(&job->job, errp); |
3d948cdf | 3374 | aio_context_release(aio_context); |
6e37fb81 PB |
3375 | } |
3376 | ||
3377 | void qmp_block_job_resume(const char *device, Error **errp) | |
3378 | { | |
3d948cdf | 3379 | AioContext *aio_context; |
24d6bffe | 3380 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3381 | |
0ec4dfb8 | 3382 | if (!job) { |
6e37fb81 PB |
3383 | return; |
3384 | } | |
3385 | ||
3386 | trace_qmp_block_job_resume(job); | |
b15de828 | 3387 | job_user_resume(&job->job, errp); |
3d948cdf | 3388 | aio_context_release(aio_context); |
6e37fb81 PB |
3389 | } |
3390 | ||
aeae883b PB |
3391 | void qmp_block_job_complete(const char *device, Error **errp) |
3392 | { | |
3d948cdf | 3393 | AioContext *aio_context; |
24d6bffe | 3394 | BlockJob *job = find_block_job(device, &aio_context, errp); |
aeae883b PB |
3395 | |
3396 | if (!job) { | |
aeae883b PB |
3397 | return; |
3398 | } | |
3399 | ||
3400 | trace_qmp_block_job_complete(job); | |
3453d972 | 3401 | job_complete(&job->job, errp); |
3d948cdf | 3402 | aio_context_release(aio_context); |
aeae883b PB |
3403 | } |
3404 | ||
11b61fbc JS |
3405 | void qmp_block_job_finalize(const char *id, Error **errp) |
3406 | { | |
3407 | AioContext *aio_context; | |
3408 | BlockJob *job = find_block_job(id, &aio_context, errp); | |
3409 | ||
3410 | if (!job) { | |
3411 | return; | |
3412 | } | |
3413 | ||
3414 | trace_qmp_block_job_finalize(job); | |
b660a84b | 3415 | job_ref(&job->job); |
7eaa8fb5 | 3416 | job_finalize(&job->job, errp); |
b660a84b SR |
3417 | |
3418 | /* | |
3419 | * Job's context might have changed via job_finalize (and job_txn_apply | |
3420 | * automatically acquires the new one), so make sure we release the correct | |
3421 | * one. | |
3422 | */ | |
3423 | aio_context = blk_get_aio_context(job->blk); | |
3424 | job_unref(&job->job); | |
11b61fbc JS |
3425 | aio_context_release(aio_context); |
3426 | } | |
3427 | ||
75f71059 JS |
3428 | void qmp_block_job_dismiss(const char *id, Error **errp) |
3429 | { | |
3430 | AioContext *aio_context; | |
5f9a6a08 KW |
3431 | BlockJob *bjob = find_block_job(id, &aio_context, errp); |
3432 | Job *job; | |
75f71059 | 3433 | |
5f9a6a08 | 3434 | if (!bjob) { |
75f71059 JS |
3435 | return; |
3436 | } | |
3437 | ||
5f9a6a08 KW |
3438 | trace_qmp_block_job_dismiss(bjob); |
3439 | job = &bjob->job; | |
3440 | job_dismiss(&job, errp); | |
75f71059 JS |
3441 | aio_context_release(aio_context); |
3442 | } | |
3443 | ||
fa40e656 JC |
3444 | void qmp_change_backing_file(const char *device, |
3445 | const char *image_node_name, | |
3446 | const char *backing_file, | |
3447 | Error **errp) | |
3448 | { | |
3449 | BlockDriverState *bs = NULL; | |
729962f6 | 3450 | AioContext *aio_context; |
fa40e656 JC |
3451 | BlockDriverState *image_bs = NULL; |
3452 | Error *local_err = NULL; | |
3453 | bool ro; | |
fa40e656 JC |
3454 | int ret; |
3455 | ||
7b5dca3f KW |
3456 | bs = qmp_get_root_bs(device, errp); |
3457 | if (!bs) { | |
fa40e656 JC |
3458 | return; |
3459 | } | |
3460 | ||
7b5dca3f | 3461 | aio_context = bdrv_get_aio_context(bs); |
729962f6 SH |
3462 | aio_context_acquire(aio_context); |
3463 | ||
fa40e656 JC |
3464 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); |
3465 | if (local_err) { | |
3466 | error_propagate(errp, local_err); | |
729962f6 | 3467 | goto out; |
fa40e656 JC |
3468 | } |
3469 | ||
3470 | if (!image_bs) { | |
3471 | error_setg(errp, "image file not found"); | |
729962f6 | 3472 | goto out; |
fa40e656 JC |
3473 | } |
3474 | ||
3475 | if (bdrv_find_base(image_bs) == image_bs) { | |
3476 | error_setg(errp, "not allowing backing file change on an image " | |
3477 | "without a backing file"); | |
729962f6 | 3478 | goto out; |
fa40e656 JC |
3479 | } |
3480 | ||
3481 | /* even though we are not necessarily operating on bs, we need it to | |
3482 | * determine if block ops are currently prohibited on the chain */ | |
3483 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
729962f6 | 3484 | goto out; |
fa40e656 JC |
3485 | } |
3486 | ||
3487 | /* final sanity check */ | |
3488 | if (!bdrv_chain_contains(bs, image_bs)) { | |
3489 | error_setg(errp, "'%s' and image file are not in the same chain", | |
3490 | device); | |
729962f6 | 3491 | goto out; |
fa40e656 JC |
3492 | } |
3493 | ||
3494 | /* if not r/w, reopen to make r/w */ | |
fa40e656 JC |
3495 | ro = bdrv_is_read_only(image_bs); |
3496 | ||
3497 | if (ro) { | |
051a60f6 | 3498 | if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) { |
729962f6 | 3499 | goto out; |
fa40e656 JC |
3500 | } |
3501 | } | |
3502 | ||
3503 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
e54ee1b3 EB |
3504 | image_bs->drv ? image_bs->drv->format_name : "", |
3505 | false); | |
fa40e656 JC |
3506 | |
3507 | if (ret < 0) { | |
3508 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
3509 | backing_file); | |
3510 | /* don't exit here, so we can try to restore open flags if | |
3511 | * appropriate */ | |
3512 | } | |
3513 | ||
3514 | if (ro) { | |
992861fb | 3515 | bdrv_reopen_set_read_only(image_bs, true, errp); |
fa40e656 | 3516 | } |
729962f6 SH |
3517 | |
3518 | out: | |
3519 | aio_context_release(aio_context); | |
fa40e656 JC |
3520 | } |
3521 | ||
d26c9a15 KW |
3522 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
3523 | { | |
be4b67bc | 3524 | BlockDriverState *bs; |
d26c9a15 | 3525 | QObject *obj; |
7d5e199a | 3526 | Visitor *v = qobject_output_visitor_new(&obj); |
d26c9a15 | 3527 | QDict *qdict; |
d26c9a15 | 3528 | |
1f584248 | 3529 | visit_type_BlockdevOptions(v, NULL, &options, &error_abort); |
3b098d56 | 3530 | visit_complete(v, &obj); |
7dc847eb | 3531 | qdict = qobject_to(QDict, obj); |
d26c9a15 KW |
3532 | |
3533 | qdict_flatten(qdict); | |
3534 | ||
9ec8873e KW |
3535 | if (!qdict_get_try_str(qdict, "node-name")) { |
3536 | error_setg(errp, "'node-name' must be specified for the root node"); | |
3537 | goto fail; | |
3538 | } | |
9c4218e9 | 3539 | |
9ec8873e KW |
3540 | bs = bds_tree_init(qdict, errp); |
3541 | if (!bs) { | |
3542 | goto fail; | |
d26c9a15 KW |
3543 | } |
3544 | ||
89802d5a | 3545 | bdrv_set_monitor_owned(bs); |
9ec8873e | 3546 | |
d26c9a15 | 3547 | fail: |
3b098d56 | 3548 | visit_free(v); |
d26c9a15 KW |
3549 | } |
3550 | ||
e60edf69 | 3551 | void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp) |
3908b7a8 AG |
3552 | { |
3553 | BlockReopenQueue *queue = NULL; | |
3554 | GSList *drained = NULL; | |
3555 | ||
3556 | /* Add each one of the BDS that we want to reopen to the queue */ | |
3557 | for (; reopen_list != NULL; reopen_list = reopen_list->next) { | |
3558 | BlockdevOptions *options = reopen_list->value; | |
3559 | BlockDriverState *bs; | |
3560 | AioContext *ctx; | |
3561 | QObject *obj; | |
3562 | Visitor *v; | |
3563 | QDict *qdict; | |
3564 | ||
3565 | /* Check for the selected node name */ | |
3566 | if (!options->has_node_name) { | |
3567 | error_setg(errp, "node-name not specified"); | |
3568 | goto fail; | |
3569 | } | |
1479c730 | 3570 | |
3908b7a8 AG |
3571 | bs = bdrv_find_node(options->node_name); |
3572 | if (!bs) { | |
3573 | error_setg(errp, "Failed to find node with node-name='%s'", | |
3574 | options->node_name); | |
3575 | goto fail; | |
3576 | } | |
1479c730 | 3577 | |
3908b7a8 AG |
3578 | /* Put all options in a QDict and flatten it */ |
3579 | v = qobject_output_visitor_new(&obj); | |
3580 | visit_type_BlockdevOptions(v, NULL, &options, &error_abort); | |
3581 | visit_complete(v, &obj); | |
3582 | visit_free(v); | |
1479c730 | 3583 | |
3908b7a8 | 3584 | qdict = qobject_to(QDict, obj); |
1479c730 | 3585 | |
3908b7a8 | 3586 | qdict_flatten(qdict); |
1479c730 | 3587 | |
3908b7a8 AG |
3588 | ctx = bdrv_get_aio_context(bs); |
3589 | aio_context_acquire(ctx); | |
6cf42ca2 | 3590 | |
3908b7a8 AG |
3591 | bdrv_subtree_drained_begin(bs); |
3592 | queue = bdrv_reopen_queue(queue, bs, qdict, false); | |
3593 | drained = g_slist_prepend(drained, bs); | |
6cf42ca2 | 3594 | |
3908b7a8 AG |
3595 | aio_context_release(ctx); |
3596 | } | |
3597 | ||
3598 | /* Perform the reopen operation */ | |
3599 | bdrv_reopen_multiple(queue, errp); | |
3600 | queue = NULL; | |
1479c730 AG |
3601 | |
3602 | fail: | |
3908b7a8 AG |
3603 | bdrv_reopen_queue_free(queue); |
3604 | g_slist_free_full(drained, (GDestroyNotify) bdrv_subtree_drained_end); | |
1479c730 AG |
3605 | } |
3606 | ||
79b7a77e | 3607 | void qmp_blockdev_del(const char *node_name, Error **errp) |
81b936ae AG |
3608 | { |
3609 | AioContext *aio_context; | |
81b936ae AG |
3610 | BlockDriverState *bs; |
3611 | ||
9ec8873e KW |
3612 | bs = bdrv_find_node(node_name); |
3613 | if (!bs) { | |
ef2e38a1 | 3614 | error_setg(errp, "Failed to find node with node-name='%s'", node_name); |
81b936ae AG |
3615 | return; |
3616 | } | |
9ec8873e KW |
3617 | if (bdrv_has_blk(bs)) { |
3618 | error_setg(errp, "Node %s is in use", node_name); | |
3619 | return; | |
81b936ae | 3620 | } |
9ec8873e | 3621 | aio_context = bdrv_get_aio_context(bs); |
81b936ae AG |
3622 | aio_context_acquire(aio_context); |
3623 | ||
9ec8873e KW |
3624 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { |
3625 | goto out; | |
3626 | } | |
9c4218e9 | 3627 | |
70537ed5 | 3628 | if (!QTAILQ_IN_USE(bs, monitor_list)) { |
9ec8873e KW |
3629 | error_setg(errp, "Node %s is not owned by the monitor", |
3630 | bs->node_name); | |
3631 | goto out; | |
81b936ae AG |
3632 | } |
3633 | ||
9ec8873e KW |
3634 | if (bs->refcnt > 1) { |
3635 | error_setg(errp, "Block device %s is in use", | |
3636 | bdrv_get_device_or_node_name(bs)); | |
3637 | goto out; | |
81b936ae AG |
3638 | } |
3639 | ||
9ec8873e KW |
3640 | QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); |
3641 | bdrv_unref(bs); | |
3642 | ||
81b936ae AG |
3643 | out: |
3644 | aio_context_release(aio_context); | |
3645 | } | |
3646 | ||
7f821597 WC |
3647 | static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, |
3648 | const char *child_name) | |
3649 | { | |
3650 | BdrvChild *child; | |
3651 | ||
3652 | QLIST_FOREACH(child, &parent_bs->children, next) { | |
3653 | if (strcmp(child->name, child_name) == 0) { | |
3654 | return child; | |
3655 | } | |
3656 | } | |
3657 | ||
3658 | return NULL; | |
3659 | } | |
3660 | ||
3661 | void qmp_x_blockdev_change(const char *parent, bool has_child, | |
3662 | const char *child, bool has_node, | |
3663 | const char *node, Error **errp) | |
3664 | { | |
3665 | BlockDriverState *parent_bs, *new_bs = NULL; | |
3666 | BdrvChild *p_child; | |
3667 | ||
3668 | parent_bs = bdrv_lookup_bs(parent, parent, errp); | |
3669 | if (!parent_bs) { | |
3670 | return; | |
3671 | } | |
3672 | ||
3673 | if (has_child == has_node) { | |
3674 | if (has_child) { | |
3675 | error_setg(errp, "The parameters child and node are in conflict"); | |
3676 | } else { | |
3677 | error_setg(errp, "Either child or node must be specified"); | |
3678 | } | |
3679 | return; | |
3680 | } | |
3681 | ||
3682 | if (has_child) { | |
3683 | p_child = bdrv_find_child(parent_bs, child); | |
3684 | if (!p_child) { | |
3685 | error_setg(errp, "Node '%s' does not have child '%s'", | |
3686 | parent, child); | |
3687 | return; | |
3688 | } | |
3689 | bdrv_del_child(parent_bs, p_child, errp); | |
3690 | } | |
3691 | ||
3692 | if (has_node) { | |
3693 | new_bs = bdrv_find_node(node); | |
3694 | if (!new_bs) { | |
3695 | error_setg(errp, "Node '%s' not found", node); | |
3696 | return; | |
3697 | } | |
3698 | bdrv_add_child(parent_bs, new_bs, errp); | |
3699 | } | |
3700 | } | |
3701 | ||
fea68bb6 | 3702 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
fb5458cd | 3703 | { |
c3033fd3 | 3704 | BlockJobInfoList *head = NULL, **tail = &head; |
f0f55ded | 3705 | BlockJob *job; |
fb5458cd | 3706 | |
f0f55ded | 3707 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { |
c3033fd3 | 3708 | BlockJobInfo *value; |
559b935f | 3709 | AioContext *aio_context; |
69691e72 | 3710 | |
559b935f JS |
3711 | if (block_job_is_internal(job)) { |
3712 | continue; | |
3713 | } | |
559b935f | 3714 | aio_context = blk_get_aio_context(job->blk); |
69691e72 | 3715 | aio_context_acquire(aio_context); |
c3033fd3 | 3716 | value = block_job_query(job, errp); |
69691e72 | 3717 | aio_context_release(aio_context); |
c3033fd3 | 3718 | if (!value) { |
559b935f JS |
3719 | qapi_free_BlockJobInfoList(head); |
3720 | return NULL; | |
3721 | } | |
c3033fd3 | 3722 | QAPI_LIST_APPEND(tail, value); |
fb5458cd | 3723 | } |
fb5458cd | 3724 | |
fea68bb6 | 3725 | return head; |
fb5458cd | 3726 | } |
4d454574 | 3727 | |
ca00bbb1 | 3728 | void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, |
882e9b89 | 3729 | bool has_force, bool force, Error **errp) |
ca00bbb1 SH |
3730 | { |
3731 | AioContext *old_context; | |
3732 | AioContext *new_context; | |
3733 | BlockDriverState *bs; | |
3734 | ||
3735 | bs = bdrv_find_node(node_name); | |
3736 | if (!bs) { | |
ef2e38a1 | 3737 | error_setg(errp, "Failed to find node with node-name='%s'", node_name); |
ca00bbb1 SH |
3738 | return; |
3739 | } | |
3740 | ||
882e9b89 SH |
3741 | /* Protects against accidents. */ |
3742 | if (!(has_force && force) && bdrv_has_blk(bs)) { | |
3743 | error_setg(errp, "Node %s is associated with a BlockBackend and could " | |
3744 | "be in use (use force=true to override this check)", | |
3745 | node_name); | |
ca00bbb1 SH |
3746 | return; |
3747 | } | |
3748 | ||
3749 | if (iothread->type == QTYPE_QSTRING) { | |
3750 | IOThread *obj = iothread_by_id(iothread->u.s); | |
3751 | if (!obj) { | |
3752 | error_setg(errp, "Cannot find iothread %s", iothread->u.s); | |
3753 | return; | |
3754 | } | |
3755 | ||
3756 | new_context = iothread_get_aio_context(obj); | |
3757 | } else { | |
3758 | new_context = qemu_get_aio_context(); | |
3759 | } | |
3760 | ||
3761 | old_context = bdrv_get_aio_context(bs); | |
3762 | aio_context_acquire(old_context); | |
3763 | ||
8ed7d42f | 3764 | bdrv_try_set_aio_context(bs, new_context, errp); |
ca00bbb1 SH |
3765 | |
3766 | aio_context_release(old_context); | |
3767 | } | |
3768 | ||
0006383e | 3769 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 3770 | .name = "drive", |
0006383e | 3771 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
3772 | .desc = { |
3773 | { | |
4d454574 PB |
3774 | .name = "snapshot", |
3775 | .type = QEMU_OPT_BOOL, | |
3776 | .help = "enable/disable snapshot mode", | |
4d454574 PB |
3777 | },{ |
3778 | .name = "aio", | |
3779 | .type = QEMU_OPT_STRING, | |
f80f2673 | 3780 | .help = "host AIO implementation (threads, native, io_uring)", |
e4b24b49 KW |
3781 | },{ |
3782 | .name = BDRV_OPT_CACHE_WB, | |
3783 | .type = QEMU_OPT_BOOL, | |
3784 | .help = "Enable writeback mode", | |
4d454574 PB |
3785 | },{ |
3786 | .name = "format", | |
3787 | .type = QEMU_OPT_STRING, | |
3788 | .help = "disk format (raw, qcow2, ...)", | |
4d454574 PB |
3789 | },{ |
3790 | .name = "rerror", | |
3791 | .type = QEMU_OPT_STRING, | |
3792 | .help = "read error action", | |
3793 | },{ | |
3794 | .name = "werror", | |
3795 | .type = QEMU_OPT_STRING, | |
3796 | .help = "write error action", | |
4d454574 | 3797 | },{ |
4e200cf8 | 3798 | .name = BDRV_OPT_READ_ONLY, |
4d454574 PB |
3799 | .type = QEMU_OPT_BOOL, |
3800 | .help = "open drive file as read-only", | |
a2a7862c PJ |
3801 | }, |
3802 | ||
3803 | THROTTLE_OPTS, | |
3804 | ||
3805 | { | |
76f4afb4 AG |
3806 | .name = "throttling.group", |
3807 | .type = QEMU_OPT_STRING, | |
3808 | .help = "name of the block throttling group", | |
4d454574 PB |
3809 | },{ |
3810 | .name = "copy-on-read", | |
3811 | .type = QEMU_OPT_BOOL, | |
3812 | .help = "copy read data from backing file into image file", | |
465bee1d PL |
3813 | },{ |
3814 | .name = "detect-zeroes", | |
3815 | .type = QEMU_OPT_STRING, | |
3816 | .help = "try to optimize zero writes (off, on, unmap)", | |
362e9299 AG |
3817 | },{ |
3818 | .name = "stats-account-invalid", | |
3819 | .type = QEMU_OPT_BOOL, | |
3820 | .help = "whether to account for invalid I/O operations " | |
3821 | "in the statistics", | |
3822 | },{ | |
3823 | .name = "stats-account-failed", | |
3824 | .type = QEMU_OPT_BOOL, | |
3825 | .help = "whether to account for failed I/O operations " | |
3826 | "in the statistics", | |
4d454574 PB |
3827 | }, |
3828 | { /* end of list */ } | |
3829 | }, | |
3830 | }; | |
0006383e KW |
3831 | |
3832 | QemuOptsList qemu_drive_opts = { | |
3833 | .name = "drive", | |
3834 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
3835 | .desc = { | |
492fdc6f KW |
3836 | /* |
3837 | * no elements => accept any params | |
3838 | * validation will happen later | |
3839 | */ | |
0006383e KW |
3840 | { /* end of list */ } |
3841 | }, | |
3842 | }; |