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