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