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