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"
28 #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>
63 #include <sys/ioctl.h>
64 #include <sys/disklabel.h>
69 #include <sys/ioctl.h>
70 #include <sys/diskslice.h>
73 //#define DEBUG_FLOPPY
76 #if defined(DEBUG_BLOCK)
77 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
78 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
80 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
83 /* OS X does not have O_DSYNC */
86 #define O_DSYNC O_SYNC
87 #elif defined(O_FSYNC)
88 #define O_DSYNC O_FSYNC
92 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
94 #define O_DIRECT O_DSYNC
101 #define ALIGNED_BUFFER_SIZE (32 * 512)
103 /* if the FD is not accessed during that time (in ms), we try to
104 reopen it to see if the disk has been changed */
105 #define FD_OPEN_TIMEOUT 1000
107 typedef struct BDRVRawState
{
110 unsigned int lseek_err_cnt
;
112 #if defined(__linux__)
113 /* linux floppy specific */
114 int64_t fd_open_time
;
115 int64_t fd_error_time
;
117 int fd_media_changed
;
119 uint8_t* aligned_buf
;
122 static int posix_aio_init(void);
124 static int fd_open(BlockDriverState
*bs
);
125 static int64_t raw_getlength(BlockDriverState
*bs
);
127 #if defined(__FreeBSD__)
128 static int cdrom_reopen(BlockDriverState
*bs
);
131 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
132 int bdrv_flags
, int open_flags
)
134 BDRVRawState
*s
= bs
->opaque
;
139 s
->lseek_err_cnt
= 0;
141 s
->open_flags
= open_flags
| O_BINARY
;
142 s
->open_flags
&= ~O_ACCMODE
;
143 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
144 s
->open_flags
|= O_RDWR
;
146 s
->open_flags
|= O_RDONLY
;
150 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
151 * and O_DIRECT for no caching. */
152 if ((bdrv_flags
& BDRV_O_NOCACHE
))
153 s
->open_flags
|= O_DIRECT
;
154 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
155 s
->open_flags
|= O_DSYNC
;
158 fd
= open(filename
, s
->open_flags
, 0644);
166 s
->aligned_buf
= NULL
;
167 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
168 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
169 if (s
->aligned_buf
== NULL
) {
178 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
180 BDRVRawState
*s
= bs
->opaque
;
183 s
->type
= FTYPE_FILE
;
184 if (flags
& BDRV_O_CREAT
)
185 open_flags
= O_CREAT
| O_TRUNC
;
187 return raw_open_common(bs
, filename
, flags
, open_flags
);
190 /* XXX: use host sector size if necessary with:
191 #ifdef DIOCGSECTORSIZE
193 unsigned int sectorsize = 512;
194 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
195 sectorsize > bufsize)
196 bufsize = sectorsize;
200 u_int32_t blockSize = 512;
201 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
208 * offset and count are in bytes, but must be multiples of 512 for files
209 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
211 * This function may be called without alignment if the caller ensures
212 * that O_DIRECT is not in effect.
214 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
215 uint8_t *buf
, int count
)
217 BDRVRawState
*s
= bs
->opaque
;
224 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
225 ++(s
->lseek_err_cnt
);
226 if(s
->lseek_err_cnt
<= 10) {
227 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
228 "] lseek failed : %d = %s\n",
229 s
->fd
, bs
->filename
, offset
, buf
, count
,
230 bs
->total_sectors
, errno
, strerror(errno
));
236 ret
= read(s
->fd
, buf
, count
);
238 goto label__raw_read__success
;
240 /* Allow reads beyond the end (needed for pwrite) */
241 if ((ret
== 0) && bs
->growable
) {
242 int64_t size
= raw_getlength(bs
);
243 if (offset
>= size
) {
244 memset(buf
, 0, count
);
246 goto label__raw_read__success
;
250 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
251 "] read failed %d : %d = %s\n",
252 s
->fd
, bs
->filename
, offset
, buf
, count
,
253 bs
->total_sectors
, ret
, errno
, strerror(errno
));
255 /* Try harder for CDrom. */
256 if (bs
->type
== BDRV_TYPE_CDROM
) {
257 lseek(s
->fd
, offset
, SEEK_SET
);
258 ret
= read(s
->fd
, buf
, count
);
260 goto label__raw_read__success
;
261 lseek(s
->fd
, offset
, SEEK_SET
);
262 ret
= read(s
->fd
, buf
, count
);
264 goto label__raw_read__success
;
266 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
267 "] retry read failed %d : %d = %s\n",
268 s
->fd
, bs
->filename
, offset
, buf
, count
,
269 bs
->total_sectors
, ret
, errno
, strerror(errno
));
272 label__raw_read__success
:
274 return (ret
< 0) ? -errno
: ret
;
278 * offset and count are in bytes, but must be multiples of 512 for files
279 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
281 * This function may be called without alignment if the caller ensures
282 * that O_DIRECT is not in effect.
284 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
285 const uint8_t *buf
, int count
)
287 BDRVRawState
*s
= bs
->opaque
;
294 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
295 ++(s
->lseek_err_cnt
);
296 if(s
->lseek_err_cnt
) {
297 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
298 PRId64
"] lseek failed : %d = %s\n",
299 s
->fd
, bs
->filename
, offset
, buf
, count
,
300 bs
->total_sectors
, errno
, strerror(errno
));
304 s
->lseek_err_cnt
= 0;
306 ret
= write(s
->fd
, buf
, count
);
308 goto label__raw_write__success
;
310 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
311 "] write failed %d : %d = %s\n",
312 s
->fd
, bs
->filename
, offset
, buf
, count
,
313 bs
->total_sectors
, ret
, errno
, strerror(errno
));
315 label__raw_write__success
:
317 return (ret
< 0) ? -errno
: ret
;
322 * offset and count are in bytes and possibly not aligned. For files opened
323 * with O_DIRECT, necessary alignments are ensured before calling
324 * raw_pread_aligned to do the actual read.
326 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
327 uint8_t *buf
, int count
)
329 BDRVRawState
*s
= bs
->opaque
;
330 int size
, ret
, shift
, sum
;
334 if (s
->aligned_buf
!= NULL
) {
336 if (offset
& 0x1ff) {
337 /* align offset on a 512 bytes boundary */
339 shift
= offset
& 0x1ff;
340 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
341 if (size
> ALIGNED_BUFFER_SIZE
)
342 size
= ALIGNED_BUFFER_SIZE
;
343 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
350 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
360 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
362 /* read on aligned buffer */
366 size
= (count
+ 0x1ff) & ~0x1ff;
367 if (size
> ALIGNED_BUFFER_SIZE
)
368 size
= ALIGNED_BUFFER_SIZE
;
370 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
378 memcpy(buf
, s
->aligned_buf
, size
);
390 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
393 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
394 uint8_t *buf
, int nb_sectors
)
398 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
399 if (ret
== (nb_sectors
* 512))
405 * offset and count are in bytes and possibly not aligned. For files opened
406 * with O_DIRECT, necessary alignments are ensured before calling
407 * raw_pwrite_aligned to do the actual write.
409 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
410 const uint8_t *buf
, int count
)
412 BDRVRawState
*s
= bs
->opaque
;
413 int size
, ret
, shift
, sum
;
417 if (s
->aligned_buf
!= NULL
) {
419 if (offset
& 0x1ff) {
420 /* align offset on a 512 bytes boundary */
421 shift
= offset
& 0x1ff;
422 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
429 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
431 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
443 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
445 while ((size
= (count
& ~0x1ff)) != 0) {
447 if (size
> ALIGNED_BUFFER_SIZE
)
448 size
= ALIGNED_BUFFER_SIZE
;
450 memcpy(s
->aligned_buf
, buf
, size
);
452 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
461 /* here, count < 512 because (count & ~0x1ff) == 0 */
463 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
466 memcpy(s
->aligned_buf
, buf
, count
);
468 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
479 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
482 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
483 const uint8_t *buf
, int nb_sectors
)
486 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
487 if (ret
== (nb_sectors
* 512))
493 /***********************************************************/
494 /* Unix AIO using POSIX AIO */
496 typedef struct RawAIOCB
{
497 BlockDriverAIOCB common
;
498 struct qemu_paiocb aiocb
;
499 struct RawAIOCB
*next
;
503 typedef struct PosixAioState
509 static void posix_aio_read(void *opaque
)
511 PosixAioState
*s
= opaque
;
512 RawAIOCB
*acb
, **pacb
;
516 /* read all bytes from signal pipe */
520 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
521 if (len
== -1 && errno
== EINTR
)
522 continue; /* try again */
523 if (len
== sizeof(bytes
))
524 continue; /* more to read */
529 pacb
= &s
->first_aio
;
534 ret
= qemu_paio_error(&acb
->aiocb
);
535 if (ret
== ECANCELED
) {
536 /* remove the request */
538 qemu_aio_release(acb
);
539 } else if (ret
!= EINPROGRESS
) {
542 ret
= qemu_paio_return(&acb
->aiocb
);
543 if (ret
== acb
->aiocb
.aio_nbytes
)
550 /* remove the request */
552 /* call the callback */
553 acb
->common
.cb(acb
->common
.opaque
, ret
);
554 qemu_aio_release(acb
);
564 static int posix_aio_flush(void *opaque
)
566 PosixAioState
*s
= opaque
;
567 return !!s
->first_aio
;
570 static PosixAioState
*posix_aio_state
;
572 static void aio_signal_handler(int signum
)
574 if (posix_aio_state
) {
577 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
583 static int posix_aio_init(void)
585 struct sigaction act
;
588 struct qemu_paioinit ai
;
593 s
= qemu_malloc(sizeof(PosixAioState
));
595 sigfillset(&act
.sa_mask
);
596 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
597 act
.sa_handler
= aio_signal_handler
;
598 sigaction(SIGUSR2
, &act
, NULL
);
601 if (pipe(fds
) == -1) {
602 fprintf(stderr
, "failed to create pipe\n");
609 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
610 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
612 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
614 memset(&ai
, 0, sizeof(ai
));
624 static void raw_aio_remove(RawAIOCB
*acb
)
628 /* remove the callback from the queue */
629 pacb
= &posix_aio_state
->first_aio
;
632 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
634 } else if (*pacb
== acb
) {
636 qemu_aio_release(acb
);
639 pacb
= &(*pacb
)->next
;
643 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
646 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
648 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
649 if (ret
== QEMU_PAIO_NOTCANCELED
) {
650 /* fail safe: if the aio could not be canceled, we wait for
652 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
658 static AIOPool raw_aio_pool
= {
659 .aiocb_size
= sizeof(RawAIOCB
),
660 .cancel
= raw_aio_cancel
,
663 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
664 QEMUIOVector
*qiov
, int nb_sectors
,
665 BlockDriverCompletionFunc
*cb
, void *opaque
)
667 BDRVRawState
*s
= bs
->opaque
;
673 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
676 acb
->aiocb
.aio_fildes
= s
->fd
;
677 acb
->aiocb
.ev_signo
= SIGUSR2
;
678 acb
->aiocb
.aio_iov
= qiov
->iov
;
679 acb
->aiocb
.aio_niov
= qiov
->niov
;
680 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
681 acb
->aiocb
.aio_offset
= sector_num
* 512;
682 acb
->aiocb
.aio_flags
= 0;
685 * If O_DIRECT is used the buffer needs to be aligned on a sector
686 * boundary. Tell the low level code to ensure that in case it's
690 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
692 acb
->next
= posix_aio_state
->first_aio
;
693 posix_aio_state
->first_aio
= acb
;
697 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
698 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
699 BlockDriverCompletionFunc
*cb
, void *opaque
)
703 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
706 if (qemu_paio_read(&acb
->aiocb
) < 0) {
713 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
714 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
715 BlockDriverCompletionFunc
*cb
, void *opaque
)
719 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
722 if (qemu_paio_write(&acb
->aiocb
) < 0) {
728 #else /* CONFIG_AIO */
729 static int posix_aio_init(void)
733 #endif /* CONFIG_AIO */
736 static void raw_close(BlockDriverState
*bs
)
738 BDRVRawState
*s
= bs
->opaque
;
742 if (s
->aligned_buf
!= NULL
)
743 qemu_free(s
->aligned_buf
);
747 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
749 BDRVRawState
*s
= bs
->opaque
;
750 if (s
->type
!= FTYPE_FILE
)
752 if (ftruncate(s
->fd
, offset
) < 0)
758 static int64_t raw_getlength(BlockDriverState
*bs
)
760 BDRVRawState
*s
= bs
->opaque
;
766 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
769 if (ioctl(fd
, DIOCGDINFO
, &dl
))
771 return (uint64_t)dl
.d_secsize
*
772 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
776 #else /* !__OpenBSD__ */
777 static int64_t raw_getlength(BlockDriverState
*bs
)
779 BDRVRawState
*s
= bs
->opaque
;
789 struct dk_minfo minfo
;
802 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
803 #ifdef DIOCGMEDIASIZE
804 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
805 #elif defined(DIOCGPART)
808 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
809 size
= pi
.media_size
;
816 size
= LONG_LONG_MAX
;
818 size
= lseek(fd
, 0LL, SEEK_END
);
823 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
824 if (size
== 2048LL * (unsigned)-1)
826 /* XXX no disc? maybe we need to reopen... */
827 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
837 * use the DKIOCGMEDIAINFO ioctl to read the size.
839 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
841 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
842 } else /* there are reports that lseek on some devices
843 fails, but irc discussion said that contingency
844 on contingency was overkill */
847 size
= lseek(fd
, 0, SEEK_END
);
853 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
857 int64_t total_size
= 0;
859 /* Read out options */
860 while (options
&& options
->name
) {
861 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
862 total_size
= options
->value
.n
/ 512;
867 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
872 if (ftruncate(fd
, total_size
* 512) != 0) {
875 if (close(fd
) != 0) {
882 static void raw_flush(BlockDriverState
*bs
)
884 BDRVRawState
*s
= bs
->opaque
;
889 static QEMUOptionParameter raw_create_options
[] = {
891 .name
= BLOCK_OPT_SIZE
,
893 .help
= "Virtual disk size"
898 static BlockDriver bdrv_raw
= {
899 .format_name
= "raw",
900 .instance_size
= sizeof(BDRVRawState
),
901 .bdrv_probe
= NULL
, /* no probe for protocols */
902 .bdrv_open
= raw_open
,
903 .bdrv_read
= raw_read
,
904 .bdrv_write
= raw_write
,
905 .bdrv_close
= raw_close
,
906 .bdrv_create
= raw_create
,
907 .bdrv_flush
= raw_flush
,
910 .bdrv_aio_readv
= raw_aio_readv
,
911 .bdrv_aio_writev
= raw_aio_writev
,
914 .bdrv_truncate
= raw_truncate
,
915 .bdrv_getlength
= raw_getlength
,
917 .create_options
= raw_create_options
,
920 /***********************************************/
924 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
925 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
927 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
929 kern_return_t kernResult
;
930 mach_port_t masterPort
;
931 CFMutableDictionaryRef classesToMatch
;
933 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
934 if ( KERN_SUCCESS
!= kernResult
) {
935 printf( "IOMasterPort returned %d\n", kernResult
);
938 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
939 if ( classesToMatch
== NULL
) {
940 printf( "IOServiceMatching returned a NULL dictionary.\n" );
942 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
944 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
945 if ( KERN_SUCCESS
!= kernResult
)
947 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
953 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
955 io_object_t nextMedia
;
956 kern_return_t kernResult
= KERN_FAILURE
;
958 nextMedia
= IOIteratorNext( mediaIterator
);
961 CFTypeRef bsdPathAsCFString
;
962 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
963 if ( bsdPathAsCFString
) {
964 size_t devPathLength
;
965 strcpy( bsdPath
, _PATH_DEV
);
966 strcat( bsdPath
, "r" );
967 devPathLength
= strlen( bsdPath
);
968 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
969 kernResult
= KERN_SUCCESS
;
971 CFRelease( bsdPathAsCFString
);
973 IOObjectRelease( nextMedia
);
981 static int hdev_probe_device(const char *filename
)
985 /* allow a dedicated CD-ROM driver to match with a higher priority */
986 if (strstart(filename
, "/dev/cdrom", NULL
))
989 if (stat(filename
, &st
) >= 0 &&
990 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
997 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
999 BDRVRawState
*s
= bs
->opaque
;
1002 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1003 kern_return_t kernResult
;
1004 io_iterator_t mediaIterator
;
1005 char bsdPath
[ MAXPATHLEN
];
1008 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1009 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1011 if ( bsdPath
[ 0 ] != '\0' ) {
1012 strcat(bsdPath
,"s0");
1013 /* some CDs don't have a partition 0 */
1014 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1016 bsdPath
[strlen(bsdPath
)-1] = '1';
1023 if ( mediaIterator
)
1024 IOObjectRelease( mediaIterator
);
1028 s
->type
= FTYPE_FILE
;
1029 #if defined(__linux__) && defined(CONFIG_AIO)
1030 if (strstart(filename
, "/dev/sg", NULL
)) {
1035 return raw_open_common(bs
, filename
, flags
, 0);
1038 #if defined(__linux__)
1039 /* Note: we do not have a reliable method to detect if the floppy is
1040 present. The current method is to try to open the floppy at every
1041 I/O and to keep it opened during a few hundreds of ms. */
1042 static int fd_open(BlockDriverState
*bs
)
1044 BDRVRawState
*s
= bs
->opaque
;
1045 int last_media_present
;
1047 if (s
->type
!= FTYPE_FD
)
1049 last_media_present
= (s
->fd
>= 0);
1051 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1055 printf("Floppy closed\n");
1059 if (s
->fd_got_error
&&
1060 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1062 printf("No floppy (open delayed)\n");
1066 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1068 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1069 s
->fd_got_error
= 1;
1070 if (last_media_present
)
1071 s
->fd_media_changed
= 1;
1073 printf("No floppy\n");
1078 printf("Floppy opened\n");
1081 if (!last_media_present
)
1082 s
->fd_media_changed
= 1;
1083 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1084 s
->fd_got_error
= 0;
1088 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1090 BDRVRawState
*s
= bs
->opaque
;
1092 return ioctl(s
->fd
, req
, buf
);
1096 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1097 unsigned long int req
, void *buf
,
1098 BlockDriverCompletionFunc
*cb
, void *opaque
)
1100 BDRVRawState
*s
= bs
->opaque
;
1103 if (fd_open(bs
) < 0)
1106 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1109 acb
->aiocb
.aio_fildes
= s
->fd
;
1110 acb
->aiocb
.ev_signo
= SIGUSR2
;
1111 acb
->aiocb
.aio_offset
= 0;
1112 acb
->aiocb
.aio_flags
= 0;
1114 acb
->next
= posix_aio_state
->first_aio
;
1115 posix_aio_state
->first_aio
= acb
;
1117 acb
->aiocb
.aio_ioctl_buf
= buf
;
1118 acb
->aiocb
.aio_ioctl_cmd
= req
;
1119 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1120 raw_aio_remove(acb
);
1124 return &acb
->common
;
1128 #elif defined(__FreeBSD__)
1129 static int fd_open(BlockDriverState
*bs
)
1131 BDRVRawState
*s
= bs
->opaque
;
1133 /* this is just to ensure s->fd is sane (its called by io ops) */
1138 #else /* !linux && !FreeBSD */
1140 static int fd_open(BlockDriverState
*bs
)
1145 #endif /* !linux && !FreeBSD */
1147 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1151 struct stat stat_buf
;
1152 int64_t total_size
= 0;
1154 /* Read out options */
1155 while (options
&& options
->name
) {
1156 if (!strcmp(options
->name
, "size")) {
1157 total_size
= options
->value
.n
/ 512;
1162 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1166 if (fstat(fd
, &stat_buf
) < 0)
1168 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1170 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1177 static BlockDriver bdrv_host_device
= {
1178 .format_name
= "host_device",
1179 .instance_size
= sizeof(BDRVRawState
),
1180 .bdrv_probe_device
= hdev_probe_device
,
1181 .bdrv_open
= hdev_open
,
1182 .bdrv_close
= raw_close
,
1183 .bdrv_create
= hdev_create
,
1184 .bdrv_flush
= raw_flush
,
1187 .bdrv_aio_readv
= raw_aio_readv
,
1188 .bdrv_aio_writev
= raw_aio_writev
,
1191 .bdrv_read
= raw_read
,
1192 .bdrv_write
= raw_write
,
1193 .bdrv_getlength
= raw_getlength
,
1195 /* generic scsi device */
1197 .bdrv_ioctl
= hdev_ioctl
,
1199 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1205 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1207 BDRVRawState
*s
= bs
->opaque
;
1214 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1215 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1219 /* close fd so that we can reopen it as needed */
1222 s
->fd_media_changed
= 1;
1227 static int floppy_probe_device(const char *filename
)
1229 if (strstart(filename
, "/dev/fd", NULL
))
1235 static int floppy_is_inserted(BlockDriverState
*bs
)
1237 return fd_open(bs
) >= 0;
1240 static int floppy_media_changed(BlockDriverState
*bs
)
1242 BDRVRawState
*s
= bs
->opaque
;
1246 * XXX: we do not have a true media changed indication.
1247 * It does not work if the floppy is changed without trying to read it.
1250 ret
= s
->fd_media_changed
;
1251 s
->fd_media_changed
= 0;
1253 printf("Floppy changed=%d\n", ret
);
1258 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1260 BDRVRawState
*s
= bs
->opaque
;
1267 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1269 if (ioctl(fd
, FDEJECT
, 0) < 0)
1277 static BlockDriver bdrv_host_floppy
= {
1278 .format_name
= "host_floppy",
1279 .instance_size
= sizeof(BDRVRawState
),
1280 .bdrv_probe_device
= floppy_probe_device
,
1281 .bdrv_open
= floppy_open
,
1282 .bdrv_close
= raw_close
,
1283 .bdrv_create
= hdev_create
,
1284 .bdrv_flush
= raw_flush
,
1287 .bdrv_aio_readv
= raw_aio_readv
,
1288 .bdrv_aio_writev
= raw_aio_writev
,
1291 .bdrv_read
= raw_read
,
1292 .bdrv_write
= raw_write
,
1293 .bdrv_getlength
= raw_getlength
,
1295 /* removable device support */
1296 .bdrv_is_inserted
= floppy_is_inserted
,
1297 .bdrv_media_changed
= floppy_media_changed
,
1298 .bdrv_eject
= floppy_eject
,
1301 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1303 BDRVRawState
*s
= bs
->opaque
;
1307 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1308 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1311 static int cdrom_probe_device(const char *filename
)
1313 if (strstart(filename
, "/dev/cd", NULL
))
1318 static int cdrom_is_inserted(BlockDriverState
*bs
)
1320 BDRVRawState
*s
= bs
->opaque
;
1323 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1324 if (ret
== CDS_DISC_OK
)
1329 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1331 BDRVRawState
*s
= bs
->opaque
;
1334 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1335 perror("CDROMEJECT");
1337 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1338 perror("CDROMEJECT");
1344 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1346 BDRVRawState
*s
= bs
->opaque
;
1348 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1350 * Note: an error can happen if the distribution automatically
1353 /* perror("CDROM_LOCKDOOR"); */
1359 static BlockDriver bdrv_host_cdrom
= {
1360 .format_name
= "host_cdrom",
1361 .instance_size
= sizeof(BDRVRawState
),
1362 .bdrv_probe_device
= cdrom_probe_device
,
1363 .bdrv_open
= cdrom_open
,
1364 .bdrv_close
= raw_close
,
1365 .bdrv_create
= hdev_create
,
1366 .bdrv_flush
= raw_flush
,
1369 .bdrv_aio_readv
= raw_aio_readv
,
1370 .bdrv_aio_writev
= raw_aio_writev
,
1373 .bdrv_read
= raw_read
,
1374 .bdrv_write
= raw_write
,
1375 .bdrv_getlength
= raw_getlength
,
1377 /* removable device support */
1378 .bdrv_is_inserted
= cdrom_is_inserted
,
1379 .bdrv_eject
= cdrom_eject
,
1380 .bdrv_set_locked
= cdrom_set_locked
,
1382 /* generic scsi device */
1383 .bdrv_ioctl
= hdev_ioctl
,
1385 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1388 #endif /* __linux__ */
1391 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1393 BDRVRawState
*s
= bs
->opaque
;
1398 ret
= raw_open_common(bs
, filename
, flags
, 0);
1402 /* make sure the door isnt locked at this time */
1403 ioctl(s
->fd
, CDIOCALLOW
);
1407 static int cdrom_probe_device(const char *filename
)
1409 if (strstart(filename
, "/dev/cd", NULL
) ||
1410 strstart(filename
, "/dev/acd", NULL
))
1415 static int cdrom_reopen(BlockDriverState
*bs
)
1417 BDRVRawState
*s
= bs
->opaque
;
1421 * Force reread of possibly changed/newly loaded disc,
1422 * FreeBSD seems to not notice sometimes...
1426 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1433 /* make sure the door isnt locked at this time */
1434 ioctl(s
->fd
, CDIOCALLOW
);
1438 static int cdrom_is_inserted(BlockDriverState
*bs
)
1440 return raw_getlength(bs
) > 0;
1443 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1445 BDRVRawState
*s
= bs
->opaque
;
1450 (void) ioctl(s
->fd
, CDIOCALLOW
);
1453 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1454 perror("CDIOCEJECT");
1456 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1457 perror("CDIOCCLOSE");
1460 if (cdrom_reopen(bs
) < 0)
1465 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1467 BDRVRawState
*s
= bs
->opaque
;
1471 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1473 * Note: an error can happen if the distribution automatically
1476 /* perror("CDROM_LOCKDOOR"); */
1482 static BlockDriver bdrv_host_cdrom
= {
1483 .format_name
= "host_cdrom",
1484 .instance_size
= sizeof(BDRVRawState
),
1485 .bdrv_probe_device
= cdrom_probe_device
,
1486 .bdrv_open
= cdrom_open
,
1487 .bdrv_close
= raw_close
,
1488 .bdrv_create
= hdev_create
,
1489 .bdrv_flush
= raw_flush
,
1492 .bdrv_aio_readv
= raw_aio_readv
,
1493 .bdrv_aio_writev
= raw_aio_writev
,
1496 .bdrv_read
= raw_read
,
1497 .bdrv_write
= raw_write
,
1498 .bdrv_getlength
= raw_getlength
,
1500 /* removable device support */
1501 .bdrv_is_inserted
= cdrom_is_inserted
,
1502 .bdrv_eject
= cdrom_eject
,
1503 .bdrv_set_locked
= cdrom_set_locked
,
1505 #endif /* __FreeBSD__ */
1507 static void bdrv_raw_init(void)
1510 * Register all the drivers. Note that order is important, the driver
1511 * registered last will get probed first.
1513 bdrv_register(&bdrv_raw
);
1514 bdrv_register(&bdrv_host_device
);
1516 bdrv_register(&bdrv_host_floppy
);
1517 bdrv_register(&bdrv_host_cdrom
);
1520 bdrv_register(&bdrv_host_cdrom
);
1524 block_init(bdrv_raw_init
);