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