]>
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 PB |
42 | #include "qemu/option.h" |
43 | #include "qemu/config-file.h" | |
9af23989 MA |
44 | #include "qapi/qapi-commands-block.h" |
45 | #include "qapi/qapi-commands-transaction.h" | |
46 | #include "qapi/qapi-visit-block-core.h" | |
452fcdbc | 47 | #include "qapi/qmp/qdict.h" |
15280c36 | 48 | #include "qapi/qmp/qnum.h" |
6b673957 | 49 | #include "qapi/qmp/qstring.h" |
e688df6b | 50 | #include "qapi/error.h" |
cc7a8ea7 | 51 | #include "qapi/qmp/qerror.h" |
47e6b297 | 52 | #include "qapi/qmp/qlist.h" |
b3db211f | 53 | #include "qapi/qobject-output-visitor.h" |
9c17d615 | 54 | #include "sysemu/sysemu.h" |
ca00bbb1 | 55 | #include "sysemu/iothread.h" |
737e150e | 56 | #include "block/block_int.h" |
0ab8ed18 | 57 | #include "block/trace.h" |
9c17d615 | 58 | #include "sysemu/arch_init.h" |
c616f16e | 59 | #include "sysemu/qtest.h" |
f348b6d1 VB |
60 | #include "qemu/cutils.h" |
61 | #include "qemu/help_option.h" | |
a2a7862c | 62 | #include "qemu/throttle-options.h" |
666daa68 | 63 | |
9c4218e9 HR |
64 | static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = |
65 | QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); | |
66 | ||
b33945cf KW |
67 | static int do_open_tray(const char *blk_name, const char *qdev_id, |
68 | bool force, Error **errp); | |
82fcf66e HR |
69 | static void blockdev_remove_medium(bool has_device, const char *device, |
70 | bool has_id, const char *id, Error **errp); | |
71 | static void blockdev_insert_medium(bool has_device, const char *device, | |
72 | bool has_id, const char *id, | |
73 | const char *node_name, Error **errp); | |
bf18bee5 | 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); |
4be74634 | 142 | BlockDriverState *bs = blk_bs(blk); |
91fddb0d | 143 | AioContext *aio_context; |
14bafc54 | 144 | |
26f8b3a8 | 145 | if (!dinfo) { |
2d246f01 KW |
146 | return; |
147 | } | |
148 | ||
5433c24f HR |
149 | if (bs) { |
150 | aio_context = bdrv_get_aio_context(bs); | |
151 | aio_context_acquire(aio_context); | |
91fddb0d | 152 | |
5433c24f | 153 | if (bs->job) { |
3d70ff53 | 154 | job_cancel(&bs->job->job, false); |
5433c24f | 155 | } |
91fddb0d | 156 | |
5433c24f HR |
157 | aio_context_release(aio_context); |
158 | } | |
91fddb0d | 159 | |
26f8b3a8 | 160 | dinfo->auto_del = 1; |
14bafc54 MA |
161 | } |
162 | ||
4be74634 | 163 | void blockdev_auto_del(BlockBackend *blk) |
14bafc54 | 164 | { |
18e46a03 | 165 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
14bafc54 | 166 | |
0fc0f1fa | 167 | if (dinfo && dinfo->auto_del) { |
efaa7c4e | 168 | monitor_remove_blk(blk); |
b9fe8a7a | 169 | blk_unref(blk); |
14bafc54 MA |
170 | } |
171 | } | |
172 | ||
d8f94e1b JS |
173 | /** |
174 | * Returns the current mapping of how many units per bus | |
175 | * a particular interface can support. | |
176 | * | |
177 | * A positive integer indicates n units per bus. | |
178 | * 0 implies the mapping has not been established. | |
179 | * -1 indicates an invalid BlockInterfaceType was given. | |
180 | */ | |
181 | int drive_get_max_devs(BlockInterfaceType type) | |
182 | { | |
183 | if (type >= IF_IDE && type < IF_COUNT) { | |
184 | return if_max_devs[type]; | |
185 | } | |
186 | ||
187 | return -1; | |
188 | } | |
189 | ||
505a7fb1 MA |
190 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
191 | { | |
192 | int max_devs = if_max_devs[type]; | |
193 | return max_devs ? index / max_devs : 0; | |
194 | } | |
195 | ||
196 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
197 | { | |
198 | int max_devs = if_max_devs[type]; | |
199 | return max_devs ? index % max_devs : index; | |
200 | } | |
201 | ||
2292ddae MA |
202 | QemuOpts *drive_def(const char *optstr) |
203 | { | |
70b94331 | 204 | return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); |
2292ddae MA |
205 | } |
206 | ||
207 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 208 | const char *optstr) |
666daa68 | 209 | { |
666daa68 MA |
210 | QemuOpts *opts; |
211 | ||
2292ddae | 212 | opts = drive_def(optstr); |
666daa68 MA |
213 | if (!opts) { |
214 | return NULL; | |
215 | } | |
2292ddae | 216 | if (type != IF_DEFAULT) { |
f43e47db | 217 | qemu_opt_set(opts, "if", if_name[type], &error_abort); |
2292ddae MA |
218 | } |
219 | if (index >= 0) { | |
a8b18f8f | 220 | qemu_opt_set_number(opts, "index", index, &error_abort); |
2292ddae | 221 | } |
666daa68 | 222 | if (file) |
f43e47db | 223 | qemu_opt_set(opts, "file", file, &error_abort); |
666daa68 MA |
224 | return opts; |
225 | } | |
226 | ||
227 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
228 | { | |
18e46a03 | 229 | BlockBackend *blk; |
666daa68 MA |
230 | DriveInfo *dinfo; |
231 | ||
18e46a03 MA |
232 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
233 | dinfo = blk_legacy_dinfo(blk); | |
234 | if (dinfo && dinfo->type == type | |
235 | && dinfo->bus == bus && dinfo->unit == unit) { | |
666daa68 | 236 | return dinfo; |
18e46a03 | 237 | } |
666daa68 MA |
238 | } |
239 | ||
240 | return NULL; | |
241 | } | |
242 | ||
720b8dc0 | 243 | void drive_check_orphaned(void) |
a66c9dc7 | 244 | { |
18e46a03 | 245 | BlockBackend *blk; |
a66c9dc7 | 246 | DriveInfo *dinfo; |
664cc623 | 247 | Location loc; |
720b8dc0 | 248 | bool orphans = false; |
a66c9dc7 | 249 | |
18e46a03 MA |
250 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
251 | dinfo = blk_legacy_dinfo(blk); | |
a7f53e26 | 252 | if (!blk_get_attached_dev(blk) && !dinfo->is_default && |
a66c9dc7 | 253 | dinfo->type != IF_NONE) { |
664cc623 MA |
254 | loc_push_none(&loc); |
255 | qemu_opts_loc_restore(dinfo->opts); | |
720b8dc0 | 256 | error_report("machine type does not support" |
664cc623 MA |
257 | " if=%s,bus=%d,unit=%d", |
258 | if_name[dinfo->type], dinfo->bus, dinfo->unit); | |
259 | loc_pop(&loc); | |
720b8dc0 | 260 | orphans = true; |
a66c9dc7 JS |
261 | } |
262 | } | |
263 | ||
720b8dc0 MA |
264 | if (orphans) { |
265 | exit(1); | |
266 | } | |
a66c9dc7 JS |
267 | } |
268 | ||
f1bd51ac MA |
269 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
270 | { | |
271 | return drive_get(type, | |
272 | drive_index_to_bus_id(type, index), | |
273 | drive_index_to_unit_id(type, index)); | |
274 | } | |
275 | ||
666daa68 MA |
276 | int drive_get_max_bus(BlockInterfaceType type) |
277 | { | |
278 | int max_bus; | |
18e46a03 | 279 | BlockBackend *blk; |
666daa68 MA |
280 | DriveInfo *dinfo; |
281 | ||
282 | max_bus = -1; | |
18e46a03 MA |
283 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
284 | dinfo = blk_legacy_dinfo(blk); | |
285 | if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { | |
666daa68 | 286 | max_bus = dinfo->bus; |
18e46a03 | 287 | } |
666daa68 MA |
288 | } |
289 | return max_bus; | |
290 | } | |
291 | ||
13839974 MA |
292 | /* Get a block device. This should only be used for single-drive devices |
293 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the | |
294 | appropriate bus. */ | |
295 | DriveInfo *drive_get_next(BlockInterfaceType type) | |
296 | { | |
297 | static int next_block_unit[IF_COUNT]; | |
298 | ||
299 | return drive_get(type, 0, next_block_unit[type]++); | |
300 | } | |
301 | ||
666daa68 MA |
302 | static void bdrv_format_print(void *opaque, const char *name) |
303 | { | |
807105a7 | 304 | error_printf(" %s", name); |
666daa68 MA |
305 | } |
306 | ||
aa398a5c SH |
307 | typedef struct { |
308 | QEMUBH *bh; | |
fa510ebf FZ |
309 | BlockDriverState *bs; |
310 | } BDRVPutRefBH; | |
aa398a5c | 311 | |
b681072d | 312 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) |
666daa68 MA |
313 | { |
314 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 315 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 316 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 317 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 318 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 319 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 320 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 321 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 322 | } else { |
b681072d KW |
323 | error_setg(errp, "'%s' invalid %s error action", |
324 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
325 | return -1; |
326 | } | |
327 | } | |
328 | ||
40119eff AG |
329 | static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, |
330 | Error **errp) | |
331 | { | |
332 | const QListEntry *entry; | |
333 | for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { | |
334 | switch (qobject_type(entry->value)) { | |
335 | ||
336 | case QTYPE_QSTRING: { | |
337 | unsigned long long length; | |
7dc847eb HR |
338 | const char *str = qstring_get_str(qobject_to(QString, |
339 | entry->value)); | |
40119eff AG |
340 | if (parse_uint_full(str, &length, 10) == 0 && |
341 | length > 0 && length <= UINT_MAX) { | |
342 | block_acct_add_interval(stats, (unsigned) length); | |
343 | } else { | |
344 | error_setg(errp, "Invalid interval length: %s", str); | |
345 | return false; | |
346 | } | |
347 | break; | |
348 | } | |
349 | ||
01b2ffce | 350 | case QTYPE_QNUM: { |
7dc847eb | 351 | int64_t length = qnum_get_int(qobject_to(QNum, entry->value)); |
01b2ffce | 352 | |
40119eff AG |
353 | if (length > 0 && length <= UINT_MAX) { |
354 | block_acct_add_interval(stats, (unsigned) length); | |
355 | } else { | |
356 | error_setg(errp, "Invalid interval length: %" PRId64, length); | |
357 | return false; | |
358 | } | |
359 | break; | |
360 | } | |
361 | ||
362 | default: | |
363 | error_setg(errp, "The specification of stats-intervals is invalid"); | |
364 | return false; | |
365 | } | |
366 | } | |
367 | return true; | |
368 | } | |
369 | ||
33cb7dc8 KW |
370 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
371 | ||
fbf8175e HR |
372 | /* All parameters but @opts are optional and may be set to NULL. */ |
373 | static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, | |
374 | const char **throttling_group, ThrottleConfig *throttle_cfg, | |
375 | BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) | |
376 | { | |
fbf8175e HR |
377 | Error *local_error = NULL; |
378 | const char *aio; | |
379 | ||
380 | if (bdrv_flags) { | |
fbf8175e HR |
381 | if (qemu_opt_get_bool(opts, "copy-on-read", false)) { |
382 | *bdrv_flags |= BDRV_O_COPY_ON_READ; | |
383 | } | |
384 | ||
fbf8175e HR |
385 | if ((aio = qemu_opt_get(opts, "aio")) != NULL) { |
386 | if (!strcmp(aio, "native")) { | |
387 | *bdrv_flags |= BDRV_O_NATIVE_AIO; | |
388 | } else if (!strcmp(aio, "threads")) { | |
389 | /* this is the default */ | |
390 | } else { | |
391 | error_setg(errp, "invalid aio option"); | |
392 | return; | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | /* disk I/O throttling */ | |
398 | if (throttling_group) { | |
399 | *throttling_group = qemu_opt_get(opts, "throttling.group"); | |
400 | } | |
401 | ||
402 | if (throttle_cfg) { | |
1588ab5d | 403 | throttle_config_init(throttle_cfg); |
fbf8175e HR |
404 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = |
405 | qemu_opt_get_number(opts, "throttling.bps-total", 0); | |
406 | throttle_cfg->buckets[THROTTLE_BPS_READ].avg = | |
407 | qemu_opt_get_number(opts, "throttling.bps-read", 0); | |
408 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = | |
409 | qemu_opt_get_number(opts, "throttling.bps-write", 0); | |
410 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = | |
411 | qemu_opt_get_number(opts, "throttling.iops-total", 0); | |
412 | throttle_cfg->buckets[THROTTLE_OPS_READ].avg = | |
413 | qemu_opt_get_number(opts, "throttling.iops-read", 0); | |
414 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = | |
415 | qemu_opt_get_number(opts, "throttling.iops-write", 0); | |
416 | ||
417 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = | |
418 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
419 | throttle_cfg->buckets[THROTTLE_BPS_READ].max = | |
420 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
421 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = | |
422 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
423 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = | |
424 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
425 | throttle_cfg->buckets[THROTTLE_OPS_READ].max = | |
426 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
427 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = | |
428 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
429 | ||
8a0fc18d AG |
430 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = |
431 | qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); | |
432 | throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = | |
433 | qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); | |
434 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = | |
435 | qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); | |
436 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = | |
437 | qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); | |
438 | throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = | |
439 | qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); | |
440 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = | |
441 | qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); | |
442 | ||
fbf8175e HR |
443 | throttle_cfg->op_size = |
444 | qemu_opt_get_number(opts, "throttling.iops-size", 0); | |
445 | ||
d5851089 | 446 | if (!throttle_is_valid(throttle_cfg, errp)) { |
fbf8175e HR |
447 | return; |
448 | } | |
449 | } | |
450 | ||
451 | if (detect_zeroes) { | |
452 | *detect_zeroes = | |
f7abe0ec | 453 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, |
fbf8175e | 454 | qemu_opt_get(opts, "detect-zeroes"), |
fbf8175e HR |
455 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
456 | &local_error); | |
457 | if (local_error) { | |
458 | error_propagate(errp, local_error); | |
459 | return; | |
460 | } | |
fbf8175e HR |
461 | } |
462 | } | |
463 | ||
f298d071 | 464 | /* Takes the ownership of bs_opts */ |
18e46a03 MA |
465 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
466 | Error **errp) | |
666daa68 MA |
467 | { |
468 | const char *buf; | |
666daa68 MA |
469 | int bdrv_flags = 0; |
470 | int on_read_error, on_write_error; | |
362e9299 | 471 | bool account_invalid, account_failed; |
f87a0e29 | 472 | bool writethrough, read_only; |
26f54e9a | 473 | BlockBackend *blk; |
a0f1eab1 | 474 | BlockDriverState *bs; |
cc0681c4 | 475 | ThrottleConfig cfg; |
666daa68 | 476 | int snapshot = 0; |
c546194f | 477 | Error *error = NULL; |
0006383e | 478 | QemuOpts *opts; |
40119eff AG |
479 | QDict *interval_dict = NULL; |
480 | QList *interval_list = NULL; | |
0006383e | 481 | const char *id; |
fbf8175e HR |
482 | BlockdevDetectZeroesOptions detect_zeroes = |
483 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; | |
484 | const char *throttling_group = NULL; | |
666daa68 | 485 | |
f298d071 KW |
486 | /* Check common options by copying from bs_opts to opts, all other options |
487 | * stay in bs_opts for processing by bdrv_open(). */ | |
488 | id = qdict_get_try_str(bs_opts, "id"); | |
0006383e | 489 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
84d18f06 | 490 | if (error) { |
b681072d | 491 | error_propagate(errp, error); |
6376f952 | 492 | goto err_no_opts; |
0006383e KW |
493 | } |
494 | ||
0006383e | 495 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
84d18f06 | 496 | if (error) { |
b681072d | 497 | error_propagate(errp, error); |
ec9c10d2 | 498 | goto early_err; |
0006383e KW |
499 | } |
500 | ||
501 | if (id) { | |
502 | qdict_del(bs_opts, "id"); | |
503 | } | |
504 | ||
666daa68 | 505 | /* extract parameters */ |
666daa68 | 506 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
666daa68 | 507 | |
362e9299 AG |
508 | account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); |
509 | account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); | |
510 | ||
e4b24b49 KW |
511 | writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); |
512 | ||
ff356ee4 AG |
513 | id = qemu_opts_id(opts); |
514 | ||
40119eff AG |
515 | qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); |
516 | qdict_array_split(interval_dict, &interval_list); | |
517 | ||
518 | if (qdict_size(interval_dict) != 0) { | |
519 | error_setg(errp, "Invalid option stats-intervals.%s", | |
520 | qdict_first(interval_dict)->key); | |
521 | goto early_err; | |
522 | } | |
2be5506f | 523 | |
fbf8175e HR |
524 | extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, |
525 | &detect_zeroes, &error); | |
526 | if (error) { | |
527 | error_propagate(errp, error); | |
528 | goto early_err; | |
666daa68 | 529 | } |
666daa68 MA |
530 | |
531 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 PM |
532 | if (is_help_option(buf)) { |
533 | error_printf("Supported formats:"); | |
534 | bdrv_iterate_format(bdrv_format_print, NULL); | |
535 | error_printf("\n"); | |
ec9c10d2 | 536 | goto early_err; |
666daa68 | 537 | } |
74fe54f2 | 538 | |
e4342ce5 HR |
539 | if (qdict_haskey(bs_opts, "driver")) { |
540 | error_setg(errp, "Cannot specify both 'driver' and 'format'"); | |
ec9c10d2 | 541 | goto early_err; |
6db5f5d6 | 542 | } |
46f5ac20 | 543 | qdict_put_str(bs_opts, "driver", buf); |
666daa68 MA |
544 | } |
545 | ||
92aa5c6d | 546 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 547 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
b681072d | 548 | on_write_error = parse_block_error_action(buf, 0, &error); |
84d18f06 | 549 | if (error) { |
b681072d | 550 | error_propagate(errp, error); |
ec9c10d2 | 551 | goto early_err; |
666daa68 MA |
552 | } |
553 | } | |
554 | ||
92aa5c6d | 555 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 556 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
b681072d | 557 | on_read_error = parse_block_error_action(buf, 1, &error); |
84d18f06 | 558 | if (error) { |
b681072d | 559 | error_propagate(errp, error); |
ec9c10d2 | 560 | goto early_err; |
666daa68 MA |
561 | } |
562 | } | |
563 | ||
5ec18f8c | 564 | if (snapshot) { |
91a097e7 | 565 | bdrv_flags |= BDRV_O_SNAPSHOT; |
5ec18f8c HR |
566 | } |
567 | ||
f87a0e29 AG |
568 | read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); |
569 | ||
326642bc | 570 | /* init */ |
39c4ae94 | 571 | if ((!file || !*file) && !qdict_size(bs_opts)) { |
5ec18f8c HR |
572 | BlockBackendRootState *blk_rs; |
573 | ||
6d0eb64d | 574 | blk = blk_new(0, BLK_PERM_ALL); |
5ec18f8c HR |
575 | blk_rs = blk_get_root_state(blk); |
576 | blk_rs->open_flags = bdrv_flags; | |
f87a0e29 | 577 | blk_rs->read_only = read_only; |
5ec18f8c HR |
578 | blk_rs->detect_zeroes = detect_zeroes; |
579 | ||
cb3e7f08 | 580 | qobject_unref(bs_opts); |
e4342ce5 HR |
581 | } else { |
582 | if (file && !*file) { | |
c2ad1b0c | 583 | file = NULL; |
c2ad1b0c | 584 | } |
666daa68 | 585 | |
91a097e7 KW |
586 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
587 | * with other callers) rather than what we want as the real defaults. | |
588 | * Apply the defaults here instead. */ | |
91a097e7 KW |
589 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
590 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
f87a0e29 AG |
591 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, |
592 | read_only ? "on" : "off"); | |
72e775c7 | 593 | assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); |
91a097e7 | 594 | |
12d5ee3a KW |
595 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
596 | bdrv_flags |= BDRV_O_INACTIVE; | |
597 | } | |
598 | ||
efaa7c4e | 599 | blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); |
e4342ce5 HR |
600 | if (!blk) { |
601 | goto err_no_bs_opts; | |
602 | } | |
603 | bs = blk_bs(blk); | |
ed9d4205 | 604 | |
5ec18f8c | 605 | bs->detect_zeroes = detect_zeroes; |
666daa68 | 606 | |
9caa6f3d | 607 | block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); |
2be5506f | 608 | |
40119eff AG |
609 | if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { |
610 | blk_unref(blk); | |
611 | blk = NULL; | |
612 | goto err_no_bs_opts; | |
2be5506f | 613 | } |
666daa68 MA |
614 | } |
615 | ||
7ca7f0f6 KW |
616 | /* disk I/O throttling */ |
617 | if (throttle_enabled(&cfg)) { | |
618 | if (!throttling_group) { | |
ff356ee4 | 619 | throttling_group = id; |
7ca7f0f6 KW |
620 | } |
621 | blk_io_limits_enable(blk, throttling_group); | |
622 | blk_set_io_limits(blk, &cfg); | |
623 | } | |
624 | ||
e4b24b49 | 625 | blk_set_enable_write_cache(blk, !writethrough); |
5ec18f8c | 626 | blk_set_on_error(blk, on_read_error, on_write_error); |
0006383e | 627 | |
ff356ee4 | 628 | if (!monitor_add_blk(blk, id, errp)) { |
efaa7c4e HR |
629 | blk_unref(blk); |
630 | blk = NULL; | |
631 | goto err_no_bs_opts; | |
632 | } | |
633 | ||
e4342ce5 | 634 | err_no_bs_opts: |
0006383e | 635 | qemu_opts_del(opts); |
cb3e7f08 MAL |
636 | qobject_unref(interval_dict); |
637 | qobject_unref(interval_list); | |
18e46a03 | 638 | return blk; |
a9ae2bff | 639 | |
ec9c10d2 | 640 | early_err: |
ec9c10d2 | 641 | qemu_opts_del(opts); |
cb3e7f08 MAL |
642 | qobject_unref(interval_dict); |
643 | qobject_unref(interval_list); | |
6376f952 | 644 | err_no_opts: |
cb3e7f08 | 645 | qobject_unref(bs_opts); |
a9ae2bff | 646 | return NULL; |
666daa68 MA |
647 | } |
648 | ||
bd745e23 HR |
649 | /* Takes the ownership of bs_opts */ |
650 | static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) | |
651 | { | |
bd745e23 HR |
652 | int bdrv_flags = 0; |
653 | ||
a81d6164 KW |
654 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
655 | * with other callers) rather than what we want as the real defaults. | |
656 | * Apply the defaults here instead. */ | |
a81d6164 KW |
657 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
658 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
f87a0e29 | 659 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); |
a81d6164 | 660 | |
12d5ee3a KW |
661 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
662 | bdrv_flags |= BDRV_O_INACTIVE; | |
663 | } | |
664 | ||
74e1ae7c | 665 | return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); |
bd745e23 HR |
666 | } |
667 | ||
9c4218e9 HR |
668 | void blockdev_close_all_bdrv_states(void) |
669 | { | |
670 | BlockDriverState *bs, *next_bs; | |
671 | ||
672 | QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { | |
673 | AioContext *ctx = bdrv_get_aio_context(bs); | |
674 | ||
675 | aio_context_acquire(ctx); | |
676 | bdrv_unref(bs); | |
677 | aio_context_release(ctx); | |
678 | } | |
679 | } | |
680 | ||
262b4e8f HR |
681 | /* Iterates over the list of monitor-owned BlockDriverStates */ |
682 | BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) | |
683 | { | |
684 | return bs ? QTAILQ_NEXT(bs, monitor_list) | |
685 | : QTAILQ_FIRST(&monitor_bdrv_states); | |
686 | } | |
687 | ||
5abbf0ee KW |
688 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
689 | Error **errp) | |
57975222 KW |
690 | { |
691 | const char *value; | |
692 | ||
693 | value = qemu_opt_get(opts, from); | |
694 | if (value) { | |
5abbf0ee KW |
695 | if (qemu_opt_find(opts, to)) { |
696 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " | |
697 | "same time", to, from); | |
698 | return; | |
699 | } | |
20d6cd47 JL |
700 | } |
701 | ||
702 | /* rename all items in opts */ | |
703 | while ((value = qemu_opt_get(opts, from))) { | |
f43e47db | 704 | qemu_opt_set(opts, to, value, &error_abort); |
57975222 KW |
705 | qemu_opt_unset(opts, from); |
706 | } | |
707 | } | |
708 | ||
33cb7dc8 KW |
709 | QemuOptsList qemu_legacy_drive_opts = { |
710 | .name = "drive", | |
711 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
712 | .desc = { | |
713 | { | |
87a899c5 KW |
714 | .name = "bus", |
715 | .type = QEMU_OPT_NUMBER, | |
716 | .help = "bus number", | |
717 | },{ | |
718 | .name = "unit", | |
719 | .type = QEMU_OPT_NUMBER, | |
720 | .help = "unit number (i.e. lun for scsi)", | |
721 | },{ | |
722 | .name = "index", | |
723 | .type = QEMU_OPT_NUMBER, | |
724 | .help = "index number", | |
725 | },{ | |
33cb7dc8 KW |
726 | .name = "media", |
727 | .type = QEMU_OPT_STRING, | |
728 | .help = "media type (disk, cdrom)", | |
593d464b KW |
729 | },{ |
730 | .name = "if", | |
731 | .type = QEMU_OPT_STRING, | |
732 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
44e8b468 CH |
733 | },{ |
734 | .name = "serial", | |
735 | .type = QEMU_OPT_STRING, | |
736 | .help = "disk serial number", | |
d095b465 HR |
737 | },{ |
738 | .name = "file", | |
739 | .type = QEMU_OPT_STRING, | |
740 | .help = "file name", | |
33cb7dc8 | 741 | }, |
0ebd24e0 KW |
742 | |
743 | /* Options that are passed on, but have special semantics with -drive */ | |
744 | { | |
4e200cf8 | 745 | .name = BDRV_OPT_READ_ONLY, |
0ebd24e0 KW |
746 | .type = QEMU_OPT_BOOL, |
747 | .help = "open drive file as read-only", | |
ee13ed1c KW |
748 | },{ |
749 | .name = "rerror", | |
750 | .type = QEMU_OPT_STRING, | |
751 | .help = "read error action", | |
752 | },{ | |
753 | .name = "werror", | |
754 | .type = QEMU_OPT_STRING, | |
755 | .help = "write error action", | |
0ebd24e0 KW |
756 | },{ |
757 | .name = "copy-on-read", | |
758 | .type = QEMU_OPT_BOOL, | |
759 | .help = "copy read data from backing file into image file", | |
760 | }, | |
761 | ||
33cb7dc8 KW |
762 | { /* end of list */ } |
763 | }, | |
764 | }; | |
765 | ||
60e19e06 | 766 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) |
57975222 | 767 | { |
29c4e2b5 | 768 | const char *value; |
18e46a03 | 769 | BlockBackend *blk; |
33cb7dc8 | 770 | DriveInfo *dinfo = NULL; |
f298d071 | 771 | QDict *bs_opts; |
33cb7dc8 KW |
772 | QemuOpts *legacy_opts; |
773 | DriveMediaType media = MEDIA_DISK; | |
593d464b | 774 | BlockInterfaceType type; |
87a899c5 | 775 | int max_devs, bus_id, unit_id, index; |
ee13ed1c | 776 | const char *werror, *rerror; |
a7fdbcf0 FZ |
777 | bool read_only = false; |
778 | bool copy_on_read; | |
44e8b468 | 779 | const char *serial; |
d095b465 | 780 | const char *filename; |
33cb7dc8 | 781 | Error *local_err = NULL; |
247147fb | 782 | int i; |
19a49c56 | 783 | const char *deprecated[] = { |
7f8fc971 | 784 | "serial" |
19a49c56 | 785 | }; |
29c4e2b5 | 786 | |
57975222 | 787 | /* Change legacy command line options into QMP ones */ |
247147fb KW |
788 | static const struct { |
789 | const char *from; | |
790 | const char *to; | |
791 | } opt_renames[] = { | |
792 | { "iops", "throttling.iops-total" }, | |
793 | { "iops_rd", "throttling.iops-read" }, | |
794 | { "iops_wr", "throttling.iops-write" }, | |
57975222 | 795 | |
247147fb KW |
796 | { "bps", "throttling.bps-total" }, |
797 | { "bps_rd", "throttling.bps-read" }, | |
798 | { "bps_wr", "throttling.bps-write" }, | |
57975222 | 799 | |
247147fb KW |
800 | { "iops_max", "throttling.iops-total-max" }, |
801 | { "iops_rd_max", "throttling.iops-read-max" }, | |
802 | { "iops_wr_max", "throttling.iops-write-max" }, | |
3e9fab69 | 803 | |
247147fb KW |
804 | { "bps_max", "throttling.bps-total-max" }, |
805 | { "bps_rd_max", "throttling.bps-read-max" }, | |
806 | { "bps_wr_max", "throttling.bps-write-max" }, | |
3e9fab69 | 807 | |
247147fb | 808 | { "iops_size", "throttling.iops-size" }, |
2024c1df | 809 | |
76f4afb4 AG |
810 | { "group", "throttling.group" }, |
811 | ||
4e200cf8 | 812 | { "readonly", BDRV_OPT_READ_ONLY }, |
247147fb KW |
813 | }; |
814 | ||
815 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
5abbf0ee KW |
816 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, |
817 | &local_err); | |
818 | if (local_err) { | |
565f65d2 | 819 | error_report_err(local_err); |
5abbf0ee KW |
820 | return NULL; |
821 | } | |
247147fb | 822 | } |
0f227a94 | 823 | |
29c4e2b5 KW |
824 | value = qemu_opt_get(all_opts, "cache"); |
825 | if (value) { | |
826 | int flags = 0; | |
04feb4a5 | 827 | bool writethrough; |
29c4e2b5 | 828 | |
04feb4a5 | 829 | if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { |
29c4e2b5 KW |
830 | error_report("invalid cache option"); |
831 | return NULL; | |
832 | } | |
833 | ||
834 | /* Specific options take precedence */ | |
54861b92 KW |
835 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { |
836 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, | |
04feb4a5 | 837 | !writethrough, &error_abort); |
29c4e2b5 | 838 | } |
54861b92 KW |
839 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { |
840 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, | |
cccb7967 | 841 | !!(flags & BDRV_O_NOCACHE), &error_abort); |
29c4e2b5 | 842 | } |
54861b92 KW |
843 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { |
844 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, | |
cccb7967 | 845 | !!(flags & BDRV_O_NO_FLUSH), &error_abort); |
29c4e2b5 KW |
846 | } |
847 | qemu_opt_unset(all_opts, "cache"); | |
848 | } | |
849 | ||
f298d071 KW |
850 | /* Get a QDict for processing the options */ |
851 | bs_opts = qdict_new(); | |
852 | qemu_opts_to_qdict(all_opts, bs_opts); | |
853 | ||
87ea75d5 PC |
854 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
855 | &error_abort); | |
33cb7dc8 | 856 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); |
84d18f06 | 857 | if (local_err) { |
565f65d2 | 858 | error_report_err(local_err); |
33cb7dc8 KW |
859 | goto fail; |
860 | } | |
861 | ||
19a49c56 CH |
862 | /* Other deprecated options */ |
863 | if (!qtest_enabled()) { | |
864 | for (i = 0; i < ARRAY_SIZE(deprecated); i++) { | |
865 | if (qemu_opt_get(legacy_opts, deprecated[i]) != NULL) { | |
866 | error_report("'%s' is deprecated, please use the corresponding " | |
867 | "option of '-device' instead", deprecated[i]); | |
868 | } | |
869 | } | |
870 | } | |
871 | ||
33cb7dc8 KW |
872 | /* Media type */ |
873 | value = qemu_opt_get(legacy_opts, "media"); | |
874 | if (value) { | |
875 | if (!strcmp(value, "disk")) { | |
876 | media = MEDIA_DISK; | |
877 | } else if (!strcmp(value, "cdrom")) { | |
878 | media = MEDIA_CDROM; | |
a7fdbcf0 | 879 | read_only = true; |
33cb7dc8 KW |
880 | } else { |
881 | error_report("'%s' invalid media", value); | |
882 | goto fail; | |
883 | } | |
884 | } | |
885 | ||
0ebd24e0 | 886 | /* copy-on-read is disabled with a warning for read-only devices */ |
4e200cf8 | 887 | read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); |
0ebd24e0 KW |
888 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
889 | ||
890 | if (read_only && copy_on_read) { | |
3dc6f869 | 891 | warn_report("disabling copy-on-read on read-only drive"); |
0ebd24e0 KW |
892 | copy_on_read = false; |
893 | } | |
894 | ||
46f5ac20 EB |
895 | qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); |
896 | qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); | |
0ebd24e0 | 897 | |
593d464b KW |
898 | /* Controller type */ |
899 | value = qemu_opt_get(legacy_opts, "if"); | |
900 | if (value) { | |
901 | for (type = 0; | |
902 | type < IF_COUNT && strcmp(value, if_name[type]); | |
903 | type++) { | |
904 | } | |
905 | if (type == IF_COUNT) { | |
906 | error_report("unsupported bus type '%s'", value); | |
907 | goto fail; | |
908 | } | |
909 | } else { | |
910 | type = block_default_type; | |
911 | } | |
912 | ||
87a899c5 KW |
913 | /* Device address specified by bus/unit or index. |
914 | * If none was specified, try to find the first free one. */ | |
915 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); | |
916 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); | |
917 | index = qemu_opt_get_number(legacy_opts, "index", -1); | |
918 | ||
919 | max_devs = if_max_devs[type]; | |
920 | ||
921 | if (index != -1) { | |
922 | if (bus_id != 0 || unit_id != -1) { | |
923 | error_report("index cannot be used with bus and unit"); | |
924 | goto fail; | |
925 | } | |
926 | bus_id = drive_index_to_bus_id(type, index); | |
927 | unit_id = drive_index_to_unit_id(type, index); | |
928 | } | |
929 | ||
930 | if (unit_id == -1) { | |
931 | unit_id = 0; | |
932 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
933 | unit_id++; | |
934 | if (max_devs && unit_id >= max_devs) { | |
935 | unit_id -= max_devs; | |
936 | bus_id++; | |
937 | } | |
938 | } | |
939 | } | |
940 | ||
941 | if (max_devs && unit_id >= max_devs) { | |
942 | error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); | |
943 | goto fail; | |
944 | } | |
945 | ||
946 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
947 | error_report("drive with bus=%d, unit=%d (index=%d) exists", | |
948 | bus_id, unit_id, index); | |
949 | goto fail; | |
950 | } | |
951 | ||
44e8b468 CH |
952 | /* Serial number */ |
953 | serial = qemu_opt_get(legacy_opts, "serial"); | |
954 | ||
87a899c5 KW |
955 | /* no id supplied -> create one */ |
956 | if (qemu_opts_id(all_opts) == NULL) { | |
957 | char *new_id; | |
958 | const char *mediastr = ""; | |
959 | if (type == IF_IDE || type == IF_SCSI) { | |
960 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
961 | } | |
962 | if (max_devs) { | |
963 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, | |
964 | mediastr, unit_id); | |
965 | } else { | |
966 | new_id = g_strdup_printf("%s%s%i", if_name[type], | |
967 | mediastr, unit_id); | |
968 | } | |
46f5ac20 | 969 | qdict_put_str(bs_opts, "id", new_id); |
87a899c5 KW |
970 | g_free(new_id); |
971 | } | |
972 | ||
394c7d4d | 973 | /* Add virtio block device */ |
394c7d4d KW |
974 | if (type == IF_VIRTIO) { |
975 | QemuOpts *devopts; | |
87ea75d5 PC |
976 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
977 | &error_abort); | |
394c7d4d | 978 | if (arch_type == QEMU_ARCH_S390X) { |
1f68f1d3 | 979 | qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); |
394c7d4d | 980 | } else { |
f43e47db | 981 | qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); |
394c7d4d | 982 | } |
f43e47db MA |
983 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), |
984 | &error_abort); | |
394c7d4d KW |
985 | } |
986 | ||
d095b465 HR |
987 | filename = qemu_opt_get(legacy_opts, "file"); |
988 | ||
ee13ed1c KW |
989 | /* Check werror/rerror compatibility with if=... */ |
990 | werror = qemu_opt_get(legacy_opts, "werror"); | |
991 | if (werror != NULL) { | |
992 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && | |
993 | type != IF_NONE) { | |
994 | error_report("werror is not supported by this bus type"); | |
995 | goto fail; | |
996 | } | |
46f5ac20 | 997 | qdict_put_str(bs_opts, "werror", werror); |
ee13ed1c KW |
998 | } |
999 | ||
1000 | rerror = qemu_opt_get(legacy_opts, "rerror"); | |
1001 | if (rerror != NULL) { | |
1002 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && | |
1003 | type != IF_NONE) { | |
1004 | error_report("rerror is not supported by this bus type"); | |
1005 | goto fail; | |
1006 | } | |
46f5ac20 | 1007 | qdict_put_str(bs_opts, "rerror", rerror); |
ee13ed1c KW |
1008 | } |
1009 | ||
2d246f01 | 1010 | /* Actual block device init: Functionality shared with blockdev-add */ |
18e46a03 | 1011 | blk = blockdev_init(filename, bs_opts, &local_err); |
3cb0e25c | 1012 | bs_opts = NULL; |
18e46a03 | 1013 | if (!blk) { |
84d18f06 | 1014 | if (local_err) { |
565f65d2 | 1015 | error_report_err(local_err); |
b681072d | 1016 | } |
2d246f01 | 1017 | goto fail; |
b681072d | 1018 | } else { |
84d18f06 | 1019 | assert(!local_err); |
2d246f01 KW |
1020 | } |
1021 | ||
26f8b3a8 MA |
1022 | /* Create legacy DriveInfo */ |
1023 | dinfo = g_malloc0(sizeof(*dinfo)); | |
f298d071 | 1024 | dinfo->opts = all_opts; |
2d246f01 | 1025 | |
ee13ed1c | 1026 | dinfo->type = type; |
87a899c5 KW |
1027 | dinfo->bus = bus_id; |
1028 | dinfo->unit = unit_id; | |
44e8b468 | 1029 | dinfo->serial = g_strdup(serial); |
bcf83158 | 1030 | |
26f8b3a8 MA |
1031 | blk_set_legacy_dinfo(blk, dinfo); |
1032 | ||
e34ef046 KW |
1033 | switch(type) { |
1034 | case IF_IDE: | |
1035 | case IF_SCSI: | |
1036 | case IF_XEN: | |
1037 | case IF_NONE: | |
1038 | dinfo->media_cd = media == MEDIA_CDROM; | |
1039 | break; | |
1040 | default: | |
1041 | break; | |
1042 | } | |
1043 | ||
2d246f01 | 1044 | fail: |
33cb7dc8 | 1045 | qemu_opts_del(legacy_opts); |
cb3e7f08 | 1046 | qobject_unref(bs_opts); |
2d246f01 | 1047 | return dinfo; |
57975222 KW |
1048 | } |
1049 | ||
b6c1bae5 KW |
1050 | static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) |
1051 | { | |
1052 | BlockDriverState *bs; | |
1053 | ||
1054 | bs = bdrv_lookup_bs(name, name, errp); | |
1055 | if (bs == NULL) { | |
1056 | return NULL; | |
1057 | } | |
1058 | ||
1059 | if (!bdrv_is_root_node(bs)) { | |
1060 | error_setg(errp, "Need a root block node"); | |
1061 | return NULL; | |
1062 | } | |
1063 | ||
1064 | if (!bdrv_is_inserted(bs)) { | |
1065 | error_setg(errp, "Device has no medium"); | |
1066 | return NULL; | |
1067 | } | |
1068 | ||
1069 | return bs; | |
1070 | } | |
1071 | ||
b33945cf KW |
1072 | static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id, |
1073 | Error **errp) | |
1074 | { | |
1075 | BlockBackend *blk; | |
1076 | ||
1077 | if (!blk_name == !qdev_id) { | |
1078 | error_setg(errp, "Need exactly one of 'device' and 'id'"); | |
1079 | return NULL; | |
1080 | } | |
1081 | ||
1082 | if (qdev_id) { | |
1083 | blk = blk_by_qdev_id(qdev_id, errp); | |
1084 | } else { | |
1085 | blk = blk_by_name(blk_name); | |
1086 | if (blk == NULL) { | |
1087 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
1088 | "Device '%s' not found", blk_name); | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | return blk; | |
1093 | } | |
1094 | ||
3e5a50d6 | 1095 | void hmp_commit(Monitor *mon, const QDict *qdict) |
666daa68 | 1096 | { |
666daa68 | 1097 | const char *device = qdict_get_str(qdict, "device"); |
a0e8544c | 1098 | BlockBackend *blk; |
e8877497 | 1099 | int ret; |
666daa68 | 1100 | |
6ab4b5ab | 1101 | if (!strcmp(device, "all")) { |
da31d594 | 1102 | ret = blk_commit_all(); |
6ab4b5ab | 1103 | } else { |
84aa0140 SH |
1104 | BlockDriverState *bs; |
1105 | AioContext *aio_context; | |
1106 | ||
a0e8544c FZ |
1107 | blk = blk_by_name(device); |
1108 | if (!blk) { | |
58513bde | 1109 | monitor_printf(mon, "Device '%s' not found\n", device); |
ac59eb95 | 1110 | return; |
6ab4b5ab | 1111 | } |
5433c24f HR |
1112 | if (!blk_is_available(blk)) { |
1113 | monitor_printf(mon, "Device '%s' has no medium\n", device); | |
1114 | return; | |
1115 | } | |
84aa0140 SH |
1116 | |
1117 | bs = blk_bs(blk); | |
1118 | aio_context = bdrv_get_aio_context(bs); | |
1119 | aio_context_acquire(aio_context); | |
1120 | ||
1121 | ret = bdrv_commit(bs); | |
1122 | ||
1123 | aio_context_release(aio_context); | |
58513bde JC |
1124 | } |
1125 | if (ret < 0) { | |
1126 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, | |
1127 | strerror(-ret)); | |
666daa68 MA |
1128 | } |
1129 | } | |
1130 | ||
10f75907 | 1131 | static void blockdev_do_action(TransactionAction *action, Error **errp) |
6cc2a415 | 1132 | { |
c8a83e85 | 1133 | TransactionActionList list; |
6cc2a415 | 1134 | |
10f75907 | 1135 | list.value = action; |
6cc2a415 | 1136 | list.next = NULL; |
94d16a64 | 1137 | qmp_transaction(&list, false, NULL, errp); |
6cc2a415 PB |
1138 | } |
1139 | ||
0901f67e BC |
1140 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
1141 | bool has_node_name, const char *node_name, | |
1142 | const char *snapshot_file, | |
1143 | bool has_snapshot_node_name, | |
1144 | const char *snapshot_node_name, | |
6106e249 | 1145 | bool has_format, const char *format, |
0901f67e | 1146 | bool has_mode, NewImageMode mode, Error **errp) |
f8882568 | 1147 | { |
a911e6ae | 1148 | BlockdevSnapshotSync snapshot = { |
0901f67e | 1149 | .has_device = has_device, |
6cc2a415 | 1150 | .device = (char *) device, |
0901f67e BC |
1151 | .has_node_name = has_node_name, |
1152 | .node_name = (char *) node_name, | |
6cc2a415 | 1153 | .snapshot_file = (char *) snapshot_file, |
0901f67e BC |
1154 | .has_snapshot_node_name = has_snapshot_node_name, |
1155 | .snapshot_node_name = (char *) snapshot_node_name, | |
6cc2a415 PB |
1156 | .has_format = has_format, |
1157 | .format = (char *) format, | |
1158 | .has_mode = has_mode, | |
1159 | .mode = mode, | |
1160 | }; | |
10f75907 EB |
1161 | TransactionAction action = { |
1162 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, | |
32bafa8f | 1163 | .u.blockdev_snapshot_sync.data = &snapshot, |
10f75907 EB |
1164 | }; |
1165 | blockdev_do_action(&action, errp); | |
f8882568 JS |
1166 | } |
1167 | ||
43de7e2d AG |
1168 | void qmp_blockdev_snapshot(const char *node, const char *overlay, |
1169 | Error **errp) | |
1170 | { | |
1171 | BlockdevSnapshot snapshot_data = { | |
1172 | .node = (char *) node, | |
1173 | .overlay = (char *) overlay | |
1174 | }; | |
10f75907 EB |
1175 | TransactionAction action = { |
1176 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, | |
32bafa8f | 1177 | .u.blockdev_snapshot.data = &snapshot_data, |
10f75907 EB |
1178 | }; |
1179 | blockdev_do_action(&action, errp); | |
43de7e2d AG |
1180 | } |
1181 | ||
f323bc9e WX |
1182 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
1183 | const char *name, | |
1184 | Error **errp) | |
1185 | { | |
1186 | BlockdevSnapshotInternal snapshot = { | |
1187 | .device = (char *) device, | |
1188 | .name = (char *) name | |
1189 | }; | |
10f75907 EB |
1190 | TransactionAction action = { |
1191 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
32bafa8f | 1192 | .u.blockdev_snapshot_internal_sync.data = &snapshot, |
10f75907 EB |
1193 | }; |
1194 | blockdev_do_action(&action, errp); | |
f323bc9e WX |
1195 | } |
1196 | ||
44e3e053 WX |
1197 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
1198 | bool has_id, | |
1199 | const char *id, | |
1200 | bool has_name, | |
1201 | const char *name, | |
1202 | Error **errp) | |
1203 | { | |
a0e8544c | 1204 | BlockDriverState *bs; |
4ef3982a | 1205 | AioContext *aio_context; |
44e3e053 WX |
1206 | QEMUSnapshotInfo sn; |
1207 | Error *local_err = NULL; | |
1208 | SnapshotInfo *info = NULL; | |
1209 | int ret; | |
1210 | ||
2dfb4c03 KW |
1211 | bs = qmp_get_root_bs(device, errp); |
1212 | if (!bs) { | |
44e3e053 WX |
1213 | return NULL; |
1214 | } | |
2dfb4c03 | 1215 | aio_context = bdrv_get_aio_context(bs); |
5433c24f | 1216 | aio_context_acquire(aio_context); |
44e3e053 WX |
1217 | |
1218 | if (!has_id) { | |
1219 | id = NULL; | |
1220 | } | |
1221 | ||
1222 | if (!has_name) { | |
1223 | name = NULL; | |
1224 | } | |
1225 | ||
1226 | if (!id && !name) { | |
1227 | error_setg(errp, "Name or id must be provided"); | |
5433c24f | 1228 | goto out_aio_context; |
44e3e053 WX |
1229 | } |
1230 | ||
0b928854 SH |
1231 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { |
1232 | goto out_aio_context; | |
1233 | } | |
1234 | ||
44e3e053 | 1235 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); |
84d18f06 | 1236 | if (local_err) { |
44e3e053 | 1237 | error_propagate(errp, local_err); |
4ef3982a | 1238 | goto out_aio_context; |
44e3e053 WX |
1239 | } |
1240 | if (!ret) { | |
1241 | error_setg(errp, | |
1242 | "Snapshot with id '%s' and name '%s' does not exist on " | |
1243 | "device '%s'", | |
1244 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
4ef3982a | 1245 | goto out_aio_context; |
44e3e053 WX |
1246 | } |
1247 | ||
1248 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
84d18f06 | 1249 | if (local_err) { |
44e3e053 | 1250 | error_propagate(errp, local_err); |
4ef3982a | 1251 | goto out_aio_context; |
44e3e053 WX |
1252 | } |
1253 | ||
4ef3982a SH |
1254 | aio_context_release(aio_context); |
1255 | ||
5839e53b | 1256 | info = g_new0(SnapshotInfo, 1); |
44e3e053 WX |
1257 | info->id = g_strdup(sn.id_str); |
1258 | info->name = g_strdup(sn.name); | |
1259 | info->date_nsec = sn.date_nsec; | |
1260 | info->date_sec = sn.date_sec; | |
1261 | info->vm_state_size = sn.vm_state_size; | |
1262 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
1263 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
1264 | ||
1265 | return info; | |
4ef3982a SH |
1266 | |
1267 | out_aio_context: | |
1268 | aio_context_release(aio_context); | |
1269 | return NULL; | |
44e3e053 | 1270 | } |
8802d1fd | 1271 | |
341ebc2f JS |
1272 | /** |
1273 | * block_dirty_bitmap_lookup: | |
1274 | * Return a dirty bitmap (if present), after validating | |
1275 | * the node reference and bitmap names. | |
1276 | * | |
1277 | * @node: The name of the BDS node to search for bitmaps | |
1278 | * @name: The name of the bitmap to search for | |
1279 | * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. | |
1280 | * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. | |
1281 | * @errp: Output pointer for error information. Can be NULL. | |
1282 | * | |
1283 | * @return: A bitmap object on success, or NULL on failure. | |
1284 | */ | |
1285 | static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, | |
1286 | const char *name, | |
1287 | BlockDriverState **pbs, | |
341ebc2f JS |
1288 | Error **errp) |
1289 | { | |
1290 | BlockDriverState *bs; | |
1291 | BdrvDirtyBitmap *bitmap; | |
341ebc2f JS |
1292 | |
1293 | if (!node) { | |
1294 | error_setg(errp, "Node cannot be NULL"); | |
1295 | return NULL; | |
1296 | } | |
1297 | if (!name) { | |
1298 | error_setg(errp, "Bitmap name cannot be NULL"); | |
1299 | return NULL; | |
1300 | } | |
1301 | bs = bdrv_lookup_bs(node, node, NULL); | |
1302 | if (!bs) { | |
1303 | error_setg(errp, "Node '%s' not found", node); | |
1304 | return NULL; | |
1305 | } | |
1306 | ||
341ebc2f JS |
1307 | bitmap = bdrv_find_dirty_bitmap(bs, name); |
1308 | if (!bitmap) { | |
1309 | error_setg(errp, "Dirty bitmap '%s' not found", name); | |
2119882c | 1310 | return NULL; |
341ebc2f JS |
1311 | } |
1312 | ||
1313 | if (pbs) { | |
1314 | *pbs = bs; | |
1315 | } | |
341ebc2f JS |
1316 | |
1317 | return bitmap; | |
341ebc2f JS |
1318 | } |
1319 | ||
b756b9ce | 1320 | /* New and old BlockDriverState structs for atomic group operations */ |
ba0c86a3 | 1321 | |
50f43f0f | 1322 | typedef struct BlkActionState BlkActionState; |
ba0c86a3 | 1323 | |
50f43f0f JS |
1324 | /** |
1325 | * BlkActionOps: | |
1326 | * Table of operations that define an Action. | |
1327 | * | |
1328 | * @instance_size: Size of state struct, in bytes. | |
1329 | * @prepare: Prepare the work, must NOT be NULL. | |
1330 | * @commit: Commit the changes, can be NULL. | |
1331 | * @abort: Abort the changes on fail, can be NULL. | |
1332 | * @clean: Clean up resources after all transaction actions have called | |
1333 | * commit() or abort(). Can be NULL. | |
1334 | * | |
1335 | * Only prepare() may fail. In a single transaction, only one of commit() or | |
1336 | * abort() will be called. clean() will always be called if it is present. | |
1337 | */ | |
1338 | typedef struct BlkActionOps { | |
ba0c86a3 | 1339 | size_t instance_size; |
50f43f0f JS |
1340 | void (*prepare)(BlkActionState *common, Error **errp); |
1341 | void (*commit)(BlkActionState *common); | |
1342 | void (*abort)(BlkActionState *common); | |
1343 | void (*clean)(BlkActionState *common); | |
1344 | } BlkActionOps; | |
ba0c86a3 | 1345 | |
50f43f0f JS |
1346 | /** |
1347 | * BlkActionState: | |
1348 | * Describes one Action's state within a Transaction. | |
1349 | * | |
1350 | * @action: QAPI-defined enum identifying which Action to perform. | |
1351 | * @ops: Table of ActionOps this Action can perform. | |
94d16a64 | 1352 | * @block_job_txn: Transaction which this action belongs to. |
50f43f0f JS |
1353 | * @entry: List membership for all Actions in this Transaction. |
1354 | * | |
1355 | * This structure must be arranged as first member in a subclassed type, | |
1356 | * assuming that the compiler will also arrange it to the same offsets as the | |
1357 | * base class. | |
ba0c86a3 | 1358 | */ |
50f43f0f | 1359 | struct BlkActionState { |
c8a83e85 | 1360 | TransactionAction *action; |
50f43f0f | 1361 | const BlkActionOps *ops; |
62c9e416 | 1362 | JobTxn *block_job_txn; |
94d16a64 | 1363 | TransactionProperties *txn_props; |
50f43f0f | 1364 | QSIMPLEQ_ENTRY(BlkActionState) entry; |
ba0c86a3 WX |
1365 | }; |
1366 | ||
bbe86010 WX |
1367 | /* internal snapshot private data */ |
1368 | typedef struct InternalSnapshotState { | |
50f43f0f | 1369 | BlkActionState common; |
bbe86010 WX |
1370 | BlockDriverState *bs; |
1371 | QEMUSnapshotInfo sn; | |
507306cc | 1372 | bool created; |
bbe86010 WX |
1373 | } InternalSnapshotState; |
1374 | ||
94d16a64 JS |
1375 | |
1376 | static int action_check_completion_mode(BlkActionState *s, Error **errp) | |
1377 | { | |
1378 | if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
1379 | error_setg(errp, | |
1380 | "Action '%s' does not support Transaction property " | |
1381 | "completion-mode = %s", | |
977c736f MA |
1382 | TransactionActionKind_str(s->action->type), |
1383 | ActionCompletionMode_str(s->txn_props->completion_mode)); | |
94d16a64 JS |
1384 | return -1; |
1385 | } | |
1386 | return 0; | |
1387 | } | |
1388 | ||
50f43f0f | 1389 | static void internal_snapshot_prepare(BlkActionState *common, |
bbe86010 WX |
1390 | Error **errp) |
1391 | { | |
f70edf99 | 1392 | Error *local_err = NULL; |
bbe86010 WX |
1393 | const char *device; |
1394 | const char *name; | |
1395 | BlockDriverState *bs; | |
1396 | QEMUSnapshotInfo old_sn, *sn; | |
1397 | bool ret; | |
1398 | qemu_timeval tv; | |
1399 | BlockdevSnapshotInternal *internal; | |
1400 | InternalSnapshotState *state; | |
a36e458c | 1401 | AioContext *aio_context; |
bbe86010 WX |
1402 | int ret1; |
1403 | ||
6a8f9661 | 1404 | g_assert(common->action->type == |
bbe86010 | 1405 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); |
32bafa8f | 1406 | internal = common->action->u.blockdev_snapshot_internal_sync.data; |
bbe86010 WX |
1407 | state = DO_UPCAST(InternalSnapshotState, common, common); |
1408 | ||
1409 | /* 1. parse input */ | |
1410 | device = internal->device; | |
1411 | name = internal->name; | |
1412 | ||
1413 | /* 2. check for validation */ | |
94d16a64 JS |
1414 | if (action_check_completion_mode(common, errp) < 0) { |
1415 | return; | |
1416 | } | |
1417 | ||
75dfd402 KW |
1418 | bs = qmp_get_root_bs(device, errp); |
1419 | if (!bs) { | |
bbe86010 WX |
1420 | return; |
1421 | } | |
1422 | ||
a36e458c SH |
1423 | aio_context = bdrv_get_aio_context(bs); |
1424 | aio_context_acquire(aio_context); | |
5d6e96ef | 1425 | |
507306cc | 1426 | state->bs = bs; |
a36e458c SH |
1427 | |
1428 | /* Paired with .clean() */ | |
507306cc FZ |
1429 | bdrv_drained_begin(bs); |
1430 | ||
3dc7ca3c | 1431 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { |
a36e458c | 1432 | goto out; |
3dc7ca3c SH |
1433 | } |
1434 | ||
bbe86010 | 1435 | if (bdrv_is_read_only(bs)) { |
81e5f78a | 1436 | error_setg(errp, "Device '%s' is read only", device); |
a36e458c | 1437 | goto out; |
bbe86010 WX |
1438 | } |
1439 | ||
1440 | if (!bdrv_can_snapshot(bs)) { | |
81e5f78a AG |
1441 | error_setg(errp, "Block format '%s' used by device '%s' " |
1442 | "does not support internal snapshots", | |
1443 | bs->drv->format_name, device); | |
a36e458c | 1444 | goto out; |
bbe86010 WX |
1445 | } |
1446 | ||
1447 | if (!strlen(name)) { | |
1448 | error_setg(errp, "Name is empty"); | |
a36e458c | 1449 | goto out; |
bbe86010 WX |
1450 | } |
1451 | ||
1452 | /* check whether a snapshot with name exist */ | |
f70edf99 MA |
1453 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
1454 | &local_err); | |
1455 | if (local_err) { | |
1456 | error_propagate(errp, local_err); | |
a36e458c | 1457 | goto out; |
bbe86010 WX |
1458 | } else if (ret) { |
1459 | error_setg(errp, | |
1460 | "Snapshot with name '%s' already exists on device '%s'", | |
1461 | name, device); | |
a36e458c | 1462 | goto out; |
bbe86010 WX |
1463 | } |
1464 | ||
1465 | /* 3. take the snapshot */ | |
1466 | sn = &state->sn; | |
1467 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1468 | qemu_gettimeofday(&tv); | |
1469 | sn->date_sec = tv.tv_sec; | |
1470 | sn->date_nsec = tv.tv_usec * 1000; | |
1471 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
1472 | ||
1473 | ret1 = bdrv_snapshot_create(bs, sn); | |
1474 | if (ret1 < 0) { | |
1475 | error_setg_errno(errp, -ret1, | |
1476 | "Failed to create snapshot '%s' on device '%s'", | |
1477 | name, device); | |
a36e458c | 1478 | goto out; |
bbe86010 WX |
1479 | } |
1480 | ||
1481 | /* 4. succeed, mark a snapshot is created */ | |
507306cc | 1482 | state->created = true; |
a36e458c SH |
1483 | |
1484 | out: | |
1485 | aio_context_release(aio_context); | |
bbe86010 WX |
1486 | } |
1487 | ||
50f43f0f | 1488 | static void internal_snapshot_abort(BlkActionState *common) |
bbe86010 WX |
1489 | { |
1490 | InternalSnapshotState *state = | |
1491 | DO_UPCAST(InternalSnapshotState, common, common); | |
1492 | BlockDriverState *bs = state->bs; | |
1493 | QEMUSnapshotInfo *sn = &state->sn; | |
a36e458c | 1494 | AioContext *aio_context; |
bbe86010 WX |
1495 | Error *local_error = NULL; |
1496 | ||
507306cc | 1497 | if (!state->created) { |
bbe86010 WX |
1498 | return; |
1499 | } | |
1500 | ||
a36e458c SH |
1501 | aio_context = bdrv_get_aio_context(state->bs); |
1502 | aio_context_acquire(aio_context); | |
1503 | ||
bbe86010 | 1504 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { |
c29b77f9 MA |
1505 | error_reportf_err(local_error, |
1506 | "Failed to delete snapshot with id '%s' and " | |
1507 | "name '%s' on device '%s' in abort: ", | |
1508 | sn->id_str, sn->name, | |
1509 | bdrv_get_device_name(bs)); | |
bbe86010 | 1510 | } |
a36e458c SH |
1511 | |
1512 | aio_context_release(aio_context); | |
bbe86010 WX |
1513 | } |
1514 | ||
50f43f0f | 1515 | static void internal_snapshot_clean(BlkActionState *common) |
5d6e96ef SH |
1516 | { |
1517 | InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, | |
1518 | common, common); | |
a36e458c | 1519 | AioContext *aio_context; |
5d6e96ef | 1520 | |
a36e458c SH |
1521 | if (!state->bs) { |
1522 | return; | |
5d6e96ef | 1523 | } |
a36e458c SH |
1524 | |
1525 | aio_context = bdrv_get_aio_context(state->bs); | |
1526 | aio_context_acquire(aio_context); | |
1527 | ||
1528 | bdrv_drained_end(state->bs); | |
1529 | ||
1530 | aio_context_release(aio_context); | |
5d6e96ef SH |
1531 | } |
1532 | ||
ba0c86a3 | 1533 | /* external snapshot private data */ |
ba5d6ab6 | 1534 | typedef struct ExternalSnapshotState { |
50f43f0f | 1535 | BlkActionState common; |
8802d1fd JC |
1536 | BlockDriverState *old_bs; |
1537 | BlockDriverState *new_bs; | |
067acf28 | 1538 | bool overlay_appended; |
ba5d6ab6 | 1539 | } ExternalSnapshotState; |
8802d1fd | 1540 | |
50f43f0f | 1541 | static void external_snapshot_prepare(BlkActionState *common, |
9b9877ee WX |
1542 | Error **errp) |
1543 | { | |
5b363937 | 1544 | int flags = 0; |
43de7e2d | 1545 | QDict *options = NULL; |
9b9877ee | 1546 | Error *local_err = NULL; |
43de7e2d | 1547 | /* Device and node name of the image to generate the snapshot from */ |
e2a31e87 | 1548 | const char *device; |
0901f67e | 1549 | const char *node_name; |
43de7e2d AG |
1550 | /* Reference to the new image (for 'blockdev-snapshot') */ |
1551 | const char *snapshot_ref; | |
1552 | /* File name of the new image (for 'blockdev-snapshot-sync') */ | |
e2a31e87 | 1553 | const char *new_image_file; |
ba5d6ab6 SH |
1554 | ExternalSnapshotState *state = |
1555 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1556 | TransactionAction *action = common->action; |
2d24b60b | 1557 | AioContext *aio_context; |
9b9877ee | 1558 | |
43de7e2d AG |
1559 | /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar |
1560 | * purpose but a different set of parameters */ | |
1561 | switch (action->type) { | |
1562 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: | |
1563 | { | |
32bafa8f | 1564 | BlockdevSnapshot *s = action->u.blockdev_snapshot.data; |
43de7e2d AG |
1565 | device = s->node; |
1566 | node_name = s->node; | |
1567 | new_image_file = NULL; | |
1568 | snapshot_ref = s->overlay; | |
1569 | } | |
1570 | break; | |
1571 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: | |
1572 | { | |
32bafa8f | 1573 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
43de7e2d AG |
1574 | device = s->has_device ? s->device : NULL; |
1575 | node_name = s->has_node_name ? s->node_name : NULL; | |
1576 | new_image_file = s->snapshot_file; | |
1577 | snapshot_ref = NULL; | |
1578 | } | |
1579 | break; | |
1580 | default: | |
1581 | g_assert_not_reached(); | |
e2a31e87 WX |
1582 | } |
1583 | ||
1584 | /* start processing */ | |
94d16a64 JS |
1585 | if (action_check_completion_mode(common, errp) < 0) { |
1586 | return; | |
1587 | } | |
1588 | ||
43de7e2d AG |
1589 | state->old_bs = bdrv_lookup_bs(device, node_name, errp); |
1590 | if (!state->old_bs) { | |
9b9877ee WX |
1591 | return; |
1592 | } | |
1593 | ||
2d24b60b SH |
1594 | aio_context = bdrv_get_aio_context(state->old_bs); |
1595 | aio_context_acquire(aio_context); | |
1596 | ||
1597 | /* Paired with .clean() */ | |
da763e83 | 1598 | bdrv_drained_begin(state->old_bs); |
5d6e96ef | 1599 | |
ba5d6ab6 | 1600 | if (!bdrv_is_inserted(state->old_bs)) { |
c6bd8c70 | 1601 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
2d24b60b | 1602 | goto out; |
9b9877ee WX |
1603 | } |
1604 | ||
3718d8ab FZ |
1605 | if (bdrv_op_is_blocked(state->old_bs, |
1606 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
2d24b60b | 1607 | goto out; |
9b9877ee WX |
1608 | } |
1609 | ||
ba5d6ab6 SH |
1610 | if (!bdrv_is_read_only(state->old_bs)) { |
1611 | if (bdrv_flush(state->old_bs)) { | |
c6bd8c70 | 1612 | error_setg(errp, QERR_IO_ERROR); |
2d24b60b | 1613 | goto out; |
9b9877ee WX |
1614 | } |
1615 | } | |
1616 | ||
212a5a8f | 1617 | if (!bdrv_is_first_non_filter(state->old_bs)) { |
c6bd8c70 | 1618 | error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); |
2d24b60b | 1619 | goto out; |
f6186f49 BC |
1620 | } |
1621 | ||
43de7e2d | 1622 | if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { |
32bafa8f | 1623 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
43de7e2d AG |
1624 | const char *format = s->has_format ? s->format : "qcow2"; |
1625 | enum NewImageMode mode; | |
1626 | const char *snapshot_node_name = | |
1627 | s->has_snapshot_node_name ? s->snapshot_node_name : NULL; | |
9b9877ee | 1628 | |
43de7e2d AG |
1629 | if (node_name && !snapshot_node_name) { |
1630 | error_setg(errp, "New snapshot node name missing"); | |
2d24b60b | 1631 | goto out; |
9b9877ee | 1632 | } |
9b9877ee | 1633 | |
43de7e2d AG |
1634 | if (snapshot_node_name && |
1635 | bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { | |
1636 | error_setg(errp, "New snapshot node name already in use"); | |
2d24b60b | 1637 | goto out; |
43de7e2d AG |
1638 | } |
1639 | ||
1640 | flags = state->old_bs->open_flags; | |
2a32c6e8 JS |
1641 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ); |
1642 | flags |= BDRV_O_NO_BACKING; | |
43de7e2d AG |
1643 | |
1644 | /* create new image w/backing file */ | |
1645 | mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1646 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
f86b8b58 KW |
1647 | int64_t size = bdrv_getlength(state->old_bs); |
1648 | if (size < 0) { | |
1649 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
2d24b60b | 1650 | goto out; |
f86b8b58 | 1651 | } |
43de7e2d AG |
1652 | bdrv_img_create(new_image_file, format, |
1653 | state->old_bs->filename, | |
1654 | state->old_bs->drv->format_name, | |
9217283d | 1655 | NULL, size, flags, false, &local_err); |
43de7e2d AG |
1656 | if (local_err) { |
1657 | error_propagate(errp, local_err); | |
2d24b60b | 1658 | goto out; |
43de7e2d AG |
1659 | } |
1660 | } | |
1661 | ||
1662 | options = qdict_new(); | |
1663 | if (s->has_snapshot_node_name) { | |
46f5ac20 | 1664 | qdict_put_str(options, "node-name", snapshot_node_name); |
43de7e2d | 1665 | } |
46f5ac20 | 1666 | qdict_put_str(options, "driver", format); |
0901f67e BC |
1667 | } |
1668 | ||
5b363937 HR |
1669 | state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, |
1670 | errp); | |
f67503e5 | 1671 | /* We will manually add the backing_hd field to the bs later */ |
5b363937 | 1672 | if (!state->new_bs) { |
2d24b60b | 1673 | goto out; |
43de7e2d AG |
1674 | } |
1675 | ||
1f0c461b | 1676 | if (bdrv_has_blk(state->new_bs)) { |
e467da7b | 1677 | error_setg(errp, "The snapshot is already in use"); |
2d24b60b | 1678 | goto out; |
43de7e2d AG |
1679 | } |
1680 | ||
1681 | if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, | |
1682 | errp)) { | |
2d24b60b | 1683 | goto out; |
43de7e2d AG |
1684 | } |
1685 | ||
1686 | if (state->new_bs->backing != NULL) { | |
1687 | error_setg(errp, "The snapshot already has a backing image"); | |
2d24b60b | 1688 | goto out; |
08b24cfe AG |
1689 | } |
1690 | ||
1691 | if (!state->new_bs->drv->supports_backing) { | |
1692 | error_setg(errp, "The snapshot does not support backing images"); | |
2d24b60b | 1693 | goto out; |
b2c2832c KW |
1694 | } |
1695 | ||
2d24b60b | 1696 | bdrv_set_aio_context(state->new_bs, aio_context); |
c26a5ab7 | 1697 | |
b2c2832c KW |
1698 | /* This removes our old bs and adds the new bs. This is an operation that |
1699 | * can fail, so we need to do it in .prepare; undoing it for abort is | |
1700 | * always possible. */ | |
1701 | bdrv_ref(state->new_bs); | |
1702 | bdrv_append(state->new_bs, state->old_bs, &local_err); | |
1703 | if (local_err) { | |
1704 | error_propagate(errp, local_err); | |
2d24b60b | 1705 | goto out; |
9b9877ee | 1706 | } |
067acf28 | 1707 | state->overlay_appended = true; |
2d24b60b SH |
1708 | |
1709 | out: | |
1710 | aio_context_release(aio_context); | |
9b9877ee WX |
1711 | } |
1712 | ||
50f43f0f | 1713 | static void external_snapshot_commit(BlkActionState *common) |
3b0047e8 | 1714 | { |
ba5d6ab6 SH |
1715 | ExternalSnapshotState *state = |
1716 | DO_UPCAST(ExternalSnapshotState, common, common); | |
2d24b60b SH |
1717 | AioContext *aio_context; |
1718 | ||
1719 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1720 | aio_context_acquire(aio_context); | |
ba0c86a3 | 1721 | |
3b0047e8 WX |
1722 | /* We don't need (or want) to use the transactional |
1723 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1724 | * don't want to abort all of them if one of them fails the reopen */ | |
d3faa13e | 1725 | if (!atomic_read(&state->old_bs->copy_on_read)) { |
4c844983 KW |
1726 | bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, |
1727 | NULL); | |
1728 | } | |
2d24b60b SH |
1729 | |
1730 | aio_context_release(aio_context); | |
3b0047e8 WX |
1731 | } |
1732 | ||
50f43f0f | 1733 | static void external_snapshot_abort(BlkActionState *common) |
96b86bf7 | 1734 | { |
ba5d6ab6 SH |
1735 | ExternalSnapshotState *state = |
1736 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1737 | if (state->new_bs) { | |
067acf28 | 1738 | if (state->overlay_appended) { |
2d24b60b SH |
1739 | AioContext *aio_context; |
1740 | ||
1741 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1742 | aio_context_acquire(aio_context); | |
1743 | ||
719fc28c JC |
1744 | bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() |
1745 | close state->old_bs; we need it */ | |
1746 | bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); | |
5fe31c25 | 1747 | bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); |
719fc28c | 1748 | bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ |
2d24b60b SH |
1749 | |
1750 | aio_context_release(aio_context); | |
b2c2832c | 1751 | } |
96b86bf7 | 1752 | } |
da763e83 FZ |
1753 | } |
1754 | ||
50f43f0f | 1755 | static void external_snapshot_clean(BlkActionState *common) |
da763e83 FZ |
1756 | { |
1757 | ExternalSnapshotState *state = | |
1758 | DO_UPCAST(ExternalSnapshotState, common, common); | |
2d24b60b SH |
1759 | AioContext *aio_context; |
1760 | ||
1761 | if (!state->old_bs) { | |
1762 | return; | |
5d6e96ef | 1763 | } |
2d24b60b SH |
1764 | |
1765 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1766 | aio_context_acquire(aio_context); | |
1767 | ||
1768 | bdrv_drained_end(state->old_bs); | |
1769 | bdrv_unref(state->new_bs); | |
1770 | ||
1771 | aio_context_release(aio_context); | |
96b86bf7 WX |
1772 | } |
1773 | ||
3037f364 | 1774 | typedef struct DriveBackupState { |
50f43f0f | 1775 | BlkActionState common; |
3037f364 SH |
1776 | BlockDriverState *bs; |
1777 | BlockJob *job; | |
1778 | } DriveBackupState; | |
1779 | ||
62c9e416 | 1780 | static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, |
81206a89 | 1781 | Error **errp); |
78f51fde | 1782 | |
50f43f0f | 1783 | static void drive_backup_prepare(BlkActionState *common, Error **errp) |
3037f364 SH |
1784 | { |
1785 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
b7e4fa22 | 1786 | BlockDriverState *bs; |
3037f364 | 1787 | DriveBackup *backup; |
66d56054 | 1788 | AioContext *aio_context; |
3037f364 SH |
1789 | Error *local_err = NULL; |
1790 | ||
6a8f9661 | 1791 | assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); |
32bafa8f | 1792 | backup = common->action->u.drive_backup.data; |
3037f364 | 1793 | |
b7e4fa22 KW |
1794 | bs = qmp_get_root_bs(backup->device, errp); |
1795 | if (!bs) { | |
1fdd4b7b FZ |
1796 | return; |
1797 | } | |
1798 | ||
66d56054 SH |
1799 | aio_context = bdrv_get_aio_context(bs); |
1800 | aio_context_acquire(aio_context); | |
1801 | ||
1802 | /* Paired with .clean() */ | |
b7e4fa22 | 1803 | bdrv_drained_begin(bs); |
66d56054 | 1804 | |
b7e4fa22 | 1805 | state->bs = bs; |
5d6e96ef | 1806 | |
111049a4 | 1807 | state->job = do_drive_backup(backup, common->block_job_txn, &local_err); |
84d18f06 | 1808 | if (local_err) { |
3037f364 | 1809 | error_propagate(errp, local_err); |
66d56054 | 1810 | goto out; |
3037f364 | 1811 | } |
66d56054 SH |
1812 | |
1813 | out: | |
1814 | aio_context_release(aio_context); | |
111049a4 | 1815 | } |
3037f364 | 1816 | |
111049a4 JS |
1817 | static void drive_backup_commit(BlkActionState *common) |
1818 | { | |
1819 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
66d56054 SH |
1820 | AioContext *aio_context; |
1821 | ||
1822 | aio_context = bdrv_get_aio_context(state->bs); | |
1823 | aio_context_acquire(aio_context); | |
1824 | ||
111049a4 | 1825 | assert(state->job); |
da01ff7f | 1826 | job_start(&state->job->job); |
66d56054 SH |
1827 | |
1828 | aio_context_release(aio_context); | |
3037f364 SH |
1829 | } |
1830 | ||
50f43f0f | 1831 | static void drive_backup_abort(BlkActionState *common) |
3037f364 SH |
1832 | { |
1833 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
3037f364 | 1834 | |
111049a4 | 1835 | if (state->job) { |
66d56054 SH |
1836 | AioContext *aio_context; |
1837 | ||
1838 | aio_context = bdrv_get_aio_context(state->bs); | |
1839 | aio_context_acquire(aio_context); | |
1840 | ||
3d70ff53 | 1841 | job_cancel_sync(&state->job->job); |
66d56054 SH |
1842 | |
1843 | aio_context_release(aio_context); | |
3037f364 SH |
1844 | } |
1845 | } | |
1846 | ||
50f43f0f | 1847 | static void drive_backup_clean(BlkActionState *common) |
5d6e96ef SH |
1848 | { |
1849 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
66d56054 | 1850 | AioContext *aio_context; |
5d6e96ef | 1851 | |
66d56054 SH |
1852 | if (!state->bs) { |
1853 | return; | |
5d6e96ef | 1854 | } |
66d56054 SH |
1855 | |
1856 | aio_context = bdrv_get_aio_context(state->bs); | |
1857 | aio_context_acquire(aio_context); | |
1858 | ||
1859 | bdrv_drained_end(state->bs); | |
1860 | ||
1861 | aio_context_release(aio_context); | |
5d6e96ef SH |
1862 | } |
1863 | ||
bd8baecd | 1864 | typedef struct BlockdevBackupState { |
50f43f0f | 1865 | BlkActionState common; |
bd8baecd FZ |
1866 | BlockDriverState *bs; |
1867 | BlockJob *job; | |
bd8baecd FZ |
1868 | } BlockdevBackupState; |
1869 | ||
62c9e416 | 1870 | static BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, |
111049a4 | 1871 | Error **errp); |
78f51fde | 1872 | |
50f43f0f | 1873 | static void blockdev_backup_prepare(BlkActionState *common, Error **errp) |
bd8baecd FZ |
1874 | { |
1875 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1876 | BlockdevBackup *backup; | |
cef34eeb | 1877 | BlockDriverState *bs, *target; |
edd5adee | 1878 | AioContext *aio_context; |
bd8baecd FZ |
1879 | Error *local_err = NULL; |
1880 | ||
6a8f9661 | 1881 | assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); |
32bafa8f | 1882 | backup = common->action->u.blockdev_backup.data; |
bd8baecd | 1883 | |
930fe17f | 1884 | bs = bdrv_lookup_bs(backup->device, backup->device, errp); |
cef34eeb | 1885 | if (!bs) { |
ff52bf36 FZ |
1886 | return; |
1887 | } | |
1888 | ||
39d990ac | 1889 | target = bdrv_lookup_bs(backup->target, backup->target, errp); |
5433c24f | 1890 | if (!target) { |
bd8baecd FZ |
1891 | return; |
1892 | } | |
1893 | ||
edd5adee SH |
1894 | aio_context = bdrv_get_aio_context(bs); |
1895 | if (aio_context != bdrv_get_aio_context(target)) { | |
bd8baecd FZ |
1896 | error_setg(errp, "Backup between two IO threads is not implemented"); |
1897 | return; | |
1898 | } | |
edd5adee | 1899 | aio_context_acquire(aio_context); |
cef34eeb | 1900 | state->bs = bs; |
edd5adee SH |
1901 | |
1902 | /* Paired with .clean() */ | |
ff52bf36 | 1903 | bdrv_drained_begin(state->bs); |
bd8baecd | 1904 | |
111049a4 | 1905 | state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err); |
bd8baecd FZ |
1906 | if (local_err) { |
1907 | error_propagate(errp, local_err); | |
edd5adee | 1908 | goto out; |
bd8baecd | 1909 | } |
edd5adee SH |
1910 | |
1911 | out: | |
1912 | aio_context_release(aio_context); | |
111049a4 | 1913 | } |
bd8baecd | 1914 | |
111049a4 JS |
1915 | static void blockdev_backup_commit(BlkActionState *common) |
1916 | { | |
1917 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
edd5adee SH |
1918 | AioContext *aio_context; |
1919 | ||
1920 | aio_context = bdrv_get_aio_context(state->bs); | |
1921 | aio_context_acquire(aio_context); | |
1922 | ||
111049a4 | 1923 | assert(state->job); |
da01ff7f | 1924 | job_start(&state->job->job); |
edd5adee SH |
1925 | |
1926 | aio_context_release(aio_context); | |
bd8baecd FZ |
1927 | } |
1928 | ||
50f43f0f | 1929 | static void blockdev_backup_abort(BlkActionState *common) |
bd8baecd FZ |
1930 | { |
1931 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
bd8baecd | 1932 | |
111049a4 | 1933 | if (state->job) { |
edd5adee SH |
1934 | AioContext *aio_context; |
1935 | ||
1936 | aio_context = bdrv_get_aio_context(state->bs); | |
1937 | aio_context_acquire(aio_context); | |
1938 | ||
3d70ff53 | 1939 | job_cancel_sync(&state->job->job); |
edd5adee SH |
1940 | |
1941 | aio_context_release(aio_context); | |
bd8baecd FZ |
1942 | } |
1943 | } | |
1944 | ||
50f43f0f | 1945 | static void blockdev_backup_clean(BlkActionState *common) |
bd8baecd FZ |
1946 | { |
1947 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
edd5adee | 1948 | AioContext *aio_context; |
bd8baecd | 1949 | |
edd5adee SH |
1950 | if (!state->bs) { |
1951 | return; | |
bd8baecd | 1952 | } |
edd5adee SH |
1953 | |
1954 | aio_context = bdrv_get_aio_context(state->bs); | |
1955 | aio_context_acquire(aio_context); | |
1956 | ||
1957 | bdrv_drained_end(state->bs); | |
1958 | ||
1959 | aio_context_release(aio_context); | |
bd8baecd FZ |
1960 | } |
1961 | ||
df9a681d | 1962 | typedef struct BlockDirtyBitmapState { |
50f43f0f | 1963 | BlkActionState common; |
df9a681d FZ |
1964 | BdrvDirtyBitmap *bitmap; |
1965 | BlockDriverState *bs; | |
df9a681d FZ |
1966 | HBitmap *backup; |
1967 | bool prepared; | |
c6490447 | 1968 | bool was_enabled; |
df9a681d FZ |
1969 | } BlockDirtyBitmapState; |
1970 | ||
50f43f0f | 1971 | static void block_dirty_bitmap_add_prepare(BlkActionState *common, |
df9a681d FZ |
1972 | Error **errp) |
1973 | { | |
1974 | Error *local_err = NULL; | |
1975 | BlockDirtyBitmapAdd *action; | |
1976 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
1977 | common, common); | |
1978 | ||
94d16a64 JS |
1979 | if (action_check_completion_mode(common, errp) < 0) { |
1980 | return; | |
1981 | } | |
1982 | ||
32bafa8f | 1983 | action = common->action->u.block_dirty_bitmap_add.data; |
df9a681d FZ |
1984 | /* AIO context taken and released within qmp_block_dirty_bitmap_add */ |
1985 | qmp_block_dirty_bitmap_add(action->node, action->name, | |
1986 | action->has_granularity, action->granularity, | |
fd5ae4cc | 1987 | action->has_persistent, action->persistent, |
eb738bb5 | 1988 | action->has_autoload, action->autoload, |
a6e2ca5f | 1989 | action->has_x_disabled, action->x_disabled, |
df9a681d FZ |
1990 | &local_err); |
1991 | ||
1992 | if (!local_err) { | |
1993 | state->prepared = true; | |
1994 | } else { | |
1995 | error_propagate(errp, local_err); | |
1996 | } | |
1997 | } | |
1998 | ||
50f43f0f | 1999 | static void block_dirty_bitmap_add_abort(BlkActionState *common) |
df9a681d FZ |
2000 | { |
2001 | BlockDirtyBitmapAdd *action; | |
2002 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2003 | common, common); | |
2004 | ||
32bafa8f | 2005 | action = common->action->u.block_dirty_bitmap_add.data; |
df9a681d FZ |
2006 | /* Should not be able to fail: IF the bitmap was added via .prepare(), |
2007 | * then the node reference and bitmap name must have been valid. | |
2008 | */ | |
2009 | if (state->prepared) { | |
2010 | qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); | |
2011 | } | |
2012 | } | |
2013 | ||
50f43f0f | 2014 | static void block_dirty_bitmap_clear_prepare(BlkActionState *common, |
df9a681d FZ |
2015 | Error **errp) |
2016 | { | |
2017 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2018 | common, common); | |
2019 | BlockDirtyBitmap *action; | |
2020 | ||
94d16a64 JS |
2021 | if (action_check_completion_mode(common, errp) < 0) { |
2022 | return; | |
2023 | } | |
2024 | ||
32bafa8f | 2025 | action = common->action->u.block_dirty_bitmap_clear.data; |
df9a681d FZ |
2026 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
2027 | action->name, | |
2028 | &state->bs, | |
df9a681d FZ |
2029 | errp); |
2030 | if (!state->bitmap) { | |
2031 | return; | |
2032 | } | |
2033 | ||
2034 | if (bdrv_dirty_bitmap_frozen(state->bitmap)) { | |
2035 | error_setg(errp, "Cannot modify a frozen bitmap"); | |
2036 | return; | |
4f43e953 VSO |
2037 | } else if (bdrv_dirty_bitmap_qmp_locked(state->bitmap)) { |
2038 | error_setg(errp, "Cannot modify a locked bitmap"); | |
2039 | return; | |
df9a681d FZ |
2040 | } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { |
2041 | error_setg(errp, "Cannot clear a disabled bitmap"); | |
2042 | return; | |
d6883bc9 VSO |
2043 | } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) { |
2044 | error_setg(errp, "Cannot clear a readonly bitmap"); | |
2045 | return; | |
df9a681d FZ |
2046 | } |
2047 | ||
2048 | bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); | |
df9a681d FZ |
2049 | } |
2050 | ||
50f43f0f | 2051 | static void block_dirty_bitmap_clear_abort(BlkActionState *common) |
df9a681d FZ |
2052 | { |
2053 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2054 | common, common); | |
2055 | ||
184dd9c4 JS |
2056 | if (state->backup) { |
2057 | bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); | |
2058 | } | |
df9a681d FZ |
2059 | } |
2060 | ||
50f43f0f | 2061 | static void block_dirty_bitmap_clear_commit(BlkActionState *common) |
df9a681d FZ |
2062 | { |
2063 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2064 | common, common); | |
2065 | ||
2066 | hbitmap_free(state->backup); | |
2067 | } | |
2068 | ||
c6490447 VSO |
2069 | static void block_dirty_bitmap_enable_prepare(BlkActionState *common, |
2070 | Error **errp) | |
2071 | { | |
2072 | BlockDirtyBitmap *action; | |
2073 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2074 | common, common); | |
2075 | ||
2076 | if (action_check_completion_mode(common, errp) < 0) { | |
2077 | return; | |
2078 | } | |
2079 | ||
2080 | action = common->action->u.x_block_dirty_bitmap_enable.data; | |
2081 | state->bitmap = block_dirty_bitmap_lookup(action->node, | |
2082 | action->name, | |
2083 | NULL, | |
2084 | errp); | |
2085 | if (!state->bitmap) { | |
2086 | return; | |
2087 | } | |
2088 | ||
2089 | state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); | |
2090 | bdrv_enable_dirty_bitmap(state->bitmap); | |
2091 | } | |
2092 | ||
2093 | static void block_dirty_bitmap_enable_abort(BlkActionState *common) | |
2094 | { | |
2095 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2096 | common, common); | |
2097 | ||
2098 | if (!state->was_enabled) { | |
2099 | bdrv_disable_dirty_bitmap(state->bitmap); | |
2100 | } | |
2101 | } | |
2102 | ||
2103 | static void block_dirty_bitmap_disable_prepare(BlkActionState *common, | |
2104 | Error **errp) | |
2105 | { | |
2106 | BlockDirtyBitmap *action; | |
2107 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2108 | common, common); | |
2109 | ||
2110 | if (action_check_completion_mode(common, errp) < 0) { | |
2111 | return; | |
2112 | } | |
2113 | ||
2114 | action = common->action->u.x_block_dirty_bitmap_disable.data; | |
2115 | state->bitmap = block_dirty_bitmap_lookup(action->node, | |
2116 | action->name, | |
2117 | NULL, | |
2118 | errp); | |
2119 | if (!state->bitmap) { | |
2120 | return; | |
2121 | } | |
2122 | ||
2123 | state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); | |
2124 | bdrv_disable_dirty_bitmap(state->bitmap); | |
2125 | } | |
2126 | ||
2127 | static void block_dirty_bitmap_disable_abort(BlkActionState *common) | |
2128 | { | |
2129 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2130 | common, common); | |
2131 | ||
2132 | if (state->was_enabled) { | |
2133 | bdrv_enable_dirty_bitmap(state->bitmap); | |
2134 | } | |
2135 | } | |
2136 | ||
50f43f0f | 2137 | static void abort_prepare(BlkActionState *common, Error **errp) |
78b18b78 SH |
2138 | { |
2139 | error_setg(errp, "Transaction aborted using Abort action"); | |
2140 | } | |
2141 | ||
50f43f0f | 2142 | static void abort_commit(BlkActionState *common) |
78b18b78 | 2143 | { |
dfc6f865 | 2144 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
2145 | } |
2146 | ||
50f43f0f | 2147 | static const BlkActionOps actions[] = { |
43de7e2d AG |
2148 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { |
2149 | .instance_size = sizeof(ExternalSnapshotState), | |
2150 | .prepare = external_snapshot_prepare, | |
2151 | .commit = external_snapshot_commit, | |
2152 | .abort = external_snapshot_abort, | |
4ad6f3db | 2153 | .clean = external_snapshot_clean, |
43de7e2d | 2154 | }, |
c8a83e85 | 2155 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 2156 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
2157 | .prepare = external_snapshot_prepare, |
2158 | .commit = external_snapshot_commit, | |
2159 | .abort = external_snapshot_abort, | |
da763e83 | 2160 | .clean = external_snapshot_clean, |
ba0c86a3 | 2161 | }, |
3037f364 SH |
2162 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
2163 | .instance_size = sizeof(DriveBackupState), | |
2164 | .prepare = drive_backup_prepare, | |
111049a4 | 2165 | .commit = drive_backup_commit, |
3037f364 | 2166 | .abort = drive_backup_abort, |
5d6e96ef | 2167 | .clean = drive_backup_clean, |
3037f364 | 2168 | }, |
bd8baecd FZ |
2169 | [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { |
2170 | .instance_size = sizeof(BlockdevBackupState), | |
2171 | .prepare = blockdev_backup_prepare, | |
111049a4 | 2172 | .commit = blockdev_backup_commit, |
bd8baecd FZ |
2173 | .abort = blockdev_backup_abort, |
2174 | .clean = blockdev_backup_clean, | |
2175 | }, | |
78b18b78 | 2176 | [TRANSACTION_ACTION_KIND_ABORT] = { |
50f43f0f | 2177 | .instance_size = sizeof(BlkActionState), |
78b18b78 SH |
2178 | .prepare = abort_prepare, |
2179 | .commit = abort_commit, | |
2180 | }, | |
bbe86010 WX |
2181 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
2182 | .instance_size = sizeof(InternalSnapshotState), | |
2183 | .prepare = internal_snapshot_prepare, | |
2184 | .abort = internal_snapshot_abort, | |
5d6e96ef | 2185 | .clean = internal_snapshot_clean, |
bbe86010 | 2186 | }, |
df9a681d FZ |
2187 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { |
2188 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2189 | .prepare = block_dirty_bitmap_add_prepare, | |
2190 | .abort = block_dirty_bitmap_add_abort, | |
2191 | }, | |
2192 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { | |
2193 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2194 | .prepare = block_dirty_bitmap_clear_prepare, | |
2195 | .commit = block_dirty_bitmap_clear_commit, | |
2196 | .abort = block_dirty_bitmap_clear_abort, | |
c6490447 VSO |
2197 | }, |
2198 | [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_ENABLE] = { | |
2199 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2200 | .prepare = block_dirty_bitmap_enable_prepare, | |
2201 | .abort = block_dirty_bitmap_enable_abort, | |
2202 | }, | |
2203 | [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_DISABLE] = { | |
2204 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2205 | .prepare = block_dirty_bitmap_disable_prepare, | |
2206 | .abort = block_dirty_bitmap_disable_abort, | |
2207 | } | |
ba0c86a3 WX |
2208 | }; |
2209 | ||
94d16a64 JS |
2210 | /** |
2211 | * Allocate a TransactionProperties structure if necessary, and fill | |
2212 | * that structure with desired defaults if they are unset. | |
2213 | */ | |
2214 | static TransactionProperties *get_transaction_properties( | |
2215 | TransactionProperties *props) | |
2216 | { | |
2217 | if (!props) { | |
2218 | props = g_new0(TransactionProperties, 1); | |
2219 | } | |
2220 | ||
2221 | if (!props->has_completion_mode) { | |
2222 | props->has_completion_mode = true; | |
2223 | props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; | |
2224 | } | |
2225 | ||
2226 | return props; | |
2227 | } | |
2228 | ||
8802d1fd | 2229 | /* |
b756b9ce SH |
2230 | * 'Atomic' group operations. The operations are performed as a set, and if |
2231 | * any fail then we roll back all operations in the group. | |
8802d1fd | 2232 | */ |
94d16a64 JS |
2233 | void qmp_transaction(TransactionActionList *dev_list, |
2234 | bool has_props, | |
2235 | struct TransactionProperties *props, | |
2236 | Error **errp) | |
8802d1fd | 2237 | { |
c8a83e85 | 2238 | TransactionActionList *dev_entry = dev_list; |
62c9e416 | 2239 | JobTxn *block_job_txn = NULL; |
50f43f0f | 2240 | BlkActionState *state, *next; |
43e17041 | 2241 | Error *local_err = NULL; |
8802d1fd | 2242 | |
50f43f0f | 2243 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; |
8802d1fd JC |
2244 | QSIMPLEQ_INIT(&snap_bdrv_states); |
2245 | ||
94d16a64 | 2246 | /* Does this transaction get canceled as a group on failure? |
62c9e416 | 2247 | * If not, we don't really need to make a JobTxn. |
94d16a64 JS |
2248 | */ |
2249 | props = get_transaction_properties(props); | |
2250 | if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
7eaa8fb5 | 2251 | block_job_txn = job_txn_new(); |
94d16a64 JS |
2252 | } |
2253 | ||
b756b9ce | 2254 | /* drain all i/o before any operations */ |
8802d1fd JC |
2255 | bdrv_drain_all(); |
2256 | ||
b756b9ce | 2257 | /* We don't do anything in this loop that commits us to the operations */ |
8802d1fd | 2258 | while (NULL != dev_entry) { |
c8a83e85 | 2259 | TransactionAction *dev_info = NULL; |
50f43f0f | 2260 | const BlkActionOps *ops; |
52e7c241 | 2261 | |
8802d1fd JC |
2262 | dev_info = dev_entry->value; |
2263 | dev_entry = dev_entry->next; | |
2264 | ||
6a8f9661 | 2265 | assert(dev_info->type < ARRAY_SIZE(actions)); |
ba0c86a3 | 2266 | |
6a8f9661 | 2267 | ops = &actions[dev_info->type]; |
aa3fe714 HR |
2268 | assert(ops->instance_size > 0); |
2269 | ||
ba5d6ab6 SH |
2270 | state = g_malloc0(ops->instance_size); |
2271 | state->ops = ops; | |
2272 | state->action = dev_info; | |
94d16a64 JS |
2273 | state->block_job_txn = block_job_txn; |
2274 | state->txn_props = props; | |
ba5d6ab6 | 2275 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); |
8802d1fd | 2276 | |
ba5d6ab6 | 2277 | state->ops->prepare(state, &local_err); |
84d18f06 | 2278 | if (local_err) { |
ba0c86a3 WX |
2279 | error_propagate(errp, local_err); |
2280 | goto delete_and_fail; | |
8802d1fd | 2281 | } |
8802d1fd JC |
2282 | } |
2283 | ||
ba5d6ab6 | 2284 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
2285 | if (state->ops->commit) { |
2286 | state->ops->commit(state); | |
2287 | } | |
8802d1fd JC |
2288 | } |
2289 | ||
2290 | /* success */ | |
2291 | goto exit; | |
2292 | ||
2293 | delete_and_fail: | |
b756b9ce | 2294 | /* failure, and it is all-or-none; roll back all operations */ |
ba5d6ab6 SH |
2295 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
2296 | if (state->ops->abort) { | |
2297 | state->ops->abort(state); | |
ba0c86a3 | 2298 | } |
8802d1fd JC |
2299 | } |
2300 | exit: | |
ba5d6ab6 SH |
2301 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
2302 | if (state->ops->clean) { | |
2303 | state->ops->clean(state); | |
ba0c86a3 | 2304 | } |
ba5d6ab6 | 2305 | g_free(state); |
8802d1fd | 2306 | } |
94d16a64 JS |
2307 | if (!has_props) { |
2308 | qapi_free_TransactionProperties(props); | |
2309 | } | |
7eaa8fb5 | 2310 | job_txn_unref(block_job_txn); |
8802d1fd JC |
2311 | } |
2312 | ||
fbe2d816 KW |
2313 | void qmp_eject(bool has_device, const char *device, |
2314 | bool has_id, const char *id, | |
2315 | bool has_force, bool force, Error **errp) | |
666daa68 | 2316 | { |
38f54bd1 | 2317 | Error *local_err = NULL; |
3a3086b7 JS |
2318 | int rc; |
2319 | ||
2320 | if (!has_force) { | |
2321 | force = false; | |
2322 | } | |
666daa68 | 2323 | |
fbe2d816 KW |
2324 | rc = do_open_tray(has_device ? device : NULL, |
2325 | has_id ? id : NULL, | |
2326 | force, &local_err); | |
bf18bee5 | 2327 | if (rc && rc != -ENOSYS) { |
38f54bd1 | 2328 | error_propagate(errp, local_err); |
c245b6a3 | 2329 | return; |
92d48558 | 2330 | } |
bf18bee5 | 2331 | error_free(local_err); |
3a3086b7 | 2332 | |
82fcf66e | 2333 | blockdev_remove_medium(has_device, device, has_id, id, errp); |
666daa68 MA |
2334 | } |
2335 | ||
12d3ba82 BC |
2336 | void qmp_block_passwd(bool has_device, const char *device, |
2337 | bool has_node_name, const char *node_name, | |
2338 | const char *password, Error **errp) | |
666daa68 | 2339 | { |
c01c214b DB |
2340 | error_setg(errp, |
2341 | "Setting block passwords directly is no longer supported"); | |
666daa68 MA |
2342 | } |
2343 | ||
bf18bee5 CL |
2344 | /* |
2345 | * Attempt to open the tray of @device. | |
2346 | * If @force, ignore its tray lock. | |
2347 | * Else, if the tray is locked, don't open it, but ask the guest to open it. | |
2348 | * On error, store an error through @errp and return -errno. | |
2349 | * If @device does not exist, return -ENODEV. | |
2350 | * If it has no removable media, return -ENOTSUP. | |
2351 | * If it has no tray, return -ENOSYS. | |
2352 | * If the guest was asked to open the tray, return -EINPROGRESS. | |
2353 | * Else, return 0. | |
3a3086b7 | 2354 | */ |
b33945cf KW |
2355 | static int do_open_tray(const char *blk_name, const char *qdev_id, |
2356 | bool force, Error **errp) | |
7d8a9f71 HR |
2357 | { |
2358 | BlockBackend *blk; | |
b33945cf | 2359 | const char *device = qdev_id ?: blk_name; |
7d8a9f71 HR |
2360 | bool locked; |
2361 | ||
b33945cf | 2362 | blk = qmp_get_blk(blk_name, qdev_id, errp); |
7d8a9f71 | 2363 | if (!blk) { |
3a3086b7 | 2364 | return -ENODEV; |
7d8a9f71 HR |
2365 | } |
2366 | ||
2367 | if (!blk_dev_has_removable_media(blk)) { | |
2368 | error_setg(errp, "Device '%s' is not removable", device); | |
3a3086b7 | 2369 | return -ENOTSUP; |
7d8a9f71 HR |
2370 | } |
2371 | ||
12c7ec87 | 2372 | if (!blk_dev_has_tray(blk)) { |
bf18bee5 CL |
2373 | error_setg(errp, "Device '%s' does not have a tray", device); |
2374 | return -ENOSYS; | |
12c7ec87 HR |
2375 | } |
2376 | ||
7d8a9f71 | 2377 | if (blk_dev_is_tray_open(blk)) { |
3a3086b7 | 2378 | return 0; |
7d8a9f71 HR |
2379 | } |
2380 | ||
2381 | locked = blk_dev_is_medium_locked(blk); | |
2382 | if (locked) { | |
2383 | blk_dev_eject_request(blk, force); | |
2384 | } | |
2385 | ||
2386 | if (!locked || force) { | |
39829a01 | 2387 | blk_dev_change_media_cb(blk, false, &error_abort); |
7d8a9f71 | 2388 | } |
3a3086b7 JS |
2389 | |
2390 | if (locked && !force) { | |
bf18bee5 CL |
2391 | error_setg(errp, "Device '%s' is locked and force was not specified, " |
2392 | "wait for tray to open and try again", device); | |
2393 | return -EINPROGRESS; | |
3a3086b7 JS |
2394 | } |
2395 | ||
2396 | return 0; | |
2397 | } | |
2398 | ||
b33945cf KW |
2399 | void qmp_blockdev_open_tray(bool has_device, const char *device, |
2400 | bool has_id, const char *id, | |
2401 | bool has_force, bool force, | |
3a3086b7 JS |
2402 | Error **errp) |
2403 | { | |
bf18bee5 CL |
2404 | Error *local_err = NULL; |
2405 | int rc; | |
2406 | ||
3a3086b7 JS |
2407 | if (!has_force) { |
2408 | force = false; | |
2409 | } | |
b33945cf KW |
2410 | rc = do_open_tray(has_device ? device : NULL, |
2411 | has_id ? id : NULL, | |
2412 | force, &local_err); | |
bf18bee5 CL |
2413 | if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { |
2414 | error_propagate(errp, local_err); | |
2415 | return; | |
2416 | } | |
2417 | error_free(local_err); | |
7d8a9f71 HR |
2418 | } |
2419 | ||
b33945cf KW |
2420 | void qmp_blockdev_close_tray(bool has_device, const char *device, |
2421 | bool has_id, const char *id, | |
2422 | Error **errp) | |
abaaf59d HR |
2423 | { |
2424 | BlockBackend *blk; | |
39829a01 | 2425 | Error *local_err = NULL; |
abaaf59d | 2426 | |
b33945cf KW |
2427 | device = has_device ? device : NULL; |
2428 | id = has_id ? id : NULL; | |
2429 | ||
2430 | blk = qmp_get_blk(device, id, errp); | |
abaaf59d | 2431 | if (!blk) { |
abaaf59d HR |
2432 | return; |
2433 | } | |
2434 | ||
2435 | if (!blk_dev_has_removable_media(blk)) { | |
b33945cf | 2436 | error_setg(errp, "Device '%s' is not removable", device ?: id); |
abaaf59d HR |
2437 | return; |
2438 | } | |
2439 | ||
12c7ec87 HR |
2440 | if (!blk_dev_has_tray(blk)) { |
2441 | /* Ignore this command on tray-less devices */ | |
2442 | return; | |
2443 | } | |
2444 | ||
abaaf59d HR |
2445 | if (!blk_dev_is_tray_open(blk)) { |
2446 | return; | |
2447 | } | |
2448 | ||
39829a01 KW |
2449 | blk_dev_change_media_cb(blk, true, &local_err); |
2450 | if (local_err) { | |
2451 | error_propagate(errp, local_err); | |
2452 | return; | |
2453 | } | |
abaaf59d HR |
2454 | } |
2455 | ||
82fcf66e HR |
2456 | static void blockdev_remove_medium(bool has_device, const char *device, |
2457 | bool has_id, const char *id, Error **errp) | |
2814f672 HR |
2458 | { |
2459 | BlockBackend *blk; | |
2460 | BlockDriverState *bs; | |
2461 | AioContext *aio_context; | |
00949bab | 2462 | bool has_attached_device; |
2814f672 | 2463 | |
00949bab KW |
2464 | device = has_device ? device : NULL; |
2465 | id = has_id ? id : NULL; | |
2466 | ||
2467 | blk = qmp_get_blk(device, id, errp); | |
2814f672 | 2468 | if (!blk) { |
2814f672 HR |
2469 | return; |
2470 | } | |
2471 | ||
2472 | /* For BBs without a device, we can exchange the BDS tree at will */ | |
00949bab | 2473 | has_attached_device = blk_get_attached_dev(blk); |
2814f672 | 2474 | |
00949bab KW |
2475 | if (has_attached_device && !blk_dev_has_removable_media(blk)) { |
2476 | error_setg(errp, "Device '%s' is not removable", device ?: id); | |
2814f672 HR |
2477 | return; |
2478 | } | |
2479 | ||
00949bab KW |
2480 | if (has_attached_device && blk_dev_has_tray(blk) && |
2481 | !blk_dev_is_tray_open(blk)) | |
2482 | { | |
2483 | error_setg(errp, "Tray of device '%s' is not open", device ?: id); | |
2814f672 HR |
2484 | return; |
2485 | } | |
2486 | ||
2487 | bs = blk_bs(blk); | |
2488 | if (!bs) { | |
2489 | return; | |
2490 | } | |
2491 | ||
2492 | aio_context = bdrv_get_aio_context(bs); | |
2493 | aio_context_acquire(aio_context); | |
2494 | ||
2495 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { | |
2496 | goto out; | |
2497 | } | |
2498 | ||
2814f672 HR |
2499 | blk_remove_bs(blk); |
2500 | ||
12c7ec87 HR |
2501 | if (!blk_dev_has_tray(blk)) { |
2502 | /* For tray-less devices, blockdev-open-tray is a no-op (or may not be | |
2503 | * called at all); therefore, the medium needs to be ejected here. | |
2504 | * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load | |
2505 | * value passed here (i.e. false). */ | |
39829a01 | 2506 | blk_dev_change_media_cb(blk, false, &error_abort); |
12c7ec87 HR |
2507 | } |
2508 | ||
2814f672 HR |
2509 | out: |
2510 | aio_context_release(aio_context); | |
2511 | } | |
2512 | ||
34ce1111 | 2513 | void qmp_blockdev_remove_medium(const char *id, Error **errp) |
82fcf66e HR |
2514 | { |
2515 | blockdev_remove_medium(false, NULL, true, id, errp); | |
2516 | } | |
2517 | ||
716df217 | 2518 | static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, |
d1299882 HR |
2519 | BlockDriverState *bs, Error **errp) |
2520 | { | |
39829a01 | 2521 | Error *local_err = NULL; |
d1299882 | 2522 | bool has_device; |
d7086422 | 2523 | int ret; |
d1299882 | 2524 | |
d1299882 HR |
2525 | /* For BBs without a device, we can exchange the BDS tree at will */ |
2526 | has_device = blk_get_attached_dev(blk); | |
2527 | ||
2528 | if (has_device && !blk_dev_has_removable_media(blk)) { | |
716df217 | 2529 | error_setg(errp, "Device is not removable"); |
d1299882 HR |
2530 | return; |
2531 | } | |
2532 | ||
12c7ec87 | 2533 | if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { |
716df217 | 2534 | error_setg(errp, "Tray of the device is not open"); |
d1299882 HR |
2535 | return; |
2536 | } | |
2537 | ||
2538 | if (blk_bs(blk)) { | |
716df217 | 2539 | error_setg(errp, "There already is a medium in the device"); |
d1299882 HR |
2540 | return; |
2541 | } | |
2542 | ||
d7086422 KW |
2543 | ret = blk_insert_bs(blk, bs, errp); |
2544 | if (ret < 0) { | |
2545 | return; | |
2546 | } | |
d1299882 | 2547 | |
12c7ec87 HR |
2548 | if (!blk_dev_has_tray(blk)) { |
2549 | /* For tray-less devices, blockdev-close-tray is a no-op (or may not be | |
2550 | * called at all); therefore, the medium needs to be pushed into the | |
2551 | * slot here. | |
2552 | * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load | |
2553 | * value passed here (i.e. true). */ | |
39829a01 KW |
2554 | blk_dev_change_media_cb(blk, true, &local_err); |
2555 | if (local_err) { | |
2556 | error_propagate(errp, local_err); | |
2557 | blk_remove_bs(blk); | |
2558 | return; | |
2559 | } | |
12c7ec87 | 2560 | } |
d1299882 HR |
2561 | } |
2562 | ||
82fcf66e HR |
2563 | static void blockdev_insert_medium(bool has_device, const char *device, |
2564 | bool has_id, const char *id, | |
2565 | const char *node_name, Error **errp) | |
d1299882 | 2566 | { |
716df217 | 2567 | BlockBackend *blk; |
d1299882 HR |
2568 | BlockDriverState *bs; |
2569 | ||
716df217 KW |
2570 | blk = qmp_get_blk(has_device ? device : NULL, |
2571 | has_id ? id : NULL, | |
2572 | errp); | |
2573 | if (!blk) { | |
2574 | return; | |
2575 | } | |
2576 | ||
d1299882 HR |
2577 | bs = bdrv_find_node(node_name); |
2578 | if (!bs) { | |
2579 | error_setg(errp, "Node '%s' not found", node_name); | |
2580 | return; | |
2581 | } | |
2582 | ||
1f0c461b | 2583 | if (bdrv_has_blk(bs)) { |
e467da7b | 2584 | error_setg(errp, "Node '%s' is already in use", node_name); |
d1299882 HR |
2585 | return; |
2586 | } | |
2587 | ||
716df217 | 2588 | qmp_blockdev_insert_anon_medium(blk, bs, errp); |
d1299882 HR |
2589 | } |
2590 | ||
34ce1111 HR |
2591 | void qmp_blockdev_insert_medium(const char *id, const char *node_name, |
2592 | Error **errp) | |
82fcf66e HR |
2593 | { |
2594 | blockdev_insert_medium(false, NULL, true, id, node_name, errp); | |
2595 | } | |
2596 | ||
70e2cb3b KW |
2597 | void qmp_blockdev_change_medium(bool has_device, const char *device, |
2598 | bool has_id, const char *id, | |
2599 | const char *filename, | |
24fb4133 | 2600 | bool has_format, const char *format, |
39ff43d9 HR |
2601 | bool has_read_only, |
2602 | BlockdevChangeReadOnlyMode read_only, | |
24fb4133 | 2603 | Error **errp) |
de2c6c05 HR |
2604 | { |
2605 | BlockBackend *blk; | |
2606 | BlockDriverState *medium_bs = NULL; | |
5b363937 | 2607 | int bdrv_flags; |
b85114f8 | 2608 | bool detect_zeroes; |
38a53d50 | 2609 | int rc; |
de2c6c05 HR |
2610 | QDict *options = NULL; |
2611 | Error *err = NULL; | |
2612 | ||
70e2cb3b KW |
2613 | blk = qmp_get_blk(has_device ? device : NULL, |
2614 | has_id ? id : NULL, | |
2615 | errp); | |
de2c6c05 | 2616 | if (!blk) { |
de2c6c05 HR |
2617 | goto fail; |
2618 | } | |
2619 | ||
2620 | if (blk_bs(blk)) { | |
2621 | blk_update_root_state(blk); | |
2622 | } | |
2623 | ||
2624 | bdrv_flags = blk_get_open_flags_from_root_state(blk); | |
156abc2f AM |
2625 | bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | |
2626 | BDRV_O_PROTOCOL); | |
de2c6c05 | 2627 | |
39ff43d9 HR |
2628 | if (!has_read_only) { |
2629 | read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; | |
2630 | } | |
2631 | ||
2632 | switch (read_only) { | |
2633 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: | |
2634 | break; | |
2635 | ||
2636 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: | |
2637 | bdrv_flags &= ~BDRV_O_RDWR; | |
2638 | break; | |
2639 | ||
2640 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: | |
2641 | bdrv_flags |= BDRV_O_RDWR; | |
2642 | break; | |
2643 | ||
2644 | default: | |
2645 | abort(); | |
2646 | } | |
2647 | ||
b85114f8 KW |
2648 | options = qdict_new(); |
2649 | detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); | |
46f5ac20 | 2650 | qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); |
b85114f8 | 2651 | |
24fb4133 | 2652 | if (has_format) { |
46f5ac20 | 2653 | qdict_put_str(options, "driver", format); |
de2c6c05 HR |
2654 | } |
2655 | ||
5b363937 HR |
2656 | medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); |
2657 | if (!medium_bs) { | |
de2c6c05 HR |
2658 | goto fail; |
2659 | } | |
2660 | ||
70e2cb3b KW |
2661 | rc = do_open_tray(has_device ? device : NULL, |
2662 | has_id ? id : NULL, | |
2663 | false, &err); | |
38a53d50 | 2664 | if (rc && rc != -ENOSYS) { |
de2c6c05 HR |
2665 | error_propagate(errp, err); |
2666 | goto fail; | |
2667 | } | |
38a53d50 CL |
2668 | error_free(err); |
2669 | err = NULL; | |
de2c6c05 | 2670 | |
82fcf66e | 2671 | blockdev_remove_medium(has_device, device, has_id, id, &err); |
de2c6c05 HR |
2672 | if (err) { |
2673 | error_propagate(errp, err); | |
2674 | goto fail; | |
2675 | } | |
2676 | ||
716df217 | 2677 | qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); |
de2c6c05 HR |
2678 | if (err) { |
2679 | error_propagate(errp, err); | |
2680 | goto fail; | |
2681 | } | |
2682 | ||
70e2cb3b | 2683 | qmp_blockdev_close_tray(has_device, device, has_id, id, errp); |
de2c6c05 HR |
2684 | |
2685 | fail: | |
2686 | /* If the medium has been inserted, the device has its own reference, so | |
2687 | * ours must be relinquished; and if it has not been inserted successfully, | |
2688 | * the reference must be relinquished anyway */ | |
2689 | bdrv_unref(medium_bs); | |
2690 | } | |
2691 | ||
727f005e | 2692 | /* throttling disk I/O limits */ |
4dc9397b | 2693 | void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) |
727f005e | 2694 | { |
cc0681c4 | 2695 | ThrottleConfig cfg; |
727f005e | 2696 | BlockDriverState *bs; |
a0e8544c | 2697 | BlockBackend *blk; |
b15446fd | 2698 | AioContext *aio_context; |
727f005e | 2699 | |
7a9877a0 KW |
2700 | blk = qmp_get_blk(arg->has_device ? arg->device : NULL, |
2701 | arg->has_id ? arg->id : NULL, | |
2702 | errp); | |
a0e8544c | 2703 | if (!blk) { |
80047da5 | 2704 | return; |
727f005e | 2705 | } |
5433c24f HR |
2706 | |
2707 | aio_context = blk_get_aio_context(blk); | |
2708 | aio_context_acquire(aio_context); | |
2709 | ||
a0e8544c | 2710 | bs = blk_bs(blk); |
5433c24f | 2711 | if (!bs) { |
7a9877a0 | 2712 | error_setg(errp, "Device has no medium"); |
5433c24f HR |
2713 | goto out; |
2714 | } | |
727f005e | 2715 | |
1588ab5d | 2716 | throttle_config_init(&cfg); |
4dc9397b EB |
2717 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; |
2718 | cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; | |
2719 | cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; | |
cc0681c4 | 2720 | |
4dc9397b EB |
2721 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; |
2722 | cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; | |
2723 | cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; | |
cc0681c4 | 2724 | |
4dc9397b EB |
2725 | if (arg->has_bps_max) { |
2726 | cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; | |
3e9fab69 | 2727 | } |
4dc9397b EB |
2728 | if (arg->has_bps_rd_max) { |
2729 | cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; | |
3e9fab69 | 2730 | } |
4dc9397b EB |
2731 | if (arg->has_bps_wr_max) { |
2732 | cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; | |
3e9fab69 | 2733 | } |
4dc9397b EB |
2734 | if (arg->has_iops_max) { |
2735 | cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; | |
3e9fab69 | 2736 | } |
4dc9397b EB |
2737 | if (arg->has_iops_rd_max) { |
2738 | cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; | |
3e9fab69 | 2739 | } |
4dc9397b EB |
2740 | if (arg->has_iops_wr_max) { |
2741 | cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; | |
3e9fab69 | 2742 | } |
727f005e | 2743 | |
4dc9397b EB |
2744 | if (arg->has_bps_max_length) { |
2745 | cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; | |
dce13204 | 2746 | } |
4dc9397b EB |
2747 | if (arg->has_bps_rd_max_length) { |
2748 | cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; | |
dce13204 | 2749 | } |
4dc9397b EB |
2750 | if (arg->has_bps_wr_max_length) { |
2751 | cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; | |
dce13204 | 2752 | } |
4dc9397b EB |
2753 | if (arg->has_iops_max_length) { |
2754 | cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; | |
dce13204 | 2755 | } |
4dc9397b EB |
2756 | if (arg->has_iops_rd_max_length) { |
2757 | cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; | |
dce13204 | 2758 | } |
4dc9397b EB |
2759 | if (arg->has_iops_wr_max_length) { |
2760 | cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; | |
dce13204 AG |
2761 | } |
2762 | ||
4dc9397b EB |
2763 | if (arg->has_iops_size) { |
2764 | cfg.op_size = arg->iops_size; | |
2024c1df | 2765 | } |
cc0681c4 | 2766 | |
d5851089 | 2767 | if (!throttle_is_valid(&cfg, errp)) { |
5433c24f | 2768 | goto out; |
727f005e ZYW |
2769 | } |
2770 | ||
76f4afb4 AG |
2771 | if (throttle_enabled(&cfg)) { |
2772 | /* Enable I/O limits if they're not enabled yet, otherwise | |
2773 | * just update the throttling group. */ | |
022cdc9f | 2774 | if (!blk_get_public(blk)->throttle_group_member.throttle_state) { |
4dc9397b | 2775 | blk_io_limits_enable(blk, |
7a9877a0 KW |
2776 | arg->has_group ? arg->group : |
2777 | arg->has_device ? arg->device : | |
2778 | arg->id); | |
4dc9397b EB |
2779 | } else if (arg->has_group) { |
2780 | blk_io_limits_update_group(blk, arg->group); | |
76f4afb4 AG |
2781 | } |
2782 | /* Set the new throttling configuration */ | |
97148076 | 2783 | blk_set_io_limits(blk, &cfg); |
022cdc9f | 2784 | } else if (blk_get_public(blk)->throttle_group_member.throttle_state) { |
76f4afb4 | 2785 | /* If all throttling settings are set to 0, disable I/O limits */ |
97148076 | 2786 | blk_io_limits_disable(blk); |
727f005e | 2787 | } |
b15446fd | 2788 | |
5433c24f | 2789 | out: |
b15446fd | 2790 | aio_context_release(aio_context); |
727f005e ZYW |
2791 | } |
2792 | ||
341ebc2f JS |
2793 | void qmp_block_dirty_bitmap_add(const char *node, const char *name, |
2794 | bool has_granularity, uint32_t granularity, | |
fd5ae4cc | 2795 | bool has_persistent, bool persistent, |
eb738bb5 | 2796 | bool has_autoload, bool autoload, |
a6e2ca5f | 2797 | bool has_disabled, bool disabled, |
341ebc2f JS |
2798 | Error **errp) |
2799 | { | |
341ebc2f | 2800 | BlockDriverState *bs; |
fd5ae4cc | 2801 | BdrvDirtyBitmap *bitmap; |
341ebc2f JS |
2802 | |
2803 | if (!name || name[0] == '\0') { | |
2804 | error_setg(errp, "Bitmap name cannot be empty"); | |
2805 | return; | |
2806 | } | |
2807 | ||
2808 | bs = bdrv_lookup_bs(node, node, errp); | |
2809 | if (!bs) { | |
2810 | return; | |
2811 | } | |
2812 | ||
341ebc2f JS |
2813 | if (has_granularity) { |
2814 | if (granularity < 512 || !is_power_of_2(granularity)) { | |
2815 | error_setg(errp, "Granularity must be power of 2 " | |
2816 | "and at least 512"); | |
2119882c | 2817 | return; |
341ebc2f JS |
2818 | } |
2819 | } else { | |
2820 | /* Default to cluster size, if available: */ | |
2821 | granularity = bdrv_get_default_bitmap_granularity(bs); | |
2822 | } | |
2823 | ||
fd5ae4cc VSO |
2824 | if (!has_persistent) { |
2825 | persistent = false; | |
2826 | } | |
eb738bb5 | 2827 | |
3e99da5e VSO |
2828 | if (has_autoload) { |
2829 | warn_report("Autoload option is deprecated and its value is ignored"); | |
eb738bb5 | 2830 | } |
fd5ae4cc | 2831 | |
a6e2ca5f VSO |
2832 | if (!has_disabled) { |
2833 | disabled = false; | |
2834 | } | |
2835 | ||
fd5ae4cc VSO |
2836 | if (persistent && |
2837 | !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) | |
2838 | { | |
2839 | return; | |
2840 | } | |
2841 | ||
2842 | bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); | |
eb738bb5 VSO |
2843 | if (bitmap == NULL) { |
2844 | return; | |
fd5ae4cc | 2845 | } |
eb738bb5 | 2846 | |
a6e2ca5f VSO |
2847 | if (disabled) { |
2848 | bdrv_disable_dirty_bitmap(bitmap); | |
2849 | } | |
2850 | ||
eb738bb5 | 2851 | bdrv_dirty_bitmap_set_persistance(bitmap, persistent); |
341ebc2f JS |
2852 | } |
2853 | ||
2854 | void qmp_block_dirty_bitmap_remove(const char *node, const char *name, | |
2855 | Error **errp) | |
2856 | { | |
341ebc2f JS |
2857 | BlockDriverState *bs; |
2858 | BdrvDirtyBitmap *bitmap; | |
5c36c1af | 2859 | Error *local_err = NULL; |
341ebc2f | 2860 | |
2119882c | 2861 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); |
341ebc2f JS |
2862 | if (!bitmap || !bs) { |
2863 | return; | |
2864 | } | |
2865 | ||
9bd2b08f JS |
2866 | if (bdrv_dirty_bitmap_frozen(bitmap)) { |
2867 | error_setg(errp, | |
2868 | "Bitmap '%s' is currently frozen and cannot be removed", | |
2869 | name); | |
2119882c | 2870 | return; |
4f43e953 VSO |
2871 | } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { |
2872 | error_setg(errp, | |
2873 | "Bitmap '%s' is currently locked and cannot be removed", | |
2874 | name); | |
2875 | return; | |
9bd2b08f | 2876 | } |
5c36c1af VSO |
2877 | |
2878 | if (bdrv_dirty_bitmap_get_persistance(bitmap)) { | |
2879 | bdrv_remove_persistent_dirty_bitmap(bs, name, &local_err); | |
2880 | if (local_err != NULL) { | |
2881 | error_propagate(errp, local_err); | |
2882 | return; | |
2883 | } | |
2884 | } | |
2885 | ||
341ebc2f | 2886 | bdrv_release_dirty_bitmap(bs, bitmap); |
341ebc2f JS |
2887 | } |
2888 | ||
e74e6b78 JS |
2889 | /** |
2890 | * Completely clear a bitmap, for the purposes of synchronizing a bitmap | |
2891 | * immediately after a full backup operation. | |
2892 | */ | |
2893 | void qmp_block_dirty_bitmap_clear(const char *node, const char *name, | |
2894 | Error **errp) | |
2895 | { | |
e74e6b78 JS |
2896 | BdrvDirtyBitmap *bitmap; |
2897 | BlockDriverState *bs; | |
2898 | ||
2119882c | 2899 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); |
e74e6b78 JS |
2900 | if (!bitmap || !bs) { |
2901 | return; | |
2902 | } | |
2903 | ||
2904 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
2905 | error_setg(errp, | |
2906 | "Bitmap '%s' is currently frozen and cannot be modified", | |
2907 | name); | |
2119882c | 2908 | return; |
4f43e953 VSO |
2909 | } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) { |
2910 | error_setg(errp, | |
2911 | "Bitmap '%s' is currently locked and cannot be modified", | |
2912 | name); | |
2913 | return; | |
e74e6b78 JS |
2914 | } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { |
2915 | error_setg(errp, | |
2916 | "Bitmap '%s' is currently disabled and cannot be cleared", | |
2917 | name); | |
2119882c | 2918 | return; |
d6883bc9 VSO |
2919 | } else if (bdrv_dirty_bitmap_readonly(bitmap)) { |
2920 | error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name); | |
2921 | return; | |
e74e6b78 JS |
2922 | } |
2923 | ||
df9a681d | 2924 | bdrv_clear_dirty_bitmap(bitmap, NULL); |
e74e6b78 JS |
2925 | } |
2926 | ||
5c5d2e50 VSO |
2927 | void qmp_x_block_dirty_bitmap_enable(const char *node, const char *name, |
2928 | Error **errp) | |
2929 | { | |
2930 | BlockDriverState *bs; | |
2931 | BdrvDirtyBitmap *bitmap; | |
2932 | ||
2933 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2934 | if (!bitmap) { | |
2935 | return; | |
2936 | } | |
2937 | ||
2938 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
2939 | error_setg(errp, | |
2940 | "Bitmap '%s' is currently frozen and cannot be enabled", | |
2941 | name); | |
2942 | return; | |
2943 | } | |
2944 | ||
2945 | bdrv_enable_dirty_bitmap(bitmap); | |
2946 | } | |
2947 | ||
2948 | void qmp_x_block_dirty_bitmap_disable(const char *node, const char *name, | |
2949 | Error **errp) | |
2950 | { | |
2951 | BlockDriverState *bs; | |
2952 | BdrvDirtyBitmap *bitmap; | |
2953 | ||
2954 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2955 | if (!bitmap) { | |
2956 | return; | |
2957 | } | |
2958 | ||
2959 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
2960 | error_setg(errp, | |
2961 | "Bitmap '%s' is currently frozen and cannot be disabled", | |
2962 | name); | |
2963 | return; | |
2964 | } | |
2965 | ||
2966 | bdrv_disable_dirty_bitmap(bitmap); | |
2967 | } | |
2968 | ||
b598e531 VSO |
2969 | void qmp_x_block_dirty_bitmap_merge(const char *node, const char *dst_name, |
2970 | const char *src_name, Error **errp) | |
2971 | { | |
2972 | BlockDriverState *bs; | |
2973 | BdrvDirtyBitmap *dst, *src; | |
2974 | ||
2975 | dst = block_dirty_bitmap_lookup(node, dst_name, &bs, errp); | |
2976 | if (!dst) { | |
2977 | return; | |
2978 | } | |
2979 | ||
2980 | if (bdrv_dirty_bitmap_frozen(dst)) { | |
2981 | error_setg(errp, "Bitmap '%s' is frozen and cannot be modified", | |
2982 | dst_name); | |
2983 | return; | |
2984 | } else if (bdrv_dirty_bitmap_readonly(dst)) { | |
2985 | error_setg(errp, "Bitmap '%s' is readonly and cannot be modified", | |
2986 | dst_name); | |
2987 | return; | |
2988 | } | |
2989 | ||
2990 | src = bdrv_find_dirty_bitmap(bs, src_name); | |
2991 | if (!src) { | |
2992 | error_setg(errp, "Dirty bitmap '%s' not found", src_name); | |
2993 | return; | |
2994 | } | |
2995 | ||
2996 | bdrv_merge_dirty_bitmap(dst, src, errp); | |
2997 | } | |
2998 | ||
a3b52535 VSO |
2999 | BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, |
3000 | const char *name, | |
3001 | Error **errp) | |
3002 | { | |
3003 | BdrvDirtyBitmap *bitmap; | |
3004 | BlockDriverState *bs; | |
3005 | BlockDirtyBitmapSha256 *ret = NULL; | |
3006 | char *sha256; | |
3007 | ||
3008 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
3009 | if (!bitmap || !bs) { | |
3010 | return NULL; | |
3011 | } | |
3012 | ||
3013 | sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); | |
3014 | if (sha256 == NULL) { | |
3015 | return NULL; | |
3016 | } | |
3017 | ||
3018 | ret = g_new(BlockDirtyBitmapSha256, 1); | |
3019 | ret->sha256 = sha256; | |
3020 | ||
3021 | return ret; | |
3022 | } | |
3023 | ||
072ebe6b | 3024 | void hmp_drive_del(Monitor *mon, const QDict *qdict) |
9063f814 RH |
3025 | { |
3026 | const char *id = qdict_get_str(qdict, "id"); | |
7e7d56d9 | 3027 | BlockBackend *blk; |
9063f814 | 3028 | BlockDriverState *bs; |
8ad4202b | 3029 | AioContext *aio_context; |
3718d8ab | 3030 | Error *local_err = NULL; |
9063f814 | 3031 | |
2073d410 KW |
3032 | bs = bdrv_find_node(id); |
3033 | if (bs) { | |
79b7a77e | 3034 | qmp_blockdev_del(id, &local_err); |
2073d410 KW |
3035 | if (local_err) { |
3036 | error_report_err(local_err); | |
3037 | } | |
3038 | return; | |
3039 | } | |
3040 | ||
7e7d56d9 MA |
3041 | blk = blk_by_name(id); |
3042 | if (!blk) { | |
b1422f20 | 3043 | error_report("Device '%s' not found", id); |
072ebe6b | 3044 | return; |
9063f814 | 3045 | } |
8ad4202b | 3046 | |
26f8b3a8 | 3047 | if (!blk_legacy_dinfo(blk)) { |
48f364dd MA |
3048 | error_report("Deleting device added with blockdev-add" |
3049 | " is not supported"); | |
072ebe6b | 3050 | return; |
48f364dd MA |
3051 | } |
3052 | ||
5433c24f | 3053 | aio_context = blk_get_aio_context(blk); |
8ad4202b SH |
3054 | aio_context_acquire(aio_context); |
3055 | ||
5433c24f HR |
3056 | bs = blk_bs(blk); |
3057 | if (bs) { | |
3058 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { | |
3059 | error_report_err(local_err); | |
3060 | aio_context_release(aio_context); | |
3061 | return; | |
3062 | } | |
9063f814 | 3063 | |
938abd43 | 3064 | blk_remove_bs(blk); |
5433c24f | 3065 | } |
9063f814 | 3066 | |
7c735873 | 3067 | /* Make the BlockBackend and the attached BlockDriverState anonymous */ |
efaa7c4e HR |
3068 | monitor_remove_blk(blk); |
3069 | ||
7c735873 HR |
3070 | /* If this BlockBackend has a device attached to it, its refcount will be |
3071 | * decremented when the device is removed; otherwise we have to do so here. | |
d22b2f41 | 3072 | */ |
a7f53e26 | 3073 | if (blk_get_attached_dev(blk)) { |
293c51a6 | 3074 | /* Further I/O must not pause the guest */ |
373340b2 HR |
3075 | blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, |
3076 | BLOCKDEV_ON_ERROR_REPORT); | |
d22b2f41 | 3077 | } else { |
b9fe8a7a | 3078 | blk_unref(blk); |
9063f814 RH |
3079 | } |
3080 | ||
8ad4202b | 3081 | aio_context_release(aio_context); |
9063f814 | 3082 | } |
6d4a2b3a | 3083 | |
3b1dbd11 BC |
3084 | void qmp_block_resize(bool has_device, const char *device, |
3085 | bool has_node_name, const char *node_name, | |
3086 | int64_t size, Error **errp) | |
6d4a2b3a | 3087 | { |
3b1dbd11 | 3088 | Error *local_err = NULL; |
7dad9ee6 | 3089 | BlockBackend *blk = NULL; |
6d4a2b3a | 3090 | BlockDriverState *bs; |
927e0e76 | 3091 | AioContext *aio_context; |
8732901e | 3092 | int ret; |
6d4a2b3a | 3093 | |
3b1dbd11 BC |
3094 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
3095 | has_node_name ? node_name : NULL, | |
3096 | &local_err); | |
84d18f06 | 3097 | if (local_err) { |
3b1dbd11 BC |
3098 | error_propagate(errp, local_err); |
3099 | return; | |
3100 | } | |
3101 | ||
927e0e76 SH |
3102 | aio_context = bdrv_get_aio_context(bs); |
3103 | aio_context_acquire(aio_context); | |
3104 | ||
3b1dbd11 | 3105 | if (!bdrv_is_first_non_filter(bs)) { |
c6bd8c70 | 3106 | error_setg(errp, QERR_FEATURE_DISABLED, "resize"); |
927e0e76 | 3107 | goto out; |
6d4a2b3a CH |
3108 | } |
3109 | ||
3110 | if (size < 0) { | |
c6bd8c70 | 3111 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
927e0e76 | 3112 | goto out; |
6d4a2b3a CH |
3113 | } |
3114 | ||
9c75e168 | 3115 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
c6bd8c70 | 3116 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
927e0e76 | 3117 | goto out; |
9c75e168 JC |
3118 | } |
3119 | ||
6d0eb64d | 3120 | blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); |
d7086422 KW |
3121 | ret = blk_insert_bs(blk, bs, errp); |
3122 | if (ret < 0) { | |
3123 | goto out; | |
3124 | } | |
7dad9ee6 | 3125 | |
698bdfa0 | 3126 | bdrv_drained_begin(bs); |
3a691c50 | 3127 | ret = blk_truncate(blk, size, PREALLOC_MODE_OFF, errp); |
698bdfa0 | 3128 | bdrv_drained_end(bs); |
927e0e76 SH |
3129 | |
3130 | out: | |
7dad9ee6 | 3131 | blk_unref(blk); |
927e0e76 | 3132 | aio_context_release(aio_context); |
6d4a2b3a | 3133 | } |
12bd451f | 3134 | |
2323322e | 3135 | void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, |
13d8cc51 | 3136 | bool has_base, const char *base, |
312fe09c | 3137 | bool has_base_node, const char *base_node, |
13d8cc51 JC |
3138 | bool has_backing_file, const char *backing_file, |
3139 | bool has_speed, int64_t speed, | |
1d809098 PB |
3140 | bool has_on_error, BlockdevOnError on_error, |
3141 | Error **errp) | |
12bd451f | 3142 | { |
554b6147 | 3143 | BlockDriverState *bs, *iter; |
c8c3080f | 3144 | BlockDriverState *base_bs = NULL; |
f3e69beb | 3145 | AioContext *aio_context; |
fd7f8c65 | 3146 | Error *local_err = NULL; |
13d8cc51 | 3147 | const char *base_name = NULL; |
12bd451f | 3148 | |
1d809098 PB |
3149 | if (!has_on_error) { |
3150 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
3151 | } | |
3152 | ||
554b6147 | 3153 | bs = bdrv_lookup_bs(device, device, errp); |
b6c1bae5 | 3154 | if (!bs) { |
12bd451f SH |
3155 | return; |
3156 | } | |
3157 | ||
b6c1bae5 | 3158 | aio_context = bdrv_get_aio_context(bs); |
f3e69beb SH |
3159 | aio_context_acquire(aio_context); |
3160 | ||
312fe09c AG |
3161 | if (has_base && has_base_node) { |
3162 | error_setg(errp, "'base' and 'base-node' cannot be specified " | |
3163 | "at the same time"); | |
3164 | goto out; | |
3165 | } | |
3166 | ||
13d8cc51 | 3167 | if (has_base) { |
c8c3080f MT |
3168 | base_bs = bdrv_find_backing_image(bs, base); |
3169 | if (base_bs == NULL) { | |
c6bd8c70 | 3170 | error_setg(errp, QERR_BASE_NOT_FOUND, base); |
f3e69beb | 3171 | goto out; |
c8c3080f | 3172 | } |
f3e69beb | 3173 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
13d8cc51 | 3174 | base_name = base; |
12bd451f SH |
3175 | } |
3176 | ||
312fe09c AG |
3177 | if (has_base_node) { |
3178 | base_bs = bdrv_lookup_bs(NULL, base_node, errp); | |
3179 | if (!base_bs) { | |
3180 | goto out; | |
3181 | } | |
3182 | if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { | |
3183 | error_setg(errp, "Node '%s' is not a backing image of '%s'", | |
3184 | base_node, device); | |
3185 | goto out; | |
3186 | } | |
3187 | assert(bdrv_get_aio_context(base_bs) == aio_context); | |
3188 | base_name = base_bs->filename; | |
3189 | } | |
3190 | ||
554b6147 AG |
3191 | /* Check for op blockers in the whole chain between bs and base */ |
3192 | for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { | |
3193 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { | |
3194 | goto out; | |
3195 | } | |
3196 | } | |
3197 | ||
13d8cc51 JC |
3198 | /* if we are streaming the entire chain, the result will have no backing |
3199 | * file, and specifying one is therefore an error */ | |
3200 | if (base_bs == NULL && has_backing_file) { | |
3201 | error_setg(errp, "backing file specified, but streaming the " | |
3202 | "entire chain"); | |
f3e69beb | 3203 | goto out; |
13d8cc51 JC |
3204 | } |
3205 | ||
3206 | /* backing_file string overrides base bs filename */ | |
3207 | base_name = has_backing_file ? backing_file : base_name; | |
3208 | ||
2323322e | 3209 | stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, |
8254b6d9 | 3210 | has_speed ? speed : 0, on_error, &local_err); |
84d18f06 | 3211 | if (local_err) { |
fd7f8c65 | 3212 | error_propagate(errp, local_err); |
f3e69beb | 3213 | goto out; |
12bd451f SH |
3214 | } |
3215 | ||
3216 | trace_qmp_block_stream(bs, bs->job); | |
f3e69beb SH |
3217 | |
3218 | out: | |
3219 | aio_context_release(aio_context); | |
12bd451f | 3220 | } |
2d47c6e9 | 3221 | |
fd62c609 | 3222 | void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, |
7676e2c5 JC |
3223 | bool has_base, const char *base, |
3224 | bool has_top, const char *top, | |
54e26900 | 3225 | bool has_backing_file, const char *backing_file, |
ed61fc10 | 3226 | bool has_speed, int64_t speed, |
0db832f4 | 3227 | bool has_filter_node_name, const char *filter_node_name, |
ed61fc10 JC |
3228 | Error **errp) |
3229 | { | |
3230 | BlockDriverState *bs; | |
058223a6 | 3231 | BlockDriverState *iter; |
ed61fc10 | 3232 | BlockDriverState *base_bs, *top_bs; |
9e85cd5c | 3233 | AioContext *aio_context; |
ed61fc10 JC |
3234 | Error *local_err = NULL; |
3235 | /* This will be part of the QMP command, if/when the | |
3236 | * BlockdevOnError change for blkmirror makes it in | |
3237 | */ | |
92aa5c6d | 3238 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
ed61fc10 | 3239 | |
54504663 HR |
3240 | if (!has_speed) { |
3241 | speed = 0; | |
3242 | } | |
0db832f4 KW |
3243 | if (!has_filter_node_name) { |
3244 | filter_node_name = NULL; | |
3245 | } | |
54504663 | 3246 | |
7676e2c5 JC |
3247 | /* Important Note: |
3248 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
3249 | * live commit feature versions; for this to work, we must make sure to | |
3250 | * perform the device lookup before any generic errors that may occur in a | |
3251 | * scenario in which all optional arguments are omitted. */ | |
1d13b167 KW |
3252 | bs = qmp_get_root_bs(device, &local_err); |
3253 | if (!bs) { | |
3254 | bs = bdrv_lookup_bs(device, device, NULL); | |
3255 | if (!bs) { | |
3256 | error_free(local_err); | |
3257 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
3258 | "Device '%s' not found", device); | |
3259 | } else { | |
3260 | error_propagate(errp, local_err); | |
3261 | } | |
ed61fc10 | 3262 | return; |
628ff683 FZ |
3263 | } |
3264 | ||
1d13b167 | 3265 | aio_context = bdrv_get_aio_context(bs); |
9e85cd5c SH |
3266 | aio_context_acquire(aio_context); |
3267 | ||
bb00021d | 3268 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { |
9e85cd5c | 3269 | goto out; |
ed61fc10 | 3270 | } |
ed61fc10 JC |
3271 | |
3272 | /* default top_bs is the active layer */ | |
3273 | top_bs = bs; | |
3274 | ||
7676e2c5 | 3275 | if (has_top && top) { |
ed61fc10 JC |
3276 | if (strcmp(bs->filename, top) != 0) { |
3277 | top_bs = bdrv_find_backing_image(bs, top); | |
3278 | } | |
3279 | } | |
3280 | ||
3281 | if (top_bs == NULL) { | |
3282 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
9e85cd5c | 3283 | goto out; |
ed61fc10 JC |
3284 | } |
3285 | ||
9e85cd5c SH |
3286 | assert(bdrv_get_aio_context(top_bs) == aio_context); |
3287 | ||
d5208c45 JC |
3288 | if (has_base && base) { |
3289 | base_bs = bdrv_find_backing_image(top_bs, base); | |
3290 | } else { | |
3291 | base_bs = bdrv_find_base(top_bs); | |
3292 | } | |
3293 | ||
3294 | if (base_bs == NULL) { | |
c6bd8c70 | 3295 | error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); |
9e85cd5c | 3296 | goto out; |
d5208c45 JC |
3297 | } |
3298 | ||
9e85cd5c SH |
3299 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
3300 | ||
058223a6 AG |
3301 | for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { |
3302 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { | |
3303 | goto out; | |
3304 | } | |
bb00021d FZ |
3305 | } |
3306 | ||
7676e2c5 JC |
3307 | /* Do not allow attempts to commit an image into itself */ |
3308 | if (top_bs == base_bs) { | |
3309 | error_setg(errp, "cannot commit an image into itself"); | |
9e85cd5c | 3310 | goto out; |
7676e2c5 JC |
3311 | } |
3312 | ||
20a63d2c | 3313 | if (top_bs == bs) { |
54e26900 JC |
3314 | if (has_backing_file) { |
3315 | error_setg(errp, "'backing-file' specified," | |
3316 | " but 'top' is the active layer"); | |
9e85cd5c | 3317 | goto out; |
54e26900 | 3318 | } |
47970dfb | 3319 | commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, |
bb02b65c | 3320 | JOB_DEFAULT, speed, on_error, |
78bbd910 | 3321 | filter_node_name, NULL, NULL, false, &local_err); |
20a63d2c | 3322 | } else { |
058223a6 AG |
3323 | BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); |
3324 | if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { | |
3325 | goto out; | |
3326 | } | |
fd62c609 | 3327 | commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, |
8254b6d9 | 3328 | on_error, has_backing_file ? backing_file : NULL, |
0db832f4 | 3329 | filter_node_name, &local_err); |
20a63d2c | 3330 | } |
ed61fc10 JC |
3331 | if (local_err != NULL) { |
3332 | error_propagate(errp, local_err); | |
9e85cd5c | 3333 | goto out; |
ed61fc10 | 3334 | } |
9e85cd5c SH |
3335 | |
3336 | out: | |
3337 | aio_context_release(aio_context); | |
ed61fc10 JC |
3338 | } |
3339 | ||
62c9e416 | 3340 | static BlockJob *do_drive_backup(DriveBackup *backup, JobTxn *txn, |
111049a4 | 3341 | Error **errp) |
99a9addf SH |
3342 | { |
3343 | BlockDriverState *bs; | |
3344 | BlockDriverState *target_bs; | |
fc5d3f84 | 3345 | BlockDriverState *source = NULL; |
111049a4 | 3346 | BlockJob *job = NULL; |
d58d8453 | 3347 | BdrvDirtyBitmap *bmap = NULL; |
761731b1 | 3348 | AioContext *aio_context; |
e6641719 | 3349 | QDict *options = NULL; |
99a9addf | 3350 | Error *local_err = NULL; |
bb02b65c | 3351 | int flags, job_flags = JOB_DEFAULT; |
99a9addf | 3352 | int64_t size; |
fc0932fd | 3353 | bool set_backing_hd = false; |
99a9addf | 3354 | |
81206a89 PB |
3355 | if (!backup->has_speed) { |
3356 | backup->speed = 0; | |
99a9addf | 3357 | } |
81206a89 PB |
3358 | if (!backup->has_on_source_error) { |
3359 | backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
99a9addf | 3360 | } |
81206a89 PB |
3361 | if (!backup->has_on_target_error) { |
3362 | backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
99a9addf | 3363 | } |
81206a89 PB |
3364 | if (!backup->has_mode) { |
3365 | backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
3366 | } | |
3367 | if (!backup->has_job_id) { | |
3368 | backup->job_id = NULL; | |
99a9addf | 3369 | } |
b40dacdc JS |
3370 | if (!backup->has_auto_finalize) { |
3371 | backup->auto_finalize = true; | |
3372 | } | |
3373 | if (!backup->has_auto_dismiss) { | |
3374 | backup->auto_dismiss = true; | |
3375 | } | |
13b9414b PB |
3376 | if (!backup->has_compress) { |
3377 | backup->compress = false; | |
3378 | } | |
99a9addf | 3379 | |
81206a89 | 3380 | bs = qmp_get_root_bs(backup->device, errp); |
b7e4fa22 | 3381 | if (!bs) { |
111049a4 | 3382 | return NULL; |
99a9addf SH |
3383 | } |
3384 | ||
b7e4fa22 | 3385 | aio_context = bdrv_get_aio_context(bs); |
761731b1 SH |
3386 | aio_context_acquire(aio_context); |
3387 | ||
81206a89 PB |
3388 | if (!backup->has_format) { |
3389 | backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? | |
3390 | NULL : (char*) bs->drv->format_name; | |
99a9addf | 3391 | } |
99a9addf | 3392 | |
c29c1dd3 | 3393 | /* Early check to avoid creating target */ |
3718d8ab | 3394 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { |
761731b1 | 3395 | goto out; |
99a9addf SH |
3396 | } |
3397 | ||
61de4c68 | 3398 | flags = bs->open_flags | BDRV_O_RDWR; |
99a9addf | 3399 | |
fc5d3f84 IM |
3400 | /* See if we have a backing HD we can use to create our new image |
3401 | * on top of. */ | |
81206a89 | 3402 | if (backup->sync == MIRROR_SYNC_MODE_TOP) { |
760e0063 | 3403 | source = backing_bs(bs); |
fc5d3f84 | 3404 | if (!source) { |
81206a89 | 3405 | backup->sync = MIRROR_SYNC_MODE_FULL; |
fc5d3f84 IM |
3406 | } |
3407 | } | |
81206a89 | 3408 | if (backup->sync == MIRROR_SYNC_MODE_NONE) { |
fc5d3f84 | 3409 | source = bs; |
fc0932fd FZ |
3410 | flags |= BDRV_O_NO_BACKING; |
3411 | set_backing_hd = true; | |
fc5d3f84 IM |
3412 | } |
3413 | ||
99a9addf SH |
3414 | size = bdrv_getlength(bs); |
3415 | if (size < 0) { | |
3416 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
761731b1 | 3417 | goto out; |
99a9addf SH |
3418 | } |
3419 | ||
81206a89 PB |
3420 | if (backup->mode != NEW_IMAGE_MODE_EXISTING) { |
3421 | assert(backup->format); | |
fc5d3f84 | 3422 | if (source) { |
81206a89 | 3423 | bdrv_img_create(backup->target, backup->format, source->filename, |
fc5d3f84 | 3424 | source->drv->format_name, NULL, |
9217283d | 3425 | size, flags, false, &local_err); |
fc5d3f84 | 3426 | } else { |
81206a89 | 3427 | bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, |
9217283d | 3428 | size, flags, false, &local_err); |
fc5d3f84 | 3429 | } |
99a9addf SH |
3430 | } |
3431 | ||
84d18f06 | 3432 | if (local_err) { |
99a9addf | 3433 | error_propagate(errp, local_err); |
761731b1 | 3434 | goto out; |
99a9addf SH |
3435 | } |
3436 | ||
81206a89 | 3437 | if (backup->format) { |
fc0932fd FZ |
3438 | if (!options) { |
3439 | options = qdict_new(); | |
3440 | } | |
46f5ac20 | 3441 | qdict_put_str(options, "driver", backup->format); |
e6641719 HR |
3442 | } |
3443 | ||
81206a89 | 3444 | target_bs = bdrv_open(backup->target, NULL, options, flags, errp); |
5b363937 | 3445 | if (!target_bs) { |
761731b1 | 3446 | goto out; |
99a9addf SH |
3447 | } |
3448 | ||
761731b1 SH |
3449 | bdrv_set_aio_context(target_bs, aio_context); |
3450 | ||
fc0932fd FZ |
3451 | if (set_backing_hd) { |
3452 | bdrv_set_backing_hd(target_bs, source, &local_err); | |
3453 | if (local_err) { | |
3454 | bdrv_unref(target_bs); | |
3455 | goto out; | |
3456 | } | |
3457 | } | |
3458 | ||
81206a89 PB |
3459 | if (backup->has_bitmap) { |
3460 | bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); | |
d58d8453 | 3461 | if (!bmap) { |
81206a89 | 3462 | error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); |
0702d3d8 | 3463 | bdrv_unref(target_bs); |
d58d8453 JS |
3464 | goto out; |
3465 | } | |
4f43e953 VSO |
3466 | if (bdrv_dirty_bitmap_qmp_locked(bmap)) { |
3467 | error_setg(errp, | |
3468 | "Bitmap '%s' is currently locked and cannot be used for " | |
3469 | "backup", backup->bitmap); | |
3470 | goto out; | |
3471 | } | |
d58d8453 | 3472 | } |
b40dacdc | 3473 | if (!backup->auto_finalize) { |
bb02b65c | 3474 | job_flags |= JOB_MANUAL_FINALIZE; |
b40dacdc JS |
3475 | } |
3476 | if (!backup->auto_dismiss) { | |
bb02b65c | 3477 | job_flags |= JOB_MANUAL_DISMISS; |
b40dacdc | 3478 | } |
d58d8453 | 3479 | |
111049a4 JS |
3480 | job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, |
3481 | backup->sync, bmap, backup->compress, | |
3482 | backup->on_source_error, backup->on_target_error, | |
b40dacdc | 3483 | job_flags, NULL, NULL, txn, &local_err); |
5c438bc6 | 3484 | bdrv_unref(target_bs); |
99a9addf | 3485 | if (local_err != NULL) { |
99a9addf | 3486 | error_propagate(errp, local_err); |
761731b1 | 3487 | goto out; |
99a9addf | 3488 | } |
761731b1 SH |
3489 | |
3490 | out: | |
3491 | aio_context_release(aio_context); | |
111049a4 | 3492 | return job; |
99a9addf SH |
3493 | } |
3494 | ||
81206a89 | 3495 | void qmp_drive_backup(DriveBackup *arg, Error **errp) |
78f51fde | 3496 | { |
111049a4 JS |
3497 | |
3498 | BlockJob *job; | |
3499 | job = do_drive_backup(arg, NULL, errp); | |
3500 | if (job) { | |
da01ff7f | 3501 | job_start(&job->job); |
111049a4 | 3502 | } |
78f51fde JS |
3503 | } |
3504 | ||
c13163fb BC |
3505 | BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) |
3506 | { | |
d5a8ee60 | 3507 | return bdrv_named_nodes_list(errp); |
c13163fb BC |
3508 | } |
3509 | ||
62c9e416 | 3510 | BlockJob *do_blockdev_backup(BlockdevBackup *backup, JobTxn *txn, |
111049a4 | 3511 | Error **errp) |
c29c1dd3 FZ |
3512 | { |
3513 | BlockDriverState *bs; | |
3514 | BlockDriverState *target_bs; | |
3515 | Error *local_err = NULL; | |
3516 | AioContext *aio_context; | |
111049a4 | 3517 | BlockJob *job = NULL; |
bb02b65c | 3518 | int job_flags = JOB_DEFAULT; |
c29c1dd3 | 3519 | |
dc7a4a9e PB |
3520 | if (!backup->has_speed) { |
3521 | backup->speed = 0; | |
c29c1dd3 | 3522 | } |
dc7a4a9e PB |
3523 | if (!backup->has_on_source_error) { |
3524 | backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3525 | } | |
3526 | if (!backup->has_on_target_error) { | |
3527 | backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
c29c1dd3 | 3528 | } |
dc7a4a9e PB |
3529 | if (!backup->has_job_id) { |
3530 | backup->job_id = NULL; | |
c29c1dd3 | 3531 | } |
b40dacdc JS |
3532 | if (!backup->has_auto_finalize) { |
3533 | backup->auto_finalize = true; | |
3534 | } | |
3535 | if (!backup->has_auto_dismiss) { | |
3536 | backup->auto_dismiss = true; | |
3537 | } | |
3b7b1236 PB |
3538 | if (!backup->has_compress) { |
3539 | backup->compress = false; | |
3540 | } | |
c29c1dd3 | 3541 | |
930fe17f | 3542 | bs = bdrv_lookup_bs(backup->device, backup->device, errp); |
cef34eeb | 3543 | if (!bs) { |
111049a4 | 3544 | return NULL; |
c29c1dd3 FZ |
3545 | } |
3546 | ||
cef34eeb | 3547 | aio_context = bdrv_get_aio_context(bs); |
c29c1dd3 FZ |
3548 | aio_context_acquire(aio_context); |
3549 | ||
dc7a4a9e | 3550 | target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); |
0d978913 | 3551 | if (!target_bs) { |
5433c24f HR |
3552 | goto out; |
3553 | } | |
c29c1dd3 | 3554 | |
efd75567 FZ |
3555 | if (bdrv_get_aio_context(target_bs) != aio_context) { |
3556 | if (!bdrv_has_blk(target_bs)) { | |
3557 | /* The target BDS is not attached, we can safely move it to another | |
3558 | * AioContext. */ | |
3559 | bdrv_set_aio_context(target_bs, aio_context); | |
3560 | } else { | |
3561 | error_setg(errp, "Target is attached to a different thread from " | |
3562 | "source."); | |
3563 | goto out; | |
3564 | } | |
3565 | } | |
b40dacdc | 3566 | if (!backup->auto_finalize) { |
bb02b65c | 3567 | job_flags |= JOB_MANUAL_FINALIZE; |
b40dacdc JS |
3568 | } |
3569 | if (!backup->auto_dismiss) { | |
bb02b65c | 3570 | job_flags |= JOB_MANUAL_DISMISS; |
b40dacdc | 3571 | } |
111049a4 JS |
3572 | job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, |
3573 | backup->sync, NULL, backup->compress, | |
3574 | backup->on_source_error, backup->on_target_error, | |
b40dacdc | 3575 | job_flags, NULL, NULL, txn, &local_err); |
c29c1dd3 | 3576 | if (local_err != NULL) { |
c29c1dd3 FZ |
3577 | error_propagate(errp, local_err); |
3578 | } | |
3579 | out: | |
3580 | aio_context_release(aio_context); | |
111049a4 | 3581 | return job; |
c29c1dd3 FZ |
3582 | } |
3583 | ||
dc7a4a9e PB |
3584 | void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) |
3585 | { | |
111049a4 JS |
3586 | BlockJob *job; |
3587 | job = do_blockdev_backup(arg, NULL, errp); | |
3588 | if (job) { | |
da01ff7f | 3589 | job_start(&job->job); |
111049a4 | 3590 | } |
78f51fde JS |
3591 | } |
3592 | ||
4193cdd7 FZ |
3593 | /* Parameter check and block job starting for drive mirroring. |
3594 | * Caller should hold @device and @target's aio context (must be the same). | |
3595 | **/ | |
71aa9867 | 3596 | static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, |
4193cdd7 FZ |
3597 | BlockDriverState *target, |
3598 | bool has_replaces, const char *replaces, | |
3599 | enum MirrorSyncMode sync, | |
274fccee | 3600 | BlockMirrorBackingMode backing_mode, |
4193cdd7 FZ |
3601 | bool has_speed, int64_t speed, |
3602 | bool has_granularity, uint32_t granularity, | |
3603 | bool has_buf_size, int64_t buf_size, | |
3604 | bool has_on_source_error, | |
3605 | BlockdevOnError on_source_error, | |
3606 | bool has_on_target_error, | |
3607 | BlockdevOnError on_target_error, | |
3608 | bool has_unmap, bool unmap, | |
6cdbceb1 KW |
3609 | bool has_filter_node_name, |
3610 | const char *filter_node_name, | |
481debaa | 3611 | bool has_copy_mode, MirrorCopyMode copy_mode, |
4193cdd7 | 3612 | Error **errp) |
d9b902db | 3613 | { |
d9b902db PB |
3614 | |
3615 | if (!has_speed) { | |
3616 | speed = 0; | |
3617 | } | |
b952b558 PB |
3618 | if (!has_on_source_error) { |
3619 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3620 | } | |
3621 | if (!has_on_target_error) { | |
3622 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3623 | } | |
eee13dfe PB |
3624 | if (!has_granularity) { |
3625 | granularity = 0; | |
3626 | } | |
08e4ed6c | 3627 | if (!has_buf_size) { |
48ac0a4d | 3628 | buf_size = 0; |
08e4ed6c | 3629 | } |
0fc9f8ea FZ |
3630 | if (!has_unmap) { |
3631 | unmap = true; | |
3632 | } | |
6cdbceb1 KW |
3633 | if (!has_filter_node_name) { |
3634 | filter_node_name = NULL; | |
3635 | } | |
481debaa HR |
3636 | if (!has_copy_mode) { |
3637 | copy_mode = MIRROR_COPY_MODE_BACKGROUND; | |
3638 | } | |
08e4ed6c | 3639 | |
eee13dfe | 3640 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
c6bd8c70 MA |
3641 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
3642 | "a value in range [512B, 64MB]"); | |
eee13dfe PB |
3643 | return; |
3644 | } | |
3645 | if (granularity & (granularity - 1)) { | |
c6bd8c70 MA |
3646 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
3647 | "power of 2"); | |
eee13dfe PB |
3648 | return; |
3649 | } | |
d9b902db | 3650 | |
4193cdd7 FZ |
3651 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { |
3652 | return; | |
3653 | } | |
e40e5027 FZ |
3654 | if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { |
3655 | return; | |
3656 | } | |
4193cdd7 FZ |
3657 | |
3658 | if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { | |
3659 | sync = MIRROR_SYNC_MODE_FULL; | |
3660 | } | |
3661 | ||
3662 | /* pass the node name to replace to mirror start since it's loose coupling | |
3663 | * and will allow to check whether the node still exist at mirror completion | |
3664 | */ | |
71aa9867 | 3665 | mirror_start(job_id, bs, target, |
4193cdd7 | 3666 | has_replaces ? replaces : NULL, |
274fccee | 3667 | speed, granularity, buf_size, sync, backing_mode, |
6cdbceb1 | 3668 | on_source_error, on_target_error, unmap, filter_node_name, |
481debaa | 3669 | copy_mode, errp); |
4193cdd7 FZ |
3670 | } |
3671 | ||
faecd40a | 3672 | void qmp_drive_mirror(DriveMirror *arg, Error **errp) |
4193cdd7 FZ |
3673 | { |
3674 | BlockDriverState *bs; | |
4193cdd7 FZ |
3675 | BlockDriverState *source, *target_bs; |
3676 | AioContext *aio_context; | |
274fccee | 3677 | BlockMirrorBackingMode backing_mode; |
4193cdd7 FZ |
3678 | Error *local_err = NULL; |
3679 | QDict *options = NULL; | |
3680 | int flags; | |
3681 | int64_t size; | |
faecd40a | 3682 | const char *format = arg->format; |
4193cdd7 | 3683 | |
0524e93a KW |
3684 | bs = qmp_get_root_bs(arg->device, errp); |
3685 | if (!bs) { | |
d9b902db PB |
3686 | return; |
3687 | } | |
3688 | ||
cb2af917 PB |
3689 | /* Early check to avoid creating target */ |
3690 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { | |
3691 | return; | |
3692 | } | |
3693 | ||
0524e93a | 3694 | aio_context = bdrv_get_aio_context(bs); |
5a7e7a0b SH |
3695 | aio_context_acquire(aio_context); |
3696 | ||
faecd40a EB |
3697 | if (!arg->has_mode) { |
3698 | arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
4193cdd7 | 3699 | } |
d9b902db | 3700 | |
faecd40a EB |
3701 | if (!arg->has_format) { |
3702 | format = (arg->mode == NEW_IMAGE_MODE_EXISTING | |
3703 | ? NULL : bs->drv->format_name); | |
d9b902db | 3704 | } |
d9b902db | 3705 | |
61de4c68 | 3706 | flags = bs->open_flags | BDRV_O_RDWR; |
760e0063 | 3707 | source = backing_bs(bs); |
faecd40a EB |
3708 | if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { |
3709 | arg->sync = MIRROR_SYNC_MODE_FULL; | |
d9b902db | 3710 | } |
faecd40a | 3711 | if (arg->sync == MIRROR_SYNC_MODE_NONE) { |
117e0c82 HR |
3712 | source = bs; |
3713 | } | |
d9b902db | 3714 | |
ac3c5d83 SH |
3715 | size = bdrv_getlength(bs); |
3716 | if (size < 0) { | |
3717 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
5a7e7a0b | 3718 | goto out; |
ac3c5d83 SH |
3719 | } |
3720 | ||
faecd40a | 3721 | if (arg->has_replaces) { |
09158f00 | 3722 | BlockDriverState *to_replace_bs; |
5a7e7a0b SH |
3723 | AioContext *replace_aio_context; |
3724 | int64_t replace_size; | |
09158f00 | 3725 | |
faecd40a | 3726 | if (!arg->has_node_name) { |
09158f00 BC |
3727 | error_setg(errp, "a node-name must be provided when replacing a" |
3728 | " named node of the graph"); | |
5a7e7a0b | 3729 | goto out; |
09158f00 BC |
3730 | } |
3731 | ||
faecd40a | 3732 | to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); |
09158f00 BC |
3733 | |
3734 | if (!to_replace_bs) { | |
3735 | error_propagate(errp, local_err); | |
5a7e7a0b | 3736 | goto out; |
09158f00 BC |
3737 | } |
3738 | ||
5a7e7a0b SH |
3739 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); |
3740 | aio_context_acquire(replace_aio_context); | |
3741 | replace_size = bdrv_getlength(to_replace_bs); | |
3742 | aio_context_release(replace_aio_context); | |
3743 | ||
3744 | if (size != replace_size) { | |
09158f00 BC |
3745 | error_setg(errp, "cannot replace image with a mirror image of " |
3746 | "different size"); | |
5a7e7a0b | 3747 | goto out; |
09158f00 BC |
3748 | } |
3749 | } | |
3750 | ||
faecd40a | 3751 | if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { |
274fccee HR |
3752 | backing_mode = MIRROR_SOURCE_BACKING_CHAIN; |
3753 | } else { | |
3754 | backing_mode = MIRROR_OPEN_BACKING_CHAIN; | |
3755 | } | |
3756 | ||
2a32c6e8 JS |
3757 | /* Don't open backing image in create() */ |
3758 | flags |= BDRV_O_NO_BACKING; | |
3759 | ||
faecd40a EB |
3760 | if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) |
3761 | && arg->mode != NEW_IMAGE_MODE_EXISTING) | |
14526864 | 3762 | { |
d9b902db | 3763 | /* create new image w/o backing file */ |
e6641719 | 3764 | assert(format); |
faecd40a | 3765 | bdrv_img_create(arg->target, format, |
9217283d | 3766 | NULL, NULL, NULL, size, flags, false, &local_err); |
d9b902db | 3767 | } else { |
faecd40a | 3768 | switch (arg->mode) { |
d9b902db | 3769 | case NEW_IMAGE_MODE_EXISTING: |
d9b902db PB |
3770 | break; |
3771 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
3772 | /* create new image with backing file */ | |
faecd40a | 3773 | bdrv_img_create(arg->target, format, |
cf8f2426 LC |
3774 | source->filename, |
3775 | source->drv->format_name, | |
9217283d | 3776 | NULL, size, flags, false, &local_err); |
d9b902db PB |
3777 | break; |
3778 | default: | |
3779 | abort(); | |
3780 | } | |
3781 | } | |
3782 | ||
84d18f06 | 3783 | if (local_err) { |
cf8f2426 | 3784 | error_propagate(errp, local_err); |
5a7e7a0b | 3785 | goto out; |
d9b902db PB |
3786 | } |
3787 | ||
e6641719 | 3788 | options = qdict_new(); |
faecd40a | 3789 | if (arg->has_node_name) { |
46f5ac20 | 3790 | qdict_put_str(options, "node-name", arg->node_name); |
4c828dc6 | 3791 | } |
e6641719 | 3792 | if (format) { |
46f5ac20 | 3793 | qdict_put_str(options, "driver", format); |
e6641719 | 3794 | } |
4c828dc6 | 3795 | |
b812f671 PB |
3796 | /* Mirroring takes care of copy-on-write using the source's backing |
3797 | * file. | |
3798 | */ | |
2a32c6e8 | 3799 | target_bs = bdrv_open(arg->target, NULL, options, flags, errp); |
5b363937 | 3800 | if (!target_bs) { |
5a7e7a0b | 3801 | goto out; |
d9b902db PB |
3802 | } |
3803 | ||
5a7e7a0b SH |
3804 | bdrv_set_aio_context(target_bs, aio_context); |
3805 | ||
faecd40a EB |
3806 | blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, |
3807 | arg->has_replaces, arg->replaces, arg->sync, | |
3808 | backing_mode, arg->has_speed, arg->speed, | |
3809 | arg->has_granularity, arg->granularity, | |
3810 | arg->has_buf_size, arg->buf_size, | |
3811 | arg->has_on_source_error, arg->on_source_error, | |
3812 | arg->has_on_target_error, arg->on_target_error, | |
3813 | arg->has_unmap, arg->unmap, | |
6cdbceb1 | 3814 | false, NULL, |
481debaa | 3815 | arg->has_copy_mode, arg->copy_mode, |
4193cdd7 | 3816 | &local_err); |
e253f4b8 | 3817 | bdrv_unref(target_bs); |
621ff94d | 3818 | error_propagate(errp, local_err); |
5a7e7a0b SH |
3819 | out: |
3820 | aio_context_release(aio_context); | |
d9b902db PB |
3821 | } |
3822 | ||
71aa9867 AG |
3823 | void qmp_blockdev_mirror(bool has_job_id, const char *job_id, |
3824 | const char *device, const char *target, | |
df92562e FZ |
3825 | bool has_replaces, const char *replaces, |
3826 | MirrorSyncMode sync, | |
3827 | bool has_speed, int64_t speed, | |
3828 | bool has_granularity, uint32_t granularity, | |
3829 | bool has_buf_size, int64_t buf_size, | |
3830 | bool has_on_source_error, | |
3831 | BlockdevOnError on_source_error, | |
3832 | bool has_on_target_error, | |
3833 | BlockdevOnError on_target_error, | |
6cdbceb1 KW |
3834 | bool has_filter_node_name, |
3835 | const char *filter_node_name, | |
481debaa | 3836 | bool has_copy_mode, MirrorCopyMode copy_mode, |
df92562e FZ |
3837 | Error **errp) |
3838 | { | |
3839 | BlockDriverState *bs; | |
df92562e FZ |
3840 | BlockDriverState *target_bs; |
3841 | AioContext *aio_context; | |
274fccee | 3842 | BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; |
df92562e FZ |
3843 | Error *local_err = NULL; |
3844 | ||
07eec652 | 3845 | bs = qmp_get_root_bs(device, errp); |
df92562e | 3846 | if (!bs) { |
df92562e FZ |
3847 | return; |
3848 | } | |
3849 | ||
3850 | target_bs = bdrv_lookup_bs(target, target, errp); | |
3851 | if (!target_bs) { | |
3852 | return; | |
3853 | } | |
3854 | ||
3855 | aio_context = bdrv_get_aio_context(bs); | |
3856 | aio_context_acquire(aio_context); | |
3857 | ||
df92562e FZ |
3858 | bdrv_set_aio_context(target_bs, aio_context); |
3859 | ||
71aa9867 | 3860 | blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, |
274fccee | 3861 | has_replaces, replaces, sync, backing_mode, |
df92562e FZ |
3862 | has_speed, speed, |
3863 | has_granularity, granularity, | |
3864 | has_buf_size, buf_size, | |
3865 | has_on_source_error, on_source_error, | |
3866 | has_on_target_error, on_target_error, | |
3867 | true, true, | |
6cdbceb1 | 3868 | has_filter_node_name, filter_node_name, |
481debaa | 3869 | has_copy_mode, copy_mode, |
df92562e | 3870 | &local_err); |
621ff94d | 3871 | error_propagate(errp, local_err); |
df92562e FZ |
3872 | |
3873 | aio_context_release(aio_context); | |
3874 | } | |
3875 | ||
3ddf3efe AG |
3876 | /* Get a block job using its ID and acquire its AioContext */ |
3877 | static BlockJob *find_block_job(const char *id, AioContext **aio_context, | |
24d6bffe | 3878 | Error **errp) |
2d47c6e9 | 3879 | { |
3ddf3efe | 3880 | BlockJob *job; |
2d47c6e9 | 3881 | |
3ddf3efe | 3882 | assert(id != NULL); |
5433c24f | 3883 | |
3ddf3efe | 3884 | *aio_context = NULL; |
3d948cdf | 3885 | |
3ddf3efe | 3886 | job = block_job_get(id); |
5433c24f | 3887 | |
3ddf3efe AG |
3888 | if (!job) { |
3889 | error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, | |
3890 | "Block job '%s' not found", id); | |
3891 | return NULL; | |
2d47c6e9 | 3892 | } |
3d948cdf | 3893 | |
3ddf3efe AG |
3894 | *aio_context = blk_get_aio_context(job->blk); |
3895 | aio_context_acquire(*aio_context); | |
3d948cdf | 3896 | |
3ddf3efe | 3897 | return job; |
2d47c6e9 SH |
3898 | } |
3899 | ||
882ec7ce | 3900 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 | 3901 | { |
3d948cdf | 3902 | AioContext *aio_context; |
24d6bffe | 3903 | BlockJob *job = find_block_job(device, &aio_context, errp); |
2d47c6e9 SH |
3904 | |
3905 | if (!job) { | |
2d47c6e9 SH |
3906 | return; |
3907 | } | |
3908 | ||
882ec7ce | 3909 | block_job_set_speed(job, speed, errp); |
3d948cdf | 3910 | aio_context_release(aio_context); |
2d47c6e9 | 3911 | } |
370521a1 | 3912 | |
6e37fb81 PB |
3913 | void qmp_block_job_cancel(const char *device, |
3914 | bool has_force, bool force, Error **errp) | |
370521a1 | 3915 | { |
3d948cdf | 3916 | AioContext *aio_context; |
24d6bffe | 3917 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3918 | |
370521a1 | 3919 | if (!job) { |
370521a1 SH |
3920 | return; |
3921 | } | |
3d948cdf SH |
3922 | |
3923 | if (!has_force) { | |
3924 | force = false; | |
3925 | } | |
3926 | ||
b15de828 | 3927 | if (job_user_paused(&job->job) && !force) { |
f231b88d CR |
3928 | error_setg(errp, "The block job for device '%s' is currently paused", |
3929 | device); | |
3d948cdf | 3930 | goto out; |
8acc72a4 | 3931 | } |
370521a1 SH |
3932 | |
3933 | trace_qmp_block_job_cancel(job); | |
3d70ff53 | 3934 | job_user_cancel(&job->job, force, errp); |
3d948cdf SH |
3935 | out: |
3936 | aio_context_release(aio_context); | |
370521a1 | 3937 | } |
fb5458cd | 3938 | |
6e37fb81 PB |
3939 | void qmp_block_job_pause(const char *device, Error **errp) |
3940 | { | |
3d948cdf | 3941 | AioContext *aio_context; |
24d6bffe | 3942 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3943 | |
0ec4dfb8 | 3944 | if (!job) { |
6e37fb81 PB |
3945 | return; |
3946 | } | |
3947 | ||
3948 | trace_qmp_block_job_pause(job); | |
b15de828 | 3949 | job_user_pause(&job->job, errp); |
3d948cdf | 3950 | aio_context_release(aio_context); |
6e37fb81 PB |
3951 | } |
3952 | ||
3953 | void qmp_block_job_resume(const char *device, Error **errp) | |
3954 | { | |
3d948cdf | 3955 | AioContext *aio_context; |
24d6bffe | 3956 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3957 | |
0ec4dfb8 | 3958 | if (!job) { |
6e37fb81 PB |
3959 | return; |
3960 | } | |
3961 | ||
3962 | trace_qmp_block_job_resume(job); | |
b15de828 | 3963 | job_user_resume(&job->job, errp); |
3d948cdf | 3964 | aio_context_release(aio_context); |
6e37fb81 PB |
3965 | } |
3966 | ||
aeae883b PB |
3967 | void qmp_block_job_complete(const char *device, Error **errp) |
3968 | { | |
3d948cdf | 3969 | AioContext *aio_context; |
24d6bffe | 3970 | BlockJob *job = find_block_job(device, &aio_context, errp); |
aeae883b PB |
3971 | |
3972 | if (!job) { | |
aeae883b PB |
3973 | return; |
3974 | } | |
3975 | ||
3976 | trace_qmp_block_job_complete(job); | |
3453d972 | 3977 | job_complete(&job->job, errp); |
3d948cdf | 3978 | aio_context_release(aio_context); |
aeae883b PB |
3979 | } |
3980 | ||
11b61fbc JS |
3981 | void qmp_block_job_finalize(const char *id, Error **errp) |
3982 | { | |
3983 | AioContext *aio_context; | |
3984 | BlockJob *job = find_block_job(id, &aio_context, errp); | |
3985 | ||
3986 | if (!job) { | |
3987 | return; | |
3988 | } | |
3989 | ||
3990 | trace_qmp_block_job_finalize(job); | |
7eaa8fb5 | 3991 | job_finalize(&job->job, errp); |
11b61fbc JS |
3992 | aio_context_release(aio_context); |
3993 | } | |
3994 | ||
75f71059 JS |
3995 | void qmp_block_job_dismiss(const char *id, Error **errp) |
3996 | { | |
3997 | AioContext *aio_context; | |
5f9a6a08 KW |
3998 | BlockJob *bjob = find_block_job(id, &aio_context, errp); |
3999 | Job *job; | |
75f71059 | 4000 | |
5f9a6a08 | 4001 | if (!bjob) { |
75f71059 JS |
4002 | return; |
4003 | } | |
4004 | ||
5f9a6a08 KW |
4005 | trace_qmp_block_job_dismiss(bjob); |
4006 | job = &bjob->job; | |
4007 | job_dismiss(&job, errp); | |
75f71059 JS |
4008 | aio_context_release(aio_context); |
4009 | } | |
4010 | ||
fa40e656 JC |
4011 | void qmp_change_backing_file(const char *device, |
4012 | const char *image_node_name, | |
4013 | const char *backing_file, | |
4014 | Error **errp) | |
4015 | { | |
4016 | BlockDriverState *bs = NULL; | |
729962f6 | 4017 | AioContext *aio_context; |
fa40e656 JC |
4018 | BlockDriverState *image_bs = NULL; |
4019 | Error *local_err = NULL; | |
4020 | bool ro; | |
4021 | int open_flags; | |
4022 | int ret; | |
4023 | ||
7b5dca3f KW |
4024 | bs = qmp_get_root_bs(device, errp); |
4025 | if (!bs) { | |
fa40e656 JC |
4026 | return; |
4027 | } | |
4028 | ||
7b5dca3f | 4029 | aio_context = bdrv_get_aio_context(bs); |
729962f6 SH |
4030 | aio_context_acquire(aio_context); |
4031 | ||
fa40e656 JC |
4032 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); |
4033 | if (local_err) { | |
4034 | error_propagate(errp, local_err); | |
729962f6 | 4035 | goto out; |
fa40e656 JC |
4036 | } |
4037 | ||
4038 | if (!image_bs) { | |
4039 | error_setg(errp, "image file not found"); | |
729962f6 | 4040 | goto out; |
fa40e656 JC |
4041 | } |
4042 | ||
4043 | if (bdrv_find_base(image_bs) == image_bs) { | |
4044 | error_setg(errp, "not allowing backing file change on an image " | |
4045 | "without a backing file"); | |
729962f6 | 4046 | goto out; |
fa40e656 JC |
4047 | } |
4048 | ||
4049 | /* even though we are not necessarily operating on bs, we need it to | |
4050 | * determine if block ops are currently prohibited on the chain */ | |
4051 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
729962f6 | 4052 | goto out; |
fa40e656 JC |
4053 | } |
4054 | ||
4055 | /* final sanity check */ | |
4056 | if (!bdrv_chain_contains(bs, image_bs)) { | |
4057 | error_setg(errp, "'%s' and image file are not in the same chain", | |
4058 | device); | |
729962f6 | 4059 | goto out; |
fa40e656 JC |
4060 | } |
4061 | ||
4062 | /* if not r/w, reopen to make r/w */ | |
4063 | open_flags = image_bs->open_flags; | |
4064 | ro = bdrv_is_read_only(image_bs); | |
4065 | ||
4066 | if (ro) { | |
4067 | bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); | |
4068 | if (local_err) { | |
4069 | error_propagate(errp, local_err); | |
729962f6 | 4070 | goto out; |
fa40e656 JC |
4071 | } |
4072 | } | |
4073 | ||
4074 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
4075 | image_bs->drv ? image_bs->drv->format_name : ""); | |
4076 | ||
4077 | if (ret < 0) { | |
4078 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
4079 | backing_file); | |
4080 | /* don't exit here, so we can try to restore open flags if | |
4081 | * appropriate */ | |
4082 | } | |
4083 | ||
4084 | if (ro) { | |
4085 | bdrv_reopen(image_bs, open_flags, &local_err); | |
621ff94d | 4086 | error_propagate(errp, local_err); |
fa40e656 | 4087 | } |
729962f6 SH |
4088 | |
4089 | out: | |
4090 | aio_context_release(aio_context); | |
fa40e656 JC |
4091 | } |
4092 | ||
abb21ac3 KW |
4093 | void hmp_drive_add_node(Monitor *mon, const char *optstr) |
4094 | { | |
4095 | QemuOpts *opts; | |
4096 | QDict *qdict; | |
4097 | Error *local_err = NULL; | |
4098 | ||
4099 | opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); | |
4100 | if (!opts) { | |
4101 | return; | |
4102 | } | |
4103 | ||
4104 | qdict = qemu_opts_to_qdict(opts, NULL); | |
4105 | ||
4106 | if (!qdict_get_try_str(qdict, "node-name")) { | |
cb3e7f08 | 4107 | qobject_unref(qdict); |
abb21ac3 KW |
4108 | error_report("'node-name' needs to be specified"); |
4109 | goto out; | |
4110 | } | |
4111 | ||
4112 | BlockDriverState *bs = bds_tree_init(qdict, &local_err); | |
4113 | if (!bs) { | |
4114 | error_report_err(local_err); | |
4115 | goto out; | |
4116 | } | |
4117 | ||
4118 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); | |
4119 | ||
4120 | out: | |
4121 | qemu_opts_del(opts); | |
4122 | } | |
4123 | ||
d26c9a15 KW |
4124 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
4125 | { | |
be4b67bc | 4126 | BlockDriverState *bs; |
d26c9a15 | 4127 | QObject *obj; |
7d5e199a | 4128 | Visitor *v = qobject_output_visitor_new(&obj); |
d26c9a15 | 4129 | QDict *qdict; |
d26c9a15 KW |
4130 | Error *local_err = NULL; |
4131 | ||
3b098d56 | 4132 | visit_type_BlockdevOptions(v, NULL, &options, &local_err); |
84d18f06 | 4133 | if (local_err) { |
d26c9a15 KW |
4134 | error_propagate(errp, local_err); |
4135 | goto fail; | |
4136 | } | |
4137 | ||
3b098d56 | 4138 | visit_complete(v, &obj); |
7dc847eb | 4139 | qdict = qobject_to(QDict, obj); |
d26c9a15 KW |
4140 | |
4141 | qdict_flatten(qdict); | |
4142 | ||
9ec8873e KW |
4143 | if (!qdict_get_try_str(qdict, "node-name")) { |
4144 | error_setg(errp, "'node-name' must be specified for the root node"); | |
4145 | goto fail; | |
4146 | } | |
9c4218e9 | 4147 | |
9ec8873e KW |
4148 | bs = bds_tree_init(qdict, errp); |
4149 | if (!bs) { | |
4150 | goto fail; | |
d26c9a15 KW |
4151 | } |
4152 | ||
9ec8873e KW |
4153 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); |
4154 | ||
d26c9a15 | 4155 | fail: |
3b098d56 | 4156 | visit_free(v); |
d26c9a15 KW |
4157 | } |
4158 | ||
79b7a77e | 4159 | void qmp_blockdev_del(const char *node_name, Error **errp) |
81b936ae AG |
4160 | { |
4161 | AioContext *aio_context; | |
81b936ae AG |
4162 | BlockDriverState *bs; |
4163 | ||
9ec8873e KW |
4164 | bs = bdrv_find_node(node_name); |
4165 | if (!bs) { | |
4166 | error_setg(errp, "Cannot find node %s", node_name); | |
81b936ae AG |
4167 | return; |
4168 | } | |
9ec8873e KW |
4169 | if (bdrv_has_blk(bs)) { |
4170 | error_setg(errp, "Node %s is in use", node_name); | |
4171 | return; | |
81b936ae | 4172 | } |
9ec8873e | 4173 | aio_context = bdrv_get_aio_context(bs); |
81b936ae AG |
4174 | aio_context_acquire(aio_context); |
4175 | ||
9ec8873e KW |
4176 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { |
4177 | goto out; | |
4178 | } | |
9c4218e9 | 4179 | |
9ec8873e KW |
4180 | if (!bs->monitor_list.tqe_prev) { |
4181 | error_setg(errp, "Node %s is not owned by the monitor", | |
4182 | bs->node_name); | |
4183 | goto out; | |
81b936ae AG |
4184 | } |
4185 | ||
9ec8873e KW |
4186 | if (bs->refcnt > 1) { |
4187 | error_setg(errp, "Block device %s is in use", | |
4188 | bdrv_get_device_or_node_name(bs)); | |
4189 | goto out; | |
81b936ae AG |
4190 | } |
4191 | ||
9ec8873e KW |
4192 | QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); |
4193 | bdrv_unref(bs); | |
4194 | ||
81b936ae AG |
4195 | out: |
4196 | aio_context_release(aio_context); | |
4197 | } | |
4198 | ||
7f821597 WC |
4199 | static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, |
4200 | const char *child_name) | |
4201 | { | |
4202 | BdrvChild *child; | |
4203 | ||
4204 | QLIST_FOREACH(child, &parent_bs->children, next) { | |
4205 | if (strcmp(child->name, child_name) == 0) { | |
4206 | return child; | |
4207 | } | |
4208 | } | |
4209 | ||
4210 | return NULL; | |
4211 | } | |
4212 | ||
4213 | void qmp_x_blockdev_change(const char *parent, bool has_child, | |
4214 | const char *child, bool has_node, | |
4215 | const char *node, Error **errp) | |
4216 | { | |
4217 | BlockDriverState *parent_bs, *new_bs = NULL; | |
4218 | BdrvChild *p_child; | |
4219 | ||
4220 | parent_bs = bdrv_lookup_bs(parent, parent, errp); | |
4221 | if (!parent_bs) { | |
4222 | return; | |
4223 | } | |
4224 | ||
4225 | if (has_child == has_node) { | |
4226 | if (has_child) { | |
4227 | error_setg(errp, "The parameters child and node are in conflict"); | |
4228 | } else { | |
4229 | error_setg(errp, "Either child or node must be specified"); | |
4230 | } | |
4231 | return; | |
4232 | } | |
4233 | ||
4234 | if (has_child) { | |
4235 | p_child = bdrv_find_child(parent_bs, child); | |
4236 | if (!p_child) { | |
4237 | error_setg(errp, "Node '%s' does not have child '%s'", | |
4238 | parent, child); | |
4239 | return; | |
4240 | } | |
4241 | bdrv_del_child(parent_bs, p_child, errp); | |
4242 | } | |
4243 | ||
4244 | if (has_node) { | |
4245 | new_bs = bdrv_find_node(node); | |
4246 | if (!new_bs) { | |
4247 | error_setg(errp, "Node '%s' not found", node); | |
4248 | return; | |
4249 | } | |
4250 | bdrv_add_child(parent_bs, new_bs, errp); | |
4251 | } | |
4252 | } | |
4253 | ||
fea68bb6 | 4254 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
fb5458cd | 4255 | { |
fea68bb6 | 4256 | BlockJobInfoList *head = NULL, **p_next = &head; |
f0f55ded | 4257 | BlockJob *job; |
fb5458cd | 4258 | |
f0f55ded | 4259 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { |
559b935f JS |
4260 | BlockJobInfoList *elem; |
4261 | AioContext *aio_context; | |
69691e72 | 4262 | |
559b935f JS |
4263 | if (block_job_is_internal(job)) { |
4264 | continue; | |
4265 | } | |
4266 | elem = g_new0(BlockJobInfoList, 1); | |
4267 | aio_context = blk_get_aio_context(job->blk); | |
69691e72 | 4268 | aio_context_acquire(aio_context); |
559b935f | 4269 | elem->value = block_job_query(job, errp); |
69691e72 | 4270 | aio_context_release(aio_context); |
559b935f JS |
4271 | if (!elem->value) { |
4272 | g_free(elem); | |
4273 | qapi_free_BlockJobInfoList(head); | |
4274 | return NULL; | |
4275 | } | |
f0f55ded AG |
4276 | *p_next = elem; |
4277 | p_next = &elem->next; | |
fb5458cd | 4278 | } |
fb5458cd | 4279 | |
fea68bb6 | 4280 | return head; |
fb5458cd | 4281 | } |
4d454574 | 4282 | |
ca00bbb1 | 4283 | void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, |
882e9b89 | 4284 | bool has_force, bool force, Error **errp) |
ca00bbb1 SH |
4285 | { |
4286 | AioContext *old_context; | |
4287 | AioContext *new_context; | |
4288 | BlockDriverState *bs; | |
4289 | ||
4290 | bs = bdrv_find_node(node_name); | |
4291 | if (!bs) { | |
4292 | error_setg(errp, "Cannot find node %s", node_name); | |
4293 | return; | |
4294 | } | |
4295 | ||
882e9b89 SH |
4296 | /* Protects against accidents. */ |
4297 | if (!(has_force && force) && bdrv_has_blk(bs)) { | |
4298 | error_setg(errp, "Node %s is associated with a BlockBackend and could " | |
4299 | "be in use (use force=true to override this check)", | |
4300 | node_name); | |
ca00bbb1 SH |
4301 | return; |
4302 | } | |
4303 | ||
4304 | if (iothread->type == QTYPE_QSTRING) { | |
4305 | IOThread *obj = iothread_by_id(iothread->u.s); | |
4306 | if (!obj) { | |
4307 | error_setg(errp, "Cannot find iothread %s", iothread->u.s); | |
4308 | return; | |
4309 | } | |
4310 | ||
4311 | new_context = iothread_get_aio_context(obj); | |
4312 | } else { | |
4313 | new_context = qemu_get_aio_context(); | |
4314 | } | |
4315 | ||
4316 | old_context = bdrv_get_aio_context(bs); | |
4317 | aio_context_acquire(old_context); | |
4318 | ||
4319 | bdrv_set_aio_context(bs, new_context); | |
4320 | ||
4321 | aio_context_release(old_context); | |
4322 | } | |
4323 | ||
7e5c776d VSO |
4324 | void qmp_x_block_latency_histogram_set( |
4325 | const char *device, | |
4326 | bool has_boundaries, uint64List *boundaries, | |
4327 | bool has_boundaries_read, uint64List *boundaries_read, | |
4328 | bool has_boundaries_write, uint64List *boundaries_write, | |
4329 | bool has_boundaries_flush, uint64List *boundaries_flush, | |
4330 | Error **errp) | |
4331 | { | |
4332 | BlockBackend *blk = blk_by_name(device); | |
4333 | BlockAcctStats *stats; | |
4334 | ||
4335 | if (!blk) { | |
4336 | error_setg(errp, "Device '%s' not found", device); | |
4337 | return; | |
4338 | } | |
4339 | stats = blk_get_stats(blk); | |
4340 | ||
4341 | if (!has_boundaries && !has_boundaries_read && !has_boundaries_write && | |
4342 | !has_boundaries_flush) | |
4343 | { | |
4344 | block_latency_histograms_clear(stats); | |
4345 | return; | |
4346 | } | |
4347 | ||
4348 | if (has_boundaries || has_boundaries_read) { | |
4349 | block_latency_histogram_set( | |
4350 | stats, BLOCK_ACCT_READ, | |
4351 | has_boundaries_read ? boundaries_read : boundaries); | |
4352 | } | |
4353 | ||
4354 | if (has_boundaries || has_boundaries_write) { | |
4355 | block_latency_histogram_set( | |
4356 | stats, BLOCK_ACCT_WRITE, | |
4357 | has_boundaries_write ? boundaries_write : boundaries); | |
4358 | } | |
4359 | ||
4360 | if (has_boundaries || has_boundaries_flush) { | |
4361 | block_latency_histogram_set( | |
4362 | stats, BLOCK_ACCT_FLUSH, | |
4363 | has_boundaries_flush ? boundaries_flush : boundaries); | |
4364 | } | |
4365 | } | |
4366 | ||
0006383e | 4367 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 4368 | .name = "drive", |
0006383e | 4369 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
4370 | .desc = { |
4371 | { | |
4d454574 PB |
4372 | .name = "snapshot", |
4373 | .type = QEMU_OPT_BOOL, | |
4374 | .help = "enable/disable snapshot mode", | |
4d454574 PB |
4375 | },{ |
4376 | .name = "aio", | |
4377 | .type = QEMU_OPT_STRING, | |
4378 | .help = "host AIO implementation (threads, native)", | |
e4b24b49 KW |
4379 | },{ |
4380 | .name = BDRV_OPT_CACHE_WB, | |
4381 | .type = QEMU_OPT_BOOL, | |
4382 | .help = "Enable writeback mode", | |
4d454574 PB |
4383 | },{ |
4384 | .name = "format", | |
4385 | .type = QEMU_OPT_STRING, | |
4386 | .help = "disk format (raw, qcow2, ...)", | |
4d454574 PB |
4387 | },{ |
4388 | .name = "rerror", | |
4389 | .type = QEMU_OPT_STRING, | |
4390 | .help = "read error action", | |
4391 | },{ | |
4392 | .name = "werror", | |
4393 | .type = QEMU_OPT_STRING, | |
4394 | .help = "write error action", | |
4d454574 | 4395 | },{ |
4e200cf8 | 4396 | .name = BDRV_OPT_READ_ONLY, |
4d454574 PB |
4397 | .type = QEMU_OPT_BOOL, |
4398 | .help = "open drive file as read-only", | |
a2a7862c PJ |
4399 | }, |
4400 | ||
4401 | THROTTLE_OPTS, | |
4402 | ||
4403 | { | |
76f4afb4 AG |
4404 | .name = "throttling.group", |
4405 | .type = QEMU_OPT_STRING, | |
4406 | .help = "name of the block throttling group", | |
4d454574 PB |
4407 | },{ |
4408 | .name = "copy-on-read", | |
4409 | .type = QEMU_OPT_BOOL, | |
4410 | .help = "copy read data from backing file into image file", | |
465bee1d PL |
4411 | },{ |
4412 | .name = "detect-zeroes", | |
4413 | .type = QEMU_OPT_STRING, | |
4414 | .help = "try to optimize zero writes (off, on, unmap)", | |
362e9299 AG |
4415 | },{ |
4416 | .name = "stats-account-invalid", | |
4417 | .type = QEMU_OPT_BOOL, | |
4418 | .help = "whether to account for invalid I/O operations " | |
4419 | "in the statistics", | |
4420 | },{ | |
4421 | .name = "stats-account-failed", | |
4422 | .type = QEMU_OPT_BOOL, | |
4423 | .help = "whether to account for failed I/O operations " | |
4424 | "in the statistics", | |
4d454574 PB |
4425 | }, |
4426 | { /* end of list */ } | |
4427 | }, | |
4428 | }; | |
0006383e KW |
4429 | |
4430 | QemuOptsList qemu_drive_opts = { | |
4431 | .name = "drive", | |
4432 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
4433 | .desc = { | |
492fdc6f KW |
4434 | /* |
4435 | * no elements => accept any params | |
4436 | * validation will happen later | |
4437 | */ | |
0006383e KW |
4438 | { /* end of list */ } |
4439 | }, | |
4440 | }; |