]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev.c
qcow2: Use dashes instead of underscores in options
[mirror_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
57975222
KW
315static DriveInfo *blockdev_init(QemuOpts *all_opts,
316 BlockInterfaceType block_default_type)
666daa68
MA
317{
318 const char *buf;
319 const char *file = NULL;
666daa68
MA
320 const char *serial;
321 const char *mediastr = "";
322 BlockInterfaceType type;
323 enum { MEDIA_DISK, MEDIA_CDROM } media;
324 int bus_id, unit_id;
325 int cyls, heads, secs, translation;
666daa68
MA
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;
74fe54f2 341 bool has_driver_specific_opts;
666daa68 342
666daa68 343 translation = BIOS_ATA_TRANSLATION_AUTO;
666daa68
MA
344 media = MEDIA_DISK;
345
0006383e
KW
346 /* Check common options by copying from all_opts to opts, all other options
347 * are stored in bs_opts. */
348 id = qemu_opts_id(all_opts);
349 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
350 if (error_is_set(&error)) {
351 qerror_report_err(error);
352 error_free(error);
353 return NULL;
354 }
355
356 bs_opts = qdict_new();
357 qemu_opts_to_qdict(all_opts, bs_opts);
358 qemu_opts_absorb_qdict(opts, bs_opts, &error);
359 if (error_is_set(&error)) {
360 qerror_report_err(error);
361 error_free(error);
362 return NULL;
363 }
364
365 if (id) {
366 qdict_del(bs_opts, "id");
367 }
368
74fe54f2
KW
369 has_driver_specific_opts = !!qdict_size(bs_opts);
370
666daa68
MA
371 /* extract parameters */
372 bus_id = qemu_opt_get_number(opts, "bus", 0);
373 unit_id = qemu_opt_get_number(opts, "unit", -1);
374 index = qemu_opt_get_number(opts, "index", -1);
375
376 cyls = qemu_opt_get_number(opts, "cyls", 0);
377 heads = qemu_opt_get_number(opts, "heads", 0);
378 secs = qemu_opt_get_number(opts, "secs", 0);
379
380 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
381 ro = qemu_opt_get_bool(opts, "readonly", 0);
fb0490f6 382 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
666daa68
MA
383
384 file = qemu_opt_get(opts, "file");
385 serial = qemu_opt_get(opts, "serial");
386
387 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
1960966d
MA
388 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
389 ;
390 if (type == IF_COUNT) {
807105a7 391 error_report("unsupported bus type '%s'", buf);
666daa68
MA
392 return NULL;
393 }
2d3999fe 394 } else {
2d0d2837 395 type = block_default_type;
666daa68 396 }
2d3999fe 397
1960966d 398 max_devs = if_max_devs[type];
666daa68
MA
399
400 if (cyls || heads || secs) {
aaea3f36 401 if (cyls < 1) {
807105a7 402 error_report("invalid physical cyls number");
666daa68
MA
403 return NULL;
404 }
aaea3f36 405 if (heads < 1) {
807105a7 406 error_report("invalid physical heads number");
666daa68
MA
407 return NULL;
408 }
aaea3f36 409 if (secs < 1) {
807105a7 410 error_report("invalid physical secs number");
666daa68
MA
411 return NULL;
412 }
413 }
414
415 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
416 if (!cyls) {
e4080f9b 417 error_report("'%s' trans must be used with cyls, heads and secs",
807105a7 418 buf);
666daa68
MA
419 return NULL;
420 }
421 if (!strcmp(buf, "none"))
422 translation = BIOS_ATA_TRANSLATION_NONE;
423 else if (!strcmp(buf, "lba"))
424 translation = BIOS_ATA_TRANSLATION_LBA;
425 else if (!strcmp(buf, "auto"))
426 translation = BIOS_ATA_TRANSLATION_AUTO;
427 else {
807105a7 428 error_report("'%s' invalid translation type", buf);
666daa68
MA
429 return NULL;
430 }
431 }
432
433 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
434 if (!strcmp(buf, "disk")) {
435 media = MEDIA_DISK;
436 } else if (!strcmp(buf, "cdrom")) {
437 if (cyls || secs || heads) {
e7ff8f0e 438 error_report("CHS can't be set with media=%s", buf);
666daa68
MA
439 return NULL;
440 }
441 media = MEDIA_CDROM;
442 } else {
807105a7 443 error_report("'%s' invalid media", buf);
666daa68
MA
444 return NULL;
445 }
446 }
447
a9384aff
PB
448 if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
449 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
450 error_report("invalid discard option");
451 return NULL;
452 }
453 }
454
1f212b9d 455 bdrv_flags |= BDRV_O_CACHE_WB;
666daa68 456 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
c3993cdc
SH
457 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
458 error_report("invalid cache option");
459 return NULL;
666daa68
MA
460 }
461 }
462
463#ifdef CONFIG_LINUX_AIO
464 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
465 if (!strcmp(buf, "native")) {
466 bdrv_flags |= BDRV_O_NATIVE_AIO;
467 } else if (!strcmp(buf, "threads")) {
468 /* this is the default */
469 } else {
807105a7 470 error_report("invalid aio option");
666daa68
MA
471 return NULL;
472 }
473 }
474#endif
475
476 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
c8057f95
PM
477 if (is_help_option(buf)) {
478 error_printf("Supported formats:");
479 bdrv_iterate_format(bdrv_format_print, NULL);
480 error_printf("\n");
481 return NULL;
666daa68 482 }
74fe54f2
KW
483
484 qdict_put(bs_opts, "driver", qstring_from_str(buf));
666daa68
MA
485 }
486
0563e191
ZYW
487 /* disk I/O throttling */
488 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
57975222 489 qemu_opt_get_number(opts, "throttling.bps-total", 0);
0563e191 490 io_limits.bps[BLOCK_IO_LIMIT_READ] =
57975222 491 qemu_opt_get_number(opts, "throttling.bps-read", 0);
0563e191 492 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
57975222 493 qemu_opt_get_number(opts, "throttling.bps-write", 0);
0563e191 494 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
57975222 495 qemu_opt_get_number(opts, "throttling.iops-total", 0);
0563e191 496 io_limits.iops[BLOCK_IO_LIMIT_READ] =
57975222 497 qemu_opt_get_number(opts, "throttling.iops-read", 0);
0563e191 498 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
57975222 499 qemu_opt_get_number(opts, "throttling.iops-write", 0);
0563e191 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) {
74fe54f2 661 if (has_driver_specific_opts) {
c2ad1b0c
KW
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
74fe54f2
KW
698 QINCREF(bs_opts);
699 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, NULL);
0006383e 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",
74fe54f2 704 file ?: dinfo->id, qdict_get_str(bs_opts, "driver"));
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 714
74fe54f2 715 QDECREF(bs_opts);
0006383e
KW
716 qemu_opts_del(opts);
717
666daa68 718 return dinfo;
a9ae2bff
MA
719
720err:
0006383e
KW
721 qemu_opts_del(opts);
722 QDECREF(bs_opts);
a9ae2bff 723 bdrv_delete(dinfo->bdrv);
7267c094 724 g_free(dinfo->id);
a9ae2bff 725 QTAILQ_REMOVE(&drives, dinfo, next);
7267c094 726 g_free(dinfo);
a9ae2bff 727 return NULL;
666daa68
MA
728}
729
57975222
KW
730static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to)
731{
732 const char *value;
733
734 value = qemu_opt_get(opts, from);
735 if (value) {
736 qemu_opt_set(opts, to, value);
737 qemu_opt_unset(opts, from);
738 }
739}
740
741DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
742{
743 /* Change legacy command line options into QMP ones */
744 qemu_opt_rename(all_opts, "iops", "throttling.iops-total");
745 qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read");
746 qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write");
747
748 qemu_opt_rename(all_opts, "bps", "throttling.bps-total");
749 qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read");
750 qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write");
751
752 return blockdev_init(all_opts, block_default_type);
753}
754
666daa68
MA
755void do_commit(Monitor *mon, const QDict *qdict)
756{
666daa68 757 const char *device = qdict_get_str(qdict, "device");
6ab4b5ab 758 BlockDriverState *bs;
e8877497 759 int ret;
666daa68 760
6ab4b5ab 761 if (!strcmp(device, "all")) {
e8877497 762 ret = bdrv_commit_all();
6ab4b5ab
MA
763 } else {
764 bs = bdrv_find(device);
ac59eb95 765 if (!bs) {
58513bde 766 monitor_printf(mon, "Device '%s' not found\n", device);
ac59eb95 767 return;
6ab4b5ab 768 }
2d3735d3 769 ret = bdrv_commit(bs);
58513bde
JC
770 }
771 if (ret < 0) {
772 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
773 strerror(-ret));
666daa68
MA
774 }
775}
776
6cc2a415
PB
777static void blockdev_do_action(int kind, void *data, Error **errp)
778{
c8a83e85
KW
779 TransactionAction action;
780 TransactionActionList list;
6cc2a415
PB
781
782 action.kind = kind;
783 action.data = data;
784 list.value = &action;
785 list.next = NULL;
786 qmp_transaction(&list, errp);
787}
788
6106e249
LC
789void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
790 bool has_format, const char *format,
6cc2a415 791 bool has_mode, enum NewImageMode mode,
6106e249 792 Error **errp)
f8882568 793{
6cc2a415
PB
794 BlockdevSnapshot snapshot = {
795 .device = (char *) device,
796 .snapshot_file = (char *) snapshot_file,
797 .has_format = has_format,
798 .format = (char *) format,
799 .has_mode = has_mode,
800 .mode = mode,
801 };
c8a83e85
KW
802 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
803 &snapshot, errp);
f8882568
JS
804}
805
8802d1fd
JC
806
807/* New and old BlockDriverState structs for group snapshots */
ba0c86a3 808
ba5d6ab6 809typedef struct BlkTransactionState BlkTransactionState;
ba0c86a3
WX
810
811/* Only prepare() may fail. In a single transaction, only one of commit() or
812 abort() will be called, clean() will always be called if it present. */
813typedef struct BdrvActionOps {
814 /* Size of state struct, in bytes. */
815 size_t instance_size;
816 /* Prepare the work, must NOT be NULL. */
ba5d6ab6 817 void (*prepare)(BlkTransactionState *common, Error **errp);
f9ea81e8 818 /* Commit the changes, can be NULL. */
ba5d6ab6 819 void (*commit)(BlkTransactionState *common);
ba0c86a3 820 /* Abort the changes on fail, can be NULL. */
ba5d6ab6 821 void (*abort)(BlkTransactionState *common);
ba0c86a3 822 /* Clean up resource in the end, can be NULL. */
ba5d6ab6 823 void (*clean)(BlkTransactionState *common);
ba0c86a3
WX
824} BdrvActionOps;
825
826/*
827 * This structure must be arranged as first member in child type, assuming
828 * that compiler will also arrange it to the same address with parent instance.
829 * Later it will be used in free().
830 */
ba5d6ab6 831struct BlkTransactionState {
c8a83e85 832 TransactionAction *action;
ba0c86a3 833 const BdrvActionOps *ops;
ba5d6ab6 834 QSIMPLEQ_ENTRY(BlkTransactionState) entry;
ba0c86a3
WX
835};
836
837/* external snapshot private data */
ba5d6ab6
SH
838typedef struct ExternalSnapshotState {
839 BlkTransactionState common;
8802d1fd
JC
840 BlockDriverState *old_bs;
841 BlockDriverState *new_bs;
ba5d6ab6 842} ExternalSnapshotState;
8802d1fd 843
ba5d6ab6 844static void external_snapshot_prepare(BlkTransactionState *common,
9b9877ee
WX
845 Error **errp)
846{
9b9877ee
WX
847 BlockDriver *drv;
848 int flags, ret;
849 Error *local_err = NULL;
e2a31e87
WX
850 const char *device;
851 const char *new_image_file;
852 const char *format = "qcow2";
853 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
ba5d6ab6
SH
854 ExternalSnapshotState *state =
855 DO_UPCAST(ExternalSnapshotState, common, common);
c8a83e85 856 TransactionAction *action = common->action;
9b9877ee 857
e2a31e87 858 /* get parameters */
c8a83e85 859 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
e2a31e87
WX
860
861 device = action->blockdev_snapshot_sync->device;
862 new_image_file = action->blockdev_snapshot_sync->snapshot_file;
863 if (action->blockdev_snapshot_sync->has_format) {
864 format = action->blockdev_snapshot_sync->format;
865 }
866 if (action->blockdev_snapshot_sync->has_mode) {
867 mode = action->blockdev_snapshot_sync->mode;
868 }
869
870 /* start processing */
9b9877ee
WX
871 drv = bdrv_find_format(format);
872 if (!drv) {
873 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
874 return;
875 }
876
ba5d6ab6
SH
877 state->old_bs = bdrv_find(device);
878 if (!state->old_bs) {
9b9877ee
WX
879 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
880 return;
881 }
882
ba5d6ab6 883 if (!bdrv_is_inserted(state->old_bs)) {
9b9877ee
WX
884 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
885 return;
886 }
887
ba5d6ab6 888 if (bdrv_in_use(state->old_bs)) {
9b9877ee
WX
889 error_set(errp, QERR_DEVICE_IN_USE, device);
890 return;
891 }
892
ba5d6ab6
SH
893 if (!bdrv_is_read_only(state->old_bs)) {
894 if (bdrv_flush(state->old_bs)) {
9b9877ee
WX
895 error_set(errp, QERR_IO_ERROR);
896 return;
897 }
898 }
899
ba5d6ab6 900 flags = state->old_bs->open_flags;
9b9877ee 901
9b9877ee
WX
902 /* create new image w/backing file */
903 if (mode != NEW_IMAGE_MODE_EXISTING) {
904 bdrv_img_create(new_image_file, format,
ba5d6ab6
SH
905 state->old_bs->filename,
906 state->old_bs->drv->format_name,
9b9877ee
WX
907 NULL, -1, flags, &local_err, false);
908 if (error_is_set(&local_err)) {
909 error_propagate(errp, local_err);
910 return;
911 }
912 }
913
914 /* We will manually add the backing_hd field to the bs later */
ba5d6ab6 915 state->new_bs = bdrv_new("");
9b9877ee
WX
916 /* TODO Inherit bs->options or only take explicit options with an
917 * extended QMP command? */
ba5d6ab6 918 ret = bdrv_open(state->new_bs, new_image_file, NULL,
9b9877ee
WX
919 flags | BDRV_O_NO_BACKING, drv);
920 if (ret != 0) {
0eef407c 921 error_setg_file_open(errp, -ret, new_image_file);
9b9877ee
WX
922 }
923}
924
ba5d6ab6 925static void external_snapshot_commit(BlkTransactionState *common)
3b0047e8 926{
ba5d6ab6
SH
927 ExternalSnapshotState *state =
928 DO_UPCAST(ExternalSnapshotState, common, common);
ba0c86a3 929
ba5d6ab6
SH
930 /* This removes our old bs and adds the new bs */
931 bdrv_append(state->new_bs, state->old_bs);
3b0047e8
WX
932 /* We don't need (or want) to use the transactional
933 * bdrv_reopen_multiple() across all the entries at once, because we
934 * don't want to abort all of them if one of them fails the reopen */
ba5d6ab6 935 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
3b0047e8
WX
936 NULL);
937}
938
ba5d6ab6 939static void external_snapshot_abort(BlkTransactionState *common)
96b86bf7 940{
ba5d6ab6
SH
941 ExternalSnapshotState *state =
942 DO_UPCAST(ExternalSnapshotState, common, common);
943 if (state->new_bs) {
944 bdrv_delete(state->new_bs);
96b86bf7
WX
945 }
946}
947
3037f364
SH
948typedef struct DriveBackupState {
949 BlkTransactionState common;
950 BlockDriverState *bs;
951 BlockJob *job;
952} DriveBackupState;
953
954static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
955{
956 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
957 DriveBackup *backup;
958 Error *local_err = NULL;
959
960 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
961 backup = common->action->drive_backup;
962
963 qmp_drive_backup(backup->device, backup->target,
964 backup->has_format, backup->format,
b53169ea 965 backup->sync,
3037f364
SH
966 backup->has_mode, backup->mode,
967 backup->has_speed, backup->speed,
968 backup->has_on_source_error, backup->on_source_error,
969 backup->has_on_target_error, backup->on_target_error,
970 &local_err);
971 if (error_is_set(&local_err)) {
972 error_propagate(errp, local_err);
973 state->bs = NULL;
974 state->job = NULL;
975 return;
976 }
977
978 state->bs = bdrv_find(backup->device);
979 state->job = state->bs->job;
980}
981
982static void drive_backup_abort(BlkTransactionState *common)
983{
984 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
985 BlockDriverState *bs = state->bs;
986
987 /* Only cancel if it's the job we started */
988 if (bs && bs->job && bs->job == state->job) {
989 block_job_cancel_sync(bs->job);
990 }
991}
992
78b18b78
SH
993static void abort_prepare(BlkTransactionState *common, Error **errp)
994{
995 error_setg(errp, "Transaction aborted using Abort action");
996}
997
998static void abort_commit(BlkTransactionState *common)
999{
1000 assert(false); /* this action never succeeds */
1001}
1002
ba0c86a3 1003static const BdrvActionOps actions[] = {
c8a83e85 1004 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
ba5d6ab6 1005 .instance_size = sizeof(ExternalSnapshotState),
ba0c86a3
WX
1006 .prepare = external_snapshot_prepare,
1007 .commit = external_snapshot_commit,
1008 .abort = external_snapshot_abort,
1009 },
3037f364
SH
1010 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
1011 .instance_size = sizeof(DriveBackupState),
1012 .prepare = drive_backup_prepare,
1013 .abort = drive_backup_abort,
1014 },
78b18b78
SH
1015 [TRANSACTION_ACTION_KIND_ABORT] = {
1016 .instance_size = sizeof(BlkTransactionState),
1017 .prepare = abort_prepare,
1018 .commit = abort_commit,
1019 },
ba0c86a3
WX
1020};
1021
8802d1fd
JC
1022/*
1023 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1024 * then we do not pivot any of the devices in the group, and abandon the
1025 * snapshots
1026 */
c8a83e85 1027void qmp_transaction(TransactionActionList *dev_list, Error **errp)
8802d1fd 1028{
c8a83e85 1029 TransactionActionList *dev_entry = dev_list;
ba5d6ab6 1030 BlkTransactionState *state, *next;
43e17041 1031 Error *local_err = NULL;
8802d1fd 1032
ba5d6ab6 1033 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
8802d1fd
JC
1034 QSIMPLEQ_INIT(&snap_bdrv_states);
1035
1036 /* drain all i/o before any snapshots */
1037 bdrv_drain_all();
1038
1039 /* We don't do anything in this loop that commits us to the snapshot */
1040 while (NULL != dev_entry) {
c8a83e85 1041 TransactionAction *dev_info = NULL;
ba0c86a3 1042 const BdrvActionOps *ops;
52e7c241 1043
8802d1fd
JC
1044 dev_info = dev_entry->value;
1045 dev_entry = dev_entry->next;
1046
ba0c86a3
WX
1047 assert(dev_info->kind < ARRAY_SIZE(actions));
1048
1049 ops = &actions[dev_info->kind];
ba5d6ab6
SH
1050 state = g_malloc0(ops->instance_size);
1051 state->ops = ops;
1052 state->action = dev_info;
1053 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
8802d1fd 1054
ba5d6ab6 1055 state->ops->prepare(state, &local_err);
ba0c86a3
WX
1056 if (error_is_set(&local_err)) {
1057 error_propagate(errp, local_err);
1058 goto delete_and_fail;
8802d1fd 1059 }
8802d1fd
JC
1060 }
1061
ba5d6ab6 1062 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
f9ea81e8
SH
1063 if (state->ops->commit) {
1064 state->ops->commit(state);
1065 }
8802d1fd
JC
1066 }
1067
1068 /* success */
1069 goto exit;
1070
1071delete_and_fail:
1072 /*
1073 * failure, and it is all-or-none; abandon each new bs, and keep using
1074 * the original bs for all images
1075 */
ba5d6ab6
SH
1076 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
1077 if (state->ops->abort) {
1078 state->ops->abort(state);
ba0c86a3 1079 }
8802d1fd
JC
1080 }
1081exit:
ba5d6ab6
SH
1082 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
1083 if (state->ops->clean) {
1084 state->ops->clean(state);
ba0c86a3 1085 }
ba5d6ab6 1086 g_free(state);
8802d1fd 1087 }
8802d1fd
JC
1088}
1089
1090
92d48558 1091static void eject_device(BlockDriverState *bs, int force, Error **errp)
666daa68 1092{
2d3735d3
SH
1093 if (bdrv_in_use(bs)) {
1094 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
1095 return;
1096 }
2c6942fa 1097 if (!bdrv_dev_has_removable_media(bs)) {
92d48558
LC
1098 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
1099 return;
ea8f942f 1100 }
92d48558 1101
025ccaa7
PB
1102 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
1103 bdrv_dev_eject_request(bs, force);
1104 if (!force) {
92d48558
LC
1105 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
1106 return;
025ccaa7 1107 }
666daa68 1108 }
92d48558 1109
3b5276b5 1110 bdrv_close(bs);
666daa68
MA
1111}
1112
c245b6a3 1113void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
666daa68
MA
1114{
1115 BlockDriverState *bs;
666daa68 1116
c245b6a3 1117 bs = bdrv_find(device);
666daa68 1118 if (!bs) {
c245b6a3
LC
1119 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1120 return;
92d48558
LC
1121 }
1122
c245b6a3 1123 eject_device(bs, force, errp);
666daa68
MA
1124}
1125
a4dea8a9 1126void qmp_block_passwd(const char *device, const char *password, Error **errp)
666daa68
MA
1127{
1128 BlockDriverState *bs;
1129 int err;
1130
a4dea8a9 1131 bs = bdrv_find(device);
666daa68 1132 if (!bs) {
a4dea8a9
LC
1133 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1134 return;
666daa68
MA
1135 }
1136
a4dea8a9 1137 err = bdrv_set_key(bs, password);
666daa68 1138 if (err == -EINVAL) {
a4dea8a9
LC
1139 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1140 return;
666daa68 1141 } else if (err < 0) {
a4dea8a9
LC
1142 error_set(errp, QERR_INVALID_PASSWORD);
1143 return;
666daa68 1144 }
666daa68
MA
1145}
1146
333a96ec
LC
1147static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
1148 int bdrv_flags, BlockDriver *drv,
1149 const char *password, Error **errp)
1150{
0eef407c
LC
1151 int ret;
1152
1153 ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv);
1154 if (ret < 0) {
1155 error_setg_file_open(errp, -ret, filename);
333a96ec
LC
1156 return;
1157 }
1158
1159 if (bdrv_key_required(bs)) {
1160 if (password) {
1161 if (bdrv_set_key(bs, password) < 0) {
1162 error_set(errp, QERR_INVALID_PASSWORD);
1163 }
1164 } else {
1165 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1166 bdrv_get_encrypted_filename(bs));
1167 }
1168 } else if (password) {
1169 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1170 }
1171}
1172
1173void qmp_change_blockdev(const char *device, const char *filename,
1174 bool has_format, const char *format, Error **errp)
666daa68
MA
1175{
1176 BlockDriverState *bs;
1177 BlockDriver *drv = NULL;
1178 int bdrv_flags;
92d48558 1179 Error *err = NULL;
666daa68
MA
1180
1181 bs = bdrv_find(device);
1182 if (!bs) {
333a96ec
LC
1183 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1184 return;
666daa68 1185 }
333a96ec
LC
1186
1187 if (format) {
b64ec4e4 1188 drv = bdrv_find_whitelisted_format(format, bs->read_only);
666daa68 1189 if (!drv) {
333a96ec
LC
1190 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1191 return;
666daa68
MA
1192 }
1193 }
333a96ec 1194
92d48558
LC
1195 eject_device(bs, 0, &err);
1196 if (error_is_set(&err)) {
333a96ec
LC
1197 error_propagate(errp, err);
1198 return;
666daa68 1199 }
333a96ec 1200
528f7663 1201 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
199630b6 1202 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
333a96ec
LC
1203
1204 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
666daa68 1205}
9063f814 1206
727f005e 1207/* throttling disk I/O limits */
80047da5
LC
1208void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1209 int64_t bps_wr, int64_t iops, int64_t iops_rd,
1210 int64_t iops_wr, Error **errp)
727f005e
ZYW
1211{
1212 BlockIOLimit io_limits;
727f005e
ZYW
1213 BlockDriverState *bs;
1214
80047da5 1215 bs = bdrv_find(device);
727f005e 1216 if (!bs) {
80047da5
LC
1217 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1218 return;
727f005e
ZYW
1219 }
1220
80047da5
LC
1221 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
1222 io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
1223 io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
1224 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
1225 io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
1226 io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
727f005e 1227
c546194f 1228 if (!do_check_io_limits(&io_limits, errp)) {
80047da5 1229 return;
727f005e
ZYW
1230 }
1231
1232 bs->io_limits = io_limits;
727f005e
ZYW
1233
1234 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
1235 bdrv_io_limits_enable(bs);
1236 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
1237 bdrv_io_limits_disable(bs);
1238 } else {
1239 if (bs->block_timer) {
1240 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
1241 }
1242 }
727f005e
ZYW
1243}
1244
9063f814
RH
1245int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1246{
1247 const char *id = qdict_get_str(qdict, "id");
1248 BlockDriverState *bs;
9063f814
RH
1249
1250 bs = bdrv_find(id);
1251 if (!bs) {
1252 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1253 return -1;
1254 }
8591675f
MT
1255 if (bdrv_in_use(bs)) {
1256 qerror_report(QERR_DEVICE_IN_USE, id);
1257 return -1;
1258 }
9063f814
RH
1259
1260 /* quiesce block driver; prevent further io */
922453bc 1261 bdrv_drain_all();
9063f814
RH
1262 bdrv_flush(bs);
1263 bdrv_close(bs);
1264
fa879d62 1265 /* if we have a device attached to this BlockDriverState
d22b2f41
RH
1266 * then we need to make the drive anonymous until the device
1267 * can be removed. If this is a drive with no device backing
1268 * then we can just get rid of the block driver state right here.
1269 */
fa879d62 1270 if (bdrv_get_attached_dev(bs)) {
d22b2f41 1271 bdrv_make_anon(bs);
293c51a6
SH
1272
1273 /* Further I/O must not pause the guest */
1274 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
1275 BLOCKDEV_ON_ERROR_REPORT);
d22b2f41
RH
1276 } else {
1277 drive_uninit(drive_get_by_blockdev(bs));
9063f814
RH
1278 }
1279
9063f814
RH
1280 return 0;
1281}
6d4a2b3a 1282
5e7caacb 1283void qmp_block_resize(const char *device, int64_t size, Error **errp)
6d4a2b3a 1284{
6d4a2b3a 1285 BlockDriverState *bs;
8732901e 1286 int ret;
6d4a2b3a
CH
1287
1288 bs = bdrv_find(device);
1289 if (!bs) {
5e7caacb
LC
1290 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1291 return;
6d4a2b3a
CH
1292 }
1293
1294 if (size < 0) {
939a1cc3 1295 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
5e7caacb 1296 return;
6d4a2b3a
CH
1297 }
1298
92b7a08d
PL
1299 /* complete all in-flight operations before resizing the device */
1300 bdrv_drain_all();
1301
8732901e
KW
1302 ret = bdrv_truncate(bs, size);
1303 switch (ret) {
939a1cc3
SH
1304 case 0:
1305 break;
1306 case -ENOMEDIUM:
1307 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1308 break;
1309 case -ENOTSUP:
1310 error_set(errp, QERR_UNSUPPORTED);
1311 break;
1312 case -EACCES:
1313 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1314 break;
1315 case -EBUSY:
1316 error_set(errp, QERR_DEVICE_IN_USE, device);
1317 break;
1318 default:
8732901e 1319 error_setg_errno(errp, -ret, "Could not resize");
939a1cc3 1320 break;
6d4a2b3a 1321 }
6d4a2b3a 1322}
12bd451f 1323
9abf2dba 1324static void block_job_cb(void *opaque, int ret)
12bd451f
SH
1325{
1326 BlockDriverState *bs = opaque;
1327 QObject *obj;
1328
9abf2dba 1329 trace_block_job_cb(bs, bs->job, ret);
12bd451f
SH
1330
1331 assert(bs->job);
1332 obj = qobject_from_block_job(bs->job);
1333 if (ret < 0) {
1334 QDict *dict = qobject_to_qdict(obj);
1335 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1336 }
1337
370521a1
SH
1338 if (block_job_is_cancelled(bs->job)) {
1339 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1340 } else {
1341 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1342 }
12bd451f 1343 qobject_decref(obj);
aa398a5c
SH
1344
1345 drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
12bd451f
SH
1346}
1347
1348void qmp_block_stream(const char *device, bool has_base,
1d809098
PB
1349 const char *base, bool has_speed, int64_t speed,
1350 bool has_on_error, BlockdevOnError on_error,
1351 Error **errp)
12bd451f
SH
1352{
1353 BlockDriverState *bs;
c8c3080f 1354 BlockDriverState *base_bs = NULL;
fd7f8c65 1355 Error *local_err = NULL;
12bd451f 1356
1d809098
PB
1357 if (!has_on_error) {
1358 on_error = BLOCKDEV_ON_ERROR_REPORT;
1359 }
1360
12bd451f
SH
1361 bs = bdrv_find(device);
1362 if (!bs) {
1363 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1364 return;
1365 }
1366
12bd451f 1367 if (base) {
c8c3080f
MT
1368 base_bs = bdrv_find_backing_image(bs, base);
1369 if (base_bs == NULL) {
1370 error_set(errp, QERR_BASE_NOT_FOUND, base);
1371 return;
1372 }
12bd451f
SH
1373 }
1374
c83c66c3 1375 stream_start(bs, base_bs, base, has_speed ? speed : 0,
1d809098 1376 on_error, block_job_cb, bs, &local_err);
fd7f8c65
SH
1377 if (error_is_set(&local_err)) {
1378 error_propagate(errp, local_err);
1379 return;
12bd451f
SH
1380 }
1381
aa398a5c
SH
1382 /* Grab a reference so hotplug does not delete the BlockDriverState from
1383 * underneath us.
1384 */
1385 drive_get_ref(drive_get_by_blockdev(bs));
1386
12bd451f
SH
1387 trace_qmp_block_stream(bs, bs->job);
1388}
2d47c6e9 1389
ed61fc10
JC
1390void qmp_block_commit(const char *device,
1391 bool has_base, const char *base, const char *top,
1392 bool has_speed, int64_t speed,
1393 Error **errp)
1394{
1395 BlockDriverState *bs;
1396 BlockDriverState *base_bs, *top_bs;
1397 Error *local_err = NULL;
1398 /* This will be part of the QMP command, if/when the
1399 * BlockdevOnError change for blkmirror makes it in
1400 */
92aa5c6d 1401 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
ed61fc10
JC
1402
1403 /* drain all i/o before commits */
1404 bdrv_drain_all();
1405
1406 bs = bdrv_find(device);
1407 if (!bs) {
1408 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1409 return;
1410 }
ed61fc10
JC
1411
1412 /* default top_bs is the active layer */
1413 top_bs = bs;
1414
1415 if (top) {
1416 if (strcmp(bs->filename, top) != 0) {
1417 top_bs = bdrv_find_backing_image(bs, top);
1418 }
1419 }
1420
1421 if (top_bs == NULL) {
1422 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1423 return;
1424 }
1425
d5208c45
JC
1426 if (has_base && base) {
1427 base_bs = bdrv_find_backing_image(top_bs, base);
1428 } else {
1429 base_bs = bdrv_find_base(top_bs);
1430 }
1431
1432 if (base_bs == NULL) {
1433 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1434 return;
1435 }
1436
ed61fc10
JC
1437 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1438 &local_err);
1439 if (local_err != NULL) {
1440 error_propagate(errp, local_err);
1441 return;
1442 }
1443 /* Grab a reference so hotplug does not delete the BlockDriverState from
1444 * underneath us.
1445 */
1446 drive_get_ref(drive_get_by_blockdev(bs));
1447}
1448
99a9addf
SH
1449void qmp_drive_backup(const char *device, const char *target,
1450 bool has_format, const char *format,
b53169ea 1451 enum MirrorSyncMode sync,
99a9addf
SH
1452 bool has_mode, enum NewImageMode mode,
1453 bool has_speed, int64_t speed,
1454 bool has_on_source_error, BlockdevOnError on_source_error,
1455 bool has_on_target_error, BlockdevOnError on_target_error,
1456 Error **errp)
1457{
1458 BlockDriverState *bs;
1459 BlockDriverState *target_bs;
1460 BlockDriver *drv = NULL;
1461 Error *local_err = NULL;
1462 int flags;
1463 int64_t size;
1464 int ret;
1465
b53169ea
SH
1466 if (sync != MIRROR_SYNC_MODE_FULL) {
1467 error_setg(errp, "only sync mode 'full' is currently supported");
1468 return;
1469 }
99a9addf
SH
1470 if (!has_speed) {
1471 speed = 0;
1472 }
1473 if (!has_on_source_error) {
1474 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1475 }
1476 if (!has_on_target_error) {
1477 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1478 }
1479 if (!has_mode) {
1480 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1481 }
1482
1483 bs = bdrv_find(device);
1484 if (!bs) {
1485 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1486 return;
1487 }
1488
1489 if (!bdrv_is_inserted(bs)) {
1490 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1491 return;
1492 }
1493
1494 if (!has_format) {
1495 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1496 }
1497 if (format) {
1498 drv = bdrv_find_format(format);
1499 if (!drv) {
1500 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1501 return;
1502 }
1503 }
1504
1505 if (bdrv_in_use(bs)) {
1506 error_set(errp, QERR_DEVICE_IN_USE, device);
1507 return;
1508 }
1509
1510 flags = bs->open_flags | BDRV_O_RDWR;
1511
1512 size = bdrv_getlength(bs);
1513 if (size < 0) {
1514 error_setg_errno(errp, -size, "bdrv_getlength failed");
1515 return;
1516 }
1517
1518 if (mode != NEW_IMAGE_MODE_EXISTING) {
1519 assert(format && drv);
1520 bdrv_img_create(target, format,
1521 NULL, NULL, NULL, size, flags, &local_err, false);
1522 }
1523
1524 if (error_is_set(&local_err)) {
1525 error_propagate(errp, local_err);
1526 return;
1527 }
1528
1529 target_bs = bdrv_new("");
1530 ret = bdrv_open(target_bs, target, NULL, flags, drv);
1531 if (ret < 0) {
1532 bdrv_delete(target_bs);
1533 error_setg_file_open(errp, -ret, target);
1534 return;
1535 }
1536
1537 backup_start(bs, target_bs, speed, on_source_error, on_target_error,
1538 block_job_cb, bs, &local_err);
1539 if (local_err != NULL) {
1540 bdrv_delete(target_bs);
1541 error_propagate(errp, local_err);
1542 return;
1543 }
1544
1545 /* Grab a reference so hotplug does not delete the BlockDriverState from
1546 * underneath us.
1547 */
1548 drive_get_ref(drive_get_by_blockdev(bs));
1549}
1550
08e4ed6c
PB
1551#define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1552
d9b902db
PB
1553void qmp_drive_mirror(const char *device, const char *target,
1554 bool has_format, const char *format,
1555 enum MirrorSyncMode sync,
1556 bool has_mode, enum NewImageMode mode,
b952b558 1557 bool has_speed, int64_t speed,
eee13dfe 1558 bool has_granularity, uint32_t granularity,
08e4ed6c 1559 bool has_buf_size, int64_t buf_size,
b952b558
PB
1560 bool has_on_source_error, BlockdevOnError on_source_error,
1561 bool has_on_target_error, BlockdevOnError on_target_error,
1562 Error **errp)
d9b902db 1563{
d9b902db
PB
1564 BlockDriverState *bs;
1565 BlockDriverState *source, *target_bs;
d9b902db
PB
1566 BlockDriver *drv = NULL;
1567 Error *local_err = NULL;
1568 int flags;
ac3c5d83 1569 int64_t size;
d9b902db
PB
1570 int ret;
1571
1572 if (!has_speed) {
1573 speed = 0;
1574 }
b952b558
PB
1575 if (!has_on_source_error) {
1576 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1577 }
1578 if (!has_on_target_error) {
1579 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1580 }
d9b902db
PB
1581 if (!has_mode) {
1582 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1583 }
eee13dfe
PB
1584 if (!has_granularity) {
1585 granularity = 0;
1586 }
08e4ed6c
PB
1587 if (!has_buf_size) {
1588 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1589 }
1590
eee13dfe
PB
1591 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1592 error_set(errp, QERR_INVALID_PARAMETER, device);
1593 return;
1594 }
1595 if (granularity & (granularity - 1)) {
1596 error_set(errp, QERR_INVALID_PARAMETER, device);
1597 return;
1598 }
d9b902db
PB
1599
1600 bs = bdrv_find(device);
1601 if (!bs) {
1602 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1603 return;
1604 }
1605
1606 if (!bdrv_is_inserted(bs)) {
1607 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1608 return;
1609 }
1610
1611 if (!has_format) {
1612 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1613 }
1614 if (format) {
1615 drv = bdrv_find_format(format);
1616 if (!drv) {
1617 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1618 return;
1619 }
1620 }
1621
1622 if (bdrv_in_use(bs)) {
1623 error_set(errp, QERR_DEVICE_IN_USE, device);
1624 return;
1625 }
1626
1627 flags = bs->open_flags | BDRV_O_RDWR;
1628 source = bs->backing_hd;
1629 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1630 sync = MIRROR_SYNC_MODE_FULL;
1631 }
1632
ac3c5d83
SH
1633 size = bdrv_getlength(bs);
1634 if (size < 0) {
1635 error_setg_errno(errp, -size, "bdrv_getlength failed");
1636 return;
1637 }
1638
d9b902db
PB
1639 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1640 /* create new image w/o backing file */
1641 assert(format && drv);
cf8f2426 1642 bdrv_img_create(target, format,
f382d43a 1643 NULL, NULL, NULL, size, flags, &local_err, false);
d9b902db
PB
1644 } else {
1645 switch (mode) {
1646 case NEW_IMAGE_MODE_EXISTING:
1647 ret = 0;
1648 break;
1649 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1650 /* create new image with backing file */
cf8f2426
LC
1651 bdrv_img_create(target, format,
1652 source->filename,
1653 source->drv->format_name,
f382d43a 1654 NULL, size, flags, &local_err, false);
d9b902db
PB
1655 break;
1656 default:
1657 abort();
1658 }
1659 }
1660
cf8f2426
LC
1661 if (error_is_set(&local_err)) {
1662 error_propagate(errp, local_err);
d9b902db
PB
1663 return;
1664 }
1665
b812f671
PB
1666 /* Mirroring takes care of copy-on-write using the source's backing
1667 * file.
1668 */
d9b902db 1669 target_bs = bdrv_new("");
de9c0cec 1670 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
d9b902db
PB
1671 if (ret < 0) {
1672 bdrv_delete(target_bs);
0eef407c 1673 error_setg_file_open(errp, -ret, target);
d9b902db
PB
1674 return;
1675 }
1676
08e4ed6c 1677 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
eee13dfe 1678 on_source_error, on_target_error,
b952b558 1679 block_job_cb, bs, &local_err);
d9b902db
PB
1680 if (local_err != NULL) {
1681 bdrv_delete(target_bs);
1682 error_propagate(errp, local_err);
1683 return;
1684 }
1685
1686 /* Grab a reference so hotplug does not delete the BlockDriverState from
1687 * underneath us.
1688 */
1689 drive_get_ref(drive_get_by_blockdev(bs));
1690}
1691
2d47c6e9
SH
1692static BlockJob *find_block_job(const char *device)
1693{
1694 BlockDriverState *bs;
1695
1696 bs = bdrv_find(device);
1697 if (!bs || !bs->job) {
1698 return NULL;
1699 }
1700 return bs->job;
1701}
1702
882ec7ce 1703void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2d47c6e9
SH
1704{
1705 BlockJob *job = find_block_job(device);
1706
1707 if (!job) {
7ef15070 1708 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2d47c6e9
SH
1709 return;
1710 }
1711
882ec7ce 1712 block_job_set_speed(job, speed, errp);
2d47c6e9 1713}
370521a1 1714
6e37fb81
PB
1715void qmp_block_job_cancel(const char *device,
1716 bool has_force, bool force, Error **errp)
370521a1
SH
1717{
1718 BlockJob *job = find_block_job(device);
1719
6e37fb81
PB
1720 if (!has_force) {
1721 force = false;
1722 }
1723
370521a1 1724 if (!job) {
7ef15070 1725 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
370521a1
SH
1726 return;
1727 }
6e37fb81 1728 if (job->paused && !force) {
8acc72a4
PB
1729 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
1730 return;
1731 }
370521a1
SH
1732
1733 trace_qmp_block_job_cancel(job);
1734 block_job_cancel(job);
1735}
fb5458cd 1736
6e37fb81
PB
1737void qmp_block_job_pause(const char *device, Error **errp)
1738{
1739 BlockJob *job = find_block_job(device);
1740
1741 if (!job) {
1742 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1743 return;
1744 }
1745
1746 trace_qmp_block_job_pause(job);
1747 block_job_pause(job);
1748}
1749
1750void qmp_block_job_resume(const char *device, Error **errp)
1751{
1752 BlockJob *job = find_block_job(device);
1753
1754 if (!job) {
1755 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1756 return;
1757 }
1758
1759 trace_qmp_block_job_resume(job);
1760 block_job_resume(job);
1761}
1762
aeae883b
PB
1763void qmp_block_job_complete(const char *device, Error **errp)
1764{
1765 BlockJob *job = find_block_job(device);
1766
1767 if (!job) {
1768 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1769 return;
1770 }
1771
1772 trace_qmp_block_job_complete(job);
1773 block_job_complete(job, errp);
1774}
1775
fb5458cd
SH
1776static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1777{
1778 BlockJobInfoList **prev = opaque;
1779 BlockJob *job = bs->job;
1780
1781 if (job) {
30e628b7
PB
1782 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
1783 elem->value = block_job_query(bs->job);
fb5458cd
SH
1784 (*prev)->next = elem;
1785 *prev = elem;
1786 }
1787}
1788
1789BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1790{
1791 /* Dummy is a fake list element for holding the head pointer */
1792 BlockJobInfoList dummy = {};
1793 BlockJobInfoList *prev = &dummy;
1794 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1795 return dummy.next;
1796}
4d454574 1797
0006383e 1798QemuOptsList qemu_common_drive_opts = {
4d454574 1799 .name = "drive",
0006383e 1800 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4d454574
PB
1801 .desc = {
1802 {
1803 .name = "bus",
1804 .type = QEMU_OPT_NUMBER,
1805 .help = "bus number",
1806 },{
1807 .name = "unit",
1808 .type = QEMU_OPT_NUMBER,
1809 .help = "unit number (i.e. lun for scsi)",
1810 },{
1811 .name = "if",
1812 .type = QEMU_OPT_STRING,
1813 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1814 },{
1815 .name = "index",
1816 .type = QEMU_OPT_NUMBER,
1817 .help = "index number",
1818 },{
1819 .name = "cyls",
1820 .type = QEMU_OPT_NUMBER,
1821 .help = "number of cylinders (ide disk geometry)",
1822 },{
1823 .name = "heads",
1824 .type = QEMU_OPT_NUMBER,
1825 .help = "number of heads (ide disk geometry)",
1826 },{
1827 .name = "secs",
1828 .type = QEMU_OPT_NUMBER,
1829 .help = "number of sectors (ide disk geometry)",
1830 },{
1831 .name = "trans",
1832 .type = QEMU_OPT_STRING,
1833 .help = "chs translation (auto, lba. none)",
1834 },{
1835 .name = "media",
1836 .type = QEMU_OPT_STRING,
1837 .help = "media type (disk, cdrom)",
1838 },{
1839 .name = "snapshot",
1840 .type = QEMU_OPT_BOOL,
1841 .help = "enable/disable snapshot mode",
1842 },{
1843 .name = "file",
1844 .type = QEMU_OPT_STRING,
1845 .help = "disk image",
a9384aff
PB
1846 },{
1847 .name = "discard",
1848 .type = QEMU_OPT_STRING,
1849 .help = "discard operation (ignore/off, unmap/on)",
4d454574
PB
1850 },{
1851 .name = "cache",
1852 .type = QEMU_OPT_STRING,
1853 .help = "host cache usage (none, writeback, writethrough, "
1854 "directsync, unsafe)",
1855 },{
1856 .name = "aio",
1857 .type = QEMU_OPT_STRING,
1858 .help = "host AIO implementation (threads, native)",
1859 },{
1860 .name = "format",
1861 .type = QEMU_OPT_STRING,
1862 .help = "disk format (raw, qcow2, ...)",
1863 },{
1864 .name = "serial",
1865 .type = QEMU_OPT_STRING,
1866 .help = "disk serial number",
1867 },{
1868 .name = "rerror",
1869 .type = QEMU_OPT_STRING,
1870 .help = "read error action",
1871 },{
1872 .name = "werror",
1873 .type = QEMU_OPT_STRING,
1874 .help = "write error action",
1875 },{
1876 .name = "addr",
1877 .type = QEMU_OPT_STRING,
1878 .help = "pci address (virtio only)",
1879 },{
1880 .name = "readonly",
1881 .type = QEMU_OPT_BOOL,
1882 .help = "open drive file as read-only",
1883 },{
57975222 1884 .name = "throttling.iops-total",
4d454574
PB
1885 .type = QEMU_OPT_NUMBER,
1886 .help = "limit total I/O operations per second",
1887 },{
57975222 1888 .name = "throttling.iops-read",
4d454574
PB
1889 .type = QEMU_OPT_NUMBER,
1890 .help = "limit read operations per second",
1891 },{
57975222 1892 .name = "throttling.iops-write",
4d454574
PB
1893 .type = QEMU_OPT_NUMBER,
1894 .help = "limit write operations per second",
1895 },{
57975222 1896 .name = "throttling.bps-total",
4d454574
PB
1897 .type = QEMU_OPT_NUMBER,
1898 .help = "limit total bytes per second",
1899 },{
57975222 1900 .name = "throttling.bps-read",
4d454574
PB
1901 .type = QEMU_OPT_NUMBER,
1902 .help = "limit read bytes per second",
1903 },{
57975222 1904 .name = "throttling.bps-write",
4d454574
PB
1905 .type = QEMU_OPT_NUMBER,
1906 .help = "limit write bytes per second",
1907 },{
1908 .name = "copy-on-read",
1909 .type = QEMU_OPT_BOOL,
1910 .help = "copy read data from backing file into image file",
1911 },{
1912 .name = "boot",
1913 .type = QEMU_OPT_BOOL,
1914 .help = "(deprecated, ignored)",
1915 },
1916 { /* end of list */ }
1917 },
1918};
0006383e
KW
1919
1920QemuOptsList qemu_drive_opts = {
1921 .name = "drive",
1922 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
1923 .desc = {
492fdc6f
KW
1924 /*
1925 * no elements => accept any params
1926 * validation will happen later
1927 */
0006383e
KW
1928 { /* end of list */ }
1929 },
1930};