Merge commit '2cc6e0a14210d2e80173ec6b4611e7e3c50c2cce' into upstream-merge
[qemu-kvm.git] / blockdev.c
blobc01735b43c41f1cecac19c079baf6cc014ea293d
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 "hw/qdev.h"
18 #include "block_int.h"
20 DriveInfo *extboot_drive = NULL;
22 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
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",
36 static const int if_max_devs[IF_COUNT] = {
38 * Do not change these numbers! They govern how drive option
39 * index maps to unit and bus. That mapping is ABI.
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.
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.
51 [IF_IDE] = 2,
52 [IF_SCSI] = 7,
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.
62 void blockdev_mark_auto_del(BlockDriverState *bs)
64 DriveInfo *dinfo = drive_get_by_blockdev(bs);
66 if (dinfo) {
67 dinfo->auto_del = 1;
71 void blockdev_auto_del(BlockDriverState *bs)
73 DriveInfo *dinfo = drive_get_by_blockdev(bs);
75 if (dinfo && dinfo->auto_del) {
76 drive_put_ref(dinfo);
80 static int drive_index_to_bus_id(BlockInterfaceType type, int index)
82 int max_devs = if_max_devs[type];
83 return max_devs ? index / max_devs : 0;
86 static int drive_index_to_unit_id(BlockInterfaceType type, int index)
88 int max_devs = if_max_devs[type];
89 return max_devs ? index % max_devs : index;
92 QemuOpts *drive_def(const char *optstr)
94 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
97 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
98 const char *optstr)
100 QemuOpts *opts;
101 char buf[32];
103 opts = drive_def(optstr);
104 if (!opts) {
105 return NULL;
107 if (type != IF_DEFAULT) {
108 qemu_opt_set(opts, "if", if_name[type]);
110 if (index >= 0) {
111 snprintf(buf, sizeof(buf), "%d", index);
112 qemu_opt_set(opts, "index", buf);
114 if (file)
115 qemu_opt_set(opts, "file", file);
116 return opts;
119 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
121 DriveInfo *dinfo;
123 /* seek interface, bus and unit */
125 QTAILQ_FOREACH(dinfo, &drives, next) {
126 if (dinfo->type == type &&
127 dinfo->bus == bus &&
128 dinfo->unit == unit)
129 return dinfo;
132 return NULL;
135 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
137 return drive_get(type,
138 drive_index_to_bus_id(type, index),
139 drive_index_to_unit_id(type, index));
142 int drive_get_max_bus(BlockInterfaceType type)
144 int max_bus;
145 DriveInfo *dinfo;
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;
153 return max_bus;
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. */
159 DriveInfo *drive_get_next(BlockInterfaceType type)
161 static int next_block_unit[IF_COUNT];
163 return drive_get(type, 0, next_block_unit[type]++);
166 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
168 DriveInfo *dinfo;
170 QTAILQ_FOREACH(dinfo, &drives, next) {
171 if (dinfo->bdrv == bs) {
172 return dinfo;
175 return NULL;
178 static void bdrv_format_print(void *opaque, const char *name)
180 error_printf(" %s", name);
183 static void drive_uninit(DriveInfo *dinfo)
185 qemu_opts_del(dinfo->opts);
186 bdrv_delete(dinfo->bdrv);
187 g_free(dinfo->id);
188 QTAILQ_REMOVE(&drives, dinfo, next);
189 g_free(dinfo);
192 void drive_put_ref(DriveInfo *dinfo)
194 assert(dinfo->refcount);
195 if (--dinfo->refcount == 0) {
196 drive_uninit(dinfo);
200 void drive_get_ref(DriveInfo *dinfo)
202 dinfo->refcount++;
205 static int parse_block_error_action(const char *buf, int is_read)
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;
222 DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
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;
245 translation = BIOS_ATA_TRANSLATION_AUTO;
246 media = MEDIA_DISK;
248 /* extract parameters */
249 bus_id = qemu_opt_get_number(opts, "bus", 0);
250 unit_id = qemu_opt_get_number(opts, "unit", -1);
251 index = qemu_opt_get_number(opts, "index", -1);
253 cyls = qemu_opt_get_number(opts, "cyls", 0);
254 heads = qemu_opt_get_number(opts, "heads", 0);
255 secs = qemu_opt_get_number(opts, "secs", 0);
257 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
258 ro = qemu_opt_get_bool(opts, "readonly", 0);
260 file = qemu_opt_get(opts, "file");
261 serial = qemu_opt_get(opts, "serial");
263 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
264 pstrcpy(devname, sizeof(devname), buf);
265 for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
267 if (type == IF_COUNT) {
268 error_report("unsupported bus type '%s'", buf);
269 return NULL;
271 } else {
272 type = default_to_scsi ? IF_SCSI : IF_IDE;
273 pstrcpy(devname, sizeof(devname), if_name[type]);
276 max_devs = if_max_devs[type];
278 if (cyls || heads || secs) {
279 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
280 error_report("invalid physical cyls number");
281 return NULL;
283 if (heads < 1 || (type == IF_IDE && heads > 16)) {
284 error_report("invalid physical heads number");
285 return NULL;
287 if (secs < 1 || (type == IF_IDE && secs > 63)) {
288 error_report("invalid physical secs number");
289 return NULL;
293 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
294 if (!cyls) {
295 error_report("'%s' trans must be used with cyls, heads and secs",
296 buf);
297 return NULL;
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 {
306 error_report("'%s' invalid translation type", buf);
307 return NULL;
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) {
316 error_report("CHS can't be set with media=%s", buf);
317 return NULL;
319 media = MEDIA_CDROM;
320 } else {
321 error_report("'%s' invalid media", buf);
322 return NULL;
326 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
327 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
328 bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
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 {
337 error_report("invalid cache option");
338 return NULL;
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 {
349 error_report("invalid aio option");
350 return NULL;
353 #endif
355 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
356 if (strcmp(buf, "?") == 0) {
357 error_printf("Supported formats:");
358 bdrv_iterate_format(bdrv_format_print, NULL);
359 error_printf("\n");
360 return NULL;
362 drv = bdrv_find_whitelisted_format(buf);
363 if (!drv) {
364 error_report("'%s' invalid format", buf);
365 return NULL;
369 is_extboot = qemu_opt_get_bool(opts, "boot", 0);
370 if (is_extboot && extboot_drive) {
371 fprintf(stderr, "qemu: two bootable drives specified\n");
372 return NULL;
375 on_write_error = BLOCK_ERR_STOP_ENOSPC;
376 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
377 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
378 error_report("werror is not supported by this bus type");
379 return NULL;
382 on_write_error = parse_block_error_action(buf, 0);
383 if (on_write_error < 0) {
384 return NULL;
388 on_read_error = BLOCK_ERR_REPORT;
389 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
390 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
391 error_report("rerror is not supported by this bus type");
392 return NULL;
395 on_read_error = parse_block_error_action(buf, 1);
396 if (on_read_error < 0) {
397 return NULL;
401 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
402 if (type != IF_VIRTIO) {
403 error_report("addr is not supported by this bus type");
404 return NULL;
408 /* compute bus and unit according index */
410 if (index != -1) {
411 if (bus_id != 0 || unit_id != -1) {
412 error_report("index cannot be used with bus and unit");
413 return NULL;
415 bus_id = drive_index_to_bus_id(type, index);
416 unit_id = drive_index_to_unit_id(type, index);
419 /* if user doesn't specify a unit_id,
420 * try to find the first free
423 if (unit_id == -1) {
424 unit_id = 0;
425 while (drive_get(type, bus_id, unit_id) != NULL) {
426 unit_id++;
427 if (max_devs && unit_id >= max_devs) {
428 unit_id -= max_devs;
429 bus_id++;
434 /* check unit id */
436 if (max_devs && unit_id >= max_devs) {
437 error_report("unit %d too big (max is %d)",
438 unit_id, max_devs - 1);
439 return NULL;
443 * catch multiple definitions
446 if (drive_get(type, bus_id, unit_id) != NULL) {
447 error_report("drive with bus=%d, unit=%d (index=%d) exists",
448 bus_id, unit_id, index);
449 return NULL;
452 /* init */
454 dinfo = g_malloc0(sizeof(*dinfo));
455 if ((buf = qemu_opts_id(opts)) != NULL) {
456 dinfo->id = g_strdup(buf);
457 } else {
458 /* no id supplied -> create one */
459 dinfo->id = g_malloc0(32);
460 if (type == IF_IDE || type == IF_SCSI)
461 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
462 if (max_devs)
463 snprintf(dinfo->id, 32, "%s%i%s%i",
464 devname, bus_id, mediastr, unit_id);
465 else
466 snprintf(dinfo->id, 32, "%s%s%i",
467 devname, mediastr, unit_id);
469 dinfo->bdrv = bdrv_new(dinfo->id);
470 dinfo->devaddr = devaddr;
471 dinfo->type = type;
472 dinfo->bus = bus_id;
473 dinfo->unit = unit_id;
474 dinfo->opts = opts;
475 dinfo->refcount = 1;
476 if (serial)
477 strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
478 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
480 if (is_extboot) {
481 extboot_drive = dinfo;
484 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
486 switch(type) {
487 case IF_IDE:
488 case IF_SCSI:
489 case IF_XEN:
490 case IF_NONE:
491 switch(media) {
492 case MEDIA_DISK:
493 if (cyls != 0) {
494 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
495 bdrv_set_translation_hint(dinfo->bdrv, translation);
497 break;
498 case MEDIA_CDROM:
499 bdrv_set_removable(dinfo->bdrv, 1);
500 dinfo->media_cd = 1;
501 break;
503 break;
504 case IF_SD:
505 /* FIXME: This isn't really a floppy, but it's a reasonable
506 approximation. */
507 case IF_FLOPPY:
508 bdrv_set_removable(dinfo->bdrv, 1);
509 break;
510 case IF_PFLASH:
511 case IF_MTD:
512 break;
513 case IF_VIRTIO:
514 /* add virtio block device */
515 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
516 qemu_opt_set(opts, "driver", "virtio-blk");
517 qemu_opt_set(opts, "drive", dinfo->id);
518 if (devaddr)
519 qemu_opt_set(opts, "addr", devaddr);
520 break;
521 default:
522 abort();
524 if (!file || !*file) {
525 return dinfo;
527 if (snapshot) {
528 /* always use cache=unsafe with snapshot */
529 bdrv_flags &= ~BDRV_O_CACHE_MASK;
530 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
533 if (media == MEDIA_CDROM) {
534 /* CDROM is fine for any interface, don't check. */
535 ro = 1;
536 } else if (ro == 1) {
537 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
538 error_report("readonly not supported by this bus type");
539 goto err;
543 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
545 ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
546 if (ret < 0) {
547 error_report("could not open disk image %s: %s",
548 file, strerror(-ret));
549 goto err;
552 if (bdrv_key_required(dinfo->bdrv))
553 autostart = 0;
554 return dinfo;
556 err:
557 bdrv_delete(dinfo->bdrv);
558 g_free(dinfo->id);
559 QTAILQ_REMOVE(&drives, dinfo, next);
560 g_free(dinfo);
561 return NULL;
564 void do_commit(Monitor *mon, const QDict *qdict)
566 const char *device = qdict_get_str(qdict, "device");
567 BlockDriverState *bs;
569 if (!strcmp(device, "all")) {
570 bdrv_commit_all();
571 } else {
572 bs = bdrv_find(device);
573 if (!bs) {
574 qerror_report(QERR_DEVICE_NOT_FOUND, device);
575 return;
577 bdrv_commit(bs);
581 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
583 const char *device = qdict_get_str(qdict, "device");
584 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
585 const char *format = qdict_get_try_str(qdict, "format");
586 BlockDriverState *bs;
587 BlockDriver *drv, *old_drv, *proto_drv;
588 int ret = 0;
589 int flags;
590 char old_filename[1024];
592 if (!filename) {
593 qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
594 ret = -1;
595 goto out;
598 bs = bdrv_find(device);
599 if (!bs) {
600 qerror_report(QERR_DEVICE_NOT_FOUND, device);
601 ret = -1;
602 goto out;
605 pstrcpy(old_filename, sizeof(old_filename), bs->filename);
607 old_drv = bs->drv;
608 flags = bs->open_flags;
610 if (!format) {
611 format = "qcow2";
614 drv = bdrv_find_format(format);
615 if (!drv) {
616 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
617 ret = -1;
618 goto out;
621 proto_drv = bdrv_find_protocol(filename);
622 if (!proto_drv) {
623 qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
624 ret = -1;
625 goto out;
628 ret = bdrv_img_create(filename, format, bs->filename,
629 bs->drv->format_name, NULL, -1, flags);
630 if (ret) {
631 goto out;
634 qemu_aio_flush();
635 bdrv_flush(bs);
637 bdrv_close(bs);
638 ret = bdrv_open(bs, filename, flags, drv);
640 * If reopening the image file we just created fails, fall back
641 * and try to re-open the original image. If that fails too, we
642 * are in serious trouble.
644 if (ret != 0) {
645 ret = bdrv_open(bs, old_filename, flags, old_drv);
646 if (ret != 0) {
647 qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
648 } else {
649 qerror_report(QERR_OPEN_FILE_FAILED, filename);
652 out:
653 if (ret) {
654 ret = -1;
657 return ret;
660 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
662 if (!bdrv_is_removable(bs)) {
663 qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
664 return -1;
666 if (!force && bdrv_is_locked(bs)) {
667 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
668 return -1;
670 bdrv_close(bs);
671 return 0;
674 int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
676 BlockDriverState *bs;
677 int force = qdict_get_try_bool(qdict, "force", 0);
678 const char *filename = qdict_get_str(qdict, "device");
680 bs = bdrv_find(filename);
681 if (!bs) {
682 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
683 return -1;
685 return eject_device(mon, bs, force);
688 int do_block_set_passwd(Monitor *mon, const QDict *qdict,
689 QObject **ret_data)
691 BlockDriverState *bs;
692 int err;
694 bs = bdrv_find(qdict_get_str(qdict, "device"));
695 if (!bs) {
696 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
697 return -1;
700 err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
701 if (err == -EINVAL) {
702 qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
703 return -1;
704 } else if (err < 0) {
705 qerror_report(QERR_INVALID_PASSWORD);
706 return -1;
709 return 0;
712 int do_change_block(Monitor *mon, const char *device,
713 const char *filename, const char *fmt)
715 BlockDriverState *bs;
716 BlockDriver *drv = NULL;
717 int bdrv_flags;
719 bs = bdrv_find(device);
720 if (!bs) {
721 qerror_report(QERR_DEVICE_NOT_FOUND, device);
722 return -1;
724 if (fmt) {
725 drv = bdrv_find_whitelisted_format(fmt);
726 if (!drv) {
727 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
728 return -1;
731 if (eject_device(mon, bs, 0) < 0) {
732 return -1;
734 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
735 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
736 if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
737 qerror_report(QERR_OPEN_FILE_FAILED, filename);
738 return -1;
740 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
743 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
745 const char *id = qdict_get_str(qdict, "id");
746 BlockDriverState *bs;
748 bs = bdrv_find(id);
749 if (!bs) {
750 qerror_report(QERR_DEVICE_NOT_FOUND, id);
751 return -1;
753 if (bdrv_in_use(bs)) {
754 qerror_report(QERR_DEVICE_IN_USE, id);
755 return -1;
758 /* quiesce block driver; prevent further io */
759 qemu_aio_flush();
760 bdrv_flush(bs);
761 bdrv_close(bs);
763 /* if we have a device associated with this BlockDriverState (bs->peer)
764 * then we need to make the drive anonymous until the device
765 * can be removed. If this is a drive with no device backing
766 * then we can just get rid of the block driver state right here.
768 if (bs->peer) {
769 bdrv_make_anon(bs);
770 } else {
771 drive_uninit(drive_get_by_blockdev(bs));
774 return 0;
778 * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
779 * existing QERR_ macro mess is cleaned up. A good example for better
780 * error reports can be found in the qemu-img resize code.
782 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
784 const char *device = qdict_get_str(qdict, "device");
785 int64_t size = qdict_get_int(qdict, "size");
786 BlockDriverState *bs;
788 bs = bdrv_find(device);
789 if (!bs) {
790 qerror_report(QERR_DEVICE_NOT_FOUND, device);
791 return -1;
794 if (size < 0) {
795 qerror_report(QERR_UNDEFINED_ERROR);
796 return -1;
799 if (bdrv_truncate(bs, size)) {
800 qerror_report(QERR_UNDEFINED_ERROR);
801 return -1;
804 return 0;