]>
Commit | Line | Data |
---|---|---|
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. | |
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. | |
31 | */ | |
32 | ||
33 | #include "qemu/osdep.h" | |
34 | #include "sysemu/block-backend.h" | |
35 | #include "sysemu/blockdev.h" | |
36 | #include "hw/block/block.h" | |
37 | #include "block/blockjob.h" | |
38 | #include "block/throttle-groups.h" | |
39 | #include "monitor/monitor.h" | |
40 | #include "qemu/error-report.h" | |
41 | #include "qemu/option.h" | |
42 | #include "qemu/config-file.h" | |
43 | #include "qapi/qmp/types.h" | |
44 | #include "qapi-visit.h" | |
45 | #include "qapi/qmp/qerror.h" | |
46 | #include "qapi/qobject-output-visitor.h" | |
47 | #include "qapi/util.h" | |
48 | #include "sysemu/sysemu.h" | |
49 | #include "block/block_int.h" | |
50 | #include "qmp-commands.h" | |
51 | #include "block/trace.h" | |
52 | #include "sysemu/arch_init.h" | |
53 | #include "sysemu/qtest.h" | |
54 | #include "qemu/cutils.h" | |
55 | #include "qemu/help_option.h" | |
56 | #include "qemu/throttle-options.h" | |
57 | ||
58 | static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = | |
59 | QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); | |
60 | ||
61 | static int do_open_tray(const char *blk_name, const char *qdev_id, | |
62 | bool force, Error **errp); | |
63 | ||
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 | ||
76 | static int if_max_devs[IF_COUNT] = { | |
77 | /* | |
78 | * Do not change these numbers! They govern how drive option | |
79 | * index maps to unit and bus. That mapping is ABI. | |
80 | * | |
81 | * All controllers used to implement if=T drives need to support | |
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, | |
93 | }; | |
94 | ||
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 | { | |
101 | BlockBackend *blk; | |
102 | DriveInfo *dinfo; | |
103 | ||
104 | if (max_devs <= 0) { | |
105 | return; | |
106 | } | |
107 | ||
108 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { | |
109 | dinfo = blk_legacy_dinfo(blk); | |
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 | ||
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 | */ | |
128 | void blockdev_mark_auto_del(BlockBackend *blk) | |
129 | { | |
130 | DriveInfo *dinfo = blk_legacy_dinfo(blk); | |
131 | BlockDriverState *bs = blk_bs(blk); | |
132 | AioContext *aio_context; | |
133 | ||
134 | if (!dinfo) { | |
135 | return; | |
136 | } | |
137 | ||
138 | if (bs) { | |
139 | aio_context = bdrv_get_aio_context(bs); | |
140 | aio_context_acquire(aio_context); | |
141 | ||
142 | if (bs->job) { | |
143 | block_job_cancel(bs->job); | |
144 | } | |
145 | ||
146 | aio_context_release(aio_context); | |
147 | } | |
148 | ||
149 | dinfo->auto_del = 1; | |
150 | } | |
151 | ||
152 | void blockdev_auto_del(BlockBackend *blk) | |
153 | { | |
154 | DriveInfo *dinfo = blk_legacy_dinfo(blk); | |
155 | ||
156 | if (dinfo && dinfo->auto_del) { | |
157 | monitor_remove_blk(blk); | |
158 | blk_unref(blk); | |
159 | } | |
160 | } | |
161 | ||
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 | ||
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 | ||
191 | QemuOpts *drive_def(const char *optstr) | |
192 | { | |
193 | return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); | |
194 | } | |
195 | ||
196 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
197 | const char *optstr) | |
198 | { | |
199 | QemuOpts *opts; | |
200 | ||
201 | opts = drive_def(optstr); | |
202 | if (!opts) { | |
203 | return NULL; | |
204 | } | |
205 | if (type != IF_DEFAULT) { | |
206 | qemu_opt_set(opts, "if", if_name[type], &error_abort); | |
207 | } | |
208 | if (index >= 0) { | |
209 | qemu_opt_set_number(opts, "index", index, &error_abort); | |
210 | } | |
211 | if (file) | |
212 | qemu_opt_set(opts, "file", file, &error_abort); | |
213 | return opts; | |
214 | } | |
215 | ||
216 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
217 | { | |
218 | BlockBackend *blk; | |
219 | DriveInfo *dinfo; | |
220 | ||
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) { | |
225 | return dinfo; | |
226 | } | |
227 | } | |
228 | ||
229 | return NULL; | |
230 | } | |
231 | ||
232 | void drive_check_orphaned(void) | |
233 | { | |
234 | BlockBackend *blk; | |
235 | DriveInfo *dinfo; | |
236 | Location loc; | |
237 | bool orphans = false; | |
238 | ||
239 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { | |
240 | dinfo = blk_legacy_dinfo(blk); | |
241 | if (!blk_get_attached_dev(blk) && !dinfo->is_default && | |
242 | dinfo->type != IF_NONE) { | |
243 | loc_push_none(&loc); | |
244 | qemu_opts_loc_restore(dinfo->opts); | |
245 | error_report("machine type does not support" | |
246 | " if=%s,bus=%d,unit=%d", | |
247 | if_name[dinfo->type], dinfo->bus, dinfo->unit); | |
248 | loc_pop(&loc); | |
249 | orphans = true; | |
250 | } | |
251 | } | |
252 | ||
253 | if (orphans) { | |
254 | exit(1); | |
255 | } | |
256 | } | |
257 | ||
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 | ||
265 | int drive_get_max_bus(BlockInterfaceType type) | |
266 | { | |
267 | int max_bus; | |
268 | BlockBackend *blk; | |
269 | DriveInfo *dinfo; | |
270 | ||
271 | max_bus = -1; | |
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) { | |
275 | max_bus = dinfo->bus; | |
276 | } | |
277 | } | |
278 | return max_bus; | |
279 | } | |
280 | ||
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 | ||
291 | static void bdrv_format_print(void *opaque, const char *name) | |
292 | { | |
293 | error_printf(" %s", name); | |
294 | } | |
295 | ||
296 | typedef struct { | |
297 | QEMUBH *bh; | |
298 | BlockDriverState *bs; | |
299 | } BDRVPutRefBH; | |
300 | ||
301 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) | |
302 | { | |
303 | if (!strcmp(buf, "ignore")) { | |
304 | return BLOCKDEV_ON_ERROR_IGNORE; | |
305 | } else if (!is_read && !strcmp(buf, "enospc")) { | |
306 | return BLOCKDEV_ON_ERROR_ENOSPC; | |
307 | } else if (!strcmp(buf, "stop")) { | |
308 | return BLOCKDEV_ON_ERROR_STOP; | |
309 | } else if (!strcmp(buf, "report")) { | |
310 | return BLOCKDEV_ON_ERROR_REPORT; | |
311 | } else { | |
312 | error_setg(errp, "'%s' invalid %s error action", | |
313 | buf, is_read ? "read" : "write"); | |
314 | return -1; | |
315 | } | |
316 | } | |
317 | ||
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 | ||
338 | case QTYPE_QNUM: { | |
339 | int64_t length = qnum_get_int(qobject_to_qnum(entry->value)); | |
340 | ||
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 | ||
358 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; | |
359 | ||
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 | { | |
365 | Error *local_error = NULL; | |
366 | const char *aio; | |
367 | ||
368 | if (bdrv_flags) { | |
369 | if (qemu_opt_get_bool(opts, "copy-on-read", false)) { | |
370 | *bdrv_flags |= BDRV_O_COPY_ON_READ; | |
371 | } | |
372 | ||
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) { | |
391 | throttle_config_init(throttle_cfg); | |
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 | ||
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 | ||
431 | throttle_cfg->op_size = | |
432 | qemu_opt_get_number(opts, "throttling.iops-size", 0); | |
433 | ||
434 | if (!throttle_is_valid(throttle_cfg, errp)) { | |
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"), | |
443 | BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, | |
444 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, | |
445 | &local_error); | |
446 | if (local_error) { | |
447 | error_propagate(errp, local_error); | |
448 | return; | |
449 | } | |
450 | } | |
451 | } | |
452 | ||
453 | /* Takes the ownership of bs_opts */ | |
454 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, | |
455 | Error **errp) | |
456 | { | |
457 | const char *buf; | |
458 | int bdrv_flags = 0; | |
459 | int on_read_error, on_write_error; | |
460 | bool account_invalid, account_failed; | |
461 | bool writethrough, read_only; | |
462 | BlockBackend *blk; | |
463 | BlockDriverState *bs; | |
464 | ThrottleConfig cfg; | |
465 | int snapshot = 0; | |
466 | Error *error = NULL; | |
467 | QemuOpts *opts; | |
468 | QDict *interval_dict = NULL; | |
469 | QList *interval_list = NULL; | |
470 | const char *id; | |
471 | BlockdevDetectZeroesOptions detect_zeroes = | |
472 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; | |
473 | const char *throttling_group = NULL; | |
474 | ||
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"); | |
478 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); | |
479 | if (error) { | |
480 | error_propagate(errp, error); | |
481 | goto err_no_opts; | |
482 | } | |
483 | ||
484 | qemu_opts_absorb_qdict(opts, bs_opts, &error); | |
485 | if (error) { | |
486 | error_propagate(errp, error); | |
487 | goto early_err; | |
488 | } | |
489 | ||
490 | if (id) { | |
491 | qdict_del(bs_opts, "id"); | |
492 | } | |
493 | ||
494 | /* extract parameters */ | |
495 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); | |
496 | ||
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 | ||
500 | writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); | |
501 | ||
502 | id = qemu_opts_id(opts); | |
503 | ||
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 | } | |
512 | ||
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; | |
518 | } | |
519 | ||
520 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
521 | if (is_help_option(buf)) { | |
522 | error_printf("Supported formats:"); | |
523 | bdrv_iterate_format(bdrv_format_print, NULL); | |
524 | error_printf("\n"); | |
525 | goto early_err; | |
526 | } | |
527 | ||
528 | if (qdict_haskey(bs_opts, "driver")) { | |
529 | error_setg(errp, "Cannot specify both 'driver' and 'format'"); | |
530 | goto early_err; | |
531 | } | |
532 | qdict_put_str(bs_opts, "driver", buf); | |
533 | } | |
534 | ||
535 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; | |
536 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { | |
537 | on_write_error = parse_block_error_action(buf, 0, &error); | |
538 | if (error) { | |
539 | error_propagate(errp, error); | |
540 | goto early_err; | |
541 | } | |
542 | } | |
543 | ||
544 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; | |
545 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { | |
546 | on_read_error = parse_block_error_action(buf, 1, &error); | |
547 | if (error) { | |
548 | error_propagate(errp, error); | |
549 | goto early_err; | |
550 | } | |
551 | } | |
552 | ||
553 | if (snapshot) { | |
554 | bdrv_flags |= BDRV_O_SNAPSHOT; | |
555 | } | |
556 | ||
557 | read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); | |
558 | ||
559 | /* init */ | |
560 | if ((!file || !*file) && !qdict_size(bs_opts)) { | |
561 | BlockBackendRootState *blk_rs; | |
562 | ||
563 | blk = blk_new(0, BLK_PERM_ALL); | |
564 | blk_rs = blk_get_root_state(blk); | |
565 | blk_rs->open_flags = bdrv_flags; | |
566 | blk_rs->read_only = read_only; | |
567 | blk_rs->detect_zeroes = detect_zeroes; | |
568 | ||
569 | QDECREF(bs_opts); | |
570 | } else { | |
571 | if (file && !*file) { | |
572 | file = NULL; | |
573 | } | |
574 | ||
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. */ | |
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"); | |
580 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, | |
581 | read_only ? "on" : "off"); | |
582 | assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); | |
583 | ||
584 | if (runstate_check(RUN_STATE_INMIGRATE)) { | |
585 | bdrv_flags |= BDRV_O_INACTIVE; | |
586 | } | |
587 | ||
588 | blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); | |
589 | if (!blk) { | |
590 | goto err_no_bs_opts; | |
591 | } | |
592 | bs = blk_bs(blk); | |
593 | ||
594 | bs->detect_zeroes = detect_zeroes; | |
595 | ||
596 | block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); | |
597 | ||
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; | |
602 | } | |
603 | } | |
604 | ||
605 | /* disk I/O throttling */ | |
606 | if (throttle_enabled(&cfg)) { | |
607 | if (!throttling_group) { | |
608 | throttling_group = id; | |
609 | } | |
610 | blk_io_limits_enable(blk, throttling_group); | |
611 | blk_set_io_limits(blk, &cfg); | |
612 | } | |
613 | ||
614 | blk_set_enable_write_cache(blk, !writethrough); | |
615 | blk_set_on_error(blk, on_read_error, on_write_error); | |
616 | ||
617 | if (!monitor_add_blk(blk, id, errp)) { | |
618 | blk_unref(blk); | |
619 | blk = NULL; | |
620 | goto err_no_bs_opts; | |
621 | } | |
622 | ||
623 | err_no_bs_opts: | |
624 | qemu_opts_del(opts); | |
625 | QDECREF(interval_dict); | |
626 | QDECREF(interval_list); | |
627 | return blk; | |
628 | ||
629 | early_err: | |
630 | qemu_opts_del(opts); | |
631 | QDECREF(interval_dict); | |
632 | QDECREF(interval_list); | |
633 | err_no_opts: | |
634 | QDECREF(bs_opts); | |
635 | return NULL; | |
636 | } | |
637 | ||
638 | /* Takes the ownership of bs_opts */ | |
639 | static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) | |
640 | { | |
641 | int bdrv_flags = 0; | |
642 | ||
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. */ | |
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"); | |
648 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); | |
649 | ||
650 | if (runstate_check(RUN_STATE_INMIGRATE)) { | |
651 | bdrv_flags |= BDRV_O_INACTIVE; | |
652 | } | |
653 | ||
654 | return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); | |
655 | } | |
656 | ||
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 | ||
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 | ||
677 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, | |
678 | Error **errp) | |
679 | { | |
680 | const char *value; | |
681 | ||
682 | value = qemu_opt_get(opts, from); | |
683 | if (value) { | |
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 | } | |
689 | } | |
690 | ||
691 | /* rename all items in opts */ | |
692 | while ((value = qemu_opt_get(opts, from))) { | |
693 | qemu_opt_set(opts, to, value, &error_abort); | |
694 | qemu_opt_unset(opts, from); | |
695 | } | |
696 | } | |
697 | ||
698 | QemuOptsList qemu_legacy_drive_opts = { | |
699 | .name = "drive", | |
700 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
701 | .desc = { | |
702 | { | |
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 | },{ | |
715 | .name = "media", | |
716 | .type = QEMU_OPT_STRING, | |
717 | .help = "media type (disk, cdrom)", | |
718 | },{ | |
719 | .name = "if", | |
720 | .type = QEMU_OPT_STRING, | |
721 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
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)", | |
738 | },{ | |
739 | .name = "boot", | |
740 | .type = QEMU_OPT_BOOL, | |
741 | .help = "(deprecated, ignored)", | |
742 | },{ | |
743 | .name = "addr", | |
744 | .type = QEMU_OPT_STRING, | |
745 | .help = "pci address (virtio only)", | |
746 | },{ | |
747 | .name = "serial", | |
748 | .type = QEMU_OPT_STRING, | |
749 | .help = "disk serial number", | |
750 | },{ | |
751 | .name = "file", | |
752 | .type = QEMU_OPT_STRING, | |
753 | .help = "file name", | |
754 | }, | |
755 | ||
756 | /* Options that are passed on, but have special semantics with -drive */ | |
757 | { | |
758 | .name = BDRV_OPT_READ_ONLY, | |
759 | .type = QEMU_OPT_BOOL, | |
760 | .help = "open drive file as read-only", | |
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", | |
769 | },{ | |
770 | .name = "copy-on-read", | |
771 | .type = QEMU_OPT_BOOL, | |
772 | .help = "copy read data from backing file into image file", | |
773 | }, | |
774 | ||
775 | { /* end of list */ } | |
776 | }, | |
777 | }; | |
778 | ||
779 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) | |
780 | { | |
781 | const char *value; | |
782 | BlockBackend *blk; | |
783 | DriveInfo *dinfo = NULL; | |
784 | QDict *bs_opts; | |
785 | QemuOpts *legacy_opts; | |
786 | DriveMediaType media = MEDIA_DISK; | |
787 | BlockInterfaceType type; | |
788 | int cyls, heads, secs, translation; | |
789 | int max_devs, bus_id, unit_id, index; | |
790 | const char *devaddr; | |
791 | const char *werror, *rerror; | |
792 | bool read_only = false; | |
793 | bool copy_on_read; | |
794 | const char *serial; | |
795 | const char *filename; | |
796 | Error *local_err = NULL; | |
797 | int i; | |
798 | const char *deprecated[] = { | |
799 | "serial", "trans", "secs", "heads", "cyls", "addr" | |
800 | }; | |
801 | ||
802 | /* Change legacy command line options into QMP ones */ | |
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" }, | |
810 | ||
811 | { "bps", "throttling.bps-total" }, | |
812 | { "bps_rd", "throttling.bps-read" }, | |
813 | { "bps_wr", "throttling.bps-write" }, | |
814 | ||
815 | { "iops_max", "throttling.iops-total-max" }, | |
816 | { "iops_rd_max", "throttling.iops-read-max" }, | |
817 | { "iops_wr_max", "throttling.iops-write-max" }, | |
818 | ||
819 | { "bps_max", "throttling.bps-total-max" }, | |
820 | { "bps_rd_max", "throttling.bps-read-max" }, | |
821 | { "bps_wr_max", "throttling.bps-write-max" }, | |
822 | ||
823 | { "iops_size", "throttling.iops-size" }, | |
824 | ||
825 | { "group", "throttling.group" }, | |
826 | ||
827 | { "readonly", BDRV_OPT_READ_ONLY }, | |
828 | }; | |
829 | ||
830 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
831 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, | |
832 | &local_err); | |
833 | if (local_err) { | |
834 | error_report_err(local_err); | |
835 | return NULL; | |
836 | } | |
837 | } | |
838 | ||
839 | value = qemu_opt_get(all_opts, "cache"); | |
840 | if (value) { | |
841 | int flags = 0; | |
842 | bool writethrough; | |
843 | ||
844 | if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { | |
845 | error_report("invalid cache option"); | |
846 | return NULL; | |
847 | } | |
848 | ||
849 | /* Specific options take precedence */ | |
850 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { | |
851 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, | |
852 | !writethrough, &error_abort); | |
853 | } | |
854 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { | |
855 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, | |
856 | !!(flags & BDRV_O_NOCACHE), &error_abort); | |
857 | } | |
858 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { | |
859 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, | |
860 | !!(flags & BDRV_O_NO_FLUSH), &error_abort); | |
861 | } | |
862 | qemu_opt_unset(all_opts, "cache"); | |
863 | } | |
864 | ||
865 | /* Get a QDict for processing the options */ | |
866 | bs_opts = qdict_new(); | |
867 | qemu_opts_to_qdict(all_opts, bs_opts); | |
868 | ||
869 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, | |
870 | &error_abort); | |
871 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); | |
872 | if (local_err) { | |
873 | error_report_err(local_err); | |
874 | goto fail; | |
875 | } | |
876 | ||
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 | ||
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 | ||
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; | |
901 | read_only = true; | |
902 | } else { | |
903 | error_report("'%s' invalid media", value); | |
904 | goto fail; | |
905 | } | |
906 | } | |
907 | ||
908 | /* copy-on-read is disabled with a warning for read-only devices */ | |
909 | read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); | |
910 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); | |
911 | ||
912 | if (read_only && copy_on_read) { | |
913 | error_report("warning: disabling copy-on-read on read-only drive"); | |
914 | copy_on_read = false; | |
915 | } | |
916 | ||
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"); | |
919 | ||
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 | ||
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; | |
967 | } else if (!strcmp(value, "large")) { | |
968 | translation = BIOS_ATA_TRANSLATION_LARGE; | |
969 | } else if (!strcmp(value, "rechs")) { | |
970 | translation = BIOS_ATA_TRANSLATION_RECHS; | |
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 | ||
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 | ||
1025 | /* Serial number */ | |
1026 | serial = qemu_opt_get(legacy_opts, "serial"); | |
1027 | ||
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 | } | |
1042 | qdict_put_str(bs_opts, "id", new_id); | |
1043 | g_free(new_id); | |
1044 | } | |
1045 | ||
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; | |
1055 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, | |
1056 | &error_abort); | |
1057 | if (arch_type == QEMU_ARCH_S390X) { | |
1058 | qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); | |
1059 | } else { | |
1060 | qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); | |
1061 | } | |
1062 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), | |
1063 | &error_abort); | |
1064 | if (devaddr) { | |
1065 | qemu_opt_set(devopts, "addr", devaddr, &error_abort); | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | filename = qemu_opt_get(legacy_opts, "file"); | |
1070 | ||
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 | } | |
1079 | qdict_put_str(bs_opts, "werror", werror); | |
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 | } | |
1089 | qdict_put_str(bs_opts, "rerror", rerror); | |
1090 | } | |
1091 | ||
1092 | /* Actual block device init: Functionality shared with blockdev-add */ | |
1093 | blk = blockdev_init(filename, bs_opts, &local_err); | |
1094 | bs_opts = NULL; | |
1095 | if (!blk) { | |
1096 | if (local_err) { | |
1097 | error_report_err(local_err); | |
1098 | } | |
1099 | goto fail; | |
1100 | } else { | |
1101 | assert(!local_err); | |
1102 | } | |
1103 | ||
1104 | /* Create legacy DriveInfo */ | |
1105 | dinfo = g_malloc0(sizeof(*dinfo)); | |
1106 | dinfo->opts = all_opts; | |
1107 | ||
1108 | dinfo->cyls = cyls; | |
1109 | dinfo->heads = heads; | |
1110 | dinfo->secs = secs; | |
1111 | dinfo->trans = translation; | |
1112 | ||
1113 | dinfo->type = type; | |
1114 | dinfo->bus = bus_id; | |
1115 | dinfo->unit = unit_id; | |
1116 | dinfo->devaddr = devaddr; | |
1117 | dinfo->serial = g_strdup(serial); | |
1118 | ||
1119 | blk_set_legacy_dinfo(blk, dinfo); | |
1120 | ||
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 | ||
1132 | fail: | |
1133 | qemu_opts_del(legacy_opts); | |
1134 | QDECREF(bs_opts); | |
1135 | return dinfo; | |
1136 | } | |
1137 | ||
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 | ||
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 | ||
1183 | void hmp_commit(Monitor *mon, const QDict *qdict) | |
1184 | { | |
1185 | const char *device = qdict_get_str(qdict, "device"); | |
1186 | BlockBackend *blk; | |
1187 | int ret; | |
1188 | ||
1189 | if (!strcmp(device, "all")) { | |
1190 | ret = blk_commit_all(); | |
1191 | } else { | |
1192 | BlockDriverState *bs; | |
1193 | AioContext *aio_context; | |
1194 | ||
1195 | blk = blk_by_name(device); | |
1196 | if (!blk) { | |
1197 | monitor_printf(mon, "Device '%s' not found\n", device); | |
1198 | return; | |
1199 | } | |
1200 | if (!blk_is_available(blk)) { | |
1201 | monitor_printf(mon, "Device '%s' has no medium\n", device); | |
1202 | return; | |
1203 | } | |
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); | |
1212 | } | |
1213 | if (ret < 0) { | |
1214 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, | |
1215 | strerror(-ret)); | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | static void blockdev_do_action(TransactionAction *action, Error **errp) | |
1220 | { | |
1221 | TransactionActionList list; | |
1222 | ||
1223 | list.value = action; | |
1224 | list.next = NULL; | |
1225 | qmp_transaction(&list, false, NULL, errp); | |
1226 | } | |
1227 | ||
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, | |
1233 | bool has_format, const char *format, | |
1234 | bool has_mode, NewImageMode mode, Error **errp) | |
1235 | { | |
1236 | BlockdevSnapshotSync snapshot = { | |
1237 | .has_device = has_device, | |
1238 | .device = (char *) device, | |
1239 | .has_node_name = has_node_name, | |
1240 | .node_name = (char *) node_name, | |
1241 | .snapshot_file = (char *) snapshot_file, | |
1242 | .has_snapshot_node_name = has_snapshot_node_name, | |
1243 | .snapshot_node_name = (char *) snapshot_node_name, | |
1244 | .has_format = has_format, | |
1245 | .format = (char *) format, | |
1246 | .has_mode = has_mode, | |
1247 | .mode = mode, | |
1248 | }; | |
1249 | TransactionAction action = { | |
1250 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, | |
1251 | .u.blockdev_snapshot_sync.data = &snapshot, | |
1252 | }; | |
1253 | blockdev_do_action(&action, errp); | |
1254 | } | |
1255 | ||
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 | }; | |
1263 | TransactionAction action = { | |
1264 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, | |
1265 | .u.blockdev_snapshot.data = &snapshot_data, | |
1266 | }; | |
1267 | blockdev_do_action(&action, errp); | |
1268 | } | |
1269 | ||
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 | }; | |
1278 | TransactionAction action = { | |
1279 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
1280 | .u.blockdev_snapshot_internal_sync.data = &snapshot, | |
1281 | }; | |
1282 | blockdev_do_action(&action, errp); | |
1283 | } | |
1284 | ||
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 | { | |
1292 | BlockDriverState *bs; | |
1293 | AioContext *aio_context; | |
1294 | QEMUSnapshotInfo sn; | |
1295 | Error *local_err = NULL; | |
1296 | SnapshotInfo *info = NULL; | |
1297 | int ret; | |
1298 | ||
1299 | bs = qmp_get_root_bs(device, errp); | |
1300 | if (!bs) { | |
1301 | return NULL; | |
1302 | } | |
1303 | aio_context = bdrv_get_aio_context(bs); | |
1304 | aio_context_acquire(aio_context); | |
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"); | |
1316 | goto out_aio_context; | |
1317 | } | |
1318 | ||
1319 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { | |
1320 | goto out_aio_context; | |
1321 | } | |
1322 | ||
1323 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); | |
1324 | if (local_err) { | |
1325 | error_propagate(errp, local_err); | |
1326 | goto out_aio_context; | |
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); | |
1333 | goto out_aio_context; | |
1334 | } | |
1335 | ||
1336 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
1337 | if (local_err) { | |
1338 | error_propagate(errp, local_err); | |
1339 | goto out_aio_context; | |
1340 | } | |
1341 | ||
1342 | aio_context_release(aio_context); | |
1343 | ||
1344 | info = g_new0(SnapshotInfo, 1); | |
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; | |
1354 | ||
1355 | out_aio_context: | |
1356 | aio_context_release(aio_context); | |
1357 | return NULL; | |
1358 | } | |
1359 | ||
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, | |
1376 | Error **errp) | |
1377 | { | |
1378 | BlockDriverState *bs; | |
1379 | BdrvDirtyBitmap *bitmap; | |
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 | ||
1395 | bitmap = bdrv_find_dirty_bitmap(bs, name); | |
1396 | if (!bitmap) { | |
1397 | error_setg(errp, "Dirty bitmap '%s' not found", name); | |
1398 | return NULL; | |
1399 | } | |
1400 | ||
1401 | if (pbs) { | |
1402 | *pbs = bs; | |
1403 | } | |
1404 | ||
1405 | return bitmap; | |
1406 | } | |
1407 | ||
1408 | /* New and old BlockDriverState structs for atomic group operations */ | |
1409 | ||
1410 | typedef struct BlkActionState BlkActionState; | |
1411 | ||
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 { | |
1427 | size_t instance_size; | |
1428 | void (*prepare)(BlkActionState *common, Error **errp); | |
1429 | void (*commit)(BlkActionState *common); | |
1430 | void (*abort)(BlkActionState *common); | |
1431 | void (*clean)(BlkActionState *common); | |
1432 | } BlkActionOps; | |
1433 | ||
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. | |
1440 | * @block_job_txn: Transaction which this action belongs to. | |
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. | |
1446 | */ | |
1447 | struct BlkActionState { | |
1448 | TransactionAction *action; | |
1449 | const BlkActionOps *ops; | |
1450 | BlockJobTxn *block_job_txn; | |
1451 | TransactionProperties *txn_props; | |
1452 | QSIMPLEQ_ENTRY(BlkActionState) entry; | |
1453 | }; | |
1454 | ||
1455 | /* internal snapshot private data */ | |
1456 | typedef struct InternalSnapshotState { | |
1457 | BlkActionState common; | |
1458 | BlockDriverState *bs; | |
1459 | AioContext *aio_context; | |
1460 | QEMUSnapshotInfo sn; | |
1461 | bool created; | |
1462 | } InternalSnapshotState; | |
1463 | ||
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 | ||
1478 | static void internal_snapshot_prepare(BlkActionState *common, | |
1479 | Error **errp) | |
1480 | { | |
1481 | Error *local_err = NULL; | |
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 | ||
1492 | g_assert(common->action->type == | |
1493 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); | |
1494 | internal = common->action->u.blockdev_snapshot_internal_sync.data; | |
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 */ | |
1502 | if (action_check_completion_mode(common, errp) < 0) { | |
1503 | return; | |
1504 | } | |
1505 | ||
1506 | bs = qmp_get_root_bs(device, errp); | |
1507 | if (!bs) { | |
1508 | return; | |
1509 | } | |
1510 | ||
1511 | /* AioContext is released in .clean() */ | |
1512 | state->aio_context = bdrv_get_aio_context(bs); | |
1513 | aio_context_acquire(state->aio_context); | |
1514 | ||
1515 | state->bs = bs; | |
1516 | bdrv_drained_begin(bs); | |
1517 | ||
1518 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { | |
1519 | return; | |
1520 | } | |
1521 | ||
1522 | if (bdrv_is_read_only(bs)) { | |
1523 | error_setg(errp, "Device '%s' is read only", device); | |
1524 | return; | |
1525 | } | |
1526 | ||
1527 | if (!bdrv_can_snapshot(bs)) { | |
1528 | error_setg(errp, "Block format '%s' used by device '%s' " | |
1529 | "does not support internal snapshots", | |
1530 | bs->drv->format_name, device); | |
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 */ | |
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); | |
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 */ | |
1569 | state->created = true; | |
1570 | } | |
1571 | ||
1572 | static void internal_snapshot_abort(BlkActionState *common) | |
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 | ||
1580 | if (!state->created) { | |
1581 | return; | |
1582 | } | |
1583 | ||
1584 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { | |
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)); | |
1590 | } | |
1591 | } | |
1592 | ||
1593 | static void internal_snapshot_clean(BlkActionState *common) | |
1594 | { | |
1595 | InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, | |
1596 | common, common); | |
1597 | ||
1598 | if (state->aio_context) { | |
1599 | if (state->bs) { | |
1600 | bdrv_drained_end(state->bs); | |
1601 | } | |
1602 | aio_context_release(state->aio_context); | |
1603 | } | |
1604 | } | |
1605 | ||
1606 | /* external snapshot private data */ | |
1607 | typedef struct ExternalSnapshotState { | |
1608 | BlkActionState common; | |
1609 | BlockDriverState *old_bs; | |
1610 | BlockDriverState *new_bs; | |
1611 | AioContext *aio_context; | |
1612 | bool overlay_appended; | |
1613 | } ExternalSnapshotState; | |
1614 | ||
1615 | static void external_snapshot_prepare(BlkActionState *common, | |
1616 | Error **errp) | |
1617 | { | |
1618 | int flags = 0; | |
1619 | QDict *options = NULL; | |
1620 | Error *local_err = NULL; | |
1621 | /* Device and node name of the image to generate the snapshot from */ | |
1622 | const char *device; | |
1623 | const char *node_name; | |
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') */ | |
1627 | const char *new_image_file; | |
1628 | ExternalSnapshotState *state = | |
1629 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1630 | TransactionAction *action = common->action; | |
1631 | ||
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 | { | |
1637 | BlockdevSnapshot *s = action->u.blockdev_snapshot.data; | |
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 | { | |
1646 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; | |
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(); | |
1655 | } | |
1656 | ||
1657 | /* start processing */ | |
1658 | if (action_check_completion_mode(common, errp) < 0) { | |
1659 | return; | |
1660 | } | |
1661 | ||
1662 | state->old_bs = bdrv_lookup_bs(device, node_name, errp); | |
1663 | if (!state->old_bs) { | |
1664 | return; | |
1665 | } | |
1666 | ||
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); | |
1670 | bdrv_drained_begin(state->old_bs); | |
1671 | ||
1672 | if (!bdrv_is_inserted(state->old_bs)) { | |
1673 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1674 | return; | |
1675 | } | |
1676 | ||
1677 | if (bdrv_op_is_blocked(state->old_bs, | |
1678 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
1679 | return; | |
1680 | } | |
1681 | ||
1682 | if (!bdrv_is_read_only(state->old_bs)) { | |
1683 | if (bdrv_flush(state->old_bs)) { | |
1684 | error_setg(errp, QERR_IO_ERROR); | |
1685 | return; | |
1686 | } | |
1687 | } | |
1688 | ||
1689 | if (!bdrv_is_first_non_filter(state->old_bs)) { | |
1690 | error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); | |
1691 | return; | |
1692 | } | |
1693 | ||
1694 | if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { | |
1695 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; | |
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; | |
1700 | ||
1701 | if (node_name && !snapshot_node_name) { | |
1702 | error_setg(errp, "New snapshot node name missing"); | |
1703 | return; | |
1704 | } | |
1705 | ||
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; | |
1713 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); | |
1714 | ||
1715 | /* create new image w/backing file */ | |
1716 | mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1717 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
1718 | int64_t size = bdrv_getlength(state->old_bs); | |
1719 | if (size < 0) { | |
1720 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
1721 | return; | |
1722 | } | |
1723 | bdrv_img_create(new_image_file, format, | |
1724 | state->old_bs->filename, | |
1725 | state->old_bs->drv->format_name, | |
1726 | NULL, size, flags, false, &local_err); | |
1727 | if (local_err) { | |
1728 | error_propagate(errp, local_err); | |
1729 | return; | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | options = qdict_new(); | |
1734 | if (s->has_snapshot_node_name) { | |
1735 | qdict_put_str(options, "node-name", snapshot_node_name); | |
1736 | } | |
1737 | qdict_put_str(options, "driver", format); | |
1738 | ||
1739 | flags |= BDRV_O_NO_BACKING; | |
1740 | } | |
1741 | ||
1742 | state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, | |
1743 | errp); | |
1744 | /* We will manually add the backing_hd field to the bs later */ | |
1745 | if (!state->new_bs) { | |
1746 | return; | |
1747 | } | |
1748 | ||
1749 | if (bdrv_has_blk(state->new_bs)) { | |
1750 | error_setg(errp, "The snapshot is already in use"); | |
1751 | return; | |
1752 | } | |
1753 | ||
1754 | if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, | |
1755 | errp)) { | |
1756 | return; | |
1757 | } | |
1758 | ||
1759 | if (state->new_bs->backing != NULL) { | |
1760 | error_setg(errp, "The snapshot already has a backing image"); | |
1761 | return; | |
1762 | } | |
1763 | ||
1764 | if (!state->new_bs->drv->supports_backing) { | |
1765 | error_setg(errp, "The snapshot does not support backing images"); | |
1766 | return; | |
1767 | } | |
1768 | ||
1769 | bdrv_set_aio_context(state->new_bs, state->aio_context); | |
1770 | ||
1771 | /* This removes our old bs and adds the new bs. This is an operation that | |
1772 | * can fail, so we need to do it in .prepare; undoing it for abort is | |
1773 | * always possible. */ | |
1774 | bdrv_ref(state->new_bs); | |
1775 | bdrv_append(state->new_bs, state->old_bs, &local_err); | |
1776 | if (local_err) { | |
1777 | error_propagate(errp, local_err); | |
1778 | return; | |
1779 | } | |
1780 | state->overlay_appended = true; | |
1781 | } | |
1782 | ||
1783 | static void external_snapshot_commit(BlkActionState *common) | |
1784 | { | |
1785 | ExternalSnapshotState *state = | |
1786 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1787 | ||
1788 | /* We don't need (or want) to use the transactional | |
1789 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1790 | * don't want to abort all of them if one of them fails the reopen */ | |
1791 | if (!atomic_read(&state->old_bs->copy_on_read)) { | |
1792 | bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, | |
1793 | NULL); | |
1794 | } | |
1795 | } | |
1796 | ||
1797 | static void external_snapshot_abort(BlkActionState *common) | |
1798 | { | |
1799 | ExternalSnapshotState *state = | |
1800 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1801 | if (state->new_bs) { | |
1802 | if (state->overlay_appended) { | |
1803 | bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() | |
1804 | close state->old_bs; we need it */ | |
1805 | bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); | |
1806 | bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); | |
1807 | bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ | |
1808 | } | |
1809 | } | |
1810 | } | |
1811 | ||
1812 | static void external_snapshot_clean(BlkActionState *common) | |
1813 | { | |
1814 | ExternalSnapshotState *state = | |
1815 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1816 | if (state->aio_context) { | |
1817 | bdrv_drained_end(state->old_bs); | |
1818 | aio_context_release(state->aio_context); | |
1819 | bdrv_unref(state->new_bs); | |
1820 | } | |
1821 | } | |
1822 | ||
1823 | typedef struct DriveBackupState { | |
1824 | BlkActionState common; | |
1825 | BlockDriverState *bs; | |
1826 | AioContext *aio_context; | |
1827 | BlockJob *job; | |
1828 | } DriveBackupState; | |
1829 | ||
1830 | static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, | |
1831 | Error **errp); | |
1832 | ||
1833 | static void drive_backup_prepare(BlkActionState *common, Error **errp) | |
1834 | { | |
1835 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1836 | BlockDriverState *bs; | |
1837 | DriveBackup *backup; | |
1838 | Error *local_err = NULL; | |
1839 | ||
1840 | assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); | |
1841 | backup = common->action->u.drive_backup.data; | |
1842 | ||
1843 | bs = qmp_get_root_bs(backup->device, errp); | |
1844 | if (!bs) { | |
1845 | return; | |
1846 | } | |
1847 | ||
1848 | /* AioContext is released in .clean() */ | |
1849 | state->aio_context = bdrv_get_aio_context(bs); | |
1850 | aio_context_acquire(state->aio_context); | |
1851 | bdrv_drained_begin(bs); | |
1852 | state->bs = bs; | |
1853 | ||
1854 | state->job = do_drive_backup(backup, common->block_job_txn, &local_err); | |
1855 | if (local_err) { | |
1856 | error_propagate(errp, local_err); | |
1857 | return; | |
1858 | } | |
1859 | } | |
1860 | ||
1861 | static void drive_backup_commit(BlkActionState *common) | |
1862 | { | |
1863 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1864 | assert(state->job); | |
1865 | block_job_start(state->job); | |
1866 | } | |
1867 | ||
1868 | static void drive_backup_abort(BlkActionState *common) | |
1869 | { | |
1870 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1871 | ||
1872 | if (state->job) { | |
1873 | block_job_cancel_sync(state->job); | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | static void drive_backup_clean(BlkActionState *common) | |
1878 | { | |
1879 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1880 | ||
1881 | if (state->aio_context) { | |
1882 | bdrv_drained_end(state->bs); | |
1883 | aio_context_release(state->aio_context); | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | typedef struct BlockdevBackupState { | |
1888 | BlkActionState common; | |
1889 | BlockDriverState *bs; | |
1890 | BlockJob *job; | |
1891 | AioContext *aio_context; | |
1892 | } BlockdevBackupState; | |
1893 | ||
1894 | static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, | |
1895 | Error **errp); | |
1896 | ||
1897 | static void blockdev_backup_prepare(BlkActionState *common, Error **errp) | |
1898 | { | |
1899 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1900 | BlockdevBackup *backup; | |
1901 | BlockDriverState *bs, *target; | |
1902 | Error *local_err = NULL; | |
1903 | ||
1904 | assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); | |
1905 | backup = common->action->u.blockdev_backup.data; | |
1906 | ||
1907 | bs = qmp_get_root_bs(backup->device, errp); | |
1908 | if (!bs) { | |
1909 | return; | |
1910 | } | |
1911 | ||
1912 | target = bdrv_lookup_bs(backup->target, backup->target, errp); | |
1913 | if (!target) { | |
1914 | return; | |
1915 | } | |
1916 | ||
1917 | /* AioContext is released in .clean() */ | |
1918 | state->aio_context = bdrv_get_aio_context(bs); | |
1919 | if (state->aio_context != bdrv_get_aio_context(target)) { | |
1920 | state->aio_context = NULL; | |
1921 | error_setg(errp, "Backup between two IO threads is not implemented"); | |
1922 | return; | |
1923 | } | |
1924 | aio_context_acquire(state->aio_context); | |
1925 | state->bs = bs; | |
1926 | bdrv_drained_begin(state->bs); | |
1927 | ||
1928 | state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err); | |
1929 | if (local_err) { | |
1930 | error_propagate(errp, local_err); | |
1931 | return; | |
1932 | } | |
1933 | } | |
1934 | ||
1935 | static void blockdev_backup_commit(BlkActionState *common) | |
1936 | { | |
1937 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1938 | assert(state->job); | |
1939 | block_job_start(state->job); | |
1940 | } | |
1941 | ||
1942 | static void blockdev_backup_abort(BlkActionState *common) | |
1943 | { | |
1944 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1945 | ||
1946 | if (state->job) { | |
1947 | block_job_cancel_sync(state->job); | |
1948 | } | |
1949 | } | |
1950 | ||
1951 | static void blockdev_backup_clean(BlkActionState *common) | |
1952 | { | |
1953 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1954 | ||
1955 | if (state->aio_context) { | |
1956 | bdrv_drained_end(state->bs); | |
1957 | aio_context_release(state->aio_context); | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | typedef struct BlockDirtyBitmapState { | |
1962 | BlkActionState common; | |
1963 | BdrvDirtyBitmap *bitmap; | |
1964 | BlockDriverState *bs; | |
1965 | AioContext *aio_context; | |
1966 | HBitmap *backup; | |
1967 | bool prepared; | |
1968 | } BlockDirtyBitmapState; | |
1969 | ||
1970 | static void block_dirty_bitmap_add_prepare(BlkActionState *common, | |
1971 | Error **errp) | |
1972 | { | |
1973 | Error *local_err = NULL; | |
1974 | BlockDirtyBitmapAdd *action; | |
1975 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
1976 | common, common); | |
1977 | ||
1978 | if (action_check_completion_mode(common, errp) < 0) { | |
1979 | return; | |
1980 | } | |
1981 | ||
1982 | action = common->action->u.block_dirty_bitmap_add.data; | |
1983 | /* AIO context taken and released within qmp_block_dirty_bitmap_add */ | |
1984 | qmp_block_dirty_bitmap_add(action->node, action->name, | |
1985 | action->has_granularity, action->granularity, | |
1986 | action->has_persistent, action->persistent, | |
1987 | action->has_autoload, action->autoload, | |
1988 | &local_err); | |
1989 | ||
1990 | if (!local_err) { | |
1991 | state->prepared = true; | |
1992 | } else { | |
1993 | error_propagate(errp, local_err); | |
1994 | } | |
1995 | } | |
1996 | ||
1997 | static void block_dirty_bitmap_add_abort(BlkActionState *common) | |
1998 | { | |
1999 | BlockDirtyBitmapAdd *action; | |
2000 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2001 | common, common); | |
2002 | ||
2003 | action = common->action->u.block_dirty_bitmap_add.data; | |
2004 | /* Should not be able to fail: IF the bitmap was added via .prepare(), | |
2005 | * then the node reference and bitmap name must have been valid. | |
2006 | */ | |
2007 | if (state->prepared) { | |
2008 | qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); | |
2009 | } | |
2010 | } | |
2011 | ||
2012 | static void block_dirty_bitmap_clear_prepare(BlkActionState *common, | |
2013 | Error **errp) | |
2014 | { | |
2015 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2016 | common, common); | |
2017 | BlockDirtyBitmap *action; | |
2018 | ||
2019 | if (action_check_completion_mode(common, errp) < 0) { | |
2020 | return; | |
2021 | } | |
2022 | ||
2023 | action = common->action->u.block_dirty_bitmap_clear.data; | |
2024 | state->bitmap = block_dirty_bitmap_lookup(action->node, | |
2025 | action->name, | |
2026 | &state->bs, | |
2027 | errp); | |
2028 | if (!state->bitmap) { | |
2029 | return; | |
2030 | } | |
2031 | ||
2032 | if (bdrv_dirty_bitmap_frozen(state->bitmap)) { | |
2033 | error_setg(errp, "Cannot modify a frozen bitmap"); | |
2034 | return; | |
2035 | } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { | |
2036 | error_setg(errp, "Cannot clear a disabled bitmap"); | |
2037 | return; | |
2038 | } else if (bdrv_dirty_bitmap_readonly(state->bitmap)) { | |
2039 | error_setg(errp, "Cannot clear a readonly bitmap"); | |
2040 | return; | |
2041 | } | |
2042 | ||
2043 | bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); | |
2044 | /* AioContext is released in .clean() */ | |
2045 | } | |
2046 | ||
2047 | static void block_dirty_bitmap_clear_abort(BlkActionState *common) | |
2048 | { | |
2049 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2050 | common, common); | |
2051 | ||
2052 | if (state->backup) { | |
2053 | bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); | |
2054 | } | |
2055 | } | |
2056 | ||
2057 | static void block_dirty_bitmap_clear_commit(BlkActionState *common) | |
2058 | { | |
2059 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2060 | common, common); | |
2061 | ||
2062 | hbitmap_free(state->backup); | |
2063 | } | |
2064 | ||
2065 | static void block_dirty_bitmap_clear_clean(BlkActionState *common) | |
2066 | { | |
2067 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2068 | common, common); | |
2069 | ||
2070 | if (state->aio_context) { | |
2071 | aio_context_release(state->aio_context); | |
2072 | } | |
2073 | } | |
2074 | ||
2075 | static void abort_prepare(BlkActionState *common, Error **errp) | |
2076 | { | |
2077 | error_setg(errp, "Transaction aborted using Abort action"); | |
2078 | } | |
2079 | ||
2080 | static void abort_commit(BlkActionState *common) | |
2081 | { | |
2082 | g_assert_not_reached(); /* this action never succeeds */ | |
2083 | } | |
2084 | ||
2085 | static const BlkActionOps actions[] = { | |
2086 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { | |
2087 | .instance_size = sizeof(ExternalSnapshotState), | |
2088 | .prepare = external_snapshot_prepare, | |
2089 | .commit = external_snapshot_commit, | |
2090 | .abort = external_snapshot_abort, | |
2091 | .clean = external_snapshot_clean, | |
2092 | }, | |
2093 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { | |
2094 | .instance_size = sizeof(ExternalSnapshotState), | |
2095 | .prepare = external_snapshot_prepare, | |
2096 | .commit = external_snapshot_commit, | |
2097 | .abort = external_snapshot_abort, | |
2098 | .clean = external_snapshot_clean, | |
2099 | }, | |
2100 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { | |
2101 | .instance_size = sizeof(DriveBackupState), | |
2102 | .prepare = drive_backup_prepare, | |
2103 | .commit = drive_backup_commit, | |
2104 | .abort = drive_backup_abort, | |
2105 | .clean = drive_backup_clean, | |
2106 | }, | |
2107 | [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { | |
2108 | .instance_size = sizeof(BlockdevBackupState), | |
2109 | .prepare = blockdev_backup_prepare, | |
2110 | .commit = blockdev_backup_commit, | |
2111 | .abort = blockdev_backup_abort, | |
2112 | .clean = blockdev_backup_clean, | |
2113 | }, | |
2114 | [TRANSACTION_ACTION_KIND_ABORT] = { | |
2115 | .instance_size = sizeof(BlkActionState), | |
2116 | .prepare = abort_prepare, | |
2117 | .commit = abort_commit, | |
2118 | }, | |
2119 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { | |
2120 | .instance_size = sizeof(InternalSnapshotState), | |
2121 | .prepare = internal_snapshot_prepare, | |
2122 | .abort = internal_snapshot_abort, | |
2123 | .clean = internal_snapshot_clean, | |
2124 | }, | |
2125 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { | |
2126 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2127 | .prepare = block_dirty_bitmap_add_prepare, | |
2128 | .abort = block_dirty_bitmap_add_abort, | |
2129 | }, | |
2130 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { | |
2131 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2132 | .prepare = block_dirty_bitmap_clear_prepare, | |
2133 | .commit = block_dirty_bitmap_clear_commit, | |
2134 | .abort = block_dirty_bitmap_clear_abort, | |
2135 | .clean = block_dirty_bitmap_clear_clean, | |
2136 | } | |
2137 | }; | |
2138 | ||
2139 | /** | |
2140 | * Allocate a TransactionProperties structure if necessary, and fill | |
2141 | * that structure with desired defaults if they are unset. | |
2142 | */ | |
2143 | static TransactionProperties *get_transaction_properties( | |
2144 | TransactionProperties *props) | |
2145 | { | |
2146 | if (!props) { | |
2147 | props = g_new0(TransactionProperties, 1); | |
2148 | } | |
2149 | ||
2150 | if (!props->has_completion_mode) { | |
2151 | props->has_completion_mode = true; | |
2152 | props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; | |
2153 | } | |
2154 | ||
2155 | return props; | |
2156 | } | |
2157 | ||
2158 | /* | |
2159 | * 'Atomic' group operations. The operations are performed as a set, and if | |
2160 | * any fail then we roll back all operations in the group. | |
2161 | */ | |
2162 | void qmp_transaction(TransactionActionList *dev_list, | |
2163 | bool has_props, | |
2164 | struct TransactionProperties *props, | |
2165 | Error **errp) | |
2166 | { | |
2167 | TransactionActionList *dev_entry = dev_list; | |
2168 | BlockJobTxn *block_job_txn = NULL; | |
2169 | BlkActionState *state, *next; | |
2170 | Error *local_err = NULL; | |
2171 | ||
2172 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; | |
2173 | QSIMPLEQ_INIT(&snap_bdrv_states); | |
2174 | ||
2175 | /* Does this transaction get canceled as a group on failure? | |
2176 | * If not, we don't really need to make a BlockJobTxn. | |
2177 | */ | |
2178 | props = get_transaction_properties(props); | |
2179 | if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
2180 | block_job_txn = block_job_txn_new(); | |
2181 | } | |
2182 | ||
2183 | /* drain all i/o before any operations */ | |
2184 | bdrv_drain_all(); | |
2185 | ||
2186 | /* We don't do anything in this loop that commits us to the operations */ | |
2187 | while (NULL != dev_entry) { | |
2188 | TransactionAction *dev_info = NULL; | |
2189 | const BlkActionOps *ops; | |
2190 | ||
2191 | dev_info = dev_entry->value; | |
2192 | dev_entry = dev_entry->next; | |
2193 | ||
2194 | assert(dev_info->type < ARRAY_SIZE(actions)); | |
2195 | ||
2196 | ops = &actions[dev_info->type]; | |
2197 | assert(ops->instance_size > 0); | |
2198 | ||
2199 | state = g_malloc0(ops->instance_size); | |
2200 | state->ops = ops; | |
2201 | state->action = dev_info; | |
2202 | state->block_job_txn = block_job_txn; | |
2203 | state->txn_props = props; | |
2204 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); | |
2205 | ||
2206 | state->ops->prepare(state, &local_err); | |
2207 | if (local_err) { | |
2208 | error_propagate(errp, local_err); | |
2209 | goto delete_and_fail; | |
2210 | } | |
2211 | } | |
2212 | ||
2213 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { | |
2214 | if (state->ops->commit) { | |
2215 | state->ops->commit(state); | |
2216 | } | |
2217 | } | |
2218 | ||
2219 | /* success */ | |
2220 | goto exit; | |
2221 | ||
2222 | delete_and_fail: | |
2223 | /* failure, and it is all-or-none; roll back all operations */ | |
2224 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { | |
2225 | if (state->ops->abort) { | |
2226 | state->ops->abort(state); | |
2227 | } | |
2228 | } | |
2229 | exit: | |
2230 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { | |
2231 | if (state->ops->clean) { | |
2232 | state->ops->clean(state); | |
2233 | } | |
2234 | g_free(state); | |
2235 | } | |
2236 | if (!has_props) { | |
2237 | qapi_free_TransactionProperties(props); | |
2238 | } | |
2239 | block_job_txn_unref(block_job_txn); | |
2240 | } | |
2241 | ||
2242 | void qmp_eject(bool has_device, const char *device, | |
2243 | bool has_id, const char *id, | |
2244 | bool has_force, bool force, Error **errp) | |
2245 | { | |
2246 | Error *local_err = NULL; | |
2247 | int rc; | |
2248 | ||
2249 | if (!has_force) { | |
2250 | force = false; | |
2251 | } | |
2252 | ||
2253 | rc = do_open_tray(has_device ? device : NULL, | |
2254 | has_id ? id : NULL, | |
2255 | force, &local_err); | |
2256 | if (rc && rc != -ENOSYS) { | |
2257 | error_propagate(errp, local_err); | |
2258 | return; | |
2259 | } | |
2260 | error_free(local_err); | |
2261 | ||
2262 | qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp); | |
2263 | } | |
2264 | ||
2265 | void qmp_block_passwd(bool has_device, const char *device, | |
2266 | bool has_node_name, const char *node_name, | |
2267 | const char *password, Error **errp) | |
2268 | { | |
2269 | error_setg(errp, | |
2270 | "Setting block passwords directly is no longer supported"); | |
2271 | } | |
2272 | ||
2273 | /* | |
2274 | * Attempt to open the tray of @device. | |
2275 | * If @force, ignore its tray lock. | |
2276 | * Else, if the tray is locked, don't open it, but ask the guest to open it. | |
2277 | * On error, store an error through @errp and return -errno. | |
2278 | * If @device does not exist, return -ENODEV. | |
2279 | * If it has no removable media, return -ENOTSUP. | |
2280 | * If it has no tray, return -ENOSYS. | |
2281 | * If the guest was asked to open the tray, return -EINPROGRESS. | |
2282 | * Else, return 0. | |
2283 | */ | |
2284 | static int do_open_tray(const char *blk_name, const char *qdev_id, | |
2285 | bool force, Error **errp) | |
2286 | { | |
2287 | BlockBackend *blk; | |
2288 | const char *device = qdev_id ?: blk_name; | |
2289 | bool locked; | |
2290 | ||
2291 | blk = qmp_get_blk(blk_name, qdev_id, errp); | |
2292 | if (!blk) { | |
2293 | return -ENODEV; | |
2294 | } | |
2295 | ||
2296 | if (!blk_dev_has_removable_media(blk)) { | |
2297 | error_setg(errp, "Device '%s' is not removable", device); | |
2298 | return -ENOTSUP; | |
2299 | } | |
2300 | ||
2301 | if (!blk_dev_has_tray(blk)) { | |
2302 | error_setg(errp, "Device '%s' does not have a tray", device); | |
2303 | return -ENOSYS; | |
2304 | } | |
2305 | ||
2306 | if (blk_dev_is_tray_open(blk)) { | |
2307 | return 0; | |
2308 | } | |
2309 | ||
2310 | locked = blk_dev_is_medium_locked(blk); | |
2311 | if (locked) { | |
2312 | blk_dev_eject_request(blk, force); | |
2313 | } | |
2314 | ||
2315 | if (!locked || force) { | |
2316 | blk_dev_change_media_cb(blk, false, &error_abort); | |
2317 | } | |
2318 | ||
2319 | if (locked && !force) { | |
2320 | error_setg(errp, "Device '%s' is locked and force was not specified, " | |
2321 | "wait for tray to open and try again", device); | |
2322 | return -EINPROGRESS; | |
2323 | } | |
2324 | ||
2325 | return 0; | |
2326 | } | |
2327 | ||
2328 | void qmp_blockdev_open_tray(bool has_device, const char *device, | |
2329 | bool has_id, const char *id, | |
2330 | bool has_force, bool force, | |
2331 | Error **errp) | |
2332 | { | |
2333 | Error *local_err = NULL; | |
2334 | int rc; | |
2335 | ||
2336 | if (!has_force) { | |
2337 | force = false; | |
2338 | } | |
2339 | rc = do_open_tray(has_device ? device : NULL, | |
2340 | has_id ? id : NULL, | |
2341 | force, &local_err); | |
2342 | if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { | |
2343 | error_propagate(errp, local_err); | |
2344 | return; | |
2345 | } | |
2346 | error_free(local_err); | |
2347 | } | |
2348 | ||
2349 | void qmp_blockdev_close_tray(bool has_device, const char *device, | |
2350 | bool has_id, const char *id, | |
2351 | Error **errp) | |
2352 | { | |
2353 | BlockBackend *blk; | |
2354 | Error *local_err = NULL; | |
2355 | ||
2356 | device = has_device ? device : NULL; | |
2357 | id = has_id ? id : NULL; | |
2358 | ||
2359 | blk = qmp_get_blk(device, id, errp); | |
2360 | if (!blk) { | |
2361 | return; | |
2362 | } | |
2363 | ||
2364 | if (!blk_dev_has_removable_media(blk)) { | |
2365 | error_setg(errp, "Device '%s' is not removable", device ?: id); | |
2366 | return; | |
2367 | } | |
2368 | ||
2369 | if (!blk_dev_has_tray(blk)) { | |
2370 | /* Ignore this command on tray-less devices */ | |
2371 | return; | |
2372 | } | |
2373 | ||
2374 | if (!blk_dev_is_tray_open(blk)) { | |
2375 | return; | |
2376 | } | |
2377 | ||
2378 | blk_dev_change_media_cb(blk, true, &local_err); | |
2379 | if (local_err) { | |
2380 | error_propagate(errp, local_err); | |
2381 | return; | |
2382 | } | |
2383 | } | |
2384 | ||
2385 | void qmp_x_blockdev_remove_medium(bool has_device, const char *device, | |
2386 | bool has_id, const char *id, Error **errp) | |
2387 | { | |
2388 | BlockBackend *blk; | |
2389 | BlockDriverState *bs; | |
2390 | AioContext *aio_context; | |
2391 | bool has_attached_device; | |
2392 | ||
2393 | device = has_device ? device : NULL; | |
2394 | id = has_id ? id : NULL; | |
2395 | ||
2396 | blk = qmp_get_blk(device, id, errp); | |
2397 | if (!blk) { | |
2398 | return; | |
2399 | } | |
2400 | ||
2401 | /* For BBs without a device, we can exchange the BDS tree at will */ | |
2402 | has_attached_device = blk_get_attached_dev(blk); | |
2403 | ||
2404 | if (has_attached_device && !blk_dev_has_removable_media(blk)) { | |
2405 | error_setg(errp, "Device '%s' is not removable", device ?: id); | |
2406 | return; | |
2407 | } | |
2408 | ||
2409 | if (has_attached_device && blk_dev_has_tray(blk) && | |
2410 | !blk_dev_is_tray_open(blk)) | |
2411 | { | |
2412 | error_setg(errp, "Tray of device '%s' is not open", device ?: id); | |
2413 | return; | |
2414 | } | |
2415 | ||
2416 | bs = blk_bs(blk); | |
2417 | if (!bs) { | |
2418 | return; | |
2419 | } | |
2420 | ||
2421 | aio_context = bdrv_get_aio_context(bs); | |
2422 | aio_context_acquire(aio_context); | |
2423 | ||
2424 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { | |
2425 | goto out; | |
2426 | } | |
2427 | ||
2428 | blk_remove_bs(blk); | |
2429 | ||
2430 | if (!blk_dev_has_tray(blk)) { | |
2431 | /* For tray-less devices, blockdev-open-tray is a no-op (or may not be | |
2432 | * called at all); therefore, the medium needs to be ejected here. | |
2433 | * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load | |
2434 | * value passed here (i.e. false). */ | |
2435 | blk_dev_change_media_cb(blk, false, &error_abort); | |
2436 | } | |
2437 | ||
2438 | out: | |
2439 | aio_context_release(aio_context); | |
2440 | } | |
2441 | ||
2442 | static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, | |
2443 | BlockDriverState *bs, Error **errp) | |
2444 | { | |
2445 | Error *local_err = NULL; | |
2446 | bool has_device; | |
2447 | int ret; | |
2448 | ||
2449 | /* For BBs without a device, we can exchange the BDS tree at will */ | |
2450 | has_device = blk_get_attached_dev(blk); | |
2451 | ||
2452 | if (has_device && !blk_dev_has_removable_media(blk)) { | |
2453 | error_setg(errp, "Device is not removable"); | |
2454 | return; | |
2455 | } | |
2456 | ||
2457 | if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { | |
2458 | error_setg(errp, "Tray of the device is not open"); | |
2459 | return; | |
2460 | } | |
2461 | ||
2462 | if (blk_bs(blk)) { | |
2463 | error_setg(errp, "There already is a medium in the device"); | |
2464 | return; | |
2465 | } | |
2466 | ||
2467 | ret = blk_insert_bs(blk, bs, errp); | |
2468 | if (ret < 0) { | |
2469 | return; | |
2470 | } | |
2471 | ||
2472 | if (!blk_dev_has_tray(blk)) { | |
2473 | /* For tray-less devices, blockdev-close-tray is a no-op (or may not be | |
2474 | * called at all); therefore, the medium needs to be pushed into the | |
2475 | * slot here. | |
2476 | * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load | |
2477 | * value passed here (i.e. true). */ | |
2478 | blk_dev_change_media_cb(blk, true, &local_err); | |
2479 | if (local_err) { | |
2480 | error_propagate(errp, local_err); | |
2481 | blk_remove_bs(blk); | |
2482 | return; | |
2483 | } | |
2484 | } | |
2485 | } | |
2486 | ||
2487 | void qmp_x_blockdev_insert_medium(bool has_device, const char *device, | |
2488 | bool has_id, const char *id, | |
2489 | const char *node_name, Error **errp) | |
2490 | { | |
2491 | BlockBackend *blk; | |
2492 | BlockDriverState *bs; | |
2493 | ||
2494 | blk = qmp_get_blk(has_device ? device : NULL, | |
2495 | has_id ? id : NULL, | |
2496 | errp); | |
2497 | if (!blk) { | |
2498 | return; | |
2499 | } | |
2500 | ||
2501 | bs = bdrv_find_node(node_name); | |
2502 | if (!bs) { | |
2503 | error_setg(errp, "Node '%s' not found", node_name); | |
2504 | return; | |
2505 | } | |
2506 | ||
2507 | if (bdrv_has_blk(bs)) { | |
2508 | error_setg(errp, "Node '%s' is already in use", node_name); | |
2509 | return; | |
2510 | } | |
2511 | ||
2512 | qmp_blockdev_insert_anon_medium(blk, bs, errp); | |
2513 | } | |
2514 | ||
2515 | void qmp_blockdev_change_medium(bool has_device, const char *device, | |
2516 | bool has_id, const char *id, | |
2517 | const char *filename, | |
2518 | bool has_format, const char *format, | |
2519 | bool has_read_only, | |
2520 | BlockdevChangeReadOnlyMode read_only, | |
2521 | Error **errp) | |
2522 | { | |
2523 | BlockBackend *blk; | |
2524 | BlockDriverState *medium_bs = NULL; | |
2525 | int bdrv_flags; | |
2526 | bool detect_zeroes; | |
2527 | int rc; | |
2528 | QDict *options = NULL; | |
2529 | Error *err = NULL; | |
2530 | ||
2531 | blk = qmp_get_blk(has_device ? device : NULL, | |
2532 | has_id ? id : NULL, | |
2533 | errp); | |
2534 | if (!blk) { | |
2535 | goto fail; | |
2536 | } | |
2537 | ||
2538 | if (blk_bs(blk)) { | |
2539 | blk_update_root_state(blk); | |
2540 | } | |
2541 | ||
2542 | bdrv_flags = blk_get_open_flags_from_root_state(blk); | |
2543 | bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | | |
2544 | BDRV_O_PROTOCOL); | |
2545 | ||
2546 | if (!has_read_only) { | |
2547 | read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; | |
2548 | } | |
2549 | ||
2550 | switch (read_only) { | |
2551 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: | |
2552 | break; | |
2553 | ||
2554 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: | |
2555 | bdrv_flags &= ~BDRV_O_RDWR; | |
2556 | break; | |
2557 | ||
2558 | case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: | |
2559 | bdrv_flags |= BDRV_O_RDWR; | |
2560 | break; | |
2561 | ||
2562 | default: | |
2563 | abort(); | |
2564 | } | |
2565 | ||
2566 | options = qdict_new(); | |
2567 | detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); | |
2568 | qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); | |
2569 | ||
2570 | if (has_format) { | |
2571 | qdict_put_str(options, "driver", format); | |
2572 | } | |
2573 | ||
2574 | medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); | |
2575 | if (!medium_bs) { | |
2576 | goto fail; | |
2577 | } | |
2578 | ||
2579 | rc = do_open_tray(has_device ? device : NULL, | |
2580 | has_id ? id : NULL, | |
2581 | false, &err); | |
2582 | if (rc && rc != -ENOSYS) { | |
2583 | error_propagate(errp, err); | |
2584 | goto fail; | |
2585 | } | |
2586 | error_free(err); | |
2587 | err = NULL; | |
2588 | ||
2589 | qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err); | |
2590 | if (err) { | |
2591 | error_propagate(errp, err); | |
2592 | goto fail; | |
2593 | } | |
2594 | ||
2595 | qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); | |
2596 | if (err) { | |
2597 | error_propagate(errp, err); | |
2598 | goto fail; | |
2599 | } | |
2600 | ||
2601 | qmp_blockdev_close_tray(has_device, device, has_id, id, errp); | |
2602 | ||
2603 | fail: | |
2604 | /* If the medium has been inserted, the device has its own reference, so | |
2605 | * ours must be relinquished; and if it has not been inserted successfully, | |
2606 | * the reference must be relinquished anyway */ | |
2607 | bdrv_unref(medium_bs); | |
2608 | } | |
2609 | ||
2610 | /* throttling disk I/O limits */ | |
2611 | void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) | |
2612 | { | |
2613 | ThrottleConfig cfg; | |
2614 | BlockDriverState *bs; | |
2615 | BlockBackend *blk; | |
2616 | AioContext *aio_context; | |
2617 | ||
2618 | blk = qmp_get_blk(arg->has_device ? arg->device : NULL, | |
2619 | arg->has_id ? arg->id : NULL, | |
2620 | errp); | |
2621 | if (!blk) { | |
2622 | return; | |
2623 | } | |
2624 | ||
2625 | aio_context = blk_get_aio_context(blk); | |
2626 | aio_context_acquire(aio_context); | |
2627 | ||
2628 | bs = blk_bs(blk); | |
2629 | if (!bs) { | |
2630 | error_setg(errp, "Device has no medium"); | |
2631 | goto out; | |
2632 | } | |
2633 | ||
2634 | throttle_config_init(&cfg); | |
2635 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; | |
2636 | cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; | |
2637 | cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; | |
2638 | ||
2639 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; | |
2640 | cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; | |
2641 | cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; | |
2642 | ||
2643 | if (arg->has_bps_max) { | |
2644 | cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; | |
2645 | } | |
2646 | if (arg->has_bps_rd_max) { | |
2647 | cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; | |
2648 | } | |
2649 | if (arg->has_bps_wr_max) { | |
2650 | cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; | |
2651 | } | |
2652 | if (arg->has_iops_max) { | |
2653 | cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; | |
2654 | } | |
2655 | if (arg->has_iops_rd_max) { | |
2656 | cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; | |
2657 | } | |
2658 | if (arg->has_iops_wr_max) { | |
2659 | cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; | |
2660 | } | |
2661 | ||
2662 | if (arg->has_bps_max_length) { | |
2663 | cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; | |
2664 | } | |
2665 | if (arg->has_bps_rd_max_length) { | |
2666 | cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; | |
2667 | } | |
2668 | if (arg->has_bps_wr_max_length) { | |
2669 | cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; | |
2670 | } | |
2671 | if (arg->has_iops_max_length) { | |
2672 | cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; | |
2673 | } | |
2674 | if (arg->has_iops_rd_max_length) { | |
2675 | cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; | |
2676 | } | |
2677 | if (arg->has_iops_wr_max_length) { | |
2678 | cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; | |
2679 | } | |
2680 | ||
2681 | if (arg->has_iops_size) { | |
2682 | cfg.op_size = arg->iops_size; | |
2683 | } | |
2684 | ||
2685 | if (!throttle_is_valid(&cfg, errp)) { | |
2686 | goto out; | |
2687 | } | |
2688 | ||
2689 | if (throttle_enabled(&cfg)) { | |
2690 | /* Enable I/O limits if they're not enabled yet, otherwise | |
2691 | * just update the throttling group. */ | |
2692 | if (!blk_get_public(blk)->throttle_state) { | |
2693 | blk_io_limits_enable(blk, | |
2694 | arg->has_group ? arg->group : | |
2695 | arg->has_device ? arg->device : | |
2696 | arg->id); | |
2697 | } else if (arg->has_group) { | |
2698 | blk_io_limits_update_group(blk, arg->group); | |
2699 | } | |
2700 | /* Set the new throttling configuration */ | |
2701 | blk_set_io_limits(blk, &cfg); | |
2702 | } else if (blk_get_public(blk)->throttle_state) { | |
2703 | /* If all throttling settings are set to 0, disable I/O limits */ | |
2704 | blk_io_limits_disable(blk); | |
2705 | } | |
2706 | ||
2707 | out: | |
2708 | aio_context_release(aio_context); | |
2709 | } | |
2710 | ||
2711 | void qmp_block_dirty_bitmap_add(const char *node, const char *name, | |
2712 | bool has_granularity, uint32_t granularity, | |
2713 | bool has_persistent, bool persistent, | |
2714 | bool has_autoload, bool autoload, | |
2715 | Error **errp) | |
2716 | { | |
2717 | BlockDriverState *bs; | |
2718 | BdrvDirtyBitmap *bitmap; | |
2719 | ||
2720 | if (!name || name[0] == '\0') { | |
2721 | error_setg(errp, "Bitmap name cannot be empty"); | |
2722 | return; | |
2723 | } | |
2724 | ||
2725 | bs = bdrv_lookup_bs(node, node, errp); | |
2726 | if (!bs) { | |
2727 | return; | |
2728 | } | |
2729 | ||
2730 | if (has_granularity) { | |
2731 | if (granularity < 512 || !is_power_of_2(granularity)) { | |
2732 | error_setg(errp, "Granularity must be power of 2 " | |
2733 | "and at least 512"); | |
2734 | return; | |
2735 | } | |
2736 | } else { | |
2737 | /* Default to cluster size, if available: */ | |
2738 | granularity = bdrv_get_default_bitmap_granularity(bs); | |
2739 | } | |
2740 | ||
2741 | if (!has_persistent) { | |
2742 | persistent = false; | |
2743 | } | |
2744 | if (!has_autoload) { | |
2745 | autoload = false; | |
2746 | } | |
2747 | ||
2748 | if (has_autoload && !persistent) { | |
2749 | error_setg(errp, "Autoload flag must be used only for persistent " | |
2750 | "bitmaps"); | |
2751 | return; | |
2752 | } | |
2753 | ||
2754 | if (persistent && | |
2755 | !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) | |
2756 | { | |
2757 | return; | |
2758 | } | |
2759 | ||
2760 | bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); | |
2761 | if (bitmap == NULL) { | |
2762 | return; | |
2763 | } | |
2764 | ||
2765 | bdrv_dirty_bitmap_set_persistance(bitmap, persistent); | |
2766 | bdrv_dirty_bitmap_set_autoload(bitmap, autoload); | |
2767 | } | |
2768 | ||
2769 | void qmp_block_dirty_bitmap_remove(const char *node, const char *name, | |
2770 | Error **errp) | |
2771 | { | |
2772 | BlockDriverState *bs; | |
2773 | BdrvDirtyBitmap *bitmap; | |
2774 | ||
2775 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2776 | if (!bitmap || !bs) { | |
2777 | return; | |
2778 | } | |
2779 | ||
2780 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
2781 | error_setg(errp, | |
2782 | "Bitmap '%s' is currently frozen and cannot be removed", | |
2783 | name); | |
2784 | return; | |
2785 | } | |
2786 | bdrv_dirty_bitmap_make_anon(bitmap); | |
2787 | bdrv_release_dirty_bitmap(bs, bitmap); | |
2788 | } | |
2789 | ||
2790 | /** | |
2791 | * Completely clear a bitmap, for the purposes of synchronizing a bitmap | |
2792 | * immediately after a full backup operation. | |
2793 | */ | |
2794 | void qmp_block_dirty_bitmap_clear(const char *node, const char *name, | |
2795 | Error **errp) | |
2796 | { | |
2797 | BdrvDirtyBitmap *bitmap; | |
2798 | BlockDriverState *bs; | |
2799 | ||
2800 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2801 | if (!bitmap || !bs) { | |
2802 | return; | |
2803 | } | |
2804 | ||
2805 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
2806 | error_setg(errp, | |
2807 | "Bitmap '%s' is currently frozen and cannot be modified", | |
2808 | name); | |
2809 | return; | |
2810 | } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
2811 | error_setg(errp, | |
2812 | "Bitmap '%s' is currently disabled and cannot be cleared", | |
2813 | name); | |
2814 | return; | |
2815 | } else if (bdrv_dirty_bitmap_readonly(bitmap)) { | |
2816 | error_setg(errp, "Bitmap '%s' is readonly and cannot be cleared", name); | |
2817 | return; | |
2818 | } | |
2819 | ||
2820 | bdrv_clear_dirty_bitmap(bitmap, NULL); | |
2821 | } | |
2822 | ||
2823 | BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, | |
2824 | const char *name, | |
2825 | Error **errp) | |
2826 | { | |
2827 | BdrvDirtyBitmap *bitmap; | |
2828 | BlockDriverState *bs; | |
2829 | BlockDirtyBitmapSha256 *ret = NULL; | |
2830 | char *sha256; | |
2831 | ||
2832 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2833 | if (!bitmap || !bs) { | |
2834 | return NULL; | |
2835 | } | |
2836 | ||
2837 | sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); | |
2838 | if (sha256 == NULL) { | |
2839 | return NULL; | |
2840 | } | |
2841 | ||
2842 | ret = g_new(BlockDirtyBitmapSha256, 1); | |
2843 | ret->sha256 = sha256; | |
2844 | ||
2845 | return ret; | |
2846 | } | |
2847 | ||
2848 | void hmp_drive_del(Monitor *mon, const QDict *qdict) | |
2849 | { | |
2850 | const char *id = qdict_get_str(qdict, "id"); | |
2851 | BlockBackend *blk; | |
2852 | BlockDriverState *bs; | |
2853 | AioContext *aio_context; | |
2854 | Error *local_err = NULL; | |
2855 | ||
2856 | bs = bdrv_find_node(id); | |
2857 | if (bs) { | |
2858 | qmp_blockdev_del(id, &local_err); | |
2859 | if (local_err) { | |
2860 | error_report_err(local_err); | |
2861 | } | |
2862 | return; | |
2863 | } | |
2864 | ||
2865 | blk = blk_by_name(id); | |
2866 | if (!blk) { | |
2867 | error_report("Device '%s' not found", id); | |
2868 | return; | |
2869 | } | |
2870 | ||
2871 | if (!blk_legacy_dinfo(blk)) { | |
2872 | error_report("Deleting device added with blockdev-add" | |
2873 | " is not supported"); | |
2874 | return; | |
2875 | } | |
2876 | ||
2877 | aio_context = blk_get_aio_context(blk); | |
2878 | aio_context_acquire(aio_context); | |
2879 | ||
2880 | bs = blk_bs(blk); | |
2881 | if (bs) { | |
2882 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { | |
2883 | error_report_err(local_err); | |
2884 | aio_context_release(aio_context); | |
2885 | return; | |
2886 | } | |
2887 | ||
2888 | blk_remove_bs(blk); | |
2889 | } | |
2890 | ||
2891 | /* Make the BlockBackend and the attached BlockDriverState anonymous */ | |
2892 | monitor_remove_blk(blk); | |
2893 | ||
2894 | /* If this BlockBackend has a device attached to it, its refcount will be | |
2895 | * decremented when the device is removed; otherwise we have to do so here. | |
2896 | */ | |
2897 | if (blk_get_attached_dev(blk)) { | |
2898 | /* Further I/O must not pause the guest */ | |
2899 | blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, | |
2900 | BLOCKDEV_ON_ERROR_REPORT); | |
2901 | } else { | |
2902 | blk_unref(blk); | |
2903 | } | |
2904 | ||
2905 | aio_context_release(aio_context); | |
2906 | } | |
2907 | ||
2908 | void qmp_block_resize(bool has_device, const char *device, | |
2909 | bool has_node_name, const char *node_name, | |
2910 | int64_t size, Error **errp) | |
2911 | { | |
2912 | Error *local_err = NULL; | |
2913 | BlockBackend *blk = NULL; | |
2914 | BlockDriverState *bs; | |
2915 | AioContext *aio_context; | |
2916 | int ret; | |
2917 | ||
2918 | bs = bdrv_lookup_bs(has_device ? device : NULL, | |
2919 | has_node_name ? node_name : NULL, | |
2920 | &local_err); | |
2921 | if (local_err) { | |
2922 | error_propagate(errp, local_err); | |
2923 | return; | |
2924 | } | |
2925 | ||
2926 | aio_context = bdrv_get_aio_context(bs); | |
2927 | aio_context_acquire(aio_context); | |
2928 | ||
2929 | if (!bdrv_is_first_non_filter(bs)) { | |
2930 | error_setg(errp, QERR_FEATURE_DISABLED, "resize"); | |
2931 | goto out; | |
2932 | } | |
2933 | ||
2934 | if (size < 0) { | |
2935 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); | |
2936 | goto out; | |
2937 | } | |
2938 | ||
2939 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { | |
2940 | error_setg(errp, QERR_DEVICE_IN_USE, device); | |
2941 | goto out; | |
2942 | } | |
2943 | ||
2944 | blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); | |
2945 | ret = blk_insert_bs(blk, bs, errp); | |
2946 | if (ret < 0) { | |
2947 | goto out; | |
2948 | } | |
2949 | ||
2950 | bdrv_drained_begin(bs); | |
2951 | ret = blk_truncate(blk, size, errp); | |
2952 | bdrv_drained_end(bs); | |
2953 | ||
2954 | out: | |
2955 | blk_unref(blk); | |
2956 | aio_context_release(aio_context); | |
2957 | } | |
2958 | ||
2959 | void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, | |
2960 | bool has_base, const char *base, | |
2961 | bool has_base_node, const char *base_node, | |
2962 | bool has_backing_file, const char *backing_file, | |
2963 | bool has_speed, int64_t speed, | |
2964 | bool has_on_error, BlockdevOnError on_error, | |
2965 | Error **errp) | |
2966 | { | |
2967 | BlockDriverState *bs, *iter; | |
2968 | BlockDriverState *base_bs = NULL; | |
2969 | AioContext *aio_context; | |
2970 | Error *local_err = NULL; | |
2971 | const char *base_name = NULL; | |
2972 | ||
2973 | if (!has_on_error) { | |
2974 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2975 | } | |
2976 | ||
2977 | bs = bdrv_lookup_bs(device, device, errp); | |
2978 | if (!bs) { | |
2979 | return; | |
2980 | } | |
2981 | ||
2982 | aio_context = bdrv_get_aio_context(bs); | |
2983 | aio_context_acquire(aio_context); | |
2984 | ||
2985 | if (has_base && has_base_node) { | |
2986 | error_setg(errp, "'base' and 'base-node' cannot be specified " | |
2987 | "at the same time"); | |
2988 | goto out; | |
2989 | } | |
2990 | ||
2991 | if (has_base) { | |
2992 | base_bs = bdrv_find_backing_image(bs, base); | |
2993 | if (base_bs == NULL) { | |
2994 | error_setg(errp, QERR_BASE_NOT_FOUND, base); | |
2995 | goto out; | |
2996 | } | |
2997 | assert(bdrv_get_aio_context(base_bs) == aio_context); | |
2998 | base_name = base; | |
2999 | } | |
3000 | ||
3001 | if (has_base_node) { | |
3002 | base_bs = bdrv_lookup_bs(NULL, base_node, errp); | |
3003 | if (!base_bs) { | |
3004 | goto out; | |
3005 | } | |
3006 | if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { | |
3007 | error_setg(errp, "Node '%s' is not a backing image of '%s'", | |
3008 | base_node, device); | |
3009 | goto out; | |
3010 | } | |
3011 | assert(bdrv_get_aio_context(base_bs) == aio_context); | |
3012 | base_name = base_bs->filename; | |
3013 | } | |
3014 | ||
3015 | /* Check for op blockers in the whole chain between bs and base */ | |
3016 | for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { | |
3017 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { | |
3018 | goto out; | |
3019 | } | |
3020 | } | |
3021 | ||
3022 | /* if we are streaming the entire chain, the result will have no backing | |
3023 | * file, and specifying one is therefore an error */ | |
3024 | if (base_bs == NULL && has_backing_file) { | |
3025 | error_setg(errp, "backing file specified, but streaming the " | |
3026 | "entire chain"); | |
3027 | goto out; | |
3028 | } | |
3029 | ||
3030 | /* backing_file string overrides base bs filename */ | |
3031 | base_name = has_backing_file ? backing_file : base_name; | |
3032 | ||
3033 | stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, | |
3034 | has_speed ? speed : 0, on_error, &local_err); | |
3035 | if (local_err) { | |
3036 | error_propagate(errp, local_err); | |
3037 | goto out; | |
3038 | } | |
3039 | ||
3040 | trace_qmp_block_stream(bs, bs->job); | |
3041 | ||
3042 | out: | |
3043 | aio_context_release(aio_context); | |
3044 | } | |
3045 | ||
3046 | void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, | |
3047 | bool has_base, const char *base, | |
3048 | bool has_top, const char *top, | |
3049 | bool has_backing_file, const char *backing_file, | |
3050 | bool has_speed, int64_t speed, | |
3051 | bool has_filter_node_name, const char *filter_node_name, | |
3052 | Error **errp) | |
3053 | { | |
3054 | BlockDriverState *bs; | |
3055 | BlockDriverState *iter; | |
3056 | BlockDriverState *base_bs, *top_bs; | |
3057 | AioContext *aio_context; | |
3058 | Error *local_err = NULL; | |
3059 | /* This will be part of the QMP command, if/when the | |
3060 | * BlockdevOnError change for blkmirror makes it in | |
3061 | */ | |
3062 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; | |
3063 | ||
3064 | if (!has_speed) { | |
3065 | speed = 0; | |
3066 | } | |
3067 | if (!has_filter_node_name) { | |
3068 | filter_node_name = NULL; | |
3069 | } | |
3070 | ||
3071 | /* Important Note: | |
3072 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
3073 | * live commit feature versions; for this to work, we must make sure to | |
3074 | * perform the device lookup before any generic errors that may occur in a | |
3075 | * scenario in which all optional arguments are omitted. */ | |
3076 | bs = qmp_get_root_bs(device, &local_err); | |
3077 | if (!bs) { | |
3078 | bs = bdrv_lookup_bs(device, device, NULL); | |
3079 | if (!bs) { | |
3080 | error_free(local_err); | |
3081 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
3082 | "Device '%s' not found", device); | |
3083 | } else { | |
3084 | error_propagate(errp, local_err); | |
3085 | } | |
3086 | return; | |
3087 | } | |
3088 | ||
3089 | aio_context = bdrv_get_aio_context(bs); | |
3090 | aio_context_acquire(aio_context); | |
3091 | ||
3092 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { | |
3093 | goto out; | |
3094 | } | |
3095 | ||
3096 | /* default top_bs is the active layer */ | |
3097 | top_bs = bs; | |
3098 | ||
3099 | if (has_top && top) { | |
3100 | if (strcmp(bs->filename, top) != 0) { | |
3101 | top_bs = bdrv_find_backing_image(bs, top); | |
3102 | } | |
3103 | } | |
3104 | ||
3105 | if (top_bs == NULL) { | |
3106 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
3107 | goto out; | |
3108 | } | |
3109 | ||
3110 | assert(bdrv_get_aio_context(top_bs) == aio_context); | |
3111 | ||
3112 | if (has_base && base) { | |
3113 | base_bs = bdrv_find_backing_image(top_bs, base); | |
3114 | } else { | |
3115 | base_bs = bdrv_find_base(top_bs); | |
3116 | } | |
3117 | ||
3118 | if (base_bs == NULL) { | |
3119 | error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); | |
3120 | goto out; | |
3121 | } | |
3122 | ||
3123 | assert(bdrv_get_aio_context(base_bs) == aio_context); | |
3124 | ||
3125 | for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { | |
3126 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { | |
3127 | goto out; | |
3128 | } | |
3129 | } | |
3130 | ||
3131 | /* Do not allow attempts to commit an image into itself */ | |
3132 | if (top_bs == base_bs) { | |
3133 | error_setg(errp, "cannot commit an image into itself"); | |
3134 | goto out; | |
3135 | } | |
3136 | ||
3137 | if (top_bs == bs) { | |
3138 | if (has_backing_file) { | |
3139 | error_setg(errp, "'backing-file' specified," | |
3140 | " but 'top' is the active layer"); | |
3141 | goto out; | |
3142 | } | |
3143 | commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, | |
3144 | BLOCK_JOB_DEFAULT, speed, on_error, | |
3145 | filter_node_name, NULL, NULL, false, &local_err); | |
3146 | } else { | |
3147 | BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); | |
3148 | if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { | |
3149 | goto out; | |
3150 | } | |
3151 | commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, | |
3152 | on_error, has_backing_file ? backing_file : NULL, | |
3153 | filter_node_name, &local_err); | |
3154 | } | |
3155 | if (local_err != NULL) { | |
3156 | error_propagate(errp, local_err); | |
3157 | goto out; | |
3158 | } | |
3159 | ||
3160 | out: | |
3161 | aio_context_release(aio_context); | |
3162 | } | |
3163 | ||
3164 | static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, | |
3165 | Error **errp) | |
3166 | { | |
3167 | BlockDriverState *bs; | |
3168 | BlockDriverState *target_bs; | |
3169 | BlockDriverState *source = NULL; | |
3170 | BlockJob *job = NULL; | |
3171 | BdrvDirtyBitmap *bmap = NULL; | |
3172 | AioContext *aio_context; | |
3173 | QDict *options = NULL; | |
3174 | Error *local_err = NULL; | |
3175 | int flags; | |
3176 | int64_t size; | |
3177 | bool set_backing_hd = false; | |
3178 | ||
3179 | if (!backup->has_speed) { | |
3180 | backup->speed = 0; | |
3181 | } | |
3182 | if (!backup->has_on_source_error) { | |
3183 | backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3184 | } | |
3185 | if (!backup->has_on_target_error) { | |
3186 | backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3187 | } | |
3188 | if (!backup->has_mode) { | |
3189 | backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
3190 | } | |
3191 | if (!backup->has_job_id) { | |
3192 | backup->job_id = NULL; | |
3193 | } | |
3194 | if (!backup->has_compress) { | |
3195 | backup->compress = false; | |
3196 | } | |
3197 | ||
3198 | bs = qmp_get_root_bs(backup->device, errp); | |
3199 | if (!bs) { | |
3200 | return NULL; | |
3201 | } | |
3202 | ||
3203 | aio_context = bdrv_get_aio_context(bs); | |
3204 | aio_context_acquire(aio_context); | |
3205 | ||
3206 | if (!backup->has_format) { | |
3207 | backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? | |
3208 | NULL : (char*) bs->drv->format_name; | |
3209 | } | |
3210 | ||
3211 | /* Early check to avoid creating target */ | |
3212 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { | |
3213 | goto out; | |
3214 | } | |
3215 | ||
3216 | flags = bs->open_flags | BDRV_O_RDWR; | |
3217 | ||
3218 | /* See if we have a backing HD we can use to create our new image | |
3219 | * on top of. */ | |
3220 | if (backup->sync == MIRROR_SYNC_MODE_TOP) { | |
3221 | source = backing_bs(bs); | |
3222 | if (!source) { | |
3223 | backup->sync = MIRROR_SYNC_MODE_FULL; | |
3224 | } | |
3225 | } | |
3226 | if (backup->sync == MIRROR_SYNC_MODE_NONE) { | |
3227 | source = bs; | |
3228 | flags |= BDRV_O_NO_BACKING; | |
3229 | set_backing_hd = true; | |
3230 | } | |
3231 | ||
3232 | size = bdrv_getlength(bs); | |
3233 | if (size < 0) { | |
3234 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
3235 | goto out; | |
3236 | } | |
3237 | ||
3238 | if (backup->mode != NEW_IMAGE_MODE_EXISTING) { | |
3239 | assert(backup->format); | |
3240 | if (source) { | |
3241 | bdrv_img_create(backup->target, backup->format, source->filename, | |
3242 | source->drv->format_name, NULL, | |
3243 | size, flags, false, &local_err); | |
3244 | } else { | |
3245 | bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, | |
3246 | size, flags, false, &local_err); | |
3247 | } | |
3248 | } | |
3249 | ||
3250 | if (local_err) { | |
3251 | error_propagate(errp, local_err); | |
3252 | goto out; | |
3253 | } | |
3254 | ||
3255 | if (backup->format) { | |
3256 | if (!options) { | |
3257 | options = qdict_new(); | |
3258 | } | |
3259 | qdict_put_str(options, "driver", backup->format); | |
3260 | } | |
3261 | ||
3262 | target_bs = bdrv_open(backup->target, NULL, options, flags, errp); | |
3263 | if (!target_bs) { | |
3264 | goto out; | |
3265 | } | |
3266 | ||
3267 | bdrv_set_aio_context(target_bs, aio_context); | |
3268 | ||
3269 | if (set_backing_hd) { | |
3270 | bdrv_set_backing_hd(target_bs, source, &local_err); | |
3271 | if (local_err) { | |
3272 | bdrv_unref(target_bs); | |
3273 | goto out; | |
3274 | } | |
3275 | } | |
3276 | ||
3277 | if (backup->has_bitmap) { | |
3278 | bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); | |
3279 | if (!bmap) { | |
3280 | error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); | |
3281 | bdrv_unref(target_bs); | |
3282 | goto out; | |
3283 | } | |
3284 | } | |
3285 | ||
3286 | job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, | |
3287 | backup->sync, bmap, backup->compress, | |
3288 | backup->on_source_error, backup->on_target_error, | |
3289 | BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err); | |
3290 | bdrv_unref(target_bs); | |
3291 | if (local_err != NULL) { | |
3292 | error_propagate(errp, local_err); | |
3293 | goto out; | |
3294 | } | |
3295 | ||
3296 | out: | |
3297 | aio_context_release(aio_context); | |
3298 | return job; | |
3299 | } | |
3300 | ||
3301 | void qmp_drive_backup(DriveBackup *arg, Error **errp) | |
3302 | { | |
3303 | ||
3304 | BlockJob *job; | |
3305 | job = do_drive_backup(arg, NULL, errp); | |
3306 | if (job) { | |
3307 | block_job_start(job); | |
3308 | } | |
3309 | } | |
3310 | ||
3311 | BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) | |
3312 | { | |
3313 | return bdrv_named_nodes_list(errp); | |
3314 | } | |
3315 | ||
3316 | BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, | |
3317 | Error **errp) | |
3318 | { | |
3319 | BlockDriverState *bs; | |
3320 | BlockDriverState *target_bs; | |
3321 | Error *local_err = NULL; | |
3322 | AioContext *aio_context; | |
3323 | BlockJob *job = NULL; | |
3324 | ||
3325 | if (!backup->has_speed) { | |
3326 | backup->speed = 0; | |
3327 | } | |
3328 | if (!backup->has_on_source_error) { | |
3329 | backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3330 | } | |
3331 | if (!backup->has_on_target_error) { | |
3332 | backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3333 | } | |
3334 | if (!backup->has_job_id) { | |
3335 | backup->job_id = NULL; | |
3336 | } | |
3337 | if (!backup->has_compress) { | |
3338 | backup->compress = false; | |
3339 | } | |
3340 | ||
3341 | bs = qmp_get_root_bs(backup->device, errp); | |
3342 | if (!bs) { | |
3343 | return NULL; | |
3344 | } | |
3345 | ||
3346 | aio_context = bdrv_get_aio_context(bs); | |
3347 | aio_context_acquire(aio_context); | |
3348 | ||
3349 | target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); | |
3350 | if (!target_bs) { | |
3351 | goto out; | |
3352 | } | |
3353 | ||
3354 | if (bdrv_get_aio_context(target_bs) != aio_context) { | |
3355 | if (!bdrv_has_blk(target_bs)) { | |
3356 | /* The target BDS is not attached, we can safely move it to another | |
3357 | * AioContext. */ | |
3358 | bdrv_set_aio_context(target_bs, aio_context); | |
3359 | } else { | |
3360 | error_setg(errp, "Target is attached to a different thread from " | |
3361 | "source."); | |
3362 | goto out; | |
3363 | } | |
3364 | } | |
3365 | job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, | |
3366 | backup->sync, NULL, backup->compress, | |
3367 | backup->on_source_error, backup->on_target_error, | |
3368 | BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err); | |
3369 | if (local_err != NULL) { | |
3370 | error_propagate(errp, local_err); | |
3371 | } | |
3372 | out: | |
3373 | aio_context_release(aio_context); | |
3374 | return job; | |
3375 | } | |
3376 | ||
3377 | void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) | |
3378 | { | |
3379 | BlockJob *job; | |
3380 | job = do_blockdev_backup(arg, NULL, errp); | |
3381 | if (job) { | |
3382 | block_job_start(job); | |
3383 | } | |
3384 | } | |
3385 | ||
3386 | /* Parameter check and block job starting for drive mirroring. | |
3387 | * Caller should hold @device and @target's aio context (must be the same). | |
3388 | **/ | |
3389 | static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, | |
3390 | BlockDriverState *target, | |
3391 | bool has_replaces, const char *replaces, | |
3392 | enum MirrorSyncMode sync, | |
3393 | BlockMirrorBackingMode backing_mode, | |
3394 | bool has_speed, int64_t speed, | |
3395 | bool has_granularity, uint32_t granularity, | |
3396 | bool has_buf_size, int64_t buf_size, | |
3397 | bool has_on_source_error, | |
3398 | BlockdevOnError on_source_error, | |
3399 | bool has_on_target_error, | |
3400 | BlockdevOnError on_target_error, | |
3401 | bool has_unmap, bool unmap, | |
3402 | bool has_filter_node_name, | |
3403 | const char *filter_node_name, | |
3404 | Error **errp) | |
3405 | { | |
3406 | ||
3407 | if (!has_speed) { | |
3408 | speed = 0; | |
3409 | } | |
3410 | if (!has_on_source_error) { | |
3411 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3412 | } | |
3413 | if (!has_on_target_error) { | |
3414 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3415 | } | |
3416 | if (!has_granularity) { | |
3417 | granularity = 0; | |
3418 | } | |
3419 | if (!has_buf_size) { | |
3420 | buf_size = 0; | |
3421 | } | |
3422 | if (!has_unmap) { | |
3423 | unmap = true; | |
3424 | } | |
3425 | if (!has_filter_node_name) { | |
3426 | filter_node_name = NULL; | |
3427 | } | |
3428 | ||
3429 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { | |
3430 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", | |
3431 | "a value in range [512B, 64MB]"); | |
3432 | return; | |
3433 | } | |
3434 | if (granularity & (granularity - 1)) { | |
3435 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", | |
3436 | "power of 2"); | |
3437 | return; | |
3438 | } | |
3439 | ||
3440 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { | |
3441 | return; | |
3442 | } | |
3443 | if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { | |
3444 | return; | |
3445 | } | |
3446 | ||
3447 | if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { | |
3448 | sync = MIRROR_SYNC_MODE_FULL; | |
3449 | } | |
3450 | ||
3451 | /* pass the node name to replace to mirror start since it's loose coupling | |
3452 | * and will allow to check whether the node still exist at mirror completion | |
3453 | */ | |
3454 | mirror_start(job_id, bs, target, | |
3455 | has_replaces ? replaces : NULL, | |
3456 | speed, granularity, buf_size, sync, backing_mode, | |
3457 | on_source_error, on_target_error, unmap, filter_node_name, | |
3458 | errp); | |
3459 | } | |
3460 | ||
3461 | void qmp_drive_mirror(DriveMirror *arg, Error **errp) | |
3462 | { | |
3463 | BlockDriverState *bs; | |
3464 | BlockDriverState *source, *target_bs; | |
3465 | AioContext *aio_context; | |
3466 | BlockMirrorBackingMode backing_mode; | |
3467 | Error *local_err = NULL; | |
3468 | QDict *options = NULL; | |
3469 | int flags; | |
3470 | int64_t size; | |
3471 | const char *format = arg->format; | |
3472 | ||
3473 | bs = qmp_get_root_bs(arg->device, errp); | |
3474 | if (!bs) { | |
3475 | return; | |
3476 | } | |
3477 | ||
3478 | aio_context = bdrv_get_aio_context(bs); | |
3479 | aio_context_acquire(aio_context); | |
3480 | ||
3481 | if (!arg->has_mode) { | |
3482 | arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
3483 | } | |
3484 | ||
3485 | if (!arg->has_format) { | |
3486 | format = (arg->mode == NEW_IMAGE_MODE_EXISTING | |
3487 | ? NULL : bs->drv->format_name); | |
3488 | } | |
3489 | ||
3490 | flags = bs->open_flags | BDRV_O_RDWR; | |
3491 | source = backing_bs(bs); | |
3492 | if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { | |
3493 | arg->sync = MIRROR_SYNC_MODE_FULL; | |
3494 | } | |
3495 | if (arg->sync == MIRROR_SYNC_MODE_NONE) { | |
3496 | source = bs; | |
3497 | } | |
3498 | ||
3499 | size = bdrv_getlength(bs); | |
3500 | if (size < 0) { | |
3501 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
3502 | goto out; | |
3503 | } | |
3504 | ||
3505 | if (arg->has_replaces) { | |
3506 | BlockDriverState *to_replace_bs; | |
3507 | AioContext *replace_aio_context; | |
3508 | int64_t replace_size; | |
3509 | ||
3510 | if (!arg->has_node_name) { | |
3511 | error_setg(errp, "a node-name must be provided when replacing a" | |
3512 | " named node of the graph"); | |
3513 | goto out; | |
3514 | } | |
3515 | ||
3516 | to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); | |
3517 | ||
3518 | if (!to_replace_bs) { | |
3519 | error_propagate(errp, local_err); | |
3520 | goto out; | |
3521 | } | |
3522 | ||
3523 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); | |
3524 | aio_context_acquire(replace_aio_context); | |
3525 | replace_size = bdrv_getlength(to_replace_bs); | |
3526 | aio_context_release(replace_aio_context); | |
3527 | ||
3528 | if (size != replace_size) { | |
3529 | error_setg(errp, "cannot replace image with a mirror image of " | |
3530 | "different size"); | |
3531 | goto out; | |
3532 | } | |
3533 | } | |
3534 | ||
3535 | if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { | |
3536 | backing_mode = MIRROR_SOURCE_BACKING_CHAIN; | |
3537 | } else { | |
3538 | backing_mode = MIRROR_OPEN_BACKING_CHAIN; | |
3539 | } | |
3540 | ||
3541 | if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) | |
3542 | && arg->mode != NEW_IMAGE_MODE_EXISTING) | |
3543 | { | |
3544 | /* create new image w/o backing file */ | |
3545 | assert(format); | |
3546 | bdrv_img_create(arg->target, format, | |
3547 | NULL, NULL, NULL, size, flags, false, &local_err); | |
3548 | } else { | |
3549 | switch (arg->mode) { | |
3550 | case NEW_IMAGE_MODE_EXISTING: | |
3551 | break; | |
3552 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
3553 | /* create new image with backing file */ | |
3554 | bdrv_img_create(arg->target, format, | |
3555 | source->filename, | |
3556 | source->drv->format_name, | |
3557 | NULL, size, flags, false, &local_err); | |
3558 | break; | |
3559 | default: | |
3560 | abort(); | |
3561 | } | |
3562 | } | |
3563 | ||
3564 | if (local_err) { | |
3565 | error_propagate(errp, local_err); | |
3566 | goto out; | |
3567 | } | |
3568 | ||
3569 | options = qdict_new(); | |
3570 | if (arg->has_node_name) { | |
3571 | qdict_put_str(options, "node-name", arg->node_name); | |
3572 | } | |
3573 | if (format) { | |
3574 | qdict_put_str(options, "driver", format); | |
3575 | } | |
3576 | ||
3577 | /* Mirroring takes care of copy-on-write using the source's backing | |
3578 | * file. | |
3579 | */ | |
3580 | target_bs = bdrv_open(arg->target, NULL, options, | |
3581 | flags | BDRV_O_NO_BACKING, errp); | |
3582 | if (!target_bs) { | |
3583 | goto out; | |
3584 | } | |
3585 | ||
3586 | bdrv_set_aio_context(target_bs, aio_context); | |
3587 | ||
3588 | blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, | |
3589 | arg->has_replaces, arg->replaces, arg->sync, | |
3590 | backing_mode, arg->has_speed, arg->speed, | |
3591 | arg->has_granularity, arg->granularity, | |
3592 | arg->has_buf_size, arg->buf_size, | |
3593 | arg->has_on_source_error, arg->on_source_error, | |
3594 | arg->has_on_target_error, arg->on_target_error, | |
3595 | arg->has_unmap, arg->unmap, | |
3596 | false, NULL, | |
3597 | &local_err); | |
3598 | bdrv_unref(target_bs); | |
3599 | error_propagate(errp, local_err); | |
3600 | out: | |
3601 | aio_context_release(aio_context); | |
3602 | } | |
3603 | ||
3604 | void qmp_blockdev_mirror(bool has_job_id, const char *job_id, | |
3605 | const char *device, const char *target, | |
3606 | bool has_replaces, const char *replaces, | |
3607 | MirrorSyncMode sync, | |
3608 | bool has_speed, int64_t speed, | |
3609 | bool has_granularity, uint32_t granularity, | |
3610 | bool has_buf_size, int64_t buf_size, | |
3611 | bool has_on_source_error, | |
3612 | BlockdevOnError on_source_error, | |
3613 | bool has_on_target_error, | |
3614 | BlockdevOnError on_target_error, | |
3615 | bool has_filter_node_name, | |
3616 | const char *filter_node_name, | |
3617 | Error **errp) | |
3618 | { | |
3619 | BlockDriverState *bs; | |
3620 | BlockDriverState *target_bs; | |
3621 | AioContext *aio_context; | |
3622 | BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; | |
3623 | Error *local_err = NULL; | |
3624 | ||
3625 | bs = qmp_get_root_bs(device, errp); | |
3626 | if (!bs) { | |
3627 | return; | |
3628 | } | |
3629 | ||
3630 | target_bs = bdrv_lookup_bs(target, target, errp); | |
3631 | if (!target_bs) { | |
3632 | return; | |
3633 | } | |
3634 | ||
3635 | aio_context = bdrv_get_aio_context(bs); | |
3636 | aio_context_acquire(aio_context); | |
3637 | ||
3638 | bdrv_set_aio_context(target_bs, aio_context); | |
3639 | ||
3640 | blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, | |
3641 | has_replaces, replaces, sync, backing_mode, | |
3642 | has_speed, speed, | |
3643 | has_granularity, granularity, | |
3644 | has_buf_size, buf_size, | |
3645 | has_on_source_error, on_source_error, | |
3646 | has_on_target_error, on_target_error, | |
3647 | true, true, | |
3648 | has_filter_node_name, filter_node_name, | |
3649 | &local_err); | |
3650 | error_propagate(errp, local_err); | |
3651 | ||
3652 | aio_context_release(aio_context); | |
3653 | } | |
3654 | ||
3655 | /* Get a block job using its ID and acquire its AioContext */ | |
3656 | static BlockJob *find_block_job(const char *id, AioContext **aio_context, | |
3657 | Error **errp) | |
3658 | { | |
3659 | BlockJob *job; | |
3660 | ||
3661 | assert(id != NULL); | |
3662 | ||
3663 | *aio_context = NULL; | |
3664 | ||
3665 | job = block_job_get(id); | |
3666 | ||
3667 | if (!job) { | |
3668 | error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, | |
3669 | "Block job '%s' not found", id); | |
3670 | return NULL; | |
3671 | } | |
3672 | ||
3673 | *aio_context = blk_get_aio_context(job->blk); | |
3674 | aio_context_acquire(*aio_context); | |
3675 | ||
3676 | return job; | |
3677 | } | |
3678 | ||
3679 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) | |
3680 | { | |
3681 | AioContext *aio_context; | |
3682 | BlockJob *job = find_block_job(device, &aio_context, errp); | |
3683 | ||
3684 | if (!job) { | |
3685 | return; | |
3686 | } | |
3687 | ||
3688 | block_job_set_speed(job, speed, errp); | |
3689 | aio_context_release(aio_context); | |
3690 | } | |
3691 | ||
3692 | void qmp_block_job_cancel(const char *device, | |
3693 | bool has_force, bool force, Error **errp) | |
3694 | { | |
3695 | AioContext *aio_context; | |
3696 | BlockJob *job = find_block_job(device, &aio_context, errp); | |
3697 | ||
3698 | if (!job) { | |
3699 | return; | |
3700 | } | |
3701 | ||
3702 | if (!has_force) { | |
3703 | force = false; | |
3704 | } | |
3705 | ||
3706 | if (block_job_user_paused(job) && !force) { | |
3707 | error_setg(errp, "The block job for device '%s' is currently paused", | |
3708 | device); | |
3709 | goto out; | |
3710 | } | |
3711 | ||
3712 | trace_qmp_block_job_cancel(job); | |
3713 | block_job_cancel(job); | |
3714 | out: | |
3715 | aio_context_release(aio_context); | |
3716 | } | |
3717 | ||
3718 | void qmp_block_job_pause(const char *device, Error **errp) | |
3719 | { | |
3720 | AioContext *aio_context; | |
3721 | BlockJob *job = find_block_job(device, &aio_context, errp); | |
3722 | ||
3723 | if (!job || block_job_user_paused(job)) { | |
3724 | return; | |
3725 | } | |
3726 | ||
3727 | trace_qmp_block_job_pause(job); | |
3728 | block_job_user_pause(job); | |
3729 | aio_context_release(aio_context); | |
3730 | } | |
3731 | ||
3732 | void qmp_block_job_resume(const char *device, Error **errp) | |
3733 | { | |
3734 | AioContext *aio_context; | |
3735 | BlockJob *job = find_block_job(device, &aio_context, errp); | |
3736 | ||
3737 | if (!job || !block_job_user_paused(job)) { | |
3738 | return; | |
3739 | } | |
3740 | ||
3741 | trace_qmp_block_job_resume(job); | |
3742 | block_job_user_resume(job); | |
3743 | aio_context_release(aio_context); | |
3744 | } | |
3745 | ||
3746 | void qmp_block_job_complete(const char *device, Error **errp) | |
3747 | { | |
3748 | AioContext *aio_context; | |
3749 | BlockJob *job = find_block_job(device, &aio_context, errp); | |
3750 | ||
3751 | if (!job) { | |
3752 | return; | |
3753 | } | |
3754 | ||
3755 | trace_qmp_block_job_complete(job); | |
3756 | block_job_complete(job, errp); | |
3757 | aio_context_release(aio_context); | |
3758 | } | |
3759 | ||
3760 | void qmp_change_backing_file(const char *device, | |
3761 | const char *image_node_name, | |
3762 | const char *backing_file, | |
3763 | Error **errp) | |
3764 | { | |
3765 | BlockDriverState *bs = NULL; | |
3766 | AioContext *aio_context; | |
3767 | BlockDriverState *image_bs = NULL; | |
3768 | Error *local_err = NULL; | |
3769 | bool ro; | |
3770 | int open_flags; | |
3771 | int ret; | |
3772 | ||
3773 | bs = qmp_get_root_bs(device, errp); | |
3774 | if (!bs) { | |
3775 | return; | |
3776 | } | |
3777 | ||
3778 | aio_context = bdrv_get_aio_context(bs); | |
3779 | aio_context_acquire(aio_context); | |
3780 | ||
3781 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); | |
3782 | if (local_err) { | |
3783 | error_propagate(errp, local_err); | |
3784 | goto out; | |
3785 | } | |
3786 | ||
3787 | if (!image_bs) { | |
3788 | error_setg(errp, "image file not found"); | |
3789 | goto out; | |
3790 | } | |
3791 | ||
3792 | if (bdrv_find_base(image_bs) == image_bs) { | |
3793 | error_setg(errp, "not allowing backing file change on an image " | |
3794 | "without a backing file"); | |
3795 | goto out; | |
3796 | } | |
3797 | ||
3798 | /* even though we are not necessarily operating on bs, we need it to | |
3799 | * determine if block ops are currently prohibited on the chain */ | |
3800 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
3801 | goto out; | |
3802 | } | |
3803 | ||
3804 | /* final sanity check */ | |
3805 | if (!bdrv_chain_contains(bs, image_bs)) { | |
3806 | error_setg(errp, "'%s' and image file are not in the same chain", | |
3807 | device); | |
3808 | goto out; | |
3809 | } | |
3810 | ||
3811 | /* if not r/w, reopen to make r/w */ | |
3812 | open_flags = image_bs->open_flags; | |
3813 | ro = bdrv_is_read_only(image_bs); | |
3814 | ||
3815 | if (ro) { | |
3816 | bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); | |
3817 | if (local_err) { | |
3818 | error_propagate(errp, local_err); | |
3819 | goto out; | |
3820 | } | |
3821 | } | |
3822 | ||
3823 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
3824 | image_bs->drv ? image_bs->drv->format_name : ""); | |
3825 | ||
3826 | if (ret < 0) { | |
3827 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
3828 | backing_file); | |
3829 | /* don't exit here, so we can try to restore open flags if | |
3830 | * appropriate */ | |
3831 | } | |
3832 | ||
3833 | if (ro) { | |
3834 | bdrv_reopen(image_bs, open_flags, &local_err); | |
3835 | error_propagate(errp, local_err); | |
3836 | } | |
3837 | ||
3838 | out: | |
3839 | aio_context_release(aio_context); | |
3840 | } | |
3841 | ||
3842 | void hmp_drive_add_node(Monitor *mon, const char *optstr) | |
3843 | { | |
3844 | QemuOpts *opts; | |
3845 | QDict *qdict; | |
3846 | Error *local_err = NULL; | |
3847 | ||
3848 | opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); | |
3849 | if (!opts) { | |
3850 | return; | |
3851 | } | |
3852 | ||
3853 | qdict = qemu_opts_to_qdict(opts, NULL); | |
3854 | ||
3855 | if (!qdict_get_try_str(qdict, "node-name")) { | |
3856 | QDECREF(qdict); | |
3857 | error_report("'node-name' needs to be specified"); | |
3858 | goto out; | |
3859 | } | |
3860 | ||
3861 | BlockDriverState *bs = bds_tree_init(qdict, &local_err); | |
3862 | if (!bs) { | |
3863 | error_report_err(local_err); | |
3864 | goto out; | |
3865 | } | |
3866 | ||
3867 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); | |
3868 | ||
3869 | out: | |
3870 | qemu_opts_del(opts); | |
3871 | } | |
3872 | ||
3873 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) | |
3874 | { | |
3875 | BlockDriverState *bs; | |
3876 | QObject *obj; | |
3877 | Visitor *v = qobject_output_visitor_new(&obj); | |
3878 | QDict *qdict; | |
3879 | Error *local_err = NULL; | |
3880 | ||
3881 | visit_type_BlockdevOptions(v, NULL, &options, &local_err); | |
3882 | if (local_err) { | |
3883 | error_propagate(errp, local_err); | |
3884 | goto fail; | |
3885 | } | |
3886 | ||
3887 | visit_complete(v, &obj); | |
3888 | qdict = qobject_to_qdict(obj); | |
3889 | ||
3890 | qdict_flatten(qdict); | |
3891 | ||
3892 | if (!qdict_get_try_str(qdict, "node-name")) { | |
3893 | error_setg(errp, "'node-name' must be specified for the root node"); | |
3894 | goto fail; | |
3895 | } | |
3896 | ||
3897 | bs = bds_tree_init(qdict, errp); | |
3898 | if (!bs) { | |
3899 | goto fail; | |
3900 | } | |
3901 | ||
3902 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); | |
3903 | ||
3904 | fail: | |
3905 | visit_free(v); | |
3906 | } | |
3907 | ||
3908 | void qmp_blockdev_del(const char *node_name, Error **errp) | |
3909 | { | |
3910 | AioContext *aio_context; | |
3911 | BlockDriverState *bs; | |
3912 | ||
3913 | bs = bdrv_find_node(node_name); | |
3914 | if (!bs) { | |
3915 | error_setg(errp, "Cannot find node %s", node_name); | |
3916 | return; | |
3917 | } | |
3918 | if (bdrv_has_blk(bs)) { | |
3919 | error_setg(errp, "Node %s is in use", node_name); | |
3920 | return; | |
3921 | } | |
3922 | aio_context = bdrv_get_aio_context(bs); | |
3923 | aio_context_acquire(aio_context); | |
3924 | ||
3925 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { | |
3926 | goto out; | |
3927 | } | |
3928 | ||
3929 | if (!bs->monitor_list.tqe_prev) { | |
3930 | error_setg(errp, "Node %s is not owned by the monitor", | |
3931 | bs->node_name); | |
3932 | goto out; | |
3933 | } | |
3934 | ||
3935 | if (bs->refcnt > 1) { | |
3936 | error_setg(errp, "Block device %s is in use", | |
3937 | bdrv_get_device_or_node_name(bs)); | |
3938 | goto out; | |
3939 | } | |
3940 | ||
3941 | QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); | |
3942 | bdrv_unref(bs); | |
3943 | ||
3944 | out: | |
3945 | aio_context_release(aio_context); | |
3946 | } | |
3947 | ||
3948 | static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, | |
3949 | const char *child_name) | |
3950 | { | |
3951 | BdrvChild *child; | |
3952 | ||
3953 | QLIST_FOREACH(child, &parent_bs->children, next) { | |
3954 | if (strcmp(child->name, child_name) == 0) { | |
3955 | return child; | |
3956 | } | |
3957 | } | |
3958 | ||
3959 | return NULL; | |
3960 | } | |
3961 | ||
3962 | void qmp_x_blockdev_change(const char *parent, bool has_child, | |
3963 | const char *child, bool has_node, | |
3964 | const char *node, Error **errp) | |
3965 | { | |
3966 | BlockDriverState *parent_bs, *new_bs = NULL; | |
3967 | BdrvChild *p_child; | |
3968 | ||
3969 | parent_bs = bdrv_lookup_bs(parent, parent, errp); | |
3970 | if (!parent_bs) { | |
3971 | return; | |
3972 | } | |
3973 | ||
3974 | if (has_child == has_node) { | |
3975 | if (has_child) { | |
3976 | error_setg(errp, "The parameters child and node are in conflict"); | |
3977 | } else { | |
3978 | error_setg(errp, "Either child or node must be specified"); | |
3979 | } | |
3980 | return; | |
3981 | } | |
3982 | ||
3983 | if (has_child) { | |
3984 | p_child = bdrv_find_child(parent_bs, child); | |
3985 | if (!p_child) { | |
3986 | error_setg(errp, "Node '%s' does not have child '%s'", | |
3987 | parent, child); | |
3988 | return; | |
3989 | } | |
3990 | bdrv_del_child(parent_bs, p_child, errp); | |
3991 | } | |
3992 | ||
3993 | if (has_node) { | |
3994 | new_bs = bdrv_find_node(node); | |
3995 | if (!new_bs) { | |
3996 | error_setg(errp, "Node '%s' not found", node); | |
3997 | return; | |
3998 | } | |
3999 | bdrv_add_child(parent_bs, new_bs, errp); | |
4000 | } | |
4001 | } | |
4002 | ||
4003 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) | |
4004 | { | |
4005 | BlockJobInfoList *head = NULL, **p_next = &head; | |
4006 | BlockJob *job; | |
4007 | ||
4008 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { | |
4009 | BlockJobInfoList *elem; | |
4010 | AioContext *aio_context; | |
4011 | ||
4012 | if (block_job_is_internal(job)) { | |
4013 | continue; | |
4014 | } | |
4015 | elem = g_new0(BlockJobInfoList, 1); | |
4016 | aio_context = blk_get_aio_context(job->blk); | |
4017 | aio_context_acquire(aio_context); | |
4018 | elem->value = block_job_query(job, errp); | |
4019 | aio_context_release(aio_context); | |
4020 | if (!elem->value) { | |
4021 | g_free(elem); | |
4022 | qapi_free_BlockJobInfoList(head); | |
4023 | return NULL; | |
4024 | } | |
4025 | *p_next = elem; | |
4026 | p_next = &elem->next; | |
4027 | } | |
4028 | ||
4029 | return head; | |
4030 | } | |
4031 | ||
4032 | QemuOptsList qemu_common_drive_opts = { | |
4033 | .name = "drive", | |
4034 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), | |
4035 | .desc = { | |
4036 | { | |
4037 | .name = "snapshot", | |
4038 | .type = QEMU_OPT_BOOL, | |
4039 | .help = "enable/disable snapshot mode", | |
4040 | },{ | |
4041 | .name = "aio", | |
4042 | .type = QEMU_OPT_STRING, | |
4043 | .help = "host AIO implementation (threads, native)", | |
4044 | },{ | |
4045 | .name = BDRV_OPT_CACHE_WB, | |
4046 | .type = QEMU_OPT_BOOL, | |
4047 | .help = "Enable writeback mode", | |
4048 | },{ | |
4049 | .name = "format", | |
4050 | .type = QEMU_OPT_STRING, | |
4051 | .help = "disk format (raw, qcow2, ...)", | |
4052 | },{ | |
4053 | .name = "rerror", | |
4054 | .type = QEMU_OPT_STRING, | |
4055 | .help = "read error action", | |
4056 | },{ | |
4057 | .name = "werror", | |
4058 | .type = QEMU_OPT_STRING, | |
4059 | .help = "write error action", | |
4060 | },{ | |
4061 | .name = BDRV_OPT_READ_ONLY, | |
4062 | .type = QEMU_OPT_BOOL, | |
4063 | .help = "open drive file as read-only", | |
4064 | }, | |
4065 | ||
4066 | THROTTLE_OPTS, | |
4067 | ||
4068 | { | |
4069 | .name = "throttling.group", | |
4070 | .type = QEMU_OPT_STRING, | |
4071 | .help = "name of the block throttling group", | |
4072 | },{ | |
4073 | .name = "copy-on-read", | |
4074 | .type = QEMU_OPT_BOOL, | |
4075 | .help = "copy read data from backing file into image file", | |
4076 | },{ | |
4077 | .name = "detect-zeroes", | |
4078 | .type = QEMU_OPT_STRING, | |
4079 | .help = "try to optimize zero writes (off, on, unmap)", | |
4080 | },{ | |
4081 | .name = "stats-account-invalid", | |
4082 | .type = QEMU_OPT_BOOL, | |
4083 | .help = "whether to account for invalid I/O operations " | |
4084 | "in the statistics", | |
4085 | },{ | |
4086 | .name = "stats-account-failed", | |
4087 | .type = QEMU_OPT_BOOL, | |
4088 | .help = "whether to account for failed I/O operations " | |
4089 | "in the statistics", | |
4090 | }, | |
4091 | { /* end of list */ } | |
4092 | }, | |
4093 | }; | |
4094 | ||
4095 | QemuOptsList qemu_drive_opts = { | |
4096 | .name = "drive", | |
4097 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
4098 | .desc = { | |
4099 | /* | |
4100 | * no elements => accept any params | |
4101 | * validation will happen later | |
4102 | */ | |
4103 | { /* end of list */ } | |
4104 | }, | |
4105 | }; |