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