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