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