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 #ifdef CONFIG_LINUX_AIO
234 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
235 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
237 /* We're falling back to POSIX AIO in some cases */
240 s
->aio_ctx
= laio_init();
248 if (paio_init() < 0) {
251 #ifdef CONFIG_LINUX_AIO
257 if (platform_test_xfs_fd(s
->fd
)) {
265 qemu_vfree(s
->aligned_buf
);
271 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
273 BDRVRawState
*s
= bs
->opaque
;
275 s
->type
= FTYPE_FILE
;
276 return raw_open_common(bs
, filename
, flags
, 0);
279 /* XXX: use host sector size if necessary with:
280 #ifdef DIOCGSECTORSIZE
282 unsigned int sectorsize = 512;
283 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
284 sectorsize > bufsize)
285 bufsize = sectorsize;
289 uint32_t blockSize = 512;
290 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
297 * offset and count are in bytes, but must be multiples of 512 for files
298 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
300 * This function may be called without alignment if the caller ensures
301 * that O_DIRECT is not in effect.
303 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
304 uint8_t *buf
, int count
)
306 BDRVRawState
*s
= bs
->opaque
;
313 ret
= pread(s
->fd
, buf
, count
, offset
);
317 /* Allow reads beyond the end (needed for pwrite) */
318 if ((ret
== 0) && bs
->growable
) {
319 int64_t size
= raw_getlength(bs
);
320 if (offset
>= size
) {
321 memset(buf
, 0, count
);
326 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
327 "] read failed %d : %d = %s\n",
328 s
->fd
, bs
->filename
, offset
, buf
, count
,
329 bs
->total_sectors
, ret
, errno
, strerror(errno
));
331 /* Try harder for CDrom. */
332 if (s
->type
!= FTYPE_FILE
) {
333 ret
= pread(s
->fd
, buf
, count
, offset
);
336 ret
= pread(s
->fd
, buf
, count
, offset
);
340 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
341 "] retry read failed %d : %d = %s\n",
342 s
->fd
, bs
->filename
, offset
, buf
, count
,
343 bs
->total_sectors
, ret
, errno
, strerror(errno
));
346 return (ret
< 0) ? -errno
: ret
;
350 * offset and count are in bytes, but must be multiples of the sector size
351 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
354 * This function may be called without alignment if the caller ensures
355 * that O_DIRECT is not in effect.
357 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
358 const uint8_t *buf
, int count
)
360 BDRVRawState
*s
= bs
->opaque
;
367 ret
= pwrite(s
->fd
, buf
, count
, offset
);
371 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
372 "] write failed %d : %d = %s\n",
373 s
->fd
, bs
->filename
, offset
, buf
, count
,
374 bs
->total_sectors
, ret
, errno
, strerror(errno
));
376 return (ret
< 0) ? -errno
: ret
;
381 * offset and count are in bytes and possibly not aligned. For files opened
382 * with O_DIRECT, necessary alignments are ensured before calling
383 * raw_pread_aligned to do the actual read.
385 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
386 uint8_t *buf
, int count
)
388 BDRVRawState
*s
= bs
->opaque
;
389 unsigned sector_mask
= bs
->buffer_alignment
- 1;
390 int size
, ret
, shift
, sum
;
394 if (s
->aligned_buf
!= NULL
) {
396 if (offset
& sector_mask
) {
397 /* align offset on a sector size bytes boundary */
399 shift
= offset
& sector_mask
;
400 size
= (shift
+ count
+ sector_mask
) & ~sector_mask
;
401 if (size
> s
->aligned_buf_size
)
402 size
= s
->aligned_buf_size
;
403 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
407 size
= bs
->buffer_alignment
- shift
;
410 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
420 if (count
& sector_mask
|| (uintptr_t) buf
& sector_mask
) {
422 /* read on aligned buffer */
426 size
= (count
+ sector_mask
) & ~sector_mask
;
427 if (size
> s
->aligned_buf_size
)
428 size
= s
->aligned_buf_size
;
430 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
433 } else if (ret
== 0) {
434 fprintf(stderr
, "raw_pread: read beyond end of file\n");
442 memcpy(buf
, s
->aligned_buf
, size
);
454 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
457 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
458 uint8_t *buf
, int nb_sectors
)
462 ret
= raw_pread(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
463 nb_sectors
* BDRV_SECTOR_SIZE
);
464 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
470 * offset and count are in bytes and possibly not aligned. For files opened
471 * with O_DIRECT, necessary alignments are ensured before calling
472 * raw_pwrite_aligned to do the actual write.
474 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
475 const uint8_t *buf
, int count
)
477 BDRVRawState
*s
= bs
->opaque
;
478 unsigned sector_mask
= bs
->buffer_alignment
- 1;
479 int size
, ret
, shift
, sum
;
483 if (s
->aligned_buf
!= NULL
) {
485 if (offset
& sector_mask
) {
486 /* align offset on a sector size bytes boundary */
487 shift
= offset
& sector_mask
;
488 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
,
489 bs
->buffer_alignment
);
493 size
= bs
->buffer_alignment
- shift
;
496 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
498 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
,
499 bs
->buffer_alignment
);
511 if (count
& sector_mask
|| (uintptr_t) buf
& sector_mask
) {
513 while ((size
= (count
& ~sector_mask
)) != 0) {
515 if (size
> s
->aligned_buf_size
)
516 size
= s
->aligned_buf_size
;
518 memcpy(s
->aligned_buf
, buf
, size
);
520 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
529 /* here, count < sector_size because (count & ~sector_mask) == 0 */
531 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
,
532 bs
->buffer_alignment
);
535 memcpy(s
->aligned_buf
, buf
, count
);
537 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
,
538 bs
->buffer_alignment
);
549 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
552 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
553 const uint8_t *buf
, int nb_sectors
)
556 ret
= raw_pwrite(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
557 nb_sectors
* BDRV_SECTOR_SIZE
);
558 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
564 * Check if all memory in this vector is sector aligned.
566 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
570 for (i
= 0; i
< qiov
->niov
; i
++) {
571 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
579 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
580 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
581 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
583 BDRVRawState
*s
= bs
->opaque
;
589 * If O_DIRECT is used the buffer needs to be aligned on a sector
590 * boundary. Check if this is the case or telll the low-level
591 * driver that it needs to copy the buffer.
593 if (s
->aligned_buf
) {
594 if (!qiov_is_aligned(bs
, qiov
)) {
595 type
|= QEMU_AIO_MISALIGNED
;
596 #ifdef CONFIG_LINUX_AIO
597 } else if (s
->use_aio
) {
598 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
599 nb_sectors
, cb
, opaque
, type
);
604 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
608 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
609 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
610 BlockDriverCompletionFunc
*cb
, void *opaque
)
612 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
613 cb
, opaque
, QEMU_AIO_READ
);
616 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
617 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
618 BlockDriverCompletionFunc
*cb
, void *opaque
)
620 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
621 cb
, opaque
, QEMU_AIO_WRITE
);
624 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
625 BlockDriverCompletionFunc
*cb
, void *opaque
)
627 BDRVRawState
*s
= bs
->opaque
;
632 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
635 static void raw_close(BlockDriverState
*bs
)
637 BDRVRawState
*s
= bs
->opaque
;
641 if (s
->aligned_buf
!= NULL
)
642 qemu_vfree(s
->aligned_buf
);
646 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
648 BDRVRawState
*s
= bs
->opaque
;
649 if (s
->type
!= FTYPE_FILE
)
651 if (ftruncate(s
->fd
, offset
) < 0)
657 static int64_t raw_getlength(BlockDriverState
*bs
)
659 BDRVRawState
*s
= bs
->opaque
;
665 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
668 if (ioctl(fd
, DIOCGDINFO
, &dl
))
670 return (uint64_t)dl
.d_secsize
*
671 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
675 #elif defined(__NetBSD__)
676 static int64_t raw_getlength(BlockDriverState
*bs
)
678 BDRVRawState
*s
= bs
->opaque
;
684 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
685 struct dkwedge_info dkw
;
687 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
688 return dkw
.dkw_size
* 512;
692 if (ioctl(fd
, DIOCGDINFO
, &dl
))
694 return (uint64_t)dl
.d_secsize
*
695 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
700 #elif defined(__sun__)
701 static int64_t raw_getlength(BlockDriverState
*bs
)
703 BDRVRawState
*s
= bs
->opaque
;
704 struct dk_minfo minfo
;
713 * Use the DKIOCGMEDIAINFO ioctl to read the size.
715 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
717 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
721 * There are reports that lseek on some devices fails, but
722 * irc discussion said that contingency on contingency was overkill.
724 return lseek(s
->fd
, 0, SEEK_END
);
726 #elif defined(CONFIG_BSD)
727 static int64_t raw_getlength(BlockDriverState
*bs
)
729 BDRVRawState
*s
= bs
->opaque
;
733 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
742 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
745 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
746 #ifdef DIOCGMEDIASIZE
747 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
748 #elif defined(DIOCGPART)
751 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
752 size
= pi
.media_size
;
759 size
= LONG_LONG_MAX
;
761 size
= lseek(fd
, 0LL, SEEK_END
);
763 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
766 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
767 if (size
== 2048LL * (unsigned)-1)
769 /* XXX no disc? maybe we need to reopen... */
770 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
777 size
= lseek(fd
, 0, SEEK_END
);
782 static int64_t raw_getlength(BlockDriverState
*bs
)
784 BDRVRawState
*s
= bs
->opaque
;
792 return lseek(s
->fd
, 0, SEEK_END
);
796 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
799 BDRVRawState
*s
= bs
->opaque
;
801 if (fstat(s
->fd
, &st
) < 0) {
804 return (int64_t)st
.st_blocks
* 512;
807 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
811 int64_t total_size
= 0;
813 /* Read out options */
814 while (options
&& options
->name
) {
815 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
816 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
821 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
826 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
829 if (close(fd
) != 0) {
836 static int raw_flush(BlockDriverState
*bs
)
838 BDRVRawState
*s
= bs
->opaque
;
839 return qemu_fdatasync(s
->fd
);
843 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
845 struct xfs_flock64 fl
;
847 memset(&fl
, 0, sizeof(fl
));
848 fl
.l_whence
= SEEK_SET
;
849 fl
.l_start
= sector_num
<< 9;
850 fl
.l_len
= (int64_t)nb_sectors
<< 9;
852 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
853 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
861 static int raw_discard(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
)
864 BDRVRawState
*s
= bs
->opaque
;
867 return xfs_discard(s
, sector_num
, nb_sectors
);
874 static QEMUOptionParameter raw_create_options
[] = {
876 .name
= BLOCK_OPT_SIZE
,
878 .help
= "Virtual disk size"
883 static BlockDriver bdrv_file
= {
884 .format_name
= "file",
885 .protocol_name
= "file",
886 .instance_size
= sizeof(BDRVRawState
),
887 .bdrv_probe
= NULL
, /* no probe for protocols */
888 .bdrv_file_open
= raw_open
,
889 .bdrv_read
= raw_read
,
890 .bdrv_write
= raw_write
,
891 .bdrv_close
= raw_close
,
892 .bdrv_create
= raw_create
,
893 .bdrv_flush
= raw_flush
,
894 .bdrv_discard
= raw_discard
,
896 .bdrv_aio_readv
= raw_aio_readv
,
897 .bdrv_aio_writev
= raw_aio_writev
,
898 .bdrv_aio_flush
= raw_aio_flush
,
900 .bdrv_truncate
= raw_truncate
,
901 .bdrv_getlength
= raw_getlength
,
902 .bdrv_get_allocated_file_size
903 = raw_get_allocated_file_size
,
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__)
1019 char resolved_path
[ MAXPATHLEN
], *temp
;
1021 temp
= realpath(filename
, resolved_path
);
1022 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1028 return raw_open_common(bs
, filename
, flags
, 0);
1031 #if defined(__linux__)
1032 /* Note: we do not have a reliable method to detect if the floppy is
1033 present. The current method is to try to open the floppy at every
1034 I/O and to keep it opened during a few hundreds of ms. */
1035 static int fd_open(BlockDriverState
*bs
)
1037 BDRVRawState
*s
= bs
->opaque
;
1038 int last_media_present
;
1040 if (s
->type
!= FTYPE_FD
)
1042 last_media_present
= (s
->fd
>= 0);
1044 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1048 printf("Floppy closed\n");
1052 if (s
->fd_got_error
&&
1053 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1055 printf("No floppy (open delayed)\n");
1059 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1061 s
->fd_error_time
= get_clock();
1062 s
->fd_got_error
= 1;
1063 if (last_media_present
)
1064 s
->fd_media_changed
= 1;
1066 printf("No floppy\n");
1071 printf("Floppy opened\n");
1074 if (!last_media_present
)
1075 s
->fd_media_changed
= 1;
1076 s
->fd_open_time
= get_clock();
1077 s
->fd_got_error
= 0;
1081 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1083 BDRVRawState
*s
= bs
->opaque
;
1085 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
;
1094 if (fd_open(bs
) < 0)
1096 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
1099 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1100 static int fd_open(BlockDriverState
*bs
)
1102 BDRVRawState
*s
= bs
->opaque
;
1104 /* this is just to ensure s->fd is sane (its called by io ops) */
1109 #else /* !linux && !FreeBSD */
1111 static int fd_open(BlockDriverState
*bs
)
1116 #endif /* !linux && !FreeBSD */
1118 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1122 struct stat stat_buf
;
1123 int64_t total_size
= 0;
1125 /* Read out options */
1126 while (options
&& options
->name
) {
1127 if (!strcmp(options
->name
, "size")) {
1128 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1133 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1137 if (fstat(fd
, &stat_buf
) < 0)
1139 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1141 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1148 static int hdev_has_zero_init(BlockDriverState
*bs
)
1153 static BlockDriver bdrv_host_device
= {
1154 .format_name
= "host_device",
1155 .protocol_name
= "host_device",
1156 .instance_size
= sizeof(BDRVRawState
),
1157 .bdrv_probe_device
= hdev_probe_device
,
1158 .bdrv_file_open
= hdev_open
,
1159 .bdrv_close
= raw_close
,
1160 .bdrv_create
= hdev_create
,
1161 .create_options
= raw_create_options
,
1162 .bdrv_has_zero_init
= hdev_has_zero_init
,
1163 .bdrv_flush
= raw_flush
,
1165 .bdrv_aio_readv
= raw_aio_readv
,
1166 .bdrv_aio_writev
= raw_aio_writev
,
1167 .bdrv_aio_flush
= raw_aio_flush
,
1169 .bdrv_read
= raw_read
,
1170 .bdrv_write
= raw_write
,
1171 .bdrv_getlength
= raw_getlength
,
1172 .bdrv_get_allocated_file_size
1173 = raw_get_allocated_file_size
,
1175 /* generic scsi device */
1177 .bdrv_ioctl
= hdev_ioctl
,
1178 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1183 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1185 BDRVRawState
*s
= bs
->opaque
;
1190 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1191 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1195 /* close fd so that we can reopen it as needed */
1198 s
->fd_media_changed
= 1;
1203 static int floppy_probe_device(const char *filename
)
1207 struct floppy_struct fdparam
;
1210 if (strstart(filename
, "/dev/fd", NULL
))
1213 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1217 ret
= fstat(fd
, &st
);
1218 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1222 /* Attempt to detect via a floppy specific ioctl */
1223 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1234 static int floppy_is_inserted(BlockDriverState
*bs
)
1236 return fd_open(bs
) >= 0;
1239 static int floppy_media_changed(BlockDriverState
*bs
)
1241 BDRVRawState
*s
= bs
->opaque
;
1245 * XXX: we do not have a true media changed indication.
1246 * It does not work if the floppy is changed without trying to read it.
1249 ret
= s
->fd_media_changed
;
1250 s
->fd_media_changed
= 0;
1252 printf("Floppy changed=%d\n", ret
);
1257 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1259 BDRVRawState
*s
= bs
->opaque
;
1266 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1268 if (ioctl(fd
, FDEJECT
, 0) < 0)
1276 static BlockDriver bdrv_host_floppy
= {
1277 .format_name
= "host_floppy",
1278 .protocol_name
= "host_floppy",
1279 .instance_size
= sizeof(BDRVRawState
),
1280 .bdrv_probe_device
= floppy_probe_device
,
1281 .bdrv_file_open
= floppy_open
,
1282 .bdrv_close
= raw_close
,
1283 .bdrv_create
= hdev_create
,
1284 .create_options
= raw_create_options
,
1285 .bdrv_has_zero_init
= hdev_has_zero_init
,
1286 .bdrv_flush
= raw_flush
,
1288 .bdrv_aio_readv
= raw_aio_readv
,
1289 .bdrv_aio_writev
= raw_aio_writev
,
1290 .bdrv_aio_flush
= raw_aio_flush
,
1292 .bdrv_read
= raw_read
,
1293 .bdrv_write
= raw_write
,
1294 .bdrv_getlength
= raw_getlength
,
1295 .bdrv_get_allocated_file_size
1296 = raw_get_allocated_file_size
,
1298 /* removable device support */
1299 .bdrv_is_inserted
= floppy_is_inserted
,
1300 .bdrv_media_changed
= floppy_media_changed
,
1301 .bdrv_eject
= floppy_eject
,
1304 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1306 BDRVRawState
*s
= bs
->opaque
;
1310 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1311 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1314 static int cdrom_probe_device(const char *filename
)
1320 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1324 ret
= fstat(fd
, &st
);
1325 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1329 /* Attempt to detect via a CDROM specific ioctl */
1330 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1340 static int cdrom_is_inserted(BlockDriverState
*bs
)
1342 BDRVRawState
*s
= bs
->opaque
;
1345 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1346 if (ret
== CDS_DISC_OK
)
1351 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1353 BDRVRawState
*s
= bs
->opaque
;
1356 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1357 perror("CDROMEJECT");
1359 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1360 perror("CDROMEJECT");
1366 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1368 BDRVRawState
*s
= bs
->opaque
;
1370 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1372 * Note: an error can happen if the distribution automatically
1375 /* perror("CDROM_LOCKDOOR"); */
1381 static BlockDriver bdrv_host_cdrom
= {
1382 .format_name
= "host_cdrom",
1383 .protocol_name
= "host_cdrom",
1384 .instance_size
= sizeof(BDRVRawState
),
1385 .bdrv_probe_device
= cdrom_probe_device
,
1386 .bdrv_file_open
= cdrom_open
,
1387 .bdrv_close
= raw_close
,
1388 .bdrv_create
= hdev_create
,
1389 .create_options
= raw_create_options
,
1390 .bdrv_has_zero_init
= hdev_has_zero_init
,
1391 .bdrv_flush
= raw_flush
,
1393 .bdrv_aio_readv
= raw_aio_readv
,
1394 .bdrv_aio_writev
= raw_aio_writev
,
1395 .bdrv_aio_flush
= raw_aio_flush
,
1397 .bdrv_read
= raw_read
,
1398 .bdrv_write
= raw_write
,
1399 .bdrv_getlength
= raw_getlength
,
1400 .bdrv_get_allocated_file_size
1401 = raw_get_allocated_file_size
,
1403 /* removable device support */
1404 .bdrv_is_inserted
= cdrom_is_inserted
,
1405 .bdrv_eject
= cdrom_eject
,
1406 .bdrv_set_locked
= cdrom_set_locked
,
1408 /* generic scsi device */
1409 .bdrv_ioctl
= hdev_ioctl
,
1410 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1412 #endif /* __linux__ */
1414 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1415 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1417 BDRVRawState
*s
= bs
->opaque
;
1422 ret
= raw_open_common(bs
, filename
, flags
, 0);
1426 /* make sure the door isnt locked at this time */
1427 ioctl(s
->fd
, CDIOCALLOW
);
1431 static int cdrom_probe_device(const char *filename
)
1433 if (strstart(filename
, "/dev/cd", NULL
) ||
1434 strstart(filename
, "/dev/acd", NULL
))
1439 static int cdrom_reopen(BlockDriverState
*bs
)
1441 BDRVRawState
*s
= bs
->opaque
;
1445 * Force reread of possibly changed/newly loaded disc,
1446 * FreeBSD seems to not notice sometimes...
1450 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1457 /* make sure the door isnt locked at this time */
1458 ioctl(s
->fd
, CDIOCALLOW
);
1462 static int cdrom_is_inserted(BlockDriverState
*bs
)
1464 return raw_getlength(bs
) > 0;
1467 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1469 BDRVRawState
*s
= bs
->opaque
;
1474 (void) ioctl(s
->fd
, CDIOCALLOW
);
1477 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1478 perror("CDIOCEJECT");
1480 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1481 perror("CDIOCCLOSE");
1484 if (cdrom_reopen(bs
) < 0)
1489 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1491 BDRVRawState
*s
= bs
->opaque
;
1495 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1497 * Note: an error can happen if the distribution automatically
1500 /* perror("CDROM_LOCKDOOR"); */
1506 static BlockDriver bdrv_host_cdrom
= {
1507 .format_name
= "host_cdrom",
1508 .protocol_name
= "host_cdrom",
1509 .instance_size
= sizeof(BDRVRawState
),
1510 .bdrv_probe_device
= cdrom_probe_device
,
1511 .bdrv_file_open
= cdrom_open
,
1512 .bdrv_close
= raw_close
,
1513 .bdrv_create
= hdev_create
,
1514 .create_options
= raw_create_options
,
1515 .bdrv_has_zero_init
= hdev_has_zero_init
,
1516 .bdrv_flush
= raw_flush
,
1518 .bdrv_aio_readv
= raw_aio_readv
,
1519 .bdrv_aio_writev
= raw_aio_writev
,
1520 .bdrv_aio_flush
= raw_aio_flush
,
1522 .bdrv_read
= raw_read
,
1523 .bdrv_write
= raw_write
,
1524 .bdrv_getlength
= raw_getlength
,
1525 .bdrv_get_allocated_file_size
1526 = raw_get_allocated_file_size
,
1528 /* removable device support */
1529 .bdrv_is_inserted
= cdrom_is_inserted
,
1530 .bdrv_eject
= cdrom_eject
,
1531 .bdrv_set_locked
= cdrom_set_locked
,
1533 #endif /* __FreeBSD__ */
1535 static void bdrv_file_init(void)
1538 * Register all the drivers. Note that order is important, the driver
1539 * registered last will get probed first.
1541 bdrv_register(&bdrv_file
);
1542 bdrv_register(&bdrv_host_device
);
1544 bdrv_register(&bdrv_host_floppy
);
1545 bdrv_register(&bdrv_host_cdrom
);
1547 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1548 bdrv_register(&bdrv_host_cdrom
);
1552 block_init(bdrv_file_init
);