]>
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) { |
84fb3925 | 74 | drive_put_ref(dinfo); |
14bafc54 MA |
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, | |
5645b0f4 | 96 | const char *optstr) |
666daa68 | 97 | { |
666daa68 | 98 | QemuOpts *opts; |
2292ddae | 99 | char buf[32]; |
666daa68 | 100 | |
2292ddae | 101 | opts = drive_def(optstr); |
666daa68 MA |
102 | if (!opts) { |
103 | return NULL; | |
104 | } | |
2292ddae MA |
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 | } | |
666daa68 MA |
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 | ||
f1bd51ac MA |
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 | ||
666daa68 MA |
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 | ||
13839974 MA |
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 | ||
e4700e59 | 164 | DriveInfo *drive_get_by_blockdev(BlockDriverState *bs) |
666daa68 MA |
165 | { |
166 | DriveInfo *dinfo; | |
167 | ||
168 | QTAILQ_FOREACH(dinfo, &drives, next) { | |
e4700e59 MA |
169 | if (dinfo->bdrv == bs) { |
170 | return dinfo; | |
171 | } | |
666daa68 | 172 | } |
e4700e59 | 173 | return NULL; |
666daa68 MA |
174 | } |
175 | ||
666daa68 MA |
176 | static void bdrv_format_print(void *opaque, const char *name) |
177 | { | |
807105a7 | 178 | error_printf(" %s", name); |
666daa68 MA |
179 | } |
180 | ||
84fb3925 | 181 | static void drive_uninit(DriveInfo *dinfo) |
666daa68 MA |
182 | { |
183 | qemu_opts_del(dinfo->opts); | |
184 | bdrv_delete(dinfo->bdrv); | |
2753d4a5 | 185 | qemu_free(dinfo->id); |
666daa68 MA |
186 | QTAILQ_REMOVE(&drives, dinfo, next); |
187 | qemu_free(dinfo); | |
188 | } | |
189 | ||
84fb3925 MT |
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 | ||
666daa68 MA |
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 { | |
807105a7 MA |
214 | error_report("'%s' invalid %s error action", |
215 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
216 | return -1; |
217 | } | |
218 | } | |
219 | ||
319ae529 | 220 | DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi) |
666daa68 MA |
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 | ||
666daa68 MA |
242 | translation = BIOS_ATA_TRANSLATION_AUTO; |
243 | ||
244 | if (default_to_scsi) { | |
245 | type = IF_SCSI; | |
666daa68 MA |
246 | pstrcpy(devname, sizeof(devname), "scsi"); |
247 | } else { | |
248 | type = IF_IDE; | |
666daa68 MA |
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); | |
1960966d MA |
270 | for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++) |
271 | ; | |
272 | if (type == IF_COUNT) { | |
807105a7 | 273 | error_report("unsupported bus type '%s'", buf); |
666daa68 MA |
274 | return NULL; |
275 | } | |
276 | } | |
1960966d | 277 | max_devs = if_max_devs[type]; |
666daa68 MA |
278 | |
279 | if (cyls || heads || secs) { | |
280 | if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { | |
807105a7 | 281 | error_report("invalid physical cyls number"); |
666daa68 MA |
282 | return NULL; |
283 | } | |
284 | if (heads < 1 || (type == IF_IDE && heads > 16)) { | |
807105a7 | 285 | error_report("invalid physical heads number"); |
666daa68 MA |
286 | return NULL; |
287 | } | |
288 | if (secs < 1 || (type == IF_IDE && secs > 63)) { | |
807105a7 | 289 | error_report("invalid physical secs number"); |
666daa68 MA |
290 | return NULL; |
291 | } | |
292 | } | |
293 | ||
294 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { | |
295 | if (!cyls) { | |
807105a7 MA |
296 | error_report("'%s' trans must be used with cyls,heads and secs", |
297 | buf); | |
666daa68 MA |
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 { | |
807105a7 | 307 | error_report("'%s' invalid translation type", buf); |
666daa68 MA |
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) { | |
807105a7 | 317 | error_report("'%s' invalid physical CHS format", buf); |
666daa68 MA |
318 | return NULL; |
319 | } | |
320 | media = MEDIA_CDROM; | |
321 | } else { | |
807105a7 | 322 | error_report("'%s' invalid media", buf); |
666daa68 MA |
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 { | |
807105a7 | 338 | error_report("invalid cache option"); |
666daa68 MA |
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 { | |
807105a7 | 350 | error_report("invalid aio option"); |
666daa68 MA |
351 | return NULL; |
352 | } | |
353 | } | |
354 | #endif | |
355 | ||
356 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
357 | if (strcmp(buf, "?") == 0) { | |
807105a7 MA |
358 | error_printf("Supported formats:"); |
359 | bdrv_iterate_format(bdrv_format_print, NULL); | |
360 | error_printf("\n"); | |
361 | return NULL; | |
666daa68 MA |
362 | } |
363 | drv = bdrv_find_whitelisted_format(buf); | |
364 | if (!drv) { | |
807105a7 | 365 | error_report("'%s' invalid format", buf); |
666daa68 MA |
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) { | |
807105a7 | 373 | error_report("werror is not supported by this bus type"); |
666daa68 MA |
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) { | |
5dba48a8 | 385 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { |
807105a7 | 386 | error_report("rerror is not supported by this bus type"); |
666daa68 MA |
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) { | |
807105a7 | 398 | error_report("addr is not supported by this bus type"); |
666daa68 MA |
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) { | |
807105a7 | 407 | error_report("index cannot be used with bus and unit"); |
666daa68 MA |
408 | return NULL; |
409 | } | |
505a7fb1 MA |
410 | bus_id = drive_index_to_bus_id(type, index); |
411 | unit_id = drive_index_to_unit_id(type, index); | |
666daa68 MA |
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) { | |
807105a7 MA |
432 | error_report("unit %d too big (max is %d)", |
433 | unit_id, max_devs - 1); | |
666daa68 MA |
434 | return NULL; |
435 | } | |
436 | ||
437 | /* | |
4e5d9b57 | 438 | * catch multiple definitions |
666daa68 MA |
439 | */ |
440 | ||
441 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
4e5d9b57 MA |
442 | error_report("drive with bus=%d, unit=%d (index=%d) exists", |
443 | bus_id, unit_id, index); | |
666daa68 MA |
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; | |
666daa68 | 469 | dinfo->opts = opts; |
84fb3925 | 470 | dinfo->refcount = 1; |
666daa68 | 471 | if (serial) |
653dbec7 | 472 | strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1); |
666daa68 MA |
473 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
474 | ||
abd7f68d MA |
475 | bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); |
476 | ||
666daa68 MA |
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: | |
490 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); | |
491 | break; | |
492 | } | |
493 | break; | |
494 | case IF_SD: | |
495 | /* FIXME: This isn't really a floppy, but it's a reasonable | |
496 | approximation. */ | |
497 | case IF_FLOPPY: | |
498 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); | |
499 | break; | |
500 | case IF_PFLASH: | |
501 | case IF_MTD: | |
502 | break; | |
503 | case IF_VIRTIO: | |
504 | /* add virtio block device */ | |
3329f07b | 505 | opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0); |
666daa68 MA |
506 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
507 | qemu_opt_set(opts, "drive", dinfo->id); | |
508 | if (devaddr) | |
509 | qemu_opt_set(opts, "addr", devaddr); | |
510 | break; | |
2292ddae | 511 | default: |
666daa68 MA |
512 | abort(); |
513 | } | |
dd5b0d71 | 514 | if (!file || !*file) { |
319ae529 | 515 | return dinfo; |
666daa68 MA |
516 | } |
517 | if (snapshot) { | |
518 | /* always use cache=unsafe with snapshot */ | |
519 | bdrv_flags &= ~BDRV_O_CACHE_MASK; | |
520 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); | |
521 | } | |
522 | ||
523 | if (media == MEDIA_CDROM) { | |
524 | /* CDROM is fine for any interface, don't check. */ | |
525 | ro = 1; | |
526 | } else if (ro == 1) { | |
527 | if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) { | |
807105a7 | 528 | error_report("readonly not supported by this bus type"); |
a9ae2bff | 529 | goto err; |
666daa68 MA |
530 | } |
531 | } | |
532 | ||
533 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; | |
534 | ||
535 | ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv); | |
536 | if (ret < 0) { | |
807105a7 MA |
537 | error_report("could not open disk image %s: %s", |
538 | file, strerror(-ret)); | |
a9ae2bff | 539 | goto err; |
666daa68 MA |
540 | } |
541 | ||
542 | if (bdrv_key_required(dinfo->bdrv)) | |
543 | autostart = 0; | |
666daa68 | 544 | return dinfo; |
a9ae2bff MA |
545 | |
546 | err: | |
547 | bdrv_delete(dinfo->bdrv); | |
548 | qemu_free(dinfo->id); | |
549 | QTAILQ_REMOVE(&drives, dinfo, next); | |
550 | qemu_free(dinfo); | |
551 | return NULL; | |
666daa68 MA |
552 | } |
553 | ||
554 | void do_commit(Monitor *mon, const QDict *qdict) | |
555 | { | |
666daa68 | 556 | const char *device = qdict_get_str(qdict, "device"); |
6ab4b5ab | 557 | BlockDriverState *bs; |
666daa68 | 558 | |
6ab4b5ab MA |
559 | if (!strcmp(device, "all")) { |
560 | bdrv_commit_all(); | |
561 | } else { | |
562 | bs = bdrv_find(device); | |
ac59eb95 MA |
563 | if (!bs) { |
564 | qerror_report(QERR_DEVICE_NOT_FOUND, device); | |
565 | return; | |
6ab4b5ab | 566 | } |
ac59eb95 | 567 | bdrv_commit(bs); |
666daa68 MA |
568 | } |
569 | } | |
570 | ||
f8882568 JS |
571 | int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data) |
572 | { | |
573 | const char *device = qdict_get_str(qdict, "device"); | |
574 | const char *filename = qdict_get_try_str(qdict, "snapshot_file"); | |
575 | const char *format = qdict_get_try_str(qdict, "format"); | |
576 | BlockDriverState *bs; | |
52f9a172 | 577 | BlockDriver *drv, *old_drv, *proto_drv; |
f8882568 JS |
578 | int ret = 0; |
579 | int flags; | |
52f9a172 | 580 | char old_filename[1024]; |
f8882568 | 581 | |
c90f1b32 JS |
582 | if (!filename) { |
583 | qerror_report(QERR_MISSING_PARAMETER, "snapshot_file"); | |
584 | ret = -1; | |
585 | goto out; | |
586 | } | |
587 | ||
f8882568 JS |
588 | bs = bdrv_find(device); |
589 | if (!bs) { | |
590 | qerror_report(QERR_DEVICE_NOT_FOUND, device); | |
591 | ret = -1; | |
592 | goto out; | |
593 | } | |
594 | ||
52f9a172 JS |
595 | pstrcpy(old_filename, sizeof(old_filename), bs->filename); |
596 | ||
597 | old_drv = bs->drv; | |
598 | flags = bs->open_flags; | |
599 | ||
f8882568 JS |
600 | if (!format) { |
601 | format = "qcow2"; | |
602 | } | |
603 | ||
604 | drv = bdrv_find_format(format); | |
605 | if (!drv) { | |
606 | qerror_report(QERR_INVALID_BLOCK_FORMAT, format); | |
607 | ret = -1; | |
608 | goto out; | |
609 | } | |
610 | ||
611 | proto_drv = bdrv_find_protocol(filename); | |
612 | if (!proto_drv) { | |
613 | qerror_report(QERR_INVALID_BLOCK_FORMAT, format); | |
614 | ret = -1; | |
615 | goto out; | |
616 | } | |
617 | ||
618 | ret = bdrv_img_create(filename, format, bs->filename, | |
52f9a172 | 619 | bs->drv->format_name, NULL, -1, flags); |
f8882568 JS |
620 | if (ret) { |
621 | goto out; | |
622 | } | |
623 | ||
624 | qemu_aio_flush(); | |
625 | bdrv_flush(bs); | |
626 | ||
f8882568 JS |
627 | bdrv_close(bs); |
628 | ret = bdrv_open(bs, filename, flags, drv); | |
629 | /* | |
52f9a172 JS |
630 | * If reopening the image file we just created fails, fall back |
631 | * and try to re-open the original image. If that fails too, we | |
632 | * are in serious trouble. | |
f8882568 JS |
633 | */ |
634 | if (ret != 0) { | |
52f9a172 JS |
635 | ret = bdrv_open(bs, old_filename, flags, old_drv); |
636 | if (ret != 0) { | |
637 | qerror_report(QERR_OPEN_FILE_FAILED, old_filename); | |
638 | } else { | |
639 | qerror_report(QERR_OPEN_FILE_FAILED, filename); | |
640 | } | |
f8882568 JS |
641 | } |
642 | out: | |
643 | if (ret) { | |
644 | ret = -1; | |
645 | } | |
646 | ||
647 | return ret; | |
648 | } | |
649 | ||
666daa68 MA |
650 | static int eject_device(Monitor *mon, BlockDriverState *bs, int force) |
651 | { | |
3b5276b5 EH |
652 | if (!force) { |
653 | if (!bdrv_is_removable(bs)) { | |
654 | qerror_report(QERR_DEVICE_NOT_REMOVABLE, | |
655 | bdrv_get_device_name(bs)); | |
656 | return -1; | |
657 | } | |
658 | if (bdrv_is_locked(bs)) { | |
659 | qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); | |
660 | return -1; | |
666daa68 | 661 | } |
666daa68 | 662 | } |
3b5276b5 | 663 | bdrv_close(bs); |
666daa68 MA |
664 | return 0; |
665 | } | |
666 | ||
667 | int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
668 | { | |
669 | BlockDriverState *bs; | |
eb159d13 | 670 | int force = qdict_get_try_bool(qdict, "force", 0); |
666daa68 MA |
671 | const char *filename = qdict_get_str(qdict, "device"); |
672 | ||
673 | bs = bdrv_find(filename); | |
674 | if (!bs) { | |
675 | qerror_report(QERR_DEVICE_NOT_FOUND, filename); | |
676 | return -1; | |
677 | } | |
678 | return eject_device(mon, bs, force); | |
679 | } | |
680 | ||
681 | int do_block_set_passwd(Monitor *mon, const QDict *qdict, | |
682 | QObject **ret_data) | |
683 | { | |
684 | BlockDriverState *bs; | |
685 | int err; | |
686 | ||
687 | bs = bdrv_find(qdict_get_str(qdict, "device")); | |
688 | if (!bs) { | |
689 | qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device")); | |
690 | return -1; | |
691 | } | |
692 | ||
693 | err = bdrv_set_key(bs, qdict_get_str(qdict, "password")); | |
694 | if (err == -EINVAL) { | |
695 | qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); | |
696 | return -1; | |
697 | } else if (err < 0) { | |
698 | qerror_report(QERR_INVALID_PASSWORD); | |
699 | return -1; | |
700 | } | |
701 | ||
702 | return 0; | |
703 | } | |
704 | ||
705 | int do_change_block(Monitor *mon, const char *device, | |
706 | const char *filename, const char *fmt) | |
707 | { | |
708 | BlockDriverState *bs; | |
709 | BlockDriver *drv = NULL; | |
710 | int bdrv_flags; | |
711 | ||
712 | bs = bdrv_find(device); | |
713 | if (!bs) { | |
714 | qerror_report(QERR_DEVICE_NOT_FOUND, device); | |
715 | return -1; | |
716 | } | |
717 | if (fmt) { | |
718 | drv = bdrv_find_whitelisted_format(fmt); | |
719 | if (!drv) { | |
720 | qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt); | |
721 | return -1; | |
722 | } | |
723 | } | |
724 | if (eject_device(mon, bs, 0) < 0) { | |
725 | return -1; | |
726 | } | |
528f7663 | 727 | bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; |
199630b6 | 728 | bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; |
666daa68 MA |
729 | if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) { |
730 | qerror_report(QERR_OPEN_FILE_FAILED, filename); | |
731 | return -1; | |
732 | } | |
733 | return monitor_read_bdrv_key_start(mon, bs, NULL, NULL); | |
734 | } | |
9063f814 RH |
735 | |
736 | int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
737 | { | |
738 | const char *id = qdict_get_str(qdict, "id"); | |
739 | BlockDriverState *bs; | |
740 | BlockDriverState **ptr; | |
741 | Property *prop; | |
742 | ||
743 | bs = bdrv_find(id); | |
744 | if (!bs) { | |
745 | qerror_report(QERR_DEVICE_NOT_FOUND, id); | |
746 | return -1; | |
747 | } | |
8591675f MT |
748 | if (bdrv_in_use(bs)) { |
749 | qerror_report(QERR_DEVICE_IN_USE, id); | |
750 | return -1; | |
751 | } | |
9063f814 RH |
752 | |
753 | /* quiesce block driver; prevent further io */ | |
754 | qemu_aio_flush(); | |
755 | bdrv_flush(bs); | |
756 | bdrv_close(bs); | |
757 | ||
758 | /* clean up guest state from pointing to host resource by | |
759 | * finding and removing DeviceState "drive" property */ | |
850ec113 MA |
760 | if (bs->peer) { |
761 | for (prop = bs->peer->info->props; prop && prop->name; prop++) { | |
762 | if (prop->info->type == PROP_TYPE_DRIVE) { | |
763 | ptr = qdev_get_prop_ptr(bs->peer, prop); | |
764 | if (*ptr == bs) { | |
765 | bdrv_detach(bs, bs->peer); | |
766 | *ptr = NULL; | |
767 | break; | |
768 | } | |
9063f814 RH |
769 | } |
770 | } | |
771 | } | |
772 | ||
773 | /* clean up host side */ | |
774 | drive_uninit(drive_get_by_blockdev(bs)); | |
775 | ||
776 | return 0; | |
777 | } | |
6d4a2b3a CH |
778 | |
779 | /* | |
780 | * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the | |
781 | * existing QERR_ macro mess is cleaned up. A good example for better | |
782 | * error reports can be found in the qemu-img resize code. | |
783 | */ | |
784 | int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
785 | { | |
786 | const char *device = qdict_get_str(qdict, "device"); | |
787 | int64_t size = qdict_get_int(qdict, "size"); | |
788 | BlockDriverState *bs; | |
789 | ||
790 | bs = bdrv_find(device); | |
791 | if (!bs) { | |
792 | qerror_report(QERR_DEVICE_NOT_FOUND, device); | |
793 | return -1; | |
794 | } | |
795 | ||
796 | if (size < 0) { | |
797 | qerror_report(QERR_UNDEFINED_ERROR); | |
798 | return -1; | |
799 | } | |
800 | ||
801 | if (bdrv_truncate(bs, size)) { | |
802 | qerror_report(QERR_UNDEFINED_ERROR); | |
803 | return -1; | |
804 | } | |
805 | ||
806 | return 0; | |
807 | } |