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 */
85 #define O_DSYNC O_SYNC
86 #elif defined(O_FSYNC)
87 #define O_DSYNC O_FSYNC
91 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
93 #define O_DIRECT O_DSYNC
100 #define ALIGNED_BUFFER_SIZE (32 * 512)
102 /* if the FD is not accessed during that time (in ms), we try to
103 reopen it to see if the disk has been changed */
104 #define FD_OPEN_TIMEOUT 1000
106 typedef struct BDRVRawState
{
109 unsigned int lseek_err_cnt
;
111 #if defined(__linux__)
112 /* linux floppy specific */
113 int64_t fd_open_time
;
114 int64_t fd_error_time
;
116 int fd_media_changed
;
118 uint8_t* aligned_buf
;
121 static int posix_aio_init(void);
123 static int fd_open(BlockDriverState
*bs
);
124 static int64_t raw_getlength(BlockDriverState
*bs
);
126 #if defined(__FreeBSD__)
127 static int cdrom_reopen(BlockDriverState
*bs
);
130 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
131 int bdrv_flags
, int open_flags
)
133 BDRVRawState
*s
= bs
->opaque
;
138 s
->lseek_err_cnt
= 0;
140 s
->open_flags
= open_flags
| O_BINARY
;
141 s
->open_flags
&= ~O_ACCMODE
;
142 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
143 s
->open_flags
|= O_RDWR
;
145 s
->open_flags
|= O_RDONLY
;
149 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
150 * and O_DIRECT for no caching. */
151 if ((bdrv_flags
& BDRV_O_NOCACHE
))
152 s
->open_flags
|= O_DIRECT
;
153 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
154 s
->open_flags
|= O_DSYNC
;
157 fd
= open(filename
, s
->open_flags
, 0644);
165 s
->aligned_buf
= NULL
;
166 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
167 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
168 if (s
->aligned_buf
== NULL
) {
177 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
179 BDRVRawState
*s
= bs
->opaque
;
182 s
->type
= FTYPE_FILE
;
183 if (flags
& BDRV_O_CREAT
)
184 open_flags
= O_CREAT
| O_TRUNC
;
186 return raw_open_common(bs
, filename
, flags
, open_flags
);
189 /* XXX: use host sector size if necessary with:
190 #ifdef DIOCGSECTORSIZE
192 unsigned int sectorsize = 512;
193 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
194 sectorsize > bufsize)
195 bufsize = sectorsize;
199 u_int32_t blockSize = 512;
200 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
207 * offset and count are in bytes, but must be multiples of 512 for files
208 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
210 * This function may be called without alignment if the caller ensures
211 * that O_DIRECT is not in effect.
213 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
214 uint8_t *buf
, int count
)
216 BDRVRawState
*s
= bs
->opaque
;
223 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
224 ++(s
->lseek_err_cnt
);
225 if(s
->lseek_err_cnt
<= 10) {
226 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
227 "] lseek failed : %d = %s\n",
228 s
->fd
, bs
->filename
, offset
, buf
, count
,
229 bs
->total_sectors
, errno
, strerror(errno
));
235 ret
= read(s
->fd
, buf
, count
);
237 goto label__raw_read__success
;
239 /* Allow reads beyond the end (needed for pwrite) */
240 if ((ret
== 0) && bs
->growable
) {
241 int64_t size
= raw_getlength(bs
);
242 if (offset
>= size
) {
243 memset(buf
, 0, count
);
245 goto label__raw_read__success
;
249 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
250 "] read failed %d : %d = %s\n",
251 s
->fd
, bs
->filename
, offset
, buf
, count
,
252 bs
->total_sectors
, ret
, errno
, strerror(errno
));
254 /* Try harder for CDrom. */
255 if (bs
->type
== BDRV_TYPE_CDROM
) {
256 lseek(s
->fd
, offset
, SEEK_SET
);
257 ret
= read(s
->fd
, buf
, count
);
259 goto label__raw_read__success
;
260 lseek(s
->fd
, offset
, SEEK_SET
);
261 ret
= read(s
->fd
, buf
, count
);
263 goto label__raw_read__success
;
265 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
266 "] retry read failed %d : %d = %s\n",
267 s
->fd
, bs
->filename
, offset
, buf
, count
,
268 bs
->total_sectors
, ret
, errno
, strerror(errno
));
271 label__raw_read__success
:
273 return (ret
< 0) ? -errno
: ret
;
277 * offset and count are in bytes, but must be multiples of 512 for files
278 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
280 * This function may be called without alignment if the caller ensures
281 * that O_DIRECT is not in effect.
283 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
284 const uint8_t *buf
, int count
)
286 BDRVRawState
*s
= bs
->opaque
;
293 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
294 ++(s
->lseek_err_cnt
);
295 if(s
->lseek_err_cnt
) {
296 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
297 PRId64
"] lseek failed : %d = %s\n",
298 s
->fd
, bs
->filename
, offset
, buf
, count
,
299 bs
->total_sectors
, errno
, strerror(errno
));
303 s
->lseek_err_cnt
= 0;
305 ret
= write(s
->fd
, buf
, count
);
307 goto label__raw_write__success
;
309 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
310 "] write failed %d : %d = %s\n",
311 s
->fd
, bs
->filename
, offset
, buf
, count
,
312 bs
->total_sectors
, ret
, errno
, strerror(errno
));
314 label__raw_write__success
:
316 return (ret
< 0) ? -errno
: ret
;
321 * offset and count are in bytes and possibly not aligned. For files opened
322 * with O_DIRECT, necessary alignments are ensured before calling
323 * raw_pread_aligned to do the actual read.
325 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
326 uint8_t *buf
, int count
)
328 BDRVRawState
*s
= bs
->opaque
;
329 int size
, ret
, shift
, sum
;
333 if (s
->aligned_buf
!= NULL
) {
335 if (offset
& 0x1ff) {
336 /* align offset on a 512 bytes boundary */
338 shift
= offset
& 0x1ff;
339 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
340 if (size
> ALIGNED_BUFFER_SIZE
)
341 size
= ALIGNED_BUFFER_SIZE
;
342 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
349 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
359 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
361 /* read on aligned buffer */
365 size
= (count
+ 0x1ff) & ~0x1ff;
366 if (size
> ALIGNED_BUFFER_SIZE
)
367 size
= ALIGNED_BUFFER_SIZE
;
369 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
377 memcpy(buf
, s
->aligned_buf
, size
);
389 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
392 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
393 uint8_t *buf
, int nb_sectors
)
397 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
398 if (ret
== (nb_sectors
* 512))
404 * offset and count are in bytes and possibly not aligned. For files opened
405 * with O_DIRECT, necessary alignments are ensured before calling
406 * raw_pwrite_aligned to do the actual write.
408 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
409 const uint8_t *buf
, int count
)
411 BDRVRawState
*s
= bs
->opaque
;
412 int size
, ret
, shift
, sum
;
416 if (s
->aligned_buf
!= NULL
) {
418 if (offset
& 0x1ff) {
419 /* align offset on a 512 bytes boundary */
420 shift
= offset
& 0x1ff;
421 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
428 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
430 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
442 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
444 while ((size
= (count
& ~0x1ff)) != 0) {
446 if (size
> ALIGNED_BUFFER_SIZE
)
447 size
= ALIGNED_BUFFER_SIZE
;
449 memcpy(s
->aligned_buf
, buf
, size
);
451 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
460 /* here, count < 512 because (count & ~0x1ff) == 0 */
462 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
465 memcpy(s
->aligned_buf
, buf
, count
);
467 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
478 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
481 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
482 const uint8_t *buf
, int nb_sectors
)
485 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
486 if (ret
== (nb_sectors
* 512))
492 /***********************************************************/
493 /* Unix AIO using POSIX AIO */
495 typedef struct RawAIOCB
{
496 BlockDriverAIOCB common
;
497 struct qemu_paiocb aiocb
;
498 struct RawAIOCB
*next
;
502 typedef struct PosixAioState
508 static void posix_aio_read(void *opaque
)
510 PosixAioState
*s
= opaque
;
511 RawAIOCB
*acb
, **pacb
;
515 /* read all bytes from signal pipe */
519 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
520 if (len
== -1 && errno
== EINTR
)
521 continue; /* try again */
522 if (len
== sizeof(bytes
))
523 continue; /* more to read */
528 pacb
= &s
->first_aio
;
533 ret
= qemu_paio_error(&acb
->aiocb
);
534 if (ret
== ECANCELED
) {
535 /* remove the request */
537 qemu_aio_release(acb
);
538 } else if (ret
!= EINPROGRESS
) {
541 ret
= qemu_paio_return(&acb
->aiocb
);
542 if (ret
== acb
->aiocb
.aio_nbytes
)
549 /* remove the request */
551 /* call the callback */
552 acb
->common
.cb(acb
->common
.opaque
, ret
);
553 qemu_aio_release(acb
);
563 static int posix_aio_flush(void *opaque
)
565 PosixAioState
*s
= opaque
;
566 return !!s
->first_aio
;
569 static PosixAioState
*posix_aio_state
;
571 static void aio_signal_handler(int signum
)
573 if (posix_aio_state
) {
576 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
582 static int posix_aio_init(void)
584 struct sigaction act
;
587 struct qemu_paioinit ai
;
592 s
= qemu_malloc(sizeof(PosixAioState
));
594 sigfillset(&act
.sa_mask
);
595 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
596 act
.sa_handler
= aio_signal_handler
;
597 sigaction(SIGUSR2
, &act
, NULL
);
600 if (pipe(fds
) == -1) {
601 fprintf(stderr
, "failed to create pipe\n");
608 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
609 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
611 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
613 memset(&ai
, 0, sizeof(ai
));
623 static void raw_aio_remove(RawAIOCB
*acb
)
627 /* remove the callback from the queue */
628 pacb
= &posix_aio_state
->first_aio
;
631 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
633 } else if (*pacb
== acb
) {
635 qemu_aio_release(acb
);
638 pacb
= &(*pacb
)->next
;
642 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
645 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
647 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
648 if (ret
== QEMU_PAIO_NOTCANCELED
) {
649 /* fail safe: if the aio could not be canceled, we wait for
651 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
657 static AIOPool raw_aio_pool
= {
658 .aiocb_size
= sizeof(RawAIOCB
),
659 .cancel
= raw_aio_cancel
,
662 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
663 QEMUIOVector
*qiov
, int nb_sectors
,
664 BlockDriverCompletionFunc
*cb
, void *opaque
)
666 BDRVRawState
*s
= bs
->opaque
;
672 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
675 acb
->aiocb
.aio_fildes
= s
->fd
;
676 acb
->aiocb
.ev_signo
= SIGUSR2
;
677 acb
->aiocb
.aio_iov
= qiov
->iov
;
678 acb
->aiocb
.aio_niov
= qiov
->niov
;
679 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
680 acb
->aiocb
.aio_offset
= sector_num
* 512;
681 acb
->aiocb
.aio_flags
= 0;
684 * If O_DIRECT is used the buffer needs to be aligned on a sector
685 * boundary. Tell the low level code to ensure that in case it's
689 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
691 acb
->next
= posix_aio_state
->first_aio
;
692 posix_aio_state
->first_aio
= acb
;
696 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
697 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
698 BlockDriverCompletionFunc
*cb
, void *opaque
)
702 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
705 if (qemu_paio_read(&acb
->aiocb
) < 0) {
712 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
713 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
714 BlockDriverCompletionFunc
*cb
, void *opaque
)
718 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
721 if (qemu_paio_write(&acb
->aiocb
) < 0) {
727 #else /* CONFIG_AIO */
728 static int posix_aio_init(void)
732 #endif /* CONFIG_AIO */
735 static void raw_close(BlockDriverState
*bs
)
737 BDRVRawState
*s
= bs
->opaque
;
741 if (s
->aligned_buf
!= NULL
)
742 qemu_free(s
->aligned_buf
);
746 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
748 BDRVRawState
*s
= bs
->opaque
;
749 if (s
->type
!= FTYPE_FILE
)
751 if (ftruncate(s
->fd
, offset
) < 0)
757 static int64_t raw_getlength(BlockDriverState
*bs
)
759 BDRVRawState
*s
= bs
->opaque
;
765 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
768 if (ioctl(fd
, DIOCGDINFO
, &dl
))
770 return (uint64_t)dl
.d_secsize
*
771 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
775 #else /* !__OpenBSD__ */
776 static int64_t raw_getlength(BlockDriverState
*bs
)
778 BDRVRawState
*s
= bs
->opaque
;
788 struct dk_minfo minfo
;
801 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
802 #ifdef DIOCGMEDIASIZE
803 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
804 #elif defined(DIOCGPART)
807 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
808 size
= pi
.media_size
;
815 size
= LONG_LONG_MAX
;
817 size
= lseek(fd
, 0LL, SEEK_END
);
822 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
823 if (size
== 2048LL * (unsigned)-1)
825 /* XXX no disc? maybe we need to reopen... */
826 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
836 * use the DKIOCGMEDIAINFO ioctl to read the size.
838 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
840 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
841 } else /* there are reports that lseek on some devices
842 fails, but irc discussion said that contingency
843 on contingency was overkill */
846 size
= lseek(fd
, 0, SEEK_END
);
852 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
855 int64_t total_size
= 0;
857 /* Read out options */
858 while (options
&& options
->name
) {
859 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
860 total_size
= options
->value
.n
/ 512;
865 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
869 ftruncate(fd
, total_size
* 512);
874 static void raw_flush(BlockDriverState
*bs
)
876 BDRVRawState
*s
= bs
->opaque
;
881 static QEMUOptionParameter raw_create_options
[] = {
883 .name
= BLOCK_OPT_SIZE
,
885 .help
= "Virtual disk size"
890 static BlockDriver bdrv_raw
= {
891 .format_name
= "raw",
892 .instance_size
= sizeof(BDRVRawState
),
893 .bdrv_probe
= NULL
, /* no probe for protocols */
894 .bdrv_open
= raw_open
,
895 .bdrv_read
= raw_read
,
896 .bdrv_write
= raw_write
,
897 .bdrv_close
= raw_close
,
898 .bdrv_create
= raw_create
,
899 .bdrv_flush
= raw_flush
,
902 .bdrv_aio_readv
= raw_aio_readv
,
903 .bdrv_aio_writev
= raw_aio_writev
,
906 .bdrv_truncate
= raw_truncate
,
907 .bdrv_getlength
= raw_getlength
,
909 .create_options
= raw_create_options
,
912 /***********************************************/
916 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
917 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
919 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
921 kern_return_t kernResult
;
922 mach_port_t masterPort
;
923 CFMutableDictionaryRef classesToMatch
;
925 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
926 if ( KERN_SUCCESS
!= kernResult
) {
927 printf( "IOMasterPort returned %d\n", kernResult
);
930 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
931 if ( classesToMatch
== NULL
) {
932 printf( "IOServiceMatching returned a NULL dictionary.\n" );
934 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
936 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
937 if ( KERN_SUCCESS
!= kernResult
)
939 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
945 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
947 io_object_t nextMedia
;
948 kern_return_t kernResult
= KERN_FAILURE
;
950 nextMedia
= IOIteratorNext( mediaIterator
);
953 CFTypeRef bsdPathAsCFString
;
954 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
955 if ( bsdPathAsCFString
) {
956 size_t devPathLength
;
957 strcpy( bsdPath
, _PATH_DEV
);
958 strcat( bsdPath
, "r" );
959 devPathLength
= strlen( bsdPath
);
960 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
961 kernResult
= KERN_SUCCESS
;
963 CFRelease( bsdPathAsCFString
);
965 IOObjectRelease( nextMedia
);
973 static int hdev_probe_device(const char *filename
)
977 /* allow a dedicated CD-ROM driver to match with a higher priority */
978 if (strstart(filename
, "/dev/cdrom", NULL
))
981 if (stat(filename
, &st
) >= 0 &&
982 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
989 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
991 BDRVRawState
*s
= bs
->opaque
;
994 if (strstart(filename
, "/dev/cdrom", NULL
)) {
995 kern_return_t kernResult
;
996 io_iterator_t mediaIterator
;
997 char bsdPath
[ MAXPATHLEN
];
1000 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1001 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1003 if ( bsdPath
[ 0 ] != '\0' ) {
1004 strcat(bsdPath
,"s0");
1005 /* some CDs don't have a partition 0 */
1006 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1008 bsdPath
[strlen(bsdPath
)-1] = '1';
1015 if ( mediaIterator
)
1016 IOObjectRelease( mediaIterator
);
1020 s
->type
= FTYPE_FILE
;
1021 #if defined(__linux__) && defined(CONFIG_AIO)
1022 if (strstart(filename
, "/dev/sg", NULL
)) {
1027 return raw_open_common(bs
, filename
, flags
, 0);
1030 #if defined(__linux__)
1031 /* Note: we do not have a reliable method to detect if the floppy is
1032 present. The current method is to try to open the floppy at every
1033 I/O and to keep it opened during a few hundreds of ms. */
1034 static int fd_open(BlockDriverState
*bs
)
1036 BDRVRawState
*s
= bs
->opaque
;
1037 int last_media_present
;
1039 if (s
->type
!= FTYPE_FD
)
1041 last_media_present
= (s
->fd
>= 0);
1043 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1047 printf("Floppy closed\n");
1051 if (s
->fd_got_error
&&
1052 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1054 printf("No floppy (open delayed)\n");
1058 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1060 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1061 s
->fd_got_error
= 1;
1062 if (last_media_present
)
1063 s
->fd_media_changed
= 1;
1065 printf("No floppy\n");
1070 printf("Floppy opened\n");
1073 if (!last_media_present
)
1074 s
->fd_media_changed
= 1;
1075 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1076 s
->fd_got_error
= 0;
1080 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1082 BDRVRawState
*s
= bs
->opaque
;
1084 return ioctl(s
->fd
, req
, buf
);
1088 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1089 unsigned long int req
, void *buf
,
1090 BlockDriverCompletionFunc
*cb
, void *opaque
)
1092 BDRVRawState
*s
= bs
->opaque
;
1095 if (fd_open(bs
) < 0)
1098 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1101 acb
->aiocb
.aio_fildes
= s
->fd
;
1102 acb
->aiocb
.ev_signo
= SIGUSR2
;
1103 acb
->aiocb
.aio_offset
= 0;
1104 acb
->aiocb
.aio_flags
= 0;
1106 acb
->next
= posix_aio_state
->first_aio
;
1107 posix_aio_state
->first_aio
= acb
;
1109 acb
->aiocb
.aio_ioctl_buf
= buf
;
1110 acb
->aiocb
.aio_ioctl_cmd
= req
;
1111 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1112 raw_aio_remove(acb
);
1116 return &acb
->common
;
1120 #elif defined(__FreeBSD__)
1121 static int fd_open(BlockDriverState
*bs
)
1123 BDRVRawState
*s
= bs
->opaque
;
1125 /* this is just to ensure s->fd is sane (its called by io ops) */
1130 #else /* !linux && !FreeBSD */
1132 static int fd_open(BlockDriverState
*bs
)
1137 #endif /* !linux && !FreeBSD */
1139 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1143 struct stat stat_buf
;
1144 int64_t total_size
= 0;
1146 /* Read out options */
1147 while (options
&& options
->name
) {
1148 if (!strcmp(options
->name
, "size")) {
1149 total_size
= options
->value
.n
/ 512;
1154 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1158 if (fstat(fd
, &stat_buf
) < 0)
1160 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1162 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1169 static BlockDriver bdrv_host_device
= {
1170 .format_name
= "host_device",
1171 .instance_size
= sizeof(BDRVRawState
),
1172 .bdrv_probe_device
= hdev_probe_device
,
1173 .bdrv_open
= hdev_open
,
1174 .bdrv_close
= raw_close
,
1175 .bdrv_create
= hdev_create
,
1176 .bdrv_flush
= raw_flush
,
1179 .bdrv_aio_readv
= raw_aio_readv
,
1180 .bdrv_aio_writev
= raw_aio_writev
,
1183 .bdrv_read
= raw_read
,
1184 .bdrv_write
= raw_write
,
1185 .bdrv_getlength
= raw_getlength
,
1187 /* generic scsi device */
1189 .bdrv_ioctl
= hdev_ioctl
,
1191 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1197 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1199 BDRVRawState
*s
= bs
->opaque
;
1206 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1207 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1211 /* close fd so that we can reopen it as needed */
1214 s
->fd_media_changed
= 1;
1219 static int floppy_probe_device(const char *filename
)
1221 if (strstart(filename
, "/dev/fd", NULL
))
1227 static int floppy_is_inserted(BlockDriverState
*bs
)
1229 return fd_open(bs
) >= 0;
1232 static int floppy_media_changed(BlockDriverState
*bs
)
1234 BDRVRawState
*s
= bs
->opaque
;
1238 * XXX: we do not have a true media changed indication.
1239 * It does not work if the floppy is changed without trying to read it.
1242 ret
= s
->fd_media_changed
;
1243 s
->fd_media_changed
= 0;
1245 printf("Floppy changed=%d\n", ret
);
1250 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1252 BDRVRawState
*s
= bs
->opaque
;
1259 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1261 if (ioctl(fd
, FDEJECT
, 0) < 0)
1269 static BlockDriver bdrv_host_floppy
= {
1270 .format_name
= "host_floppy",
1271 .instance_size
= sizeof(BDRVRawState
),
1272 .bdrv_probe_device
= floppy_probe_device
,
1273 .bdrv_open
= floppy_open
,
1274 .bdrv_close
= raw_close
,
1275 .bdrv_create
= hdev_create
,
1276 .bdrv_flush
= raw_flush
,
1279 .bdrv_aio_readv
= raw_aio_readv
,
1280 .bdrv_aio_writev
= raw_aio_writev
,
1283 .bdrv_read
= raw_read
,
1284 .bdrv_write
= raw_write
,
1285 .bdrv_getlength
= raw_getlength
,
1287 /* removable device support */
1288 .bdrv_is_inserted
= floppy_is_inserted
,
1289 .bdrv_media_changed
= floppy_media_changed
,
1290 .bdrv_eject
= floppy_eject
,
1293 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1295 BDRVRawState
*s
= bs
->opaque
;
1299 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1300 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1303 static int cdrom_probe_device(const char *filename
)
1305 if (strstart(filename
, "/dev/cd", NULL
))
1310 static int cdrom_is_inserted(BlockDriverState
*bs
)
1312 BDRVRawState
*s
= bs
->opaque
;
1315 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1316 if (ret
== CDS_DISC_OK
)
1321 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1323 BDRVRawState
*s
= bs
->opaque
;
1326 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1327 perror("CDROMEJECT");
1329 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1330 perror("CDROMEJECT");
1336 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1338 BDRVRawState
*s
= bs
->opaque
;
1340 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1342 * Note: an error can happen if the distribution automatically
1345 /* perror("CDROM_LOCKDOOR"); */
1351 static BlockDriver bdrv_host_cdrom
= {
1352 .format_name
= "host_cdrom",
1353 .instance_size
= sizeof(BDRVRawState
),
1354 .bdrv_probe_device
= cdrom_probe_device
,
1355 .bdrv_open
= cdrom_open
,
1356 .bdrv_close
= raw_close
,
1357 .bdrv_create
= hdev_create
,
1358 .bdrv_flush
= raw_flush
,
1361 .bdrv_aio_readv
= raw_aio_readv
,
1362 .bdrv_aio_writev
= raw_aio_writev
,
1365 .bdrv_read
= raw_read
,
1366 .bdrv_write
= raw_write
,
1367 .bdrv_getlength
= raw_getlength
,
1369 /* removable device support */
1370 .bdrv_is_inserted
= cdrom_is_inserted
,
1371 .bdrv_eject
= cdrom_eject
,
1372 .bdrv_set_locked
= cdrom_set_locked
,
1374 /* generic scsi device */
1375 .bdrv_ioctl
= hdev_ioctl
,
1377 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1380 #endif /* __linux__ */
1383 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1385 BDRVRawState
*s
= bs
->opaque
;
1390 ret
= raw_open_common(bs
, filename
, flags
, 0);
1394 /* make sure the door isnt locked at this time */
1395 ioctl(s
->fd
, CDIOCALLOW
);
1399 static int cdrom_probe_device(const char *filename
)
1401 if (strstart(filename
, "/dev/cd", NULL
) ||
1402 strstart(filename
, "/dev/acd", NULL
))
1407 static int cdrom_reopen(BlockDriverState
*bs
)
1409 BDRVRawState
*s
= bs
->opaque
;
1413 * Force reread of possibly changed/newly loaded disc,
1414 * FreeBSD seems to not notice sometimes...
1418 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1425 /* make sure the door isnt locked at this time */
1426 ioctl(s
->fd
, CDIOCALLOW
);
1430 static int cdrom_is_inserted(BlockDriverState
*bs
)
1432 return raw_getlength(bs
) > 0;
1435 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1437 BDRVRawState
*s
= bs
->opaque
;
1442 (void) ioctl(s
->fd
, CDIOCALLOW
);
1445 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1446 perror("CDIOCEJECT");
1448 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1449 perror("CDIOCCLOSE");
1452 if (cdrom_reopen(bs
) < 0)
1457 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1459 BDRVRawState
*s
= bs
->opaque
;
1463 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1465 * Note: an error can happen if the distribution automatically
1468 /* perror("CDROM_LOCKDOOR"); */
1474 static BlockDriver bdrv_host_cdrom
= {
1475 .format_name
= "host_cdrom",
1476 .instance_size
= sizeof(BDRVRawState
),
1477 .bdrv_probe_device
= cdrom_probe_device
,
1478 .bdrv_open
= cdrom_open
,
1479 .bdrv_close
= raw_close
,
1480 .bdrv_create
= hdev_create
,
1481 .bdrv_flush
= raw_flush
,
1484 .bdrv_aio_readv
= raw_aio_readv
,
1485 .bdrv_aio_writev
= raw_aio_writev
,
1488 .bdrv_read
= raw_read
,
1489 .bdrv_write
= raw_write
,
1490 .bdrv_getlength
= raw_getlength
,
1492 /* removable device support */
1493 .bdrv_is_inserted
= cdrom_is_inserted
,
1494 .bdrv_eject
= cdrom_eject
,
1495 .bdrv_set_locked
= cdrom_set_locked
,
1497 #endif /* __FreeBSD__ */
1499 static void bdrv_raw_init(void)
1502 * Register all the drivers. Note that order is important, the driver
1503 * registered last will get probed first.
1505 bdrv_register(&bdrv_raw
);
1506 bdrv_register(&bdrv_host_device
);
1508 bdrv_register(&bdrv_host_floppy
);
1509 bdrv_register(&bdrv_host_cdrom
);
1512 bdrv_register(&bdrv_host_cdrom
);
1516 block_init(bdrv_raw_init
);