2 * Block driver for RAW files (posix)
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
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "qemu-char.h"
27 #include "block_int.h"
35 #include <sys/param.h>
36 #include <IOKit/IOKitLib.h>
37 #include <IOKit/IOBSD.h>
38 #include <IOKit/storage/IOMediaBSDClient.h>
39 #include <IOKit/storage/IOMedia.h>
40 #include <IOKit/storage/IOCDMedia.h>
41 //#include <IOKit/storage/IOCDTypes.h>
42 #include <CoreFoundation/CoreFoundation.h>
46 #define _POSIX_PTHREAD_SEMANTICS 1
51 #include <sys/ioctl.h>
52 #include <linux/cdrom.h>
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
66 //#define DEBUG_FLOPPY
69 #if defined(DEBUG_BLOCK)
70 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
71 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
73 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
76 /* OS X does not have O_DSYNC */
78 #define O_DSYNC O_SYNC
81 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
83 #define O_DIRECT O_DSYNC
90 #define ALIGNED_BUFFER_SIZE (32 * 512)
92 /* if the FD is not accessed during that time (in ms), we try to
93 reopen it to see if the disk has been changed */
94 #define FD_OPEN_TIMEOUT 1000
96 /* posix-aio doesn't allow multiple outstanding requests to a single file
97 * descriptor. we implement a pool of dup()'d file descriptors to work
99 #define RAW_FD_POOL_SIZE 64
101 typedef struct BDRVRawState
{
104 unsigned int lseek_err_cnt
;
105 int fd_pool
[RAW_FD_POOL_SIZE
];
106 #if defined(__linux__)
107 /* linux floppy specific */
109 int64_t fd_open_time
;
110 int64_t fd_error_time
;
112 int fd_media_changed
;
114 uint8_t* aligned_buf
;
117 static int posix_aio_init(void);
119 static int fd_open(BlockDriverState
*bs
);
121 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
123 BDRVRawState
*s
= bs
->opaque
;
124 int fd
, open_flags
, ret
;
129 s
->lseek_err_cnt
= 0;
131 open_flags
= O_BINARY
;
132 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
133 open_flags
|= O_RDWR
;
135 open_flags
|= O_RDONLY
;
138 if (flags
& BDRV_O_CREAT
)
139 open_flags
|= O_CREAT
| O_TRUNC
;
141 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
142 * and O_DIRECT for no caching. */
143 if ((flags
& BDRV_O_NOCACHE
))
144 open_flags
|= O_DIRECT
;
145 else if (!(flags
& BDRV_O_CACHE_WB
))
146 open_flags
|= O_DSYNC
;
148 s
->type
= FTYPE_FILE
;
150 fd
= open(filename
, open_flags
, 0644);
158 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++)
160 s
->aligned_buf
= NULL
;
161 if ((flags
& BDRV_O_NOCACHE
)) {
162 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
163 if (s
->aligned_buf
== NULL
) {
172 /* XXX: use host sector size if necessary with:
173 #ifdef DIOCGSECTORSIZE
175 unsigned int sectorsize = 512;
176 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
177 sectorsize > bufsize)
178 bufsize = sectorsize;
182 u_int32_t blockSize = 512;
183 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
190 * offset and count are in bytes, but must be multiples of 512 for files
191 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
193 * This function may be called without alignment if the caller ensures
194 * that O_DIRECT is not in effect.
196 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
197 uint8_t *buf
, int count
)
199 BDRVRawState
*s
= bs
->opaque
;
206 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
207 ++(s
->lseek_err_cnt
);
208 if(s
->lseek_err_cnt
<= 10) {
209 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
210 "] lseek failed : %d = %s\n",
211 s
->fd
, bs
->filename
, offset
, buf
, count
,
212 bs
->total_sectors
, errno
, strerror(errno
));
218 ret
= read(s
->fd
, buf
, count
);
220 goto label__raw_read__success
;
222 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
223 "] read failed %d : %d = %s\n",
224 s
->fd
, bs
->filename
, offset
, buf
, count
,
225 bs
->total_sectors
, ret
, errno
, strerror(errno
));
227 /* Try harder for CDrom. */
228 if (bs
->type
== BDRV_TYPE_CDROM
) {
229 lseek(s
->fd
, offset
, SEEK_SET
);
230 ret
= read(s
->fd
, buf
, count
);
232 goto label__raw_read__success
;
233 lseek(s
->fd
, offset
, SEEK_SET
);
234 ret
= read(s
->fd
, buf
, count
);
236 goto label__raw_read__success
;
238 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
239 "] retry read failed %d : %d = %s\n",
240 s
->fd
, bs
->filename
, offset
, buf
, count
,
241 bs
->total_sectors
, ret
, errno
, strerror(errno
));
244 label__raw_read__success
:
250 * offset and count are in bytes, but must be multiples of 512 for files
251 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
253 * This function may be called without alignment if the caller ensures
254 * that O_DIRECT is not in effect.
256 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
257 const uint8_t *buf
, int count
)
259 BDRVRawState
*s
= bs
->opaque
;
266 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
267 ++(s
->lseek_err_cnt
);
268 if(s
->lseek_err_cnt
) {
269 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
270 PRId64
"] lseek failed : %d = %s\n",
271 s
->fd
, bs
->filename
, offset
, buf
, count
,
272 bs
->total_sectors
, errno
, strerror(errno
));
276 s
->lseek_err_cnt
= 0;
278 ret
= write(s
->fd
, buf
, count
);
280 goto label__raw_write__success
;
282 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
283 "] write failed %d : %d = %s\n",
284 s
->fd
, bs
->filename
, offset
, buf
, count
,
285 bs
->total_sectors
, ret
, errno
, strerror(errno
));
287 label__raw_write__success
:
294 * offset and count are in bytes and possibly not aligned. For files opened
295 * with O_DIRECT, necessary alignments are ensured before calling
296 * raw_pread_aligned to do the actual read.
298 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
299 uint8_t *buf
, int count
)
301 BDRVRawState
*s
= bs
->opaque
;
302 int size
, ret
, shift
, sum
;
306 if (s
->aligned_buf
!= NULL
) {
308 if (offset
& 0x1ff) {
309 /* align offset on a 512 bytes boundary */
311 shift
= offset
& 0x1ff;
312 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
313 if (size
> ALIGNED_BUFFER_SIZE
)
314 size
= ALIGNED_BUFFER_SIZE
;
315 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
322 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
332 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
334 /* read on aligned buffer */
338 size
= (count
+ 0x1ff) & ~0x1ff;
339 if (size
> ALIGNED_BUFFER_SIZE
)
340 size
= ALIGNED_BUFFER_SIZE
;
342 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
350 memcpy(buf
, s
->aligned_buf
, size
);
362 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
366 * offset and count are in bytes and possibly not aligned. For files opened
367 * with O_DIRECT, necessary alignments are ensured before calling
368 * raw_pwrite_aligned to do the actual write.
370 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
371 const uint8_t *buf
, int count
)
373 BDRVRawState
*s
= bs
->opaque
;
374 int size
, ret
, shift
, sum
;
378 if (s
->aligned_buf
!= NULL
) {
380 if (offset
& 0x1ff) {
381 /* align offset on a 512 bytes boundary */
382 shift
= offset
& 0x1ff;
383 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
390 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
392 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
404 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
406 while ((size
= (count
& ~0x1ff)) != 0) {
408 if (size
> ALIGNED_BUFFER_SIZE
)
409 size
= ALIGNED_BUFFER_SIZE
;
411 memcpy(s
->aligned_buf
, buf
, size
);
413 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
422 /* here, count < 512 because (count & ~0x1ff) == 0 */
424 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
427 memcpy(s
->aligned_buf
, buf
, count
);
429 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
440 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
444 /***********************************************************/
445 /* Unix AIO using POSIX AIO */
447 typedef struct RawAIOCB
{
448 BlockDriverAIOCB common
;
451 struct RawAIOCB
*next
;
455 typedef struct PosixAioState
461 static int raw_fd_pool_get(BDRVRawState
*s
)
465 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
467 if (s
->fd_pool
[i
] != -1)
470 /* try to dup file descriptor */
471 s
->fd_pool
[i
] = dup(s
->fd
);
472 if (s
->fd_pool
[i
] != -1)
473 return s
->fd_pool
[i
];
476 /* we couldn't dup the file descriptor so just use the main one */
480 static void raw_fd_pool_put(RawAIOCB
*acb
)
482 BDRVRawState
*s
= acb
->common
.bs
->opaque
;
485 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
486 if (s
->fd_pool
[i
] == acb
->fd
) {
487 close(s
->fd_pool
[i
]);
493 static void posix_aio_read(void *opaque
)
495 PosixAioState
*s
= opaque
;
496 RawAIOCB
*acb
, **pacb
;
503 len
= read(s
->rfd
, &byte
, 1);
504 if (len
== -1 && errno
== EINTR
)
506 if (len
== -1 && errno
== EAGAIN
)
511 pacb
= &s
->first_aio
;
516 ret
= aio_error(&acb
->aiocb
);
517 if (ret
== ECANCELED
) {
518 /* remove the request */
520 raw_fd_pool_put(acb
);
521 qemu_aio_release(acb
);
522 } else if (ret
!= EINPROGRESS
) {
525 ret
= aio_return(&acb
->aiocb
);
526 if (ret
== acb
->aiocb
.aio_nbytes
)
533 /* remove the request */
535 /* call the callback */
536 acb
->common
.cb(acb
->common
.opaque
, ret
);
537 raw_fd_pool_put(acb
);
538 qemu_aio_release(acb
);
548 static int posix_aio_flush(void *opaque
)
550 PosixAioState
*s
= opaque
;
551 return !!s
->first_aio
;
554 static PosixAioState
*posix_aio_state
;
556 static void aio_signal_handler(int signum
)
558 if (posix_aio_state
) {
561 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
567 static int posix_aio_init(void)
569 struct sigaction act
;
576 s
= qemu_malloc(sizeof(PosixAioState
));
580 sigfillset(&act
.sa_mask
);
581 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
582 act
.sa_handler
= aio_signal_handler
;
583 sigaction(SIGUSR2
, &act
, NULL
);
586 if (pipe(fds
) == -1) {
587 fprintf(stderr
, "failed to create pipe\n");
594 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
596 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
598 #if defined(__linux__)
602 memset(&ai
, 0, sizeof(ai
));
603 #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 4)
607 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
608 seems to fix the problem. */
611 ai
.aio_idle_time
= 365 * 100000;
621 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
622 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
623 BlockDriverCompletionFunc
*cb
, void *opaque
)
625 BDRVRawState
*s
= bs
->opaque
;
631 acb
= qemu_aio_get(bs
, cb
, opaque
);
634 acb
->fd
= raw_fd_pool_get(s
);
635 acb
->aiocb
.aio_fildes
= acb
->fd
;
636 acb
->aiocb
.aio_sigevent
.sigev_signo
= SIGUSR2
;
637 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
638 acb
->aiocb
.aio_buf
= buf
;
640 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
642 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
643 acb
->aiocb
.aio_offset
= sector_num
* 512;
644 acb
->next
= posix_aio_state
->first_aio
;
645 posix_aio_state
->first_aio
= acb
;
649 static void raw_aio_em_cb(void* opaque
)
651 RawAIOCB
*acb
= opaque
;
652 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
653 qemu_aio_release(acb
);
656 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
657 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
658 BlockDriverCompletionFunc
*cb
, void *opaque
)
663 * If O_DIRECT is used and the buffer is not aligned fall back
666 BDRVRawState
*s
= bs
->opaque
;
668 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
670 acb
= qemu_aio_get(bs
, cb
, opaque
);
671 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
672 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
673 qemu_bh_schedule(bh
);
677 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
680 if (aio_read(&acb
->aiocb
) < 0) {
681 qemu_aio_release(acb
);
687 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
688 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
689 BlockDriverCompletionFunc
*cb
, void *opaque
)
694 * If O_DIRECT is used and the buffer is not aligned fall back
697 BDRVRawState
*s
= bs
->opaque
;
699 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
701 acb
= qemu_aio_get(bs
, cb
, opaque
);
702 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
703 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
704 qemu_bh_schedule(bh
);
708 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
711 if (aio_write(&acb
->aiocb
) < 0) {
712 qemu_aio_release(acb
);
718 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
721 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
724 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
725 if (ret
== AIO_NOTCANCELED
) {
726 /* fail safe: if the aio could not be canceled, we wait for
728 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
731 /* remove the callback from the queue */
732 pacb
= &posix_aio_state
->first_aio
;
736 } else if (*pacb
== acb
) {
738 raw_fd_pool_put(acb
);
739 qemu_aio_release(acb
);
746 #else /* CONFIG_AIO */
747 static int posix_aio_init(void)
751 #endif /* CONFIG_AIO */
753 static void raw_close_fd_pool(BDRVRawState
*s
)
757 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
758 if (s
->fd_pool
[i
] != -1) {
759 close(s
->fd_pool
[i
]);
765 static void raw_close(BlockDriverState
*bs
)
767 BDRVRawState
*s
= bs
->opaque
;
771 if (s
->aligned_buf
!= NULL
)
772 qemu_free(s
->aligned_buf
);
774 raw_close_fd_pool(s
);
777 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
779 BDRVRawState
*s
= bs
->opaque
;
780 if (s
->type
!= FTYPE_FILE
)
782 if (ftruncate(s
->fd
, offset
) < 0)
788 static int64_t raw_getlength(BlockDriverState
*bs
)
790 BDRVRawState
*s
= bs
->opaque
;
796 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
799 if (ioctl(fd
, DIOCGDINFO
, &dl
))
801 return (uint64_t)dl
.d_secsize
*
802 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
806 #else /* !__OpenBSD__ */
807 static int64_t raw_getlength(BlockDriverState
*bs
)
809 BDRVRawState
*s
= bs
->opaque
;
816 struct dk_minfo minfo
;
826 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
827 #ifdef DIOCGMEDIASIZE
828 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
831 size
= LONG_LONG_MAX
;
833 size
= lseek(fd
, 0LL, SEEK_END
);
839 * use the DKIOCGMEDIAINFO ioctl to read the size.
841 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
843 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
844 } else /* there are reports that lseek on some devices
845 fails, but irc discussion said that contingency
846 on contingency was overkill */
849 size
= lseek(fd
, 0, SEEK_END
);
855 static int raw_create(const char *filename
, int64_t total_size
,
856 const char *backing_file
, int flags
)
860 if (flags
|| backing_file
)
863 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
867 ftruncate(fd
, total_size
* 512);
872 static void raw_flush(BlockDriverState
*bs
)
874 BDRVRawState
*s
= bs
->opaque
;
878 BlockDriver bdrv_raw
= {
880 sizeof(BDRVRawState
),
881 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
),
895 .bdrv_pread
= raw_pread
,
896 .bdrv_pwrite
= raw_pwrite
,
897 .bdrv_truncate
= raw_truncate
,
898 .bdrv_getlength
= raw_getlength
,
901 /***********************************************/
905 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
906 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
908 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
910 kern_return_t kernResult
;
911 mach_port_t masterPort
;
912 CFMutableDictionaryRef classesToMatch
;
914 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
915 if ( KERN_SUCCESS
!= kernResult
) {
916 printf( "IOMasterPort returned %d\n", kernResult
);
919 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
920 if ( classesToMatch
== NULL
) {
921 printf( "IOServiceMatching returned a NULL dictionary.\n" );
923 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
925 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
926 if ( KERN_SUCCESS
!= kernResult
)
928 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
934 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
936 io_object_t nextMedia
;
937 kern_return_t kernResult
= KERN_FAILURE
;
939 nextMedia
= IOIteratorNext( mediaIterator
);
942 CFTypeRef bsdPathAsCFString
;
943 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
944 if ( bsdPathAsCFString
) {
945 size_t devPathLength
;
946 strcpy( bsdPath
, _PATH_DEV
);
947 strcat( bsdPath
, "r" );
948 devPathLength
= strlen( bsdPath
);
949 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
950 kernResult
= KERN_SUCCESS
;
952 CFRelease( bsdPathAsCFString
);
954 IOObjectRelease( nextMedia
);
962 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
964 BDRVRawState
*s
= bs
->opaque
;
965 int fd
, open_flags
, ret
, i
;
970 if (strstart(filename
, "/dev/cdrom", NULL
)) {
971 kern_return_t kernResult
;
972 io_iterator_t mediaIterator
;
973 char bsdPath
[ MAXPATHLEN
];
976 kernResult
= FindEjectableCDMedia( &mediaIterator
);
977 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
979 if ( bsdPath
[ 0 ] != '\0' ) {
980 strcat(bsdPath
,"s0");
981 /* some CDs don't have a partition 0 */
982 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
984 bsdPath
[strlen(bsdPath
)-1] = '1';
992 IOObjectRelease( mediaIterator
);
995 open_flags
= O_BINARY
;
996 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
997 open_flags
|= O_RDWR
;
999 open_flags
|= O_RDONLY
;
1002 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
1003 * and O_DIRECT for no caching. */
1004 if ((flags
& BDRV_O_NOCACHE
))
1005 open_flags
|= O_DIRECT
;
1006 else if (!(flags
& BDRV_O_CACHE_WB
))
1007 open_flags
|= O_DSYNC
;
1009 s
->type
= FTYPE_FILE
;
1010 #if defined(__linux__)
1011 if (strstart(filename
, "/dev/cd", NULL
)) {
1012 /* open will not fail even if no CD is inserted */
1013 open_flags
|= O_NONBLOCK
;
1015 } else if (strstart(filename
, "/dev/fd", NULL
)) {
1017 s
->fd_open_flags
= open_flags
;
1018 /* open will not fail even if no floppy is inserted */
1019 open_flags
|= O_NONBLOCK
;
1020 } else if (strstart(filename
, "/dev/sg", NULL
)) {
1024 fd
= open(filename
, open_flags
, 0644);
1032 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++)
1034 #if defined(__linux__)
1035 /* close fd so that we can reopen it as needed */
1036 if (s
->type
== FTYPE_FD
) {
1039 s
->fd_media_changed
= 1;
1045 #if defined(__linux__)
1046 /* Note: we do not have a reliable method to detect if the floppy is
1047 present. The current method is to try to open the floppy at every
1048 I/O and to keep it opened during a few hundreds of ms. */
1049 static int fd_open(BlockDriverState
*bs
)
1051 BDRVRawState
*s
= bs
->opaque
;
1052 int last_media_present
;
1054 if (s
->type
!= FTYPE_FD
)
1056 last_media_present
= (s
->fd
>= 0);
1058 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1061 raw_close_fd_pool(s
);
1063 printf("Floppy closed\n");
1067 if (s
->fd_got_error
&&
1068 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1070 printf("No floppy (open delayed)\n");
1074 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1076 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1077 s
->fd_got_error
= 1;
1078 if (last_media_present
)
1079 s
->fd_media_changed
= 1;
1081 printf("No floppy\n");
1086 printf("Floppy opened\n");
1089 if (!last_media_present
)
1090 s
->fd_media_changed
= 1;
1091 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1092 s
->fd_got_error
= 0;
1096 static int raw_is_inserted(BlockDriverState
*bs
)
1098 BDRVRawState
*s
= bs
->opaque
;
1103 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1104 if (ret
== CDS_DISC_OK
)
1117 /* currently only used by fdc.c, but a CD version would be good too */
1118 static int raw_media_changed(BlockDriverState
*bs
)
1120 BDRVRawState
*s
= bs
->opaque
;
1126 /* XXX: we do not have a true media changed indication. It
1127 does not work if the floppy is changed without trying
1130 ret
= s
->fd_media_changed
;
1131 s
->fd_media_changed
= 0;
1133 printf("Floppy changed=%d\n", ret
);
1142 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1144 BDRVRawState
*s
= bs
->opaque
;
1149 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1150 perror("CDROMEJECT");
1152 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1153 perror("CDROMEJECT");
1162 raw_close_fd_pool(s
);
1164 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1166 if (ioctl(fd
, FDEJECT
, 0) < 0)
1178 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1180 BDRVRawState
*s
= bs
->opaque
;
1184 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1185 /* Note: an error can happen if the distribution automatically
1186 mounts the CD-ROM */
1187 // perror("CDROM_LOCKDOOR");
1196 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1198 BDRVRawState
*s
= bs
->opaque
;
1200 return ioctl(s
->fd
, req
, buf
);
1204 static int fd_open(BlockDriverState
*bs
)
1209 static int raw_is_inserted(BlockDriverState
*bs
)
1214 static int raw_media_changed(BlockDriverState
*bs
)
1219 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1224 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1229 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1235 BlockDriver bdrv_host_device
= {
1237 sizeof(BDRVRawState
),
1238 NULL
, /* no probe for protocols */
1247 .bdrv_aio_read
= raw_aio_read
,
1248 .bdrv_aio_write
= raw_aio_write
,
1249 .bdrv_aio_cancel
= raw_aio_cancel
,
1250 .aiocb_size
= sizeof(RawAIOCB
),
1252 .bdrv_pread
= raw_pread
,
1253 .bdrv_pwrite
= raw_pwrite
,
1254 .bdrv_getlength
= raw_getlength
,
1256 /* removable device support */
1257 .bdrv_is_inserted
= raw_is_inserted
,
1258 .bdrv_media_changed
= raw_media_changed
,
1259 .bdrv_eject
= raw_eject
,
1260 .bdrv_set_locked
= raw_set_locked
,
1261 /* generic scsi device */
1262 .bdrv_ioctl
= raw_ioctl
,