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)
312 sigaddset(&set
, aio_sig_num
);
313 sigwait(&set
, &nb_sigs
);
317 void qemu_aio_wait_end(void)
319 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
322 static int raw_aio_new(BlockDriverAIOCB
*acb
)
325 BDRVRawState
*s
= acb
->bs
->opaque
;
327 acb1
= qemu_mallocz(sizeof(RawAIOCB
));
331 acb1
->aiocb
.aio_fildes
= s
->fd
;
332 acb1
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
333 acb1
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
337 static int raw_aio_read(BlockDriverAIOCB
*acb
, int64_t sector_num
,
338 uint8_t *buf
, int nb_sectors
)
340 RawAIOCB
*acb1
= acb
->opaque
;
342 assert(acb1
->busy
== 0);
344 acb1
->aiocb
.aio_buf
= buf
;
345 acb1
->aiocb
.aio_nbytes
= nb_sectors
* 512;
346 acb1
->aiocb
.aio_offset
= sector_num
* 512;
347 acb1
->next
= first_aio
;
349 if (aio_read(&acb1
->aiocb
) < 0) {
356 static int raw_aio_write(BlockDriverAIOCB
*acb
, int64_t sector_num
,
357 const uint8_t *buf
, int nb_sectors
)
359 RawAIOCB
*acb1
= acb
->opaque
;
361 assert(acb1
->busy
== 0);
363 acb1
->aiocb
.aio_buf
= (uint8_t *)buf
;
364 acb1
->aiocb
.aio_nbytes
= nb_sectors
* 512;
365 acb1
->aiocb
.aio_offset
= sector_num
* 512;
366 acb1
->next
= first_aio
;
368 if (aio_write(&acb1
->aiocb
) < 0) {
375 static void raw_aio_cancel(BlockDriverAIOCB
*acb
)
377 RawAIOCB
*acb1
= acb
->opaque
;
379 BlockDriverAIOCB
**pacb
;
381 ret
= aio_cancel(acb1
->aiocb
.aio_fildes
, &acb1
->aiocb
);
382 if (ret
== AIO_NOTCANCELED
) {
383 /* fail safe: if the aio could not be canceled, we wait for
385 while (aio_error(&acb1
->aiocb
) == EINPROGRESS
);
388 /* remove the callback from the queue */
393 } else if (*pacb
== acb
) {
398 acb1
= (*pacb
)->opaque
;
403 static void raw_aio_delete(BlockDriverAIOCB
*acb
)
405 RawAIOCB
*acb1
= acb
->opaque
;
410 static void raw_close(BlockDriverState
*bs
)
412 BDRVRawState
*s
= bs
->opaque
;
416 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
418 BDRVRawState
*s
= bs
->opaque
;
419 if (ftruncate(s
->fd
, offset
) < 0)
424 static int64_t raw_getlength(BlockDriverState
*bs
)
426 BDRVRawState
*s
= bs
->opaque
;
433 struct dk_minfo minfo
;
438 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
439 #ifdef DIOCGMEDIASIZE
440 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
443 size
= LONG_LONG_MAX
;
445 size
= lseek(fd
, 0LL, SEEK_END
);
451 * use the DKIOCGMEDIAINFO ioctl to read the size.
453 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
455 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
456 } else /* there are reports that lseek on some devices
457 fails, but irc discussion said that contingency
458 on contingency was overkill */
461 size
= lseek(fd
, 0, SEEK_END
);
464 /* On Windows hosts it can happen that we're unable to get file size
465 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
467 size
= LONG_LONG_MAX
;
472 static int raw_create(const char *filename
, int64_t total_size
,
473 const char *backing_file
, int flags
)
477 if (flags
|| backing_file
)
480 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
484 ftruncate(fd
, total_size
* 512);
489 static void raw_flush(BlockDriverState
*bs
)
491 BDRVRawState
*s
= bs
->opaque
;
495 BlockDriver bdrv_raw
= {
497 sizeof(BDRVRawState
),
498 NULL
, /* no probe for protocols */
506 .bdrv_aio_new
= raw_aio_new
,
507 .bdrv_aio_read
= raw_aio_read
,
508 .bdrv_aio_write
= raw_aio_write
,
509 .bdrv_aio_cancel
= raw_aio_cancel
,
510 .bdrv_aio_delete
= raw_aio_delete
,
511 .protocol_name
= "file",
512 .bdrv_pread
= raw_pread
,
513 .bdrv_pwrite
= raw_pwrite
,
514 .bdrv_truncate
= raw_truncate
,
515 .bdrv_getlength
= raw_getlength
,
520 /* XXX: use another file ? */
521 #include <winioctl.h>
523 typedef struct BDRVRawState
{
527 typedef struct RawAIOCB
{
533 int qemu_ftruncate64(int fd
, int64_t length
)
540 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
543 h
= (HANDLE
)_get_osfhandle(fd
);
545 /* get current position, ftruncate do not change position */
547 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
548 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
552 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
554 res
= SetEndOfFile(h
);
556 /* back to old position */
557 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
561 static int set_sparse(int fd
)
564 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
565 NULL
, 0, NULL
, 0, &returned
, NULL
);
568 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
570 BDRVRawState
*s
= bs
->opaque
;
571 int access_flags
, create_flags
;
573 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
574 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
576 access_flags
= GENERIC_READ
;
578 if (flags
& BDRV_O_CREAT
) {
579 create_flags
= CREATE_ALWAYS
;
581 create_flags
= OPEN_EXISTING
;
583 s
->hfile
= CreateFile(filename
, access_flags
,
584 FILE_SHARE_READ
, NULL
,
585 create_flags
, FILE_FLAG_OVERLAPPED
, 0);
586 if (s
->hfile
== INVALID_HANDLE_VALUE
)
591 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
592 uint8_t *buf
, int count
)
594 BDRVRawState
*s
= bs
->opaque
;
599 memset(&ov
, 0, sizeof(ov
));
601 ov
.OffsetHigh
= offset
>> 32;
602 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
604 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
613 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
614 const uint8_t *buf
, int count
)
616 BDRVRawState
*s
= bs
->opaque
;
621 memset(&ov
, 0, sizeof(ov
));
623 ov
.OffsetHigh
= offset
>> 32;
624 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
626 ret
= GetOverlappedResult(s
->hfile
, &ov
, &ret_count
, TRUE
);
635 static int raw_aio_new(BlockDriverAIOCB
*acb
)
639 acb1
= qemu_mallocz(sizeof(RawAIOCB
));
643 acb1
->hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
649 static void raw_aio_cb(void *opaque
)
651 BlockDriverAIOCB
*acb
= opaque
;
652 BlockDriverState
*bs
= acb
->bs
;
653 BDRVRawState
*s
= bs
->opaque
;
654 RawAIOCB
*acb1
= acb
->opaque
;
658 ret
= GetOverlappedResult(s
->hfile
, &acb1
->ov
, &ret_count
, TRUE
);
659 if (!ret
|| ret_count
!= acb1
->count
) {
660 acb
->cb(acb
->cb_opaque
, -EIO
);
662 acb
->cb(acb
->cb_opaque
, 0);
666 static int raw_aio_read(BlockDriverAIOCB
*acb
, int64_t sector_num
,
667 uint8_t *buf
, int nb_sectors
)
669 BlockDriverState
*bs
= acb
->bs
;
670 BDRVRawState
*s
= bs
->opaque
;
671 RawAIOCB
*acb1
= acb
->opaque
;
675 memset(&acb1
->ov
, 0, sizeof(acb1
->ov
));
676 offset
= sector_num
* 512;
677 acb1
->ov
.Offset
= offset
;
678 acb1
->ov
.OffsetHigh
= offset
>> 32;
679 acb1
->ov
.hEvent
= acb1
->hEvent
;
680 acb1
->count
= nb_sectors
* 512;
682 qemu_add_wait_object(acb1
->ov
.hEvent
, raw_aio_cb
, acb
);
684 ret
= ReadFile(s
->hfile
, buf
, acb1
->count
, NULL
, &acb1
->ov
);
690 static int raw_aio_write(BlockDriverAIOCB
*acb
, int64_t sector_num
,
691 uint8_t *buf
, int nb_sectors
)
693 BlockDriverState
*bs
= acb
->bs
;
694 BDRVRawState
*s
= bs
->opaque
;
695 RawAIOCB
*acb1
= acb
->opaque
;
699 memset(&acb1
->ov
, 0, sizeof(acb1
->ov
));
700 offset
= sector_num
* 512;
701 acb1
->ov
.Offset
= offset
;
702 acb1
->ov
.OffsetHigh
= offset
>> 32;
703 acb1
->ov
.hEvent
= acb1
->hEvent
;
704 acb1
->count
= nb_sectors
* 512;
706 qemu_add_wait_object(acb1
->ov
.hEvent
, raw_aio_cb
, acb
);
708 ret
= ReadFile(s
->hfile
, buf
, acb1
->count
, NULL
, &acb1
->ov
);
714 static void raw_aio_cancel(BlockDriverAIOCB
*acb
)
716 BlockDriverState
*bs
= acb
->bs
;
717 BDRVRawState
*s
= bs
->opaque
;
719 RawAIOCB
*acb1
= acb
->opaque
;
721 qemu_del_wait_object(acb1
->ov
.hEvent
, raw_aio_cb
, acb
);
723 /* XXX: if more than one async I/O it is not correct */
727 static void raw_aio_delete(BlockDriverAIOCB
*acb
)
729 RawAIOCB
*acb1
= acb
->opaque
;
731 CloseHandle(acb1
->hEvent
);
735 static void raw_flush(BlockDriverState
*bs
)
740 static void raw_close(BlockDriverState
*bs
)
742 BDRVRawState
*s
= bs
->opaque
;
743 CloseHandle(s
->hfile
);
746 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
748 BDRVRawState
*s
= bs
->opaque
;
753 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
755 if (!SetEndOfFile(s
->hfile
))
760 static int64_t raw_getlength(BlockDriverState
*bs
)
762 BDRVRawState
*s
= bs
->opaque
;
765 l
.LowPart
= GetFileSize(s
->hfile
, &l
.HighPart
);
766 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
771 static int raw_create(const char *filename
, int64_t total_size
,
772 const char *backing_file
, int flags
)
776 if (flags
|| backing_file
)
779 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
784 ftruncate(fd
, total_size
* 512);
789 void qemu_aio_init(void)
793 void qemu_aio_poll(void)
797 void qemu_aio_wait_start(void)
801 void qemu_aio_wait(void)
805 void qemu_aio_wait_end(void)
809 BlockDriver bdrv_raw
= {
811 sizeof(BDRVRawState
),
812 NULL
, /* no probe for protocols */
821 .bdrv_aio_new
= raw_aio_new
,
822 .bdrv_aio_read
= raw_aio_read
,
823 .bdrv_aio_write
= raw_aio_write
,
824 .bdrv_aio_cancel
= raw_aio_cancel
,
825 .bdrv_aio_delete
= raw_aio_delete
,
827 .protocol_name
= "file",
828 .bdrv_pread
= raw_pread
,
829 .bdrv_pwrite
= raw_pwrite
,
830 .bdrv_truncate
= raw_truncate
,
831 .bdrv_getlength
= raw_getlength
,