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"
30 #include "posix-aio-compat.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>
67 #include <sys/ioctl.h>
68 #include <sys/diskslice.h>
71 //#define DEBUG_FLOPPY
74 #if defined(DEBUG_BLOCK)
75 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled()) \
76 { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0)
78 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
81 /* OS X does not have O_DSYNC */
83 #define O_DSYNC O_SYNC
86 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
88 #define O_DIRECT O_DSYNC
95 #define ALIGNED_BUFFER_SIZE (32 * 512)
97 /* if the FD is not accessed during that time (in ms), we try to
98 reopen it to see if the disk has been changed */
99 #define FD_OPEN_TIMEOUT 1000
101 typedef struct BDRVRawState
{
104 unsigned int lseek_err_cnt
;
105 #if defined(__linux__)
106 /* linux floppy specific */
108 int64_t fd_open_time
;
109 int64_t fd_error_time
;
111 int fd_media_changed
;
113 uint8_t* aligned_buf
;
116 static int posix_aio_init(void);
118 static int fd_open(BlockDriverState
*bs
);
120 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
122 BDRVRawState
*s
= bs
->opaque
;
123 int fd
, open_flags
, ret
;
127 s
->lseek_err_cnt
= 0;
129 open_flags
= O_BINARY
;
130 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
131 open_flags
|= O_RDWR
;
133 open_flags
|= O_RDONLY
;
136 if (flags
& BDRV_O_CREAT
)
137 open_flags
|= O_CREAT
| O_TRUNC
;
139 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
140 * and O_DIRECT for no caching. */
141 if ((flags
& BDRV_O_NOCACHE
))
142 open_flags
|= O_DIRECT
;
143 else if (!(flags
& BDRV_O_CACHE_WB
))
144 open_flags
|= O_DSYNC
;
146 s
->type
= FTYPE_FILE
;
148 fd
= open(filename
, open_flags
, 0644);
156 s
->aligned_buf
= NULL
;
157 if ((flags
& BDRV_O_NOCACHE
)) {
158 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
159 if (s
->aligned_buf
== NULL
) {
168 /* XXX: use host sector size if necessary with:
169 #ifdef DIOCGSECTORSIZE
171 unsigned int sectorsize = 512;
172 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
173 sectorsize > bufsize)
174 bufsize = sectorsize;
178 u_int32_t blockSize = 512;
179 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
186 * offset and count are in bytes, but must be multiples of 512 for files
187 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
189 * This function may be called without alignment if the caller ensures
190 * that O_DIRECT is not in effect.
192 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
193 uint8_t *buf
, int count
)
195 BDRVRawState
*s
= bs
->opaque
;
202 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
203 ++(s
->lseek_err_cnt
);
204 if(s
->lseek_err_cnt
<= 10) {
205 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
206 "] lseek failed : %d = %s\n",
207 s
->fd
, bs
->filename
, offset
, buf
, count
,
208 bs
->total_sectors
, errno
, strerror(errno
));
214 ret
= read(s
->fd
, buf
, count
);
216 goto label__raw_read__success
;
218 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
219 "] read failed %d : %d = %s\n",
220 s
->fd
, bs
->filename
, offset
, buf
, count
,
221 bs
->total_sectors
, ret
, errno
, strerror(errno
));
223 /* Try harder for CDrom. */
224 if (bs
->type
== BDRV_TYPE_CDROM
) {
225 lseek(s
->fd
, offset
, SEEK_SET
);
226 ret
= read(s
->fd
, buf
, count
);
228 goto label__raw_read__success
;
229 lseek(s
->fd
, offset
, SEEK_SET
);
230 ret
= read(s
->fd
, buf
, count
);
232 goto label__raw_read__success
;
234 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
235 "] retry read failed %d : %d = %s\n",
236 s
->fd
, bs
->filename
, offset
, buf
, count
,
237 bs
->total_sectors
, ret
, errno
, strerror(errno
));
240 label__raw_read__success
:
246 * offset and count are in bytes, but must be multiples of 512 for files
247 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
249 * This function may be called without alignment if the caller ensures
250 * that O_DIRECT is not in effect.
252 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
253 const uint8_t *buf
, int count
)
255 BDRVRawState
*s
= bs
->opaque
;
262 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
263 ++(s
->lseek_err_cnt
);
264 if(s
->lseek_err_cnt
) {
265 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
266 PRId64
"] lseek failed : %d = %s\n",
267 s
->fd
, bs
->filename
, offset
, buf
, count
,
268 bs
->total_sectors
, errno
, strerror(errno
));
272 s
->lseek_err_cnt
= 0;
274 ret
= write(s
->fd
, buf
, count
);
276 goto label__raw_write__success
;
278 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
279 "] write failed %d : %d = %s\n",
280 s
->fd
, bs
->filename
, offset
, buf
, count
,
281 bs
->total_sectors
, ret
, errno
, strerror(errno
));
283 label__raw_write__success
:
285 return (ret
< 0) ? -errno
: ret
;
290 * offset and count are in bytes and possibly not aligned. For files opened
291 * with O_DIRECT, necessary alignments are ensured before calling
292 * raw_pread_aligned to do the actual read.
294 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
295 uint8_t *buf
, int count
)
297 BDRVRawState
*s
= bs
->opaque
;
298 int size
, ret
, shift
, sum
;
302 if (s
->aligned_buf
!= NULL
) {
304 if (offset
& 0x1ff) {
305 /* align offset on a 512 bytes boundary */
307 shift
= offset
& 0x1ff;
308 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
309 if (size
> ALIGNED_BUFFER_SIZE
)
310 size
= ALIGNED_BUFFER_SIZE
;
311 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
318 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
328 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
330 /* read on aligned buffer */
334 size
= (count
+ 0x1ff) & ~0x1ff;
335 if (size
> ALIGNED_BUFFER_SIZE
)
336 size
= ALIGNED_BUFFER_SIZE
;
338 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
346 memcpy(buf
, s
->aligned_buf
, size
);
358 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
361 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
362 uint8_t *buf
, int nb_sectors
)
366 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
367 if (ret
== (nb_sectors
* 512))
373 * offset and count are in bytes and possibly not aligned. For files opened
374 * with O_DIRECT, necessary alignments are ensured before calling
375 * raw_pwrite_aligned to do the actual write.
377 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
378 const uint8_t *buf
, int count
)
380 BDRVRawState
*s
= bs
->opaque
;
381 int size
, ret
, shift
, sum
;
385 if (s
->aligned_buf
!= NULL
) {
387 if (offset
& 0x1ff) {
388 /* align offset on a 512 bytes boundary */
389 shift
= offset
& 0x1ff;
390 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
397 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
399 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
411 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
413 while ((size
= (count
& ~0x1ff)) != 0) {
415 if (size
> ALIGNED_BUFFER_SIZE
)
416 size
= ALIGNED_BUFFER_SIZE
;
418 memcpy(s
->aligned_buf
, buf
, size
);
420 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
429 /* here, count < 512 because (count & ~0x1ff) == 0 */
431 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
434 memcpy(s
->aligned_buf
, buf
, count
);
436 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
447 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
450 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
451 const uint8_t *buf
, int nb_sectors
)
454 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
455 if (ret
== (nb_sectors
* 512))
461 /***********************************************************/
462 /* Unix AIO using POSIX AIO */
464 typedef struct RawAIOCB
{
465 BlockDriverAIOCB common
;
466 struct qemu_paiocb aiocb
;
467 struct RawAIOCB
*next
;
471 typedef struct PosixAioState
477 static void posix_aio_read(void *opaque
)
479 PosixAioState
*s
= opaque
;
480 RawAIOCB
*acb
, **pacb
;
484 /* read all bytes from signal pipe */
488 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
489 if (len
== -1 && errno
== EINTR
)
490 continue; /* try again */
491 if (len
== sizeof(bytes
))
492 continue; /* more to read */
497 pacb
= &s
->first_aio
;
502 ret
= qemu_paio_error(&acb
->aiocb
);
503 if (ret
== ECANCELED
) {
504 /* remove the request */
506 qemu_aio_release(acb
);
507 } else if (ret
!= EINPROGRESS
) {
510 ret
= qemu_paio_return(&acb
->aiocb
);
511 if (ret
== acb
->aiocb
.aio_nbytes
)
518 /* remove the request */
520 /* call the callback */
521 acb
->common
.cb(acb
->common
.opaque
, ret
);
522 qemu_aio_release(acb
);
532 static int posix_aio_flush(void *opaque
)
534 PosixAioState
*s
= opaque
;
535 return !!s
->first_aio
;
538 static PosixAioState
*posix_aio_state
;
540 static void aio_signal_handler(int signum
)
542 if (posix_aio_state
) {
545 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
551 static int posix_aio_init(void)
553 struct sigaction act
;
556 struct qemu_paioinit ai
;
561 s
= qemu_malloc(sizeof(PosixAioState
));
563 sigfillset(&act
.sa_mask
);
564 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
565 act
.sa_handler
= aio_signal_handler
;
566 sigaction(SIGUSR2
, &act
, NULL
);
569 if (pipe(fds
) == -1) {
570 fprintf(stderr
, "failed to create pipe\n");
577 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
578 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
580 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
582 memset(&ai
, 0, sizeof(ai
));
592 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
593 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
594 BlockDriverCompletionFunc
*cb
, void *opaque
)
596 BDRVRawState
*s
= bs
->opaque
;
602 acb
= qemu_aio_get(bs
, cb
, opaque
);
605 acb
->aiocb
.aio_fildes
= s
->fd
;
606 acb
->aiocb
.ev_signo
= SIGUSR2
;
607 acb
->aiocb
.aio_buf
= buf
;
609 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
611 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
612 acb
->aiocb
.aio_offset
= sector_num
* 512;
613 acb
->next
= posix_aio_state
->first_aio
;
614 posix_aio_state
->first_aio
= acb
;
618 static void raw_aio_em_cb(void* opaque
)
620 RawAIOCB
*acb
= opaque
;
621 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
622 qemu_aio_release(acb
);
625 static void raw_aio_remove(RawAIOCB
*acb
)
629 /* remove the callback from the queue */
630 pacb
= &posix_aio_state
->first_aio
;
633 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
635 } else if (*pacb
== acb
) {
637 qemu_aio_release(acb
);
640 pacb
= &(*pacb
)->next
;
644 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
645 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
646 BlockDriverCompletionFunc
*cb
, void *opaque
)
651 * If O_DIRECT is used and the buffer is not aligned fall back
654 BDRVRawState
*s
= bs
->opaque
;
656 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
658 acb
= qemu_aio_get(bs
, cb
, opaque
);
659 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
660 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
661 qemu_bh_schedule(bh
);
665 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
668 if (qemu_paio_read(&acb
->aiocb
) < 0) {
675 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
676 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
677 BlockDriverCompletionFunc
*cb
, void *opaque
)
682 * If O_DIRECT is used and the buffer is not aligned fall back
685 BDRVRawState
*s
= bs
->opaque
;
687 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
689 acb
= qemu_aio_get(bs
, cb
, opaque
);
690 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
691 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
692 qemu_bh_schedule(bh
);
696 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
699 if (qemu_paio_write(&acb
->aiocb
) < 0) {
706 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
709 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
711 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
712 if (ret
== QEMU_PAIO_NOTCANCELED
) {
713 /* fail safe: if the aio could not be canceled, we wait for
715 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
720 #else /* CONFIG_AIO */
721 static int posix_aio_init(void)
725 #endif /* CONFIG_AIO */
728 static void raw_close(BlockDriverState
*bs
)
730 BDRVRawState
*s
= bs
->opaque
;
734 if (s
->aligned_buf
!= NULL
)
735 qemu_free(s
->aligned_buf
);
739 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
741 BDRVRawState
*s
= bs
->opaque
;
742 if (s
->type
!= FTYPE_FILE
)
744 if (ftruncate(s
->fd
, offset
) < 0)
750 static int64_t raw_getlength(BlockDriverState
*bs
)
752 BDRVRawState
*s
= bs
->opaque
;
758 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
761 if (ioctl(fd
, DIOCGDINFO
, &dl
))
763 return (uint64_t)dl
.d_secsize
*
764 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
768 #else /* !__OpenBSD__ */
769 static int64_t raw_getlength(BlockDriverState
*bs
)
771 BDRVRawState
*s
= bs
->opaque
;
778 struct dk_minfo minfo
;
788 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
789 #ifdef DIOCGMEDIASIZE
790 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
791 #elif defined(DIOCGPART)
794 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
795 size
= pi
.media_size
;
802 size
= LONG_LONG_MAX
;
804 size
= lseek(fd
, 0LL, SEEK_END
);
810 * use the DKIOCGMEDIAINFO ioctl to read the size.
812 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
814 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
815 } else /* there are reports that lseek on some devices
816 fails, but irc discussion said that contingency
817 on contingency was overkill */
820 size
= lseek(fd
, 0, SEEK_END
);
826 static int raw_create(const char *filename
, int64_t total_size
,
827 const char *backing_file
, int flags
)
831 if (flags
|| backing_file
)
834 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
838 ftruncate(fd
, total_size
* 512);
843 static void raw_flush(BlockDriverState
*bs
)
845 BDRVRawState
*s
= bs
->opaque
;
849 BlockDriver bdrv_raw
= {
851 sizeof(BDRVRawState
),
852 NULL
, /* no probe for protocols */
861 .bdrv_aio_read
= raw_aio_read
,
862 .bdrv_aio_write
= raw_aio_write
,
863 .bdrv_aio_cancel
= raw_aio_cancel
,
864 .aiocb_size
= sizeof(RawAIOCB
),
867 .bdrv_read
= raw_read
,
868 .bdrv_write
= raw_write
,
869 .bdrv_truncate
= raw_truncate
,
870 .bdrv_getlength
= raw_getlength
,
873 /***********************************************/
877 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
878 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
880 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
882 kern_return_t kernResult
;
883 mach_port_t masterPort
;
884 CFMutableDictionaryRef classesToMatch
;
886 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
887 if ( KERN_SUCCESS
!= kernResult
) {
888 printf( "IOMasterPort returned %d\n", kernResult
);
891 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
892 if ( classesToMatch
== NULL
) {
893 printf( "IOServiceMatching returned a NULL dictionary.\n" );
895 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
897 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
898 if ( KERN_SUCCESS
!= kernResult
)
900 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
906 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
908 io_object_t nextMedia
;
909 kern_return_t kernResult
= KERN_FAILURE
;
911 nextMedia
= IOIteratorNext( mediaIterator
);
914 CFTypeRef bsdPathAsCFString
;
915 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
916 if ( bsdPathAsCFString
) {
917 size_t devPathLength
;
918 strcpy( bsdPath
, _PATH_DEV
);
919 strcat( bsdPath
, "r" );
920 devPathLength
= strlen( bsdPath
);
921 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
922 kernResult
= KERN_SUCCESS
;
924 CFRelease( bsdPathAsCFString
);
926 IOObjectRelease( nextMedia
);
934 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
936 BDRVRawState
*s
= bs
->opaque
;
937 int fd
, open_flags
, ret
;
942 if (strstart(filename
, "/dev/cdrom", NULL
)) {
943 kern_return_t kernResult
;
944 io_iterator_t mediaIterator
;
945 char bsdPath
[ MAXPATHLEN
];
948 kernResult
= FindEjectableCDMedia( &mediaIterator
);
949 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
951 if ( bsdPath
[ 0 ] != '\0' ) {
952 strcat(bsdPath
,"s0");
953 /* some CDs don't have a partition 0 */
954 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
956 bsdPath
[strlen(bsdPath
)-1] = '1';
964 IOObjectRelease( mediaIterator
);
967 open_flags
= O_BINARY
;
968 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
969 open_flags
|= O_RDWR
;
971 open_flags
|= O_RDONLY
;
974 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
975 * and O_DIRECT for no caching. */
976 if ((flags
& BDRV_O_NOCACHE
))
977 open_flags
|= O_DIRECT
;
978 else if (!(flags
& BDRV_O_CACHE_WB
))
979 open_flags
|= O_DSYNC
;
981 s
->type
= FTYPE_FILE
;
982 #if defined(__linux__)
983 if (strstart(filename
, "/dev/cd", NULL
)) {
984 /* open will not fail even if no CD is inserted */
985 open_flags
|= O_NONBLOCK
;
987 } else if (strstart(filename
, "/dev/fd", NULL
)) {
989 s
->fd_open_flags
= open_flags
;
990 /* open will not fail even if no floppy is inserted */
991 open_flags
|= O_NONBLOCK
;
992 } else if (strstart(filename
, "/dev/sg", NULL
)) {
996 fd
= open(filename
, open_flags
, 0644);
1004 #if defined(__linux__)
1005 /* close fd so that we can reopen it as needed */
1006 if (s
->type
== FTYPE_FD
) {
1009 s
->fd_media_changed
= 1;
1015 #if defined(__linux__)
1016 /* Note: we do not have a reliable method to detect if the floppy is
1017 present. The current method is to try to open the floppy at every
1018 I/O and to keep it opened during a few hundreds of ms. */
1019 static int fd_open(BlockDriverState
*bs
)
1021 BDRVRawState
*s
= bs
->opaque
;
1022 int last_media_present
;
1024 if (s
->type
!= FTYPE_FD
)
1026 last_media_present
= (s
->fd
>= 0);
1028 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1032 printf("Floppy closed\n");
1036 if (s
->fd_got_error
&&
1037 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1039 printf("No floppy (open delayed)\n");
1043 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1045 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1046 s
->fd_got_error
= 1;
1047 if (last_media_present
)
1048 s
->fd_media_changed
= 1;
1050 printf("No floppy\n");
1055 printf("Floppy opened\n");
1058 if (!last_media_present
)
1059 s
->fd_media_changed
= 1;
1060 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1061 s
->fd_got_error
= 0;
1065 static int raw_is_inserted(BlockDriverState
*bs
)
1067 BDRVRawState
*s
= bs
->opaque
;
1072 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1073 if (ret
== CDS_DISC_OK
)
1086 /* currently only used by fdc.c, but a CD version would be good too */
1087 static int raw_media_changed(BlockDriverState
*bs
)
1089 BDRVRawState
*s
= bs
->opaque
;
1095 /* XXX: we do not have a true media changed indication. It
1096 does not work if the floppy is changed without trying
1099 ret
= s
->fd_media_changed
;
1100 s
->fd_media_changed
= 0;
1102 printf("Floppy changed=%d\n", ret
);
1111 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1113 BDRVRawState
*s
= bs
->opaque
;
1118 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1119 perror("CDROMEJECT");
1121 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1122 perror("CDROMEJECT");
1132 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1134 if (ioctl(fd
, FDEJECT
, 0) < 0)
1146 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1148 BDRVRawState
*s
= bs
->opaque
;
1152 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1153 /* Note: an error can happen if the distribution automatically
1154 mounts the CD-ROM */
1155 // perror("CDROM_LOCKDOOR");
1164 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1166 BDRVRawState
*s
= bs
->opaque
;
1168 return ioctl(s
->fd
, req
, buf
);
1172 static int fd_open(BlockDriverState
*bs
)
1177 static int raw_is_inserted(BlockDriverState
*bs
)
1182 static int raw_media_changed(BlockDriverState
*bs
)
1187 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1192 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1197 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1203 static int raw_sg_send_command(BlockDriverState
*bs
, void *buf
, int count
)
1205 return raw_pwrite(bs
, -1, buf
, count
);
1208 static int raw_sg_recv_response(BlockDriverState
*bs
, void *buf
, int count
)
1210 return raw_pread(bs
, -1, buf
, count
);
1213 static BlockDriverAIOCB
*raw_sg_aio_read(BlockDriverState
*bs
,
1214 void *buf
, int count
,
1215 BlockDriverCompletionFunc
*cb
,
1218 return raw_aio_read(bs
, 0, buf
, -(int64_t)count
, cb
, opaque
);
1221 static BlockDriverAIOCB
*raw_sg_aio_write(BlockDriverState
*bs
,
1222 void *buf
, int count
,
1223 BlockDriverCompletionFunc
*cb
,
1226 return raw_aio_write(bs
, 0, buf
, -(int64_t)count
, cb
, opaque
);
1229 BlockDriver bdrv_host_device
= {
1230 .format_name
= "host_device",
1231 .instance_size
= sizeof(BDRVRawState
),
1232 .bdrv_open
= hdev_open
,
1233 .bdrv_close
= raw_close
,
1234 .bdrv_flush
= raw_flush
,
1237 .bdrv_aio_read
= raw_aio_read
,
1238 .bdrv_aio_write
= raw_aio_write
,
1239 .bdrv_aio_cancel
= raw_aio_cancel
,
1240 .aiocb_size
= sizeof(RawAIOCB
),
1243 .bdrv_read
= raw_read
,
1244 .bdrv_write
= raw_write
,
1245 .bdrv_getlength
= raw_getlength
,
1247 /* removable device support */
1248 .bdrv_is_inserted
= raw_is_inserted
,
1249 .bdrv_media_changed
= raw_media_changed
,
1250 .bdrv_eject
= raw_eject
,
1251 .bdrv_set_locked
= raw_set_locked
,
1252 /* generic scsi device */
1253 .bdrv_ioctl
= raw_ioctl
,
1254 .bdrv_sg_send_command
= raw_sg_send_command
,
1255 .bdrv_sg_recv_response
= raw_sg_recv_response
,
1256 .bdrv_sg_aio_read
= raw_sg_aio_read
,
1257 .bdrv_sg_aio_write
= raw_sg_aio_write
,