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 "block/raw-posix-aio.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
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
52 #include <sys/param.h>
53 #include <linux/cdrom.h>
56 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
68 #include <sys/ioctl.h>
69 #include <sys/disklabel.h>
75 #include <sys/ioctl.h>
76 #include <sys/diskslice.h>
83 //#define DEBUG_FLOPPY
86 #if defined(DEBUG_BLOCK)
87 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
88 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
90 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
93 /* OS X does not have O_DSYNC */
96 #define O_DSYNC O_SYNC
97 #elif defined(O_FSYNC)
98 #define O_DSYNC O_FSYNC
102 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
104 #define O_DIRECT O_DSYNC
111 /* if the FD is not accessed during that time (in ns), we try to
112 reopen it to see if the disk has been changed */
113 #define FD_OPEN_TIMEOUT (1000000000)
115 #define MAX_BLOCKSIZE 4096
117 typedef struct BDRVRawState
{
121 #if defined(__linux__)
122 /* linux floppy specific */
123 int64_t fd_open_time
;
124 int64_t fd_error_time
;
126 int fd_media_changed
;
128 #ifdef CONFIG_LINUX_AIO
132 uint8_t *aligned_buf
;
133 unsigned aligned_buf_size
;
139 static int fd_open(BlockDriverState
*bs
);
140 static int64_t raw_getlength(BlockDriverState
*bs
);
142 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
143 static int cdrom_reopen(BlockDriverState
*bs
);
146 #if defined(__NetBSD__)
147 static int raw_normalize_devicepath(const char **filename
)
149 static char namebuf
[PATH_MAX
];
150 const char *dp
, *fname
;
154 dp
= strrchr(fname
, '/');
155 if (lstat(fname
, &sb
) < 0) {
156 fprintf(stderr
, "%s: stat failed: %s\n",
157 fname
, strerror(errno
));
161 if (!S_ISBLK(sb
.st_mode
)) {
166 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
168 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
169 (int)(dp
- fname
), fname
, dp
+ 1);
171 fprintf(stderr
, "%s is a block device", fname
);
173 fprintf(stderr
, ", using %s\n", *filename
);
178 static int raw_normalize_devicepath(const char **filename
)
184 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
185 int bdrv_flags
, int open_flags
)
187 BDRVRawState
*s
= bs
->opaque
;
190 ret
= raw_normalize_devicepath(&filename
);
195 s
->open_flags
= open_flags
| O_BINARY
;
196 s
->open_flags
&= ~O_ACCMODE
;
197 if (bdrv_flags
& BDRV_O_RDWR
) {
198 s
->open_flags
|= O_RDWR
;
200 s
->open_flags
|= O_RDONLY
;
203 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
204 * and O_DIRECT for no caching. */
205 if ((bdrv_flags
& BDRV_O_NOCACHE
))
206 s
->open_flags
|= O_DIRECT
;
207 if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
208 s
->open_flags
|= O_DSYNC
;
211 fd
= qemu_open(filename
, s
->open_flags
, 0644);
219 s
->aligned_buf
= NULL
;
221 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
223 * Allocate a buffer for read/modify/write cycles. Chose the size
224 * pessimistically as we don't know the block size yet.
226 s
->aligned_buf_size
= 32 * MAX_BLOCKSIZE
;
227 s
->aligned_buf
= qemu_memalign(MAX_BLOCKSIZE
, s
->aligned_buf_size
);
228 if (s
->aligned_buf
== NULL
) {
233 /* We're falling back to POSIX AIO in some cases so init always */
234 if (paio_init() < 0) {
238 #ifdef CONFIG_LINUX_AIO
239 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
240 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
242 s
->aio_ctx
= laio_init();
250 #ifdef CONFIG_LINUX_AIO
256 if (platform_test_xfs_fd(s
->fd
)) {
264 qemu_vfree(s
->aligned_buf
);
270 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
272 BDRVRawState
*s
= bs
->opaque
;
274 s
->type
= FTYPE_FILE
;
275 return raw_open_common(bs
, filename
, flags
, 0);
278 /* XXX: use host sector size if necessary with:
279 #ifdef DIOCGSECTORSIZE
281 unsigned int sectorsize = 512;
282 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
283 sectorsize > bufsize)
284 bufsize = sectorsize;
288 uint32_t blockSize = 512;
289 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
296 * offset and count are in bytes, but must be multiples of 512 for files
297 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
299 * This function may be called without alignment if the caller ensures
300 * that O_DIRECT is not in effect.
302 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
303 uint8_t *buf
, int count
)
305 BDRVRawState
*s
= bs
->opaque
;
312 ret
= pread(s
->fd
, buf
, count
, offset
);
316 /* Allow reads beyond the end (needed for pwrite) */
317 if ((ret
== 0) && bs
->growable
) {
318 int64_t size
= raw_getlength(bs
);
319 if (offset
>= size
) {
320 memset(buf
, 0, count
);
325 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
326 "] read failed %d : %d = %s\n",
327 s
->fd
, bs
->filename
, offset
, buf
, count
,
328 bs
->total_sectors
, ret
, errno
, strerror(errno
));
330 /* Try harder for CDrom. */
331 if (s
->type
!= FTYPE_FILE
) {
332 ret
= pread(s
->fd
, buf
, count
, offset
);
335 ret
= pread(s
->fd
, buf
, count
, offset
);
339 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
340 "] retry read failed %d : %d = %s\n",
341 s
->fd
, bs
->filename
, offset
, buf
, count
,
342 bs
->total_sectors
, ret
, errno
, strerror(errno
));
345 return (ret
< 0) ? -errno
: ret
;
349 * offset and count are in bytes, but must be multiples of the sector size
350 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
353 * This function may be called without alignment if the caller ensures
354 * that O_DIRECT is not in effect.
356 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
357 const uint8_t *buf
, int count
)
359 BDRVRawState
*s
= bs
->opaque
;
366 ret
= pwrite(s
->fd
, buf
, count
, offset
);
370 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
371 "] write failed %d : %d = %s\n",
372 s
->fd
, bs
->filename
, offset
, buf
, count
,
373 bs
->total_sectors
, ret
, errno
, strerror(errno
));
375 return (ret
< 0) ? -errno
: ret
;
380 * offset and count are in bytes and possibly not aligned. For files opened
381 * with O_DIRECT, necessary alignments are ensured before calling
382 * raw_pread_aligned to do the actual read.
384 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
385 uint8_t *buf
, int count
)
387 BDRVRawState
*s
= bs
->opaque
;
388 unsigned sector_mask
= bs
->buffer_alignment
- 1;
389 int size
, ret
, shift
, sum
;
393 if (s
->aligned_buf
!= NULL
) {
395 if (offset
& sector_mask
) {
396 /* align offset on a sector size bytes boundary */
398 shift
= offset
& sector_mask
;
399 size
= (shift
+ count
+ sector_mask
) & ~sector_mask
;
400 if (size
> s
->aligned_buf_size
)
401 size
= s
->aligned_buf_size
;
402 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
406 size
= bs
->buffer_alignment
- shift
;
409 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
419 if (count
& sector_mask
|| (uintptr_t) buf
& sector_mask
) {
421 /* read on aligned buffer */
425 size
= (count
+ sector_mask
) & ~sector_mask
;
426 if (size
> s
->aligned_buf_size
)
427 size
= s
->aligned_buf_size
;
429 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
432 } else if (ret
== 0) {
433 fprintf(stderr
, "raw_pread: read beyond end of file\n");
441 memcpy(buf
, s
->aligned_buf
, size
);
453 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
456 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
457 uint8_t *buf
, int nb_sectors
)
461 ret
= raw_pread(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
462 nb_sectors
* BDRV_SECTOR_SIZE
);
463 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
469 * offset and count are in bytes and possibly not aligned. For files opened
470 * with O_DIRECT, necessary alignments are ensured before calling
471 * raw_pwrite_aligned to do the actual write.
473 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
474 const uint8_t *buf
, int count
)
476 BDRVRawState
*s
= bs
->opaque
;
477 unsigned sector_mask
= bs
->buffer_alignment
- 1;
478 int size
, ret
, shift
, sum
;
482 if (s
->aligned_buf
!= NULL
) {
484 if (offset
& sector_mask
) {
485 /* align offset on a sector size bytes boundary */
486 shift
= offset
& sector_mask
;
487 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
,
488 bs
->buffer_alignment
);
492 size
= bs
->buffer_alignment
- shift
;
495 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
497 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
,
498 bs
->buffer_alignment
);
510 if (count
& sector_mask
|| (uintptr_t) buf
& sector_mask
) {
512 while ((size
= (count
& ~sector_mask
)) != 0) {
514 if (size
> s
->aligned_buf_size
)
515 size
= s
->aligned_buf_size
;
517 memcpy(s
->aligned_buf
, buf
, size
);
519 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
528 /* here, count < sector_size because (count & ~sector_mask) == 0 */
530 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
,
531 bs
->buffer_alignment
);
534 memcpy(s
->aligned_buf
, buf
, count
);
536 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
,
537 bs
->buffer_alignment
);
548 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
551 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
552 const uint8_t *buf
, int nb_sectors
)
555 ret
= raw_pwrite(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
556 nb_sectors
* BDRV_SECTOR_SIZE
);
557 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
563 * Check if all memory in this vector is sector aligned.
565 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
569 for (i
= 0; i
< qiov
->niov
; i
++) {
570 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
578 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
579 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
580 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
582 BDRVRawState
*s
= bs
->opaque
;
588 * If O_DIRECT is used the buffer needs to be aligned on a sector
589 * boundary. Check if this is the case or tell the low-level
590 * driver that it needs to copy the buffer.
592 if (s
->aligned_buf
) {
593 if (!qiov_is_aligned(bs
, qiov
)) {
594 type
|= QEMU_AIO_MISALIGNED
;
595 #ifdef CONFIG_LINUX_AIO
596 } else if (s
->use_aio
) {
597 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
598 nb_sectors
, cb
, opaque
, type
);
603 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
607 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
608 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
609 BlockDriverCompletionFunc
*cb
, void *opaque
)
611 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
612 cb
, opaque
, QEMU_AIO_READ
);
615 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
616 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
617 BlockDriverCompletionFunc
*cb
, void *opaque
)
619 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
620 cb
, opaque
, QEMU_AIO_WRITE
);
623 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
624 BlockDriverCompletionFunc
*cb
, void *opaque
)
626 BDRVRawState
*s
= bs
->opaque
;
631 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
634 static void raw_close(BlockDriverState
*bs
)
636 BDRVRawState
*s
= bs
->opaque
;
640 if (s
->aligned_buf
!= NULL
)
641 qemu_vfree(s
->aligned_buf
);
645 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
647 BDRVRawState
*s
= bs
->opaque
;
648 if (s
->type
!= FTYPE_FILE
)
650 if (ftruncate(s
->fd
, offset
) < 0)
656 static int64_t raw_getlength(BlockDriverState
*bs
)
658 BDRVRawState
*s
= bs
->opaque
;
664 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
667 if (ioctl(fd
, DIOCGDINFO
, &dl
))
669 return (uint64_t)dl
.d_secsize
*
670 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
674 #elif defined(__NetBSD__)
675 static int64_t raw_getlength(BlockDriverState
*bs
)
677 BDRVRawState
*s
= bs
->opaque
;
683 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
684 struct dkwedge_info dkw
;
686 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
687 return dkw
.dkw_size
* 512;
691 if (ioctl(fd
, DIOCGDINFO
, &dl
))
693 return (uint64_t)dl
.d_secsize
*
694 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
699 #elif defined(__sun__)
700 static int64_t raw_getlength(BlockDriverState
*bs
)
702 BDRVRawState
*s
= bs
->opaque
;
703 struct dk_minfo minfo
;
712 * Use the DKIOCGMEDIAINFO ioctl to read the size.
714 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
716 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
720 * There are reports that lseek on some devices fails, but
721 * irc discussion said that contingency on contingency was overkill.
723 return lseek(s
->fd
, 0, SEEK_END
);
725 #elif defined(CONFIG_BSD)
726 static int64_t raw_getlength(BlockDriverState
*bs
)
728 BDRVRawState
*s
= bs
->opaque
;
732 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
741 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
744 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
745 #ifdef DIOCGMEDIASIZE
746 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
747 #elif defined(DIOCGPART)
750 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
751 size
= pi
.media_size
;
758 size
= LONG_LONG_MAX
;
760 size
= lseek(fd
, 0LL, SEEK_END
);
762 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
765 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
766 if (size
== 2048LL * (unsigned)-1)
768 /* XXX no disc? maybe we need to reopen... */
769 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
776 size
= lseek(fd
, 0, SEEK_END
);
781 static int64_t raw_getlength(BlockDriverState
*bs
)
783 BDRVRawState
*s
= bs
->opaque
;
791 return lseek(s
->fd
, 0, SEEK_END
);
795 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
798 BDRVRawState
*s
= bs
->opaque
;
800 if (fstat(s
->fd
, &st
) < 0) {
803 return (int64_t)st
.st_blocks
* 512;
806 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
810 int64_t total_size
= 0;
812 /* Read out options */
813 while (options
&& options
->name
) {
814 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
815 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
820 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
825 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
828 if (close(fd
) != 0) {
835 static int raw_flush(BlockDriverState
*bs
)
837 BDRVRawState
*s
= bs
->opaque
;
838 return qemu_fdatasync(s
->fd
);
842 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
844 struct xfs_flock64 fl
;
846 memset(&fl
, 0, sizeof(fl
));
847 fl
.l_whence
= SEEK_SET
;
848 fl
.l_start
= sector_num
<< 9;
849 fl
.l_len
= (int64_t)nb_sectors
<< 9;
851 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
852 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
860 static int raw_discard(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
)
863 BDRVRawState
*s
= bs
->opaque
;
866 return xfs_discard(s
, sector_num
, nb_sectors
);
873 static QEMUOptionParameter raw_create_options
[] = {
875 .name
= BLOCK_OPT_SIZE
,
877 .help
= "Virtual disk size"
882 static BlockDriver bdrv_file
= {
883 .format_name
= "file",
884 .protocol_name
= "file",
885 .instance_size
= sizeof(BDRVRawState
),
886 .bdrv_probe
= NULL
, /* no probe for protocols */
887 .bdrv_file_open
= raw_open
,
888 .bdrv_read
= raw_read
,
889 .bdrv_write
= raw_write
,
890 .bdrv_close
= raw_close
,
891 .bdrv_create
= raw_create
,
892 .bdrv_flush
= raw_flush
,
893 .bdrv_discard
= raw_discard
,
895 .bdrv_aio_readv
= raw_aio_readv
,
896 .bdrv_aio_writev
= raw_aio_writev
,
897 .bdrv_aio_flush
= raw_aio_flush
,
899 .bdrv_truncate
= raw_truncate
,
900 .bdrv_getlength
= raw_getlength
,
901 .bdrv_get_allocated_file_size
902 = raw_get_allocated_file_size
,
904 .create_options
= raw_create_options
,
907 /***********************************************/
911 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
912 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
914 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
916 kern_return_t kernResult
;
917 mach_port_t masterPort
;
918 CFMutableDictionaryRef classesToMatch
;
920 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
921 if ( KERN_SUCCESS
!= kernResult
) {
922 printf( "IOMasterPort returned %d\n", kernResult
);
925 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
926 if ( classesToMatch
== NULL
) {
927 printf( "IOServiceMatching returned a NULL dictionary.\n" );
929 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
931 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
932 if ( KERN_SUCCESS
!= kernResult
)
934 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
940 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
942 io_object_t nextMedia
;
943 kern_return_t kernResult
= KERN_FAILURE
;
945 nextMedia
= IOIteratorNext( mediaIterator
);
948 CFTypeRef bsdPathAsCFString
;
949 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
950 if ( bsdPathAsCFString
) {
951 size_t devPathLength
;
952 strcpy( bsdPath
, _PATH_DEV
);
953 strcat( bsdPath
, "r" );
954 devPathLength
= strlen( bsdPath
);
955 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
956 kernResult
= KERN_SUCCESS
;
958 CFRelease( bsdPathAsCFString
);
960 IOObjectRelease( nextMedia
);
968 static int hdev_probe_device(const char *filename
)
972 /* allow a dedicated CD-ROM driver to match with a higher priority */
973 if (strstart(filename
, "/dev/cdrom", NULL
))
976 if (stat(filename
, &st
) >= 0 &&
977 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
984 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
986 BDRVRawState
*s
= bs
->opaque
;
989 if (strstart(filename
, "/dev/cdrom", NULL
)) {
990 kern_return_t kernResult
;
991 io_iterator_t mediaIterator
;
992 char bsdPath
[ MAXPATHLEN
];
995 kernResult
= FindEjectableCDMedia( &mediaIterator
);
996 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
998 if ( bsdPath
[ 0 ] != '\0' ) {
999 strcat(bsdPath
,"s0");
1000 /* some CDs don't have a partition 0 */
1001 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1003 bsdPath
[strlen(bsdPath
)-1] = '1';
1010 if ( mediaIterator
)
1011 IOObjectRelease( mediaIterator
);
1015 s
->type
= FTYPE_FILE
;
1016 #if defined(__linux__)
1018 char resolved_path
[ MAXPATHLEN
], *temp
;
1020 temp
= realpath(filename
, resolved_path
);
1021 if (temp
&& strstart(temp
, "/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 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1047 printf("Floppy closed\n");
1051 if (s
->fd_got_error
&&
1052 (get_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
= get_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
= get_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
);
1087 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1088 unsigned long int req
, void *buf
,
1089 BlockDriverCompletionFunc
*cb
, void *opaque
)
1091 BDRVRawState
*s
= bs
->opaque
;
1093 if (fd_open(bs
) < 0)
1095 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
1098 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1099 static int fd_open(BlockDriverState
*bs
)
1101 BDRVRawState
*s
= bs
->opaque
;
1103 /* this is just to ensure s->fd is sane (its called by io ops) */
1108 #else /* !linux && !FreeBSD */
1110 static int fd_open(BlockDriverState
*bs
)
1115 #endif /* !linux && !FreeBSD */
1117 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1121 struct stat stat_buf
;
1122 int64_t total_size
= 0;
1124 /* Read out options */
1125 while (options
&& options
->name
) {
1126 if (!strcmp(options
->name
, "size")) {
1127 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1132 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1136 if (fstat(fd
, &stat_buf
) < 0)
1138 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1140 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1147 static int hdev_has_zero_init(BlockDriverState
*bs
)
1152 static BlockDriver bdrv_host_device
= {
1153 .format_name
= "host_device",
1154 .protocol_name
= "host_device",
1155 .instance_size
= sizeof(BDRVRawState
),
1156 .bdrv_probe_device
= hdev_probe_device
,
1157 .bdrv_file_open
= hdev_open
,
1158 .bdrv_close
= raw_close
,
1159 .bdrv_create
= hdev_create
,
1160 .create_options
= raw_create_options
,
1161 .bdrv_has_zero_init
= hdev_has_zero_init
,
1162 .bdrv_flush
= raw_flush
,
1164 .bdrv_aio_readv
= raw_aio_readv
,
1165 .bdrv_aio_writev
= raw_aio_writev
,
1166 .bdrv_aio_flush
= raw_aio_flush
,
1168 .bdrv_read
= raw_read
,
1169 .bdrv_write
= raw_write
,
1170 .bdrv_getlength
= raw_getlength
,
1171 .bdrv_get_allocated_file_size
1172 = raw_get_allocated_file_size
,
1174 /* generic scsi device */
1176 .bdrv_ioctl
= hdev_ioctl
,
1177 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1182 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1184 BDRVRawState
*s
= bs
->opaque
;
1189 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1190 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1194 /* close fd so that we can reopen it as needed */
1197 s
->fd_media_changed
= 1;
1202 static int floppy_probe_device(const char *filename
)
1206 struct floppy_struct fdparam
;
1209 if (strstart(filename
, "/dev/fd", NULL
))
1212 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1216 ret
= fstat(fd
, &st
);
1217 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1221 /* Attempt to detect via a floppy specific ioctl */
1222 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1233 static int floppy_is_inserted(BlockDriverState
*bs
)
1235 return fd_open(bs
) >= 0;
1238 static int floppy_media_changed(BlockDriverState
*bs
)
1240 BDRVRawState
*s
= bs
->opaque
;
1244 * XXX: we do not have a true media changed indication.
1245 * It does not work if the floppy is changed without trying to read it.
1248 ret
= s
->fd_media_changed
;
1249 s
->fd_media_changed
= 0;
1251 printf("Floppy changed=%d\n", ret
);
1256 static void floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1258 BDRVRawState
*s
= bs
->opaque
;
1265 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1267 if (ioctl(fd
, FDEJECT
, 0) < 0)
1273 static BlockDriver bdrv_host_floppy
= {
1274 .format_name
= "host_floppy",
1275 .protocol_name
= "host_floppy",
1276 .instance_size
= sizeof(BDRVRawState
),
1277 .bdrv_probe_device
= floppy_probe_device
,
1278 .bdrv_file_open
= floppy_open
,
1279 .bdrv_close
= raw_close
,
1280 .bdrv_create
= hdev_create
,
1281 .create_options
= raw_create_options
,
1282 .bdrv_has_zero_init
= hdev_has_zero_init
,
1283 .bdrv_flush
= raw_flush
,
1285 .bdrv_aio_readv
= raw_aio_readv
,
1286 .bdrv_aio_writev
= raw_aio_writev
,
1287 .bdrv_aio_flush
= raw_aio_flush
,
1289 .bdrv_read
= raw_read
,
1290 .bdrv_write
= raw_write
,
1291 .bdrv_getlength
= raw_getlength
,
1292 .bdrv_get_allocated_file_size
1293 = raw_get_allocated_file_size
,
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
)
1317 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1321 ret
= fstat(fd
, &st
);
1322 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1326 /* Attempt to detect via a CDROM specific ioctl */
1327 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1337 static int cdrom_is_inserted(BlockDriverState
*bs
)
1339 BDRVRawState
*s
= bs
->opaque
;
1342 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1343 if (ret
== CDS_DISC_OK
)
1348 static void cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1350 BDRVRawState
*s
= bs
->opaque
;
1353 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1354 perror("CDROMEJECT");
1356 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1357 perror("CDROMEJECT");
1361 static void cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1363 BDRVRawState
*s
= bs
->opaque
;
1365 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1367 * Note: an error can happen if the distribution automatically
1370 /* perror("CDROM_LOCKDOOR"); */
1374 static BlockDriver bdrv_host_cdrom
= {
1375 .format_name
= "host_cdrom",
1376 .protocol_name
= "host_cdrom",
1377 .instance_size
= sizeof(BDRVRawState
),
1378 .bdrv_probe_device
= cdrom_probe_device
,
1379 .bdrv_file_open
= cdrom_open
,
1380 .bdrv_close
= raw_close
,
1381 .bdrv_create
= hdev_create
,
1382 .create_options
= raw_create_options
,
1383 .bdrv_has_zero_init
= hdev_has_zero_init
,
1384 .bdrv_flush
= raw_flush
,
1386 .bdrv_aio_readv
= raw_aio_readv
,
1387 .bdrv_aio_writev
= raw_aio_writev
,
1388 .bdrv_aio_flush
= raw_aio_flush
,
1390 .bdrv_read
= raw_read
,
1391 .bdrv_write
= raw_write
,
1392 .bdrv_getlength
= raw_getlength
,
1393 .bdrv_get_allocated_file_size
1394 = raw_get_allocated_file_size
,
1396 /* removable device support */
1397 .bdrv_is_inserted
= cdrom_is_inserted
,
1398 .bdrv_eject
= cdrom_eject
,
1399 .bdrv_set_locked
= cdrom_set_locked
,
1401 /* generic scsi device */
1402 .bdrv_ioctl
= hdev_ioctl
,
1403 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1405 #endif /* __linux__ */
1407 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1408 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1410 BDRVRawState
*s
= bs
->opaque
;
1415 ret
= raw_open_common(bs
, filename
, flags
, 0);
1419 /* make sure the door isnt locked at this time */
1420 ioctl(s
->fd
, CDIOCALLOW
);
1424 static int cdrom_probe_device(const char *filename
)
1426 if (strstart(filename
, "/dev/cd", NULL
) ||
1427 strstart(filename
, "/dev/acd", NULL
))
1432 static int cdrom_reopen(BlockDriverState
*bs
)
1434 BDRVRawState
*s
= bs
->opaque
;
1438 * Force reread of possibly changed/newly loaded disc,
1439 * FreeBSD seems to not notice sometimes...
1443 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1450 /* make sure the door isnt locked at this time */
1451 ioctl(s
->fd
, CDIOCALLOW
);
1455 static int cdrom_is_inserted(BlockDriverState
*bs
)
1457 return raw_getlength(bs
) > 0;
1460 static void cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1462 BDRVRawState
*s
= bs
->opaque
;
1467 (void) ioctl(s
->fd
, CDIOCALLOW
);
1470 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1471 perror("CDIOCEJECT");
1473 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1474 perror("CDIOCCLOSE");
1480 static void cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1482 BDRVRawState
*s
= bs
->opaque
;
1486 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1488 * Note: an error can happen if the distribution automatically
1491 /* perror("CDROM_LOCKDOOR"); */
1495 static BlockDriver bdrv_host_cdrom
= {
1496 .format_name
= "host_cdrom",
1497 .protocol_name
= "host_cdrom",
1498 .instance_size
= sizeof(BDRVRawState
),
1499 .bdrv_probe_device
= cdrom_probe_device
,
1500 .bdrv_file_open
= cdrom_open
,
1501 .bdrv_close
= raw_close
,
1502 .bdrv_create
= hdev_create
,
1503 .create_options
= raw_create_options
,
1504 .bdrv_has_zero_init
= hdev_has_zero_init
,
1505 .bdrv_flush
= raw_flush
,
1507 .bdrv_aio_readv
= raw_aio_readv
,
1508 .bdrv_aio_writev
= raw_aio_writev
,
1509 .bdrv_aio_flush
= raw_aio_flush
,
1511 .bdrv_read
= raw_read
,
1512 .bdrv_write
= raw_write
,
1513 .bdrv_getlength
= raw_getlength
,
1514 .bdrv_get_allocated_file_size
1515 = raw_get_allocated_file_size
,
1517 /* removable device support */
1518 .bdrv_is_inserted
= cdrom_is_inserted
,
1519 .bdrv_eject
= cdrom_eject
,
1520 .bdrv_set_locked
= cdrom_set_locked
,
1522 #endif /* __FreeBSD__ */
1524 static void bdrv_file_init(void)
1527 * Register all the drivers. Note that order is important, the driver
1528 * registered last will get probed first.
1530 bdrv_register(&bdrv_file
);
1531 bdrv_register(&bdrv_host_device
);
1533 bdrv_register(&bdrv_host_floppy
);
1534 bdrv_register(&bdrv_host_cdrom
);
1536 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1537 bdrv_register(&bdrv_host_cdrom
);
1541 block_init(bdrv_file_init
);