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...)
80 #define ALIGNED_BUFFER_SIZE (32 * 512)
82 /* if the FD is not accessed during that time (in ms), we try to
83 reopen it to see if the disk has been changed */
84 #define FD_OPEN_TIMEOUT 1000
86 /* posix-aio doesn't allow multiple outstanding requests to a single file
87 * descriptor. we implement a pool of dup()'d file descriptors to work
89 #define RAW_FD_POOL_SIZE 64
91 typedef struct BDRVRawState
{
94 unsigned int lseek_err_cnt
;
95 int fd_pool
[RAW_FD_POOL_SIZE
];
96 #if defined(__linux__)
97 /* linux floppy specific */
100 int64_t fd_error_time
;
102 int fd_media_changed
;
104 #if defined(O_DIRECT)
105 uint8_t* aligned_buf
;
109 static int posix_aio_init(void);
111 static int fd_open(BlockDriverState
*bs
);
113 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
115 BDRVRawState
*s
= bs
->opaque
;
116 int fd
, open_flags
, ret
;
121 s
->lseek_err_cnt
= 0;
123 open_flags
= O_BINARY
;
124 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
125 open_flags
|= O_RDWR
;
127 open_flags
|= O_RDONLY
;
130 if (flags
& BDRV_O_CREAT
)
131 open_flags
|= O_CREAT
| O_TRUNC
;
133 if (flags
& BDRV_O_DIRECT
)
134 open_flags
|= O_DIRECT
;
137 s
->type
= FTYPE_FILE
;
139 fd
= open(filename
, open_flags
, 0644);
147 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++)
149 #if defined(O_DIRECT)
150 s
->aligned_buf
= NULL
;
151 if (flags
& BDRV_O_DIRECT
) {
152 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
153 if (s
->aligned_buf
== NULL
) {
163 /* XXX: use host sector size if necessary with:
164 #ifdef DIOCGSECTORSIZE
166 unsigned int sectorsize = 512;
167 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
168 sectorsize > bufsize)
169 bufsize = sectorsize;
173 u_int32_t blockSize = 512;
174 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
181 * offset and count are in bytes, but must be multiples of 512 for files
182 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
184 * This function may be called without alignment if the caller ensures
185 * that O_DIRECT is not in effect.
187 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
188 uint8_t *buf
, int count
)
190 BDRVRawState
*s
= bs
->opaque
;
197 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
198 ++(s
->lseek_err_cnt
);
199 if(s
->lseek_err_cnt
<= 10) {
200 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
201 "] lseek failed : %d = %s\n",
202 s
->fd
, bs
->filename
, offset
, buf
, count
,
203 bs
->total_sectors
, errno
, strerror(errno
));
209 ret
= read(s
->fd
, buf
, count
);
211 goto label__raw_read__success
;
213 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
214 "] read failed %d : %d = %s\n",
215 s
->fd
, bs
->filename
, offset
, buf
, count
,
216 bs
->total_sectors
, ret
, errno
, strerror(errno
));
218 /* Try harder for CDrom. */
219 if (bs
->type
== BDRV_TYPE_CDROM
) {
220 lseek(s
->fd
, offset
, SEEK_SET
);
221 ret
= read(s
->fd
, buf
, count
);
223 goto label__raw_read__success
;
224 lseek(s
->fd
, offset
, SEEK_SET
);
225 ret
= read(s
->fd
, buf
, count
);
227 goto label__raw_read__success
;
229 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
230 "] retry read failed %d : %d = %s\n",
231 s
->fd
, bs
->filename
, offset
, buf
, count
,
232 bs
->total_sectors
, ret
, errno
, strerror(errno
));
235 label__raw_read__success
:
241 * offset and count are in bytes, but must be multiples of 512 for files
242 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
244 * This function may be called without alignment if the caller ensures
245 * that O_DIRECT is not in effect.
247 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
248 const uint8_t *buf
, int count
)
250 BDRVRawState
*s
= bs
->opaque
;
257 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
258 ++(s
->lseek_err_cnt
);
259 if(s
->lseek_err_cnt
) {
260 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
261 PRId64
"] lseek failed : %d = %s\n",
262 s
->fd
, bs
->filename
, offset
, buf
, count
,
263 bs
->total_sectors
, errno
, strerror(errno
));
267 s
->lseek_err_cnt
= 0;
269 ret
= write(s
->fd
, buf
, count
);
271 goto label__raw_write__success
;
273 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
274 "] write failed %d : %d = %s\n",
275 s
->fd
, bs
->filename
, offset
, buf
, count
,
276 bs
->total_sectors
, ret
, errno
, strerror(errno
));
278 label__raw_write__success
:
284 #if defined(O_DIRECT)
286 * offset and count are in bytes and possibly not aligned. For files opened
287 * with O_DIRECT, necessary alignments are ensured before calling
288 * raw_pread_aligned to do the actual read.
290 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
291 uint8_t *buf
, int count
)
293 BDRVRawState
*s
= bs
->opaque
;
294 int size
, ret
, shift
, sum
;
298 if (s
->aligned_buf
!= NULL
) {
300 if (offset
& 0x1ff) {
301 /* align offset on a 512 bytes boundary */
303 shift
= offset
& 0x1ff;
304 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
305 if (size
> ALIGNED_BUFFER_SIZE
)
306 size
= ALIGNED_BUFFER_SIZE
;
307 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
314 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
324 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
326 /* read on aligned buffer */
330 size
= (count
+ 0x1ff) & ~0x1ff;
331 if (size
> ALIGNED_BUFFER_SIZE
)
332 size
= ALIGNED_BUFFER_SIZE
;
334 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
342 memcpy(buf
, s
->aligned_buf
, size
);
354 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
358 * offset and count are in bytes and possibly not aligned. For files opened
359 * with O_DIRECT, necessary alignments are ensured before calling
360 * raw_pwrite_aligned to do the actual write.
362 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
363 const uint8_t *buf
, int count
)
365 BDRVRawState
*s
= bs
->opaque
;
366 int size
, ret
, shift
, sum
;
370 if (s
->aligned_buf
!= NULL
) {
372 if (offset
& 0x1ff) {
373 /* align offset on a 512 bytes boundary */
374 shift
= offset
& 0x1ff;
375 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
382 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
384 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
396 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
398 while ((size
= (count
& ~0x1ff)) != 0) {
400 if (size
> ALIGNED_BUFFER_SIZE
)
401 size
= ALIGNED_BUFFER_SIZE
;
403 memcpy(s
->aligned_buf
, buf
, size
);
405 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
414 /* here, count < 512 because (count & ~0x1ff) == 0 */
416 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
419 memcpy(s
->aligned_buf
, buf
, count
);
421 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
432 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
436 #define raw_pread raw_pread_aligned
437 #define raw_pwrite raw_pwrite_aligned
442 /***********************************************************/
443 /* Unix AIO using POSIX AIO */
445 typedef struct RawAIOCB
{
446 BlockDriverAIOCB common
;
449 struct RawAIOCB
*next
;
453 typedef struct PosixAioState
459 static int raw_fd_pool_get(BDRVRawState
*s
)
463 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
465 if (s
->fd_pool
[i
] != -1)
468 /* try to dup file descriptor */
469 s
->fd_pool
[i
] = dup(s
->fd
);
470 if (s
->fd_pool
[i
] != -1)
471 return s
->fd_pool
[i
];
474 /* we couldn't dup the file descriptor so just use the main one */
478 static void raw_fd_pool_put(RawAIOCB
*acb
)
480 BDRVRawState
*s
= acb
->common
.bs
->opaque
;
483 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
484 if (s
->fd_pool
[i
] == acb
->fd
) {
485 close(s
->fd_pool
[i
]);
491 static void posix_aio_read(void *opaque
)
493 PosixAioState
*s
= opaque
;
494 RawAIOCB
*acb
, **pacb
;
501 len
= read(s
->rfd
, &byte
, 1);
502 if (len
== -1 && errno
== EINTR
)
504 if (len
== -1 && errno
== EAGAIN
)
509 pacb
= &s
->first_aio
;
514 ret
= aio_error(&acb
->aiocb
);
515 if (ret
== ECANCELED
) {
516 /* remove the request */
518 raw_fd_pool_put(acb
);
519 qemu_aio_release(acb
);
520 } else if (ret
!= EINPROGRESS
) {
523 ret
= aio_return(&acb
->aiocb
);
524 if (ret
== acb
->aiocb
.aio_nbytes
)
531 /* remove the request */
533 /* call the callback */
534 acb
->common
.cb(acb
->common
.opaque
, ret
);
535 raw_fd_pool_put(acb
);
536 qemu_aio_release(acb
);
546 static int posix_aio_flush(void *opaque
)
548 PosixAioState
*s
= opaque
;
549 return !!s
->first_aio
;
552 static PosixAioState
*posix_aio_state
;
554 static void aio_signal_handler(int signum
)
556 if (posix_aio_state
) {
559 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
565 static int posix_aio_init(void)
567 struct sigaction act
;
574 s
= qemu_malloc(sizeof(PosixAioState
));
578 sigfillset(&act
.sa_mask
);
579 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
580 act
.sa_handler
= aio_signal_handler
;
581 sigaction(SIGUSR2
, &act
, NULL
);
584 if (pipe(fds
) == -1) {
585 fprintf(stderr
, "failed to create pipe\n");
592 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
594 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
596 #if defined(__linux__)
600 memset(&ai
, 0, sizeof(ai
));
601 #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 4)
605 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
606 seems to fix the problem. */
609 ai
.aio_idle_time
= 365 * 100000;
619 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
620 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
621 BlockDriverCompletionFunc
*cb
, void *opaque
)
623 BDRVRawState
*s
= bs
->opaque
;
629 acb
= qemu_aio_get(bs
, cb
, opaque
);
632 acb
->fd
= raw_fd_pool_get(s
);
633 acb
->aiocb
.aio_fildes
= acb
->fd
;
634 acb
->aiocb
.aio_sigevent
.sigev_signo
= SIGUSR2
;
635 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
636 acb
->aiocb
.aio_buf
= buf
;
638 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
640 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
641 acb
->aiocb
.aio_offset
= sector_num
* 512;
642 acb
->next
= posix_aio_state
->first_aio
;
643 posix_aio_state
->first_aio
= acb
;
647 static void raw_aio_em_cb(void* opaque
)
649 RawAIOCB
*acb
= opaque
;
650 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
651 qemu_aio_release(acb
);
654 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
655 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
656 BlockDriverCompletionFunc
*cb
, void *opaque
)
661 * If O_DIRECT is used and the buffer is not aligned fall back
664 #if defined(O_DIRECT)
665 BDRVRawState
*s
= bs
->opaque
;
667 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
669 acb
= qemu_aio_get(bs
, cb
, opaque
);
670 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
671 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
672 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 #if defined(O_DIRECT)
698 BDRVRawState
*s
= bs
->opaque
;
700 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
702 acb
= qemu_aio_get(bs
, cb
, opaque
);
703 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
704 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
705 qemu_bh_schedule(bh
);
710 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
713 if (aio_write(&acb
->aiocb
) < 0) {
714 qemu_aio_release(acb
);
720 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
723 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
726 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
727 if (ret
== AIO_NOTCANCELED
) {
728 /* fail safe: if the aio could not be canceled, we wait for
730 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
733 /* remove the callback from the queue */
734 pacb
= &posix_aio_state
->first_aio
;
738 } else if (*pacb
== acb
) {
740 raw_fd_pool_put(acb
);
741 qemu_aio_release(acb
);
748 #else /* CONFIG_AIO */
749 static int posix_aio_init(void)
753 #endif /* CONFIG_AIO */
755 static void raw_close_fd_pool(BDRVRawState
*s
)
759 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
760 if (s
->fd_pool
[i
] != -1) {
761 close(s
->fd_pool
[i
]);
767 static void raw_close(BlockDriverState
*bs
)
769 BDRVRawState
*s
= bs
->opaque
;
773 #if defined(O_DIRECT)
774 if (s
->aligned_buf
!= NULL
)
775 qemu_free(s
->aligned_buf
);
778 raw_close_fd_pool(s
);
781 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
783 BDRVRawState
*s
= bs
->opaque
;
784 if (s
->type
!= FTYPE_FILE
)
786 if (ftruncate(s
->fd
, offset
) < 0)
792 static int64_t raw_getlength(BlockDriverState
*bs
)
794 BDRVRawState
*s
= bs
->opaque
;
800 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
803 if (ioctl(fd
, DIOCGDINFO
, &dl
))
805 return (uint64_t)dl
.d_secsize
*
806 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
810 #else /* !__OpenBSD__ */
811 static int64_t raw_getlength(BlockDriverState
*bs
)
813 BDRVRawState
*s
= bs
->opaque
;
820 struct dk_minfo minfo
;
830 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
831 #ifdef DIOCGMEDIASIZE
832 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
835 size
= LONG_LONG_MAX
;
837 size
= lseek(fd
, 0LL, SEEK_END
);
843 * use the DKIOCGMEDIAINFO ioctl to read the size.
845 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
847 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
848 } else /* there are reports that lseek on some devices
849 fails, but irc discussion said that contingency
850 on contingency was overkill */
853 size
= lseek(fd
, 0, SEEK_END
);
859 static int raw_create(const char *filename
, int64_t total_size
,
860 const char *backing_file
, int flags
)
864 if (flags
|| backing_file
)
867 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
871 ftruncate(fd
, total_size
* 512);
876 static void raw_flush(BlockDriverState
*bs
)
878 BDRVRawState
*s
= bs
->opaque
;
882 BlockDriver bdrv_raw
= {
884 sizeof(BDRVRawState
),
885 NULL
, /* no probe for protocols */
894 .bdrv_aio_read
= raw_aio_read
,
895 .bdrv_aio_write
= raw_aio_write
,
896 .bdrv_aio_cancel
= raw_aio_cancel
,
897 .aiocb_size
= sizeof(RawAIOCB
),
899 .bdrv_pread
= raw_pread
,
900 .bdrv_pwrite
= raw_pwrite
,
901 .bdrv_truncate
= raw_truncate
,
902 .bdrv_getlength
= raw_getlength
,
905 /***********************************************/
909 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
910 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
912 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
914 kern_return_t kernResult
;
915 mach_port_t masterPort
;
916 CFMutableDictionaryRef classesToMatch
;
918 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
919 if ( KERN_SUCCESS
!= kernResult
) {
920 printf( "IOMasterPort returned %d\n", kernResult
);
923 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
924 if ( classesToMatch
== NULL
) {
925 printf( "IOServiceMatching returned a NULL dictionary.\n" );
927 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
929 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
930 if ( KERN_SUCCESS
!= kernResult
)
932 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
938 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
940 io_object_t nextMedia
;
941 kern_return_t kernResult
= KERN_FAILURE
;
943 nextMedia
= IOIteratorNext( mediaIterator
);
946 CFTypeRef bsdPathAsCFString
;
947 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
948 if ( bsdPathAsCFString
) {
949 size_t devPathLength
;
950 strcpy( bsdPath
, _PATH_DEV
);
951 strcat( bsdPath
, "r" );
952 devPathLength
= strlen( bsdPath
);
953 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
954 kernResult
= KERN_SUCCESS
;
956 CFRelease( bsdPathAsCFString
);
958 IOObjectRelease( nextMedia
);
966 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
968 BDRVRawState
*s
= bs
->opaque
;
969 int fd
, open_flags
, ret
, i
;
974 if (strstart(filename
, "/dev/cdrom", NULL
)) {
975 kern_return_t kernResult
;
976 io_iterator_t mediaIterator
;
977 char bsdPath
[ MAXPATHLEN
];
980 kernResult
= FindEjectableCDMedia( &mediaIterator
);
981 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
983 if ( bsdPath
[ 0 ] != '\0' ) {
984 strcat(bsdPath
,"s0");
985 /* some CDs don't have a partition 0 */
986 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
988 bsdPath
[strlen(bsdPath
)-1] = '1';
996 IOObjectRelease( mediaIterator
);
999 open_flags
= O_BINARY
;
1000 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
1001 open_flags
|= O_RDWR
;
1003 open_flags
|= O_RDONLY
;
1007 if (flags
& BDRV_O_DIRECT
)
1008 open_flags
|= O_DIRECT
;
1011 s
->type
= FTYPE_FILE
;
1012 #if defined(__linux__)
1013 if (strstart(filename
, "/dev/cd", NULL
)) {
1014 /* open will not fail even if no CD is inserted */
1015 open_flags
|= O_NONBLOCK
;
1017 } else if (strstart(filename
, "/dev/fd", NULL
)) {
1019 s
->fd_open_flags
= open_flags
;
1020 /* open will not fail even if no floppy is inserted */
1021 open_flags
|= O_NONBLOCK
;
1022 } else if (strstart(filename
, "/dev/sg", NULL
)) {
1026 fd
= open(filename
, open_flags
, 0644);
1034 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++)
1036 #if defined(__linux__)
1037 /* close fd so that we can reopen it as needed */
1038 if (s
->type
== FTYPE_FD
) {
1041 s
->fd_media_changed
= 1;
1047 #if defined(__linux__)
1048 /* Note: we do not have a reliable method to detect if the floppy is
1049 present. The current method is to try to open the floppy at every
1050 I/O and to keep it opened during a few hundreds of ms. */
1051 static int fd_open(BlockDriverState
*bs
)
1053 BDRVRawState
*s
= bs
->opaque
;
1054 int last_media_present
;
1056 if (s
->type
!= FTYPE_FD
)
1058 last_media_present
= (s
->fd
>= 0);
1060 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1063 raw_close_fd_pool(s
);
1065 printf("Floppy closed\n");
1069 if (s
->fd_got_error
&&
1070 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1072 printf("No floppy (open delayed)\n");
1076 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1078 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1079 s
->fd_got_error
= 1;
1080 if (last_media_present
)
1081 s
->fd_media_changed
= 1;
1083 printf("No floppy\n");
1088 printf("Floppy opened\n");
1091 if (!last_media_present
)
1092 s
->fd_media_changed
= 1;
1093 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1094 s
->fd_got_error
= 0;
1098 static int raw_is_inserted(BlockDriverState
*bs
)
1100 BDRVRawState
*s
= bs
->opaque
;
1105 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1106 if (ret
== CDS_DISC_OK
)
1119 /* currently only used by fdc.c, but a CD version would be good too */
1120 static int raw_media_changed(BlockDriverState
*bs
)
1122 BDRVRawState
*s
= bs
->opaque
;
1128 /* XXX: we do not have a true media changed indication. It
1129 does not work if the floppy is changed without trying
1132 ret
= s
->fd_media_changed
;
1133 s
->fd_media_changed
= 0;
1135 printf("Floppy changed=%d\n", ret
);
1144 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1146 BDRVRawState
*s
= bs
->opaque
;
1151 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1152 perror("CDROMEJECT");
1154 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1155 perror("CDROMEJECT");
1164 raw_close_fd_pool(s
);
1166 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1168 if (ioctl(fd
, FDEJECT
, 0) < 0)
1180 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1182 BDRVRawState
*s
= bs
->opaque
;
1186 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1187 /* Note: an error can happen if the distribution automatically
1188 mounts the CD-ROM */
1189 // perror("CDROM_LOCKDOOR");
1198 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1200 BDRVRawState
*s
= bs
->opaque
;
1202 return ioctl(s
->fd
, req
, buf
);
1206 static int fd_open(BlockDriverState
*bs
)
1211 static int raw_is_inserted(BlockDriverState
*bs
)
1216 static int raw_media_changed(BlockDriverState
*bs
)
1221 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1226 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1231 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1237 BlockDriver bdrv_host_device
= {
1239 sizeof(BDRVRawState
),
1240 NULL
, /* no probe for protocols */
1249 .bdrv_aio_read
= raw_aio_read
,
1250 .bdrv_aio_write
= raw_aio_write
,
1251 .bdrv_aio_cancel
= raw_aio_cancel
,
1252 .aiocb_size
= sizeof(RawAIOCB
),
1254 .bdrv_pread
= raw_pread
,
1255 .bdrv_pwrite
= raw_pwrite
,
1256 .bdrv_getlength
= raw_getlength
,
1258 /* removable device support */
1259 .bdrv_is_inserted
= raw_is_inserted
,
1260 .bdrv_media_changed
= raw_media_changed
,
1261 .bdrv_eject
= raw_eject
,
1262 .bdrv_set_locked
= raw_set_locked
,
1263 /* generic scsi device */
1264 .bdrv_ioctl
= raw_ioctl
,