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