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