blob: aba7aaf17431890dd6d1570b98456344ae0b474b [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"
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -020018#include "qmp-commands.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020019
Markus Armbrusterc9b62a72010-06-02 18:55:22 +020020static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
Markus Armbruster666daa62010-06-02 18:48:27 +020021
Markus Armbruster19609662011-01-28 11:21:39 +010022static const char *const if_name[IF_COUNT] = {
23 [IF_NONE] = "none",
24 [IF_IDE] = "ide",
25 [IF_SCSI] = "scsi",
26 [IF_FLOPPY] = "floppy",
27 [IF_PFLASH] = "pflash",
28 [IF_MTD] = "mtd",
29 [IF_SD] = "sd",
30 [IF_VIRTIO] = "virtio",
31 [IF_XEN] = "xen",
32};
33
34static const int if_max_devs[IF_COUNT] = {
Markus Armbruster27d6bf42011-01-28 11:21:40 +010035 /*
36 * Do not change these numbers! They govern how drive option
37 * index maps to unit and bus. That mapping is ABI.
38 *
39 * All controllers used to imlement if=T drives need to support
40 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
41 * Otherwise, some index values map to "impossible" bus, unit
42 * values.
43 *
44 * For instance, if you change [IF_SCSI] to 255, -drive
45 * if=scsi,index=12 no longer means bus=1,unit=5, but
46 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
47 * the drive can't be set up. Regression.
48 */
49 [IF_IDE] = 2,
50 [IF_SCSI] = 7,
Markus Armbruster19609662011-01-28 11:21:39 +010051};
52
Markus Armbruster14bafc52010-06-25 08:09:10 +020053/*
54 * We automatically delete the drive when a device using it gets
55 * unplugged. Questionable feature, but we can't just drop it.
56 * Device models call blockdev_mark_auto_del() to schedule the
57 * automatic deletion, and generic qdev code calls blockdev_auto_del()
58 * when deletion is actually safe.
59 */
60void blockdev_mark_auto_del(BlockDriverState *bs)
61{
62 DriveInfo *dinfo = drive_get_by_blockdev(bs);
63
Ryan Harper0fc0f1f2010-12-08 10:05:00 -060064 if (dinfo) {
65 dinfo->auto_del = 1;
66 }
Markus Armbruster14bafc52010-06-25 08:09:10 +020067}
68
69void blockdev_auto_del(BlockDriverState *bs)
70{
71 DriveInfo *dinfo = drive_get_by_blockdev(bs);
72
Ryan Harper0fc0f1f2010-12-08 10:05:00 -060073 if (dinfo && dinfo->auto_del) {
Marcelo Tosatti84fb3922011-01-26 12:12:32 -020074 drive_put_ref(dinfo);
Markus Armbruster14bafc52010-06-25 08:09:10 +020075 }
76}
77
Markus Armbruster505a7fb2011-01-28 11:21:43 +010078static int drive_index_to_bus_id(BlockInterfaceType type, int index)
79{
80 int max_devs = if_max_devs[type];
81 return max_devs ? index / max_devs : 0;
82}
83
84static int drive_index_to_unit_id(BlockInterfaceType type, int index)
85{
86 int max_devs = if_max_devs[type];
87 return max_devs ? index % max_devs : index;
88}
89
Markus Armbruster2292dda2011-01-28 11:21:41 +010090QemuOpts *drive_def(const char *optstr)
91{
92 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
93}
94
95QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
Markus Armbruster5645b0f2011-01-31 11:50:09 +010096 const char *optstr)
Markus Armbruster666daa62010-06-02 18:48:27 +020097{
Markus Armbruster666daa62010-06-02 18:48:27 +020098 QemuOpts *opts;
Markus Armbruster2292dda2011-01-28 11:21:41 +010099 char buf[32];
Markus Armbruster666daa62010-06-02 18:48:27 +0200100
Markus Armbruster2292dda2011-01-28 11:21:41 +0100101 opts = drive_def(optstr);
Markus Armbruster666daa62010-06-02 18:48:27 +0200102 if (!opts) {
103 return NULL;
104 }
Markus Armbruster2292dda2011-01-28 11:21:41 +0100105 if (type != IF_DEFAULT) {
106 qemu_opt_set(opts, "if", if_name[type]);
107 }
108 if (index >= 0) {
109 snprintf(buf, sizeof(buf), "%d", index);
110 qemu_opt_set(opts, "index", buf);
111 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200112 if (file)
113 qemu_opt_set(opts, "file", file);
114 return opts;
115}
116
117DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
118{
119 DriveInfo *dinfo;
120
121 /* seek interface, bus and unit */
122
123 QTAILQ_FOREACH(dinfo, &drives, next) {
124 if (dinfo->type == type &&
125 dinfo->bus == bus &&
126 dinfo->unit == unit)
127 return dinfo;
128 }
129
130 return NULL;
131}
132
Markus Armbrusterf1bd51a2011-01-28 11:21:44 +0100133DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
134{
135 return drive_get(type,
136 drive_index_to_bus_id(type, index),
137 drive_index_to_unit_id(type, index));
138}
139
Markus Armbruster666daa62010-06-02 18:48:27 +0200140int drive_get_max_bus(BlockInterfaceType type)
141{
142 int max_bus;
143 DriveInfo *dinfo;
144
145 max_bus = -1;
146 QTAILQ_FOREACH(dinfo, &drives, next) {
147 if(dinfo->type == type &&
148 dinfo->bus > max_bus)
149 max_bus = dinfo->bus;
150 }
151 return max_bus;
152}
153
Markus Armbruster13839972011-01-28 11:21:37 +0100154/* Get a block device. This should only be used for single-drive devices
155 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
156 appropriate bus. */
157DriveInfo *drive_get_next(BlockInterfaceType type)
158{
159 static int next_block_unit[IF_COUNT];
160
161 return drive_get(type, 0, next_block_unit[type]++);
162}
163
Markus Armbrustere4700e52010-06-24 17:25:32 +0200164DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
Markus Armbruster666daa62010-06-02 18:48:27 +0200165{
166 DriveInfo *dinfo;
167
168 QTAILQ_FOREACH(dinfo, &drives, next) {
Markus Armbrustere4700e52010-06-24 17:25:32 +0200169 if (dinfo->bdrv == bs) {
170 return dinfo;
171 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200172 }
Markus Armbrustere4700e52010-06-24 17:25:32 +0200173 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200174}
175
Markus Armbruster666daa62010-06-02 18:48:27 +0200176static void bdrv_format_print(void *opaque, const char *name)
177{
Markus Armbruster807105a2011-01-17 19:31:27 +0100178 error_printf(" %s", name);
Markus Armbruster666daa62010-06-02 18:48:27 +0200179}
180
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200181static void drive_uninit(DriveInfo *dinfo)
Markus Armbruster666daa62010-06-02 18:48:27 +0200182{
183 qemu_opts_del(dinfo->opts);
184 bdrv_delete(dinfo->bdrv);
Anthony Liguori7267c092011-08-20 22:09:37 -0500185 g_free(dinfo->id);
Markus Armbruster666daa62010-06-02 18:48:27 +0200186 QTAILQ_REMOVE(&drives, dinfo, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500187 g_free(dinfo);
Markus Armbruster666daa62010-06-02 18:48:27 +0200188}
189
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200190void drive_put_ref(DriveInfo *dinfo)
191{
192 assert(dinfo->refcount);
193 if (--dinfo->refcount == 0) {
194 drive_uninit(dinfo);
195 }
196}
197
198void drive_get_ref(DriveInfo *dinfo)
199{
200 dinfo->refcount++;
201}
202
Markus Armbruster666daa62010-06-02 18:48:27 +0200203static int parse_block_error_action(const char *buf, int is_read)
204{
205 if (!strcmp(buf, "ignore")) {
206 return BLOCK_ERR_IGNORE;
207 } else if (!is_read && !strcmp(buf, "enospc")) {
208 return BLOCK_ERR_STOP_ENOSPC;
209 } else if (!strcmp(buf, "stop")) {
210 return BLOCK_ERR_STOP_ANY;
211 } else if (!strcmp(buf, "report")) {
212 return BLOCK_ERR_REPORT;
213 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100214 error_report("'%s' invalid %s error action",
215 buf, is_read ? "read" : "write");
Markus Armbruster666daa62010-06-02 18:48:27 +0200216 return -1;
217 }
218}
219
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800220static bool do_check_io_limits(BlockIOLimit *io_limits)
221{
222 bool bps_flag;
223 bool iops_flag;
224
225 assert(io_limits);
226
227 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
228 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
229 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
230 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
231 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
232 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
233 if (bps_flag || iops_flag) {
234 return false;
235 }
236
237 return true;
238}
239
Markus Armbruster319ae522011-01-28 11:21:46 +0100240DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
Markus Armbruster666daa62010-06-02 18:48:27 +0200241{
242 const char *buf;
243 const char *file = NULL;
244 char devname[128];
245 const char *serial;
246 const char *mediastr = "";
247 BlockInterfaceType type;
248 enum { MEDIA_DISK, MEDIA_CDROM } media;
249 int bus_id, unit_id;
250 int cyls, heads, secs, translation;
251 BlockDriver *drv = NULL;
252 int max_devs;
253 int index;
254 int ro = 0;
255 int bdrv_flags = 0;
256 int on_read_error, on_write_error;
257 const char *devaddr;
258 DriveInfo *dinfo;
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800259 BlockIOLimit io_limits;
Markus Armbruster666daa62010-06-02 18:48:27 +0200260 int snapshot = 0;
Stefan Hajnoczifb0490f2011-11-17 13:40:32 +0000261 bool copy_on_read;
Markus Armbruster666daa62010-06-02 18:48:27 +0200262 int ret;
263
Markus Armbruster666daa62010-06-02 18:48:27 +0200264 translation = BIOS_ATA_TRANSLATION_AUTO;
Markus Armbruster666daa62010-06-02 18:48:27 +0200265 media = MEDIA_DISK;
266
267 /* extract parameters */
268 bus_id = qemu_opt_get_number(opts, "bus", 0);
269 unit_id = qemu_opt_get_number(opts, "unit", -1);
270 index = qemu_opt_get_number(opts, "index", -1);
271
272 cyls = qemu_opt_get_number(opts, "cyls", 0);
273 heads = qemu_opt_get_number(opts, "heads", 0);
274 secs = qemu_opt_get_number(opts, "secs", 0);
275
276 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
277 ro = qemu_opt_get_bool(opts, "readonly", 0);
Stefan Hajnoczifb0490f2011-11-17 13:40:32 +0000278 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
Markus Armbruster666daa62010-06-02 18:48:27 +0200279
280 file = qemu_opt_get(opts, "file");
281 serial = qemu_opt_get(opts, "serial");
282
283 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
284 pstrcpy(devname, sizeof(devname), buf);
Markus Armbruster19609662011-01-28 11:21:39 +0100285 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
286 ;
287 if (type == IF_COUNT) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100288 error_report("unsupported bus type '%s'", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200289 return NULL;
290 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300291 } else {
292 type = default_to_scsi ? IF_SCSI : IF_IDE;
293 pstrcpy(devname, sizeof(devname), if_name[type]);
Markus Armbruster666daa62010-06-02 18:48:27 +0200294 }
Luiz Capitulino2d3999f2011-07-01 10:46:12 -0300295
Markus Armbruster19609662011-01-28 11:21:39 +0100296 max_devs = if_max_devs[type];
Markus Armbruster666daa62010-06-02 18:48:27 +0200297
298 if (cyls || heads || secs) {
299 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100300 error_report("invalid physical cyls number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200301 return NULL;
302 }
303 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100304 error_report("invalid physical heads number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200305 return NULL;
306 }
307 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100308 error_report("invalid physical secs number");
Markus Armbruster666daa62010-06-02 18:48:27 +0200309 return NULL;
310 }
311 }
312
313 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
314 if (!cyls) {
Markus Armbrustere4080f92011-06-22 14:03:57 +0200315 error_report("'%s' trans must be used with cyls, heads and secs",
Markus Armbruster807105a2011-01-17 19:31:27 +0100316 buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200317 return NULL;
318 }
319 if (!strcmp(buf, "none"))
320 translation = BIOS_ATA_TRANSLATION_NONE;
321 else if (!strcmp(buf, "lba"))
322 translation = BIOS_ATA_TRANSLATION_LBA;
323 else if (!strcmp(buf, "auto"))
324 translation = BIOS_ATA_TRANSLATION_AUTO;
325 else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100326 error_report("'%s' invalid translation type", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200327 return NULL;
328 }
329 }
330
331 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
332 if (!strcmp(buf, "disk")) {
333 media = MEDIA_DISK;
334 } else if (!strcmp(buf, "cdrom")) {
335 if (cyls || secs || heads) {
Luiz Capitulinoe7ff8f02011-07-01 10:46:13 -0300336 error_report("CHS can't be set with media=%s", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200337 return NULL;
338 }
339 media = MEDIA_CDROM;
340 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100341 error_report("'%s' invalid media", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200342 return NULL;
343 }
344 }
345
346 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100347 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
348 error_report("invalid cache option");
349 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200350 }
351 }
352
353#ifdef CONFIG_LINUX_AIO
354 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
355 if (!strcmp(buf, "native")) {
356 bdrv_flags |= BDRV_O_NATIVE_AIO;
357 } else if (!strcmp(buf, "threads")) {
358 /* this is the default */
359 } else {
Markus Armbruster807105a2011-01-17 19:31:27 +0100360 error_report("invalid aio option");
Markus Armbruster666daa62010-06-02 18:48:27 +0200361 return NULL;
362 }
363 }
364#endif
365
366 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
367 if (strcmp(buf, "?") == 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100368 error_printf("Supported formats:");
369 bdrv_iterate_format(bdrv_format_print, NULL);
370 error_printf("\n");
371 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200372 }
373 drv = bdrv_find_whitelisted_format(buf);
374 if (!drv) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100375 error_report("'%s' invalid format", buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200376 return NULL;
377 }
378 }
379
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800380 /* disk I/O throttling */
381 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
382 qemu_opt_get_number(opts, "bps", 0);
383 io_limits.bps[BLOCK_IO_LIMIT_READ] =
384 qemu_opt_get_number(opts, "bps_rd", 0);
385 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
386 qemu_opt_get_number(opts, "bps_wr", 0);
387 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
388 qemu_opt_get_number(opts, "iops", 0);
389 io_limits.iops[BLOCK_IO_LIMIT_READ] =
390 qemu_opt_get_number(opts, "iops_rd", 0);
391 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
392 qemu_opt_get_number(opts, "iops_wr", 0);
393
394 if (!do_check_io_limits(&io_limits)) {
395 error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
396 "cannot be used at the same time");
397 return NULL;
398 }
399
Markus Armbruster666daa62010-06-02 18:48:27 +0200400 on_write_error = BLOCK_ERR_STOP_ENOSPC;
401 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
402 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100403 error_report("werror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200404 return NULL;
405 }
406
407 on_write_error = parse_block_error_action(buf, 0);
408 if (on_write_error < 0) {
409 return NULL;
410 }
411 }
412
413 on_read_error = BLOCK_ERR_REPORT;
414 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolf5dba48a2010-10-25 14:52:21 +0200415 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100416 error_report("rerror is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200417 return NULL;
418 }
419
420 on_read_error = parse_block_error_action(buf, 1);
421 if (on_read_error < 0) {
422 return NULL;
423 }
424 }
425
426 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
427 if (type != IF_VIRTIO) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100428 error_report("addr is not supported by this bus type");
Markus Armbruster666daa62010-06-02 18:48:27 +0200429 return NULL;
430 }
431 }
432
433 /* compute bus and unit according index */
434
435 if (index != -1) {
436 if (bus_id != 0 || unit_id != -1) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100437 error_report("index cannot be used with bus and unit");
Markus Armbruster666daa62010-06-02 18:48:27 +0200438 return NULL;
439 }
Markus Armbruster505a7fb2011-01-28 11:21:43 +0100440 bus_id = drive_index_to_bus_id(type, index);
441 unit_id = drive_index_to_unit_id(type, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200442 }
443
444 /* if user doesn't specify a unit_id,
445 * try to find the first free
446 */
447
448 if (unit_id == -1) {
449 unit_id = 0;
450 while (drive_get(type, bus_id, unit_id) != NULL) {
451 unit_id++;
452 if (max_devs && unit_id >= max_devs) {
453 unit_id -= max_devs;
454 bus_id++;
455 }
456 }
457 }
458
459 /* check unit id */
460
461 if (max_devs && unit_id >= max_devs) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100462 error_report("unit %d too big (max is %d)",
463 unit_id, max_devs - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200464 return NULL;
465 }
466
467 /*
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100468 * catch multiple definitions
Markus Armbruster666daa62010-06-02 18:48:27 +0200469 */
470
471 if (drive_get(type, bus_id, unit_id) != NULL) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100472 error_report("drive with bus=%d, unit=%d (index=%d) exists",
473 bus_id, unit_id, index);
Markus Armbruster666daa62010-06-02 18:48:27 +0200474 return NULL;
475 }
476
477 /* init */
478
Anthony Liguori7267c092011-08-20 22:09:37 -0500479 dinfo = g_malloc0(sizeof(*dinfo));
Markus Armbruster666daa62010-06-02 18:48:27 +0200480 if ((buf = qemu_opts_id(opts)) != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500481 dinfo->id = g_strdup(buf);
Markus Armbruster666daa62010-06-02 18:48:27 +0200482 } else {
483 /* no id supplied -> create one */
Anthony Liguori7267c092011-08-20 22:09:37 -0500484 dinfo->id = g_malloc0(32);
Markus Armbruster666daa62010-06-02 18:48:27 +0200485 if (type == IF_IDE || type == IF_SCSI)
486 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
487 if (max_devs)
488 snprintf(dinfo->id, 32, "%s%i%s%i",
489 devname, bus_id, mediastr, unit_id);
490 else
491 snprintf(dinfo->id, 32, "%s%s%i",
492 devname, mediastr, unit_id);
493 }
494 dinfo->bdrv = bdrv_new(dinfo->id);
495 dinfo->devaddr = devaddr;
496 dinfo->type = type;
497 dinfo->bus = bus_id;
498 dinfo->unit = unit_id;
Markus Armbruster666daa62010-06-02 18:48:27 +0200499 dinfo->opts = opts;
Marcelo Tosatti84fb3922011-01-26 12:12:32 -0200500 dinfo->refcount = 1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200501 if (serial)
Luiz Capitulino653dbec2010-06-02 17:46:31 -0300502 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
Markus Armbruster666daa62010-06-02 18:48:27 +0200503 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
504
Markus Armbrusterabd7f682010-06-02 18:55:17 +0200505 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
506
Zhi Yong Wu0563e192011-11-03 16:57:25 +0800507 /* disk I/O throttling */
508 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
509
Markus Armbruster666daa62010-06-02 18:48:27 +0200510 switch(type) {
511 case IF_IDE:
512 case IF_SCSI:
513 case IF_XEN:
514 case IF_NONE:
515 switch(media) {
516 case MEDIA_DISK:
517 if (cyls != 0) {
518 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
519 bdrv_set_translation_hint(dinfo->bdrv, translation);
520 }
521 break;
522 case MEDIA_CDROM:
Markus Armbruster95b5edc2011-05-16 15:04:56 +0200523 dinfo->media_cd = 1;
Markus Armbruster666daa62010-06-02 18:48:27 +0200524 break;
525 }
526 break;
527 case IF_SD:
Markus Armbruster666daa62010-06-02 18:48:27 +0200528 case IF_FLOPPY:
Markus Armbruster666daa62010-06-02 18:48:27 +0200529 case IF_PFLASH:
530 case IF_MTD:
531 break;
532 case IF_VIRTIO:
533 /* add virtio block device */
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200534 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Alexander Graf29f82b32011-03-29 15:29:29 +0200535 qemu_opt_set(opts, "driver", "virtio-blk");
Markus Armbruster666daa62010-06-02 18:48:27 +0200536 qemu_opt_set(opts, "drive", dinfo->id);
537 if (devaddr)
538 qemu_opt_set(opts, "addr", devaddr);
539 break;
Markus Armbruster2292dda2011-01-28 11:21:41 +0100540 default:
Markus Armbruster666daa62010-06-02 18:48:27 +0200541 abort();
542 }
Markus Armbrusterdd5b0d72010-05-11 15:36:46 +0200543 if (!file || !*file) {
Markus Armbruster319ae522011-01-28 11:21:46 +0100544 return dinfo;
Markus Armbruster666daa62010-06-02 18:48:27 +0200545 }
546 if (snapshot) {
547 /* always use cache=unsafe with snapshot */
548 bdrv_flags &= ~BDRV_O_CACHE_MASK;
549 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
550 }
551
Stefan Hajnoczifb0490f2011-11-17 13:40:32 +0000552 if (copy_on_read) {
553 bdrv_flags |= BDRV_O_COPY_ON_READ;
554 }
555
Markus Armbruster666daa62010-06-02 18:48:27 +0200556 if (media == MEDIA_CDROM) {
557 /* CDROM is fine for any interface, don't check. */
558 ro = 1;
559 } else if (ro == 1) {
560 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100561 error_report("readonly not supported by this bus type");
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100562 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200563 }
564 }
565
566 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
567
568 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
569 if (ret < 0) {
Markus Armbruster807105a2011-01-17 19:31:27 +0100570 error_report("could not open disk image %s: %s",
571 file, strerror(-ret));
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100572 goto err;
Markus Armbruster666daa62010-06-02 18:48:27 +0200573 }
574
575 if (bdrv_key_required(dinfo->bdrv))
576 autostart = 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200577 return dinfo;
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100578
579err:
580 bdrv_delete(dinfo->bdrv);
Anthony Liguori7267c092011-08-20 22:09:37 -0500581 g_free(dinfo->id);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100582 QTAILQ_REMOVE(&drives, dinfo, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500583 g_free(dinfo);
Markus Armbrustera9ae2bf2011-02-08 15:12:39 +0100584 return NULL;
Markus Armbruster666daa62010-06-02 18:48:27 +0200585}
586
587void do_commit(Monitor *mon, const QDict *qdict)
588{
Markus Armbruster666daa62010-06-02 18:48:27 +0200589 const char *device = qdict_get_str(qdict, "device");
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200590 BlockDriverState *bs;
Markus Armbruster666daa62010-06-02 18:48:27 +0200591
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200592 if (!strcmp(device, "all")) {
593 bdrv_commit_all();
594 } else {
595 bs = bdrv_find(device);
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200596 if (!bs) {
597 qerror_report(QERR_DEVICE_NOT_FOUND, device);
598 return;
Markus Armbruster6ab4b5a2010-06-02 18:55:18 +0200599 }
Markus Armbrusterac59eb92010-06-02 18:55:19 +0200600 bdrv_commit(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200601 }
602}
603
Jes Sorensenf8882562010-12-16 13:52:16 +0100604int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
605{
606 const char *device = qdict_get_str(qdict, "device");
Jes Sorensend967b2f2011-07-11 20:01:09 +0200607 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
Jes Sorensenf8882562010-12-16 13:52:16 +0100608 const char *format = qdict_get_try_str(qdict, "format");
609 BlockDriverState *bs;
Jes Sorensen52f9a172011-03-09 11:20:30 +0100610 BlockDriver *drv, *old_drv, *proto_drv;
Jes Sorensenf8882562010-12-16 13:52:16 +0100611 int ret = 0;
612 int flags;
Jes Sorensen52f9a172011-03-09 11:20:30 +0100613 char old_filename[1024];
Jes Sorensenf8882562010-12-16 13:52:16 +0100614
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100615 if (!filename) {
Jes Sorensend967b2f2011-07-11 20:01:09 +0200616 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
Jes Sorensenc90f1b32011-01-06 17:02:23 +0100617 ret = -1;
618 goto out;
619 }
620
Jes Sorensenf8882562010-12-16 13:52:16 +0100621 bs = bdrv_find(device);
622 if (!bs) {
623 qerror_report(QERR_DEVICE_NOT_FOUND, device);
624 ret = -1;
625 goto out;
626 }
627
Jes Sorensen52f9a172011-03-09 11:20:30 +0100628 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
629
630 old_drv = bs->drv;
631 flags = bs->open_flags;
632
Jes Sorensenf8882562010-12-16 13:52:16 +0100633 if (!format) {
634 format = "qcow2";
635 }
636
637 drv = bdrv_find_format(format);
638 if (!drv) {
639 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
640 ret = -1;
641 goto out;
642 }
643
644 proto_drv = bdrv_find_protocol(filename);
645 if (!proto_drv) {
646 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
647 ret = -1;
648 goto out;
649 }
650
651 ret = bdrv_img_create(filename, format, bs->filename,
Jes Sorensen52f9a172011-03-09 11:20:30 +0100652 bs->drv->format_name, NULL, -1, flags);
Jes Sorensenf8882562010-12-16 13:52:16 +0100653 if (ret) {
654 goto out;
655 }
656
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000657 bdrv_drain_all();
Jes Sorensenf8882562010-12-16 13:52:16 +0100658 bdrv_flush(bs);
659
Jes Sorensenf8882562010-12-16 13:52:16 +0100660 bdrv_close(bs);
661 ret = bdrv_open(bs, filename, flags, drv);
662 /*
Jes Sorensen52f9a172011-03-09 11:20:30 +0100663 * If reopening the image file we just created fails, fall back
664 * and try to re-open the original image. If that fails too, we
665 * are in serious trouble.
Jes Sorensenf8882562010-12-16 13:52:16 +0100666 */
667 if (ret != 0) {
Jes Sorensen52f9a172011-03-09 11:20:30 +0100668 ret = bdrv_open(bs, old_filename, flags, old_drv);
669 if (ret != 0) {
670 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
671 } else {
672 qerror_report(QERR_OPEN_FILE_FAILED, filename);
673 }
Jes Sorensenf8882562010-12-16 13:52:16 +0100674 }
675out:
676 if (ret) {
677 ret = -1;
678 }
679
680 return ret;
681}
682
Markus Armbruster666daa62010-06-02 18:48:27 +0200683static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
684{
Markus Armbruster2c6942f2011-09-06 18:58:51 +0200685 if (!bdrv_dev_has_removable_media(bs)) {
Markus Armbrusterea8f9422011-07-20 18:23:35 +0200686 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
687 return -1;
688 }
Paolo Bonzini025ccaa2011-11-07 17:50:13 +0100689 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
690 bdrv_dev_eject_request(bs, force);
691 if (!force) {
692 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
693 return -1;
694 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200695 }
Eduardo Habkost3b5276b2010-06-01 19:12:19 -0300696 bdrv_close(bs);
Markus Armbruster666daa62010-06-02 18:48:27 +0200697 return 0;
698}
699
700int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
701{
702 BlockDriverState *bs;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -0300703 int force = qdict_get_try_bool(qdict, "force", 0);
Markus Armbruster666daa62010-06-02 18:48:27 +0200704 const char *filename = qdict_get_str(qdict, "device");
705
706 bs = bdrv_find(filename);
707 if (!bs) {
708 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
709 return -1;
710 }
711 return eject_device(mon, bs, force);
712}
713
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200714void qmp_block_passwd(const char *device, const char *password, Error **errp)
Markus Armbruster666daa62010-06-02 18:48:27 +0200715{
716 BlockDriverState *bs;
717 int err;
718
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200719 bs = bdrv_find(device);
Markus Armbruster666daa62010-06-02 18:48:27 +0200720 if (!bs) {
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200721 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
722 return;
Markus Armbruster666daa62010-06-02 18:48:27 +0200723 }
724
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200725 err = bdrv_set_key(bs, password);
Markus Armbruster666daa62010-06-02 18:48:27 +0200726 if (err == -EINVAL) {
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200727 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
728 return;
Markus Armbruster666daa62010-06-02 18:48:27 +0200729 } else if (err < 0) {
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200730 error_set(errp, QERR_INVALID_PASSWORD);
731 return;
Markus Armbruster666daa62010-06-02 18:48:27 +0200732 }
Markus Armbruster666daa62010-06-02 18:48:27 +0200733}
734
735int do_change_block(Monitor *mon, const char *device,
736 const char *filename, const char *fmt)
737{
738 BlockDriverState *bs;
739 BlockDriver *drv = NULL;
740 int bdrv_flags;
741
742 bs = bdrv_find(device);
743 if (!bs) {
744 qerror_report(QERR_DEVICE_NOT_FOUND, device);
745 return -1;
746 }
747 if (fmt) {
748 drv = bdrv_find_whitelisted_format(fmt);
749 if (!drv) {
750 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
751 return -1;
752 }
753 }
754 if (eject_device(mon, bs, 0) < 0) {
755 return -1;
756 }
Markus Armbruster528f7662010-06-25 15:26:48 +0200757 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
Blue Swirl199630b2010-07-25 20:49:34 +0000758 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
Markus Armbruster666daa62010-06-02 18:48:27 +0200759 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
760 qerror_report(QERR_OPEN_FILE_FAILED, filename);
761 return -1;
762 }
763 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
764}
Ryan Harper9063f812010-11-12 11:07:13 -0600765
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800766/* throttling disk I/O limits */
767int do_block_set_io_throttle(Monitor *mon,
768 const QDict *qdict, QObject **ret_data)
769{
770 BlockIOLimit io_limits;
771 const char *devname = qdict_get_str(qdict, "device");
772 BlockDriverState *bs;
773
774 io_limits.bps[BLOCK_IO_LIMIT_TOTAL]
775 = qdict_get_try_int(qdict, "bps", -1);
776 io_limits.bps[BLOCK_IO_LIMIT_READ]
777 = qdict_get_try_int(qdict, "bps_rd", -1);
778 io_limits.bps[BLOCK_IO_LIMIT_WRITE]
779 = qdict_get_try_int(qdict, "bps_wr", -1);
780 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]
781 = qdict_get_try_int(qdict, "iops", -1);
782 io_limits.iops[BLOCK_IO_LIMIT_READ]
783 = qdict_get_try_int(qdict, "iops_rd", -1);
784 io_limits.iops[BLOCK_IO_LIMIT_WRITE]
785 = qdict_get_try_int(qdict, "iops_wr", -1);
786
787 bs = bdrv_find(devname);
788 if (!bs) {
789 qerror_report(QERR_DEVICE_NOT_FOUND, devname);
790 return -1;
791 }
792
793 if ((io_limits.bps[BLOCK_IO_LIMIT_TOTAL] == -1)
794 || (io_limits.bps[BLOCK_IO_LIMIT_READ] == -1)
795 || (io_limits.bps[BLOCK_IO_LIMIT_WRITE] == -1)
796 || (io_limits.iops[BLOCK_IO_LIMIT_TOTAL] == -1)
797 || (io_limits.iops[BLOCK_IO_LIMIT_READ] == -1)
798 || (io_limits.iops[BLOCK_IO_LIMIT_WRITE] == -1)) {
799 qerror_report(QERR_MISSING_PARAMETER,
800 "bps/bps_rd/bps_wr/iops/iops_rd/iops_wr");
801 return -1;
802 }
803
804 if (!do_check_io_limits(&io_limits)) {
805 qerror_report(QERR_INVALID_PARAMETER_COMBINATION);
806 return -1;
807 }
808
809 bs->io_limits = io_limits;
810 bs->slice_time = BLOCK_IO_SLICE_TIME;
811
812 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
813 bdrv_io_limits_enable(bs);
814 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
815 bdrv_io_limits_disable(bs);
816 } else {
817 if (bs->block_timer) {
818 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
819 }
820 }
821
822 return 0;
823}
824
Ryan Harper9063f812010-11-12 11:07:13 -0600825int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
826{
827 const char *id = qdict_get_str(qdict, "id");
828 BlockDriverState *bs;
Ryan Harper9063f812010-11-12 11:07:13 -0600829
830 bs = bdrv_find(id);
831 if (!bs) {
832 qerror_report(QERR_DEVICE_NOT_FOUND, id);
833 return -1;
834 }
Marcelo Tosatti85916752011-01-26 12:12:35 -0200835 if (bdrv_in_use(bs)) {
836 qerror_report(QERR_DEVICE_IN_USE, id);
837 return -1;
838 }
Ryan Harper9063f812010-11-12 11:07:13 -0600839
840 /* quiesce block driver; prevent further io */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000841 bdrv_drain_all();
Ryan Harper9063f812010-11-12 11:07:13 -0600842 bdrv_flush(bs);
843 bdrv_close(bs);
844
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200845 /* if we have a device attached to this BlockDriverState
Ryan Harperd22b2f42011-03-29 20:51:47 -0500846 * then we need to make the drive anonymous until the device
847 * can be removed. If this is a drive with no device backing
848 * then we can just get rid of the block driver state right here.
849 */
Markus Armbrusterfa879d62011-08-03 15:07:40 +0200850 if (bdrv_get_attached_dev(bs)) {
Ryan Harperd22b2f42011-03-29 20:51:47 -0500851 bdrv_make_anon(bs);
852 } else {
853 drive_uninit(drive_get_by_blockdev(bs));
Ryan Harper9063f812010-11-12 11:07:13 -0600854 }
855
Ryan Harper9063f812010-11-12 11:07:13 -0600856 return 0;
857}
Christoph Hellwig6d4a2b32011-01-24 13:32:33 +0100858
859/*
860 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
861 * existing QERR_ macro mess is cleaned up. A good example for better
862 * error reports can be found in the qemu-img resize code.
863 */
864int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
865{
866 const char *device = qdict_get_str(qdict, "device");
867 int64_t size = qdict_get_int(qdict, "size");
868 BlockDriverState *bs;
869
870 bs = bdrv_find(device);
871 if (!bs) {
872 qerror_report(QERR_DEVICE_NOT_FOUND, device);
873 return -1;
874 }
875
876 if (size < 0) {
877 qerror_report(QERR_UNDEFINED_ERROR);
878 return -1;
879 }
880
881 if (bdrv_truncate(bs, size)) {
882 qerror_report(QERR_UNDEFINED_ERROR);
883 return -1;
884 }
885
886 return 0;
887}