Merge commit 'qemu-svn/trunk'
[qemu-kvm/fedora.git] / block.c
bloba86c48250ed548f3410e87345fab89f565f58f9b
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 /**************************************************************/
1159 /* handling of snapshots */
1161 int bdrv_snapshot_create(BlockDriverState *bs,
1162 QEMUSnapshotInfo *sn_info)
1164 BlockDriver *drv = bs->drv;
1165 if (!drv)
1166 return -ENOMEDIUM;
1167 if (!drv->bdrv_snapshot_create)
1168 return -ENOTSUP;
1169 return drv->bdrv_snapshot_create(bs, sn_info);
1172 int bdrv_snapshot_goto(BlockDriverState *bs,
1173 const char *snapshot_id)
1175 BlockDriver *drv = bs->drv;
1176 if (!drv)
1177 return -ENOMEDIUM;
1178 if (!drv->bdrv_snapshot_goto)
1179 return -ENOTSUP;
1180 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1183 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1185 BlockDriver *drv = bs->drv;
1186 if (!drv)
1187 return -ENOMEDIUM;
1188 if (!drv->bdrv_snapshot_delete)
1189 return -ENOTSUP;
1190 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1193 int bdrv_snapshot_list(BlockDriverState *bs,
1194 QEMUSnapshotInfo **psn_info)
1196 BlockDriver *drv = bs->drv;
1197 if (!drv)
1198 return -ENOMEDIUM;
1199 if (!drv->bdrv_snapshot_list)
1200 return -ENOTSUP;
1201 return drv->bdrv_snapshot_list(bs, psn_info);
1204 #define NB_SUFFIXES 4
1206 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1208 static const char suffixes[NB_SUFFIXES] = "KMGT";
1209 int64_t base;
1210 int i;
1212 if (size <= 999) {
1213 snprintf(buf, buf_size, "%" PRId64, size);
1214 } else {
1215 base = 1024;
1216 for(i = 0; i < NB_SUFFIXES; i++) {
1217 if (size < (10 * base)) {
1218 snprintf(buf, buf_size, "%0.1f%c",
1219 (double)size / base,
1220 suffixes[i]);
1221 break;
1222 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1223 snprintf(buf, buf_size, "%" PRId64 "%c",
1224 ((size + (base >> 1)) / base),
1225 suffixes[i]);
1226 break;
1228 base = base * 1024;
1231 return buf;
1234 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1236 char buf1[128], date_buf[128], clock_buf[128];
1237 #ifdef _WIN32
1238 struct tm *ptm;
1239 #else
1240 struct tm tm;
1241 #endif
1242 time_t ti;
1243 int64_t secs;
1245 if (!sn) {
1246 snprintf(buf, buf_size,
1247 "%-10s%-20s%7s%20s%15s",
1248 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1249 } else {
1250 ti = sn->date_sec;
1251 #ifdef _WIN32
1252 ptm = localtime(&ti);
1253 strftime(date_buf, sizeof(date_buf),
1254 "%Y-%m-%d %H:%M:%S", ptm);
1255 #else
1256 localtime_r(&ti, &tm);
1257 strftime(date_buf, sizeof(date_buf),
1258 "%Y-%m-%d %H:%M:%S", &tm);
1259 #endif
1260 secs = sn->vm_clock_nsec / 1000000000;
1261 snprintf(clock_buf, sizeof(clock_buf),
1262 "%02d:%02d:%02d.%03d",
1263 (int)(secs / 3600),
1264 (int)((secs / 60) % 60),
1265 (int)(secs % 60),
1266 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1267 snprintf(buf, buf_size,
1268 "%-10s%-20s%7s%20s%15s",
1269 sn->id_str, sn->name,
1270 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1271 date_buf,
1272 clock_buf);
1274 return buf;
1278 /**************************************************************/
1279 /* async I/Os */
1281 typedef struct VectorTranslationAIOCB {
1282 BlockDriverAIOCB common;
1283 QEMUIOVector *iov;
1284 uint8_t *bounce;
1285 int is_write;
1286 BlockDriverAIOCB *aiocb;
1287 } VectorTranslationAIOCB;
1289 static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
1291 VectorTranslationAIOCB *acb
1292 = container_of(_acb, VectorTranslationAIOCB, common);
1294 bdrv_aio_cancel(acb->aiocb);
1297 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1299 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
1301 if (!s->is_write) {
1302 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1304 qemu_vfree(s->bounce);
1305 s->common.cb(s->common.opaque, ret);
1306 qemu_aio_release(s);
1309 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1310 int64_t sector_num,
1311 QEMUIOVector *iov,
1312 int nb_sectors,
1313 BlockDriverCompletionFunc *cb,
1314 void *opaque,
1315 int is_write)
1318 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1319 cb, opaque);
1321 s->iov = iov;
1322 s->bounce = qemu_memalign(512, nb_sectors * 512);
1323 s->is_write = is_write;
1324 if (is_write) {
1325 qemu_iovec_to_buffer(s->iov, s->bounce);
1326 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1327 bdrv_aio_rw_vector_cb, s);
1328 } else {
1329 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1330 bdrv_aio_rw_vector_cb, s);
1332 if (!s->aiocb) {
1333 qemu_vfree(s->bounce);
1334 qemu_aio_release(s);
1335 return NULL;
1337 return &s->common;
1340 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1341 QEMUIOVector *iov, int nb_sectors,
1342 BlockDriverCompletionFunc *cb, void *opaque)
1344 if (bdrv_check_request(bs, sector_num, nb_sectors))
1345 return NULL;
1347 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1348 cb, opaque, 0);
1351 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1352 QEMUIOVector *iov, int nb_sectors,
1353 BlockDriverCompletionFunc *cb, void *opaque)
1355 if (bdrv_check_request(bs, sector_num, nb_sectors))
1356 return NULL;
1358 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1359 cb, opaque, 1);
1362 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1363 uint8_t *buf, int nb_sectors,
1364 BlockDriverCompletionFunc *cb, void *opaque)
1366 BlockDriver *drv = bs->drv;
1367 BlockDriverAIOCB *ret;
1369 if (!drv)
1370 return NULL;
1371 if (bdrv_check_request(bs, sector_num, nb_sectors))
1372 return NULL;
1374 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1376 if (ret) {
1377 /* Update stats even though technically transfer has not happened. */
1378 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1379 bs->rd_ops ++;
1382 return ret;
1385 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1386 const uint8_t *buf, int nb_sectors,
1387 BlockDriverCompletionFunc *cb, void *opaque)
1389 BlockDriver *drv = bs->drv;
1390 BlockDriverAIOCB *ret;
1392 if (!drv)
1393 return NULL;
1394 if (bs->read_only)
1395 return NULL;
1396 if (bdrv_check_request(bs, sector_num, nb_sectors))
1397 return NULL;
1399 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1401 if (ret) {
1402 /* Update stats even though technically transfer has not happened. */
1403 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1404 bs->wr_ops ++;
1407 return ret;
1410 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1412 acb->pool->cancel(acb);
1416 /**************************************************************/
1417 /* async block device emulation */
1419 static void bdrv_aio_bh_cb(void *opaque)
1421 BlockDriverAIOCBSync *acb = opaque;
1422 acb->common.cb(acb->common.opaque, acb->ret);
1423 qemu_aio_release(acb);
1426 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1427 int64_t sector_num, uint8_t *buf, int nb_sectors,
1428 BlockDriverCompletionFunc *cb, void *opaque)
1430 BlockDriverAIOCBSync *acb;
1431 int ret;
1433 acb = qemu_aio_get(bs, cb, opaque);
1434 if (!acb->bh)
1435 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1436 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1437 acb->ret = ret;
1438 qemu_bh_schedule(acb->bh);
1439 return &acb->common;
1442 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1443 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1444 BlockDriverCompletionFunc *cb, void *opaque)
1446 BlockDriverAIOCBSync *acb;
1447 int ret;
1449 acb = qemu_aio_get(bs, cb, opaque);
1450 if (!acb->bh)
1451 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1452 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1453 acb->ret = ret;
1454 qemu_bh_schedule(acb->bh);
1455 return &acb->common;
1458 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1460 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1461 qemu_bh_cancel(acb->bh);
1462 qemu_aio_release(acb);
1465 /**************************************************************/
1466 /* sync block device emulation */
1468 static void bdrv_rw_em_cb(void *opaque, int ret)
1470 *(int *)opaque = ret;
1473 #define NOT_DONE 0x7fffffff
1475 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1476 uint8_t *buf, int nb_sectors)
1478 int async_ret;
1479 BlockDriverAIOCB *acb;
1481 async_ret = NOT_DONE;
1482 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1483 bdrv_rw_em_cb, &async_ret);
1484 if (acb == NULL)
1485 return -1;
1487 while (async_ret == NOT_DONE) {
1488 qemu_aio_wait();
1491 return async_ret;
1494 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1495 const uint8_t *buf, int nb_sectors)
1497 int async_ret;
1498 BlockDriverAIOCB *acb;
1500 async_ret = NOT_DONE;
1501 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1502 bdrv_rw_em_cb, &async_ret);
1503 if (acb == NULL)
1504 return -1;
1505 while (async_ret == NOT_DONE) {
1506 qemu_aio_wait();
1508 return async_ret;
1511 void bdrv_init(void)
1513 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
1514 bdrv_aio_cancel_vector);
1516 bdrv_register(&bdrv_raw);
1517 bdrv_register(&bdrv_host_device);
1518 #ifndef _WIN32
1519 bdrv_register(&bdrv_cow);
1520 #endif
1521 bdrv_register(&bdrv_qcow);
1522 bdrv_register(&bdrv_vmdk);
1523 bdrv_register(&bdrv_cloop);
1524 bdrv_register(&bdrv_dmg);
1525 bdrv_register(&bdrv_bochs);
1526 bdrv_register(&bdrv_vpc);
1527 bdrv_register(&bdrv_vvfat);
1528 bdrv_register(&bdrv_qcow2);
1529 bdrv_register(&bdrv_parallels);
1530 bdrv_register(&bdrv_nbd);
1533 void aio_pool_init(AIOPool *pool, int aiocb_size,
1534 void (*cancel)(BlockDriverAIOCB *acb))
1536 pool->aiocb_size = aiocb_size;
1537 pool->cancel = cancel;
1538 pool->free_aiocb = NULL;
1541 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1542 BlockDriverCompletionFunc *cb, void *opaque)
1544 BlockDriverAIOCB *acb;
1546 if (pool->free_aiocb) {
1547 acb = pool->free_aiocb;
1548 pool->free_aiocb = acb->next;
1549 } else {
1550 acb = qemu_mallocz(pool->aiocb_size);
1551 acb->pool = pool;
1553 acb->bs = bs;
1554 acb->cb = cb;
1555 acb->opaque = opaque;
1556 return acb;
1559 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1560 void *opaque)
1562 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1565 void qemu_aio_release(void *p)
1567 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1568 AIOPool *pool = acb->pool;
1569 acb->next = pool->free_aiocb;
1570 pool->free_aiocb = acb;
1573 /**************************************************************/
1574 /* removable device support */
1577 * Return TRUE if the media is present
1579 int bdrv_is_inserted(BlockDriverState *bs)
1581 BlockDriver *drv = bs->drv;
1582 int ret;
1583 if (!drv)
1584 return 0;
1585 if (!drv->bdrv_is_inserted)
1586 return 1;
1587 ret = drv->bdrv_is_inserted(bs);
1588 return ret;
1592 * Return TRUE if the media changed since the last call to this
1593 * function. It is currently only used for floppy disks
1595 int bdrv_media_changed(BlockDriverState *bs)
1597 BlockDriver *drv = bs->drv;
1598 int ret;
1600 if (!drv || !drv->bdrv_media_changed)
1601 ret = -ENOTSUP;
1602 else
1603 ret = drv->bdrv_media_changed(bs);
1604 if (ret == -ENOTSUP)
1605 ret = bs->media_changed;
1606 bs->media_changed = 0;
1607 return ret;
1611 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1613 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1615 BlockDriver *drv = bs->drv;
1616 int ret;
1618 if (!drv || !drv->bdrv_eject) {
1619 ret = -ENOTSUP;
1620 } else {
1621 ret = drv->bdrv_eject(bs, eject_flag);
1623 if (ret == -ENOTSUP) {
1624 if (eject_flag)
1625 bdrv_close(bs);
1629 int bdrv_is_locked(BlockDriverState *bs)
1631 return bs->locked;
1635 * Lock or unlock the media (if it is locked, the user won't be able
1636 * to eject it manually).
1638 void bdrv_set_locked(BlockDriverState *bs, int locked)
1640 BlockDriver *drv = bs->drv;
1642 bs->locked = locked;
1643 if (drv && drv->bdrv_set_locked) {
1644 drv->bdrv_set_locked(bs, locked);
1648 /* needed for generic scsi interface */
1650 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1652 BlockDriver *drv = bs->drv;
1654 if (drv && drv->bdrv_ioctl)
1655 return drv->bdrv_ioctl(bs, req, buf);
1656 return -ENOTSUP;
1659 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1660 unsigned long int req, void *buf,
1661 BlockDriverCompletionFunc *cb, void *opaque)
1663 BlockDriver *drv = bs->drv;
1665 if (drv && drv->bdrv_aio_ioctl)
1666 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1667 return NULL;