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"
31 #include "posix-aio-compat.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>
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
67 //#define DEBUG_FLOPPY
70 #if defined(DEBUG_BLOCK)
71 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled()) \
72 { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0)
74 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
77 /* OS X does not have O_DSYNC */
79 #define O_DSYNC O_SYNC
82 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
84 #define O_DIRECT O_DSYNC
91 #define ALIGNED_BUFFER_SIZE (32 * 512)
93 /* if the FD is not accessed during that time (in ms), we try to
94 reopen it to see if the disk has been changed */
95 #define FD_OPEN_TIMEOUT 1000
97 typedef struct BDRVRawState
{
100 unsigned int lseek_err_cnt
;
101 #if defined(__linux__)
102 /* linux floppy specific */
104 int64_t fd_open_time
;
105 int64_t fd_error_time
;
107 int fd_media_changed
;
109 uint8_t* aligned_buf
;
112 static int posix_aio_init(void);
114 static int fd_open(BlockDriverState
*bs
);
116 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
118 BDRVRawState
*s
= bs
->opaque
;
119 int fd
, open_flags
, ret
;
123 s
->lseek_err_cnt
= 0;
125 open_flags
= O_BINARY
;
126 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
127 open_flags
|= O_RDWR
;
129 open_flags
|= O_RDONLY
;
132 if (flags
& BDRV_O_CREAT
)
133 open_flags
|= O_CREAT
| O_TRUNC
;
135 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
136 * and O_DIRECT for no caching. */
137 if ((flags
& BDRV_O_NOCACHE
))
138 open_flags
|= O_DIRECT
;
139 else if (!(flags
& BDRV_O_CACHE_WB
))
140 open_flags
|= O_DSYNC
;
142 s
->type
= FTYPE_FILE
;
144 fd
= open(filename
, open_flags
, 0644);
152 s
->aligned_buf
= NULL
;
153 if ((flags
& BDRV_O_NOCACHE
)) {
154 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
155 if (s
->aligned_buf
== NULL
) {
164 /* XXX: use host sector size if necessary with:
165 #ifdef DIOCGSECTORSIZE
167 unsigned int sectorsize = 512;
168 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
169 sectorsize > bufsize)
170 bufsize = sectorsize;
174 u_int32_t blockSize = 512;
175 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
182 * offset and count are in bytes, but must be multiples of 512 for files
183 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
185 * This function may be called without alignment if the caller ensures
186 * that O_DIRECT is not in effect.
188 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
189 uint8_t *buf
, int count
)
191 BDRVRawState
*s
= bs
->opaque
;
198 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
199 ++(s
->lseek_err_cnt
);
200 if(s
->lseek_err_cnt
<= 10) {
201 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
202 "] lseek failed : %d = %s\n",
203 s
->fd
, bs
->filename
, offset
, buf
, count
,
204 bs
->total_sectors
, errno
, strerror(errno
));
210 ret
= read(s
->fd
, buf
, count
);
212 goto label__raw_read__success
;
214 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
215 "] read failed %d : %d = %s\n",
216 s
->fd
, bs
->filename
, offset
, buf
, count
,
217 bs
->total_sectors
, ret
, errno
, strerror(errno
));
219 /* Try harder for CDrom. */
220 if (bs
->type
== BDRV_TYPE_CDROM
) {
221 lseek(s
->fd
, offset
, SEEK_SET
);
222 ret
= read(s
->fd
, buf
, count
);
224 goto label__raw_read__success
;
225 lseek(s
->fd
, offset
, SEEK_SET
);
226 ret
= read(s
->fd
, buf
, count
);
228 goto label__raw_read__success
;
230 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
231 "] retry read failed %d : %d = %s\n",
232 s
->fd
, bs
->filename
, offset
, buf
, count
,
233 bs
->total_sectors
, ret
, errno
, strerror(errno
));
236 label__raw_read__success
:
242 * offset and count are in bytes, but must be multiples of 512 for files
243 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
245 * This function may be called without alignment if the caller ensures
246 * that O_DIRECT is not in effect.
248 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
249 const uint8_t *buf
, int count
)
251 BDRVRawState
*s
= bs
->opaque
;
258 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
259 ++(s
->lseek_err_cnt
);
260 if(s
->lseek_err_cnt
) {
261 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
262 PRId64
"] lseek failed : %d = %s\n",
263 s
->fd
, bs
->filename
, offset
, buf
, count
,
264 bs
->total_sectors
, errno
, strerror(errno
));
268 s
->lseek_err_cnt
= 0;
270 ret
= write(s
->fd
, buf
, count
);
272 goto label__raw_write__success
;
274 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
275 "] write failed %d : %d = %s\n",
276 s
->fd
, bs
->filename
, offset
, buf
, count
,
277 bs
->total_sectors
, ret
, errno
, strerror(errno
));
279 label__raw_write__success
:
281 return (ret
< 0) ? -errno
: ret
;
286 * offset and count are in bytes and possibly not aligned. For files opened
287 * with O_DIRECT, necessary alignments are ensured before calling
288 * raw_pread_aligned to do the actual read.
290 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
291 uint8_t *buf
, int count
)
293 BDRVRawState
*s
= bs
->opaque
;
294 int size
, ret
, shift
, sum
;
298 if (s
->aligned_buf
!= NULL
) {
300 if (offset
& 0x1ff) {
301 /* align offset on a 512 bytes boundary */
303 shift
= offset
& 0x1ff;
304 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
305 if (size
> ALIGNED_BUFFER_SIZE
)
306 size
= ALIGNED_BUFFER_SIZE
;
307 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
314 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
324 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
326 /* read on aligned buffer */
330 size
= (count
+ 0x1ff) & ~0x1ff;
331 if (size
> ALIGNED_BUFFER_SIZE
)
332 size
= ALIGNED_BUFFER_SIZE
;
334 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
342 memcpy(buf
, s
->aligned_buf
, size
);
354 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
358 * offset and count are in bytes and possibly not aligned. For files opened
359 * with O_DIRECT, necessary alignments are ensured before calling
360 * raw_pwrite_aligned to do the actual write.
362 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
363 const uint8_t *buf
, int count
)
365 BDRVRawState
*s
= bs
->opaque
;
366 int size
, ret
, shift
, sum
;
370 if (s
->aligned_buf
!= NULL
) {
372 if (offset
& 0x1ff) {
373 /* align offset on a 512 bytes boundary */
374 shift
= offset
& 0x1ff;
375 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
382 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
384 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
396 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
398 while ((size
= (count
& ~0x1ff)) != 0) {
400 if (size
> ALIGNED_BUFFER_SIZE
)
401 size
= ALIGNED_BUFFER_SIZE
;
403 memcpy(s
->aligned_buf
, buf
, size
);
405 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
414 /* here, count < 512 because (count & ~0x1ff) == 0 */
416 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
419 memcpy(s
->aligned_buf
, buf
, count
);
421 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
432 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
436 /***********************************************************/
437 /* Unix AIO using POSIX AIO */
439 typedef struct RawAIOCB
{
440 BlockDriverAIOCB common
;
441 struct qemu_paiocb aiocb
;
442 struct RawAIOCB
*next
;
446 typedef struct PosixAioState
452 static void posix_aio_read(void *opaque
)
454 PosixAioState
*s
= opaque
;
455 RawAIOCB
*acb
, **pacb
;
459 struct qemu_signalfd_siginfo siginfo
;
463 /* try to read from signalfd, don't freak out if we can't read anything */
465 while (offset
< 128) {
468 len
= read(s
->fd
, sig
.buf
+ offset
, 128 - offset
);
469 if (len
== -1 && errno
== EINTR
)
471 if (len
== -1 && errno
== EAGAIN
) {
472 /* there is no natural reason for this to happen,
473 * so we'll spin hard until we get everything just
474 * to be on the safe side. */
483 pacb
= &s
->first_aio
;
488 ret
= qemu_paio_error(&acb
->aiocb
);
489 if (ret
== ECANCELED
) {
490 /* remove the request */
492 qemu_aio_release(acb
);
493 } else if (ret
!= EINPROGRESS
) {
496 ret
= qemu_paio_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 static int posix_aio_flush(void *opaque
)
520 PosixAioState
*s
= opaque
;
521 return !!s
->first_aio
;
524 static PosixAioState
*posix_aio_state
;
526 static int posix_aio_init(void)
530 struct qemu_paioinit ai
;
535 s
= qemu_malloc(sizeof(PosixAioState
));
537 /* Make sure to block AIO signal */
539 sigaddset(&mask
, SIGUSR2
);
540 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
543 s
->fd
= qemu_signalfd(&mask
);
545 fprintf(stderr
, "failed to create signalfd\n");
549 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
551 qemu_aio_set_fd_handler(s
->fd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
553 memset(&ai
, 0, sizeof(ai
));
563 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
,
564 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
565 BlockDriverCompletionFunc
*cb
, void *opaque
)
567 BDRVRawState
*s
= bs
->opaque
;
573 acb
= qemu_aio_get(bs
, cb
, opaque
);
576 acb
->aiocb
.aio_fildes
= s
->fd
;
577 acb
->aiocb
.ev_signo
= SIGUSR2
;
578 acb
->aiocb
.aio_buf
= buf
;
580 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
582 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
583 acb
->aiocb
.aio_offset
= sector_num
* 512;
584 acb
->next
= posix_aio_state
->first_aio
;
585 posix_aio_state
->first_aio
= acb
;
589 static void raw_aio_em_cb(void* opaque
)
591 RawAIOCB
*acb
= opaque
;
592 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
593 qemu_aio_release(acb
);
596 static void raw_aio_remove(RawAIOCB
*acb
)
600 /* remove the callback from the queue */
601 pacb
= &posix_aio_state
->first_aio
;
604 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
606 } else if (*pacb
== acb
) {
608 qemu_aio_release(acb
);
611 pacb
= &(*pacb
)->next
;
615 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
616 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
617 BlockDriverCompletionFunc
*cb
, void *opaque
)
622 * If O_DIRECT is used and the buffer is not aligned fall back
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
);
636 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
639 if (qemu_paio_read(&acb
->aiocb
) < 0) {
646 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
647 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
648 BlockDriverCompletionFunc
*cb
, void *opaque
)
653 * If O_DIRECT is used and the buffer is not aligned fall back
656 BDRVRawState
*s
= bs
->opaque
;
658 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
660 acb
= qemu_aio_get(bs
, cb
, opaque
);
661 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
662 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
663 qemu_bh_schedule(bh
);
667 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
670 if (qemu_paio_write(&acb
->aiocb
) < 0) {
677 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
680 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
682 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
683 if (ret
== QEMU_PAIO_NOTCANCELED
) {
684 /* fail safe: if the aio could not be canceled, we wait for
686 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
691 #else /* CONFIG_AIO */
692 static int posix_aio_init(void)
696 #endif /* CONFIG_AIO */
699 static void raw_close(BlockDriverState
*bs
)
701 BDRVRawState
*s
= bs
->opaque
;
705 if (s
->aligned_buf
!= NULL
)
706 qemu_free(s
->aligned_buf
);
710 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
712 BDRVRawState
*s
= bs
->opaque
;
713 if (s
->type
!= FTYPE_FILE
)
715 if (ftruncate(s
->fd
, offset
) < 0)
721 static int64_t raw_getlength(BlockDriverState
*bs
)
723 BDRVRawState
*s
= bs
->opaque
;
729 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
732 if (ioctl(fd
, DIOCGDINFO
, &dl
))
734 return (uint64_t)dl
.d_secsize
*
735 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
739 #else /* !__OpenBSD__ */
740 static int64_t raw_getlength(BlockDriverState
*bs
)
742 BDRVRawState
*s
= bs
->opaque
;
749 struct dk_minfo minfo
;
759 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
760 #ifdef DIOCGMEDIASIZE
761 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
764 size
= LONG_LONG_MAX
;
766 size
= lseek(fd
, 0LL, SEEK_END
);
772 * use the DKIOCGMEDIAINFO ioctl to read the size.
774 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
776 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
777 } else /* there are reports that lseek on some devices
778 fails, but irc discussion said that contingency
779 on contingency was overkill */
782 size
= lseek(fd
, 0, SEEK_END
);
788 static int raw_create(const char *filename
, int64_t total_size
,
789 const char *backing_file
, int flags
)
793 if (flags
|| backing_file
)
796 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
800 ftruncate(fd
, total_size
* 512);
805 static void raw_flush(BlockDriverState
*bs
)
807 BDRVRawState
*s
= bs
->opaque
;
811 BlockDriver bdrv_raw
= {
813 sizeof(BDRVRawState
),
814 NULL
, /* no probe for protocols */
823 .bdrv_aio_read
= raw_aio_read
,
824 .bdrv_aio_write
= raw_aio_write
,
825 .bdrv_aio_cancel
= raw_aio_cancel
,
826 .aiocb_size
= sizeof(RawAIOCB
),
829 .bdrv_pread
= raw_pread
,
830 .bdrv_pwrite
= raw_pwrite
,
831 .bdrv_truncate
= raw_truncate
,
832 .bdrv_getlength
= raw_getlength
,
835 /***********************************************/
839 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
840 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
842 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
844 kern_return_t kernResult
;
845 mach_port_t masterPort
;
846 CFMutableDictionaryRef classesToMatch
;
848 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
849 if ( KERN_SUCCESS
!= kernResult
) {
850 printf( "IOMasterPort returned %d\n", kernResult
);
853 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
854 if ( classesToMatch
== NULL
) {
855 printf( "IOServiceMatching returned a NULL dictionary.\n" );
857 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
859 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
860 if ( KERN_SUCCESS
!= kernResult
)
862 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
868 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
870 io_object_t nextMedia
;
871 kern_return_t kernResult
= KERN_FAILURE
;
873 nextMedia
= IOIteratorNext( mediaIterator
);
876 CFTypeRef bsdPathAsCFString
;
877 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
878 if ( bsdPathAsCFString
) {
879 size_t devPathLength
;
880 strcpy( bsdPath
, _PATH_DEV
);
881 strcat( bsdPath
, "r" );
882 devPathLength
= strlen( bsdPath
);
883 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
884 kernResult
= KERN_SUCCESS
;
886 CFRelease( bsdPathAsCFString
);
888 IOObjectRelease( nextMedia
);
896 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
898 BDRVRawState
*s
= bs
->opaque
;
899 int fd
, open_flags
, ret
;
904 if (strstart(filename
, "/dev/cdrom", NULL
)) {
905 kern_return_t kernResult
;
906 io_iterator_t mediaIterator
;
907 char bsdPath
[ MAXPATHLEN
];
910 kernResult
= FindEjectableCDMedia( &mediaIterator
);
911 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
913 if ( bsdPath
[ 0 ] != '\0' ) {
914 strcat(bsdPath
,"s0");
915 /* some CDs don't have a partition 0 */
916 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
918 bsdPath
[strlen(bsdPath
)-1] = '1';
926 IOObjectRelease( mediaIterator
);
929 open_flags
= O_BINARY
;
930 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
931 open_flags
|= O_RDWR
;
933 open_flags
|= O_RDONLY
;
936 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
937 * and O_DIRECT for no caching. */
938 if ((flags
& BDRV_O_NOCACHE
))
939 open_flags
|= O_DIRECT
;
940 else if (!(flags
& BDRV_O_CACHE_WB
))
941 open_flags
|= O_DSYNC
;
943 s
->type
= FTYPE_FILE
;
944 #if defined(__linux__)
945 if (strstart(filename
, "/dev/cd", NULL
)) {
946 /* open will not fail even if no CD is inserted */
947 open_flags
|= O_NONBLOCK
;
949 } else if (strstart(filename
, "/dev/fd", NULL
)) {
951 s
->fd_open_flags
= open_flags
;
952 /* open will not fail even if no floppy is inserted */
953 open_flags
|= O_NONBLOCK
;
954 } else if (strstart(filename
, "/dev/sg", NULL
)) {
958 fd
= open(filename
, open_flags
, 0644);
966 #if defined(__linux__)
967 /* close fd so that we can reopen it as needed */
968 if (s
->type
== FTYPE_FD
) {
971 s
->fd_media_changed
= 1;
977 #if defined(__linux__)
978 /* Note: we do not have a reliable method to detect if the floppy is
979 present. The current method is to try to open the floppy at every
980 I/O and to keep it opened during a few hundreds of ms. */
981 static int fd_open(BlockDriverState
*bs
)
983 BDRVRawState
*s
= bs
->opaque
;
984 int last_media_present
;
986 if (s
->type
!= FTYPE_FD
)
988 last_media_present
= (s
->fd
>= 0);
990 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
994 printf("Floppy closed\n");
998 if (s
->fd_got_error
&&
999 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1001 printf("No floppy (open delayed)\n");
1005 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1007 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1008 s
->fd_got_error
= 1;
1009 if (last_media_present
)
1010 s
->fd_media_changed
= 1;
1012 printf("No floppy\n");
1017 printf("Floppy opened\n");
1020 if (!last_media_present
)
1021 s
->fd_media_changed
= 1;
1022 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1023 s
->fd_got_error
= 0;
1027 static int raw_is_inserted(BlockDriverState
*bs
)
1029 BDRVRawState
*s
= bs
->opaque
;
1034 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1035 if (ret
== CDS_DISC_OK
)
1048 /* currently only used by fdc.c, but a CD version would be good too */
1049 static int raw_media_changed(BlockDriverState
*bs
)
1051 BDRVRawState
*s
= bs
->opaque
;
1057 /* XXX: we do not have a true media changed indication. It
1058 does not work if the floppy is changed without trying
1061 ret
= s
->fd_media_changed
;
1062 s
->fd_media_changed
= 0;
1064 printf("Floppy changed=%d\n", ret
);
1073 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1075 BDRVRawState
*s
= bs
->opaque
;
1080 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1081 perror("CDROMEJECT");
1083 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1084 perror("CDROMEJECT");
1094 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1096 if (ioctl(fd
, FDEJECT
, 0) < 0)
1108 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1110 BDRVRawState
*s
= bs
->opaque
;
1114 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1115 /* Note: an error can happen if the distribution automatically
1116 mounts the CD-ROM */
1117 // perror("CDROM_LOCKDOOR");
1126 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1128 BDRVRawState
*s
= bs
->opaque
;
1130 return ioctl(s
->fd
, req
, buf
);
1134 static int fd_open(BlockDriverState
*bs
)
1139 static int raw_is_inserted(BlockDriverState
*bs
)
1144 static int raw_media_changed(BlockDriverState
*bs
)
1149 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1154 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1159 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1165 BlockDriver bdrv_host_device
= {
1167 sizeof(BDRVRawState
),
1168 NULL
, /* no probe for protocols */
1177 .bdrv_aio_read
= raw_aio_read
,
1178 .bdrv_aio_write
= raw_aio_write
,
1179 .bdrv_aio_cancel
= raw_aio_cancel
,
1180 .aiocb_size
= sizeof(RawAIOCB
),
1183 .bdrv_pread
= raw_pread
,
1184 .bdrv_pwrite
= raw_pwrite
,
1185 .bdrv_getlength
= raw_getlength
,
1187 /* removable device support */
1188 .bdrv_is_inserted
= raw_is_inserted
,
1189 .bdrv_media_changed
= raw_media_changed
,
1190 .bdrv_eject
= raw_eject
,
1191 .bdrv_set_locked
= raw_set_locked
,
1192 /* generic scsi device */
1193 .bdrv_ioctl
= raw_ioctl
,