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