]> git.proxmox.com Git - qemu.git/blob - blockdev.c
coroutine: use AioContext for CoQueue BH
[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
10 #include "sysemu/blockdev.h"
11 #include "hw/block-common.h"
12 #include "block/blockjob.h"
13 #include "monitor/monitor.h"
14 #include "qapi/qmp/qerror.h"
15 #include "qemu/option.h"
16 #include "qemu/config-file.h"
17 #include "qapi/qmp/types.h"
18 #include "sysemu/sysemu.h"
19 #include "block/block_int.h"
20 #include "qmp-commands.h"
21 #include "trace.h"
22 #include "sysemu/arch_init.h"
23
24 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
25 extern QemuOptsList qemu_common_drive_opts;
26
27 static const char *const if_name[IF_COUNT] = {
28 [IF_NONE] = "none",
29 [IF_IDE] = "ide",
30 [IF_SCSI] = "scsi",
31 [IF_FLOPPY] = "floppy",
32 [IF_PFLASH] = "pflash",
33 [IF_MTD] = "mtd",
34 [IF_SD] = "sd",
35 [IF_VIRTIO] = "virtio",
36 [IF_XEN] = "xen",
37 };
38
39 static const int if_max_devs[IF_COUNT] = {
40 /*
41 * Do not change these numbers! They govern how drive option
42 * index maps to unit and bus. That mapping is ABI.
43 *
44 * All controllers used to imlement if=T drives need to support
45 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
46 * Otherwise, some index values map to "impossible" bus, unit
47 * values.
48 *
49 * For instance, if you change [IF_SCSI] to 255, -drive
50 * if=scsi,index=12 no longer means bus=1,unit=5, but
51 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
52 * the drive can't be set up. Regression.
53 */
54 [IF_IDE] = 2,
55 [IF_SCSI] = 7,
56 };
57
58 /*
59 * We automatically delete the drive when a device using it gets
60 * unplugged. Questionable feature, but we can't just drop it.
61 * Device models call blockdev_mark_auto_del() to schedule the
62 * automatic deletion, and generic qdev code calls blockdev_auto_del()
63 * when deletion is actually safe.
64 */
65 void blockdev_mark_auto_del(BlockDriverState *bs)
66 {
67 DriveInfo *dinfo = drive_get_by_blockdev(bs);
68
69 if (bs->job) {
70 block_job_cancel(bs->job);
71 }
72 if (dinfo) {
73 dinfo->auto_del = 1;
74 }
75 }
76
77 void blockdev_auto_del(BlockDriverState *bs)
78 {
79 DriveInfo *dinfo = drive_get_by_blockdev(bs);
80
81 if (dinfo && dinfo->auto_del) {
82 drive_put_ref(dinfo);
83 }
84 }
85
86 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
87 {
88 int max_devs = if_max_devs[type];
89 return max_devs ? index / max_devs : 0;
90 }
91
92 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
93 {
94 int max_devs = if_max_devs[type];
95 return max_devs ? index % max_devs : index;
96 }
97
98 QemuOpts *drive_def(const char *optstr)
99 {
100 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
101 }
102
103 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
104 const char *optstr)
105 {
106 QemuOpts *opts;
107 char buf[32];
108
109 opts = drive_def(optstr);
110 if (!opts) {
111 return NULL;
112 }
113 if (type != IF_DEFAULT) {
114 qemu_opt_set(opts, "if", if_name[type]);
115 }
116 if (index >= 0) {
117 snprintf(buf, sizeof(buf), "%d", index);
118 qemu_opt_set(opts, "index", buf);
119 }
120 if (file)
121 qemu_opt_set(opts, "file", file);
122 return opts;
123 }
124
125 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
126 {
127 DriveInfo *dinfo;
128
129 /* seek interface, bus and unit */
130
131 QTAILQ_FOREACH(dinfo, &drives, next) {
132 if (dinfo->type == type &&
133 dinfo->bus == bus &&
134 dinfo->unit == unit)
135 return dinfo;
136 }
137
138 return NULL;
139 }
140
141 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
142 {
143 return drive_get(type,
144 drive_index_to_bus_id(type, index),
145 drive_index_to_unit_id(type, index));
146 }
147
148 int drive_get_max_bus(BlockInterfaceType type)
149 {
150 int max_bus;
151 DriveInfo *dinfo;
152
153 max_bus = -1;
154 QTAILQ_FOREACH(dinfo, &drives, next) {
155 if(dinfo->type == type &&
156 dinfo->bus > max_bus)
157 max_bus = dinfo->bus;
158 }
159 return max_bus;
160 }
161
162 /* Get a block device. This should only be used for single-drive devices
163 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
164 appropriate bus. */
165 DriveInfo *drive_get_next(BlockInterfaceType type)
166 {
167 static int next_block_unit[IF_COUNT];
168
169 return drive_get(type, 0, next_block_unit[type]++);
170 }
171
172 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
173 {
174 DriveInfo *dinfo;
175
176 QTAILQ_FOREACH(dinfo, &drives, next) {
177 if (dinfo->bdrv == bs) {
178 return dinfo;
179 }
180 }
181 return NULL;
182 }
183
184 static void bdrv_format_print(void *opaque, const char *name)
185 {
186 error_printf(" %s", name);
187 }
188
189 static void drive_uninit(DriveInfo *dinfo)
190 {
191 qemu_opts_del(dinfo->opts);
192 bdrv_delete(dinfo->bdrv);
193 g_free(dinfo->id);
194 QTAILQ_REMOVE(&drives, dinfo, next);
195 g_free(dinfo->serial);
196 g_free(dinfo);
197 }
198
199 void drive_put_ref(DriveInfo *dinfo)
200 {
201 assert(dinfo->refcount);
202 if (--dinfo->refcount == 0) {
203 drive_uninit(dinfo);
204 }
205 }
206
207 void drive_get_ref(DriveInfo *dinfo)
208 {
209 dinfo->refcount++;
210 }
211
212 typedef struct {
213 QEMUBH *bh;
214 DriveInfo *dinfo;
215 } DrivePutRefBH;
216
217 static void drive_put_ref_bh(void *opaque)
218 {
219 DrivePutRefBH *s = opaque;
220
221 drive_put_ref(s->dinfo);
222 qemu_bh_delete(s->bh);
223 g_free(s);
224 }
225
226 /*
227 * Release a drive reference in a BH
228 *
229 * It is not possible to use drive_put_ref() from a callback function when the
230 * callers still need the drive. In such cases we schedule a BH to release the
231 * reference.
232 */
233 static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
234 {
235 DrivePutRefBH *s;
236
237 s = g_new(DrivePutRefBH, 1);
238 s->bh = qemu_bh_new(drive_put_ref_bh, s);
239 s->dinfo = dinfo;
240 qemu_bh_schedule(s->bh);
241 }
242
243 static int parse_block_error_action(const char *buf, bool is_read)
244 {
245 if (!strcmp(buf, "ignore")) {
246 return BLOCKDEV_ON_ERROR_IGNORE;
247 } else if (!is_read && !strcmp(buf, "enospc")) {
248 return BLOCKDEV_ON_ERROR_ENOSPC;
249 } else if (!strcmp(buf, "stop")) {
250 return BLOCKDEV_ON_ERROR_STOP;
251 } else if (!strcmp(buf, "report")) {
252 return BLOCKDEV_ON_ERROR_REPORT;
253 } else {
254 error_report("'%s' invalid %s error action",
255 buf, is_read ? "read" : "write");
256 return -1;
257 }
258 }
259
260 static bool do_check_io_limits(BlockIOLimit *io_limits, Error **errp)
261 {
262 bool bps_flag;
263 bool iops_flag;
264
265 assert(io_limits);
266
267 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
268 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
269 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
270 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
271 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
272 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
273 if (bps_flag || iops_flag) {
274 error_setg(errp, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
275 "cannot be used at the same time");
276 return false;
277 }
278
279 if (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] < 0 ||
280 io_limits->bps[BLOCK_IO_LIMIT_WRITE] < 0 ||
281 io_limits->bps[BLOCK_IO_LIMIT_READ] < 0 ||
282 io_limits->iops[BLOCK_IO_LIMIT_TOTAL] < 0 ||
283 io_limits->iops[BLOCK_IO_LIMIT_WRITE] < 0 ||
284 io_limits->iops[BLOCK_IO_LIMIT_READ] < 0) {
285 error_setg(errp, "bps and iops values must be 0 or greater");
286 return false;
287 }
288
289 return true;
290 }
291
292 DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
293 {
294 const char *buf;
295 const char *file = NULL;
296 const char *serial;
297 const char *mediastr = "";
298 BlockInterfaceType type;
299 enum { MEDIA_DISK, MEDIA_CDROM } media;
300 int bus_id, unit_id;
301 int cyls, heads, secs, translation;
302 BlockDriver *drv = NULL;
303 int max_devs;
304 int index;
305 int ro = 0;
306 int bdrv_flags = 0;
307 int on_read_error, on_write_error;
308 const char *devaddr;
309 DriveInfo *dinfo;
310 BlockIOLimit io_limits;
311 int snapshot = 0;
312 bool copy_on_read;
313 int ret;
314 Error *error = NULL;
315 QemuOpts *opts;
316 QDict *bs_opts;
317 const char *id;
318
319 translation = BIOS_ATA_TRANSLATION_AUTO;
320 media = MEDIA_DISK;
321
322 /* Check common options by copying from all_opts to opts, all other options
323 * are stored in bs_opts. */
324 id = qemu_opts_id(all_opts);
325 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
326 if (error_is_set(&error)) {
327 qerror_report_err(error);
328 error_free(error);
329 return NULL;
330 }
331
332 bs_opts = qdict_new();
333 qemu_opts_to_qdict(all_opts, bs_opts);
334 qemu_opts_absorb_qdict(opts, bs_opts, &error);
335 if (error_is_set(&error)) {
336 qerror_report_err(error);
337 error_free(error);
338 return NULL;
339 }
340
341 if (id) {
342 qdict_del(bs_opts, "id");
343 }
344
345 /* extract parameters */
346 bus_id = qemu_opt_get_number(opts, "bus", 0);
347 unit_id = qemu_opt_get_number(opts, "unit", -1);
348 index = qemu_opt_get_number(opts, "index", -1);
349
350 cyls = qemu_opt_get_number(opts, "cyls", 0);
351 heads = qemu_opt_get_number(opts, "heads", 0);
352 secs = qemu_opt_get_number(opts, "secs", 0);
353
354 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
355 ro = qemu_opt_get_bool(opts, "readonly", 0);
356 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
357
358 file = qemu_opt_get(opts, "file");
359 serial = qemu_opt_get(opts, "serial");
360
361 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
362 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
363 ;
364 if (type == IF_COUNT) {
365 error_report("unsupported bus type '%s'", buf);
366 return NULL;
367 }
368 } else {
369 type = block_default_type;
370 }
371
372 max_devs = if_max_devs[type];
373
374 if (cyls || heads || secs) {
375 if (cyls < 1) {
376 error_report("invalid physical cyls number");
377 return NULL;
378 }
379 if (heads < 1) {
380 error_report("invalid physical heads number");
381 return NULL;
382 }
383 if (secs < 1) {
384 error_report("invalid physical secs number");
385 return NULL;
386 }
387 }
388
389 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
390 if (!cyls) {
391 error_report("'%s' trans must be used with cyls, heads and secs",
392 buf);
393 return NULL;
394 }
395 if (!strcmp(buf, "none"))
396 translation = BIOS_ATA_TRANSLATION_NONE;
397 else if (!strcmp(buf, "lba"))
398 translation = BIOS_ATA_TRANSLATION_LBA;
399 else if (!strcmp(buf, "auto"))
400 translation = BIOS_ATA_TRANSLATION_AUTO;
401 else {
402 error_report("'%s' invalid translation type", buf);
403 return NULL;
404 }
405 }
406
407 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
408 if (!strcmp(buf, "disk")) {
409 media = MEDIA_DISK;
410 } else if (!strcmp(buf, "cdrom")) {
411 if (cyls || secs || heads) {
412 error_report("CHS can't be set with media=%s", buf);
413 return NULL;
414 }
415 media = MEDIA_CDROM;
416 } else {
417 error_report("'%s' invalid media", buf);
418 return NULL;
419 }
420 }
421
422 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
423 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
424 error_report("invalid discard option");
425 return NULL;
426 }
427 }
428
429 bdrv_flags |= BDRV_O_CACHE_WB;
430 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
431 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
432 error_report("invalid cache option");
433 return NULL;
434 }
435 }
436
437 #ifdef CONFIG_LINUX_AIO
438 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
439 if (!strcmp(buf, "native")) {
440 bdrv_flags |= BDRV_O_NATIVE_AIO;
441 } else if (!strcmp(buf, "threads")) {
442 /* this is the default */
443 } else {
444 error_report("invalid aio option");
445 return NULL;
446 }
447 }
448 #endif
449
450 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
451 if (is_help_option(buf)) {
452 error_printf("Supported formats:");
453 bdrv_iterate_format(bdrv_format_print, NULL);
454 error_printf("\n");
455 return NULL;
456 }
457 drv = bdrv_find_whitelisted_format(buf);
458 if (!drv) {
459 error_report("'%s' invalid format", buf);
460 return NULL;
461 }
462 }
463
464 /* disk I/O throttling */
465 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
466 qemu_opt_get_number(opts, "bps", 0);
467 io_limits.bps[BLOCK_IO_LIMIT_READ] =
468 qemu_opt_get_number(opts, "bps_rd", 0);
469 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
470 qemu_opt_get_number(opts, "bps_wr", 0);
471 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
472 qemu_opt_get_number(opts, "iops", 0);
473 io_limits.iops[BLOCK_IO_LIMIT_READ] =
474 qemu_opt_get_number(opts, "iops_rd", 0);
475 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
476 qemu_opt_get_number(opts, "iops_wr", 0);
477
478 if (!do_check_io_limits(&io_limits, &error)) {
479 error_report("%s", error_get_pretty(error));
480 error_free(error);
481 return NULL;
482 }
483
484 if (qemu_opt_get(opts, "boot") != NULL) {
485 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
486 "ignored. Future versions will reject this parameter. Please "
487 "update your scripts.\n");
488 }
489
490 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
491 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
492 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
493 error_report("werror is not supported by this bus type");
494 return NULL;
495 }
496
497 on_write_error = parse_block_error_action(buf, 0);
498 if (on_write_error < 0) {
499 return NULL;
500 }
501 }
502
503 on_read_error = BLOCKDEV_ON_ERROR_REPORT;
504 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
505 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
506 error_report("rerror is not supported by this bus type");
507 return NULL;
508 }
509
510 on_read_error = parse_block_error_action(buf, 1);
511 if (on_read_error < 0) {
512 return NULL;
513 }
514 }
515
516 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
517 if (type != IF_VIRTIO) {
518 error_report("addr is not supported by this bus type");
519 return NULL;
520 }
521 }
522
523 /* compute bus and unit according index */
524
525 if (index != -1) {
526 if (bus_id != 0 || unit_id != -1) {
527 error_report("index cannot be used with bus and unit");
528 return NULL;
529 }
530 bus_id = drive_index_to_bus_id(type, index);
531 unit_id = drive_index_to_unit_id(type, index);
532 }
533
534 /* if user doesn't specify a unit_id,
535 * try to find the first free
536 */
537
538 if (unit_id == -1) {
539 unit_id = 0;
540 while (drive_get(type, bus_id, unit_id) != NULL) {
541 unit_id++;
542 if (max_devs && unit_id >= max_devs) {
543 unit_id -= max_devs;
544 bus_id++;
545 }
546 }
547 }
548
549 /* check unit id */
550
551 if (max_devs && unit_id >= max_devs) {
552 error_report("unit %d too big (max is %d)",
553 unit_id, max_devs - 1);
554 return NULL;
555 }
556
557 /*
558 * catch multiple definitions
559 */
560
561 if (drive_get(type, bus_id, unit_id) != NULL) {
562 error_report("drive with bus=%d, unit=%d (index=%d) exists",
563 bus_id, unit_id, index);
564 return NULL;
565 }
566
567 /* init */
568
569 dinfo = g_malloc0(sizeof(*dinfo));
570 if ((buf = qemu_opts_id(opts)) != NULL) {
571 dinfo->id = g_strdup(buf);
572 } else {
573 /* no id supplied -> create one */
574 dinfo->id = g_malloc0(32);
575 if (type == IF_IDE || type == IF_SCSI)
576 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
577 if (max_devs)
578 snprintf(dinfo->id, 32, "%s%i%s%i",
579 if_name[type], bus_id, mediastr, unit_id);
580 else
581 snprintf(dinfo->id, 32, "%s%s%i",
582 if_name[type], mediastr, unit_id);
583 }
584 dinfo->bdrv = bdrv_new(dinfo->id);
585 dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
586 dinfo->bdrv->read_only = ro;
587 dinfo->devaddr = devaddr;
588 dinfo->type = type;
589 dinfo->bus = bus_id;
590 dinfo->unit = unit_id;
591 dinfo->cyls = cyls;
592 dinfo->heads = heads;
593 dinfo->secs = secs;
594 dinfo->trans = translation;
595 dinfo->opts = all_opts;
596 dinfo->refcount = 1;
597 if (serial != NULL) {
598 dinfo->serial = g_strdup(serial);
599 }
600 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
601
602 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
603
604 /* disk I/O throttling */
605 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
606
607 switch(type) {
608 case IF_IDE:
609 case IF_SCSI:
610 case IF_XEN:
611 case IF_NONE:
612 dinfo->media_cd = media == MEDIA_CDROM;
613 break;
614 case IF_SD:
615 case IF_FLOPPY:
616 case IF_PFLASH:
617 case IF_MTD:
618 break;
619 case IF_VIRTIO:
620 {
621 /* add virtio block device */
622 QemuOpts *devopts;
623 devopts = qemu_opts_create_nofail(qemu_find_opts("device"));
624 if (arch_type == QEMU_ARCH_S390X) {
625 qemu_opt_set(devopts, "driver", "virtio-blk-s390");
626 } else {
627 qemu_opt_set(devopts, "driver", "virtio-blk-pci");
628 }
629 qemu_opt_set(devopts, "drive", dinfo->id);
630 if (devaddr)
631 qemu_opt_set(devopts, "addr", devaddr);
632 break;
633 }
634 default:
635 abort();
636 }
637 if (!file || !*file) {
638 return dinfo;
639 }
640 if (snapshot) {
641 /* always use cache=unsafe with snapshot */
642 bdrv_flags &= ~BDRV_O_CACHE_MASK;
643 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
644 }
645
646 if (copy_on_read) {
647 bdrv_flags |= BDRV_O_COPY_ON_READ;
648 }
649
650 if (runstate_check(RUN_STATE_INMIGRATE)) {
651 bdrv_flags |= BDRV_O_INCOMING;
652 }
653
654 if (media == MEDIA_CDROM) {
655 /* CDROM is fine for any interface, don't check. */
656 ro = 1;
657 } else if (ro == 1) {
658 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
659 type != IF_NONE && type != IF_PFLASH) {
660 error_report("readonly not supported by this bus type");
661 goto err;
662 }
663 }
664
665 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
666
667 if (ro && copy_on_read) {
668 error_report("warning: disabling copy_on_read on readonly drive");
669 }
670
671 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv);
672 bs_opts = NULL;
673
674 if (ret < 0) {
675 if (ret == -EMEDIUMTYPE) {
676 error_report("could not open disk image %s: not in %s format",
677 file, drv->format_name);
678 } else {
679 error_report("could not open disk image %s: %s",
680 file, strerror(-ret));
681 }
682 goto err;
683 }
684
685 if (bdrv_key_required(dinfo->bdrv))
686 autostart = 0;
687
688 qemu_opts_del(opts);
689
690 return dinfo;
691
692 err:
693 qemu_opts_del(opts);
694 QDECREF(bs_opts);
695 bdrv_delete(dinfo->bdrv);
696 g_free(dinfo->id);
697 QTAILQ_REMOVE(&drives, dinfo, next);
698 g_free(dinfo);
699 return NULL;
700 }
701
702 void do_commit(Monitor *mon, const QDict *qdict)
703 {
704 const char *device = qdict_get_str(qdict, "device");
705 BlockDriverState *bs;
706 int ret;
707
708 if (!strcmp(device, "all")) {
709 ret = bdrv_commit_all();
710 } else {
711 bs = bdrv_find(device);
712 if (!bs) {
713 monitor_printf(mon, "Device '%s' not found\n", device);
714 return;
715 }
716 ret = bdrv_commit(bs);
717 }
718 if (ret < 0) {
719 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
720 strerror(-ret));
721 }
722 }
723
724 static void blockdev_do_action(int kind, void *data, Error **errp)
725 {
726 BlockdevAction action;
727 BlockdevActionList list;
728
729 action.kind = kind;
730 action.data = data;
731 list.value = &action;
732 list.next = NULL;
733 qmp_transaction(&list, errp);
734 }
735
736 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
737 bool has_format, const char *format,
738 bool has_mode, enum NewImageMode mode,
739 Error **errp)
740 {
741 BlockdevSnapshot snapshot = {
742 .device = (char *) device,
743 .snapshot_file = (char *) snapshot_file,
744 .has_format = has_format,
745 .format = (char *) format,
746 .has_mode = has_mode,
747 .mode = mode,
748 };
749 blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
750 errp);
751 }
752
753
754 /* New and old BlockDriverState structs for group snapshots */
755 typedef struct BlkTransactionStates {
756 BlockDriverState *old_bs;
757 BlockDriverState *new_bs;
758 QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
759 } BlkTransactionStates;
760
761 /*
762 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
763 * then we do not pivot any of the devices in the group, and abandon the
764 * snapshots
765 */
766 void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
767 {
768 int ret = 0;
769 BlockdevActionList *dev_entry = dev_list;
770 BlkTransactionStates *states, *next;
771 Error *local_err = NULL;
772
773 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
774 QSIMPLEQ_INIT(&snap_bdrv_states);
775
776 /* drain all i/o before any snapshots */
777 bdrv_drain_all();
778
779 /* We don't do anything in this loop that commits us to the snapshot */
780 while (NULL != dev_entry) {
781 BlockdevAction *dev_info = NULL;
782 BlockDriver *proto_drv;
783 BlockDriver *drv;
784 int flags;
785 enum NewImageMode mode;
786 const char *new_image_file;
787 const char *device;
788 const char *format = "qcow2";
789
790 dev_info = dev_entry->value;
791 dev_entry = dev_entry->next;
792
793 states = g_malloc0(sizeof(BlkTransactionStates));
794 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
795
796 switch (dev_info->kind) {
797 case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
798 device = dev_info->blockdev_snapshot_sync->device;
799 if (!dev_info->blockdev_snapshot_sync->has_mode) {
800 dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
801 }
802 new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
803 if (dev_info->blockdev_snapshot_sync->has_format) {
804 format = dev_info->blockdev_snapshot_sync->format;
805 }
806 mode = dev_info->blockdev_snapshot_sync->mode;
807 break;
808 default:
809 abort();
810 }
811
812 drv = bdrv_find_format(format);
813 if (!drv) {
814 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
815 goto delete_and_fail;
816 }
817
818 states->old_bs = bdrv_find(device);
819 if (!states->old_bs) {
820 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
821 goto delete_and_fail;
822 }
823
824 if (!bdrv_is_inserted(states->old_bs)) {
825 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
826 goto delete_and_fail;
827 }
828
829 if (bdrv_in_use(states->old_bs)) {
830 error_set(errp, QERR_DEVICE_IN_USE, device);
831 goto delete_and_fail;
832 }
833
834 if (!bdrv_is_read_only(states->old_bs)) {
835 if (bdrv_flush(states->old_bs)) {
836 error_set(errp, QERR_IO_ERROR);
837 goto delete_and_fail;
838 }
839 }
840
841 flags = states->old_bs->open_flags;
842
843 proto_drv = bdrv_find_protocol(new_image_file);
844 if (!proto_drv) {
845 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
846 goto delete_and_fail;
847 }
848
849 /* create new image w/backing file */
850 if (mode != NEW_IMAGE_MODE_EXISTING) {
851 bdrv_img_create(new_image_file, format,
852 states->old_bs->filename,
853 states->old_bs->drv->format_name,
854 NULL, -1, flags, &local_err, false);
855 if (error_is_set(&local_err)) {
856 error_propagate(errp, local_err);
857 goto delete_and_fail;
858 }
859 }
860
861 /* We will manually add the backing_hd field to the bs later */
862 states->new_bs = bdrv_new("");
863 /* TODO Inherit bs->options or only take explicit options with an
864 * extended QMP command? */
865 ret = bdrv_open(states->new_bs, new_image_file, NULL,
866 flags | BDRV_O_NO_BACKING, drv);
867 if (ret != 0) {
868 error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
869 goto delete_and_fail;
870 }
871 }
872
873
874 /* Now we are going to do the actual pivot. Everything up to this point
875 * is reversible, but we are committed at this point */
876 QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
877 /* This removes our old bs from the bdrv_states, and adds the new bs */
878 bdrv_append(states->new_bs, states->old_bs);
879 /* We don't need (or want) to use the transactional
880 * bdrv_reopen_multiple() across all the entries at once, because we
881 * don't want to abort all of them if one of them fails the reopen */
882 bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR,
883 NULL);
884 }
885
886 /* success */
887 goto exit;
888
889 delete_and_fail:
890 /*
891 * failure, and it is all-or-none; abandon each new bs, and keep using
892 * the original bs for all images
893 */
894 QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
895 if (states->new_bs) {
896 bdrv_delete(states->new_bs);
897 }
898 }
899 exit:
900 QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
901 g_free(states);
902 }
903 }
904
905
906 static void eject_device(BlockDriverState *bs, int force, Error **errp)
907 {
908 if (bdrv_in_use(bs)) {
909 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
910 return;
911 }
912 if (!bdrv_dev_has_removable_media(bs)) {
913 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
914 return;
915 }
916
917 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
918 bdrv_dev_eject_request(bs, force);
919 if (!force) {
920 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
921 return;
922 }
923 }
924
925 bdrv_close(bs);
926 }
927
928 void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
929 {
930 BlockDriverState *bs;
931
932 bs = bdrv_find(device);
933 if (!bs) {
934 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
935 return;
936 }
937
938 eject_device(bs, force, errp);
939 }
940
941 void qmp_block_passwd(const char *device, const char *password, Error **errp)
942 {
943 BlockDriverState *bs;
944 int err;
945
946 bs = bdrv_find(device);
947 if (!bs) {
948 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
949 return;
950 }
951
952 err = bdrv_set_key(bs, password);
953 if (err == -EINVAL) {
954 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
955 return;
956 } else if (err < 0) {
957 error_set(errp, QERR_INVALID_PASSWORD);
958 return;
959 }
960 }
961
962 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
963 int bdrv_flags, BlockDriver *drv,
964 const char *password, Error **errp)
965 {
966 if (bdrv_open(bs, filename, NULL, bdrv_flags, drv) < 0) {
967 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
968 return;
969 }
970
971 if (bdrv_key_required(bs)) {
972 if (password) {
973 if (bdrv_set_key(bs, password) < 0) {
974 error_set(errp, QERR_INVALID_PASSWORD);
975 }
976 } else {
977 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
978 bdrv_get_encrypted_filename(bs));
979 }
980 } else if (password) {
981 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
982 }
983 }
984
985 void qmp_change_blockdev(const char *device, const char *filename,
986 bool has_format, const char *format, Error **errp)
987 {
988 BlockDriverState *bs;
989 BlockDriver *drv = NULL;
990 int bdrv_flags;
991 Error *err = NULL;
992
993 bs = bdrv_find(device);
994 if (!bs) {
995 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
996 return;
997 }
998
999 if (format) {
1000 drv = bdrv_find_whitelisted_format(format);
1001 if (!drv) {
1002 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1003 return;
1004 }
1005 }
1006
1007 eject_device(bs, 0, &err);
1008 if (error_is_set(&err)) {
1009 error_propagate(errp, err);
1010 return;
1011 }
1012
1013 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
1014 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
1015
1016 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
1017 }
1018
1019 /* throttling disk I/O limits */
1020 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1021 int64_t bps_wr, int64_t iops, int64_t iops_rd,
1022 int64_t iops_wr, Error **errp)
1023 {
1024 BlockIOLimit io_limits;
1025 BlockDriverState *bs;
1026
1027 bs = bdrv_find(device);
1028 if (!bs) {
1029 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1030 return;
1031 }
1032
1033 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
1034 io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
1035 io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
1036 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
1037 io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
1038 io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
1039
1040 if (!do_check_io_limits(&io_limits, errp)) {
1041 return;
1042 }
1043
1044 bs->io_limits = io_limits;
1045 bs->slice_time = BLOCK_IO_SLICE_TIME;
1046
1047 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
1048 bdrv_io_limits_enable(bs);
1049 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
1050 bdrv_io_limits_disable(bs);
1051 } else {
1052 if (bs->block_timer) {
1053 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
1054 }
1055 }
1056 }
1057
1058 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1059 {
1060 const char *id = qdict_get_str(qdict, "id");
1061 BlockDriverState *bs;
1062
1063 bs = bdrv_find(id);
1064 if (!bs) {
1065 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1066 return -1;
1067 }
1068 if (bdrv_in_use(bs)) {
1069 qerror_report(QERR_DEVICE_IN_USE, id);
1070 return -1;
1071 }
1072
1073 /* quiesce block driver; prevent further io */
1074 bdrv_drain_all();
1075 bdrv_flush(bs);
1076 bdrv_close(bs);
1077
1078 /* if we have a device attached to this BlockDriverState
1079 * then we need to make the drive anonymous until the device
1080 * can be removed. If this is a drive with no device backing
1081 * then we can just get rid of the block driver state right here.
1082 */
1083 if (bdrv_get_attached_dev(bs)) {
1084 bdrv_make_anon(bs);
1085 } else {
1086 drive_uninit(drive_get_by_blockdev(bs));
1087 }
1088
1089 return 0;
1090 }
1091
1092 void qmp_block_resize(const char *device, int64_t size, Error **errp)
1093 {
1094 BlockDriverState *bs;
1095
1096 bs = bdrv_find(device);
1097 if (!bs) {
1098 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1099 return;
1100 }
1101
1102 if (size < 0) {
1103 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
1104 return;
1105 }
1106
1107 switch (bdrv_truncate(bs, size)) {
1108 case 0:
1109 break;
1110 case -ENOMEDIUM:
1111 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1112 break;
1113 case -ENOTSUP:
1114 error_set(errp, QERR_UNSUPPORTED);
1115 break;
1116 case -EACCES:
1117 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1118 break;
1119 case -EBUSY:
1120 error_set(errp, QERR_DEVICE_IN_USE, device);
1121 break;
1122 default:
1123 error_set(errp, QERR_UNDEFINED_ERROR);
1124 break;
1125 }
1126 }
1127
1128 static void block_job_cb(void *opaque, int ret)
1129 {
1130 BlockDriverState *bs = opaque;
1131 QObject *obj;
1132
1133 trace_block_job_cb(bs, bs->job, ret);
1134
1135 assert(bs->job);
1136 obj = qobject_from_block_job(bs->job);
1137 if (ret < 0) {
1138 QDict *dict = qobject_to_qdict(obj);
1139 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1140 }
1141
1142 if (block_job_is_cancelled(bs->job)) {
1143 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1144 } else {
1145 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1146 }
1147 qobject_decref(obj);
1148
1149 drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
1150 }
1151
1152 void qmp_block_stream(const char *device, bool has_base,
1153 const char *base, bool has_speed, int64_t speed,
1154 bool has_on_error, BlockdevOnError on_error,
1155 Error **errp)
1156 {
1157 BlockDriverState *bs;
1158 BlockDriverState *base_bs = NULL;
1159 Error *local_err = NULL;
1160
1161 if (!has_on_error) {
1162 on_error = BLOCKDEV_ON_ERROR_REPORT;
1163 }
1164
1165 bs = bdrv_find(device);
1166 if (!bs) {
1167 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1168 return;
1169 }
1170
1171 if (base) {
1172 base_bs = bdrv_find_backing_image(bs, base);
1173 if (base_bs == NULL) {
1174 error_set(errp, QERR_BASE_NOT_FOUND, base);
1175 return;
1176 }
1177 }
1178
1179 stream_start(bs, base_bs, base, has_speed ? speed : 0,
1180 on_error, block_job_cb, bs, &local_err);
1181 if (error_is_set(&local_err)) {
1182 error_propagate(errp, local_err);
1183 return;
1184 }
1185
1186 /* Grab a reference so hotplug does not delete the BlockDriverState from
1187 * underneath us.
1188 */
1189 drive_get_ref(drive_get_by_blockdev(bs));
1190
1191 trace_qmp_block_stream(bs, bs->job);
1192 }
1193
1194 void qmp_block_commit(const char *device,
1195 bool has_base, const char *base, const char *top,
1196 bool has_speed, int64_t speed,
1197 Error **errp)
1198 {
1199 BlockDriverState *bs;
1200 BlockDriverState *base_bs, *top_bs;
1201 Error *local_err = NULL;
1202 /* This will be part of the QMP command, if/when the
1203 * BlockdevOnError change for blkmirror makes it in
1204 */
1205 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
1206
1207 /* drain all i/o before commits */
1208 bdrv_drain_all();
1209
1210 bs = bdrv_find(device);
1211 if (!bs) {
1212 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1213 return;
1214 }
1215
1216 /* default top_bs is the active layer */
1217 top_bs = bs;
1218
1219 if (top) {
1220 if (strcmp(bs->filename, top) != 0) {
1221 top_bs = bdrv_find_backing_image(bs, top);
1222 }
1223 }
1224
1225 if (top_bs == NULL) {
1226 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1227 return;
1228 }
1229
1230 if (has_base && base) {
1231 base_bs = bdrv_find_backing_image(top_bs, base);
1232 } else {
1233 base_bs = bdrv_find_base(top_bs);
1234 }
1235
1236 if (base_bs == NULL) {
1237 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1238 return;
1239 }
1240
1241 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1242 &local_err);
1243 if (local_err != NULL) {
1244 error_propagate(errp, local_err);
1245 return;
1246 }
1247 /* Grab a reference so hotplug does not delete the BlockDriverState from
1248 * underneath us.
1249 */
1250 drive_get_ref(drive_get_by_blockdev(bs));
1251 }
1252
1253 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1254
1255 void qmp_drive_mirror(const char *device, const char *target,
1256 bool has_format, const char *format,
1257 enum MirrorSyncMode sync,
1258 bool has_mode, enum NewImageMode mode,
1259 bool has_speed, int64_t speed,
1260 bool has_granularity, uint32_t granularity,
1261 bool has_buf_size, int64_t buf_size,
1262 bool has_on_source_error, BlockdevOnError on_source_error,
1263 bool has_on_target_error, BlockdevOnError on_target_error,
1264 Error **errp)
1265 {
1266 BlockDriverState *bs;
1267 BlockDriverState *source, *target_bs;
1268 BlockDriver *proto_drv;
1269 BlockDriver *drv = NULL;
1270 Error *local_err = NULL;
1271 int flags;
1272 uint64_t size;
1273 int ret;
1274
1275 if (!has_speed) {
1276 speed = 0;
1277 }
1278 if (!has_on_source_error) {
1279 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1280 }
1281 if (!has_on_target_error) {
1282 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1283 }
1284 if (!has_mode) {
1285 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1286 }
1287 if (!has_granularity) {
1288 granularity = 0;
1289 }
1290 if (!has_buf_size) {
1291 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1292 }
1293
1294 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1295 error_set(errp, QERR_INVALID_PARAMETER, device);
1296 return;
1297 }
1298 if (granularity & (granularity - 1)) {
1299 error_set(errp, QERR_INVALID_PARAMETER, device);
1300 return;
1301 }
1302
1303 bs = bdrv_find(device);
1304 if (!bs) {
1305 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1306 return;
1307 }
1308
1309 if (!bdrv_is_inserted(bs)) {
1310 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1311 return;
1312 }
1313
1314 if (!has_format) {
1315 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1316 }
1317 if (format) {
1318 drv = bdrv_find_format(format);
1319 if (!drv) {
1320 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1321 return;
1322 }
1323 }
1324
1325 if (bdrv_in_use(bs)) {
1326 error_set(errp, QERR_DEVICE_IN_USE, device);
1327 return;
1328 }
1329
1330 flags = bs->open_flags | BDRV_O_RDWR;
1331 source = bs->backing_hd;
1332 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1333 sync = MIRROR_SYNC_MODE_FULL;
1334 }
1335
1336 proto_drv = bdrv_find_protocol(target);
1337 if (!proto_drv) {
1338 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1339 return;
1340 }
1341
1342 bdrv_get_geometry(bs, &size);
1343 size *= 512;
1344 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1345 /* create new image w/o backing file */
1346 assert(format && drv);
1347 bdrv_img_create(target, format,
1348 NULL, NULL, NULL, size, flags, &local_err, false);
1349 } else {
1350 switch (mode) {
1351 case NEW_IMAGE_MODE_EXISTING:
1352 ret = 0;
1353 break;
1354 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1355 /* create new image with backing file */
1356 bdrv_img_create(target, format,
1357 source->filename,
1358 source->drv->format_name,
1359 NULL, size, flags, &local_err, false);
1360 break;
1361 default:
1362 abort();
1363 }
1364 }
1365
1366 if (error_is_set(&local_err)) {
1367 error_propagate(errp, local_err);
1368 return;
1369 }
1370
1371 /* Mirroring takes care of copy-on-write using the source's backing
1372 * file.
1373 */
1374 target_bs = bdrv_new("");
1375 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
1376
1377 if (ret < 0) {
1378 bdrv_delete(target_bs);
1379 error_set(errp, QERR_OPEN_FILE_FAILED, target);
1380 return;
1381 }
1382
1383 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
1384 on_source_error, on_target_error,
1385 block_job_cb, bs, &local_err);
1386 if (local_err != NULL) {
1387 bdrv_delete(target_bs);
1388 error_propagate(errp, local_err);
1389 return;
1390 }
1391
1392 /* Grab a reference so hotplug does not delete the BlockDriverState from
1393 * underneath us.
1394 */
1395 drive_get_ref(drive_get_by_blockdev(bs));
1396 }
1397
1398 static BlockJob *find_block_job(const char *device)
1399 {
1400 BlockDriverState *bs;
1401
1402 bs = bdrv_find(device);
1403 if (!bs || !bs->job) {
1404 return NULL;
1405 }
1406 return bs->job;
1407 }
1408
1409 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
1410 {
1411 BlockJob *job = find_block_job(device);
1412
1413 if (!job) {
1414 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1415 return;
1416 }
1417
1418 block_job_set_speed(job, speed, errp);
1419 }
1420
1421 void qmp_block_job_cancel(const char *device,
1422 bool has_force, bool force, Error **errp)
1423 {
1424 BlockJob *job = find_block_job(device);
1425
1426 if (!has_force) {
1427 force = false;
1428 }
1429
1430 if (!job) {
1431 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1432 return;
1433 }
1434 if (job->paused && !force) {
1435 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
1436 return;
1437 }
1438
1439 trace_qmp_block_job_cancel(job);
1440 block_job_cancel(job);
1441 }
1442
1443 void qmp_block_job_pause(const char *device, Error **errp)
1444 {
1445 BlockJob *job = find_block_job(device);
1446
1447 if (!job) {
1448 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1449 return;
1450 }
1451
1452 trace_qmp_block_job_pause(job);
1453 block_job_pause(job);
1454 }
1455
1456 void qmp_block_job_resume(const char *device, Error **errp)
1457 {
1458 BlockJob *job = find_block_job(device);
1459
1460 if (!job) {
1461 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1462 return;
1463 }
1464
1465 trace_qmp_block_job_resume(job);
1466 block_job_resume(job);
1467 }
1468
1469 void qmp_block_job_complete(const char *device, Error **errp)
1470 {
1471 BlockJob *job = find_block_job(device);
1472
1473 if (!job) {
1474 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1475 return;
1476 }
1477
1478 trace_qmp_block_job_complete(job);
1479 block_job_complete(job, errp);
1480 }
1481
1482 static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1483 {
1484 BlockJobInfoList **prev = opaque;
1485 BlockJob *job = bs->job;
1486
1487 if (job) {
1488 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
1489 elem->value = block_job_query(bs->job);
1490 (*prev)->next = elem;
1491 *prev = elem;
1492 }
1493 }
1494
1495 BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1496 {
1497 /* Dummy is a fake list element for holding the head pointer */
1498 BlockJobInfoList dummy = {};
1499 BlockJobInfoList *prev = &dummy;
1500 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1501 return dummy.next;
1502 }
1503
1504 QemuOptsList qemu_common_drive_opts = {
1505 .name = "drive",
1506 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
1507 .desc = {
1508 {
1509 .name = "bus",
1510 .type = QEMU_OPT_NUMBER,
1511 .help = "bus number",
1512 },{
1513 .name = "unit",
1514 .type = QEMU_OPT_NUMBER,
1515 .help = "unit number (i.e. lun for scsi)",
1516 },{
1517 .name = "if",
1518 .type = QEMU_OPT_STRING,
1519 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1520 },{
1521 .name = "index",
1522 .type = QEMU_OPT_NUMBER,
1523 .help = "index number",
1524 },{
1525 .name = "cyls",
1526 .type = QEMU_OPT_NUMBER,
1527 .help = "number of cylinders (ide disk geometry)",
1528 },{
1529 .name = "heads",
1530 .type = QEMU_OPT_NUMBER,
1531 .help = "number of heads (ide disk geometry)",
1532 },{
1533 .name = "secs",
1534 .type = QEMU_OPT_NUMBER,
1535 .help = "number of sectors (ide disk geometry)",
1536 },{
1537 .name = "trans",
1538 .type = QEMU_OPT_STRING,
1539 .help = "chs translation (auto, lba. none)",
1540 },{
1541 .name = "media",
1542 .type = QEMU_OPT_STRING,
1543 .help = "media type (disk, cdrom)",
1544 },{
1545 .name = "snapshot",
1546 .type = QEMU_OPT_BOOL,
1547 .help = "enable/disable snapshot mode",
1548 },{
1549 .name = "file",
1550 .type = QEMU_OPT_STRING,
1551 .help = "disk image",
1552 },{
1553 .name = "discard",
1554 .type = QEMU_OPT_STRING,
1555 .help = "discard operation (ignore/off, unmap/on)",
1556 },{
1557 .name = "cache",
1558 .type = QEMU_OPT_STRING,
1559 .help = "host cache usage (none, writeback, writethrough, "
1560 "directsync, unsafe)",
1561 },{
1562 .name = "aio",
1563 .type = QEMU_OPT_STRING,
1564 .help = "host AIO implementation (threads, native)",
1565 },{
1566 .name = "format",
1567 .type = QEMU_OPT_STRING,
1568 .help = "disk format (raw, qcow2, ...)",
1569 },{
1570 .name = "serial",
1571 .type = QEMU_OPT_STRING,
1572 .help = "disk serial number",
1573 },{
1574 .name = "rerror",
1575 .type = QEMU_OPT_STRING,
1576 .help = "read error action",
1577 },{
1578 .name = "werror",
1579 .type = QEMU_OPT_STRING,
1580 .help = "write error action",
1581 },{
1582 .name = "addr",
1583 .type = QEMU_OPT_STRING,
1584 .help = "pci address (virtio only)",
1585 },{
1586 .name = "readonly",
1587 .type = QEMU_OPT_BOOL,
1588 .help = "open drive file as read-only",
1589 },{
1590 .name = "iops",
1591 .type = QEMU_OPT_NUMBER,
1592 .help = "limit total I/O operations per second",
1593 },{
1594 .name = "iops_rd",
1595 .type = QEMU_OPT_NUMBER,
1596 .help = "limit read operations per second",
1597 },{
1598 .name = "iops_wr",
1599 .type = QEMU_OPT_NUMBER,
1600 .help = "limit write operations per second",
1601 },{
1602 .name = "bps",
1603 .type = QEMU_OPT_NUMBER,
1604 .help = "limit total bytes per second",
1605 },{
1606 .name = "bps_rd",
1607 .type = QEMU_OPT_NUMBER,
1608 .help = "limit read bytes per second",
1609 },{
1610 .name = "bps_wr",
1611 .type = QEMU_OPT_NUMBER,
1612 .help = "limit write bytes per second",
1613 },{
1614 .name = "copy-on-read",
1615 .type = QEMU_OPT_BOOL,
1616 .help = "copy read data from backing file into image file",
1617 },{
1618 .name = "boot",
1619 .type = QEMU_OPT_BOOL,
1620 .help = "(deprecated, ignored)",
1621 },
1622 { /* end of list */ }
1623 },
1624 };
1625
1626 QemuOptsList qemu_drive_opts = {
1627 .name = "drive",
1628 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
1629 .desc = {
1630 /*
1631 * no elements => accept any params
1632 * validation will happen later
1633 */
1634 { /* end of list */ }
1635 },
1636 };