]> git.proxmox.com Git - qemu.git/blob - blockdev.c
blockdev: Separate ID generation from DriveInfo creation
[qemu.git] / blockdev.c
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/blockdev.h"
34 #include "hw/block/block.h"
35 #include "block/blockjob.h"
36 #include "monitor/monitor.h"
37 #include "qapi/qmp/qerror.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qapi/qmp/types.h"
41 #include "qapi-visit.h"
42 #include "qapi/qmp-output-visitor.h"
43 #include "sysemu/sysemu.h"
44 #include "block/block_int.h"
45 #include "qmp-commands.h"
46 #include "trace.h"
47 #include "sysemu/arch_init.h"
48
49 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
50 extern QemuOptsList qemu_common_drive_opts;
51
52 static const char *const if_name[IF_COUNT] = {
53 [IF_NONE] = "none",
54 [IF_IDE] = "ide",
55 [IF_SCSI] = "scsi",
56 [IF_FLOPPY] = "floppy",
57 [IF_PFLASH] = "pflash",
58 [IF_MTD] = "mtd",
59 [IF_SD] = "sd",
60 [IF_VIRTIO] = "virtio",
61 [IF_XEN] = "xen",
62 };
63
64 static const int if_max_devs[IF_COUNT] = {
65 /*
66 * Do not change these numbers! They govern how drive option
67 * index maps to unit and bus. That mapping is ABI.
68 *
69 * All controllers used to imlement if=T drives need to support
70 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
71 * Otherwise, some index values map to "impossible" bus, unit
72 * values.
73 *
74 * For instance, if you change [IF_SCSI] to 255, -drive
75 * if=scsi,index=12 no longer means bus=1,unit=5, but
76 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
77 * the drive can't be set up. Regression.
78 */
79 [IF_IDE] = 2,
80 [IF_SCSI] = 7,
81 };
82
83 /*
84 * We automatically delete the drive when a device using it gets
85 * unplugged. Questionable feature, but we can't just drop it.
86 * Device models call blockdev_mark_auto_del() to schedule the
87 * automatic deletion, and generic qdev code calls blockdev_auto_del()
88 * when deletion is actually safe.
89 */
90 void blockdev_mark_auto_del(BlockDriverState *bs)
91 {
92 DriveInfo *dinfo = drive_get_by_blockdev(bs);
93
94 if (dinfo && !dinfo->enable_auto_del) {
95 return;
96 }
97
98 if (bs->job) {
99 block_job_cancel(bs->job);
100 }
101 if (dinfo) {
102 dinfo->auto_del = 1;
103 }
104 }
105
106 void blockdev_auto_del(BlockDriverState *bs)
107 {
108 DriveInfo *dinfo = drive_get_by_blockdev(bs);
109
110 if (dinfo && dinfo->auto_del) {
111 drive_put_ref(dinfo);
112 }
113 }
114
115 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
116 {
117 int max_devs = if_max_devs[type];
118 return max_devs ? index / max_devs : 0;
119 }
120
121 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
122 {
123 int max_devs = if_max_devs[type];
124 return max_devs ? index % max_devs : index;
125 }
126
127 QemuOpts *drive_def(const char *optstr)
128 {
129 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
130 }
131
132 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
133 const char *optstr)
134 {
135 QemuOpts *opts;
136 char buf[32];
137
138 opts = drive_def(optstr);
139 if (!opts) {
140 return NULL;
141 }
142 if (type != IF_DEFAULT) {
143 qemu_opt_set(opts, "if", if_name[type]);
144 }
145 if (index >= 0) {
146 snprintf(buf, sizeof(buf), "%d", index);
147 qemu_opt_set(opts, "index", buf);
148 }
149 if (file)
150 qemu_opt_set(opts, "file", file);
151 return opts;
152 }
153
154 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
155 {
156 DriveInfo *dinfo;
157
158 /* seek interface, bus and unit */
159
160 QTAILQ_FOREACH(dinfo, &drives, next) {
161 if (dinfo->type == type &&
162 dinfo->bus == bus &&
163 dinfo->unit == unit)
164 return dinfo;
165 }
166
167 return NULL;
168 }
169
170 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
171 {
172 return drive_get(type,
173 drive_index_to_bus_id(type, index),
174 drive_index_to_unit_id(type, index));
175 }
176
177 int drive_get_max_bus(BlockInterfaceType type)
178 {
179 int max_bus;
180 DriveInfo *dinfo;
181
182 max_bus = -1;
183 QTAILQ_FOREACH(dinfo, &drives, next) {
184 if(dinfo->type == type &&
185 dinfo->bus > max_bus)
186 max_bus = dinfo->bus;
187 }
188 return max_bus;
189 }
190
191 /* Get a block device. This should only be used for single-drive devices
192 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
193 appropriate bus. */
194 DriveInfo *drive_get_next(BlockInterfaceType type)
195 {
196 static int next_block_unit[IF_COUNT];
197
198 return drive_get(type, 0, next_block_unit[type]++);
199 }
200
201 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
202 {
203 DriveInfo *dinfo;
204
205 QTAILQ_FOREACH(dinfo, &drives, next) {
206 if (dinfo->bdrv == bs) {
207 return dinfo;
208 }
209 }
210 return NULL;
211 }
212
213 static void bdrv_format_print(void *opaque, const char *name)
214 {
215 error_printf(" %s", name);
216 }
217
218 static void drive_uninit(DriveInfo *dinfo)
219 {
220 qemu_opts_del(dinfo->opts);
221 bdrv_unref(dinfo->bdrv);
222 g_free(dinfo->id);
223 QTAILQ_REMOVE(&drives, dinfo, next);
224 g_free(dinfo->serial);
225 g_free(dinfo);
226 }
227
228 void drive_put_ref(DriveInfo *dinfo)
229 {
230 assert(dinfo->refcount);
231 if (--dinfo->refcount == 0) {
232 drive_uninit(dinfo);
233 }
234 }
235
236 void drive_get_ref(DriveInfo *dinfo)
237 {
238 dinfo->refcount++;
239 }
240
241 typedef struct {
242 QEMUBH *bh;
243 BlockDriverState *bs;
244 } BDRVPutRefBH;
245
246 static void bdrv_put_ref_bh(void *opaque)
247 {
248 BDRVPutRefBH *s = opaque;
249
250 bdrv_unref(s->bs);
251 qemu_bh_delete(s->bh);
252 g_free(s);
253 }
254
255 /*
256 * Release a BDS reference in a BH
257 *
258 * It is not safe to use bdrv_unref() from a callback function when the callers
259 * still need the BlockDriverState. In such cases we schedule a BH to release
260 * the reference.
261 */
262 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
263 {
264 BDRVPutRefBH *s;
265
266 s = g_new(BDRVPutRefBH, 1);
267 s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
268 s->bs = bs;
269 qemu_bh_schedule(s->bh);
270 }
271
272 static int parse_block_error_action(const char *buf, bool is_read)
273 {
274 if (!strcmp(buf, "ignore")) {
275 return BLOCKDEV_ON_ERROR_IGNORE;
276 } else if (!is_read && !strcmp(buf, "enospc")) {
277 return BLOCKDEV_ON_ERROR_ENOSPC;
278 } else if (!strcmp(buf, "stop")) {
279 return BLOCKDEV_ON_ERROR_STOP;
280 } else if (!strcmp(buf, "report")) {
281 return BLOCKDEV_ON_ERROR_REPORT;
282 } else {
283 error_report("'%s' invalid %s error action",
284 buf, is_read ? "read" : "write");
285 return -1;
286 }
287 }
288
289 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
290 {
291 if (throttle_conflicting(cfg)) {
292 error_setg(errp, "bps/iops/max total values and read/write values"
293 " cannot be used at the same time");
294 return false;
295 }
296
297 if (!throttle_is_valid(cfg)) {
298 error_setg(errp, "bps/iops/maxs values must be 0 or greater");
299 return false;
300 }
301
302 return true;
303 }
304
305 static DriveInfo *blockdev_init(QemuOpts *all_opts,
306 BlockInterfaceType block_default_type)
307 {
308 const char *buf;
309 const char *file = NULL;
310 const char *serial;
311 const char *mediastr = "";
312 BlockInterfaceType type;
313 enum { MEDIA_DISK, MEDIA_CDROM } media;
314 int bus_id, unit_id;
315 int cyls, heads, secs, translation;
316 int max_devs;
317 int index;
318 int ro = 0;
319 int bdrv_flags = 0;
320 int on_read_error, on_write_error;
321 const char *devaddr;
322 DriveInfo *dinfo;
323 ThrottleConfig cfg;
324 int snapshot = 0;
325 bool copy_on_read;
326 int ret;
327 Error *error = NULL;
328 QemuOpts *opts;
329 QDict *bs_opts;
330 const char *id;
331 bool has_driver_specific_opts;
332 BlockDriver *drv = NULL;
333
334 translation = BIOS_ATA_TRANSLATION_AUTO;
335 media = MEDIA_DISK;
336
337 /* Check common options by copying from all_opts to opts, all other options
338 * are stored in bs_opts. */
339 id = qemu_opts_id(all_opts);
340 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
341 if (error_is_set(&error)) {
342 qerror_report_err(error);
343 error_free(error);
344 return NULL;
345 }
346
347 bs_opts = qdict_new();
348 qemu_opts_to_qdict(all_opts, bs_opts);
349 qemu_opts_absorb_qdict(opts, bs_opts, &error);
350 if (error_is_set(&error)) {
351 qerror_report_err(error);
352 error_free(error);
353 return NULL;
354 }
355
356 if (id) {
357 qdict_del(bs_opts, "id");
358 }
359
360 has_driver_specific_opts = !!qdict_size(bs_opts);
361
362 /* extract parameters */
363 bus_id = qemu_opt_get_number(opts, "bus", 0);
364 unit_id = qemu_opt_get_number(opts, "unit", -1);
365 index = qemu_opt_get_number(opts, "index", -1);
366
367 cyls = qemu_opt_get_number(opts, "cyls", 0);
368 heads = qemu_opt_get_number(opts, "heads", 0);
369 secs = qemu_opt_get_number(opts, "secs", 0);
370
371 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
372 ro = qemu_opt_get_bool(opts, "read-only", 0);
373 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
374
375 file = qemu_opt_get(opts, "file");
376 serial = qemu_opt_get(opts, "serial");
377
378 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
379 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
380 ;
381 if (type == IF_COUNT) {
382 error_report("unsupported bus type '%s'", buf);
383 return NULL;
384 }
385 } else {
386 type = block_default_type;
387 }
388
389 max_devs = if_max_devs[type];
390
391 if (cyls || heads || secs) {
392 if (cyls < 1) {
393 error_report("invalid physical cyls number");
394 return NULL;
395 }
396 if (heads < 1) {
397 error_report("invalid physical heads number");
398 return NULL;
399 }
400 if (secs < 1) {
401 error_report("invalid physical secs number");
402 return NULL;
403 }
404 }
405
406 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
407 if (!cyls) {
408 error_report("'%s' trans must be used with cyls, heads and secs",
409 buf);
410 return NULL;
411 }
412 if (!strcmp(buf, "none"))
413 translation = BIOS_ATA_TRANSLATION_NONE;
414 else if (!strcmp(buf, "lba"))
415 translation = BIOS_ATA_TRANSLATION_LBA;
416 else if (!strcmp(buf, "auto"))
417 translation = BIOS_ATA_TRANSLATION_AUTO;
418 else {
419 error_report("'%s' invalid translation type", buf);
420 return NULL;
421 }
422 }
423
424 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
425 if (!strcmp(buf, "disk")) {
426 media = MEDIA_DISK;
427 } else if (!strcmp(buf, "cdrom")) {
428 if (cyls || secs || heads) {
429 error_report("CHS can't be set with media=%s", buf);
430 return NULL;
431 }
432 media = MEDIA_CDROM;
433 } else {
434 error_report("'%s' invalid media", buf);
435 return NULL;
436 }
437 }
438
439 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
440 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
441 error_report("invalid discard option");
442 return NULL;
443 }
444 }
445
446 if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
447 bdrv_flags |= BDRV_O_CACHE_WB;
448 }
449 if (qemu_opt_get_bool(opts, "cache.direct", false)) {
450 bdrv_flags |= BDRV_O_NOCACHE;
451 }
452 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
453 bdrv_flags |= BDRV_O_NO_FLUSH;
454 }
455
456 #ifdef CONFIG_LINUX_AIO
457 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
458 if (!strcmp(buf, "native")) {
459 bdrv_flags |= BDRV_O_NATIVE_AIO;
460 } else if (!strcmp(buf, "threads")) {
461 /* this is the default */
462 } else {
463 error_report("invalid aio option");
464 return NULL;
465 }
466 }
467 #endif
468
469 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
470 if (is_help_option(buf)) {
471 error_printf("Supported formats:");
472 bdrv_iterate_format(bdrv_format_print, NULL);
473 error_printf("\n");
474 return NULL;
475 }
476
477 drv = bdrv_find_format(buf);
478 if (!drv) {
479 error_report("'%s' invalid format", buf);
480 return NULL;
481 }
482 }
483
484 /* disk I/O throttling */
485 memset(&cfg, 0, sizeof(cfg));
486 cfg.buckets[THROTTLE_BPS_TOTAL].avg =
487 qemu_opt_get_number(opts, "throttling.bps-total", 0);
488 cfg.buckets[THROTTLE_BPS_READ].avg =
489 qemu_opt_get_number(opts, "throttling.bps-read", 0);
490 cfg.buckets[THROTTLE_BPS_WRITE].avg =
491 qemu_opt_get_number(opts, "throttling.bps-write", 0);
492 cfg.buckets[THROTTLE_OPS_TOTAL].avg =
493 qemu_opt_get_number(opts, "throttling.iops-total", 0);
494 cfg.buckets[THROTTLE_OPS_READ].avg =
495 qemu_opt_get_number(opts, "throttling.iops-read", 0);
496 cfg.buckets[THROTTLE_OPS_WRITE].avg =
497 qemu_opt_get_number(opts, "throttling.iops-write", 0);
498
499 cfg.buckets[THROTTLE_BPS_TOTAL].max =
500 qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
501 cfg.buckets[THROTTLE_BPS_READ].max =
502 qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
503 cfg.buckets[THROTTLE_BPS_WRITE].max =
504 qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
505 cfg.buckets[THROTTLE_OPS_TOTAL].max =
506 qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
507 cfg.buckets[THROTTLE_OPS_READ].max =
508 qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
509 cfg.buckets[THROTTLE_OPS_WRITE].max =
510 qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
511
512 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
513
514 if (!check_throttle_config(&cfg, &error)) {
515 error_report("%s", error_get_pretty(error));
516 error_free(error);
517 return NULL;
518 }
519
520 if (qemu_opt_get(opts, "boot") != NULL) {
521 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
522 "ignored. Future versions will reject this parameter. Please "
523 "update your scripts.\n");
524 }
525
526 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
527 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
528 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
529 error_report("werror is not supported by this bus type");
530 return NULL;
531 }
532
533 on_write_error = parse_block_error_action(buf, 0);
534 if (on_write_error < 0) {
535 return NULL;
536 }
537 }
538
539 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
540 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
541 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
542 error_report("rerror is not supported by this bus type");
543 return NULL;
544 }
545
546 on_read_error = parse_block_error_action(buf, 1);
547 if (on_read_error < 0) {
548 return NULL;
549 }
550 }
551
552 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
553 if (type != IF_VIRTIO) {
554 error_report("addr is not supported by this bus type");
555 return NULL;
556 }
557 }
558
559 /* compute bus and unit according index */
560
561 if (index != -1) {
562 if (bus_id != 0 || unit_id != -1) {
563 error_report("index cannot be used with bus and unit");
564 return NULL;
565 }
566 bus_id = drive_index_to_bus_id(type, index);
567 unit_id = drive_index_to_unit_id(type, index);
568 }
569
570 /* if user doesn't specify a unit_id,
571 * try to find the first free
572 */
573
574 if (unit_id == -1) {
575 unit_id = 0;
576 while (drive_get(type, bus_id, unit_id) != NULL) {
577 unit_id++;
578 if (max_devs && unit_id >= max_devs) {
579 unit_id -= max_devs;
580 bus_id++;
581 }
582 }
583 }
584
585 /* check unit id */
586
587 if (max_devs && unit_id >= max_devs) {
588 error_report("unit %d too big (max is %d)",
589 unit_id, max_devs - 1);
590 return NULL;
591 }
592
593 /*
594 * catch multiple definitions
595 */
596
597 if (drive_get(type, bus_id, unit_id) != NULL) {
598 error_report("drive with bus=%d, unit=%d (index=%d) exists",
599 bus_id, unit_id, index);
600 return NULL;
601 }
602
603 /* no id supplied -> create one */
604 if (qemu_opts_id(opts) == NULL) {
605 char *new_id;
606 if (type == IF_IDE || type == IF_SCSI) {
607 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
608 }
609 if (max_devs) {
610 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
611 mediastr, unit_id);
612 } else {
613 new_id = g_strdup_printf("%s%s%i", if_name[type],
614 mediastr, unit_id);
615 }
616 qemu_opts_set_id(opts, new_id);
617 }
618
619 /* init */
620 dinfo = g_malloc0(sizeof(*dinfo));
621 dinfo->id = g_strdup(qemu_opts_id(opts));
622 dinfo->bdrv = bdrv_new(dinfo->id);
623 dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
624 dinfo->bdrv->read_only = ro;
625 dinfo->devaddr = devaddr;
626 dinfo->type = type;
627 dinfo->bus = bus_id;
628 dinfo->unit = unit_id;
629 dinfo->cyls = cyls;
630 dinfo->heads = heads;
631 dinfo->secs = secs;
632 dinfo->trans = translation;
633 dinfo->opts = all_opts;
634 dinfo->refcount = 1;
635 if (serial != NULL) {
636 dinfo->serial = g_strdup(serial);
637 }
638 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
639
640 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
641
642 /* disk I/O throttling */
643 if (throttle_enabled(&cfg)) {
644 bdrv_io_limits_enable(dinfo->bdrv);
645 bdrv_set_io_limits(dinfo->bdrv, &cfg);
646 }
647
648 switch(type) {
649 case IF_IDE:
650 case IF_SCSI:
651 case IF_XEN:
652 case IF_NONE:
653 dinfo->media_cd = media == MEDIA_CDROM;
654 break;
655 case IF_SD:
656 case IF_FLOPPY:
657 case IF_PFLASH:
658 case IF_MTD:
659 break;
660 case IF_VIRTIO:
661 {
662 /* add virtio block device */
663 QemuOpts *devopts;
664 devopts = qemu_opts_create_nofail(qemu_find_opts("device"));
665 if (arch_type == QEMU_ARCH_S390X) {
666 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
667 } else {
668 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
669 }
670 qemu_opt_set(devopts, "drive", dinfo->id);
671 if (devaddr)
672 qemu_opt_set(devopts, "addr", devaddr);
673 break;
674 }
675 default:
676 abort();
677 }
678 if (!file || !*file) {
679 if (has_driver_specific_opts) {
680 file = NULL;
681 } else {
682 return dinfo;
683 }
684 }
685 if (snapshot) {
686 /* always use cache=unsafe with snapshot */
687 bdrv_flags &= ~BDRV_O_CACHE_MASK;
688 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
689 }
690
691 if (copy_on_read) {
692 bdrv_flags |= BDRV_O_COPY_ON_READ;
693 }
694
695 if (runstate_check(RUN_STATE_INMIGRATE)) {
696 bdrv_flags |= BDRV_O_INCOMING;
697 }
698
699 if (media == MEDIA_CDROM) {
700 /* CDROM is fine for any interface, don't check. */
701 ro = 1;
702 } else if (ro == 1) {
703 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
704 type != IF_NONE && type != IF_PFLASH) {
705 error_report("read-only not supported by this bus type");
706 goto err;
707 }
708 }
709
710 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
711
712 if (ro && copy_on_read) {
713 error_report("warning: disabling copy_on_read on read-only drive");
714 }
715
716 QINCREF(bs_opts);
717 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error);
718
719 if (ret < 0) {
720 error_report("could not open disk image %s: %s",
721 file ?: dinfo->id, error_get_pretty(error));
722 goto err;
723 }
724
725 if (bdrv_key_required(dinfo->bdrv))
726 autostart = 0;
727
728 QDECREF(bs_opts);
729 qemu_opts_del(opts);
730
731 return dinfo;
732
733 err:
734 qemu_opts_del(opts);
735 QDECREF(bs_opts);
736 bdrv_unref(dinfo->bdrv);
737 g_free(dinfo->id);
738 QTAILQ_REMOVE(&drives, dinfo, next);
739 g_free(dinfo);
740 return NULL;
741 }
742
743 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to)
744 {
745 const char *value;
746
747 value = qemu_opt_get(opts, from);
748 if (value) {
749 qemu_opt_set(opts, to, value);
750 qemu_opt_unset(opts, from);
751 }
752 }
753
754 DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
755 {
756 const char *value;
757 DriveInfo *dinfo;
758
759 /* Change legacy command line options into QMP ones */
760 qemu_opt_rename(all_opts, "iops", "throttling.iops-total");
761 qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read");
762 qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write");
763
764 qemu_opt_rename(all_opts, "bps", "throttling.bps-total");
765 qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read");
766 qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write");
767
768 qemu_opt_rename(all_opts, "iops_max", "throttling.iops-total-max");
769 qemu_opt_rename(all_opts, "iops_rd_max", "throttling.iops-read-max");
770 qemu_opt_rename(all_opts, "iops_wr_max", "throttling.iops-write-max");
771
772 qemu_opt_rename(all_opts, "bps_max", "throttling.bps-total-max");
773 qemu_opt_rename(all_opts, "bps_rd_max", "throttling.bps-read-max");
774 qemu_opt_rename(all_opts, "bps_wr_max", "throttling.bps-write-max");
775
776 qemu_opt_rename(all_opts,
777 "iops_size", "throttling.iops-size");
778
779 qemu_opt_rename(all_opts, "readonly", "read-only");
780
781 value = qemu_opt_get(all_opts, "cache");
782 if (value) {
783 int flags = 0;
784
785 if (bdrv_parse_cache_flags(value, &flags) != 0) {
786 error_report("invalid cache option");
787 return NULL;
788 }
789
790 /* Specific options take precedence */
791 if (!qemu_opt_get(all_opts, "cache.writeback")) {
792 qemu_opt_set_bool(all_opts, "cache.writeback",
793 !!(flags & BDRV_O_CACHE_WB));
794 }
795 if (!qemu_opt_get(all_opts, "cache.direct")) {
796 qemu_opt_set_bool(all_opts, "cache.direct",
797 !!(flags & BDRV_O_NOCACHE));
798 }
799 if (!qemu_opt_get(all_opts, "cache.no-flush")) {
800 qemu_opt_set_bool(all_opts, "cache.no-flush",
801 !!(flags & BDRV_O_NO_FLUSH));
802 }
803 qemu_opt_unset(all_opts, "cache");
804 }
805
806 /* Actual block device init: Functionality shared with blockdev-add */
807 dinfo = blockdev_init(all_opts, block_default_type);
808 if (dinfo == NULL) {
809 goto fail;
810 }
811
812 /* Set legacy DriveInfo fields */
813 dinfo->enable_auto_del = true;
814
815 fail:
816 return dinfo;
817 }
818
819 void do_commit(Monitor *mon, const QDict *qdict)
820 {
821 const char *device = qdict_get_str(qdict, "device");
822 BlockDriverState *bs;
823 int ret;
824
825 if (!strcmp(device, "all")) {
826 ret = bdrv_commit_all();
827 } else {
828 bs = bdrv_find(device);
829 if (!bs) {
830 monitor_printf(mon, "Device '%s' not found\n", device);
831 return;
832 }
833 ret = bdrv_commit(bs);
834 }
835 if (ret < 0) {
836 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
837 strerror(-ret));
838 }
839 }
840
841 static void blockdev_do_action(int kind, void *data, Error **errp)
842 {
843 TransactionAction action;
844 TransactionActionList list;
845
846 action.kind = kind;
847 action.data = data;
848 list.value = &action;
849 list.next = NULL;
850 qmp_transaction(&list, errp);
851 }
852
853 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
854 bool has_format, const char *format,
855 bool has_mode, enum NewImageMode mode,
856 Error **errp)
857 {
858 BlockdevSnapshot snapshot = {
859 .device = (char *) device,
860 .snapshot_file = (char *) snapshot_file,
861 .has_format = has_format,
862 .format = (char *) format,
863 .has_mode = has_mode,
864 .mode = mode,
865 };
866 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
867 &snapshot, errp);
868 }
869
870 void qmp_blockdev_snapshot_internal_sync(const char *device,
871 const char *name,
872 Error **errp)
873 {
874 BlockdevSnapshotInternal snapshot = {
875 .device = (char *) device,
876 .name = (char *) name
877 };
878
879 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
880 &snapshot, errp);
881 }
882
883 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
884 bool has_id,
885 const char *id,
886 bool has_name,
887 const char *name,
888 Error **errp)
889 {
890 BlockDriverState *bs = bdrv_find(device);
891 QEMUSnapshotInfo sn;
892 Error *local_err = NULL;
893 SnapshotInfo *info = NULL;
894 int ret;
895
896 if (!bs) {
897 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
898 return NULL;
899 }
900
901 if (!has_id) {
902 id = NULL;
903 }
904
905 if (!has_name) {
906 name = NULL;
907 }
908
909 if (!id && !name) {
910 error_setg(errp, "Name or id must be provided");
911 return NULL;
912 }
913
914 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
915 if (error_is_set(&local_err)) {
916 error_propagate(errp, local_err);
917 return NULL;
918 }
919 if (!ret) {
920 error_setg(errp,
921 "Snapshot with id '%s' and name '%s' does not exist on "
922 "device '%s'",
923 STR_OR_NULL(id), STR_OR_NULL(name), device);
924 return NULL;
925 }
926
927 bdrv_snapshot_delete(bs, id, name, &local_err);
928 if (error_is_set(&local_err)) {
929 error_propagate(errp, local_err);
930 return NULL;
931 }
932
933 info = g_malloc0(sizeof(SnapshotInfo));
934 info->id = g_strdup(sn.id_str);
935 info->name = g_strdup(sn.name);
936 info->date_nsec = sn.date_nsec;
937 info->date_sec = sn.date_sec;
938 info->vm_state_size = sn.vm_state_size;
939 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
940 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
941
942 return info;
943 }
944
945 /* New and old BlockDriverState structs for group snapshots */
946
947 typedef struct BlkTransactionState BlkTransactionState;
948
949 /* Only prepare() may fail. In a single transaction, only one of commit() or
950 abort() will be called, clean() will always be called if it present. */
951 typedef struct BdrvActionOps {
952 /* Size of state struct, in bytes. */
953 size_t instance_size;
954 /* Prepare the work, must NOT be NULL. */
955 void (*prepare)(BlkTransactionState *common, Error **errp);
956 /* Commit the changes, can be NULL. */
957 void (*commit)(BlkTransactionState *common);
958 /* Abort the changes on fail, can be NULL. */
959 void (*abort)(BlkTransactionState *common);
960 /* Clean up resource in the end, can be NULL. */
961 void (*clean)(BlkTransactionState *common);
962 } BdrvActionOps;
963
964 /*
965 * This structure must be arranged as first member in child type, assuming
966 * that compiler will also arrange it to the same address with parent instance.
967 * Later it will be used in free().
968 */
969 struct BlkTransactionState {
970 TransactionAction *action;
971 const BdrvActionOps *ops;
972 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
973 };
974
975 /* internal snapshot private data */
976 typedef struct InternalSnapshotState {
977 BlkTransactionState common;
978 BlockDriverState *bs;
979 QEMUSnapshotInfo sn;
980 } InternalSnapshotState;
981
982 static void internal_snapshot_prepare(BlkTransactionState *common,
983 Error **errp)
984 {
985 const char *device;
986 const char *name;
987 BlockDriverState *bs;
988 QEMUSnapshotInfo old_sn, *sn;
989 bool ret;
990 qemu_timeval tv;
991 BlockdevSnapshotInternal *internal;
992 InternalSnapshotState *state;
993 int ret1;
994
995 g_assert(common->action->kind ==
996 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
997 internal = common->action->blockdev_snapshot_internal_sync;
998 state = DO_UPCAST(InternalSnapshotState, common, common);
999
1000 /* 1. parse input */
1001 device = internal->device;
1002 name = internal->name;
1003
1004 /* 2. check for validation */
1005 bs = bdrv_find(device);
1006 if (!bs) {
1007 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1008 return;
1009 }
1010
1011 if (!bdrv_is_inserted(bs)) {
1012 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1013 return;
1014 }
1015
1016 if (bdrv_is_read_only(bs)) {
1017 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1018 return;
1019 }
1020
1021 if (!bdrv_can_snapshot(bs)) {
1022 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1023 bs->drv->format_name, device, "internal snapshot");
1024 return;
1025 }
1026
1027 if (!strlen(name)) {
1028 error_setg(errp, "Name is empty");
1029 return;
1030 }
1031
1032 /* check whether a snapshot with name exist */
1033 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, errp);
1034 if (error_is_set(errp)) {
1035 return;
1036 } else if (ret) {
1037 error_setg(errp,
1038 "Snapshot with name '%s' already exists on device '%s'",
1039 name, device);
1040 return;
1041 }
1042
1043 /* 3. take the snapshot */
1044 sn = &state->sn;
1045 pstrcpy(sn->name, sizeof(sn->name), name);
1046 qemu_gettimeofday(&tv);
1047 sn->date_sec = tv.tv_sec;
1048 sn->date_nsec = tv.tv_usec * 1000;
1049 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1050
1051 ret1 = bdrv_snapshot_create(bs, sn);
1052 if (ret1 < 0) {
1053 error_setg_errno(errp, -ret1,
1054 "Failed to create snapshot '%s' on device '%s'",
1055 name, device);
1056 return;
1057 }
1058
1059 /* 4. succeed, mark a snapshot is created */
1060 state->bs = bs;
1061 }
1062
1063 static void internal_snapshot_abort(BlkTransactionState *common)
1064 {
1065 InternalSnapshotState *state =
1066 DO_UPCAST(InternalSnapshotState, common, common);
1067 BlockDriverState *bs = state->bs;
1068 QEMUSnapshotInfo *sn = &state->sn;
1069 Error *local_error = NULL;
1070
1071 if (!bs) {
1072 return;
1073 }
1074
1075 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
1076 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1077 "device '%s' in abort: %s",
1078 sn->id_str,
1079 sn->name,
1080 bdrv_get_device_name(bs),
1081 error_get_pretty(local_error));
1082 error_free(local_error);
1083 }
1084 }
1085
1086 /* external snapshot private data */
1087 typedef struct ExternalSnapshotState {
1088 BlkTransactionState common;
1089 BlockDriverState *old_bs;
1090 BlockDriverState *new_bs;
1091 } ExternalSnapshotState;
1092
1093 static void external_snapshot_prepare(BlkTransactionState *common,
1094 Error **errp)
1095 {
1096 BlockDriver *drv;
1097 int flags, ret;
1098 Error *local_err = NULL;
1099 const char *device;
1100 const char *new_image_file;
1101 const char *format = "qcow2";
1102 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1103 ExternalSnapshotState *state =
1104 DO_UPCAST(ExternalSnapshotState, common, common);
1105 TransactionAction *action = common->action;
1106
1107 /* get parameters */
1108 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
1109
1110 device = action->blockdev_snapshot_sync->device;
1111 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
1112 if (action->blockdev_snapshot_sync->has_format) {
1113 format = action->blockdev_snapshot_sync->format;
1114 }
1115 if (action->blockdev_snapshot_sync->has_mode) {
1116 mode = action->blockdev_snapshot_sync->mode;
1117 }
1118
1119 /* start processing */
1120 drv = bdrv_find_format(format);
1121 if (!drv) {
1122 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1123 return;
1124 }
1125
1126 state->old_bs = bdrv_find(device);
1127 if (!state->old_bs) {
1128 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1129 return;
1130 }
1131
1132 if (!bdrv_is_inserted(state->old_bs)) {
1133 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1134 return;
1135 }
1136
1137 if (bdrv_in_use(state->old_bs)) {
1138 error_set(errp, QERR_DEVICE_IN_USE, device);
1139 return;
1140 }
1141
1142 if (!bdrv_is_read_only(state->old_bs)) {
1143 if (bdrv_flush(state->old_bs)) {
1144 error_set(errp, QERR_IO_ERROR);
1145 return;
1146 }
1147 }
1148
1149 if (bdrv_check_ext_snapshot(state->old_bs) != EXT_SNAPSHOT_ALLOWED) {
1150 error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
1151 return;
1152 }
1153
1154 flags = state->old_bs->open_flags;
1155
1156 /* create new image w/backing file */
1157 if (mode != NEW_IMAGE_MODE_EXISTING) {
1158 bdrv_img_create(new_image_file, format,
1159 state->old_bs->filename,
1160 state->old_bs->drv->format_name,
1161 NULL, -1, flags, &local_err, false);
1162 if (error_is_set(&local_err)) {
1163 error_propagate(errp, local_err);
1164 return;
1165 }
1166 }
1167
1168 /* We will manually add the backing_hd field to the bs later */
1169 state->new_bs = bdrv_new("");
1170 /* TODO Inherit bs->options or only take explicit options with an
1171 * extended QMP command? */
1172 ret = bdrv_open(state->new_bs, new_image_file, NULL,
1173 flags | BDRV_O_NO_BACKING, drv, &local_err);
1174 if (ret != 0) {
1175 error_propagate(errp, local_err);
1176 }
1177 }
1178
1179 static void external_snapshot_commit(BlkTransactionState *common)
1180 {
1181 ExternalSnapshotState *state =
1182 DO_UPCAST(ExternalSnapshotState, common, common);
1183
1184 /* This removes our old bs and adds the new bs */
1185 bdrv_append(state->new_bs, state->old_bs);
1186 /* We don't need (or want) to use the transactional
1187 * bdrv_reopen_multiple() across all the entries at once, because we
1188 * don't want to abort all of them if one of them fails the reopen */
1189 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
1190 NULL);
1191 }
1192
1193 static void external_snapshot_abort(BlkTransactionState *common)
1194 {
1195 ExternalSnapshotState *state =
1196 DO_UPCAST(ExternalSnapshotState, common, common);
1197 if (state->new_bs) {
1198 bdrv_unref(state->new_bs);
1199 }
1200 }
1201
1202 typedef struct DriveBackupState {
1203 BlkTransactionState common;
1204 BlockDriverState *bs;
1205 BlockJob *job;
1206 } DriveBackupState;
1207
1208 static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
1209 {
1210 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1211 DriveBackup *backup;
1212 Error *local_err = NULL;
1213
1214 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
1215 backup = common->action->drive_backup;
1216
1217 qmp_drive_backup(backup->device, backup->target,
1218 backup->has_format, backup->format,
1219 backup->sync,
1220 backup->has_mode, backup->mode,
1221 backup->has_speed, backup->speed,
1222 backup->has_on_source_error, backup->on_source_error,
1223 backup->has_on_target_error, backup->on_target_error,
1224 &local_err);
1225 if (error_is_set(&local_err)) {
1226 error_propagate(errp, local_err);
1227 state->bs = NULL;
1228 state->job = NULL;
1229 return;
1230 }
1231
1232 state->bs = bdrv_find(backup->device);
1233 state->job = state->bs->job;
1234 }
1235
1236 static void drive_backup_abort(BlkTransactionState *common)
1237 {
1238 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
1239 BlockDriverState *bs = state->bs;
1240
1241 /* Only cancel if it's the job we started */
1242 if (bs && bs->job && bs->job == state->job) {
1243 block_job_cancel_sync(bs->job);
1244 }
1245 }
1246
1247 static void abort_prepare(BlkTransactionState *common, Error **errp)
1248 {
1249 error_setg(errp, "Transaction aborted using Abort action");
1250 }
1251
1252 static void abort_commit(BlkTransactionState *common)
1253 {
1254 g_assert_not_reached(); /* this action never succeeds */
1255 }
1256
1257 static const BdrvActionOps actions[] = {
1258 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
1259 .instance_size = sizeof(ExternalSnapshotState),
1260 .prepare = external_snapshot_prepare,
1261 .commit = external_snapshot_commit,
1262 .abort = external_snapshot_abort,
1263 },
1264 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1265 .instance_size = sizeof(DriveBackupState),
1266 .prepare = drive_backup_prepare,
1267 .abort = drive_backup_abort,
1268 },
1269 [TRANSACTION_ACTION_KIND_ABORT] = {
1270 .instance_size = sizeof(BlkTransactionState),
1271 .prepare = abort_prepare,
1272 .commit = abort_commit,
1273 },
1274 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
1275 .instance_size = sizeof(InternalSnapshotState),
1276 .prepare = internal_snapshot_prepare,
1277 .abort = internal_snapshot_abort,
1278 },
1279 };
1280
1281 /*
1282 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1283 * then we do not pivot any of the devices in the group, and abandon the
1284 * snapshots
1285 */
1286 void qmp_transaction(TransactionActionList *dev_list, Error **errp)
1287 {
1288 TransactionActionList *dev_entry = dev_list;
1289 BlkTransactionState *state, *next;
1290 Error *local_err = NULL;
1291
1292 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
1293 QSIMPLEQ_INIT(&snap_bdrv_states);
1294
1295 /* drain all i/o before any snapshots */
1296 bdrv_drain_all();
1297
1298 /* We don't do anything in this loop that commits us to the snapshot */
1299 while (NULL != dev_entry) {
1300 TransactionAction *dev_info = NULL;
1301 const BdrvActionOps *ops;
1302
1303 dev_info = dev_entry->value;
1304 dev_entry = dev_entry->next;
1305
1306 assert(dev_info->kind < ARRAY_SIZE(actions));
1307
1308 ops = &actions[dev_info->kind];
1309 assert(ops->instance_size > 0);
1310
1311 state = g_malloc0(ops->instance_size);
1312 state->ops = ops;
1313 state->action = dev_info;
1314 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
1315
1316 state->ops->prepare(state, &local_err);
1317 if (error_is_set(&local_err)) {
1318 error_propagate(errp, local_err);
1319 goto delete_and_fail;
1320 }
1321 }
1322
1323 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1324 if (state->ops->commit) {
1325 state->ops->commit(state);
1326 }
1327 }
1328
1329 /* success */
1330 goto exit;
1331
1332 delete_and_fail:
1333 /*
1334 * failure, and it is all-or-none; abandon each new bs, and keep using
1335 * the original bs for all images
1336 */
1337 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1338 if (state->ops->abort) {
1339 state->ops->abort(state);
1340 }
1341 }
1342 exit:
1343 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1344 if (state->ops->clean) {
1345 state->ops->clean(state);
1346 }
1347 g_free(state);
1348 }
1349 }
1350
1351
1352 static void eject_device(BlockDriverState *bs, int force, Error **errp)
1353 {
1354 if (bdrv_in_use(bs)) {
1355 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
1356 return;
1357 }
1358 if (!bdrv_dev_has_removable_media(bs)) {
1359 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
1360 return;
1361 }
1362
1363 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1364 bdrv_dev_eject_request(bs, force);
1365 if (!force) {
1366 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
1367 return;
1368 }
1369 }
1370
1371 bdrv_close(bs);
1372 }
1373
1374 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
1375 {
1376 BlockDriverState *bs;
1377
1378 bs = bdrv_find(device);
1379 if (!bs) {
1380 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1381 return;
1382 }
1383
1384 eject_device(bs, force, errp);
1385 }
1386
1387 void qmp_block_passwd(const char *device, const char *password, Error **errp)
1388 {
1389 BlockDriverState *bs;
1390 int err;
1391
1392 bs = bdrv_find(device);
1393 if (!bs) {
1394 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1395 return;
1396 }
1397
1398 err = bdrv_set_key(bs, password);
1399 if (err == -EINVAL) {
1400 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1401 return;
1402 } else if (err < 0) {
1403 error_set(errp, QERR_INVALID_PASSWORD);
1404 return;
1405 }
1406 }
1407
1408 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1409 int bdrv_flags, BlockDriver *drv,
1410 const char *password, Error **errp)
1411 {
1412 Error *local_err = NULL;
1413 int ret;
1414
1415 ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv, &local_err);
1416 if (ret < 0) {
1417 error_propagate(errp, local_err);
1418 return;
1419 }
1420
1421 if (bdrv_key_required(bs)) {
1422 if (password) {
1423 if (bdrv_set_key(bs, password) < 0) {
1424 error_set(errp, QERR_INVALID_PASSWORD);
1425 }
1426 } else {
1427 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1428 bdrv_get_encrypted_filename(bs));
1429 }
1430 } else if (password) {
1431 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1432 }
1433 }
1434
1435 void qmp_change_blockdev(const char *device, const char *filename,
1436 bool has_format, const char *format, Error **errp)
1437 {
1438 BlockDriverState *bs;
1439 BlockDriver *drv = NULL;
1440 int bdrv_flags;
1441 Error *err = NULL;
1442
1443 bs = bdrv_find(device);
1444 if (!bs) {
1445 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1446 return;
1447 }
1448
1449 if (format) {
1450 drv = bdrv_find_whitelisted_format(format, bs->read_only);
1451 if (!drv) {
1452 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1453 return;
1454 }
1455 }
1456
1457 eject_device(bs, 0, &err);
1458 if (error_is_set(&err)) {
1459 error_propagate(errp, err);
1460 return;
1461 }
1462
1463 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1464 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1465
1466 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1467 }
1468
1469 /* throttling disk I/O limits */
1470 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1471 int64_t bps_wr,
1472 int64_t iops,
1473 int64_t iops_rd,
1474 int64_t iops_wr,
1475 bool has_bps_max,
1476 int64_t bps_max,
1477 bool has_bps_rd_max,
1478 int64_t bps_rd_max,
1479 bool has_bps_wr_max,
1480 int64_t bps_wr_max,
1481 bool has_iops_max,
1482 int64_t iops_max,
1483 bool has_iops_rd_max,
1484 int64_t iops_rd_max,
1485 bool has_iops_wr_max,
1486 int64_t iops_wr_max,
1487 bool has_iops_size,
1488 int64_t iops_size, Error **errp)
1489 {
1490 ThrottleConfig cfg;
1491 BlockDriverState *bs;
1492
1493 bs = bdrv_find(device);
1494 if (!bs) {
1495 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1496 return;
1497 }
1498
1499 memset(&cfg, 0, sizeof(cfg));
1500 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
1501 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
1502 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
1503
1504 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
1505 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
1506 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
1507
1508 if (has_bps_max) {
1509 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
1510 }
1511 if (has_bps_rd_max) {
1512 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
1513 }
1514 if (has_bps_wr_max) {
1515 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
1516 }
1517 if (has_iops_max) {
1518 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
1519 }
1520 if (has_iops_rd_max) {
1521 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
1522 }
1523 if (has_iops_wr_max) {
1524 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
1525 }
1526
1527 if (has_iops_size) {
1528 cfg.op_size = iops_size;
1529 }
1530
1531 if (!check_throttle_config(&cfg, errp)) {
1532 return;
1533 }
1534
1535 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
1536 bdrv_io_limits_enable(bs);
1537 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
1538 bdrv_io_limits_disable(bs);
1539 }
1540
1541 if (bs->io_limits_enabled) {
1542 bdrv_set_io_limits(bs, &cfg);
1543 }
1544 }
1545
1546 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1547 {
1548 const char *id = qdict_get_str(qdict, "id");
1549 BlockDriverState *bs;
1550
1551 bs = bdrv_find(id);
1552 if (!bs) {
1553 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1554 return -1;
1555 }
1556 if (bdrv_in_use(bs)) {
1557 qerror_report(QERR_DEVICE_IN_USE, id);
1558 return -1;
1559 }
1560
1561 /* quiesce block driver; prevent further io */
1562 bdrv_drain_all();
1563 bdrv_flush(bs);
1564 bdrv_close(bs);
1565
1566 /* if we have a device attached to this BlockDriverState
1567 * then we need to make the drive anonymous until the device
1568 * can be removed. If this is a drive with no device backing
1569 * then we can just get rid of the block driver state right here.
1570 */
1571 if (bdrv_get_attached_dev(bs)) {
1572 bdrv_make_anon(bs);
1573
1574 /* Further I/O must not pause the guest */
1575 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1576 BLOCKDEV_ON_ERROR_REPORT);
1577 } else {
1578 drive_uninit(drive_get_by_blockdev(bs));
1579 }
1580
1581 return 0;
1582 }
1583
1584 void qmp_block_resize(const char *device, int64_t size, Error **errp)
1585 {
1586 BlockDriverState *bs;
1587 int ret;
1588
1589 bs = bdrv_find(device);
1590 if (!bs) {
1591 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1592 return;
1593 }
1594
1595 if (size < 0) {
1596 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1597 return;
1598 }
1599
1600 /* complete all in-flight operations before resizing the device */
1601 bdrv_drain_all();
1602
1603 ret = bdrv_truncate(bs, size);
1604 switch (ret) {
1605 case 0:
1606 break;
1607 case -ENOMEDIUM:
1608 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1609 break;
1610 case -ENOTSUP:
1611 error_set(errp, QERR_UNSUPPORTED);
1612 break;
1613 case -EACCES:
1614 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1615 break;
1616 case -EBUSY:
1617 error_set(errp, QERR_DEVICE_IN_USE, device);
1618 break;
1619 default:
1620 error_setg_errno(errp, -ret, "Could not resize");
1621 break;
1622 }
1623 }
1624
1625 static void block_job_cb(void *opaque, int ret)
1626 {
1627 BlockDriverState *bs = opaque;
1628 QObject *obj;
1629
1630 trace_block_job_cb(bs, bs->job, ret);
1631
1632 assert(bs->job);
1633 obj = qobject_from_block_job(bs->job);
1634 if (ret < 0) {
1635 QDict *dict = qobject_to_qdict(obj);
1636 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1637 }
1638
1639 if (block_job_is_cancelled(bs->job)) {
1640 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1641 } else {
1642 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1643 }
1644 qobject_decref(obj);
1645
1646 bdrv_put_ref_bh_schedule(bs);
1647 }
1648
1649 void qmp_block_stream(const char *device, bool has_base,
1650 const char *base, bool has_speed, int64_t speed,
1651 bool has_on_error, BlockdevOnError on_error,
1652 Error **errp)
1653 {
1654 BlockDriverState *bs;
1655 BlockDriverState *base_bs = NULL;
1656 Error *local_err = NULL;
1657
1658 if (!has_on_error) {
1659 on_error = BLOCKDEV_ON_ERROR_REPORT;
1660 }
1661
1662 bs = bdrv_find(device);
1663 if (!bs) {
1664 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1665 return;
1666 }
1667
1668 if (base) {
1669 base_bs = bdrv_find_backing_image(bs, base);
1670 if (base_bs == NULL) {
1671 error_set(errp, QERR_BASE_NOT_FOUND, base);
1672 return;
1673 }
1674 }
1675
1676 stream_start(bs, base_bs, base, has_speed ? speed : 0,
1677 on_error, block_job_cb, bs, &local_err);
1678 if (error_is_set(&local_err)) {
1679 error_propagate(errp, local_err);
1680 return;
1681 }
1682
1683 trace_qmp_block_stream(bs, bs->job);
1684 }
1685
1686 void qmp_block_commit(const char *device,
1687 bool has_base, const char *base, const char *top,
1688 bool has_speed, int64_t speed,
1689 Error **errp)
1690 {
1691 BlockDriverState *bs;
1692 BlockDriverState *base_bs, *top_bs;
1693 Error *local_err = NULL;
1694 /* This will be part of the QMP command, if/when the
1695 * BlockdevOnError change for blkmirror makes it in
1696 */
1697 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1698
1699 /* drain all i/o before commits */
1700 bdrv_drain_all();
1701
1702 bs = bdrv_find(device);
1703 if (!bs) {
1704 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1705 return;
1706 }
1707
1708 /* default top_bs is the active layer */
1709 top_bs = bs;
1710
1711 if (top) {
1712 if (strcmp(bs->filename, top) != 0) {
1713 top_bs = bdrv_find_backing_image(bs, top);
1714 }
1715 }
1716
1717 if (top_bs == NULL) {
1718 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1719 return;
1720 }
1721
1722 if (has_base && base) {
1723 base_bs = bdrv_find_backing_image(top_bs, base);
1724 } else {
1725 base_bs = bdrv_find_base(top_bs);
1726 }
1727
1728 if (base_bs == NULL) {
1729 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1730 return;
1731 }
1732
1733 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1734 &local_err);
1735 if (local_err != NULL) {
1736 error_propagate(errp, local_err);
1737 return;
1738 }
1739 }
1740
1741 void qmp_drive_backup(const char *device, const char *target,
1742 bool has_format, const char *format,
1743 enum MirrorSyncMode sync,
1744 bool has_mode, enum NewImageMode mode,
1745 bool has_speed, int64_t speed,
1746 bool has_on_source_error, BlockdevOnError on_source_error,
1747 bool has_on_target_error, BlockdevOnError on_target_error,
1748 Error **errp)
1749 {
1750 BlockDriverState *bs;
1751 BlockDriverState *target_bs;
1752 BlockDriverState *source = NULL;
1753 BlockDriver *drv = NULL;
1754 Error *local_err = NULL;
1755 int flags;
1756 int64_t size;
1757 int ret;
1758
1759 if (!has_speed) {
1760 speed = 0;
1761 }
1762 if (!has_on_source_error) {
1763 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1764 }
1765 if (!has_on_target_error) {
1766 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1767 }
1768 if (!has_mode) {
1769 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1770 }
1771
1772 bs = bdrv_find(device);
1773 if (!bs) {
1774 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1775 return;
1776 }
1777
1778 if (!bdrv_is_inserted(bs)) {
1779 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1780 return;
1781 }
1782
1783 if (!has_format) {
1784 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1785 }
1786 if (format) {
1787 drv = bdrv_find_format(format);
1788 if (!drv) {
1789 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1790 return;
1791 }
1792 }
1793
1794 if (bdrv_in_use(bs)) {
1795 error_set(errp, QERR_DEVICE_IN_USE, device);
1796 return;
1797 }
1798
1799 flags = bs->open_flags | BDRV_O_RDWR;
1800
1801 /* See if we have a backing HD we can use to create our new image
1802 * on top of. */
1803 if (sync == MIRROR_SYNC_MODE_TOP) {
1804 source = bs->backing_hd;
1805 if (!source) {
1806 sync = MIRROR_SYNC_MODE_FULL;
1807 }
1808 }
1809 if (sync == MIRROR_SYNC_MODE_NONE) {
1810 source = bs;
1811 }
1812
1813 size = bdrv_getlength(bs);
1814 if (size < 0) {
1815 error_setg_errno(errp, -size, "bdrv_getlength failed");
1816 return;
1817 }
1818
1819 if (mode != NEW_IMAGE_MODE_EXISTING) {
1820 assert(format && drv);
1821 if (source) {
1822 bdrv_img_create(target, format, source->filename,
1823 source->drv->format_name, NULL,
1824 size, flags, &local_err, false);
1825 } else {
1826 bdrv_img_create(target, format, NULL, NULL, NULL,
1827 size, flags, &local_err, false);
1828 }
1829 }
1830
1831 if (error_is_set(&local_err)) {
1832 error_propagate(errp, local_err);
1833 return;
1834 }
1835
1836 target_bs = bdrv_new("");
1837 ret = bdrv_open(target_bs, target, NULL, flags, drv, &local_err);
1838 if (ret < 0) {
1839 bdrv_unref(target_bs);
1840 error_propagate(errp, local_err);
1841 return;
1842 }
1843
1844 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
1845 block_job_cb, bs, &local_err);
1846 if (local_err != NULL) {
1847 bdrv_unref(target_bs);
1848 error_propagate(errp, local_err);
1849 return;
1850 }
1851 }
1852
1853 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1854
1855 void qmp_drive_mirror(const char *device, const char *target,
1856 bool has_format, const char *format,
1857 enum MirrorSyncMode sync,
1858 bool has_mode, enum NewImageMode mode,
1859 bool has_speed, int64_t speed,
1860 bool has_granularity, uint32_t granularity,
1861 bool has_buf_size, int64_t buf_size,
1862 bool has_on_source_error, BlockdevOnError on_source_error,
1863 bool has_on_target_error, BlockdevOnError on_target_error,
1864 Error **errp)
1865 {
1866 BlockDriverState *bs;
1867 BlockDriverState *source, *target_bs;
1868 BlockDriver *drv = NULL;
1869 Error *local_err = NULL;
1870 int flags;
1871 int64_t size;
1872 int ret;
1873
1874 if (!has_speed) {
1875 speed = 0;
1876 }
1877 if (!has_on_source_error) {
1878 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1879 }
1880 if (!has_on_target_error) {
1881 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1882 }
1883 if (!has_mode) {
1884 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1885 }
1886 if (!has_granularity) {
1887 granularity = 0;
1888 }
1889 if (!has_buf_size) {
1890 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1891 }
1892
1893 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1894 error_set(errp, QERR_INVALID_PARAMETER, device);
1895 return;
1896 }
1897 if (granularity & (granularity - 1)) {
1898 error_set(errp, QERR_INVALID_PARAMETER, device);
1899 return;
1900 }
1901
1902 bs = bdrv_find(device);
1903 if (!bs) {
1904 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1905 return;
1906 }
1907
1908 if (!bdrv_is_inserted(bs)) {
1909 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1910 return;
1911 }
1912
1913 if (!has_format) {
1914 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1915 }
1916 if (format) {
1917 drv = bdrv_find_format(format);
1918 if (!drv) {
1919 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1920 return;
1921 }
1922 }
1923
1924 if (bdrv_in_use(bs)) {
1925 error_set(errp, QERR_DEVICE_IN_USE, device);
1926 return;
1927 }
1928
1929 flags = bs->open_flags | BDRV_O_RDWR;
1930 source = bs->backing_hd;
1931 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1932 sync = MIRROR_SYNC_MODE_FULL;
1933 }
1934
1935 size = bdrv_getlength(bs);
1936 if (size < 0) {
1937 error_setg_errno(errp, -size, "bdrv_getlength failed");
1938 return;
1939 }
1940
1941 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1942 /* create new image w/o backing file */
1943 assert(format && drv);
1944 bdrv_img_create(target, format,
1945 NULL, NULL, NULL, size, flags, &local_err, false);
1946 } else {
1947 switch (mode) {
1948 case NEW_IMAGE_MODE_EXISTING:
1949 break;
1950 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1951 /* create new image with backing file */
1952 bdrv_img_create(target, format,
1953 source->filename,
1954 source->drv->format_name,
1955 NULL, size, flags, &local_err, false);
1956 break;
1957 default:
1958 abort();
1959 }
1960 }
1961
1962 if (error_is_set(&local_err)) {
1963 error_propagate(errp, local_err);
1964 return;
1965 }
1966
1967 /* Mirroring takes care of copy-on-write using the source's backing
1968 * file.
1969 */
1970 target_bs = bdrv_new("");
1971 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv,
1972 &local_err);
1973 if (ret < 0) {
1974 bdrv_unref(target_bs);
1975 error_propagate(errp, local_err);
1976 return;
1977 }
1978
1979 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
1980 on_source_error, on_target_error,
1981 block_job_cb, bs, &local_err);
1982 if (local_err != NULL) {
1983 bdrv_unref(target_bs);
1984 error_propagate(errp, local_err);
1985 return;
1986 }
1987 }
1988
1989 static BlockJob *find_block_job(const char *device)
1990 {
1991 BlockDriverState *bs;
1992
1993 bs = bdrv_find(device);
1994 if (!bs || !bs->job) {
1995 return NULL;
1996 }
1997 return bs->job;
1998 }
1999
2000 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2001 {
2002 BlockJob *job = find_block_job(device);
2003
2004 if (!job) {
2005 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2006 return;
2007 }
2008
2009 block_job_set_speed(job, speed, errp);
2010 }
2011
2012 void qmp_block_job_cancel(const char *device,
2013 bool has_force, bool force, Error **errp)
2014 {
2015 BlockJob *job = find_block_job(device);
2016
2017 if (!has_force) {
2018 force = false;
2019 }
2020
2021 if (!job) {
2022 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2023 return;
2024 }
2025 if (job->paused && !force) {
2026 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
2027 return;
2028 }
2029
2030 trace_qmp_block_job_cancel(job);
2031 block_job_cancel(job);
2032 }
2033
2034 void qmp_block_job_pause(const char *device, Error **errp)
2035 {
2036 BlockJob *job = find_block_job(device);
2037
2038 if (!job) {
2039 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2040 return;
2041 }
2042
2043 trace_qmp_block_job_pause(job);
2044 block_job_pause(job);
2045 }
2046
2047 void qmp_block_job_resume(const char *device, Error **errp)
2048 {
2049 BlockJob *job = find_block_job(device);
2050
2051 if (!job) {
2052 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2053 return;
2054 }
2055
2056 trace_qmp_block_job_resume(job);
2057 block_job_resume(job);
2058 }
2059
2060 void qmp_block_job_complete(const char *device, Error **errp)
2061 {
2062 BlockJob *job = find_block_job(device);
2063
2064 if (!job) {
2065 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2066 return;
2067 }
2068
2069 trace_qmp_block_job_complete(job);
2070 block_job_complete(job, errp);
2071 }
2072
2073 void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
2074 {
2075 QmpOutputVisitor *ov = qmp_output_visitor_new();
2076 QObject *obj;
2077 QDict *qdict;
2078 DriveInfo *dinfo;
2079 Error *local_err = NULL;
2080
2081 /* Require an ID in the top level */
2082 if (!options->has_id) {
2083 error_setg(errp, "Block device needs an ID");
2084 goto fail;
2085 }
2086
2087 /* TODO Sort it out in raw-posix and drive_init: Reject aio=native with
2088 * cache.direct=false instead of silently switching to aio=threads, except
2089 * if called from drive_init.
2090 *
2091 * For now, simply forbidding the combination for all drivers will do. */
2092 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
2093 bool direct = options->cache->has_direct && options->cache->direct;
2094 if (!options->has_cache && !direct) {
2095 error_setg(errp, "aio=native requires cache.direct=true");
2096 goto fail;
2097 }
2098 }
2099
2100 visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
2101 &options, NULL, &local_err);
2102 if (error_is_set(&local_err)) {
2103 error_propagate(errp, local_err);
2104 goto fail;
2105 }
2106
2107 obj = qmp_output_get_qobject(ov);
2108 qdict = qobject_to_qdict(obj);
2109
2110 qdict_flatten(qdict);
2111
2112 QemuOpts *opts = qemu_opts_from_qdict(&qemu_drive_opts, qdict, &local_err);
2113 if (error_is_set(&local_err)) {
2114 error_propagate(errp, local_err);
2115 goto fail;
2116 }
2117
2118 dinfo = blockdev_init(opts, IF_NONE);
2119 if (!dinfo) {
2120 error_setg(errp, "Could not open image");
2121 goto fail;
2122 }
2123
2124 fail:
2125 qmp_output_visitor_cleanup(ov);
2126 }
2127
2128 static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
2129 {
2130 BlockJobInfoList **prev = opaque;
2131 BlockJob *job = bs->job;
2132
2133 if (job) {
2134 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
2135 elem->value = block_job_query(bs->job);
2136 (*prev)->next = elem;
2137 *prev = elem;
2138 }
2139 }
2140
2141 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
2142 {
2143 /* Dummy is a fake list element for holding the head pointer */
2144 BlockJobInfoList dummy = {};
2145 BlockJobInfoList *prev = &dummy;
2146 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
2147 return dummy.next;
2148 }
2149
2150 QemuOptsList qemu_common_drive_opts = {
2151 .name = "drive",
2152 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
2153 .desc = {
2154 {
2155 .name = "bus",
2156 .type = QEMU_OPT_NUMBER,
2157 .help = "bus number",
2158 },{
2159 .name = "unit",
2160 .type = QEMU_OPT_NUMBER,
2161 .help = "unit number (i.e. lun for scsi)",
2162 },{
2163 .name = "if",
2164 .type = QEMU_OPT_STRING,
2165 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
2166 },{
2167 .name = "index",
2168 .type = QEMU_OPT_NUMBER,
2169 .help = "index number",
2170 },{
2171 .name = "cyls",
2172 .type = QEMU_OPT_NUMBER,
2173 .help = "number of cylinders (ide disk geometry)",
2174 },{
2175 .name = "heads",
2176 .type = QEMU_OPT_NUMBER,
2177 .help = "number of heads (ide disk geometry)",
2178 },{
2179 .name = "secs",
2180 .type = QEMU_OPT_NUMBER,
2181 .help = "number of sectors (ide disk geometry)",
2182 },{
2183 .name = "trans",
2184 .type = QEMU_OPT_STRING,
2185 .help = "chs translation (auto, lba. none)",
2186 },{
2187 .name = "media",
2188 .type = QEMU_OPT_STRING,
2189 .help = "media type (disk, cdrom)",
2190 },{
2191 .name = "snapshot",
2192 .type = QEMU_OPT_BOOL,
2193 .help = "enable/disable snapshot mode",
2194 },{
2195 .name = "file",
2196 .type = QEMU_OPT_STRING,
2197 .help = "disk image",
2198 },{
2199 .name = "discard",
2200 .type = QEMU_OPT_STRING,
2201 .help = "discard operation (ignore/off, unmap/on)",
2202 },{
2203 .name = "cache.writeback",
2204 .type = QEMU_OPT_BOOL,
2205 .help = "enables writeback mode for any caches",
2206 },{
2207 .name = "cache.direct",
2208 .type = QEMU_OPT_BOOL,
2209 .help = "enables use of O_DIRECT (bypass the host page cache)",
2210 },{
2211 .name = "cache.no-flush",
2212 .type = QEMU_OPT_BOOL,
2213 .help = "ignore any flush requests for the device",
2214 },{
2215 .name = "aio",
2216 .type = QEMU_OPT_STRING,
2217 .help = "host AIO implementation (threads, native)",
2218 },{
2219 .name = "format",
2220 .type = QEMU_OPT_STRING,
2221 .help = "disk format (raw, qcow2, ...)",
2222 },{
2223 .name = "serial",
2224 .type = QEMU_OPT_STRING,
2225 .help = "disk serial number",
2226 },{
2227 .name = "rerror",
2228 .type = QEMU_OPT_STRING,
2229 .help = "read error action",
2230 },{
2231 .name = "werror",
2232 .type = QEMU_OPT_STRING,
2233 .help = "write error action",
2234 },{
2235 .name = "addr",
2236 .type = QEMU_OPT_STRING,
2237 .help = "pci address (virtio only)",
2238 },{
2239 .name = "read-only",
2240 .type = QEMU_OPT_BOOL,
2241 .help = "open drive file as read-only",
2242 },{
2243 .name = "throttling.iops-total",
2244 .type = QEMU_OPT_NUMBER,
2245 .help = "limit total I/O operations per second",
2246 },{
2247 .name = "throttling.iops-read",
2248 .type = QEMU_OPT_NUMBER,
2249 .help = "limit read operations per second",
2250 },{
2251 .name = "throttling.iops-write",
2252 .type = QEMU_OPT_NUMBER,
2253 .help = "limit write operations per second",
2254 },{
2255 .name = "throttling.bps-total",
2256 .type = QEMU_OPT_NUMBER,
2257 .help = "limit total bytes per second",
2258 },{
2259 .name = "throttling.bps-read",
2260 .type = QEMU_OPT_NUMBER,
2261 .help = "limit read bytes per second",
2262 },{
2263 .name = "throttling.bps-write",
2264 .type = QEMU_OPT_NUMBER,
2265 .help = "limit write bytes per second",
2266 },{
2267 .name = "throttling.iops-total-max",
2268 .type = QEMU_OPT_NUMBER,
2269 .help = "I/O operations burst",
2270 },{
2271 .name = "throttling.iops-read-max",
2272 .type = QEMU_OPT_NUMBER,
2273 .help = "I/O operations read burst",
2274 },{
2275 .name = "throttling.iops-write-max",
2276 .type = QEMU_OPT_NUMBER,
2277 .help = "I/O operations write burst",
2278 },{
2279 .name = "throttling.bps-total-max",
2280 .type = QEMU_OPT_NUMBER,
2281 .help = "total bytes burst",
2282 },{
2283 .name = "throttling.bps-read-max",
2284 .type = QEMU_OPT_NUMBER,
2285 .help = "total bytes read burst",
2286 },{
2287 .name = "throttling.bps-write-max",
2288 .type = QEMU_OPT_NUMBER,
2289 .help = "total bytes write burst",
2290 },{
2291 .name = "throttling.iops-size",
2292 .type = QEMU_OPT_NUMBER,
2293 .help = "when limiting by iops max size of an I/O in bytes",
2294 },{
2295 .name = "copy-on-read",
2296 .type = QEMU_OPT_BOOL,
2297 .help = "copy read data from backing file into image file",
2298 },{
2299 .name = "boot",
2300 .type = QEMU_OPT_BOOL,
2301 .help = "(deprecated, ignored)",
2302 },
2303 { /* end of list */ }
2304 },
2305 };
2306
2307 QemuOptsList qemu_drive_opts = {
2308 .name = "drive",
2309 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
2310 .desc = {
2311 /*
2312 * no elements => accept any params
2313 * validation will happen later
2314 */
2315 { /* end of list */ }
2316 },
2317 };