Merge commit 'qemu-svn/trunk'
[kvm-userspace.git] / qemu / block.c
blob8733f5e247d6fbbf3ecb72916cf9928bcfe78cec
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 int64_t offset;
575 /* Deal with byte accesses */
576 if (sector_num < 0)
577 offset = -sector_num;
578 else
579 offset = sector_num * 512;
581 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
584 /* return < 0 if error. See bdrv_write() for the return codes */
585 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
586 uint8_t *buf, int nb_sectors)
588 BlockDriver *drv = bs->drv;
590 if (!drv)
591 return -ENOMEDIUM;
592 if (bdrv_check_request(bs, sector_num, nb_sectors))
593 return -EIO;
595 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
598 /* Return < 0 if error. Important errors are:
599 -EIO generic I/O error (may happen for all errors)
600 -ENOMEDIUM No media inserted.
601 -EINVAL Invalid sector number or nb_sectors
602 -EACCES Trying to write a read-only device
604 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
605 const uint8_t *buf, int nb_sectors)
607 BlockDriver *drv = bs->drv;
608 if (!bs->drv)
609 return -ENOMEDIUM;
610 if (bs->read_only)
611 return -EACCES;
612 if (bdrv_check_request(bs, sector_num, nb_sectors))
613 return -EIO;
615 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
618 int bdrv_pread(BlockDriverState *bs, int64_t offset,
619 void *buf, int count1)
621 uint8_t tmp_buf[SECTOR_SIZE];
622 int len, nb_sectors, count;
623 int64_t sector_num;
625 count = count1;
626 /* first read to align to sector start */
627 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
628 if (len > count)
629 len = count;
630 sector_num = offset >> SECTOR_BITS;
631 if (len > 0) {
632 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
633 return -EIO;
634 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
635 count -= len;
636 if (count == 0)
637 return count1;
638 sector_num++;
639 buf += len;
642 /* read the sectors "in place" */
643 nb_sectors = count >> SECTOR_BITS;
644 if (nb_sectors > 0) {
645 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
646 return -EIO;
647 sector_num += nb_sectors;
648 len = nb_sectors << SECTOR_BITS;
649 buf += len;
650 count -= len;
653 /* add data from the last sector */
654 if (count > 0) {
655 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
656 return -EIO;
657 memcpy(buf, tmp_buf, count);
659 return count1;
662 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
663 const void *buf, int count1)
665 uint8_t tmp_buf[SECTOR_SIZE];
666 int len, nb_sectors, count;
667 int64_t sector_num;
669 count = count1;
670 /* first write to align to sector start */
671 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
672 if (len > count)
673 len = count;
674 sector_num = offset >> SECTOR_BITS;
675 if (len > 0) {
676 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
677 return -EIO;
678 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
679 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
680 return -EIO;
681 count -= len;
682 if (count == 0)
683 return count1;
684 sector_num++;
685 buf += len;
688 /* write the sectors "in place" */
689 nb_sectors = count >> SECTOR_BITS;
690 if (nb_sectors > 0) {
691 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
692 return -EIO;
693 sector_num += nb_sectors;
694 len = nb_sectors << SECTOR_BITS;
695 buf += len;
696 count -= len;
699 /* add data from the last sector */
700 if (count > 0) {
701 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
702 return -EIO;
703 memcpy(tmp_buf, buf, count);
704 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
705 return -EIO;
707 return count1;
711 * Truncate file to 'offset' bytes (needed only for file protocols)
713 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
715 BlockDriver *drv = bs->drv;
716 if (!drv)
717 return -ENOMEDIUM;
718 if (!drv->bdrv_truncate)
719 return -ENOTSUP;
720 return drv->bdrv_truncate(bs, offset);
724 * Length of a file in bytes. Return < 0 if error or unknown.
726 int64_t bdrv_getlength(BlockDriverState *bs)
728 BlockDriver *drv = bs->drv;
729 if (!drv)
730 return -ENOMEDIUM;
731 if (!drv->bdrv_getlength) {
732 /* legacy mode */
733 return bs->total_sectors * SECTOR_SIZE;
735 return drv->bdrv_getlength(bs);
738 /* return 0 as number of sectors if no device present or error */
739 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
741 int64_t length;
742 length = bdrv_getlength(bs);
743 if (length < 0)
744 length = 0;
745 else
746 length = length >> SECTOR_BITS;
747 *nb_sectors_ptr = length;
750 struct partition {
751 uint8_t boot_ind; /* 0x80 - active */
752 uint8_t head; /* starting head */
753 uint8_t sector; /* starting sector */
754 uint8_t cyl; /* starting cylinder */
755 uint8_t sys_ind; /* What partition type */
756 uint8_t end_head; /* end head */
757 uint8_t end_sector; /* end sector */
758 uint8_t end_cyl; /* end cylinder */
759 uint32_t start_sect; /* starting sector counting from 0 */
760 uint32_t nr_sects; /* nr of sectors in partition */
761 } __attribute__((packed));
763 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
764 static int guess_disk_lchs(BlockDriverState *bs,
765 int *pcylinders, int *pheads, int *psectors)
767 uint8_t *buf;
768 int ret, i, heads, sectors, cylinders;
769 struct partition *p;
770 uint32_t nr_sects;
771 uint64_t nb_sectors;
773 buf = qemu_memalign(512, 512);
774 if (buf == NULL)
775 return -1;
777 bdrv_get_geometry(bs, &nb_sectors);
779 ret = bdrv_read(bs, 0, buf, 1);
780 if (ret < 0)
781 return -1;
782 /* test msdos magic */
783 if (buf[510] != 0x55 || buf[511] != 0xaa) {
784 qemu_free(buf);
785 return -1;
787 for(i = 0; i < 4; i++) {
788 p = ((struct partition *)(buf + 0x1be)) + i;
789 nr_sects = le32_to_cpu(p->nr_sects);
790 if (nr_sects && p->end_head) {
791 /* We make the assumption that the partition terminates on
792 a cylinder boundary */
793 heads = p->end_head + 1;
794 sectors = p->end_sector & 63;
795 if (sectors == 0)
796 continue;
797 cylinders = nb_sectors / (heads * sectors);
798 if (cylinders < 1 || cylinders > 16383)
799 continue;
800 *pheads = heads;
801 *psectors = sectors;
802 *pcylinders = cylinders;
803 #if 0
804 printf("guessed geometry: LCHS=%d %d %d\n",
805 cylinders, heads, sectors);
806 #endif
807 qemu_free(buf);
808 return 0;
811 qemu_free(buf);
812 return -1;
815 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
817 int translation, lba_detected = 0;
818 int cylinders, heads, secs;
819 uint64_t nb_sectors;
821 /* if a geometry hint is available, use it */
822 bdrv_get_geometry(bs, &nb_sectors);
823 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
824 translation = bdrv_get_translation_hint(bs);
825 if (cylinders != 0) {
826 *pcyls = cylinders;
827 *pheads = heads;
828 *psecs = secs;
829 } else {
830 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
831 if (heads > 16) {
832 /* if heads > 16, it means that a BIOS LBA
833 translation was active, so the default
834 hardware geometry is OK */
835 lba_detected = 1;
836 goto default_geometry;
837 } else {
838 *pcyls = cylinders;
839 *pheads = heads;
840 *psecs = secs;
841 /* disable any translation to be in sync with
842 the logical geometry */
843 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
844 bdrv_set_translation_hint(bs,
845 BIOS_ATA_TRANSLATION_NONE);
848 } else {
849 default_geometry:
850 /* if no geometry, use a standard physical disk geometry */
851 cylinders = nb_sectors / (16 * 63);
853 if (cylinders > 16383)
854 cylinders = 16383;
855 else if (cylinders < 2)
856 cylinders = 2;
857 *pcyls = cylinders;
858 *pheads = 16;
859 *psecs = 63;
860 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
861 if ((*pcyls * *pheads) <= 131072) {
862 bdrv_set_translation_hint(bs,
863 BIOS_ATA_TRANSLATION_LARGE);
864 } else {
865 bdrv_set_translation_hint(bs,
866 BIOS_ATA_TRANSLATION_LBA);
870 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
874 void bdrv_set_geometry_hint(BlockDriverState *bs,
875 int cyls, int heads, int secs)
877 bs->cyls = cyls;
878 bs->heads = heads;
879 bs->secs = secs;
882 void bdrv_set_type_hint(BlockDriverState *bs, int type)
884 bs->type = type;
885 bs->removable = ((type == BDRV_TYPE_CDROM ||
886 type == BDRV_TYPE_FLOPPY));
889 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
891 bs->translation = translation;
894 void bdrv_get_geometry_hint(BlockDriverState *bs,
895 int *pcyls, int *pheads, int *psecs)
897 *pcyls = bs->cyls;
898 *pheads = bs->heads;
899 *psecs = bs->secs;
902 int bdrv_get_type_hint(BlockDriverState *bs)
904 return bs->type;
907 int bdrv_get_translation_hint(BlockDriverState *bs)
909 return bs->translation;
912 int bdrv_is_removable(BlockDriverState *bs)
914 return bs->removable;
917 int bdrv_is_read_only(BlockDriverState *bs)
919 return bs->read_only;
922 int bdrv_is_sg(BlockDriverState *bs)
924 return bs->sg;
927 /* XXX: no longer used */
928 void bdrv_set_change_cb(BlockDriverState *bs,
929 void (*change_cb)(void *opaque), void *opaque)
931 bs->change_cb = change_cb;
932 bs->change_opaque = opaque;
935 int bdrv_is_encrypted(BlockDriverState *bs)
937 if (bs->backing_hd && bs->backing_hd->encrypted)
938 return 1;
939 return bs->encrypted;
942 int bdrv_key_required(BlockDriverState *bs)
944 BlockDriverState *backing_hd = bs->backing_hd;
946 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
947 return 1;
948 return (bs->encrypted && !bs->valid_key);
951 int bdrv_set_key(BlockDriverState *bs, const char *key)
953 int ret;
954 if (bs->backing_hd && bs->backing_hd->encrypted) {
955 ret = bdrv_set_key(bs->backing_hd, key);
956 if (ret < 0)
957 return ret;
958 if (!bs->encrypted)
959 return 0;
961 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
962 return -1;
963 ret = bs->drv->bdrv_set_key(bs, key);
964 if (ret < 0) {
965 bs->valid_key = 0;
966 } else if (!bs->valid_key) {
967 bs->valid_key = 1;
968 /* call the change callback now, we skipped it on open */
969 bs->media_changed = 1;
970 if (bs->change_cb)
971 bs->change_cb(bs->change_opaque);
973 return ret;
976 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
978 if (!bs->drv) {
979 buf[0] = '\0';
980 } else {
981 pstrcpy(buf, buf_size, bs->drv->format_name);
985 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
986 void *opaque)
988 BlockDriver *drv;
990 for (drv = first_drv; drv != NULL; drv = drv->next) {
991 it(opaque, drv->format_name);
995 BlockDriverState *bdrv_find(const char *name)
997 BlockDriverState *bs;
999 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1000 if (!strcmp(name, bs->device_name))
1001 return bs;
1003 return NULL;
1006 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1008 BlockDriverState *bs;
1010 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1011 it(opaque, bs);
1015 const char *bdrv_get_device_name(BlockDriverState *bs)
1017 return bs->device_name;
1020 void bdrv_flush(BlockDriverState *bs)
1022 if (bs->drv->bdrv_flush)
1023 bs->drv->bdrv_flush(bs);
1024 if (bs->backing_hd)
1025 bdrv_flush(bs->backing_hd);
1028 void bdrv_flush_all(void)
1030 BlockDriverState *bs;
1032 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1033 if (bs->drv && !bdrv_is_read_only(bs) &&
1034 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1035 bdrv_flush(bs);
1039 * Returns true iff the specified sector is present in the disk image. Drivers
1040 * not implementing the functionality are assumed to not support backing files,
1041 * hence all their sectors are reported as allocated.
1043 * 'pnum' is set to the number of sectors (including and immediately following
1044 * the specified sector) that are known to be in the same
1045 * allocated/unallocated state.
1047 * 'nb_sectors' is the max value 'pnum' should be set to.
1049 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1050 int *pnum)
1052 int64_t n;
1053 if (!bs->drv->bdrv_is_allocated) {
1054 if (sector_num >= bs->total_sectors) {
1055 *pnum = 0;
1056 return 0;
1058 n = bs->total_sectors - sector_num;
1059 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1060 return 1;
1062 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1065 void bdrv_info(Monitor *mon)
1067 BlockDriverState *bs;
1069 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1070 monitor_printf(mon, "%s:", bs->device_name);
1071 monitor_printf(mon, " type=");
1072 switch(bs->type) {
1073 case BDRV_TYPE_HD:
1074 monitor_printf(mon, "hd");
1075 break;
1076 case BDRV_TYPE_CDROM:
1077 monitor_printf(mon, "cdrom");
1078 break;
1079 case BDRV_TYPE_FLOPPY:
1080 monitor_printf(mon, "floppy");
1081 break;
1083 monitor_printf(mon, " removable=%d", bs->removable);
1084 if (bs->removable) {
1085 monitor_printf(mon, " locked=%d", bs->locked);
1087 if (bs->drv) {
1088 monitor_printf(mon, " file=");
1089 monitor_print_filename(mon, bs->filename);
1090 if (bs->backing_file[0] != '\0') {
1091 monitor_printf(mon, " backing_file=");
1092 monitor_print_filename(mon, bs->backing_file);
1094 monitor_printf(mon, " ro=%d", bs->read_only);
1095 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1096 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1097 } else {
1098 monitor_printf(mon, " [not inserted]");
1100 monitor_printf(mon, "\n");
1104 /* The "info blockstats" command. */
1105 void bdrv_info_stats(Monitor *mon)
1107 BlockDriverState *bs;
1109 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1110 monitor_printf(mon, "%s:"
1111 " rd_bytes=%" PRIu64
1112 " wr_bytes=%" PRIu64
1113 " rd_operations=%" PRIu64
1114 " wr_operations=%" PRIu64
1115 "\n",
1116 bs->device_name,
1117 bs->rd_bytes, bs->wr_bytes,
1118 bs->rd_ops, bs->wr_ops);
1122 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1124 if (bs->backing_hd && bs->backing_hd->encrypted)
1125 return bs->backing_file;
1126 else if (bs->encrypted)
1127 return bs->filename;
1128 else
1129 return NULL;
1132 void bdrv_get_backing_filename(BlockDriverState *bs,
1133 char *filename, int filename_size)
1135 if (!bs->backing_hd) {
1136 pstrcpy(filename, filename_size, "");
1137 } else {
1138 pstrcpy(filename, filename_size, bs->backing_file);
1142 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1143 const uint8_t *buf, int nb_sectors)
1145 BlockDriver *drv = bs->drv;
1146 if (!drv)
1147 return -ENOMEDIUM;
1148 if (!drv->bdrv_write_compressed)
1149 return -ENOTSUP;
1150 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1153 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1155 BlockDriver *drv = bs->drv;
1156 if (!drv)
1157 return -ENOMEDIUM;
1158 if (!drv->bdrv_get_info)
1159 return -ENOTSUP;
1160 memset(bdi, 0, sizeof(*bdi));
1161 return drv->bdrv_get_info(bs, bdi);
1164 /**************************************************************/
1165 /* handling of snapshots */
1167 int bdrv_snapshot_create(BlockDriverState *bs,
1168 QEMUSnapshotInfo *sn_info)
1170 BlockDriver *drv = bs->drv;
1171 if (!drv)
1172 return -ENOMEDIUM;
1173 if (!drv->bdrv_snapshot_create)
1174 return -ENOTSUP;
1175 return drv->bdrv_snapshot_create(bs, sn_info);
1178 int bdrv_snapshot_goto(BlockDriverState *bs,
1179 const char *snapshot_id)
1181 BlockDriver *drv = bs->drv;
1182 if (!drv)
1183 return -ENOMEDIUM;
1184 if (!drv->bdrv_snapshot_goto)
1185 return -ENOTSUP;
1186 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1189 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1191 BlockDriver *drv = bs->drv;
1192 if (!drv)
1193 return -ENOMEDIUM;
1194 if (!drv->bdrv_snapshot_delete)
1195 return -ENOTSUP;
1196 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1199 int bdrv_snapshot_list(BlockDriverState *bs,
1200 QEMUSnapshotInfo **psn_info)
1202 BlockDriver *drv = bs->drv;
1203 if (!drv)
1204 return -ENOMEDIUM;
1205 if (!drv->bdrv_snapshot_list)
1206 return -ENOTSUP;
1207 return drv->bdrv_snapshot_list(bs, psn_info);
1210 #define NB_SUFFIXES 4
1212 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1214 static const char suffixes[NB_SUFFIXES] = "KMGT";
1215 int64_t base;
1216 int i;
1218 if (size <= 999) {
1219 snprintf(buf, buf_size, "%" PRId64, size);
1220 } else {
1221 base = 1024;
1222 for(i = 0; i < NB_SUFFIXES; i++) {
1223 if (size < (10 * base)) {
1224 snprintf(buf, buf_size, "%0.1f%c",
1225 (double)size / base,
1226 suffixes[i]);
1227 break;
1228 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1229 snprintf(buf, buf_size, "%" PRId64 "%c",
1230 ((size + (base >> 1)) / base),
1231 suffixes[i]);
1232 break;
1234 base = base * 1024;
1237 return buf;
1240 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1242 char buf1[128], date_buf[128], clock_buf[128];
1243 #ifdef _WIN32
1244 struct tm *ptm;
1245 #else
1246 struct tm tm;
1247 #endif
1248 time_t ti;
1249 int64_t secs;
1251 if (!sn) {
1252 snprintf(buf, buf_size,
1253 "%-10s%-20s%7s%20s%15s",
1254 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1255 } else {
1256 ti = sn->date_sec;
1257 #ifdef _WIN32
1258 ptm = localtime(&ti);
1259 strftime(date_buf, sizeof(date_buf),
1260 "%Y-%m-%d %H:%M:%S", ptm);
1261 #else
1262 localtime_r(&ti, &tm);
1263 strftime(date_buf, sizeof(date_buf),
1264 "%Y-%m-%d %H:%M:%S", &tm);
1265 #endif
1266 secs = sn->vm_clock_nsec / 1000000000;
1267 snprintf(clock_buf, sizeof(clock_buf),
1268 "%02d:%02d:%02d.%03d",
1269 (int)(secs / 3600),
1270 (int)((secs / 60) % 60),
1271 (int)(secs % 60),
1272 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1273 snprintf(buf, buf_size,
1274 "%-10s%-20s%7s%20s%15s",
1275 sn->id_str, sn->name,
1276 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1277 date_buf,
1278 clock_buf);
1280 return buf;
1284 /**************************************************************/
1285 /* async I/Os */
1287 typedef struct VectorTranslationAIOCB {
1288 BlockDriverAIOCB common;
1289 QEMUIOVector *iov;
1290 uint8_t *bounce;
1291 int is_write;
1292 BlockDriverAIOCB *aiocb;
1293 } VectorTranslationAIOCB;
1295 static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb)
1297 VectorTranslationAIOCB *acb
1298 = container_of(_acb, VectorTranslationAIOCB, common);
1300 bdrv_aio_cancel(acb->aiocb);
1303 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1305 VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque;
1307 if (!s->is_write) {
1308 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1310 qemu_vfree(s->bounce);
1311 s->common.cb(s->common.opaque, ret);
1312 qemu_aio_release(s);
1315 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1316 int64_t sector_num,
1317 QEMUIOVector *iov,
1318 int nb_sectors,
1319 BlockDriverCompletionFunc *cb,
1320 void *opaque,
1321 int is_write)
1324 VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs,
1325 cb, opaque);
1327 s->iov = iov;
1328 s->bounce = qemu_memalign(512, nb_sectors * 512);
1329 s->is_write = is_write;
1330 if (is_write) {
1331 qemu_iovec_to_buffer(s->iov, s->bounce);
1332 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1333 bdrv_aio_rw_vector_cb, s);
1334 } else {
1335 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1336 bdrv_aio_rw_vector_cb, s);
1338 if (!s->aiocb) {
1339 qemu_vfree(s->bounce);
1340 qemu_aio_release(s);
1341 return NULL;
1343 return &s->common;
1346 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1347 QEMUIOVector *iov, int nb_sectors,
1348 BlockDriverCompletionFunc *cb, void *opaque)
1350 if (bdrv_check_request(bs, sector_num, nb_sectors))
1351 return NULL;
1353 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1354 cb, opaque, 0);
1357 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1358 QEMUIOVector *iov, int nb_sectors,
1359 BlockDriverCompletionFunc *cb, void *opaque)
1361 if (bdrv_check_request(bs, sector_num, nb_sectors))
1362 return NULL;
1364 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1365 cb, opaque, 1);
1368 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1369 uint8_t *buf, int nb_sectors,
1370 BlockDriverCompletionFunc *cb, void *opaque)
1372 BlockDriver *drv = bs->drv;
1373 BlockDriverAIOCB *ret;
1375 if (!drv)
1376 return NULL;
1377 if (bdrv_check_request(bs, sector_num, nb_sectors))
1378 return NULL;
1380 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1382 if (ret) {
1383 /* Update stats even though technically transfer has not happened. */
1384 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1385 bs->rd_ops ++;
1388 return ret;
1391 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1392 const uint8_t *buf, int nb_sectors,
1393 BlockDriverCompletionFunc *cb, void *opaque)
1395 BlockDriver *drv = bs->drv;
1396 BlockDriverAIOCB *ret;
1398 if (!drv)
1399 return NULL;
1400 if (bs->read_only)
1401 return NULL;
1402 if (bdrv_check_request(bs, sector_num, nb_sectors))
1403 return NULL;
1405 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1407 if (ret) {
1408 /* Update stats even though technically transfer has not happened. */
1409 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1410 bs->wr_ops ++;
1413 return ret;
1416 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1418 acb->pool->cancel(acb);
1422 /**************************************************************/
1423 /* async block device emulation */
1425 static void bdrv_aio_bh_cb(void *opaque)
1427 BlockDriverAIOCBSync *acb = opaque;
1428 acb->common.cb(acb->common.opaque, acb->ret);
1429 qemu_aio_release(acb);
1432 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1433 int64_t sector_num, uint8_t *buf, int nb_sectors,
1434 BlockDriverCompletionFunc *cb, void *opaque)
1436 BlockDriverAIOCBSync *acb;
1437 int ret;
1439 acb = qemu_aio_get(bs, cb, opaque);
1440 if (!acb->bh)
1441 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1442 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1443 acb->ret = ret;
1444 qemu_bh_schedule(acb->bh);
1445 return &acb->common;
1448 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1449 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1450 BlockDriverCompletionFunc *cb, void *opaque)
1452 BlockDriverAIOCBSync *acb;
1453 int ret;
1455 acb = qemu_aio_get(bs, cb, opaque);
1456 if (!acb->bh)
1457 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1458 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1459 acb->ret = ret;
1460 qemu_bh_schedule(acb->bh);
1461 return &acb->common;
1464 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1466 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1467 qemu_bh_cancel(acb->bh);
1468 qemu_aio_release(acb);
1471 /**************************************************************/
1472 /* sync block device emulation */
1474 static void bdrv_rw_em_cb(void *opaque, int ret)
1476 *(int *)opaque = ret;
1479 #define NOT_DONE 0x7fffffff
1481 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1482 uint8_t *buf, int nb_sectors)
1484 int async_ret;
1485 BlockDriverAIOCB *acb;
1487 async_ret = NOT_DONE;
1488 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1489 bdrv_rw_em_cb, &async_ret);
1490 if (acb == NULL)
1491 return -1;
1493 while (async_ret == NOT_DONE) {
1494 qemu_aio_wait();
1497 return async_ret;
1500 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1501 const uint8_t *buf, int nb_sectors)
1503 int async_ret;
1504 BlockDriverAIOCB *acb;
1506 async_ret = NOT_DONE;
1507 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1508 bdrv_rw_em_cb, &async_ret);
1509 if (acb == NULL)
1510 return -1;
1511 while (async_ret == NOT_DONE) {
1512 qemu_aio_wait();
1514 return async_ret;
1517 void bdrv_init(void)
1519 aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB),
1520 bdrv_aio_cancel_vector);
1522 bdrv_register(&bdrv_raw);
1523 bdrv_register(&bdrv_host_device);
1524 #ifndef _WIN32
1525 bdrv_register(&bdrv_cow);
1526 #endif
1527 bdrv_register(&bdrv_qcow);
1528 bdrv_register(&bdrv_vmdk);
1529 bdrv_register(&bdrv_cloop);
1530 bdrv_register(&bdrv_dmg);
1531 bdrv_register(&bdrv_bochs);
1532 bdrv_register(&bdrv_vpc);
1533 bdrv_register(&bdrv_vvfat);
1534 bdrv_register(&bdrv_qcow2);
1535 bdrv_register(&bdrv_parallels);
1536 bdrv_register(&bdrv_nbd);
1539 void aio_pool_init(AIOPool *pool, int aiocb_size,
1540 void (*cancel)(BlockDriverAIOCB *acb))
1542 pool->aiocb_size = aiocb_size;
1543 pool->cancel = cancel;
1544 pool->free_aiocb = NULL;
1547 void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1548 BlockDriverCompletionFunc *cb, void *opaque)
1550 BlockDriverAIOCB *acb;
1552 if (pool->free_aiocb) {
1553 acb = pool->free_aiocb;
1554 pool->free_aiocb = acb->next;
1555 } else {
1556 acb = qemu_mallocz(pool->aiocb_size);
1557 acb->pool = pool;
1559 acb->bs = bs;
1560 acb->cb = cb;
1561 acb->opaque = opaque;
1562 return acb;
1565 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1566 void *opaque)
1568 return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1571 void qemu_aio_release(void *p)
1573 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1574 AIOPool *pool = acb->pool;
1575 acb->next = pool->free_aiocb;
1576 pool->free_aiocb = acb;
1579 /**************************************************************/
1580 /* removable device support */
1583 * Return TRUE if the media is present
1585 int bdrv_is_inserted(BlockDriverState *bs)
1587 BlockDriver *drv = bs->drv;
1588 int ret;
1589 if (!drv)
1590 return 0;
1591 if (!drv->bdrv_is_inserted)
1592 return 1;
1593 ret = drv->bdrv_is_inserted(bs);
1594 return ret;
1598 * Return TRUE if the media changed since the last call to this
1599 * function. It is currently only used for floppy disks
1601 int bdrv_media_changed(BlockDriverState *bs)
1603 BlockDriver *drv = bs->drv;
1604 int ret;
1606 if (!drv || !drv->bdrv_media_changed)
1607 ret = -ENOTSUP;
1608 else
1609 ret = drv->bdrv_media_changed(bs);
1610 if (ret == -ENOTSUP)
1611 ret = bs->media_changed;
1612 bs->media_changed = 0;
1613 return ret;
1617 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1619 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1621 BlockDriver *drv = bs->drv;
1622 int ret;
1624 if (!drv || !drv->bdrv_eject) {
1625 ret = -ENOTSUP;
1626 } else {
1627 ret = drv->bdrv_eject(bs, eject_flag);
1629 if (ret == -ENOTSUP) {
1630 if (eject_flag)
1631 bdrv_close(bs);
1635 int bdrv_is_locked(BlockDriverState *bs)
1637 return bs->locked;
1641 * Lock or unlock the media (if it is locked, the user won't be able
1642 * to eject it manually).
1644 void bdrv_set_locked(BlockDriverState *bs, int locked)
1646 BlockDriver *drv = bs->drv;
1648 bs->locked = locked;
1649 if (drv && drv->bdrv_set_locked) {
1650 drv->bdrv_set_locked(bs, locked);
1654 /* needed for generic scsi interface */
1656 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1658 BlockDriver *drv = bs->drv;
1660 if (drv && drv->bdrv_ioctl)
1661 return drv->bdrv_ioctl(bs, req, buf);
1662 return -ENOTSUP;
1665 BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1666 unsigned long int req, void *buf,
1667 BlockDriverCompletionFunc *cb, void *opaque)
1669 BlockDriver *drv = bs->drv;
1671 if (drv && drv->bdrv_aio_ioctl)
1672 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1673 return NULL;