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"
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>
47 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
67 //#define DEBUG_FLOPPY
70 #if defined(DEBUG_BLOCK)
71 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
72 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
74 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
81 #define ALIGNED_BUFFER_SIZE (32 * 512)
83 /* if the FD is not accessed during that time (in ms), we try to
84 reopen it to see if the disk has been changed */
85 #define FD_OPEN_TIMEOUT 1000
87 /* posix-aio doesn't allow multiple outstanding requests to a single file
88 * descriptor. we implement a pool of dup()'d file descriptors to work
90 #define RAW_FD_POOL_SIZE 64
92 typedef struct BDRVRawState
{
95 unsigned int lseek_err_cnt
;
96 int fd_pool
[RAW_FD_POOL_SIZE
];
97 #if defined(__linux__)
98 /* linux floppy specific */
100 int64_t fd_open_time
;
101 int64_t fd_error_time
;
103 int fd_media_changed
;
105 #if defined(O_DIRECT)
106 uint8_t* aligned_buf
;
110 static int posix_aio_init(void);
112 static int fd_open(BlockDriverState
*bs
);
114 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
116 BDRVRawState
*s
= bs
->opaque
;
117 int fd
, open_flags
, ret
;
122 s
->lseek_err_cnt
= 0;
124 open_flags
= O_BINARY
;
125 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
126 open_flags
|= O_RDWR
;
128 open_flags
|= O_RDONLY
;
131 if (flags
& BDRV_O_CREAT
)
132 open_flags
|= O_CREAT
| O_TRUNC
;
134 if (flags
& BDRV_O_DIRECT
)
135 open_flags
|= O_DIRECT
;
138 s
->type
= FTYPE_FILE
;
140 fd
= open(filename
, open_flags
, 0644);
148 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++)
150 #if defined(O_DIRECT)
151 s
->aligned_buf
= NULL
;
152 if (flags
& BDRV_O_DIRECT
) {
153 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
154 if (s
->aligned_buf
== NULL
) {
164 /* XXX: use host sector size if necessary with:
165 #ifdef DIOCGSECTORSIZE
167 unsigned int sectorsize = 512;
168 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
169 sectorsize > bufsize)
170 bufsize = sectorsize;
174 u_int32_t blockSize = 512;
175 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
182 * offset and count are in bytes, but must be multiples of 512 for files
183 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
185 * This function may be called without alignment if the caller ensures
186 * that O_DIRECT is not in effect.
188 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
189 uint8_t *buf
, int count
)
191 BDRVRawState
*s
= bs
->opaque
;
198 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
199 ++(s
->lseek_err_cnt
);
200 if(s
->lseek_err_cnt
<= 10) {
201 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
202 "] lseek failed : %d = %s\n",
203 s
->fd
, bs
->filename
, offset
, buf
, count
,
204 bs
->total_sectors
, errno
, strerror(errno
));
210 ret
= read(s
->fd
, buf
, count
);
212 goto label__raw_read__success
;
214 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
215 "] read failed %d : %d = %s\n",
216 s
->fd
, bs
->filename
, offset
, buf
, count
,
217 bs
->total_sectors
, ret
, errno
, strerror(errno
));
219 /* Try harder for CDrom. */
220 if (bs
->type
== BDRV_TYPE_CDROM
) {
221 lseek(s
->fd
, offset
, SEEK_SET
);
222 ret
= read(s
->fd
, buf
, count
);
224 goto label__raw_read__success
;
225 lseek(s
->fd
, offset
, SEEK_SET
);
226 ret
= read(s
->fd
, buf
, count
);
228 goto label__raw_read__success
;
230 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
231 "] retry read failed %d : %d = %s\n",
232 s
->fd
, bs
->filename
, offset
, buf
, count
,
233 bs
->total_sectors
, ret
, errno
, strerror(errno
));
236 label__raw_read__success
:
242 * offset and count are in bytes, but must be multiples of 512 for files
243 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
245 * This function may be called without alignment if the caller ensures
246 * that O_DIRECT is not in effect.
248 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
249 const uint8_t *buf
, int count
)
251 BDRVRawState
*s
= bs
->opaque
;
258 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
259 ++(s
->lseek_err_cnt
);
260 if(s
->lseek_err_cnt
) {
261 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
262 PRId64
"] lseek failed : %d = %s\n",
263 s
->fd
, bs
->filename
, offset
, buf
, count
,
264 bs
->total_sectors
, errno
, strerror(errno
));
268 s
->lseek_err_cnt
= 0;
270 ret
= write(s
->fd
, buf
, count
);
272 goto label__raw_write__success
;
274 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
275 "] write failed %d : %d = %s\n",
276 s
->fd
, bs
->filename
, offset
, buf
, count
,
277 bs
->total_sectors
, ret
, errno
, strerror(errno
));
279 label__raw_write__success
:
285 #if defined(O_DIRECT)
287 * offset and count are in bytes and possibly not aligned. For files opened
288 * with O_DIRECT, necessary alignments are ensured before calling
289 * raw_pread_aligned to do the actual read.
291 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
292 uint8_t *buf
, int count
)
294 BDRVRawState
*s
= bs
->opaque
;
295 int size
, ret
, shift
, sum
;
299 if (s
->aligned_buf
!= NULL
) {
301 if (offset
& 0x1ff) {
302 /* align offset on a 512 bytes boundary */
304 shift
= offset
& 0x1ff;
305 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
306 if (size
> ALIGNED_BUFFER_SIZE
)
307 size
= ALIGNED_BUFFER_SIZE
;
308 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
315 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
325 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
327 /* read on aligned buffer */
331 size
= (count
+ 0x1ff) & ~0x1ff;
332 if (size
> ALIGNED_BUFFER_SIZE
)
333 size
= ALIGNED_BUFFER_SIZE
;
335 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
343 memcpy(buf
, s
->aligned_buf
, size
);
355 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
359 * offset and count are in bytes and possibly not aligned. For files opened
360 * with O_DIRECT, necessary alignments are ensured before calling
361 * raw_pwrite_aligned to do the actual write.
363 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
364 const uint8_t *buf
, int count
)
366 BDRVRawState
*s
= bs
->opaque
;
367 int size
, ret
, shift
, sum
;
371 if (s
->aligned_buf
!= NULL
) {
373 if (offset
& 0x1ff) {
374 /* align offset on a 512 bytes boundary */
375 shift
= offset
& 0x1ff;
376 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
383 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
385 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
397 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
399 while ((size
= (count
& ~0x1ff)) != 0) {
401 if (size
> ALIGNED_BUFFER_SIZE
)
402 size
= ALIGNED_BUFFER_SIZE
;
404 memcpy(s
->aligned_buf
, buf
, size
);
406 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
415 /* here, count < 512 because (count & ~0x1ff) == 0 */
417 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
420 memcpy(s
->aligned_buf
, buf
, count
);
422 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
433 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
437 #define raw_pread raw_pread_aligned
438 #define raw_pwrite raw_pwrite_aligned
443 /***********************************************************/
444 /* Unix AIO using POSIX AIO */
446 typedef struct RawAIOCB
{
447 BlockDriverAIOCB common
;
450 struct RawAIOCB
*next
;
454 typedef struct PosixAioState
460 static int raw_fd_pool_get(BDRVRawState
*s
)
464 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
466 if (s
->fd_pool
[i
] != -1)
469 /* try to dup file descriptor */
470 s
->fd_pool
[i
] = dup(s
->fd
);
471 if (s
->fd_pool
[i
] != -1)
472 return s
->fd_pool
[i
];
475 /* we couldn't dup the file descriptor so just use the main one */
479 static void raw_fd_pool_put(RawAIOCB
*acb
)
481 BDRVRawState
*s
= acb
->common
.bs
->opaque
;
484 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
485 if (s
->fd_pool
[i
] == acb
->fd
) {
486 close(s
->fd_pool
[i
]);
492 static void posix_aio_read(void *opaque
)
494 PosixAioState
*s
= opaque
;
495 RawAIOCB
*acb
, **pacb
;
499 struct qemu_signalfd_siginfo siginfo
;
503 /* try to read from signalfd, don't freak out if we can't read anything */
505 while (offset
< 128) {
508 len
= read(s
->fd
, sig
.buf
+ offset
, 128 - offset
);
509 if (len
== -1 && errno
== EINTR
)
511 if (len
== -1 && errno
== EAGAIN
) {
512 /* there is no natural reason for this to happen,
513 * so we'll spin hard until we get everything just
514 * to be on the safe side. */
523 pacb
= &s
->first_aio
;
528 ret
= aio_error(&acb
->aiocb
);
529 if (ret
== ECANCELED
) {
530 /* remove the request */
532 raw_fd_pool_put(acb
);
533 qemu_aio_release(acb
);
534 } else if (ret
!= EINPROGRESS
) {
537 ret
= aio_return(&acb
->aiocb
);
538 if (ret
== acb
->aiocb
.aio_nbytes
)
545 /* remove the request */
547 /* call the callback */
548 acb
->common
.cb(acb
->common
.opaque
, ret
);
549 raw_fd_pool_put(acb
);
550 qemu_aio_release(acb
);
560 static int posix_aio_flush(void *opaque
)
562 PosixAioState
*s
= opaque
;
563 return !!s
->first_aio
;
566 static PosixAioState
*posix_aio_state
;
568 static int posix_aio_init(void)
576 s
= qemu_malloc(sizeof(PosixAioState
));
580 /* Make sure to block AIO signal */
582 sigaddset(&mask
, SIGUSR2
);
583 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
586 s
->fd
= qemu_signalfd(&mask
);
588 fprintf(stderr
, "failed to create signalfd\n");
592 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
594 qemu_aio_set_fd_handler(s
->fd
, 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)
752 #endif /* CONFIG_AIO */
754 static void raw_close_fd_pool(BDRVRawState
*s
)
758 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++) {
759 if (s
->fd_pool
[i
] != -1) {
760 close(s
->fd_pool
[i
]);
766 static void raw_close(BlockDriverState
*bs
)
768 BDRVRawState
*s
= bs
->opaque
;
772 #if defined(O_DIRECT)
773 if (s
->aligned_buf
!= NULL
)
774 qemu_free(s
->aligned_buf
);
777 raw_close_fd_pool(s
);
780 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
782 BDRVRawState
*s
= bs
->opaque
;
783 if (s
->type
!= FTYPE_FILE
)
785 if (ftruncate(s
->fd
, offset
) < 0)
791 static int64_t raw_getlength(BlockDriverState
*bs
)
793 BDRVRawState
*s
= bs
->opaque
;
799 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
802 if (ioctl(fd
, DIOCGDINFO
, &dl
))
804 return (uint64_t)dl
.d_secsize
*
805 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
809 #else /* !__OpenBSD__ */
810 static int64_t raw_getlength(BlockDriverState
*bs
)
812 BDRVRawState
*s
= bs
->opaque
;
819 struct dk_minfo minfo
;
829 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
830 #ifdef DIOCGMEDIASIZE
831 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
834 size
= LONG_LONG_MAX
;
836 size
= lseek(fd
, 0LL, SEEK_END
);
842 * use the DKIOCGMEDIAINFO ioctl to read the size.
844 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
846 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
847 } else /* there are reports that lseek on some devices
848 fails, but irc discussion said that contingency
849 on contingency was overkill */
852 size
= lseek(fd
, 0, SEEK_END
);
858 static int raw_create(const char *filename
, int64_t total_size
,
859 const char *backing_file
, int flags
)
863 if (flags
|| backing_file
)
866 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
870 ftruncate(fd
, total_size
* 512);
875 static void raw_flush(BlockDriverState
*bs
)
877 BDRVRawState
*s
= bs
->opaque
;
881 BlockDriver bdrv_raw
= {
883 sizeof(BDRVRawState
),
884 NULL
, /* no probe for protocols */
893 .bdrv_aio_read
= raw_aio_read
,
894 .bdrv_aio_write
= raw_aio_write
,
895 .bdrv_aio_cancel
= raw_aio_cancel
,
896 .aiocb_size
= sizeof(RawAIOCB
),
898 .bdrv_pread
= raw_pread
,
899 .bdrv_pwrite
= raw_pwrite
,
900 .bdrv_truncate
= raw_truncate
,
901 .bdrv_getlength
= raw_getlength
,
904 /***********************************************/
908 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
909 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
911 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
913 kern_return_t kernResult
;
914 mach_port_t masterPort
;
915 CFMutableDictionaryRef classesToMatch
;
917 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
918 if ( KERN_SUCCESS
!= kernResult
) {
919 printf( "IOMasterPort returned %d\n", kernResult
);
922 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
923 if ( classesToMatch
== NULL
) {
924 printf( "IOServiceMatching returned a NULL dictionary.\n" );
926 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
928 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
929 if ( KERN_SUCCESS
!= kernResult
)
931 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
937 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
939 io_object_t nextMedia
;
940 kern_return_t kernResult
= KERN_FAILURE
;
942 nextMedia
= IOIteratorNext( mediaIterator
);
945 CFTypeRef bsdPathAsCFString
;
946 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
947 if ( bsdPathAsCFString
) {
948 size_t devPathLength
;
949 strcpy( bsdPath
, _PATH_DEV
);
950 strcat( bsdPath
, "r" );
951 devPathLength
= strlen( bsdPath
);
952 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
953 kernResult
= KERN_SUCCESS
;
955 CFRelease( bsdPathAsCFString
);
957 IOObjectRelease( nextMedia
);
965 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
967 BDRVRawState
*s
= bs
->opaque
;
968 int fd
, open_flags
, ret
, i
;
973 if (strstart(filename
, "/dev/cdrom", NULL
)) {
974 kern_return_t kernResult
;
975 io_iterator_t mediaIterator
;
976 char bsdPath
[ MAXPATHLEN
];
979 kernResult
= FindEjectableCDMedia( &mediaIterator
);
980 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
982 if ( bsdPath
[ 0 ] != '\0' ) {
983 strcat(bsdPath
,"s0");
984 /* some CDs don't have a partition 0 */
985 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
987 bsdPath
[strlen(bsdPath
)-1] = '1';
995 IOObjectRelease( mediaIterator
);
998 open_flags
= O_BINARY
;
999 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
1000 open_flags
|= O_RDWR
;
1002 open_flags
|= O_RDONLY
;
1006 if (flags
& BDRV_O_DIRECT
)
1007 open_flags
|= O_DIRECT
;
1010 s
->type
= FTYPE_FILE
;
1011 #if defined(__linux__)
1012 if (strstart(filename
, "/dev/cd", NULL
)) {
1013 /* open will not fail even if no CD is inserted */
1014 open_flags
|= O_NONBLOCK
;
1016 } else if (strstart(filename
, "/dev/fd", NULL
)) {
1018 s
->fd_open_flags
= open_flags
;
1019 /* open will not fail even if no floppy is inserted */
1020 open_flags
|= O_NONBLOCK
;
1021 } else if (strstart(filename
, "/dev/sg", NULL
)) {
1025 fd
= open(filename
, open_flags
, 0644);
1033 for (i
= 0; i
< RAW_FD_POOL_SIZE
; i
++)
1035 #if defined(__linux__)
1036 /* close fd so that we can reopen it as needed */
1037 if (s
->type
== FTYPE_FD
) {
1040 s
->fd_media_changed
= 1;
1046 #if defined(__linux__)
1047 /* Note: we do not have a reliable method to detect if the floppy is
1048 present. The current method is to try to open the floppy at every
1049 I/O and to keep it opened during a few hundreds of ms. */
1050 static int fd_open(BlockDriverState
*bs
)
1052 BDRVRawState
*s
= bs
->opaque
;
1053 int last_media_present
;
1055 if (s
->type
!= FTYPE_FD
)
1057 last_media_present
= (s
->fd
>= 0);
1059 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1062 raw_close_fd_pool(s
);
1064 printf("Floppy closed\n");
1068 if (s
->fd_got_error
&&
1069 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1071 printf("No floppy (open delayed)\n");
1075 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1077 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1078 s
->fd_got_error
= 1;
1079 if (last_media_present
)
1080 s
->fd_media_changed
= 1;
1082 printf("No floppy\n");
1087 printf("Floppy opened\n");
1090 if (!last_media_present
)
1091 s
->fd_media_changed
= 1;
1092 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1093 s
->fd_got_error
= 0;
1097 static int raw_is_inserted(BlockDriverState
*bs
)
1099 BDRVRawState
*s
= bs
->opaque
;
1104 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1105 if (ret
== CDS_DISC_OK
)
1118 /* currently only used by fdc.c, but a CD version would be good too */
1119 static int raw_media_changed(BlockDriverState
*bs
)
1121 BDRVRawState
*s
= bs
->opaque
;
1127 /* XXX: we do not have a true media changed indication. It
1128 does not work if the floppy is changed without trying
1131 ret
= s
->fd_media_changed
;
1132 s
->fd_media_changed
= 0;
1134 printf("Floppy changed=%d\n", ret
);
1143 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1145 BDRVRawState
*s
= bs
->opaque
;
1150 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1151 perror("CDROMEJECT");
1153 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1154 perror("CDROMEJECT");
1163 raw_close_fd_pool(s
);
1165 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1167 if (ioctl(fd
, FDEJECT
, 0) < 0)
1179 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1181 BDRVRawState
*s
= bs
->opaque
;
1185 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1186 /* Note: an error can happen if the distribution automatically
1187 mounts the CD-ROM */
1188 // perror("CDROM_LOCKDOOR");
1197 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1199 BDRVRawState
*s
= bs
->opaque
;
1201 return ioctl(s
->fd
, req
, buf
);
1205 static int fd_open(BlockDriverState
*bs
)
1210 static int raw_is_inserted(BlockDriverState
*bs
)
1215 static int raw_media_changed(BlockDriverState
*bs
)
1220 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1225 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1230 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1236 BlockDriver bdrv_host_device
= {
1238 sizeof(BDRVRawState
),
1239 NULL
, /* no probe for protocols */
1248 .bdrv_aio_read
= raw_aio_read
,
1249 .bdrv_aio_write
= raw_aio_write
,
1250 .bdrv_aio_cancel
= raw_aio_cancel
,
1251 .aiocb_size
= sizeof(RawAIOCB
),
1253 .bdrv_pread
= raw_pread
,
1254 .bdrv_pwrite
= raw_pwrite
,
1255 .bdrv_getlength
= raw_getlength
,
1257 /* removable device support */
1258 .bdrv_is_inserted
= raw_is_inserted
,
1259 .bdrv_media_changed
= raw_media_changed
,
1260 .bdrv_eject
= raw_eject
,
1261 .bdrv_set_locked
= raw_set_locked
,
1262 /* generic scsi device */
1263 .bdrv_ioctl
= raw_ioctl
,