Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 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. |
| 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 Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 17 | #include "hw/qdev.h" |
| 18 | #include "block_int.h" |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 19 | |
Markus Armbruster | c9b62a7 | 2010-06-02 18:55:22 +0200 | [diff] [blame] | 20 | static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 21 | |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 22 | /* |
| 23 | * We automatically delete the drive when a device using it gets |
| 24 | * unplugged. Questionable feature, but we can't just drop it. |
| 25 | * Device models call blockdev_mark_auto_del() to schedule the |
| 26 | * automatic deletion, and generic qdev code calls blockdev_auto_del() |
| 27 | * when deletion is actually safe. |
| 28 | */ |
| 29 | void blockdev_mark_auto_del(BlockDriverState *bs) |
| 30 | { |
| 31 | DriveInfo *dinfo = drive_get_by_blockdev(bs); |
| 32 | |
Ryan Harper | 0fc0f1f | 2010-12-08 10:05:00 -0600 | [diff] [blame] | 33 | if (dinfo) { |
| 34 | dinfo->auto_del = 1; |
| 35 | } |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void blockdev_auto_del(BlockDriverState *bs) |
| 39 | { |
| 40 | DriveInfo *dinfo = drive_get_by_blockdev(bs); |
| 41 | |
Ryan Harper | 0fc0f1f | 2010-12-08 10:05:00 -0600 | [diff] [blame] | 42 | if (dinfo && dinfo->auto_del) { |
Markus Armbruster | 14bafc5 | 2010-06-25 08:09:10 +0200 | [diff] [blame] | 43 | drive_uninit(dinfo); |
| 44 | } |
| 45 | } |
| 46 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 47 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
| 48 | { |
| 49 | va_list ap; |
| 50 | char optstr[1024]; |
| 51 | QemuOpts *opts; |
| 52 | |
| 53 | va_start(ap, fmt); |
| 54 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
| 55 | va_end(ap); |
| 56 | |
Gerd Hoffmann | 3329f07 | 2010-08-20 13:52:01 +0200 | [diff] [blame] | 57 | opts = qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 58 | if (!opts) { |
| 59 | return NULL; |
| 60 | } |
| 61 | if (file) |
| 62 | qemu_opt_set(opts, "file", file); |
| 63 | return opts; |
| 64 | } |
| 65 | |
| 66 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
| 67 | { |
| 68 | DriveInfo *dinfo; |
| 69 | |
| 70 | /* seek interface, bus and unit */ |
| 71 | |
| 72 | QTAILQ_FOREACH(dinfo, &drives, next) { |
| 73 | if (dinfo->type == type && |
| 74 | dinfo->bus == bus && |
| 75 | dinfo->unit == unit) |
| 76 | return dinfo; |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 82 | int drive_get_max_bus(BlockInterfaceType type) |
| 83 | { |
| 84 | int max_bus; |
| 85 | DriveInfo *dinfo; |
| 86 | |
| 87 | max_bus = -1; |
| 88 | QTAILQ_FOREACH(dinfo, &drives, next) { |
| 89 | if(dinfo->type == type && |
| 90 | dinfo->bus > max_bus) |
| 91 | max_bus = dinfo->bus; |
| 92 | } |
| 93 | return max_bus; |
| 94 | } |
| 95 | |
Markus Armbruster | e4700e5 | 2010-06-24 17:25:32 +0200 | [diff] [blame] | 96 | DriveInfo *drive_get_by_blockdev(BlockDriverState *bs) |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 97 | { |
| 98 | DriveInfo *dinfo; |
| 99 | |
| 100 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Markus Armbruster | e4700e5 | 2010-06-24 17:25:32 +0200 | [diff] [blame] | 101 | if (dinfo->bdrv == bs) { |
| 102 | return dinfo; |
| 103 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 104 | } |
Markus Armbruster | e4700e5 | 2010-06-24 17:25:32 +0200 | [diff] [blame] | 105 | return NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 108 | static void bdrv_format_print(void *opaque, const char *name) |
| 109 | { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 110 | error_printf(" %s", name); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void drive_uninit(DriveInfo *dinfo) |
| 114 | { |
| 115 | qemu_opts_del(dinfo->opts); |
| 116 | bdrv_delete(dinfo->bdrv); |
| 117 | QTAILQ_REMOVE(&drives, dinfo, next); |
| 118 | qemu_free(dinfo); |
| 119 | } |
| 120 | |
| 121 | static int parse_block_error_action(const char *buf, int is_read) |
| 122 | { |
| 123 | if (!strcmp(buf, "ignore")) { |
| 124 | return BLOCK_ERR_IGNORE; |
| 125 | } else if (!is_read && !strcmp(buf, "enospc")) { |
| 126 | return BLOCK_ERR_STOP_ENOSPC; |
| 127 | } else if (!strcmp(buf, "stop")) { |
| 128 | return BLOCK_ERR_STOP_ANY; |
| 129 | } else if (!strcmp(buf, "report")) { |
| 130 | return BLOCK_ERR_REPORT; |
| 131 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 132 | error_report("'%s' invalid %s error action", |
| 133 | buf, is_read ? "read" : "write"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error) |
| 139 | { |
| 140 | const char *buf; |
| 141 | const char *file = NULL; |
| 142 | char devname[128]; |
| 143 | const char *serial; |
| 144 | const char *mediastr = ""; |
| 145 | BlockInterfaceType type; |
| 146 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 147 | int bus_id, unit_id; |
| 148 | int cyls, heads, secs, translation; |
| 149 | BlockDriver *drv = NULL; |
| 150 | int max_devs; |
| 151 | int index; |
| 152 | int ro = 0; |
| 153 | int bdrv_flags = 0; |
| 154 | int on_read_error, on_write_error; |
| 155 | const char *devaddr; |
| 156 | DriveInfo *dinfo; |
| 157 | int snapshot = 0; |
| 158 | int ret; |
| 159 | |
| 160 | *fatal_error = 1; |
| 161 | |
| 162 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 163 | |
| 164 | if (default_to_scsi) { |
| 165 | type = IF_SCSI; |
| 166 | max_devs = MAX_SCSI_DEVS; |
| 167 | pstrcpy(devname, sizeof(devname), "scsi"); |
| 168 | } else { |
| 169 | type = IF_IDE; |
| 170 | max_devs = MAX_IDE_DEVS; |
| 171 | pstrcpy(devname, sizeof(devname), "ide"); |
| 172 | } |
| 173 | media = MEDIA_DISK; |
| 174 | |
| 175 | /* extract parameters */ |
| 176 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 177 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 178 | index = qemu_opt_get_number(opts, "index", -1); |
| 179 | |
| 180 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 181 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 182 | secs = qemu_opt_get_number(opts, "secs", 0); |
| 183 | |
| 184 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
| 185 | ro = qemu_opt_get_bool(opts, "readonly", 0); |
| 186 | |
| 187 | file = qemu_opt_get(opts, "file"); |
| 188 | serial = qemu_opt_get(opts, "serial"); |
| 189 | |
| 190 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
| 191 | pstrcpy(devname, sizeof(devname), buf); |
| 192 | if (!strcmp(buf, "ide")) { |
| 193 | type = IF_IDE; |
| 194 | max_devs = MAX_IDE_DEVS; |
| 195 | } else if (!strcmp(buf, "scsi")) { |
| 196 | type = IF_SCSI; |
| 197 | max_devs = MAX_SCSI_DEVS; |
| 198 | } else if (!strcmp(buf, "floppy")) { |
| 199 | type = IF_FLOPPY; |
| 200 | max_devs = 0; |
| 201 | } else if (!strcmp(buf, "pflash")) { |
| 202 | type = IF_PFLASH; |
| 203 | max_devs = 0; |
| 204 | } else if (!strcmp(buf, "mtd")) { |
| 205 | type = IF_MTD; |
| 206 | max_devs = 0; |
| 207 | } else if (!strcmp(buf, "sd")) { |
| 208 | type = IF_SD; |
| 209 | max_devs = 0; |
| 210 | } else if (!strcmp(buf, "virtio")) { |
| 211 | type = IF_VIRTIO; |
| 212 | max_devs = 0; |
| 213 | } else if (!strcmp(buf, "xen")) { |
| 214 | type = IF_XEN; |
| 215 | max_devs = 0; |
| 216 | } else if (!strcmp(buf, "none")) { |
| 217 | type = IF_NONE; |
| 218 | max_devs = 0; |
| 219 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 220 | error_report("unsupported bus type '%s'", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (cyls || heads || secs) { |
| 226 | if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 227 | error_report("invalid physical cyls number"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 228 | return NULL; |
| 229 | } |
| 230 | if (heads < 1 || (type == IF_IDE && heads > 16)) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 231 | error_report("invalid physical heads number"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | if (secs < 1 || (type == IF_IDE && secs > 63)) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 235 | error_report("invalid physical secs number"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 236 | return NULL; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
| 241 | if (!cyls) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 242 | error_report("'%s' trans must be used with cyls,heads and secs", |
| 243 | buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 244 | return NULL; |
| 245 | } |
| 246 | if (!strcmp(buf, "none")) |
| 247 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 248 | else if (!strcmp(buf, "lba")) |
| 249 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 250 | else if (!strcmp(buf, "auto")) |
| 251 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 252 | else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 253 | error_report("'%s' invalid translation type", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
| 259 | if (!strcmp(buf, "disk")) { |
| 260 | media = MEDIA_DISK; |
| 261 | } else if (!strcmp(buf, "cdrom")) { |
| 262 | if (cyls || secs || heads) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 263 | error_report("'%s' invalid physical CHS format", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 264 | return NULL; |
| 265 | } |
| 266 | media = MEDIA_CDROM; |
| 267 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 268 | error_report("'%s' invalid media", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 269 | return NULL; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
| 274 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) { |
| 275 | bdrv_flags |= BDRV_O_NOCACHE; |
| 276 | } else if (!strcmp(buf, "writeback")) { |
| 277 | bdrv_flags |= BDRV_O_CACHE_WB; |
| 278 | } else if (!strcmp(buf, "unsafe")) { |
| 279 | bdrv_flags |= BDRV_O_CACHE_WB; |
| 280 | bdrv_flags |= BDRV_O_NO_FLUSH; |
| 281 | } else if (!strcmp(buf, "writethrough")) { |
| 282 | /* this is the default */ |
| 283 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 284 | error_report("invalid cache option"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 285 | return NULL; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | #ifdef CONFIG_LINUX_AIO |
| 290 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 291 | if (!strcmp(buf, "native")) { |
| 292 | bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 293 | } else if (!strcmp(buf, "threads")) { |
| 294 | /* this is the default */ |
| 295 | } else { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 296 | error_report("invalid aio option"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
| 303 | if (strcmp(buf, "?") == 0) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 304 | error_printf("Supported formats:"); |
| 305 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 306 | error_printf("\n"); |
| 307 | return NULL; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 308 | } |
| 309 | drv = bdrv_find_whitelisted_format(buf); |
| 310 | if (!drv) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 311 | error_report("'%s' invalid format", buf); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 312 | return NULL; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | on_write_error = BLOCK_ERR_STOP_ENOSPC; |
| 317 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
| 318 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 319 | error_report("werror is not supported by this bus type"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | on_write_error = parse_block_error_action(buf, 0); |
| 324 | if (on_write_error < 0) { |
| 325 | return NULL; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | on_read_error = BLOCK_ERR_REPORT; |
| 330 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
Kevin Wolf | 5dba48a | 2010-10-25 14:52:21 +0200 | [diff] [blame] | 331 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 332 | error_report("rerror is not supported by this bus type"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | on_read_error = parse_block_error_action(buf, 1); |
| 337 | if (on_read_error < 0) { |
| 338 | return NULL; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
| 343 | if (type != IF_VIRTIO) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 344 | error_report("addr is not supported by this bus type"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 345 | return NULL; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /* compute bus and unit according index */ |
| 350 | |
| 351 | if (index != -1) { |
| 352 | if (bus_id != 0 || unit_id != -1) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 353 | error_report("index cannot be used with bus and unit"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
| 356 | if (max_devs == 0) |
| 357 | { |
| 358 | unit_id = index; |
| 359 | bus_id = 0; |
| 360 | } else { |
| 361 | unit_id = index % max_devs; |
| 362 | bus_id = index / max_devs; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /* if user doesn't specify a unit_id, |
| 367 | * try to find the first free |
| 368 | */ |
| 369 | |
| 370 | if (unit_id == -1) { |
| 371 | unit_id = 0; |
| 372 | while (drive_get(type, bus_id, unit_id) != NULL) { |
| 373 | unit_id++; |
| 374 | if (max_devs && unit_id >= max_devs) { |
| 375 | unit_id -= max_devs; |
| 376 | bus_id++; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* check unit id */ |
| 382 | |
| 383 | if (max_devs && unit_id >= max_devs) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 384 | error_report("unit %d too big (max is %d)", |
| 385 | unit_id, max_devs - 1); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * ignore multiple definitions |
| 391 | */ |
| 392 | |
| 393 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 394 | *fatal_error = 0; |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | /* init */ |
| 399 | |
| 400 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
| 401 | if ((buf = qemu_opts_id(opts)) != NULL) { |
| 402 | dinfo->id = qemu_strdup(buf); |
| 403 | } else { |
| 404 | /* no id supplied -> create one */ |
| 405 | dinfo->id = qemu_mallocz(32); |
| 406 | if (type == IF_IDE || type == IF_SCSI) |
| 407 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 408 | if (max_devs) |
| 409 | snprintf(dinfo->id, 32, "%s%i%s%i", |
| 410 | devname, bus_id, mediastr, unit_id); |
| 411 | else |
| 412 | snprintf(dinfo->id, 32, "%s%s%i", |
| 413 | devname, mediastr, unit_id); |
| 414 | } |
| 415 | dinfo->bdrv = bdrv_new(dinfo->id); |
| 416 | dinfo->devaddr = devaddr; |
| 417 | dinfo->type = type; |
| 418 | dinfo->bus = bus_id; |
| 419 | dinfo->unit = unit_id; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 420 | dinfo->opts = opts; |
| 421 | if (serial) |
Luiz Capitulino | 653dbec | 2010-06-02 17:46:31 -0300 | [diff] [blame] | 422 | strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 423 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
| 424 | |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 425 | bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); |
| 426 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 427 | switch(type) { |
| 428 | case IF_IDE: |
| 429 | case IF_SCSI: |
| 430 | case IF_XEN: |
| 431 | case IF_NONE: |
| 432 | switch(media) { |
| 433 | case MEDIA_DISK: |
| 434 | if (cyls != 0) { |
| 435 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
| 436 | bdrv_set_translation_hint(dinfo->bdrv, translation); |
| 437 | } |
| 438 | break; |
| 439 | case MEDIA_CDROM: |
| 440 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
| 441 | break; |
| 442 | } |
| 443 | break; |
| 444 | case IF_SD: |
| 445 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 446 | approximation. */ |
| 447 | case IF_FLOPPY: |
| 448 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
| 449 | break; |
| 450 | case IF_PFLASH: |
| 451 | case IF_MTD: |
| 452 | break; |
| 453 | case IF_VIRTIO: |
| 454 | /* add virtio block device */ |
Gerd Hoffmann | 3329f07 | 2010-08-20 13:52:01 +0200 | [diff] [blame] | 455 | opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 456 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
| 457 | qemu_opt_set(opts, "drive", dinfo->id); |
| 458 | if (devaddr) |
| 459 | qemu_opt_set(opts, "addr", devaddr); |
| 460 | break; |
| 461 | case IF_COUNT: |
| 462 | abort(); |
| 463 | } |
Markus Armbruster | dd5b0d7 | 2010-05-11 15:36:46 +0200 | [diff] [blame] | 464 | if (!file || !*file) { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 465 | *fatal_error = 0; |
| 466 | return NULL; |
| 467 | } |
| 468 | if (snapshot) { |
| 469 | /* always use cache=unsafe with snapshot */ |
| 470 | bdrv_flags &= ~BDRV_O_CACHE_MASK; |
| 471 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); |
| 472 | } |
| 473 | |
| 474 | if (media == MEDIA_CDROM) { |
| 475 | /* CDROM is fine for any interface, don't check. */ |
| 476 | ro = 1; |
| 477 | } else if (ro == 1) { |
| 478 | if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 479 | error_report("readonly not supported by this bus type"); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 480 | return NULL; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; |
| 485 | |
| 486 | ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv); |
| 487 | if (ret < 0) { |
Markus Armbruster | 807105a | 2011-01-17 19:31:27 +0100 | [diff] [blame] | 488 | error_report("could not open disk image %s: %s", |
| 489 | file, strerror(-ret)); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | if (bdrv_key_required(dinfo->bdrv)) |
| 494 | autostart = 0; |
| 495 | *fatal_error = 0; |
| 496 | return dinfo; |
| 497 | } |
| 498 | |
| 499 | void do_commit(Monitor *mon, const QDict *qdict) |
| 500 | { |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 501 | const char *device = qdict_get_str(qdict, "device"); |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 502 | BlockDriverState *bs; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 503 | |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 504 | if (!strcmp(device, "all")) { |
| 505 | bdrv_commit_all(); |
| 506 | } else { |
| 507 | bs = bdrv_find(device); |
Markus Armbruster | ac59eb9 | 2010-06-02 18:55:19 +0200 | [diff] [blame] | 508 | if (!bs) { |
| 509 | qerror_report(QERR_DEVICE_NOT_FOUND, device); |
| 510 | return; |
Markus Armbruster | 6ab4b5a | 2010-06-02 18:55:18 +0200 | [diff] [blame] | 511 | } |
Markus Armbruster | ac59eb9 | 2010-06-02 18:55:19 +0200 | [diff] [blame] | 512 | bdrv_commit(bs); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Jes Sorensen | f888256 | 2010-12-16 13:52:16 +0100 | [diff] [blame] | 516 | int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data) |
| 517 | { |
| 518 | const char *device = qdict_get_str(qdict, "device"); |
| 519 | const char *filename = qdict_get_try_str(qdict, "snapshot_file"); |
| 520 | const char *format = qdict_get_try_str(qdict, "format"); |
| 521 | BlockDriverState *bs; |
| 522 | BlockDriver *drv, *proto_drv; |
| 523 | int ret = 0; |
| 524 | int flags; |
| 525 | |
Jes Sorensen | c90f1b3 | 2011-01-06 17:02:23 +0100 | [diff] [blame] | 526 | if (!filename) { |
| 527 | qerror_report(QERR_MISSING_PARAMETER, "snapshot_file"); |
| 528 | ret = -1; |
| 529 | goto out; |
| 530 | } |
| 531 | |
Jes Sorensen | f888256 | 2010-12-16 13:52:16 +0100 | [diff] [blame] | 532 | bs = bdrv_find(device); |
| 533 | if (!bs) { |
| 534 | qerror_report(QERR_DEVICE_NOT_FOUND, device); |
| 535 | ret = -1; |
| 536 | goto out; |
| 537 | } |
| 538 | |
| 539 | if (!format) { |
| 540 | format = "qcow2"; |
| 541 | } |
| 542 | |
| 543 | drv = bdrv_find_format(format); |
| 544 | if (!drv) { |
| 545 | qerror_report(QERR_INVALID_BLOCK_FORMAT, format); |
| 546 | ret = -1; |
| 547 | goto out; |
| 548 | } |
| 549 | |
| 550 | proto_drv = bdrv_find_protocol(filename); |
| 551 | if (!proto_drv) { |
| 552 | qerror_report(QERR_INVALID_BLOCK_FORMAT, format); |
| 553 | ret = -1; |
| 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | ret = bdrv_img_create(filename, format, bs->filename, |
| 558 | bs->drv->format_name, NULL, -1, bs->open_flags); |
| 559 | if (ret) { |
| 560 | goto out; |
| 561 | } |
| 562 | |
| 563 | qemu_aio_flush(); |
| 564 | bdrv_flush(bs); |
| 565 | |
| 566 | flags = bs->open_flags; |
| 567 | bdrv_close(bs); |
| 568 | ret = bdrv_open(bs, filename, flags, drv); |
| 569 | /* |
| 570 | * If reopening the image file we just created fails, we really |
| 571 | * are in trouble :( |
| 572 | */ |
| 573 | if (ret != 0) { |
| 574 | abort(); |
| 575 | } |
| 576 | out: |
| 577 | if (ret) { |
| 578 | ret = -1; |
| 579 | } |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 584 | static int eject_device(Monitor *mon, BlockDriverState *bs, int force) |
| 585 | { |
Eduardo Habkost | 3b5276b | 2010-06-01 19:12:19 -0300 | [diff] [blame] | 586 | if (!force) { |
| 587 | if (!bdrv_is_removable(bs)) { |
| 588 | qerror_report(QERR_DEVICE_NOT_REMOVABLE, |
| 589 | bdrv_get_device_name(bs)); |
| 590 | return -1; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 591 | } |
Eduardo Habkost | 3b5276b | 2010-06-01 19:12:19 -0300 | [diff] [blame] | 592 | if (bdrv_is_locked(bs)) { |
| 593 | qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); |
| 594 | return -1; |
| 595 | } |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 596 | } |
Eduardo Habkost | 3b5276b | 2010-06-01 19:12:19 -0300 | [diff] [blame] | 597 | bdrv_close(bs); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data) |
| 602 | { |
| 603 | BlockDriverState *bs; |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 604 | int force = qdict_get_try_bool(qdict, "force", 0); |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 605 | const char *filename = qdict_get_str(qdict, "device"); |
| 606 | |
| 607 | bs = bdrv_find(filename); |
| 608 | if (!bs) { |
| 609 | qerror_report(QERR_DEVICE_NOT_FOUND, filename); |
| 610 | return -1; |
| 611 | } |
| 612 | return eject_device(mon, bs, force); |
| 613 | } |
| 614 | |
| 615 | int do_block_set_passwd(Monitor *mon, const QDict *qdict, |
| 616 | QObject **ret_data) |
| 617 | { |
| 618 | BlockDriverState *bs; |
| 619 | int err; |
| 620 | |
| 621 | bs = bdrv_find(qdict_get_str(qdict, "device")); |
| 622 | if (!bs) { |
| 623 | qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device")); |
| 624 | return -1; |
| 625 | } |
| 626 | |
| 627 | err = bdrv_set_key(bs, qdict_get_str(qdict, "password")); |
| 628 | if (err == -EINVAL) { |
| 629 | qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
| 630 | return -1; |
| 631 | } else if (err < 0) { |
| 632 | qerror_report(QERR_INVALID_PASSWORD); |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | int do_change_block(Monitor *mon, const char *device, |
| 640 | const char *filename, const char *fmt) |
| 641 | { |
| 642 | BlockDriverState *bs; |
| 643 | BlockDriver *drv = NULL; |
| 644 | int bdrv_flags; |
| 645 | |
| 646 | bs = bdrv_find(device); |
| 647 | if (!bs) { |
| 648 | qerror_report(QERR_DEVICE_NOT_FOUND, device); |
| 649 | return -1; |
| 650 | } |
| 651 | if (fmt) { |
| 652 | drv = bdrv_find_whitelisted_format(fmt); |
| 653 | if (!drv) { |
| 654 | qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt); |
| 655 | return -1; |
| 656 | } |
| 657 | } |
| 658 | if (eject_device(mon, bs, 0) < 0) { |
| 659 | return -1; |
| 660 | } |
Markus Armbruster | 528f766 | 2010-06-25 15:26:48 +0200 | [diff] [blame] | 661 | bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; |
Blue Swirl | 199630b | 2010-07-25 20:49:34 +0000 | [diff] [blame] | 662 | bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; |
Markus Armbruster | 666daa6 | 2010-06-02 18:48:27 +0200 | [diff] [blame] | 663 | if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) { |
| 664 | qerror_report(QERR_OPEN_FILE_FAILED, filename); |
| 665 | return -1; |
| 666 | } |
| 667 | return monitor_read_bdrv_key_start(mon, bs, NULL, NULL); |
| 668 | } |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 669 | |
| 670 | int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
| 671 | { |
| 672 | const char *id = qdict_get_str(qdict, "id"); |
| 673 | BlockDriverState *bs; |
| 674 | BlockDriverState **ptr; |
| 675 | Property *prop; |
| 676 | |
| 677 | bs = bdrv_find(id); |
| 678 | if (!bs) { |
| 679 | qerror_report(QERR_DEVICE_NOT_FOUND, id); |
| 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | /* quiesce block driver; prevent further io */ |
| 684 | qemu_aio_flush(); |
| 685 | bdrv_flush(bs); |
| 686 | bdrv_close(bs); |
| 687 | |
| 688 | /* clean up guest state from pointing to host resource by |
| 689 | * finding and removing DeviceState "drive" property */ |
Markus Armbruster | 850ec11 | 2011-01-17 19:31:29 +0100 | [diff] [blame] | 690 | if (bs->peer) { |
| 691 | for (prop = bs->peer->info->props; prop && prop->name; prop++) { |
| 692 | if (prop->info->type == PROP_TYPE_DRIVE) { |
| 693 | ptr = qdev_get_prop_ptr(bs->peer, prop); |
| 694 | if (*ptr == bs) { |
| 695 | bdrv_detach(bs, bs->peer); |
| 696 | *ptr = NULL; |
| 697 | break; |
| 698 | } |
Ryan Harper | 9063f81 | 2010-11-12 11:07:13 -0600 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /* clean up host side */ |
| 704 | drive_uninit(drive_get_by_blockdev(bs)); |
| 705 | |
| 706 | return 0; |
| 707 | } |
Christoph Hellwig | 6d4a2b3 | 2011-01-24 13:32:33 +0100 | [diff] [blame^] | 708 | |
| 709 | /* |
| 710 | * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the |
| 711 | * existing QERR_ macro mess is cleaned up. A good example for better |
| 712 | * error reports can be found in the qemu-img resize code. |
| 713 | */ |
| 714 | int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data) |
| 715 | { |
| 716 | const char *device = qdict_get_str(qdict, "device"); |
| 717 | int64_t size = qdict_get_int(qdict, "size"); |
| 718 | BlockDriverState *bs; |
| 719 | |
| 720 | bs = bdrv_find(device); |
| 721 | if (!bs) { |
| 722 | qerror_report(QERR_DEVICE_NOT_FOUND, device); |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | if (size < 0) { |
| 727 | qerror_report(QERR_UNDEFINED_ERROR); |
| 728 | return -1; |
| 729 | } |
| 730 | |
| 731 | if (bdrv_truncate(bs, size)) { |
| 732 | qerror_report(QERR_UNDEFINED_ERROR); |
| 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | return 0; |
| 737 | } |