]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev.c
blockdev: Clean up automatic drive deletion
[mirror_qemu.git] / blockdev.c
CommitLineData
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"
17
c9b62a7e 18static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
666daa68 19
14bafc54
MA
20/*
21 * We automatically delete the drive when a device using it gets
22 * unplugged. Questionable feature, but we can't just drop it.
23 * Device models call blockdev_mark_auto_del() to schedule the
24 * automatic deletion, and generic qdev code calls blockdev_auto_del()
25 * when deletion is actually safe.
26 */
27void blockdev_mark_auto_del(BlockDriverState *bs)
28{
29 DriveInfo *dinfo = drive_get_by_blockdev(bs);
30
31 dinfo->auto_del = 1;
32}
33
34void blockdev_auto_del(BlockDriverState *bs)
35{
36 DriveInfo *dinfo = drive_get_by_blockdev(bs);
37
38 if (dinfo->auto_del) {
39 drive_uninit(dinfo);
40 }
41}
42
666daa68
MA
43QemuOpts *drive_add(const char *file, const char *fmt, ...)
44{
45 va_list ap;
46 char optstr[1024];
47 QemuOpts *opts;
48
49 va_start(ap, fmt);
50 vsnprintf(optstr, sizeof(optstr), fmt, ap);
51 va_end(ap);
52
53 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
54 if (!opts) {
55 return NULL;
56 }
57 if (file)
58 qemu_opt_set(opts, "file", file);
59 return opts;
60}
61
62DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
63{
64 DriveInfo *dinfo;
65
66 /* seek interface, bus and unit */
67
68 QTAILQ_FOREACH(dinfo, &drives, next) {
69 if (dinfo->type == type &&
70 dinfo->bus == bus &&
71 dinfo->unit == unit)
72 return dinfo;
73 }
74
75 return NULL;
76}
77
78DriveInfo *drive_get_by_id(const char *id)
79{
80 DriveInfo *dinfo;
81
82 QTAILQ_FOREACH(dinfo, &drives, next) {
83 if (strcmp(id, dinfo->id))
84 continue;
85 return dinfo;
86 }
87 return NULL;
88}
89
90int drive_get_max_bus(BlockInterfaceType type)
91{
92 int max_bus;
93 DriveInfo *dinfo;
94
95 max_bus = -1;
96 QTAILQ_FOREACH(dinfo, &drives, next) {
97 if(dinfo->type == type &&
98 dinfo->bus > max_bus)
99 max_bus = dinfo->bus;
100 }
101 return max_bus;
102}
103
e4700e59
MA
104DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
105{
106 DriveInfo *dinfo;
107
108 QTAILQ_FOREACH(dinfo, &drives, next) {
109 if (dinfo->bdrv == bs) {
110 return dinfo;
111 }
112 }
113 return NULL;
114}
115
666daa68
MA
116static void bdrv_format_print(void *opaque, const char *name)
117{
118 fprintf(stderr, " %s", name);
119}
120
121void drive_uninit(DriveInfo *dinfo)
122{
123 qemu_opts_del(dinfo->opts);
124 bdrv_delete(dinfo->bdrv);
125 QTAILQ_REMOVE(&drives, dinfo, next);
126 qemu_free(dinfo);
127}
128
129static int parse_block_error_action(const char *buf, int is_read)
130{
131 if (!strcmp(buf, "ignore")) {
132 return BLOCK_ERR_IGNORE;
133 } else if (!is_read && !strcmp(buf, "enospc")) {
134 return BLOCK_ERR_STOP_ENOSPC;
135 } else if (!strcmp(buf, "stop")) {
136 return BLOCK_ERR_STOP_ANY;
137 } else if (!strcmp(buf, "report")) {
138 return BLOCK_ERR_REPORT;
139 } else {
140 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
141 buf, is_read ? "read" : "write");
142 return -1;
143 }
144}
145
146DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
147{
148 const char *buf;
149 const char *file = NULL;
150 char devname[128];
151 const char *serial;
152 const char *mediastr = "";
153 BlockInterfaceType type;
154 enum { MEDIA_DISK, MEDIA_CDROM } media;
155 int bus_id, unit_id;
156 int cyls, heads, secs, translation;
157 BlockDriver *drv = NULL;
158 int max_devs;
159 int index;
160 int ro = 0;
161 int bdrv_flags = 0;
162 int on_read_error, on_write_error;
163 const char *devaddr;
164 DriveInfo *dinfo;
165 int snapshot = 0;
166 int ret;
167
168 *fatal_error = 1;
169
170 translation = BIOS_ATA_TRANSLATION_AUTO;
171
172 if (default_to_scsi) {
173 type = IF_SCSI;
174 max_devs = MAX_SCSI_DEVS;
175 pstrcpy(devname, sizeof(devname), "scsi");
176 } else {
177 type = IF_IDE;
178 max_devs = MAX_IDE_DEVS;
179 pstrcpy(devname, sizeof(devname), "ide");
180 }
181 media = MEDIA_DISK;
182
183 /* extract parameters */
184 bus_id = qemu_opt_get_number(opts, "bus", 0);
185 unit_id = qemu_opt_get_number(opts, "unit", -1);
186 index = qemu_opt_get_number(opts, "index", -1);
187
188 cyls = qemu_opt_get_number(opts, "cyls", 0);
189 heads = qemu_opt_get_number(opts, "heads", 0);
190 secs = qemu_opt_get_number(opts, "secs", 0);
191
192 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
193 ro = qemu_opt_get_bool(opts, "readonly", 0);
194
195 file = qemu_opt_get(opts, "file");
196 serial = qemu_opt_get(opts, "serial");
197
198 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
199 pstrcpy(devname, sizeof(devname), buf);
200 if (!strcmp(buf, "ide")) {
201 type = IF_IDE;
202 max_devs = MAX_IDE_DEVS;
203 } else if (!strcmp(buf, "scsi")) {
204 type = IF_SCSI;
205 max_devs = MAX_SCSI_DEVS;
206 } else if (!strcmp(buf, "floppy")) {
207 type = IF_FLOPPY;
208 max_devs = 0;
209 } else if (!strcmp(buf, "pflash")) {
210 type = IF_PFLASH;
211 max_devs = 0;
212 } else if (!strcmp(buf, "mtd")) {
213 type = IF_MTD;
214 max_devs = 0;
215 } else if (!strcmp(buf, "sd")) {
216 type = IF_SD;
217 max_devs = 0;
218 } else if (!strcmp(buf, "virtio")) {
219 type = IF_VIRTIO;
220 max_devs = 0;
221 } else if (!strcmp(buf, "xen")) {
222 type = IF_XEN;
223 max_devs = 0;
224 } else if (!strcmp(buf, "none")) {
225 type = IF_NONE;
226 max_devs = 0;
227 } else {
228 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
229 return NULL;
230 }
231 }
232
233 if (cyls || heads || secs) {
234 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
235 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
236 return NULL;
237 }
238 if (heads < 1 || (type == IF_IDE && heads > 16)) {
239 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
240 return NULL;
241 }
242 if (secs < 1 || (type == IF_IDE && secs > 63)) {
243 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
244 return NULL;
245 }
246 }
247
248 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
249 if (!cyls) {
250 fprintf(stderr,
251 "qemu: '%s' trans must be used with cyls,heads and secs\n",
252 buf);
253 return NULL;
254 }
255 if (!strcmp(buf, "none"))
256 translation = BIOS_ATA_TRANSLATION_NONE;
257 else if (!strcmp(buf, "lba"))
258 translation = BIOS_ATA_TRANSLATION_LBA;
259 else if (!strcmp(buf, "auto"))
260 translation = BIOS_ATA_TRANSLATION_AUTO;
261 else {
262 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
263 return NULL;
264 }
265 }
266
267 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
268 if (!strcmp(buf, "disk")) {
269 media = MEDIA_DISK;
270 } else if (!strcmp(buf, "cdrom")) {
271 if (cyls || secs || heads) {
272 fprintf(stderr,
273 "qemu: '%s' invalid physical CHS format\n", buf);
274 return NULL;
275 }
276 media = MEDIA_CDROM;
277 } else {
278 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
279 return NULL;
280 }
281 }
282
283 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
284 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
285 bdrv_flags |= BDRV_O_NOCACHE;
286 } else if (!strcmp(buf, "writeback")) {
287 bdrv_flags |= BDRV_O_CACHE_WB;
288 } else if (!strcmp(buf, "unsafe")) {
289 bdrv_flags |= BDRV_O_CACHE_WB;
290 bdrv_flags |= BDRV_O_NO_FLUSH;
291 } else if (!strcmp(buf, "writethrough")) {
292 /* this is the default */
293 } else {
294 fprintf(stderr, "qemu: invalid cache option\n");
295 return NULL;
296 }
297 }
298
299#ifdef CONFIG_LINUX_AIO
300 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
301 if (!strcmp(buf, "native")) {
302 bdrv_flags |= BDRV_O_NATIVE_AIO;
303 } else if (!strcmp(buf, "threads")) {
304 /* this is the default */
305 } else {
306 fprintf(stderr, "qemu: invalid aio option\n");
307 return NULL;
308 }
309 }
310#endif
311
312 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
313 if (strcmp(buf, "?") == 0) {
314 fprintf(stderr, "qemu: Supported formats:");
315 bdrv_iterate_format(bdrv_format_print, NULL);
316 fprintf(stderr, "\n");
317 return NULL;
318 }
319 drv = bdrv_find_whitelisted_format(buf);
320 if (!drv) {
321 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
322 return NULL;
323 }
324 }
325
326 on_write_error = BLOCK_ERR_STOP_ENOSPC;
327 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
328 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
329 fprintf(stderr, "werror is no supported by this format\n");
330 return NULL;
331 }
332
333 on_write_error = parse_block_error_action(buf, 0);
334 if (on_write_error < 0) {
335 return NULL;
336 }
337 }
338
339 on_read_error = BLOCK_ERR_REPORT;
340 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
341 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
342 fprintf(stderr, "rerror is no supported by this format\n");
343 return NULL;
344 }
345
346 on_read_error = parse_block_error_action(buf, 1);
347 if (on_read_error < 0) {
348 return NULL;
349 }
350 }
351
352 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
353 if (type != IF_VIRTIO) {
354 fprintf(stderr, "addr is not supported\n");
355 return NULL;
356 }
357 }
358
359 /* compute bus and unit according index */
360
361 if (index != -1) {
362 if (bus_id != 0 || unit_id != -1) {
363 fprintf(stderr,
364 "qemu: index cannot be used with bus and unit\n");
365 return NULL;
366 }
367 if (max_devs == 0)
368 {
369 unit_id = index;
370 bus_id = 0;
371 } else {
372 unit_id = index % max_devs;
373 bus_id = index / max_devs;
374 }
375 }
376
377 /* if user doesn't specify a unit_id,
378 * try to find the first free
379 */
380
381 if (unit_id == -1) {
382 unit_id = 0;
383 while (drive_get(type, bus_id, unit_id) != NULL) {
384 unit_id++;
385 if (max_devs && unit_id >= max_devs) {
386 unit_id -= max_devs;
387 bus_id++;
388 }
389 }
390 }
391
392 /* check unit id */
393
394 if (max_devs && unit_id >= max_devs) {
395 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
396 unit_id, max_devs - 1);
397 return NULL;
398 }
399
400 /*
401 * ignore multiple definitions
402 */
403
404 if (drive_get(type, bus_id, unit_id) != NULL) {
405 *fatal_error = 0;
406 return NULL;
407 }
408
409 /* init */
410
411 dinfo = qemu_mallocz(sizeof(*dinfo));
412 if ((buf = qemu_opts_id(opts)) != NULL) {
413 dinfo->id = qemu_strdup(buf);
414 } else {
415 /* no id supplied -> create one */
416 dinfo->id = qemu_mallocz(32);
417 if (type == IF_IDE || type == IF_SCSI)
418 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
419 if (max_devs)
420 snprintf(dinfo->id, 32, "%s%i%s%i",
421 devname, bus_id, mediastr, unit_id);
422 else
423 snprintf(dinfo->id, 32, "%s%s%i",
424 devname, mediastr, unit_id);
425 }
426 dinfo->bdrv = bdrv_new(dinfo->id);
427 dinfo->devaddr = devaddr;
428 dinfo->type = type;
429 dinfo->bus = bus_id;
430 dinfo->unit = unit_id;
666daa68
MA
431 dinfo->opts = opts;
432 if (serial)
653dbec7 433 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
666daa68
MA
434 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
435
abd7f68d
MA
436 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
437
666daa68
MA
438 switch(type) {
439 case IF_IDE:
440 case IF_SCSI:
441 case IF_XEN:
442 case IF_NONE:
443 switch(media) {
444 case MEDIA_DISK:
445 if (cyls != 0) {
446 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
447 bdrv_set_translation_hint(dinfo->bdrv, translation);
448 }
449 break;
450 case MEDIA_CDROM:
451 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
452 break;
453 }
454 break;
455 case IF_SD:
456 /* FIXME: This isn't really a floppy, but it's a reasonable
457 approximation. */
458 case IF_FLOPPY:
459 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
460 break;
461 case IF_PFLASH:
462 case IF_MTD:
463 break;
464 case IF_VIRTIO:
465 /* add virtio block device */
466 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
467 qemu_opt_set(opts, "driver", "virtio-blk-pci");
468 qemu_opt_set(opts, "drive", dinfo->id);
469 if (devaddr)
470 qemu_opt_set(opts, "addr", devaddr);
471 break;
472 case IF_COUNT:
473 abort();
474 }
dd5b0d71 475 if (!file || !*file) {
666daa68
MA
476 *fatal_error = 0;
477 return NULL;
478 }
479 if (snapshot) {
480 /* always use cache=unsafe with snapshot */
481 bdrv_flags &= ~BDRV_O_CACHE_MASK;
482 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
483 }
484
485 if (media == MEDIA_CDROM) {
486 /* CDROM is fine for any interface, don't check. */
487 ro = 1;
488 } else if (ro == 1) {
489 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
490 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
491 return NULL;
492 }
493 }
494
495 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
496
497 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
498 if (ret < 0) {
499 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
500 file, strerror(-ret));
501 return NULL;
502 }
503
504 if (bdrv_key_required(dinfo->bdrv))
505 autostart = 0;
506 *fatal_error = 0;
507 return dinfo;
508}
509
510void do_commit(Monitor *mon, const QDict *qdict)
511{
666daa68 512 const char *device = qdict_get_str(qdict, "device");
6ab4b5ab 513 BlockDriverState *bs;
666daa68 514
6ab4b5ab
MA
515 if (!strcmp(device, "all")) {
516 bdrv_commit_all();
517 } else {
518 bs = bdrv_find(device);
ac59eb95
MA
519 if (!bs) {
520 qerror_report(QERR_DEVICE_NOT_FOUND, device);
521 return;
6ab4b5ab 522 }
ac59eb95 523 bdrv_commit(bs);
666daa68
MA
524 }
525}
526
527static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
528{
3b5276b5
EH
529 if (!force) {
530 if (!bdrv_is_removable(bs)) {
531 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
532 bdrv_get_device_name(bs));
533 return -1;
534 }
535 if (bdrv_is_locked(bs)) {
536 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
537 return -1;
666daa68 538 }
666daa68 539 }
3b5276b5 540 bdrv_close(bs);
666daa68
MA
541 return 0;
542}
543
544int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
545{
546 BlockDriverState *bs;
547 int force = qdict_get_int(qdict, "force");
548 const char *filename = qdict_get_str(qdict, "device");
549
550 bs = bdrv_find(filename);
551 if (!bs) {
552 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
553 return -1;
554 }
555 return eject_device(mon, bs, force);
556}
557
558int do_block_set_passwd(Monitor *mon, const QDict *qdict,
559 QObject **ret_data)
560{
561 BlockDriverState *bs;
562 int err;
563
564 bs = bdrv_find(qdict_get_str(qdict, "device"));
565 if (!bs) {
566 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
567 return -1;
568 }
569
570 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
571 if (err == -EINVAL) {
572 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
573 return -1;
574 } else if (err < 0) {
575 qerror_report(QERR_INVALID_PASSWORD);
576 return -1;
577 }
578
579 return 0;
580}
581
582int do_change_block(Monitor *mon, const char *device,
583 const char *filename, const char *fmt)
584{
585 BlockDriverState *bs;
586 BlockDriver *drv = NULL;
587 int bdrv_flags;
588
589 bs = bdrv_find(device);
590 if (!bs) {
591 qerror_report(QERR_DEVICE_NOT_FOUND, device);
592 return -1;
593 }
594 if (fmt) {
595 drv = bdrv_find_whitelisted_format(fmt);
596 if (!drv) {
597 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
598 return -1;
599 }
600 }
601 if (eject_device(mon, bs, 0) < 0) {
602 return -1;
603 }
604 bdrv_flags = bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM ? 0 : BDRV_O_RDWR;
605 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
606 qerror_report(QERR_OPEN_FILE_FAILED, filename);
607 return -1;
608 }
609 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
610}