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