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