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