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 typedef struct BDRVRawState
{
55 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
56 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
58 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
60 kern_return_t kernResult
;
61 mach_port_t masterPort
;
62 CFMutableDictionaryRef classesToMatch
;
64 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
65 if ( KERN_SUCCESS
!= kernResult
) {
66 printf( "IOMasterPort returned %d\n", kernResult
);
69 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
70 if ( classesToMatch
== NULL
) {
71 printf( "IOServiceMatching returned a NULL dictionary.\n" );
73 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
75 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
76 if ( KERN_SUCCESS
!= kernResult
)
78 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
84 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
86 io_object_t nextMedia
;
87 kern_return_t kernResult
= KERN_FAILURE
;
89 nextMedia
= IOIteratorNext( mediaIterator
);
92 CFTypeRef bsdPathAsCFString
;
93 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
94 if ( bsdPathAsCFString
) {
96 strcpy( bsdPath
, _PATH_DEV
);
97 strcat( bsdPath
, "r" );
98 devPathLength
= strlen( bsdPath
);
99 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
100 kernResult
= KERN_SUCCESS
;
102 CFRelease( bsdPathAsCFString
);
104 IOObjectRelease( nextMedia
);
112 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
114 BDRVRawState
*s
= bs
->opaque
;
118 if (strstart(filename
, "/dev/cdrom", NULL
)) {
119 kern_return_t kernResult
;
120 io_iterator_t mediaIterator
;
121 char bsdPath
[ MAXPATHLEN
];
124 kernResult
= FindEjectableCDMedia( &mediaIterator
);
125 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
127 if ( bsdPath
[ 0 ] != '\0' ) {
128 strcat(bsdPath
,"s0");
129 /* some CDs don't have a partition 0 */
130 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
132 bsdPath
[strlen(bsdPath
)-1] = '1';
140 IOObjectRelease( mediaIterator
);
143 open_flags
= O_BINARY
;
144 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
145 open_flags
|= O_RDWR
;
147 open_flags
|= O_RDONLY
;
150 if (flags
& BDRV_O_CREAT
)
151 open_flags
|= O_CREAT
| O_TRUNC
;
153 fd
= open(filename
, open_flags
, 0644);
160 /* XXX: use host sector size if necessary with:
161 #ifdef DIOCGSECTORSIZE
163 unsigned int sectorsize = 512;
164 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
165 sectorsize > bufsize)
166 bufsize = sectorsize;
170 u_int32_t blockSize = 512;
171 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
177 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
178 uint8_t *buf
, int count
)
180 BDRVRawState
*s
= bs
->opaque
;
183 lseek(s
->fd
, offset
, SEEK_SET
);
184 ret
= read(s
->fd
, buf
, count
);
188 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
189 const uint8_t *buf
, int count
)
191 BDRVRawState
*s
= bs
->opaque
;
194 lseek(s
->fd
, offset
, SEEK_SET
);
195 ret
= write(s
->fd
, buf
, count
);
199 /***********************************************************/
200 /* Unix AOP using POSIX AIO */
202 typedef struct RawAIOCB
{
204 int busy
; /* only used for debugging */
205 BlockDriverAIOCB
*next
;
208 static int aio_sig_num
= SIGUSR2
;
209 static BlockDriverAIOCB
*first_aio
; /* AIO issued */
210 static int aio_initialized
= 0;
212 static void aio_signal_handler(int signum
)
215 CPUState
*env
= cpu_single_env
;
217 /* stop the currently executing cpu because a timer occured */
218 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
220 if (env
->kqemu_enabled
) {
221 kqemu_cpu_interrupt(env
);
228 void qemu_aio_init(void)
230 struct sigaction act
;
234 sigfillset(&act
.sa_mask
);
235 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
236 act
.sa_handler
= aio_signal_handler
;
237 sigaction(aio_sig_num
, &act
, NULL
);
240 /* XXX: aio thread exit seems to hang on RH 9 */
242 memset(&ai
, 0, sizeof(ai
));
245 ai
.aio_idle_time
= 365 * 100000;
250 void qemu_aio_poll(void)
252 BlockDriverAIOCB
*acb
, **pacb
;
263 ret
= aio_error(&acb1
->aiocb
);
264 if (ret
== ECANCELED
) {
265 /* remove the request */
268 } else if (ret
!= EINPROGRESS
) {
271 ret
= aio_return(&acb1
->aiocb
);
272 if (ret
== acb1
->aiocb
.aio_nbytes
)
279 /* remove the request */
282 /* call the callback */
283 acb
->cb(acb
->cb_opaque
, ret
);
293 /* wait until at least one AIO was handled */
294 static sigset_t wait_oset
;
296 void qemu_aio_wait_start(void)
300 if (!aio_initialized
)
303 sigaddset(&set
, aio_sig_num
);
304 sigprocmask(SIG_BLOCK
, &set
, &wait_oset
);
307 void qemu_aio_wait(void)
317 sigaddset(&set
, aio_sig_num
);
318 sigwait(&set
, &nb_sigs
);
322 void qemu_aio_wait_end(void)
324 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
327 static int raw_aio_new(BlockDriverAIOCB
*acb
)
330 BDRVRawState
*s
= acb
->bs
->opaque
;
332 acb1
= qemu_mallocz(sizeof(RawAIOCB
));
336 acb1
->aiocb
.aio_fildes
= s
->fd
;
337 acb1
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
338 acb1
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
342 static int raw_aio_read(BlockDriverAIOCB
*acb
, int64_t sector_num
,
343 uint8_t *buf
, int nb_sectors
)
345 RawAIOCB
*acb1
= acb
->opaque
;
347 assert(acb1
->busy
== 0);
349 acb1
->aiocb
.aio_buf
= buf
;
350 acb1
->aiocb
.aio_nbytes
= nb_sectors
* 512;
351 acb1
->aiocb
.aio_offset
= sector_num
* 512;
352 acb1
->next
= first_aio
;
354 if (aio_read(&acb1
->aiocb
) < 0) {
361 static int raw_aio_write(BlockDriverAIOCB
*acb
, int64_t sector_num
,
362 const uint8_t *buf
, int nb_sectors
)
364 RawAIOCB
*acb1
= acb
->opaque
;
366 assert(acb1
->busy
== 0);
368 acb1
->aiocb
.aio_buf
= (uint8_t *)buf
;
369 acb1
->aiocb
.aio_nbytes
= nb_sectors
* 512;
370 acb1
->aiocb
.aio_offset
= sector_num
* 512;
371 acb1
->next
= first_aio
;
373 if (aio_write(&acb1
->aiocb
) < 0) {
380 static void raw_aio_cancel(BlockDriverAIOCB
*acb
)
382 RawAIOCB
*acb1
= acb
->opaque
;
384 BlockDriverAIOCB
**pacb
;
386 ret
= aio_cancel(acb1
->aiocb
.aio_fildes
, &acb1
->aiocb
);
387 if (ret
== AIO_NOTCANCELED
) {
388 /* fail safe: if the aio could not be canceled, we wait for
390 while (aio_error(&acb1
->aiocb
) == EINPROGRESS
);
393 /* remove the callback from the queue */
398 } else if (*pacb
== acb
) {
403 acb1
= (*pacb
)->opaque
;
408 static void raw_aio_delete(BlockDriverAIOCB
*acb
)
410 RawAIOCB
*acb1
= acb
->opaque
;
415 static void raw_close(BlockDriverState
*bs
)
417 BDRVRawState
*s
= bs
->opaque
;
421 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
423 BDRVRawState
*s
= bs
->opaque
;
424 if (ftruncate(s
->fd
, offset
) < 0)
429 static int64_t raw_getlength(BlockDriverState
*bs
)
431 BDRVRawState
*s
= bs
->opaque
;
438 struct dk_minfo minfo
;
443 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
444 #ifdef DIOCGMEDIASIZE
445 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
448 size
= LONG_LONG_MAX
;
450 size
= lseek(fd
, 0LL, SEEK_END
);
456 * use the DKIOCGMEDIAINFO ioctl to read the size.
458 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
460 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
461 } else /* there are reports that lseek on some devices
462 fails, but irc discussion said that contingency
463 on contingency was overkill */
466 size
= lseek(fd
, 0, SEEK_END
);
469 /* On Windows hosts it can happen that we're unable to get file size
470 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
472 size
= LONG_LONG_MAX
;
477 static int raw_create(const char *filename
, int64_t total_size
,
478 const char *backing_file
, int flags
)
482 if (flags
|| backing_file
)
485 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
489 ftruncate(fd
, total_size
* 512);
494 static void raw_flush(BlockDriverState
*bs
)
496 BDRVRawState
*s
= bs
->opaque
;
500 BlockDriver bdrv_raw
= {
502 sizeof(BDRVRawState
),
503 NULL
, /* no probe for protocols */
511 .bdrv_aio_new
= raw_aio_new
,
512 .bdrv_aio_read
= raw_aio_read
,
513 .bdrv_aio_write
= raw_aio_write
,
514 .bdrv_aio_cancel
= raw_aio_cancel
,
515 .bdrv_aio_delete
= raw_aio_delete
,
516 .protocol_name
= "file",
517 .bdrv_pread
= raw_pread
,
518 .bdrv_pwrite
= raw_pwrite
,
519 .bdrv_truncate
= raw_truncate
,
520 .bdrv_getlength
= raw_getlength
,
525 /* XXX: use another file ? */
526 #include <winioctl.h>
528 typedef struct BDRVRawState
{
532 typedef struct RawAIOCB
{
538 int qemu_ftruncate64(int fd
, int64_t length
)
545 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
548 h
= (HANDLE
)_get_osfhandle(fd
);
550 /* get current position, ftruncate do not change position */
552 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
553 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
557 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
559 res
= SetEndOfFile(h
);
561 /* back to old position */
562 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
566 static int set_sparse(int fd
)
569 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
570 NULL
, 0, NULL
, 0, &returned
, NULL
);
573 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
575 BDRVRawState
*s
= bs
->opaque
;
576 int access_flags
, create_flags
;
578 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
579 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
581 access_flags
= GENERIC_READ
;
583 if (flags
& BDRV_O_CREAT
) {
584 create_flags
= CREATE_ALWAYS
;
586 create_flags
= OPEN_EXISTING
;
588 s
->hfile
= CreateFile(filename
, access_flags
,
589 FILE_SHARE_READ
, NULL
,
590 create_flags
, FILE_FLAG_OVERLAPPED
, 0);
591 if (s
->hfile
== INVALID_HANDLE_VALUE
)
596 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
597 uint8_t *buf
, int count
)
599 BDRVRawState
*s
= bs
->opaque
;
604 memset(&ov
, 0, sizeof(ov
));
606 ov
.OffsetHigh
= offset
>> 32;
607 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
609 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
618 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
619 const uint8_t *buf
, int count
)
621 BDRVRawState
*s
= bs
->opaque
;
626 memset(&ov
, 0, sizeof(ov
));
628 ov
.OffsetHigh
= offset
>> 32;
629 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
631 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
640 static int raw_aio_new(BlockDriverAIOCB
*acb
)
644 acb1
= qemu_mallocz(sizeof(RawAIOCB
));
648 acb1
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
654 static void raw_aio_cb(void *opaque
)
656 BlockDriverAIOCB
*acb
= opaque
;
657 BlockDriverState
*bs
= acb
->bs
;
658 BDRVRawState
*s
= bs
->opaque
;
659 RawAIOCB
*acb1
= acb
->opaque
;
663 ret
= GetOverlappedResult(s
->hfile
, &acb1
->ov
, &ret_count
, TRUE
);
664 if (!ret
|| ret_count
!= acb1
->count
) {
665 acb
->cb(acb
->cb_opaque
, -EIO
);
667 acb
->cb(acb
->cb_opaque
, 0);
671 static int raw_aio_read(BlockDriverAIOCB
*acb
, int64_t sector_num
,
672 uint8_t *buf
, int nb_sectors
)
674 BlockDriverState
*bs
= acb
->bs
;
675 BDRVRawState
*s
= bs
->opaque
;
676 RawAIOCB
*acb1
= acb
->opaque
;
680 memset(&acb1
->ov
, 0, sizeof(acb1
->ov
));
681 offset
= sector_num
* 512;
682 acb1
->ov
.Offset
= offset
;
683 acb1
->ov
.OffsetHigh
= offset
>> 32;
684 acb1
->ov
.hEvent
= acb1
->hEvent
;
685 acb1
->count
= nb_sectors
* 512;
687 qemu_add_wait_object(acb1
->ov
.hEvent
, raw_aio_cb
, acb
);
689 ret
= ReadFile(s
->hfile
, buf
, acb1
->count
, NULL
, &acb1
->ov
);
695 static int raw_aio_write(BlockDriverAIOCB
*acb
, int64_t sector_num
,
696 uint8_t *buf
, int nb_sectors
)
698 BlockDriverState
*bs
= acb
->bs
;
699 BDRVRawState
*s
= bs
->opaque
;
700 RawAIOCB
*acb1
= acb
->opaque
;
704 memset(&acb1
->ov
, 0, sizeof(acb1
->ov
));
705 offset
= sector_num
* 512;
706 acb1
->ov
.Offset
= offset
;
707 acb1
->ov
.OffsetHigh
= offset
>> 32;
708 acb1
->ov
.hEvent
= acb1
->hEvent
;
709 acb1
->count
= nb_sectors
* 512;
711 qemu_add_wait_object(acb1
->ov
.hEvent
, raw_aio_cb
, acb
);
713 ret
= ReadFile(s
->hfile
, buf
, acb1
->count
, NULL
, &acb1
->ov
);
719 static void raw_aio_cancel(BlockDriverAIOCB
*acb
)
721 BlockDriverState
*bs
= acb
->bs
;
722 BDRVRawState
*s
= bs
->opaque
;
724 RawAIOCB
*acb1
= acb
->opaque
;
726 qemu_del_wait_object(acb1
->ov
.hEvent
, raw_aio_cb
, acb
);
728 /* XXX: if more than one async I/O it is not correct */
732 static void raw_aio_delete(BlockDriverAIOCB
*acb
)
734 RawAIOCB
*acb1
= acb
->opaque
;
736 CloseHandle(acb1
->hEvent
);
740 static void raw_flush(BlockDriverState
*bs
)
745 static void raw_close(BlockDriverState
*bs
)
747 BDRVRawState
*s
= bs
->opaque
;
748 CloseHandle(s
->hfile
);
751 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
753 BDRVRawState
*s
= bs
->opaque
;
758 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
760 if (!SetEndOfFile(s
->hfile
))
765 static int64_t raw_getlength(BlockDriverState
*bs
)
767 BDRVRawState
*s
= bs
->opaque
;
770 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
771 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
776 static int raw_create(const char *filename
, int64_t total_size
,
777 const char *backing_file
, int flags
)
781 if (flags
|| backing_file
)
784 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
789 ftruncate(fd
, total_size
* 512);
794 void qemu_aio_init(void)
798 void qemu_aio_poll(void)
802 void qemu_aio_wait_start(void)
806 void qemu_aio_wait(void)
810 void qemu_aio_wait_end(void)
814 BlockDriver bdrv_raw
= {
816 sizeof(BDRVRawState
),
817 NULL
, /* no probe for protocols */
826 .bdrv_aio_new
= raw_aio_new
,
827 .bdrv_aio_read
= raw_aio_read
,
828 .bdrv_aio_write
= raw_aio_write
,
829 .bdrv_aio_cancel
= raw_aio_cancel
,
830 .bdrv_aio_delete
= raw_aio_delete
,
832 .protocol_name
= "file",
833 .bdrv_pread
= raw_pread
,
834 .bdrv_pwrite
= raw_pwrite
,
835 .bdrv_truncate
= raw_truncate
,
836 .bdrv_getlength
= raw_getlength
,