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