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(logfile, formatCstr, ##args); fflush(logfile); } } 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, %" PRId64 ", %p, %d) [%" PRId64
155 "] lseek failed : %d = %s\n",
156 s->fd, bs->filename, offset, buf, count,
157 bs->total_sectors, errno, strerror(errno));
163 ret = read(s->fd, buf, count);
165 goto label__raw_read__success;
167 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
168 "] read failed %d : %d = %s\n",
169 s->fd, bs->filename, offset, buf, count,
170 bs->total_sectors, ret, errno, strerror(errno));
172 /* Try harder for CDrom. */
173 if (bs->type == BDRV_TYPE_CDROM) {
174 lseek(s->fd, offset, SEEK_SET);
175 ret = read(s->fd, buf, count);
177 goto label__raw_read__success;
178 lseek(s->fd, offset, SEEK_SET);
179 ret = read(s->fd, buf, count);
181 goto label__raw_read__success;
183 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
184 "] retry read failed %d : %d = %s\n",
185 s->fd, bs->filename, offset, buf, count,
186 bs->total_sectors, ret, errno, strerror(errno));
189 label__raw_read__success:
194 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
195 const uint8_t *buf, int count)
197 BDRVRawState *s = bs->opaque;
204 if (lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
205 ++(s->lseek_err_cnt);
206 if(s->lseek_err_cnt) {
207 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
208 PRId64 "] lseek failed : %d = %s\n",
209 s->fd, bs->filename, offset, buf, count,
210 bs->total_sectors, errno, strerror(errno));
214 s->lseek_err_cnt = 0;
216 ret = write(s->fd, buf, count);
218 goto label__raw_write__success;
220 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
221 "] write failed %d : %d = %s\n",
222 s->fd, bs->filename, offset, buf, count,
223 bs->total_sectors, ret, errno, strerror(errno));
225 label__raw_write__success:
230 /***********************************************************/
231 /* Unix AIO using POSIX AIO */
233 typedef struct RawAIOCB {
234 BlockDriverAIOCB common;
236 struct RawAIOCB *next;
239 static int aio_sig_num = SIGUSR2;
240 static RawAIOCB *first_aio; /* AIO issued */
241 static int aio_initialized = 0;
243 static void aio_signal_handler(int signum)
246 CPUState *env = cpu_single_env;
248 /* stop the currently executing cpu because a timer occured */
249 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
251 if (env->kqemu_enabled) {
252 kqemu_cpu_interrupt(env);
259 void qemu_aio_init(void)
261 struct sigaction act;
265 sigfillset(&act.sa_mask);
266 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
267 act.sa_handler = aio_signal_handler;
268 sigaction(aio_sig_num, &act, NULL);
270 #if defined(__GLIBC__) && defined(__linux__)
272 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
273 seems to fix the problem. */
275 memset(&ai, 0, sizeof(ai));
278 ai.aio_idle_time = 365 * 100000;
284 void qemu_aio_poll(void)
286 RawAIOCB *acb, **pacb;
295 ret = aio_error(&acb->aiocb);
296 if (ret == ECANCELED) {
297 /* remove the request */
299 qemu_aio_release(acb);
300 } else if (ret != EINPROGRESS) {
303 ret = aio_return(&acb->aiocb);
304 if (ret == acb->aiocb.aio_nbytes)
311 /* remove the request */
313 /* call the callback */
314 acb->common.cb(acb->common.opaque, ret);
315 qemu_aio_release(acb);
325 /* Wait for all IO requests to complete. */
326 void qemu_aio_flush(void)
328 qemu_aio_wait_start();
336 /* wait until at least one AIO was handled */
337 static sigset_t wait_oset;
339 void qemu_aio_wait_start(void)
343 if (!aio_initialized)
346 sigaddset(&set, aio_sig_num);
347 sigprocmask(SIG_BLOCK, &set, &wait_oset);
350 void qemu_aio_wait(void)
360 sigaddset(&set, aio_sig_num);
361 sigwait(&set, &nb_sigs);
365 void qemu_aio_wait_end(void)
367 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
370 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
371 int64_t sector_num, uint8_t *buf, int nb_sectors,
372 BlockDriverCompletionFunc *cb, void *opaque)
374 BDRVRawState *s = bs->opaque;
380 acb = qemu_aio_get(bs, cb, opaque);
383 acb->aiocb.aio_fildes = s->fd;
384 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
385 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
386 acb->aiocb.aio_buf = buf;
387 acb->aiocb.aio_nbytes = nb_sectors * 512;
388 acb->aiocb.aio_offset = sector_num * 512;
389 acb->next = first_aio;
394 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
395 int64_t sector_num, uint8_t *buf, int nb_sectors,
396 BlockDriverCompletionFunc *cb, void *opaque)
400 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
403 if (aio_read(&acb->aiocb) < 0) {
404 qemu_aio_release(acb);
410 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
411 int64_t sector_num, const uint8_t *buf, int nb_sectors,
412 BlockDriverCompletionFunc *cb, void *opaque)
416 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
419 if (aio_write(&acb->aiocb) < 0) {
420 qemu_aio_release(acb);
426 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
429 RawAIOCB *acb = (RawAIOCB *)blockacb;
432 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
433 if (ret == AIO_NOTCANCELED) {
434 /* fail safe: if the aio could not be canceled, we wait for
436 while (aio_error(&acb->aiocb) == EINPROGRESS);
439 /* remove the callback from the queue */
444 } else if (*pacb == acb) {
446 qemu_aio_release(acb);
453 static void raw_close(BlockDriverState *bs)
455 BDRVRawState *s = bs->opaque;
462 static int raw_truncate(BlockDriverState *bs, int64_t offset)
464 BDRVRawState *s = bs->opaque;
465 if (s->type != FTYPE_FILE)
467 if (ftruncate(s->fd, offset) < 0)
472 static int64_t raw_getlength(BlockDriverState *bs)
474 BDRVRawState *s = bs->opaque;
481 struct dk_minfo minfo;
491 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
492 #ifdef DIOCGMEDIASIZE
493 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
496 size = LONG_LONG_MAX;
498 size = lseek(fd, 0LL, SEEK_END);
504 * use the DKIOCGMEDIAINFO ioctl to read the size.
506 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
508 size = minfo.dki_lbsize * minfo.dki_capacity;
509 } else /* there are reports that lseek on some devices
510 fails, but irc discussion said that contingency
511 on contingency was overkill */
514 size = lseek(fd, 0, SEEK_END);
519 static int raw_create(const char *filename, int64_t total_size,
520 const char *backing_file, int flags)
524 if (flags || backing_file)
527 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
531 ftruncate(fd, total_size * 512);
536 static void raw_flush(BlockDriverState *bs)
538 BDRVRawState *s = bs->opaque;
542 BlockDriver bdrv_raw = {
544 sizeof(BDRVRawState),
545 NULL, /* no probe for protocols */
553 .bdrv_aio_read = raw_aio_read,
554 .bdrv_aio_write = raw_aio_write,
555 .bdrv_aio_cancel = raw_aio_cancel,
556 .aiocb_size = sizeof(RawAIOCB),
557 .protocol_name = "file",
558 .bdrv_pread = raw_pread,
559 .bdrv_pwrite = raw_pwrite,
560 .bdrv_truncate = raw_truncate,
561 .bdrv_getlength = raw_getlength,
564 /***********************************************/
568 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
569 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
571 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
573 kern_return_t kernResult;
574 mach_port_t masterPort;
575 CFMutableDictionaryRef classesToMatch;
577 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
578 if ( KERN_SUCCESS != kernResult ) {
579 printf( "IOMasterPort returned %d\n", kernResult );
582 classesToMatch = IOServiceMatching( kIOCDMediaClass );
583 if ( classesToMatch == NULL ) {
584 printf( "IOServiceMatching returned a NULL dictionary.\n" );
586 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
588 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
589 if ( KERN_SUCCESS != kernResult )
591 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
597 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
599 io_object_t nextMedia;
600 kern_return_t kernResult = KERN_FAILURE;
602 nextMedia = IOIteratorNext( mediaIterator );
605 CFTypeRef bsdPathAsCFString;
606 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
607 if ( bsdPathAsCFString ) {
608 size_t devPathLength;
609 strcpy( bsdPath, _PATH_DEV );
610 strcat( bsdPath, "r" );
611 devPathLength = strlen( bsdPath );
612 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
613 kernResult = KERN_SUCCESS;
615 CFRelease( bsdPathAsCFString );
617 IOObjectRelease( nextMedia );
625 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
627 BDRVRawState *s = bs->opaque;
628 int fd, open_flags, ret;
631 if (strstart(filename, "/dev/cdrom", NULL)) {
632 kern_return_t kernResult;
633 io_iterator_t mediaIterator;
634 char bsdPath[ MAXPATHLEN ];
637 kernResult = FindEjectableCDMedia( &mediaIterator );
638 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
640 if ( bsdPath[ 0 ] != '\0' ) {
641 strcat(bsdPath,"s0");
642 /* some CDs don't have a partition 0 */
643 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
645 bsdPath[strlen(bsdPath)-1] = '1';
653 IOObjectRelease( mediaIterator );
656 open_flags = O_BINARY;
657 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
658 open_flags |= O_RDWR;
660 open_flags |= O_RDONLY;
664 s->type = FTYPE_FILE;
665 #if defined(__linux__)
666 if (strstart(filename, "/dev/cd", NULL)) {
667 /* open will not fail even if no CD is inserted */
668 open_flags |= O_NONBLOCK;
670 } else if (strstart(filename, "/dev/fd", NULL)) {
672 s->fd_open_flags = open_flags;
673 /* open will not fail even if no floppy is inserted */
674 open_flags |= O_NONBLOCK;
677 fd = open(filename, open_flags, 0644);
685 #if defined(__linux__)
686 /* close fd so that we can reopen it as needed */
687 if (s->type == FTYPE_FD) {
690 s->fd_media_changed = 1;
696 #if defined(__linux__) && !defined(QEMU_TOOL)
698 /* Note: we do not have a reliable method to detect if the floppy is
699 present. The current method is to try to open the floppy at every
700 I/O and to keep it opened during a few hundreds of ms. */
701 static int fd_open(BlockDriverState *bs)
703 BDRVRawState *s = bs->opaque;
704 int last_media_present;
706 if (s->type != FTYPE_FD)
708 last_media_present = (s->fd >= 0);
710 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
714 printf("Floppy closed\n");
718 if (s->fd_got_error &&
719 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
721 printf("No floppy (open delayed)\n");
725 s->fd = open(bs->filename, s->fd_open_flags);
727 s->fd_error_time = qemu_get_clock(rt_clock);
729 if (last_media_present)
730 s->fd_media_changed = 1;
732 printf("No floppy\n");
737 printf("Floppy opened\n");
740 if (!last_media_present)
741 s->fd_media_changed = 1;
742 s->fd_open_time = qemu_get_clock(rt_clock);
747 static int fd_open(BlockDriverState *bs)
753 #if defined(__linux__)
755 static int raw_is_inserted(BlockDriverState *bs)
757 BDRVRawState *s = bs->opaque;
762 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
763 if (ret == CDS_DISC_OK)
776 /* currently only used by fdc.c, but a CD version would be good too */
777 static int raw_media_changed(BlockDriverState *bs)
779 BDRVRawState *s = bs->opaque;
785 /* XXX: we do not have a true media changed indication. It
786 does not work if the floppy is changed without trying
789 ret = s->fd_media_changed;
790 s->fd_media_changed = 0;
792 printf("Floppy changed=%d\n", ret);
801 static int raw_eject(BlockDriverState *bs, int eject_flag)
803 BDRVRawState *s = bs->opaque;
808 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
809 perror("CDROMEJECT");
811 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
812 perror("CDROMEJECT");
822 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
824 if (ioctl(fd, FDEJECT, 0) < 0)
836 static int raw_set_locked(BlockDriverState *bs, int locked)
838 BDRVRawState *s = bs->opaque;
842 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
843 /* Note: an error can happen if the distribution automatically
845 // perror("CDROM_LOCKDOOR");
856 static int raw_is_inserted(BlockDriverState *bs)
861 static int raw_media_changed(BlockDriverState *bs)
866 static int raw_eject(BlockDriverState *bs, int eject_flag)
871 static int raw_set_locked(BlockDriverState *bs, int locked)
878 BlockDriver bdrv_host_device = {
880 sizeof(BDRVRawState),
881 NULL, /* no probe for protocols */
889 .bdrv_aio_read = raw_aio_read,
890 .bdrv_aio_write = raw_aio_write,
891 .bdrv_aio_cancel = raw_aio_cancel,
892 .aiocb_size = sizeof(RawAIOCB),
893 .bdrv_pread = raw_pread,
894 .bdrv_pwrite = raw_pwrite,
895 .bdrv_getlength = raw_getlength,
897 /* removable device support */
898 .bdrv_is_inserted = raw_is_inserted,
899 .bdrv_media_changed = raw_media_changed,
900 .bdrv_eject = raw_eject,
901 .bdrv_set_locked = raw_set_locked,
906 /* XXX: use another file ? */
907 #include <winioctl.h>
911 #define FTYPE_HARDDISK 2
913 typedef struct BDRVRawState {
916 char drive_path[16]; /* format: "d:\" */
919 typedef struct RawAIOCB {
920 BlockDriverAIOCB common;
926 int qemu_ftruncate64(int fd, int64_t length)
933 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
936 h = (HANDLE)_get_osfhandle(fd);
938 /* get current position, ftruncate do not change position */
940 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
941 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
945 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
947 res = SetEndOfFile(h);
949 /* back to old position */
950 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
954 static int set_sparse(int fd)
957 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
958 NULL, 0, NULL, 0, &returned, NULL);
961 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
963 BDRVRawState *s = bs->opaque;
964 int access_flags, create_flags;
967 s->type = FTYPE_FILE;
969 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
970 access_flags = GENERIC_READ | GENERIC_WRITE;
972 access_flags = GENERIC_READ;
974 if (flags & BDRV_O_CREAT) {
975 create_flags = CREATE_ALWAYS;
977 create_flags = OPEN_EXISTING;
980 overlapped = FILE_ATTRIBUTE_NORMAL;
982 overlapped = FILE_FLAG_OVERLAPPED;
984 s->hfile = CreateFile(filename, access_flags,
985 FILE_SHARE_READ, NULL,
986 create_flags, overlapped, NULL);
987 if (s->hfile == INVALID_HANDLE_VALUE) {
988 int err = GetLastError();
990 if (err == ERROR_ACCESS_DENIED)
997 static int raw_pread(BlockDriverState *bs, int64_t offset,
998 uint8_t *buf, int count)
1000 BDRVRawState *s = bs->opaque;
1005 memset(&ov, 0, sizeof(ov));
1007 ov.OffsetHigh = offset >> 32;
1008 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
1010 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
1019 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
1020 const uint8_t *buf, int count)
1022 BDRVRawState *s = bs->opaque;
1027 memset(&ov, 0, sizeof(ov));
1029 ov.OffsetHigh = offset >> 32;
1030 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
1032 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
1043 static void raw_aio_cb(void *opaque)
1045 RawAIOCB *acb = opaque;
1046 BlockDriverState *bs = acb->common.bs;
1047 BDRVRawState *s = bs->opaque;
1051 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
1052 if (!ret || ret_count != acb->count) {
1053 acb->common.cb(acb->common.opaque, -EIO);
1055 acb->common.cb(acb->common.opaque, 0);
1060 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
1061 int64_t sector_num, uint8_t *buf, int nb_sectors,
1062 BlockDriverCompletionFunc *cb, void *opaque)
1067 acb = qemu_aio_get(bs, cb, opaque);
1069 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1071 qemu_aio_release(acb);
1075 memset(&acb->ov, 0, sizeof(acb->ov));
1076 offset = sector_num * 512;
1077 acb->ov.Offset = offset;
1078 acb->ov.OffsetHigh = offset >> 32;
1079 acb->ov.hEvent = acb->hEvent;
1080 acb->count = nb_sectors * 512;
1082 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1087 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1088 int64_t sector_num, uint8_t *buf, int nb_sectors,
1089 BlockDriverCompletionFunc *cb, void *opaque)
1091 BDRVRawState *s = bs->opaque;
1095 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1098 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1100 qemu_aio_release(acb);
1104 qemu_aio_release(acb);
1106 return (BlockDriverAIOCB *)acb;
1109 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1110 int64_t sector_num, uint8_t *buf, int nb_sectors,
1111 BlockDriverCompletionFunc *cb, void *opaque)
1113 BDRVRawState *s = bs->opaque;
1117 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1120 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1122 qemu_aio_release(acb);
1126 qemu_aio_release(acb);
1128 return (BlockDriverAIOCB *)acb;
1131 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1134 RawAIOCB *acb = (RawAIOCB *)blockacb;
1135 BlockDriverState *bs = acb->common.bs;
1136 BDRVRawState *s = bs->opaque;
1138 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1139 /* XXX: if more than one async I/O it is not correct */
1141 qemu_aio_release(acb);
1146 static void raw_flush(BlockDriverState *bs)
1148 BDRVRawState *s = bs->opaque;
1149 FlushFileBuffers(s->hfile);
1152 static void raw_close(BlockDriverState *bs)
1154 BDRVRawState *s = bs->opaque;
1155 CloseHandle(s->hfile);
1158 static int raw_truncate(BlockDriverState *bs, int64_t offset)
1160 BDRVRawState *s = bs->opaque;
1164 high = offset >> 32;
1165 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1167 if (!SetEndOfFile(s->hfile))
1172 static int64_t raw_getlength(BlockDriverState *bs)
1174 BDRVRawState *s = bs->opaque;
1176 ULARGE_INTEGER available, total, total_free;
1177 DISK_GEOMETRY_EX dg;
1183 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1184 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1188 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1190 l.QuadPart = total.QuadPart;
1192 case FTYPE_HARDDISK:
1193 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
1194 NULL, 0, &dg, sizeof(dg), &count, NULL);
1205 static int raw_create(const char *filename, int64_t total_size,
1206 const char *backing_file, int flags)
1210 if (flags || backing_file)
1213 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1218 ftruncate(fd, total_size * 512);
1223 void qemu_aio_init(void)
1227 void qemu_aio_poll(void)
1231 void qemu_aio_flush(void)
1235 void qemu_aio_wait_start(void)
1239 void qemu_aio_wait(void)
1246 void qemu_aio_wait_end(void)
1250 BlockDriver bdrv_raw = {
1252 sizeof(BDRVRawState),
1253 NULL, /* no probe for protocols */
1262 .bdrv_aio_read = raw_aio_read,
1263 .bdrv_aio_write = raw_aio_write,
1264 .bdrv_aio_cancel = raw_aio_cancel,
1265 .aiocb_size = sizeof(RawAIOCB);
1267 .protocol_name = "file",
1268 .bdrv_pread = raw_pread,
1269 .bdrv_pwrite = raw_pwrite,
1270 .bdrv_truncate = raw_truncate,
1271 .bdrv_getlength = raw_getlength,
1274 /***********************************************/
1277 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1279 char drives[256], *pdrv = drives;
1282 memset(drives, 0, sizeof(drives));
1283 GetLogicalDriveStrings(sizeof(drives), drives);
1284 while(pdrv[0] != '\0') {
1285 type = GetDriveType(pdrv);
1288 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1292 pdrv += lstrlen(pdrv) + 1;
1297 static int find_device_type(BlockDriverState *bs, const char *filename)
1299 BDRVRawState *s = bs->opaque;
1303 if (strstart(filename, "\\\\.\\", &p) ||
1304 strstart(filename, "//./", &p)) {
1305 if (stristart(p, "PhysicalDrive", NULL))
1306 return FTYPE_HARDDISK;
1307 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1308 type = GetDriveType(s->drive_path);
1309 if (type == DRIVE_CDROM)
1318 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1320 BDRVRawState *s = bs->opaque;
1321 int access_flags, create_flags;
1323 char device_name[64];
1325 if (strstart(filename, "/dev/cdrom", NULL)) {
1326 if (find_cdrom(device_name, sizeof(device_name)) < 0)
1328 filename = device_name;
1330 /* transform drive letters into device name */
1331 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1332 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1333 filename[1] == ':' && filename[2] == '\0') {
1334 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1335 filename = device_name;
1338 s->type = find_device_type(bs, filename);
1340 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1341 access_flags = GENERIC_READ | GENERIC_WRITE;
1343 access_flags = GENERIC_READ;
1345 create_flags = OPEN_EXISTING;
1348 overlapped = FILE_ATTRIBUTE_NORMAL;
1350 overlapped = FILE_FLAG_OVERLAPPED;
1352 s->hfile = CreateFile(filename, access_flags,
1353 FILE_SHARE_READ, NULL,
1354 create_flags, overlapped, NULL);
1355 if (s->hfile == INVALID_HANDLE_VALUE) {
1356 int err = GetLastError();
1358 if (err == ERROR_ACCESS_DENIED)
1366 /***********************************************/
1367 /* removable device additional commands */
1369 static int raw_is_inserted(BlockDriverState *bs)
1374 static int raw_media_changed(BlockDriverState *bs)
1379 static int raw_eject(BlockDriverState *bs, int eject_flag)
1383 if (s->type == FTYPE_FILE)
1386 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1387 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1389 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1390 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1394 static int raw_set_locked(BlockDriverState *bs, int locked)
1400 BlockDriver bdrv_host_device = {
1402 sizeof(BDRVRawState),
1403 NULL, /* no probe for protocols */
1412 .bdrv_aio_read = raw_aio_read,
1413 .bdrv_aio_write = raw_aio_write,
1414 .bdrv_aio_cancel = raw_aio_cancel,
1415 .aiocb_size = sizeof(RawAIOCB);
1417 .bdrv_pread = raw_pread,
1418 .bdrv_pwrite = raw_pwrite,
1419 .bdrv_getlength = raw_getlength,