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