]> git.proxmox.com Git - qemu.git/blame - blockdev.c
block: Make find_image_format safe with NULL filename
[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"
2b584959 34#include "hw/block-common.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
MA
479 }
480 drv = bdrv_find_whitelisted_format(buf);
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) {
319ae529 661 return dinfo;
666daa68
MA
662 }
663 if (snapshot) {
664 /* always use cache=unsafe with snapshot */
665 bdrv_flags &= ~BDRV_O_CACHE_MASK;
666 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
667 }
668
fb0490f6
SH
669 if (copy_on_read) {
670 bdrv_flags |= BDRV_O_COPY_ON_READ;
671 }
672
ed9d4205
BC
673 if (runstate_check(RUN_STATE_INMIGRATE)) {
674 bdrv_flags |= BDRV_O_INCOMING;
675 }
676
666daa68
MA
677 if (media == MEDIA_CDROM) {
678 /* CDROM is fine for any interface, don't check. */
679 ro = 1;
680 } else if (ro == 1) {
1e9eb78a
JJ
681 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
682 type != IF_NONE && type != IF_PFLASH) {
807105a7 683 error_report("readonly not supported by this bus type");
a9ae2bff 684 goto err;
666daa68
MA
685 }
686 }
687
688 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
689
04d4abe9
SH
690 if (ro && copy_on_read) {
691 error_report("warning: disabling copy_on_read on readonly drive");
692 }
693
0006383e
KW
694 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv);
695 bs_opts = NULL;
696
666daa68 697 if (ret < 0) {
02582abd
SW
698 if (ret == -EMEDIUMTYPE) {
699 error_report("could not open disk image %s: not in %s format",
700 file, drv->format_name);
701 } else {
702 error_report("could not open disk image %s: %s",
703 file, strerror(-ret));
704 }
a9ae2bff 705 goto err;
666daa68
MA
706 }
707
708 if (bdrv_key_required(dinfo->bdrv))
709 autostart = 0;
0006383e
KW
710
711 qemu_opts_del(opts);
712
666daa68 713 return dinfo;
a9ae2bff
MA
714
715err:
0006383e
KW
716 qemu_opts_del(opts);
717 QDECREF(bs_opts);
a9ae2bff 718 bdrv_delete(dinfo->bdrv);
7267c094 719 g_free(dinfo->id);
a9ae2bff 720 QTAILQ_REMOVE(&drives, dinfo, next);
7267c094 721 g_free(dinfo);
a9ae2bff 722 return NULL;
666daa68
MA
723}
724
725void do_commit(Monitor *mon, const QDict *qdict)
726{
666daa68 727 const char *device = qdict_get_str(qdict, "device");
6ab4b5ab 728 BlockDriverState *bs;
e8877497 729 int ret;
666daa68 730
6ab4b5ab 731 if (!strcmp(device, "all")) {
e8877497 732 ret = bdrv_commit_all();
6ab4b5ab
MA
733 } else {
734 bs = bdrv_find(device);
ac59eb95 735 if (!bs) {
58513bde 736 monitor_printf(mon, "Device '%s' not found\n", device);
ac59eb95 737 return;
6ab4b5ab 738 }
2d3735d3 739 ret = bdrv_commit(bs);
58513bde
JC
740 }
741 if (ret < 0) {
742 monitor_printf(mon, "'commit' error for '%s': %s\n", device,
743 strerror(-ret));
666daa68
MA
744 }
745}
746
6cc2a415
PB
747static void blockdev_do_action(int kind, void *data, Error **errp)
748{
749 BlockdevAction action;
750 BlockdevActionList list;
751
752 action.kind = kind;
753 action.data = data;
754 list.value = &action;
755 list.next = NULL;
756 qmp_transaction(&list, errp);
757}
758
6106e249
LC
759void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
760 bool has_format, const char *format,
6cc2a415 761 bool has_mode, enum NewImageMode mode,
6106e249 762 Error **errp)
f8882568 763{
6cc2a415
PB
764 BlockdevSnapshot snapshot = {
765 .device = (char *) device,
766 .snapshot_file = (char *) snapshot_file,
767 .has_format = has_format,
768 .format = (char *) format,
769 .has_mode = has_mode,
770 .mode = mode,
771 };
772 blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
773 errp);
f8882568
JS
774}
775
8802d1fd
JC
776
777/* New and old BlockDriverState structs for group snapshots */
52e7c241 778typedef struct BlkTransactionStates {
8802d1fd
JC
779 BlockDriverState *old_bs;
780 BlockDriverState *new_bs;
52e7c241
PB
781 QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
782} BlkTransactionStates;
8802d1fd
JC
783
784/*
785 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
786 * then we do not pivot any of the devices in the group, and abandon the
787 * snapshots
788 */
52e7c241 789void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
8802d1fd
JC
790{
791 int ret = 0;
52e7c241
PB
792 BlockdevActionList *dev_entry = dev_list;
793 BlkTransactionStates *states, *next;
43e17041 794 Error *local_err = NULL;
8802d1fd 795
52e7c241 796 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
8802d1fd
JC
797 QSIMPLEQ_INIT(&snap_bdrv_states);
798
799 /* drain all i/o before any snapshots */
800 bdrv_drain_all();
801
802 /* We don't do anything in this loop that commits us to the snapshot */
803 while (NULL != dev_entry) {
52e7c241
PB
804 BlockdevAction *dev_info = NULL;
805 BlockDriver *proto_drv;
806 BlockDriver *drv;
807 int flags;
bc8b094f
PB
808 enum NewImageMode mode;
809 const char *new_image_file;
52e7c241
PB
810 const char *device;
811 const char *format = "qcow2";
52e7c241 812
8802d1fd
JC
813 dev_info = dev_entry->value;
814 dev_entry = dev_entry->next;
815
52e7c241 816 states = g_malloc0(sizeof(BlkTransactionStates));
8802d1fd
JC
817 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
818
52e7c241
PB
819 switch (dev_info->kind) {
820 case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
821 device = dev_info->blockdev_snapshot_sync->device;
bc8b094f
PB
822 if (!dev_info->blockdev_snapshot_sync->has_mode) {
823 dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
824 }
825 new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
52e7c241
PB
826 if (dev_info->blockdev_snapshot_sync->has_format) {
827 format = dev_info->blockdev_snapshot_sync->format;
828 }
bc8b094f 829 mode = dev_info->blockdev_snapshot_sync->mode;
52e7c241
PB
830 break;
831 default:
832 abort();
833 }
8802d1fd 834
52e7c241
PB
835 drv = bdrv_find_format(format);
836 if (!drv) {
837 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
838 goto delete_and_fail;
839 }
840
841 states->old_bs = bdrv_find(device);
8802d1fd 842 if (!states->old_bs) {
52e7c241 843 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
8802d1fd
JC
844 goto delete_and_fail;
845 }
846
e86fe18a
PB
847 if (!bdrv_is_inserted(states->old_bs)) {
848 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
849 goto delete_and_fail;
850 }
851
8802d1fd 852 if (bdrv_in_use(states->old_bs)) {
52e7c241 853 error_set(errp, QERR_DEVICE_IN_USE, device);
8802d1fd
JC
854 goto delete_and_fail;
855 }
856
e86fe18a 857 if (!bdrv_is_read_only(states->old_bs)) {
8802d1fd
JC
858 if (bdrv_flush(states->old_bs)) {
859 error_set(errp, QERR_IO_ERROR);
860 goto delete_and_fail;
861 }
862 }
863
8802d1fd
JC
864 flags = states->old_bs->open_flags;
865
52e7c241 866 proto_drv = bdrv_find_protocol(new_image_file);
8802d1fd
JC
867 if (!proto_drv) {
868 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
869 goto delete_and_fail;
870 }
871
872 /* create new image w/backing file */
bc8b094f 873 if (mode != NEW_IMAGE_MODE_EXISTING) {
43e17041
LC
874 bdrv_img_create(new_image_file, format,
875 states->old_bs->filename,
876 states->old_bs->drv->format_name,
f382d43a 877 NULL, -1, flags, &local_err, false);
43e17041
LC
878 if (error_is_set(&local_err)) {
879 error_propagate(errp, local_err);
bc8b094f
PB
880 goto delete_and_fail;
881 }
8802d1fd
JC
882 }
883
884 /* We will manually add the backing_hd field to the bs later */
885 states->new_bs = bdrv_new("");
de9c0cec
KW
886 /* TODO Inherit bs->options or only take explicit options with an
887 * extended QMP command? */
888 ret = bdrv_open(states->new_bs, new_image_file, NULL,
8802d1fd
JC
889 flags | BDRV_O_NO_BACKING, drv);
890 if (ret != 0) {
52e7c241 891 error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
8802d1fd
JC
892 goto delete_and_fail;
893 }
894 }
895
896
897 /* Now we are going to do the actual pivot. Everything up to this point
898 * is reversible, but we are committed at this point */
899 QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
900 /* This removes our old bs from the bdrv_states, and adds the new bs */
901 bdrv_append(states->new_bs, states->old_bs);
870f5681
JC
902 /* We don't need (or want) to use the transactional
903 * bdrv_reopen_multiple() across all the entries at once, because we
904 * don't want to abort all of them if one of them fails the reopen */
905 bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR,
906 NULL);
8802d1fd
JC
907 }
908
909 /* success */
910 goto exit;
911
912delete_and_fail:
913 /*
914 * failure, and it is all-or-none; abandon each new bs, and keep using
915 * the original bs for all images
916 */
917 QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
918 if (states->new_bs) {
919 bdrv_delete(states->new_bs);
920 }
921 }
922exit:
622d2419 923 QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
8802d1fd
JC
924 g_free(states);
925 }
8802d1fd
JC
926}
927
928
92d48558 929static void eject_device(BlockDriverState *bs, int force, Error **errp)
666daa68 930{
2d3735d3
SH
931 if (bdrv_in_use(bs)) {
932 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
933 return;
934 }
2c6942fa 935 if (!bdrv_dev_has_removable_media(bs)) {
92d48558
LC
936 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
937 return;
ea8f942f 938 }
92d48558 939
025ccaa7
PB
940 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
941 bdrv_dev_eject_request(bs, force);
942 if (!force) {
92d48558
LC
943 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
944 return;
025ccaa7 945 }
666daa68 946 }
92d48558 947
3b5276b5 948 bdrv_close(bs);
666daa68
MA
949}
950
c245b6a3 951void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
666daa68
MA
952{
953 BlockDriverState *bs;
666daa68 954
c245b6a3 955 bs = bdrv_find(device);
666daa68 956 if (!bs) {
c245b6a3
LC
957 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
958 return;
92d48558
LC
959 }
960
c245b6a3 961 eject_device(bs, force, errp);
666daa68
MA
962}
963
a4dea8a9 964void qmp_block_passwd(const char *device, const char *password, Error **errp)
666daa68
MA
965{
966 BlockDriverState *bs;
967 int err;
968
a4dea8a9 969 bs = bdrv_find(device);
666daa68 970 if (!bs) {
a4dea8a9
LC
971 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
972 return;
666daa68
MA
973 }
974
a4dea8a9 975 err = bdrv_set_key(bs, password);
666daa68 976 if (err == -EINVAL) {
a4dea8a9
LC
977 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
978 return;
666daa68 979 } else if (err < 0) {
a4dea8a9
LC
980 error_set(errp, QERR_INVALID_PASSWORD);
981 return;
666daa68 982 }
666daa68
MA
983}
984
333a96ec
LC
985static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
986 int bdrv_flags, BlockDriver *drv,
987 const char *password, Error **errp)
988{
de9c0cec 989 if (bdrv_open(bs, filename, NULL, bdrv_flags, drv) < 0) {
333a96ec
LC
990 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
991 return;
992 }
993
994 if (bdrv_key_required(bs)) {
995 if (password) {
996 if (bdrv_set_key(bs, password) < 0) {
997 error_set(errp, QERR_INVALID_PASSWORD);
998 }
999 } else {
1000 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
1001 bdrv_get_encrypted_filename(bs));
1002 }
1003 } else if (password) {
1004 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1005 }
1006}
1007
1008void qmp_change_blockdev(const char *device, const char *filename,
1009 bool has_format, const char *format, Error **errp)
666daa68
MA
1010{
1011 BlockDriverState *bs;
1012 BlockDriver *drv = NULL;
1013 int bdrv_flags;
92d48558 1014 Error *err = NULL;
666daa68
MA
1015
1016 bs = bdrv_find(device);
1017 if (!bs) {
333a96ec
LC
1018 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1019 return;
666daa68 1020 }
333a96ec
LC
1021
1022 if (format) {
1023 drv = bdrv_find_whitelisted_format(format);
666daa68 1024 if (!drv) {
333a96ec
LC
1025 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1026 return;
666daa68
MA
1027 }
1028 }
333a96ec 1029
92d48558
LC
1030 eject_device(bs, 0, &err);
1031 if (error_is_set(&err)) {
333a96ec
LC
1032 error_propagate(errp, err);
1033 return;
666daa68 1034 }
333a96ec 1035
528f7663 1036 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
199630b6 1037 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
333a96ec
LC
1038
1039 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
666daa68 1040}
9063f814 1041
727f005e 1042/* throttling disk I/O limits */
80047da5
LC
1043void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
1044 int64_t bps_wr, int64_t iops, int64_t iops_rd,
1045 int64_t iops_wr, Error **errp)
727f005e
ZYW
1046{
1047 BlockIOLimit io_limits;
727f005e
ZYW
1048 BlockDriverState *bs;
1049
80047da5 1050 bs = bdrv_find(device);
727f005e 1051 if (!bs) {
80047da5
LC
1052 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1053 return;
727f005e
ZYW
1054 }
1055
80047da5
LC
1056 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
1057 io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
1058 io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
1059 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
1060 io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
1061 io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
727f005e 1062
c546194f 1063 if (!do_check_io_limits(&io_limits, errp)) {
80047da5 1064 return;
727f005e
ZYW
1065 }
1066
1067 bs->io_limits = io_limits;
1068 bs->slice_time = BLOCK_IO_SLICE_TIME;
1069
1070 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
1071 bdrv_io_limits_enable(bs);
1072 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
1073 bdrv_io_limits_disable(bs);
1074 } else {
1075 if (bs->block_timer) {
1076 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
1077 }
1078 }
727f005e
ZYW
1079}
1080
9063f814
RH
1081int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1082{
1083 const char *id = qdict_get_str(qdict, "id");
1084 BlockDriverState *bs;
9063f814
RH
1085
1086 bs = bdrv_find(id);
1087 if (!bs) {
1088 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1089 return -1;
1090 }
8591675f
MT
1091 if (bdrv_in_use(bs)) {
1092 qerror_report(QERR_DEVICE_IN_USE, id);
1093 return -1;
1094 }
9063f814
RH
1095
1096 /* quiesce block driver; prevent further io */
922453bc 1097 bdrv_drain_all();
9063f814
RH
1098 bdrv_flush(bs);
1099 bdrv_close(bs);
1100
fa879d62 1101 /* if we have a device attached to this BlockDriverState
d22b2f41
RH
1102 * then we need to make the drive anonymous until the device
1103 * can be removed. If this is a drive with no device backing
1104 * then we can just get rid of the block driver state right here.
1105 */
fa879d62 1106 if (bdrv_get_attached_dev(bs)) {
d22b2f41
RH
1107 bdrv_make_anon(bs);
1108 } else {
1109 drive_uninit(drive_get_by_blockdev(bs));
9063f814
RH
1110 }
1111
9063f814
RH
1112 return 0;
1113}
6d4a2b3a 1114
5e7caacb 1115void qmp_block_resize(const char *device, int64_t size, Error **errp)
6d4a2b3a 1116{
6d4a2b3a
CH
1117 BlockDriverState *bs;
1118
1119 bs = bdrv_find(device);
1120 if (!bs) {
5e7caacb
LC
1121 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1122 return;
6d4a2b3a
CH
1123 }
1124
1125 if (size < 0) {
939a1cc3 1126 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
5e7caacb 1127 return;
6d4a2b3a
CH
1128 }
1129
92b7a08d
PL
1130 /* complete all in-flight operations before resizing the device */
1131 bdrv_drain_all();
1132
939a1cc3
SH
1133 switch (bdrv_truncate(bs, size)) {
1134 case 0:
1135 break;
1136 case -ENOMEDIUM:
1137 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1138 break;
1139 case -ENOTSUP:
1140 error_set(errp, QERR_UNSUPPORTED);
1141 break;
1142 case -EACCES:
1143 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
1144 break;
1145 case -EBUSY:
1146 error_set(errp, QERR_DEVICE_IN_USE, device);
1147 break;
1148 default:
5e7caacb 1149 error_set(errp, QERR_UNDEFINED_ERROR);
939a1cc3 1150 break;
6d4a2b3a 1151 }
6d4a2b3a 1152}
12bd451f 1153
9abf2dba 1154static void block_job_cb(void *opaque, int ret)
12bd451f
SH
1155{
1156 BlockDriverState *bs = opaque;
1157 QObject *obj;
1158
9abf2dba 1159 trace_block_job_cb(bs, bs->job, ret);
12bd451f
SH
1160
1161 assert(bs->job);
1162 obj = qobject_from_block_job(bs->job);
1163 if (ret < 0) {
1164 QDict *dict = qobject_to_qdict(obj);
1165 qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
1166 }
1167
370521a1
SH
1168 if (block_job_is_cancelled(bs->job)) {
1169 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
1170 } else {
1171 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
1172 }
12bd451f 1173 qobject_decref(obj);
aa398a5c
SH
1174
1175 drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
12bd451f
SH
1176}
1177
1178void qmp_block_stream(const char *device, bool has_base,
1d809098
PB
1179 const char *base, bool has_speed, int64_t speed,
1180 bool has_on_error, BlockdevOnError on_error,
1181 Error **errp)
12bd451f
SH
1182{
1183 BlockDriverState *bs;
c8c3080f 1184 BlockDriverState *base_bs = NULL;
fd7f8c65 1185 Error *local_err = NULL;
12bd451f 1186
1d809098
PB
1187 if (!has_on_error) {
1188 on_error = BLOCKDEV_ON_ERROR_REPORT;
1189 }
1190
12bd451f
SH
1191 bs = bdrv_find(device);
1192 if (!bs) {
1193 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1194 return;
1195 }
1196
12bd451f 1197 if (base) {
c8c3080f
MT
1198 base_bs = bdrv_find_backing_image(bs, base);
1199 if (base_bs == NULL) {
1200 error_set(errp, QERR_BASE_NOT_FOUND, base);
1201 return;
1202 }
12bd451f
SH
1203 }
1204
c83c66c3 1205 stream_start(bs, base_bs, base, has_speed ? speed : 0,
1d809098 1206 on_error, block_job_cb, bs, &local_err);
fd7f8c65
SH
1207 if (error_is_set(&local_err)) {
1208 error_propagate(errp, local_err);
1209 return;
12bd451f
SH
1210 }
1211
aa398a5c
SH
1212 /* Grab a reference so hotplug does not delete the BlockDriverState from
1213 * underneath us.
1214 */
1215 drive_get_ref(drive_get_by_blockdev(bs));
1216
12bd451f
SH
1217 trace_qmp_block_stream(bs, bs->job);
1218}
2d47c6e9 1219
ed61fc10
JC
1220void qmp_block_commit(const char *device,
1221 bool has_base, const char *base, const char *top,
1222 bool has_speed, int64_t speed,
1223 Error **errp)
1224{
1225 BlockDriverState *bs;
1226 BlockDriverState *base_bs, *top_bs;
1227 Error *local_err = NULL;
1228 /* This will be part of the QMP command, if/when the
1229 * BlockdevOnError change for blkmirror makes it in
1230 */
92aa5c6d 1231 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
ed61fc10
JC
1232
1233 /* drain all i/o before commits */
1234 bdrv_drain_all();
1235
1236 bs = bdrv_find(device);
1237 if (!bs) {
1238 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1239 return;
1240 }
ed61fc10
JC
1241
1242 /* default top_bs is the active layer */
1243 top_bs = bs;
1244
1245 if (top) {
1246 if (strcmp(bs->filename, top) != 0) {
1247 top_bs = bdrv_find_backing_image(bs, top);
1248 }
1249 }
1250
1251 if (top_bs == NULL) {
1252 error_setg(errp, "Top image file %s not found", top ? top : "NULL");
1253 return;
1254 }
1255
d5208c45
JC
1256 if (has_base && base) {
1257 base_bs = bdrv_find_backing_image(top_bs, base);
1258 } else {
1259 base_bs = bdrv_find_base(top_bs);
1260 }
1261
1262 if (base_bs == NULL) {
1263 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
1264 return;
1265 }
1266
ed61fc10
JC
1267 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
1268 &local_err);
1269 if (local_err != NULL) {
1270 error_propagate(errp, local_err);
1271 return;
1272 }
1273 /* Grab a reference so hotplug does not delete the BlockDriverState from
1274 * underneath us.
1275 */
1276 drive_get_ref(drive_get_by_blockdev(bs));
1277}
1278
08e4ed6c
PB
1279#define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1280
d9b902db
PB
1281void qmp_drive_mirror(const char *device, const char *target,
1282 bool has_format, const char *format,
1283 enum MirrorSyncMode sync,
1284 bool has_mode, enum NewImageMode mode,
b952b558 1285 bool has_speed, int64_t speed,
eee13dfe 1286 bool has_granularity, uint32_t granularity,
08e4ed6c 1287 bool has_buf_size, int64_t buf_size,
b952b558
PB
1288 bool has_on_source_error, BlockdevOnError on_source_error,
1289 bool has_on_target_error, BlockdevOnError on_target_error,
1290 Error **errp)
d9b902db 1291{
d9b902db
PB
1292 BlockDriverState *bs;
1293 BlockDriverState *source, *target_bs;
1294 BlockDriver *proto_drv;
1295 BlockDriver *drv = NULL;
1296 Error *local_err = NULL;
1297 int flags;
1298 uint64_t size;
1299 int ret;
1300
1301 if (!has_speed) {
1302 speed = 0;
1303 }
b952b558
PB
1304 if (!has_on_source_error) {
1305 on_source_error = BLOCKDEV_ON_ERROR_REPORT;
1306 }
1307 if (!has_on_target_error) {
1308 on_target_error = BLOCKDEV_ON_ERROR_REPORT;
1309 }
d9b902db
PB
1310 if (!has_mode) {
1311 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1312 }
eee13dfe
PB
1313 if (!has_granularity) {
1314 granularity = 0;
1315 }
08e4ed6c
PB
1316 if (!has_buf_size) {
1317 buf_size = DEFAULT_MIRROR_BUF_SIZE;
1318 }
1319
eee13dfe
PB
1320 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
1321 error_set(errp, QERR_INVALID_PARAMETER, device);
1322 return;
1323 }
1324 if (granularity & (granularity - 1)) {
1325 error_set(errp, QERR_INVALID_PARAMETER, device);
1326 return;
1327 }
d9b902db
PB
1328
1329 bs = bdrv_find(device);
1330 if (!bs) {
1331 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
1332 return;
1333 }
1334
1335 if (!bdrv_is_inserted(bs)) {
1336 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
1337 return;
1338 }
1339
1340 if (!has_format) {
1341 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
1342 }
1343 if (format) {
1344 drv = bdrv_find_format(format);
1345 if (!drv) {
1346 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1347 return;
1348 }
1349 }
1350
1351 if (bdrv_in_use(bs)) {
1352 error_set(errp, QERR_DEVICE_IN_USE, device);
1353 return;
1354 }
1355
1356 flags = bs->open_flags | BDRV_O_RDWR;
1357 source = bs->backing_hd;
1358 if (!source && sync == MIRROR_SYNC_MODE_TOP) {
1359 sync = MIRROR_SYNC_MODE_FULL;
1360 }
1361
1362 proto_drv = bdrv_find_protocol(target);
1363 if (!proto_drv) {
1364 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
1365 return;
1366 }
1367
86899072
VI
1368 bdrv_get_geometry(bs, &size);
1369 size *= 512;
d9b902db
PB
1370 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
1371 /* create new image w/o backing file */
1372 assert(format && drv);
cf8f2426 1373 bdrv_img_create(target, format,
f382d43a 1374 NULL, NULL, NULL, size, flags, &local_err, false);
d9b902db
PB
1375 } else {
1376 switch (mode) {
1377 case NEW_IMAGE_MODE_EXISTING:
1378 ret = 0;
1379 break;
1380 case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
1381 /* create new image with backing file */
cf8f2426
LC
1382 bdrv_img_create(target, format,
1383 source->filename,
1384 source->drv->format_name,
f382d43a 1385 NULL, size, flags, &local_err, false);
d9b902db
PB
1386 break;
1387 default:
1388 abort();
1389 }
1390 }
1391
cf8f2426
LC
1392 if (error_is_set(&local_err)) {
1393 error_propagate(errp, local_err);
d9b902db
PB
1394 return;
1395 }
1396
b812f671
PB
1397 /* Mirroring takes care of copy-on-write using the source's backing
1398 * file.
1399 */
d9b902db 1400 target_bs = bdrv_new("");
de9c0cec 1401 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv);
d9b902db
PB
1402
1403 if (ret < 0) {
1404 bdrv_delete(target_bs);
1405 error_set(errp, QERR_OPEN_FILE_FAILED, target);
1406 return;
1407 }
1408
08e4ed6c 1409 mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
eee13dfe 1410 on_source_error, on_target_error,
b952b558 1411 block_job_cb, bs, &local_err);
d9b902db
PB
1412 if (local_err != NULL) {
1413 bdrv_delete(target_bs);
1414 error_propagate(errp, local_err);
1415 return;
1416 }
1417
1418 /* Grab a reference so hotplug does not delete the BlockDriverState from
1419 * underneath us.
1420 */
1421 drive_get_ref(drive_get_by_blockdev(bs));
1422}
1423
2d47c6e9
SH
1424static BlockJob *find_block_job(const char *device)
1425{
1426 BlockDriverState *bs;
1427
1428 bs = bdrv_find(device);
1429 if (!bs || !bs->job) {
1430 return NULL;
1431 }
1432 return bs->job;
1433}
1434
882ec7ce 1435void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
2d47c6e9
SH
1436{
1437 BlockJob *job = find_block_job(device);
1438
1439 if (!job) {
7ef15070 1440 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
2d47c6e9
SH
1441 return;
1442 }
1443
882ec7ce 1444 block_job_set_speed(job, speed, errp);
2d47c6e9 1445}
370521a1 1446
6e37fb81
PB
1447void qmp_block_job_cancel(const char *device,
1448 bool has_force, bool force, Error **errp)
370521a1
SH
1449{
1450 BlockJob *job = find_block_job(device);
1451
6e37fb81
PB
1452 if (!has_force) {
1453 force = false;
1454 }
1455
370521a1 1456 if (!job) {
7ef15070 1457 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
370521a1
SH
1458 return;
1459 }
6e37fb81 1460 if (job->paused && !force) {
8acc72a4
PB
1461 error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
1462 return;
1463 }
370521a1
SH
1464
1465 trace_qmp_block_job_cancel(job);
1466 block_job_cancel(job);
1467}
fb5458cd 1468
6e37fb81
PB
1469void qmp_block_job_pause(const char *device, Error **errp)
1470{
1471 BlockJob *job = find_block_job(device);
1472
1473 if (!job) {
1474 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1475 return;
1476 }
1477
1478 trace_qmp_block_job_pause(job);
1479 block_job_pause(job);
1480}
1481
1482void qmp_block_job_resume(const char *device, Error **errp)
1483{
1484 BlockJob *job = find_block_job(device);
1485
1486 if (!job) {
1487 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1488 return;
1489 }
1490
1491 trace_qmp_block_job_resume(job);
1492 block_job_resume(job);
1493}
1494
aeae883b
PB
1495void qmp_block_job_complete(const char *device, Error **errp)
1496{
1497 BlockJob *job = find_block_job(device);
1498
1499 if (!job) {
1500 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
1501 return;
1502 }
1503
1504 trace_qmp_block_job_complete(job);
1505 block_job_complete(job, errp);
1506}
1507
fb5458cd
SH
1508static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
1509{
1510 BlockJobInfoList **prev = opaque;
1511 BlockJob *job = bs->job;
1512
1513 if (job) {
30e628b7
PB
1514 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
1515 elem->value = block_job_query(bs->job);
fb5458cd
SH
1516 (*prev)->next = elem;
1517 *prev = elem;
1518 }
1519}
1520
1521BlockJobInfoList *qmp_query_block_jobs(Error **errp)
1522{
1523 /* Dummy is a fake list element for holding the head pointer */
1524 BlockJobInfoList dummy = {};
1525 BlockJobInfoList *prev = &dummy;
1526 bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
1527 return dummy.next;
1528}
4d454574 1529
0006383e 1530QemuOptsList qemu_common_drive_opts = {
4d454574 1531 .name = "drive",
0006383e 1532 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
4d454574
PB
1533 .desc = {
1534 {
1535 .name = "bus",
1536 .type = QEMU_OPT_NUMBER,
1537 .help = "bus number",
1538 },{
1539 .name = "unit",
1540 .type = QEMU_OPT_NUMBER,
1541 .help = "unit number (i.e. lun for scsi)",
1542 },{
1543 .name = "if",
1544 .type = QEMU_OPT_STRING,
1545 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1546 },{
1547 .name = "index",
1548 .type = QEMU_OPT_NUMBER,
1549 .help = "index number",
1550 },{
1551 .name = "cyls",
1552 .type = QEMU_OPT_NUMBER,
1553 .help = "number of cylinders (ide disk geometry)",
1554 },{
1555 .name = "heads",
1556 .type = QEMU_OPT_NUMBER,
1557 .help = "number of heads (ide disk geometry)",
1558 },{
1559 .name = "secs",
1560 .type = QEMU_OPT_NUMBER,
1561 .help = "number of sectors (ide disk geometry)",
1562 },{
1563 .name = "trans",
1564 .type = QEMU_OPT_STRING,
1565 .help = "chs translation (auto, lba. none)",
1566 },{
1567 .name = "media",
1568 .type = QEMU_OPT_STRING,
1569 .help = "media type (disk, cdrom)",
1570 },{
1571 .name = "snapshot",
1572 .type = QEMU_OPT_BOOL,
1573 .help = "enable/disable snapshot mode",
1574 },{
1575 .name = "file",
1576 .type = QEMU_OPT_STRING,
1577 .help = "disk image",
a9384aff
PB
1578 },{
1579 .name = "discard",
1580 .type = QEMU_OPT_STRING,
1581 .help = "discard operation (ignore/off, unmap/on)",
4d454574
PB
1582 },{
1583 .name = "cache",
1584 .type = QEMU_OPT_STRING,
1585 .help = "host cache usage (none, writeback, writethrough, "
1586 "directsync, unsafe)",
1587 },{
1588 .name = "aio",
1589 .type = QEMU_OPT_STRING,
1590 .help = "host AIO implementation (threads, native)",
1591 },{
1592 .name = "format",
1593 .type = QEMU_OPT_STRING,
1594 .help = "disk format (raw, qcow2, ...)",
1595 },{
1596 .name = "serial",
1597 .type = QEMU_OPT_STRING,
1598 .help = "disk serial number",
1599 },{
1600 .name = "rerror",
1601 .type = QEMU_OPT_STRING,
1602 .help = "read error action",
1603 },{
1604 .name = "werror",
1605 .type = QEMU_OPT_STRING,
1606 .help = "write error action",
1607 },{
1608 .name = "addr",
1609 .type = QEMU_OPT_STRING,
1610 .help = "pci address (virtio only)",
1611 },{
1612 .name = "readonly",
1613 .type = QEMU_OPT_BOOL,
1614 .help = "open drive file as read-only",
1615 },{
1616 .name = "iops",
1617 .type = QEMU_OPT_NUMBER,
1618 .help = "limit total I/O operations per second",
1619 },{
1620 .name = "iops_rd",
1621 .type = QEMU_OPT_NUMBER,
1622 .help = "limit read operations per second",
1623 },{
1624 .name = "iops_wr",
1625 .type = QEMU_OPT_NUMBER,
1626 .help = "limit write operations per second",
1627 },{
1628 .name = "bps",
1629 .type = QEMU_OPT_NUMBER,
1630 .help = "limit total bytes per second",
1631 },{
1632 .name = "bps_rd",
1633 .type = QEMU_OPT_NUMBER,
1634 .help = "limit read bytes per second",
1635 },{
1636 .name = "bps_wr",
1637 .type = QEMU_OPT_NUMBER,
1638 .help = "limit write bytes per second",
1639 },{
1640 .name = "copy-on-read",
1641 .type = QEMU_OPT_BOOL,
1642 .help = "copy read data from backing file into image file",
1643 },{
1644 .name = "boot",
1645 .type = QEMU_OPT_BOOL,
1646 .help = "(deprecated, ignored)",
1647 },
1648 { /* end of list */ }
1649 },
1650};
0006383e
KW
1651
1652QemuOptsList qemu_drive_opts = {
1653 .name = "drive",
1654 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
1655 .desc = {
1656 /*
1657 * no elements => accept any params
1658 * validation will happen later
1659 */
1660 { /* end of list */ }
1661 },
1662};