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