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)
568 FD_SET(aio_sig_fd
, &rdfds
);
570 ret
= select(aio_sig_fd
+ 1, &rdfds
, NULL
, NULL
, NULL
);
571 if (ret
== -1 && errno
== EINTR
)
578 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
579 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
580 BlockDriverCompletionFunc
*cb
, void *opaque
)
582 BDRVRawState
*s
= bs
->opaque
;
588 acb
= qemu_aio_get(bs
, cb
, opaque
);
591 acb
->aiocb
.aio_fildes
= s
->fd
;
592 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
593 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
594 acb
->aiocb
.aio_buf
= buf
;
596 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
598 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
599 acb
->aiocb
.aio_offset
= sector_num
* 512;
600 acb
->next
= first_aio
;
605 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
606 static void raw_aio_em_cb(void* opaque
)
608 RawAIOCB
*acb
= opaque
;
609 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
610 qemu_aio_release(acb
);
614 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
615 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
616 BlockDriverCompletionFunc
*cb
, void *opaque
)
621 * If O_DIRECT is used and the buffer is not aligned fall back
624 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
625 BDRVRawState
*s
= bs
->opaque
;
627 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
629 acb
= qemu_aio_get(bs
, cb
, opaque
);
630 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
631 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
632 qemu_bh_schedule(bh
);
637 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
640 if (aio_read(&acb
->aiocb
) < 0) {
641 qemu_aio_release(acb
);
647 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
648 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
649 BlockDriverCompletionFunc
*cb
, void *opaque
)
654 * If O_DIRECT is used and the buffer is not aligned fall back
657 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
658 BDRVRawState
*s
= bs
->opaque
;
660 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
662 acb
= qemu_aio_get(bs
, cb
, opaque
);
663 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
664 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
665 qemu_bh_schedule(bh
);
670 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
673 if (aio_write(&acb
->aiocb
) < 0) {
674 qemu_aio_release(acb
);
680 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
683 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
686 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
687 if (ret
== AIO_NOTCANCELED
) {
688 /* fail safe: if the aio could not be canceled, we wait for
690 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
693 /* remove the callback from the queue */
698 } else if (*pacb
== acb
) {
700 qemu_aio_release(acb
);
707 # else /* CONFIG_AIO */
709 void qemu_aio_init(void)
713 void qemu_aio_flush(void)
717 void qemu_aio_wait(void)
719 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
724 #endif /* CONFIG_AIO */
726 static void raw_close(BlockDriverState
*bs
)
728 BDRVRawState
*s
= bs
->opaque
;
732 #if defined(O_DIRECT) && !defined(QEMU_IMG)
733 if (s
->aligned_buf
!= NULL
)
734 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
))
793 size
= LONG_LONG_MAX
;
795 size
= lseek(fd
, 0LL, SEEK_END
);
801 * use the DKIOCGMEDIAINFO ioctl to read the size.
803 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
805 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
806 } else /* there are reports that lseek on some devices
807 fails, but irc discussion said that contingency
808 on contingency was overkill */
811 size
= lseek(fd
, 0, SEEK_END
);
817 static int raw_create(const char *filename
, int64_t total_size
,
818 const char *backing_file
, int flags
)
822 if (flags
|| backing_file
)
825 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
829 ftruncate(fd
, total_size
* 512);
834 static void raw_flush(BlockDriverState
*bs
)
836 BDRVRawState
*s
= bs
->opaque
;
840 BlockDriver bdrv_raw
= {
842 sizeof(BDRVRawState
),
843 NULL
, /* no probe for protocols */
852 .bdrv_aio_read
= raw_aio_read
,
853 .bdrv_aio_write
= raw_aio_write
,
854 .bdrv_aio_cancel
= raw_aio_cancel
,
855 .aiocb_size
= sizeof(RawAIOCB
),
857 .protocol_name
= "file",
858 .bdrv_pread
= raw_pread
,
859 .bdrv_pwrite
= raw_pwrite
,
860 .bdrv_truncate
= raw_truncate
,
861 .bdrv_getlength
= raw_getlength
,
864 /***********************************************/
868 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
869 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
871 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
873 kern_return_t kernResult
;
874 mach_port_t masterPort
;
875 CFMutableDictionaryRef classesToMatch
;
877 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
878 if ( KERN_SUCCESS
!= kernResult
) {
879 printf( "IOMasterPort returned %d\n", kernResult
);
882 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
883 if ( classesToMatch
== NULL
) {
884 printf( "IOServiceMatching returned a NULL dictionary.\n" );
886 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
888 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
889 if ( KERN_SUCCESS
!= kernResult
)
891 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
897 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
899 io_object_t nextMedia
;
900 kern_return_t kernResult
= KERN_FAILURE
;
902 nextMedia
= IOIteratorNext( mediaIterator
);
905 CFTypeRef bsdPathAsCFString
;
906 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
907 if ( bsdPathAsCFString
) {
908 size_t devPathLength
;
909 strcpy( bsdPath
, _PATH_DEV
);
910 strcat( bsdPath
, "r" );
911 devPathLength
= strlen( bsdPath
);
912 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
913 kernResult
= KERN_SUCCESS
;
915 CFRelease( bsdPathAsCFString
);
917 IOObjectRelease( nextMedia
);
925 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
927 BDRVRawState
*s
= bs
->opaque
;
928 int fd
, open_flags
, ret
;
931 if (strstart(filename
, "/dev/cdrom", NULL
)) {
932 kern_return_t kernResult
;
933 io_iterator_t mediaIterator
;
934 char bsdPath
[ MAXPATHLEN
];
937 kernResult
= FindEjectableCDMedia( &mediaIterator
);
938 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
940 if ( bsdPath
[ 0 ] != '\0' ) {
941 strcat(bsdPath
,"s0");
942 /* some CDs don't have a partition 0 */
943 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
945 bsdPath
[strlen(bsdPath
)-1] = '1';
953 IOObjectRelease( mediaIterator
);
956 open_flags
= O_BINARY
;
957 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
958 open_flags
|= O_RDWR
;
960 open_flags
|= O_RDONLY
;
964 if (flags
& BDRV_O_DIRECT
)
965 open_flags
|= O_DIRECT
;
968 s
->type
= FTYPE_FILE
;
969 #if defined(__linux__)
970 if (strstart(filename
, "/dev/cd", NULL
)) {
971 /* open will not fail even if no CD is inserted */
972 open_flags
|= O_NONBLOCK
;
974 } else if (strstart(filename
, "/dev/fd", NULL
)) {
976 s
->fd_open_flags
= open_flags
;
977 /* open will not fail even if no floppy is inserted */
978 open_flags
|= O_NONBLOCK
;
979 } else if (strstart(filename
, "/dev/sg", NULL
)) {
983 fd
= open(filename
, open_flags
, 0644);
991 #if defined(__linux__)
992 /* close fd so that we can reopen it as needed */
993 if (s
->type
== FTYPE_FD
) {
996 s
->fd_media_changed
= 1;
1002 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1004 /* Note: we do not have a reliable method to detect if the floppy is
1005 present. The current method is to try to open the floppy at every
1006 I/O and to keep it opened during a few hundreds of ms. */
1007 static int fd_open(BlockDriverState
*bs
)
1009 BDRVRawState
*s
= bs
->opaque
;
1010 int last_media_present
;
1012 if (s
->type
!= FTYPE_FD
)
1014 last_media_present
= (s
->fd
>= 0);
1016 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1020 printf("Floppy closed\n");
1024 if (s
->fd_got_error
&&
1025 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1027 printf("No floppy (open delayed)\n");
1031 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1033 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1034 s
->fd_got_error
= 1;
1035 if (last_media_present
)
1036 s
->fd_media_changed
= 1;
1038 printf("No floppy\n");
1043 printf("Floppy opened\n");
1046 if (!last_media_present
)
1047 s
->fd_media_changed
= 1;
1048 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1049 s
->fd_got_error
= 0;
1053 static int fd_open(BlockDriverState
*bs
)
1059 #if defined(__linux__)
1061 static int raw_is_inserted(BlockDriverState
*bs
)
1063 BDRVRawState
*s
= bs
->opaque
;
1068 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1069 if (ret
== CDS_DISC_OK
)
1082 /* currently only used by fdc.c, but a CD version would be good too */
1083 static int raw_media_changed(BlockDriverState
*bs
)
1085 BDRVRawState
*s
= bs
->opaque
;
1091 /* XXX: we do not have a true media changed indication. It
1092 does not work if the floppy is changed without trying
1095 ret
= s
->fd_media_changed
;
1096 s
->fd_media_changed
= 0;
1098 printf("Floppy changed=%d\n", ret
);
1107 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1109 BDRVRawState
*s
= bs
->opaque
;
1114 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1115 perror("CDROMEJECT");
1117 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1118 perror("CDROMEJECT");
1128 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1130 if (ioctl(fd
, FDEJECT
, 0) < 0)
1142 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1144 BDRVRawState
*s
= bs
->opaque
;
1148 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1149 /* Note: an error can happen if the distribution automatically
1150 mounts the CD-ROM */
1151 // perror("CDROM_LOCKDOOR");
1160 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1162 BDRVRawState
*s
= bs
->opaque
;
1164 return ioctl(s
->fd
, req
, buf
);
1168 static int raw_is_inserted(BlockDriverState
*bs
)
1173 static int raw_media_changed(BlockDriverState
*bs
)
1178 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1183 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1188 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1194 BlockDriver bdrv_host_device
= {
1196 sizeof(BDRVRawState
),
1197 NULL
, /* no probe for protocols */
1206 .bdrv_aio_read
= raw_aio_read
,
1207 .bdrv_aio_write
= raw_aio_write
,
1208 .bdrv_aio_cancel
= raw_aio_cancel
,
1209 .aiocb_size
= sizeof(RawAIOCB
),
1211 .bdrv_pread
= raw_pread
,
1212 .bdrv_pwrite
= raw_pwrite
,
1213 .bdrv_getlength
= raw_getlength
,
1215 /* removable device support */
1216 .bdrv_is_inserted
= raw_is_inserted
,
1217 .bdrv_media_changed
= raw_media_changed
,
1218 .bdrv_eject
= raw_eject
,
1219 .bdrv_set_locked
= raw_set_locked
,
1220 /* generic scsi device */
1221 .bdrv_ioctl
= raw_ioctl
,