qemu-kvm: Remove obsolete io_thread line from configure
[qemu-kvm.git] / blockdev.c
blobaa11291adc5feab61996b0f010be18b9a0678154
1 /*
2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
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 */
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 "block_int.h"
18 #include "qmp-commands.h"
20 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
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",
34 static const int if_max_devs[IF_COUNT] = {
36 * Do not change these numbers! They govern how drive option
37 * index maps to unit and bus. That mapping is ABI.
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.
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.
49 [IF_IDE] = 2,
50 [IF_SCSI] = 7,
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.
60 void blockdev_mark_auto_del(BlockDriverState *bs)
62 DriveInfo *dinfo = drive_get_by_blockdev(bs);
64 if (dinfo) {
65 dinfo->auto_del = 1;
69 void blockdev_auto_del(BlockDriverState *bs)
71 DriveInfo *dinfo = drive_get_by_blockdev(bs);
73 if (dinfo && dinfo->auto_del) {
74 drive_put_ref(dinfo);
78 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
80 int max_devs = if_max_devs[type];
81 return max_devs ? index / max_devs : 0;
84 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
86 int max_devs = if_max_devs[type];
87 return max_devs ? index % max_devs : index;
90 QemuOpts *drive_def(const char *optstr)
92 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
95 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
96 const char *optstr)
98 QemuOpts *opts;
99 char buf[32];
101 opts = drive_def(optstr);
102 if (!opts) {
103 return NULL;
105 if (type != IF_DEFAULT) {
106 qemu_opt_set(opts, "if", if_name[type]);
108 if (index >= 0) {
109 snprintf(buf, sizeof(buf), "%d", index);
110 qemu_opt_set(opts, "index", buf);
112 if (file)
113 qemu_opt_set(opts, "file", file);
114 return opts;
117 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
119 DriveInfo *dinfo;
121 /* seek interface, bus and unit */
123 QTAILQ_FOREACH(dinfo, &drives, next) {
124 if (dinfo->type == type &&
125 dinfo->bus == bus &&
126 dinfo->unit == unit)
127 return dinfo;
130 return NULL;
133 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
135 return drive_get(type,
136 drive_index_to_bus_id(type, index),
137 drive_index_to_unit_id(type, index));
140 int drive_get_max_bus(BlockInterfaceType type)
142 int max_bus;
143 DriveInfo *dinfo;
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;
151 return max_bus;
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)
159 static int next_block_unit[IF_COUNT];
161 return drive_get(type, 0, next_block_unit[type]++);
164 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
166 DriveInfo *dinfo;
168 QTAILQ_FOREACH(dinfo, &drives, next) {
169 if (dinfo->bdrv == bs) {
170 return dinfo;
173 return NULL;
176 static void bdrv_format_print(void *opaque, const char *name)
178 error_printf(" %s", name);
181 static void drive_uninit(DriveInfo *dinfo)
183 qemu_opts_del(dinfo->opts);
184 bdrv_delete(dinfo->bdrv);
185 g_free(dinfo->id);
186 QTAILQ_REMOVE(&drives, dinfo, next);
187 g_free(dinfo);
190 void drive_put_ref(DriveInfo *dinfo)
192 assert(dinfo->refcount);
193 if (--dinfo->refcount == 0) {
194 drive_uninit(dinfo);
198 void drive_get_ref(DriveInfo *dinfo)
200 dinfo->refcount++;
203 static int parse_block_error_action(const char *buf, int is_read)
205 if (!strcmp(buf, "ignore")) {
206 return BLOCK_ERR_IGNORE;
207 } else if (!is_read && !strcmp(buf, "enospc")) {
208 return BLOCK_ERR_STOP_ENOSPC;
209 } else if (!strcmp(buf, "stop")) {
210 return BLOCK_ERR_STOP_ANY;
211 } else if (!strcmp(buf, "report")) {
212 return BLOCK_ERR_REPORT;
213 } else {
214 error_report("'%s' invalid %s error action",
215 buf, is_read ? "read" : "write");
216 return -1;
220 static bool do_check_io_limits(BlockIOLimit *io_limits)
222 bool bps_flag;
223 bool iops_flag;
225 assert(io_limits);
227 bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
228 && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
229 || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
230 iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
231 && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
232 || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
233 if (bps_flag || iops_flag) {
234 return false;
237 return true;
240 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
242 const char *buf;
243 const char *file = NULL;
244 char devname[128];
245 const char *serial;
246 const char *mediastr = "";
247 BlockInterfaceType type;
248 enum { MEDIA_DISK, MEDIA_CDROM } media;
249 int bus_id, unit_id;
250 int cyls, heads, secs, translation;
251 BlockDriver *drv = NULL;
252 int max_devs;
253 int index;
254 int ro = 0;
255 int bdrv_flags = 0;
256 int on_read_error, on_write_error;
257 const char *devaddr;
258 DriveInfo *dinfo;
259 BlockIOLimit io_limits;
260 int snapshot = 0;
261 bool copy_on_read;
262 int ret;
264 translation = BIOS_ATA_TRANSLATION_AUTO;
265 media = MEDIA_DISK;
267 /* extract parameters */
268 bus_id = qemu_opt_get_number(opts, "bus", 0);
269 unit_id = qemu_opt_get_number(opts, "unit", -1);
270 index = qemu_opt_get_number(opts, "index", -1);
272 cyls = qemu_opt_get_number(opts, "cyls", 0);
273 heads = qemu_opt_get_number(opts, "heads", 0);
274 secs = qemu_opt_get_number(opts, "secs", 0);
276 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
277 ro = qemu_opt_get_bool(opts, "readonly", 0);
278 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
280 file = qemu_opt_get(opts, "file");
281 serial = qemu_opt_get(opts, "serial");
283 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
284 pstrcpy(devname, sizeof(devname), buf);
285 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
287 if (type == IF_COUNT) {
288 error_report("unsupported bus type '%s'", buf);
289 return NULL;
291 } else {
292 type = default_to_scsi ? IF_SCSI : IF_IDE;
293 pstrcpy(devname, sizeof(devname), if_name[type]);
296 max_devs = if_max_devs[type];
298 if (cyls || heads || secs) {
299 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
300 error_report("invalid physical cyls number");
301 return NULL;
303 if (heads < 1 || (type == IF_IDE && heads > 16)) {
304 error_report("invalid physical heads number");
305 return NULL;
307 if (secs < 1 || (type == IF_IDE && secs > 63)) {
308 error_report("invalid physical secs number");
309 return NULL;
313 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
314 if (!cyls) {
315 error_report("'%s' trans must be used with cyls, heads and secs",
316 buf);
317 return NULL;
319 if (!strcmp(buf, "none"))
320 translation = BIOS_ATA_TRANSLATION_NONE;
321 else if (!strcmp(buf, "lba"))
322 translation = BIOS_ATA_TRANSLATION_LBA;
323 else if (!strcmp(buf, "auto"))
324 translation = BIOS_ATA_TRANSLATION_AUTO;
325 else {
326 error_report("'%s' invalid translation type", buf);
327 return NULL;
331 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
332 if (!strcmp(buf, "disk")) {
333 media = MEDIA_DISK;
334 } else if (!strcmp(buf, "cdrom")) {
335 if (cyls || secs || heads) {
336 error_report("CHS can't be set with media=%s", buf);
337 return NULL;
339 media = MEDIA_CDROM;
340 } else {
341 error_report("'%s' invalid media", buf);
342 return NULL;
346 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
347 if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
348 error_report("invalid cache option");
349 return NULL;
353 #ifdef CONFIG_LINUX_AIO
354 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
355 if (!strcmp(buf, "native")) {
356 bdrv_flags |= BDRV_O_NATIVE_AIO;
357 } else if (!strcmp(buf, "threads")) {
358 /* this is the default */
359 } else {
360 error_report("invalid aio option");
361 return NULL;
364 #endif
366 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
367 if (strcmp(buf, "?") == 0) {
368 error_printf("Supported formats:");
369 bdrv_iterate_format(bdrv_format_print, NULL);
370 error_printf("\n");
371 return NULL;
373 drv = bdrv_find_whitelisted_format(buf);
374 if (!drv) {
375 error_report("'%s' invalid format", buf);
376 return NULL;
380 /* disk I/O throttling */
381 io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
382 qemu_opt_get_number(opts, "bps", 0);
383 io_limits.bps[BLOCK_IO_LIMIT_READ] =
384 qemu_opt_get_number(opts, "bps_rd", 0);
385 io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
386 qemu_opt_get_number(opts, "bps_wr", 0);
387 io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
388 qemu_opt_get_number(opts, "iops", 0);
389 io_limits.iops[BLOCK_IO_LIMIT_READ] =
390 qemu_opt_get_number(opts, "iops_rd", 0);
391 io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
392 qemu_opt_get_number(opts, "iops_wr", 0);
394 if (!do_check_io_limits(&io_limits)) {
395 error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
396 "cannot be used at the same time");
397 return NULL;
400 if (qemu_opt_get(opts, "boot") != NULL) {
401 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
402 "ignored. Future versions will reject this parameter. Please "
403 "update your scripts.\n");
406 on_write_error = BLOCK_ERR_STOP_ENOSPC;
407 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
408 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
409 error_report("werror is not supported by this bus type");
410 return NULL;
413 on_write_error = parse_block_error_action(buf, 0);
414 if (on_write_error < 0) {
415 return NULL;
419 on_read_error = BLOCK_ERR_REPORT;
420 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
421 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
422 error_report("rerror is not supported by this bus type");
423 return NULL;
426 on_read_error = parse_block_error_action(buf, 1);
427 if (on_read_error < 0) {
428 return NULL;
432 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
433 if (type != IF_VIRTIO) {
434 error_report("addr is not supported by this bus type");
435 return NULL;
439 /* compute bus and unit according index */
441 if (index != -1) {
442 if (bus_id != 0 || unit_id != -1) {
443 error_report("index cannot be used with bus and unit");
444 return NULL;
446 bus_id = drive_index_to_bus_id(type, index);
447 unit_id = drive_index_to_unit_id(type, index);
450 /* if user doesn't specify a unit_id,
451 * try to find the first free
454 if (unit_id == -1) {
455 unit_id = 0;
456 while (drive_get(type, bus_id, unit_id) != NULL) {
457 unit_id++;
458 if (max_devs && unit_id >= max_devs) {
459 unit_id -= max_devs;
460 bus_id++;
465 /* check unit id */
467 if (max_devs && unit_id >= max_devs) {
468 error_report("unit %d too big (max is %d)",
469 unit_id, max_devs - 1);
470 return NULL;
474 * catch multiple definitions
477 if (drive_get(type, bus_id, unit_id) != NULL) {
478 error_report("drive with bus=%d, unit=%d (index=%d) exists",
479 bus_id, unit_id, index);
480 return NULL;
483 /* init */
485 dinfo = g_malloc0(sizeof(*dinfo));
486 if ((buf = qemu_opts_id(opts)) != NULL) {
487 dinfo->id = g_strdup(buf);
488 } else {
489 /* no id supplied -> create one */
490 dinfo->id = g_malloc0(32);
491 if (type == IF_IDE || type == IF_SCSI)
492 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
493 if (max_devs)
494 snprintf(dinfo->id, 32, "%s%i%s%i",
495 devname, bus_id, mediastr, unit_id);
496 else
497 snprintf(dinfo->id, 32, "%s%s%i",
498 devname, mediastr, unit_id);
500 dinfo->bdrv = bdrv_new(dinfo->id);
501 dinfo->devaddr = devaddr;
502 dinfo->type = type;
503 dinfo->bus = bus_id;
504 dinfo->unit = unit_id;
505 dinfo->opts = opts;
506 dinfo->refcount = 1;
507 if (serial)
508 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
509 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
511 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
513 /* disk I/O throttling */
514 bdrv_set_io_limits(dinfo->bdrv, &io_limits);
516 switch(type) {
517 case IF_IDE:
518 case IF_SCSI:
519 case IF_XEN:
520 case IF_NONE:
521 switch(media) {
522 case MEDIA_DISK:
523 if (cyls != 0) {
524 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
525 bdrv_set_translation_hint(dinfo->bdrv, translation);
527 break;
528 case MEDIA_CDROM:
529 dinfo->media_cd = 1;
530 break;
532 break;
533 case IF_SD:
534 case IF_FLOPPY:
535 case IF_PFLASH:
536 case IF_MTD:
537 break;
538 case IF_VIRTIO:
539 /* add virtio block device */
540 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
541 qemu_opt_set(opts, "driver", "virtio-blk");
542 qemu_opt_set(opts, "drive", dinfo->id);
543 if (devaddr)
544 qemu_opt_set(opts, "addr", devaddr);
545 break;
546 default:
547 abort();
549 if (!file || !*file) {
550 return dinfo;
552 if (snapshot) {
553 /* always use cache=unsafe with snapshot */
554 bdrv_flags &= ~BDRV_O_CACHE_MASK;
555 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
558 if (copy_on_read) {
559 bdrv_flags |= BDRV_O_COPY_ON_READ;
562 if (media == MEDIA_CDROM) {
563 /* CDROM is fine for any interface, don't check. */
564 ro = 1;
565 } else if (ro == 1) {
566 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
567 error_report("readonly not supported by this bus type");
568 goto err;
572 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
574 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
575 if (ret < 0) {
576 error_report("could not open disk image %s: %s",
577 file, strerror(-ret));
578 goto err;
581 if (bdrv_key_required(dinfo->bdrv))
582 autostart = 0;
583 return dinfo;
585 err:
586 bdrv_delete(dinfo->bdrv);
587 g_free(dinfo->id);
588 QTAILQ_REMOVE(&drives, dinfo, next);
589 g_free(dinfo);
590 return NULL;
593 void do_commit(Monitor *mon, const QDict *qdict)
595 const char *device = qdict_get_str(qdict, "device");
596 BlockDriverState *bs;
598 if (!strcmp(device, "all")) {
599 bdrv_commit_all();
600 } else {
601 bs = bdrv_find(device);
602 if (!bs) {
603 qerror_report(QERR_DEVICE_NOT_FOUND, device);
604 return;
606 bdrv_commit(bs);
610 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
611 bool has_format, const char *format,
612 Error **errp)
614 BlockDriverState *bs;
615 BlockDriver *drv, *old_drv, *proto_drv;
616 int ret = 0;
617 int flags;
618 char old_filename[1024];
620 bs = bdrv_find(device);
621 if (!bs) {
622 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
623 return;
626 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
628 old_drv = bs->drv;
629 flags = bs->open_flags;
631 if (!has_format) {
632 format = "qcow2";
635 drv = bdrv_find_format(format);
636 if (!drv) {
637 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
638 return;
641 proto_drv = bdrv_find_protocol(snapshot_file);
642 if (!proto_drv) {
643 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
644 return;
647 ret = bdrv_img_create(snapshot_file, format, bs->filename,
648 bs->drv->format_name, NULL, -1, flags);
649 if (ret) {
650 error_set(errp, QERR_UNDEFINED_ERROR);
651 return;
654 bdrv_drain_all();
655 bdrv_flush(bs);
657 bdrv_close(bs);
658 ret = bdrv_open(bs, snapshot_file, flags, drv);
660 * If reopening the image file we just created fails, fall back
661 * and try to re-open the original image. If that fails too, we
662 * are in serious trouble.
664 if (ret != 0) {
665 ret = bdrv_open(bs, old_filename, flags, old_drv);
666 if (ret != 0) {
667 error_set(errp, QERR_OPEN_FILE_FAILED, old_filename);
668 } else {
669 error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
674 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
676 if (!bdrv_dev_has_removable_media(bs)) {
677 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
678 return -1;
680 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
681 bdrv_dev_eject_request(bs, force);
682 if (!force) {
683 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
684 return -1;
687 bdrv_close(bs);
688 return 0;
691 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
693 BlockDriverState *bs;
694 int force = qdict_get_try_bool(qdict, "force", 0);
695 const char *filename = qdict_get_str(qdict, "device");
697 bs = bdrv_find(filename);
698 if (!bs) {
699 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
700 return -1;
702 return eject_device(mon, bs, force);
705 void qmp_block_passwd(const char *device, const char *password, Error **errp)
707 BlockDriverState *bs;
708 int err;
710 bs = bdrv_find(device);
711 if (!bs) {
712 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
713 return;
716 err = bdrv_set_key(bs, password);
717 if (err == -EINVAL) {
718 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
719 return;
720 } else if (err < 0) {
721 error_set(errp, QERR_INVALID_PASSWORD);
722 return;
726 int do_change_block(Monitor *mon, const char *device,
727 const char *filename, const char *fmt)
729 BlockDriverState *bs;
730 BlockDriver *drv = NULL;
731 int bdrv_flags;
733 bs = bdrv_find(device);
734 if (!bs) {
735 qerror_report(QERR_DEVICE_NOT_FOUND, device);
736 return -1;
738 if (fmt) {
739 drv = bdrv_find_whitelisted_format(fmt);
740 if (!drv) {
741 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
742 return -1;
745 if (eject_device(mon, bs, 0) < 0) {
746 return -1;
748 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
749 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
750 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
751 qerror_report(QERR_OPEN_FILE_FAILED, filename);
752 return -1;
754 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
757 /* throttling disk I/O limits */
758 int do_block_set_io_throttle(Monitor *mon,
759 const QDict *qdict, QObject **ret_data)
761 BlockIOLimit io_limits;
762 const char *devname = qdict_get_str(qdict, "device");
763 BlockDriverState *bs;
765 io_limits.bps[BLOCK_IO_LIMIT_TOTAL]
766 = qdict_get_try_int(qdict, "bps", -1);
767 io_limits.bps[BLOCK_IO_LIMIT_READ]
768 = qdict_get_try_int(qdict, "bps_rd", -1);
769 io_limits.bps[BLOCK_IO_LIMIT_WRITE]
770 = qdict_get_try_int(qdict, "bps_wr", -1);
771 io_limits.iops[BLOCK_IO_LIMIT_TOTAL]
772 = qdict_get_try_int(qdict, "iops", -1);
773 io_limits.iops[BLOCK_IO_LIMIT_READ]
774 = qdict_get_try_int(qdict, "iops_rd", -1);
775 io_limits.iops[BLOCK_IO_LIMIT_WRITE]
776 = qdict_get_try_int(qdict, "iops_wr", -1);
778 bs = bdrv_find(devname);
779 if (!bs) {
780 qerror_report(QERR_DEVICE_NOT_FOUND, devname);
781 return -1;
784 if ((io_limits.bps[BLOCK_IO_LIMIT_TOTAL] == -1)
785 || (io_limits.bps[BLOCK_IO_LIMIT_READ] == -1)
786 || (io_limits.bps[BLOCK_IO_LIMIT_WRITE] == -1)
787 || (io_limits.iops[BLOCK_IO_LIMIT_TOTAL] == -1)
788 || (io_limits.iops[BLOCK_IO_LIMIT_READ] == -1)
789 || (io_limits.iops[BLOCK_IO_LIMIT_WRITE] == -1)) {
790 qerror_report(QERR_MISSING_PARAMETER,
791 "bps/bps_rd/bps_wr/iops/iops_rd/iops_wr");
792 return -1;
795 if (!do_check_io_limits(&io_limits)) {
796 qerror_report(QERR_INVALID_PARAMETER_COMBINATION);
797 return -1;
800 bs->io_limits = io_limits;
801 bs->slice_time = BLOCK_IO_SLICE_TIME;
803 if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
804 bdrv_io_limits_enable(bs);
805 } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
806 bdrv_io_limits_disable(bs);
807 } else {
808 if (bs->block_timer) {
809 qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
813 return 0;
816 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
818 const char *id = qdict_get_str(qdict, "id");
819 BlockDriverState *bs;
821 bs = bdrv_find(id);
822 if (!bs) {
823 qerror_report(QERR_DEVICE_NOT_FOUND, id);
824 return -1;
826 if (bdrv_in_use(bs)) {
827 qerror_report(QERR_DEVICE_IN_USE, id);
828 return -1;
831 /* quiesce block driver; prevent further io */
832 bdrv_drain_all();
833 bdrv_flush(bs);
834 bdrv_close(bs);
836 /* if we have a device attached to this BlockDriverState
837 * then we need to make the drive anonymous until the device
838 * can be removed. If this is a drive with no device backing
839 * then we can just get rid of the block driver state right here.
841 if (bdrv_get_attached_dev(bs)) {
842 bdrv_make_anon(bs);
843 } else {
844 drive_uninit(drive_get_by_blockdev(bs));
847 return 0;
851 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
852 * existing QERR_ macro mess is cleaned up. A good example for better
853 * error reports can be found in the qemu-img resize code.
855 void qmp_block_resize(const char *device, int64_t size, Error **errp)
857 BlockDriverState *bs;
859 bs = bdrv_find(device);
860 if (!bs) {
861 error_set(errp, QERR_DEVICE_NOT_FOUND, device);
862 return;
865 if (size < 0) {
866 error_set(errp, QERR_UNDEFINED_ERROR);
867 return;
870 if (bdrv_truncate(bs, size)) {
871 error_set(errp, QERR_UNDEFINED_ERROR);
872 return;