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