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