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