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