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>
50 #include <sys/ioctl.h>
51 #include <linux/cdrom.h>
55 //#define DEBUG_FLOPPY
61 /* if the FD is not accessed during that time (in ms), we try to
62 reopen it to see if the disk has been changed */
63 #define FD_OPEN_TIMEOUT 1000
65 typedef struct BDRVRawState
{
68 #if defined(__linux__)
69 /* linux floppy specific */
72 int64_t fd_error_time
;
78 static int fd_open(BlockDriverState
*bs
);
80 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
82 BDRVRawState
*s
= bs
->opaque
;
83 int fd
, open_flags
, ret
;
85 open_flags
= O_BINARY
;
86 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
89 open_flags
|= O_RDONLY
;
92 if (flags
& BDRV_O_CREAT
)
93 open_flags
|= O_CREAT
| O_TRUNC
;
97 fd
= open(filename
, open_flags
, 0644);
108 /* XXX: use host sector size if necessary with:
109 #ifdef DIOCGSECTORSIZE
111 unsigned int sectorsize = 512;
112 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
113 sectorsize > bufsize)
114 bufsize = sectorsize;
118 u_int32_t blockSize = 512;
119 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
125 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
126 uint8_t *buf
, int count
)
128 BDRVRawState
*s
= bs
->opaque
;
135 lseek(s
->fd
, offset
, SEEK_SET
);
136 ret
= read(s
->fd
, buf
, count
);
140 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
141 const uint8_t *buf
, int count
)
143 BDRVRawState
*s
= bs
->opaque
;
150 lseek(s
->fd
, offset
, SEEK_SET
);
151 ret
= write(s
->fd
, buf
, count
);
155 /***********************************************************/
156 /* Unix AIO using POSIX AIO */
158 typedef struct RawAIOCB
{
159 BlockDriverAIOCB common
;
161 struct RawAIOCB
*next
;
164 static int aio_sig_num
= SIGUSR2
;
165 static RawAIOCB
*first_aio
; /* AIO issued */
166 static int aio_initialized
= 0;
168 static void aio_signal_handler(int signum
)
171 CPUState
*env
= cpu_single_env
;
173 /* stop the currently executing cpu because a timer occured */
174 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
176 if (env
->kqemu_enabled
) {
177 kqemu_cpu_interrupt(env
);
184 void qemu_aio_init(void)
186 struct sigaction act
;
190 sigfillset(&act
.sa_mask
);
191 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
192 act
.sa_handler
= aio_signal_handler
;
193 sigaction(aio_sig_num
, &act
, NULL
);
195 #if defined(__GLIBC__) && defined(__linux__)
197 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
198 seems to fix the problem. */
200 memset(&ai
, 0, sizeof(ai
));
203 ai
.aio_idle_time
= 365 * 100000;
209 void qemu_aio_poll(void)
211 RawAIOCB
*acb
, **pacb
;
220 ret
= aio_error(&acb
->aiocb
);
221 if (ret
== ECANCELED
) {
222 /* remove the request */
224 qemu_aio_release(acb
);
225 } else if (ret
!= EINPROGRESS
) {
228 ret
= aio_return(&acb
->aiocb
);
229 if (ret
== acb
->aiocb
.aio_nbytes
)
236 /* remove the request */
238 /* call the callback */
239 acb
->common
.cb(acb
->common
.opaque
, ret
);
240 qemu_aio_release(acb
);
250 /* Wait for all IO requests to complete. */
251 void qemu_aio_flush(void)
253 qemu_aio_wait_start();
261 /* wait until at least one AIO was handled */
262 static sigset_t wait_oset
;
264 void qemu_aio_wait_start(void)
268 if (!aio_initialized
)
271 sigaddset(&set
, aio_sig_num
);
272 sigprocmask(SIG_BLOCK
, &set
, &wait_oset
);
275 void qemu_aio_wait(void)
285 sigaddset(&set
, aio_sig_num
);
286 sigwait(&set
, &nb_sigs
);
290 void qemu_aio_wait_end(void)
292 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
295 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
296 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
297 BlockDriverCompletionFunc
*cb
, void *opaque
)
299 BDRVRawState
*s
= bs
->opaque
;
305 acb
= qemu_aio_get(bs
, cb
, opaque
);
308 acb
->aiocb
.aio_fildes
= s
->fd
;
309 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
310 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
311 acb
->aiocb
.aio_buf
= buf
;
312 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
313 acb
->aiocb
.aio_offset
= sector_num
* 512;
314 acb
->next
= first_aio
;
319 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
320 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
321 BlockDriverCompletionFunc
*cb
, void *opaque
)
325 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
328 if (aio_read(&acb
->aiocb
) < 0) {
329 qemu_aio_release(acb
);
335 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
336 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
337 BlockDriverCompletionFunc
*cb
, void *opaque
)
341 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
344 if (aio_write(&acb
->aiocb
) < 0) {
345 qemu_aio_release(acb
);
351 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
354 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
357 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
358 if (ret
== AIO_NOTCANCELED
) {
359 /* fail safe: if the aio could not be canceled, we wait for
361 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
364 /* remove the callback from the queue */
369 } else if (*pacb
== acb
) {
371 qemu_aio_release(acb
);
378 static void raw_close(BlockDriverState
*bs
)
380 BDRVRawState
*s
= bs
->opaque
;
387 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
389 BDRVRawState
*s
= bs
->opaque
;
390 if (s
->type
!= FTYPE_FILE
)
392 if (ftruncate(s
->fd
, offset
) < 0)
397 static int64_t raw_getlength(BlockDriverState
*bs
)
399 BDRVRawState
*s
= bs
->opaque
;
406 struct dk_minfo minfo
;
416 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
417 #ifdef DIOCGMEDIASIZE
418 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
421 size
= LONG_LONG_MAX
;
423 size
= lseek(fd
, 0LL, SEEK_END
);
429 * use the DKIOCGMEDIAINFO ioctl to read the size.
431 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
433 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
434 } else /* there are reports that lseek on some devices
435 fails, but irc discussion said that contingency
436 on contingency was overkill */
439 size
= lseek(fd
, 0, SEEK_END
);
444 static int raw_create(const char *filename
, int64_t total_size
,
445 const char *backing_file
, int flags
)
449 if (flags
|| backing_file
)
452 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
456 ftruncate(fd
, total_size
* 512);
461 static void raw_flush(BlockDriverState
*bs
)
463 BDRVRawState
*s
= bs
->opaque
;
467 BlockDriver bdrv_raw
= {
469 sizeof(BDRVRawState
),
470 NULL
, /* no probe for protocols */
478 .bdrv_aio_read
= raw_aio_read
,
479 .bdrv_aio_write
= raw_aio_write
,
480 .bdrv_aio_cancel
= raw_aio_cancel
,
481 .aiocb_size
= sizeof(RawAIOCB
),
482 .protocol_name
= "file",
483 .bdrv_pread
= raw_pread
,
484 .bdrv_pwrite
= raw_pwrite
,
485 .bdrv_truncate
= raw_truncate
,
486 .bdrv_getlength
= raw_getlength
,
489 /***********************************************/
493 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
494 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
496 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
498 kern_return_t kernResult
;
499 mach_port_t masterPort
;
500 CFMutableDictionaryRef classesToMatch
;
502 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
503 if ( KERN_SUCCESS
!= kernResult
) {
504 printf( "IOMasterPort returned %d\n", kernResult
);
507 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
508 if ( classesToMatch
== NULL
) {
509 printf( "IOServiceMatching returned a NULL dictionary.\n" );
511 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
513 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
514 if ( KERN_SUCCESS
!= kernResult
)
516 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
522 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
524 io_object_t nextMedia
;
525 kern_return_t kernResult
= KERN_FAILURE
;
527 nextMedia
= IOIteratorNext( mediaIterator
);
530 CFTypeRef bsdPathAsCFString
;
531 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
532 if ( bsdPathAsCFString
) {
533 size_t devPathLength
;
534 strcpy( bsdPath
, _PATH_DEV
);
535 strcat( bsdPath
, "r" );
536 devPathLength
= strlen( bsdPath
);
537 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
538 kernResult
= KERN_SUCCESS
;
540 CFRelease( bsdPathAsCFString
);
542 IOObjectRelease( nextMedia
);
550 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
552 BDRVRawState
*s
= bs
->opaque
;
553 int fd
, open_flags
, ret
;
556 if (strstart(filename
, "/dev/cdrom", NULL
)) {
557 kern_return_t kernResult
;
558 io_iterator_t mediaIterator
;
559 char bsdPath
[ MAXPATHLEN
];
562 kernResult
= FindEjectableCDMedia( &mediaIterator
);
563 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
565 if ( bsdPath
[ 0 ] != '\0' ) {
566 strcat(bsdPath
,"s0");
567 /* some CDs don't have a partition 0 */
568 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
570 bsdPath
[strlen(bsdPath
)-1] = '1';
578 IOObjectRelease( mediaIterator
);
581 open_flags
= O_BINARY
;
582 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
583 open_flags
|= O_RDWR
;
585 open_flags
|= O_RDONLY
;
589 s
->type
= FTYPE_FILE
;
590 #if defined(__linux__)
591 if (strstart(filename
, "/dev/cd", NULL
)) {
592 /* open will not fail even if no CD is inserted */
593 open_flags
|= O_NONBLOCK
;
595 } else if (strstart(filename
, "/dev/fd", NULL
)) {
597 s
->fd_open_flags
= open_flags
;
598 /* open will not fail even if no floppy is inserted */
599 open_flags
|= O_NONBLOCK
;
602 fd
= open(filename
, open_flags
, 0644);
610 #if defined(__linux__)
611 /* close fd so that we can reopen it as needed */
612 if (s
->type
== FTYPE_FD
) {
615 s
->fd_media_changed
= 1;
621 #if defined(__linux__) && !defined(QEMU_TOOL)
623 /* Note: we do not have a reliable method to detect if the floppy is
624 present. The current method is to try to open the floppy at every
625 I/O and to keep it opened during a few hundreds of ms. */
626 static int fd_open(BlockDriverState
*bs
)
628 BDRVRawState
*s
= bs
->opaque
;
629 int last_media_present
;
631 if (s
->type
!= FTYPE_FD
)
633 last_media_present
= (s
->fd
>= 0);
635 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
639 printf("Floppy closed\n");
643 if (s
->fd_got_error
&&
644 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
646 printf("No floppy (open delayed)\n");
650 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
652 s
->fd_error_time
= qemu_get_clock(rt_clock
);
654 if (last_media_present
)
655 s
->fd_media_changed
= 1;
657 printf("No floppy\n");
662 printf("Floppy opened\n");
665 if (!last_media_present
)
666 s
->fd_media_changed
= 1;
667 s
->fd_open_time
= qemu_get_clock(rt_clock
);
672 static int fd_open(BlockDriverState
*bs
)
678 #if defined(__linux__)
680 static int raw_is_inserted(BlockDriverState
*bs
)
682 BDRVRawState
*s
= bs
->opaque
;
687 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
688 if (ret
== CDS_DISC_OK
)
701 /* currently only used by fdc.c, but a CD version would be good too */
702 static int raw_media_changed(BlockDriverState
*bs
)
704 BDRVRawState
*s
= bs
->opaque
;
710 /* XXX: we do not have a true media changed indication. It
711 does not work if the floppy is changed without trying
714 ret
= s
->fd_media_changed
;
715 s
->fd_media_changed
= 0;
717 printf("Floppy changed=%d\n", ret
);
726 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
728 BDRVRawState
*s
= bs
->opaque
;
733 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
734 perror("CDROMEJECT");
736 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
737 perror("CDROMEJECT");
747 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
749 if (ioctl(fd
, FDEJECT
, 0) < 0)
761 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
763 BDRVRawState
*s
= bs
->opaque
;
767 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
768 /* Note: an error can happen if the distribution automatically
770 // perror("CDROM_LOCKDOOR");
781 static int raw_is_inserted(BlockDriverState
*bs
)
786 static int raw_media_changed(BlockDriverState
*bs
)
791 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
796 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
803 BlockDriver bdrv_host_device
= {
805 sizeof(BDRVRawState
),
806 NULL
, /* no probe for protocols */
814 .bdrv_aio_read
= raw_aio_read
,
815 .bdrv_aio_write
= raw_aio_write
,
816 .bdrv_aio_cancel
= raw_aio_cancel
,
817 .aiocb_size
= sizeof(RawAIOCB
),
818 .bdrv_pread
= raw_pread
,
819 .bdrv_pwrite
= raw_pwrite
,
820 .bdrv_getlength
= raw_getlength
,
822 /* removable device support */
823 .bdrv_is_inserted
= raw_is_inserted
,
824 .bdrv_media_changed
= raw_media_changed
,
825 .bdrv_eject
= raw_eject
,
826 .bdrv_set_locked
= raw_set_locked
,
831 /* XXX: use another file ? */
832 #include <winioctl.h>
837 typedef struct BDRVRawState
{
840 char drive_path
[16]; /* format: "d:\" */
843 typedef struct RawAIOCB
{
844 BlockDriverAIOCB common
;
850 int qemu_ftruncate64(int fd
, int64_t length
)
857 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
860 h
= (HANDLE
)_get_osfhandle(fd
);
862 /* get current position, ftruncate do not change position */
864 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
865 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
869 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
871 res
= SetEndOfFile(h
);
873 /* back to old position */
874 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
878 static int set_sparse(int fd
)
881 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
882 NULL
, 0, NULL
, 0, &returned
, NULL
);
885 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
887 BDRVRawState
*s
= bs
->opaque
;
888 int access_flags
, create_flags
;
891 s
->type
= FTYPE_FILE
;
893 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
894 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
896 access_flags
= GENERIC_READ
;
898 if (flags
& BDRV_O_CREAT
) {
899 create_flags
= CREATE_ALWAYS
;
901 create_flags
= OPEN_EXISTING
;
906 overlapped
= FILE_FLAG_OVERLAPPED
;
908 s
->hfile
= CreateFile(filename
, access_flags
,
909 FILE_SHARE_READ
, NULL
,
910 create_flags
, overlapped
, 0);
911 if (s
->hfile
== INVALID_HANDLE_VALUE
)
916 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
917 uint8_t *buf
, int count
)
919 BDRVRawState
*s
= bs
->opaque
;
924 memset(&ov
, 0, sizeof(ov
));
926 ov
.OffsetHigh
= offset
>> 32;
927 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
929 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
938 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
939 const uint8_t *buf
, int count
)
941 BDRVRawState
*s
= bs
->opaque
;
946 memset(&ov
, 0, sizeof(ov
));
948 ov
.OffsetHigh
= offset
>> 32;
949 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
951 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
961 static void raw_aio_cb(void *opaque
)
963 RawAIOCB
*acb
= opaque
;
964 BlockDriverState
*bs
= acb
->common
.bs
;
965 BDRVRawState
*s
= bs
->opaque
;
969 ret
= GetOverlappedResult(s
->hfile
, &acb
->ov
, &ret_count
, TRUE
);
970 if (!ret
|| ret_count
!= acb
->count
) {
971 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
973 acb
->common
.cb(acb
->common
.opaque
, 0);
978 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
979 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
980 BlockDriverCompletionFunc
*cb
, void *opaque
)
985 acb
= qemu_aio_get(bs
, cb
, opaque
);
987 acb
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
989 qemu_aio_release(acb
);
993 memset(&acb
->ov
, 0, sizeof(acb
->ov
));
994 offset
= sector_num
* 512;
995 acb
->ov
.Offset
= offset
;
996 acb
->ov
.OffsetHigh
= offset
>> 32;
997 acb
->ov
.hEvent
= acb
->hEvent
;
998 acb
->count
= nb_sectors
* 512;
1000 qemu_add_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
1005 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
1006 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1007 BlockDriverCompletionFunc
*cb
, void *opaque
)
1009 BDRVRawState
*s
= bs
->opaque
;
1013 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1016 ret
= ReadFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
1018 qemu_aio_release(acb
);
1022 qemu_aio_release(acb
);
1024 return (BlockDriverAIOCB
*)acb
;
1027 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
1028 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1029 BlockDriverCompletionFunc
*cb
, void *opaque
)
1031 BDRVRawState
*s
= bs
->opaque
;
1035 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1038 ret
= WriteFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
1040 qemu_aio_release(acb
);
1044 qemu_aio_release(acb
);
1046 return (BlockDriverAIOCB
*)acb
;
1049 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
1052 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
1053 BlockDriverState
*bs
= acb
->common
.bs
;
1054 BDRVRawState
*s
= bs
->opaque
;
1056 qemu_del_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
1057 /* XXX: if more than one async I/O it is not correct */
1059 qemu_aio_release(acb
);
1063 static void raw_flush(BlockDriverState
*bs
)
1068 static void raw_close(BlockDriverState
*bs
)
1070 BDRVRawState
*s
= bs
->opaque
;
1071 CloseHandle(s
->hfile
);
1074 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
1076 BDRVRawState
*s
= bs
->opaque
;
1080 high
= offset
>> 32;
1081 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
1083 if (!SetEndOfFile(s
->hfile
))
1088 static int64_t raw_getlength(BlockDriverState
*bs
)
1090 BDRVRawState
*s
= bs
->opaque
;
1092 ULARGE_INTEGER available
, total
, total_free
;
1096 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
1097 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
1101 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
1103 l
.QuadPart
= total
.QuadPart
;
1111 static int raw_create(const char *filename
, int64_t total_size
,
1112 const char *backing_file
, int flags
)
1116 if (flags
|| backing_file
)
1119 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1124 ftruncate(fd
, total_size
* 512);
1129 void qemu_aio_init(void)
1133 void qemu_aio_poll(void)
1137 void qemu_aio_wait_start(void)
1141 void qemu_aio_wait(void)
1148 void qemu_aio_wait_end(void)
1152 BlockDriver bdrv_raw
= {
1154 sizeof(BDRVRawState
),
1155 NULL
, /* no probe for protocols */
1164 .bdrv_aio_read
= raw_aio_read
,
1165 .bdrv_aio_write
= raw_aio_write
,
1166 .bdrv_aio_cancel
= raw_aio_cancel
,
1167 .aiocb_size
= sizeof(RawAIOCB
);
1169 .protocol_name
= "file",
1170 .bdrv_pread
= raw_pread
,
1171 .bdrv_pwrite
= raw_pwrite
,
1172 .bdrv_truncate
= raw_truncate
,
1173 .bdrv_getlength
= raw_getlength
,
1176 /***********************************************/
1179 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
1181 char drives
[256], *pdrv
= drives
;
1184 memset(drives
, 0, sizeof(drives
));
1185 GetLogicalDriveStrings(sizeof(drives
), drives
);
1186 while(pdrv
[0] != '\0') {
1187 type
= GetDriveType(pdrv
);
1190 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
1194 pdrv
+= lstrlen(pdrv
) + 1;
1199 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
1201 BDRVRawState
*s
= bs
->opaque
;
1205 if (strstart(filename
, "\\\\.\\", &p
) ||
1206 strstart(filename
, "//./", &p
)) {
1207 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
1208 type
= GetDriveType(s
->drive_path
);
1209 if (type
== DRIVE_CDROM
)
1218 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1220 BDRVRawState
*s
= bs
->opaque
;
1221 int access_flags
, create_flags
;
1223 char device_name
[64];
1225 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1226 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
1228 filename
= device_name
;
1230 /* transform drive letters into device name */
1231 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
1232 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
1233 filename
[1] == ':' && filename
[2] == '\0') {
1234 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
1235 filename
= device_name
;
1238 s
->type
= find_device_type(bs
, filename
);
1240 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
1241 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
1243 access_flags
= GENERIC_READ
;
1245 create_flags
= OPEN_EXISTING
;
1250 overlapped
= FILE_FLAG_OVERLAPPED
;
1252 s
->hfile
= CreateFile(filename
, access_flags
,
1253 FILE_SHARE_READ
, NULL
,
1254 create_flags
, overlapped
, 0);
1255 if (s
->hfile
== INVALID_HANDLE_VALUE
)
1261 /***********************************************/
1262 /* removable device additionnal commands */
1264 static int raw_is_inserted(BlockDriverState
*bs
)
1269 static int raw_media_changed(BlockDriverState
*bs
)
1274 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1278 if (s
->type
== FTYPE_FILE
)
1281 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
1282 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
1284 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
1285 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
1289 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1295 BlockDriver bdrv_host_device
= {
1297 sizeof(BDRVRawState
),
1298 NULL
, /* no probe for protocols */
1307 .bdrv_aio_read
= raw_aio_read
,
1308 .bdrv_aio_write
= raw_aio_write
,
1309 .bdrv_aio_cancel
= raw_aio_cancel
,
1310 .aiocb_size
= sizeof(RawAIOCB
);
1312 .bdrv_pread
= raw_pread
,
1313 .bdrv_pwrite
= raw_pwrite
,
1314 .bdrv_getlength
= raw_getlength
,