2 * Block driver for RAW files
4 * Copyright (c) 2006 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
25 #include "block_int.h"
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
47 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
60 //#define DEBUG_FLOPPY
63 #if defined(DEBUG_BLOCK) && !defined(QEMU_TOOL)
64 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
65 { fprintf(stderr, formatCstr, ##args); fflush(stderr); } } while (0)
67 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
74 /* if the FD is not accessed during that time (in ms), we try to
75 reopen it to see if the disk has been changed */
76 #define FD_OPEN_TIMEOUT 1000
78 typedef struct BDRVRawState {
81 unsigned int lseek_err_cnt;
82 #if defined(__linux__)
83 /* linux floppy specific */
86 int64_t fd_error_time;
92 static int fd_open(BlockDriverState *bs);
94 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
96 BDRVRawState *s = bs->opaque;
97 int fd, open_flags, ret;
101 open_flags = O_BINARY;
102 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
103 open_flags |= O_RDWR;
105 open_flags |= O_RDONLY;
108 if (flags & BDRV_O_CREAT)
109 open_flags |= O_CREAT | O_TRUNC;
111 s->type = FTYPE_FILE;
113 fd = open(filename, open_flags, 0644);
124 /* XXX: use host sector size if necessary with:
125 #ifdef DIOCGSECTORSIZE
127 unsigned int sectorsize = 512;
128 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
129 sectorsize > bufsize)
130 bufsize = sectorsize;
134 u_int32_t blockSize = 512;
135 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
141 static int raw_pread(BlockDriverState *bs, int64_t offset,
142 uint8_t *buf, int count)
144 BDRVRawState *s = bs->opaque;
151 if (lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
152 ++(s->lseek_err_cnt);
153 if(s->lseek_err_cnt <= 10) {
154 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %lld, %p, %d) [%lld] lseek failed : %d = %s\n",
155 s->fd, bs->filename, offset, buf, count,
156 bs->total_sectors, errno, strerror(errno));
162 ret = read(s->fd, buf, count);
164 goto label__raw_read__success;
166 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %lld, %p, %d) [%lld] read failed %d : %d = %s\n",
167 s->fd, bs->filename, offset, buf, count,
168 bs->total_sectors, ret, errno, strerror(errno));
170 /* Try harder for CDrom. */
171 if (bs->type == BDRV_TYPE_CDROM) {
172 lseek(s->fd, offset, SEEK_SET);
173 ret = read(s->fd, buf, count);
175 goto label__raw_read__success;
176 lseek(s->fd, offset, SEEK_SET);
177 ret = read(s->fd, buf, count);
179 goto label__raw_read__success;
181 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %lld, %p, %d) [%lld] retry read failed %d : %d = %s\n",
182 s->fd, bs->filename, offset, buf, count,
183 bs->total_sectors, ret, errno, strerror(errno));
186 label__raw_read__success:
191 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
192 const uint8_t *buf, int count)
194 BDRVRawState *s = bs->opaque;
201 if (lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
202 ++(s->lseek_err_cnt);
203 if(s->lseek_err_cnt) {
204 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %lld, %p, %d) [%lld] lseek failed : %d = %s\n",
205 s->fd, bs->filename, offset, buf, count,
206 bs->total_sectors, errno, strerror(errno));
210 s->lseek_err_cnt = 0;
212 ret = write(s->fd, buf, count);
214 goto label__raw_write__success;
216 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %lld, %p, %d) [%lld] write failed %d : %d = %s\n",
217 s->fd, bs->filename, offset, buf, count,
218 bs->total_sectors, ret, errno, strerror(errno));
220 label__raw_write__success:
225 /***********************************************************/
226 /* Unix AIO using POSIX AIO */
228 typedef struct RawAIOCB {
229 BlockDriverAIOCB common;
231 struct RawAIOCB *next;
234 static int aio_sig_num = SIGUSR2;
235 static RawAIOCB *first_aio; /* AIO issued */
236 static int aio_initialized = 0;
238 static void aio_signal_handler(int signum)
241 CPUState *env = cpu_single_env;
243 /* stop the currently executing cpu because a timer occured */
244 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
246 if (env->kqemu_enabled) {
247 kqemu_cpu_interrupt(env);
254 void qemu_aio_init(void)
256 struct sigaction act;
260 sigfillset(&act.sa_mask);
261 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
262 act.sa_handler = aio_signal_handler;
263 sigaction(aio_sig_num, &act, NULL);
265 #if defined(__GLIBC__) && defined(__linux__)
267 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
268 seems to fix the problem. */
270 memset(&ai, 0, sizeof(ai));
273 ai.aio_idle_time = 365 * 100000;
279 void qemu_aio_poll(void)
281 RawAIOCB *acb, **pacb;
290 ret = aio_error(&acb->aiocb);
291 if (ret == ECANCELED) {
292 /* remove the request */
294 qemu_aio_release(acb);
295 } else if (ret != EINPROGRESS) {
298 ret = aio_return(&acb->aiocb);
299 if (ret == acb->aiocb.aio_nbytes)
306 /* remove the request */
308 /* call the callback */
309 acb->common.cb(acb->common.opaque, ret);
310 qemu_aio_release(acb);
320 /* Wait for all IO requests to complete. */
321 void qemu_aio_flush(void)
323 qemu_aio_wait_start();
331 /* wait until at least one AIO was handled */
332 static sigset_t wait_oset;
334 void qemu_aio_wait_start(void)
338 if (!aio_initialized)
341 sigaddset(&set, aio_sig_num);
342 sigprocmask(SIG_BLOCK, &set, &wait_oset);
345 void qemu_aio_wait(void)
355 sigaddset(&set, aio_sig_num);
356 sigwait(&set, &nb_sigs);
360 void qemu_aio_wait_end(void)
362 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
365 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
366 int64_t sector_num, uint8_t *buf, int nb_sectors,
367 BlockDriverCompletionFunc *cb, void *opaque)
369 BDRVRawState *s = bs->opaque;
375 acb = qemu_aio_get(bs, cb, opaque);
378 acb->aiocb.aio_fildes = s->fd;
379 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
380 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
381 acb->aiocb.aio_buf = buf;
382 acb->aiocb.aio_nbytes = nb_sectors * 512;
383 acb->aiocb.aio_offset = sector_num * 512;
384 acb->next = first_aio;
389 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
390 int64_t sector_num, uint8_t *buf, int nb_sectors,
391 BlockDriverCompletionFunc *cb, void *opaque)
395 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
398 if (aio_read(&acb->aiocb) < 0) {
399 qemu_aio_release(acb);
405 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
406 int64_t sector_num, const uint8_t *buf, int nb_sectors,
407 BlockDriverCompletionFunc *cb, void *opaque)
411 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
414 if (aio_write(&acb->aiocb) < 0) {
415 qemu_aio_release(acb);
421 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
424 RawAIOCB *acb = (RawAIOCB *)blockacb;
427 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
428 if (ret == AIO_NOTCANCELED) {
429 /* fail safe: if the aio could not be canceled, we wait for
431 while (aio_error(&acb->aiocb) == EINPROGRESS);
434 /* remove the callback from the queue */
439 } else if (*pacb == acb) {
441 qemu_aio_release(acb);
448 static void raw_close(BlockDriverState *bs)
450 BDRVRawState *s = bs->opaque;
457 static int raw_truncate(BlockDriverState *bs, int64_t offset)
459 BDRVRawState *s = bs->opaque;
460 if (s->type != FTYPE_FILE)
462 if (ftruncate(s->fd, offset) < 0)
467 static int64_t raw_getlength(BlockDriverState *bs)
469 BDRVRawState *s = bs->opaque;
476 struct dk_minfo minfo;
486 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
487 #ifdef DIOCGMEDIASIZE
488 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
491 size = LONG_LONG_MAX;
493 size = lseek(fd, 0LL, SEEK_END);
499 * use the DKIOCGMEDIAINFO ioctl to read the size.
501 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
503 size = minfo.dki_lbsize * minfo.dki_capacity;
504 } else /* there are reports that lseek on some devices
505 fails, but irc discussion said that contingency
506 on contingency was overkill */
509 size = lseek(fd, 0, SEEK_END);
514 static int raw_create(const char *filename, int64_t total_size,
515 const char *backing_file, int flags)
519 if (flags || backing_file)
522 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
526 ftruncate(fd, total_size * 512);
531 static void raw_flush(BlockDriverState *bs)
533 BDRVRawState *s = bs->opaque;
537 BlockDriver bdrv_raw = {
539 sizeof(BDRVRawState),
540 NULL, /* no probe for protocols */
548 .bdrv_aio_read = raw_aio_read,
549 .bdrv_aio_write = raw_aio_write,
550 .bdrv_aio_cancel = raw_aio_cancel,
551 .aiocb_size = sizeof(RawAIOCB),
552 .protocol_name = "file",
553 .bdrv_pread = raw_pread,
554 .bdrv_pwrite = raw_pwrite,
555 .bdrv_truncate = raw_truncate,
556 .bdrv_getlength = raw_getlength,
559 /***********************************************/
563 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
564 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
566 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
568 kern_return_t kernResult;
569 mach_port_t masterPort;
570 CFMutableDictionaryRef classesToMatch;
572 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
573 if ( KERN_SUCCESS != kernResult ) {
574 printf( "IOMasterPort returned %d\n", kernResult );
577 classesToMatch = IOServiceMatching( kIOCDMediaClass );
578 if ( classesToMatch == NULL ) {
579 printf( "IOServiceMatching returned a NULL dictionary.\n" );
581 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
583 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
584 if ( KERN_SUCCESS != kernResult )
586 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
592 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
594 io_object_t nextMedia;
595 kern_return_t kernResult = KERN_FAILURE;
597 nextMedia = IOIteratorNext( mediaIterator );
600 CFTypeRef bsdPathAsCFString;
601 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
602 if ( bsdPathAsCFString ) {
603 size_t devPathLength;
604 strcpy( bsdPath, _PATH_DEV );
605 strcat( bsdPath, "r" );
606 devPathLength = strlen( bsdPath );
607 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
608 kernResult = KERN_SUCCESS;
610 CFRelease( bsdPathAsCFString );
612 IOObjectRelease( nextMedia );
620 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
622 BDRVRawState *s = bs->opaque;
623 int fd, open_flags, ret;
626 if (strstart(filename, "/dev/cdrom", NULL)) {
627 kern_return_t kernResult;
628 io_iterator_t mediaIterator;
629 char bsdPath[ MAXPATHLEN ];
632 kernResult = FindEjectableCDMedia( &mediaIterator );
633 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
635 if ( bsdPath[ 0 ] != '\0' ) {
636 strcat(bsdPath,"s0");
637 /* some CDs don't have a partition 0 */
638 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
640 bsdPath[strlen(bsdPath)-1] = '1';
648 IOObjectRelease( mediaIterator );
651 open_flags = O_BINARY;
652 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
653 open_flags |= O_RDWR;
655 open_flags |= O_RDONLY;
659 s->type = FTYPE_FILE;
660 #if defined(__linux__)
661 if (strstart(filename, "/dev/cd", NULL)) {
662 /* open will not fail even if no CD is inserted */
663 open_flags |= O_NONBLOCK;
665 } else if (strstart(filename, "/dev/fd", NULL)) {
667 s->fd_open_flags = open_flags;
668 /* open will not fail even if no floppy is inserted */
669 open_flags |= O_NONBLOCK;
672 fd = open(filename, open_flags, 0644);
680 #if defined(__linux__)
681 /* close fd so that we can reopen it as needed */
682 if (s->type == FTYPE_FD) {
685 s->fd_media_changed = 1;
691 #if defined(__linux__) && !defined(QEMU_TOOL)
693 /* Note: we do not have a reliable method to detect if the floppy is
694 present. The current method is to try to open the floppy at every
695 I/O and to keep it opened during a few hundreds of ms. */
696 static int fd_open(BlockDriverState *bs)
698 BDRVRawState *s = bs->opaque;
699 int last_media_present;
701 if (s->type != FTYPE_FD)
703 last_media_present = (s->fd >= 0);
705 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
709 printf("Floppy closed\n");
713 if (s->fd_got_error &&
714 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
716 printf("No floppy (open delayed)\n");
720 s->fd = open(bs->filename, s->fd_open_flags);
722 s->fd_error_time = qemu_get_clock(rt_clock);
724 if (last_media_present)
725 s->fd_media_changed = 1;
727 printf("No floppy\n");
732 printf("Floppy opened\n");
735 if (!last_media_present)
736 s->fd_media_changed = 1;
737 s->fd_open_time = qemu_get_clock(rt_clock);
742 static int fd_open(BlockDriverState *bs)
748 #if defined(__linux__)
750 static int raw_is_inserted(BlockDriverState *bs)
752 BDRVRawState *s = bs->opaque;
757 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
758 if (ret == CDS_DISC_OK)
771 /* currently only used by fdc.c, but a CD version would be good too */
772 static int raw_media_changed(BlockDriverState *bs)
774 BDRVRawState *s = bs->opaque;
780 /* XXX: we do not have a true media changed indication. It
781 does not work if the floppy is changed without trying
784 ret = s->fd_media_changed;
785 s->fd_media_changed = 0;
787 printf("Floppy changed=%d\n", ret);
796 static int raw_eject(BlockDriverState *bs, int eject_flag)
798 BDRVRawState *s = bs->opaque;
803 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
804 perror("CDROMEJECT");
806 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
807 perror("CDROMEJECT");
817 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
819 if (ioctl(fd, FDEJECT, 0) < 0)
831 static int raw_set_locked(BlockDriverState *bs, int locked)
833 BDRVRawState *s = bs->opaque;
837 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
838 /* Note: an error can happen if the distribution automatically
840 // perror("CDROM_LOCKDOOR");
851 static int raw_is_inserted(BlockDriverState *bs)
856 static int raw_media_changed(BlockDriverState *bs)
861 static int raw_eject(BlockDriverState *bs, int eject_flag)
866 static int raw_set_locked(BlockDriverState *bs, int locked)
873 BlockDriver bdrv_host_device = {
875 sizeof(BDRVRawState),
876 NULL, /* no probe for protocols */
884 .bdrv_aio_read = raw_aio_read,
885 .bdrv_aio_write = raw_aio_write,
886 .bdrv_aio_cancel = raw_aio_cancel,
887 .aiocb_size = sizeof(RawAIOCB),
888 .bdrv_pread = raw_pread,
889 .bdrv_pwrite = raw_pwrite,
890 .bdrv_getlength = raw_getlength,
892 /* removable device support */
893 .bdrv_is_inserted = raw_is_inserted,
894 .bdrv_media_changed = raw_media_changed,
895 .bdrv_eject = raw_eject,
896 .bdrv_set_locked = raw_set_locked,
901 /* XXX: use another file ? */
902 #include <winioctl.h>
906 #define FTYPE_HARDDISK 2
908 typedef struct BDRVRawState {
911 char drive_path[16]; /* format: "d:\" */
914 typedef struct RawAIOCB {
915 BlockDriverAIOCB common;
921 int qemu_ftruncate64(int fd, int64_t length)
928 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
931 h = (HANDLE)_get_osfhandle(fd);
933 /* get current position, ftruncate do not change position */
935 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
936 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
940 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
942 res = SetEndOfFile(h);
944 /* back to old position */
945 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
949 static int set_sparse(int fd)
952 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
953 NULL, 0, NULL, 0, &returned, NULL);
956 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
958 BDRVRawState *s = bs->opaque;
959 int access_flags, create_flags;
962 s->type = FTYPE_FILE;
964 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
965 access_flags = GENERIC_READ | GENERIC_WRITE;
967 access_flags = GENERIC_READ;
969 if (flags & BDRV_O_CREAT) {
970 create_flags = CREATE_ALWAYS;
972 create_flags = OPEN_EXISTING;
975 overlapped = FILE_ATTRIBUTE_NORMAL;
977 overlapped = FILE_FLAG_OVERLAPPED;
979 s->hfile = CreateFile(filename, access_flags,
980 FILE_SHARE_READ, NULL,
981 create_flags, overlapped, NULL);
982 if (s->hfile == INVALID_HANDLE_VALUE) {
983 int err = GetLastError();
985 if (err == ERROR_ACCESS_DENIED)
992 static int raw_pread(BlockDriverState *bs, int64_t offset,
993 uint8_t *buf, int count)
995 BDRVRawState *s = bs->opaque;
1000 memset(&ov, 0, sizeof(ov));
1002 ov.OffsetHigh = offset >> 32;
1003 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
1005 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
1014 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
1015 const uint8_t *buf, int count)
1017 BDRVRawState *s = bs->opaque;
1022 memset(&ov, 0, sizeof(ov));
1024 ov.OffsetHigh = offset >> 32;
1025 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
1027 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
1038 static void raw_aio_cb(void *opaque)
1040 RawAIOCB *acb = opaque;
1041 BlockDriverState *bs = acb->common.bs;
1042 BDRVRawState *s = bs->opaque;
1046 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
1047 if (!ret || ret_count != acb->count) {
1048 acb->common.cb(acb->common.opaque, -EIO);
1050 acb->common.cb(acb->common.opaque, 0);
1055 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
1056 int64_t sector_num, uint8_t *buf, int nb_sectors,
1057 BlockDriverCompletionFunc *cb, void *opaque)
1062 acb = qemu_aio_get(bs, cb, opaque);
1064 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1066 qemu_aio_release(acb);
1070 memset(&acb->ov, 0, sizeof(acb->ov));
1071 offset = sector_num * 512;
1072 acb->ov.Offset = offset;
1073 acb->ov.OffsetHigh = offset >> 32;
1074 acb->ov.hEvent = acb->hEvent;
1075 acb->count = nb_sectors * 512;
1077 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1082 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1083 int64_t sector_num, uint8_t *buf, int nb_sectors,
1084 BlockDriverCompletionFunc *cb, void *opaque)
1086 BDRVRawState *s = bs->opaque;
1090 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1093 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1095 qemu_aio_release(acb);
1099 qemu_aio_release(acb);
1101 return (BlockDriverAIOCB *)acb;
1104 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1105 int64_t sector_num, uint8_t *buf, int nb_sectors,
1106 BlockDriverCompletionFunc *cb, void *opaque)
1108 BDRVRawState *s = bs->opaque;
1112 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1115 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1117 qemu_aio_release(acb);
1121 qemu_aio_release(acb);
1123 return (BlockDriverAIOCB *)acb;
1126 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1129 RawAIOCB *acb = (RawAIOCB *)blockacb;
1130 BlockDriverState *bs = acb->common.bs;
1131 BDRVRawState *s = bs->opaque;
1133 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1134 /* XXX: if more than one async I/O it is not correct */
1136 qemu_aio_release(acb);
1141 static void raw_flush(BlockDriverState *bs)
1143 BDRVRawState *s = bs->opaque;
1144 FlushFileBuffers(s->hfile);
1147 static void raw_close(BlockDriverState *bs)
1149 BDRVRawState *s = bs->opaque;
1150 CloseHandle(s->hfile);
1153 static int raw_truncate(BlockDriverState *bs, int64_t offset)
1155 BDRVRawState *s = bs->opaque;
1159 high = offset >> 32;
1160 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1162 if (!SetEndOfFile(s->hfile))
1167 static int64_t raw_getlength(BlockDriverState *bs)
1169 BDRVRawState *s = bs->opaque;
1171 ULARGE_INTEGER available, total, total_free;
1178 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1179 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1183 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1185 l.QuadPart = total.QuadPart;
1187 case FTYPE_HARDDISK:
1188 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY,
1189 NULL, 0, &dg, sizeof(dg), &count, NULL);
1190 if (status != FALSE) {
1191 l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder
1192 * dg.SectorsPerTrack * dg.BytesPerSector;
1201 static int raw_create(const char *filename, int64_t total_size,
1202 const char *backing_file, int flags)
1206 if (flags || backing_file)
1209 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1214 ftruncate(fd, total_size * 512);
1219 void qemu_aio_init(void)
1223 void qemu_aio_poll(void)
1227 void qemu_aio_flush(void)
1231 void qemu_aio_wait_start(void)
1235 void qemu_aio_wait(void)
1242 void qemu_aio_wait_end(void)
1246 BlockDriver bdrv_raw = {
1248 sizeof(BDRVRawState),
1249 NULL, /* no probe for protocols */
1258 .bdrv_aio_read = raw_aio_read,
1259 .bdrv_aio_write = raw_aio_write,
1260 .bdrv_aio_cancel = raw_aio_cancel,
1261 .aiocb_size = sizeof(RawAIOCB);
1263 .protocol_name = "file",
1264 .bdrv_pread = raw_pread,
1265 .bdrv_pwrite = raw_pwrite,
1266 .bdrv_truncate = raw_truncate,
1267 .bdrv_getlength = raw_getlength,
1270 /***********************************************/
1273 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1275 char drives[256], *pdrv = drives;
1278 memset(drives, 0, sizeof(drives));
1279 GetLogicalDriveStrings(sizeof(drives), drives);
1280 while(pdrv[0] != '\0') {
1281 type = GetDriveType(pdrv);
1284 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1288 pdrv += lstrlen(pdrv) + 1;
1293 static int find_device_type(BlockDriverState *bs, const char *filename)
1295 BDRVRawState *s = bs->opaque;
1299 if (strstart(filename, "\\\\.\\", &p) ||
1300 strstart(filename, "//./", &p)) {
1301 if (stristart(p, "PhysicalDrive", NULL))
1302 return FTYPE_HARDDISK;
1303 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1304 type = GetDriveType(s->drive_path);
1305 if (type == DRIVE_CDROM)
1314 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1316 BDRVRawState *s = bs->opaque;
1317 int access_flags, create_flags;
1319 char device_name[64];
1321 if (strstart(filename, "/dev/cdrom", NULL)) {
1322 if (find_cdrom(device_name, sizeof(device_name)) < 0)
1324 filename = device_name;
1326 /* transform drive letters into device name */
1327 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1328 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1329 filename[1] == ':' && filename[2] == '\0') {
1330 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1331 filename = device_name;
1334 s->type = find_device_type(bs, filename);
1336 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1337 access_flags = GENERIC_READ | GENERIC_WRITE;
1339 access_flags = GENERIC_READ;
1341 create_flags = OPEN_EXISTING;
1344 overlapped = FILE_ATTRIBUTE_NORMAL;
1346 overlapped = FILE_FLAG_OVERLAPPED;
1348 s->hfile = CreateFile(filename, access_flags,
1349 FILE_SHARE_READ, NULL,
1350 create_flags, overlapped, NULL);
1351 if (s->hfile == INVALID_HANDLE_VALUE) {
1352 int err = GetLastError();
1354 if (err == ERROR_ACCESS_DENIED)
1362 /***********************************************/
1363 /* removable device additional commands */
1365 static int raw_is_inserted(BlockDriverState *bs)
1370 static int raw_media_changed(BlockDriverState *bs)
1375 static int raw_eject(BlockDriverState *bs, int eject_flag)
1379 if (s->type == FTYPE_FILE)
1382 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1383 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1385 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1386 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1390 static int raw_set_locked(BlockDriverState *bs, int locked)
1396 BlockDriver bdrv_host_device = {
1398 sizeof(BDRVRawState),
1399 NULL, /* no probe for protocols */
1408 .bdrv_aio_read = raw_aio_read,
1409 .bdrv_aio_write = raw_aio_write,
1410 .bdrv_aio_cancel = raw_aio_cancel,
1411 .aiocb_size = sizeof(RawAIOCB);
1413 .bdrv_pread = raw_pread,
1414 .bdrv_pwrite = raw_pwrite,
1415 .bdrv_getlength = raw_getlength,