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