remove bdrv_aio_read/bdrv_aio_write (Christoph Hellwig)
[qemu] / 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 #ifdef HOST_BSD
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
28 #endif
29
30 #include "qemu-common.h"
31 #include "monitor.h"
32 #include "block_int.h"
33
34 #ifdef HOST_BSD
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #ifndef __DragonFly__
39 #include <sys/disk.h>
40 #endif
41 #endif
42
43 #ifdef _WIN32
44 #include <windows.h>
45 #endif
46
47 #define SECTOR_BITS 9
48 #define SECTOR_SIZE (1 << SECTOR_BITS)
49
50 static AIOPool vectored_aio_pool;
51
52 typedef struct BlockDriverAIOCBSync {
53     BlockDriverAIOCB common;
54     QEMUBH *bh;
55     int ret;
56 } BlockDriverAIOCBSync;
57
58 static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs,
59         int64_t sector_num, uint8_t *buf, int nb_sectors,
60         BlockDriverCompletionFunc *cb, void *opaque);
61 static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs,
62         int64_t sector_num, const uint8_t *buf, int nb_sectors,
63         BlockDriverCompletionFunc *cb, void *opaque);
64 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
65         int64_t sector_num, uint8_t *buf, int nb_sectors,
66         BlockDriverCompletionFunc *cb, void *opaque);
67 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
68         int64_t sector_num, const uint8_t *buf, int nb_sectors,
69         BlockDriverCompletionFunc *cb, void *opaque);
70 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
71 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
72                         uint8_t *buf, int nb_sectors);
73 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
74                          const uint8_t *buf, int nb_sectors);
75
76 BlockDriverState *bdrv_first;
77
78 static BlockDriver *first_drv;
79
80 int path_is_absolute(const char *path)
81 {
82     const char *p;
83 #ifdef _WIN32
84     /* specific case for names like: "\\.\d:" */
85     if (*path == '/' || *path == '\\')
86         return 1;
87 #endif
88     p = strchr(path, ':');
89     if (p)
90         p++;
91     else
92         p = path;
93 #ifdef _WIN32
94     return (*p == '/' || *p == '\\');
95 #else
96     return (*p == '/');
97 #endif
98 }
99
100 /* if filename is absolute, just copy it to dest. Otherwise, build a
101    path to it by considering it is relative to base_path. URL are
102    supported. */
103 void path_combine(char *dest, int dest_size,
104                   const char *base_path,
105                   const char *filename)
106 {
107     const char *p, *p1;
108     int len;
109
110     if (dest_size <= 0)
111         return;
112     if (path_is_absolute(filename)) {
113         pstrcpy(dest, dest_size, filename);
114     } else {
115         p = strchr(base_path, ':');
116         if (p)
117             p++;
118         else
119             p = base_path;
120         p1 = strrchr(base_path, '/');
121 #ifdef _WIN32
122         {
123             const char *p2;
124             p2 = strrchr(base_path, '\\');
125             if (!p1 || p2 > p1)
126                 p1 = p2;
127         }
128 #endif
129         if (p1)
130             p1++;
131         else
132             p1 = base_path;
133         if (p1 > p)
134             p = p1;
135         len = p - base_path;
136         if (len > dest_size - 1)
137             len = dest_size - 1;
138         memcpy(dest, base_path, len);
139         dest[len] = '\0';
140         pstrcat(dest, dest_size, filename);
141     }
142 }
143
144
145 static void bdrv_register(BlockDriver *bdrv)
146 {
147     if (!bdrv->bdrv_aio_read) {
148         /* add AIO emulation layer */
149         bdrv->bdrv_aio_read = bdrv_aio_read_em;
150         bdrv->bdrv_aio_write = bdrv_aio_write_em;
151         bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
152         bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
153     } else if (!bdrv->bdrv_read) {
154         /* add synchronous IO emulation layer */
155         bdrv->bdrv_read = bdrv_read_em;
156         bdrv->bdrv_write = bdrv_write_em;
157     }
158     aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
159     bdrv->next = first_drv;
160     first_drv = bdrv;
161 }
162
163 /* create a new block device (by default it is empty) */
164 BlockDriverState *bdrv_new(const char *device_name)
165 {
166     BlockDriverState **pbs, *bs;
167
168     bs = qemu_mallocz(sizeof(BlockDriverState));
169     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
170     if (device_name[0] != '\0') {
171         /* insert at the end */
172         pbs = &bdrv_first;
173         while (*pbs != NULL)
174             pbs = &(*pbs)->next;
175         *pbs = bs;
176     }
177     return bs;
178 }
179
180 BlockDriver *bdrv_find_format(const char *format_name)
181 {
182     BlockDriver *drv1;
183     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
184         if (!strcmp(drv1->format_name, format_name))
185             return drv1;
186     }
187     return NULL;
188 }
189
190 int bdrv_create2(BlockDriver *drv,
191                 const char *filename, int64_t size_in_sectors,
192                 const char *backing_file, const char *backing_format,
193                 int flags)
194 {
195     if (drv->bdrv_create2)
196         return drv->bdrv_create2(filename, size_in_sectors, backing_file,
197                                  backing_format, flags);
198     if (drv->bdrv_create)
199         return drv->bdrv_create(filename, size_in_sectors, backing_file,
200                                 flags);
201     return -ENOTSUP;
202 }
203
204 int bdrv_create(BlockDriver *drv,
205                 const char *filename, int64_t size_in_sectors,
206                 const char *backing_file, int flags)
207 {
208     if (!drv->bdrv_create)
209         return -ENOTSUP;
210     return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
211 }
212
213 #ifdef _WIN32
214 void get_tmp_filename(char *filename, int size)
215 {
216     char temp_dir[MAX_PATH];
217
218     GetTempPath(MAX_PATH, temp_dir);
219     GetTempFileName(temp_dir, "qem", 0, filename);
220 }
221 #else
222 void get_tmp_filename(char *filename, int size)
223 {
224     int fd;
225     const char *tmpdir;
226     /* XXX: race condition possible */
227     tmpdir = getenv("TMPDIR");
228     if (!tmpdir)
229         tmpdir = "/tmp";
230     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
231     fd = mkstemp(filename);
232     close(fd);
233 }
234 #endif
235
236 #ifdef _WIN32
237 static int is_windows_drive_prefix(const char *filename)
238 {
239     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
240              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
241             filename[1] == ':');
242 }
243
244 static int is_windows_drive(const char *filename)
245 {
246     if (is_windows_drive_prefix(filename) &&
247         filename[2] == '\0')
248         return 1;
249     if (strstart(filename, "\\\\.\\", NULL) ||
250         strstart(filename, "//./", NULL))
251         return 1;
252     return 0;
253 }
254 #endif
255
256 static BlockDriver *find_protocol(const char *filename)
257 {
258     BlockDriver *drv1;
259     char protocol[128];
260     int len;
261     const char *p;
262
263 #ifdef _WIN32
264     if (is_windows_drive(filename) ||
265         is_windows_drive_prefix(filename))
266         return &bdrv_raw;
267 #endif
268     p = strchr(filename, ':');
269     if (!p)
270         return &bdrv_raw;
271     len = p - filename;
272     if (len > sizeof(protocol) - 1)
273         len = sizeof(protocol) - 1;
274     memcpy(protocol, filename, len);
275     protocol[len] = '\0';
276     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
277         if (drv1->protocol_name &&
278             !strcmp(drv1->protocol_name, protocol))
279             return drv1;
280     }
281     return NULL;
282 }
283
284 /* XXX: force raw format if block or character device ? It would
285    simplify the BSD case */
286 static BlockDriver *find_image_format(const char *filename)
287 {
288     int ret, score, score_max;
289     BlockDriver *drv1, *drv;
290     uint8_t buf[2048];
291     BlockDriverState *bs;
292
293     /* detect host devices. By convention, /dev/cdrom[N] is always
294        recognized as a host CDROM */
295     if (strstart(filename, "/dev/cdrom", NULL))
296         return &bdrv_host_device;
297 #ifdef _WIN32
298     if (is_windows_drive(filename))
299         return &bdrv_host_device;
300 #else
301     {
302         struct stat st;
303         if (stat(filename, &st) >= 0 &&
304             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
305             return &bdrv_host_device;
306         }
307     }
308 #endif
309
310     drv = find_protocol(filename);
311     /* no need to test disk image formats for vvfat */
312     if (drv == &bdrv_vvfat)
313         return drv;
314
315     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
316     if (ret < 0)
317         return NULL;
318     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
319     bdrv_delete(bs);
320     if (ret < 0) {
321         return NULL;
322     }
323
324     score_max = 0;
325     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
326         if (drv1->bdrv_probe) {
327             score = drv1->bdrv_probe(buf, ret, filename);
328             if (score > score_max) {
329                 score_max = score;
330                 drv = drv1;
331             }
332         }
333     }
334     return drv;
335 }
336
337 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
338 {
339     BlockDriverState *bs;
340     int ret;
341
342     bs = bdrv_new("");
343     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
344     if (ret < 0) {
345         bdrv_delete(bs);
346         return ret;
347     }
348     bs->growable = 1;
349     *pbs = bs;
350     return 0;
351 }
352
353 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
354 {
355     return bdrv_open2(bs, filename, flags, NULL);
356 }
357
358 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
359                BlockDriver *drv)
360 {
361     int ret, open_flags;
362     char tmp_filename[PATH_MAX];
363     char backing_filename[PATH_MAX];
364
365     bs->read_only = 0;
366     bs->is_temporary = 0;
367     bs->encrypted = 0;
368     bs->valid_key = 0;
369
370     if (flags & BDRV_O_SNAPSHOT) {
371         BlockDriverState *bs1;
372         int64_t total_size;
373         int is_protocol = 0;
374
375         /* if snapshot, we create a temporary backing file and open it
376            instead of opening 'filename' directly */
377
378         /* if there is a backing file, use it */
379         bs1 = bdrv_new("");
380         ret = bdrv_open2(bs1, filename, 0, drv);
381         if (ret < 0) {
382             bdrv_delete(bs1);
383             return ret;
384         }
385         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
386
387         if (bs1->drv && bs1->drv->protocol_name)
388             is_protocol = 1;
389
390         bdrv_delete(bs1);
391
392         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
393
394         /* Real path is meaningless for protocols */
395         if (is_protocol)
396             snprintf(backing_filename, sizeof(backing_filename),
397                      "%s", filename);
398         else
399             realpath(filename, backing_filename);
400
401         ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
402                            total_size, backing_filename, 
403                            (drv ? drv->format_name : NULL), 0);
404         if (ret < 0) {
405             return ret;
406         }
407         filename = tmp_filename;
408         drv = &bdrv_qcow2;
409         bs->is_temporary = 1;
410     }
411
412     pstrcpy(bs->filename, sizeof(bs->filename), filename);
413     if (flags & BDRV_O_FILE) {
414         drv = find_protocol(filename);
415     } else if (!drv) {
416         drv = find_image_format(filename);
417     }
418     if (!drv) {
419         ret = -ENOENT;
420         goto unlink_and_fail;
421     }
422     bs->drv = drv;
423     bs->opaque = qemu_mallocz(drv->instance_size);
424     /* Note: for compatibility, we open disk image files as RDWR, and
425        RDONLY as fallback */
426     if (!(flags & BDRV_O_FILE))
427         open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
428     else
429         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
430     ret = drv->bdrv_open(bs, filename, open_flags);
431     if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
432         ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
433         bs->read_only = 1;
434     }
435     if (ret < 0) {
436         qemu_free(bs->opaque);
437         bs->opaque = NULL;
438         bs->drv = NULL;
439     unlink_and_fail:
440         if (bs->is_temporary)
441             unlink(filename);
442         return ret;
443     }
444     if (drv->bdrv_getlength) {
445         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
446     }
447 #ifndef _WIN32
448     if (bs->is_temporary) {
449         unlink(filename);
450     }
451 #endif
452     if (bs->backing_file[0] != '\0') {
453         /* if there is a backing file, use it */
454         BlockDriver *back_drv = NULL;
455         bs->backing_hd = bdrv_new("");
456         path_combine(backing_filename, sizeof(backing_filename),
457                      filename, bs->backing_file);
458         if (bs->backing_format[0] != '\0')
459             back_drv = bdrv_find_format(bs->backing_format);
460         ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
461                          back_drv);
462         if (ret < 0) {
463             bdrv_close(bs);
464             return ret;
465         }
466     }
467
468     if (!bdrv_key_required(bs)) {
469         /* call the change callback */
470         bs->media_changed = 1;
471         if (bs->change_cb)
472             bs->change_cb(bs->change_opaque);
473     }
474     return 0;
475 }
476
477 void bdrv_close(BlockDriverState *bs)
478 {
479     if (bs->drv) {
480         if (bs->backing_hd)
481             bdrv_delete(bs->backing_hd);
482         bs->drv->bdrv_close(bs);
483         qemu_free(bs->opaque);
484 #ifdef _WIN32
485         if (bs->is_temporary) {
486             unlink(bs->filename);
487         }
488 #endif
489         bs->opaque = NULL;
490         bs->drv = NULL;
491
492         /* call the change callback */
493         bs->media_changed = 1;
494         if (bs->change_cb)
495             bs->change_cb(bs->change_opaque);
496     }
497 }
498
499 void bdrv_delete(BlockDriverState *bs)
500 {
501     BlockDriverState **pbs;
502
503     pbs = &bdrv_first;
504     while (*pbs != bs && *pbs != NULL)
505         pbs = &(*pbs)->next;
506     if (*pbs == bs)
507         *pbs = bs->next;
508
509     bdrv_close(bs);
510     qemu_free(bs);
511 }
512
513 /* commit COW file into the raw image */
514 int bdrv_commit(BlockDriverState *bs)
515 {
516     BlockDriver *drv = bs->drv;
517     int64_t i, total_sectors;
518     int n, j;
519     unsigned char sector[512];
520
521     if (!drv)
522         return -ENOMEDIUM;
523
524     if (bs->read_only) {
525         return -EACCES;
526     }
527
528     if (!bs->backing_hd) {
529         return -ENOTSUP;
530     }
531
532     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
533     for (i = 0; i < total_sectors;) {
534         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
535             for(j = 0; j < n; j++) {
536                 if (bdrv_read(bs, i, sector, 1) != 0) {
537                     return -EIO;
538                 }
539
540                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
541                     return -EIO;
542                 }
543                 i++;
544             }
545         } else {
546             i += n;
547         }
548     }
549
550     if (drv->bdrv_make_empty)
551         return drv->bdrv_make_empty(bs);
552
553     return 0;
554 }
555
556 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
557                                    size_t size)
558 {
559     int64_t len;
560
561     if (!bdrv_is_inserted(bs))
562         return -ENOMEDIUM;
563
564     if (bs->growable)
565         return 0;
566
567     len = bdrv_getlength(bs);
568
569     if ((offset + size) > len)
570         return -EIO;
571
572     return 0;
573 }
574
575 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
576                               int nb_sectors)
577 {
578     return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
579 }
580
581 /* return < 0 if error. See bdrv_write() for the return codes */
582 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
583               uint8_t *buf, int nb_sectors)
584 {
585     BlockDriver *drv = bs->drv;
586
587     if (!drv)
588         return -ENOMEDIUM;
589     if (bdrv_check_request(bs, sector_num, nb_sectors))
590         return -EIO;
591
592     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
593 }
594
595 /* Return < 0 if error. Important errors are:
596   -EIO         generic I/O error (may happen for all errors)
597   -ENOMEDIUM   No media inserted.
598   -EINVAL      Invalid sector number or nb_sectors
599   -EACCES      Trying to write a read-only device
600 */
601 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
602                const uint8_t *buf, int nb_sectors)
603 {
604     BlockDriver *drv = bs->drv;
605     if (!bs->drv)
606         return -ENOMEDIUM;
607     if (bs->read_only)
608         return -EACCES;
609     if (bdrv_check_request(bs, sector_num, nb_sectors))
610         return -EIO;
611
612     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
613 }
614
615 int bdrv_pread(BlockDriverState *bs, int64_t offset,
616                void *buf, int count1)
617 {
618     uint8_t tmp_buf[SECTOR_SIZE];
619     int len, nb_sectors, count;
620     int64_t sector_num;
621
622     count = count1;
623     /* first read to align to sector start */
624     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
625     if (len > count)
626         len = count;
627     sector_num = offset >> SECTOR_BITS;
628     if (len > 0) {
629         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
630             return -EIO;
631         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
632         count -= len;
633         if (count == 0)
634             return count1;
635         sector_num++;
636         buf += len;
637     }
638
639     /* read the sectors "in place" */
640     nb_sectors = count >> SECTOR_BITS;
641     if (nb_sectors > 0) {
642         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
643             return -EIO;
644         sector_num += nb_sectors;
645         len = nb_sectors << SECTOR_BITS;
646         buf += len;
647         count -= len;
648     }
649
650     /* add data from the last sector */
651     if (count > 0) {
652         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
653             return -EIO;
654         memcpy(buf, tmp_buf, count);
655     }
656     return count1;
657 }
658
659 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
660                 const void *buf, int count1)
661 {
662     uint8_t tmp_buf[SECTOR_SIZE];
663     int len, nb_sectors, count;
664     int64_t sector_num;
665
666     count = count1;
667     /* first write to align to sector start */
668     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
669     if (len > count)
670         len = count;
671     sector_num = offset >> SECTOR_BITS;
672     if (len > 0) {
673         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
674             return -EIO;
675         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
676         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
677             return -EIO;
678         count -= len;
679         if (count == 0)
680             return count1;
681         sector_num++;
682         buf += len;
683     }
684
685     /* write the sectors "in place" */
686     nb_sectors = count >> SECTOR_BITS;
687     if (nb_sectors > 0) {
688         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
689             return -EIO;
690         sector_num += nb_sectors;
691         len = nb_sectors << SECTOR_BITS;
692         buf += len;
693         count -= len;
694     }
695
696     /* add data from the last sector */
697     if (count > 0) {
698         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
699             return -EIO;
700         memcpy(tmp_buf, buf, count);
701         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
702             return -EIO;
703     }
704     return count1;
705 }
706
707 /**
708  * Truncate file to 'offset' bytes (needed only for file protocols)
709  */
710 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
711 {
712     BlockDriver *drv = bs->drv;
713     if (!drv)
714         return -ENOMEDIUM;
715     if (!drv->bdrv_truncate)
716         return -ENOTSUP;
717     return drv->bdrv_truncate(bs, offset);
718 }
719
720 /**
721  * Length of a file in bytes. Return < 0 if error or unknown.
722  */
723 int64_t bdrv_getlength(BlockDriverState *bs)
724 {
725     BlockDriver *drv = bs->drv;
726     if (!drv)
727         return -ENOMEDIUM;
728     if (!drv->bdrv_getlength) {
729         /* legacy mode */
730         return bs->total_sectors * SECTOR_SIZE;
731     }
732     return drv->bdrv_getlength(bs);
733 }
734
735 /* return 0 as number of sectors if no device present or error */
736 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
737 {
738     int64_t length;
739     length = bdrv_getlength(bs);
740     if (length < 0)
741         length = 0;
742     else
743         length = length >> SECTOR_BITS;
744     *nb_sectors_ptr = length;
745 }
746
747 struct partition {
748         uint8_t boot_ind;           /* 0x80 - active */
749         uint8_t head;               /* starting head */
750         uint8_t sector;             /* starting sector */
751         uint8_t cyl;                /* starting cylinder */
752         uint8_t sys_ind;            /* What partition type */
753         uint8_t end_head;           /* end head */
754         uint8_t end_sector;         /* end sector */
755         uint8_t end_cyl;            /* end cylinder */
756         uint32_t start_sect;        /* starting sector counting from 0 */
757         uint32_t nr_sects;          /* nr of sectors in partition */
758 } __attribute__((packed));
759
760 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
761 static int guess_disk_lchs(BlockDriverState *bs,
762                            int *pcylinders, int *pheads, int *psectors)
763 {
764     uint8_t buf[512];
765     int ret, i, heads, sectors, cylinders;
766     struct partition *p;
767     uint32_t nr_sects;
768     uint64_t nb_sectors;
769
770     bdrv_get_geometry(bs, &nb_sectors);
771
772     ret = bdrv_read(bs, 0, buf, 1);
773     if (ret < 0)
774         return -1;
775     /* test msdos magic */
776     if (buf[510] != 0x55 || buf[511] != 0xaa)
777         return -1;
778     for(i = 0; i < 4; i++) {
779         p = ((struct partition *)(buf + 0x1be)) + i;
780         nr_sects = le32_to_cpu(p->nr_sects);
781         if (nr_sects && p->end_head) {
782             /* We make the assumption that the partition terminates on
783                a cylinder boundary */
784             heads = p->end_head + 1;
785             sectors = p->end_sector & 63;
786             if (sectors == 0)
787                 continue;
788             cylinders = nb_sectors / (heads * sectors);
789             if (cylinders < 1 || cylinders > 16383)
790                 continue;
791             *pheads = heads;
792             *psectors = sectors;
793             *pcylinders = cylinders;
794 #if 0
795             printf("guessed geometry: LCHS=%d %d %d\n",
796                    cylinders, heads, sectors);
797 #endif
798             return 0;
799         }
800     }
801     return -1;
802 }
803
804 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
805 {
806     int translation, lba_detected = 0;
807     int cylinders, heads, secs;
808     uint64_t nb_sectors;
809
810     /* if a geometry hint is available, use it */
811     bdrv_get_geometry(bs, &nb_sectors);
812     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
813     translation = bdrv_get_translation_hint(bs);
814     if (cylinders != 0) {
815         *pcyls = cylinders;
816         *pheads = heads;
817         *psecs = secs;
818     } else {
819         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
820             if (heads > 16) {
821                 /* if heads > 16, it means that a BIOS LBA
822                    translation was active, so the default
823                    hardware geometry is OK */
824                 lba_detected = 1;
825                 goto default_geometry;
826             } else {
827                 *pcyls = cylinders;
828                 *pheads = heads;
829                 *psecs = secs;
830                 /* disable any translation to be in sync with
831                    the logical geometry */
832                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
833                     bdrv_set_translation_hint(bs,
834                                               BIOS_ATA_TRANSLATION_NONE);
835                 }
836             }
837         } else {
838         default_geometry:
839             /* if no geometry, use a standard physical disk geometry */
840             cylinders = nb_sectors / (16 * 63);
841
842             if (cylinders > 16383)
843                 cylinders = 16383;
844             else if (cylinders < 2)
845                 cylinders = 2;
846             *pcyls = cylinders;
847             *pheads = 16;
848             *psecs = 63;
849             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
850                 if ((*pcyls * *pheads) <= 131072) {
851                     bdrv_set_translation_hint(bs,
852                                               BIOS_ATA_TRANSLATION_LARGE);
853                 } else {
854                     bdrv_set_translation_hint(bs,
855                                               BIOS_ATA_TRANSLATION_LBA);
856                 }
857             }
858         }
859         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
860     }
861 }
862
863 void bdrv_set_geometry_hint(BlockDriverState *bs,
864                             int cyls, int heads, int secs)
865 {
866     bs->cyls = cyls;
867     bs->heads = heads;
868     bs->secs = secs;
869 }
870
871 void bdrv_set_type_hint(BlockDriverState *bs, int type)
872 {
873     bs->type = type;
874     bs->removable = ((type == BDRV_TYPE_CDROM ||
875                       type == BDRV_TYPE_FLOPPY));
876 }
877
878 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
879 {
880     bs->translation = translation;
881 }
882
883 void bdrv_get_geometry_hint(BlockDriverState *bs,
884                             int *pcyls, int *pheads, int *psecs)
885 {
886     *pcyls = bs->cyls;
887     *pheads = bs->heads;
888     *psecs = bs->secs;
889 }
890
891 int bdrv_get_type_hint(BlockDriverState *bs)
892 {
893     return bs->type;
894 }
895
896 int bdrv_get_translation_hint(BlockDriverState *bs)
897 {
898     return bs->translation;
899 }
900
901 int bdrv_is_removable(BlockDriverState *bs)
902 {
903     return bs->removable;
904 }
905
906 int bdrv_is_read_only(BlockDriverState *bs)
907 {
908     return bs->read_only;
909 }
910
911 int bdrv_is_sg(BlockDriverState *bs)
912 {
913     return bs->sg;
914 }
915
916 /* XXX: no longer used */
917 void bdrv_set_change_cb(BlockDriverState *bs,
918                         void (*change_cb)(void *opaque), void *opaque)
919 {
920     bs->change_cb = change_cb;
921     bs->change_opaque = opaque;
922 }
923
924 int bdrv_is_encrypted(BlockDriverState *bs)
925 {
926     if (bs->backing_hd && bs->backing_hd->encrypted)
927         return 1;
928     return bs->encrypted;
929 }
930
931 int bdrv_key_required(BlockDriverState *bs)
932 {
933     BlockDriverState *backing_hd = bs->backing_hd;
934
935     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
936         return 1;
937     return (bs->encrypted && !bs->valid_key);
938 }
939
940 int bdrv_set_key(BlockDriverState *bs, const char *key)
941 {
942     int ret;
943     if (bs->backing_hd && bs->backing_hd->encrypted) {
944         ret = bdrv_set_key(bs->backing_hd, key);
945         if (ret < 0)
946             return ret;
947         if (!bs->encrypted)
948             return 0;
949     }
950     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
951         return -1;
952     ret = bs->drv->bdrv_set_key(bs, key);
953     if (ret < 0) {
954         bs->valid_key = 0;
955     } else if (!bs->valid_key) {
956         bs->valid_key = 1;
957         /* call the change callback now, we skipped it on open */
958         bs->media_changed = 1;
959         if (bs->change_cb)
960             bs->change_cb(bs->change_opaque);
961     }
962     return ret;
963 }
964
965 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
966 {
967     if (!bs->drv) {
968         buf[0] = '\0';
969     } else {
970         pstrcpy(buf, buf_size, bs->drv->format_name);
971     }
972 }
973
974 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
975                          void *opaque)
976 {
977     BlockDriver *drv;
978
979     for (drv = first_drv; drv != NULL; drv = drv->next) {
980         it(opaque, drv->format_name);
981     }
982 }
983
984 BlockDriverState *bdrv_find(const char *name)
985 {
986     BlockDriverState *bs;
987
988     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
989         if (!strcmp(name, bs->device_name))
990             return bs;
991     }
992     return NULL;
993 }
994
995 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
996 {
997     BlockDriverState *bs;
998
999     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1000         it(opaque, bs);
1001     }
1002 }
1003
1004 const char *bdrv_get_device_name(BlockDriverState *bs)
1005 {
1006     return bs->device_name;
1007 }
1008
1009 void bdrv_flush(BlockDriverState *bs)
1010 {
1011     if (!bs->drv)
1012         return;
1013     if (bs->drv->bdrv_flush)
1014         bs->drv->bdrv_flush(bs);
1015     if (bs->backing_hd)
1016         bdrv_flush(bs->backing_hd);
1017 }
1018
1019 void bdrv_flush_all(void)
1020 {
1021     BlockDriverState *bs;
1022
1023     for (bs = bdrv_first; bs != NULL; bs = bs->next)
1024         if (bs->drv && !bdrv_is_read_only(bs) && 
1025             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1026             bdrv_flush(bs);
1027 }
1028
1029 /*
1030  * Returns true iff the specified sector is present in the disk image. Drivers
1031  * not implementing the functionality are assumed to not support backing files,
1032  * hence all their sectors are reported as allocated.
1033  *
1034  * 'pnum' is set to the number of sectors (including and immediately following
1035  * the specified sector) that are known to be in the same
1036  * allocated/unallocated state.
1037  *
1038  * 'nb_sectors' is the max value 'pnum' should be set to.
1039  */
1040 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1041         int *pnum)
1042 {
1043     int64_t n;
1044     if (!bs->drv->bdrv_is_allocated) {
1045         if (sector_num >= bs->total_sectors) {
1046             *pnum = 0;
1047             return 0;
1048         }
1049         n = bs->total_sectors - sector_num;
1050         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1051         return 1;
1052     }
1053     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1054 }
1055
1056 void bdrv_info(Monitor *mon)
1057 {
1058     BlockDriverState *bs;
1059
1060     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1061         monitor_printf(mon, "%s:", bs->device_name);
1062         monitor_printf(mon, " type=");
1063         switch(bs->type) {
1064         case BDRV_TYPE_HD:
1065             monitor_printf(mon, "hd");
1066             break;
1067         case BDRV_TYPE_CDROM:
1068             monitor_printf(mon, "cdrom");
1069             break;
1070         case BDRV_TYPE_FLOPPY:
1071             monitor_printf(mon, "floppy");
1072             break;
1073         }
1074         monitor_printf(mon, " removable=%d", bs->removable);
1075         if (bs->removable) {
1076             monitor_printf(mon, " locked=%d", bs->locked);
1077         }
1078         if (bs->drv) {
1079             monitor_printf(mon, " file=");
1080             monitor_print_filename(mon, bs->filename);
1081             if (bs->backing_file[0] != '\0') {
1082                 monitor_printf(mon, " backing_file=");
1083                 monitor_print_filename(mon, bs->backing_file);
1084             }
1085             monitor_printf(mon, " ro=%d", bs->read_only);
1086             monitor_printf(mon, " drv=%s", bs->drv->format_name);
1087             monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1088         } else {
1089             monitor_printf(mon, " [not inserted]");
1090         }
1091         monitor_printf(mon, "\n");
1092     }
1093 }
1094
1095 /* The "info blockstats" command. */
1096 void bdrv_info_stats(Monitor *mon)
1097 {
1098     BlockDriverState *bs;
1099
1100     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1101         monitor_printf(mon, "%s:"
1102                        " rd_bytes=%" PRIu64
1103                        " wr_bytes=%" PRIu64
1104                        " rd_operations=%" PRIu64
1105                        " wr_operations=%" PRIu64
1106                        "\n",
1107                        bs->device_name,
1108                        bs->rd_bytes, bs->wr_bytes,
1109                        bs->rd_ops, bs->wr_ops);
1110     }
1111 }
1112
1113 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1114 {
1115     if (bs->backing_hd && bs->backing_hd->encrypted)
1116         return bs->backing_file;
1117     else if (bs->encrypted)
1118         return bs->filename;
1119     else
1120         return NULL;
1121 }
1122
1123 void bdrv_get_backing_filename(BlockDriverState *bs,
1124                                char *filename, int filename_size)
1125 {
1126     if (!bs->backing_hd) {
1127         pstrcpy(filename, filename_size, "");
1128     } else {
1129         pstrcpy(filename, filename_size, bs->backing_file);
1130     }
1131 }
1132
1133 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1134                           const uint8_t *buf, int nb_sectors)
1135 {
1136     BlockDriver *drv = bs->drv;
1137     if (!drv)
1138         return -ENOMEDIUM;
1139     if (!drv->bdrv_write_compressed)
1140         return -ENOTSUP;
1141     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1142 }
1143
1144 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1145 {
1146     BlockDriver *drv = bs->drv;
1147     if (!drv)
1148         return -ENOMEDIUM;
1149     if (!drv->bdrv_get_info)
1150         return -ENOTSUP;
1151     memset(bdi, 0, sizeof(*bdi));
1152     return drv->bdrv_get_info(bs, bdi);
1153 }
1154
1155 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1156 {
1157     BlockDriver *drv = bs->drv;
1158     if (!drv)
1159         return -ENOMEDIUM;
1160     if (!drv->bdrv_put_buffer)
1161         return -ENOTSUP;
1162     return drv->bdrv_put_buffer(bs, buf, pos, size);
1163 }
1164
1165 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1166 {
1167     BlockDriver *drv = bs->drv;
1168     if (!drv)
1169         return -ENOMEDIUM;
1170     if (!drv->bdrv_get_buffer)
1171         return -ENOTSUP;
1172     return drv->bdrv_get_buffer(bs, buf, pos, size);
1173 }
1174
1175 /**************************************************************/
1176 /* handling of snapshots */
1177
1178 int bdrv_snapshot_create(BlockDriverState *bs,
1179                          QEMUSnapshotInfo *sn_info)
1180 {
1181     BlockDriver *drv = bs->drv;
1182     if (!drv)
1183         return -ENOMEDIUM;
1184     if (!drv->bdrv_snapshot_create)
1185         return -ENOTSUP;
1186     return drv->bdrv_snapshot_create(bs, sn_info);
1187 }
1188
1189 int bdrv_snapshot_goto(BlockDriverState *bs,
1190                        const char *snapshot_id)
1191 {
1192     BlockDriver *drv = bs->drv;
1193     if (!drv)
1194         return -ENOMEDIUM;
1195     if (!drv->bdrv_snapshot_goto)
1196         return -ENOTSUP;
1197     return drv->bdrv_snapshot_goto(bs, snapshot_id);
1198 }
1199
1200 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1201 {
1202     BlockDriver *drv = bs->drv;
1203     if (!drv)
1204         return -ENOMEDIUM;
1205     if (!drv->bdrv_snapshot_delete)
1206         return -ENOTSUP;
1207     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1208 }
1209
1210 int bdrv_snapshot_list(BlockDriverState *bs,
1211                        QEMUSnapshotInfo **psn_info)
1212 {
1213     BlockDriver *drv = bs->drv;
1214     if (!drv)
1215         return -ENOMEDIUM;
1216     if (!drv->bdrv_snapshot_list)
1217         return -ENOTSUP;
1218     return drv->bdrv_snapshot_list(bs, psn_info);
1219 }
1220
1221 #define NB_SUFFIXES 4
1222
1223 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1224 {
1225     static const char suffixes[NB_SUFFIXES] = "KMGT";
1226     int64_t base;
1227     int i;
1228
1229     if (size <= 999) {
1230         snprintf(buf, buf_size, "%" PRId64, size);
1231     } else {
1232         base = 1024;
1233         for(i = 0; i < NB_SUFFIXES; i++) {
1234             if (size < (10 * base)) {
1235                 snprintf(buf, buf_size, "%0.1f%c",
1236                          (double)size / base,
1237                          suffixes[i]);
1238                 break;
1239             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1240                 snprintf(buf, buf_size, "%" PRId64 "%c",
1241                          ((size + (base >> 1)) / base),
1242                          suffixes[i]);
1243                 break;
1244             }
1245             base = base * 1024;
1246         }
1247     }
1248     return buf;
1249 }
1250
1251 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1252 {
1253     char buf1[128], date_buf[128], clock_buf[128];
1254 #ifdef _WIN32
1255     struct tm *ptm;
1256 #else
1257     struct tm tm;
1258 #endif
1259     time_t ti;
1260     int64_t secs;
1261
1262     if (!sn) {
1263         snprintf(buf, buf_size,
1264                  "%-10s%-20s%7s%20s%15s",
1265                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1266     } else {
1267         ti = sn->date_sec;
1268 #ifdef _WIN32
1269         ptm = localtime(&ti);
1270         strftime(date_buf, sizeof(date_buf),
1271                  "%Y-%m-%d %H:%M:%S", ptm);
1272 #else
1273         localtime_r(&ti, &tm);
1274         strftime(date_buf, sizeof(date_buf),
1275                  "%Y-%m-%d %H:%M:%S", &tm);
1276 #endif
1277         secs = sn->vm_clock_nsec / 1000000000;
1278         snprintf(clock_buf, sizeof(clock_buf),
1279                  "%02d:%02d:%02d.%03d",
1280                  (int)(secs / 3600),
1281                  (int)((secs / 60) % 60),
1282                  (int)(secs % 60),
1283                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1284         snprintf(buf, buf_size,
1285                  "%-10s%-20s%7s%20s%15s",
1286                  sn->id_str, sn->name,
1287                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1288                  date_buf,
1289                  clock_buf);
1290     }
1291     return buf;
1292 }
1293
1294
1295 /**************************************************************/
1296 /* async I/Os */
1297
1298 typedef struct VectorTranslationAIOCB {
1299     BlockDriverAIOCB common;
1300     QEMUIOVector *iov;
1301     uint8_t *bounce;
1302     int is_write;
1303     BlockDriverAIOCB *aiocb;
1304 } VectorTranslationAIOCB;
1305
1306 static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
1307 {
1308     VectorTranslationAIOCB *acb
1309         = container_of(_acb, VectorTranslationAIOCB, common);
1310
1311     bdrv_aio_cancel(acb->aiocb);
1312 }
1313
1314 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1315 {
1316     VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
1317
1318     if (!s->is_write) {
1319         qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1320     }
1321     qemu_vfree(s->bounce);
1322     s->common.cb(s->common.opaque, ret);
1323     qemu_aio_release(s);
1324 }
1325
1326 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1327                                             int64_t sector_num,
1328                                             QEMUIOVector *iov,
1329                                             int nb_sectors,
1330                                             BlockDriverCompletionFunc *cb,
1331                                             void *opaque,
1332                                             int is_write)
1333
1334 {
1335     VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1336                                                   cb, opaque);
1337
1338     s->iov = iov;
1339     s->bounce = qemu_memalign(512, nb_sectors * 512);
1340     s->is_write = is_write;
1341     if (is_write) {
1342         qemu_iovec_to_buffer(s->iov, s->bounce);
1343         s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1344                                   bdrv_aio_rw_vector_cb, s);
1345     } else {
1346         s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1347                                  bdrv_aio_rw_vector_cb, s);
1348     }
1349     if (!s->aiocb) {
1350         qemu_vfree(s->bounce);
1351         qemu_aio_release(s);
1352         return NULL;
1353     }
1354     return &s->common;
1355 }
1356
1357 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1358                                  QEMUIOVector *iov, int nb_sectors,
1359                                  BlockDriverCompletionFunc *cb, void *opaque)
1360 {
1361     if (bdrv_check_request(bs, sector_num, nb_sectors))
1362         return NULL;
1363
1364     return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1365                               cb, opaque, 0);
1366 }
1367
1368 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1369                                   QEMUIOVector *iov, int nb_sectors,
1370                                   BlockDriverCompletionFunc *cb, void *opaque)
1371 {
1372     if (bdrv_check_request(bs, sector_num, nb_sectors))
1373         return NULL;
1374
1375     return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1376                               cb, opaque, 1);
1377 }
1378
1379 static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1380                                 uint8_t *buf, int nb_sectors,
1381                                 BlockDriverCompletionFunc *cb, void *opaque)
1382 {
1383     BlockDriver *drv = bs->drv;
1384     BlockDriverAIOCB *ret;
1385
1386     if (!drv)
1387         return NULL;
1388     if (bdrv_check_request(bs, sector_num, nb_sectors))
1389         return NULL;
1390
1391     ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1392
1393     if (ret) {
1394         /* Update stats even though technically transfer has not happened. */
1395         bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1396         bs->rd_ops ++;
1397     }
1398
1399     return ret;
1400 }
1401
1402 static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1403                                  const uint8_t *buf, int nb_sectors,
1404                                  BlockDriverCompletionFunc *cb, void *opaque)
1405 {
1406     BlockDriver *drv = bs->drv;
1407     BlockDriverAIOCB *ret;
1408
1409     if (!drv)
1410         return NULL;
1411     if (bs->read_only)
1412         return NULL;
1413     if (bdrv_check_request(bs, sector_num, nb_sectors))
1414         return NULL;
1415
1416     ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1417
1418     if (ret) {
1419         /* Update stats even though technically transfer has not happened. */
1420         bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1421         bs->wr_ops ++;
1422     }
1423
1424     return ret;
1425 }
1426
1427 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1428 {
1429     acb->pool->cancel(acb);
1430 }
1431
1432
1433 /**************************************************************/
1434 /* async block device emulation */
1435
1436 static void bdrv_aio_bh_cb(void *opaque)
1437 {
1438     BlockDriverAIOCBSync *acb = opaque;
1439     acb->common.cb(acb->common.opaque, acb->ret);
1440     qemu_aio_release(acb);
1441 }
1442
1443 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1444         int64_t sector_num, uint8_t *buf, int nb_sectors,
1445         BlockDriverCompletionFunc *cb, void *opaque)
1446 {
1447     BlockDriverAIOCBSync *acb;
1448     int ret;
1449
1450     acb = qemu_aio_get(bs, cb, opaque);
1451     if (!acb->bh)
1452         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1453     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1454     acb->ret = ret;
1455     qemu_bh_schedule(acb->bh);
1456     return &acb->common;
1457 }
1458
1459 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1460         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1461         BlockDriverCompletionFunc *cb, void *opaque)
1462 {
1463     BlockDriverAIOCBSync *acb;
1464     int ret;
1465
1466     acb = qemu_aio_get(bs, cb, opaque);
1467     if (!acb->bh)
1468         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1469     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1470     acb->ret = ret;
1471     qemu_bh_schedule(acb->bh);
1472     return &acb->common;
1473 }
1474
1475 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1476 {
1477     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1478     qemu_bh_cancel(acb->bh);
1479     qemu_aio_release(acb);
1480 }
1481
1482 /**************************************************************/
1483 /* sync block device emulation */
1484
1485 static void bdrv_rw_em_cb(void *opaque, int ret)
1486 {
1487     *(int *)opaque = ret;
1488 }
1489
1490 #define NOT_DONE 0x7fffffff
1491
1492 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1493                         uint8_t *buf, int nb_sectors)
1494 {
1495     int async_ret;
1496     BlockDriverAIOCB *acb;
1497
1498     async_ret = NOT_DONE;
1499     acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1500                         bdrv_rw_em_cb, &async_ret);
1501     if (acb == NULL)
1502         return -1;
1503
1504     while (async_ret == NOT_DONE) {
1505         qemu_aio_wait();
1506     }
1507
1508     return async_ret;
1509 }
1510
1511 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1512                          const uint8_t *buf, int nb_sectors)
1513 {
1514     int async_ret;
1515     BlockDriverAIOCB *acb;
1516
1517     async_ret = NOT_DONE;
1518     acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1519                          bdrv_rw_em_cb, &async_ret);
1520     if (acb == NULL)
1521         return -1;
1522     while (async_ret == NOT_DONE) {
1523         qemu_aio_wait();
1524     }
1525     return async_ret;
1526 }
1527
1528 void bdrv_init(void)
1529 {
1530     aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
1531                   bdrv_aio_cancel_vector);
1532
1533     bdrv_register(&bdrv_raw);
1534     bdrv_register(&bdrv_host_device);
1535 #ifndef _WIN32
1536     bdrv_register(&bdrv_cow);
1537 #endif
1538     bdrv_register(&bdrv_qcow);
1539     bdrv_register(&bdrv_vmdk);
1540     bdrv_register(&bdrv_cloop);
1541     bdrv_register(&bdrv_dmg);
1542     bdrv_register(&bdrv_bochs);
1543     bdrv_register(&bdrv_vpc);
1544     bdrv_register(&bdrv_vvfat);
1545     bdrv_register(&bdrv_qcow2);
1546     bdrv_register(&bdrv_parallels);
1547     bdrv_register(&bdrv_nbd);
1548 }
1549
1550 void aio_pool_init(AIOPool *pool, int aiocb_size,
1551                    void (*cancel)(BlockDriverAIOCB *acb))
1552 {
1553     pool->aiocb_size = aiocb_size;
1554     pool->cancel = cancel;
1555     pool->free_aiocb = NULL;
1556 }
1557
1558 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1559                         BlockDriverCompletionFunc *cb, void *opaque)
1560 {
1561     BlockDriverAIOCB *acb;
1562
1563     if (pool->free_aiocb) {
1564         acb = pool->free_aiocb;
1565         pool->free_aiocb = acb->next;
1566     } else {
1567         acb = qemu_mallocz(pool->aiocb_size);
1568         acb->pool = pool;
1569     }
1570     acb->bs = bs;
1571     acb->cb = cb;
1572     acb->opaque = opaque;
1573     return acb;
1574 }
1575
1576 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1577                    void *opaque)
1578 {
1579     return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1580 }
1581
1582 void qemu_aio_release(void *p)
1583 {
1584     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1585     AIOPool *pool = acb->pool;
1586     acb->next = pool->free_aiocb;
1587     pool->free_aiocb = acb;
1588 }
1589
1590 /**************************************************************/
1591 /* removable device support */
1592
1593 /**
1594  * Return TRUE if the media is present
1595  */
1596 int bdrv_is_inserted(BlockDriverState *bs)
1597 {
1598     BlockDriver *drv = bs->drv;
1599     int ret;
1600     if (!drv)
1601         return 0;
1602     if (!drv->bdrv_is_inserted)
1603         return 1;
1604     ret = drv->bdrv_is_inserted(bs);
1605     return ret;
1606 }
1607
1608 /**
1609  * Return TRUE if the media changed since the last call to this
1610  * function. It is currently only used for floppy disks
1611  */
1612 int bdrv_media_changed(BlockDriverState *bs)
1613 {
1614     BlockDriver *drv = bs->drv;
1615     int ret;
1616
1617     if (!drv || !drv->bdrv_media_changed)
1618         ret = -ENOTSUP;
1619     else
1620         ret = drv->bdrv_media_changed(bs);
1621     if (ret == -ENOTSUP)
1622         ret = bs->media_changed;
1623     bs->media_changed = 0;
1624     return ret;
1625 }
1626
1627 /**
1628  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1629  */
1630 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1631 {
1632     BlockDriver *drv = bs->drv;
1633     int ret;
1634
1635     if (!drv || !drv->bdrv_eject) {
1636         ret = -ENOTSUP;
1637     } else {
1638         ret = drv->bdrv_eject(bs, eject_flag);
1639     }
1640     if (ret == -ENOTSUP) {
1641         if (eject_flag)
1642             bdrv_close(bs);
1643     }
1644 }
1645
1646 int bdrv_is_locked(BlockDriverState *bs)
1647 {
1648     return bs->locked;
1649 }
1650
1651 /**
1652  * Lock or unlock the media (if it is locked, the user won't be able
1653  * to eject it manually).
1654  */
1655 void bdrv_set_locked(BlockDriverState *bs, int locked)
1656 {
1657     BlockDriver *drv = bs->drv;
1658
1659     bs->locked = locked;
1660     if (drv && drv->bdrv_set_locked) {
1661         drv->bdrv_set_locked(bs, locked);
1662     }
1663 }
1664
1665 /* needed for generic scsi interface */
1666
1667 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1668 {
1669     BlockDriver *drv = bs->drv;
1670
1671     if (drv && drv->bdrv_ioctl)
1672         return drv->bdrv_ioctl(bs, req, buf);
1673     return -ENOTSUP;
1674 }
1675
1676 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1677         unsigned long int req, void *buf,
1678         BlockDriverCompletionFunc *cb, void *opaque)
1679 {
1680     BlockDriver *drv = bs->drv;
1681
1682     if (drv && drv->bdrv_aio_ioctl)
1683         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1684     return NULL;
1685 }