kvm: user: include arch specific headers from $(KERNELDIR)
[qemu-kvm/fedora.git] / block.c
blob863897a76482bdee1388053e02ce2bf1ece6eb22
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 < 0)
538 return -EIO;
540 if ((offset > len) || (len - offset < size))
541 return -EIO;
543 return 0;
546 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
547 int nb_sectors)
549 int64_t offset;
551 /* Deal with byte accesses */
552 if (sector_num < 0)
553 offset = -sector_num;
554 else
555 offset = sector_num * 512;
557 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
560 /* return < 0 if error. See bdrv_write() for the return codes */
561 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
562 uint8_t *buf, int nb_sectors)
564 BlockDriver *drv = bs->drv;
566 if (!drv)
567 return -ENOMEDIUM;
568 if (bdrv_check_request(bs, sector_num, nb_sectors))
569 return -EIO;
571 if (drv->bdrv_pread) {
572 int ret, len;
573 len = nb_sectors * 512;
574 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
575 if (ret < 0)
576 return ret;
577 else if (ret != len)
578 return -EINVAL;
579 else {
580 bs->rd_bytes += (unsigned) len;
581 bs->rd_ops ++;
582 return 0;
584 } else {
585 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
589 /* Return < 0 if error. Important errors are:
590 -EIO generic I/O error (may happen for all errors)
591 -ENOMEDIUM No media inserted.
592 -EINVAL Invalid sector number or nb_sectors
593 -EACCES Trying to write a read-only device
595 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
596 const uint8_t *buf, int nb_sectors)
598 BlockDriver *drv = bs->drv;
599 if (!bs->drv)
600 return -ENOMEDIUM;
601 if (bs->read_only)
602 return -EACCES;
603 if (bdrv_check_request(bs, sector_num, nb_sectors))
604 return -EIO;
606 if (drv->bdrv_pwrite) {
607 int ret, len, count = 0;
608 len = nb_sectors * 512;
609 do {
610 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
611 if (ret < 0) {
612 printf("bdrv_write ret=%d\n", ret);
613 return ret;
615 count += ret;
616 buf += ret;
617 } while (count != len);
618 bs->wr_bytes += (unsigned) len;
619 bs->wr_ops ++;
620 return 0;
622 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
625 static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
626 uint8_t *buf, int count1)
628 uint8_t tmp_buf[SECTOR_SIZE];
629 int len, nb_sectors, count;
630 int64_t sector_num;
632 count = count1;
633 /* first read to align to sector start */
634 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
635 if (len > count)
636 len = count;
637 sector_num = offset >> SECTOR_BITS;
638 if (len > 0) {
639 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
640 return -EIO;
641 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
642 count -= len;
643 if (count == 0)
644 return count1;
645 sector_num++;
646 buf += len;
649 /* read the sectors "in place" */
650 nb_sectors = count >> SECTOR_BITS;
651 if (nb_sectors > 0) {
652 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
653 return -EIO;
654 sector_num += nb_sectors;
655 len = nb_sectors << SECTOR_BITS;
656 buf += len;
657 count -= len;
660 /* add data from the last sector */
661 if (count > 0) {
662 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
663 return -EIO;
664 memcpy(buf, tmp_buf, count);
666 return count1;
669 static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
670 const uint8_t *buf, int count1)
672 uint8_t tmp_buf[SECTOR_SIZE];
673 int len, nb_sectors, count;
674 int64_t sector_num;
676 count = count1;
677 /* first write to align to sector start */
678 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
679 if (len > count)
680 len = count;
681 sector_num = offset >> SECTOR_BITS;
682 if (len > 0) {
683 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
684 return -EIO;
685 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
686 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
687 return -EIO;
688 count -= len;
689 if (count == 0)
690 return count1;
691 sector_num++;
692 buf += len;
695 /* write the sectors "in place" */
696 nb_sectors = count >> SECTOR_BITS;
697 if (nb_sectors > 0) {
698 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
699 return -EIO;
700 sector_num += nb_sectors;
701 len = nb_sectors << SECTOR_BITS;
702 buf += len;
703 count -= len;
706 /* add data from the last sector */
707 if (count > 0) {
708 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
709 return -EIO;
710 memcpy(tmp_buf, buf, count);
711 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
712 return -EIO;
714 return count1;
718 * Read with byte offsets (needed only for file protocols)
720 int bdrv_pread(BlockDriverState *bs, int64_t offset,
721 void *buf1, int count1)
723 BlockDriver *drv = bs->drv;
725 if (!drv)
726 return -ENOMEDIUM;
727 if (bdrv_check_byte_request(bs, offset, count1))
728 return -EIO;
730 if (!drv->bdrv_pread)
731 return bdrv_pread_em(bs, offset, buf1, count1);
732 return drv->bdrv_pread(bs, offset, buf1, count1);
736 * Write with byte offsets (needed only for file protocols)
738 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
739 const void *buf1, int count1)
741 BlockDriver *drv = bs->drv;
743 if (!drv)
744 return -ENOMEDIUM;
745 if (bdrv_check_byte_request(bs, offset, count1))
746 return -EIO;
748 if (!drv->bdrv_pwrite)
749 return bdrv_pwrite_em(bs, offset, buf1, count1);
750 return drv->bdrv_pwrite(bs, offset, buf1, count1);
754 * Truncate file to 'offset' bytes (needed only for file protocols)
756 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
758 BlockDriver *drv = bs->drv;
759 if (!drv)
760 return -ENOMEDIUM;
761 if (!drv->bdrv_truncate)
762 return -ENOTSUP;
763 return drv->bdrv_truncate(bs, offset);
767 * Length of a file in bytes. Return < 0 if error or unknown.
769 int64_t bdrv_getlength(BlockDriverState *bs)
771 BlockDriver *drv = bs->drv;
772 if (!drv)
773 return -ENOMEDIUM;
774 if (!drv->bdrv_getlength) {
775 /* legacy mode */
776 return bs->total_sectors * SECTOR_SIZE;
778 return drv->bdrv_getlength(bs);
781 /* return 0 as number of sectors if no device present or error */
782 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
784 int64_t length;
785 length = bdrv_getlength(bs);
786 if (length < 0)
787 length = 0;
788 else
789 length = length >> SECTOR_BITS;
790 *nb_sectors_ptr = length;
793 struct partition {
794 uint8_t boot_ind; /* 0x80 - active */
795 uint8_t head; /* starting head */
796 uint8_t sector; /* starting sector */
797 uint8_t cyl; /* starting cylinder */
798 uint8_t sys_ind; /* What partition type */
799 uint8_t end_head; /* end head */
800 uint8_t end_sector; /* end sector */
801 uint8_t end_cyl; /* end cylinder */
802 uint32_t start_sect; /* starting sector counting from 0 */
803 uint32_t nr_sects; /* nr of sectors in partition */
804 } __attribute__((packed));
806 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
807 static int guess_disk_lchs(BlockDriverState *bs,
808 int *pcylinders, int *pheads, int *psectors)
810 uint8_t *buf;
811 int ret, i, heads, sectors, cylinders;
812 struct partition *p;
813 uint32_t nr_sects;
814 uint64_t nb_sectors;
816 buf = qemu_memalign(512, 512);
817 if (buf == NULL)
818 return -1;
820 bdrv_get_geometry(bs, &nb_sectors);
822 ret = bdrv_read(bs, 0, buf, 1);
823 if (ret < 0)
824 return -1;
825 /* test msdos magic */
826 if (buf[510] != 0x55 || buf[511] != 0xaa) {
827 qemu_free(buf);
828 return -1;
830 for(i = 0; i < 4; i++) {
831 p = ((struct partition *)(buf + 0x1be)) + i;
832 nr_sects = le32_to_cpu(p->nr_sects);
833 if (nr_sects && p->end_head) {
834 /* We make the assumption that the partition terminates on
835 a cylinder boundary */
836 heads = p->end_head + 1;
837 sectors = p->end_sector & 63;
838 if (sectors == 0)
839 continue;
840 cylinders = nb_sectors / (heads * sectors);
841 if (cylinders < 1 || cylinders > 16383)
842 continue;
843 *pheads = heads;
844 *psectors = sectors;
845 *pcylinders = cylinders;
846 #if 0
847 printf("guessed geometry: LCHS=%d %d %d\n",
848 cylinders, heads, sectors);
849 #endif
850 qemu_free(buf);
851 return 0;
854 qemu_free(buf);
855 return -1;
858 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
860 int translation, lba_detected = 0;
861 int cylinders, heads, secs;
862 uint64_t nb_sectors;
864 /* if a geometry hint is available, use it */
865 bdrv_get_geometry(bs, &nb_sectors);
866 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
867 translation = bdrv_get_translation_hint(bs);
868 if (cylinders != 0) {
869 *pcyls = cylinders;
870 *pheads = heads;
871 *psecs = secs;
872 } else {
873 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
874 if (heads > 16) {
875 /* if heads > 16, it means that a BIOS LBA
876 translation was active, so the default
877 hardware geometry is OK */
878 lba_detected = 1;
879 goto default_geometry;
880 } else {
881 *pcyls = cylinders;
882 *pheads = heads;
883 *psecs = secs;
884 /* disable any translation to be in sync with
885 the logical geometry */
886 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
887 bdrv_set_translation_hint(bs,
888 BIOS_ATA_TRANSLATION_NONE);
891 } else {
892 default_geometry:
893 /* if no geometry, use a standard physical disk geometry */
894 cylinders = nb_sectors / (16 * 63);
896 if (cylinders > 16383)
897 cylinders = 16383;
898 else if (cylinders < 2)
899 cylinders = 2;
900 *pcyls = cylinders;
901 *pheads = 16;
902 *psecs = 63;
903 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
904 if ((*pcyls * *pheads) <= 131072) {
905 bdrv_set_translation_hint(bs,
906 BIOS_ATA_TRANSLATION_LARGE);
907 } else {
908 bdrv_set_translation_hint(bs,
909 BIOS_ATA_TRANSLATION_LBA);
913 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
917 void bdrv_set_geometry_hint(BlockDriverState *bs,
918 int cyls, int heads, int secs)
920 bs->cyls = cyls;
921 bs->heads = heads;
922 bs->secs = secs;
925 void bdrv_set_type_hint(BlockDriverState *bs, int type)
927 bs->type = type;
928 bs->removable = ((type == BDRV_TYPE_CDROM ||
929 type == BDRV_TYPE_FLOPPY));
932 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
934 bs->translation = translation;
937 void bdrv_get_geometry_hint(BlockDriverState *bs,
938 int *pcyls, int *pheads, int *psecs)
940 *pcyls = bs->cyls;
941 *pheads = bs->heads;
942 *psecs = bs->secs;
945 int bdrv_get_type_hint(BlockDriverState *bs)
947 return bs->type;
950 int bdrv_get_translation_hint(BlockDriverState *bs)
952 return bs->translation;
955 int bdrv_is_removable(BlockDriverState *bs)
957 return bs->removable;
960 int bdrv_is_read_only(BlockDriverState *bs)
962 return bs->read_only;
965 int bdrv_is_sg(BlockDriverState *bs)
967 return bs->sg;
970 /* XXX: no longer used */
971 void bdrv_set_change_cb(BlockDriverState *bs,
972 void (*change_cb)(void *opaque), void *opaque)
974 bs->change_cb = change_cb;
975 bs->change_opaque = opaque;
978 int bdrv_is_encrypted(BlockDriverState *bs)
980 if (bs->backing_hd && bs->backing_hd->encrypted)
981 return 1;
982 return bs->encrypted;
985 int bdrv_key_required(BlockDriverState *bs)
987 BlockDriverState *backing_hd = bs->backing_hd;
989 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
990 return 1;
991 return (bs->encrypted && !bs->valid_key);
994 int bdrv_set_key(BlockDriverState *bs, const char *key)
996 int ret;
997 if (bs->backing_hd && bs->backing_hd->encrypted) {
998 ret = bdrv_set_key(bs->backing_hd, key);
999 if (ret < 0)
1000 return ret;
1001 if (!bs->encrypted)
1002 return 0;
1004 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
1005 return -1;
1006 ret = bs->drv->bdrv_set_key(bs, key);
1007 bs->valid_key = (ret == 0);
1008 return ret;
1011 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1013 if (!bs->drv) {
1014 buf[0] = '\0';
1015 } else {
1016 pstrcpy(buf, buf_size, bs->drv->format_name);
1020 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1021 void *opaque)
1023 BlockDriver *drv;
1025 for (drv = first_drv; drv != NULL; drv = drv->next) {
1026 it(opaque, drv->format_name);
1030 BlockDriverState *bdrv_find(const char *name)
1032 BlockDriverState *bs;
1034 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1035 if (!strcmp(name, bs->device_name))
1036 return bs;
1038 return NULL;
1041 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1043 BlockDriverState *bs;
1045 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1046 it(opaque, bs);
1050 const char *bdrv_get_device_name(BlockDriverState *bs)
1052 return bs->device_name;
1055 void bdrv_flush(BlockDriverState *bs)
1057 if (bs->drv->bdrv_flush)
1058 bs->drv->bdrv_flush(bs);
1059 if (bs->backing_hd)
1060 bdrv_flush(bs->backing_hd);
1063 void bdrv_flush_all(void)
1065 BlockDriverState *bs;
1067 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1068 if (bs->drv && !bdrv_is_read_only(bs) &&
1069 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1070 bdrv_flush(bs);
1074 * Returns true iff the specified sector is present in the disk image. Drivers
1075 * not implementing the functionality are assumed to not support backing files,
1076 * hence all their sectors are reported as allocated.
1078 * 'pnum' is set to the number of sectors (including and immediately following
1079 * the specified sector) that are known to be in the same
1080 * allocated/unallocated state.
1082 * 'nb_sectors' is the max value 'pnum' should be set to.
1084 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1085 int *pnum)
1087 int64_t n;
1088 if (!bs->drv->bdrv_is_allocated) {
1089 if (sector_num >= bs->total_sectors) {
1090 *pnum = 0;
1091 return 0;
1093 n = bs->total_sectors - sector_num;
1094 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1095 return 1;
1097 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1100 void bdrv_info(void)
1102 BlockDriverState *bs;
1104 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1105 term_printf("%s:", bs->device_name);
1106 term_printf(" type=");
1107 switch(bs->type) {
1108 case BDRV_TYPE_HD:
1109 term_printf("hd");
1110 break;
1111 case BDRV_TYPE_CDROM:
1112 term_printf("cdrom");
1113 break;
1114 case BDRV_TYPE_FLOPPY:
1115 term_printf("floppy");
1116 break;
1118 term_printf(" removable=%d", bs->removable);
1119 if (bs->removable) {
1120 term_printf(" locked=%d", bs->locked);
1122 if (bs->drv) {
1123 term_printf(" file=");
1124 term_print_filename(bs->filename);
1125 if (bs->backing_file[0] != '\0') {
1126 term_printf(" backing_file=");
1127 term_print_filename(bs->backing_file);
1129 term_printf(" ro=%d", bs->read_only);
1130 term_printf(" drv=%s", bs->drv->format_name);
1131 term_printf(" encrypted=%d", bdrv_is_encrypted(bs));
1132 } else {
1133 term_printf(" [not inserted]");
1135 term_printf("\n");
1139 /* The "info blockstats" command. */
1140 void bdrv_info_stats (void)
1142 BlockDriverState *bs;
1144 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1145 term_printf ("%s:"
1146 " rd_bytes=%" PRIu64
1147 " wr_bytes=%" PRIu64
1148 " rd_operations=%" PRIu64
1149 " wr_operations=%" PRIu64
1150 "\n",
1151 bs->device_name,
1152 bs->rd_bytes, bs->wr_bytes,
1153 bs->rd_ops, bs->wr_ops);
1157 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1159 if (bs->backing_hd && bs->backing_hd->encrypted)
1160 return bs->backing_file;
1161 else if (bs->encrypted)
1162 return bs->filename;
1163 else
1164 return NULL;
1167 void bdrv_get_backing_filename(BlockDriverState *bs,
1168 char *filename, int filename_size)
1170 if (!bs->backing_hd) {
1171 pstrcpy(filename, filename_size, "");
1172 } else {
1173 pstrcpy(filename, filename_size, bs->backing_file);
1177 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1178 const uint8_t *buf, int nb_sectors)
1180 BlockDriver *drv = bs->drv;
1181 if (!drv)
1182 return -ENOMEDIUM;
1183 if (!drv->bdrv_write_compressed)
1184 return -ENOTSUP;
1185 if (bdrv_check_request(bs, sector_num, nb_sectors))
1186 return -EIO;
1187 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1190 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1192 BlockDriver *drv = bs->drv;
1193 if (!drv)
1194 return -ENOMEDIUM;
1195 if (!drv->bdrv_get_info)
1196 return -ENOTSUP;
1197 memset(bdi, 0, sizeof(*bdi));
1198 return drv->bdrv_get_info(bs, bdi);
1201 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1203 BlockDriver *drv = bs->drv;
1204 if (!drv)
1205 return -ENOMEDIUM;
1206 if (!drv->bdrv_put_buffer)
1207 return -ENOTSUP;
1208 return drv->bdrv_put_buffer(bs, buf, pos, size);
1211 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1213 BlockDriver *drv = bs->drv;
1214 if (!drv)
1215 return -ENOMEDIUM;
1216 if (!drv->bdrv_get_buffer)
1217 return -ENOTSUP;
1218 return drv->bdrv_get_buffer(bs, buf, pos, size);
1221 /**************************************************************/
1222 /* handling of snapshots */
1224 int bdrv_snapshot_create(BlockDriverState *bs,
1225 QEMUSnapshotInfo *sn_info)
1227 BlockDriver *drv = bs->drv;
1228 if (!drv)
1229 return -ENOMEDIUM;
1230 if (!drv->bdrv_snapshot_create)
1231 return -ENOTSUP;
1232 return drv->bdrv_snapshot_create(bs, sn_info);
1235 int bdrv_snapshot_goto(BlockDriverState *bs,
1236 const char *snapshot_id)
1238 BlockDriver *drv = bs->drv;
1239 if (!drv)
1240 return -ENOMEDIUM;
1241 if (!drv->bdrv_snapshot_goto)
1242 return -ENOTSUP;
1243 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1246 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1248 BlockDriver *drv = bs->drv;
1249 if (!drv)
1250 return -ENOMEDIUM;
1251 if (!drv->bdrv_snapshot_delete)
1252 return -ENOTSUP;
1253 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1256 int bdrv_snapshot_list(BlockDriverState *bs,
1257 QEMUSnapshotInfo **psn_info)
1259 BlockDriver *drv = bs->drv;
1260 if (!drv)
1261 return -ENOMEDIUM;
1262 if (!drv->bdrv_snapshot_list)
1263 return -ENOTSUP;
1264 return drv->bdrv_snapshot_list(bs, psn_info);
1267 #define NB_SUFFIXES 4
1269 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1271 static const char suffixes[NB_SUFFIXES] = "KMGT";
1272 int64_t base;
1273 int i;
1275 if (size <= 999) {
1276 snprintf(buf, buf_size, "%" PRId64, size);
1277 } else {
1278 base = 1024;
1279 for(i = 0; i < NB_SUFFIXES; i++) {
1280 if (size < (10 * base)) {
1281 snprintf(buf, buf_size, "%0.1f%c",
1282 (double)size / base,
1283 suffixes[i]);
1284 break;
1285 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1286 snprintf(buf, buf_size, "%" PRId64 "%c",
1287 ((size + (base >> 1)) / base),
1288 suffixes[i]);
1289 break;
1291 base = base * 1024;
1294 return buf;
1297 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1299 char buf1[128], date_buf[128], clock_buf[128];
1300 #ifdef _WIN32
1301 struct tm *ptm;
1302 #else
1303 struct tm tm;
1304 #endif
1305 time_t ti;
1306 int64_t secs;
1308 if (!sn) {
1309 snprintf(buf, buf_size,
1310 "%-10s%-20s%7s%20s%15s",
1311 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1312 } else {
1313 ti = sn->date_sec;
1314 #ifdef _WIN32
1315 ptm = localtime(&ti);
1316 strftime(date_buf, sizeof(date_buf),
1317 "%Y-%m-%d %H:%M:%S", ptm);
1318 #else
1319 localtime_r(&ti, &tm);
1320 strftime(date_buf, sizeof(date_buf),
1321 "%Y-%m-%d %H:%M:%S", &tm);
1322 #endif
1323 secs = sn->vm_clock_nsec / 1000000000;
1324 snprintf(clock_buf, sizeof(clock_buf),
1325 "%02d:%02d:%02d.%03d",
1326 (int)(secs / 3600),
1327 (int)((secs / 60) % 60),
1328 (int)(secs % 60),
1329 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1330 snprintf(buf, buf_size,
1331 "%-10s%-20s%7s%20s%15s",
1332 sn->id_str, sn->name,
1333 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1334 date_buf,
1335 clock_buf);
1337 return buf;
1341 /**************************************************************/
1342 /* async I/Os */
1344 typedef struct VectorTranslationAIOCB {
1345 BlockDriverAIOCB common;
1346 QEMUIOVector *iov;
1347 uint8_t *bounce;
1348 int is_write;
1349 BlockDriverAIOCB *aiocb;
1350 } VectorTranslationAIOCB;
1352 static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
1354 VectorTranslationAIOCB *acb
1355 = container_of(_acb, VectorTranslationAIOCB, common);
1357 bdrv_aio_cancel(acb->aiocb);
1360 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1362 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
1364 if (!s->is_write) {
1365 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1367 qemu_vfree(s->bounce);
1368 s->common.cb(s->common.opaque, ret);
1369 qemu_aio_release(s);
1372 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1373 int64_t sector_num,
1374 QEMUIOVector *iov,
1375 int nb_sectors,
1376 BlockDriverCompletionFunc *cb,
1377 void *opaque,
1378 int is_write)
1381 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1382 cb, opaque);
1384 s->iov = iov;
1385 s->bounce = qemu_memalign(512, nb_sectors * 512);
1386 s->is_write = is_write;
1387 if (is_write) {
1388 qemu_iovec_to_buffer(s->iov, s->bounce);
1389 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1390 bdrv_aio_rw_vector_cb, s);
1391 } else {
1392 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1393 bdrv_aio_rw_vector_cb, s);
1395 if (!s->aiocb) {
1396 qemu_vfree(s->bounce);
1397 qemu_aio_release(s);
1398 return NULL;
1400 return &s->common;
1403 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1404 QEMUIOVector *iov, int nb_sectors,
1405 BlockDriverCompletionFunc *cb, void *opaque)
1407 if (bdrv_check_request(bs, sector_num, nb_sectors))
1408 return NULL;
1410 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1411 cb, opaque, 0);
1414 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1415 QEMUIOVector *iov, int nb_sectors,
1416 BlockDriverCompletionFunc *cb, void *opaque)
1418 if (bdrv_check_request(bs, sector_num, nb_sectors))
1419 return NULL;
1421 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1422 cb, opaque, 1);
1425 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1426 uint8_t *buf, int nb_sectors,
1427 BlockDriverCompletionFunc *cb, void *opaque)
1429 BlockDriver *drv = bs->drv;
1430 BlockDriverAIOCB *ret;
1432 if (!drv)
1433 return NULL;
1434 if (bdrv_check_request(bs, sector_num, nb_sectors))
1435 return NULL;
1437 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1439 if (ret) {
1440 /* Update stats even though technically transfer has not happened. */
1441 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1442 bs->rd_ops ++;
1445 return ret;
1448 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1449 const uint8_t *buf, int nb_sectors,
1450 BlockDriverCompletionFunc *cb, void *opaque)
1452 BlockDriver *drv = bs->drv;
1453 BlockDriverAIOCB *ret;
1455 if (!drv)
1456 return NULL;
1457 if (bs->read_only)
1458 return NULL;
1459 if (bdrv_check_request(bs, sector_num, nb_sectors))
1460 return NULL;
1462 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1464 if (ret) {
1465 /* Update stats even though technically transfer has not happened. */
1466 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1467 bs->wr_ops ++;
1470 return ret;
1473 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1475 acb->pool->cancel(acb);
1479 /**************************************************************/
1480 /* async block device emulation */
1482 static void bdrv_aio_bh_cb(void *opaque)
1484 BlockDriverAIOCBSync *acb = opaque;
1485 acb->common.cb(acb->common.opaque, acb->ret);
1486 qemu_aio_release(acb);
1489 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1490 int64_t sector_num, uint8_t *buf, int nb_sectors,
1491 BlockDriverCompletionFunc *cb, void *opaque)
1493 BlockDriverAIOCBSync *acb;
1494 int ret;
1496 acb = qemu_aio_get(bs, cb, opaque);
1497 if (!acb->bh)
1498 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1499 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1500 acb->ret = ret;
1501 qemu_bh_schedule(acb->bh);
1502 return &acb->common;
1505 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1506 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1507 BlockDriverCompletionFunc *cb, void *opaque)
1509 BlockDriverAIOCBSync *acb;
1510 int ret;
1512 acb = qemu_aio_get(bs, cb, opaque);
1513 if (!acb->bh)
1514 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1515 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1516 acb->ret = ret;
1517 qemu_bh_schedule(acb->bh);
1518 return &acb->common;
1521 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1523 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1524 qemu_bh_cancel(acb->bh);
1525 qemu_aio_release(acb);
1528 /**************************************************************/
1529 /* sync block device emulation */
1531 static void bdrv_rw_em_cb(void *opaque, int ret)
1533 *(int *)opaque = ret;
1536 #define NOT_DONE 0x7fffffff
1538 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1539 uint8_t *buf, int nb_sectors)
1541 int async_ret;
1542 BlockDriverAIOCB *acb;
1544 async_ret = NOT_DONE;
1545 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1546 bdrv_rw_em_cb, &async_ret);
1547 if (acb == NULL)
1548 return -1;
1550 while (async_ret == NOT_DONE) {
1551 qemu_aio_wait();
1554 return async_ret;
1557 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1558 const uint8_t *buf, int nb_sectors)
1560 int async_ret;
1561 BlockDriverAIOCB *acb;
1563 async_ret = NOT_DONE;
1564 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1565 bdrv_rw_em_cb, &async_ret);
1566 if (acb == NULL)
1567 return -1;
1568 while (async_ret == NOT_DONE) {
1569 qemu_aio_wait();
1571 return async_ret;
1574 void bdrv_init(void)
1576 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
1577 bdrv_aio_cancel_vector);
1579 bdrv_register(&bdrv_raw);
1580 bdrv_register(&bdrv_host_device);
1581 #ifndef _WIN32
1582 bdrv_register(&bdrv_cow);
1583 #endif
1584 bdrv_register(&bdrv_qcow);
1585 bdrv_register(&bdrv_vmdk);
1586 bdrv_register(&bdrv_cloop);
1587 bdrv_register(&bdrv_dmg);
1588 bdrv_register(&bdrv_bochs);
1589 bdrv_register(&bdrv_vpc);
1590 bdrv_register(&bdrv_vvfat);
1591 bdrv_register(&bdrv_qcow2);
1592 bdrv_register(&bdrv_parallels);
1593 bdrv_register(&bdrv_nbd);
1596 void aio_pool_init(AIOPool *pool, int aiocb_size,
1597 void (*cancel)(BlockDriverAIOCB *acb))
1599 pool->aiocb_size = aiocb_size;
1600 pool->cancel = cancel;
1601 pool->free_aiocb = NULL;
1604 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1605 BlockDriverCompletionFunc *cb, void *opaque)
1607 BlockDriverAIOCB *acb;
1609 if (pool->free_aiocb) {
1610 acb = pool->free_aiocb;
1611 pool->free_aiocb = acb->next;
1612 } else {
1613 acb = qemu_mallocz(pool->aiocb_size);
1614 acb->pool = pool;
1616 acb->bs = bs;
1617 acb->cb = cb;
1618 acb->opaque = opaque;
1619 return acb;
1622 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1623 void *opaque)
1625 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1628 void qemu_aio_release(void *p)
1630 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1631 AIOPool *pool = acb->pool;
1632 acb->next = pool->free_aiocb;
1633 pool->free_aiocb = acb;
1636 /**************************************************************/
1637 /* removable device support */
1640 * Return TRUE if the media is present
1642 int bdrv_is_inserted(BlockDriverState *bs)
1644 BlockDriver *drv = bs->drv;
1645 int ret;
1646 if (!drv)
1647 return 0;
1648 if (!drv->bdrv_is_inserted)
1649 return 1;
1650 ret = drv->bdrv_is_inserted(bs);
1651 return ret;
1655 * Return TRUE if the media changed since the last call to this
1656 * function. It is currently only used for floppy disks
1658 int bdrv_media_changed(BlockDriverState *bs)
1660 BlockDriver *drv = bs->drv;
1661 int ret;
1663 if (!drv || !drv->bdrv_media_changed)
1664 ret = -ENOTSUP;
1665 else
1666 ret = drv->bdrv_media_changed(bs);
1667 if (ret == -ENOTSUP)
1668 ret = bs->media_changed;
1669 bs->media_changed = 0;
1670 return ret;
1674 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1676 int bdrv_eject(BlockDriverState *bs, int eject_flag)
1678 BlockDriver *drv = bs->drv;
1679 int ret;
1681 if (bs->locked) {
1682 return -EBUSY;
1685 if (!drv || !drv->bdrv_eject) {
1686 ret = -ENOTSUP;
1687 } else {
1688 ret = drv->bdrv_eject(bs, eject_flag);
1690 if (ret == -ENOTSUP) {
1691 if (eject_flag)
1692 bdrv_close(bs);
1693 ret = 0;
1696 return ret;
1699 int bdrv_is_locked(BlockDriverState *bs)
1701 return bs->locked;
1705 * Lock or unlock the media (if it is locked, the user won't be able
1706 * to eject it manually).
1708 void bdrv_set_locked(BlockDriverState *bs, int locked)
1710 BlockDriver *drv = bs->drv;
1712 bs->locked = locked;
1713 if (drv && drv->bdrv_set_locked) {
1714 drv->bdrv_set_locked(bs, locked);
1718 /* needed for generic scsi interface */
1720 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1722 BlockDriver *drv = bs->drv;
1724 if (drv && drv->bdrv_ioctl)
1725 return drv->bdrv_ioctl(bs, req, buf);
1726 return -ENOTSUP;