Merge commit 'v0.10.3' into stable-0.10
[qemu-kvm/fedora.git] / block.c
blobbca92970b3dfb6c96d8ae3aff3577af4464c9f5f
1 /*
2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "config-host.h"
25 #ifdef _BSD
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
28 #endif
30 #include "qemu-common.h"
31 #include "console.h"
32 #include "block_int.h"
33 #include "osdep.h"
35 #ifdef _BSD
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/disk.h>
40 #endif
42 #define SECTOR_BITS 9
43 #define SECTOR_SIZE (1 << SECTOR_BITS)
45 static AIOPool vectored_aio_pool;
47 typedef struct BlockDriverAIOCBSync {
48 BlockDriverAIOCB common;
49 QEMUBH *bh;
50 int ret;
51 } BlockDriverAIOCBSync;
53 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
54 int64_t sector_num, uint8_t *buf, int nb_sectors,
55 BlockDriverCompletionFunc *cb, void *opaque);
56 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
57 int64_t sector_num, const uint8_t *buf, int nb_sectors,
58 BlockDriverCompletionFunc *cb, void *opaque);
59 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
60 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
61 uint8_t *buf, int nb_sectors);
62 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
63 const uint8_t *buf, int nb_sectors);
65 BlockDriverState *bdrv_first;
67 static BlockDriver *first_drv;
69 int path_is_absolute(const char *path)
71 const char *p;
72 #ifdef _WIN32
73 /* specific case for names like: "\\.\d:" */
74 if (*path == '/' || *path == '\\')
75 return 1;
76 #endif
77 p = strchr(path, ':');
78 if (p)
79 p++;
80 else
81 p = path;
82 #ifdef _WIN32
83 return (*p == '/' || *p == '\\');
84 #else
85 return (*p == '/');
86 #endif
89 /* if filename is absolute, just copy it to dest. Otherwise, build a
90 path to it by considering it is relative to base_path. URL are
91 supported. */
92 void path_combine(char *dest, int dest_size,
93 const char *base_path,
94 const char *filename)
96 const char *p, *p1;
97 int len;
99 if (dest_size <= 0)
100 return;
101 if (path_is_absolute(filename)) {
102 pstrcpy(dest, dest_size, filename);
103 } else {
104 p = strchr(base_path, ':');
105 if (p)
106 p++;
107 else
108 p = base_path;
109 p1 = strrchr(base_path, '/');
110 #ifdef _WIN32
112 const char *p2;
113 p2 = strrchr(base_path, '\\');
114 if (!p1 || p2 > p1)
115 p1 = p2;
117 #endif
118 if (p1)
119 p1++;
120 else
121 p1 = base_path;
122 if (p1 > p)
123 p = p1;
124 len = p - base_path;
125 if (len > dest_size - 1)
126 len = dest_size - 1;
127 memcpy(dest, base_path, len);
128 dest[len] = '\0';
129 pstrcat(dest, dest_size, filename);
134 static void bdrv_register(BlockDriver *bdrv)
136 if (!bdrv->bdrv_aio_read) {
137 /* add AIO emulation layer */
138 bdrv->bdrv_aio_read = bdrv_aio_read_em;
139 bdrv->bdrv_aio_write = bdrv_aio_write_em;
140 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
141 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
142 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
143 /* add synchronous IO emulation layer */
144 bdrv->bdrv_read = bdrv_read_em;
145 bdrv->bdrv_write = bdrv_write_em;
147 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
148 bdrv->next = first_drv;
149 first_drv = bdrv;
152 /* create a new block device (by default it is empty) */
153 BlockDriverState *bdrv_new(const char *device_name)
155 BlockDriverState **pbs, *bs;
157 bs = qemu_mallocz(sizeof(BlockDriverState));
158 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
159 if (device_name[0] != '\0') {
160 /* insert at the end */
161 pbs = &bdrv_first;
162 while (*pbs != NULL)
163 pbs = &(*pbs)->next;
164 *pbs = bs;
166 return bs;
169 BlockDriver *bdrv_find_format(const char *format_name)
171 BlockDriver *drv1;
172 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
173 if (!strcmp(drv1->format_name, format_name))
174 return drv1;
176 return NULL;
179 int bdrv_create(BlockDriver *drv,
180 const char *filename, int64_t size_in_sectors,
181 const char *backing_file, int flags)
183 if (!drv->bdrv_create)
184 return -ENOTSUP;
185 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
188 #ifdef _WIN32
189 void get_tmp_filename(char *filename, int size)
191 char temp_dir[MAX_PATH];
193 GetTempPath(MAX_PATH, temp_dir);
194 GetTempFileName(temp_dir, "qem", 0, filename);
196 #else
197 void get_tmp_filename(char *filename, int size)
199 int fd;
200 const char *tmpdir;
201 /* XXX: race condition possible */
202 tmpdir = getenv("TMPDIR");
203 if (!tmpdir)
204 tmpdir = "/tmp";
205 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
206 fd = mkstemp(filename);
207 close(fd);
209 #endif
211 #ifdef _WIN32
212 static int is_windows_drive_prefix(const char *filename)
214 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
215 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
216 filename[1] == ':');
219 static int is_windows_drive(const char *filename)
221 if (is_windows_drive_prefix(filename) &&
222 filename[2] == '\0')
223 return 1;
224 if (strstart(filename, "\\\\.\\", NULL) ||
225 strstart(filename, "//./", NULL))
226 return 1;
227 return 0;
229 #endif
231 static BlockDriver *find_protocol(const char *filename)
233 BlockDriver *drv1;
234 char protocol[128];
235 int len;
236 const char *p;
238 #ifdef _WIN32
239 if (is_windows_drive(filename) ||
240 is_windows_drive_prefix(filename))
241 return &bdrv_raw;
242 #endif
243 p = strchr(filename, ':');
244 if (!p)
245 return &bdrv_raw;
246 len = p - filename;
247 if (len > sizeof(protocol) - 1)
248 len = sizeof(protocol) - 1;
249 memcpy(protocol, filename, len);
250 protocol[len] = '\0';
251 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
252 if (drv1->protocol_name &&
253 !strcmp(drv1->protocol_name, protocol))
254 return drv1;
256 return NULL;
259 /* XXX: force raw format if block or character device ? It would
260 simplify the BSD case */
261 static BlockDriver *find_image_format(const char *filename)
263 int ret, score, score_max;
264 BlockDriver *drv1, *drv;
265 uint8_t buf[2048];
266 BlockDriverState *bs;
268 /* detect host devices. By convention, /dev/cdrom[N] is always
269 recognized as a host CDROM */
270 if (strstart(filename, "/dev/cdrom", NULL))
271 return &bdrv_host_device;
272 #ifdef _WIN32
273 if (is_windows_drive(filename))
274 return &bdrv_host_device;
275 #else
277 struct stat st;
278 if (stat(filename, &st) >= 0 &&
279 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
280 return &bdrv_host_device;
283 #endif
285 drv = find_protocol(filename);
286 /* no need to test disk image formats for vvfat */
287 if (drv == &bdrv_vvfat)
288 return drv;
290 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
291 if (ret < 0)
292 return NULL;
293 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
294 bdrv_delete(bs);
295 if (ret < 0) {
296 return NULL;
299 score_max = 0;
300 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
301 if (drv1->bdrv_probe) {
302 score = drv1->bdrv_probe(buf, ret, filename);
303 if (score > score_max) {
304 score_max = score;
305 drv = drv1;
309 return drv;
312 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
314 BlockDriverState *bs;
315 int ret;
317 bs = bdrv_new("");
318 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
319 if (ret < 0) {
320 bdrv_delete(bs);
321 return ret;
323 bs->growable = 1;
324 *pbs = bs;
325 return 0;
328 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
330 return bdrv_open2(bs, filename, flags, NULL);
333 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
334 BlockDriver *drv)
336 int ret, open_flags;
337 char tmp_filename[PATH_MAX];
338 char backing_filename[PATH_MAX];
340 bs->read_only = 0;
341 bs->is_temporary = 0;
342 bs->encrypted = 0;
343 bs->valid_key = 0;
345 if (flags & BDRV_O_SNAPSHOT) {
346 BlockDriverState *bs1;
347 int64_t total_size;
348 int is_protocol = 0;
350 /* if snapshot, we create a temporary backing file and open it
351 instead of opening 'filename' directly */
353 /* if there is a backing file, use it */
354 bs1 = bdrv_new("");
355 ret = bdrv_open(bs1, filename, 0);
356 if (ret < 0) {
357 bdrv_delete(bs1);
358 return ret;
360 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
362 if (bs1->drv && bs1->drv->protocol_name)
363 is_protocol = 1;
365 bdrv_delete(bs1);
367 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
369 /* Real path is meaningless for protocols */
370 if (is_protocol)
371 snprintf(backing_filename, sizeof(backing_filename),
372 "%s", filename);
373 else
374 realpath(filename, backing_filename);
376 ret = bdrv_create(&bdrv_qcow2, tmp_filename,
377 total_size, backing_filename, 0);
378 if (ret < 0) {
379 return ret;
381 filename = tmp_filename;
382 bs->is_temporary = 1;
385 pstrcpy(bs->filename, sizeof(bs->filename), filename);
386 if (flags & BDRV_O_FILE) {
387 drv = find_protocol(filename);
388 } else if (!drv) {
389 drv = find_image_format(filename);
391 if (!drv) {
392 ret = -ENOENT;
393 goto unlink_and_fail;
395 bs->drv = drv;
396 bs->opaque = qemu_mallocz(drv->instance_size);
397 /* Note: for compatibility, we open disk image files as RDWR, and
398 RDONLY as fallback */
399 if (!(flags & BDRV_O_FILE))
400 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
401 else
402 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
403 ret = drv->bdrv_open(bs, filename, open_flags);
404 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
405 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
406 bs->read_only = 1;
408 if (ret < 0) {
409 qemu_free(bs->opaque);
410 bs->opaque = NULL;
411 bs->drv = NULL;
412 unlink_and_fail:
413 if (bs->is_temporary)
414 unlink(filename);
415 return ret;
417 if (drv->bdrv_getlength) {
418 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
420 #ifndef _WIN32
421 if (bs->is_temporary) {
422 unlink(filename);
424 #endif
425 if (bs->backing_file[0] != '\0') {
426 /* if there is a backing file, use it */
427 bs->backing_hd = bdrv_new("");
428 path_combine(backing_filename, sizeof(backing_filename),
429 filename, bs->backing_file);
430 ret = bdrv_open(bs->backing_hd, backing_filename, open_flags);
431 if (ret < 0) {
432 bdrv_close(bs);
433 return ret;
437 /* call the change callback */
438 bs->media_changed = 1;
439 if (bs->change_cb)
440 bs->change_cb(bs->change_opaque);
442 return 0;
445 void bdrv_close(BlockDriverState *bs)
447 if (bs->drv) {
448 if (bs->backing_hd)
449 bdrv_delete(bs->backing_hd);
450 bs->drv->bdrv_close(bs);
451 qemu_free(bs->opaque);
452 #ifdef _WIN32
453 if (bs->is_temporary) {
454 unlink(bs->filename);
456 #endif
457 bs->opaque = NULL;
458 bs->drv = NULL;
460 /* call the change callback */
461 bs->media_changed = 1;
462 if (bs->change_cb)
463 bs->change_cb(bs->change_opaque);
467 void bdrv_delete(BlockDriverState *bs)
469 BlockDriverState **pbs;
471 pbs = &bdrv_first;
472 while (*pbs != bs && *pbs != NULL)
473 pbs = &(*pbs)->next;
474 if (*pbs == bs)
475 *pbs = bs->next;
477 bdrv_close(bs);
478 qemu_free(bs);
481 /* commit COW file into the raw image */
482 int bdrv_commit(BlockDriverState *bs)
484 BlockDriver *drv = bs->drv;
485 int64_t i, total_sectors;
486 int n, j;
487 unsigned char sector[512];
489 if (!drv)
490 return -ENOMEDIUM;
492 if (bs->read_only) {
493 return -EACCES;
496 if (!bs->backing_hd) {
497 return -ENOTSUP;
500 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
501 for (i = 0; i < total_sectors;) {
502 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
503 for(j = 0; j < n; j++) {
504 if (bdrv_read(bs, i, sector, 1) != 0) {
505 return -EIO;
508 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
509 return -EIO;
511 i++;
513 } else {
514 i += n;
518 if (drv->bdrv_make_empty)
519 return drv->bdrv_make_empty(bs);
521 return 0;
524 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
525 size_t size)
527 int64_t len;
529 if (!bdrv_is_inserted(bs))
530 return -ENOMEDIUM;
532 if (bs->growable)
533 return 0;
535 len = bdrv_getlength(bs);
537 if ((offset + size) > len)
538 return -EIO;
540 return 0;
543 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
544 int nb_sectors)
546 int64_t offset;
548 /* Deal with byte accesses */
549 if (sector_num < 0)
550 offset = -sector_num;
551 else
552 offset = sector_num * 512;
554 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
557 /* return < 0 if error. See bdrv_write() for the return codes */
558 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
559 uint8_t *buf, int nb_sectors)
561 BlockDriver *drv = bs->drv;
563 if (!drv)
564 return -ENOMEDIUM;
565 if (bdrv_check_request(bs, sector_num, nb_sectors))
566 return -EIO;
568 if (drv->bdrv_pread) {
569 int ret, len;
570 len = nb_sectors * 512;
571 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
572 if (ret < 0)
573 return ret;
574 else if (ret != len)
575 return -EINVAL;
576 else {
577 bs->rd_bytes += (unsigned) len;
578 bs->rd_ops ++;
579 return 0;
581 } else {
582 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
586 /* Return < 0 if error. Important errors are:
587 -EIO generic I/O error (may happen for all errors)
588 -ENOMEDIUM No media inserted.
589 -EINVAL Invalid sector number or nb_sectors
590 -EACCES Trying to write a read-only device
592 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
593 const uint8_t *buf, int nb_sectors)
595 BlockDriver *drv = bs->drv;
596 if (!bs->drv)
597 return -ENOMEDIUM;
598 if (bs->read_only)
599 return -EACCES;
600 if (bdrv_check_request(bs, sector_num, nb_sectors))
601 return -EIO;
603 if (drv->bdrv_pwrite) {
604 int ret, len, count = 0;
605 len = nb_sectors * 512;
606 do {
607 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
608 if (ret < 0) {
609 printf("bdrv_write ret=%d\n", ret);
610 return ret;
612 count += ret;
613 buf += ret;
614 } while (count != len);
615 bs->wr_bytes += (unsigned) len;
616 bs->wr_ops ++;
617 return 0;
619 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
622 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
623 uint8_t *buf, int count1)
625 uint8_t tmp_buf[SECTOR_SIZE];
626 int len, nb_sectors, count;
627 int64_t sector_num;
629 count = count1;
630 /* first read to align to sector start */
631 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
632 if (len > count)
633 len = count;
634 sector_num = offset >> SECTOR_BITS;
635 if (len > 0) {
636 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
637 return -EIO;
638 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
639 count -= len;
640 if (count == 0)
641 return count1;
642 sector_num++;
643 buf += len;
646 /* read the sectors "in place" */
647 nb_sectors = count >> SECTOR_BITS;
648 if (nb_sectors > 0) {
649 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
650 return -EIO;
651 sector_num += nb_sectors;
652 len = nb_sectors << SECTOR_BITS;
653 buf += len;
654 count -= len;
657 /* add data from the last sector */
658 if (count > 0) {
659 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
660 return -EIO;
661 memcpy(buf, tmp_buf, count);
663 return count1;
666 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
667 const uint8_t *buf, int count1)
669 uint8_t tmp_buf[SECTOR_SIZE];
670 int len, nb_sectors, count;
671 int64_t sector_num;
673 count = count1;
674 /* first write to align to sector start */
675 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
676 if (len > count)
677 len = count;
678 sector_num = offset >> SECTOR_BITS;
679 if (len > 0) {
680 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
681 return -EIO;
682 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
683 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
684 return -EIO;
685 count -= len;
686 if (count == 0)
687 return count1;
688 sector_num++;
689 buf += len;
692 /* write the sectors "in place" */
693 nb_sectors = count >> SECTOR_BITS;
694 if (nb_sectors > 0) {
695 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
696 return -EIO;
697 sector_num += nb_sectors;
698 len = nb_sectors << SECTOR_BITS;
699 buf += len;
700 count -= len;
703 /* add data from the last sector */
704 if (count > 0) {
705 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
706 return -EIO;
707 memcpy(tmp_buf, buf, count);
708 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
709 return -EIO;
711 return count1;
715 * Read with byte offsets (needed only for file protocols)
717 int bdrv_pread(BlockDriverState *bs, int64_t offset,
718 void *buf1, int count1)
720 BlockDriver *drv = bs->drv;
722 if (!drv)
723 return -ENOMEDIUM;
724 if (bdrv_check_byte_request(bs, offset, count1))
725 return -EIO;
727 if (!drv->bdrv_pread)
728 return bdrv_pread_em(bs, offset, buf1, count1);
729 return drv->bdrv_pread(bs, offset, buf1, count1);
733 * Write with byte offsets (needed only for file protocols)
735 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
736 const void *buf1, int count1)
738 BlockDriver *drv = bs->drv;
740 if (!drv)
741 return -ENOMEDIUM;
742 if (bdrv_check_byte_request(bs, offset, count1))
743 return -EIO;
745 if (!drv->bdrv_pwrite)
746 return bdrv_pwrite_em(bs, offset, buf1, count1);
747 return drv->bdrv_pwrite(bs, offset, buf1, count1);
751 * Truncate file to 'offset' bytes (needed only for file protocols)
753 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
755 BlockDriver *drv = bs->drv;
756 if (!drv)
757 return -ENOMEDIUM;
758 if (!drv->bdrv_truncate)
759 return -ENOTSUP;
760 return drv->bdrv_truncate(bs, offset);
764 * Length of a file in bytes. Return < 0 if error or unknown.
766 int64_t bdrv_getlength(BlockDriverState *bs)
768 BlockDriver *drv = bs->drv;
769 if (!drv)
770 return -ENOMEDIUM;
771 if (!drv->bdrv_getlength) {
772 /* legacy mode */
773 return bs->total_sectors * SECTOR_SIZE;
775 return drv->bdrv_getlength(bs);
778 /* return 0 as number of sectors if no device present or error */
779 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
781 int64_t length;
782 length = bdrv_getlength(bs);
783 if (length < 0)
784 length = 0;
785 else
786 length = length >> SECTOR_BITS;
787 *nb_sectors_ptr = length;
790 struct partition {
791 uint8_t boot_ind; /* 0x80 - active */
792 uint8_t head; /* starting head */
793 uint8_t sector; /* starting sector */
794 uint8_t cyl; /* starting cylinder */
795 uint8_t sys_ind; /* What partition type */
796 uint8_t end_head; /* end head */
797 uint8_t end_sector; /* end sector */
798 uint8_t end_cyl; /* end cylinder */
799 uint32_t start_sect; /* starting sector counting from 0 */
800 uint32_t nr_sects; /* nr of sectors in partition */
801 } __attribute__((packed));
803 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
804 static int guess_disk_lchs(BlockDriverState *bs,
805 int *pcylinders, int *pheads, int *psectors)
807 uint8_t *buf;
808 int ret, i, heads, sectors, cylinders;
809 struct partition *p;
810 uint32_t nr_sects;
811 uint64_t nb_sectors;
813 buf = qemu_memalign(512, 512);
814 if (buf == NULL)
815 return -1;
817 bdrv_get_geometry(bs, &nb_sectors);
819 ret = bdrv_read(bs, 0, buf, 1);
820 if (ret < 0)
821 return -1;
822 /* test msdos magic */
823 if (buf[510] != 0x55 || buf[511] != 0xaa) {
824 qemu_free(buf);
825 return -1;
827 for(i = 0; i < 4; i++) {
828 p = ((struct partition *)(buf + 0x1be)) + i;
829 nr_sects = le32_to_cpu(p->nr_sects);
830 if (nr_sects && p->end_head) {
831 /* We make the assumption that the partition terminates on
832 a cylinder boundary */
833 heads = p->end_head + 1;
834 sectors = p->end_sector & 63;
835 if (sectors == 0)
836 continue;
837 cylinders = nb_sectors / (heads * sectors);
838 if (cylinders < 1 || cylinders > 16383)
839 continue;
840 *pheads = heads;
841 *psectors = sectors;
842 *pcylinders = cylinders;
843 #if 0
844 printf("guessed geometry: LCHS=%d %d %d\n",
845 cylinders, heads, sectors);
846 #endif
847 qemu_free(buf);
848 return 0;
851 qemu_free(buf);
852 return -1;
855 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
857 int translation, lba_detected = 0;
858 int cylinders, heads, secs;
859 uint64_t nb_sectors;
861 /* if a geometry hint is available, use it */
862 bdrv_get_geometry(bs, &nb_sectors);
863 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
864 translation = bdrv_get_translation_hint(bs);
865 if (cylinders != 0) {
866 *pcyls = cylinders;
867 *pheads = heads;
868 *psecs = secs;
869 } else {
870 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
871 if (heads > 16) {
872 /* if heads > 16, it means that a BIOS LBA
873 translation was active, so the default
874 hardware geometry is OK */
875 lba_detected = 1;
876 goto default_geometry;
877 } else {
878 *pcyls = cylinders;
879 *pheads = heads;
880 *psecs = secs;
881 /* disable any translation to be in sync with
882 the logical geometry */
883 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
884 bdrv_set_translation_hint(bs,
885 BIOS_ATA_TRANSLATION_NONE);
888 } else {
889 default_geometry:
890 /* if no geometry, use a standard physical disk geometry */
891 cylinders = nb_sectors / (16 * 63);
893 if (cylinders > 16383)
894 cylinders = 16383;
895 else if (cylinders < 2)
896 cylinders = 2;
897 *pcyls = cylinders;
898 *pheads = 16;
899 *psecs = 63;
900 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
901 if ((*pcyls * *pheads) <= 131072) {
902 bdrv_set_translation_hint(bs,
903 BIOS_ATA_TRANSLATION_LARGE);
904 } else {
905 bdrv_set_translation_hint(bs,
906 BIOS_ATA_TRANSLATION_LBA);
910 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
914 void bdrv_set_geometry_hint(BlockDriverState *bs,
915 int cyls, int heads, int secs)
917 bs->cyls = cyls;
918 bs->heads = heads;
919 bs->secs = secs;
922 void bdrv_set_type_hint(BlockDriverState *bs, int type)
924 bs->type = type;
925 bs->removable = ((type == BDRV_TYPE_CDROM ||
926 type == BDRV_TYPE_FLOPPY));
929 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
931 bs->translation = translation;
934 void bdrv_get_geometry_hint(BlockDriverState *bs,
935 int *pcyls, int *pheads, int *psecs)
937 *pcyls = bs->cyls;
938 *pheads = bs->heads;
939 *psecs = bs->secs;
942 int bdrv_get_type_hint(BlockDriverState *bs)
944 return bs->type;
947 int bdrv_get_translation_hint(BlockDriverState *bs)
949 return bs->translation;
952 int bdrv_is_removable(BlockDriverState *bs)
954 return bs->removable;
957 int bdrv_is_read_only(BlockDriverState *bs)
959 return bs->read_only;
962 int bdrv_is_sg(BlockDriverState *bs)
964 return bs->sg;
967 /* XXX: no longer used */
968 void bdrv_set_change_cb(BlockDriverState *bs,
969 void (*change_cb)(void *opaque), void *opaque)
971 bs->change_cb = change_cb;
972 bs->change_opaque = opaque;
975 int bdrv_is_encrypted(BlockDriverState *bs)
977 if (bs->backing_hd && bs->backing_hd->encrypted)
978 return 1;
979 return bs->encrypted;
982 int bdrv_key_required(BlockDriverState *bs)
984 BlockDriverState *backing_hd = bs->backing_hd;
986 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
987 return 1;
988 return (bs->encrypted && !bs->valid_key);
991 int bdrv_set_key(BlockDriverState *bs, const char *key)
993 int ret;
994 if (bs->backing_hd && bs->backing_hd->encrypted) {
995 ret = bdrv_set_key(bs->backing_hd, key);
996 if (ret < 0)
997 return ret;
998 if (!bs->encrypted)
999 return 0;
1001 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
1002 return -1;
1003 ret = bs->drv->bdrv_set_key(bs, key);
1004 bs->valid_key = (ret == 0);
1005 return ret;
1008 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1010 if (!bs->drv) {
1011 buf[0] = '\0';
1012 } else {
1013 pstrcpy(buf, buf_size, bs->drv->format_name);
1017 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1018 void *opaque)
1020 BlockDriver *drv;
1022 for (drv = first_drv; drv != NULL; drv = drv->next) {
1023 it(opaque, drv->format_name);
1027 BlockDriverState *bdrv_find(const char *name)
1029 BlockDriverState *bs;
1031 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1032 if (!strcmp(name, bs->device_name))
1033 return bs;
1035 return NULL;
1038 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1040 BlockDriverState *bs;
1042 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1043 it(opaque, bs);
1047 const char *bdrv_get_device_name(BlockDriverState *bs)
1049 return bs->device_name;
1052 void bdrv_flush(BlockDriverState *bs)
1054 if (bs->drv->bdrv_flush)
1055 bs->drv->bdrv_flush(bs);
1056 if (bs->backing_hd)
1057 bdrv_flush(bs->backing_hd);
1060 void bdrv_flush_all(void)
1062 BlockDriverState *bs;
1064 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1065 if (bs->drv && !bdrv_is_read_only(bs) &&
1066 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1067 bdrv_flush(bs);
1071 * Returns true iff the specified sector is present in the disk image. Drivers
1072 * not implementing the functionality are assumed to not support backing files,
1073 * hence all their sectors are reported as allocated.
1075 * 'pnum' is set to the number of sectors (including and immediately following
1076 * the specified sector) that are known to be in the same
1077 * allocated/unallocated state.
1079 * 'nb_sectors' is the max value 'pnum' should be set to.
1081 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1082 int *pnum)
1084 int64_t n;
1085 if (!bs->drv->bdrv_is_allocated) {
1086 if (sector_num >= bs->total_sectors) {
1087 *pnum = 0;
1088 return 0;
1090 n = bs->total_sectors - sector_num;
1091 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1092 return 1;
1094 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1097 void bdrv_info(void)
1099 BlockDriverState *bs;
1101 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1102 term_printf("%s:", bs->device_name);
1103 term_printf(" type=");
1104 switch(bs->type) {
1105 case BDRV_TYPE_HD:
1106 term_printf("hd");
1107 break;
1108 case BDRV_TYPE_CDROM:
1109 term_printf("cdrom");
1110 break;
1111 case BDRV_TYPE_FLOPPY:
1112 term_printf("floppy");
1113 break;
1115 term_printf(" removable=%d", bs->removable);
1116 if (bs->removable) {
1117 term_printf(" locked=%d", bs->locked);
1119 if (bs->drv) {
1120 term_printf(" file=");
1121 term_print_filename(bs->filename);
1122 if (bs->backing_file[0] != '\0') {
1123 term_printf(" backing_file=");
1124 term_print_filename(bs->backing_file);
1126 term_printf(" ro=%d", bs->read_only);
1127 term_printf(" drv=%s", bs->drv->format_name);
1128 term_printf(" encrypted=%d", bdrv_is_encrypted(bs));
1129 } else {
1130 term_printf(" [not inserted]");
1132 term_printf("\n");
1136 /* The "info blockstats" command. */
1137 void bdrv_info_stats (void)
1139 BlockDriverState *bs;
1141 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1142 term_printf ("%s:"
1143 " rd_bytes=%" PRIu64
1144 " wr_bytes=%" PRIu64
1145 " rd_operations=%" PRIu64
1146 " wr_operations=%" PRIu64
1147 "\n",
1148 bs->device_name,
1149 bs->rd_bytes, bs->wr_bytes,
1150 bs->rd_ops, bs->wr_ops);
1154 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1156 if (bs->backing_hd && bs->backing_hd->encrypted)
1157 return bs->backing_file;
1158 else if (bs->encrypted)
1159 return bs->filename;
1160 else
1161 return NULL;
1164 void bdrv_get_backing_filename(BlockDriverState *bs,
1165 char *filename, int filename_size)
1167 if (!bs->backing_hd) {
1168 pstrcpy(filename, filename_size, "");
1169 } else {
1170 pstrcpy(filename, filename_size, bs->backing_file);
1174 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1175 const uint8_t *buf, int nb_sectors)
1177 BlockDriver *drv = bs->drv;
1178 if (!drv)
1179 return -ENOMEDIUM;
1180 if (!drv->bdrv_write_compressed)
1181 return -ENOTSUP;
1182 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1185 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1187 BlockDriver *drv = bs->drv;
1188 if (!drv)
1189 return -ENOMEDIUM;
1190 if (!drv->bdrv_get_info)
1191 return -ENOTSUP;
1192 memset(bdi, 0, sizeof(*bdi));
1193 return drv->bdrv_get_info(bs, bdi);
1196 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1198 BlockDriver *drv = bs->drv;
1199 if (!drv)
1200 return -ENOMEDIUM;
1201 if (!drv->bdrv_put_buffer)
1202 return -ENOTSUP;
1203 return drv->bdrv_put_buffer(bs, buf, pos, size);
1206 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1208 BlockDriver *drv = bs->drv;
1209 if (!drv)
1210 return -ENOMEDIUM;
1211 if (!drv->bdrv_get_buffer)
1212 return -ENOTSUP;
1213 return drv->bdrv_get_buffer(bs, buf, pos, size);
1216 /**************************************************************/
1217 /* handling of snapshots */
1219 int bdrv_snapshot_create(BlockDriverState *bs,
1220 QEMUSnapshotInfo *sn_info)
1222 BlockDriver *drv = bs->drv;
1223 if (!drv)
1224 return -ENOMEDIUM;
1225 if (!drv->bdrv_snapshot_create)
1226 return -ENOTSUP;
1227 return drv->bdrv_snapshot_create(bs, sn_info);
1230 int bdrv_snapshot_goto(BlockDriverState *bs,
1231 const char *snapshot_id)
1233 BlockDriver *drv = bs->drv;
1234 if (!drv)
1235 return -ENOMEDIUM;
1236 if (!drv->bdrv_snapshot_goto)
1237 return -ENOTSUP;
1238 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1241 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1243 BlockDriver *drv = bs->drv;
1244 if (!drv)
1245 return -ENOMEDIUM;
1246 if (!drv->bdrv_snapshot_delete)
1247 return -ENOTSUP;
1248 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1251 int bdrv_snapshot_list(BlockDriverState *bs,
1252 QEMUSnapshotInfo **psn_info)
1254 BlockDriver *drv = bs->drv;
1255 if (!drv)
1256 return -ENOMEDIUM;
1257 if (!drv->bdrv_snapshot_list)
1258 return -ENOTSUP;
1259 return drv->bdrv_snapshot_list(bs, psn_info);
1262 #define NB_SUFFIXES 4
1264 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1266 static const char suffixes[NB_SUFFIXES] = "KMGT";
1267 int64_t base;
1268 int i;
1270 if (size <= 999) {
1271 snprintf(buf, buf_size, "%" PRId64, size);
1272 } else {
1273 base = 1024;
1274 for(i = 0; i < NB_SUFFIXES; i++) {
1275 if (size < (10 * base)) {
1276 snprintf(buf, buf_size, "%0.1f%c",
1277 (double)size / base,
1278 suffixes[i]);
1279 break;
1280 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1281 snprintf(buf, buf_size, "%" PRId64 "%c",
1282 ((size + (base >> 1)) / base),
1283 suffixes[i]);
1284 break;
1286 base = base * 1024;
1289 return buf;
1292 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1294 char buf1[128], date_buf[128], clock_buf[128];
1295 #ifdef _WIN32
1296 struct tm *ptm;
1297 #else
1298 struct tm tm;
1299 #endif
1300 time_t ti;
1301 int64_t secs;
1303 if (!sn) {
1304 snprintf(buf, buf_size,
1305 "%-10s%-20s%7s%20s%15s",
1306 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1307 } else {
1308 ti = sn->date_sec;
1309 #ifdef _WIN32
1310 ptm = localtime(&ti);
1311 strftime(date_buf, sizeof(date_buf),
1312 "%Y-%m-%d %H:%M:%S", ptm);
1313 #else
1314 localtime_r(&ti, &tm);
1315 strftime(date_buf, sizeof(date_buf),
1316 "%Y-%m-%d %H:%M:%S", &tm);
1317 #endif
1318 secs = sn->vm_clock_nsec / 1000000000;
1319 snprintf(clock_buf, sizeof(clock_buf),
1320 "%02d:%02d:%02d.%03d",
1321 (int)(secs / 3600),
1322 (int)((secs / 60) % 60),
1323 (int)(secs % 60),
1324 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1325 snprintf(buf, buf_size,
1326 "%-10s%-20s%7s%20s%15s",
1327 sn->id_str, sn->name,
1328 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1329 date_buf,
1330 clock_buf);
1332 return buf;
1336 /**************************************************************/
1337 /* async I/Os */
1339 typedef struct VectorTranslationState {
1340 QEMUIOVector *iov;
1341 uint8_t *bounce;
1342 int is_write;
1343 BlockDriverAIOCB *aiocb;
1344 BlockDriverAIOCB *this_aiocb;
1345 } VectorTranslationState;
1347 static void bdrv_aio_cancel_vector(BlockDriverAIOCB *acb)
1349 VectorTranslationState *s = acb->opaque;
1351 bdrv_aio_cancel(s->aiocb);
1354 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1356 VectorTranslationState *s = opaque;
1358 if (!s->is_write) {
1359 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1361 qemu_vfree(s->bounce);
1362 s->this_aiocb->cb(s->this_aiocb->opaque, ret);
1363 qemu_aio_release(s->this_aiocb);
1366 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1367 int64_t sector_num,
1368 QEMUIOVector *iov,
1369 int nb_sectors,
1370 BlockDriverCompletionFunc *cb,
1371 void *opaque,
1372 int is_write)
1375 VectorTranslationState *s = qemu_mallocz(sizeof(*s));
1376 BlockDriverAIOCB *aiocb = qemu_aio_get_pool(&vectored_aio_pool, bs,
1377 cb, opaque);
1379 s->this_aiocb = aiocb;
1380 s->iov = iov;
1381 s->bounce = qemu_memalign(512, nb_sectors * 512);
1382 s->is_write = is_write;
1383 if (is_write) {
1384 qemu_iovec_to_buffer(s->iov, s->bounce);
1385 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1386 bdrv_aio_rw_vector_cb, s);
1387 } else {
1388 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1389 bdrv_aio_rw_vector_cb, s);
1391 return aiocb;
1394 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1395 QEMUIOVector *iov, int nb_sectors,
1396 BlockDriverCompletionFunc *cb, void *opaque)
1398 if (bdrv_check_request(bs, sector_num, nb_sectors))
1399 return NULL;
1401 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1402 cb, opaque, 0);
1405 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1406 QEMUIOVector *iov, int nb_sectors,
1407 BlockDriverCompletionFunc *cb, void *opaque)
1409 if (bdrv_check_request(bs, sector_num, nb_sectors))
1410 return NULL;
1412 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1413 cb, opaque, 1);
1416 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1417 uint8_t *buf, int nb_sectors,
1418 BlockDriverCompletionFunc *cb, void *opaque)
1420 BlockDriver *drv = bs->drv;
1421 BlockDriverAIOCB *ret;
1423 if (!drv)
1424 return NULL;
1425 if (bdrv_check_request(bs, sector_num, nb_sectors))
1426 return NULL;
1428 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1430 if (ret) {
1431 /* Update stats even though technically transfer has not happened. */
1432 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1433 bs->rd_ops ++;
1436 return ret;
1439 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1440 const uint8_t *buf, int nb_sectors,
1441 BlockDriverCompletionFunc *cb, void *opaque)
1443 BlockDriver *drv = bs->drv;
1444 BlockDriverAIOCB *ret;
1446 if (!drv)
1447 return NULL;
1448 if (bs->read_only)
1449 return NULL;
1450 if (bdrv_check_request(bs, sector_num, nb_sectors))
1451 return NULL;
1453 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1455 if (ret) {
1456 /* Update stats even though technically transfer has not happened. */
1457 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1458 bs->wr_ops ++;
1461 return ret;
1464 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1466 acb->pool->cancel(acb);
1470 /**************************************************************/
1471 /* async block device emulation */
1473 static void bdrv_aio_bh_cb(void *opaque)
1475 BlockDriverAIOCBSync *acb = opaque;
1476 acb->common.cb(acb->common.opaque, acb->ret);
1477 qemu_aio_release(acb);
1480 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1481 int64_t sector_num, uint8_t *buf, int nb_sectors,
1482 BlockDriverCompletionFunc *cb, void *opaque)
1484 BlockDriverAIOCBSync *acb;
1485 int ret;
1487 acb = qemu_aio_get(bs, cb, opaque);
1488 if (!acb->bh)
1489 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1490 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1491 acb->ret = ret;
1492 qemu_bh_schedule(acb->bh);
1493 return &acb->common;
1496 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1497 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1498 BlockDriverCompletionFunc *cb, void *opaque)
1500 BlockDriverAIOCBSync *acb;
1501 int ret;
1503 acb = qemu_aio_get(bs, cb, opaque);
1504 if (!acb->bh)
1505 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1506 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1507 acb->ret = ret;
1508 qemu_bh_schedule(acb->bh);
1509 return &acb->common;
1512 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1514 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1515 qemu_bh_cancel(acb->bh);
1516 qemu_aio_release(acb);
1519 /**************************************************************/
1520 /* sync block device emulation */
1522 static void bdrv_rw_em_cb(void *opaque, int ret)
1524 *(int *)opaque = ret;
1527 #define NOT_DONE 0x7fffffff
1529 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1530 uint8_t *buf, int nb_sectors)
1532 int async_ret;
1533 BlockDriverAIOCB *acb;
1535 async_ret = NOT_DONE;
1536 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1537 bdrv_rw_em_cb, &async_ret);
1538 if (acb == NULL)
1539 return -1;
1541 while (async_ret == NOT_DONE) {
1542 qemu_aio_wait();
1545 return async_ret;
1548 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1549 const uint8_t *buf, int nb_sectors)
1551 int async_ret;
1552 BlockDriverAIOCB *acb;
1554 async_ret = NOT_DONE;
1555 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1556 bdrv_rw_em_cb, &async_ret);
1557 if (acb == NULL)
1558 return -1;
1559 while (async_ret == NOT_DONE) {
1560 qemu_aio_wait();
1562 return async_ret;
1565 void bdrv_init(void)
1567 aio_pool_init(&vectored_aio_pool, sizeof(BlockDriverAIOCB),
1568 bdrv_aio_cancel_vector);
1570 bdrv_register(&bdrv_raw);
1571 bdrv_register(&bdrv_host_device);
1572 #ifndef _WIN32
1573 bdrv_register(&bdrv_cow);
1574 #endif
1575 bdrv_register(&bdrv_qcow);
1576 bdrv_register(&bdrv_vmdk);
1577 bdrv_register(&bdrv_cloop);
1578 bdrv_register(&bdrv_dmg);
1579 bdrv_register(&bdrv_bochs);
1580 bdrv_register(&bdrv_vpc);
1581 bdrv_register(&bdrv_vvfat);
1582 bdrv_register(&bdrv_qcow2);
1583 bdrv_register(&bdrv_parallels);
1584 bdrv_register(&bdrv_nbd);
1587 void aio_pool_init(AIOPool *pool, int aiocb_size,
1588 void (*cancel)(BlockDriverAIOCB *acb))
1590 pool->aiocb_size = aiocb_size;
1591 pool->cancel = cancel;
1592 pool->free_aiocb = NULL;
1595 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1596 BlockDriverCompletionFunc *cb, void *opaque)
1598 BlockDriverAIOCB *acb;
1600 if (pool->free_aiocb) {
1601 acb = pool->free_aiocb;
1602 pool->free_aiocb = acb->next;
1603 } else {
1604 acb = qemu_mallocz(pool->aiocb_size);
1605 acb->pool = pool;
1607 acb->bs = bs;
1608 acb->cb = cb;
1609 acb->opaque = opaque;
1610 return acb;
1613 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1614 void *opaque)
1616 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1619 void qemu_aio_release(void *p)
1621 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1622 AIOPool *pool = acb->pool;
1623 acb->next = pool->free_aiocb;
1624 pool->free_aiocb = acb;
1627 /**************************************************************/
1628 /* removable device support */
1631 * Return TRUE if the media is present
1633 int bdrv_is_inserted(BlockDriverState *bs)
1635 BlockDriver *drv = bs->drv;
1636 int ret;
1637 if (!drv)
1638 return 0;
1639 if (!drv->bdrv_is_inserted)
1640 return 1;
1641 ret = drv->bdrv_is_inserted(bs);
1642 return ret;
1646 * Return TRUE if the media changed since the last call to this
1647 * function. It is currently only used for floppy disks
1649 int bdrv_media_changed(BlockDriverState *bs)
1651 BlockDriver *drv = bs->drv;
1652 int ret;
1654 if (!drv || !drv->bdrv_media_changed)
1655 ret = -ENOTSUP;
1656 else
1657 ret = drv->bdrv_media_changed(bs);
1658 if (ret == -ENOTSUP)
1659 ret = bs->media_changed;
1660 bs->media_changed = 0;
1661 return ret;
1665 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1667 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1669 BlockDriver *drv = bs->drv;
1670 int ret;
1672 if (!drv || !drv->bdrv_eject) {
1673 ret = -ENOTSUP;
1674 } else {
1675 ret = drv->bdrv_eject(bs, eject_flag);
1677 if (ret == -ENOTSUP) {
1678 if (eject_flag)
1679 bdrv_close(bs);
1683 int bdrv_is_locked(BlockDriverState *bs)
1685 return bs->locked;
1689 * Lock or unlock the media (if it is locked, the user won't be able
1690 * to eject it manually).
1692 void bdrv_set_locked(BlockDriverState *bs, int locked)
1694 BlockDriver *drv = bs->drv;
1696 bs->locked = locked;
1697 if (drv && drv->bdrv_set_locked) {
1698 drv->bdrv_set_locked(bs, locked);
1702 /* needed for generic scsi interface */
1704 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1706 BlockDriver *drv = bs->drv;
1708 if (drv && drv->bdrv_ioctl)
1709 return drv->bdrv_ioctl(bs, req, buf);
1710 return -ENOTSUP;