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