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