Add MSI-X related macro to pci.c
[qemu-kvm/fedora.git] / block.c
blobcfa0620ea0cbc4fa7dd484b5481b8b440b496e3c
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 typedef struct BlockDriverAIOCBSync {
52 BlockDriverAIOCB common;
53 QEMUBH *bh;
54 int ret;
55 } BlockDriverAIOCBSync;
57 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
58 int64_t sector_num, uint8_t *buf, int nb_sectors,
59 BlockDriverCompletionFunc *cb, void *opaque);
60 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
61 int64_t sector_num, const uint8_t *buf, int nb_sectors,
62 BlockDriverCompletionFunc *cb, void *opaque);
63 static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
64 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
65 uint8_t *buf, int nb_sectors);
66 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
67 const uint8_t *buf, int nb_sectors);
69 BlockDriverState *bdrv_first;
71 static BlockDriver *first_drv;
73 int path_is_absolute(const char *path)
75 const char *p;
76 #ifdef _WIN32
77 /* specific case for names like: "\\.\d:" */
78 if (*path == '/' || *path == '\\')
79 return 1;
80 #endif
81 p = strchr(path, ':');
82 if (p)
83 p++;
84 else
85 p = path;
86 #ifdef _WIN32
87 return (*p == '/' || *p == '\\');
88 #else
89 return (*p == '/');
90 #endif
93 /* if filename is absolute, just copy it to dest. Otherwise, build a
94 path to it by considering it is relative to base_path. URL are
95 supported. */
96 void path_combine(char *dest, int dest_size,
97 const char *base_path,
98 const char *filename)
100 const char *p, *p1;
101 int len;
103 if (dest_size <= 0)
104 return;
105 if (path_is_absolute(filename)) {
106 pstrcpy(dest, dest_size, filename);
107 } else {
108 p = strchr(base_path, ':');
109 if (p)
110 p++;
111 else
112 p = base_path;
113 p1 = strrchr(base_path, '/');
114 #ifdef _WIN32
116 const char *p2;
117 p2 = strrchr(base_path, '\\');
118 if (!p1 || p2 > p1)
119 p1 = p2;
121 #endif
122 if (p1)
123 p1++;
124 else
125 p1 = base_path;
126 if (p1 > p)
127 p = p1;
128 len = p - base_path;
129 if (len > dest_size - 1)
130 len = dest_size - 1;
131 memcpy(dest, base_path, len);
132 dest[len] = '\0';
133 pstrcat(dest, dest_size, filename);
138 static void bdrv_register(BlockDriver *bdrv)
140 if (!bdrv->bdrv_aio_read) {
141 /* add AIO emulation layer */
142 bdrv->bdrv_aio_read = bdrv_aio_read_em;
143 bdrv->bdrv_aio_write = bdrv_aio_write_em;
144 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
145 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
146 } else if (!bdrv->bdrv_read) {
147 /* add synchronous IO emulation layer */
148 bdrv->bdrv_read = bdrv_read_em;
149 bdrv->bdrv_write = bdrv_write_em;
151 bdrv->next = first_drv;
152 first_drv = bdrv;
155 /* create a new block device (by default it is empty) */
156 BlockDriverState *bdrv_new(const char *device_name)
158 BlockDriverState **pbs, *bs;
160 bs = qemu_mallocz(sizeof(BlockDriverState));
161 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
162 if (device_name[0] != '\0') {
163 /* insert at the end */
164 pbs = &bdrv_first;
165 while (*pbs != NULL)
166 pbs = &(*pbs)->next;
167 *pbs = bs;
169 return bs;
172 BlockDriver *bdrv_find_format(const char *format_name)
174 BlockDriver *drv1;
175 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
176 if (!strcmp(drv1->format_name, format_name))
177 return drv1;
179 return NULL;
182 int bdrv_create(BlockDriver *drv,
183 const char *filename, int64_t size_in_sectors,
184 const char *backing_file, int flags)
186 if (!drv->bdrv_create)
187 return -ENOTSUP;
188 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
191 #ifdef _WIN32
192 void get_tmp_filename(char *filename, int size)
194 char temp_dir[MAX_PATH];
196 GetTempPath(MAX_PATH, temp_dir);
197 GetTempFileName(temp_dir, "qem", 0, filename);
199 #else
200 void get_tmp_filename(char *filename, int size)
202 int fd;
203 const char *tmpdir;
204 /* XXX: race condition possible */
205 tmpdir = getenv("TMPDIR");
206 if (!tmpdir)
207 tmpdir = "/tmp";
208 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
209 fd = mkstemp(filename);
210 close(fd);
212 #endif
214 #ifdef _WIN32
215 static int is_windows_drive_prefix(const char *filename)
217 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
218 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
219 filename[1] == ':');
222 static int is_windows_drive(const char *filename)
224 if (is_windows_drive_prefix(filename) &&
225 filename[2] == '\0')
226 return 1;
227 if (strstart(filename, "\\\\.\\", NULL) ||
228 strstart(filename, "//./", NULL))
229 return 1;
230 return 0;
232 #endif
234 static BlockDriver *find_protocol(const char *filename)
236 BlockDriver *drv1;
237 char protocol[128];
238 int len;
239 const char *p;
241 #ifdef _WIN32
242 if (is_windows_drive(filename) ||
243 is_windows_drive_prefix(filename))
244 return &bdrv_raw;
245 #endif
246 p = strchr(filename, ':');
247 if (!p)
248 return &bdrv_raw;
249 len = p - filename;
250 if (len > sizeof(protocol) - 1)
251 len = sizeof(protocol) - 1;
252 memcpy(protocol, filename, len);
253 protocol[len] = '\0';
254 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
255 if (drv1->protocol_name &&
256 !strcmp(drv1->protocol_name, protocol))
257 return drv1;
259 return NULL;
262 /* XXX: force raw format if block or character device ? It would
263 simplify the BSD case */
264 static BlockDriver *find_image_format(const char *filename)
266 int ret, score, score_max;
267 BlockDriver *drv1, *drv;
268 uint8_t buf[2048];
269 BlockDriverState *bs;
271 /* detect host devices. By convention, /dev/cdrom[N] is always
272 recognized as a host CDROM */
273 if (strstart(filename, "/dev/cdrom", NULL))
274 return &bdrv_host_device;
275 #ifdef _WIN32
276 if (is_windows_drive(filename))
277 return &bdrv_host_device;
278 #else
280 struct stat st;
281 if (stat(filename, &st) >= 0 &&
282 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
283 return &bdrv_host_device;
286 #endif
288 drv = find_protocol(filename);
289 /* no need to test disk image formats for vvfat */
290 if (drv == &bdrv_vvfat)
291 return drv;
293 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
294 if (ret < 0)
295 return NULL;
296 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
297 bdrv_delete(bs);
298 if (ret < 0) {
299 return NULL;
302 score_max = 0;
303 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
304 if (drv1->bdrv_probe) {
305 score = drv1->bdrv_probe(buf, ret, filename);
306 if (score > score_max) {
307 score_max = score;
308 drv = drv1;
312 return drv;
315 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
317 BlockDriverState *bs;
318 int ret;
320 bs = bdrv_new("");
321 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
322 if (ret < 0) {
323 bdrv_delete(bs);
324 return ret;
326 bs->growable = 1;
327 *pbs = bs;
328 return 0;
331 int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
333 return bdrv_open2(bs, filename, flags, NULL);
336 int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
337 BlockDriver *drv)
339 int ret, open_flags;
340 char tmp_filename[PATH_MAX];
341 char backing_filename[PATH_MAX];
343 bs->read_only = 0;
344 bs->is_temporary = 0;
345 bs->encrypted = 0;
346 bs->valid_key = 0;
348 if (flags & BDRV_O_SNAPSHOT) {
349 BlockDriverState *bs1;
350 int64_t total_size;
351 int is_protocol = 0;
353 /* if snapshot, we create a temporary backing file and open it
354 instead of opening 'filename' directly */
356 /* if there is a backing file, use it */
357 bs1 = bdrv_new("");
358 ret = bdrv_open(bs1, filename, 0);
359 if (ret < 0) {
360 bdrv_delete(bs1);
361 return ret;
363 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
365 if (bs1->drv && bs1->drv->protocol_name)
366 is_protocol = 1;
368 bdrv_delete(bs1);
370 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
372 /* Real path is meaningless for protocols */
373 if (is_protocol)
374 snprintf(backing_filename, sizeof(backing_filename),
375 "%s", filename);
376 else
377 realpath(filename, backing_filename);
379 ret = bdrv_create(&bdrv_qcow2, tmp_filename,
380 total_size, backing_filename, 0);
381 if (ret < 0) {
382 return ret;
384 filename = tmp_filename;
385 bs->is_temporary = 1;
388 pstrcpy(bs->filename, sizeof(bs->filename), filename);
389 if (flags & BDRV_O_FILE) {
390 drv = find_protocol(filename);
391 } else if (!drv) {
392 drv = find_image_format(filename);
394 if (!drv) {
395 ret = -ENOENT;
396 goto unlink_and_fail;
398 bs->drv = drv;
399 bs->opaque = qemu_mallocz(drv->instance_size);
400 /* Note: for compatibility, we open disk image files as RDWR, and
401 RDONLY as fallback */
402 if (!(flags & BDRV_O_FILE))
403 open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
404 else
405 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
406 ret = drv->bdrv_open(bs, filename, open_flags);
407 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
408 ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
409 bs->read_only = 1;
411 if (ret < 0) {
412 qemu_free(bs->opaque);
413 bs->opaque = NULL;
414 bs->drv = NULL;
415 unlink_and_fail:
416 if (bs->is_temporary)
417 unlink(filename);
418 return ret;
420 if (drv->bdrv_getlength) {
421 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
423 #ifndef _WIN32
424 if (bs->is_temporary) {
425 unlink(filename);
427 #endif
428 if (bs->backing_file[0] != '\0') {
429 /* if there is a backing file, use it */
430 bs->backing_hd = bdrv_new("");
431 path_combine(backing_filename, sizeof(backing_filename),
432 filename, bs->backing_file);
433 ret = bdrv_open(bs->backing_hd, backing_filename, open_flags);
434 if (ret < 0) {
435 bdrv_close(bs);
436 return ret;
440 if (!bdrv_key_required(bs)) {
441 /* call the change callback */
442 bs->media_changed = 1;
443 if (bs->change_cb)
444 bs->change_cb(bs->change_opaque);
446 return 0;
449 void bdrv_close(BlockDriverState *bs)
451 if (bs->drv) {
452 if (bs->backing_hd)
453 bdrv_delete(bs->backing_hd);
454 bs->drv->bdrv_close(bs);
455 qemu_free(bs->opaque);
456 #ifdef _WIN32
457 if (bs->is_temporary) {
458 unlink(bs->filename);
460 #endif
461 bs->opaque = NULL;
462 bs->drv = NULL;
464 /* call the change callback */
465 bs->media_changed = 1;
466 if (bs->change_cb)
467 bs->change_cb(bs->change_opaque);
471 void bdrv_delete(BlockDriverState *bs)
473 BlockDriverState **pbs;
475 pbs = &bdrv_first;
476 while (*pbs != bs && *pbs != NULL)
477 pbs = &(*pbs)->next;
478 if (*pbs == bs)
479 *pbs = bs->next;
481 bdrv_close(bs);
482 qemu_free(bs);
485 /* commit COW file into the raw image */
486 int bdrv_commit(BlockDriverState *bs)
488 BlockDriver *drv = bs->drv;
489 int64_t i, total_sectors;
490 int n, j;
491 unsigned char sector[512];
493 if (!drv)
494 return -ENOMEDIUM;
496 if (bs->read_only) {
497 return -EACCES;
500 if (!bs->backing_hd) {
501 return -ENOTSUP;
504 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
505 for (i = 0; i < total_sectors;) {
506 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
507 for(j = 0; j < n; j++) {
508 if (bdrv_read(bs, i, sector, 1) != 0) {
509 return -EIO;
512 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
513 return -EIO;
515 i++;
517 } else {
518 i += n;
522 if (drv->bdrv_make_empty)
523 return drv->bdrv_make_empty(bs);
525 return 0;
528 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
529 size_t size)
531 int64_t len;
533 if (!bdrv_is_inserted(bs))
534 return -ENOMEDIUM;
536 if (bs->growable)
537 return 0;
539 len = bdrv_getlength(bs);
541 if ((offset + size) > len)
542 return -EIO;
544 return 0;
547 static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
548 int nb_sectors)
550 int64_t offset;
552 /* Deal with byte accesses */
553 if (sector_num < 0)
554 offset = -sector_num;
555 else
556 offset = sector_num * 512;
558 return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
561 /* return < 0 if error. See bdrv_write() for the return codes */
562 int bdrv_read(BlockDriverState *bs, int64_t sector_num,
563 uint8_t *buf, int nb_sectors)
565 BlockDriver *drv = bs->drv;
567 if (!drv)
568 return -ENOMEDIUM;
569 if (bdrv_check_request(bs, sector_num, nb_sectors))
570 return -EIO;
572 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
575 /* Return < 0 if error. Important errors are:
576 -EIO generic I/O error (may happen for all errors)
577 -ENOMEDIUM No media inserted.
578 -EINVAL Invalid sector number or nb_sectors
579 -EACCES Trying to write a read-only device
581 int bdrv_write(BlockDriverState *bs, int64_t sector_num,
582 const uint8_t *buf, int nb_sectors)
584 BlockDriver *drv = bs->drv;
585 if (!bs->drv)
586 return -ENOMEDIUM;
587 if (bs->read_only)
588 return -EACCES;
589 if (bdrv_check_request(bs, sector_num, nb_sectors))
590 return -EIO;
592 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
595 int bdrv_pread(BlockDriverState *bs, int64_t offset,
596 void *buf, int count1)
598 uint8_t tmp_buf[SECTOR_SIZE];
599 int len, nb_sectors, count;
600 int64_t sector_num;
602 count = count1;
603 /* first read to align to sector start */
604 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
605 if (len > count)
606 len = count;
607 sector_num = offset >> SECTOR_BITS;
608 if (len > 0) {
609 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
610 return -EIO;
611 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
612 count -= len;
613 if (count == 0)
614 return count1;
615 sector_num++;
616 buf += len;
619 /* read the sectors "in place" */
620 nb_sectors = count >> SECTOR_BITS;
621 if (nb_sectors > 0) {
622 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
623 return -EIO;
624 sector_num += nb_sectors;
625 len = nb_sectors << SECTOR_BITS;
626 buf += len;
627 count -= len;
630 /* add data from the last sector */
631 if (count > 0) {
632 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
633 return -EIO;
634 memcpy(buf, tmp_buf, count);
636 return count1;
639 int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
640 const void *buf, int count1)
642 uint8_t tmp_buf[SECTOR_SIZE];
643 int len, nb_sectors, count;
644 int64_t sector_num;
646 count = count1;
647 /* first write to align to sector start */
648 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
649 if (len > count)
650 len = count;
651 sector_num = offset >> SECTOR_BITS;
652 if (len > 0) {
653 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
654 return -EIO;
655 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
656 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
657 return -EIO;
658 count -= len;
659 if (count == 0)
660 return count1;
661 sector_num++;
662 buf += len;
665 /* write the sectors "in place" */
666 nb_sectors = count >> SECTOR_BITS;
667 if (nb_sectors > 0) {
668 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
669 return -EIO;
670 sector_num += nb_sectors;
671 len = nb_sectors << SECTOR_BITS;
672 buf += len;
673 count -= len;
676 /* add data from the last sector */
677 if (count > 0) {
678 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
679 return -EIO;
680 memcpy(tmp_buf, buf, count);
681 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
682 return -EIO;
684 return count1;
688 * Truncate file to 'offset' bytes (needed only for file protocols)
690 int bdrv_truncate(BlockDriverState *bs, int64_t offset)
692 BlockDriver *drv = bs->drv;
693 if (!drv)
694 return -ENOMEDIUM;
695 if (!drv->bdrv_truncate)
696 return -ENOTSUP;
697 return drv->bdrv_truncate(bs, offset);
701 * Length of a file in bytes. Return < 0 if error or unknown.
703 int64_t bdrv_getlength(BlockDriverState *bs)
705 BlockDriver *drv = bs->drv;
706 if (!drv)
707 return -ENOMEDIUM;
708 if (!drv->bdrv_getlength) {
709 /* legacy mode */
710 return bs->total_sectors * SECTOR_SIZE;
712 return drv->bdrv_getlength(bs);
715 /* return 0 as number of sectors if no device present or error */
716 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
718 int64_t length;
719 length = bdrv_getlength(bs);
720 if (length < 0)
721 length = 0;
722 else
723 length = length >> SECTOR_BITS;
724 *nb_sectors_ptr = length;
727 struct partition {
728 uint8_t boot_ind; /* 0x80 - active */
729 uint8_t head; /* starting head */
730 uint8_t sector; /* starting sector */
731 uint8_t cyl; /* starting cylinder */
732 uint8_t sys_ind; /* What partition type */
733 uint8_t end_head; /* end head */
734 uint8_t end_sector; /* end sector */
735 uint8_t end_cyl; /* end cylinder */
736 uint32_t start_sect; /* starting sector counting from 0 */
737 uint32_t nr_sects; /* nr of sectors in partition */
738 } __attribute__((packed));
740 /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
741 static int guess_disk_lchs(BlockDriverState *bs,
742 int *pcylinders, int *pheads, int *psectors)
744 uint8_t *buf;
745 int ret, i, heads, sectors, cylinders;
746 struct partition *p;
747 uint32_t nr_sects;
748 uint64_t nb_sectors;
750 buf = qemu_memalign(512, 512);
751 if (buf == NULL)
752 return -1;
754 bdrv_get_geometry(bs, &nb_sectors);
756 ret = bdrv_read(bs, 0, buf, 1);
757 if (ret < 0)
758 return -1;
759 /* test msdos magic */
760 if (buf[510] != 0x55 || buf[511] != 0xaa) {
761 qemu_free(buf);
762 return -1;
764 for(i = 0; i < 4; i++) {
765 p = ((struct partition *)(buf + 0x1be)) + i;
766 nr_sects = le32_to_cpu(p->nr_sects);
767 if (nr_sects && p->end_head) {
768 /* We make the assumption that the partition terminates on
769 a cylinder boundary */
770 heads = p->end_head + 1;
771 sectors = p->end_sector & 63;
772 if (sectors == 0)
773 continue;
774 cylinders = nb_sectors / (heads * sectors);
775 if (cylinders < 1 || cylinders > 16383)
776 continue;
777 *pheads = heads;
778 *psectors = sectors;
779 *pcylinders = cylinders;
780 #if 0
781 printf("guessed geometry: LCHS=%d %d %d\n",
782 cylinders, heads, sectors);
783 #endif
784 qemu_free(buf);
785 return 0;
788 qemu_free(buf);
789 return -1;
792 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
794 int translation, lba_detected = 0;
795 int cylinders, heads, secs;
796 uint64_t nb_sectors;
798 /* if a geometry hint is available, use it */
799 bdrv_get_geometry(bs, &nb_sectors);
800 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
801 translation = bdrv_get_translation_hint(bs);
802 if (cylinders != 0) {
803 *pcyls = cylinders;
804 *pheads = heads;
805 *psecs = secs;
806 } else {
807 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
808 if (heads > 16) {
809 /* if heads > 16, it means that a BIOS LBA
810 translation was active, so the default
811 hardware geometry is OK */
812 lba_detected = 1;
813 goto default_geometry;
814 } else {
815 *pcyls = cylinders;
816 *pheads = heads;
817 *psecs = secs;
818 /* disable any translation to be in sync with
819 the logical geometry */
820 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
821 bdrv_set_translation_hint(bs,
822 BIOS_ATA_TRANSLATION_NONE);
825 } else {
826 default_geometry:
827 /* if no geometry, use a standard physical disk geometry */
828 cylinders = nb_sectors / (16 * 63);
830 if (cylinders > 16383)
831 cylinders = 16383;
832 else if (cylinders < 2)
833 cylinders = 2;
834 *pcyls = cylinders;
835 *pheads = 16;
836 *psecs = 63;
837 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
838 if ((*pcyls * *pheads) <= 131072) {
839 bdrv_set_translation_hint(bs,
840 BIOS_ATA_TRANSLATION_LARGE);
841 } else {
842 bdrv_set_translation_hint(bs,
843 BIOS_ATA_TRANSLATION_LBA);
847 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
851 void bdrv_set_geometry_hint(BlockDriverState *bs,
852 int cyls, int heads, int secs)
854 bs->cyls = cyls;
855 bs->heads = heads;
856 bs->secs = secs;
859 void bdrv_set_type_hint(BlockDriverState *bs, int type)
861 bs->type = type;
862 bs->removable = ((type == BDRV_TYPE_CDROM ||
863 type == BDRV_TYPE_FLOPPY));
866 void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
868 bs->translation = translation;
871 void bdrv_get_geometry_hint(BlockDriverState *bs,
872 int *pcyls, int *pheads, int *psecs)
874 *pcyls = bs->cyls;
875 *pheads = bs->heads;
876 *psecs = bs->secs;
879 int bdrv_get_type_hint(BlockDriverState *bs)
881 return bs->type;
884 int bdrv_get_translation_hint(BlockDriverState *bs)
886 return bs->translation;
889 int bdrv_is_removable(BlockDriverState *bs)
891 return bs->removable;
894 int bdrv_is_read_only(BlockDriverState *bs)
896 return bs->read_only;
899 int bdrv_is_sg(BlockDriverState *bs)
901 return bs->sg;
904 /* XXX: no longer used */
905 void bdrv_set_change_cb(BlockDriverState *bs,
906 void (*change_cb)(void *opaque), void *opaque)
908 bs->change_cb = change_cb;
909 bs->change_opaque = opaque;
912 int bdrv_is_encrypted(BlockDriverState *bs)
914 if (bs->backing_hd && bs->backing_hd->encrypted)
915 return 1;
916 return bs->encrypted;
919 int bdrv_key_required(BlockDriverState *bs)
921 BlockDriverState *backing_hd = bs->backing_hd;
923 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
924 return 1;
925 return (bs->encrypted && !bs->valid_key);
928 int bdrv_set_key(BlockDriverState *bs, const char *key)
930 int ret;
931 if (bs->backing_hd && bs->backing_hd->encrypted) {
932 ret = bdrv_set_key(bs->backing_hd, key);
933 if (ret < 0)
934 return ret;
935 if (!bs->encrypted)
936 return 0;
938 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
939 return -1;
940 ret = bs->drv->bdrv_set_key(bs, key);
941 if (ret < 0) {
942 bs->valid_key = 0;
943 } else if (!bs->valid_key) {
944 bs->valid_key = 1;
945 /* call the change callback now, we skipped it on open */
946 bs->media_changed = 1;
947 if (bs->change_cb)
948 bs->change_cb(bs->change_opaque);
950 return ret;
953 void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
955 if (!bs->drv) {
956 buf[0] = '\0';
957 } else {
958 pstrcpy(buf, buf_size, bs->drv->format_name);
962 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
963 void *opaque)
965 BlockDriver *drv;
967 for (drv = first_drv; drv != NULL; drv = drv->next) {
968 it(opaque, drv->format_name);
972 BlockDriverState *bdrv_find(const char *name)
974 BlockDriverState *bs;
976 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
977 if (!strcmp(name, bs->device_name))
978 return bs;
980 return NULL;
983 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
985 BlockDriverState *bs;
987 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
988 it(opaque, bs);
992 const char *bdrv_get_device_name(BlockDriverState *bs)
994 return bs->device_name;
997 void bdrv_flush(BlockDriverState *bs)
999 if (bs->drv->bdrv_flush)
1000 bs->drv->bdrv_flush(bs);
1001 if (bs->backing_hd)
1002 bdrv_flush(bs->backing_hd);
1005 void bdrv_flush_all(void)
1007 BlockDriverState *bs;
1009 for (bs = bdrv_first; bs != NULL; bs = bs->next)
1010 if (bs->drv && !bdrv_is_read_only(bs) &&
1011 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1012 bdrv_flush(bs);
1016 * Returns true iff the specified sector is present in the disk image. Drivers
1017 * not implementing the functionality are assumed to not support backing files,
1018 * hence all their sectors are reported as allocated.
1020 * 'pnum' is set to the number of sectors (including and immediately following
1021 * the specified sector) that are known to be in the same
1022 * allocated/unallocated state.
1024 * 'nb_sectors' is the max value 'pnum' should be set to.
1026 int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1027 int *pnum)
1029 int64_t n;
1030 if (!bs->drv->bdrv_is_allocated) {
1031 if (sector_num >= bs->total_sectors) {
1032 *pnum = 0;
1033 return 0;
1035 n = bs->total_sectors - sector_num;
1036 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1037 return 1;
1039 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1042 void bdrv_info(Monitor *mon)
1044 BlockDriverState *bs;
1046 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1047 monitor_printf(mon, "%s:", bs->device_name);
1048 monitor_printf(mon, " type=");
1049 switch(bs->type) {
1050 case BDRV_TYPE_HD:
1051 monitor_printf(mon, "hd");
1052 break;
1053 case BDRV_TYPE_CDROM:
1054 monitor_printf(mon, "cdrom");
1055 break;
1056 case BDRV_TYPE_FLOPPY:
1057 monitor_printf(mon, "floppy");
1058 break;
1060 monitor_printf(mon, " removable=%d", bs->removable);
1061 if (bs->removable) {
1062 monitor_printf(mon, " locked=%d", bs->locked);
1064 if (bs->drv) {
1065 monitor_printf(mon, " file=");
1066 monitor_print_filename(mon, bs->filename);
1067 if (bs->backing_file[0] != '\0') {
1068 monitor_printf(mon, " backing_file=");
1069 monitor_print_filename(mon, bs->backing_file);
1071 monitor_printf(mon, " ro=%d", bs->read_only);
1072 monitor_printf(mon, " drv=%s", bs->drv->format_name);
1073 monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1074 } else {
1075 monitor_printf(mon, " [not inserted]");
1077 monitor_printf(mon, "\n");
1081 /* The "info blockstats" command. */
1082 void bdrv_info_stats(Monitor *mon)
1084 BlockDriverState *bs;
1086 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1087 monitor_printf(mon, "%s:"
1088 " rd_bytes=%" PRIu64
1089 " wr_bytes=%" PRIu64
1090 " rd_operations=%" PRIu64
1091 " wr_operations=%" PRIu64
1092 "\n",
1093 bs->device_name,
1094 bs->rd_bytes, bs->wr_bytes,
1095 bs->rd_ops, bs->wr_ops);
1099 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1101 if (bs->backing_hd && bs->backing_hd->encrypted)
1102 return bs->backing_file;
1103 else if (bs->encrypted)
1104 return bs->filename;
1105 else
1106 return NULL;
1109 void bdrv_get_backing_filename(BlockDriverState *bs,
1110 char *filename, int filename_size)
1112 if (!bs->backing_hd) {
1113 pstrcpy(filename, filename_size, "");
1114 } else {
1115 pstrcpy(filename, filename_size, bs->backing_file);
1119 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1120 const uint8_t *buf, int nb_sectors)
1122 BlockDriver *drv = bs->drv;
1123 if (!drv)
1124 return -ENOMEDIUM;
1125 if (!drv->bdrv_write_compressed)
1126 return -ENOTSUP;
1127 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1130 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1132 BlockDriver *drv = bs->drv;
1133 if (!drv)
1134 return -ENOMEDIUM;
1135 if (!drv->bdrv_get_info)
1136 return -ENOTSUP;
1137 memset(bdi, 0, sizeof(*bdi));
1138 return drv->bdrv_get_info(bs, bdi);
1141 /**************************************************************/
1142 /* handling of snapshots */
1144 int bdrv_snapshot_create(BlockDriverState *bs,
1145 QEMUSnapshotInfo *sn_info)
1147 BlockDriver *drv = bs->drv;
1148 if (!drv)
1149 return -ENOMEDIUM;
1150 if (!drv->bdrv_snapshot_create)
1151 return -ENOTSUP;
1152 return drv->bdrv_snapshot_create(bs, sn_info);
1155 int bdrv_snapshot_goto(BlockDriverState *bs,
1156 const char *snapshot_id)
1158 BlockDriver *drv = bs->drv;
1159 if (!drv)
1160 return -ENOMEDIUM;
1161 if (!drv->bdrv_snapshot_goto)
1162 return -ENOTSUP;
1163 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1166 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1168 BlockDriver *drv = bs->drv;
1169 if (!drv)
1170 return -ENOMEDIUM;
1171 if (!drv->bdrv_snapshot_delete)
1172 return -ENOTSUP;
1173 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1176 int bdrv_snapshot_list(BlockDriverState *bs,
1177 QEMUSnapshotInfo **psn_info)
1179 BlockDriver *drv = bs->drv;
1180 if (!drv)
1181 return -ENOMEDIUM;
1182 if (!drv->bdrv_snapshot_list)
1183 return -ENOTSUP;
1184 return drv->bdrv_snapshot_list(bs, psn_info);
1187 #define NB_SUFFIXES 4
1189 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1191 static const char suffixes[NB_SUFFIXES] = "KMGT";
1192 int64_t base;
1193 int i;
1195 if (size <= 999) {
1196 snprintf(buf, buf_size, "%" PRId64, size);
1197 } else {
1198 base = 1024;
1199 for(i = 0; i < NB_SUFFIXES; i++) {
1200 if (size < (10 * base)) {
1201 snprintf(buf, buf_size, "%0.1f%c",
1202 (double)size / base,
1203 suffixes[i]);
1204 break;
1205 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1206 snprintf(buf, buf_size, "%" PRId64 "%c",
1207 ((size + (base >> 1)) / base),
1208 suffixes[i]);
1209 break;
1211 base = base * 1024;
1214 return buf;
1217 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1219 char buf1[128], date_buf[128], clock_buf[128];
1220 #ifdef _WIN32
1221 struct tm *ptm;
1222 #else
1223 struct tm tm;
1224 #endif
1225 time_t ti;
1226 int64_t secs;
1228 if (!sn) {
1229 snprintf(buf, buf_size,
1230 "%-10s%-20s%7s%20s%15s",
1231 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1232 } else {
1233 ti = sn->date_sec;
1234 #ifdef _WIN32
1235 ptm = localtime(&ti);
1236 strftime(date_buf, sizeof(date_buf),
1237 "%Y-%m-%d %H:%M:%S", ptm);
1238 #else
1239 localtime_r(&ti, &tm);
1240 strftime(date_buf, sizeof(date_buf),
1241 "%Y-%m-%d %H:%M:%S", &tm);
1242 #endif
1243 secs = sn->vm_clock_nsec / 1000000000;
1244 snprintf(clock_buf, sizeof(clock_buf),
1245 "%02d:%02d:%02d.%03d",
1246 (int)(secs / 3600),
1247 (int)((secs / 60) % 60),
1248 (int)(secs % 60),
1249 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1250 snprintf(buf, buf_size,
1251 "%-10s%-20s%7s%20s%15s",
1252 sn->id_str, sn->name,
1253 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1254 date_buf,
1255 clock_buf);
1257 return buf;
1261 /**************************************************************/
1262 /* async I/Os */
1264 typedef struct VectorTranslationState {
1265 QEMUIOVector *iov;
1266 uint8_t *bounce;
1267 int is_write;
1268 BlockDriverAIOCB *aiocb;
1269 BlockDriverAIOCB *this_aiocb;
1270 } VectorTranslationState;
1272 static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
1274 VectorTranslationState *s = opaque;
1276 if (!s->is_write) {
1277 qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
1279 qemu_vfree(s->bounce);
1280 s->this_aiocb->cb(s->this_aiocb->opaque, ret);
1281 qemu_aio_release(s->this_aiocb);
1284 static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1285 int64_t sector_num,
1286 QEMUIOVector *iov,
1287 int nb_sectors,
1288 BlockDriverCompletionFunc *cb,
1289 void *opaque,
1290 int is_write)
1293 VectorTranslationState *s = qemu_mallocz(sizeof(*s));
1294 BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
1296 s->this_aiocb = aiocb;
1297 s->iov = iov;
1298 s->bounce = qemu_memalign(512, nb_sectors * 512);
1299 s->is_write = is_write;
1300 if (is_write) {
1301 qemu_iovec_to_buffer(s->iov, s->bounce);
1302 s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
1303 bdrv_aio_rw_vector_cb, s);
1304 } else {
1305 s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
1306 bdrv_aio_rw_vector_cb, s);
1308 return aiocb;
1311 BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1312 QEMUIOVector *iov, int nb_sectors,
1313 BlockDriverCompletionFunc *cb, void *opaque)
1315 if (bdrv_check_request(bs, sector_num, nb_sectors))
1316 return NULL;
1318 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1319 cb, opaque, 0);
1322 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1323 QEMUIOVector *iov, int nb_sectors,
1324 BlockDriverCompletionFunc *cb, void *opaque)
1326 if (bdrv_check_request(bs, sector_num, nb_sectors))
1327 return NULL;
1329 return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
1330 cb, opaque, 1);
1333 BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1334 uint8_t *buf, int nb_sectors,
1335 BlockDriverCompletionFunc *cb, void *opaque)
1337 BlockDriver *drv = bs->drv;
1338 BlockDriverAIOCB *ret;
1340 if (!drv)
1341 return NULL;
1342 if (bdrv_check_request(bs, sector_num, nb_sectors))
1343 return NULL;
1345 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1347 if (ret) {
1348 /* Update stats even though technically transfer has not happened. */
1349 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1350 bs->rd_ops ++;
1353 return ret;
1356 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1357 const uint8_t *buf, int nb_sectors,
1358 BlockDriverCompletionFunc *cb, void *opaque)
1360 BlockDriver *drv = bs->drv;
1361 BlockDriverAIOCB *ret;
1363 if (!drv)
1364 return NULL;
1365 if (bs->read_only)
1366 return NULL;
1367 if (bdrv_check_request(bs, sector_num, nb_sectors))
1368 return NULL;
1370 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1372 if (ret) {
1373 /* Update stats even though technically transfer has not happened. */
1374 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1375 bs->wr_ops ++;
1378 return ret;
1381 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1383 BlockDriver *drv = acb->bs->drv;
1385 if (acb->cb == bdrv_aio_rw_vector_cb) {
1386 VectorTranslationState *s = acb->opaque;
1387 acb = s->aiocb;
1390 drv->bdrv_aio_cancel(acb);
1394 /**************************************************************/
1395 /* async block device emulation */
1397 static void bdrv_aio_bh_cb(void *opaque)
1399 BlockDriverAIOCBSync *acb = opaque;
1400 acb->common.cb(acb->common.opaque, acb->ret);
1401 qemu_aio_release(acb);
1404 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1405 int64_t sector_num, uint8_t *buf, int nb_sectors,
1406 BlockDriverCompletionFunc *cb, void *opaque)
1408 BlockDriverAIOCBSync *acb;
1409 int ret;
1411 acb = qemu_aio_get(bs, cb, opaque);
1412 if (!acb->bh)
1413 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1414 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1415 acb->ret = ret;
1416 qemu_bh_schedule(acb->bh);
1417 return &acb->common;
1420 static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1421 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1422 BlockDriverCompletionFunc *cb, void *opaque)
1424 BlockDriverAIOCBSync *acb;
1425 int ret;
1427 acb = qemu_aio_get(bs, cb, opaque);
1428 if (!acb->bh)
1429 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1430 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1431 acb->ret = ret;
1432 qemu_bh_schedule(acb->bh);
1433 return &acb->common;
1436 static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1438 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1439 qemu_bh_cancel(acb->bh);
1440 qemu_aio_release(acb);
1443 /**************************************************************/
1444 /* sync block device emulation */
1446 static void bdrv_rw_em_cb(void *opaque, int ret)
1448 *(int *)opaque = ret;
1451 #define NOT_DONE 0x7fffffff
1453 static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1454 uint8_t *buf, int nb_sectors)
1456 int async_ret;
1457 BlockDriverAIOCB *acb;
1459 async_ret = NOT_DONE;
1460 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1461 bdrv_rw_em_cb, &async_ret);
1462 if (acb == NULL)
1463 return -1;
1465 while (async_ret == NOT_DONE) {
1466 qemu_aio_wait();
1469 return async_ret;
1472 static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1473 const uint8_t *buf, int nb_sectors)
1475 int async_ret;
1476 BlockDriverAIOCB *acb;
1478 async_ret = NOT_DONE;
1479 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1480 bdrv_rw_em_cb, &async_ret);
1481 if (acb == NULL)
1482 return -1;
1483 while (async_ret == NOT_DONE) {
1484 qemu_aio_wait();
1486 return async_ret;
1489 void bdrv_init(void)
1491 bdrv_register(&bdrv_raw);
1492 bdrv_register(&bdrv_host_device);
1493 #ifndef _WIN32
1494 bdrv_register(&bdrv_cow);
1495 #endif
1496 bdrv_register(&bdrv_qcow);
1497 bdrv_register(&bdrv_vmdk);
1498 bdrv_register(&bdrv_cloop);
1499 bdrv_register(&bdrv_dmg);
1500 bdrv_register(&bdrv_bochs);
1501 bdrv_register(&bdrv_vpc);
1502 bdrv_register(&bdrv_vvfat);
1503 bdrv_register(&bdrv_qcow2);
1504 bdrv_register(&bdrv_parallels);
1505 bdrv_register(&bdrv_nbd);
1508 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1509 void *opaque)
1511 BlockDriver *drv;
1512 BlockDriverAIOCB *acb;
1514 drv = bs->drv;
1515 if (drv->free_aiocb) {
1516 acb = drv->free_aiocb;
1517 drv->free_aiocb = acb->next;
1518 } else {
1519 acb = qemu_mallocz(drv->aiocb_size);
1521 acb->bs = bs;
1522 acb->cb = cb;
1523 acb->opaque = opaque;
1524 return acb;
1527 void qemu_aio_release(void *p)
1529 BlockDriverAIOCB *acb = p;
1530 BlockDriver *drv = acb->bs->drv;
1531 acb->next = drv->free_aiocb;
1532 drv->free_aiocb = acb;
1535 /**************************************************************/
1536 /* removable device support */
1539 * Return TRUE if the media is present
1541 int bdrv_is_inserted(BlockDriverState *bs)
1543 BlockDriver *drv = bs->drv;
1544 int ret;
1545 if (!drv)
1546 return 0;
1547 if (!drv->bdrv_is_inserted)
1548 return 1;
1549 ret = drv->bdrv_is_inserted(bs);
1550 return ret;
1554 * Return TRUE if the media changed since the last call to this
1555 * function. It is currently only used for floppy disks
1557 int bdrv_media_changed(BlockDriverState *bs)
1559 BlockDriver *drv = bs->drv;
1560 int ret;
1562 if (!drv || !drv->bdrv_media_changed)
1563 ret = -ENOTSUP;
1564 else
1565 ret = drv->bdrv_media_changed(bs);
1566 if (ret == -ENOTSUP)
1567 ret = bs->media_changed;
1568 bs->media_changed = 0;
1569 return ret;
1573 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1575 void bdrv_eject(BlockDriverState *bs, int eject_flag)
1577 BlockDriver *drv = bs->drv;
1578 int ret;
1580 if (!drv || !drv->bdrv_eject) {
1581 ret = -ENOTSUP;
1582 } else {
1583 ret = drv->bdrv_eject(bs, eject_flag);
1585 if (ret == -ENOTSUP) {
1586 if (eject_flag)
1587 bdrv_close(bs);
1591 int bdrv_is_locked(BlockDriverState *bs)
1593 return bs->locked;
1597 * Lock or unlock the media (if it is locked, the user won't be able
1598 * to eject it manually).
1600 void bdrv_set_locked(BlockDriverState *bs, int locked)
1602 BlockDriver *drv = bs->drv;
1604 bs->locked = locked;
1605 if (drv && drv->bdrv_set_locked) {
1606 drv->bdrv_set_locked(bs, locked);
1610 /* needed for generic scsi interface */
1612 int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1614 BlockDriver *drv = bs->drv;
1616 if (drv && drv->bdrv_ioctl)
1617 return drv->bdrv_ioctl(bs, req, buf);
1618 return -ENOTSUP;
1621 int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count)
1623 return bs->drv->bdrv_sg_send_command(bs, buf, count);
1626 int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count)
1628 return bs->drv->bdrv_sg_recv_response(bs, buf, count);
1631 BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
1632 BlockDriverCompletionFunc *cb, void *opaque)
1634 return bs->drv->bdrv_sg_aio_read(bs, buf, count, cb, opaque);
1637 BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
1638 BlockDriverCompletionFunc *cb, void *opaque)
1640 return bs->drv->bdrv_sg_aio_write(bs, buf, count, cb, opaque);