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