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