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"
32 #include "posix-aio-compat.h"
37 #include <sys/param.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/IOBSD.h>
40 #include <IOKit/storage/IOMediaBSDClient.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <IOKit/storage/IOCDMedia.h>
43 //#include <IOKit/storage/IOCDTypes.h>
44 #include <CoreFoundation/CoreFoundation.h>
48 #define _POSIX_PTHREAD_SEMANTICS 1
53 #include <sys/ioctl.h>
54 #include <linux/cdrom.h>
64 #include <sys/ioctl.h>
65 #include <sys/disklabel.h>
70 #include <sys/ioctl.h>
71 #include <sys/diskslice.h>
74 //#define DEBUG_FLOPPY
77 #if defined(DEBUG_BLOCK)
78 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
79 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
81 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
84 /* OS X does not have O_DSYNC */
86 #define O_DSYNC O_SYNC
89 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
91 #define O_DIRECT O_DSYNC
98 #define ALIGNED_BUFFER_SIZE (32 * 512)
100 /* if the FD is not accessed during that time (in ms), we try to
101 reopen it to see if the disk has been changed */
102 #define FD_OPEN_TIMEOUT 1000
104 typedef struct BDRVRawState
{
107 unsigned int lseek_err_cnt
;
108 #if defined(__linux__)
109 /* linux floppy specific */
111 int64_t fd_open_time
;
112 int64_t fd_error_time
;
114 int fd_media_changed
;
116 #if defined(__FreeBSD__)
119 uint8_t* aligned_buf
;
122 static int posix_aio_init(void);
124 static int fd_open(BlockDriverState
*bs
);
126 #if defined(__FreeBSD__)
127 static int cd_open(BlockDriverState
*bs
);
130 static int raw_is_inserted(BlockDriverState
*bs
);
132 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
134 BDRVRawState
*s
= bs
->opaque
;
135 int fd
, open_flags
, ret
;
139 s
->lseek_err_cnt
= 0;
141 open_flags
= O_BINARY
;
142 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
143 open_flags
|= O_RDWR
;
145 open_flags
|= O_RDONLY
;
148 if (flags
& BDRV_O_CREAT
)
149 open_flags
|= O_CREAT
| O_TRUNC
;
151 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
152 * and O_DIRECT for no caching. */
153 if ((flags
& BDRV_O_NOCACHE
))
154 open_flags
|= O_DIRECT
;
155 else if (!(flags
& BDRV_O_CACHE_WB
))
156 open_flags
|= O_DSYNC
;
158 s
->type
= FTYPE_FILE
;
160 fd
= open(filename
, open_flags
, 0644);
168 s
->aligned_buf
= NULL
;
169 if ((flags
& BDRV_O_NOCACHE
)) {
170 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
171 if (s
->aligned_buf
== NULL
) {
180 /* XXX: use host sector size if necessary with:
181 #ifdef DIOCGSECTORSIZE
183 unsigned int sectorsize = 512;
184 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
185 sectorsize > bufsize)
186 bufsize = sectorsize;
190 u_int32_t blockSize = 512;
191 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
198 * offset and count are in bytes, but must be multiples of 512 for files
199 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
201 * This function may be called without alignment if the caller ensures
202 * that O_DIRECT is not in effect.
204 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
205 uint8_t *buf
, int count
)
207 BDRVRawState
*s
= bs
->opaque
;
214 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
215 ++(s
->lseek_err_cnt
);
216 if(s
->lseek_err_cnt
<= 10) {
217 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
218 "] lseek failed : %d = %s\n",
219 s
->fd
, bs
->filename
, offset
, buf
, count
,
220 bs
->total_sectors
, errno
, strerror(errno
));
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 "] read failed %d : %d = %s\n",
232 s
->fd
, bs
->filename
, offset
, buf
, count
,
233 bs
->total_sectors
, ret
, errno
, strerror(errno
));
235 /* Try harder for CDrom. */
236 if (bs
->type
== BDRV_TYPE_CDROM
) {
237 lseek(s
->fd
, offset
, SEEK_SET
);
238 ret
= read(s
->fd
, buf
, count
);
240 goto label__raw_read__success
;
241 lseek(s
->fd
, offset
, SEEK_SET
);
242 ret
= read(s
->fd
, buf
, count
);
244 goto label__raw_read__success
;
246 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
247 "] retry read failed %d : %d = %s\n",
248 s
->fd
, bs
->filename
, offset
, buf
, count
,
249 bs
->total_sectors
, ret
, errno
, strerror(errno
));
252 label__raw_read__success
:
254 return (ret
< 0) ? -errno
: ret
;
258 * offset and count are in bytes, but must be multiples of 512 for files
259 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
261 * This function may be called without alignment if the caller ensures
262 * that O_DIRECT is not in effect.
264 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
265 const uint8_t *buf
, int count
)
267 BDRVRawState
*s
= bs
->opaque
;
274 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
275 ++(s
->lseek_err_cnt
);
276 if(s
->lseek_err_cnt
) {
277 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
278 PRId64
"] lseek failed : %d = %s\n",
279 s
->fd
, bs
->filename
, offset
, buf
, count
,
280 bs
->total_sectors
, errno
, strerror(errno
));
284 s
->lseek_err_cnt
= 0;
286 ret
= write(s
->fd
, buf
, count
);
288 goto label__raw_write__success
;
290 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
291 "] write failed %d : %d = %s\n",
292 s
->fd
, bs
->filename
, offset
, buf
, count
,
293 bs
->total_sectors
, ret
, errno
, strerror(errno
));
295 label__raw_write__success
:
297 return (ret
< 0) ? -errno
: ret
;
302 * offset and count are in bytes and possibly not aligned. For files opened
303 * with O_DIRECT, necessary alignments are ensured before calling
304 * raw_pread_aligned to do the actual read.
306 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
307 uint8_t *buf
, int count
)
309 BDRVRawState
*s
= bs
->opaque
;
310 int size
, ret
, shift
, sum
;
314 if (s
->aligned_buf
!= NULL
) {
316 if (offset
& 0x1ff) {
317 /* align offset on a 512 bytes boundary */
319 shift
= offset
& 0x1ff;
320 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
321 if (size
> ALIGNED_BUFFER_SIZE
)
322 size
= ALIGNED_BUFFER_SIZE
;
323 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
330 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
340 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
342 /* read on aligned buffer */
346 size
= (count
+ 0x1ff) & ~0x1ff;
347 if (size
> ALIGNED_BUFFER_SIZE
)
348 size
= ALIGNED_BUFFER_SIZE
;
350 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
358 memcpy(buf
, s
->aligned_buf
, size
);
370 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
373 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
374 uint8_t *buf
, int nb_sectors
)
378 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
379 if (ret
== (nb_sectors
* 512))
385 * offset and count are in bytes and possibly not aligned. For files opened
386 * with O_DIRECT, necessary alignments are ensured before calling
387 * raw_pwrite_aligned to do the actual write.
389 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
390 const uint8_t *buf
, int count
)
392 BDRVRawState
*s
= bs
->opaque
;
393 int size
, ret
, shift
, sum
;
397 if (s
->aligned_buf
!= NULL
) {
399 if (offset
& 0x1ff) {
400 /* align offset on a 512 bytes boundary */
401 shift
= offset
& 0x1ff;
402 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
409 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
411 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
423 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
425 while ((size
= (count
& ~0x1ff)) != 0) {
427 if (size
> ALIGNED_BUFFER_SIZE
)
428 size
= ALIGNED_BUFFER_SIZE
;
430 memcpy(s
->aligned_buf
, buf
, size
);
432 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
441 /* here, count < 512 because (count & ~0x1ff) == 0 */
443 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
446 memcpy(s
->aligned_buf
, buf
, count
);
448 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
459 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
462 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
463 const uint8_t *buf
, int nb_sectors
)
466 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
467 if (ret
== (nb_sectors
* 512))
473 /***********************************************************/
474 /* Unix AIO using POSIX AIO */
476 typedef struct RawAIOCB
{
477 BlockDriverAIOCB common
;
478 struct qemu_paiocb aiocb
;
479 struct RawAIOCB
*next
;
483 typedef struct PosixAioState
489 static void posix_aio_read(void *opaque
)
491 PosixAioState
*s
= opaque
;
492 RawAIOCB
*acb
, **pacb
;
496 struct qemu_signalfd_siginfo siginfo
;
500 /* try to read from signalfd, don't freak out if we can't read anything */
502 while (offset
< 128) {
505 len
= read(s
->fd
, sig
.buf
+ offset
, 128 - offset
);
506 if (len
== -1 && errno
== EINTR
)
508 if (len
== -1 && errno
== EAGAIN
) {
509 /* there is no natural reason for this to happen,
510 * so we'll spin hard until we get everything just
511 * to be on the safe side. */
520 pacb
= &s
->first_aio
;
525 ret
= qemu_paio_error(&acb
->aiocb
);
526 if (ret
== ECANCELED
) {
527 /* remove the request */
529 qemu_aio_release(acb
);
530 } else if (ret
!= EINPROGRESS
) {
533 ret
= qemu_paio_return(&acb
->aiocb
);
534 if (ret
== acb
->aiocb
.aio_nbytes
)
541 /* remove the request */
543 /* call the callback */
544 acb
->common
.cb(acb
->common
.opaque
, ret
);
545 qemu_aio_release(acb
);
555 static int posix_aio_flush(void *opaque
)
557 PosixAioState
*s
= opaque
;
558 return !!s
->first_aio
;
561 static PosixAioState
*posix_aio_state
;
563 static int posix_aio_init(void)
567 struct qemu_paioinit ai
;
572 s
= qemu_malloc(sizeof(PosixAioState
));
574 /* Make sure to block AIO signal */
576 sigaddset(&mask
, SIGUSR2
);
577 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
580 s
->fd
= qemu_signalfd(&mask
);
582 fprintf(stderr
, "failed to create signalfd\n");
586 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
588 qemu_aio_set_fd_handler(s
->fd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
590 memset(&ai
, 0, sizeof(ai
));
600 static void raw_aio_remove(RawAIOCB
*acb
)
604 /* remove the callback from the queue */
605 pacb
= &posix_aio_state
->first_aio
;
608 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
610 } else if (*pacb
== acb
) {
612 qemu_aio_release(acb
);
615 pacb
= &(*pacb
)->next
;
619 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
622 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
624 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
625 if (ret
== QEMU_PAIO_NOTCANCELED
) {
626 /* fail safe: if the aio could not be canceled, we wait for
628 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
634 static AIOPool raw_aio_pool
= {
635 .aiocb_size
= sizeof(RawAIOCB
),
636 .cancel
= raw_aio_cancel
,
639 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
640 QEMUIOVector
*qiov
, int nb_sectors
,
641 BlockDriverCompletionFunc
*cb
, void *opaque
)
643 BDRVRawState
*s
= bs
->opaque
;
649 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
652 acb
->aiocb
.aio_fildes
= s
->fd
;
653 acb
->aiocb
.ev_signo
= SIGUSR2
;
654 acb
->aiocb
.aio_iov
= qiov
->iov
;
655 acb
->aiocb
.aio_niov
= qiov
->niov
;
656 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
657 acb
->aiocb
.aio_offset
= sector_num
* 512;
658 acb
->aiocb
.aio_flags
= 0;
661 * If O_DIRECT is used the buffer needs to be aligned on a sector
662 * boundary. Tell the low level code to ensure that in case it's
666 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
668 acb
->next
= posix_aio_state
->first_aio
;
669 posix_aio_state
->first_aio
= acb
;
673 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
674 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
675 BlockDriverCompletionFunc
*cb
, void *opaque
)
679 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
682 if (qemu_paio_read(&acb
->aiocb
) < 0) {
689 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
690 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
691 BlockDriverCompletionFunc
*cb
, void *opaque
)
695 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
698 if (qemu_paio_write(&acb
->aiocb
) < 0) {
704 #else /* CONFIG_AIO */
705 static int posix_aio_init(void)
709 #endif /* CONFIG_AIO */
712 static void raw_close(BlockDriverState
*bs
)
714 BDRVRawState
*s
= bs
->opaque
;
718 if (s
->aligned_buf
!= NULL
)
719 qemu_free(s
->aligned_buf
);
723 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
725 BDRVRawState
*s
= bs
->opaque
;
726 if (s
->type
!= FTYPE_FILE
)
728 if (ftruncate(s
->fd
, offset
) < 0)
734 static int64_t raw_getlength(BlockDriverState
*bs
)
736 BDRVRawState
*s
= bs
->opaque
;
742 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
745 if (ioctl(fd
, DIOCGDINFO
, &dl
))
747 return (uint64_t)dl
.d_secsize
*
748 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
752 #else /* !__OpenBSD__ */
753 static int64_t raw_getlength(BlockDriverState
*bs
)
755 BDRVRawState
*s
= bs
->opaque
;
765 struct dk_minfo minfo
;
778 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
779 #ifdef DIOCGMEDIASIZE
780 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
781 #elif defined(DIOCGPART)
784 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
785 size
= pi
.media_size
;
792 size
= LONG_LONG_MAX
;
794 size
= lseek(fd
, 0LL, SEEK_END
);
799 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
800 if (size
== 2048LL * (unsigned)-1)
802 /* XXX no disc? maybe we need to reopen... */
803 if (size
<= 0 && !reopened
&& cd_open(bs
) >= 0) {
813 * use the DKIOCGMEDIAINFO ioctl to read the size.
815 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
817 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
818 } else /* there are reports that lseek on some devices
819 fails, but irc discussion said that contingency
820 on contingency was overkill */
823 size
= lseek(fd
, 0, SEEK_END
);
829 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
832 int64_t total_size
= 0;
834 /* Read out options */
835 while (options
&& options
->name
) {
836 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
837 total_size
= options
->value
.n
/ 512;
842 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
846 ftruncate(fd
, total_size
* 512);
851 static void raw_flush(BlockDriverState
*bs
)
853 BDRVRawState
*s
= bs
->opaque
;
858 static QEMUOptionParameter raw_create_options
[] = {
859 { BLOCK_OPT_SIZE
, OPT_SIZE
},
863 static BlockDriver bdrv_raw
= {
864 .format_name
= "raw",
865 .instance_size
= sizeof(BDRVRawState
),
866 .bdrv_probe
= NULL
, /* no probe for protocols */
867 .bdrv_open
= raw_open
,
868 .bdrv_read
= raw_read
,
869 .bdrv_write
= raw_write
,
870 .bdrv_close
= raw_close
,
871 .bdrv_create
= raw_create
,
872 .bdrv_flush
= raw_flush
,
875 .bdrv_aio_readv
= raw_aio_readv
,
876 .bdrv_aio_writev
= raw_aio_writev
,
879 .bdrv_truncate
= raw_truncate
,
880 .bdrv_getlength
= raw_getlength
,
882 .create_options
= raw_create_options
,
885 /***********************************************/
889 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
890 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
892 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
894 kern_return_t kernResult
;
895 mach_port_t masterPort
;
896 CFMutableDictionaryRef classesToMatch
;
898 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
899 if ( KERN_SUCCESS
!= kernResult
) {
900 printf( "IOMasterPort returned %d\n", kernResult
);
903 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
904 if ( classesToMatch
== NULL
) {
905 printf( "IOServiceMatching returned a NULL dictionary.\n" );
907 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
909 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
910 if ( KERN_SUCCESS
!= kernResult
)
912 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
918 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
920 io_object_t nextMedia
;
921 kern_return_t kernResult
= KERN_FAILURE
;
923 nextMedia
= IOIteratorNext( mediaIterator
);
926 CFTypeRef bsdPathAsCFString
;
927 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
928 if ( bsdPathAsCFString
) {
929 size_t devPathLength
;
930 strcpy( bsdPath
, _PATH_DEV
);
931 strcat( bsdPath
, "r" );
932 devPathLength
= strlen( bsdPath
);
933 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
934 kernResult
= KERN_SUCCESS
;
936 CFRelease( bsdPathAsCFString
);
938 IOObjectRelease( nextMedia
);
946 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
948 BDRVRawState
*s
= bs
->opaque
;
949 int fd
, open_flags
, ret
;
954 if (strstart(filename
, "/dev/cdrom", NULL
)) {
955 kern_return_t kernResult
;
956 io_iterator_t mediaIterator
;
957 char bsdPath
[ MAXPATHLEN
];
960 kernResult
= FindEjectableCDMedia( &mediaIterator
);
961 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
963 if ( bsdPath
[ 0 ] != '\0' ) {
964 strcat(bsdPath
,"s0");
965 /* some CDs don't have a partition 0 */
966 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
968 bsdPath
[strlen(bsdPath
)-1] = '1';
976 IOObjectRelease( mediaIterator
);
979 open_flags
= O_BINARY
;
980 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
981 open_flags
|= O_RDWR
;
983 open_flags
|= O_RDONLY
;
986 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
987 * and O_DIRECT for no caching. */
988 if ((flags
& BDRV_O_NOCACHE
))
989 open_flags
|= O_DIRECT
;
990 else if (!(flags
& BDRV_O_CACHE_WB
))
991 open_flags
|= O_DSYNC
;
993 s
->type
= FTYPE_FILE
;
994 #if defined(__linux__)
995 if (strstart(filename
, "/dev/cd", NULL
)) {
996 /* open will not fail even if no CD is inserted */
997 open_flags
|= O_NONBLOCK
;
999 } else if (strstart(filename
, "/dev/fd", NULL
)) {
1001 s
->fd_open_flags
= open_flags
;
1002 /* open will not fail even if no floppy is inserted */
1003 open_flags
|= O_NONBLOCK
;
1005 } else if (strstart(filename
, "/dev/sg", NULL
)) {
1010 #if defined(__FreeBSD__)
1011 if (strstart(filename
, "/dev/cd", NULL
) ||
1012 strstart(filename
, "/dev/acd", NULL
)) {
1014 s
->cd_open_flags
= open_flags
;
1018 fd
= open(filename
, open_flags
, 0644);
1026 #if defined(__FreeBSD__)
1027 /* make sure the door isnt locked at this time */
1028 if (s
->type
== FTYPE_CD
)
1029 ioctl (s
->fd
, CDIOCALLOW
);
1031 #if defined(__linux__)
1032 /* close fd so that we can reopen it as needed */
1033 if (s
->type
== FTYPE_FD
) {
1036 s
->fd_media_changed
= 1;
1042 #if defined(__linux__)
1043 /* Note: we do not have a reliable method to detect if the floppy is
1044 present. The current method is to try to open the floppy at every
1045 I/O and to keep it opened during a few hundreds of ms. */
1046 static int fd_open(BlockDriverState
*bs
)
1048 BDRVRawState
*s
= bs
->opaque
;
1049 int last_media_present
;
1051 if (s
->type
!= FTYPE_FD
)
1053 last_media_present
= (s
->fd
>= 0);
1055 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1059 printf("Floppy closed\n");
1063 if (s
->fd_got_error
&&
1064 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1066 printf("No floppy (open delayed)\n");
1070 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1072 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1073 s
->fd_got_error
= 1;
1074 if (last_media_present
)
1075 s
->fd_media_changed
= 1;
1077 printf("No floppy\n");
1082 printf("Floppy opened\n");
1085 if (!last_media_present
)
1086 s
->fd_media_changed
= 1;
1087 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1088 s
->fd_got_error
= 0;
1092 static int raw_is_inserted(BlockDriverState
*bs
)
1094 BDRVRawState
*s
= bs
->opaque
;
1099 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1100 if (ret
== CDS_DISC_OK
)
1113 /* currently only used by fdc.c, but a CD version would be good too */
1114 static int raw_media_changed(BlockDriverState
*bs
)
1116 BDRVRawState
*s
= bs
->opaque
;
1122 /* XXX: we do not have a true media changed indication. It
1123 does not work if the floppy is changed without trying
1126 ret
= s
->fd_media_changed
;
1127 s
->fd_media_changed
= 0;
1129 printf("Floppy changed=%d\n", ret
);
1138 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1140 BDRVRawState
*s
= bs
->opaque
;
1145 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1146 perror("CDROMEJECT");
1148 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1149 perror("CDROMEJECT");
1159 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1161 if (ioctl(fd
, FDEJECT
, 0) < 0)
1173 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1175 BDRVRawState
*s
= bs
->opaque
;
1179 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1180 /* Note: an error can happen if the distribution automatically
1181 mounts the CD-ROM */
1182 // perror("CDROM_LOCKDOOR");
1191 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1193 BDRVRawState
*s
= bs
->opaque
;
1195 return ioctl(s
->fd
, req
, buf
);
1199 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1200 unsigned long int req
, void *buf
,
1201 BlockDriverCompletionFunc
*cb
, void *opaque
)
1203 BDRVRawState
*s
= bs
->opaque
;
1206 if (fd_open(bs
) < 0)
1209 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1212 acb
->aiocb
.aio_fildes
= s
->fd
;
1213 acb
->aiocb
.ev_signo
= SIGUSR2
;
1214 acb
->aiocb
.aio_offset
= 0;
1215 acb
->aiocb
.aio_flags
= 0;
1217 acb
->next
= posix_aio_state
->first_aio
;
1218 posix_aio_state
->first_aio
= acb
;
1220 acb
->aiocb
.aio_ioctl_buf
= buf
;
1221 acb
->aiocb
.aio_ioctl_cmd
= req
;
1222 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1223 raw_aio_remove(acb
);
1227 return &acb
->common
;
1231 #elif defined(__FreeBSD__)
1233 static int fd_open(BlockDriverState
*bs
)
1235 BDRVRawState
*s
= bs
->opaque
;
1237 /* this is just to ensure s->fd is sane (its called by io ops) */
1243 static int cd_open(BlockDriverState
*bs
)
1245 #if defined(__FreeBSD__)
1246 BDRVRawState
*s
= bs
->opaque
;
1251 /* XXX force reread of possibly changed/newly loaded disc,
1252 * FreeBSD seems to not notice sometimes... */
1255 fd
= open(bs
->filename
, s
->cd_open_flags
, 0644);
1261 /* make sure the door isnt locked at this time */
1262 ioctl (s
->fd
, CDIOCALLOW
);
1268 static int raw_is_inserted(BlockDriverState
*bs
)
1270 BDRVRawState
*s
= bs
->opaque
;
1274 return (raw_getlength(bs
) > 0);
1276 /* XXX handle this */
1283 static int raw_media_changed(BlockDriverState
*bs
)
1288 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1290 BDRVRawState
*s
= bs
->opaque
;
1296 (void) ioctl (s
->fd
, CDIOCALLOW
);
1298 if (ioctl (s
->fd
, CDIOCEJECT
) < 0)
1299 perror("CDIOCEJECT");
1301 if (ioctl (s
->fd
, CDIOCCLOSE
) < 0)
1302 perror("CDIOCCLOSE");
1304 if (cd_open(bs
) < 0)
1308 /* XXX handle this */
1316 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1318 BDRVRawState
*s
= bs
->opaque
;
1324 if (ioctl (s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1325 /* Note: an error can happen if the distribution automatically
1326 mounts the CD-ROM */
1327 // perror("CDROM_LOCKDOOR");
1336 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1340 #else /* !linux && !FreeBSD */
1342 static int fd_open(BlockDriverState
*bs
)
1347 static int raw_is_inserted(BlockDriverState
*bs
)
1352 static int raw_media_changed(BlockDriverState
*bs
)
1357 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1362 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1367 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1372 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1373 unsigned long int req
, void *buf
,
1374 BlockDriverCompletionFunc
*cb
, void *opaque
)
1378 #endif /* !linux && !FreeBSD */
1380 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1384 struct stat stat_buf
;
1385 int64_t total_size
= 0;
1387 /* Read out options */
1388 while (options
&& options
->name
) {
1389 if (!strcmp(options
->name
, "size")) {
1390 total_size
= options
->value
.n
/ 512;
1395 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1399 if (fstat(fd
, &stat_buf
) < 0)
1401 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1403 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1410 static BlockDriver bdrv_host_device
= {
1411 .format_name
= "host_device",
1412 .instance_size
= sizeof(BDRVRawState
),
1413 .bdrv_open
= hdev_open
,
1414 .bdrv_close
= raw_close
,
1415 .bdrv_create
= hdev_create
,
1416 .bdrv_flush
= raw_flush
,
1419 .bdrv_aio_readv
= raw_aio_readv
,
1420 .bdrv_aio_writev
= raw_aio_writev
,
1423 .bdrv_read
= raw_read
,
1424 .bdrv_write
= raw_write
,
1425 .bdrv_getlength
= raw_getlength
,
1427 /* removable device support */
1428 .bdrv_is_inserted
= raw_is_inserted
,
1429 .bdrv_media_changed
= raw_media_changed
,
1430 .bdrv_eject
= raw_eject
,
1431 .bdrv_set_locked
= raw_set_locked
,
1432 /* generic scsi device */
1433 .bdrv_ioctl
= raw_ioctl
,
1435 .bdrv_aio_ioctl
= raw_aio_ioctl
,
1439 static void bdrv_raw_init(void)
1441 bdrv_register(&bdrv_raw
);
1442 bdrv_register(&bdrv_host_device
);
1445 block_init(bdrv_raw_init
);