Merge commit 'qemu-svn/trunk'
[kvm-userspace.git] / qemu / block.c
blob657de8107501293bd143fd8872009e237ab16853
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 HOST_BSD
26 /* include native header before sys-queue.h */
27 #include <sys/queue.h>
28 #endif
30 #include "qemu-common.h"
31 #include "monitor.h"
32 #include "block_int.h"
33 #include "osdep.h"
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
44 #ifdef _WIN32
45 #include <windows.h>
46 #endif
48 #define SECTOR_BITS 9
49 #define SECTOR_SIZE (1 << SECTOR_BITS)
51 static AIOPool vectored_aio_pool;
53 typedef struct BlockDriverAIOCBSync {
54 BlockDriverAIOCB common;
55 QEMUBH *bh;
56 int ret;
57 } BlockDriverAIOCBSync;
59 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
60 int64_t sector_num, uint8_t *buf, int nb_sectors,
61 BlockDriverCompletionFunc *cb, void *opaque);
62 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
63 int64_t sector_num, const uint8_t *buf, int nb_sectors,
64 BlockDriverCompletionFunc *cb, void *opaque);
65 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
66 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
67 uint8_t *buf, int nb_sectors);
68 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
69 const uint8_t *buf, int nb_sectors);
71 BlockDriverState *bdrv_first;
73 static BlockDriver *first_drv;
75 int path_is_absolute(const char *path)
77 const char *p;
78 #ifdef _WIN32
79 /* specific case for names like: "\\.\d:" */
80 if (*path == '/' || *path == '\\')
81 return 1;
82 #endif
83 p = strchr(path, ':');
84 if (p)
85 p++;
86 else
87 p = path;
88 #ifdef _WIN32
89 return (*p == '/' || *p == '\\');
90 #else
91 return (*p == '/');
92 #endif
95 /* if filename is absolute, just copy it to dest. Otherwise, build a
96 path to it by considering it is relative to base_path. URL are
97 supported. */
98 void path_combine(char *dest, int dest_size,
99 const char *base_path,
100 const char *filename)
102 const char *p, *p1;
103 int len;
105 if (dest_size <= 0)
106 return;
107 if (path_is_absolute(filename)) {
108 pstrcpy(dest, dest_size, filename);
109 } else {
110 p = strchr(base_path, ':');
111 if (p)
112 p++;
113 else
114 p = base_path;
115 p1 = strrchr(base_path, '/');
116 #ifdef _WIN32
118 const char *p2;
119 p2 = strrchr(base_path, '\\');
120 if (!p1 || p2 > p1)
121 p1 = p2;
123 #endif
124 if (p1)
125 p1++;
126 else
127 p1 = base_path;
128 if (p1 > p)
129 p = p1;
130 len = p - base_path;
131 if (len > dest_size - 1)
132 len = dest_size - 1;
133 memcpy(dest, base_path, len);
134 dest[len] = '\0';
135 pstrcat(dest, dest_size, filename);
140 static void bdrv_register(BlockDriver *bdrv)
142 if (!bdrv->bdrv_aio_read) {
143 /* add AIO emulation layer */
144 bdrv->bdrv_aio_read = bdrv_aio_read_em;
145 bdrv->bdrv_aio_write = bdrv_aio_write_em;
146 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
147 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
148 } else if (!bdrv->bdrv_read) {
149 /* add synchronous IO emulation layer */
150 bdrv->bdrv_read = bdrv_read_em;
151 bdrv->bdrv_write = bdrv_write_em;
153 aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
154 bdrv->next = first_drv;
155 first_drv = bdrv;
158 /* create a new block device (by default it is empty) */
159 BlockDriverState *bdrv_new(const char *device_name)
161 BlockDriverState **pbs, *bs;
163 bs = qemu_mallocz(sizeof(BlockDriverState));
164 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
165 if (device_name[0] != '\0') {
166 /* insert at the end */
167 pbs = &bdrv_first;
168 while (*pbs != NULL)
169 pbs = &(*pbs)->next;
170 *pbs = bs;
172 return bs;
175 BlockDriver *bdrv_find_format(const char *format_name)
177 BlockDriver *drv1;
178 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
179 if (!strcmp(drv1->format_name, format_name))
180 return drv1;
182 return NULL;
185 int bdrv_create2(BlockDriver *drv,
186 const char *filename, int64_t size_in_sectors,
187 const char *backing_file, const char *backing_format,
188 int flags)
190 if (drv->bdrv_create2)
191 return drv->bdrv_create2(filename, size_in_sectors, backing_file,
192 backing_format, flags);
193 if (drv->bdrv_create)
194 return drv->bdrv_create(filename, size_in_sectors, backing_file,
195 flags);
196 return -ENOTSUP;
199 int bdrv_create(BlockDriver *drv,
200 const char *filename, int64_t size_in_sectors,
201 const char *backing_file, int flags)
203 if (!drv->bdrv_create)
204 return -ENOTSUP;
205 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
208 #ifdef _WIN32
209 void get_tmp_filename(char *filename, int size)
211 char temp_dir[MAX_PATH];
213 GetTempPath(MAX_PATH, temp_dir);
214 GetTempFileName(temp_dir, "qem", 0, filename);
216 #else
217 void get_tmp_filename(char *filename, int size)
219 int fd;
220 const char *tmpdir;
221 /* XXX: race condition possible */
222 tmpdir = getenv("TMPDIR");
223 if (!tmpdir)
224 tmpdir = "/tmp";
225 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
226 fd = mkstemp(filename);
227 close(fd);
229 #endif
231 #ifdef _WIN32
232 static int is_windows_drive_prefix(const char *filename)
234 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
235 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
236 filename[1] == ':');
239 static int is_windows_drive(const char *filename)
241 if (is_windows_drive_prefix(filename) &&
242 filename[2] == '\0')
243 return 1;
244 if (strstart(filename, "\\\\.\\", NULL) ||
245 strstart(filename, "//./", NULL))
246 return 1;
247 return 0;
249 #endif
251 static BlockDriver *find_protocol(const char *filename)
253 BlockDriver *drv1;
254 char protocol[128];
255 int len;
256 const char *p;
258 #ifdef _WIN32
259 if (is_windows_drive(filename) ||
260 is_windows_drive_prefix(filename))
261 return &bdrv_raw;
262 #endif
263 p = strchr(filename, ':');
264 if (!p)
265 return &bdrv_raw;
266 len = p - filename;
267 if (len > sizeof(protocol) - 1)
268 len = sizeof(protocol) - 1;
269 memcpy(protocol, filename, len);
270 protocol[len] = '\0';
271 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
272 if (drv1->protocol_name &&
273 !strcmp(drv1->protocol_name, protocol))
274 return drv1;
276 return NULL;
279 /* XXX: force raw format if block or character device ? It would
280 simplify the BSD case */
281 static BlockDriver *find_image_format(const char *filename)
283 int ret, score, score_max;
284 BlockDriver *drv1, *drv;
285 uint8_t buf[2048];
286 BlockDriverState *bs;
288 /* detect host devices. By convention, /dev/cdrom[N] is always
289 recognized as a host CDROM */
290 if (strstart(filename, "/dev/cdrom", NULL))
291 return &bdrv_host_device;
292 #ifdef _WIN32
293 if (is_windows_drive(filename))
294 return &bdrv_host_device;
295 #else
297 struct stat st;
298 if (stat(filename, &st) >= 0 &&
299 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
300 return &bdrv_host_device;
303 #endif
305 drv = find_protocol(filename);
306 /* no need to test disk image formats for vvfat */
307 if (drv == &bdrv_vvfat)
308 return drv;
310 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
311 if (ret < 0)
312 return NULL;
313 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
314 bdrv_delete(bs);
315 if (ret < 0) {
316 return NULL;
319 score_max = 0;
320 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
321 if (drv1->bdrv_probe) {
322 score = drv1->bdrv_probe(buf, ret, filename);
323 if (score > score_max) {
324 score_max = score;
325 drv = drv1;
329 return drv;
332 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
334 BlockDriverState *bs;
335 int ret;
337 bs = bdrv_new("");
338 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
339 if (ret < 0) {
340 bdrv_delete(bs);
341 return ret;
343 bs->growable = 1;
344 *pbs = bs;
345 return 0;
348 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
350 return bdrv_open2(bs, filename, flags, NULL);
353 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
354 BlockDriver *drv)
356 int ret, open_flags;
357 char tmp_filename[PATH_MAX];
358 char backing_filename[PATH_MAX];
360 bs->read_only = 0;
361 bs->is_temporary = 0;
362 bs->encrypted = 0;
363 bs->valid_key = 0;
365 if (flags & BDRV_O_SNAPSHOT) {
366 BlockDriverState *bs1;
367 int64_t total_size;
368 int is_protocol = 0;
370 /* if snapshot, we create a temporary backing file and open it
371 instead of opening 'filename' directly */
373 /* if there is a backing file, use it */
374 bs1 = bdrv_new("");
375 ret = bdrv_open2(bs1, filename, 0, drv);
376 if (ret < 0) {
377 bdrv_delete(bs1);
378 return ret;
380 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
382 if (bs1->drv && bs1->drv->protocol_name)
383 is_protocol = 1;
385 bdrv_delete(bs1);
387 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
389 /* Real path is meaningless for protocols */
390 if (is_protocol)
391 snprintf(backing_filename, sizeof(backing_filename),
392 "%s", filename);
393 else
394 realpath(filename, backing_filename);
396 ret = bdrv_create2(&bdrv_qcow2, tmp_filename,
397 total_size, backing_filename,
398 (drv ? drv->format_name : NULL), 0);
399 if (ret < 0) {
400 return ret;
402 filename = tmp_filename;
403 drv = &bdrv_qcow2;
404 bs->is_temporary = 1;
407 pstrcpy(bs->filename, sizeof(bs->filename), filename);
408 if (flags & BDRV_O_FILE) {
409 drv = find_protocol(filename);
410 } else if (!drv) {
411 drv = find_image_format(filename);
413 if (!drv) {
414 ret = -ENOENT;
415 goto unlink_and_fail;
417 bs->drv = drv;
418 bs->opaque = qemu_mallocz(drv->instance_size);
419 /* Note: for compatibility, we open disk image files as RDWR, and
420 RDONLY as fallback */
421 if (!(flags & BDRV_O_FILE))
422 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
423 else
424 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
425 ret = drv->bdrv_open(bs, filename, open_flags);
426 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
427 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
428 bs->read_only = 1;
430 if (ret < 0) {
431 qemu_free(bs->opaque);
432 bs->opaque = NULL;
433 bs->drv = NULL;
434 unlink_and_fail:
435 if (bs->is_temporary)
436 unlink(filename);
437 return ret;
439 if (drv->bdrv_getlength) {
440 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
442 #ifndef _WIN32
443 if (bs->is_temporary) {
444 unlink(filename);
446 #endif
447 if (bs->backing_file[0] != '\0') {
448 /* if there is a backing file, use it */
449 BlockDriver *back_drv = NULL;
450 bs->backing_hd = bdrv_new("");
451 path_combine(backing_filename, sizeof(backing_filename),
452 filename, bs->backing_file);
453 if (bs->backing_format[0] != '\0')
454 back_drv = bdrv_find_format(bs->backing_format);
455 ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
456 back_drv);
457 if (ret < 0) {
458 bdrv_close(bs);
459 return ret;
463 if (!bdrv_key_required(bs)) {
464 /* call the change callback */
465 bs->media_changed = 1;
466 if (bs->change_cb)
467 bs->change_cb(bs->change_opaque);
469 return 0;
472 void bdrv_close(BlockDriverState *bs)
474 if (bs->drv) {
475 if (bs->backing_hd)
476 bdrv_delete(bs->backing_hd);
477 bs->drv->bdrv_close(bs);
478 qemu_free(bs->opaque);
479 #ifdef _WIN32
480 if (bs->is_temporary) {
481 unlink(bs->filename);
483 #endif
484 bs->opaque = NULL;
485 bs->drv = NULL;
487 /* call the change callback */
488 bs->media_changed = 1;
489 if (bs->change_cb)
490 bs->change_cb(bs->change_opaque);
494 void bdrv_delete(BlockDriverState *bs)
496 BlockDriverState **pbs;
498 pbs = &bdrv_first;
499 while (*pbs != bs && *pbs != NULL)
500 pbs = &(*pbs)->next;
501 if (*pbs == bs)
502 *pbs = bs->next;
504 bdrv_close(bs);
505 qemu_free(bs);
508 /* commit COW file into the raw image */
509 int bdrv_commit(BlockDriverState *bs)
511 BlockDriver *drv = bs->drv;
512 int64_t i, total_sectors;
513 int n, j;
514 unsigned char sector[512];
516 if (!drv)
517 return -ENOMEDIUM;
519 if (bs->read_only) {
520 return -EACCES;
523 if (!bs->backing_hd) {
524 return -ENOTSUP;
527 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
528 for (i = 0; i < total_sectors;) {
529 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
530 for(j = 0; j < n; j++) {
531 if (bdrv_read(bs, i, sector, 1) != 0) {
532 return -EIO;
535 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
536 return -EIO;
538 i++;
540 } else {
541 i += n;
545 if (drv->bdrv_make_empty)
546 return drv->bdrv_make_empty(bs);
548 return 0;
551 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
552 size_t size)
554 int64_t len;
556 if (!bdrv_is_inserted(bs))
557 return -ENOMEDIUM;
559 if (bs->growable)
560 return 0;
562 len = bdrv_getlength(bs);
564 if ((offset + size) > len)
565 return -EIO;
567 return 0;
570 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
571 int nb_sectors)
573 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
576 /* return < 0 if error. See bdrv_write() for the return codes */
577 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
578 uint8_t *buf, int nb_sectors)
580 BlockDriver *drv = bs->drv;
582 if (!drv)
583 return -ENOMEDIUM;
584 if (bdrv_check_request(bs, sector_num, nb_sectors))
585 return -EIO;
587 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
590 /* Return < 0 if error. Important errors are:
591 -EIO generic I/O error (may happen for all errors)
592 -ENOMEDIUM No media inserted.
593 -EINVAL Invalid sector number or nb_sectors
594 -EACCES Trying to write a read-only device
596 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
597 const uint8_t *buf, int nb_sectors)
599 BlockDriver *drv = bs->drv;
600 if (!bs->drv)
601 return -ENOMEDIUM;
602 if (bs->read_only)
603 return -EACCES;
604 if (bdrv_check_request(bs, sector_num, nb_sectors))
605 return -EIO;
607 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
610 int bdrv_pread(BlockDriverState *bs, int64_t offset,
611 void *buf, int count1)
613 uint8_t tmp_buf[SECTOR_SIZE];
614 int len, nb_sectors, count;
615 int64_t sector_num;
617 count = count1;
618 /* first read to align to sector start */
619 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
620 if (len > count)
621 len = count;
622 sector_num = offset >> SECTOR_BITS;
623 if (len > 0) {
624 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
625 return -EIO;
626 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
627 count -= len;
628 if (count == 0)
629 return count1;
630 sector_num++;
631 buf += len;
634 /* read the sectors "in place" */
635 nb_sectors = count >> SECTOR_BITS;
636 if (nb_sectors > 0) {
637 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
638 return -EIO;
639 sector_num += nb_sectors;
640 len = nb_sectors << SECTOR_BITS;
641 buf += len;
642 count -= len;
645 /* add data from the last sector */
646 if (count > 0) {
647 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
648 return -EIO;
649 memcpy(buf, tmp_buf, count);
651 return count1;
654 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
655 const void *buf, int count1)
657 uint8_t tmp_buf[SECTOR_SIZE];
658 int len, nb_sectors, count;
659 int64_t sector_num;
661 count = count1;
662 /* first write to align to sector start */
663 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
664 if (len > count)
665 len = count;
666 sector_num = offset >> SECTOR_BITS;
667 if (len > 0) {
668 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
669 return -EIO;
670 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
671 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
672 return -EIO;
673 count -= len;
674 if (count == 0)
675 return count1;
676 sector_num++;
677 buf += len;
680 /* write the sectors "in place" */
681 nb_sectors = count >> SECTOR_BITS;
682 if (nb_sectors > 0) {
683 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
684 return -EIO;
685 sector_num += nb_sectors;
686 len = nb_sectors << SECTOR_BITS;
687 buf += len;
688 count -= len;
691 /* add data from the last sector */
692 if (count > 0) {
693 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
694 return -EIO;
695 memcpy(tmp_buf, buf, count);
696 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
697 return -EIO;
699 return count1;
703 * Truncate file to 'offset' bytes (needed only for file protocols)
705 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
707 BlockDriver *drv = bs->drv;
708 if (!drv)
709 return -ENOMEDIUM;
710 if (!drv->bdrv_truncate)
711 return -ENOTSUP;
712 return drv->bdrv_truncate(bs, offset);
716 * Length of a file in bytes. Return < 0 if error or unknown.
718 int64_t bdrv_getlength(BlockDriverState *bs)
720 BlockDriver *drv = bs->drv;
721 if (!drv)
722 return -ENOMEDIUM;
723 if (!drv->bdrv_getlength) {
724 /* legacy mode */
725 return bs->total_sectors * SECTOR_SIZE;
727 return drv->bdrv_getlength(bs);
730 /* return 0 as number of sectors if no device present or error */
731 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
733 int64_t length;
734 length = bdrv_getlength(bs);
735 if (length < 0)
736 length = 0;
737 else
738 length = length >> SECTOR_BITS;
739 *nb_sectors_ptr = length;
742 struct partition {
743 uint8_t boot_ind; /* 0x80 - active */
744 uint8_t head; /* starting head */
745 uint8_t sector; /* starting sector */
746 uint8_t cyl; /* starting cylinder */
747 uint8_t sys_ind; /* What partition type */
748 uint8_t end_head; /* end head */
749 uint8_t end_sector; /* end sector */
750 uint8_t end_cyl; /* end cylinder */
751 uint32_t start_sect; /* starting sector counting from 0 */
752 uint32_t nr_sects; /* nr of sectors in partition */
753 } __attribute__((packed));
755 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
756 static int guess_disk_lchs(BlockDriverState *bs,
757 int *pcylinders, int *pheads, int *psectors)
759 uint8_t *buf;
760 int ret, i, heads, sectors, cylinders;
761 struct partition *p;
762 uint32_t nr_sects;
763 uint64_t nb_sectors;
765 buf = qemu_memalign(512, 512);
766 if (buf == NULL)
767 return -1;
769 bdrv_get_geometry(bs, &nb_sectors);
771 ret = bdrv_read(bs, 0, buf, 1);
772 if (ret < 0)
773 return -1;
774 /* test msdos magic */
775 if (buf[510] != 0x55 || buf[511] != 0xaa) {
776 qemu_free(buf);
777 return -1;
779 for(i = 0; i < 4; i++) {
780 p = ((struct partition *)(buf + 0x1be)) + i;
781 nr_sects = le32_to_cpu(p->nr_sects);
782 if (nr_sects && p->end_head) {
783 /* We make the assumption that the partition terminates on
784 a cylinder boundary */
785 heads = p->end_head + 1;
786 sectors = p->end_sector & 63;
787 if (sectors == 0)
788 continue;
789 cylinders = nb_sectors / (heads * sectors);
790 if (cylinders < 1 || cylinders > 16383)
791 continue;
792 *pheads = heads;
793 *psectors = sectors;
794 *pcylinders = cylinders;
795 #if 0
796 printf("guessed geometry: LCHS=%d %d %d\n",
797 cylinders, heads, sectors);
798 #endif
799 qemu_free(buf);
800 return 0;
803 qemu_free(buf);
804 return -1;
807 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
809 int translation, lba_detected = 0;
810 int cylinders, heads, secs;
811 uint64_t nb_sectors;
813 /* if a geometry hint is available, use it */
814 bdrv_get_geometry(bs, &nb_sectors);
815 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
816 translation = bdrv_get_translation_hint(bs);
817 if (cylinders != 0) {
818 *pcyls = cylinders;
819 *pheads = heads;
820 *psecs = secs;
821 } else {
822 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
823 if (heads > 16) {
824 /* if heads > 16, it means that a BIOS LBA
825 translation was active, so the default
826 hardware geometry is OK */
827 lba_detected = 1;
828 goto default_geometry;
829 } else {
830 *pcyls = cylinders;
831 *pheads = heads;
832 *psecs = secs;
833 /* disable any translation to be in sync with
834 the logical geometry */
835 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
836 bdrv_set_translation_hint(bs,
837 BIOS_ATA_TRANSLATION_NONE);
840 } else {
841 default_geometry:
842 /* if no geometry, use a standard physical disk geometry */
843 cylinders = nb_sectors / (16 * 63);
845 if (cylinders > 16383)
846 cylinders = 16383;
847 else if (cylinders < 2)
848 cylinders = 2;
849 *pcyls = cylinders;
850 *pheads = 16;
851 *psecs = 63;
852 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
853 if ((*pcyls * *pheads) <= 131072) {
854 bdrv_set_translation_hint(bs,
855 BIOS_ATA_TRANSLATION_LARGE);
856 } else {
857 bdrv_set_translation_hint(bs,
858 BIOS_ATA_TRANSLATION_LBA);
862 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
866 void bdrv_set_geometry_hint(BlockDriverState *bs,
867 int cyls, int heads, int secs)
869 bs->cyls = cyls;
870 bs->heads = heads;
871 bs->secs = secs;
874 void bdrv_set_type_hint(BlockDriverState *bs, int type)
876 bs->type = type;
877 bs->removable = ((type == BDRV_TYPE_CDROM ||
878 type == BDRV_TYPE_FLOPPY));
881 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
883 bs->translation = translation;
886 void bdrv_get_geometry_hint(BlockDriverState *bs,
887 int *pcyls, int *pheads, int *psecs)
889 *pcyls = bs->cyls;
890 *pheads = bs->heads;
891 *psecs = bs->secs;
894 int bdrv_get_type_hint(BlockDriverState *bs)
896 return bs->type;
899 int bdrv_get_translation_hint(BlockDriverState *bs)
901 return bs->translation;
904 int bdrv_is_removable(BlockDriverState *bs)
906 return bs->removable;
909 int bdrv_is_read_only(BlockDriverState *bs)
911 return bs->read_only;
914 int bdrv_is_sg(BlockDriverState *bs)
916 return bs->sg;
919 /* XXX: no longer used */
920 void bdrv_set_change_cb(BlockDriverState *bs,
921 void (*change_cb)(void *opaque), void *opaque)
923 bs->change_cb = change_cb;
924 bs->change_opaque = opaque;
927 int bdrv_is_encrypted(BlockDriverState *bs)
929 if (bs->backing_hd && bs->backing_hd->encrypted)
930 return 1;
931 return bs->encrypted;
934 int bdrv_key_required(BlockDriverState *bs)
936 BlockDriverState *backing_hd = bs->backing_hd;
938 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
939 return 1;
940 return (bs->encrypted && !bs->valid_key);
943 int bdrv_set_key(BlockDriverState *bs, const char *key)
945 int ret;
946 if (bs->backing_hd && bs->backing_hd->encrypted) {
947 ret = bdrv_set_key(bs->backing_hd, key);
948 if (ret < 0)
949 return ret;
950 if (!bs->encrypted)
951 return 0;
953 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
954 return -1;
955 ret = bs->drv->bdrv_set_key(bs, key);
956 if (ret < 0) {
957 bs->valid_key = 0;
958 } else if (!bs->valid_key) {
959 bs->valid_key = 1;
960 /* call the change callback now, we skipped it on open */
961 bs->media_changed = 1;
962 if (bs->change_cb)
963 bs->change_cb(bs->change_opaque);
965 return ret;
968 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
970 if (!bs->drv) {
971 buf[0] = '\0';
972 } else {
973 pstrcpy(buf, buf_size, bs->drv->format_name);
977 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
978 void *opaque)
980 BlockDriver *drv;
982 for (drv = first_drv; drv != NULL; drv = drv->next) {
983 it(opaque, drv->format_name);
987 BlockDriverState *bdrv_find(const char *name)
989 BlockDriverState *bs;
991 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
992 if (!strcmp(name, bs->device_name))
993 return bs;
995 return NULL;
998 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1000 BlockDriverState *bs;
1002 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1003 it(opaque, bs);
1007 const char *bdrv_get_device_name(BlockDriverState *bs)
1009 return bs->device_name;
1012 void bdrv_flush(BlockDriverState *bs)
1014 if (!bs->drv)
1015 return;
1016 if (bs->drv->bdrv_flush)
1017 bs->drv->bdrv_flush(bs);
1018 if (bs->backing_hd)
1019 bdrv_flush(bs->backing_hd);
1022 void bdrv_flush_all(void)
1024 BlockDriverState *bs;
1026 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1027 if (bs->drv && !bdrv_is_read_only(bs) &&
1028 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1029 bdrv_flush(bs);
1033 * Returns true iff the specified sector is present in the disk image. Drivers
1034 * not implementing the functionality are assumed to not support backing files,
1035 * hence all their sectors are reported as allocated.
1037 * 'pnum' is set to the number of sectors (including and immediately following
1038 * the specified sector) that are known to be in the same
1039 * allocated/unallocated state.
1041 * 'nb_sectors' is the max value 'pnum' should be set to.
1043 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1044 int *pnum)
1046 int64_t n;
1047 if (!bs->drv->bdrv_is_allocated) {
1048 if (sector_num >= bs->total_sectors) {
1049 *pnum = 0;
1050 return 0;
1052 n = bs->total_sectors - sector_num;
1053 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1054 return 1;
1056 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1059 void bdrv_info(Monitor *mon)
1061 BlockDriverState *bs;
1063 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1064 monitor_printf(mon, "%s:", bs->device_name);
1065 monitor_printf(mon, " type=");
1066 switch(bs->type) {
1067 case BDRV_TYPE_HD:
1068 monitor_printf(mon, "hd");
1069 break;
1070 case BDRV_TYPE_CDROM:
1071 monitor_printf(mon, "cdrom");
1072 break;
1073 case BDRV_TYPE_FLOPPY:
1074 monitor_printf(mon, "floppy");
1075 break;
1077 monitor_printf(mon, " removable=%d", bs->removable);
1078 if (bs->removable) {
1079 monitor_printf(mon, " locked=%d", bs->locked);
1081 if (bs->drv) {
1082 monitor_printf(mon, " file=");
1083 monitor_print_filename(mon, bs->filename);
1084 if (bs->backing_file[0] != '\0') {
1085 monitor_printf(mon, " backing_file=");
1086 monitor_print_filename(mon, bs->backing_file);
1088 monitor_printf(mon, " ro=%d", bs->read_only);
1089 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1090 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1091 } else {
1092 monitor_printf(mon, " [not inserted]");
1094 monitor_printf(mon, "\n");
1098 /* The "info blockstats" command. */
1099 void bdrv_info_stats(Monitor *mon)
1101 BlockDriverState *bs;
1103 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1104 monitor_printf(mon, "%s:"
1105 " rd_bytes=%" PRIu64
1106 " wr_bytes=%" PRIu64
1107 " rd_operations=%" PRIu64
1108 " wr_operations=%" PRIu64
1109 "\n",
1110 bs->device_name,
1111 bs->rd_bytes, bs->wr_bytes,
1112 bs->rd_ops, bs->wr_ops);
1116 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1118 if (bs->backing_hd && bs->backing_hd->encrypted)
1119 return bs->backing_file;
1120 else if (bs->encrypted)
1121 return bs->filename;
1122 else
1123 return NULL;
1126 void bdrv_get_backing_filename(BlockDriverState *bs,
1127 char *filename, int filename_size)
1129 if (!bs->backing_hd) {
1130 pstrcpy(filename, filename_size, "");
1131 } else {
1132 pstrcpy(filename, filename_size, bs->backing_file);
1136 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1137 const uint8_t *buf, int nb_sectors)
1139 BlockDriver *drv = bs->drv;
1140 if (!drv)
1141 return -ENOMEDIUM;
1142 if (!drv->bdrv_write_compressed)
1143 return -ENOTSUP;
1144 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1147 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1149 BlockDriver *drv = bs->drv;
1150 if (!drv)
1151 return -ENOMEDIUM;
1152 if (!drv->bdrv_get_info)
1153 return -ENOTSUP;
1154 memset(bdi, 0, sizeof(*bdi));
1155 return drv->bdrv_get_info(bs, bdi);
1158 int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1160 BlockDriver *drv = bs->drv;
1161 if (!drv)
1162 return -ENOMEDIUM;
1163 if (!drv->bdrv_put_buffer)
1164 return -ENOTSUP;
1165 return drv->bdrv_put_buffer(bs, buf, pos, size);
1168 int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1170 BlockDriver *drv = bs->drv;
1171 if (!drv)
1172 return -ENOMEDIUM;
1173 if (!drv->bdrv_get_buffer)
1174 return -ENOTSUP;
1175 return drv->bdrv_get_buffer(bs, buf, pos, size);
1178 /**************************************************************/
1179 /* handling of snapshots */
1181 int bdrv_snapshot_create(BlockDriverState *bs,
1182 QEMUSnapshotInfo *sn_info)
1184 BlockDriver *drv = bs->drv;
1185 if (!drv)
1186 return -ENOMEDIUM;
1187 if (!drv->bdrv_snapshot_create)
1188 return -ENOTSUP;
1189 return drv->bdrv_snapshot_create(bs, sn_info);
1192 int bdrv_snapshot_goto(BlockDriverState *bs,
1193 const char *snapshot_id)
1195 BlockDriver *drv = bs->drv;
1196 if (!drv)
1197 return -ENOMEDIUM;
1198 if (!drv->bdrv_snapshot_goto)
1199 return -ENOTSUP;
1200 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1203 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1205 BlockDriver *drv = bs->drv;
1206 if (!drv)
1207 return -ENOMEDIUM;
1208 if (!drv->bdrv_snapshot_delete)
1209 return -ENOTSUP;
1210 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1213 int bdrv_snapshot_list(BlockDriverState *bs,
1214 QEMUSnapshotInfo **psn_info)
1216 BlockDriver *drv = bs->drv;
1217 if (!drv)
1218 return -ENOMEDIUM;
1219 if (!drv->bdrv_snapshot_list)
1220 return -ENOTSUP;
1221 return drv->bdrv_snapshot_list(bs, psn_info);
1224 #define NB_SUFFIXES 4
1226 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1228 static const char suffixes[NB_SUFFIXES] = "KMGT";
1229 int64_t base;
1230 int i;
1232 if (size <= 999) {
1233 snprintf(buf, buf_size, "%" PRId64, size);
1234 } else {
1235 base = 1024;
1236 for(i = 0; i < NB_SUFFIXES; i++) {
1237 if (size < (10 * base)) {
1238 snprintf(buf, buf_size, "%0.1f%c",
1239 (double)size / base,
1240 suffixes[i]);
1241 break;
1242 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1243 snprintf(buf, buf_size, "%" PRId64 "%c",
1244 ((size + (base >> 1)) / base),
1245 suffixes[i]);
1246 break;
1248 base = base * 1024;
1251 return buf;
1254 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1256 char buf1[128], date_buf[128], clock_buf[128];
1257 #ifdef _WIN32
1258 struct tm *ptm;
1259 #else
1260 struct tm tm;
1261 #endif
1262 time_t ti;
1263 int64_t secs;
1265 if (!sn) {
1266 snprintf(buf, buf_size,
1267 "%-10s%-20s%7s%20s%15s",
1268 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1269 } else {
1270 ti = sn->date_sec;
1271 #ifdef _WIN32
1272 ptm = localtime(&ti);
1273 strftime(date_buf, sizeof(date_buf),
1274 "%Y-%m-%d %H:%M:%S", ptm);
1275 #else
1276 localtime_r(&ti, &tm);
1277 strftime(date_buf, sizeof(date_buf),
1278 "%Y-%m-%d %H:%M:%S", &tm);
1279 #endif
1280 secs = sn->vm_clock_nsec / 1000000000;
1281 snprintf(clock_buf, sizeof(clock_buf),
1282 "%02d:%02d:%02d.%03d",
1283 (int)(secs / 3600),
1284 (int)((secs / 60) % 60),
1285 (int)(secs % 60),
1286 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1287 snprintf(buf, buf_size,
1288 "%-10s%-20s%7s%20s%15s",
1289 sn->id_str, sn->name,
1290 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1291 date_buf,
1292 clock_buf);
1294 return buf;
1298 /**************************************************************/
1299 /* async I/Os */
1301 typedef struct VectorTranslationAIOCB {
1302 BlockDriverAIOCB common;
1303 QEMUIOVector *iov;
1304 uint8_t *bounce;
1305 int is_write;
1306 BlockDriverAIOCB *aiocb;
1307 } VectorTranslationAIOCB;
1309 static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
1311 VectorTranslationAIOCB *acb
1312 = container_of(_acb, VectorTranslationAIOCB, common);
1314 bdrv_aio_cancel(acb->aiocb);
1317 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1319 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
1321 if (!s->is_write) {
1322 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1324 qemu_vfree(s->bounce);
1325 s->common.cb(s->common.opaque, ret);
1326 qemu_aio_release(s);
1329 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1330 int64_t sector_num,
1331 QEMUIOVector *iov,
1332 int nb_sectors,
1333 BlockDriverCompletionFunc *cb,
1334 void *opaque,
1335 int is_write)
1338 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1339 cb, opaque);
1341 s->iov = iov;
1342 s->bounce = qemu_memalign(512, nb_sectors * 512);
1343 s->is_write = is_write;
1344 if (is_write) {
1345 qemu_iovec_to_buffer(s->iov, s->bounce);
1346 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1347 bdrv_aio_rw_vector_cb, s);
1348 } else {
1349 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1350 bdrv_aio_rw_vector_cb, s);
1352 if (!s->aiocb) {
1353 qemu_vfree(s->bounce);
1354 qemu_aio_release(s);
1355 return NULL;
1357 return &s->common;
1360 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1361 QEMUIOVector *iov, int nb_sectors,
1362 BlockDriverCompletionFunc *cb, void *opaque)
1364 if (bdrv_check_request(bs, sector_num, nb_sectors))
1365 return NULL;
1367 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1368 cb, opaque, 0);
1371 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1372 QEMUIOVector *iov, int nb_sectors,
1373 BlockDriverCompletionFunc *cb, void *opaque)
1375 if (bdrv_check_request(bs, sector_num, nb_sectors))
1376 return NULL;
1378 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1379 cb, opaque, 1);
1382 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1383 uint8_t *buf, int nb_sectors,
1384 BlockDriverCompletionFunc *cb, void *opaque)
1386 BlockDriver *drv = bs->drv;
1387 BlockDriverAIOCB *ret;
1389 if (!drv)
1390 return NULL;
1391 if (bdrv_check_request(bs, sector_num, nb_sectors))
1392 return NULL;
1394 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1396 if (ret) {
1397 /* Update stats even though technically transfer has not happened. */
1398 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1399 bs->rd_ops ++;
1402 return ret;
1405 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1406 const uint8_t *buf, int nb_sectors,
1407 BlockDriverCompletionFunc *cb, void *opaque)
1409 BlockDriver *drv = bs->drv;
1410 BlockDriverAIOCB *ret;
1412 if (!drv)
1413 return NULL;
1414 if (bs->read_only)
1415 return NULL;
1416 if (bdrv_check_request(bs, sector_num, nb_sectors))
1417 return NULL;
1419 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1421 if (ret) {
1422 /* Update stats even though technically transfer has not happened. */
1423 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1424 bs->wr_ops ++;
1427 return ret;
1430 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1432 acb->pool->cancel(acb);
1436 /**************************************************************/
1437 /* async block device emulation */
1439 static void bdrv_aio_bh_cb(void *opaque)
1441 BlockDriverAIOCBSync *acb = opaque;
1442 acb->common.cb(acb->common.opaque, acb->ret);
1443 qemu_aio_release(acb);
1446 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1447 int64_t sector_num, uint8_t *buf, int nb_sectors,
1448 BlockDriverCompletionFunc *cb, void *opaque)
1450 BlockDriverAIOCBSync *acb;
1451 int ret;
1453 acb = qemu_aio_get(bs, cb, opaque);
1454 if (!acb->bh)
1455 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1456 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1457 acb->ret = ret;
1458 qemu_bh_schedule(acb->bh);
1459 return &acb->common;
1462 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1463 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1464 BlockDriverCompletionFunc *cb, void *opaque)
1466 BlockDriverAIOCBSync *acb;
1467 int ret;
1469 acb = qemu_aio_get(bs, cb, opaque);
1470 if (!acb->bh)
1471 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1472 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1473 acb->ret = ret;
1474 qemu_bh_schedule(acb->bh);
1475 return &acb->common;
1478 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1480 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1481 qemu_bh_cancel(acb->bh);
1482 qemu_aio_release(acb);
1485 /**************************************************************/
1486 /* sync block device emulation */
1488 static void bdrv_rw_em_cb(void *opaque, int ret)
1490 *(int *)opaque = ret;
1493 #define NOT_DONE 0x7fffffff
1495 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1496 uint8_t *buf, int nb_sectors)
1498 int async_ret;
1499 BlockDriverAIOCB *acb;
1501 async_ret = NOT_DONE;
1502 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1503 bdrv_rw_em_cb, &async_ret);
1504 if (acb == NULL)
1505 return -1;
1507 while (async_ret == NOT_DONE) {
1508 qemu_aio_wait();
1511 return async_ret;
1514 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1515 const uint8_t *buf, int nb_sectors)
1517 int async_ret;
1518 BlockDriverAIOCB *acb;
1520 async_ret = NOT_DONE;
1521 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1522 bdrv_rw_em_cb, &async_ret);
1523 if (acb == NULL)
1524 return -1;
1525 while (async_ret == NOT_DONE) {
1526 qemu_aio_wait();
1528 return async_ret;
1531 void bdrv_init(void)
1533 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
1534 bdrv_aio_cancel_vector);
1536 bdrv_register(&bdrv_raw);
1537 bdrv_register(&bdrv_host_device);
1538 #ifndef _WIN32
1539 bdrv_register(&bdrv_cow);
1540 #endif
1541 bdrv_register(&bdrv_qcow);
1542 bdrv_register(&bdrv_vmdk);
1543 bdrv_register(&bdrv_cloop);
1544 bdrv_register(&bdrv_dmg);
1545 bdrv_register(&bdrv_bochs);
1546 bdrv_register(&bdrv_vpc);
1547 bdrv_register(&bdrv_vvfat);
1548 bdrv_register(&bdrv_qcow2);
1549 bdrv_register(&bdrv_parallels);
1550 bdrv_register(&bdrv_nbd);
1553 void aio_pool_init(AIOPool *pool, int aiocb_size,
1554 void (*cancel)(BlockDriverAIOCB *acb))
1556 pool->aiocb_size = aiocb_size;
1557 pool->cancel = cancel;
1558 pool->free_aiocb = NULL;
1561 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1562 BlockDriverCompletionFunc *cb, void *opaque)
1564 BlockDriverAIOCB *acb;
1566 if (pool->free_aiocb) {
1567 acb = pool->free_aiocb;
1568 pool->free_aiocb = acb->next;
1569 } else {
1570 acb = qemu_mallocz(pool->aiocb_size);
1571 acb->pool = pool;
1573 acb->bs = bs;
1574 acb->cb = cb;
1575 acb->opaque = opaque;
1576 return acb;
1579 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1580 void *opaque)
1582 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1585 void qemu_aio_release(void *p)
1587 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1588 AIOPool *pool = acb->pool;
1589 acb->next = pool->free_aiocb;
1590 pool->free_aiocb = acb;
1593 /**************************************************************/
1594 /* removable device support */
1597 * Return TRUE if the media is present
1599 int bdrv_is_inserted(BlockDriverState *bs)
1601 BlockDriver *drv = bs->drv;
1602 int ret;
1603 if (!drv)
1604 return 0;
1605 if (!drv->bdrv_is_inserted)
1606 return 1;
1607 ret = drv->bdrv_is_inserted(bs);
1608 return ret;
1612 * Return TRUE if the media changed since the last call to this
1613 * function. It is currently only used for floppy disks
1615 int bdrv_media_changed(BlockDriverState *bs)
1617 BlockDriver *drv = bs->drv;
1618 int ret;
1620 if (!drv || !drv->bdrv_media_changed)
1621 ret = -ENOTSUP;
1622 else
1623 ret = drv->bdrv_media_changed(bs);
1624 if (ret == -ENOTSUP)
1625 ret = bs->media_changed;
1626 bs->media_changed = 0;
1627 return ret;
1631 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1633 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1635 BlockDriver *drv = bs->drv;
1636 int ret;
1638 if (!drv || !drv->bdrv_eject) {
1639 ret = -ENOTSUP;
1640 } else {
1641 ret = drv->bdrv_eject(bs, eject_flag);
1643 if (ret == -ENOTSUP) {
1644 if (eject_flag)
1645 bdrv_close(bs);
1649 int bdrv_is_locked(BlockDriverState *bs)
1651 return bs->locked;
1655 * Lock or unlock the media (if it is locked, the user won't be able
1656 * to eject it manually).
1658 void bdrv_set_locked(BlockDriverState *bs, int locked)
1660 BlockDriver *drv = bs->drv;
1662 bs->locked = locked;
1663 if (drv && drv->bdrv_set_locked) {
1664 drv->bdrv_set_locked(bs, locked);
1668 /* needed for generic scsi interface */
1670 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1672 BlockDriver *drv = bs->drv;
1674 if (drv && drv->bdrv_ioctl)
1675 return drv->bdrv_ioctl(bs, req, buf);
1676 return -ENOTSUP;
1679 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1680 unsigned long int req, void *buf,
1681 BlockDriverCompletionFunc *cb, void *opaque)
1683 BlockDriver *drv = bs->drv;
1685 if (drv && drv->bdrv_aio_ioctl)
1686 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1687 return NULL;