blob: 9068c5b1d0d4b61c28b10e7f64b2712ebf5c279a [file] [log] [blame]
Markus Armbruster666daa62010-06-02 18:48:27 +02001/*
2 * QEMU host block devices
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
8 */
9
10#include "block.h"
11#include "blockdev.h"
12#include "monitor.h"
13#include "qerror.h"
14#include "qemu-option.h"
15#include "qemu-config.h"
16#include "sysemu.h"
Ryan Harper9063f812010-11-12 11:07:13 -060017#include "block_int.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020018
Markus Armbrusterc9b62a72010-06-02 18:55:22 +020019static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
Markus Armbruster666daa62010-06-02 18:48:27 +020020
Markus Armbruster19609662011-01-28 11:21:39 +010021static const char *const if_name[IF_COUNT] = {
22 [IF_NONE] = "none",
23 [IF_IDE] = "ide",
24 [IF_SCSI] = "scsi",
25 [IF_FLOPPY] = "floppy",
26 [IF_PFLASH] = "pflash",
27 [IF_MTD] = "mtd",
28 [IF_SD] = "sd",
29 [IF_VIRTIO] = "virtio",
30 [IF_XEN] = "xen",
31};
32
33static const int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010034 /*
35 * Do not change these numbers! They govern how drive option
36 * index maps to unit and bus. That mapping is ABI.
37 *
38 * All controllers used to imlement if=T drives need to support
39 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
40 * Otherwise, some index values map to "impossible" bus, unit
41 * values.
42 *
43 * For instance, if you change [IF_SCSI] to 255, -drive
44 * if=scsi,index=12 no longer means bus=1,unit=5, but
45 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
46 * the drive can't be set up. Regression.
47 */
48 [IF_IDE] = 2,
49 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010050};
51
Markus Armbruster14bafc52010-06-25 08:09:10 +020052/*
53 * We automatically delete the drive when a device using it gets
54 * unplugged. Questionable feature, but we can't just drop it.
55 * Device models call blockdev_mark_auto_del() to schedule the
56 * automatic deletion, and generic qdev code calls blockdev_auto_del()
57 * when deletion is actually safe.
58 */
59void blockdev_mark_auto_del(BlockDriverState *bs)
60{
61 DriveInfo *dinfo = drive_get_by_blockdev(bs);
62
Ryan Harper0fc0f1f2010-12-08 10:05:00 -060063 if (dinfo) {
64 dinfo->auto_del = 1;
65 }
Markus Armbruster14bafc52010-06-25 08:09:10 +020066}
67
68void blockdev_auto_del(BlockDriverState *bs)
69{
70 DriveInfo *dinfo = drive_get_by_blockdev(bs);
71
Ryan Harper0fc0f1f2010-12-08 10:05:00 -060072 if (dinfo && dinfo->auto_del) {
Marcelo Tosatti84fb3922011-01-26 12:12:32 -020073 drive_put_ref(dinfo);
Markus Armbruster14bafc52010-06-25 08:09:10 +020074 }
75}
76
Markus Armbruster505a7fb2011-01-28 11:21:43 +010077static int drive_index_to_bus_id(BlockInterfaceType type, int index)
78{
79 int max_devs = if_max_devs[type];
80 return max_devs ? index / max_devs : 0;
81}
82
83static int drive_index_to_unit_id(BlockInterfaceType type, int index)
84{
85 int max_devs = if_max_devs[type];
86 return max_devs ? index % max_devs : index;
87}
88
Markus Armbruster2292dda2011-01-28 11:21:41 +010089QemuOpts *drive_def(const char *optstr)
90{
91 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
92}
93
94QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +010095 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +020096{
Markus Armbruster666daa62010-06-02 18:48:27 +020097 QemuOpts *opts;
Markus Armbruster2292dda2011-01-28 11:21:41 +010098 char buf[32];
Markus Armbruster666daa62010-06-02 18:48:27 +020099
Markus Armbruster2292dda2011-01-28 11:21:41 +0100100 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200101 if (!opts) {
102 return NULL;
103 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100104 if (type != IF_DEFAULT) {
105 qemu_opt_set(opts, "if", if_name[type]);
106 }
107 if (index >= 0) {
108 snprintf(buf, sizeof(buf), "%d", index);
109 qemu_opt_set(opts, "index", buf);
110 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200111 if (file)
112 qemu_opt_set(opts, "file", file);
113 return opts;
114}
115
116DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
117{
118 DriveInfo *dinfo;
119
120 /* seek interface, bus and unit */
121
122 QTAILQ_FOREACH(dinfo, &drives, next) {
123 if (dinfo->type == type &&
124 dinfo->bus == bus &&
125 dinfo->unit == unit)
126 return dinfo;
127 }
128
129 return NULL;
130}
131
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100132DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
133{
134 return drive_get(type,
135 drive_index_to_bus_id(type, index),
136 drive_index_to_unit_id(type, index));
137}
138
Markus Armbruster666daa62010-06-02 18:48:27 +0200139int drive_get_max_bus(BlockInterfaceType type)
140{
141 int max_bus;
142 DriveInfo *dinfo;
143
144 max_bus = -1;
145 QTAILQ_FOREACH(dinfo, &drives, next) {
146 if(dinfo->type == type &&
147 dinfo->bus > max_bus)
148 max_bus = dinfo->bus;
149 }
150 return max_bus;
151}
152
Markus Armbruster13839972011-01-28 11:21:37 +0100153/* Get a block device. This should only be used for single-drive devices
154 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
155 appropriate bus. */
156DriveInfo *drive_get_next(BlockInterfaceType type)
157{
158 static int next_block_unit[IF_COUNT];
159
160 return drive_get(type, 0, next_block_unit[type]++);
161}
162
Markus Armbrustere4700e52010-06-24 17:25:32 +0200163DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
Markus Armbruster666daa62010-06-02 18:48:27 +0200164{
165 DriveInfo *dinfo;
166
167 QTAILQ_FOREACH(dinfo, &drives, next) {
Markus Armbrustere4700e52010-06-24 17:25:32 +0200168 if (dinfo->bdrv == bs) {
169 return dinfo;
170 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200171 }
Markus Armbrustere4700e52010-06-24 17:25:32 +0200172 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200173}
174
Markus Armbruster666daa62010-06-02 18:48:27 +0200175static void bdrv_format_print(void *opaque, const char *name)
176{
Markus Armbruster807105a2011-01-17 19:31:27 +0100177 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200178}
179
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200180static void drive_uninit(DriveInfo *dinfo)
Markus Armbruster666daa62010-06-02 18:48:27 +0200181{
182 qemu_opts_del(dinfo->opts);
183 bdrv_delete(dinfo->bdrv);
Anthony Liguori7267c092011-08-20 22:09:37 -0500184 g_free(dinfo->id);
Markus Armbruster666daa62010-06-02 18:48:27 +0200185 QTAILQ_REMOVE(&drives, dinfo, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500186 g_free(dinfo);
Markus Armbruster666daa62010-06-02 18:48:27 +0200187}
188
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200189void drive_put_ref(DriveInfo *dinfo)
190{
191 assert(dinfo->refcount);
192 if (--dinfo->refcount == 0) {
193 drive_uninit(dinfo);
194 }
195}
196
197void drive_get_ref(DriveInfo *dinfo)
198{
199 dinfo->refcount++;
200}
201
Markus Armbruster666daa62010-06-02 18:48:27 +0200202static int parse_block_error_action(const char *buf, int is_read)
203{
204 if (!strcmp(buf, "ignore")) {
205 return BLOCK_ERR_IGNORE;
206 } else if (!is_read && !strcmp(buf, "enospc")) {
207 return BLOCK_ERR_STOP_ENOSPC;
208 } else if (!strcmp(buf, "stop")) {
209 return BLOCK_ERR_STOP_ANY;
210 } else if (!strcmp(buf, "report")) {
211 return BLOCK_ERR_REPORT;
212 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100213 error_report("'%s' invalid %s error action",
214 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200215 return -1;
216 }
217}
218
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800219static bool do_check_io_limits(BlockIOLimit *io_limits)
220{
221 bool bps_flag;
222 bool iops_flag;
223
224 assert(io_limits);
225
226 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
227 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
228 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
229 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
230 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
231 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
232 if (bps_flag || iops_flag) {
233 return false;
234 }
235
236 return true;
237}
238
Markus Armbruster319ae522011-01-28 11:21:46 +0100239DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
Markus Armbruster666daa62010-06-02 18:48:27 +0200240{
241 const char *buf;
242 const char *file = NULL;
243 char devname[128];
244 const char *serial;
245 const char *mediastr = "";
246 BlockInterfaceType type;
247 enum { MEDIA_DISK, MEDIA_CDROM } media;
248 int bus_id, unit_id;
249 int cyls, heads, secs, translation;
250 BlockDriver *drv = NULL;
251 int max_devs;
252 int index;
253 int ro = 0;
254 int bdrv_flags = 0;
255 int on_read_error, on_write_error;
256 const char *devaddr;
257 DriveInfo *dinfo;
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800258 BlockIOLimit io_limits;
Markus Armbruster666daa62010-06-02 18:48:27 +0200259 int snapshot = 0;
260 int ret;
261
Markus Armbruster666daa62010-06-02 18:48:27 +0200262 translation = BIOS_ATA_TRANSLATION_AUTO;
Markus Armbruster666daa62010-06-02 18:48:27 +0200263 media = MEDIA_DISK;
264
265 /* extract parameters */
266 bus_id = qemu_opt_get_number(opts, "bus", 0);
267 unit_id = qemu_opt_get_number(opts, "unit", -1);
268 index = qemu_opt_get_number(opts, "index", -1);
269
270 cyls = qemu_opt_get_number(opts, "cyls", 0);
271 heads = qemu_opt_get_number(opts, "heads", 0);
272 secs = qemu_opt_get_number(opts, "secs", 0);
273
274 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
275 ro = qemu_opt_get_bool(opts, "readonly", 0);
276
277 file = qemu_opt_get(opts, "file");
278 serial = qemu_opt_get(opts, "serial");
279
280 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
281 pstrcpy(devname, sizeof(devname), buf);
Markus Armbruster19609662011-01-28 11:21:39 +0100282 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
283 ;
284 if (type == IF_COUNT) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100285 error_report("unsupported bus type '%s'", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200286 return NULL;
287 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300288 } else {
289 type = default_to_scsi ? IF_SCSI : IF_IDE;
290 pstrcpy(devname, sizeof(devname), if_name[type]);
Markus Armbruster666daa62010-06-02 18:48:27 +0200291 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300292
Markus Armbruster19609662011-01-28 11:21:39 +0100293 max_devs = if_max_devs[type];
Markus Armbruster666daa62010-06-02 18:48:27 +0200294
295 if (cyls || heads || secs) {
296 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100297 error_report("invalid physical cyls number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200298 return NULL;
299 }
300 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100301 error_report("invalid physical heads number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200302 return NULL;
303 }
304 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100305 error_report("invalid physical secs number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200306 return NULL;
307 }
308 }
309
310 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
311 if (!cyls) {
Markus Armbrustere4080f92011-06-22 14:03:57 +0200312 error_report("'%s' trans must be used with cyls, heads and secs",
Markus Armbruster807105a2011-01-17 19:31:27 +0100313 buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200314 return NULL;
315 }
316 if (!strcmp(buf, "none"))
317 translation = BIOS_ATA_TRANSLATION_NONE;
318 else if (!strcmp(buf, "lba"))
319 translation = BIOS_ATA_TRANSLATION_LBA;
320 else if (!strcmp(buf, "auto"))
321 translation = BIOS_ATA_TRANSLATION_AUTO;
322 else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100323 error_report("'%s' invalid translation type", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200324 return NULL;
325 }
326 }
327
328 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
329 if (!strcmp(buf, "disk")) {
330 media = MEDIA_DISK;
331 } else if (!strcmp(buf, "cdrom")) {
332 if (cyls || secs || heads) {
Luiz Capitulinoe7ff8f02011-07-01 10:46:13 -0300333 error_report("CHS can't be set with media=%s", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200334 return NULL;
335 }
336 media = MEDIA_CDROM;
337 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100338 error_report("'%s' invalid media", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200339 return NULL;
340 }
341 }
342
343 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100344 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
345 error_report("invalid cache option");
346 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200347 }
348 }
349
350#ifdef CONFIG_LINUX_AIO
351 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
352 if (!strcmp(buf, "native")) {
353 bdrv_flags |= BDRV_O_NATIVE_AIO;
354 } else if (!strcmp(buf, "threads")) {
355 /* this is the default */
356 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100357 error_report("invalid aio option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200358 return NULL;
359 }
360 }
361#endif
362
363 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
364 if (strcmp(buf, "?") == 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100365 error_printf("Supported formats:");
366 bdrv_iterate_format(bdrv_format_print, NULL);
367 error_printf("\n");
368 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200369 }
370 drv = bdrv_find_whitelisted_format(buf);
371 if (!drv) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100372 error_report("'%s' invalid format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200373 return NULL;
374 }
375 }
376
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800377 /* disk I/O throttling */
378 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
379 qemu_opt_get_number(opts, "bps", 0);
380 io_limits.bps[BLOCK_IO_LIMIT_READ] =
381 qemu_opt_get_number(opts, "bps_rd", 0);
382 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
383 qemu_opt_get_number(opts, "bps_wr", 0);
384 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
385 qemu_opt_get_number(opts, "iops", 0);
386 io_limits.iops[BLOCK_IO_LIMIT_READ] =
387 qemu_opt_get_number(opts, "iops_rd", 0);
388 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
389 qemu_opt_get_number(opts, "iops_wr", 0);
390
391 if (!do_check_io_limits(&io_limits)) {
392 error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
393 "cannot be used at the same time");
394 return NULL;
395 }
396
Markus Armbruster666daa62010-06-02 18:48:27 +0200397 on_write_error = BLOCK_ERR_STOP_ENOSPC;
398 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
399 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100400 error_report("werror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200401 return NULL;
402 }
403
404 on_write_error = parse_block_error_action(buf, 0);
405 if (on_write_error < 0) {
406 return NULL;
407 }
408 }
409
410 on_read_error = BLOCK_ERR_REPORT;
411 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolf5dba48a2010-10-25 14:52:21 +0200412 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100413 error_report("rerror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200414 return NULL;
415 }
416
417 on_read_error = parse_block_error_action(buf, 1);
418 if (on_read_error < 0) {
419 return NULL;
420 }
421 }
422
423 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
424 if (type != IF_VIRTIO) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100425 error_report("addr is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200426 return NULL;
427 }
428 }
429
430 /* compute bus and unit according index */
431
432 if (index != -1) {
433 if (bus_id != 0 || unit_id != -1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100434 error_report("index cannot be used with bus and unit");
Markus Armbruster666daa62010-06-02 18:48:27 +0200435 return NULL;
436 }
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100437 bus_id = drive_index_to_bus_id(type, index);
438 unit_id = drive_index_to_unit_id(type, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200439 }
440
441 /* if user doesn't specify a unit_id,
442 * try to find the first free
443 */
444
445 if (unit_id == -1) {
446 unit_id = 0;
447 while (drive_get(type, bus_id, unit_id) != NULL) {
448 unit_id++;
449 if (max_devs && unit_id >= max_devs) {
450 unit_id -= max_devs;
451 bus_id++;
452 }
453 }
454 }
455
456 /* check unit id */
457
458 if (max_devs && unit_id >= max_devs) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100459 error_report("unit %d too big (max is %d)",
460 unit_id, max_devs - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200461 return NULL;
462 }
463
464 /*
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100465 * catch multiple definitions
Markus Armbruster666daa62010-06-02 18:48:27 +0200466 */
467
468 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100469 error_report("drive with bus=%d, unit=%d (index=%d) exists",
470 bus_id, unit_id, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200471 return NULL;
472 }
473
474 /* init */
475
Anthony Liguori7267c092011-08-20 22:09:37 -0500476 dinfo = g_malloc0(sizeof(*dinfo));
Markus Armbruster666daa62010-06-02 18:48:27 +0200477 if ((buf = qemu_opts_id(opts)) != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500478 dinfo->id = g_strdup(buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200479 } else {
480 /* no id supplied -> create one */
Anthony Liguori7267c092011-08-20 22:09:37 -0500481 dinfo->id = g_malloc0(32);
Markus Armbruster666daa62010-06-02 18:48:27 +0200482 if (type == IF_IDE || type == IF_SCSI)
483 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
484 if (max_devs)
485 snprintf(dinfo->id, 32, "%s%i%s%i",
486 devname, bus_id, mediastr, unit_id);
487 else
488 snprintf(dinfo->id, 32, "%s%s%i",
489 devname, mediastr, unit_id);
490 }
491 dinfo->bdrv = bdrv_new(dinfo->id);
492 dinfo->devaddr = devaddr;
493 dinfo->type = type;
494 dinfo->bus = bus_id;
495 dinfo->unit = unit_id;
Markus Armbruster666daa62010-06-02 18:48:27 +0200496 dinfo->opts = opts;
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200497 dinfo->refcount = 1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200498 if (serial)
Luiz Capitulino653dbec2010-06-02 17:46:31 -0300499 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200500 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
501
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200502 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
503
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800504 /* disk I/O throttling */
505 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
506
Markus Armbruster666daa62010-06-02 18:48:27 +0200507 switch(type) {
508 case IF_IDE:
509 case IF_SCSI:
510 case IF_XEN:
511 case IF_NONE:
512 switch(media) {
513 case MEDIA_DISK:
514 if (cyls != 0) {
515 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
516 bdrv_set_translation_hint(dinfo->bdrv, translation);
517 }
518 break;
519 case MEDIA_CDROM:
Markus Armbruster95b5edc2011-05-16 15:04:56 +0200520 dinfo->media_cd = 1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200521 break;
522 }
523 break;
524 case IF_SD:
Markus Armbruster666daa62010-06-02 18:48:27 +0200525 case IF_FLOPPY:
Markus Armbruster666daa62010-06-02 18:48:27 +0200526 case IF_PFLASH:
527 case IF_MTD:
528 break;
529 case IF_VIRTIO:
530 /* add virtio block device */
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200531 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Alexander Graf29f82b32011-03-29 15:29:29 +0200532 qemu_opt_set(opts, "driver", "virtio-blk");
Markus Armbruster666daa62010-06-02 18:48:27 +0200533 qemu_opt_set(opts, "drive", dinfo->id);
534 if (devaddr)
535 qemu_opt_set(opts, "addr", devaddr);
536 break;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100537 default:
Markus Armbruster666daa62010-06-02 18:48:27 +0200538 abort();
539 }
Markus Armbrusterdd5b0d72010-05-11 15:36:46 +0200540 if (!file || !*file) {
Markus Armbruster319ae522011-01-28 11:21:46 +0100541 return dinfo;
Markus Armbruster666daa62010-06-02 18:48:27 +0200542 }
543 if (snapshot) {
544 /* always use cache=unsafe with snapshot */
545 bdrv_flags &= ~BDRV_O_CACHE_MASK;
546 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
547 }
548
549 if (media == MEDIA_CDROM) {
550 /* CDROM is fine for any interface, don't check. */
551 ro = 1;
552 } else if (ro == 1) {
553 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100554 error_report("readonly not supported by this bus type");
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100555 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200556 }
557 }
558
559 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
560
561 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
562 if (ret < 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100563 error_report("could not open disk image %s: %s",
564 file, strerror(-ret));
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100565 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200566 }
567
568 if (bdrv_key_required(dinfo->bdrv))
569 autostart = 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200570 return dinfo;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100571
572err:
573 bdrv_delete(dinfo->bdrv);
Anthony Liguori7267c092011-08-20 22:09:37 -0500574 g_free(dinfo->id);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100575 QTAILQ_REMOVE(&drives, dinfo, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500576 g_free(dinfo);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100577 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200578}
579
580void do_commit(Monitor *mon, const QDict *qdict)
581{
Markus Armbruster666daa62010-06-02 18:48:27 +0200582 const char *device = qdict_get_str(qdict, "device");
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200583 BlockDriverState *bs;
Markus Armbruster666daa62010-06-02 18:48:27 +0200584
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200585 if (!strcmp(device, "all")) {
586 bdrv_commit_all();
587 } else {
588 bs = bdrv_find(device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200589 if (!bs) {
590 qerror_report(QERR_DEVICE_NOT_FOUND, device);
591 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200592 }
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200593 bdrv_commit(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200594 }
595}
596
Jes Sorensenf8882562010-12-16 13:52:16 +0100597int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
598{
599 const char *device = qdict_get_str(qdict, "device");
Jes Sorensend967b2f2011-07-11 20:01:09 +0200600 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
Jes Sorensenf8882562010-12-16 13:52:16 +0100601 const char *format = qdict_get_try_str(qdict, "format");
602 BlockDriverState *bs;
Jes Sorensen52f9a172011-03-09 11:20:30 +0100603 BlockDriver *drv, *old_drv, *proto_drv;
Jes Sorensenf8882562010-12-16 13:52:16 +0100604 int ret = 0;
605 int flags;
Jes Sorensen52f9a172011-03-09 11:20:30 +0100606 char old_filename[1024];
Jes Sorensenf8882562010-12-16 13:52:16 +0100607
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100608 if (!filename) {
Jes Sorensend967b2f2011-07-11 20:01:09 +0200609 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100610 ret = -1;
611 goto out;
612 }
613
Jes Sorensenf8882562010-12-16 13:52:16 +0100614 bs = bdrv_find(device);
615 if (!bs) {
616 qerror_report(QERR_DEVICE_NOT_FOUND, device);
617 ret = -1;
618 goto out;
619 }
620
Jes Sorensen52f9a172011-03-09 11:20:30 +0100621 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
622
623 old_drv = bs->drv;
624 flags = bs->open_flags;
625
Jes Sorensenf8882562010-12-16 13:52:16 +0100626 if (!format) {
627 format = "qcow2";
628 }
629
630 drv = bdrv_find_format(format);
631 if (!drv) {
632 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
633 ret = -1;
634 goto out;
635 }
636
637 proto_drv = bdrv_find_protocol(filename);
638 if (!proto_drv) {
639 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
640 ret = -1;
641 goto out;
642 }
643
644 ret = bdrv_img_create(filename, format, bs->filename,
Jes Sorensen52f9a172011-03-09 11:20:30 +0100645 bs->drv->format_name, NULL, -1, flags);
Jes Sorensenf8882562010-12-16 13:52:16 +0100646 if (ret) {
647 goto out;
648 }
649
650 qemu_aio_flush();
651 bdrv_flush(bs);
652
Jes Sorensenf8882562010-12-16 13:52:16 +0100653 bdrv_close(bs);
654 ret = bdrv_open(bs, filename, flags, drv);
655 /*
Jes Sorensen52f9a172011-03-09 11:20:30 +0100656 * If reopening the image file we just created fails, fall back
657 * and try to re-open the original image. If that fails too, we
658 * are in serious trouble.
Jes Sorensenf8882562010-12-16 13:52:16 +0100659 */
660 if (ret != 0) {
Jes Sorensen52f9a172011-03-09 11:20:30 +0100661 ret = bdrv_open(bs, old_filename, flags, old_drv);
662 if (ret != 0) {
663 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
664 } else {
665 qerror_report(QERR_OPEN_FILE_FAILED, filename);
666 }
Jes Sorensenf8882562010-12-16 13:52:16 +0100667 }
668out:
669 if (ret) {
670 ret = -1;
671 }
672
673 return ret;
674}
675
Markus Armbruster666daa62010-06-02 18:48:27 +0200676static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
677{
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200678 if (!bdrv_dev_has_removable_media(bs)) {
Markus Armbrusterea8f9422011-07-20 18:23:35 +0200679 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
680 return -1;
681 }
Paolo Bonzini025ccaa2011-11-07 17:50:13 +0100682 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
683 bdrv_dev_eject_request(bs, force);
684 if (!force) {
685 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
686 return -1;
687 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200688 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300689 bdrv_close(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200690 return 0;
691}
692
693int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
694{
695 BlockDriverState *bs;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -0300696 int force = qdict_get_try_bool(qdict, "force", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200697 const char *filename = qdict_get_str(qdict, "device");
698
699 bs = bdrv_find(filename);
700 if (!bs) {
701 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
702 return -1;
703 }
704 return eject_device(mon, bs, force);
705}
706
707int do_block_set_passwd(Monitor *mon, const QDict *qdict,
708 QObject **ret_data)
709{
710 BlockDriverState *bs;
711 int err;
712
713 bs = bdrv_find(qdict_get_str(qdict, "device"));
714 if (!bs) {
715 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
716 return -1;
717 }
718
719 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
720 if (err == -EINVAL) {
721 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
722 return -1;
723 } else if (err < 0) {
724 qerror_report(QERR_INVALID_PASSWORD);
725 return -1;
726 }
727
728 return 0;
729}
730
731int do_change_block(Monitor *mon, const char *device,
732 const char *filename, const char *fmt)
733{
734 BlockDriverState *bs;
735 BlockDriver *drv = NULL;
736 int bdrv_flags;
737
738 bs = bdrv_find(device);
739 if (!bs) {
740 qerror_report(QERR_DEVICE_NOT_FOUND, device);
741 return -1;
742 }
743 if (fmt) {
744 drv = bdrv_find_whitelisted_format(fmt);
745 if (!drv) {
746 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
747 return -1;
748 }
749 }
750 if (eject_device(mon, bs, 0) < 0) {
751 return -1;
752 }
Markus Armbruster528f7662010-06-25 15:26:48 +0200753 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
Blue Swirl199630b2010-07-25 20:49:34 +0000754 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200755 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
756 qerror_report(QERR_OPEN_FILE_FAILED, filename);
757 return -1;
758 }
759 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
760}
Ryan Harper9063f812010-11-12 11:07:13 -0600761
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800762/* throttling disk I/O limits */
763int do_block_set_io_throttle(Monitor *mon,
764 const QDict *qdict, QObject **ret_data)
765{
766 BlockIOLimit io_limits;
767 const char *devname = qdict_get_str(qdict, "device");
768 BlockDriverState *bs;
769
770 io_limits.bps[BLOCK_IO_LIMIT_TOTAL]
771 = qdict_get_try_int(qdict, "bps", -1);
772 io_limits.bps[BLOCK_IO_LIMIT_READ]
773 = qdict_get_try_int(qdict, "bps_rd", -1);
774 io_limits.bps[BLOCK_IO_LIMIT_WRITE]
775 = qdict_get_try_int(qdict, "bps_wr", -1);
776 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]
777 = qdict_get_try_int(qdict, "iops", -1);
778 io_limits.iops[BLOCK_IO_LIMIT_READ]
779 = qdict_get_try_int(qdict, "iops_rd", -1);
780 io_limits.iops[BLOCK_IO_LIMIT_WRITE]
781 = qdict_get_try_int(qdict, "iops_wr", -1);
782
783 bs = bdrv_find(devname);
784 if (!bs) {
785 qerror_report(QERR_DEVICE_NOT_FOUND, devname);
786 return -1;
787 }
788
789 if ((io_limits.bps[BLOCK_IO_LIMIT_TOTAL] == -1)
790 || (io_limits.bps[BLOCK_IO_LIMIT_READ] == -1)
791 || (io_limits.bps[BLOCK_IO_LIMIT_WRITE] == -1)
792 || (io_limits.iops[BLOCK_IO_LIMIT_TOTAL] == -1)
793 || (io_limits.iops[BLOCK_IO_LIMIT_READ] == -1)
794 || (io_limits.iops[BLOCK_IO_LIMIT_WRITE] == -1)) {
795 qerror_report(QERR_MISSING_PARAMETER,
796 "bps/bps_rd/bps_wr/iops/iops_rd/iops_wr");
797 return -1;
798 }
799
800 if (!do_check_io_limits(&io_limits)) {
801 qerror_report(QERR_INVALID_PARAMETER_COMBINATION);
802 return -1;
803 }
804
805 bs->io_limits = io_limits;
806 bs->slice_time = BLOCK_IO_SLICE_TIME;
807
808 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
809 bdrv_io_limits_enable(bs);
810 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
811 bdrv_io_limits_disable(bs);
812 } else {
813 if (bs->block_timer) {
814 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
815 }
816 }
817
818 return 0;
819}
820
Ryan Harper9063f812010-11-12 11:07:13 -0600821int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
822{
823 const char *id = qdict_get_str(qdict, "id");
824 BlockDriverState *bs;
Ryan Harper9063f812010-11-12 11:07:13 -0600825
826 bs = bdrv_find(id);
827 if (!bs) {
828 qerror_report(QERR_DEVICE_NOT_FOUND, id);
829 return -1;
830 }
Marcelo Tosatti85916752011-01-26 12:12:35 -0200831 if (bdrv_in_use(bs)) {
832 qerror_report(QERR_DEVICE_IN_USE, id);
833 return -1;
834 }
Ryan Harper9063f812010-11-12 11:07:13 -0600835
836 /* quiesce block driver; prevent further io */
837 qemu_aio_flush();
838 bdrv_flush(bs);
839 bdrv_close(bs);
840
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200841 /* if we have a device attached to this BlockDriverState
Ryan Harperd22b2f42011-03-29 20:51:47 -0500842 * then we need to make the drive anonymous until the device
843 * can be removed. If this is a drive with no device backing
844 * then we can just get rid of the block driver state right here.
845 */
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200846 if (bdrv_get_attached_dev(bs)) {
Ryan Harperd22b2f42011-03-29 20:51:47 -0500847 bdrv_make_anon(bs);
848 } else {
849 drive_uninit(drive_get_by_blockdev(bs));
Ryan Harper9063f812010-11-12 11:07:13 -0600850 }
851
Ryan Harper9063f812010-11-12 11:07:13 -0600852 return 0;
853}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +0100854
855/*
856 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
857 * existing QERR_ macro mess is cleaned up. A good example for better
858 * error reports can be found in the qemu-img resize code.
859 */
860int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
861{
862 const char *device = qdict_get_str(qdict, "device");
863 int64_t size = qdict_get_int(qdict, "size");
864 BlockDriverState *bs;
865
866 bs = bdrv_find(device);
867 if (!bs) {
868 qerror_report(QERR_DEVICE_NOT_FOUND, device);
869 return -1;
870 }
871
872 if (size < 0) {
873 qerror_report(QERR_UNDEFINED_ERROR);
874 return -1;
875 }
876
877 if (bdrv_truncate(bs, size)) {
878 qerror_report(QERR_UNDEFINED_ERROR);
879 return -1;
880 }
881
882 return 0;
883}