]> git.proxmox.com Git - mirror_qemu.git/blame_incremental - blockdev.c
block: support passing 'backing': '' to 'blockdev-add'
[mirror_qemu.git] / blockdev.c
... / ...
CommitLineData
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 "sysemu/block-backend.h"
34#include "sysemu/blockdev.h"
35#include "hw/block/block.h"
36#include "block/blockjob.h"
37#include "block/throttle-groups.h"
38#include "monitor/monitor.h"
39#include "qemu/error-report.h"
40#include "qemu/option.h"
41#include "qemu/config-file.h"
42#include "qapi/qmp/types.h"
43#include "qapi-visit.h"
44#include "qapi/qmp/qerror.h"
45#include "qapi/qmp-output-visitor.h"
46#include "qapi/util.h"
47#include "sysemu/sysemu.h"
48#include "block/block_int.h"
49#include "qmp-commands.h"
50#include "trace.h"
51#include "sysemu/arch_init.h"
52
53static const char *const if_name[IF_COUNT] = {
54 [IF_NONE] = "none",
55 [IF_IDE] = "ide",
56 [IF_SCSI] = "scsi",
57 [IF_FLOPPY] = "floppy",
58 [IF_PFLASH] = "pflash",
59 [IF_MTD] = "mtd",
60 [IF_SD] = "sd",
61 [IF_VIRTIO] = "virtio",
62 [IF_XEN] = "xen",
63};
64
65static int if_max_devs[IF_COUNT] = {
66 /*
67 * Do not change these numbers! They govern how drive option
68 * index maps to unit and bus. That mapping is ABI.
69 *
70 * All controllers used to imlement if=T drives need to support
71 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
72 * Otherwise, some index values map to "impossible" bus, unit
73 * values.
74 *
75 * For instance, if you change [IF_SCSI] to 255, -drive
76 * if=scsi,index=12 no longer means bus=1,unit=5, but
77 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
78 * the drive can't be set up. Regression.
79 */
80 [IF_IDE] = 2,
81 [IF_SCSI] = 7,
82};
83
84/**
85 * Boards may call this to offer board-by-board overrides
86 * of the default, global values.
87 */
88void override_max_devs(BlockInterfaceType type, int max_devs)
89{
90 BlockBackend *blk;
91 DriveInfo *dinfo;
92
93 if (max_devs <= 0) {
94 return;
95 }
96
97 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
98 dinfo = blk_legacy_dinfo(blk);
99 if (dinfo->type == type) {
100 fprintf(stderr, "Cannot override units-per-bus property of"
101 " the %s interface, because a drive of that type has"
102 " already been added.\n", if_name[type]);
103 g_assert_not_reached();
104 }
105 }
106
107 if_max_devs[type] = max_devs;
108}
109
110/*
111 * We automatically delete the drive when a device using it gets
112 * unplugged. Questionable feature, but we can't just drop it.
113 * Device models call blockdev_mark_auto_del() to schedule the
114 * automatic deletion, and generic qdev code calls blockdev_auto_del()
115 * when deletion is actually safe.
116 */
117void blockdev_mark_auto_del(BlockBackend *blk)
118{
119 DriveInfo *dinfo = blk_legacy_dinfo(blk);
120 BlockDriverState *bs = blk_bs(blk);
121 AioContext *aio_context;
122
123 if (!dinfo) {
124 return;
125 }
126
127 if (bs) {
128 aio_context = bdrv_get_aio_context(bs);
129 aio_context_acquire(aio_context);
130
131 if (bs->job) {
132 block_job_cancel(bs->job);
133 }
134
135 aio_context_release(aio_context);
136 }
137
138 dinfo->auto_del = 1;
139}
140
141void blockdev_auto_del(BlockBackend *blk)
142{
143 DriveInfo *dinfo = blk_legacy_dinfo(blk);
144
145 if (dinfo && dinfo->auto_del) {
146 blk_unref(blk);
147 }
148}
149
150/**
151 * Returns the current mapping of how many units per bus
152 * a particular interface can support.
153 *
154 * A positive integer indicates n units per bus.
155 * 0 implies the mapping has not been established.
156 * -1 indicates an invalid BlockInterfaceType was given.
157 */
158int drive_get_max_devs(BlockInterfaceType type)
159{
160 if (type >= IF_IDE && type < IF_COUNT) {
161 return if_max_devs[type];
162 }
163
164 return -1;
165}
166
167static int drive_index_to_bus_id(BlockInterfaceType type, int index)
168{
169 int max_devs = if_max_devs[type];
170 return max_devs ? index / max_devs : 0;
171}
172
173static int drive_index_to_unit_id(BlockInterfaceType type, int index)
174{
175 int max_devs = if_max_devs[type];
176 return max_devs ? index % max_devs : index;
177}
178
179QemuOpts *drive_def(const char *optstr)
180{
181 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
182}
183
184QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
185 const char *optstr)
186{
187 QemuOpts *opts;
188
189 opts = drive_def(optstr);
190 if (!opts) {
191 return NULL;
192 }
193 if (type != IF_DEFAULT) {
194 qemu_opt_set(opts, "if", if_name[type], &error_abort);
195 }
196 if (index >= 0) {
197 qemu_opt_set_number(opts, "index", index, &error_abort);
198 }
199 if (file)
200 qemu_opt_set(opts, "file", file, &error_abort);
201 return opts;
202}
203
204DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
205{
206 BlockBackend *blk;
207 DriveInfo *dinfo;
208
209 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
210 dinfo = blk_legacy_dinfo(blk);
211 if (dinfo && dinfo->type == type
212 && dinfo->bus == bus && dinfo->unit == unit) {
213 return dinfo;
214 }
215 }
216
217 return NULL;
218}
219
220bool drive_check_orphaned(void)
221{
222 BlockBackend *blk;
223 DriveInfo *dinfo;
224 bool rs = false;
225
226 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
227 dinfo = blk_legacy_dinfo(blk);
228 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
229 /* Unless this is a default drive, this may be an oversight. */
230 if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
231 dinfo->type != IF_NONE) {
232 fprintf(stderr, "Warning: Orphaned drive without device: "
233 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
234 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "",
235 if_name[dinfo->type], dinfo->bus, dinfo->unit);
236 rs = true;
237 }
238 }
239
240 return rs;
241}
242
243DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
244{
245 return drive_get(type,
246 drive_index_to_bus_id(type, index),
247 drive_index_to_unit_id(type, index));
248}
249
250int drive_get_max_bus(BlockInterfaceType type)
251{
252 int max_bus;
253 BlockBackend *blk;
254 DriveInfo *dinfo;
255
256 max_bus = -1;
257 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
258 dinfo = blk_legacy_dinfo(blk);
259 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
260 max_bus = dinfo->bus;
261 }
262 }
263 return max_bus;
264}
265
266/* Get a block device. This should only be used for single-drive devices
267 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
268 appropriate bus. */
269DriveInfo *drive_get_next(BlockInterfaceType type)
270{
271 static int next_block_unit[IF_COUNT];
272
273 return drive_get(type, 0, next_block_unit[type]++);
274}
275
276static void bdrv_format_print(void *opaque, const char *name)
277{
278 error_printf(" %s", name);
279}
280
281typedef struct {
282 QEMUBH *bh;
283 BlockDriverState *bs;
284} BDRVPutRefBH;
285
286static void bdrv_put_ref_bh(void *opaque)
287{
288 BDRVPutRefBH *s = opaque;
289
290 bdrv_unref(s->bs);
291 qemu_bh_delete(s->bh);
292 g_free(s);
293}
294
295/*
296 * Release a BDS reference in a BH
297 *
298 * It is not safe to use bdrv_unref() from a callback function when the callers
299 * still need the BlockDriverState. In such cases we schedule a BH to release
300 * the reference.
301 */
302static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
303{
304 BDRVPutRefBH *s;
305
306 s = g_new(BDRVPutRefBH, 1);
307 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
308 s->bs = bs;
309 qemu_bh_schedule(s->bh);
310}
311
312static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
313{
314 if (!strcmp(buf, "ignore")) {
315 return BLOCKDEV_ON_ERROR_IGNORE;
316 } else if (!is_read && !strcmp(buf, "enospc")) {
317 return BLOCKDEV_ON_ERROR_ENOSPC;
318 } else if (!strcmp(buf, "stop")) {
319 return BLOCKDEV_ON_ERROR_STOP;
320 } else if (!strcmp(buf, "report")) {
321 return BLOCKDEV_ON_ERROR_REPORT;
322 } else {
323 error_setg(errp, "'%s' invalid %s error action",
324 buf, is_read ? "read" : "write");
325 return -1;
326 }
327}
328
329static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
330{
331 if (throttle_conflicting(cfg)) {
332 error_setg(errp, "bps/iops/max total values and read/write values"
333 " cannot be used at the same time");
334 return false;
335 }
336
337 if (!throttle_is_valid(cfg)) {
338 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
339 return false;
340 }
341
342 if (throttle_max_is_missing_limit(cfg)) {
343 error_setg(errp, "bps_max/iops_max require corresponding"
344 " bps/iops values");
345 return false;
346 }
347
348 return true;
349}
350
351typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
352
353/* All parameters but @opts are optional and may be set to NULL. */
354static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
355 const char **throttling_group, ThrottleConfig *throttle_cfg,
356 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
357{
358 const char *discard;
359 Error *local_error = NULL;
360 const char *aio;
361
362 if (bdrv_flags) {
363 if (!qemu_opt_get_bool(opts, "read-only", false)) {
364 *bdrv_flags |= BDRV_O_RDWR;
365 }
366 if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
367 *bdrv_flags |= BDRV_O_COPY_ON_READ;
368 }
369
370 if ((discard = qemu_opt_get(opts, "discard")) != NULL) {
371 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) {
372 error_setg(errp, "Invalid discard option");
373 return;
374 }
375 }
376
377 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
378 *bdrv_flags |= BDRV_O_CACHE_WB;
379 }
380 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
381 *bdrv_flags |= BDRV_O_NOCACHE;
382 }
383 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
384 *bdrv_flags |= BDRV_O_NO_FLUSH;
385 }
386
387 if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
388 if (!strcmp(aio, "native")) {
389 *bdrv_flags |= BDRV_O_NATIVE_AIO;
390 } else if (!strcmp(aio, "threads")) {
391 /* this is the default */
392 } else {
393 error_setg(errp, "invalid aio option");
394 return;
395 }
396 }
397 }
398
399 /* disk I/O throttling */
400 if (throttling_group) {
401 *throttling_group = qemu_opt_get(opts, "throttling.group");
402 }
403
404 if (throttle_cfg) {
405 memset(throttle_cfg, 0, sizeof(*throttle_cfg));
406 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
407 qemu_opt_get_number(opts, "throttling.bps-total", 0);
408 throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
409 qemu_opt_get_number(opts, "throttling.bps-read", 0);
410 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
411 qemu_opt_get_number(opts, "throttling.bps-write", 0);
412 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
413 qemu_opt_get_number(opts, "throttling.iops-total", 0);
414 throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
415 qemu_opt_get_number(opts, "throttling.iops-read", 0);
416 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
417 qemu_opt_get_number(opts, "throttling.iops-write", 0);
418
419 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
420 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
421 throttle_cfg->buckets[THROTTLE_BPS_READ].max =
422 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
423 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
424 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
425 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
426 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
427 throttle_cfg->buckets[THROTTLE_OPS_READ].max =
428 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
429 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
430 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
431
432 throttle_cfg->op_size =
433 qemu_opt_get_number(opts, "throttling.iops-size", 0);
434
435 if (!check_throttle_config(throttle_cfg, errp)) {
436 return;
437 }
438 }
439
440 if (detect_zeroes) {
441 *detect_zeroes =
442 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
443 qemu_opt_get(opts, "detect-zeroes"),
444 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
445 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
446 &local_error);
447 if (local_error) {
448 error_propagate(errp, local_error);
449 return;
450 }
451
452 if (bdrv_flags &&
453 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
454 !(*bdrv_flags & BDRV_O_UNMAP))
455 {
456 error_setg(errp, "setting detect-zeroes to unmap is not allowed "
457 "without setting discard operation to unmap");
458 return;
459 }
460 }
461}
462
463/* Takes the ownership of bs_opts */
464static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
465 Error **errp)
466{
467 const char *buf;
468 int bdrv_flags = 0;
469 int on_read_error, on_write_error;
470 BlockBackend *blk;
471 BlockDriverState *bs;
472 ThrottleConfig cfg;
473 int snapshot = 0;
474 Error *error = NULL;
475 QemuOpts *opts;
476 const char *id;
477 bool has_driver_specific_opts;
478 BlockdevDetectZeroesOptions detect_zeroes =
479 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
480 const char *throttling_group = NULL;
481
482 /* Check common options by copying from bs_opts to opts, all other options
483 * stay in bs_opts for processing by bdrv_open(). */
484 id = qdict_get_try_str(bs_opts, "id");
485 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
486 if (error) {
487 error_propagate(errp, error);
488 goto err_no_opts;
489 }
490
491 qemu_opts_absorb_qdict(opts, bs_opts, &error);
492 if (error) {
493 error_propagate(errp, error);
494 goto early_err;
495 }
496
497 if (id) {
498 qdict_del(bs_opts, "id");
499 }
500
501 has_driver_specific_opts = !!qdict_size(bs_opts);
502
503 /* extract parameters */
504 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
505
506 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
507 &detect_zeroes, &error);
508 if (error) {
509 error_propagate(errp, error);
510 goto early_err;
511 }
512
513 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
514 if (is_help_option(buf)) {
515 error_printf("Supported formats:");
516 bdrv_iterate_format(bdrv_format_print, NULL);
517 error_printf("\n");
518 goto early_err;
519 }
520
521 if (qdict_haskey(bs_opts, "driver")) {
522 error_setg(errp, "Cannot specify both 'driver' and 'format'");
523 goto early_err;
524 }
525 qdict_put(bs_opts, "driver", qstring_from_str(buf));
526 }
527
528 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
529 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
530 on_write_error = parse_block_error_action(buf, 0, &error);
531 if (error) {
532 error_propagate(errp, error);
533 goto early_err;
534 }
535 }
536
537 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
538 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
539 on_read_error = parse_block_error_action(buf, 1, &error);
540 if (error) {
541 error_propagate(errp, error);
542 goto early_err;
543 }
544 }
545
546 if (snapshot) {
547 /* always use cache=unsafe with snapshot */
548 bdrv_flags &= ~BDRV_O_CACHE_MASK;
549 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
550 }
551
552 /* init */
553 if ((!file || !*file) && !has_driver_specific_opts) {
554 BlockBackendRootState *blk_rs;
555
556 blk = blk_new(qemu_opts_id(opts), errp);
557 if (!blk) {
558 goto early_err;
559 }
560
561 blk_rs = blk_get_root_state(blk);
562 blk_rs->open_flags = bdrv_flags;
563 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR);
564 blk_rs->detect_zeroes = detect_zeroes;
565
566 if (throttle_enabled(&cfg)) {
567 if (!throttling_group) {
568 throttling_group = blk_name(blk);
569 }
570 blk_rs->throttle_group = g_strdup(throttling_group);
571 blk_rs->throttle_state = throttle_group_incref(throttling_group);
572 blk_rs->throttle_state->cfg = cfg;
573 }
574
575 QDECREF(bs_opts);
576 } else {
577 if (file && !*file) {
578 file = NULL;
579 }
580
581 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
582 errp);
583 if (!blk) {
584 goto err_no_bs_opts;
585 }
586 bs = blk_bs(blk);
587
588 bs->detect_zeroes = detect_zeroes;
589
590 /* disk I/O throttling */
591 if (throttle_enabled(&cfg)) {
592 if (!throttling_group) {
593 throttling_group = blk_name(blk);
594 }
595 bdrv_io_limits_enable(bs, throttling_group);
596 bdrv_set_io_limits(bs, &cfg);
597 }
598
599 if (bdrv_key_required(bs)) {
600 autostart = 0;
601 }
602 }
603
604 blk_set_on_error(blk, on_read_error, on_write_error);
605
606err_no_bs_opts:
607 qemu_opts_del(opts);
608 return blk;
609
610early_err:
611 qemu_opts_del(opts);
612err_no_opts:
613 QDECREF(bs_opts);
614 return NULL;
615}
616
617static QemuOptsList qemu_root_bds_opts;
618
619/* Takes the ownership of bs_opts */
620static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
621{
622 BlockDriverState *bs;
623 QemuOpts *opts;
624 Error *local_error = NULL;
625 BlockdevDetectZeroesOptions detect_zeroes;
626 int ret;
627 int bdrv_flags = 0;
628
629 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp);
630 if (!opts) {
631 goto fail;
632 }
633
634 qemu_opts_absorb_qdict(opts, bs_opts, &local_error);
635 if (local_error) {
636 error_propagate(errp, local_error);
637 goto fail;
638 }
639
640 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL,
641 &detect_zeroes, &local_error);
642 if (local_error) {
643 error_propagate(errp, local_error);
644 goto fail;
645 }
646
647 bs = NULL;
648 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp);
649 if (ret < 0) {
650 goto fail_no_bs_opts;
651 }
652
653 bs->detect_zeroes = detect_zeroes;
654
655fail_no_bs_opts:
656 qemu_opts_del(opts);
657 return bs;
658
659fail:
660 qemu_opts_del(opts);
661 QDECREF(bs_opts);
662 return NULL;
663}
664
665static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
666 Error **errp)
667{
668 const char *value;
669
670 value = qemu_opt_get(opts, from);
671 if (value) {
672 if (qemu_opt_find(opts, to)) {
673 error_setg(errp, "'%s' and its alias '%s' can't be used at the "
674 "same time", to, from);
675 return;
676 }
677 }
678
679 /* rename all items in opts */
680 while ((value = qemu_opt_get(opts, from))) {
681 qemu_opt_set(opts, to, value, &error_abort);
682 qemu_opt_unset(opts, from);
683 }
684}
685
686QemuOptsList qemu_legacy_drive_opts = {
687 .name = "drive",
688 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
689 .desc = {
690 {
691 .name = "bus",
692 .type = QEMU_OPT_NUMBER,
693 .help = "bus number",
694 },{
695 .name = "unit",
696 .type = QEMU_OPT_NUMBER,
697 .help = "unit number (i.e. lun for scsi)",
698 },{
699 .name = "index",
700 .type = QEMU_OPT_NUMBER,
701 .help = "index number",
702 },{
703 .name = "media",
704 .type = QEMU_OPT_STRING,
705 .help = "media type (disk, cdrom)",
706 },{
707 .name = "if",
708 .type = QEMU_OPT_STRING,
709 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
710 },{
711 .name = "cyls",
712 .type = QEMU_OPT_NUMBER,
713 .help = "number of cylinders (ide disk geometry)",
714 },{
715 .name = "heads",
716 .type = QEMU_OPT_NUMBER,
717 .help = "number of heads (ide disk geometry)",
718 },{
719 .name = "secs",
720 .type = QEMU_OPT_NUMBER,
721 .help = "number of sectors (ide disk geometry)",
722 },{
723 .name = "trans",
724 .type = QEMU_OPT_STRING,
725 .help = "chs translation (auto, lba, none)",
726 },{
727 .name = "boot",
728 .type = QEMU_OPT_BOOL,
729 .help = "(deprecated, ignored)",
730 },{
731 .name = "addr",
732 .type = QEMU_OPT_STRING,
733 .help = "pci address (virtio only)",
734 },{
735 .name = "serial",
736 .type = QEMU_OPT_STRING,
737 .help = "disk serial number",
738 },{
739 .name = "file",
740 .type = QEMU_OPT_STRING,
741 .help = "file name",
742 },
743
744 /* Options that are passed on, but have special semantics with -drive */
745 {
746 .name = "read-only",
747 .type = QEMU_OPT_BOOL,
748 .help = "open drive file as read-only",
749 },{
750 .name = "rerror",
751 .type = QEMU_OPT_STRING,
752 .help = "read error action",
753 },{
754 .name = "werror",
755 .type = QEMU_OPT_STRING,
756 .help = "write error action",
757 },{
758 .name = "copy-on-read",
759 .type = QEMU_OPT_BOOL,
760 .help = "copy read data from backing file into image file",
761 },
762
763 { /* end of list */ }
764 },
765};
766
767DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
768{
769 const char *value;
770 BlockBackend *blk;
771 DriveInfo *dinfo = NULL;
772 QDict *bs_opts;
773 QemuOpts *legacy_opts;
774 DriveMediaType media = MEDIA_DISK;
775 BlockInterfaceType type;
776 int cyls, heads, secs, translation;
777 int max_devs, bus_id, unit_id, index;
778 const char *devaddr;
779 const char *werror, *rerror;
780 bool read_only = false;
781 bool copy_on_read;
782 const char *serial;
783 const char *filename;
784 Error *local_err = NULL;
785 int i;
786
787 /* Change legacy command line options into QMP ones */
788 static const struct {
789 const char *from;
790 const char *to;
791 } opt_renames[] = {
792 { "iops", "throttling.iops-total" },
793 { "iops_rd", "throttling.iops-read" },
794 { "iops_wr", "throttling.iops-write" },
795
796 { "bps", "throttling.bps-total" },
797 { "bps_rd", "throttling.bps-read" },
798 { "bps_wr", "throttling.bps-write" },
799
800 { "iops_max", "throttling.iops-total-max" },
801 { "iops_rd_max", "throttling.iops-read-max" },
802 { "iops_wr_max", "throttling.iops-write-max" },
803
804 { "bps_max", "throttling.bps-total-max" },
805 { "bps_rd_max", "throttling.bps-read-max" },
806 { "bps_wr_max", "throttling.bps-write-max" },
807
808 { "iops_size", "throttling.iops-size" },
809
810 { "group", "throttling.group" },
811
812 { "readonly", "read-only" },
813 };
814
815 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
816 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
817 &local_err);
818 if (local_err) {
819 error_report_err(local_err);
820 return NULL;
821 }
822 }
823
824 value = qemu_opt_get(all_opts, "cache");
825 if (value) {
826 int flags = 0;
827
828 if (bdrv_parse_cache_flags(value, &flags) != 0) {
829 error_report("invalid cache option");
830 return NULL;
831 }
832
833 /* Specific options take precedence */
834 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
835 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
836 !!(flags & BDRV_O_CACHE_WB), &error_abort);
837 }
838 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
839 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
840 !!(flags & BDRV_O_NOCACHE), &error_abort);
841 }
842 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
843 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
844 !!(flags & BDRV_O_NO_FLUSH), &error_abort);
845 }
846 qemu_opt_unset(all_opts, "cache");
847 }
848
849 /* Get a QDict for processing the options */
850 bs_opts = qdict_new();
851 qemu_opts_to_qdict(all_opts, bs_opts);
852
853 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
854 &error_abort);
855 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
856 if (local_err) {
857 error_report_err(local_err);
858 goto fail;
859 }
860
861 /* Deprecated option boot=[on|off] */
862 if (qemu_opt_get(legacy_opts, "boot") != NULL) {
863 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
864 "ignored. Future versions will reject this parameter. Please "
865 "update your scripts.\n");
866 }
867
868 /* Media type */
869 value = qemu_opt_get(legacy_opts, "media");
870 if (value) {
871 if (!strcmp(value, "disk")) {
872 media = MEDIA_DISK;
873 } else if (!strcmp(value, "cdrom")) {
874 media = MEDIA_CDROM;
875 read_only = true;
876 } else {
877 error_report("'%s' invalid media", value);
878 goto fail;
879 }
880 }
881
882 /* copy-on-read is disabled with a warning for read-only devices */
883 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
884 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
885
886 if (read_only && copy_on_read) {
887 error_report("warning: disabling copy-on-read on read-only drive");
888 copy_on_read = false;
889 }
890
891 qdict_put(bs_opts, "read-only",
892 qstring_from_str(read_only ? "on" : "off"));
893 qdict_put(bs_opts, "copy-on-read",
894 qstring_from_str(copy_on_read ? "on" :"off"));
895
896 /* Controller type */
897 value = qemu_opt_get(legacy_opts, "if");
898 if (value) {
899 for (type = 0;
900 type < IF_COUNT && strcmp(value, if_name[type]);
901 type++) {
902 }
903 if (type == IF_COUNT) {
904 error_report("unsupported bus type '%s'", value);
905 goto fail;
906 }
907 } else {
908 type = block_default_type;
909 }
910
911 /* Geometry */
912 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
913 heads = qemu_opt_get_number(legacy_opts, "heads", 0);
914 secs = qemu_opt_get_number(legacy_opts, "secs", 0);
915
916 if (cyls || heads || secs) {
917 if (cyls < 1) {
918 error_report("invalid physical cyls number");
919 goto fail;
920 }
921 if (heads < 1) {
922 error_report("invalid physical heads number");
923 goto fail;
924 }
925 if (secs < 1) {
926 error_report("invalid physical secs number");
927 goto fail;
928 }
929 }
930
931 translation = BIOS_ATA_TRANSLATION_AUTO;
932 value = qemu_opt_get(legacy_opts, "trans");
933 if (value != NULL) {
934 if (!cyls) {
935 error_report("'%s' trans must be used with cyls, heads and secs",
936 value);
937 goto fail;
938 }
939 if (!strcmp(value, "none")) {
940 translation = BIOS_ATA_TRANSLATION_NONE;
941 } else if (!strcmp(value, "lba")) {
942 translation = BIOS_ATA_TRANSLATION_LBA;
943 } else if (!strcmp(value, "large")) {
944 translation = BIOS_ATA_TRANSLATION_LARGE;
945 } else if (!strcmp(value, "rechs")) {
946 translation = BIOS_ATA_TRANSLATION_RECHS;
947 } else if (!strcmp(value, "auto")) {
948 translation = BIOS_ATA_TRANSLATION_AUTO;
949 } else {
950 error_report("'%s' invalid translation type", value);
951 goto fail;
952 }
953 }
954
955 if (media == MEDIA_CDROM) {
956 if (cyls || secs || heads) {
957 error_report("CHS can't be set with media=cdrom");
958 goto fail;
959 }
960 }
961
962 /* Device address specified by bus/unit or index.
963 * If none was specified, try to find the first free one. */
964 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
965 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
966 index = qemu_opt_get_number(legacy_opts, "index", -1);
967
968 max_devs = if_max_devs[type];
969
970 if (index != -1) {
971 if (bus_id != 0 || unit_id != -1) {
972 error_report("index cannot be used with bus and unit");
973 goto fail;
974 }
975 bus_id = drive_index_to_bus_id(type, index);
976 unit_id = drive_index_to_unit_id(type, index);
977 }
978
979 if (unit_id == -1) {
980 unit_id = 0;
981 while (drive_get(type, bus_id, unit_id) != NULL) {
982 unit_id++;
983 if (max_devs && unit_id >= max_devs) {
984 unit_id -= max_devs;
985 bus_id++;
986 }
987 }
988 }
989
990 if (max_devs && unit_id >= max_devs) {
991 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
992 goto fail;
993 }
994
995 if (drive_get(type, bus_id, unit_id) != NULL) {
996 error_report("drive with bus=%d, unit=%d (index=%d) exists",
997 bus_id, unit_id, index);
998 goto fail;
999 }
1000
1001 /* Serial number */
1002 serial = qemu_opt_get(legacy_opts, "serial");
1003
1004 /* no id supplied -> create one */
1005 if (qemu_opts_id(all_opts) == NULL) {
1006 char *new_id;
1007 const char *mediastr = "";
1008 if (type == IF_IDE || type == IF_SCSI) {
1009 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1010 }
1011 if (max_devs) {
1012 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
1013 mediastr, unit_id);
1014 } else {
1015 new_id = g_strdup_printf("%s%s%i", if_name[type],
1016 mediastr, unit_id);
1017 }
1018 qdict_put(bs_opts, "id", qstring_from_str(new_id));
1019 g_free(new_id);
1020 }
1021
1022 /* Add virtio block device */
1023 devaddr = qemu_opt_get(legacy_opts, "addr");
1024 if (devaddr && type != IF_VIRTIO) {
1025 error_report("addr is not supported by this bus type");
1026 goto fail;
1027 }
1028
1029 if (type == IF_VIRTIO) {
1030 QemuOpts *devopts;
1031 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
1032 &error_abort);
1033 if (arch_type == QEMU_ARCH_S390X) {
1034 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
1035 } else {
1036 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
1037 }
1038 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
1039 &error_abort);
1040 if (devaddr) {
1041 qemu_opt_set(devopts, "addr", devaddr, &error_abort);
1042 }
1043 }
1044
1045 filename = qemu_opt_get(legacy_opts, "file");
1046
1047 /* Check werror/rerror compatibility with if=... */
1048 werror = qemu_opt_get(legacy_opts, "werror");
1049 if (werror != NULL) {
1050 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
1051 type != IF_NONE) {
1052 error_report("werror is not supported by this bus type");
1053 goto fail;
1054 }
1055 qdict_put(bs_opts, "werror", qstring_from_str(werror));
1056 }
1057
1058 rerror = qemu_opt_get(legacy_opts, "rerror");
1059 if (rerror != NULL) {
1060 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
1061 type != IF_NONE) {
1062 error_report("rerror is not supported by this bus type");
1063 goto fail;
1064 }
1065 qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
1066 }
1067
1068 /* Actual block device init: Functionality shared with blockdev-add */
1069 blk = blockdev_init(filename, bs_opts, &local_err);
1070 bs_opts = NULL;
1071 if (!blk) {
1072 if (local_err) {
1073 error_report_err(local_err);
1074 }
1075 goto fail;
1076 } else {
1077 assert(!local_err);
1078 }
1079
1080 /* Create legacy DriveInfo */
1081 dinfo = g_malloc0(sizeof(*dinfo));
1082 dinfo->opts = all_opts;
1083
1084 dinfo->cyls = cyls;
1085 dinfo->heads = heads;
1086 dinfo->secs = secs;
1087 dinfo->trans = translation;
1088
1089 dinfo->type = type;
1090 dinfo->bus = bus_id;
1091 dinfo->unit = unit_id;
1092 dinfo->devaddr = devaddr;
1093 dinfo->serial = g_strdup(serial);
1094
1095 blk_set_legacy_dinfo(blk, dinfo);
1096
1097 switch(type) {
1098 case IF_IDE:
1099 case IF_SCSI:
1100 case IF_XEN:
1101 case IF_NONE:
1102 dinfo->media_cd = media == MEDIA_CDROM;
1103 break;
1104 default:
1105 break;
1106 }
1107
1108fail:
1109 qemu_opts_del(legacy_opts);
1110 QDECREF(bs_opts);
1111 return dinfo;
1112}
1113
1114void hmp_commit(Monitor *mon, const QDict *qdict)
1115{
1116 const char *device = qdict_get_str(qdict, "device");
1117 BlockBackend *blk;
1118 int ret;
1119
1120 if (!strcmp(device, "all")) {
1121 ret = bdrv_commit_all();
1122 } else {
1123 BlockDriverState *bs;
1124 AioContext *aio_context;
1125
1126 blk = blk_by_name(device);
1127 if (!blk) {
1128 monitor_printf(mon, "Device '%s' not found\n", device);
1129 return;
1130 }
1131 if (!blk_is_available(blk)) {
1132 monitor_printf(mon, "Device '%s' has no medium\n", device);
1133 return;
1134 }
1135
1136 bs = blk_bs(blk);
1137 aio_context = bdrv_get_aio_context(bs);
1138 aio_context_acquire(aio_context);
1139
1140 ret = bdrv_commit(bs);
1141
1142 aio_context_release(aio_context);
1143 }
1144 if (ret < 0) {
1145 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
1146 strerror(-ret));
1147 }
1148}
1149
1150static void blockdev_do_action(TransactionActionKind type, void *data,
1151 Error **errp)
1152{
1153 TransactionAction action;
1154 TransactionActionList list;
1155
1156 action.type = type;
1157 action.u.data = data;
1158 list.value = &action;
1159 list.next = NULL;
1160 qmp_transaction(&list, errp);
1161}
1162
1163void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
1164 bool has_node_name, const char *node_name,
1165 const char *snapshot_file,
1166 bool has_snapshot_node_name,
1167 const char *snapshot_node_name,
1168 bool has_format, const char *format,
1169 bool has_mode, NewImageMode mode, Error **errp)
1170{
1171 BlockdevSnapshotSync snapshot = {
1172 .has_device = has_device,
1173 .device = (char *) device,
1174 .has_node_name = has_node_name,
1175 .node_name = (char *) node_name,
1176 .snapshot_file = (char *) snapshot_file,
1177 .has_snapshot_node_name = has_snapshot_node_name,
1178 .snapshot_node_name = (char *) snapshot_node_name,
1179 .has_format = has_format,
1180 .format = (char *) format,
1181 .has_mode = has_mode,
1182 .mode = mode,
1183 };
1184 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
1185 &snapshot, errp);
1186}
1187
1188void qmp_blockdev_snapshot_internal_sync(const char *device,
1189 const char *name,
1190 Error **errp)
1191{
1192 BlockdevSnapshotInternal snapshot = {
1193 .device = (char *) device,
1194 .name = (char *) name
1195 };
1196
1197 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
1198 &snapshot, errp);
1199}
1200
1201SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
1202 bool has_id,
1203 const char *id,
1204 bool has_name,
1205 const char *name,
1206 Error **errp)
1207{
1208 BlockDriverState *bs;
1209 BlockBackend *blk;
1210 AioContext *aio_context;
1211 QEMUSnapshotInfo sn;
1212 Error *local_err = NULL;
1213 SnapshotInfo *info = NULL;
1214 int ret;
1215
1216 blk = blk_by_name(device);
1217 if (!blk) {
1218 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1219 "Device '%s' not found", device);
1220 return NULL;
1221 }
1222
1223 aio_context = blk_get_aio_context(blk);
1224 aio_context_acquire(aio_context);
1225
1226 if (!has_id) {
1227 id = NULL;
1228 }
1229
1230 if (!has_name) {
1231 name = NULL;
1232 }
1233
1234 if (!id && !name) {
1235 error_setg(errp, "Name or id must be provided");
1236 goto out_aio_context;
1237 }
1238
1239 if (!blk_is_available(blk)) {
1240 error_setg(errp, "Device '%s' has no medium", device);
1241 goto out_aio_context;
1242 }
1243 bs = blk_bs(blk);
1244
1245 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
1246 goto out_aio_context;
1247 }
1248
1249 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
1250 if (local_err) {
1251 error_propagate(errp, local_err);
1252 goto out_aio_context;
1253 }
1254 if (!ret) {
1255 error_setg(errp,
1256 "Snapshot with id '%s' and name '%s' does not exist on "
1257 "device '%s'",
1258 STR_OR_NULL(id), STR_OR_NULL(name), device);
1259 goto out_aio_context;
1260 }
1261
1262 bdrv_snapshot_delete(bs, id, name, &local_err);
1263 if (local_err) {
1264 error_propagate(errp, local_err);
1265 goto out_aio_context;
1266 }
1267
1268 aio_context_release(aio_context);
1269
1270 info = g_new0(SnapshotInfo, 1);
1271 info->id = g_strdup(sn.id_str);
1272 info->name = g_strdup(sn.name);
1273 info->date_nsec = sn.date_nsec;
1274 info->date_sec = sn.date_sec;
1275 info->vm_state_size = sn.vm_state_size;
1276 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
1277 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
1278
1279 return info;
1280
1281out_aio_context:
1282 aio_context_release(aio_context);
1283 return NULL;
1284}
1285
1286/**
1287 * block_dirty_bitmap_lookup:
1288 * Return a dirty bitmap (if present), after validating
1289 * the node reference and bitmap names.
1290 *
1291 * @node: The name of the BDS node to search for bitmaps
1292 * @name: The name of the bitmap to search for
1293 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1294 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1295 * @errp: Output pointer for error information. Can be NULL.
1296 *
1297 * @return: A bitmap object on success, or NULL on failure.
1298 */
1299static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
1300 const char *name,
1301 BlockDriverState **pbs,
1302 AioContext **paio,
1303 Error **errp)
1304{
1305 BlockDriverState *bs;
1306 BdrvDirtyBitmap *bitmap;
1307 AioContext *aio_context;
1308
1309 if (!node) {
1310 error_setg(errp, "Node cannot be NULL");
1311 return NULL;
1312 }
1313 if (!name) {
1314 error_setg(errp, "Bitmap name cannot be NULL");
1315 return NULL;
1316 }
1317 bs = bdrv_lookup_bs(node, node, NULL);
1318 if (!bs) {
1319 error_setg(errp, "Node '%s' not found", node);
1320 return NULL;
1321 }
1322
1323 aio_context = bdrv_get_aio_context(bs);
1324 aio_context_acquire(aio_context);
1325
1326 bitmap = bdrv_find_dirty_bitmap(bs, name);
1327 if (!bitmap) {
1328 error_setg(errp, "Dirty bitmap '%s' not found", name);
1329 goto fail;
1330 }
1331
1332 if (pbs) {
1333 *pbs = bs;
1334 }
1335 if (paio) {
1336 *paio = aio_context;
1337 } else {
1338 aio_context_release(aio_context);
1339 }
1340
1341 return bitmap;
1342
1343 fail:
1344 aio_context_release(aio_context);
1345 return NULL;
1346}
1347
1348/* New and old BlockDriverState structs for atomic group operations */
1349
1350typedef struct BlkTransactionState BlkTransactionState;
1351
1352/* Only prepare() may fail. In a single transaction, only one of commit() or
1353 abort() will be called, clean() will always be called if it present. */
1354typedef struct BdrvActionOps {
1355 /* Size of state struct, in bytes. */
1356 size_t instance_size;
1357 /* Prepare the work, must NOT be NULL. */
1358 void (*prepare)(BlkTransactionState *common, Error **errp);
1359 /* Commit the changes, can be NULL. */
1360 void (*commit)(BlkTransactionState *common);
1361 /* Abort the changes on fail, can be NULL. */
1362 void (*abort)(BlkTransactionState *common);
1363 /* Clean up resource in the end, can be NULL. */
1364 void (*clean)(BlkTransactionState *common);
1365} BdrvActionOps;
1366
1367/*
1368 * This structure must be arranged as first member in child type, assuming
1369 * that compiler will also arrange it to the same address with parent instance.
1370 * Later it will be used in free().
1371 */
1372struct BlkTransactionState {
1373 TransactionAction *action;
1374 const BdrvActionOps *ops;
1375 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
1376};
1377
1378/* internal snapshot private data */
1379typedef struct InternalSnapshotState {
1380 BlkTransactionState common;
1381 BlockDriverState *bs;
1382 AioContext *aio_context;
1383 QEMUSnapshotInfo sn;
1384 bool created;
1385} InternalSnapshotState;
1386
1387static void internal_snapshot_prepare(BlkTransactionState *common,
1388 Error **errp)
1389{
1390 Error *local_err = NULL;
1391 const char *device;
1392 const char *name;
1393 BlockBackend *blk;
1394 BlockDriverState *bs;
1395 QEMUSnapshotInfo old_sn, *sn;
1396 bool ret;
1397 qemu_timeval tv;
1398 BlockdevSnapshotInternal *internal;
1399 InternalSnapshotState *state;
1400 int ret1;
1401
1402 g_assert(common->action->type ==
1403 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
1404 internal = common->action->u.blockdev_snapshot_internal_sync;
1405 state = DO_UPCAST(InternalSnapshotState, common, common);
1406
1407 /* 1. parse input */
1408 device = internal->device;
1409 name = internal->name;
1410
1411 /* 2. check for validation */
1412 blk = blk_by_name(device);
1413 if (!blk) {
1414 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1415 "Device '%s' not found", device);
1416 return;
1417 }
1418
1419 /* AioContext is released in .clean() */
1420 state->aio_context = blk_get_aio_context(blk);
1421 aio_context_acquire(state->aio_context);
1422
1423 if (!blk_is_available(blk)) {
1424 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1425 return;
1426 }
1427 bs = blk_bs(blk);
1428
1429 state->bs = bs;
1430 bdrv_drained_begin(bs);
1431
1432 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
1433 return;
1434 }
1435
1436 if (bdrv_is_read_only(bs)) {
1437 error_setg(errp, "Device '%s' is read only", device);
1438 return;
1439 }
1440
1441 if (!bdrv_can_snapshot(bs)) {
1442 error_setg(errp, "Block format '%s' used by device '%s' "
1443 "does not support internal snapshots",
1444 bs->drv->format_name, device);
1445 return;
1446 }
1447
1448 if (!strlen(name)) {
1449 error_setg(errp, "Name is empty");
1450 return;
1451 }
1452
1453 /* check whether a snapshot with name exist */
1454 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
1455 &local_err);
1456 if (local_err) {
1457 error_propagate(errp, local_err);
1458 return;
1459 } else if (ret) {
1460 error_setg(errp,
1461 "Snapshot with name '%s' already exists on device '%s'",
1462 name, device);
1463 return;
1464 }
1465
1466 /* 3. take the snapshot */
1467 sn = &state->sn;
1468 pstrcpy(sn->name, sizeof(sn->name), name);
1469 qemu_gettimeofday(&tv);
1470 sn->date_sec = tv.tv_sec;
1471 sn->date_nsec = tv.tv_usec * 1000;
1472 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1473
1474 ret1 = bdrv_snapshot_create(bs, sn);
1475 if (ret1 < 0) {
1476 error_setg_errno(errp, -ret1,
1477 "Failed to create snapshot '%s' on device '%s'",
1478 name, device);
1479 return;
1480 }
1481
1482 /* 4. succeed, mark a snapshot is created */
1483 state->created = true;
1484}
1485
1486static void internal_snapshot_abort(BlkTransactionState *common)
1487{
1488 InternalSnapshotState *state =
1489 DO_UPCAST(InternalSnapshotState, common, common);
1490 BlockDriverState *bs = state->bs;
1491 QEMUSnapshotInfo *sn = &state->sn;
1492 Error *local_error = NULL;
1493
1494 if (!state->created) {
1495 return;
1496 }
1497
1498 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1499 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1500 "device '%s' in abort: %s",
1501 sn->id_str,
1502 sn->name,
1503 bdrv_get_device_name(bs),
1504 error_get_pretty(local_error));
1505 error_free(local_error);
1506 }
1507}
1508
1509static void internal_snapshot_clean(BlkTransactionState *common)
1510{
1511 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
1512 common, common);
1513
1514 if (state->aio_context) {
1515 if (state->bs) {
1516 bdrv_drained_end(state->bs);
1517 }
1518 aio_context_release(state->aio_context);
1519 }
1520}
1521
1522/* external snapshot private data */
1523typedef struct ExternalSnapshotState {
1524 BlkTransactionState common;
1525 BlockDriverState *old_bs;
1526 BlockDriverState *new_bs;
1527 AioContext *aio_context;
1528} ExternalSnapshotState;
1529
1530static void external_snapshot_prepare(BlkTransactionState *common,
1531 Error **errp)
1532{
1533 int flags, ret;
1534 QDict *options;
1535 Error *local_err = NULL;
1536 bool has_device = false;
1537 const char *device;
1538 bool has_node_name = false;
1539 const char *node_name;
1540 bool has_snapshot_node_name = false;
1541 const char *snapshot_node_name;
1542 const char *new_image_file;
1543 const char *format = "qcow2";
1544 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1545 ExternalSnapshotState *state =
1546 DO_UPCAST(ExternalSnapshotState, common, common);
1547 TransactionAction *action = common->action;
1548
1549 /* get parameters */
1550 g_assert(action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1551
1552 has_device = action->u.blockdev_snapshot_sync->has_device;
1553 device = action->u.blockdev_snapshot_sync->device;
1554 has_node_name = action->u.blockdev_snapshot_sync->has_node_name;
1555 node_name = action->u.blockdev_snapshot_sync->node_name;
1556 has_snapshot_node_name =
1557 action->u.blockdev_snapshot_sync->has_snapshot_node_name;
1558 snapshot_node_name = action->u.blockdev_snapshot_sync->snapshot_node_name;
1559
1560 new_image_file = action->u.blockdev_snapshot_sync->snapshot_file;
1561 if (action->u.blockdev_snapshot_sync->has_format) {
1562 format = action->u.blockdev_snapshot_sync->format;
1563 }
1564 if (action->u.blockdev_snapshot_sync->has_mode) {
1565 mode = action->u.blockdev_snapshot_sync->mode;
1566 }
1567
1568 /* start processing */
1569 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
1570 has_node_name ? node_name : NULL,
1571 &local_err);
1572 if (local_err) {
1573 error_propagate(errp, local_err);
1574 return;
1575 }
1576
1577 if (has_node_name && !has_snapshot_node_name) {
1578 error_setg(errp, "New snapshot node name missing");
1579 return;
1580 }
1581
1582 if (has_snapshot_node_name &&
1583 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
1584 error_setg(errp, "New snapshot node name already in use");
1585 return;
1586 }
1587
1588 /* Acquire AioContext now so any threads operating on old_bs stop */
1589 state->aio_context = bdrv_get_aio_context(state->old_bs);
1590 aio_context_acquire(state->aio_context);
1591 bdrv_drained_begin(state->old_bs);
1592
1593 if (!bdrv_is_inserted(state->old_bs)) {
1594 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1595 return;
1596 }
1597
1598 if (bdrv_op_is_blocked(state->old_bs,
1599 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
1600 return;
1601 }
1602
1603 if (!bdrv_is_read_only(state->old_bs)) {
1604 if (bdrv_flush(state->old_bs)) {
1605 error_setg(errp, QERR_IO_ERROR);
1606 return;
1607 }
1608 }
1609
1610 if (!bdrv_is_first_non_filter(state->old_bs)) {
1611 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
1612 return;
1613 }
1614
1615 flags = state->old_bs->open_flags;
1616
1617 /* create new image w/backing file */
1618 if (mode != NEW_IMAGE_MODE_EXISTING) {
1619 bdrv_img_create(new_image_file, format,
1620 state->old_bs->filename,
1621 state->old_bs->drv->format_name,
1622 NULL, -1, flags, &local_err, false);
1623 if (local_err) {
1624 error_propagate(errp, local_err);
1625 return;
1626 }
1627 }
1628
1629 options = qdict_new();
1630 if (has_snapshot_node_name) {
1631 qdict_put(options, "node-name",
1632 qstring_from_str(snapshot_node_name));
1633 }
1634 qdict_put(options, "driver", qstring_from_str(format));
1635
1636 /* TODO Inherit bs->options or only take explicit options with an
1637 * extended QMP command? */
1638 assert(state->new_bs == NULL);
1639 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
1640 flags | BDRV_O_NO_BACKING, &local_err);
1641 /* We will manually add the backing_hd field to the bs later */
1642 if (ret != 0) {
1643 error_propagate(errp, local_err);
1644 }
1645}
1646
1647static void external_snapshot_commit(BlkTransactionState *common)
1648{
1649 ExternalSnapshotState *state =
1650 DO_UPCAST(ExternalSnapshotState, common, common);
1651
1652 bdrv_set_aio_context(state->new_bs, state->aio_context);
1653
1654 /* This removes our old bs and adds the new bs */
1655 bdrv_append(state->new_bs, state->old_bs);
1656 /* We don't need (or want) to use the transactional
1657 * bdrv_reopen_multiple() across all the entries at once, because we
1658 * don't want to abort all of them if one of them fails the reopen */
1659 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR,
1660 NULL);
1661}
1662
1663static void external_snapshot_abort(BlkTransactionState *common)
1664{
1665 ExternalSnapshotState *state =
1666 DO_UPCAST(ExternalSnapshotState, common, common);
1667 if (state->new_bs) {
1668 bdrv_unref(state->new_bs);
1669 }
1670}
1671
1672static void external_snapshot_clean(BlkTransactionState *common)
1673{
1674 ExternalSnapshotState *state =
1675 DO_UPCAST(ExternalSnapshotState, common, common);
1676 if (state->aio_context) {
1677 bdrv_drained_end(state->old_bs);
1678 aio_context_release(state->aio_context);
1679 }
1680}
1681
1682typedef struct DriveBackupState {
1683 BlkTransactionState common;
1684 BlockDriverState *bs;
1685 AioContext *aio_context;
1686 BlockJob *job;
1687} DriveBackupState;
1688
1689static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1690{
1691 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1692 BlockBackend *blk;
1693 DriveBackup *backup;
1694 Error *local_err = NULL;
1695
1696 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1697 backup = common->action->u.drive_backup;
1698
1699 blk = blk_by_name(backup->device);
1700 if (!blk) {
1701 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1702 "Device '%s' not found", backup->device);
1703 return;
1704 }
1705
1706 if (!blk_is_available(blk)) {
1707 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1708 return;
1709 }
1710
1711 /* AioContext is released in .clean() */
1712 state->aio_context = blk_get_aio_context(blk);
1713 aio_context_acquire(state->aio_context);
1714 bdrv_drained_begin(blk_bs(blk));
1715 state->bs = blk_bs(blk);
1716
1717 qmp_drive_backup(backup->device, backup->target,
1718 backup->has_format, backup->format,
1719 backup->sync,
1720 backup->has_mode, backup->mode,
1721 backup->has_speed, backup->speed,
1722 backup->has_bitmap, backup->bitmap,
1723 backup->has_on_source_error, backup->on_source_error,
1724 backup->has_on_target_error, backup->on_target_error,
1725 &local_err);
1726 if (local_err) {
1727 error_propagate(errp, local_err);
1728 return;
1729 }
1730
1731 state->job = state->bs->job;
1732}
1733
1734static void drive_backup_abort(BlkTransactionState *common)
1735{
1736 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1737 BlockDriverState *bs = state->bs;
1738
1739 /* Only cancel if it's the job we started */
1740 if (bs && bs->job && bs->job == state->job) {
1741 block_job_cancel_sync(bs->job);
1742 }
1743}
1744
1745static void drive_backup_clean(BlkTransactionState *common)
1746{
1747 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1748
1749 if (state->aio_context) {
1750 bdrv_drained_end(state->bs);
1751 aio_context_release(state->aio_context);
1752 }
1753}
1754
1755typedef struct BlockdevBackupState {
1756 BlkTransactionState common;
1757 BlockDriverState *bs;
1758 BlockJob *job;
1759 AioContext *aio_context;
1760} BlockdevBackupState;
1761
1762static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp)
1763{
1764 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1765 BlockdevBackup *backup;
1766 BlockBackend *blk, *target;
1767 Error *local_err = NULL;
1768
1769 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
1770 backup = common->action->u.blockdev_backup;
1771
1772 blk = blk_by_name(backup->device);
1773 if (!blk) {
1774 error_setg(errp, "Device '%s' not found", backup->device);
1775 return;
1776 }
1777
1778 if (!blk_is_available(blk)) {
1779 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device);
1780 return;
1781 }
1782
1783 target = blk_by_name(backup->target);
1784 if (!target) {
1785 error_setg(errp, "Device '%s' not found", backup->target);
1786 return;
1787 }
1788
1789 /* AioContext is released in .clean() */
1790 state->aio_context = blk_get_aio_context(blk);
1791 if (state->aio_context != blk_get_aio_context(target)) {
1792 state->aio_context = NULL;
1793 error_setg(errp, "Backup between two IO threads is not implemented");
1794 return;
1795 }
1796 aio_context_acquire(state->aio_context);
1797 state->bs = blk_bs(blk);
1798 bdrv_drained_begin(state->bs);
1799
1800 qmp_blockdev_backup(backup->device, backup->target,
1801 backup->sync,
1802 backup->has_speed, backup->speed,
1803 backup->has_on_source_error, backup->on_source_error,
1804 backup->has_on_target_error, backup->on_target_error,
1805 &local_err);
1806 if (local_err) {
1807 error_propagate(errp, local_err);
1808 return;
1809 }
1810
1811 state->job = state->bs->job;
1812}
1813
1814static void blockdev_backup_abort(BlkTransactionState *common)
1815{
1816 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1817 BlockDriverState *bs = state->bs;
1818
1819 /* Only cancel if it's the job we started */
1820 if (bs && bs->job && bs->job == state->job) {
1821 block_job_cancel_sync(bs->job);
1822 }
1823}
1824
1825static void blockdev_backup_clean(BlkTransactionState *common)
1826{
1827 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
1828
1829 if (state->aio_context) {
1830 bdrv_drained_end(state->bs);
1831 aio_context_release(state->aio_context);
1832 }
1833}
1834
1835static void abort_prepare(BlkTransactionState *common, Error **errp)
1836{
1837 error_setg(errp, "Transaction aborted using Abort action");
1838}
1839
1840static void abort_commit(BlkTransactionState *common)
1841{
1842 g_assert_not_reached(); /* this action never succeeds */
1843}
1844
1845static const BdrvActionOps actions[] = {
1846 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1847 .instance_size = sizeof(ExternalSnapshotState),
1848 .prepare = external_snapshot_prepare,
1849 .commit = external_snapshot_commit,
1850 .abort = external_snapshot_abort,
1851 .clean = external_snapshot_clean,
1852 },
1853 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1854 .instance_size = sizeof(DriveBackupState),
1855 .prepare = drive_backup_prepare,
1856 .abort = drive_backup_abort,
1857 .clean = drive_backup_clean,
1858 },
1859 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
1860 .instance_size = sizeof(BlockdevBackupState),
1861 .prepare = blockdev_backup_prepare,
1862 .abort = blockdev_backup_abort,
1863 .clean = blockdev_backup_clean,
1864 },
1865 [TRANSACTION_ACTION_KIND_ABORT] = {
1866 .instance_size = sizeof(BlkTransactionState),
1867 .prepare = abort_prepare,
1868 .commit = abort_commit,
1869 },
1870 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1871 .instance_size = sizeof(InternalSnapshotState),
1872 .prepare = internal_snapshot_prepare,
1873 .abort = internal_snapshot_abort,
1874 .clean = internal_snapshot_clean,
1875 },
1876};
1877
1878/*
1879 * 'Atomic' group operations. The operations are performed as a set, and if
1880 * any fail then we roll back all operations in the group.
1881 */
1882void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1883{
1884 TransactionActionList *dev_entry = dev_list;
1885 BlkTransactionState *state, *next;
1886 Error *local_err = NULL;
1887
1888 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1889 QSIMPLEQ_INIT(&snap_bdrv_states);
1890
1891 /* drain all i/o before any operations */
1892 bdrv_drain_all();
1893
1894 /* We don't do anything in this loop that commits us to the operations */
1895 while (NULL != dev_entry) {
1896 TransactionAction *dev_info = NULL;
1897 const BdrvActionOps *ops;
1898
1899 dev_info = dev_entry->value;
1900 dev_entry = dev_entry->next;
1901
1902 assert(dev_info->type < ARRAY_SIZE(actions));
1903
1904 ops = &actions[dev_info->type];
1905 assert(ops->instance_size > 0);
1906
1907 state = g_malloc0(ops->instance_size);
1908 state->ops = ops;
1909 state->action = dev_info;
1910 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1911
1912 state->ops->prepare(state, &local_err);
1913 if (local_err) {
1914 error_propagate(errp, local_err);
1915 goto delete_and_fail;
1916 }
1917 }
1918
1919 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1920 if (state->ops->commit) {
1921 state->ops->commit(state);
1922 }
1923 }
1924
1925 /* success */
1926 goto exit;
1927
1928delete_and_fail:
1929 /* failure, and it is all-or-none; roll back all operations */
1930 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1931 if (state->ops->abort) {
1932 state->ops->abort(state);
1933 }
1934 }
1935exit:
1936 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1937 if (state->ops->clean) {
1938 state->ops->clean(state);
1939 }
1940 g_free(state);
1941 }
1942}
1943
1944void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1945{
1946 Error *local_err = NULL;
1947
1948 qmp_blockdev_open_tray(device, has_force, force, &local_err);
1949 if (local_err) {
1950 error_propagate(errp, local_err);
1951 return;
1952 }
1953
1954 qmp_blockdev_remove_medium(device, errp);
1955}
1956
1957void qmp_block_passwd(bool has_device, const char *device,
1958 bool has_node_name, const char *node_name,
1959 const char *password, Error **errp)
1960{
1961 Error *local_err = NULL;
1962 BlockDriverState *bs;
1963 AioContext *aio_context;
1964
1965 bs = bdrv_lookup_bs(has_device ? device : NULL,
1966 has_node_name ? node_name : NULL,
1967 &local_err);
1968 if (local_err) {
1969 error_propagate(errp, local_err);
1970 return;
1971 }
1972
1973 aio_context = bdrv_get_aio_context(bs);
1974 aio_context_acquire(aio_context);
1975
1976 bdrv_add_key(bs, password, errp);
1977
1978 aio_context_release(aio_context);
1979}
1980
1981void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
1982 Error **errp)
1983{
1984 BlockBackend *blk;
1985 bool locked;
1986
1987 if (!has_force) {
1988 force = false;
1989 }
1990
1991 blk = blk_by_name(device);
1992 if (!blk) {
1993 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1994 "Device '%s' not found", device);
1995 return;
1996 }
1997
1998 if (!blk_dev_has_removable_media(blk)) {
1999 error_setg(errp, "Device '%s' is not removable", device);
2000 return;
2001 }
2002
2003 if (blk_dev_is_tray_open(blk)) {
2004 return;
2005 }
2006
2007 locked = blk_dev_is_medium_locked(blk);
2008 if (locked) {
2009 blk_dev_eject_request(blk, force);
2010 }
2011
2012 if (!locked || force) {
2013 blk_dev_change_media_cb(blk, false);
2014 }
2015}
2016
2017void qmp_blockdev_close_tray(const char *device, Error **errp)
2018{
2019 BlockBackend *blk;
2020
2021 blk = blk_by_name(device);
2022 if (!blk) {
2023 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2024 "Device '%s' not found", device);
2025 return;
2026 }
2027
2028 if (!blk_dev_has_removable_media(blk)) {
2029 error_setg(errp, "Device '%s' is not removable", device);
2030 return;
2031 }
2032
2033 if (!blk_dev_is_tray_open(blk)) {
2034 return;
2035 }
2036
2037 blk_dev_change_media_cb(blk, true);
2038}
2039
2040void qmp_blockdev_remove_medium(const char *device, Error **errp)
2041{
2042 BlockBackend *blk;
2043 BlockDriverState *bs;
2044 AioContext *aio_context;
2045 bool has_device;
2046
2047 blk = blk_by_name(device);
2048 if (!blk) {
2049 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2050 "Device '%s' not found", device);
2051 return;
2052 }
2053
2054 /* For BBs without a device, we can exchange the BDS tree at will */
2055 has_device = blk_get_attached_dev(blk);
2056
2057 if (has_device && !blk_dev_has_removable_media(blk)) {
2058 error_setg(errp, "Device '%s' is not removable", device);
2059 return;
2060 }
2061
2062 if (has_device && !blk_dev_is_tray_open(blk)) {
2063 error_setg(errp, "Tray of device '%s' is not open", device);
2064 return;
2065 }
2066
2067 bs = blk_bs(blk);
2068 if (!bs) {
2069 return;
2070 }
2071
2072 aio_context = bdrv_get_aio_context(bs);
2073 aio_context_acquire(aio_context);
2074
2075 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
2076 goto out;
2077 }
2078
2079 /* This follows the convention established by bdrv_make_anon() */
2080 if (bs->device_list.tqe_prev) {
2081 QTAILQ_REMOVE(&bdrv_states, bs, device_list);
2082 bs->device_list.tqe_prev = NULL;
2083 }
2084
2085 blk_remove_bs(blk);
2086
2087out:
2088 aio_context_release(aio_context);
2089}
2090
2091static void qmp_blockdev_insert_anon_medium(const char *device,
2092 BlockDriverState *bs, Error **errp)
2093{
2094 BlockBackend *blk;
2095 bool has_device;
2096
2097 blk = blk_by_name(device);
2098 if (!blk) {
2099 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2100 "Device '%s' not found", device);
2101 return;
2102 }
2103
2104 /* For BBs without a device, we can exchange the BDS tree at will */
2105 has_device = blk_get_attached_dev(blk);
2106
2107 if (has_device && !blk_dev_has_removable_media(blk)) {
2108 error_setg(errp, "Device '%s' is not removable", device);
2109 return;
2110 }
2111
2112 if (has_device && !blk_dev_is_tray_open(blk)) {
2113 error_setg(errp, "Tray of device '%s' is not open", device);
2114 return;
2115 }
2116
2117 if (blk_bs(blk)) {
2118 error_setg(errp, "There already is a medium in device '%s'", device);
2119 return;
2120 }
2121
2122 blk_insert_bs(blk, bs);
2123
2124 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list);
2125}
2126
2127void qmp_blockdev_insert_medium(const char *device, const char *node_name,
2128 Error **errp)
2129{
2130 BlockDriverState *bs;
2131
2132 bs = bdrv_find_node(node_name);
2133 if (!bs) {
2134 error_setg(errp, "Node '%s' not found", node_name);
2135 return;
2136 }
2137
2138 if (bs->blk) {
2139 error_setg(errp, "Node '%s' is already in use by '%s'", node_name,
2140 blk_name(bs->blk));
2141 return;
2142 }
2143
2144 qmp_blockdev_insert_anon_medium(device, bs, errp);
2145}
2146
2147void qmp_blockdev_change_medium(const char *device, const char *filename,
2148 bool has_format, const char *format,
2149 bool has_read_only,
2150 BlockdevChangeReadOnlyMode read_only,
2151 Error **errp)
2152{
2153 BlockBackend *blk;
2154 BlockDriverState *medium_bs = NULL;
2155 int bdrv_flags, ret;
2156 QDict *options = NULL;
2157 Error *err = NULL;
2158
2159 blk = blk_by_name(device);
2160 if (!blk) {
2161 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2162 "Device '%s' not found", device);
2163 goto fail;
2164 }
2165
2166 if (blk_bs(blk)) {
2167 blk_update_root_state(blk);
2168 }
2169
2170 bdrv_flags = blk_get_open_flags_from_root_state(blk);
2171
2172 if (!has_read_only) {
2173 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
2174 }
2175
2176 switch (read_only) {
2177 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
2178 break;
2179
2180 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
2181 bdrv_flags &= ~BDRV_O_RDWR;
2182 break;
2183
2184 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
2185 bdrv_flags |= BDRV_O_RDWR;
2186 break;
2187
2188 default:
2189 abort();
2190 }
2191
2192 if (has_format) {
2193 options = qdict_new();
2194 qdict_put(options, "driver", qstring_from_str(format));
2195 }
2196
2197 assert(!medium_bs);
2198 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp);
2199 if (ret < 0) {
2200 goto fail;
2201 }
2202
2203 blk_apply_root_state(blk, medium_bs);
2204
2205 bdrv_add_key(medium_bs, NULL, &err);
2206 if (err) {
2207 error_propagate(errp, err);
2208 goto fail;
2209 }
2210
2211 qmp_blockdev_open_tray(device, false, false, &err);
2212 if (err) {
2213 error_propagate(errp, err);
2214 goto fail;
2215 }
2216
2217 qmp_blockdev_remove_medium(device, &err);
2218 if (err) {
2219 error_propagate(errp, err);
2220 goto fail;
2221 }
2222
2223 qmp_blockdev_insert_anon_medium(device, medium_bs, &err);
2224 if (err) {
2225 error_propagate(errp, err);
2226 goto fail;
2227 }
2228
2229 qmp_blockdev_close_tray(device, errp);
2230
2231fail:
2232 /* If the medium has been inserted, the device has its own reference, so
2233 * ours must be relinquished; and if it has not been inserted successfully,
2234 * the reference must be relinquished anyway */
2235 bdrv_unref(medium_bs);
2236}
2237
2238/* throttling disk I/O limits */
2239void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
2240 int64_t bps_wr,
2241 int64_t iops,
2242 int64_t iops_rd,
2243 int64_t iops_wr,
2244 bool has_bps_max,
2245 int64_t bps_max,
2246 bool has_bps_rd_max,
2247 int64_t bps_rd_max,
2248 bool has_bps_wr_max,
2249 int64_t bps_wr_max,
2250 bool has_iops_max,
2251 int64_t iops_max,
2252 bool has_iops_rd_max,
2253 int64_t iops_rd_max,
2254 bool has_iops_wr_max,
2255 int64_t iops_wr_max,
2256 bool has_iops_size,
2257 int64_t iops_size,
2258 bool has_group,
2259 const char *group, Error **errp)
2260{
2261 ThrottleConfig cfg;
2262 BlockDriverState *bs;
2263 BlockBackend *blk;
2264 AioContext *aio_context;
2265
2266 blk = blk_by_name(device);
2267 if (!blk) {
2268 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2269 "Device '%s' not found", device);
2270 return;
2271 }
2272
2273 aio_context = blk_get_aio_context(blk);
2274 aio_context_acquire(aio_context);
2275
2276 bs = blk_bs(blk);
2277 if (!bs) {
2278 error_setg(errp, "Device '%s' has no medium", device);
2279 goto out;
2280 }
2281
2282 memset(&cfg, 0, sizeof(cfg));
2283 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
2284 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
2285 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
2286
2287 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
2288 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
2289 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
2290
2291 if (has_bps_max) {
2292 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
2293 }
2294 if (has_bps_rd_max) {
2295 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
2296 }
2297 if (has_bps_wr_max) {
2298 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
2299 }
2300 if (has_iops_max) {
2301 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
2302 }
2303 if (has_iops_rd_max) {
2304 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
2305 }
2306 if (has_iops_wr_max) {
2307 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
2308 }
2309
2310 if (has_iops_size) {
2311 cfg.op_size = iops_size;
2312 }
2313
2314 if (!check_throttle_config(&cfg, errp)) {
2315 goto out;
2316 }
2317
2318 if (throttle_enabled(&cfg)) {
2319 /* Enable I/O limits if they're not enabled yet, otherwise
2320 * just update the throttling group. */
2321 if (!bs->io_limits_enabled) {
2322 bdrv_io_limits_enable(bs, has_group ? group : device);
2323 } else if (has_group) {
2324 bdrv_io_limits_update_group(bs, group);
2325 }
2326 /* Set the new throttling configuration */
2327 bdrv_set_io_limits(bs, &cfg);
2328 } else if (bs->io_limits_enabled) {
2329 /* If all throttling settings are set to 0, disable I/O limits */
2330 bdrv_io_limits_disable(bs);
2331 }
2332
2333out:
2334 aio_context_release(aio_context);
2335}
2336
2337void qmp_block_dirty_bitmap_add(const char *node, const char *name,
2338 bool has_granularity, uint32_t granularity,
2339 Error **errp)
2340{
2341 AioContext *aio_context;
2342 BlockDriverState *bs;
2343
2344 if (!name || name[0] == '\0') {
2345 error_setg(errp, "Bitmap name cannot be empty");
2346 return;
2347 }
2348
2349 bs = bdrv_lookup_bs(node, node, errp);
2350 if (!bs) {
2351 return;
2352 }
2353
2354 aio_context = bdrv_get_aio_context(bs);
2355 aio_context_acquire(aio_context);
2356
2357 if (has_granularity) {
2358 if (granularity < 512 || !is_power_of_2(granularity)) {
2359 error_setg(errp, "Granularity must be power of 2 "
2360 "and at least 512");
2361 goto out;
2362 }
2363 } else {
2364 /* Default to cluster size, if available: */
2365 granularity = bdrv_get_default_bitmap_granularity(bs);
2366 }
2367
2368 bdrv_create_dirty_bitmap(bs, granularity, name, errp);
2369
2370 out:
2371 aio_context_release(aio_context);
2372}
2373
2374void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
2375 Error **errp)
2376{
2377 AioContext *aio_context;
2378 BlockDriverState *bs;
2379 BdrvDirtyBitmap *bitmap;
2380
2381 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2382 if (!bitmap || !bs) {
2383 return;
2384 }
2385
2386 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2387 error_setg(errp,
2388 "Bitmap '%s' is currently frozen and cannot be removed",
2389 name);
2390 goto out;
2391 }
2392 bdrv_dirty_bitmap_make_anon(bitmap);
2393 bdrv_release_dirty_bitmap(bs, bitmap);
2394
2395 out:
2396 aio_context_release(aio_context);
2397}
2398
2399/**
2400 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2401 * immediately after a full backup operation.
2402 */
2403void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
2404 Error **errp)
2405{
2406 AioContext *aio_context;
2407 BdrvDirtyBitmap *bitmap;
2408 BlockDriverState *bs;
2409
2410 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
2411 if (!bitmap || !bs) {
2412 return;
2413 }
2414
2415 if (bdrv_dirty_bitmap_frozen(bitmap)) {
2416 error_setg(errp,
2417 "Bitmap '%s' is currently frozen and cannot be modified",
2418 name);
2419 goto out;
2420 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
2421 error_setg(errp,
2422 "Bitmap '%s' is currently disabled and cannot be cleared",
2423 name);
2424 goto out;
2425 }
2426
2427 bdrv_clear_dirty_bitmap(bitmap);
2428
2429 out:
2430 aio_context_release(aio_context);
2431}
2432
2433void hmp_drive_del(Monitor *mon, const QDict *qdict)
2434{
2435 const char *id = qdict_get_str(qdict, "id");
2436 BlockBackend *blk;
2437 BlockDriverState *bs;
2438 AioContext *aio_context;
2439 Error *local_err = NULL;
2440
2441 blk = blk_by_name(id);
2442 if (!blk) {
2443 error_report("Device '%s' not found", id);
2444 return;
2445 }
2446
2447 if (!blk_legacy_dinfo(blk)) {
2448 error_report("Deleting device added with blockdev-add"
2449 " is not supported");
2450 return;
2451 }
2452
2453 aio_context = blk_get_aio_context(blk);
2454 aio_context_acquire(aio_context);
2455
2456 bs = blk_bs(blk);
2457 if (bs) {
2458 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
2459 error_report_err(local_err);
2460 aio_context_release(aio_context);
2461 return;
2462 }
2463
2464 bdrv_close(bs);
2465 }
2466
2467 /* if we have a device attached to this BlockDriverState
2468 * then we need to make the drive anonymous until the device
2469 * can be removed. If this is a drive with no device backing
2470 * then we can just get rid of the block driver state right here.
2471 */
2472 if (blk_get_attached_dev(blk)) {
2473 blk_hide_on_behalf_of_hmp_drive_del(blk);
2474 /* Further I/O must not pause the guest */
2475 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
2476 BLOCKDEV_ON_ERROR_REPORT);
2477 } else {
2478 blk_unref(blk);
2479 }
2480
2481 aio_context_release(aio_context);
2482}
2483
2484void qmp_block_resize(bool has_device, const char *device,
2485 bool has_node_name, const char *node_name,
2486 int64_t size, Error **errp)
2487{
2488 Error *local_err = NULL;
2489 BlockDriverState *bs;
2490 AioContext *aio_context;
2491 int ret;
2492
2493 bs = bdrv_lookup_bs(has_device ? device : NULL,
2494 has_node_name ? node_name : NULL,
2495 &local_err);
2496 if (local_err) {
2497 error_propagate(errp, local_err);
2498 return;
2499 }
2500
2501 aio_context = bdrv_get_aio_context(bs);
2502 aio_context_acquire(aio_context);
2503
2504 if (!bdrv_is_first_non_filter(bs)) {
2505 error_setg(errp, QERR_FEATURE_DISABLED, "resize");
2506 goto out;
2507 }
2508
2509 if (size < 0) {
2510 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
2511 goto out;
2512 }
2513
2514 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
2515 error_setg(errp, QERR_DEVICE_IN_USE, device);
2516 goto out;
2517 }
2518
2519 /* complete all in-flight operations before resizing the device */
2520 bdrv_drain_all();
2521
2522 ret = bdrv_truncate(bs, size);
2523 switch (ret) {
2524 case 0:
2525 break;
2526 case -ENOMEDIUM:
2527 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2528 break;
2529 case -ENOTSUP:
2530 error_setg(errp, QERR_UNSUPPORTED);
2531 break;
2532 case -EACCES:
2533 error_setg(errp, "Device '%s' is read only", device);
2534 break;
2535 case -EBUSY:
2536 error_setg(errp, QERR_DEVICE_IN_USE, device);
2537 break;
2538 default:
2539 error_setg_errno(errp, -ret, "Could not resize");
2540 break;
2541 }
2542
2543out:
2544 aio_context_release(aio_context);
2545}
2546
2547static void block_job_cb(void *opaque, int ret)
2548{
2549 /* Note that this function may be executed from another AioContext besides
2550 * the QEMU main loop. If you need to access anything that assumes the
2551 * QEMU global mutex, use a BH or introduce a mutex.
2552 */
2553
2554 BlockDriverState *bs = opaque;
2555 const char *msg = NULL;
2556
2557 trace_block_job_cb(bs, bs->job, ret);
2558
2559 assert(bs->job);
2560
2561 if (ret < 0) {
2562 msg = strerror(-ret);
2563 }
2564
2565 if (block_job_is_cancelled(bs->job)) {
2566 block_job_event_cancelled(bs->job);
2567 } else {
2568 block_job_event_completed(bs->job, msg);
2569 }
2570
2571 bdrv_put_ref_bh_schedule(bs);
2572}
2573
2574void qmp_block_stream(const char *device,
2575 bool has_base, const char *base,
2576 bool has_backing_file, const char *backing_file,
2577 bool has_speed, int64_t speed,
2578 bool has_on_error, BlockdevOnError on_error,
2579 Error **errp)
2580{
2581 BlockBackend *blk;
2582 BlockDriverState *bs;
2583 BlockDriverState *base_bs = NULL;
2584 AioContext *aio_context;
2585 Error *local_err = NULL;
2586 const char *base_name = NULL;
2587
2588 if (!has_on_error) {
2589 on_error = BLOCKDEV_ON_ERROR_REPORT;
2590 }
2591
2592 blk = blk_by_name(device);
2593 if (!blk) {
2594 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2595 "Device '%s' not found", device);
2596 return;
2597 }
2598
2599 aio_context = blk_get_aio_context(blk);
2600 aio_context_acquire(aio_context);
2601
2602 if (!blk_is_available(blk)) {
2603 error_setg(errp, "Device '%s' has no medium", device);
2604 goto out;
2605 }
2606 bs = blk_bs(blk);
2607
2608 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
2609 goto out;
2610 }
2611
2612 if (has_base) {
2613 base_bs = bdrv_find_backing_image(bs, base);
2614 if (base_bs == NULL) {
2615 error_setg(errp, QERR_BASE_NOT_FOUND, base);
2616 goto out;
2617 }
2618 assert(bdrv_get_aio_context(base_bs) == aio_context);
2619 base_name = base;
2620 }
2621
2622 /* if we are streaming the entire chain, the result will have no backing
2623 * file, and specifying one is therefore an error */
2624 if (base_bs == NULL && has_backing_file) {
2625 error_setg(errp, "backing file specified, but streaming the "
2626 "entire chain");
2627 goto out;
2628 }
2629
2630 /* backing_file string overrides base bs filename */
2631 base_name = has_backing_file ? backing_file : base_name;
2632
2633 stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
2634 on_error, block_job_cb, bs, &local_err);
2635 if (local_err) {
2636 error_propagate(errp, local_err);
2637 goto out;
2638 }
2639
2640 trace_qmp_block_stream(bs, bs->job);
2641
2642out:
2643 aio_context_release(aio_context);
2644}
2645
2646void qmp_block_commit(const char *device,
2647 bool has_base, const char *base,
2648 bool has_top, const char *top,
2649 bool has_backing_file, const char *backing_file,
2650 bool has_speed, int64_t speed,
2651 Error **errp)
2652{
2653 BlockBackend *blk;
2654 BlockDriverState *bs;
2655 BlockDriverState *base_bs, *top_bs;
2656 AioContext *aio_context;
2657 Error *local_err = NULL;
2658 /* This will be part of the QMP command, if/when the
2659 * BlockdevOnError change for blkmirror makes it in
2660 */
2661 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
2662
2663 if (!has_speed) {
2664 speed = 0;
2665 }
2666
2667 /* Important Note:
2668 * libvirt relies on the DeviceNotFound error class in order to probe for
2669 * live commit feature versions; for this to work, we must make sure to
2670 * perform the device lookup before any generic errors that may occur in a
2671 * scenario in which all optional arguments are omitted. */
2672 blk = blk_by_name(device);
2673 if (!blk) {
2674 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2675 "Device '%s' not found", device);
2676 return;
2677 }
2678
2679 aio_context = blk_get_aio_context(blk);
2680 aio_context_acquire(aio_context);
2681
2682 if (!blk_is_available(blk)) {
2683 error_setg(errp, "Device '%s' has no medium", device);
2684 goto out;
2685 }
2686 bs = blk_bs(blk);
2687
2688 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
2689 goto out;
2690 }
2691
2692 /* default top_bs is the active layer */
2693 top_bs = bs;
2694
2695 if (has_top && top) {
2696 if (strcmp(bs->filename, top) != 0) {
2697 top_bs = bdrv_find_backing_image(bs, top);
2698 }
2699 }
2700
2701 if (top_bs == NULL) {
2702 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
2703 goto out;
2704 }
2705
2706 assert(bdrv_get_aio_context(top_bs) == aio_context);
2707
2708 if (has_base && base) {
2709 base_bs = bdrv_find_backing_image(top_bs, base);
2710 } else {
2711 base_bs = bdrv_find_base(top_bs);
2712 }
2713
2714 if (base_bs == NULL) {
2715 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
2716 goto out;
2717 }
2718
2719 assert(bdrv_get_aio_context(base_bs) == aio_context);
2720
2721 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
2722 goto out;
2723 }
2724
2725 /* Do not allow attempts to commit an image into itself */
2726 if (top_bs == base_bs) {
2727 error_setg(errp, "cannot commit an image into itself");
2728 goto out;
2729 }
2730
2731 if (top_bs == bs) {
2732 if (has_backing_file) {
2733 error_setg(errp, "'backing-file' specified,"
2734 " but 'top' is the active layer");
2735 goto out;
2736 }
2737 commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
2738 bs, &local_err);
2739 } else {
2740 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
2741 has_backing_file ? backing_file : NULL, &local_err);
2742 }
2743 if (local_err != NULL) {
2744 error_propagate(errp, local_err);
2745 goto out;
2746 }
2747
2748out:
2749 aio_context_release(aio_context);
2750}
2751
2752void qmp_drive_backup(const char *device, const char *target,
2753 bool has_format, const char *format,
2754 enum MirrorSyncMode sync,
2755 bool has_mode, enum NewImageMode mode,
2756 bool has_speed, int64_t speed,
2757 bool has_bitmap, const char *bitmap,
2758 bool has_on_source_error, BlockdevOnError on_source_error,
2759 bool has_on_target_error, BlockdevOnError on_target_error,
2760 Error **errp)
2761{
2762 BlockBackend *blk;
2763 BlockDriverState *bs;
2764 BlockDriverState *target_bs;
2765 BlockDriverState *source = NULL;
2766 BdrvDirtyBitmap *bmap = NULL;
2767 AioContext *aio_context;
2768 QDict *options = NULL;
2769 Error *local_err = NULL;
2770 int flags;
2771 int64_t size;
2772 int ret;
2773
2774 if (!has_speed) {
2775 speed = 0;
2776 }
2777 if (!has_on_source_error) {
2778 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2779 }
2780 if (!has_on_target_error) {
2781 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2782 }
2783 if (!has_mode) {
2784 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2785 }
2786
2787 blk = blk_by_name(device);
2788 if (!blk) {
2789 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
2790 "Device '%s' not found", device);
2791 return;
2792 }
2793
2794 aio_context = blk_get_aio_context(blk);
2795 aio_context_acquire(aio_context);
2796
2797 /* Although backup_run has this check too, we need to use bs->drv below, so
2798 * do an early check redundantly. */
2799 if (!blk_is_available(blk)) {
2800 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
2801 goto out;
2802 }
2803 bs = blk_bs(blk);
2804
2805 if (!has_format) {
2806 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
2807 }
2808
2809 /* Early check to avoid creating target */
2810 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
2811 goto out;
2812 }
2813
2814 flags = bs->open_flags | BDRV_O_RDWR;
2815
2816 /* See if we have a backing HD we can use to create our new image
2817 * on top of. */
2818 if (sync == MIRROR_SYNC_MODE_TOP) {
2819 source = backing_bs(bs);
2820 if (!source) {
2821 sync = MIRROR_SYNC_MODE_FULL;
2822 }
2823 }
2824 if (sync == MIRROR_SYNC_MODE_NONE) {
2825 source = bs;
2826 }
2827
2828 size = bdrv_getlength(bs);
2829 if (size < 0) {
2830 error_setg_errno(errp, -size, "bdrv_getlength failed");
2831 goto out;
2832 }
2833
2834 if (mode != NEW_IMAGE_MODE_EXISTING) {
2835 assert(format);
2836 if (source) {
2837 bdrv_img_create(target, format, source->filename,
2838 source->drv->format_name, NULL,
2839 size, flags, &local_err, false);
2840 } else {
2841 bdrv_img_create(target, format, NULL, NULL, NULL,
2842 size, flags, &local_err, false);
2843 }
2844 }
2845
2846 if (local_err) {
2847 error_propagate(errp, local_err);
2848 goto out;
2849 }
2850
2851 if (format) {
2852 options = qdict_new();
2853 qdict_put(options, "driver", qstring_from_str(format));
2854 }
2855
2856 target_bs = NULL;
2857 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err);
2858 if (ret < 0) {
2859 error_propagate(errp, local_err);
2860 goto out;
2861 }
2862
2863 bdrv_set_aio_context(target_bs, aio_context);
2864
2865 if (has_bitmap) {
2866 bmap = bdrv_find_dirty_bitmap(bs, bitmap);
2867 if (!bmap) {
2868 error_setg(errp, "Bitmap '%s' could not be found", bitmap);
2869 goto out;
2870 }
2871 }
2872
2873 backup_start(bs, target_bs, speed, sync, bmap,
2874 on_source_error, on_target_error,
2875 block_job_cb, bs, &local_err);
2876 if (local_err != NULL) {
2877 bdrv_unref(target_bs);
2878 error_propagate(errp, local_err);
2879 goto out;
2880 }
2881
2882out:
2883 aio_context_release(aio_context);
2884}
2885
2886BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
2887{
2888 return bdrv_named_nodes_list(errp);
2889}
2890
2891void qmp_blockdev_backup(const char *device, const char *target,
2892 enum MirrorSyncMode sync,
2893 bool has_speed, int64_t speed,
2894 bool has_on_source_error,
2895 BlockdevOnError on_source_error,
2896 bool has_on_target_error,
2897 BlockdevOnError on_target_error,
2898 Error **errp)
2899{
2900 BlockBackend *blk, *target_blk;
2901 BlockDriverState *bs;
2902 BlockDriverState *target_bs;
2903 Error *local_err = NULL;
2904 AioContext *aio_context;
2905
2906 if (!has_speed) {
2907 speed = 0;
2908 }
2909 if (!has_on_source_error) {
2910 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2911 }
2912 if (!has_on_target_error) {
2913 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2914 }
2915
2916 blk = blk_by_name(device);
2917 if (!blk) {
2918 error_setg(errp, "Device '%s' not found", device);
2919 return;
2920 }
2921
2922 aio_context = blk_get_aio_context(blk);
2923 aio_context_acquire(aio_context);
2924
2925 if (!blk_is_available(blk)) {
2926 error_setg(errp, "Device '%s' has no medium", device);
2927 goto out;
2928 }
2929 bs = blk_bs(blk);
2930
2931 target_blk = blk_by_name(target);
2932 if (!target_blk) {
2933 error_setg(errp, "Device '%s' not found", target);
2934 goto out;
2935 }
2936
2937 if (!blk_is_available(target_blk)) {
2938 error_setg(errp, "Device '%s' has no medium", target);
2939 goto out;
2940 }
2941 target_bs = blk_bs(target_blk);
2942
2943 bdrv_ref(target_bs);
2944 bdrv_set_aio_context(target_bs, aio_context);
2945 backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
2946 on_target_error, block_job_cb, bs, &local_err);
2947 if (local_err != NULL) {
2948 bdrv_unref(target_bs);
2949 error_propagate(errp, local_err);
2950 }
2951out:
2952 aio_context_release(aio_context);
2953}
2954
2955void qmp_drive_mirror(const char *device, const char *target,
2956 bool has_format, const char *format,
2957 bool has_node_name, const char *node_name,
2958 bool has_replaces, const char *replaces,
2959 enum MirrorSyncMode sync,
2960 bool has_mode, enum NewImageMode mode,
2961 bool has_speed, int64_t speed,
2962 bool has_granularity, uint32_t granularity,
2963 bool has_buf_size, int64_t buf_size,
2964 bool has_on_source_error, BlockdevOnError on_source_error,
2965 bool has_on_target_error, BlockdevOnError on_target_error,
2966 bool has_unmap, bool unmap,
2967 Error **errp)
2968{
2969 BlockBackend *blk;
2970 BlockDriverState *bs;
2971 BlockDriverState *source, *target_bs;
2972 AioContext *aio_context;
2973 Error *local_err = NULL;
2974 QDict *options;
2975 int flags;
2976 int64_t size;
2977 int ret;
2978
2979 if (!has_speed) {
2980 speed = 0;
2981 }
2982 if (!has_on_source_error) {
2983 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
2984 }
2985 if (!has_on_target_error) {
2986 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
2987 }
2988 if (!has_mode) {
2989 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
2990 }
2991 if (!has_granularity) {
2992 granularity = 0;
2993 }
2994 if (!has_buf_size) {
2995 buf_size = 0;
2996 }
2997 if (!has_unmap) {
2998 unmap = true;
2999 }
3000
3001 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
3002 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3003 "a value in range [512B, 64MB]");
3004 return;
3005 }
3006 if (granularity & (granularity - 1)) {
3007 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
3008 "power of 2");
3009 return;
3010 }
3011
3012 blk = blk_by_name(device);
3013 if (!blk) {
3014 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3015 "Device '%s' not found", device);
3016 return;
3017 }
3018
3019 aio_context = blk_get_aio_context(blk);
3020 aio_context_acquire(aio_context);
3021
3022 if (!blk_is_available(blk)) {
3023 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
3024 goto out;
3025 }
3026 bs = blk_bs(blk);
3027
3028 if (!has_format) {
3029 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
3030 }
3031
3032 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
3033 goto out;
3034 }
3035
3036 flags = bs->open_flags | BDRV_O_RDWR;
3037 source = backing_bs(bs);
3038 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
3039 sync = MIRROR_SYNC_MODE_FULL;
3040 }
3041 if (sync == MIRROR_SYNC_MODE_NONE) {
3042 source = bs;
3043 }
3044
3045 size = bdrv_getlength(bs);
3046 if (size < 0) {
3047 error_setg_errno(errp, -size, "bdrv_getlength failed");
3048 goto out;
3049 }
3050
3051 if (has_replaces) {
3052 BlockDriverState *to_replace_bs;
3053 AioContext *replace_aio_context;
3054 int64_t replace_size;
3055
3056 if (!has_node_name) {
3057 error_setg(errp, "a node-name must be provided when replacing a"
3058 " named node of the graph");
3059 goto out;
3060 }
3061
3062 to_replace_bs = check_to_replace_node(bs, replaces, &local_err);
3063
3064 if (!to_replace_bs) {
3065 error_propagate(errp, local_err);
3066 goto out;
3067 }
3068
3069 replace_aio_context = bdrv_get_aio_context(to_replace_bs);
3070 aio_context_acquire(replace_aio_context);
3071 replace_size = bdrv_getlength(to_replace_bs);
3072 aio_context_release(replace_aio_context);
3073
3074 if (size != replace_size) {
3075 error_setg(errp, "cannot replace image with a mirror image of "
3076 "different size");
3077 goto out;
3078 }
3079 }
3080
3081 if ((sync == MIRROR_SYNC_MODE_FULL || !source)
3082 && mode != NEW_IMAGE_MODE_EXISTING)
3083 {
3084 /* create new image w/o backing file */
3085 assert(format);
3086 bdrv_img_create(target, format,
3087 NULL, NULL, NULL, size, flags, &local_err, false);
3088 } else {
3089 switch (mode) {
3090 case NEW_IMAGE_MODE_EXISTING:
3091 break;
3092 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
3093 /* create new image with backing file */
3094 bdrv_img_create(target, format,
3095 source->filename,
3096 source->drv->format_name,
3097 NULL, size, flags, &local_err, false);
3098 break;
3099 default:
3100 abort();
3101 }
3102 }
3103
3104 if (local_err) {
3105 error_propagate(errp, local_err);
3106 goto out;
3107 }
3108
3109 options = qdict_new();
3110 if (has_node_name) {
3111 qdict_put(options, "node-name", qstring_from_str(node_name));
3112 }
3113 if (format) {
3114 qdict_put(options, "driver", qstring_from_str(format));
3115 }
3116
3117 /* Mirroring takes care of copy-on-write using the source's backing
3118 * file.
3119 */
3120 target_bs = NULL;
3121 ret = bdrv_open(&target_bs, target, NULL, options,
3122 flags | BDRV_O_NO_BACKING, &local_err);
3123 if (ret < 0) {
3124 error_propagate(errp, local_err);
3125 goto out;
3126 }
3127
3128 bdrv_set_aio_context(target_bs, aio_context);
3129
3130 /* pass the node name to replace to mirror start since it's loose coupling
3131 * and will allow to check whether the node still exist at mirror completion
3132 */
3133 mirror_start(bs, target_bs,
3134 has_replaces ? replaces : NULL,
3135 speed, granularity, buf_size, sync,
3136 on_source_error, on_target_error,
3137 unmap,
3138 block_job_cb, bs, &local_err);
3139 if (local_err != NULL) {
3140 bdrv_unref(target_bs);
3141 error_propagate(errp, local_err);
3142 goto out;
3143 }
3144
3145out:
3146 aio_context_release(aio_context);
3147}
3148
3149/* Get the block job for a given device name and acquire its AioContext */
3150static BlockJob *find_block_job(const char *device, AioContext **aio_context,
3151 Error **errp)
3152{
3153 BlockBackend *blk;
3154 BlockDriverState *bs;
3155
3156 *aio_context = NULL;
3157
3158 blk = blk_by_name(device);
3159 if (!blk) {
3160 goto notfound;
3161 }
3162
3163 *aio_context = blk_get_aio_context(blk);
3164 aio_context_acquire(*aio_context);
3165
3166 if (!blk_is_available(blk)) {
3167 goto notfound;
3168 }
3169 bs = blk_bs(blk);
3170
3171 if (!bs->job) {
3172 goto notfound;
3173 }
3174
3175 return bs->job;
3176
3177notfound:
3178 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
3179 "No active block job on device '%s'", device);
3180 if (*aio_context) {
3181 aio_context_release(*aio_context);
3182 *aio_context = NULL;
3183 }
3184 return NULL;
3185}
3186
3187void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
3188{
3189 AioContext *aio_context;
3190 BlockJob *job = find_block_job(device, &aio_context, errp);
3191
3192 if (!job) {
3193 return;
3194 }
3195
3196 block_job_set_speed(job, speed, errp);
3197 aio_context_release(aio_context);
3198}
3199
3200void qmp_block_job_cancel(const char *device,
3201 bool has_force, bool force, Error **errp)
3202{
3203 AioContext *aio_context;
3204 BlockJob *job = find_block_job(device, &aio_context, errp);
3205
3206 if (!job) {
3207 return;
3208 }
3209
3210 if (!has_force) {
3211 force = false;
3212 }
3213
3214 if (job->user_paused && !force) {
3215 error_setg(errp, "The block job for device '%s' is currently paused",
3216 device);
3217 goto out;
3218 }
3219
3220 trace_qmp_block_job_cancel(job);
3221 block_job_cancel(job);
3222out:
3223 aio_context_release(aio_context);
3224}
3225
3226void qmp_block_job_pause(const char *device, Error **errp)
3227{
3228 AioContext *aio_context;
3229 BlockJob *job = find_block_job(device, &aio_context, errp);
3230
3231 if (!job || job->user_paused) {
3232 return;
3233 }
3234
3235 job->user_paused = true;
3236 trace_qmp_block_job_pause(job);
3237 block_job_pause(job);
3238 aio_context_release(aio_context);
3239}
3240
3241void qmp_block_job_resume(const char *device, Error **errp)
3242{
3243 AioContext *aio_context;
3244 BlockJob *job = find_block_job(device, &aio_context, errp);
3245
3246 if (!job || !job->user_paused) {
3247 return;
3248 }
3249
3250 job->user_paused = false;
3251 trace_qmp_block_job_resume(job);
3252 block_job_resume(job);
3253 aio_context_release(aio_context);
3254}
3255
3256void qmp_block_job_complete(const char *device, Error **errp)
3257{
3258 AioContext *aio_context;
3259 BlockJob *job = find_block_job(device, &aio_context, errp);
3260
3261 if (!job) {
3262 return;
3263 }
3264
3265 trace_qmp_block_job_complete(job);
3266 block_job_complete(job, errp);
3267 aio_context_release(aio_context);
3268}
3269
3270void qmp_change_backing_file(const char *device,
3271 const char *image_node_name,
3272 const char *backing_file,
3273 Error **errp)
3274{
3275 BlockBackend *blk;
3276 BlockDriverState *bs = NULL;
3277 AioContext *aio_context;
3278 BlockDriverState *image_bs = NULL;
3279 Error *local_err = NULL;
3280 bool ro;
3281 int open_flags;
3282 int ret;
3283
3284 blk = blk_by_name(device);
3285 if (!blk) {
3286 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
3287 "Device '%s' not found", device);
3288 return;
3289 }
3290
3291 aio_context = blk_get_aio_context(blk);
3292 aio_context_acquire(aio_context);
3293
3294 if (!blk_is_available(blk)) {
3295 error_setg(errp, "Device '%s' has no medium", device);
3296 goto out;
3297 }
3298 bs = blk_bs(blk);
3299
3300 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
3301 if (local_err) {
3302 error_propagate(errp, local_err);
3303 goto out;
3304 }
3305
3306 if (!image_bs) {
3307 error_setg(errp, "image file not found");
3308 goto out;
3309 }
3310
3311 if (bdrv_find_base(image_bs) == image_bs) {
3312 error_setg(errp, "not allowing backing file change on an image "
3313 "without a backing file");
3314 goto out;
3315 }
3316
3317 /* even though we are not necessarily operating on bs, we need it to
3318 * determine if block ops are currently prohibited on the chain */
3319 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
3320 goto out;
3321 }
3322
3323 /* final sanity check */
3324 if (!bdrv_chain_contains(bs, image_bs)) {
3325 error_setg(errp, "'%s' and image file are not in the same chain",
3326 device);
3327 goto out;
3328 }
3329
3330 /* if not r/w, reopen to make r/w */
3331 open_flags = image_bs->open_flags;
3332 ro = bdrv_is_read_only(image_bs);
3333
3334 if (ro) {
3335 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
3336 if (local_err) {
3337 error_propagate(errp, local_err);
3338 goto out;
3339 }
3340 }
3341
3342 ret = bdrv_change_backing_file(image_bs, backing_file,
3343 image_bs->drv ? image_bs->drv->format_name : "");
3344
3345 if (ret < 0) {
3346 error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
3347 backing_file);
3348 /* don't exit here, so we can try to restore open flags if
3349 * appropriate */
3350 }
3351
3352 if (ro) {
3353 bdrv_reopen(image_bs, open_flags, &local_err);
3354 if (local_err) {
3355 error_propagate(errp, local_err); /* will preserve prior errp */
3356 }
3357 }
3358
3359out:
3360 aio_context_release(aio_context);
3361}
3362
3363void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
3364{
3365 QmpOutputVisitor *ov = qmp_output_visitor_new();
3366 BlockDriverState *bs;
3367 BlockBackend *blk = NULL;
3368 QObject *obj;
3369 QDict *qdict;
3370 Error *local_err = NULL;
3371
3372 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3373 * cache.direct=false instead of silently switching to aio=threads, except
3374 * when called from drive_new().
3375 *
3376 * For now, simply forbidding the combination for all drivers will do. */
3377 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
3378 bool direct = options->has_cache &&
3379 options->cache->has_direct &&
3380 options->cache->direct;
3381 if (!direct) {
3382 error_setg(errp, "aio=native requires cache.direct=true");
3383 goto fail;
3384 }
3385 }
3386
3387 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
3388 &options, NULL, &local_err);
3389 if (local_err) {
3390 error_propagate(errp, local_err);
3391 goto fail;
3392 }
3393
3394 obj = qmp_output_get_qobject(ov);
3395 qdict = qobject_to_qdict(obj);
3396
3397 qdict_flatten(qdict);
3398
3399 if (options->has_id) {
3400 blk = blockdev_init(NULL, qdict, &local_err);
3401 if (local_err) {
3402 error_propagate(errp, local_err);
3403 goto fail;
3404 }
3405
3406 bs = blk_bs(blk);
3407 } else {
3408 if (!qdict_get_try_str(qdict, "node-name")) {
3409 error_setg(errp, "'id' and/or 'node-name' need to be specified for "
3410 "the root node");
3411 goto fail;
3412 }
3413
3414 bs = bds_tree_init(qdict, errp);
3415 if (!bs) {
3416 goto fail;
3417 }
3418 }
3419
3420 if (bs && bdrv_key_required(bs)) {
3421 if (blk) {
3422 blk_unref(blk);
3423 } else {
3424 bdrv_unref(bs);
3425 }
3426 error_setg(errp, "blockdev-add doesn't support encrypted devices");
3427 goto fail;
3428 }
3429
3430fail:
3431 qmp_output_visitor_cleanup(ov);
3432}
3433
3434BlockJobInfoList *qmp_query_block_jobs(Error **errp)
3435{
3436 BlockJobInfoList *head = NULL, **p_next = &head;
3437 BlockDriverState *bs;
3438
3439 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3440 AioContext *aio_context = bdrv_get_aio_context(bs);
3441
3442 aio_context_acquire(aio_context);
3443
3444 if (bs->job) {
3445 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
3446 elem->value = block_job_query(bs->job);
3447 *p_next = elem;
3448 p_next = &elem->next;
3449 }
3450
3451 aio_context_release(aio_context);
3452 }
3453
3454 return head;
3455}
3456
3457QemuOptsList qemu_common_drive_opts = {
3458 .name = "drive",
3459 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3460 .desc = {
3461 {
3462 .name = "snapshot",
3463 .type = QEMU_OPT_BOOL,
3464 .help = "enable/disable snapshot mode",
3465 },{
3466 .name = "discard",
3467 .type = QEMU_OPT_STRING,
3468 .help = "discard operation (ignore/off, unmap/on)",
3469 },{
3470 .name = BDRV_OPT_CACHE_WB,
3471 .type = QEMU_OPT_BOOL,
3472 .help = "enables writeback mode for any caches",
3473 },{
3474 .name = BDRV_OPT_CACHE_DIRECT,
3475 .type = QEMU_OPT_BOOL,
3476 .help = "enables use of O_DIRECT (bypass the host page cache)",
3477 },{
3478 .name = BDRV_OPT_CACHE_NO_FLUSH,
3479 .type = QEMU_OPT_BOOL,
3480 .help = "ignore any flush requests for the device",
3481 },{
3482 .name = "aio",
3483 .type = QEMU_OPT_STRING,
3484 .help = "host AIO implementation (threads, native)",
3485 },{
3486 .name = "format",
3487 .type = QEMU_OPT_STRING,
3488 .help = "disk format (raw, qcow2, ...)",
3489 },{
3490 .name = "rerror",
3491 .type = QEMU_OPT_STRING,
3492 .help = "read error action",
3493 },{
3494 .name = "werror",
3495 .type = QEMU_OPT_STRING,
3496 .help = "write error action",
3497 },{
3498 .name = "read-only",
3499 .type = QEMU_OPT_BOOL,
3500 .help = "open drive file as read-only",
3501 },{
3502 .name = "throttling.iops-total",
3503 .type = QEMU_OPT_NUMBER,
3504 .help = "limit total I/O operations per second",
3505 },{
3506 .name = "throttling.iops-read",
3507 .type = QEMU_OPT_NUMBER,
3508 .help = "limit read operations per second",
3509 },{
3510 .name = "throttling.iops-write",
3511 .type = QEMU_OPT_NUMBER,
3512 .help = "limit write operations per second",
3513 },{
3514 .name = "throttling.bps-total",
3515 .type = QEMU_OPT_NUMBER,
3516 .help = "limit total bytes per second",
3517 },{
3518 .name = "throttling.bps-read",
3519 .type = QEMU_OPT_NUMBER,
3520 .help = "limit read bytes per second",
3521 },{
3522 .name = "throttling.bps-write",
3523 .type = QEMU_OPT_NUMBER,
3524 .help = "limit write bytes per second",
3525 },{
3526 .name = "throttling.iops-total-max",
3527 .type = QEMU_OPT_NUMBER,
3528 .help = "I/O operations burst",
3529 },{
3530 .name = "throttling.iops-read-max",
3531 .type = QEMU_OPT_NUMBER,
3532 .help = "I/O operations read burst",
3533 },{
3534 .name = "throttling.iops-write-max",
3535 .type = QEMU_OPT_NUMBER,
3536 .help = "I/O operations write burst",
3537 },{
3538 .name = "throttling.bps-total-max",
3539 .type = QEMU_OPT_NUMBER,
3540 .help = "total bytes burst",
3541 },{
3542 .name = "throttling.bps-read-max",
3543 .type = QEMU_OPT_NUMBER,
3544 .help = "total bytes read burst",
3545 },{
3546 .name = "throttling.bps-write-max",
3547 .type = QEMU_OPT_NUMBER,
3548 .help = "total bytes write burst",
3549 },{
3550 .name = "throttling.iops-size",
3551 .type = QEMU_OPT_NUMBER,
3552 .help = "when limiting by iops max size of an I/O in bytes",
3553 },{
3554 .name = "throttling.group",
3555 .type = QEMU_OPT_STRING,
3556 .help = "name of the block throttling group",
3557 },{
3558 .name = "copy-on-read",
3559 .type = QEMU_OPT_BOOL,
3560 .help = "copy read data from backing file into image file",
3561 },{
3562 .name = "detect-zeroes",
3563 .type = QEMU_OPT_STRING,
3564 .help = "try to optimize zero writes (off, on, unmap)",
3565 },
3566 { /* end of list */ }
3567 },
3568};
3569
3570static QemuOptsList qemu_root_bds_opts = {
3571 .name = "root-bds",
3572 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
3573 .desc = {
3574 {
3575 .name = "discard",
3576 .type = QEMU_OPT_STRING,
3577 .help = "discard operation (ignore/off, unmap/on)",
3578 },{
3579 .name = "cache.writeback",
3580 .type = QEMU_OPT_BOOL,
3581 .help = "enables writeback mode for any caches",
3582 },{
3583 .name = "cache.direct",
3584 .type = QEMU_OPT_BOOL,
3585 .help = "enables use of O_DIRECT (bypass the host page cache)",
3586 },{
3587 .name = "cache.no-flush",
3588 .type = QEMU_OPT_BOOL,
3589 .help = "ignore any flush requests for the device",
3590 },{
3591 .name = "aio",
3592 .type = QEMU_OPT_STRING,
3593 .help = "host AIO implementation (threads, native)",
3594 },{
3595 .name = "read-only",
3596 .type = QEMU_OPT_BOOL,
3597 .help = "open drive file as read-only",
3598 },{
3599 .name = "copy-on-read",
3600 .type = QEMU_OPT_BOOL,
3601 .help = "copy read data from backing file into image file",
3602 },{
3603 .name = "detect-zeroes",
3604 .type = QEMU_OPT_STRING,
3605 .help = "try to optimize zero writes (off, on, unmap)",
3606 },
3607 { /* end of list */ }
3608 },
3609};
3610
3611QemuOptsList qemu_drive_opts = {
3612 .name = "drive",
3613 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
3614 .desc = {
3615 /*
3616 * no elements => accept any params
3617 * validation will happen later
3618 */
3619 { /* end of list */ }
3620 },
3621};