2 * Block driver for RAW files (posix)
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "qemu-char.h"
27 #include "block_int.h"
30 #include "posix-aio-compat.h"
35 #include <sys/param.h>
36 #include <IOKit/IOKitLib.h>
37 #include <IOKit/IOBSD.h>
38 #include <IOKit/storage/IOMediaBSDClient.h>
39 #include <IOKit/storage/IOMedia.h>
40 #include <IOKit/storage/IOCDMedia.h>
41 //#include <IOKit/storage/IOCDTypes.h>
42 #include <CoreFoundation/CoreFoundation.h>
46 #define _POSIX_PTHREAD_SEMANTICS 1
51 #include <sys/ioctl.h>
52 #include <linux/cdrom.h>
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
68 #include <sys/ioctl.h>
69 #include <sys/diskslice.h>
72 //#define DEBUG_FLOPPY
75 #if defined(DEBUG_BLOCK)
76 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
77 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
79 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
82 /* OS X does not have O_DSYNC */
84 #define O_DSYNC O_SYNC
87 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
89 #define O_DIRECT O_DSYNC
96 #define ALIGNED_BUFFER_SIZE (32 * 512)
98 /* if the FD is not accessed during that time (in ms), we try to
99 reopen it to see if the disk has been changed */
100 #define FD_OPEN_TIMEOUT 1000
102 typedef struct BDRVRawState
{
105 unsigned int lseek_err_cnt
;
107 #if defined(__linux__)
108 /* linux floppy specific */
109 int64_t fd_open_time
;
110 int64_t fd_error_time
;
112 int fd_media_changed
;
114 uint8_t* aligned_buf
;
117 static int posix_aio_init(void);
119 static int fd_open(BlockDriverState
*bs
);
120 static int64_t raw_getlength(BlockDriverState
*bs
);
122 #if defined(__FreeBSD__)
123 static int cdrom_reopen(BlockDriverState
*bs
);
126 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
127 int bdrv_flags
, int open_flags
)
129 BDRVRawState
*s
= bs
->opaque
;
134 s
->lseek_err_cnt
= 0;
136 s
->open_flags
= open_flags
| O_BINARY
;
137 s
->open_flags
&= ~O_ACCMODE
;
138 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
139 s
->open_flags
|= O_RDWR
;
141 s
->open_flags
|= O_RDONLY
;
145 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
146 * and O_DIRECT for no caching. */
147 if ((bdrv_flags
& BDRV_O_NOCACHE
))
148 s
->open_flags
|= O_DIRECT
;
149 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
150 s
->open_flags
|= O_DSYNC
;
153 fd
= open(filename
, s
->open_flags
, 0644);
161 s
->aligned_buf
= NULL
;
162 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
163 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
164 if (s
->aligned_buf
== NULL
) {
173 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
175 BDRVRawState
*s
= bs
->opaque
;
178 s
->type
= FTYPE_FILE
;
179 if (flags
& BDRV_O_CREAT
)
180 open_flags
= O_CREAT
| O_TRUNC
;
182 return raw_open_common(bs
, filename
, flags
, open_flags
);
185 /* XXX: use host sector size if necessary with:
186 #ifdef DIOCGSECTORSIZE
188 unsigned int sectorsize = 512;
189 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
190 sectorsize > bufsize)
191 bufsize = sectorsize;
195 u_int32_t blockSize = 512;
196 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
203 * offset and count are in bytes, but must be multiples of 512 for files
204 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
206 * This function may be called without alignment if the caller ensures
207 * that O_DIRECT is not in effect.
209 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
210 uint8_t *buf
, int count
)
212 BDRVRawState
*s
= bs
->opaque
;
219 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
220 ++(s
->lseek_err_cnt
);
221 if(s
->lseek_err_cnt
<= 10) {
222 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
223 "] lseek failed : %d = %s\n",
224 s
->fd
, bs
->filename
, offset
, buf
, count
,
225 bs
->total_sectors
, errno
, strerror(errno
));
231 ret
= read(s
->fd
, buf
, count
);
233 goto label__raw_read__success
;
235 /* Allow reads beyond the end (needed for pwrite) */
236 if ((ret
== 0) && bs
->growable
) {
237 int64_t size
= raw_getlength(bs
);
238 if (offset
>= size
) {
239 memset(buf
, 0, count
);
241 goto label__raw_read__success
;
245 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
246 "] read failed %d : %d = %s\n",
247 s
->fd
, bs
->filename
, offset
, buf
, count
,
248 bs
->total_sectors
, ret
, errno
, strerror(errno
));
250 /* Try harder for CDrom. */
251 if (bs
->type
== BDRV_TYPE_CDROM
) {
252 lseek(s
->fd
, offset
, SEEK_SET
);
253 ret
= read(s
->fd
, buf
, count
);
255 goto label__raw_read__success
;
256 lseek(s
->fd
, offset
, SEEK_SET
);
257 ret
= read(s
->fd
, buf
, count
);
259 goto label__raw_read__success
;
261 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
262 "] retry read failed %d : %d = %s\n",
263 s
->fd
, bs
->filename
, offset
, buf
, count
,
264 bs
->total_sectors
, ret
, errno
, strerror(errno
));
267 label__raw_read__success
:
269 return (ret
< 0) ? -errno
: ret
;
273 * offset and count are in bytes, but must be multiples of 512 for files
274 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
276 * This function may be called without alignment if the caller ensures
277 * that O_DIRECT is not in effect.
279 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
280 const uint8_t *buf
, int count
)
282 BDRVRawState
*s
= bs
->opaque
;
289 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
290 ++(s
->lseek_err_cnt
);
291 if(s
->lseek_err_cnt
) {
292 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
293 PRId64
"] lseek failed : %d = %s\n",
294 s
->fd
, bs
->filename
, offset
, buf
, count
,
295 bs
->total_sectors
, errno
, strerror(errno
));
299 s
->lseek_err_cnt
= 0;
301 ret
= write(s
->fd
, buf
, count
);
303 goto label__raw_write__success
;
305 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
306 "] write failed %d : %d = %s\n",
307 s
->fd
, bs
->filename
, offset
, buf
, count
,
308 bs
->total_sectors
, ret
, errno
, strerror(errno
));
310 label__raw_write__success
:
312 return (ret
< 0) ? -errno
: ret
;
317 * offset and count are in bytes and possibly not aligned. For files opened
318 * with O_DIRECT, necessary alignments are ensured before calling
319 * raw_pread_aligned to do the actual read.
321 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
322 uint8_t *buf
, int count
)
324 BDRVRawState
*s
= bs
->opaque
;
325 int size
, ret
, shift
, sum
;
329 if (s
->aligned_buf
!= NULL
) {
331 if (offset
& 0x1ff) {
332 /* align offset on a 512 bytes boundary */
334 shift
= offset
& 0x1ff;
335 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
336 if (size
> ALIGNED_BUFFER_SIZE
)
337 size
= ALIGNED_BUFFER_SIZE
;
338 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
345 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
355 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
357 /* read on aligned buffer */
361 size
= (count
+ 0x1ff) & ~0x1ff;
362 if (size
> ALIGNED_BUFFER_SIZE
)
363 size
= ALIGNED_BUFFER_SIZE
;
365 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
373 memcpy(buf
, s
->aligned_buf
, size
);
385 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
388 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
389 uint8_t *buf
, int nb_sectors
)
393 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
394 if (ret
== (nb_sectors
* 512))
400 * offset and count are in bytes and possibly not aligned. For files opened
401 * with O_DIRECT, necessary alignments are ensured before calling
402 * raw_pwrite_aligned to do the actual write.
404 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
405 const uint8_t *buf
, int count
)
407 BDRVRawState
*s
= bs
->opaque
;
408 int size
, ret
, shift
, sum
;
412 if (s
->aligned_buf
!= NULL
) {
414 if (offset
& 0x1ff) {
415 /* align offset on a 512 bytes boundary */
416 shift
= offset
& 0x1ff;
417 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
424 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
426 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
438 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
440 while ((size
= (count
& ~0x1ff)) != 0) {
442 if (size
> ALIGNED_BUFFER_SIZE
)
443 size
= ALIGNED_BUFFER_SIZE
;
445 memcpy(s
->aligned_buf
, buf
, size
);
447 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
456 /* here, count < 512 because (count & ~0x1ff) == 0 */
458 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
461 memcpy(s
->aligned_buf
, buf
, count
);
463 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
474 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
477 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
478 const uint8_t *buf
, int nb_sectors
)
481 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
482 if (ret
== (nb_sectors
* 512))
488 /***********************************************************/
489 /* Unix AIO using POSIX AIO */
491 typedef struct RawAIOCB
{
492 BlockDriverAIOCB common
;
493 struct qemu_paiocb aiocb
;
494 struct RawAIOCB
*next
;
498 typedef struct PosixAioState
504 static void posix_aio_read(void *opaque
)
506 PosixAioState
*s
= opaque
;
507 RawAIOCB
*acb
, **pacb
;
511 /* read all bytes from signal pipe */
515 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
516 if (len
== -1 && errno
== EINTR
)
517 continue; /* try again */
518 if (len
== sizeof(bytes
))
519 continue; /* more to read */
524 pacb
= &s
->first_aio
;
529 ret
= qemu_paio_error(&acb
->aiocb
);
530 if (ret
== ECANCELED
) {
531 /* remove the request */
533 qemu_aio_release(acb
);
534 } else if (ret
!= EINPROGRESS
) {
537 ret
= qemu_paio_return(&acb
->aiocb
);
538 if (ret
== acb
->aiocb
.aio_nbytes
)
545 /* remove the request */
547 /* call the callback */
548 acb
->common
.cb(acb
->common
.opaque
, ret
);
549 qemu_aio_release(acb
);
559 static int posix_aio_flush(void *opaque
)
561 PosixAioState
*s
= opaque
;
562 return !!s
->first_aio
;
565 static PosixAioState
*posix_aio_state
;
567 static void aio_signal_handler(int signum
)
569 if (posix_aio_state
) {
572 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
578 static int posix_aio_init(void)
580 struct sigaction act
;
583 struct qemu_paioinit ai
;
588 s
= qemu_malloc(sizeof(PosixAioState
));
590 sigfillset(&act
.sa_mask
);
591 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
592 act
.sa_handler
= aio_signal_handler
;
593 sigaction(SIGUSR2
, &act
, NULL
);
596 if (pipe(fds
) == -1) {
597 fprintf(stderr
, "failed to create pipe\n");
604 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
605 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
607 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
609 memset(&ai
, 0, sizeof(ai
));
619 static void raw_aio_remove(RawAIOCB
*acb
)
623 /* remove the callback from the queue */
624 pacb
= &posix_aio_state
->first_aio
;
627 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
629 } else if (*pacb
== acb
) {
631 qemu_aio_release(acb
);
634 pacb
= &(*pacb
)->next
;
638 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
641 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
643 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
644 if (ret
== QEMU_PAIO_NOTCANCELED
) {
645 /* fail safe: if the aio could not be canceled, we wait for
647 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
653 static AIOPool raw_aio_pool
= {
654 .aiocb_size
= sizeof(RawAIOCB
),
655 .cancel
= raw_aio_cancel
,
658 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
659 QEMUIOVector
*qiov
, int nb_sectors
,
660 BlockDriverCompletionFunc
*cb
, void *opaque
)
662 BDRVRawState
*s
= bs
->opaque
;
668 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
671 acb
->aiocb
.aio_fildes
= s
->fd
;
672 acb
->aiocb
.ev_signo
= SIGUSR2
;
673 acb
->aiocb
.aio_iov
= qiov
->iov
;
674 acb
->aiocb
.aio_niov
= qiov
->niov
;
675 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
676 acb
->aiocb
.aio_offset
= sector_num
* 512;
677 acb
->aiocb
.aio_flags
= 0;
680 * If O_DIRECT is used the buffer needs to be aligned on a sector
681 * boundary. Tell the low level code to ensure that in case it's
685 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
687 acb
->next
= posix_aio_state
->first_aio
;
688 posix_aio_state
->first_aio
= acb
;
692 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
693 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
694 BlockDriverCompletionFunc
*cb
, void *opaque
)
698 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
701 if (qemu_paio_read(&acb
->aiocb
) < 0) {
708 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
709 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
710 BlockDriverCompletionFunc
*cb
, void *opaque
)
714 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
717 if (qemu_paio_write(&acb
->aiocb
) < 0) {
723 #else /* CONFIG_AIO */
724 static int posix_aio_init(void)
728 #endif /* CONFIG_AIO */
731 static void raw_close(BlockDriverState
*bs
)
733 BDRVRawState
*s
= bs
->opaque
;
737 if (s
->aligned_buf
!= NULL
)
738 qemu_free(s
->aligned_buf
);
742 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
744 BDRVRawState
*s
= bs
->opaque
;
745 if (s
->type
!= FTYPE_FILE
)
747 if (ftruncate(s
->fd
, offset
) < 0)
753 static int64_t raw_getlength(BlockDriverState
*bs
)
755 BDRVRawState
*s
= bs
->opaque
;
761 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
764 if (ioctl(fd
, DIOCGDINFO
, &dl
))
766 return (uint64_t)dl
.d_secsize
*
767 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
771 #else /* !__OpenBSD__ */
772 static int64_t raw_getlength(BlockDriverState
*bs
)
774 BDRVRawState
*s
= bs
->opaque
;
784 struct dk_minfo minfo
;
797 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
798 #ifdef DIOCGMEDIASIZE
799 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
800 #elif defined(DIOCGPART)
803 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
804 size
= pi
.media_size
;
811 size
= LONG_LONG_MAX
;
813 size
= lseek(fd
, 0LL, SEEK_END
);
818 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
819 if (size
== 2048LL * (unsigned)-1)
821 /* XXX no disc? maybe we need to reopen... */
822 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
832 * use the DKIOCGMEDIAINFO ioctl to read the size.
834 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
836 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
837 } else /* there are reports that lseek on some devices
838 fails, but irc discussion said that contingency
839 on contingency was overkill */
842 size
= lseek(fd
, 0, SEEK_END
);
848 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
851 int64_t total_size
= 0;
853 /* Read out options */
854 while (options
&& options
->name
) {
855 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
856 total_size
= options
->value
.n
/ 512;
861 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
865 ftruncate(fd
, total_size
* 512);
870 static void raw_flush(BlockDriverState
*bs
)
872 BDRVRawState
*s
= bs
->opaque
;
877 static QEMUOptionParameter raw_create_options
[] = {
879 .name
= BLOCK_OPT_SIZE
,
881 .help
= "Virtual disk size"
886 static BlockDriver bdrv_raw
= {
887 .format_name
= "raw",
888 .instance_size
= sizeof(BDRVRawState
),
889 .bdrv_probe
= NULL
, /* no probe for protocols */
890 .bdrv_open
= raw_open
,
891 .bdrv_read
= raw_read
,
892 .bdrv_write
= raw_write
,
893 .bdrv_close
= raw_close
,
894 .bdrv_create
= raw_create
,
895 .bdrv_flush
= raw_flush
,
898 .bdrv_aio_readv
= raw_aio_readv
,
899 .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__) && defined(CONFIG_AIO)
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
);
1084 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1085 unsigned long int req
, void *buf
,
1086 BlockDriverCompletionFunc
*cb
, void *opaque
)
1088 BDRVRawState
*s
= bs
->opaque
;
1091 if (fd_open(bs
) < 0)
1094 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1097 acb
->aiocb
.aio_fildes
= s
->fd
;
1098 acb
->aiocb
.ev_signo
= SIGUSR2
;
1099 acb
->aiocb
.aio_offset
= 0;
1100 acb
->aiocb
.aio_flags
= 0;
1102 acb
->next
= posix_aio_state
->first_aio
;
1103 posix_aio_state
->first_aio
= acb
;
1105 acb
->aiocb
.aio_ioctl_buf
= buf
;
1106 acb
->aiocb
.aio_ioctl_cmd
= req
;
1107 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1108 raw_aio_remove(acb
);
1112 return &acb
->common
;
1116 #elif defined(__FreeBSD__)
1117 static int fd_open(BlockDriverState
*bs
)
1119 BDRVRawState
*s
= bs
->opaque
;
1121 /* this is just to ensure s->fd is sane (its called by io ops) */
1126 #else /* !linux && !FreeBSD */
1128 static int fd_open(BlockDriverState
*bs
)
1133 #endif /* !linux && !FreeBSD */
1135 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1139 struct stat stat_buf
;
1140 int64_t total_size
= 0;
1142 /* Read out options */
1143 while (options
&& options
->name
) {
1144 if (!strcmp(options
->name
, "size")) {
1145 total_size
= options
->value
.n
/ 512;
1150 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1154 if (fstat(fd
, &stat_buf
) < 0)
1156 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1158 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1165 static BlockDriver bdrv_host_device
= {
1166 .format_name
= "host_device",
1167 .instance_size
= sizeof(BDRVRawState
),
1168 .bdrv_probe_device
= hdev_probe_device
,
1169 .bdrv_open
= hdev_open
,
1170 .bdrv_close
= raw_close
,
1171 .bdrv_create
= hdev_create
,
1172 .bdrv_flush
= raw_flush
,
1175 .bdrv_aio_readv
= raw_aio_readv
,
1176 .bdrv_aio_writev
= raw_aio_writev
,
1179 .bdrv_read
= raw_read
,
1180 .bdrv_write
= raw_write
,
1181 .bdrv_getlength
= raw_getlength
,
1183 /* generic scsi device */
1185 .bdrv_ioctl
= hdev_ioctl
,
1187 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1193 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1195 BDRVRawState
*s
= bs
->opaque
;
1202 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1203 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1207 /* close fd so that we can reopen it as needed */
1210 s
->fd_media_changed
= 1;
1215 static int floppy_probe_device(const char *filename
)
1217 if (strstart(filename
, "/dev/fd", NULL
))
1223 static int floppy_is_inserted(BlockDriverState
*bs
)
1225 return fd_open(bs
) >= 0;
1228 static int floppy_media_changed(BlockDriverState
*bs
)
1230 BDRVRawState
*s
= bs
->opaque
;
1234 * XXX: we do not have a true media changed indication.
1235 * It does not work if the floppy is changed without trying to read it.
1238 ret
= s
->fd_media_changed
;
1239 s
->fd_media_changed
= 0;
1241 printf("Floppy changed=%d\n", ret
);
1246 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1248 BDRVRawState
*s
= bs
->opaque
;
1255 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1257 if (ioctl(fd
, FDEJECT
, 0) < 0)
1265 static BlockDriver bdrv_host_floppy
= {
1266 .format_name
= "host_floppy",
1267 .instance_size
= sizeof(BDRVRawState
),
1268 .bdrv_probe_device
= floppy_probe_device
,
1269 .bdrv_open
= floppy_open
,
1270 .bdrv_close
= raw_close
,
1271 .bdrv_create
= hdev_create
,
1272 .bdrv_flush
= raw_flush
,
1275 .bdrv_aio_readv
= raw_aio_readv
,
1276 .bdrv_aio_writev
= raw_aio_writev
,
1279 .bdrv_read
= raw_read
,
1280 .bdrv_write
= raw_write
,
1281 .bdrv_getlength
= raw_getlength
,
1283 /* removable device support */
1284 .bdrv_is_inserted
= floppy_is_inserted
,
1285 .bdrv_media_changed
= floppy_media_changed
,
1286 .bdrv_eject
= floppy_eject
,
1289 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1291 BDRVRawState
*s
= bs
->opaque
;
1295 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1296 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1299 static int cdrom_probe_device(const char *filename
)
1301 if (strstart(filename
, "/dev/cd", NULL
))
1306 static int cdrom_is_inserted(BlockDriverState
*bs
)
1308 BDRVRawState
*s
= bs
->opaque
;
1311 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1312 if (ret
== CDS_DISC_OK
)
1317 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1319 BDRVRawState
*s
= bs
->opaque
;
1322 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1323 perror("CDROMEJECT");
1325 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1326 perror("CDROMEJECT");
1332 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1334 BDRVRawState
*s
= bs
->opaque
;
1336 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1338 * Note: an error can happen if the distribution automatically
1341 /* perror("CDROM_LOCKDOOR"); */
1347 static BlockDriver bdrv_host_cdrom
= {
1348 .format_name
= "host_cdrom",
1349 .instance_size
= sizeof(BDRVRawState
),
1350 .bdrv_probe_device
= cdrom_probe_device
,
1351 .bdrv_open
= cdrom_open
,
1352 .bdrv_close
= raw_close
,
1353 .bdrv_create
= hdev_create
,
1354 .bdrv_flush
= raw_flush
,
1357 .bdrv_aio_readv
= raw_aio_readv
,
1358 .bdrv_aio_writev
= raw_aio_writev
,
1361 .bdrv_read
= raw_read
,
1362 .bdrv_write
= raw_write
,
1363 .bdrv_getlength
= raw_getlength
,
1365 /* removable device support */
1366 .bdrv_is_inserted
= cdrom_is_inserted
,
1367 .bdrv_eject
= cdrom_eject
,
1368 .bdrv_set_locked
= cdrom_set_locked
,
1370 /* generic scsi device */
1371 .bdrv_ioctl
= hdev_ioctl
,
1373 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1376 #endif /* __linux__ */
1379 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1381 BDRVRawState
*s
= bs
->opaque
;
1386 ret
= raw_open_common(bs
, filename
, flags
, 0);
1390 /* make sure the door isnt locked at this time */
1391 ioctl(s
->fd
, CDIOCALLOW
);
1395 static int cdrom_probe_device(const char *filename
)
1397 if (strstart(filename
, "/dev/cd", NULL
) ||
1398 strstart(filename
, "/dev/acd", NULL
))
1403 static int cdrom_reopen(BlockDriverState
*bs
)
1405 BDRVRawState
*s
= bs
->opaque
;
1409 * Force reread of possibly changed/newly loaded disc,
1410 * FreeBSD seems to not notice sometimes...
1414 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1421 /* make sure the door isnt locked at this time */
1422 ioctl(s
->fd
, CDIOCALLOW
);
1426 static int cdrom_is_inserted(BlockDriverState
*bs
)
1428 return raw_getlength(bs
) > 0;
1431 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1433 BDRVRawState
*s
= bs
->opaque
;
1438 (void) ioctl(s
->fd
, CDIOCALLOW
);
1441 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1442 perror("CDIOCEJECT");
1444 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1445 perror("CDIOCCLOSE");
1448 if (cdrom_reopen(bs
) < 0)
1453 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1455 BDRVRawState
*s
= bs
->opaque
;
1459 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1461 * Note: an error can happen if the distribution automatically
1464 /* perror("CDROM_LOCKDOOR"); */
1470 static BlockDriver bdrv_host_cdrom
= {
1471 .format_name
= "host_cdrom",
1472 .instance_size
= sizeof(BDRVRawState
),
1473 .bdrv_probe_device
= cdrom_probe_device
,
1474 .bdrv_open
= cdrom_open
,
1475 .bdrv_close
= raw_close
,
1476 .bdrv_create
= hdev_create
,
1477 .bdrv_flush
= raw_flush
,
1480 .bdrv_aio_readv
= raw_aio_readv
,
1481 .bdrv_aio_writev
= raw_aio_writev
,
1484 .bdrv_read
= raw_read
,
1485 .bdrv_write
= raw_write
,
1486 .bdrv_getlength
= raw_getlength
,
1488 /* removable device support */
1489 .bdrv_is_inserted
= cdrom_is_inserted
,
1490 .bdrv_eject
= cdrom_eject
,
1491 .bdrv_set_locked
= cdrom_set_locked
,
1493 #endif /* __FreeBSD__ */
1495 static void bdrv_raw_init(void)
1498 * Register all the drivers. Note that order is important, the driver
1499 * registered last will get probed first.
1501 bdrv_register(&bdrv_raw
);
1502 bdrv_register(&bdrv_host_device
);
1504 bdrv_register(&bdrv_host_floppy
);
1505 bdrv_register(&bdrv_host_cdrom
);
1508 bdrv_register(&bdrv_host_cdrom
);
1512 block_init(bdrv_raw_init
);