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 "qemu-common.h"
30 #include "block_int.h"
37 #include <sys/param.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/IOBSD.h>
40 #include <IOKit/storage/IOMediaBSDClient.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <IOKit/storage/IOCDMedia.h>
43 //#include <IOKit/storage/IOCDTypes.h>
44 #include <CoreFoundation/CoreFoundation.h>
48 #define _POSIX_PTHREAD_SEMANTICS 1
53 #include <sys/ioctl.h>
54 #include <linux/cdrom.h>
61 //#define DEBUG_FLOPPY
64 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG)
65 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
66 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
68 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
75 /* if the FD is not accessed during that time (in ms), we try to
76 reopen it to see if the disk has been changed */
77 #define FD_OPEN_TIMEOUT 1000
79 typedef struct BDRVRawState
{
82 unsigned int lseek_err_cnt
;
83 #if defined(__linux__)
84 /* linux floppy specific */
87 int64_t fd_error_time
;
93 static int fd_open(BlockDriverState
*bs
);
95 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
97 BDRVRawState
*s
= bs
->opaque
;
98 int fd
, open_flags
, ret
;
100 s
->lseek_err_cnt
= 0;
102 open_flags
= O_BINARY
;
103 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
104 open_flags
|= O_RDWR
;
106 open_flags
|= O_RDONLY
;
109 if (flags
& BDRV_O_CREAT
)
110 open_flags
|= O_CREAT
| O_TRUNC
;
112 s
->type
= FTYPE_FILE
;
114 fd
= open(filename
, open_flags
, 0644);
125 /* XXX: use host sector size if necessary with:
126 #ifdef DIOCGSECTORSIZE
128 unsigned int sectorsize = 512;
129 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
130 sectorsize > bufsize)
131 bufsize = sectorsize;
135 u_int32_t blockSize = 512;
136 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
142 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
143 uint8_t *buf
, int count
)
145 BDRVRawState
*s
= bs
->opaque
;
152 if (lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
153 ++(s
->lseek_err_cnt
);
154 if(s
->lseek_err_cnt
<= 10) {
155 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
156 "] lseek failed : %d = %s\n",
157 s
->fd
, bs
->filename
, offset
, buf
, count
,
158 bs
->total_sectors
, errno
, strerror(errno
));
164 ret
= read(s
->fd
, buf
, count
);
166 goto label__raw_read__success
;
168 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
169 "] read failed %d : %d = %s\n",
170 s
->fd
, bs
->filename
, offset
, buf
, count
,
171 bs
->total_sectors
, ret
, errno
, strerror(errno
));
173 /* Try harder for CDrom. */
174 if (bs
->type
== BDRV_TYPE_CDROM
) {
175 lseek(s
->fd
, offset
, SEEK_SET
);
176 ret
= read(s
->fd
, buf
, count
);
178 goto label__raw_read__success
;
179 lseek(s
->fd
, offset
, SEEK_SET
);
180 ret
= read(s
->fd
, buf
, count
);
182 goto label__raw_read__success
;
184 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
185 "] retry read failed %d : %d = %s\n",
186 s
->fd
, bs
->filename
, offset
, buf
, count
,
187 bs
->total_sectors
, ret
, errno
, strerror(errno
));
190 label__raw_read__success
:
195 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
196 const uint8_t *buf
, int count
)
198 BDRVRawState
*s
= bs
->opaque
;
205 if (lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
206 ++(s
->lseek_err_cnt
);
207 if(s
->lseek_err_cnt
) {
208 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
209 PRId64
"] lseek failed : %d = %s\n",
210 s
->fd
, bs
->filename
, offset
, buf
, count
,
211 bs
->total_sectors
, errno
, strerror(errno
));
215 s
->lseek_err_cnt
= 0;
217 ret
= write(s
->fd
, buf
, count
);
219 goto label__raw_write__success
;
221 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
222 "] write failed %d : %d = %s\n",
223 s
->fd
, bs
->filename
, offset
, buf
, count
,
224 bs
->total_sectors
, ret
, errno
, strerror(errno
));
226 label__raw_write__success
:
231 /***********************************************************/
232 /* Unix AIO using POSIX AIO */
234 typedef struct RawAIOCB
{
235 BlockDriverAIOCB common
;
237 struct RawAIOCB
*next
;
240 static int aio_sig_num
= SIGUSR2
;
241 static RawAIOCB
*first_aio
; /* AIO issued */
242 static int aio_initialized
= 0;
244 static void aio_signal_handler(int signum
)
247 CPUState
*env
= cpu_single_env
;
249 /* stop the currently executing cpu because a timer occured */
250 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
252 if (env
->kqemu_enabled
) {
253 kqemu_cpu_interrupt(env
);
260 void qemu_aio_init(void)
262 struct sigaction act
;
266 sigfillset(&act
.sa_mask
);
267 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
268 act
.sa_handler
= aio_signal_handler
;
269 sigaction(aio_sig_num
, &act
, NULL
);
271 #if defined(__GLIBC__) && defined(__linux__)
273 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
274 seems to fix the problem. */
276 memset(&ai
, 0, sizeof(ai
));
279 ai
.aio_idle_time
= 365 * 100000;
285 void qemu_aio_poll(void)
287 RawAIOCB
*acb
, **pacb
;
296 ret
= aio_error(&acb
->aiocb
);
297 if (ret
== ECANCELED
) {
298 /* remove the request */
300 qemu_aio_release(acb
);
301 } else if (ret
!= EINPROGRESS
) {
304 ret
= aio_return(&acb
->aiocb
);
305 if (ret
== acb
->aiocb
.aio_nbytes
)
312 /* remove the request */
314 /* call the callback */
315 acb
->common
.cb(acb
->common
.opaque
, ret
);
316 qemu_aio_release(acb
);
326 /* Wait for all IO requests to complete. */
327 void qemu_aio_flush(void)
329 qemu_aio_wait_start();
337 /* wait until at least one AIO was handled */
338 static sigset_t wait_oset
;
340 void qemu_aio_wait_start(void)
344 if (!aio_initialized
)
347 sigaddset(&set
, aio_sig_num
);
348 sigprocmask(SIG_BLOCK
, &set
, &wait_oset
);
351 void qemu_aio_wait(void)
361 sigaddset(&set
, aio_sig_num
);
362 sigwait(&set
, &nb_sigs
);
366 void qemu_aio_wait_end(void)
368 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
371 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
372 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
373 BlockDriverCompletionFunc
*cb
, void *opaque
)
375 BDRVRawState
*s
= bs
->opaque
;
381 acb
= qemu_aio_get(bs
, cb
, opaque
);
384 acb
->aiocb
.aio_fildes
= s
->fd
;
385 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
386 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
387 acb
->aiocb
.aio_buf
= buf
;
388 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
389 acb
->aiocb
.aio_offset
= sector_num
* 512;
390 acb
->next
= first_aio
;
395 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
396 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
397 BlockDriverCompletionFunc
*cb
, void *opaque
)
401 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
404 if (aio_read(&acb
->aiocb
) < 0) {
405 qemu_aio_release(acb
);
411 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
412 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
413 BlockDriverCompletionFunc
*cb
, void *opaque
)
417 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
420 if (aio_write(&acb
->aiocb
) < 0) {
421 qemu_aio_release(acb
);
427 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
430 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
433 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
434 if (ret
== AIO_NOTCANCELED
) {
435 /* fail safe: if the aio could not be canceled, we wait for
437 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
440 /* remove the callback from the queue */
445 } else if (*pacb
== acb
) {
447 qemu_aio_release(acb
);
454 static void raw_close(BlockDriverState
*bs
)
456 BDRVRawState
*s
= bs
->opaque
;
463 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
465 BDRVRawState
*s
= bs
->opaque
;
466 if (s
->type
!= FTYPE_FILE
)
468 if (ftruncate(s
->fd
, offset
) < 0)
473 static int64_t raw_getlength(BlockDriverState
*bs
)
475 BDRVRawState
*s
= bs
->opaque
;
482 struct dk_minfo minfo
;
492 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
493 #ifdef DIOCGMEDIASIZE
494 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
497 size
= LONG_LONG_MAX
;
499 size
= lseek(fd
, 0LL, SEEK_END
);
505 * use the DKIOCGMEDIAINFO ioctl to read the size.
507 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
509 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
510 } else /* there are reports that lseek on some devices
511 fails, but irc discussion said that contingency
512 on contingency was overkill */
515 size
= lseek(fd
, 0, SEEK_END
);
520 static int raw_create(const char *filename
, int64_t total_size
,
521 const char *backing_file
, int flags
)
525 if (flags
|| backing_file
)
528 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
532 ftruncate(fd
, total_size
* 512);
537 static void raw_flush(BlockDriverState
*bs
)
539 BDRVRawState
*s
= bs
->opaque
;
543 BlockDriver bdrv_raw
= {
545 sizeof(BDRVRawState
),
546 NULL
, /* no probe for protocols */
554 .bdrv_aio_read
= raw_aio_read
,
555 .bdrv_aio_write
= raw_aio_write
,
556 .bdrv_aio_cancel
= raw_aio_cancel
,
557 .aiocb_size
= sizeof(RawAIOCB
),
558 .protocol_name
= "file",
559 .bdrv_pread
= raw_pread
,
560 .bdrv_pwrite
= raw_pwrite
,
561 .bdrv_truncate
= raw_truncate
,
562 .bdrv_getlength
= raw_getlength
,
565 /***********************************************/
569 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
570 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
572 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
574 kern_return_t kernResult
;
575 mach_port_t masterPort
;
576 CFMutableDictionaryRef classesToMatch
;
578 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
579 if ( KERN_SUCCESS
!= kernResult
) {
580 printf( "IOMasterPort returned %d\n", kernResult
);
583 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
584 if ( classesToMatch
== NULL
) {
585 printf( "IOServiceMatching returned a NULL dictionary.\n" );
587 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
589 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
590 if ( KERN_SUCCESS
!= kernResult
)
592 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
598 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
600 io_object_t nextMedia
;
601 kern_return_t kernResult
= KERN_FAILURE
;
603 nextMedia
= IOIteratorNext( mediaIterator
);
606 CFTypeRef bsdPathAsCFString
;
607 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
608 if ( bsdPathAsCFString
) {
609 size_t devPathLength
;
610 strcpy( bsdPath
, _PATH_DEV
);
611 strcat( bsdPath
, "r" );
612 devPathLength
= strlen( bsdPath
);
613 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
614 kernResult
= KERN_SUCCESS
;
616 CFRelease( bsdPathAsCFString
);
618 IOObjectRelease( nextMedia
);
626 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
628 BDRVRawState
*s
= bs
->opaque
;
629 int fd
, open_flags
, ret
;
632 if (strstart(filename
, "/dev/cdrom", NULL
)) {
633 kern_return_t kernResult
;
634 io_iterator_t mediaIterator
;
635 char bsdPath
[ MAXPATHLEN
];
638 kernResult
= FindEjectableCDMedia( &mediaIterator
);
639 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
641 if ( bsdPath
[ 0 ] != '\0' ) {
642 strcat(bsdPath
,"s0");
643 /* some CDs don't have a partition 0 */
644 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
646 bsdPath
[strlen(bsdPath
)-1] = '1';
654 IOObjectRelease( mediaIterator
);
657 open_flags
= O_BINARY
;
658 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
659 open_flags
|= O_RDWR
;
661 open_flags
|= O_RDONLY
;
665 s
->type
= FTYPE_FILE
;
666 #if defined(__linux__)
667 if (strstart(filename
, "/dev/cd", NULL
)) {
668 /* open will not fail even if no CD is inserted */
669 open_flags
|= O_NONBLOCK
;
671 } else if (strstart(filename
, "/dev/fd", NULL
)) {
673 s
->fd_open_flags
= open_flags
;
674 /* open will not fail even if no floppy is inserted */
675 open_flags
|= O_NONBLOCK
;
678 fd
= open(filename
, open_flags
, 0644);
686 #if defined(__linux__)
687 /* close fd so that we can reopen it as needed */
688 if (s
->type
== FTYPE_FD
) {
691 s
->fd_media_changed
= 1;
697 #if defined(__linux__) && !defined(QEMU_IMG)
699 /* Note: we do not have a reliable method to detect if the floppy is
700 present. The current method is to try to open the floppy at every
701 I/O and to keep it opened during a few hundreds of ms. */
702 static int fd_open(BlockDriverState
*bs
)
704 BDRVRawState
*s
= bs
->opaque
;
705 int last_media_present
;
707 if (s
->type
!= FTYPE_FD
)
709 last_media_present
= (s
->fd
>= 0);
711 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
715 printf("Floppy closed\n");
719 if (s
->fd_got_error
&&
720 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
722 printf("No floppy (open delayed)\n");
726 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
728 s
->fd_error_time
= qemu_get_clock(rt_clock
);
730 if (last_media_present
)
731 s
->fd_media_changed
= 1;
733 printf("No floppy\n");
738 printf("Floppy opened\n");
741 if (!last_media_present
)
742 s
->fd_media_changed
= 1;
743 s
->fd_open_time
= qemu_get_clock(rt_clock
);
748 static int fd_open(BlockDriverState
*bs
)
754 #if defined(__linux__)
756 static int raw_is_inserted(BlockDriverState
*bs
)
758 BDRVRawState
*s
= bs
->opaque
;
763 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
764 if (ret
== CDS_DISC_OK
)
777 /* currently only used by fdc.c, but a CD version would be good too */
778 static int raw_media_changed(BlockDriverState
*bs
)
780 BDRVRawState
*s
= bs
->opaque
;
786 /* XXX: we do not have a true media changed indication. It
787 does not work if the floppy is changed without trying
790 ret
= s
->fd_media_changed
;
791 s
->fd_media_changed
= 0;
793 printf("Floppy changed=%d\n", ret
);
802 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
804 BDRVRawState
*s
= bs
->opaque
;
809 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
810 perror("CDROMEJECT");
812 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
813 perror("CDROMEJECT");
823 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
825 if (ioctl(fd
, FDEJECT
, 0) < 0)
837 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
839 BDRVRawState
*s
= bs
->opaque
;
843 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
844 /* Note: an error can happen if the distribution automatically
846 // perror("CDROM_LOCKDOOR");
857 static int raw_is_inserted(BlockDriverState
*bs
)
862 static int raw_media_changed(BlockDriverState
*bs
)
867 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
872 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
879 BlockDriver bdrv_host_device
= {
881 sizeof(BDRVRawState
),
882 NULL
, /* no probe for protocols */
890 .bdrv_aio_read
= raw_aio_read
,
891 .bdrv_aio_write
= raw_aio_write
,
892 .bdrv_aio_cancel
= raw_aio_cancel
,
893 .aiocb_size
= sizeof(RawAIOCB
),
894 .bdrv_pread
= raw_pread
,
895 .bdrv_pwrite
= raw_pwrite
,
896 .bdrv_getlength
= raw_getlength
,
898 /* removable device support */
899 .bdrv_is_inserted
= raw_is_inserted
,
900 .bdrv_media_changed
= raw_media_changed
,
901 .bdrv_eject
= raw_eject
,
902 .bdrv_set_locked
= raw_set_locked
,
907 /* XXX: use another file ? */
908 #include <winioctl.h>
912 #define FTYPE_HARDDISK 2
914 typedef struct BDRVRawState
{
917 char drive_path
[16]; /* format: "d:\" */
920 typedef struct RawAIOCB
{
921 BlockDriverAIOCB common
;
927 int qemu_ftruncate64(int fd
, int64_t length
)
934 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
937 h
= (HANDLE
)_get_osfhandle(fd
);
939 /* get current position, ftruncate do not change position */
941 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
942 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
946 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
948 res
= SetEndOfFile(h
);
950 /* back to old position */
951 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
955 static int set_sparse(int fd
)
958 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
959 NULL
, 0, NULL
, 0, &returned
, NULL
);
962 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
964 BDRVRawState
*s
= bs
->opaque
;
965 int access_flags
, create_flags
;
968 s
->type
= FTYPE_FILE
;
970 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
971 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
973 access_flags
= GENERIC_READ
;
975 if (flags
& BDRV_O_CREAT
) {
976 create_flags
= CREATE_ALWAYS
;
978 create_flags
= OPEN_EXISTING
;
981 overlapped
= FILE_ATTRIBUTE_NORMAL
;
983 overlapped
= FILE_FLAG_OVERLAPPED
;
985 s
->hfile
= CreateFile(filename
, access_flags
,
986 FILE_SHARE_READ
, NULL
,
987 create_flags
, overlapped
, NULL
);
988 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
989 int err
= GetLastError();
991 if (err
== ERROR_ACCESS_DENIED
)
998 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
999 uint8_t *buf
, int count
)
1001 BDRVRawState
*s
= bs
->opaque
;
1006 memset(&ov
, 0, sizeof(ov
));
1008 ov
.OffsetHigh
= offset
>> 32;
1009 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
1011 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
1020 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
1021 const uint8_t *buf
, int count
)
1023 BDRVRawState
*s
= bs
->opaque
;
1028 memset(&ov
, 0, sizeof(ov
));
1030 ov
.OffsetHigh
= offset
>> 32;
1031 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
1033 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
1044 static void raw_aio_cb(void *opaque
)
1046 RawAIOCB
*acb
= opaque
;
1047 BlockDriverState
*bs
= acb
->common
.bs
;
1048 BDRVRawState
*s
= bs
->opaque
;
1052 ret
= GetOverlappedResult(s
->hfile
, &acb
->ov
, &ret_count
, TRUE
);
1053 if (!ret
|| ret_count
!= acb
->count
) {
1054 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
1056 acb
->common
.cb(acb
->common
.opaque
, 0);
1061 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
1062 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1063 BlockDriverCompletionFunc
*cb
, void *opaque
)
1068 acb
= qemu_aio_get(bs
, cb
, opaque
);
1070 acb
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
1072 qemu_aio_release(acb
);
1076 memset(&acb
->ov
, 0, sizeof(acb
->ov
));
1077 offset
= sector_num
* 512;
1078 acb
->ov
.Offset
= offset
;
1079 acb
->ov
.OffsetHigh
= offset
>> 32;
1080 acb
->ov
.hEvent
= acb
->hEvent
;
1081 acb
->count
= nb_sectors
* 512;
1083 qemu_add_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
1088 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
1089 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1090 BlockDriverCompletionFunc
*cb
, void *opaque
)
1092 BDRVRawState
*s
= bs
->opaque
;
1096 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1099 ret
= ReadFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
1101 qemu_aio_release(acb
);
1105 qemu_aio_release(acb
);
1107 return (BlockDriverAIOCB
*)acb
;
1110 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
1111 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
1112 BlockDriverCompletionFunc
*cb
, void *opaque
)
1114 BDRVRawState
*s
= bs
->opaque
;
1118 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
1121 ret
= WriteFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
1123 qemu_aio_release(acb
);
1127 qemu_aio_release(acb
);
1129 return (BlockDriverAIOCB
*)acb
;
1132 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
1135 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
1136 BlockDriverState
*bs
= acb
->common
.bs
;
1137 BDRVRawState
*s
= bs
->opaque
;
1139 qemu_del_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
1140 /* XXX: if more than one async I/O it is not correct */
1142 qemu_aio_release(acb
);
1147 static void raw_flush(BlockDriverState
*bs
)
1149 BDRVRawState
*s
= bs
->opaque
;
1150 FlushFileBuffers(s
->hfile
);
1153 static void raw_close(BlockDriverState
*bs
)
1155 BDRVRawState
*s
= bs
->opaque
;
1156 CloseHandle(s
->hfile
);
1159 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
1161 BDRVRawState
*s
= bs
->opaque
;
1165 high
= offset
>> 32;
1166 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
1168 if (!SetEndOfFile(s
->hfile
))
1173 static int64_t raw_getlength(BlockDriverState
*bs
)
1175 BDRVRawState
*s
= bs
->opaque
;
1177 ULARGE_INTEGER available
, total
, total_free
;
1178 DISK_GEOMETRY_EX dg
;
1184 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
1185 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
1189 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
1191 l
.QuadPart
= total
.QuadPart
;
1193 case FTYPE_HARDDISK
:
1194 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
1195 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
1206 static int raw_create(const char *filename
, int64_t total_size
,
1207 const char *backing_file
, int flags
)
1211 if (flags
|| backing_file
)
1214 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1219 ftruncate(fd
, total_size
* 512);
1224 void qemu_aio_init(void)
1228 void qemu_aio_poll(void)
1232 void qemu_aio_flush(void)
1236 void qemu_aio_wait_start(void)
1240 void qemu_aio_wait(void)
1247 void qemu_aio_wait_end(void)
1251 BlockDriver bdrv_raw
= {
1253 sizeof(BDRVRawState
),
1254 NULL
, /* no probe for protocols */
1263 .bdrv_aio_read
= raw_aio_read
,
1264 .bdrv_aio_write
= raw_aio_write
,
1265 .bdrv_aio_cancel
= raw_aio_cancel
,
1266 .aiocb_size
= sizeof(RawAIOCB
);
1268 .protocol_name
= "file",
1269 .bdrv_pread
= raw_pread
,
1270 .bdrv_pwrite
= raw_pwrite
,
1271 .bdrv_truncate
= raw_truncate
,
1272 .bdrv_getlength
= raw_getlength
,
1275 /***********************************************/
1278 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
1280 char drives
[256], *pdrv
= drives
;
1283 memset(drives
, 0, sizeof(drives
));
1284 GetLogicalDriveStrings(sizeof(drives
), drives
);
1285 while(pdrv
[0] != '\0') {
1286 type
= GetDriveType(pdrv
);
1289 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
1293 pdrv
+= lstrlen(pdrv
) + 1;
1298 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
1300 BDRVRawState
*s
= bs
->opaque
;
1304 if (strstart(filename
, "\\\\.\\", &p
) ||
1305 strstart(filename
, "//./", &p
)) {
1306 if (stristart(p
, "PhysicalDrive", NULL
))
1307 return FTYPE_HARDDISK
;
1308 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
1309 type
= GetDriveType(s
->drive_path
);
1310 if (type
== DRIVE_CDROM
)
1319 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1321 BDRVRawState
*s
= bs
->opaque
;
1322 int access_flags
, create_flags
;
1324 char device_name
[64];
1326 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1327 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
1329 filename
= device_name
;
1331 /* transform drive letters into device name */
1332 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
1333 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
1334 filename
[1] == ':' && filename
[2] == '\0') {
1335 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
1336 filename
= device_name
;
1339 s
->type
= find_device_type(bs
, filename
);
1341 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
1342 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
1344 access_flags
= GENERIC_READ
;
1346 create_flags
= OPEN_EXISTING
;
1349 overlapped
= FILE_ATTRIBUTE_NORMAL
;
1351 overlapped
= FILE_FLAG_OVERLAPPED
;
1353 s
->hfile
= CreateFile(filename
, access_flags
,
1354 FILE_SHARE_READ
, NULL
,
1355 create_flags
, overlapped
, NULL
);
1356 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
1357 int err
= GetLastError();
1359 if (err
== ERROR_ACCESS_DENIED
)
1367 /***********************************************/
1368 /* removable device additional commands */
1370 static int raw_is_inserted(BlockDriverState
*bs
)
1375 static int raw_media_changed(BlockDriverState
*bs
)
1380 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1384 if (s
->type
== FTYPE_FILE
)
1387 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
1388 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
1390 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
1391 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
1395 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1401 BlockDriver bdrv_host_device
= {
1403 sizeof(BDRVRawState
),
1404 NULL
, /* no probe for protocols */
1413 .bdrv_aio_read
= raw_aio_read
,
1414 .bdrv_aio_write
= raw_aio_write
,
1415 .bdrv_aio_cancel
= raw_aio_cancel
,
1416 .aiocb_size
= sizeof(RawAIOCB
);
1418 .bdrv_pread
= raw_pread
,
1419 .bdrv_pwrite
= raw_pwrite
,
1420 .bdrv_getlength
= raw_getlength
,