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>
70 #include <sys/ioctl.h>
71 #include <sys/disklabel.h>
75 //#define DEBUG_FLOPPY
78 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
79 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
80 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
82 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
89 #define ALIGNED_BUFFER_SIZE (32 * 512)
91 /* if the FD is not accessed during that time (in ms), we try to
92 reopen it to see if the disk has been changed */
93 #define FD_OPEN_TIMEOUT 1000
95 typedef struct BDRVRawState
{
98 unsigned int lseek_err_cnt
;
99 #if defined(__linux__)
100 /* linux floppy specific */
102 int64_t fd_open_time
;
103 int64_t fd_error_time
;
105 int fd_media_changed
;
107 #if defined(O_DIRECT) && !defined(QEMU_IMG)
108 uint8_t* aligned_buf
;
112 static int fd_open(BlockDriverState
*bs
);
114 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
116 BDRVRawState
*s
= bs
->opaque
;
117 int fd
, open_flags
, ret
;
119 s
->lseek_err_cnt
= 0;
121 open_flags
= O_BINARY
;
122 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
123 open_flags
|= O_RDWR
;
125 open_flags
|= O_RDONLY
;
128 if (flags
& BDRV_O_CREAT
)
129 open_flags
|= O_CREAT
| O_TRUNC
;
131 if (flags
& BDRV_O_DIRECT
)
132 open_flags
|= O_DIRECT
;
135 s
->type
= FTYPE_FILE
;
137 fd
= open(filename
, open_flags
, 0644);
145 #if defined(O_DIRECT) && !defined(QEMU_IMG)
146 s
->aligned_buf
= NULL
;
147 if (flags
& BDRV_O_DIRECT
) {
148 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
149 if (s
->aligned_buf
== NULL
) {
159 /* XXX: use host sector size if necessary with:
160 #ifdef DIOCGSECTORSIZE
162 unsigned int sectorsize = 512;
163 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
164 sectorsize > bufsize)
165 bufsize = sectorsize;
169 u_int32_t blockSize = 512;
170 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
177 * offset and count are in bytes, but must be multiples of 512 for files
178 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
180 * This function may be called without alignment if the caller ensures
181 * that O_DIRECT is not in effect.
183 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
184 uint8_t *buf
, int count
)
186 BDRVRawState
*s
= bs
->opaque
;
193 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
194 ++(s
->lseek_err_cnt
);
195 if(s
->lseek_err_cnt
<= 10) {
196 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
197 "] lseek failed : %d = %s\n",
198 s
->fd
, bs
->filename
, offset
, buf
, count
,
199 bs
->total_sectors
, errno
, strerror(errno
));
205 ret
= read(s
->fd
, buf
, count
);
207 goto label__raw_read__success
;
209 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
210 "] read failed %d : %d = %s\n",
211 s
->fd
, bs
->filename
, offset
, buf
, count
,
212 bs
->total_sectors
, ret
, errno
, strerror(errno
));
214 /* Try harder for CDrom. */
215 if (bs
->type
== BDRV_TYPE_CDROM
) {
216 lseek(s
->fd
, offset
, SEEK_SET
);
217 ret
= read(s
->fd
, buf
, count
);
219 goto label__raw_read__success
;
220 lseek(s
->fd
, offset
, SEEK_SET
);
221 ret
= read(s
->fd
, buf
, count
);
223 goto label__raw_read__success
;
225 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
226 "] retry read failed %d : %d = %s\n",
227 s
->fd
, bs
->filename
, offset
, buf
, count
,
228 bs
->total_sectors
, ret
, errno
, strerror(errno
));
231 label__raw_read__success
:
237 * offset and count are in bytes, but must be multiples of 512 for files
238 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
240 * This function may be called without alignment if the caller ensures
241 * that O_DIRECT is not in effect.
243 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
244 const uint8_t *buf
, int count
)
246 BDRVRawState
*s
= bs
->opaque
;
253 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
254 ++(s
->lseek_err_cnt
);
255 if(s
->lseek_err_cnt
) {
256 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
257 PRId64
"] lseek failed : %d = %s\n",
258 s
->fd
, bs
->filename
, offset
, buf
, count
,
259 bs
->total_sectors
, errno
, strerror(errno
));
263 s
->lseek_err_cnt
= 0;
265 ret
= write(s
->fd
, buf
, count
);
267 goto label__raw_write__success
;
269 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
270 "] write failed %d : %d = %s\n",
271 s
->fd
, bs
->filename
, offset
, buf
, count
,
272 bs
->total_sectors
, ret
, errno
, strerror(errno
));
274 label__raw_write__success
:
280 #if defined(O_DIRECT) && !defined(QEMU_IMG)
282 * offset and count are in bytes and possibly not aligned. For files opened
283 * with O_DIRECT, necessary alignments are ensured before calling
284 * raw_pread_aligned to do the actual read.
286 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
287 uint8_t *buf
, int count
)
289 BDRVRawState
*s
= bs
->opaque
;
290 int size
, ret
, shift
, sum
;
294 if (s
->aligned_buf
!= NULL
) {
296 if (offset
& 0x1ff) {
297 /* align offset on a 512 bytes boundary */
299 shift
= offset
& 0x1ff;
300 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
301 if (size
> ALIGNED_BUFFER_SIZE
)
302 size
= ALIGNED_BUFFER_SIZE
;
303 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
310 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
320 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
322 /* read on aligned buffer */
326 size
= (count
+ 0x1ff) & ~0x1ff;
327 if (size
> ALIGNED_BUFFER_SIZE
)
328 size
= ALIGNED_BUFFER_SIZE
;
330 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
338 memcpy(buf
, s
->aligned_buf
, size
);
350 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
354 * offset and count are in bytes and possibly not aligned. For files opened
355 * with O_DIRECT, necessary alignments are ensured before calling
356 * raw_pwrite_aligned to do the actual write.
358 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
359 const uint8_t *buf
, int count
)
361 BDRVRawState
*s
= bs
->opaque
;
362 int size
, ret
, shift
, sum
;
366 if (s
->aligned_buf
!= NULL
) {
368 if (offset
& 0x1ff) {
369 /* align offset on a 512 bytes boundary */
370 shift
= offset
& 0x1ff;
371 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
378 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
380 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
392 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
394 while ((size
= (count
& ~0x1ff)) != 0) {
396 if (size
> ALIGNED_BUFFER_SIZE
)
397 size
= ALIGNED_BUFFER_SIZE
;
399 memcpy(s
->aligned_buf
, buf
, size
);
401 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
410 /* here, count < 512 because (count & ~0x1ff) == 0 */
412 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
415 memcpy(s
->aligned_buf
, buf
, count
);
417 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
428 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
432 #define raw_pread raw_pread_aligned
433 #define raw_pwrite raw_pwrite_aligned
438 /***********************************************************/
439 /* Unix AIO using POSIX AIO */
441 typedef struct RawAIOCB
{
442 BlockDriverAIOCB common
;
444 struct RawAIOCB
*next
;
448 static int aio_sig_num
= SIGUSR2
;
449 static RawAIOCB
*first_aio
; /* AIO issued */
450 static int aio_initialized
= 0;
452 static void aio_signal_handler(int signum
)
454 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
455 CPUState
*env
= cpu_single_env
;
457 /* stop the currently executing cpu because a timer occured */
458 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
460 if (env
->kqemu_enabled
) {
461 kqemu_cpu_interrupt(env
);
468 void qemu_aio_init(void)
470 struct sigaction act
;
474 sigfillset(&act
.sa_mask
);
475 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
476 act
.sa_handler
= aio_signal_handler
;
477 sigaction(aio_sig_num
, &act
, NULL
);
479 #if defined(__GLIBC__) && defined(__linux__)
481 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
482 seems to fix the problem. */
484 memset(&ai
, 0, sizeof(ai
));
487 ai
.aio_idle_time
= 365 * 100000;
493 void qemu_aio_poll(void)
495 RawAIOCB
*acb
, **pacb
;
504 ret
= aio_error(&acb
->aiocb
);
505 if (ret
== ECANCELED
) {
506 /* remove the request */
508 qemu_aio_release(acb
);
509 } else if (ret
!= EINPROGRESS
) {
512 ret
= aio_return(&acb
->aiocb
);
513 if (ret
== acb
->aiocb
.aio_nbytes
)
520 /* remove the request */
522 /* call the callback */
523 acb
->common
.cb(acb
->common
.opaque
, ret
);
524 qemu_aio_release(acb
);
534 /* Wait for all IO requests to complete. */
535 void qemu_aio_flush(void)
537 qemu_aio_wait_start();
545 /* wait until at least one AIO was handled */
546 static sigset_t wait_oset
;
548 void qemu_aio_wait_start(void)
552 if (!aio_initialized
)
556 qemu_kvm_aio_wait_start();
561 sigaddset(&set
, aio_sig_num
);
562 sigprocmask(SIG_BLOCK
, &set
, &wait_oset
);
565 void qemu_aio_wait(void)
570 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
580 sigaddset(&set
, aio_sig_num
);
581 sigwait(&set
, &nb_sigs
);
585 void qemu_aio_wait_end(void)
589 qemu_kvm_aio_wait_end();
593 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
596 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
597 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
598 BlockDriverCompletionFunc
*cb
, void *opaque
)
600 BDRVRawState
*s
= bs
->opaque
;
606 acb
= qemu_aio_get(bs
, cb
, opaque
);
609 acb
->aiocb
.aio_fildes
= s
->fd
;
610 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
611 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
612 acb
->aiocb
.aio_buf
= buf
;
614 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
616 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
617 acb
->aiocb
.aio_offset
= sector_num
* 512;
618 acb
->next
= first_aio
;
623 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
624 static void raw_aio_em_cb(void* opaque
)
626 RawAIOCB
*acb
= opaque
;
627 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
628 qemu_aio_release(acb
);
632 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
633 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
634 BlockDriverCompletionFunc
*cb
, void *opaque
)
639 * If O_DIRECT is used and the buffer is not aligned fall back
642 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
643 BDRVRawState
*s
= bs
->opaque
;
645 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
647 acb
= qemu_aio_get(bs
, cb
, opaque
);
648 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
649 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
650 qemu_bh_schedule(bh
);
655 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
658 if (aio_read(&acb
->aiocb
) < 0) {
659 qemu_aio_release(acb
);
665 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
666 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
667 BlockDriverCompletionFunc
*cb
, void *opaque
)
672 * If O_DIRECT is used and the buffer is not aligned fall back
675 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
676 BDRVRawState
*s
= bs
->opaque
;
678 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
680 acb
= qemu_aio_get(bs
, cb
, opaque
);
681 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
682 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
683 qemu_bh_schedule(bh
);
688 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
691 if (aio_write(&acb
->aiocb
) < 0) {
692 qemu_aio_release(acb
);
698 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
701 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
704 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
705 if (ret
== AIO_NOTCANCELED
) {
706 /* fail safe: if the aio could not be canceled, we wait for
708 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
711 /* remove the callback from the queue */
716 } else if (*pacb
== acb
) {
718 qemu_aio_release(acb
);
725 # else /* CONFIG_AIO */
727 void qemu_aio_init(void)
731 void qemu_aio_poll(void)
735 void qemu_aio_flush(void)
739 void qemu_aio_wait_start(void)
743 void qemu_aio_wait(void)
745 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
750 void qemu_aio_wait_end(void)
754 #endif /* CONFIG_AIO */
756 static void raw_close(BlockDriverState
*bs
)
758 BDRVRawState
*s
= bs
->opaque
;
762 #if defined(O_DIRECT) && !defined(QEMU_IMG)
763 if (s
->aligned_buf
!= NULL
)
764 qemu_free(s
->aligned_buf
);
769 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
771 BDRVRawState
*s
= bs
->opaque
;
772 if (s
->type
!= FTYPE_FILE
)
774 if (ftruncate(s
->fd
, offset
) < 0)
780 static int64_t raw_getlength(BlockDriverState
*bs
)
782 BDRVRawState
*s
= bs
->opaque
;
788 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
791 if (ioctl(fd
, DIOCGDINFO
, &dl
))
793 return (uint64_t)dl
.d_secsize
*
794 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
798 #else /* !__OpenBSD__ */
799 static int64_t raw_getlength(BlockDriverState
*bs
)
801 BDRVRawState
*s
= bs
->opaque
;
808 struct dk_minfo minfo
;
818 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
819 #ifdef DIOCGMEDIASIZE
820 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
823 size
= LONG_LONG_MAX
;
825 size
= lseek(fd
, 0LL, SEEK_END
);
831 * use the DKIOCGMEDIAINFO ioctl to read the size.
833 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
835 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
836 } else /* there are reports that lseek on some devices
837 fails, but irc discussion said that contingency
838 on contingency was overkill */
841 size
= lseek(fd
, 0, SEEK_END
);
847 static int raw_create(const char *filename
, int64_t total_size
,
848 const char *backing_file
, int flags
)
852 if (flags
|| backing_file
)
855 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
859 ftruncate(fd
, total_size
* 512);
864 static void raw_flush(BlockDriverState
*bs
)
866 BDRVRawState
*s
= bs
->opaque
;
870 BlockDriver bdrv_raw
= {
872 sizeof(BDRVRawState
),
873 NULL
, /* no probe for protocols */
882 .bdrv_aio_read
= raw_aio_read
,
883 .bdrv_aio_write
= raw_aio_write
,
884 .bdrv_aio_cancel
= raw_aio_cancel
,
885 .aiocb_size
= sizeof(RawAIOCB
),
887 .protocol_name
= "file",
888 .bdrv_pread
= raw_pread
,
889 .bdrv_pwrite
= raw_pwrite
,
890 .bdrv_truncate
= raw_truncate
,
891 .bdrv_getlength
= raw_getlength
,
894 /***********************************************/
898 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
899 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
901 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
903 kern_return_t kernResult
;
904 mach_port_t masterPort
;
905 CFMutableDictionaryRef classesToMatch
;
907 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
908 if ( KERN_SUCCESS
!= kernResult
) {
909 printf( "IOMasterPort returned %d\n", kernResult
);
912 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
913 if ( classesToMatch
== NULL
) {
914 printf( "IOServiceMatching returned a NULL dictionary.\n" );
916 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
918 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
919 if ( KERN_SUCCESS
!= kernResult
)
921 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
927 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
929 io_object_t nextMedia
;
930 kern_return_t kernResult
= KERN_FAILURE
;
932 nextMedia
= IOIteratorNext( mediaIterator
);
935 CFTypeRef bsdPathAsCFString
;
936 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
937 if ( bsdPathAsCFString
) {
938 size_t devPathLength
;
939 strcpy( bsdPath
, _PATH_DEV
);
940 strcat( bsdPath
, "r" );
941 devPathLength
= strlen( bsdPath
);
942 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
943 kernResult
= KERN_SUCCESS
;
945 CFRelease( bsdPathAsCFString
);
947 IOObjectRelease( nextMedia
);
955 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
957 BDRVRawState
*s
= bs
->opaque
;
958 int fd
, open_flags
, ret
;
961 if (strstart(filename
, "/dev/cdrom", NULL
)) {
962 kern_return_t kernResult
;
963 io_iterator_t mediaIterator
;
964 char bsdPath
[ MAXPATHLEN
];
967 kernResult
= FindEjectableCDMedia( &mediaIterator
);
968 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
970 if ( bsdPath
[ 0 ] != '\0' ) {
971 strcat(bsdPath
,"s0");
972 /* some CDs don't have a partition 0 */
973 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
975 bsdPath
[strlen(bsdPath
)-1] = '1';
983 IOObjectRelease( mediaIterator
);
986 open_flags
= O_BINARY
;
987 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
988 open_flags
|= O_RDWR
;
990 open_flags
|= O_RDONLY
;
994 if (flags
& BDRV_O_DIRECT
)
995 open_flags
|= O_DIRECT
;
998 s
->type
= FTYPE_FILE
;
999 #if defined(__linux__)
1000 if (strstart(filename
, "/dev/cd", NULL
)) {
1001 /* open will not fail even if no CD is inserted */
1002 open_flags
|= O_NONBLOCK
;
1004 } else if (strstart(filename
, "/dev/fd", NULL
)) {
1006 s
->fd_open_flags
= open_flags
;
1007 /* open will not fail even if no floppy is inserted */
1008 open_flags
|= O_NONBLOCK
;
1009 } else if (strstart(filename
, "/dev/sg", NULL
)) {
1013 fd
= open(filename
, open_flags
, 0644);
1021 #if defined(__linux__)
1022 /* close fd so that we can reopen it as needed */
1023 if (s
->type
== FTYPE_FD
) {
1026 s
->fd_media_changed
= 1;
1032 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1034 /* Note: we do not have a reliable method to detect if the floppy is
1035 present. The current method is to try to open the floppy at every
1036 I/O and to keep it opened during a few hundreds of ms. */
1037 static int fd_open(BlockDriverState
*bs
)
1039 BDRVRawState
*s
= bs
->opaque
;
1040 int last_media_present
;
1042 if (s
->type
!= FTYPE_FD
)
1044 last_media_present
= (s
->fd
>= 0);
1046 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1050 printf("Floppy closed\n");
1054 if (s
->fd_got_error
&&
1055 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1057 printf("No floppy (open delayed)\n");
1061 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1063 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1064 s
->fd_got_error
= 1;
1065 if (last_media_present
)
1066 s
->fd_media_changed
= 1;
1068 printf("No floppy\n");
1073 printf("Floppy opened\n");
1076 if (!last_media_present
)
1077 s
->fd_media_changed
= 1;
1078 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1079 s
->fd_got_error
= 0;
1083 static int fd_open(BlockDriverState
*bs
)
1089 #if defined(__linux__)
1091 static int raw_is_inserted(BlockDriverState
*bs
)
1093 BDRVRawState
*s
= bs
->opaque
;
1098 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1099 if (ret
== CDS_DISC_OK
)
1112 /* currently only used by fdc.c, but a CD version would be good too */
1113 static int raw_media_changed(BlockDriverState
*bs
)
1115 BDRVRawState
*s
= bs
->opaque
;
1121 /* XXX: we do not have a true media changed indication. It
1122 does not work if the floppy is changed without trying
1125 ret
= s
->fd_media_changed
;
1126 s
->fd_media_changed
= 0;
1128 printf("Floppy changed=%d\n", ret
);
1137 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1139 BDRVRawState
*s
= bs
->opaque
;
1144 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1145 perror("CDROMEJECT");
1147 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1148 perror("CDROMEJECT");
1158 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1160 if (ioctl(fd
, FDEJECT
, 0) < 0)
1172 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1174 BDRVRawState
*s
= bs
->opaque
;
1178 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1179 /* Note: an error can happen if the distribution automatically
1180 mounts the CD-ROM */
1181 // perror("CDROM_LOCKDOOR");
1190 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1192 BDRVRawState
*s
= bs
->opaque
;
1194 return ioctl(s
->fd
, req
, buf
);
1198 static int raw_is_inserted(BlockDriverState
*bs
)
1203 static int raw_media_changed(BlockDriverState
*bs
)
1208 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1213 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1218 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1224 BlockDriver bdrv_host_device
= {
1226 sizeof(BDRVRawState
),
1227 NULL
, /* no probe for protocols */
1236 .bdrv_aio_read
= raw_aio_read
,
1237 .bdrv_aio_write
= raw_aio_write
,
1238 .bdrv_aio_cancel
= raw_aio_cancel
,
1239 .aiocb_size
= sizeof(RawAIOCB
),
1241 .bdrv_pread
= raw_pread
,
1242 .bdrv_pwrite
= raw_pwrite
,
1243 .bdrv_getlength
= raw_getlength
,
1245 /* removable device support */
1246 .bdrv_is_inserted
= raw_is_inserted
,
1247 .bdrv_media_changed
= raw_media_changed
,
1248 .bdrv_eject
= raw_eject
,
1249 .bdrv_set_locked
= raw_set_locked
,
1250 /* generic scsi device */
1251 .bdrv_ioctl
= raw_ioctl
,