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
{
203 BlockDriverAIOCB common
;
205 struct RawAIOCB
*next
;
208 static int aio_sig_num
= SIGUSR2
;
209 static RawAIOCB
*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 RawAIOCB
*acb
, **pacb
;
261 ret
= aio_error(&acb
->aiocb
);
262 if (ret
== ECANCELED
) {
263 /* remove the request */
265 qemu_aio_release(acb
);
266 } else if (ret
!= EINPROGRESS
) {
269 ret
= aio_return(&acb
->aiocb
);
270 if (ret
== acb
->aiocb
.aio_nbytes
)
277 /* remove the request */
279 /* call the callback */
280 acb
->common
.cb(acb
->common
.opaque
, ret
);
281 qemu_aio_release(acb
);
291 /* wait until at least one AIO was handled */
292 static sigset_t wait_oset
;
294 void qemu_aio_wait_start(void)
298 if (!aio_initialized
)
301 sigaddset(&set
, aio_sig_num
);
302 sigprocmask(SIG_BLOCK
, &set
, &wait_oset
);
305 void qemu_aio_wait(void)
315 sigaddset(&set
, aio_sig_num
);
316 sigwait(&set
, &nb_sigs
);
320 void qemu_aio_wait_end(void)
322 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
325 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
326 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
327 BlockDriverCompletionFunc
*cb
, void *opaque
)
329 BDRVRawState
*s
= bs
->opaque
;
332 acb
= qemu_aio_get(bs
, cb
, opaque
);
335 acb
->aiocb
.aio_fildes
= s
->fd
;
336 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
337 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
338 acb
->aiocb
.aio_buf
= buf
;
339 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
340 acb
->aiocb
.aio_offset
= sector_num
* 512;
341 acb
->next
= first_aio
;
346 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
347 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
348 BlockDriverCompletionFunc
*cb
, void *opaque
)
352 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
355 if (aio_read(&acb
->aiocb
) < 0) {
356 qemu_aio_release(acb
);
362 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
363 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
364 BlockDriverCompletionFunc
*cb
, void *opaque
)
368 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
371 if (aio_write(&acb
->aiocb
) < 0) {
372 qemu_aio_release(acb
);
378 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
381 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
384 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
385 if (ret
== AIO_NOTCANCELED
) {
386 /* fail safe: if the aio could not be canceled, we wait for
388 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
391 /* remove the callback from the queue */
396 } else if (*pacb
== acb
) {
398 qemu_aio_release(acb
);
405 static void raw_close(BlockDriverState
*bs
)
407 BDRVRawState
*s
= bs
->opaque
;
411 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
413 BDRVRawState
*s
= bs
->opaque
;
414 if (ftruncate(s
->fd
, offset
) < 0)
419 static int64_t raw_getlength(BlockDriverState
*bs
)
421 BDRVRawState
*s
= bs
->opaque
;
428 struct dk_minfo minfo
;
433 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
434 #ifdef DIOCGMEDIASIZE
435 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
438 size
= LONG_LONG_MAX
;
440 size
= lseek(fd
, 0LL, SEEK_END
);
446 * use the DKIOCGMEDIAINFO ioctl to read the size.
448 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
450 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
451 } else /* there are reports that lseek on some devices
452 fails, but irc discussion said that contingency
453 on contingency was overkill */
456 size
= lseek(fd
, 0, SEEK_END
);
459 /* On Windows hosts it can happen that we're unable to get file size
460 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
462 size
= LONG_LONG_MAX
;
467 static int raw_create(const char *filename
, int64_t total_size
,
468 const char *backing_file
, int flags
)
472 if (flags
|| backing_file
)
475 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
479 ftruncate(fd
, total_size
* 512);
484 static void raw_flush(BlockDriverState
*bs
)
486 BDRVRawState
*s
= bs
->opaque
;
490 BlockDriver bdrv_raw
= {
492 sizeof(BDRVRawState
),
493 NULL
, /* no probe for protocols */
501 .bdrv_aio_read
= raw_aio_read
,
502 .bdrv_aio_write
= raw_aio_write
,
503 .bdrv_aio_cancel
= raw_aio_cancel
,
504 .aiocb_size
= sizeof(RawAIOCB
),
505 .protocol_name
= "file",
506 .bdrv_pread
= raw_pread
,
507 .bdrv_pwrite
= raw_pwrite
,
508 .bdrv_truncate
= raw_truncate
,
509 .bdrv_getlength
= raw_getlength
,
514 /* XXX: use another file ? */
515 #include <winioctl.h>
517 typedef struct BDRVRawState
{
521 typedef struct RawAIOCB
{
522 BlockDriverAIOCB common
;
528 int qemu_ftruncate64(int fd
, int64_t length
)
535 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
538 h
= (HANDLE
)_get_osfhandle(fd
);
540 /* get current position, ftruncate do not change position */
542 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
543 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
547 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
549 res
= SetEndOfFile(h
);
551 /* back to old position */
552 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
556 static int set_sparse(int fd
)
559 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
560 NULL
, 0, NULL
, 0, &returned
, NULL
);
563 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
565 BDRVRawState
*s
= bs
->opaque
;
566 int access_flags
, create_flags
;
569 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
570 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
572 access_flags
= GENERIC_READ
;
574 if (flags
& BDRV_O_CREAT
) {
575 create_flags
= CREATE_ALWAYS
;
577 create_flags
= OPEN_EXISTING
;
582 overlapped
= FILE_FLAG_OVERLAPPED
;
584 s
->hfile
= CreateFile(filename
, access_flags
,
585 FILE_SHARE_READ
, NULL
,
586 create_flags
, overlapped
, 0);
587 if (s
->hfile
== INVALID_HANDLE_VALUE
)
592 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
593 uint8_t *buf
, int count
)
595 BDRVRawState
*s
= bs
->opaque
;
600 memset(&ov
, 0, sizeof(ov
));
602 ov
.OffsetHigh
= offset
>> 32;
603 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
605 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
614 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
615 const uint8_t *buf
, int count
)
617 BDRVRawState
*s
= bs
->opaque
;
622 memset(&ov
, 0, sizeof(ov
));
624 ov
.OffsetHigh
= offset
>> 32;
625 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
627 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
637 static void raw_aio_cb(void *opaque
)
639 RawAIOCB
*acb
= opaque
;
640 BlockDriverState
*bs
= acb
->common
.bs
;
641 BDRVRawState
*s
= bs
->opaque
;
645 ret
= GetOverlappedResult(s
->hfile
, &acb
->ov
, &ret_count
, TRUE
);
646 if (!ret
|| ret_count
!= acb
->count
) {
647 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
649 acb
->common
.cb(acb
->common
.opaque
, 0);
654 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
655 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
656 BlockDriverCompletionFunc
*cb
, void *opaque
)
661 acb
= qemu_aio_get(bs
, cb
, opaque
);
663 acb
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
665 qemu_aio_release(acb
);
669 memset(&acb
->ov
, 0, sizeof(acb
->ov
));
670 offset
= sector_num
* 512;
671 acb
->ov
.Offset
= offset
;
672 acb
->ov
.OffsetHigh
= offset
>> 32;
673 acb
->ov
.hEvent
= acb
->hEvent
;
674 acb
->count
= nb_sectors
* 512;
676 qemu_add_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
681 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
682 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
683 BlockDriverCompletionFunc
*cb
, void *opaque
)
685 BDRVRawState
*s
= bs
->opaque
;
689 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
692 ret
= ReadFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
694 qemu_aio_release(acb
);
698 qemu_aio_release(acb
);
700 return (BlockDriverAIOCB
*)acb
;
703 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
704 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
705 BlockDriverCompletionFunc
*cb
, void *opaque
)
707 BDRVRawState
*s
= bs
->opaque
;
711 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
714 ret
= WriteFile(s
->hfile
, buf
, acb
->count
, NULL
, &acb
->ov
);
716 qemu_aio_release(acb
);
720 qemu_aio_release(acb
);
722 return (BlockDriverAIOCB
*)acb
;
725 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
728 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
729 BlockDriverState
*bs
= acb
->common
.bs
;
730 BDRVRawState
*s
= bs
->opaque
;
732 qemu_del_wait_object(acb
->ov
.hEvent
, raw_aio_cb
, acb
);
733 /* XXX: if more than one async I/O it is not correct */
735 qemu_aio_release(acb
);
739 static void raw_flush(BlockDriverState
*bs
)
744 static void raw_close(BlockDriverState
*bs
)
746 BDRVRawState
*s
= bs
->opaque
;
747 CloseHandle(s
->hfile
);
750 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
752 BDRVRawState
*s
= bs
->opaque
;
757 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
759 if (!SetEndOfFile(s
->hfile
))
764 static int64_t raw_getlength(BlockDriverState
*bs
)
766 BDRVRawState
*s
= bs
->opaque
;
769 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
770 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
775 static int raw_create(const char *filename
, int64_t total_size
,
776 const char *backing_file
, int flags
)
780 if (flags
|| backing_file
)
783 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
788 ftruncate(fd
, total_size
* 512);
793 void qemu_aio_init(void)
797 void qemu_aio_poll(void)
801 void qemu_aio_wait_start(void)
805 void qemu_aio_wait(void)
809 void qemu_aio_wait_end(void)
813 BlockDriver bdrv_raw
= {
815 sizeof(BDRVRawState
),
816 NULL
, /* no probe for protocols */
825 .bdrv_aio_read
= raw_aio_read
,
826 .bdrv_aio_write
= raw_aio_write
,
827 .bdrv_aio_cancel
= raw_aio_cancel
,
828 .aiocb_size
= sizeof(RawAIOCB
);
830 .protocol_name
= "file",
831 .bdrv_pread
= raw_pread
,
832 .bdrv_pwrite
= raw_pwrite
,
833 .bdrv_truncate
= raw_truncate
,
834 .bdrv_getlength
= raw_getlength
,