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)
27 #include "qemu-timer.h"
30 #define kvm_enabled() 0
31 #define qemu_kvm_aio_start() ((void)0)
32 #define qemu_kvm_aio_end() ((void)0)
33 #define qemu_kvm_aio_wait() ((void)0)
34 #define qemu_kvm_aio_poll() ((void)0)
36 #include "block_int.h"
44 #include <sys/param.h>
45 #include <IOKit/IOKitLib.h>
46 #include <IOKit/IOBSD.h>
47 #include <IOKit/storage/IOMediaBSDClient.h>
48 #include <IOKit/storage/IOMedia.h>
49 #include <IOKit/storage/IOCDMedia.h>
50 //#include <IOKit/storage/IOCDTypes.h>
51 #include <CoreFoundation/CoreFoundation.h>
55 #define _POSIX_PTHREAD_SEMANTICS 1
60 #include <sys/ioctl.h>
61 #include <linux/cdrom.h>
69 #include <sys/ioctl.h>
70 #include <sys/disklabel.h>
74 //#define DEBUG_FLOPPY
77 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
78 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
79 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
81 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
88 #define ALIGNED_BUFFER_SIZE (32 * 512)
90 /* if the FD is not accessed during that time (in ms), we try to
91 reopen it to see if the disk has been changed */
92 #define FD_OPEN_TIMEOUT 1000
94 typedef struct BDRVRawState
{
97 unsigned int lseek_err_cnt
;
98 #if defined(__linux__)
99 /* linux floppy specific */
101 int64_t fd_open_time
;
102 int64_t fd_error_time
;
104 int fd_media_changed
;
106 #if defined(O_DIRECT) && !defined(QEMU_IMG)
107 uint8_t* aligned_buf
;
111 static int fd_open(BlockDriverState
*bs
);
113 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
115 BDRVRawState
*s
= bs
->opaque
;
116 int fd
, open_flags
, ret
;
118 s
->lseek_err_cnt
= 0;
120 open_flags
= O_BINARY
;
121 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
122 open_flags
|= O_RDWR
;
124 open_flags
|= O_RDONLY
;
127 if (flags
& BDRV_O_CREAT
)
128 open_flags
|= O_CREAT
| O_TRUNC
;
130 if (flags
& BDRV_O_DIRECT
)
131 open_flags
|= O_DIRECT
;
134 s
->type
= FTYPE_FILE
;
136 fd
= open(filename
, open_flags
, 0644);
144 #if defined(O_DIRECT) && !defined(QEMU_IMG)
145 s
->aligned_buf
= NULL
;
146 if (flags
& BDRV_O_DIRECT
) {
147 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
148 if (s
->aligned_buf
== NULL
) {
158 /* XXX: use host sector size if necessary with:
159 #ifdef DIOCGSECTORSIZE
161 unsigned int sectorsize = 512;
162 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
163 sectorsize > bufsize)
164 bufsize = sectorsize;
168 u_int32_t blockSize = 512;
169 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
176 * offset and count are in bytes, but must be multiples of 512 for files
177 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
179 * This function may be called without alignment if the caller ensures
180 * that O_DIRECT is not in effect.
182 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
183 uint8_t *buf
, int count
)
185 BDRVRawState
*s
= bs
->opaque
;
192 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
193 ++(s
->lseek_err_cnt
);
194 if(s
->lseek_err_cnt
<= 10) {
195 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
196 "] lseek failed : %d = %s\n",
197 s
->fd
, bs
->filename
, offset
, buf
, count
,
198 bs
->total_sectors
, errno
, strerror(errno
));
204 ret
= read(s
->fd
, buf
, count
);
206 goto label__raw_read__success
;
208 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
209 "] read failed %d : %d = %s\n",
210 s
->fd
, bs
->filename
, offset
, buf
, count
,
211 bs
->total_sectors
, ret
, errno
, strerror(errno
));
213 /* Try harder for CDrom. */
214 if (bs
->type
== BDRV_TYPE_CDROM
) {
215 lseek(s
->fd
, offset
, SEEK_SET
);
216 ret
= read(s
->fd
, buf
, count
);
218 goto label__raw_read__success
;
219 lseek(s
->fd
, offset
, SEEK_SET
);
220 ret
= read(s
->fd
, buf
, count
);
222 goto label__raw_read__success
;
224 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
225 "] retry read failed %d : %d = %s\n",
226 s
->fd
, bs
->filename
, offset
, buf
, count
,
227 bs
->total_sectors
, ret
, errno
, strerror(errno
));
230 label__raw_read__success
:
236 * offset and count are in bytes, but must be multiples of 512 for files
237 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
239 * This function may be called without alignment if the caller ensures
240 * that O_DIRECT is not in effect.
242 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
243 const uint8_t *buf
, int count
)
245 BDRVRawState
*s
= bs
->opaque
;
252 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
253 ++(s
->lseek_err_cnt
);
254 if(s
->lseek_err_cnt
) {
255 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
256 PRId64
"] lseek failed : %d = %s\n",
257 s
->fd
, bs
->filename
, offset
, buf
, count
,
258 bs
->total_sectors
, errno
, strerror(errno
));
262 s
->lseek_err_cnt
= 0;
264 ret
= write(s
->fd
, buf
, count
);
266 goto label__raw_write__success
;
268 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
269 "] write failed %d : %d = %s\n",
270 s
->fd
, bs
->filename
, offset
, buf
, count
,
271 bs
->total_sectors
, ret
, errno
, strerror(errno
));
273 label__raw_write__success
:
279 #if defined(O_DIRECT) && !defined(QEMU_IMG)
281 * offset and count are in bytes and possibly not aligned. For files opened
282 * with O_DIRECT, necessary alignments are ensured before calling
283 * raw_pread_aligned to do the actual read.
285 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
286 uint8_t *buf
, int count
)
288 BDRVRawState
*s
= bs
->opaque
;
289 int size
, ret
, shift
, sum
;
293 if (s
->aligned_buf
!= NULL
) {
295 if (offset
& 0x1ff) {
296 /* align offset on a 512 bytes boundary */
298 shift
= offset
& 0x1ff;
299 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
300 if (size
> ALIGNED_BUFFER_SIZE
)
301 size
= ALIGNED_BUFFER_SIZE
;
302 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
309 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
319 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
321 /* read on aligned buffer */
325 size
= (count
+ 0x1ff) & ~0x1ff;
326 if (size
> ALIGNED_BUFFER_SIZE
)
327 size
= ALIGNED_BUFFER_SIZE
;
329 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
337 memcpy(buf
, s
->aligned_buf
, size
);
349 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
353 * offset and count are in bytes and possibly not aligned. For files opened
354 * with O_DIRECT, necessary alignments are ensured before calling
355 * raw_pwrite_aligned to do the actual write.
357 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
358 const uint8_t *buf
, int count
)
360 BDRVRawState
*s
= bs
->opaque
;
361 int size
, ret
, shift
, sum
;
365 if (s
->aligned_buf
!= NULL
) {
367 if (offset
& 0x1ff) {
368 /* align offset on a 512 bytes boundary */
369 shift
= offset
& 0x1ff;
370 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
377 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
379 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
391 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
393 while ((size
= (count
& ~0x1ff)) != 0) {
395 if (size
> ALIGNED_BUFFER_SIZE
)
396 size
= ALIGNED_BUFFER_SIZE
;
398 memcpy(s
->aligned_buf
, buf
, size
);
400 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
409 /* here, count < 512 because (count & ~0x1ff) == 0 */
411 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
414 memcpy(s
->aligned_buf
, buf
, count
);
416 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
427 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
431 #define raw_pread raw_pread_aligned
432 #define raw_pwrite raw_pwrite_aligned
437 /***********************************************************/
438 /* Unix AIO using POSIX AIO */
440 typedef struct RawAIOCB
{
441 BlockDriverAIOCB common
;
443 struct RawAIOCB
*next
;
447 static int aio_sig_num
= SIGUSR2
;
448 static RawAIOCB
*first_aio
; /* AIO issued */
449 static int aio_initialized
= 0;
451 static void aio_signal_handler(int signum
)
453 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
454 CPUState
*env
= cpu_single_env
;
456 /* stop the currently executing cpu because a timer occured */
457 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
459 if (env
->kqemu_enabled
) {
460 kqemu_cpu_interrupt(env
);
467 void qemu_aio_init(void)
469 struct sigaction act
;
473 sigfillset(&act
.sa_mask
);
474 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
475 act
.sa_handler
= aio_signal_handler
;
476 sigaction(aio_sig_num
, &act
, NULL
);
478 #if defined(__GLIBC__) && defined(__linux__)
480 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
481 seems to fix the problem. */
483 memset(&ai
, 0, sizeof(ai
));
486 ai
.aio_idle_time
= 365 * 100000;
492 void qemu_aio_poll(void)
494 RawAIOCB
*acb
, **pacb
;
503 ret
= aio_error(&acb
->aiocb
);
504 if (ret
== ECANCELED
) {
505 /* remove the request */
507 qemu_aio_release(acb
);
508 } else if (ret
!= EINPROGRESS
) {
511 ret
= aio_return(&acb
->aiocb
);
512 if (ret
== acb
->aiocb
.aio_nbytes
)
519 /* remove the request */
521 /* call the callback */
522 acb
->common
.cb(acb
->common
.opaque
, ret
);
523 qemu_aio_release(acb
);
533 /* Wait for all IO requests to complete. */
534 void qemu_aio_flush(void)
536 qemu_aio_wait_start();
544 /* wait until at least one AIO was handled */
545 static sigset_t wait_oset
;
547 void qemu_aio_wait_start(void)
551 if (!aio_initialized
)
555 qemu_kvm_aio_wait_start();
560 sigaddset(&set
, aio_sig_num
);
561 sigprocmask(SIG_BLOCK
, &set
, &wait_oset
);
564 void qemu_aio_wait(void)
569 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
579 sigaddset(&set
, aio_sig_num
);
580 sigwait(&set
, &nb_sigs
);
584 void qemu_aio_wait_end(void)
588 qemu_kvm_aio_wait_end();
592 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
595 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
596 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
597 BlockDriverCompletionFunc
*cb
, void *opaque
)
599 BDRVRawState
*s
= bs
->opaque
;
605 acb
= qemu_aio_get(bs
, cb
, opaque
);
608 acb
->aiocb
.aio_fildes
= s
->fd
;
609 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
610 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
611 acb
->aiocb
.aio_buf
= buf
;
613 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
615 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
616 acb
->aiocb
.aio_offset
= sector_num
* 512;
617 acb
->next
= first_aio
;
622 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
623 static void raw_aio_em_cb(void* opaque
)
625 RawAIOCB
*acb
= opaque
;
626 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
627 qemu_aio_release(acb
);
631 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
632 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
633 BlockDriverCompletionFunc
*cb
, void *opaque
)
638 * If O_DIRECT is used and the buffer is not aligned fall back
641 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
642 BDRVRawState
*s
= bs
->opaque
;
644 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
646 acb
= qemu_aio_get(bs
, cb
, opaque
);
647 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
648 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
649 qemu_bh_schedule(bh
);
654 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
657 if (aio_read(&acb
->aiocb
) < 0) {
658 qemu_aio_release(acb
);
664 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
665 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
666 BlockDriverCompletionFunc
*cb
, void *opaque
)
671 * If O_DIRECT is used and the buffer is not aligned fall back
674 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
675 BDRVRawState
*s
= bs
->opaque
;
677 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
679 acb
= qemu_aio_get(bs
, cb
, opaque
);
680 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
681 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
682 qemu_bh_schedule(bh
);
687 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
690 if (aio_write(&acb
->aiocb
) < 0) {
691 qemu_aio_release(acb
);
697 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
700 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
703 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
704 if (ret
== AIO_NOTCANCELED
) {
705 /* fail safe: if the aio could not be canceled, we wait for
707 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
710 /* remove the callback from the queue */
715 } else if (*pacb
== acb
) {
717 qemu_aio_release(acb
);
724 # else /* CONFIG_AIO */
726 void qemu_aio_init(void)
730 void qemu_aio_poll(void)
734 void qemu_aio_flush(void)
738 void qemu_aio_wait_start(void)
742 void qemu_aio_wait(void)
744 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
749 void qemu_aio_wait_end(void)
753 #endif /* CONFIG_AIO */
755 static void raw_close(BlockDriverState
*bs
)
757 BDRVRawState
*s
= bs
->opaque
;
761 #if defined(O_DIRECT) && !defined(QEMU_IMG)
762 if (s
->aligned_buf
!= NULL
)
763 qemu_free(s
->aligned_buf
);
768 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
770 BDRVRawState
*s
= bs
->opaque
;
771 if (s
->type
!= FTYPE_FILE
)
773 if (ftruncate(s
->fd
, offset
) < 0)
779 static int64_t raw_getlength(BlockDriverState
*bs
)
781 BDRVRawState
*s
= bs
->opaque
;
787 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
790 if (ioctl(fd
, DIOCGDINFO
, &dl
))
792 return (uint64_t)dl
.d_secsize
*
793 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
797 #else /* !__OpenBSD__ */
798 static int64_t raw_getlength(BlockDriverState
*bs
)
800 BDRVRawState
*s
= bs
->opaque
;
807 struct dk_minfo minfo
;
817 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
818 #ifdef DIOCGMEDIASIZE
819 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
822 size
= LONG_LONG_MAX
;
824 size
= lseek(fd
, 0LL, SEEK_END
);
830 * use the DKIOCGMEDIAINFO ioctl to read the size.
832 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
834 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
835 } else /* there are reports that lseek on some devices
836 fails, but irc discussion said that contingency
837 on contingency was overkill */
840 size
= lseek(fd
, 0, SEEK_END
);
846 static int raw_create(const char *filename
, int64_t total_size
,
847 const char *backing_file
, int flags
)
851 if (flags
|| backing_file
)
854 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
858 ftruncate(fd
, total_size
* 512);
863 static void raw_flush(BlockDriverState
*bs
)
865 BDRVRawState
*s
= bs
->opaque
;
869 BlockDriver bdrv_raw
= {
871 sizeof(BDRVRawState
),
872 NULL
, /* no probe for protocols */
881 .bdrv_aio_read
= raw_aio_read
,
882 .bdrv_aio_write
= raw_aio_write
,
883 .bdrv_aio_cancel
= raw_aio_cancel
,
884 .aiocb_size
= sizeof(RawAIOCB
),
886 .protocol_name
= "file",
887 .bdrv_pread
= raw_pread
,
888 .bdrv_pwrite
= raw_pwrite
,
889 .bdrv_truncate
= raw_truncate
,
890 .bdrv_getlength
= raw_getlength
,
893 /***********************************************/
897 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
898 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
900 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
902 kern_return_t kernResult
;
903 mach_port_t masterPort
;
904 CFMutableDictionaryRef classesToMatch
;
906 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
907 if ( KERN_SUCCESS
!= kernResult
) {
908 printf( "IOMasterPort returned %d\n", kernResult
);
911 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
912 if ( classesToMatch
== NULL
) {
913 printf( "IOServiceMatching returned a NULL dictionary.\n" );
915 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
917 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
918 if ( KERN_SUCCESS
!= kernResult
)
920 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
926 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
928 io_object_t nextMedia
;
929 kern_return_t kernResult
= KERN_FAILURE
;
931 nextMedia
= IOIteratorNext( mediaIterator
);
934 CFTypeRef bsdPathAsCFString
;
935 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
936 if ( bsdPathAsCFString
) {
937 size_t devPathLength
;
938 strcpy( bsdPath
, _PATH_DEV
);
939 strcat( bsdPath
, "r" );
940 devPathLength
= strlen( bsdPath
);
941 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
942 kernResult
= KERN_SUCCESS
;
944 CFRelease( bsdPathAsCFString
);
946 IOObjectRelease( nextMedia
);
954 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
956 BDRVRawState
*s
= bs
->opaque
;
957 int fd
, open_flags
, ret
;
960 if (strstart(filename
, "/dev/cdrom", NULL
)) {
961 kern_return_t kernResult
;
962 io_iterator_t mediaIterator
;
963 char bsdPath
[ MAXPATHLEN
];
966 kernResult
= FindEjectableCDMedia( &mediaIterator
);
967 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
969 if ( bsdPath
[ 0 ] != '\0' ) {
970 strcat(bsdPath
,"s0");
971 /* some CDs don't have a partition 0 */
972 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
974 bsdPath
[strlen(bsdPath
)-1] = '1';
982 IOObjectRelease( mediaIterator
);
985 open_flags
= O_BINARY
;
986 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
987 open_flags
|= O_RDWR
;
989 open_flags
|= O_RDONLY
;
993 if (flags
& BDRV_O_DIRECT
)
994 open_flags
|= O_DIRECT
;
997 s
->type
= FTYPE_FILE
;
998 #if defined(__linux__)
999 if (strstart(filename
, "/dev/cd", NULL
)) {
1000 /* open will not fail even if no CD is inserted */
1001 open_flags
|= O_NONBLOCK
;
1003 } else if (strstart(filename
, "/dev/fd", NULL
)) {
1005 s
->fd_open_flags
= open_flags
;
1006 /* open will not fail even if no floppy is inserted */
1007 open_flags
|= O_NONBLOCK
;
1008 } else if (strstart(filename
, "/dev/sg", NULL
)) {
1012 fd
= open(filename
, open_flags
, 0644);
1020 #if defined(__linux__)
1021 /* close fd so that we can reopen it as needed */
1022 if (s
->type
== FTYPE_FD
) {
1025 s
->fd_media_changed
= 1;
1031 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1033 /* Note: we do not have a reliable method to detect if the floppy is
1034 present. The current method is to try to open the floppy at every
1035 I/O and to keep it opened during a few hundreds of ms. */
1036 static int fd_open(BlockDriverState
*bs
)
1038 BDRVRawState
*s
= bs
->opaque
;
1039 int last_media_present
;
1041 if (s
->type
!= FTYPE_FD
)
1043 last_media_present
= (s
->fd
>= 0);
1045 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1049 printf("Floppy closed\n");
1053 if (s
->fd_got_error
&&
1054 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1056 printf("No floppy (open delayed)\n");
1060 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1062 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1063 s
->fd_got_error
= 1;
1064 if (last_media_present
)
1065 s
->fd_media_changed
= 1;
1067 printf("No floppy\n");
1072 printf("Floppy opened\n");
1075 if (!last_media_present
)
1076 s
->fd_media_changed
= 1;
1077 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1078 s
->fd_got_error
= 0;
1082 static int fd_open(BlockDriverState
*bs
)
1088 #if defined(__linux__)
1090 static int raw_is_inserted(BlockDriverState
*bs
)
1092 BDRVRawState
*s
= bs
->opaque
;
1097 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1098 if (ret
== CDS_DISC_OK
)
1111 /* currently only used by fdc.c, but a CD version would be good too */
1112 static int raw_media_changed(BlockDriverState
*bs
)
1114 BDRVRawState
*s
= bs
->opaque
;
1120 /* XXX: we do not have a true media changed indication. It
1121 does not work if the floppy is changed without trying
1124 ret
= s
->fd_media_changed
;
1125 s
->fd_media_changed
= 0;
1127 printf("Floppy changed=%d\n", ret
);
1136 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1138 BDRVRawState
*s
= bs
->opaque
;
1143 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1144 perror("CDROMEJECT");
1146 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1147 perror("CDROMEJECT");
1157 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1159 if (ioctl(fd
, FDEJECT
, 0) < 0)
1171 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1173 BDRVRawState
*s
= bs
->opaque
;
1177 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1178 /* Note: an error can happen if the distribution automatically
1179 mounts the CD-ROM */
1180 // perror("CDROM_LOCKDOOR");
1189 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1191 BDRVRawState
*s
= bs
->opaque
;
1193 return ioctl(s
->fd
, req
, buf
);
1197 static int raw_is_inserted(BlockDriverState
*bs
)
1202 static int raw_media_changed(BlockDriverState
*bs
)
1207 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1212 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1217 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1223 BlockDriver bdrv_host_device
= {
1225 sizeof(BDRVRawState
),
1226 NULL
, /* no probe for protocols */
1235 .bdrv_aio_read
= raw_aio_read
,
1236 .bdrv_aio_write
= raw_aio_write
,
1237 .bdrv_aio_cancel
= raw_aio_cancel
,
1238 .aiocb_size
= sizeof(RawAIOCB
),
1240 .bdrv_pread
= raw_pread
,
1241 .bdrv_pwrite
= raw_pwrite
,
1242 .bdrv_getlength
= raw_getlength
,
1244 /* removable device support */
1245 .bdrv_is_inserted
= raw_is_inserted
,
1246 .bdrv_media_changed
= raw_media_changed
,
1247 .bdrv_eject
= raw_eject
,
1248 .bdrv_set_locked
= raw_set_locked
,
1249 /* generic scsi device */
1250 .bdrv_ioctl
= raw_ioctl
,