Tizen 2.0 Release
[external/qemu.git] / block.c
1 /*
2  * QEMU System Emulator block driver
3  *
4  * Copyright (c) 2003 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "config-host.h"
25 #include "qemu-common.h"
26 #include "trace.h"
27 #include "monitor.h"
28 #include "block_int.h"
29 #include "module.h"
30 #include "qemu-objects.h"
31
32 #ifdef CONFIG_BSD
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <sys/queue.h>
37 #ifndef __DragonFly__
38 #include <sys/disk.h>
39 #endif
40 #endif
41
42 #ifdef _WIN32
43 #include <windows.h>
44 #endif
45
46 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
47         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
48         BlockDriverCompletionFunc *cb, void *opaque);
49 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
50         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
51         BlockDriverCompletionFunc *cb, void *opaque);
52 static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
53         BlockDriverCompletionFunc *cb, void *opaque);
54 static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
55         BlockDriverCompletionFunc *cb, void *opaque);
56 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
57                         uint8_t *buf, int nb_sectors);
58 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
59                          const uint8_t *buf, int nb_sectors);
60
61 static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
62     QTAILQ_HEAD_INITIALIZER(bdrv_states);
63
64 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
65     QLIST_HEAD_INITIALIZER(bdrv_drivers);
66
67 /* The device to use for VM snapshots */
68 static BlockDriverState *bs_snapshots;
69
70 /* If non-zero, use only whitelisted block drivers */
71 static int use_bdrv_whitelist;
72
73 #ifdef _WIN32
74 static int is_windows_drive_prefix(const char *filename)
75 {
76     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
77              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
78             filename[1] == ':');
79 }
80
81 int is_windows_drive(const char *filename)
82 {
83     if (is_windows_drive_prefix(filename) &&
84         filename[2] == '\0')
85         return 1;
86     if (strstart(filename, "\\\\.\\", NULL) ||
87         strstart(filename, "//./", NULL))
88         return 1;
89     return 0;
90 }
91 #endif
92
93 /* check if the path starts with "<protocol>:" */
94 static int path_has_protocol(const char *path)
95 {
96 #ifdef _WIN32
97     if (is_windows_drive(path) ||
98         is_windows_drive_prefix(path)) {
99         return 0;
100     }
101 #endif
102
103     return strchr(path, ':') != NULL;
104 }
105
106 int path_is_absolute(const char *path)
107 {
108     const char *p;
109 #ifdef _WIN32
110     /* specific case for names like: "\\.\d:" */
111     if (*path == '/' || *path == '\\')
112         return 1;
113 #endif
114     p = strchr(path, ':');
115     if (p)
116         p++;
117     else
118         p = path;
119 #ifdef _WIN32
120     return (*p == '/' || *p == '\\');
121 #else
122     return (*p == '/');
123 #endif
124 }
125
126 /* if filename is absolute, just copy it to dest. Otherwise, build a
127    path to it by considering it is relative to base_path. URL are
128    supported. */
129 void path_combine(char *dest, int dest_size,
130                   const char *base_path,
131                   const char *filename)
132 {
133     const char *p, *p1;
134     int len;
135
136     if (dest_size <= 0)
137         return;
138     if (path_is_absolute(filename)) {
139         pstrcpy(dest, dest_size, filename);
140     } else {
141         p = strchr(base_path, ':');
142         if (p)
143             p++;
144         else
145             p = base_path;
146         p1 = strrchr(base_path, '/');
147 #ifdef _WIN32
148         {
149             const char *p2;
150             p2 = strrchr(base_path, '\\');
151             if (!p1 || p2 > p1)
152                 p1 = p2;
153         }
154 #endif
155         if (p1)
156             p1++;
157         else
158             p1 = base_path;
159         if (p1 > p)
160             p = p1;
161         len = p - base_path;
162         if (len > dest_size - 1)
163             len = dest_size - 1;
164         memcpy(dest, base_path, len);
165         dest[len] = '\0';
166         pstrcat(dest, dest_size, filename);
167     }
168 }
169
170 void bdrv_register(BlockDriver *bdrv)
171 {
172     if (!bdrv->bdrv_aio_readv) {
173         /* add AIO emulation layer */
174         bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
175         bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
176     } else if (!bdrv->bdrv_read) {
177         /* add synchronous IO emulation layer */
178         bdrv->bdrv_read = bdrv_read_em;
179         bdrv->bdrv_write = bdrv_write_em;
180     }
181
182     if (!bdrv->bdrv_aio_flush)
183         bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
184
185     QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
186 }
187
188 /* create a new block device (by default it is empty) */
189 BlockDriverState *bdrv_new(const char *device_name)
190 {
191     BlockDriverState *bs;
192
193     bs = qemu_mallocz(sizeof(BlockDriverState));
194     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
195     if (device_name[0] != '\0') {
196         QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
197     }
198     return bs;
199 }
200
201 BlockDriver *bdrv_find_format(const char *format_name)
202 {
203     BlockDriver *drv1;
204     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
205         if (!strcmp(drv1->format_name, format_name)) {
206             return drv1;
207         }
208     }
209     return NULL;
210 }
211
212 static int bdrv_is_whitelisted(BlockDriver *drv)
213 {
214     static const char *whitelist[] = {
215         CONFIG_BDRV_WHITELIST
216     };
217     const char **p;
218
219     if (!whitelist[0])
220         return 1;               /* no whitelist, anything goes */
221
222     for (p = whitelist; *p; p++) {
223         if (!strcmp(drv->format_name, *p)) {
224             return 1;
225         }
226     }
227     return 0;
228 }
229
230 BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
231 {
232     BlockDriver *drv = bdrv_find_format(format_name);
233     return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
234 }
235
236 int bdrv_create(BlockDriver *drv, const char* filename,
237     QEMUOptionParameter *options)
238 {
239     if (!drv->bdrv_create)
240         return -ENOTSUP;
241
242     return drv->bdrv_create(filename, options);
243 }
244
245 int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
246 {
247     BlockDriver *drv;
248
249     drv = bdrv_find_protocol(filename);
250     if (drv == NULL) {
251         return -ENOENT;
252     }
253
254     return bdrv_create(drv, filename, options);
255 }
256
257 #ifdef _WIN32
258 void get_tmp_filename(char *filename, int size)
259 {
260     char temp_dir[MAX_PATH];
261
262     GetTempPath(MAX_PATH, temp_dir);
263     GetTempFileName(temp_dir, "qem", 0, filename);
264 }
265 #else
266 void get_tmp_filename(char *filename, int size)
267 {
268     int fd;
269     const char *tmpdir;
270     /* XXX: race condition possible */
271     tmpdir = getenv("TMPDIR");
272     if (!tmpdir)
273         tmpdir = "/tmp";
274     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
275     fd = mkstemp(filename);
276     close(fd);
277 }
278 #endif
279
280 /*
281  * Detect host devices. By convention, /dev/cdrom[N] is always
282  * recognized as a host CDROM.
283  */
284 static BlockDriver *find_hdev_driver(const char *filename)
285 {
286     int score_max = 0, score;
287     BlockDriver *drv = NULL, *d;
288
289     QLIST_FOREACH(d, &bdrv_drivers, list) {
290         if (d->bdrv_probe_device) {
291             score = d->bdrv_probe_device(filename);
292             if (score > score_max) {
293                 score_max = score;
294                 drv = d;
295             }
296         }
297     }
298
299     return drv;
300 }
301
302 BlockDriver *bdrv_find_protocol(const char *filename)
303 {
304     BlockDriver *drv1;
305     char protocol[128];
306     int len;
307     const char *p;
308
309     /* TODO Drivers without bdrv_file_open must be specified explicitly */
310
311     /*
312      * XXX(hch): we really should not let host device detection
313      * override an explicit protocol specification, but moving this
314      * later breaks access to device names with colons in them.
315      * Thanks to the brain-dead persistent naming schemes on udev-
316      * based Linux systems those actually are quite common.
317      */
318     drv1 = find_hdev_driver(filename);
319     if (drv1) {
320         return drv1;
321     }
322
323     if (!path_has_protocol(filename)) {
324         return bdrv_find_format("file");
325     }
326     p = strchr(filename, ':');
327     assert(p != NULL);
328     len = p - filename;
329     if (len > sizeof(protocol) - 1)
330         len = sizeof(protocol) - 1;
331     memcpy(protocol, filename, len);
332     protocol[len] = '\0';
333     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
334         if (drv1->protocol_name &&
335             !strcmp(drv1->protocol_name, protocol)) {
336             return drv1;
337         }
338     }
339     return NULL;
340 }
341
342 static int find_image_format(const char *filename, BlockDriver **pdrv)
343 {
344     int ret, score, score_max;
345     BlockDriver *drv1, *drv;
346     uint8_t buf[2048];
347     BlockDriverState *bs;
348
349     ret = bdrv_file_open(&bs, filename, 0);
350     if (ret < 0) {
351         *pdrv = NULL;
352         return ret;
353     }
354
355     /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
356     if (bs->sg || !bdrv_is_inserted(bs)) {
357         bdrv_delete(bs);
358         drv = bdrv_find_format("raw");
359         if (!drv) {
360             ret = -ENOENT;
361         }
362         *pdrv = drv;
363         return ret;
364     }
365
366     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
367     bdrv_delete(bs);
368     if (ret < 0) {
369         *pdrv = NULL;
370         return ret;
371     }
372
373     score_max = 0;
374     drv = NULL;
375     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
376         if (drv1->bdrv_probe) {
377             score = drv1->bdrv_probe(buf, ret, filename);
378             if (score > score_max) {
379                 score_max = score;
380                 drv = drv1;
381             }
382         }
383     }
384     if (!drv) {
385         ret = -ENOENT;
386     }
387     *pdrv = drv;
388     return ret;
389 }
390
391 /**
392  * Set the current 'total_sectors' value
393  */
394 static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
395 {
396     BlockDriver *drv = bs->drv;
397
398     /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
399     if (bs->sg)
400         return 0;
401
402     /* query actual device if possible, otherwise just trust the hint */
403     if (drv->bdrv_getlength) {
404         int64_t length = drv->bdrv_getlength(bs);
405         if (length < 0) {
406             return length;
407         }
408         hint = length >> BDRV_SECTOR_BITS;
409     }
410
411     bs->total_sectors = hint;
412     return 0;
413 }
414
415 /*
416  * Common part for opening disk images and files
417  */
418 static int bdrv_open_common(BlockDriverState *bs, const char *filename,
419     int flags, BlockDriver *drv)
420 {
421     int ret, open_flags;
422
423     assert(drv != NULL);
424
425     bs->file = NULL;
426     bs->total_sectors = 0;
427     bs->encrypted = 0;
428     bs->valid_key = 0;
429     bs->open_flags = flags;
430     /* buffer_alignment defaulted to 512, drivers can change this value */
431     bs->buffer_alignment = 512;
432
433     pstrcpy(bs->filename, sizeof(bs->filename), filename);
434
435     if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
436         return -ENOTSUP;
437     }
438
439     bs->drv = drv;
440     bs->opaque = qemu_mallocz(drv->instance_size);
441
442     /*
443      * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
444      * write cache to the guest.  We do need the fdatasync to flush
445      * out transactions for block allocations, and we maybe have a
446      * volatile write cache in our backing device to deal with.
447      */
448     if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
449         bs->enable_write_cache = 1;
450
451     /*
452      * Clear flags that are internal to the block layer before opening the
453      * image.
454      */
455     open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
456
457     /*
458      * Snapshots should be writeable.
459      */
460     if (bs->is_temporary) {
461         open_flags |= BDRV_O_RDWR;
462     }
463
464     /* Open the image, either directly or using a protocol */
465     if (drv->bdrv_file_open) {
466         ret = drv->bdrv_file_open(bs, filename, open_flags);
467     } else {
468         ret = bdrv_file_open(&bs->file, filename, open_flags);
469         if (ret >= 0) {
470             ret = drv->bdrv_open(bs, open_flags);
471         }
472     }
473
474     if (ret < 0) {
475         goto free_and_fail;
476     }
477
478     bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
479
480     ret = refresh_total_sectors(bs, bs->total_sectors);
481     if (ret < 0) {
482         goto free_and_fail;
483     }
484
485 #ifndef _WIN32
486     if (bs->is_temporary) {
487         unlink(filename);
488     }
489 #endif
490     return 0;
491
492 free_and_fail:
493     if (bs->file) {
494         bdrv_delete(bs->file);
495         bs->file = NULL;
496     }
497     qemu_free(bs->opaque);
498     bs->opaque = NULL;
499     bs->drv = NULL;
500     return ret;
501 }
502
503 /*
504  * Opens a file using a protocol (file, host_device, nbd, ...)
505  */
506 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
507 {
508     BlockDriverState *bs;
509     BlockDriver *drv;
510     int ret;
511
512     drv = bdrv_find_protocol(filename);
513     if (!drv) {
514         return -ENOENT;
515     }
516
517     bs = bdrv_new("");
518     ret = bdrv_open_common(bs, filename, flags, drv);
519     if (ret < 0) {
520         bdrv_delete(bs);
521         return ret;
522     }
523     bs->growable = 1;
524     *pbs = bs;
525     return 0;
526 }
527
528 /*
529  * Opens a disk image (raw, qcow2, vmdk, ...)
530  */
531 int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
532               BlockDriver *drv)
533 {
534     int ret;
535
536     if (flags & BDRV_O_SNAPSHOT) {
537         BlockDriverState *bs1;
538         int64_t total_size;
539         int is_protocol = 0;
540         BlockDriver *bdrv_qcow2;
541         QEMUOptionParameter *options;
542         char tmp_filename[PATH_MAX];
543         char backing_filename[PATH_MAX];
544
545         /* if snapshot, we create a temporary backing file and open it
546            instead of opening 'filename' directly */
547
548         /* if there is a backing file, use it */
549         bs1 = bdrv_new("");
550         ret = bdrv_open(bs1, filename, 0, drv);
551         if (ret < 0) {
552             bdrv_delete(bs1);
553             return ret;
554         }
555         total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
556
557         if (bs1->drv && bs1->drv->protocol_name)
558             is_protocol = 1;
559
560         bdrv_delete(bs1);
561
562         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
563
564         /* Real path is meaningless for protocols */
565         if (is_protocol)
566             snprintf(backing_filename, sizeof(backing_filename),
567                      "%s", filename);
568         else if (!realpath(filename, backing_filename))
569             return -errno;
570
571         bdrv_qcow2 = bdrv_find_format("qcow2");
572         options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
573
574         set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
575         set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
576         if (drv) {
577             set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
578                 drv->format_name);
579         }
580
581         ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
582         free_option_parameters(options);
583         if (ret < 0) {
584             return ret;
585         }
586
587         filename = tmp_filename;
588         drv = bdrv_qcow2;
589         bs->is_temporary = 1;
590     }
591
592     /* Find the right image format driver */
593     if (!drv) {
594         ret = find_image_format(filename, &drv);
595     }
596
597     if (!drv) {
598         goto unlink_and_fail;
599     }
600
601     /* Open the image */
602     ret = bdrv_open_common(bs, filename, flags, drv);
603     if (ret < 0) {
604         goto unlink_and_fail;
605     }
606
607     /* If there is a backing file, use it */
608     if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
609         char backing_filename[PATH_MAX];
610         int back_flags;
611         BlockDriver *back_drv = NULL;
612
613         bs->backing_hd = bdrv_new("");
614
615         if (path_has_protocol(bs->backing_file)) {
616             pstrcpy(backing_filename, sizeof(backing_filename),
617                     bs->backing_file);
618         } else {
619             path_combine(backing_filename, sizeof(backing_filename),
620                          filename, bs->backing_file);
621         }
622
623         if (bs->backing_format[0] != '\0') {
624             back_drv = bdrv_find_format(bs->backing_format);
625         }
626
627         /* backing files always opened read-only */
628         back_flags =
629             flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
630
631         ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
632         if (ret < 0) {
633             bdrv_close(bs);
634             return ret;
635         }
636         if (bs->is_temporary) {
637             bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
638         } else {
639             /* base image inherits from "parent" */
640             bs->backing_hd->keep_read_only = bs->keep_read_only;
641         }
642     }
643
644     if (!bdrv_key_required(bs)) {
645         /* call the change callback */
646         bs->media_changed = 1;
647         if (bs->change_cb)
648             bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
649     }
650
651     return 0;
652
653 unlink_and_fail:
654     if (bs->is_temporary) {
655         unlink(filename);
656     }
657     return ret;
658 }
659
660 void bdrv_close(BlockDriverState *bs)
661 {
662     if (bs->drv) {
663         if (bs == bs_snapshots) {
664             bs_snapshots = NULL;
665         }
666         if (bs->backing_hd) {
667             bdrv_delete(bs->backing_hd);
668             bs->backing_hd = NULL;
669         }
670         bs->drv->bdrv_close(bs);
671         qemu_free(bs->opaque);
672 #ifdef _WIN32
673         if (bs->is_temporary) {
674             unlink(bs->filename);
675         }
676 #endif
677         bs->opaque = NULL;
678         bs->drv = NULL;
679
680         if (bs->file != NULL) {
681             bdrv_close(bs->file);
682         }
683
684         /* call the change callback */
685         bs->media_changed = 1;
686         if (bs->change_cb)
687             bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
688     }
689 }
690
691 void bdrv_close_all(void)
692 {
693     BlockDriverState *bs;
694
695     QTAILQ_FOREACH(bs, &bdrv_states, list) {
696         bdrv_close(bs);
697     }
698 }
699
700 /* make a BlockDriverState anonymous by removing from bdrv_state list.
701    Also, NULL terminate the device_name to prevent double remove */
702 void bdrv_make_anon(BlockDriverState *bs)
703 {
704     if (bs->device_name[0] != '\0') {
705         QTAILQ_REMOVE(&bdrv_states, bs, list);
706     }
707     bs->device_name[0] = '\0';
708 }
709
710 void bdrv_delete(BlockDriverState *bs)
711 {
712     assert(!bs->peer);
713
714     /* remove from list, if necessary */
715     bdrv_make_anon(bs);
716
717     bdrv_close(bs);
718     if (bs->file != NULL) {
719         bdrv_delete(bs->file);
720     }
721
722     assert(bs != bs_snapshots);
723     qemu_free(bs);
724 }
725
726 int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
727 {
728     if (bs->peer) {
729         return -EBUSY;
730     }
731     bs->peer = qdev;
732     return 0;
733 }
734
735 void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
736 {
737     assert(bs->peer == qdev);
738     bs->peer = NULL;
739 }
740
741 DeviceState *bdrv_get_attached(BlockDriverState *bs)
742 {
743     return bs->peer;
744 }
745
746 /*
747  * Run consistency checks on an image
748  *
749  * Returns 0 if the check could be completed (it doesn't mean that the image is
750  * free of errors) or -errno when an internal error occured. The results of the
751  * check are stored in res.
752  */
753 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
754 {
755     if (bs->drv->bdrv_check == NULL) {
756         return -ENOTSUP;
757     }
758
759     memset(res, 0, sizeof(*res));
760     return bs->drv->bdrv_check(bs, res);
761 }
762
763 #define COMMIT_BUF_SECTORS 2048
764
765 /* commit COW file into the raw image */
766 int bdrv_commit(BlockDriverState *bs)
767 {
768     BlockDriver *drv = bs->drv;
769     BlockDriver *backing_drv;
770     int64_t sector, total_sectors;
771     int n, ro, open_flags;
772     int ret = 0, rw_ret = 0;
773     uint8_t *buf;
774     char filename[1024];
775     BlockDriverState *bs_rw, *bs_ro;
776
777     if (!drv)
778         return -ENOMEDIUM;
779     
780     if (!bs->backing_hd) {
781         return -ENOTSUP;
782     }
783
784     if (bs->backing_hd->keep_read_only) {
785         return -EACCES;
786     }
787
788     backing_drv = bs->backing_hd->drv;
789     ro = bs->backing_hd->read_only;
790     strncpy(filename, bs->backing_hd->filename, sizeof(filename));
791     open_flags =  bs->backing_hd->open_flags;
792
793     if (ro) {
794         /* re-open as RW */
795         bdrv_delete(bs->backing_hd);
796         bs->backing_hd = NULL;
797         bs_rw = bdrv_new("");
798         rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
799             backing_drv);
800         if (rw_ret < 0) {
801             bdrv_delete(bs_rw);
802             /* try to re-open read-only */
803             bs_ro = bdrv_new("");
804             ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
805                 backing_drv);
806             if (ret < 0) {
807                 bdrv_delete(bs_ro);
808                 /* drive not functional anymore */
809                 bs->drv = NULL;
810                 return ret;
811             }
812             bs->backing_hd = bs_ro;
813             return rw_ret;
814         }
815         bs->backing_hd = bs_rw;
816     }
817
818     total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
819     buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
820
821     for (sector = 0; sector < total_sectors; sector += n) {
822         if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
823
824             if (bdrv_read(bs, sector, buf, n) != 0) {
825                 ret = -EIO;
826                 goto ro_cleanup;
827             }
828
829             if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
830                 ret = -EIO;
831                 goto ro_cleanup;
832             }
833         }
834     }
835
836     if (drv->bdrv_make_empty) {
837         ret = drv->bdrv_make_empty(bs);
838         bdrv_flush(bs);
839     }
840
841     /*
842      * Make sure all data we wrote to the backing device is actually
843      * stable on disk.
844      */
845     if (bs->backing_hd)
846         bdrv_flush(bs->backing_hd);
847
848 ro_cleanup:
849     qemu_free(buf);
850
851     if (ro) {
852         /* re-open as RO */
853         bdrv_delete(bs->backing_hd);
854         bs->backing_hd = NULL;
855         bs_ro = bdrv_new("");
856         ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
857             backing_drv);
858         if (ret < 0) {
859             bdrv_delete(bs_ro);
860             /* drive not functional anymore */
861             bs->drv = NULL;
862             return ret;
863         }
864         bs->backing_hd = bs_ro;
865         bs->backing_hd->keep_read_only = 0;
866     }
867
868     return ret;
869 }
870
871 void bdrv_commit_all(void)
872 {
873     BlockDriverState *bs;
874
875     QTAILQ_FOREACH(bs, &bdrv_states, list) {
876         bdrv_commit(bs);
877     }
878 }
879
880 /*
881  * Return values:
882  * 0        - success
883  * -EINVAL  - backing format specified, but no file
884  * -ENOSPC  - can't update the backing file because no space is left in the
885  *            image file header
886  * -ENOTSUP - format driver doesn't support changing the backing file
887  */
888 int bdrv_change_backing_file(BlockDriverState *bs,
889     const char *backing_file, const char *backing_fmt)
890 {
891     BlockDriver *drv = bs->drv;
892
893     if (drv->bdrv_change_backing_file != NULL) {
894         return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
895     } else {
896         return -ENOTSUP;
897     }
898 }
899
900 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
901                                    size_t size)
902 {
903     int64_t len;
904
905     if (!bdrv_is_inserted(bs))
906         return -ENOMEDIUM;
907
908     if (bs->growable)
909         return 0;
910
911     len = bdrv_getlength(bs);
912
913     if (offset < 0)
914         return -EIO;
915
916     if ((offset > len) || (len - offset < size))
917         return -EIO;
918
919     return 0;
920 }
921
922 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
923                               int nb_sectors)
924 {
925     return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
926                                    nb_sectors * BDRV_SECTOR_SIZE);
927 }
928
929 /* return < 0 if error. See bdrv_write() for the return codes */
930 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
931               uint8_t *buf, int nb_sectors)
932 {
933     BlockDriver *drv = bs->drv;
934
935     if (!drv)
936         return -ENOMEDIUM;
937     if (bdrv_check_request(bs, sector_num, nb_sectors))
938         return -EIO;
939
940     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
941 }
942
943 static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
944                              int nb_sectors, int dirty)
945 {
946     int64_t start, end;
947     unsigned long val, idx, bit;
948
949     start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
950     end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
951
952     for (; start <= end; start++) {
953         idx = start / (sizeof(unsigned long) * 8);
954         bit = start % (sizeof(unsigned long) * 8);
955         val = bs->dirty_bitmap[idx];
956         if (dirty) {
957             if (!(val & (1UL << bit))) {
958                 bs->dirty_count++;
959                 val |= 1UL << bit;
960             }
961         } else {
962             if (val & (1UL << bit)) {
963                 bs->dirty_count--;
964                 val &= ~(1UL << bit);
965             }
966         }
967         bs->dirty_bitmap[idx] = val;
968     }
969 }
970
971 /* Return < 0 if error. Important errors are:
972   -EIO         generic I/O error (may happen for all errors)
973   -ENOMEDIUM   No media inserted.
974   -EINVAL      Invalid sector number or nb_sectors
975   -EACCES      Trying to write a read-only device
976 */
977 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
978                const uint8_t *buf, int nb_sectors)
979 {
980     BlockDriver *drv = bs->drv;
981     if (!bs->drv)
982         return -ENOMEDIUM;
983     if (bs->read_only)
984         return -EACCES;
985     if (bdrv_check_request(bs, sector_num, nb_sectors))
986         return -EIO;
987
988     if (bs->dirty_bitmap) {
989         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
990     }
991
992     if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
993         bs->wr_highest_sector = sector_num + nb_sectors - 1;
994     }
995
996     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
997 }
998
999 int bdrv_pread(BlockDriverState *bs, int64_t offset,
1000                void *buf, int count1)
1001 {
1002     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1003     int len, nb_sectors, count;
1004     int64_t sector_num;
1005     int ret;
1006
1007     count = count1;
1008     /* first read to align to sector start */
1009     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1010     if (len > count)
1011         len = count;
1012     sector_num = offset >> BDRV_SECTOR_BITS;
1013     if (len > 0) {
1014         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1015             return ret;
1016         memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
1017         count -= len;
1018         if (count == 0)
1019             return count1;
1020         sector_num++;
1021         buf += len;
1022     }
1023
1024     /* read the sectors "in place" */
1025     nb_sectors = count >> BDRV_SECTOR_BITS;
1026     if (nb_sectors > 0) {
1027         if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1028             return ret;
1029         sector_num += nb_sectors;
1030         len = nb_sectors << BDRV_SECTOR_BITS;
1031         buf += len;
1032         count -= len;
1033     }
1034
1035     /* add data from the last sector */
1036     if (count > 0) {
1037         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1038             return ret;
1039         memcpy(buf, tmp_buf, count);
1040     }
1041     return count1;
1042 }
1043
1044 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1045                 const void *buf, int count1)
1046 {
1047     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1048     int len, nb_sectors, count;
1049     int64_t sector_num;
1050     int ret;
1051
1052     count = count1;
1053     /* first write to align to sector start */
1054     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1055     if (len > count)
1056         len = count;
1057     sector_num = offset >> BDRV_SECTOR_BITS;
1058     if (len > 0) {
1059         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1060             return ret;
1061         memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
1062         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1063             return ret;
1064         count -= len;
1065         if (count == 0)
1066             return count1;
1067         sector_num++;
1068         buf += len;
1069     }
1070
1071     /* write the sectors "in place" */
1072     nb_sectors = count >> BDRV_SECTOR_BITS;
1073     if (nb_sectors > 0) {
1074         if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1075             return ret;
1076         sector_num += nb_sectors;
1077         len = nb_sectors << BDRV_SECTOR_BITS;
1078         buf += len;
1079         count -= len;
1080     }
1081
1082     /* add data from the last sector */
1083     if (count > 0) {
1084         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1085             return ret;
1086         memcpy(tmp_buf, buf, count);
1087         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1088             return ret;
1089     }
1090     return count1;
1091 }
1092
1093 /*
1094  * Writes to the file and ensures that no writes are reordered across this
1095  * request (acts as a barrier)
1096  *
1097  * Returns 0 on success, -errno in error cases.
1098  */
1099 int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1100     const void *buf, int count)
1101 {
1102     int ret;
1103
1104     ret = bdrv_pwrite(bs, offset, buf, count);
1105     if (ret < 0) {
1106         return ret;
1107     }
1108
1109     /* No flush needed for cache=writethrough, it uses O_DSYNC */
1110     if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1111         bdrv_flush(bs);
1112     }
1113
1114     return 0;
1115 }
1116
1117 /*
1118  * Writes to the file and ensures that no writes are reordered across this
1119  * request (acts as a barrier)
1120  *
1121  * Returns 0 on success, -errno in error cases.
1122  */
1123 int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1124     const uint8_t *buf, int nb_sectors)
1125 {
1126     return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1127         buf, BDRV_SECTOR_SIZE * nb_sectors);
1128 }
1129
1130 /**
1131  * Truncate file to 'offset' bytes (needed only for file protocols)
1132  */
1133 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1134 {
1135     BlockDriver *drv = bs->drv;
1136     int ret;
1137     if (!drv)
1138         return -ENOMEDIUM;
1139     if (!drv->bdrv_truncate)
1140         return -ENOTSUP;
1141     if (bs->read_only)
1142         return -EACCES;
1143     if (bdrv_in_use(bs))
1144         return -EBUSY;
1145     ret = drv->bdrv_truncate(bs, offset);
1146     if (ret == 0) {
1147         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1148         if (bs->change_cb) {
1149             bs->change_cb(bs->change_opaque, CHANGE_SIZE);
1150         }
1151     }
1152     return ret;
1153 }
1154
1155 /**
1156  * Length of a file in bytes. Return < 0 if error or unknown.
1157  */
1158 int64_t bdrv_getlength(BlockDriverState *bs)
1159 {
1160     BlockDriver *drv = bs->drv;
1161     if (!drv)
1162         return -ENOMEDIUM;
1163
1164     /* Fixed size devices use the total_sectors value for speed instead of
1165        issuing a length query (like lseek) on each call.  Also, legacy block
1166        drivers don't provide a bdrv_getlength function and must use
1167        total_sectors. */
1168     if (!bs->growable || !drv->bdrv_getlength) {
1169         return bs->total_sectors * BDRV_SECTOR_SIZE;
1170     }
1171     return drv->bdrv_getlength(bs);
1172 }
1173
1174 /* return 0 as number of sectors if no device present or error */
1175 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1176 {
1177     int64_t length;
1178     length = bdrv_getlength(bs);
1179     if (length < 0)
1180         length = 0;
1181     else
1182         length = length >> BDRV_SECTOR_BITS;
1183     *nb_sectors_ptr = length;
1184 }
1185
1186 struct partition {
1187         uint8_t boot_ind;           /* 0x80 - active */
1188         uint8_t head;               /* starting head */
1189         uint8_t sector;             /* starting sector */
1190         uint8_t cyl;                /* starting cylinder */
1191         uint8_t sys_ind;            /* What partition type */
1192         uint8_t end_head;           /* end head */
1193         uint8_t end_sector;         /* end sector */
1194         uint8_t end_cyl;            /* end cylinder */
1195         uint32_t start_sect;        /* starting sector counting from 0 */
1196         uint32_t nr_sects;          /* nr of sectors in partition */
1197 } __attribute__((packed));
1198
1199 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1200 static int guess_disk_lchs(BlockDriverState *bs,
1201                            int *pcylinders, int *pheads, int *psectors)
1202 {
1203     uint8_t buf[BDRV_SECTOR_SIZE];
1204     int ret, i, heads, sectors, cylinders;
1205     struct partition *p;
1206     uint32_t nr_sects;
1207     uint64_t nb_sectors;
1208
1209     bdrv_get_geometry(bs, &nb_sectors);
1210
1211     ret = bdrv_read(bs, 0, buf, 1);
1212     if (ret < 0)
1213         return -1;
1214     /* test msdos magic */
1215     if (buf[510] != 0x55 || buf[511] != 0xaa)
1216         return -1;
1217     for(i = 0; i < 4; i++) {
1218         p = ((struct partition *)(buf + 0x1be)) + i;
1219         nr_sects = le32_to_cpu(p->nr_sects);
1220         if (nr_sects && p->end_head) {
1221             /* We make the assumption that the partition terminates on
1222                a cylinder boundary */
1223             heads = p->end_head + 1;
1224             sectors = p->end_sector & 63;
1225             if (sectors == 0)
1226                 continue;
1227             cylinders = nb_sectors / (heads * sectors);
1228             if (cylinders < 1 || cylinders > 16383)
1229                 continue;
1230             *pheads = heads;
1231             *psectors = sectors;
1232             *pcylinders = cylinders;
1233 #if 0
1234             printf("guessed geometry: LCHS=%d %d %d\n",
1235                    cylinders, heads, sectors);
1236 #endif
1237             return 0;
1238         }
1239     }
1240     return -1;
1241 }
1242
1243 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1244 {
1245     int translation, lba_detected = 0;
1246     int cylinders, heads, secs;
1247     uint64_t nb_sectors;
1248
1249     /* if a geometry hint is available, use it */
1250     bdrv_get_geometry(bs, &nb_sectors);
1251     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1252     translation = bdrv_get_translation_hint(bs);
1253     if (cylinders != 0) {
1254         *pcyls = cylinders;
1255         *pheads = heads;
1256         *psecs = secs;
1257     } else {
1258         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1259             if (heads > 16) {
1260                 /* if heads > 16, it means that a BIOS LBA
1261                    translation was active, so the default
1262                    hardware geometry is OK */
1263                 lba_detected = 1;
1264                 goto default_geometry;
1265             } else {
1266                 *pcyls = cylinders;
1267                 *pheads = heads;
1268                 *psecs = secs;
1269                 /* disable any translation to be in sync with
1270                    the logical geometry */
1271                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1272                     bdrv_set_translation_hint(bs,
1273                                               BIOS_ATA_TRANSLATION_NONE);
1274                 }
1275             }
1276         } else {
1277         default_geometry:
1278             /* if no geometry, use a standard physical disk geometry */
1279             cylinders = nb_sectors / (16 * 63);
1280
1281             if (cylinders > 16383)
1282                 cylinders = 16383;
1283             else if (cylinders < 2)
1284                 cylinders = 2;
1285             *pcyls = cylinders;
1286             *pheads = 16;
1287             *psecs = 63;
1288             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1289                 if ((*pcyls * *pheads) <= 131072) {
1290                     bdrv_set_translation_hint(bs,
1291                                               BIOS_ATA_TRANSLATION_LARGE);
1292                 } else {
1293                     bdrv_set_translation_hint(bs,
1294                                               BIOS_ATA_TRANSLATION_LBA);
1295                 }
1296             }
1297         }
1298         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1299     }
1300 }
1301
1302 void bdrv_set_geometry_hint(BlockDriverState *bs,
1303                             int cyls, int heads, int secs)
1304 {
1305     bs->cyls = cyls;
1306     bs->heads = heads;
1307     bs->secs = secs;
1308 }
1309
1310 void bdrv_set_type_hint(BlockDriverState *bs, int type)
1311 {
1312     bs->type = type;
1313     bs->removable = ((type == BDRV_TYPE_CDROM ||
1314                       type == BDRV_TYPE_FLOPPY));
1315 }
1316
1317 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1318 {
1319     bs->translation = translation;
1320 }
1321
1322 void bdrv_get_geometry_hint(BlockDriverState *bs,
1323                             int *pcyls, int *pheads, int *psecs)
1324 {
1325     *pcyls = bs->cyls;
1326     *pheads = bs->heads;
1327     *psecs = bs->secs;
1328 }
1329
1330 int bdrv_get_type_hint(BlockDriverState *bs)
1331 {
1332     return bs->type;
1333 }
1334
1335 int bdrv_get_translation_hint(BlockDriverState *bs)
1336 {
1337     return bs->translation;
1338 }
1339
1340 void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1341                        BlockErrorAction on_write_error)
1342 {
1343     bs->on_read_error = on_read_error;
1344     bs->on_write_error = on_write_error;
1345 }
1346
1347 BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1348 {
1349     return is_read ? bs->on_read_error : bs->on_write_error;
1350 }
1351
1352 void bdrv_set_removable(BlockDriverState *bs, int removable)
1353 {
1354     bs->removable = removable;
1355     if (removable && bs == bs_snapshots) {
1356         bs_snapshots = NULL;
1357     }
1358 }
1359
1360 int bdrv_is_removable(BlockDriverState *bs)
1361 {
1362     return bs->removable;
1363 }
1364
1365 int bdrv_is_read_only(BlockDriverState *bs)
1366 {
1367     return bs->read_only;
1368 }
1369
1370 int bdrv_is_sg(BlockDriverState *bs)
1371 {
1372     return bs->sg;
1373 }
1374
1375 int bdrv_enable_write_cache(BlockDriverState *bs)
1376 {
1377     return bs->enable_write_cache;
1378 }
1379
1380 /* XXX: no longer used */
1381 void bdrv_set_change_cb(BlockDriverState *bs,
1382                         void (*change_cb)(void *opaque, int reason),
1383                         void *opaque)
1384 {
1385     bs->change_cb = change_cb;
1386     bs->change_opaque = opaque;
1387 }
1388
1389 int bdrv_is_encrypted(BlockDriverState *bs)
1390 {
1391     if (bs->backing_hd && bs->backing_hd->encrypted)
1392         return 1;
1393     return bs->encrypted;
1394 }
1395
1396 int bdrv_key_required(BlockDriverState *bs)
1397 {
1398     BlockDriverState *backing_hd = bs->backing_hd;
1399
1400     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1401         return 1;
1402     return (bs->encrypted && !bs->valid_key);
1403 }
1404
1405 int bdrv_set_key(BlockDriverState *bs, const char *key)
1406 {
1407     int ret;
1408     if (bs->backing_hd && bs->backing_hd->encrypted) {
1409         ret = bdrv_set_key(bs->backing_hd, key);
1410         if (ret < 0)
1411             return ret;
1412         if (!bs->encrypted)
1413             return 0;
1414     }
1415     if (!bs->encrypted) {
1416         return -EINVAL;
1417     } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1418         return -ENOMEDIUM;
1419     }
1420     ret = bs->drv->bdrv_set_key(bs, key);
1421     if (ret < 0) {
1422         bs->valid_key = 0;
1423     } else if (!bs->valid_key) {
1424         bs->valid_key = 1;
1425         /* call the change callback now, we skipped it on open */
1426         bs->media_changed = 1;
1427         if (bs->change_cb)
1428             bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
1429     }
1430     return ret;
1431 }
1432
1433 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1434 {
1435     if (!bs->drv) {
1436         buf[0] = '\0';
1437     } else {
1438         pstrcpy(buf, buf_size, bs->drv->format_name);
1439     }
1440 }
1441
1442 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1443                          void *opaque)
1444 {
1445     BlockDriver *drv;
1446
1447     QLIST_FOREACH(drv, &bdrv_drivers, list) {
1448         it(opaque, drv->format_name);
1449     }
1450 }
1451
1452 BlockDriverState *bdrv_find(const char *name)
1453 {
1454     BlockDriverState *bs;
1455
1456     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1457         if (!strcmp(name, bs->device_name)) {
1458             return bs;
1459         }
1460     }
1461     return NULL;
1462 }
1463
1464 BlockDriverState *bdrv_next(BlockDriverState *bs)
1465 {
1466     if (!bs) {
1467         return QTAILQ_FIRST(&bdrv_states);
1468     }
1469     return QTAILQ_NEXT(bs, list);
1470 }
1471
1472 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1473 {
1474     BlockDriverState *bs;
1475
1476     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1477         it(opaque, bs);
1478     }
1479 }
1480
1481 const char *bdrv_get_device_name(BlockDriverState *bs)
1482 {
1483     return bs->device_name;
1484 }
1485
1486 int bdrv_flush(BlockDriverState *bs)
1487 {
1488     if (bs->open_flags & BDRV_O_NO_FLUSH) {
1489         return 0;
1490     }
1491
1492     if (bs->drv && bs->drv->bdrv_flush) {
1493         return bs->drv->bdrv_flush(bs);
1494     }
1495
1496     /*
1497      * Some block drivers always operate in either writethrough or unsafe mode
1498      * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1499      * the server works (because the behaviour is hardcoded or depends on
1500      * server-side configuration), so we can't ensure that everything is safe
1501      * on disk. Returning an error doesn't work because that would break guests
1502      * even if the server operates in writethrough mode.
1503      *
1504      * Let's hope the user knows what he's doing.
1505      */
1506     return 0;
1507 }
1508
1509 void bdrv_flush_all(void)
1510 {
1511     BlockDriverState *bs;
1512
1513     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1514         if (bs->drv && !bdrv_is_read_only(bs) &&
1515             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1516             bdrv_flush(bs);
1517         }
1518     }
1519 }
1520
1521 int bdrv_has_zero_init(BlockDriverState *bs)
1522 {
1523     assert(bs->drv);
1524
1525     if (bs->drv->bdrv_has_zero_init) {
1526         return bs->drv->bdrv_has_zero_init(bs);
1527     }
1528
1529     return 1;
1530 }
1531
1532 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
1533 {
1534     if (!bs->drv) {
1535         return -ENOMEDIUM;
1536     }
1537     if (!bs->drv->bdrv_discard) {
1538         return 0;
1539     }
1540     return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
1541 }
1542
1543 /*
1544  * Returns true iff the specified sector is present in the disk image. Drivers
1545  * not implementing the functionality are assumed to not support backing files,
1546  * hence all their sectors are reported as allocated.
1547  *
1548  * 'pnum' is set to the number of sectors (including and immediately following
1549  * the specified sector) that are known to be in the same
1550  * allocated/unallocated state.
1551  *
1552  * 'nb_sectors' is the max value 'pnum' should be set to.
1553  */
1554 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1555         int *pnum)
1556 {
1557     int64_t n;
1558     if (!bs->drv->bdrv_is_allocated) {
1559         if (sector_num >= bs->total_sectors) {
1560             *pnum = 0;
1561             return 0;
1562         }
1563         n = bs->total_sectors - sector_num;
1564         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1565         return 1;
1566     }
1567     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1568 }
1569
1570 void bdrv_mon_event(const BlockDriverState *bdrv,
1571                     BlockMonEventAction action, int is_read)
1572 {
1573     QObject *data;
1574     const char *action_str;
1575
1576     switch (action) {
1577     case BDRV_ACTION_REPORT:
1578         action_str = "report";
1579         break;
1580     case BDRV_ACTION_IGNORE:
1581         action_str = "ignore";
1582         break;
1583     case BDRV_ACTION_STOP:
1584         action_str = "stop";
1585         break;
1586     default:
1587         abort();
1588     }
1589
1590     data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1591                               bdrv->device_name,
1592                               action_str,
1593                               is_read ? "read" : "write");
1594     monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1595
1596     qobject_decref(data);
1597 }
1598
1599 static void bdrv_print_dict(QObject *obj, void *opaque)
1600 {
1601     QDict *bs_dict;
1602     Monitor *mon = opaque;
1603
1604     bs_dict = qobject_to_qdict(obj);
1605
1606     monitor_printf(mon, "%s: type=%s removable=%d",
1607                         qdict_get_str(bs_dict, "device"),
1608                         qdict_get_str(bs_dict, "type"),
1609                         qdict_get_bool(bs_dict, "removable"));
1610
1611     if (qdict_get_bool(bs_dict, "removable")) {
1612         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1613     }
1614
1615     if (qdict_haskey(bs_dict, "inserted")) {
1616         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1617
1618         monitor_printf(mon, " file=");
1619         monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1620         if (qdict_haskey(qdict, "backing_file")) {
1621             monitor_printf(mon, " backing_file=");
1622             monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1623         }
1624         monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1625                             qdict_get_bool(qdict, "ro"),
1626                             qdict_get_str(qdict, "drv"),
1627                             qdict_get_bool(qdict, "encrypted"));
1628     } else {
1629         monitor_printf(mon, " [not inserted]");
1630     }
1631
1632     monitor_printf(mon, "\n");
1633 }
1634
1635 void bdrv_info_print(Monitor *mon, const QObject *data)
1636 {
1637     qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1638 }
1639
1640 void bdrv_info(Monitor *mon, QObject **ret_data)
1641 {
1642     QList *bs_list;
1643     BlockDriverState *bs;
1644
1645     bs_list = qlist_new();
1646
1647     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1648         QObject *bs_obj;
1649         const char *type = "unknown";
1650
1651         switch(bs->type) {
1652         case BDRV_TYPE_HD:
1653             type = "hd";
1654             break;
1655         case BDRV_TYPE_CDROM:
1656             type = "cdrom";
1657             break;
1658         case BDRV_TYPE_FLOPPY:
1659             type = "floppy";
1660             break;
1661         }
1662
1663         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1664                                     "'removable': %i, 'locked': %i }",
1665                                     bs->device_name, type, bs->removable,
1666                                     bs->locked);
1667
1668         if (bs->drv) {
1669             QObject *obj;
1670             QDict *bs_dict = qobject_to_qdict(bs_obj);
1671
1672             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1673                                      "'encrypted': %i }",
1674                                      bs->filename, bs->read_only,
1675                                      bs->drv->format_name,
1676                                      bdrv_is_encrypted(bs));
1677             if (bs->backing_file[0] != '\0') {
1678                 QDict *qdict = qobject_to_qdict(obj);
1679                 qdict_put(qdict, "backing_file",
1680                           qstring_from_str(bs->backing_file));
1681             }
1682
1683             qdict_put_obj(bs_dict, "inserted", obj);
1684         }
1685         qlist_append_obj(bs_list, bs_obj);
1686     }
1687
1688     *ret_data = QOBJECT(bs_list);
1689 }
1690
1691 static void bdrv_stats_iter(QObject *data, void *opaque)
1692 {
1693     QDict *qdict;
1694     Monitor *mon = opaque;
1695
1696     qdict = qobject_to_qdict(data);
1697     monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1698
1699     qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1700     monitor_printf(mon, " rd_bytes=%" PRId64
1701                         " wr_bytes=%" PRId64
1702                         " rd_operations=%" PRId64
1703                         " wr_operations=%" PRId64
1704                         "\n",
1705                         qdict_get_int(qdict, "rd_bytes"),
1706                         qdict_get_int(qdict, "wr_bytes"),
1707                         qdict_get_int(qdict, "rd_operations"),
1708                         qdict_get_int(qdict, "wr_operations"));
1709 }
1710
1711 void bdrv_stats_print(Monitor *mon, const QObject *data)
1712 {
1713     qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1714 }
1715
1716 static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1717 {
1718     QObject *res;
1719     QDict *dict;
1720
1721     res = qobject_from_jsonf("{ 'stats': {"
1722                              "'rd_bytes': %" PRId64 ","
1723                              "'wr_bytes': %" PRId64 ","
1724                              "'rd_operations': %" PRId64 ","
1725                              "'wr_operations': %" PRId64 ","
1726                              "'wr_highest_offset': %" PRId64
1727                              "} }",
1728                              bs->rd_bytes, bs->wr_bytes,
1729                              bs->rd_ops, bs->wr_ops,
1730                              bs->wr_highest_sector *
1731                              (uint64_t)BDRV_SECTOR_SIZE);
1732     dict  = qobject_to_qdict(res);
1733
1734     if (*bs->device_name) {
1735         qdict_put(dict, "device", qstring_from_str(bs->device_name));
1736     }
1737
1738     if (bs->file) {
1739         QObject *parent = bdrv_info_stats_bs(bs->file);
1740         qdict_put_obj(dict, "parent", parent);
1741     }
1742
1743     return res;
1744 }
1745
1746 void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1747 {
1748     QObject *obj;
1749     QList *devices;
1750     BlockDriverState *bs;
1751
1752     devices = qlist_new();
1753
1754     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1755         obj = bdrv_info_stats_bs(bs);
1756         qlist_append_obj(devices, obj);
1757     }
1758
1759     *ret_data = QOBJECT(devices);
1760 }
1761
1762 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1763 {
1764     if (bs->backing_hd && bs->backing_hd->encrypted)
1765         return bs->backing_file;
1766     else if (bs->encrypted)
1767         return bs->filename;
1768     else
1769         return NULL;
1770 }
1771
1772 void bdrv_get_backing_filename(BlockDriverState *bs,
1773                                char *filename, int filename_size)
1774 {
1775     if (!bs->backing_file) {
1776         pstrcpy(filename, filename_size, "");
1777     } else {
1778         pstrcpy(filename, filename_size, bs->backing_file);
1779     }
1780 }
1781
1782 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1783                           const uint8_t *buf, int nb_sectors)
1784 {
1785     BlockDriver *drv = bs->drv;
1786     if (!drv)
1787         return -ENOMEDIUM;
1788     if (!drv->bdrv_write_compressed)
1789         return -ENOTSUP;
1790     if (bdrv_check_request(bs, sector_num, nb_sectors))
1791         return -EIO;
1792
1793     if (bs->dirty_bitmap) {
1794         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1795     }
1796
1797     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1798 }
1799
1800 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1801 {
1802     BlockDriver *drv = bs->drv;
1803     if (!drv)
1804         return -ENOMEDIUM;
1805     if (!drv->bdrv_get_info)
1806         return -ENOTSUP;
1807     memset(bdi, 0, sizeof(*bdi));
1808     return drv->bdrv_get_info(bs, bdi);
1809 }
1810
1811 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1812                       int64_t pos, int size)
1813 {
1814     BlockDriver *drv = bs->drv;
1815     if (!drv)
1816         return -ENOMEDIUM;
1817     if (drv->bdrv_save_vmstate)
1818         return drv->bdrv_save_vmstate(bs, buf, pos, size);
1819     if (bs->file)
1820         return bdrv_save_vmstate(bs->file, buf, pos, size);
1821     return -ENOTSUP;
1822 }
1823
1824 int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1825                       int64_t pos, int size)
1826 {
1827     BlockDriver *drv = bs->drv;
1828     if (!drv)
1829         return -ENOMEDIUM;
1830     if (drv->bdrv_load_vmstate)
1831         return drv->bdrv_load_vmstate(bs, buf, pos, size);
1832     if (bs->file)
1833         return bdrv_load_vmstate(bs->file, buf, pos, size);
1834     return -ENOTSUP;
1835 }
1836
1837 void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1838 {
1839     BlockDriver *drv = bs->drv;
1840
1841     if (!drv || !drv->bdrv_debug_event) {
1842         return;
1843     }
1844
1845     return drv->bdrv_debug_event(bs, event);
1846
1847 }
1848
1849 /**************************************************************/
1850 /* handling of snapshots */
1851
1852 int bdrv_can_snapshot(BlockDriverState *bs)
1853 {
1854     BlockDriver *drv = bs->drv;
1855     if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1856         return 0;
1857     }
1858
1859     if (!drv->bdrv_snapshot_create) {
1860         if (bs->file != NULL) {
1861             return bdrv_can_snapshot(bs->file);
1862         }
1863         return 0;
1864     }
1865
1866     return 1;
1867 }
1868
1869 int bdrv_is_snapshot(BlockDriverState *bs)
1870 {
1871     return !!(bs->open_flags & BDRV_O_SNAPSHOT);
1872 }
1873
1874 BlockDriverState *bdrv_snapshots(void)
1875 {
1876     BlockDriverState *bs;
1877
1878     if (bs_snapshots) {
1879         return bs_snapshots;
1880     }
1881
1882     bs = NULL;
1883     while ((bs = bdrv_next(bs))) {
1884         if (bdrv_can_snapshot(bs)) {
1885             bs_snapshots = bs;
1886             return bs;
1887         }
1888     }
1889     return NULL;
1890 }
1891
1892 int bdrv_snapshot_create(BlockDriverState *bs,
1893                          QEMUSnapshotInfo *sn_info)
1894 {
1895     BlockDriver *drv = bs->drv;
1896     if (!drv)
1897         return -ENOMEDIUM;
1898     if (drv->bdrv_snapshot_create)
1899         return drv->bdrv_snapshot_create(bs, sn_info);
1900     if (bs->file)
1901         return bdrv_snapshot_create(bs->file, sn_info);
1902     return -ENOTSUP;
1903 }
1904
1905 int bdrv_snapshot_goto(BlockDriverState *bs,
1906                        const char *snapshot_id)
1907 {
1908     BlockDriver *drv = bs->drv;
1909     int ret, open_ret;
1910
1911     if (!drv)
1912         return -ENOMEDIUM;
1913     if (drv->bdrv_snapshot_goto)
1914         return drv->bdrv_snapshot_goto(bs, snapshot_id);
1915
1916     if (bs->file) {
1917         drv->bdrv_close(bs);
1918         ret = bdrv_snapshot_goto(bs->file, snapshot_id);
1919         open_ret = drv->bdrv_open(bs, bs->open_flags);
1920         if (open_ret < 0) {
1921             bdrv_delete(bs->file);
1922             bs->drv = NULL;
1923             return open_ret;
1924         }
1925         return ret;
1926     }
1927
1928     return -ENOTSUP;
1929 }
1930
1931 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1932 {
1933     BlockDriver *drv = bs->drv;
1934     if (!drv)
1935         return -ENOMEDIUM;
1936     if (drv->bdrv_snapshot_delete)
1937         return drv->bdrv_snapshot_delete(bs, snapshot_id);
1938     if (bs->file)
1939         return bdrv_snapshot_delete(bs->file, snapshot_id);
1940     return -ENOTSUP;
1941 }
1942
1943 int bdrv_snapshot_list(BlockDriverState *bs,
1944                        QEMUSnapshotInfo **psn_info)
1945 {
1946     BlockDriver *drv = bs->drv;
1947     if (!drv)
1948         return -ENOMEDIUM;
1949     if (drv->bdrv_snapshot_list)
1950         return drv->bdrv_snapshot_list(bs, psn_info);
1951     if (bs->file)
1952         return bdrv_snapshot_list(bs->file, psn_info);
1953     return -ENOTSUP;
1954 }
1955
1956 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
1957         const char *snapshot_name)
1958 {
1959     BlockDriver *drv = bs->drv;
1960     if (!drv) {
1961         return -ENOMEDIUM;
1962     }
1963     if (!bs->read_only) {
1964         return -EINVAL;
1965     }
1966     if (drv->bdrv_snapshot_load_tmp) {
1967         return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
1968     }
1969     return -ENOTSUP;
1970 }
1971
1972 #define NB_SUFFIXES 4
1973
1974 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1975 {
1976     static const char suffixes[NB_SUFFIXES] = "KMGT";
1977     int64_t base;
1978     int i;
1979
1980     if (size <= 999) {
1981         snprintf(buf, buf_size, "%" PRId64, size);
1982     } else {
1983         base = 1024;
1984         for(i = 0; i < NB_SUFFIXES; i++) {
1985             if (size < (10 * base)) {
1986                 snprintf(buf, buf_size, "%0.1f%c",
1987                          (double)size / base,
1988                          suffixes[i]);
1989                 break;
1990             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1991                 snprintf(buf, buf_size, "%" PRId64 "%c",
1992                          ((size + (base >> 1)) / base),
1993                          suffixes[i]);
1994                 break;
1995             }
1996             base = base * 1024;
1997         }
1998     }
1999     return buf;
2000 }
2001
2002 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2003 {
2004     char buf1[128], date_buf[128], clock_buf[128];
2005 #ifdef _WIN32
2006     struct tm *ptm;
2007 #else
2008     struct tm tm;
2009 #endif
2010     time_t ti;
2011     int64_t secs;
2012
2013     if (!sn) {
2014         snprintf(buf, buf_size,
2015                  "%-10s%-20s%7s%20s%15s",
2016                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2017     } else {
2018         ti = sn->date_sec;
2019 #ifdef _WIN32
2020         ptm = localtime(&ti);
2021         strftime(date_buf, sizeof(date_buf),
2022                  "%Y-%m-%d %H:%M:%S", ptm);
2023 #else
2024         localtime_r(&ti, &tm);
2025         strftime(date_buf, sizeof(date_buf),
2026                  "%Y-%m-%d %H:%M:%S", &tm);
2027 #endif
2028         secs = sn->vm_clock_nsec / 1000000000;
2029         snprintf(clock_buf, sizeof(clock_buf),
2030                  "%02d:%02d:%02d.%03d",
2031                  (int)(secs / 3600),
2032                  (int)((secs / 60) % 60),
2033                  (int)(secs % 60),
2034                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
2035         snprintf(buf, buf_size,
2036                  "%-10s%-20s%7s%20s%15s",
2037                  sn->id_str, sn->name,
2038                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2039                  date_buf,
2040                  clock_buf);
2041     }
2042     return buf;
2043 }
2044
2045
2046 /**************************************************************/
2047 /* async I/Os */
2048
2049 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
2050                                  QEMUIOVector *qiov, int nb_sectors,
2051                                  BlockDriverCompletionFunc *cb, void *opaque)
2052 {
2053     BlockDriver *drv = bs->drv;
2054     BlockDriverAIOCB *ret;
2055
2056     trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2057
2058     if (!drv)
2059         return NULL;
2060     if (bdrv_check_request(bs, sector_num, nb_sectors))
2061         return NULL;
2062
2063     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
2064                               cb, opaque);
2065
2066     if (ret) {
2067         /* Update stats even though technically transfer has not happened. */
2068         bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2069         bs->rd_ops ++;
2070     }
2071
2072     return ret;
2073 }
2074
2075 typedef struct BlockCompleteData {
2076     BlockDriverCompletionFunc *cb;
2077     void *opaque;
2078     BlockDriverState *bs;
2079     int64_t sector_num;
2080     int nb_sectors;
2081 } BlockCompleteData;
2082
2083 static void block_complete_cb(void *opaque, int ret)
2084 {
2085     BlockCompleteData *b = opaque;
2086
2087     if (b->bs->dirty_bitmap) {
2088         set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1);
2089     }
2090     b->cb(b->opaque, ret);
2091     qemu_free(b);
2092 }
2093
2094 static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs,
2095                                              int64_t sector_num,
2096                                              int nb_sectors,
2097                                              BlockDriverCompletionFunc *cb,
2098                                              void *opaque)
2099 {
2100     BlockCompleteData *blkdata = qemu_mallocz(sizeof(BlockCompleteData));
2101
2102     blkdata->bs = bs;
2103     blkdata->cb = cb;
2104     blkdata->opaque = opaque;
2105     blkdata->sector_num = sector_num;
2106     blkdata->nb_sectors = nb_sectors;
2107
2108     return blkdata;
2109 }
2110
2111 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2112                                   QEMUIOVector *qiov, int nb_sectors,
2113                                   BlockDriverCompletionFunc *cb, void *opaque)
2114 {
2115     BlockDriver *drv = bs->drv;
2116     BlockDriverAIOCB *ret;
2117     BlockCompleteData *blk_cb_data;
2118
2119     trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2120
2121     if (!drv)
2122         return NULL;
2123     if (bs->read_only)
2124         return NULL;
2125     if (bdrv_check_request(bs, sector_num, nb_sectors))
2126         return NULL;
2127
2128     if (bs->dirty_bitmap) {
2129         blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb,
2130                                          opaque);
2131         cb = &block_complete_cb;
2132         opaque = blk_cb_data;
2133     }
2134
2135     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2136                                cb, opaque);
2137
2138     if (ret) {
2139         /* Update stats even though technically transfer has not happened. */
2140         bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2141         bs->wr_ops ++;
2142         if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2143             bs->wr_highest_sector = sector_num + nb_sectors - 1;
2144         }
2145     }
2146
2147     return ret;
2148 }
2149
2150
2151 typedef struct MultiwriteCB {
2152     int error;
2153     int num_requests;
2154     int num_callbacks;
2155     struct {
2156         BlockDriverCompletionFunc *cb;
2157         void *opaque;
2158         QEMUIOVector *free_qiov;
2159         void *free_buf;
2160     } callbacks[];
2161 } MultiwriteCB;
2162
2163 static void multiwrite_user_cb(MultiwriteCB *mcb)
2164 {
2165     int i;
2166
2167     for (i = 0; i < mcb->num_callbacks; i++) {
2168         mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
2169         if (mcb->callbacks[i].free_qiov) {
2170             qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2171         }
2172         qemu_free(mcb->callbacks[i].free_qiov);
2173         qemu_vfree(mcb->callbacks[i].free_buf);
2174     }
2175 }
2176
2177 static void multiwrite_cb(void *opaque, int ret)
2178 {
2179     MultiwriteCB *mcb = opaque;
2180
2181     trace_multiwrite_cb(mcb, ret);
2182
2183     if (ret < 0 && !mcb->error) {
2184         mcb->error = ret;
2185     }
2186
2187     mcb->num_requests--;
2188     if (mcb->num_requests == 0) {
2189         multiwrite_user_cb(mcb);
2190         qemu_free(mcb);
2191     }
2192 }
2193
2194 static int multiwrite_req_compare(const void *a, const void *b)
2195 {
2196     const BlockRequest *req1 = a, *req2 = b;
2197
2198     /*
2199      * Note that we can't simply subtract req2->sector from req1->sector
2200      * here as that could overflow the return value.
2201      */
2202     if (req1->sector > req2->sector) {
2203         return 1;
2204     } else if (req1->sector < req2->sector) {
2205         return -1;
2206     } else {
2207         return 0;
2208     }
2209 }
2210
2211 /*
2212  * Takes a bunch of requests and tries to merge them. Returns the number of
2213  * requests that remain after merging.
2214  */
2215 static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2216     int num_reqs, MultiwriteCB *mcb)
2217 {
2218     int i, outidx;
2219
2220     // Sort requests by start sector
2221     qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2222
2223     // Check if adjacent requests touch the same clusters. If so, combine them,
2224     // filling up gaps with zero sectors.
2225     outidx = 0;
2226     for (i = 1; i < num_reqs; i++) {
2227         int merge = 0;
2228         int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2229
2230         // This handles the cases that are valid for all block drivers, namely
2231         // exactly sequential writes and overlapping writes.
2232         if (reqs[i].sector <= oldreq_last) {
2233             merge = 1;
2234         }
2235
2236         // The block driver may decide that it makes sense to combine requests
2237         // even if there is a gap of some sectors between them. In this case,
2238         // the gap is filled with zeros (therefore only applicable for yet
2239         // unused space in format like qcow2).
2240         if (!merge && bs->drv->bdrv_merge_requests) {
2241             merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
2242         }
2243
2244         if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2245             merge = 0;
2246         }
2247
2248         if (merge) {
2249             size_t size;
2250             QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2251             qemu_iovec_init(qiov,
2252                 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2253
2254             // Add the first request to the merged one. If the requests are
2255             // overlapping, drop the last sectors of the first request.
2256             size = (reqs[i].sector - reqs[outidx].sector) << 9;
2257             qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2258
2259             // We might need to add some zeros between the two requests
2260             if (reqs[i].sector > oldreq_last) {
2261                 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2262                 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2263                 memset(buf, 0, zero_bytes);
2264                 qemu_iovec_add(qiov, buf, zero_bytes);
2265                 mcb->callbacks[i].free_buf = buf;
2266             }
2267
2268             // Add the second request
2269             qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2270
2271             reqs[outidx].nb_sectors = qiov->size >> 9;
2272             reqs[outidx].qiov = qiov;
2273
2274             mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2275         } else {
2276             outidx++;
2277             reqs[outidx].sector     = reqs[i].sector;
2278             reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2279             reqs[outidx].qiov       = reqs[i].qiov;
2280         }
2281     }
2282
2283     return outidx + 1;
2284 }
2285
2286 /*
2287  * Submit multiple AIO write requests at once.
2288  *
2289  * On success, the function returns 0 and all requests in the reqs array have
2290  * been submitted. In error case this function returns -1, and any of the
2291  * requests may or may not be submitted yet. In particular, this means that the
2292  * callback will be called for some of the requests, for others it won't. The
2293  * caller must check the error field of the BlockRequest to wait for the right
2294  * callbacks (if error != 0, no callback will be called).
2295  *
2296  * The implementation may modify the contents of the reqs array, e.g. to merge
2297  * requests. However, the fields opaque and error are left unmodified as they
2298  * are used to signal failure for a single request to the caller.
2299  */
2300 int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2301 {
2302     BlockDriverAIOCB *acb;
2303     MultiwriteCB *mcb;
2304     int i;
2305
2306     /* don't submit writes if we don't have a medium */
2307     if (bs->drv == NULL) {
2308         for (i = 0; i < num_reqs; i++) {
2309             reqs[i].error = -ENOMEDIUM;
2310         }
2311         return -1;
2312     }
2313
2314     if (num_reqs == 0) {
2315         return 0;
2316     }
2317
2318     // Create MultiwriteCB structure
2319     mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2320     mcb->num_requests = 0;
2321     mcb->num_callbacks = num_reqs;
2322
2323     for (i = 0; i < num_reqs; i++) {
2324         mcb->callbacks[i].cb = reqs[i].cb;
2325         mcb->callbacks[i].opaque = reqs[i].opaque;
2326     }
2327
2328     // Check for mergable requests
2329     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2330
2331     trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
2332
2333     /*
2334      * Run the aio requests. As soon as one request can't be submitted
2335      * successfully, fail all requests that are not yet submitted (we must
2336      * return failure for all requests anyway)
2337      *
2338      * num_requests cannot be set to the right value immediately: If
2339      * bdrv_aio_writev fails for some request, num_requests would be too high
2340      * and therefore multiwrite_cb() would never recognize the multiwrite
2341      * request as completed. We also cannot use the loop variable i to set it
2342      * when the first request fails because the callback may already have been
2343      * called for previously submitted requests. Thus, num_requests must be
2344      * incremented for each request that is submitted.
2345      *
2346      * The problem that callbacks may be called early also means that we need
2347      * to take care that num_requests doesn't become 0 before all requests are
2348      * submitted - multiwrite_cb() would consider the multiwrite request
2349      * completed. A dummy request that is "completed" by a manual call to
2350      * multiwrite_cb() takes care of this.
2351      */
2352     mcb->num_requests = 1;
2353
2354     // Run the aio requests
2355     for (i = 0; i < num_reqs; i++) {
2356         mcb->num_requests++;
2357         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2358             reqs[i].nb_sectors, multiwrite_cb, mcb);
2359
2360         if (acb == NULL) {
2361             // We can only fail the whole thing if no request has been
2362             // submitted yet. Otherwise we'll wait for the submitted AIOs to
2363             // complete and report the error in the callback.
2364             if (i == 0) {
2365                 trace_bdrv_aio_multiwrite_earlyfail(mcb);
2366                 goto fail;
2367             } else {
2368                 trace_bdrv_aio_multiwrite_latefail(mcb, i);
2369                 multiwrite_cb(mcb, -EIO);
2370                 break;
2371             }
2372         }
2373     }
2374
2375     /* Complete the dummy request */
2376     multiwrite_cb(mcb, 0);
2377
2378     return 0;
2379
2380 fail:
2381     for (i = 0; i < mcb->num_callbacks; i++) {
2382         reqs[i].error = -EIO;
2383     }
2384     qemu_free(mcb);
2385     return -1;
2386 }
2387
2388 BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2389         BlockDriverCompletionFunc *cb, void *opaque)
2390 {
2391     BlockDriver *drv = bs->drv;
2392
2393     if (bs->open_flags & BDRV_O_NO_FLUSH) {
2394         return bdrv_aio_noop_em(bs, cb, opaque);
2395     }
2396
2397     if (!drv)
2398         return NULL;
2399     return drv->bdrv_aio_flush(bs, cb, opaque);
2400 }
2401
2402 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
2403 {
2404     acb->pool->cancel(acb);
2405 }
2406
2407
2408 /**************************************************************/
2409 /* async block device emulation */
2410
2411 typedef struct BlockDriverAIOCBSync {
2412     BlockDriverAIOCB common;
2413     QEMUBH *bh;
2414     int ret;
2415     /* vector translation state */
2416     QEMUIOVector *qiov;
2417     uint8_t *bounce;
2418     int is_write;
2419 } BlockDriverAIOCBSync;
2420
2421 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2422 {
2423     BlockDriverAIOCBSync *acb =
2424         container_of(blockacb, BlockDriverAIOCBSync, common);
2425     qemu_bh_delete(acb->bh);
2426     acb->bh = NULL;
2427     qemu_aio_release(acb);
2428 }
2429
2430 static AIOPool bdrv_em_aio_pool = {
2431     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2432     .cancel             = bdrv_aio_cancel_em,
2433 };
2434
2435 static void bdrv_aio_bh_cb(void *opaque)
2436 {
2437     BlockDriverAIOCBSync *acb = opaque;
2438
2439     if (!acb->is_write)
2440         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2441     qemu_vfree(acb->bounce);
2442     acb->common.cb(acb->common.opaque, acb->ret);
2443     qemu_bh_delete(acb->bh);
2444     acb->bh = NULL;
2445     qemu_aio_release(acb);
2446 }
2447
2448 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2449                                             int64_t sector_num,
2450                                             QEMUIOVector *qiov,
2451                                             int nb_sectors,
2452                                             BlockDriverCompletionFunc *cb,
2453                                             void *opaque,
2454                                             int is_write)
2455
2456 {
2457     BlockDriverAIOCBSync *acb;
2458
2459     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2460     acb->is_write = is_write;
2461     acb->qiov = qiov;
2462     acb->bounce = qemu_blockalign(bs, qiov->size);
2463
2464     if (!acb->bh)
2465         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2466
2467     if (is_write) {
2468         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2469         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2470     } else {
2471         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2472     }
2473
2474     qemu_bh_schedule(acb->bh);
2475
2476     return &acb->common;
2477 }
2478
2479 static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2480         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2481         BlockDriverCompletionFunc *cb, void *opaque)
2482 {
2483     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2484 }
2485
2486 static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2487         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2488         BlockDriverCompletionFunc *cb, void *opaque)
2489 {
2490     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2491 }
2492
2493 static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2494         BlockDriverCompletionFunc *cb, void *opaque)
2495 {
2496     BlockDriverAIOCBSync *acb;
2497
2498     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2499     acb->is_write = 1; /* don't bounce in the completion hadler */
2500     acb->qiov = NULL;
2501     acb->bounce = NULL;
2502     acb->ret = 0;
2503
2504     if (!acb->bh)
2505         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2506
2507     bdrv_flush(bs);
2508     qemu_bh_schedule(acb->bh);
2509     return &acb->common;
2510 }
2511
2512 static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2513         BlockDriverCompletionFunc *cb, void *opaque)
2514 {
2515     BlockDriverAIOCBSync *acb;
2516
2517     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2518     acb->is_write = 1; /* don't bounce in the completion handler */
2519     acb->qiov = NULL;
2520     acb->bounce = NULL;
2521     acb->ret = 0;
2522
2523     if (!acb->bh) {
2524         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2525     }
2526
2527     qemu_bh_schedule(acb->bh);
2528     return &acb->common;
2529 }
2530
2531 /**************************************************************/
2532 /* sync block device emulation */
2533
2534 static void bdrv_rw_em_cb(void *opaque, int ret)
2535 {
2536     *(int *)opaque = ret;
2537 }
2538
2539 #define NOT_DONE 0x7fffffff
2540
2541 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
2542                         uint8_t *buf, int nb_sectors)
2543 {
2544     int async_ret;
2545     BlockDriverAIOCB *acb;
2546     struct iovec iov;
2547     QEMUIOVector qiov;
2548
2549     async_context_push();
2550
2551     async_ret = NOT_DONE;
2552     iov.iov_base = (void *)buf;
2553     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2554     qemu_iovec_init_external(&qiov, &iov, 1);
2555     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2556         bdrv_rw_em_cb, &async_ret);
2557     if (acb == NULL) {
2558         async_ret = -1;
2559         goto fail;
2560     }
2561
2562     while (async_ret == NOT_DONE) {
2563         qemu_aio_wait();
2564     }
2565
2566
2567 fail:
2568     async_context_pop();
2569     return async_ret;
2570 }
2571
2572 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2573                          const uint8_t *buf, int nb_sectors)
2574 {
2575     int async_ret;
2576     BlockDriverAIOCB *acb;
2577     struct iovec iov;
2578     QEMUIOVector qiov;
2579
2580     async_context_push();
2581
2582     async_ret = NOT_DONE;
2583     iov.iov_base = (void *)buf;
2584     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2585     qemu_iovec_init_external(&qiov, &iov, 1);
2586     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2587         bdrv_rw_em_cb, &async_ret);
2588     if (acb == NULL) {
2589         async_ret = -1;
2590         goto fail;
2591     }
2592     while (async_ret == NOT_DONE) {
2593         qemu_aio_wait();
2594     }
2595
2596 fail:
2597     async_context_pop();
2598     return async_ret;
2599 }
2600
2601 void bdrv_init(void)
2602 {
2603     module_call_init(MODULE_INIT_BLOCK);
2604 }
2605
2606 void bdrv_init_with_whitelist(void)
2607 {
2608     use_bdrv_whitelist = 1;
2609     bdrv_init();
2610 }
2611
2612 void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2613                    BlockDriverCompletionFunc *cb, void *opaque)
2614 {
2615     BlockDriverAIOCB *acb;
2616
2617     if (pool->free_aiocb) {
2618         acb = pool->free_aiocb;
2619         pool->free_aiocb = acb->next;
2620     } else {
2621         acb = qemu_mallocz(pool->aiocb_size);
2622         acb->pool = pool;
2623     }
2624     acb->bs = bs;
2625     acb->cb = cb;
2626     acb->opaque = opaque;
2627     return acb;
2628 }
2629
2630 void qemu_aio_release(void *p)
2631 {
2632     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2633     AIOPool *pool = acb->pool;
2634     acb->next = pool->free_aiocb;
2635     pool->free_aiocb = acb;
2636 }
2637
2638 /**************************************************************/
2639 /* removable device support */
2640
2641 /**
2642  * Return TRUE if the media is present
2643  */
2644 int bdrv_is_inserted(BlockDriverState *bs)
2645 {
2646     BlockDriver *drv = bs->drv;
2647     int ret;
2648     if (!drv)
2649         return 0;
2650     if (!drv->bdrv_is_inserted)
2651         return !bs->tray_open;
2652     ret = drv->bdrv_is_inserted(bs);
2653     return ret;
2654 }
2655
2656 /**
2657  * Return TRUE if the media changed since the last call to this
2658  * function. It is currently only used for floppy disks
2659  */
2660 int bdrv_media_changed(BlockDriverState *bs)
2661 {
2662     BlockDriver *drv = bs->drv;
2663     int ret;
2664
2665     if (!drv || !drv->bdrv_media_changed)
2666         ret = -ENOTSUP;
2667     else
2668         ret = drv->bdrv_media_changed(bs);
2669     if (ret == -ENOTSUP)
2670         ret = bs->media_changed;
2671     bs->media_changed = 0;
2672     return ret;
2673 }
2674
2675 /**
2676  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2677  */
2678 int bdrv_eject(BlockDriverState *bs, int eject_flag)
2679 {
2680     BlockDriver *drv = bs->drv;
2681     int ret;
2682
2683     if (bs->locked) {
2684         return -EBUSY;
2685     }
2686
2687     if (!drv || !drv->bdrv_eject) {
2688         ret = -ENOTSUP;
2689     } else {
2690         ret = drv->bdrv_eject(bs, eject_flag);
2691     }
2692     if (ret == -ENOTSUP) {
2693         ret = 0;
2694     }
2695     if (ret >= 0) {
2696         bs->tray_open = eject_flag;
2697     }
2698
2699     return ret;
2700 }
2701
2702 int bdrv_is_locked(BlockDriverState *bs)
2703 {
2704     return bs->locked;
2705 }
2706
2707 /**
2708  * Lock or unlock the media (if it is locked, the user won't be able
2709  * to eject it manually).
2710  */
2711 void bdrv_set_locked(BlockDriverState *bs, int locked)
2712 {
2713     BlockDriver *drv = bs->drv;
2714
2715     bs->locked = locked;
2716     if (drv && drv->bdrv_set_locked) {
2717         drv->bdrv_set_locked(bs, locked);
2718     }
2719 }
2720
2721 /* needed for generic scsi interface */
2722
2723 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2724 {
2725     BlockDriver *drv = bs->drv;
2726
2727     if (drv && drv->bdrv_ioctl)
2728         return drv->bdrv_ioctl(bs, req, buf);
2729     return -ENOTSUP;
2730 }
2731
2732 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2733         unsigned long int req, void *buf,
2734         BlockDriverCompletionFunc *cb, void *opaque)
2735 {
2736     BlockDriver *drv = bs->drv;
2737
2738     if (drv && drv->bdrv_aio_ioctl)
2739         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2740     return NULL;
2741 }
2742
2743
2744
2745 void *qemu_blockalign(BlockDriverState *bs, size_t size)
2746 {
2747     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2748 }
2749
2750 void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2751 {
2752     int64_t bitmap_size;
2753
2754     bs->dirty_count = 0;
2755     if (enable) {
2756         if (!bs->dirty_bitmap) {
2757             bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2758                     BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2759             bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
2760
2761             bs->dirty_bitmap = qemu_mallocz(bitmap_size);
2762         }
2763     } else {
2764         if (bs->dirty_bitmap) {
2765             qemu_free(bs->dirty_bitmap);
2766             bs->dirty_bitmap = NULL;
2767         }
2768     }
2769 }
2770
2771 int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2772 {
2773     int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
2774
2775     if (bs->dirty_bitmap &&
2776         (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2777         return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2778             (1UL << (chunk % (sizeof(unsigned long) * 8))));
2779     } else {
2780         return 0;
2781     }
2782 }
2783
2784 void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2785                       int nr_sectors)
2786 {
2787     set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2788 }
2789
2790 int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2791 {
2792     return bs->dirty_count;
2793 }
2794
2795 void bdrv_set_in_use(BlockDriverState *bs, int in_use)
2796 {
2797     assert(bs->in_use != in_use);
2798     bs->in_use = in_use;
2799 }
2800
2801 int bdrv_in_use(BlockDriverState *bs)
2802 {
2803     return bs->in_use;
2804 }
2805
2806 int bdrv_img_create(const char *filename, const char *fmt,
2807                     const char *base_filename, const char *base_fmt,
2808                     char *options, uint64_t img_size, int flags)
2809 {
2810     QEMUOptionParameter *param = NULL, *create_options = NULL;
2811     QEMUOptionParameter *backing_fmt, *backing_file;
2812     BlockDriverState *bs = NULL;
2813     BlockDriver *drv, *proto_drv;
2814     BlockDriver *backing_drv = NULL;
2815     int ret = 0;
2816
2817     /* Find driver and parse its options */
2818     drv = bdrv_find_format(fmt);
2819     if (!drv) {
2820         error_report("Unknown file format '%s'", fmt);
2821         ret = -EINVAL;
2822         goto out;
2823     }
2824
2825     proto_drv = bdrv_find_protocol(filename);
2826     if (!proto_drv) {
2827         error_report("Unknown protocol '%s'", filename);
2828         ret = -EINVAL;
2829         goto out;
2830     }
2831
2832     create_options = append_option_parameters(create_options,
2833                                               drv->create_options);
2834     create_options = append_option_parameters(create_options,
2835                                               proto_drv->create_options);
2836
2837     /* Create parameter list with default values */
2838     param = parse_option_parameters("", create_options, param);
2839
2840     set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
2841
2842     /* Parse -o options */
2843     if (options) {
2844         param = parse_option_parameters(options, create_options, param);
2845         if (param == NULL) {
2846             error_report("Invalid options for file format '%s'.", fmt);
2847             ret = -EINVAL;
2848             goto out;
2849         }
2850     }
2851
2852     if (base_filename) {
2853         if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
2854                                  base_filename)) {
2855             error_report("Backing file not supported for file format '%s'",
2856                          fmt);
2857             ret = -EINVAL;
2858             goto out;
2859         }
2860     }
2861
2862     if (base_fmt) {
2863         if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
2864             error_report("Backing file format not supported for file "
2865                          "format '%s'", fmt);
2866             ret = -EINVAL;
2867             goto out;
2868         }
2869     }
2870
2871     backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
2872     if (backing_file && backing_file->value.s) {
2873         if (!strcmp(filename, backing_file->value.s)) {
2874             error_report("Error: Trying to create an image with the "
2875                          "same filename as the backing file");
2876             ret = -EINVAL;
2877             goto out;
2878         }
2879     }
2880
2881     backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
2882     if (backing_fmt && backing_fmt->value.s) {
2883         backing_drv = bdrv_find_format(backing_fmt->value.s);
2884         if (!backing_drv) {
2885             error_report("Unknown backing file format '%s'",
2886                          backing_fmt->value.s);
2887             ret = -EINVAL;
2888             goto out;
2889         }
2890     }
2891
2892     // The size for the image must always be specified, with one exception:
2893     // If we are using a backing file, we can obtain the size from there
2894     if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
2895         if (backing_file && backing_file->value.s) {
2896             uint64_t size;
2897             char buf[32];
2898
2899             bs = bdrv_new("");
2900
2901             ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
2902             if (ret < 0) {
2903                 error_report("Could not open '%s'", backing_file->value.s);
2904                 goto out;
2905             }
2906             bdrv_get_geometry(bs, &size);
2907             size *= 512;
2908
2909             snprintf(buf, sizeof(buf), "%" PRId64, size);
2910             set_option_parameter(param, BLOCK_OPT_SIZE, buf);
2911         } else {
2912             error_report("Image creation needs a size parameter");
2913             ret = -EINVAL;
2914             goto out;
2915         }
2916     }
2917
2918     printf("Formatting '%s', fmt=%s ", filename, fmt);
2919     print_option_parameters(param);
2920     puts("");
2921
2922     ret = bdrv_create(drv, filename, param);
2923
2924     if (ret < 0) {
2925         if (ret == -ENOTSUP) {
2926             error_report("Formatting or formatting option not supported for "
2927                          "file format '%s'", fmt);
2928         } else if (ret == -EFBIG) {
2929             error_report("The image size is too large for file format '%s'",
2930                          fmt);
2931         } else {
2932             error_report("%s: error while creating %s: %s", filename, fmt,
2933                          strerror(-ret));
2934         }
2935     }
2936
2937 out:
2938     free_option_parameters(create_options);
2939     free_option_parameters(param);
2940
2941     if (bs) {
2942         bdrv_delete(bs);
2943     }
2944
2945     return ret;
2946 }