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"
30 #include "posix-aio-compat.h"
34 #include <sys/param.h>
35 #include <IOKit/IOKitLib.h>
36 #include <IOKit/IOBSD.h>
37 #include <IOKit/storage/IOMediaBSDClient.h>
38 #include <IOKit/storage/IOMedia.h>
39 #include <IOKit/storage/IOCDMedia.h>
40 //#include <IOKit/storage/IOCDTypes.h>
41 #include <CoreFoundation/CoreFoundation.h>
45 #define _POSIX_PTHREAD_SEMANTICS 1
50 #include <sys/ioctl.h>
51 #include <linux/cdrom.h>
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
67 #include <sys/ioctl.h>
68 #include <sys/diskslice.h>
71 //#define DEBUG_FLOPPY
74 #if defined(DEBUG_BLOCK)
75 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
76 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
78 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
81 /* OS X does not have O_DSYNC */
84 #define O_DSYNC O_SYNC
85 #elif defined(O_FSYNC)
86 #define O_DSYNC O_FSYNC
90 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
92 #define O_DIRECT O_DSYNC
99 #define ALIGNED_BUFFER_SIZE (32 * 512)
101 /* if the FD is not accessed during that time (in ms), we try to
102 reopen it to see if the disk has been changed */
103 #define FD_OPEN_TIMEOUT 1000
105 typedef struct BDRVRawState
{
108 unsigned int lseek_err_cnt
;
110 #if defined(__linux__)
111 /* linux floppy specific */
112 int64_t fd_open_time
;
113 int64_t fd_error_time
;
115 int fd_media_changed
;
117 uint8_t* aligned_buf
;
120 static int posix_aio_init(void);
122 static int fd_open(BlockDriverState
*bs
);
123 static int64_t raw_getlength(BlockDriverState
*bs
);
125 #if defined(__FreeBSD__)
126 static int cdrom_reopen(BlockDriverState
*bs
);
129 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
130 int bdrv_flags
, int open_flags
)
132 BDRVRawState
*s
= bs
->opaque
;
137 s
->lseek_err_cnt
= 0;
139 s
->open_flags
= open_flags
| O_BINARY
;
140 s
->open_flags
&= ~O_ACCMODE
;
141 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
142 s
->open_flags
|= O_RDWR
;
144 s
->open_flags
|= O_RDONLY
;
148 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
149 * and O_DIRECT for no caching. */
150 if ((bdrv_flags
& BDRV_O_NOCACHE
))
151 s
->open_flags
|= O_DIRECT
;
152 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
153 s
->open_flags
|= O_DSYNC
;
156 fd
= open(filename
, s
->open_flags
, 0644);
164 s
->aligned_buf
= NULL
;
165 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
166 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
167 if (s
->aligned_buf
== NULL
) {
176 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
178 BDRVRawState
*s
= bs
->opaque
;
181 s
->type
= FTYPE_FILE
;
182 if (flags
& BDRV_O_CREAT
)
183 open_flags
= O_CREAT
| O_TRUNC
;
185 return raw_open_common(bs
, filename
, flags
, open_flags
);
188 /* XXX: use host sector size if necessary with:
189 #ifdef DIOCGSECTORSIZE
191 unsigned int sectorsize = 512;
192 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
193 sectorsize > bufsize)
194 bufsize = sectorsize;
198 u_int32_t blockSize = 512;
199 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
206 * offset and count are in bytes, but must be multiples of 512 for files
207 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
209 * This function may be called without alignment if the caller ensures
210 * that O_DIRECT is not in effect.
212 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
213 uint8_t *buf
, int count
)
215 BDRVRawState
*s
= bs
->opaque
;
222 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
223 ++(s
->lseek_err_cnt
);
224 if(s
->lseek_err_cnt
<= 10) {
225 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
226 "] lseek failed : %d = %s\n",
227 s
->fd
, bs
->filename
, offset
, buf
, count
,
228 bs
->total_sectors
, errno
, strerror(errno
));
234 ret
= read(s
->fd
, buf
, count
);
236 goto label__raw_read__success
;
238 /* Allow reads beyond the end (needed for pwrite) */
239 if ((ret
== 0) && bs
->growable
) {
240 int64_t size
= raw_getlength(bs
);
241 if (offset
>= size
) {
242 memset(buf
, 0, count
);
244 goto label__raw_read__success
;
248 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
249 "] read failed %d : %d = %s\n",
250 s
->fd
, bs
->filename
, offset
, buf
, count
,
251 bs
->total_sectors
, ret
, errno
, strerror(errno
));
253 /* Try harder for CDrom. */
254 if (bs
->type
== BDRV_TYPE_CDROM
) {
255 lseek(s
->fd
, offset
, SEEK_SET
);
256 ret
= read(s
->fd
, buf
, count
);
258 goto label__raw_read__success
;
259 lseek(s
->fd
, offset
, SEEK_SET
);
260 ret
= read(s
->fd
, buf
, count
);
262 goto label__raw_read__success
;
264 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
265 "] retry read failed %d : %d = %s\n",
266 s
->fd
, bs
->filename
, offset
, buf
, count
,
267 bs
->total_sectors
, ret
, errno
, strerror(errno
));
270 label__raw_read__success
:
272 return (ret
< 0) ? -errno
: ret
;
276 * offset and count are in bytes, but must be multiples of 512 for files
277 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
279 * This function may be called without alignment if the caller ensures
280 * that O_DIRECT is not in effect.
282 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
283 const uint8_t *buf
, int count
)
285 BDRVRawState
*s
= bs
->opaque
;
292 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
293 ++(s
->lseek_err_cnt
);
294 if(s
->lseek_err_cnt
) {
295 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
296 PRId64
"] lseek failed : %d = %s\n",
297 s
->fd
, bs
->filename
, offset
, buf
, count
,
298 bs
->total_sectors
, errno
, strerror(errno
));
302 s
->lseek_err_cnt
= 0;
304 ret
= write(s
->fd
, buf
, count
);
306 goto label__raw_write__success
;
308 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
309 "] write failed %d : %d = %s\n",
310 s
->fd
, bs
->filename
, offset
, buf
, count
,
311 bs
->total_sectors
, ret
, errno
, strerror(errno
));
313 label__raw_write__success
:
315 return (ret
< 0) ? -errno
: ret
;
320 * offset and count are in bytes and possibly not aligned. For files opened
321 * with O_DIRECT, necessary alignments are ensured before calling
322 * raw_pread_aligned to do the actual read.
324 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
325 uint8_t *buf
, int count
)
327 BDRVRawState
*s
= bs
->opaque
;
328 int size
, ret
, shift
, sum
;
332 if (s
->aligned_buf
!= NULL
) {
334 if (offset
& 0x1ff) {
335 /* align offset on a 512 bytes boundary */
337 shift
= offset
& 0x1ff;
338 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
339 if (size
> ALIGNED_BUFFER_SIZE
)
340 size
= ALIGNED_BUFFER_SIZE
;
341 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
348 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
358 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
360 /* read on aligned buffer */
364 size
= (count
+ 0x1ff) & ~0x1ff;
365 if (size
> ALIGNED_BUFFER_SIZE
)
366 size
= ALIGNED_BUFFER_SIZE
;
368 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
376 memcpy(buf
, s
->aligned_buf
, size
);
388 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
391 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
392 uint8_t *buf
, int nb_sectors
)
396 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
397 if (ret
== (nb_sectors
* 512))
403 * offset and count are in bytes and possibly not aligned. For files opened
404 * with O_DIRECT, necessary alignments are ensured before calling
405 * raw_pwrite_aligned to do the actual write.
407 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
408 const uint8_t *buf
, int count
)
410 BDRVRawState
*s
= bs
->opaque
;
411 int size
, ret
, shift
, sum
;
415 if (s
->aligned_buf
!= NULL
) {
417 if (offset
& 0x1ff) {
418 /* align offset on a 512 bytes boundary */
419 shift
= offset
& 0x1ff;
420 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
427 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
429 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
441 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
443 while ((size
= (count
& ~0x1ff)) != 0) {
445 if (size
> ALIGNED_BUFFER_SIZE
)
446 size
= ALIGNED_BUFFER_SIZE
;
448 memcpy(s
->aligned_buf
, buf
, size
);
450 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
459 /* here, count < 512 because (count & ~0x1ff) == 0 */
461 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
464 memcpy(s
->aligned_buf
, buf
, count
);
466 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
477 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
480 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
481 const uint8_t *buf
, int nb_sectors
)
484 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
485 if (ret
== (nb_sectors
* 512))
490 /***********************************************************/
491 /* Unix AIO using POSIX AIO */
493 typedef struct RawAIOCB
{
494 BlockDriverAIOCB common
;
495 struct qemu_paiocb aiocb
;
496 struct RawAIOCB
*next
;
500 typedef struct PosixAioState
506 static void posix_aio_read(void *opaque
)
508 PosixAioState
*s
= opaque
;
509 RawAIOCB
*acb
, **pacb
;
513 /* read all bytes from signal pipe */
517 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
518 if (len
== -1 && errno
== EINTR
)
519 continue; /* try again */
520 if (len
== sizeof(bytes
))
521 continue; /* more to read */
526 pacb
= &s
->first_aio
;
531 ret
= qemu_paio_error(&acb
->aiocb
);
532 if (ret
== ECANCELED
) {
533 /* remove the request */
535 qemu_aio_release(acb
);
536 } else if (ret
!= EINPROGRESS
) {
539 ret
= qemu_paio_return(&acb
->aiocb
);
540 if (ret
== acb
->aiocb
.aio_nbytes
)
547 /* remove the request */
549 /* call the callback */
550 acb
->common
.cb(acb
->common
.opaque
, ret
);
551 qemu_aio_release(acb
);
561 static int posix_aio_flush(void *opaque
)
563 PosixAioState
*s
= opaque
;
564 return !!s
->first_aio
;
567 static PosixAioState
*posix_aio_state
;
569 static void aio_signal_handler(int signum
)
571 if (posix_aio_state
) {
574 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
580 static int posix_aio_init(void)
582 struct sigaction act
;
585 struct qemu_paioinit ai
;
590 s
= qemu_malloc(sizeof(PosixAioState
));
592 sigfillset(&act
.sa_mask
);
593 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
594 act
.sa_handler
= aio_signal_handler
;
595 sigaction(SIGUSR2
, &act
, NULL
);
598 if (pipe(fds
) == -1) {
599 fprintf(stderr
, "failed to create pipe\n");
606 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
607 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
609 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
611 memset(&ai
, 0, sizeof(ai
));
621 static void raw_aio_remove(RawAIOCB
*acb
)
625 /* remove the callback from the queue */
626 pacb
= &posix_aio_state
->first_aio
;
629 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
631 } else if (*pacb
== acb
) {
633 qemu_aio_release(acb
);
636 pacb
= &(*pacb
)->next
;
640 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
643 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
645 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
646 if (ret
== QEMU_PAIO_NOTCANCELED
) {
647 /* fail safe: if the aio could not be canceled, we wait for
649 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
655 static AIOPool raw_aio_pool
= {
656 .aiocb_size
= sizeof(RawAIOCB
),
657 .cancel
= raw_aio_cancel
,
660 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
661 QEMUIOVector
*qiov
, int nb_sectors
,
662 BlockDriverCompletionFunc
*cb
, void *opaque
)
664 BDRVRawState
*s
= bs
->opaque
;
670 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
673 acb
->aiocb
.aio_fildes
= s
->fd
;
674 acb
->aiocb
.ev_signo
= SIGUSR2
;
675 acb
->aiocb
.aio_iov
= qiov
->iov
;
676 acb
->aiocb
.aio_niov
= qiov
->niov
;
677 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
678 acb
->aiocb
.aio_offset
= sector_num
* 512;
679 acb
->aiocb
.aio_flags
= 0;
682 * If O_DIRECT is used the buffer needs to be aligned on a sector
683 * boundary. Tell the low level code to ensure that in case it's
687 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
689 acb
->next
= posix_aio_state
->first_aio
;
690 posix_aio_state
->first_aio
= acb
;
694 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
695 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
696 BlockDriverCompletionFunc
*cb
, void *opaque
)
700 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
703 if (qemu_paio_read(&acb
->aiocb
) < 0) {
710 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
711 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
712 BlockDriverCompletionFunc
*cb
, void *opaque
)
716 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
719 if (qemu_paio_write(&acb
->aiocb
) < 0) {
726 static void raw_close(BlockDriverState
*bs
)
728 BDRVRawState
*s
= bs
->opaque
;
732 if (s
->aligned_buf
!= NULL
)
733 qemu_free(s
->aligned_buf
);
737 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
739 BDRVRawState
*s
= bs
->opaque
;
740 if (s
->type
!= FTYPE_FILE
)
742 if (ftruncate(s
->fd
, offset
) < 0)
748 static int64_t raw_getlength(BlockDriverState
*bs
)
750 BDRVRawState
*s
= bs
->opaque
;
756 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
759 if (ioctl(fd
, DIOCGDINFO
, &dl
))
761 return (uint64_t)dl
.d_secsize
*
762 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
766 #else /* !__OpenBSD__ */
767 static int64_t raw_getlength(BlockDriverState
*bs
)
769 BDRVRawState
*s
= bs
->opaque
;
779 struct dk_minfo minfo
;
792 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
793 #ifdef DIOCGMEDIASIZE
794 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
795 #elif defined(DIOCGPART)
798 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
799 size
= pi
.media_size
;
806 size
= LONG_LONG_MAX
;
808 size
= lseek(fd
, 0LL, SEEK_END
);
813 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
814 if (size
== 2048LL * (unsigned)-1)
816 /* XXX no disc? maybe we need to reopen... */
817 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
827 * use the DKIOCGMEDIAINFO ioctl to read the size.
829 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
831 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
832 } else /* there are reports that lseek on some devices
833 fails, but irc discussion said that contingency
834 on contingency was overkill */
837 size
= lseek(fd
, 0, SEEK_END
);
843 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
847 int64_t total_size
= 0;
849 /* Read out options */
850 while (options
&& options
->name
) {
851 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
852 total_size
= options
->value
.n
/ 512;
857 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
862 if (ftruncate(fd
, total_size
* 512) != 0) {
865 if (close(fd
) != 0) {
872 static void raw_flush(BlockDriverState
*bs
)
874 BDRVRawState
*s
= bs
->opaque
;
879 static QEMUOptionParameter raw_create_options
[] = {
881 .name
= BLOCK_OPT_SIZE
,
883 .help
= "Virtual disk size"
888 static BlockDriver bdrv_raw
= {
889 .format_name
= "raw",
890 .instance_size
= sizeof(BDRVRawState
),
891 .bdrv_probe
= NULL
, /* no probe for protocols */
892 .bdrv_open
= raw_open
,
893 .bdrv_read
= raw_read
,
894 .bdrv_write
= raw_write
,
895 .bdrv_close
= raw_close
,
896 .bdrv_create
= raw_create
,
897 .bdrv_flush
= raw_flush
,
899 .bdrv_aio_readv
= raw_aio_readv
,
900 .bdrv_aio_writev
= raw_aio_writev
,
902 .bdrv_truncate
= raw_truncate
,
903 .bdrv_getlength
= raw_getlength
,
905 .create_options
= raw_create_options
,
908 /***********************************************/
912 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
913 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
915 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
917 kern_return_t kernResult
;
918 mach_port_t masterPort
;
919 CFMutableDictionaryRef classesToMatch
;
921 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
922 if ( KERN_SUCCESS
!= kernResult
) {
923 printf( "IOMasterPort returned %d\n", kernResult
);
926 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
927 if ( classesToMatch
== NULL
) {
928 printf( "IOServiceMatching returned a NULL dictionary.\n" );
930 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
932 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
933 if ( KERN_SUCCESS
!= kernResult
)
935 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
941 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
943 io_object_t nextMedia
;
944 kern_return_t kernResult
= KERN_FAILURE
;
946 nextMedia
= IOIteratorNext( mediaIterator
);
949 CFTypeRef bsdPathAsCFString
;
950 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
951 if ( bsdPathAsCFString
) {
952 size_t devPathLength
;
953 strcpy( bsdPath
, _PATH_DEV
);
954 strcat( bsdPath
, "r" );
955 devPathLength
= strlen( bsdPath
);
956 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
957 kernResult
= KERN_SUCCESS
;
959 CFRelease( bsdPathAsCFString
);
961 IOObjectRelease( nextMedia
);
969 static int hdev_probe_device(const char *filename
)
973 /* allow a dedicated CD-ROM driver to match with a higher priority */
974 if (strstart(filename
, "/dev/cdrom", NULL
))
977 if (stat(filename
, &st
) >= 0 &&
978 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
985 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
987 BDRVRawState
*s
= bs
->opaque
;
990 if (strstart(filename
, "/dev/cdrom", NULL
)) {
991 kern_return_t kernResult
;
992 io_iterator_t mediaIterator
;
993 char bsdPath
[ MAXPATHLEN
];
996 kernResult
= FindEjectableCDMedia( &mediaIterator
);
997 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
999 if ( bsdPath
[ 0 ] != '\0' ) {
1000 strcat(bsdPath
,"s0");
1001 /* some CDs don't have a partition 0 */
1002 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1004 bsdPath
[strlen(bsdPath
)-1] = '1';
1011 if ( mediaIterator
)
1012 IOObjectRelease( mediaIterator
);
1016 s
->type
= FTYPE_FILE
;
1017 #if defined(__linux__)
1018 if (strstart(filename
, "/dev/sg", NULL
)) {
1023 return raw_open_common(bs
, filename
, flags
, 0);
1026 #if defined(__linux__)
1027 /* Note: we do not have a reliable method to detect if the floppy is
1028 present. The current method is to try to open the floppy at every
1029 I/O and to keep it opened during a few hundreds of ms. */
1030 static int fd_open(BlockDriverState
*bs
)
1032 BDRVRawState
*s
= bs
->opaque
;
1033 int last_media_present
;
1035 if (s
->type
!= FTYPE_FD
)
1037 last_media_present
= (s
->fd
>= 0);
1039 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1043 printf("Floppy closed\n");
1047 if (s
->fd_got_error
&&
1048 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1050 printf("No floppy (open delayed)\n");
1054 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1056 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1057 s
->fd_got_error
= 1;
1058 if (last_media_present
)
1059 s
->fd_media_changed
= 1;
1061 printf("No floppy\n");
1066 printf("Floppy opened\n");
1069 if (!last_media_present
)
1070 s
->fd_media_changed
= 1;
1071 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1072 s
->fd_got_error
= 0;
1076 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1078 BDRVRawState
*s
= bs
->opaque
;
1080 return ioctl(s
->fd
, req
, buf
);
1083 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1084 unsigned long int req
, void *buf
,
1085 BlockDriverCompletionFunc
*cb
, void *opaque
)
1087 BDRVRawState
*s
= bs
->opaque
;
1090 if (fd_open(bs
) < 0)
1093 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1096 acb
->aiocb
.aio_fildes
= s
->fd
;
1097 acb
->aiocb
.ev_signo
= SIGUSR2
;
1098 acb
->aiocb
.aio_offset
= 0;
1099 acb
->aiocb
.aio_flags
= 0;
1101 acb
->next
= posix_aio_state
->first_aio
;
1102 posix_aio_state
->first_aio
= acb
;
1104 acb
->aiocb
.aio_ioctl_buf
= buf
;
1105 acb
->aiocb
.aio_ioctl_cmd
= req
;
1106 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1107 raw_aio_remove(acb
);
1111 return &acb
->common
;
1114 #elif defined(__FreeBSD__)
1115 static int fd_open(BlockDriverState
*bs
)
1117 BDRVRawState
*s
= bs
->opaque
;
1119 /* this is just to ensure s->fd is sane (its called by io ops) */
1124 #else /* !linux && !FreeBSD */
1126 static int fd_open(BlockDriverState
*bs
)
1131 #endif /* !linux && !FreeBSD */
1133 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1137 struct stat stat_buf
;
1138 int64_t total_size
= 0;
1140 /* Read out options */
1141 while (options
&& options
->name
) {
1142 if (!strcmp(options
->name
, "size")) {
1143 total_size
= options
->value
.n
/ 512;
1148 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1152 if (fstat(fd
, &stat_buf
) < 0)
1154 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1156 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1163 static BlockDriver bdrv_host_device
= {
1164 .format_name
= "host_device",
1165 .instance_size
= sizeof(BDRVRawState
),
1166 .bdrv_probe_device
= hdev_probe_device
,
1167 .bdrv_open
= hdev_open
,
1168 .bdrv_close
= raw_close
,
1169 .bdrv_create
= hdev_create
,
1170 .bdrv_flush
= raw_flush
,
1172 .bdrv_aio_readv
= raw_aio_readv
,
1173 .bdrv_aio_writev
= raw_aio_writev
,
1175 .bdrv_read
= raw_read
,
1176 .bdrv_write
= raw_write
,
1177 .bdrv_getlength
= raw_getlength
,
1179 /* generic scsi device */
1181 .bdrv_ioctl
= hdev_ioctl
,
1182 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1187 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1189 BDRVRawState
*s
= bs
->opaque
;
1196 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1197 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1201 /* close fd so that we can reopen it as needed */
1204 s
->fd_media_changed
= 1;
1209 static int floppy_probe_device(const char *filename
)
1211 if (strstart(filename
, "/dev/fd", NULL
))
1217 static int floppy_is_inserted(BlockDriverState
*bs
)
1219 return fd_open(bs
) >= 0;
1222 static int floppy_media_changed(BlockDriverState
*bs
)
1224 BDRVRawState
*s
= bs
->opaque
;
1228 * XXX: we do not have a true media changed indication.
1229 * It does not work if the floppy is changed without trying to read it.
1232 ret
= s
->fd_media_changed
;
1233 s
->fd_media_changed
= 0;
1235 printf("Floppy changed=%d\n", ret
);
1240 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1242 BDRVRawState
*s
= bs
->opaque
;
1249 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1251 if (ioctl(fd
, FDEJECT
, 0) < 0)
1259 static BlockDriver bdrv_host_floppy
= {
1260 .format_name
= "host_floppy",
1261 .instance_size
= sizeof(BDRVRawState
),
1262 .bdrv_probe_device
= floppy_probe_device
,
1263 .bdrv_open
= floppy_open
,
1264 .bdrv_close
= raw_close
,
1265 .bdrv_create
= hdev_create
,
1266 .bdrv_flush
= raw_flush
,
1268 .bdrv_aio_readv
= raw_aio_readv
,
1269 .bdrv_aio_writev
= raw_aio_writev
,
1271 .bdrv_read
= raw_read
,
1272 .bdrv_write
= raw_write
,
1273 .bdrv_getlength
= raw_getlength
,
1275 /* removable device support */
1276 .bdrv_is_inserted
= floppy_is_inserted
,
1277 .bdrv_media_changed
= floppy_media_changed
,
1278 .bdrv_eject
= floppy_eject
,
1281 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1283 BDRVRawState
*s
= bs
->opaque
;
1287 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1288 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1291 static int cdrom_probe_device(const char *filename
)
1293 if (strstart(filename
, "/dev/cd", NULL
))
1298 static int cdrom_is_inserted(BlockDriverState
*bs
)
1300 BDRVRawState
*s
= bs
->opaque
;
1303 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1304 if (ret
== CDS_DISC_OK
)
1309 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1311 BDRVRawState
*s
= bs
->opaque
;
1314 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1315 perror("CDROMEJECT");
1317 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1318 perror("CDROMEJECT");
1324 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1326 BDRVRawState
*s
= bs
->opaque
;
1328 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1330 * Note: an error can happen if the distribution automatically
1333 /* perror("CDROM_LOCKDOOR"); */
1339 static BlockDriver bdrv_host_cdrom
= {
1340 .format_name
= "host_cdrom",
1341 .instance_size
= sizeof(BDRVRawState
),
1342 .bdrv_probe_device
= cdrom_probe_device
,
1343 .bdrv_open
= cdrom_open
,
1344 .bdrv_close
= raw_close
,
1345 .bdrv_create
= hdev_create
,
1346 .bdrv_flush
= raw_flush
,
1348 .bdrv_aio_readv
= raw_aio_readv
,
1349 .bdrv_aio_writev
= raw_aio_writev
,
1351 .bdrv_read
= raw_read
,
1352 .bdrv_write
= raw_write
,
1353 .bdrv_getlength
= raw_getlength
,
1355 /* removable device support */
1356 .bdrv_is_inserted
= cdrom_is_inserted
,
1357 .bdrv_eject
= cdrom_eject
,
1358 .bdrv_set_locked
= cdrom_set_locked
,
1360 /* generic scsi device */
1361 .bdrv_ioctl
= hdev_ioctl
,
1362 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1364 #endif /* __linux__ */
1367 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1369 BDRVRawState
*s
= bs
->opaque
;
1374 ret
= raw_open_common(bs
, filename
, flags
, 0);
1378 /* make sure the door isnt locked at this time */
1379 ioctl(s
->fd
, CDIOCALLOW
);
1383 static int cdrom_probe_device(const char *filename
)
1385 if (strstart(filename
, "/dev/cd", NULL
) ||
1386 strstart(filename
, "/dev/acd", NULL
))
1391 static int cdrom_reopen(BlockDriverState
*bs
)
1393 BDRVRawState
*s
= bs
->opaque
;
1397 * Force reread of possibly changed/newly loaded disc,
1398 * FreeBSD seems to not notice sometimes...
1402 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1409 /* make sure the door isnt locked at this time */
1410 ioctl(s
->fd
, CDIOCALLOW
);
1414 static int cdrom_is_inserted(BlockDriverState
*bs
)
1416 return raw_getlength(bs
) > 0;
1419 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1421 BDRVRawState
*s
= bs
->opaque
;
1426 (void) ioctl(s
->fd
, CDIOCALLOW
);
1429 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1430 perror("CDIOCEJECT");
1432 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1433 perror("CDIOCCLOSE");
1436 if (cdrom_reopen(bs
) < 0)
1441 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1443 BDRVRawState
*s
= bs
->opaque
;
1447 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1449 * Note: an error can happen if the distribution automatically
1452 /* perror("CDROM_LOCKDOOR"); */
1458 static BlockDriver bdrv_host_cdrom
= {
1459 .format_name
= "host_cdrom",
1460 .instance_size
= sizeof(BDRVRawState
),
1461 .bdrv_probe_device
= cdrom_probe_device
,
1462 .bdrv_open
= cdrom_open
,
1463 .bdrv_close
= raw_close
,
1464 .bdrv_create
= hdev_create
,
1465 .bdrv_flush
= raw_flush
,
1467 .bdrv_aio_readv
= raw_aio_readv
,
1468 .bdrv_aio_writev
= raw_aio_writev
,
1470 .bdrv_read
= raw_read
,
1471 .bdrv_write
= raw_write
,
1472 .bdrv_getlength
= raw_getlength
,
1474 /* removable device support */
1475 .bdrv_is_inserted
= cdrom_is_inserted
,
1476 .bdrv_eject
= cdrom_eject
,
1477 .bdrv_set_locked
= cdrom_set_locked
,
1479 #endif /* __FreeBSD__ */
1481 static void bdrv_raw_init(void)
1484 * Register all the drivers. Note that order is important, the driver
1485 * registered last will get probed first.
1487 bdrv_register(&bdrv_raw
);
1488 bdrv_register(&bdrv_host_device
);
1490 bdrv_register(&bdrv_host_floppy
);
1491 bdrv_register(&bdrv_host_cdrom
);
1494 bdrv_register(&bdrv_host_cdrom
);
1498 block_init(bdrv_raw_init
);