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"
27 #include "qemu-timer.h"
30 #include "block_int.h"
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
47 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
60 //#define DEBUG_FLOPPY
63 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG)
64 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
65 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
67 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
74 #define ALIGNED_BUFFER_SIZE (32 * 512)
76 /* if the FD is not accessed during that time (in ms), we try to
77 reopen it to see if the disk has been changed */
78 #define FD_OPEN_TIMEOUT 1000
80 typedef struct BDRVRawState
{
83 unsigned int lseek_err_cnt
;
84 #if defined(__linux__)
85 /* linux floppy specific */
88 int64_t fd_error_time
;
92 #if defined(O_DIRECT) && !defined(QEMU_IMG)
97 static int fd_open(BlockDriverState
*bs
);
99 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
101 BDRVRawState
*s
= bs
->opaque
;
102 int fd
, open_flags
, ret
;
104 s
->lseek_err_cnt
= 0;
106 open_flags
= O_BINARY
;
107 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
108 open_flags
|= O_RDWR
;
110 open_flags
|= O_RDONLY
;
113 if (flags
& BDRV_O_CREAT
)
114 open_flags
|= O_CREAT
| O_TRUNC
;
116 if (flags
& BDRV_O_DIRECT
)
117 open_flags
|= O_DIRECT
;
120 s
->type
= FTYPE_FILE
;
122 fd
= open(filename
, open_flags
, 0644);
130 #if defined(O_DIRECT) && !defined(QEMU_IMG)
131 s
->aligned_buf
= NULL
;
132 if (flags
& BDRV_O_DIRECT
) {
133 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
134 if (s
->aligned_buf
== NULL
) {
144 /* XXX: use host sector size if necessary with:
145 #ifdef DIOCGSECTORSIZE
147 unsigned int sectorsize = 512;
148 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
149 sectorsize > bufsize)
150 bufsize = sectorsize;
154 u_int32_t blockSize = 512;
155 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
162 * offset and count are in bytes, but must be multiples of 512 for files
163 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
165 * This function may be called without alignment if the caller ensures
166 * that O_DIRECT is not in effect.
168 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
169 uint8_t *buf
, int count
)
171 BDRVRawState
*s
= bs
->opaque
;
178 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
179 ++(s
->lseek_err_cnt
);
180 if(s
->lseek_err_cnt
<= 10) {
181 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
182 "] lseek failed : %d = %s\n",
183 s
->fd
, bs
->filename
, offset
, buf
, count
,
184 bs
->total_sectors
, errno
, strerror(errno
));
190 ret
= read(s
->fd
, buf
, count
);
192 goto label__raw_read__success
;
194 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
195 "] read failed %d : %d = %s\n",
196 s
->fd
, bs
->filename
, offset
, buf
, count
,
197 bs
->total_sectors
, ret
, errno
, strerror(errno
));
199 /* Try harder for CDrom. */
200 if (bs
->type
== BDRV_TYPE_CDROM
) {
201 lseek(s
->fd
, offset
, SEEK_SET
);
202 ret
= read(s
->fd
, buf
, count
);
204 goto label__raw_read__success
;
205 lseek(s
->fd
, offset
, SEEK_SET
);
206 ret
= read(s
->fd
, buf
, count
);
208 goto label__raw_read__success
;
210 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
211 "] retry read failed %d : %d = %s\n",
212 s
->fd
, bs
->filename
, offset
, buf
, count
,
213 bs
->total_sectors
, ret
, errno
, strerror(errno
));
216 label__raw_read__success
:
222 * offset and count are in bytes, but must be multiples of 512 for files
223 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
225 * This function may be called without alignment if the caller ensures
226 * that O_DIRECT is not in effect.
228 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
229 const uint8_t *buf
, int count
)
231 BDRVRawState
*s
= bs
->opaque
;
238 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
239 ++(s
->lseek_err_cnt
);
240 if(s
->lseek_err_cnt
) {
241 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
242 PRId64
"] lseek failed : %d = %s\n",
243 s
->fd
, bs
->filename
, offset
, buf
, count
,
244 bs
->total_sectors
, errno
, strerror(errno
));
248 s
->lseek_err_cnt
= 0;
250 ret
= write(s
->fd
, buf
, count
);
252 goto label__raw_write__success
;
254 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
255 "] write failed %d : %d = %s\n",
256 s
->fd
, bs
->filename
, offset
, buf
, count
,
257 bs
->total_sectors
, ret
, errno
, strerror(errno
));
259 label__raw_write__success
:
265 #if defined(O_DIRECT) && !defined(QEMU_IMG)
267 * offset and count are in bytes and possibly not aligned. For files opened
268 * with O_DIRECT, necessary alignments are ensured before calling
269 * raw_pread_aligned to do the actual read.
271 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
272 uint8_t *buf
, int count
)
274 BDRVRawState
*s
= bs
->opaque
;
275 int size
, ret
, shift
, sum
;
279 if (s
->aligned_buf
!= NULL
) {
281 if (offset
& 0x1ff) {
282 /* align offset on a 512 bytes boundary */
284 shift
= offset
& 0x1ff;
285 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
286 if (size
> ALIGNED_BUFFER_SIZE
)
287 size
= ALIGNED_BUFFER_SIZE
;
288 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
295 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
305 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
307 /* read on aligned buffer */
311 size
= (count
+ 0x1ff) & ~0x1ff;
312 if (size
> ALIGNED_BUFFER_SIZE
)
313 size
= ALIGNED_BUFFER_SIZE
;
315 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
323 memcpy(buf
, s
->aligned_buf
, size
);
335 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
339 * offset and count are in bytes and possibly not aligned. For files opened
340 * with O_DIRECT, necessary alignments are ensured before calling
341 * raw_pwrite_aligned to do the actual write.
343 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
344 const uint8_t *buf
, int count
)
346 BDRVRawState
*s
= bs
->opaque
;
347 int size
, ret
, shift
, sum
;
351 if (s
->aligned_buf
!= NULL
) {
353 if (offset
& 0x1ff) {
354 /* align offset on a 512 bytes boundary */
355 shift
= offset
& 0x1ff;
356 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
363 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
365 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
377 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
379 while ((size
= (count
& ~0x1ff)) != 0) {
381 if (size
> ALIGNED_BUFFER_SIZE
)
382 size
= ALIGNED_BUFFER_SIZE
;
384 memcpy(s
->aligned_buf
, buf
, size
);
386 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
395 /* here, count < 512 because (count & ~0x1ff) == 0 */
397 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
400 memcpy(s
->aligned_buf
, buf
, count
);
402 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
413 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
417 #define raw_pread raw_pread_aligned
418 #define raw_pwrite raw_pwrite_aligned
422 /***********************************************************/
423 /* Unix AIO using POSIX AIO */
425 typedef struct RawAIOCB
{
426 BlockDriverAIOCB common
;
428 struct RawAIOCB
*next
;
432 static int aio_sig_num
= SIGUSR2
;
433 static RawAIOCB
*first_aio
; /* AIO issued */
434 static int aio_initialized
= 0;
436 static void aio_signal_handler(int signum
)
439 CPUState
*env
= cpu_single_env
;
441 /* stop the currently executing cpu because a timer occured */
442 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
444 if (env
->kqemu_enabled
) {
445 kqemu_cpu_interrupt(env
);
452 void qemu_aio_init(void)
454 struct sigaction act
;
458 sigfillset(&act
.sa_mask
);
459 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
460 act
.sa_handler
= aio_signal_handler
;
461 sigaction(aio_sig_num
, &act
, NULL
);
463 #if defined(__GLIBC__) && defined(__linux__)
465 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
466 seems to fix the problem. */
468 memset(&ai
, 0, sizeof(ai
));
471 ai
.aio_idle_time
= 365 * 100000;
477 void qemu_aio_poll(void)
479 RawAIOCB
*acb
, **pacb
;
488 ret
= aio_error(&acb
->aiocb
);
489 if (ret
== ECANCELED
) {
490 /* remove the request */
492 qemu_aio_release(acb
);
493 } else if (ret
!= EINPROGRESS
) {
496 ret
= aio_return(&acb
->aiocb
);
497 if (ret
== acb
->aiocb
.aio_nbytes
)
504 /* remove the request */
506 /* call the callback */
507 acb
->common
.cb(acb
->common
.opaque
, ret
);
508 qemu_aio_release(acb
);
518 /* Wait for all IO requests to complete. */
519 void qemu_aio_flush(void)
521 qemu_aio_wait_start();
529 /* wait until at least one AIO was handled */
530 static sigset_t wait_oset
;
532 void qemu_aio_wait_start(void)
536 if (!aio_initialized
)
540 qemu_kvm_aio_wait_start();
545 sigaddset(&set
, aio_sig_num
);
546 sigprocmask(SIG_BLOCK
, &set
, &wait_oset
);
549 void qemu_aio_wait(void)
564 sigaddset(&set
, aio_sig_num
);
565 sigwait(&set
, &nb_sigs
);
569 void qemu_aio_wait_end(void)
573 qemu_kvm_aio_wait_end();
577 sigprocmask(SIG_SETMASK
, &wait_oset
, NULL
);
580 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
581 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
582 BlockDriverCompletionFunc
*cb
, void *opaque
)
584 BDRVRawState
*s
= bs
->opaque
;
590 acb
= qemu_aio_get(bs
, cb
, opaque
);
593 acb
->aiocb
.aio_fildes
= s
->fd
;
594 acb
->aiocb
.aio_sigevent
.sigev_signo
= aio_sig_num
;
595 acb
->aiocb
.aio_sigevent
.sigev_notify
= SIGEV_SIGNAL
;
596 acb
->aiocb
.aio_buf
= buf
;
598 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
600 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
601 acb
->aiocb
.aio_offset
= sector_num
* 512;
602 acb
->next
= first_aio
;
608 static void raw_aio_em_cb(void* opaque
)
610 RawAIOCB
*acb
= opaque
;
611 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
612 qemu_aio_release(acb
);
616 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
617 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
618 BlockDriverCompletionFunc
*cb
, void *opaque
)
623 * If O_DIRECT is used and the buffer is not aligned fall back
626 #if defined(O_DIRECT) && !defined(QEMU_IMG)
627 BDRVRawState
*s
= bs
->opaque
;
629 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
631 acb
= qemu_aio_get(bs
, cb
, opaque
);
632 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
633 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
634 qemu_bh_schedule(bh
);
639 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
642 if (aio_read(&acb
->aiocb
) < 0) {
643 qemu_aio_release(acb
);
649 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
650 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
651 BlockDriverCompletionFunc
*cb
, void *opaque
)
656 * If O_DIRECT is used and the buffer is not aligned fall back
659 #if defined(O_DIRECT) && !defined(QEMU_IMG)
660 BDRVRawState
*s
= bs
->opaque
;
662 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
664 acb
= qemu_aio_get(bs
, cb
, opaque
);
665 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
666 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
667 qemu_bh_schedule(bh
);
672 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
675 if (aio_write(&acb
->aiocb
) < 0) {
676 qemu_aio_release(acb
);
682 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
685 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
688 ret
= aio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
689 if (ret
== AIO_NOTCANCELED
) {
690 /* fail safe: if the aio could not be canceled, we wait for
692 while (aio_error(&acb
->aiocb
) == EINPROGRESS
);
695 /* remove the callback from the queue */
700 } else if (*pacb
== acb
) {
702 qemu_aio_release(acb
);
709 static void raw_close(BlockDriverState
*bs
)
711 BDRVRawState
*s
= bs
->opaque
;
715 #if defined(O_DIRECT) && !defined(QEMU_IMG)
716 if (s
->aligned_buf
!= NULL
)
717 qemu_free(s
->aligned_buf
);
722 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
724 BDRVRawState
*s
= bs
->opaque
;
725 if (s
->type
!= FTYPE_FILE
)
727 if (ftruncate(s
->fd
, offset
) < 0)
732 static int64_t raw_getlength(BlockDriverState
*bs
)
734 BDRVRawState
*s
= bs
->opaque
;
741 struct dk_minfo minfo
;
751 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
752 #ifdef DIOCGMEDIASIZE
753 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
756 size
= LONG_LONG_MAX
;
758 size
= lseek(fd
, 0LL, SEEK_END
);
764 * use the DKIOCGMEDIAINFO ioctl to read the size.
766 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
768 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
769 } else /* there are reports that lseek on some devices
770 fails, but irc discussion said that contingency
771 on contingency was overkill */
774 size
= lseek(fd
, 0, SEEK_END
);
779 static int raw_create(const char *filename
, int64_t total_size
,
780 const char *backing_file
, int flags
)
784 if (flags
|| backing_file
)
787 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
791 ftruncate(fd
, total_size
* 512);
796 static void raw_flush(BlockDriverState
*bs
)
798 BDRVRawState
*s
= bs
->opaque
;
802 BlockDriver bdrv_raw
= {
804 sizeof(BDRVRawState
),
805 NULL
, /* no probe for protocols */
813 .bdrv_aio_read
= raw_aio_read
,
814 .bdrv_aio_write
= raw_aio_write
,
815 .bdrv_aio_cancel
= raw_aio_cancel
,
816 .aiocb_size
= sizeof(RawAIOCB
),
817 .protocol_name
= "file",
818 .bdrv_pread
= raw_pread
,
819 .bdrv_pwrite
= raw_pwrite
,
820 .bdrv_truncate
= raw_truncate
,
821 .bdrv_getlength
= raw_getlength
,
824 /***********************************************/
828 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
829 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
831 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
833 kern_return_t kernResult
;
834 mach_port_t masterPort
;
835 CFMutableDictionaryRef classesToMatch
;
837 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
838 if ( KERN_SUCCESS
!= kernResult
) {
839 printf( "IOMasterPort returned %d\n", kernResult
);
842 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
843 if ( classesToMatch
== NULL
) {
844 printf( "IOServiceMatching returned a NULL dictionary.\n" );
846 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
848 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
849 if ( KERN_SUCCESS
!= kernResult
)
851 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
857 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
859 io_object_t nextMedia
;
860 kern_return_t kernResult
= KERN_FAILURE
;
862 nextMedia
= IOIteratorNext( mediaIterator
);
865 CFTypeRef bsdPathAsCFString
;
866 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
867 if ( bsdPathAsCFString
) {
868 size_t devPathLength
;
869 strcpy( bsdPath
, _PATH_DEV
);
870 strcat( bsdPath
, "r" );
871 devPathLength
= strlen( bsdPath
);
872 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
873 kernResult
= KERN_SUCCESS
;
875 CFRelease( bsdPathAsCFString
);
877 IOObjectRelease( nextMedia
);
885 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
887 BDRVRawState
*s
= bs
->opaque
;
888 int fd
, open_flags
, ret
;
891 if (strstart(filename
, "/dev/cdrom", NULL
)) {
892 kern_return_t kernResult
;
893 io_iterator_t mediaIterator
;
894 char bsdPath
[ MAXPATHLEN
];
897 kernResult
= FindEjectableCDMedia( &mediaIterator
);
898 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
900 if ( bsdPath
[ 0 ] != '\0' ) {
901 strcat(bsdPath
,"s0");
902 /* some CDs don't have a partition 0 */
903 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
905 bsdPath
[strlen(bsdPath
)-1] = '1';
913 IOObjectRelease( mediaIterator
);
916 open_flags
= O_BINARY
;
917 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
918 open_flags
|= O_RDWR
;
920 open_flags
|= O_RDONLY
;
924 if (flags
& BDRV_O_DIRECT
)
925 open_flags
|= O_DIRECT
;
928 s
->type
= FTYPE_FILE
;
929 #if defined(__linux__)
930 if (strstart(filename
, "/dev/cd", NULL
)) {
931 /* open will not fail even if no CD is inserted */
932 open_flags
|= O_NONBLOCK
;
934 } else if (strstart(filename
, "/dev/fd", NULL
)) {
936 s
->fd_open_flags
= open_flags
;
937 /* open will not fail even if no floppy is inserted */
938 open_flags
|= O_NONBLOCK
;
939 } else if (strstart(filename
, "/dev/sg", NULL
)) {
943 fd
= open(filename
, open_flags
, 0644);
951 #if defined(__linux__)
952 /* close fd so that we can reopen it as needed */
953 if (s
->type
== FTYPE_FD
) {
956 s
->fd_media_changed
= 1;
962 #if defined(__linux__) && !defined(QEMU_IMG)
964 /* Note: we do not have a reliable method to detect if the floppy is
965 present. The current method is to try to open the floppy at every
966 I/O and to keep it opened during a few hundreds of ms. */
967 static int fd_open(BlockDriverState
*bs
)
969 BDRVRawState
*s
= bs
->opaque
;
970 int last_media_present
;
972 if (s
->type
!= FTYPE_FD
)
974 last_media_present
= (s
->fd
>= 0);
976 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
980 printf("Floppy closed\n");
984 if (s
->fd_got_error
&&
985 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
987 printf("No floppy (open delayed)\n");
991 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
993 s
->fd_error_time
= qemu_get_clock(rt_clock
);
995 if (last_media_present
)
996 s
->fd_media_changed
= 1;
998 printf("No floppy\n");
1003 printf("Floppy opened\n");
1006 if (!last_media_present
)
1007 s
->fd_media_changed
= 1;
1008 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1009 s
->fd_got_error
= 0;
1013 static int fd_open(BlockDriverState
*bs
)
1019 #if defined(__linux__)
1021 static int raw_is_inserted(BlockDriverState
*bs
)
1023 BDRVRawState
*s
= bs
->opaque
;
1028 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1029 if (ret
== CDS_DISC_OK
)
1042 /* currently only used by fdc.c, but a CD version would be good too */
1043 static int raw_media_changed(BlockDriverState
*bs
)
1045 BDRVRawState
*s
= bs
->opaque
;
1051 /* XXX: we do not have a true media changed indication. It
1052 does not work if the floppy is changed without trying
1055 ret
= s
->fd_media_changed
;
1056 s
->fd_media_changed
= 0;
1058 printf("Floppy changed=%d\n", ret
);
1067 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1069 BDRVRawState
*s
= bs
->opaque
;
1074 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1075 perror("CDROMEJECT");
1077 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1078 perror("CDROMEJECT");
1088 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1090 if (ioctl(fd
, FDEJECT
, 0) < 0)
1102 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1104 BDRVRawState
*s
= bs
->opaque
;
1108 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1109 /* Note: an error can happen if the distribution automatically
1110 mounts the CD-ROM */
1111 // perror("CDROM_LOCKDOOR");
1120 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1122 BDRVRawState
*s
= bs
->opaque
;
1124 return ioctl(s
->fd
, req
, buf
);
1128 static int raw_is_inserted(BlockDriverState
*bs
)
1133 static int raw_media_changed(BlockDriverState
*bs
)
1138 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1143 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1148 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1154 BlockDriver bdrv_host_device
= {
1156 sizeof(BDRVRawState
),
1157 NULL
, /* no probe for protocols */
1165 .bdrv_aio_read
= raw_aio_read
,
1166 .bdrv_aio_write
= raw_aio_write
,
1167 .bdrv_aio_cancel
= raw_aio_cancel
,
1168 .aiocb_size
= sizeof(RawAIOCB
),
1169 .bdrv_pread
= raw_pread
,
1170 .bdrv_pwrite
= raw_pwrite
,
1171 .bdrv_getlength
= raw_getlength
,
1173 /* removable device support */
1174 .bdrv_is_inserted
= raw_is_inserted
,
1175 .bdrv_media_changed
= raw_media_changed
,
1176 .bdrv_eject
= raw_eject
,
1177 .bdrv_set_locked
= raw_set_locked
,
1178 /* generic scsi device */
1179 .bdrv_ioctl
= raw_ioctl
,