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 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
26 #include "qemu-timer.h"
28 #include "qemu-char.h"
30 #include "block_int.h"
39 #include <sys/param.h>
40 #include <IOKit/IOKitLib.h>
41 #include <IOKit/IOBSD.h>
42 #include <IOKit/storage/IOMediaBSDClient.h>
43 #include <IOKit/storage/IOMedia.h>
44 #include <IOKit/storage/IOCDMedia.h>
45 //#include <IOKit/storage/IOCDTypes.h>
46 #include <CoreFoundation/CoreFoundation.h>
50 #define _POSIX_PTHREAD_SEMANTICS 1
55 #include <sys/ioctl.h>
56 #include <linux/cdrom.h>
65 #include <sys/ioctl.h>
66 #include <sys/disklabel.h>
70 //#define DEBUG_FLOPPY
73 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
74 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
75 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
77 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
84 #define ALIGNED_BUFFER_SIZE (32 * 512)
86 /* if the FD is not accessed during that time (in ms), we try to
87 reopen it to see if the disk has been changed */
88 #define FD_OPEN_TIMEOUT 1000
90 typedef struct BDRVRawState
{
93 unsigned int lseek_err_cnt
;
94 #if defined(__linux__)
95 /* linux floppy specific */
98 int64_t fd_error_time
;
100 int fd_media_changed
;
102 #if defined(O_DIRECT) && !defined(QEMU_IMG)
103 uint8_t* aligned_buf
;
107 static int fd_open(BlockDriverState
*bs
);
109 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
111 BDRVRawState
*s
= bs
->opaque
;
112 int fd
, open_flags
, ret
;
114 s
->lseek_err_cnt
= 0;
116 open_flags
= O_BINARY
;
117 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
118 open_flags
|= O_RDWR
;
120 open_flags
|= O_RDONLY
;
123 if (flags
& BDRV_O_CREAT
)
124 open_flags
|= O_CREAT
| O_TRUNC
;
126 if (flags
& BDRV_O_DIRECT
)
127 open_flags
|= O_DIRECT
;
130 s
->type
= FTYPE_FILE
;
132 fd
= open(filename
, open_flags
, 0644);
140 #if defined(O_DIRECT) && !defined(QEMU_IMG)
141 s
->aligned_buf
= NULL
;
142 if (flags
& BDRV_O_DIRECT
) {
143 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
144 if (s
->aligned_buf
== NULL
) {
154 /* XXX: use host sector size if necessary with:
155 #ifdef DIOCGSECTORSIZE
157 unsigned int sectorsize = 512;
158 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
159 sectorsize > bufsize)
160 bufsize = sectorsize;
164 u_int32_t blockSize = 512;
165 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
172 * offset and count are in bytes, but must be multiples of 512 for files
173 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
175 * This function may be called without alignment if the caller ensures
176 * that O_DIRECT is not in effect.
178 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
179 uint8_t *buf
, int count
)
181 BDRVRawState
*s
= bs
->opaque
;
188 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
189 ++(s
->lseek_err_cnt
);
190 if(s
->lseek_err_cnt
<= 10) {
191 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
192 "] lseek failed : %d = %s\n",
193 s
->fd
, bs
->filename
, offset
, buf
, count
,
194 bs
->total_sectors
, errno
, strerror(errno
));
200 ret
= read(s
->fd
, buf
, count
);
202 goto label__raw_read__success
;
204 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
205 "] read failed %d : %d = %s\n",
206 s
->fd
, bs
->filename
, offset
, buf
, count
,
207 bs
->total_sectors
, ret
, errno
, strerror(errno
));
209 /* Try harder for CDrom. */
210 if (bs
->type
== BDRV_TYPE_CDROM
) {
211 lseek(s
->fd
, offset
, SEEK_SET
);
212 ret
= read(s
->fd
, buf
, count
);
214 goto label__raw_read__success
;
215 lseek(s
->fd
, offset
, SEEK_SET
);
216 ret
= read(s
->fd
, buf
, count
);
218 goto label__raw_read__success
;
220 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
221 "] retry read failed %d : %d = %s\n",
222 s
->fd
, bs
->filename
, offset
, buf
, count
,
223 bs
->total_sectors
, ret
, errno
, strerror(errno
));
226 label__raw_read__success
:
232 * offset and count are in bytes, but must be multiples of 512 for files
233 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
235 * This function may be called without alignment if the caller ensures
236 * that O_DIRECT is not in effect.
238 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
239 const uint8_t *buf
, int count
)
241 BDRVRawState
*s
= bs
->opaque
;
248 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
249 ++(s
->lseek_err_cnt
);
250 if(s
->lseek_err_cnt
) {
251 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
252 PRId64
"] lseek failed : %d = %s\n",
253 s
->fd
, bs
->filename
, offset
, buf
, count
,
254 bs
->total_sectors
, errno
, strerror(errno
));
258 s
->lseek_err_cnt
= 0;
260 ret
= write(s
->fd
, buf
, count
);
262 goto label__raw_write__success
;
264 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
265 "] write failed %d : %d = %s\n",
266 s
->fd
, bs
->filename
, offset
, buf
, count
,
267 bs
->total_sectors
, ret
, errno
, strerror(errno
));
269 label__raw_write__success
:
275 #if defined(O_DIRECT) && !defined(QEMU_IMG)
277 * offset and count are in bytes and possibly not aligned. For files opened
278 * with O_DIRECT, necessary alignments are ensured before calling
279 * raw_pread_aligned to do the actual read.
281 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
282 uint8_t *buf
, int count
)
284 BDRVRawState
*s
= bs
->opaque
;
285 int size
, ret
, shift
, sum
;
289 if (s
->aligned_buf
!= NULL
) {
291 if (offset
& 0x1ff) {
292 /* align offset on a 512 bytes boundary */
294 shift
= offset
& 0x1ff;
295 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
296 if (size
> ALIGNED_BUFFER_SIZE
)
297 size
= ALIGNED_BUFFER_SIZE
;
298 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
305 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
315 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
317 /* read on aligned buffer */
321 size
= (count
+ 0x1ff) & ~0x1ff;
322 if (size
> ALIGNED_BUFFER_SIZE
)
323 size
= ALIGNED_BUFFER_SIZE
;
325 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
333 memcpy(buf
, s
->aligned_buf
, size
);
345 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
349 * offset and count are in bytes and possibly not aligned. For files opened
350 * with O_DIRECT, necessary alignments are ensured before calling
351 * raw_pwrite_aligned to do the actual write.
353 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
354 const uint8_t *buf
, int count
)
356 BDRVRawState
*s
= bs
->opaque
;
357 int size
, ret
, shift
, sum
;
361 if (s
->aligned_buf
!= NULL
) {
363 if (offset
& 0x1ff) {
364 /* align offset on a 512 bytes boundary */
365 shift
= offset
& 0x1ff;
366 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
373 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
375 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
387 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
389 while ((size
= (count
& ~0x1ff)) != 0) {
391 if (size
> ALIGNED_BUFFER_SIZE
)
392 size
= ALIGNED_BUFFER_SIZE
;
394 memcpy(s
->aligned_buf
, buf
, size
);
396 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
405 /* here, count < 512 because (count & ~0x1ff) == 0 */
407 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
410 memcpy(s
->aligned_buf
, buf
, count
);
412 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
423 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
427 #define raw_pread raw_pread_aligned
428 #define raw_pwrite raw_pwrite_aligned
433 /***********************************************************/
434 /* Unix AIO using POSIX AIO */
436 typedef struct RawAIOCB
{
437 BlockDriverAIOCB common
;
439 struct RawAIOCB
*next
;
443 static int aio_sig_fd
= -1;
444 static int aio_sig_num
= SIGUSR2
;
445 static RawAIOCB
*first_aio
; /* AIO issued */
446 static int aio_initialized
= 0;
448 static void qemu_aio_poll(void *opaque
)
450 RawAIOCB
*acb
, **pacb
;
454 struct qemu_signalfd_siginfo siginfo
;
458 /* try to read from signalfd, don't freak out if we can't read anything */
460 while (offset
< 128) {
463 len
= read(aio_sig_fd
, sig
.buf
+ offset
, 128 - offset
);
464 if (len
== -1 && errno
== EINTR
)
466 if (len
== -1 && errno
== EAGAIN
) {
467 /* there is no natural reason for this to happen,
468 * so we'll spin hard until we get everything just
469 * to be on the safe side. */
483 ret
= aio_error(&acb
->aiocb
);
484 if (ret
== ECANCELED
) {
485 /* remove the request */
487 qemu_aio_release(acb
);
488 } else if (ret
!= EINPROGRESS
) {
491 ret
= aio_return(&acb
->aiocb
);
492 if (ret
== acb
->aiocb
.aio_nbytes
)
499 /* remove the request */
501 /* call the callback */
502 acb
->common
.cb(acb
->common
.opaque
, ret
);
503 qemu_aio_release(acb
);
513 void qemu_aio_init(void)
519 /* Make sure to block AIO signal */
521 sigaddset(&mask
, aio_sig_num
);
522 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
524 aio_sig_fd
= qemu_signalfd(&mask
);
526 fcntl(aio_sig_fd
, F_SETFL
, O_NONBLOCK
);
528 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
529 qemu_set_fd_handler2(aio_sig_fd
, NULL
, qemu_aio_poll
, NULL
, NULL
);
532 #if defined(__GLIBC__) && defined(__linux__)
534 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
535 seems to fix the problem. */
537 memset(&ai
, 0, sizeof(ai
));
540 ai
.aio_idle_time
= 365 * 100000;
546 /* Wait for all IO requests to complete. */
547 void qemu_aio_flush(void)
555 void qemu_aio_wait(void)
559 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
571 FD_SET(aio_sig_fd
, &rdfds
);
573 ret
= select(aio_sig_fd
+ 1, &rdfds
, NULL
, NULL
, NULL
);
574 if (ret
== -1 && errno
== EINTR
)
581 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
582 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
583 BlockDriverCompletionFunc
*cb
, void *opaque
)
585 BDRVRawState
*s
= bs
->opaque
;
591 acb
= qemu_aio_get(bs
, cb
, opaque
);
594 acb
->aiocb
.aio_fildes
= s
->fd
;
595 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
596 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
597 acb
->aiocb
.aio_buf
= buf
;
599 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
601 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
602 acb
->aiocb
.aio_offset
= sector_num
* 512;
603 acb
->next
= first_aio
;
608 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
609 static void raw_aio_em_cb(void* opaque
)
611 RawAIOCB
*acb
= opaque
;
612 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
613 qemu_aio_release(acb
);
617 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
618 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
619 BlockDriverCompletionFunc
*cb
, void *opaque
)
624 * If O_DIRECT is used and the buffer is not aligned fall back
627 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
628 BDRVRawState
*s
= bs
->opaque
;
630 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
632 acb
= qemu_aio_get(bs
, cb
, opaque
);
633 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
634 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
635 qemu_bh_schedule(bh
);
640 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
643 if (aio_read(&acb
->aiocb
) < 0) {
644 qemu_aio_release(acb
);
650 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
651 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
652 BlockDriverCompletionFunc
*cb
, void *opaque
)
657 * If O_DIRECT is used and the buffer is not aligned fall back
660 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
661 BDRVRawState
*s
= bs
->opaque
;
663 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
665 acb
= qemu_aio_get(bs
, cb
, opaque
);
666 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
667 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
668 qemu_bh_schedule(bh
);
673 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
676 if (aio_write(&acb
->aiocb
) < 0) {
677 qemu_aio_release(acb
);
683 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
686 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
689 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
690 if (ret
== AIO_NOTCANCELED
) {
691 /* fail safe: if the aio could not be canceled, we wait for
693 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
696 /* remove the callback from the queue */
701 } else if (*pacb
== acb
) {
703 qemu_aio_release(acb
);
710 # else /* CONFIG_AIO */
712 void qemu_aio_init(void)
716 void qemu_aio_flush(void)
720 void qemu_aio_wait(void)
722 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
727 #endif /* CONFIG_AIO */
729 static void raw_close(BlockDriverState
*bs
)
731 BDRVRawState
*s
= bs
->opaque
;
735 #if defined(O_DIRECT) && !defined(QEMU_IMG)
736 if (s
->aligned_buf
!= NULL
)
737 qemu_free(s
->aligned_buf
);
742 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
744 BDRVRawState
*s
= bs
->opaque
;
745 if (s
->type
!= FTYPE_FILE
)
747 if (ftruncate(s
->fd
, offset
) < 0)
753 static int64_t raw_getlength(BlockDriverState
*bs
)
755 BDRVRawState
*s
= bs
->opaque
;
761 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
764 if (ioctl(fd
, DIOCGDINFO
, &dl
))
766 return (uint64_t)dl
.d_secsize
*
767 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
771 #else /* !__OpenBSD__ */
772 static int64_t raw_getlength(BlockDriverState
*bs
)
774 BDRVRawState
*s
= bs
->opaque
;
781 struct dk_minfo minfo
;
791 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
792 #ifdef DIOCGMEDIASIZE
793 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
796 size
= LONG_LONG_MAX
;
798 size
= lseek(fd
, 0LL, SEEK_END
);
804 * use the DKIOCGMEDIAINFO ioctl to read the size.
806 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
808 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
809 } else /* there are reports that lseek on some devices
810 fails, but irc discussion said that contingency
811 on contingency was overkill */
814 size
= lseek(fd
, 0, SEEK_END
);
820 static int raw_create(const char *filename
, int64_t total_size
,
821 const char *backing_file
, int flags
)
825 if (flags
|| backing_file
)
828 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
832 ftruncate(fd
, total_size
* 512);
837 static void raw_flush(BlockDriverState
*bs
)
839 BDRVRawState
*s
= bs
->opaque
;
843 BlockDriver bdrv_raw
= {
845 sizeof(BDRVRawState
),
846 NULL
, /* no probe for protocols */
855 .bdrv_aio_read
= raw_aio_read
,
856 .bdrv_aio_write
= raw_aio_write
,
857 .bdrv_aio_cancel
= raw_aio_cancel
,
858 .aiocb_size
= sizeof(RawAIOCB
),
860 .protocol_name
= "file",
861 .bdrv_pread
= raw_pread
,
862 .bdrv_pwrite
= raw_pwrite
,
863 .bdrv_truncate
= raw_truncate
,
864 .bdrv_getlength
= raw_getlength
,
867 /***********************************************/
871 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
872 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
874 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
876 kern_return_t kernResult
;
877 mach_port_t masterPort
;
878 CFMutableDictionaryRef classesToMatch
;
880 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
881 if ( KERN_SUCCESS
!= kernResult
) {
882 printf( "IOMasterPort returned %d\n", kernResult
);
885 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
886 if ( classesToMatch
== NULL
) {
887 printf( "IOServiceMatching returned a NULL dictionary.\n" );
889 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
891 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
892 if ( KERN_SUCCESS
!= kernResult
)
894 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
900 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
902 io_object_t nextMedia
;
903 kern_return_t kernResult
= KERN_FAILURE
;
905 nextMedia
= IOIteratorNext( mediaIterator
);
908 CFTypeRef bsdPathAsCFString
;
909 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
910 if ( bsdPathAsCFString
) {
911 size_t devPathLength
;
912 strcpy( bsdPath
, _PATH_DEV
);
913 strcat( bsdPath
, "r" );
914 devPathLength
= strlen( bsdPath
);
915 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
916 kernResult
= KERN_SUCCESS
;
918 CFRelease( bsdPathAsCFString
);
920 IOObjectRelease( nextMedia
);
928 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
930 BDRVRawState
*s
= bs
->opaque
;
931 int fd
, open_flags
, ret
;
934 if (strstart(filename
, "/dev/cdrom", NULL
)) {
935 kern_return_t kernResult
;
936 io_iterator_t mediaIterator
;
937 char bsdPath
[ MAXPATHLEN
];
940 kernResult
= FindEjectableCDMedia( &mediaIterator
);
941 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
943 if ( bsdPath
[ 0 ] != '\0' ) {
944 strcat(bsdPath
,"s0");
945 /* some CDs don't have a partition 0 */
946 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
948 bsdPath
[strlen(bsdPath
)-1] = '1';
956 IOObjectRelease( mediaIterator
);
959 open_flags
= O_BINARY
;
960 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
961 open_flags
|= O_RDWR
;
963 open_flags
|= O_RDONLY
;
967 if (flags
& BDRV_O_DIRECT
)
968 open_flags
|= O_DIRECT
;
971 s
->type
= FTYPE_FILE
;
972 #if defined(__linux__)
973 if (strstart(filename
, "/dev/cd", NULL
)) {
974 /* open will not fail even if no CD is inserted */
975 open_flags
|= O_NONBLOCK
;
977 } else if (strstart(filename
, "/dev/fd", NULL
)) {
979 s
->fd_open_flags
= open_flags
;
980 /* open will not fail even if no floppy is inserted */
981 open_flags
|= O_NONBLOCK
;
982 } else if (strstart(filename
, "/dev/sg", NULL
)) {
986 fd
= open(filename
, open_flags
, 0644);
994 #if defined(__linux__)
995 /* close fd so that we can reopen it as needed */
996 if (s
->type
== FTYPE_FD
) {
999 s
->fd_media_changed
= 1;
1005 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1007 /* Note: we do not have a reliable method to detect if the floppy is
1008 present. The current method is to try to open the floppy at every
1009 I/O and to keep it opened during a few hundreds of ms. */
1010 static int fd_open(BlockDriverState
*bs
)
1012 BDRVRawState
*s
= bs
->opaque
;
1013 int last_media_present
;
1015 if (s
->type
!= FTYPE_FD
)
1017 last_media_present
= (s
->fd
>= 0);
1019 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1023 printf("Floppy closed\n");
1027 if (s
->fd_got_error
&&
1028 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1030 printf("No floppy (open delayed)\n");
1034 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1036 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1037 s
->fd_got_error
= 1;
1038 if (last_media_present
)
1039 s
->fd_media_changed
= 1;
1041 printf("No floppy\n");
1046 printf("Floppy opened\n");
1049 if (!last_media_present
)
1050 s
->fd_media_changed
= 1;
1051 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1052 s
->fd_got_error
= 0;
1056 static int fd_open(BlockDriverState
*bs
)
1062 #if defined(__linux__)
1064 static int raw_is_inserted(BlockDriverState
*bs
)
1066 BDRVRawState
*s
= bs
->opaque
;
1071 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1072 if (ret
== CDS_DISC_OK
)
1085 /* currently only used by fdc.c, but a CD version would be good too */
1086 static int raw_media_changed(BlockDriverState
*bs
)
1088 BDRVRawState
*s
= bs
->opaque
;
1094 /* XXX: we do not have a true media changed indication. It
1095 does not work if the floppy is changed without trying
1098 ret
= s
->fd_media_changed
;
1099 s
->fd_media_changed
= 0;
1101 printf("Floppy changed=%d\n", ret
);
1110 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1112 BDRVRawState
*s
= bs
->opaque
;
1117 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1118 perror("CDROMEJECT");
1120 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1121 perror("CDROMEJECT");
1131 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1133 if (ioctl(fd
, FDEJECT
, 0) < 0)
1145 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1147 BDRVRawState
*s
= bs
->opaque
;
1151 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1152 /* Note: an error can happen if the distribution automatically
1153 mounts the CD-ROM */
1154 // perror("CDROM_LOCKDOOR");
1163 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1165 BDRVRawState
*s
= bs
->opaque
;
1167 return ioctl(s
->fd
, req
, buf
);
1171 static int raw_is_inserted(BlockDriverState
*bs
)
1176 static int raw_media_changed(BlockDriverState
*bs
)
1181 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1186 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1191 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1197 BlockDriver bdrv_host_device
= {
1199 sizeof(BDRVRawState
),
1200 NULL
, /* no probe for protocols */
1209 .bdrv_aio_read
= raw_aio_read
,
1210 .bdrv_aio_write
= raw_aio_write
,
1211 .bdrv_aio_cancel
= raw_aio_cancel
,
1212 .aiocb_size
= sizeof(RawAIOCB
),
1214 .bdrv_pread
= raw_pread
,
1215 .bdrv_pwrite
= raw_pwrite
,
1216 .bdrv_getlength
= raw_getlength
,
1218 /* removable device support */
1219 .bdrv_is_inserted
= raw_is_inserted
,
1220 .bdrv_media_changed
= raw_media_changed
,
1221 .bdrv_eject
= raw_eject
,
1222 .bdrv_set_locked
= raw_set_locked
,
1223 /* generic scsi device */
1224 .bdrv_ioctl
= raw_ioctl
,