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