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 #include "qemu-timer.h"
26 #include "qemu-char.h"
27 #include "block_int.h"
30 #include "posix-aio-compat.h"
35 #include <sys/param.h>
36 #include <IOKit/IOKitLib.h>
37 #include <IOKit/IOBSD.h>
38 #include <IOKit/storage/IOMediaBSDClient.h>
39 #include <IOKit/storage/IOMedia.h>
40 #include <IOKit/storage/IOCDMedia.h>
41 //#include <IOKit/storage/IOCDTypes.h>
42 #include <CoreFoundation/CoreFoundation.h>
46 #define _POSIX_PTHREAD_SEMANTICS 1
51 #include <sys/ioctl.h>
52 #include <linux/cdrom.h>
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
66 //#define DEBUG_FLOPPY
69 #if defined(DEBUG_BLOCK)
70 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled()) \
71 { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0)
73 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
76 /* OS X does not have O_DSYNC */
78 #define O_DSYNC O_SYNC
81 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
83 #define O_DIRECT O_DSYNC
90 #define ALIGNED_BUFFER_SIZE (32 * 512)
92 /* if the FD is not accessed during that time (in ms), we try to
93 reopen it to see if the disk has been changed */
94 #define FD_OPEN_TIMEOUT 1000
96 typedef struct BDRVRawState
{
99 unsigned int lseek_err_cnt
;
100 #if defined(__linux__)
101 /* linux floppy specific */
103 int64_t fd_open_time
;
104 int64_t fd_error_time
;
106 int fd_media_changed
;
108 uint8_t* aligned_buf
;
111 static int posix_aio_init(void);
113 static int fd_open(BlockDriverState
*bs
);
115 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
117 BDRVRawState
*s
= bs
->opaque
;
118 int fd
, open_flags
, ret
;
122 s
->lseek_err_cnt
= 0;
124 open_flags
= O_BINARY
;
125 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
126 open_flags
|= O_RDWR
;
128 open_flags
|= O_RDONLY
;
131 if (flags
& BDRV_O_CREAT
)
132 open_flags
|= O_CREAT
| O_TRUNC
;
134 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
135 * and O_DIRECT for no caching. */
136 if ((flags
& BDRV_O_NOCACHE
))
137 open_flags
|= O_DIRECT
;
138 else if (!(flags
& BDRV_O_CACHE_WB
))
139 open_flags
|= O_DSYNC
;
141 s
->type
= FTYPE_FILE
;
143 fd
= open(filename
, open_flags
, 0644);
151 s
->aligned_buf
= NULL
;
152 if ((flags
& BDRV_O_NOCACHE
)) {
153 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
154 if (s
->aligned_buf
== NULL
) {
163 /* XXX: use host sector size if necessary with:
164 #ifdef DIOCGSECTORSIZE
166 unsigned int sectorsize = 512;
167 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
168 sectorsize > bufsize)
169 bufsize = sectorsize;
173 u_int32_t blockSize = 512;
174 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
181 * offset and count are in bytes, but must be multiples of 512 for files
182 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
184 * This function may be called without alignment if the caller ensures
185 * that O_DIRECT is not in effect.
187 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
188 uint8_t *buf
, int count
)
190 BDRVRawState
*s
= bs
->opaque
;
197 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
198 ++(s
->lseek_err_cnt
);
199 if(s
->lseek_err_cnt
<= 10) {
200 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
201 "] lseek failed : %d = %s\n",
202 s
->fd
, bs
->filename
, offset
, buf
, count
,
203 bs
->total_sectors
, errno
, strerror(errno
));
209 ret
= read(s
->fd
, buf
, count
);
211 goto label__raw_read__success
;
213 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
214 "] read failed %d : %d = %s\n",
215 s
->fd
, bs
->filename
, offset
, buf
, count
,
216 bs
->total_sectors
, ret
, errno
, strerror(errno
));
218 /* Try harder for CDrom. */
219 if (bs
->type
== BDRV_TYPE_CDROM
) {
220 lseek(s
->fd
, offset
, SEEK_SET
);
221 ret
= read(s
->fd
, buf
, count
);
223 goto label__raw_read__success
;
224 lseek(s
->fd
, offset
, SEEK_SET
);
225 ret
= read(s
->fd
, buf
, count
);
227 goto label__raw_read__success
;
229 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
230 "] retry read failed %d : %d = %s\n",
231 s
->fd
, bs
->filename
, offset
, buf
, count
,
232 bs
->total_sectors
, ret
, errno
, strerror(errno
));
235 label__raw_read__success
:
241 * offset and count are in bytes, but must be multiples of 512 for files
242 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
244 * This function may be called without alignment if the caller ensures
245 * that O_DIRECT is not in effect.
247 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
248 const uint8_t *buf
, int count
)
250 BDRVRawState
*s
= bs
->opaque
;
257 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
258 ++(s
->lseek_err_cnt
);
259 if(s
->lseek_err_cnt
) {
260 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
261 PRId64
"] lseek failed : %d = %s\n",
262 s
->fd
, bs
->filename
, offset
, buf
, count
,
263 bs
->total_sectors
, errno
, strerror(errno
));
267 s
->lseek_err_cnt
= 0;
269 ret
= write(s
->fd
, buf
, count
);
271 goto label__raw_write__success
;
273 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
274 "] write failed %d : %d = %s\n",
275 s
->fd
, bs
->filename
, offset
, buf
, count
,
276 bs
->total_sectors
, ret
, errno
, strerror(errno
));
278 label__raw_write__success
:
280 return (ret
< 0) ? -errno
: ret
;
285 * offset and count are in bytes and possibly not aligned. For files opened
286 * with O_DIRECT, necessary alignments are ensured before calling
287 * raw_pread_aligned to do the actual read.
289 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
290 uint8_t *buf
, int count
)
292 BDRVRawState
*s
= bs
->opaque
;
293 int size
, ret
, shift
, sum
;
297 if (s
->aligned_buf
!= NULL
) {
299 if (offset
& 0x1ff) {
300 /* align offset on a 512 bytes boundary */
302 shift
= offset
& 0x1ff;
303 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
304 if (size
> ALIGNED_BUFFER_SIZE
)
305 size
= ALIGNED_BUFFER_SIZE
;
306 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
313 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
323 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
325 /* read on aligned buffer */
329 size
= (count
+ 0x1ff) & ~0x1ff;
330 if (size
> ALIGNED_BUFFER_SIZE
)
331 size
= ALIGNED_BUFFER_SIZE
;
333 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
341 memcpy(buf
, s
->aligned_buf
, size
);
353 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
357 * offset and count are in bytes and possibly not aligned. For files opened
358 * with O_DIRECT, necessary alignments are ensured before calling
359 * raw_pwrite_aligned to do the actual write.
361 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
362 const uint8_t *buf
, int count
)
364 BDRVRawState
*s
= bs
->opaque
;
365 int size
, ret
, shift
, sum
;
369 if (s
->aligned_buf
!= NULL
) {
371 if (offset
& 0x1ff) {
372 /* align offset on a 512 bytes boundary */
373 shift
= offset
& 0x1ff;
374 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
381 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
383 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
395 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
397 while ((size
= (count
& ~0x1ff)) != 0) {
399 if (size
> ALIGNED_BUFFER_SIZE
)
400 size
= ALIGNED_BUFFER_SIZE
;
402 memcpy(s
->aligned_buf
, buf
, size
);
404 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
413 /* here, count < 512 because (count & ~0x1ff) == 0 */
415 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
418 memcpy(s
->aligned_buf
, buf
, count
);
420 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
431 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
435 /***********************************************************/
436 /* Unix AIO using POSIX AIO */
438 typedef struct RawAIOCB
{
439 BlockDriverAIOCB common
;
440 struct qemu_paiocb aiocb
;
441 struct RawAIOCB
*next
;
445 typedef struct PosixAioState
451 static void posix_aio_read(void *opaque
)
453 PosixAioState
*s
= opaque
;
454 RawAIOCB
*acb
, **pacb
;
458 /* read all bytes from signal pipe */
462 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
463 if (len
== -1 && errno
== EINTR
)
464 continue; /* try again */
465 if (len
== sizeof(bytes
))
466 continue; /* more to read */
471 pacb
= &s
->first_aio
;
476 ret
= qemu_paio_error(&acb
->aiocb
);
477 if (ret
== ECANCELED
) {
478 /* remove the request */
480 qemu_aio_release(acb
);
481 } else if (ret
!= EINPROGRESS
) {
484 ret
= qemu_paio_return(&acb
->aiocb
);
485 if (ret
== acb
->aiocb
.aio_nbytes
)
492 /* remove the request */
494 /* call the callback */
495 acb
->common
.cb(acb
->common
.opaque
, ret
);
496 qemu_aio_release(acb
);
506 static int posix_aio_flush(void *opaque
)
508 PosixAioState
*s
= opaque
;
509 return !!s
->first_aio
;
512 static PosixAioState
*posix_aio_state
;
514 static void aio_signal_handler(int signum
)
516 if (posix_aio_state
) {
519 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
525 static int posix_aio_init(void)
527 struct sigaction act
;
530 struct qemu_paioinit ai
;
535 s
= qemu_malloc(sizeof(PosixAioState
));
537 sigfillset(&act
.sa_mask
);
538 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
539 act
.sa_handler
= aio_signal_handler
;
540 sigaction(SIGUSR2
, &act
, NULL
);
543 if (pipe(fds
) == -1) {
544 fprintf(stderr
, "failed to create pipe\n");
551 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
552 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
554 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
556 memset(&ai
, 0, sizeof(ai
));
566 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
567 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
568 BlockDriverCompletionFunc
*cb
, void *opaque
)
570 BDRVRawState
*s
= bs
->opaque
;
576 acb
= qemu_aio_get(bs
, cb
, opaque
);
579 acb
->aiocb
.aio_fildes
= s
->fd
;
580 acb
->aiocb
.ev_signo
= SIGUSR2
;
581 acb
->aiocb
.aio_buf
= buf
;
583 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
585 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
586 acb
->aiocb
.aio_offset
= sector_num
* 512;
587 acb
->next
= posix_aio_state
->first_aio
;
588 posix_aio_state
->first_aio
= acb
;
592 static void raw_aio_em_cb(void* opaque
)
594 RawAIOCB
*acb
= opaque
;
595 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
596 qemu_aio_release(acb
);
599 static void raw_aio_remove(RawAIOCB
*acb
)
603 /* remove the callback from the queue */
604 pacb
= &posix_aio_state
->first_aio
;
608 } else if (*pacb
== acb
) {
610 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 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
);
638 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
641 if (qemu_paio_read(&acb
->aiocb
) < 0) {
648 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
649 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
650 BlockDriverCompletionFunc
*cb
, void *opaque
)
655 * If O_DIRECT is used and the buffer is not aligned fall back
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
);
669 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
672 if (qemu_paio_write(&acb
->aiocb
) < 0) {
679 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
682 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
684 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
685 if (ret
== QEMU_PAIO_NOTCANCELED
) {
686 /* fail safe: if the aio could not be canceled, we wait for
688 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
693 #else /* CONFIG_AIO */
694 static int posix_aio_init(void)
698 #endif /* CONFIG_AIO */
701 static void raw_close(BlockDriverState
*bs
)
703 BDRVRawState
*s
= bs
->opaque
;
707 if (s
->aligned_buf
!= NULL
)
708 qemu_free(s
->aligned_buf
);
712 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
714 BDRVRawState
*s
= bs
->opaque
;
715 if (s
->type
!= FTYPE_FILE
)
717 if (ftruncate(s
->fd
, offset
) < 0)
723 static int64_t raw_getlength(BlockDriverState
*bs
)
725 BDRVRawState
*s
= bs
->opaque
;
731 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
734 if (ioctl(fd
, DIOCGDINFO
, &dl
))
736 return (uint64_t)dl
.d_secsize
*
737 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
741 #else /* !__OpenBSD__ */
742 static int64_t raw_getlength(BlockDriverState
*bs
)
744 BDRVRawState
*s
= bs
->opaque
;
751 struct dk_minfo minfo
;
761 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
762 #ifdef DIOCGMEDIASIZE
763 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
766 size
= LONG_LONG_MAX
;
768 size
= lseek(fd
, 0LL, SEEK_END
);
774 * use the DKIOCGMEDIAINFO ioctl to read the size.
776 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
778 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
779 } else /* there are reports that lseek on some devices
780 fails, but irc discussion said that contingency
781 on contingency was overkill */
784 size
= lseek(fd
, 0, SEEK_END
);
790 static int raw_create(const char *filename
, int64_t total_size
,
791 const char *backing_file
, int flags
)
795 if (flags
|| backing_file
)
798 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
802 ftruncate(fd
, total_size
* 512);
807 static void raw_flush(BlockDriverState
*bs
)
809 BDRVRawState
*s
= bs
->opaque
;
813 BlockDriver bdrv_raw
= {
815 sizeof(BDRVRawState
),
816 NULL
, /* no probe for protocols */
825 .bdrv_aio_read
= raw_aio_read
,
826 .bdrv_aio_write
= raw_aio_write
,
827 .bdrv_aio_cancel
= raw_aio_cancel
,
828 .aiocb_size
= sizeof(RawAIOCB
),
831 .bdrv_pread
= raw_pread
,
832 .bdrv_pwrite
= raw_pwrite
,
833 .bdrv_truncate
= raw_truncate
,
834 .bdrv_getlength
= raw_getlength
,
837 /***********************************************/
841 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
842 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
844 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
846 kern_return_t kernResult
;
847 mach_port_t masterPort
;
848 CFMutableDictionaryRef classesToMatch
;
850 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
851 if ( KERN_SUCCESS
!= kernResult
) {
852 printf( "IOMasterPort returned %d\n", kernResult
);
855 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
856 if ( classesToMatch
== NULL
) {
857 printf( "IOServiceMatching returned a NULL dictionary.\n" );
859 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
861 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
862 if ( KERN_SUCCESS
!= kernResult
)
864 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
870 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
872 io_object_t nextMedia
;
873 kern_return_t kernResult
= KERN_FAILURE
;
875 nextMedia
= IOIteratorNext( mediaIterator
);
878 CFTypeRef bsdPathAsCFString
;
879 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
880 if ( bsdPathAsCFString
) {
881 size_t devPathLength
;
882 strcpy( bsdPath
, _PATH_DEV
);
883 strcat( bsdPath
, "r" );
884 devPathLength
= strlen( bsdPath
);
885 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
886 kernResult
= KERN_SUCCESS
;
888 CFRelease( bsdPathAsCFString
);
890 IOObjectRelease( nextMedia
);
898 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
900 BDRVRawState
*s
= bs
->opaque
;
901 int fd
, open_flags
, ret
;
906 if (strstart(filename
, "/dev/cdrom", NULL
)) {
907 kern_return_t kernResult
;
908 io_iterator_t mediaIterator
;
909 char bsdPath
[ MAXPATHLEN
];
912 kernResult
= FindEjectableCDMedia( &mediaIterator
);
913 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
915 if ( bsdPath
[ 0 ] != '\0' ) {
916 strcat(bsdPath
,"s0");
917 /* some CDs don't have a partition 0 */
918 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
920 bsdPath
[strlen(bsdPath
)-1] = '1';
928 IOObjectRelease( mediaIterator
);
931 open_flags
= O_BINARY
;
932 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
933 open_flags
|= O_RDWR
;
935 open_flags
|= O_RDONLY
;
938 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
939 * and O_DIRECT for no caching. */
940 if ((flags
& BDRV_O_NOCACHE
))
941 open_flags
|= O_DIRECT
;
942 else if (!(flags
& BDRV_O_CACHE_WB
))
943 open_flags
|= O_DSYNC
;
945 s
->type
= FTYPE_FILE
;
946 #if defined(__linux__)
947 if (strstart(filename
, "/dev/cd", NULL
)) {
948 /* open will not fail even if no CD is inserted */
949 open_flags
|= O_NONBLOCK
;
951 } else if (strstart(filename
, "/dev/fd", NULL
)) {
953 s
->fd_open_flags
= open_flags
;
954 /* open will not fail even if no floppy is inserted */
955 open_flags
|= O_NONBLOCK
;
956 } else if (strstart(filename
, "/dev/sg", NULL
)) {
960 fd
= open(filename
, open_flags
, 0644);
968 #if defined(__linux__)
969 /* close fd so that we can reopen it as needed */
970 if (s
->type
== FTYPE_FD
) {
973 s
->fd_media_changed
= 1;
979 #if defined(__linux__)
980 /* Note: we do not have a reliable method to detect if the floppy is
981 present. The current method is to try to open the floppy at every
982 I/O and to keep it opened during a few hundreds of ms. */
983 static int fd_open(BlockDriverState
*bs
)
985 BDRVRawState
*s
= bs
->opaque
;
986 int last_media_present
;
988 if (s
->type
!= FTYPE_FD
)
990 last_media_present
= (s
->fd
>= 0);
992 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
996 printf("Floppy closed\n");
1000 if (s
->fd_got_error
&&
1001 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1003 printf("No floppy (open delayed)\n");
1007 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1009 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1010 s
->fd_got_error
= 1;
1011 if (last_media_present
)
1012 s
->fd_media_changed
= 1;
1014 printf("No floppy\n");
1019 printf("Floppy opened\n");
1022 if (!last_media_present
)
1023 s
->fd_media_changed
= 1;
1024 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1025 s
->fd_got_error
= 0;
1029 static int raw_is_inserted(BlockDriverState
*bs
)
1031 BDRVRawState
*s
= bs
->opaque
;
1036 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1037 if (ret
== CDS_DISC_OK
)
1050 /* currently only used by fdc.c, but a CD version would be good too */
1051 static int raw_media_changed(BlockDriverState
*bs
)
1053 BDRVRawState
*s
= bs
->opaque
;
1059 /* XXX: we do not have a true media changed indication. It
1060 does not work if the floppy is changed without trying
1063 ret
= s
->fd_media_changed
;
1064 s
->fd_media_changed
= 0;
1066 printf("Floppy changed=%d\n", ret
);
1075 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1077 BDRVRawState
*s
= bs
->opaque
;
1082 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1083 perror("CDROMEJECT");
1085 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1086 perror("CDROMEJECT");
1096 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1098 if (ioctl(fd
, FDEJECT
, 0) < 0)
1110 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1112 BDRVRawState
*s
= bs
->opaque
;
1116 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1117 /* Note: an error can happen if the distribution automatically
1118 mounts the CD-ROM */
1119 // perror("CDROM_LOCKDOOR");
1128 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1130 BDRVRawState
*s
= bs
->opaque
;
1132 return ioctl(s
->fd
, req
, buf
);
1136 static int fd_open(BlockDriverState
*bs
)
1141 static int raw_is_inserted(BlockDriverState
*bs
)
1146 static int raw_media_changed(BlockDriverState
*bs
)
1151 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1156 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1161 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1167 BlockDriver bdrv_host_device
= {
1169 sizeof(BDRVRawState
),
1170 NULL
, /* no probe for protocols */
1179 .bdrv_aio_read
= raw_aio_read
,
1180 .bdrv_aio_write
= raw_aio_write
,
1181 .bdrv_aio_cancel
= raw_aio_cancel
,
1182 .aiocb_size
= sizeof(RawAIOCB
),
1185 .bdrv_pread
= raw_pread
,
1186 .bdrv_pwrite
= raw_pwrite
,
1187 .bdrv_getlength
= raw_getlength
,
1189 /* removable device support */
1190 .bdrv_is_inserted
= raw_is_inserted
,
1191 .bdrv_media_changed
= raw_media_changed
,
1192 .bdrv_eject
= raw_eject
,
1193 .bdrv_set_locked
= raw_set_locked
,
1194 /* generic scsi device */
1195 .bdrv_ioctl
= raw_ioctl
,