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
50 #include <sys/ioctl.h>
51 #include <sys/param.h>
52 #include <linux/cdrom.h>
55 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
68 #include <sys/ioctl.h>
69 #include <sys/diskslice.h>
76 //#define DEBUG_FLOPPY
79 #if defined(DEBUG_BLOCK)
80 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
81 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
83 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
86 /* OS X does not have O_DSYNC */
89 #define O_DSYNC O_SYNC
90 #elif defined(O_FSYNC)
91 #define O_DSYNC O_FSYNC
95 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
97 #define O_DIRECT O_DSYNC
104 /* if the FD is not accessed during that time (in ns), we try to
105 reopen it to see if the disk has been changed */
106 #define FD_OPEN_TIMEOUT (1000000000)
108 #define MAX_BLOCKSIZE 4096
110 typedef struct BDRVRawState
{
114 #if defined(__linux__)
115 /* linux floppy specific */
116 int64_t fd_open_time
;
117 int64_t fd_error_time
;
119 int fd_media_changed
;
121 #ifdef CONFIG_LINUX_AIO
125 uint8_t *aligned_buf
;
126 unsigned aligned_buf_size
;
132 static int fd_open(BlockDriverState
*bs
);
133 static int64_t raw_getlength(BlockDriverState
*bs
);
135 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
136 static int cdrom_reopen(BlockDriverState
*bs
);
139 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
140 int bdrv_flags
, int open_flags
)
142 BDRVRawState
*s
= bs
->opaque
;
145 s
->open_flags
= open_flags
| O_BINARY
;
146 s
->open_flags
&= ~O_ACCMODE
;
147 if (bdrv_flags
& BDRV_O_RDWR
) {
148 s
->open_flags
|= O_RDWR
;
150 s
->open_flags
|= O_RDONLY
;
153 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
154 * and O_DIRECT for no caching. */
155 if ((bdrv_flags
& BDRV_O_NOCACHE
))
156 s
->open_flags
|= O_DIRECT
;
157 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
158 s
->open_flags
|= O_DSYNC
;
161 fd
= qemu_open(filename
, s
->open_flags
, 0644);
169 s
->aligned_buf
= NULL
;
171 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
173 * Allocate a buffer for read/modify/write cycles. Chose the size
174 * pessimistically as we don't know the block size yet.
176 s
->aligned_buf_size
= 32 * MAX_BLOCKSIZE
;
177 s
->aligned_buf
= qemu_memalign(MAX_BLOCKSIZE
, s
->aligned_buf_size
);
178 if (s
->aligned_buf
== NULL
) {
183 #ifdef CONFIG_LINUX_AIO
184 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
185 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
187 /* We're falling back to POSIX AIO in some cases */
190 s
->aio_ctx
= laio_init();
198 if (paio_init() < 0) {
201 #ifdef CONFIG_LINUX_AIO
207 if (platform_test_xfs_fd(s
->fd
)) {
215 qemu_vfree(s
->aligned_buf
);
221 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
223 BDRVRawState
*s
= bs
->opaque
;
225 s
->type
= FTYPE_FILE
;
226 return raw_open_common(bs
, filename
, flags
, 0);
229 /* XXX: use host sector size if necessary with:
230 #ifdef DIOCGSECTORSIZE
232 unsigned int sectorsize = 512;
233 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
234 sectorsize > bufsize)
235 bufsize = sectorsize;
239 uint32_t blockSize = 512;
240 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
247 * offset and count are in bytes, but must be multiples of 512 for files
248 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
250 * This function may be called without alignment if the caller ensures
251 * that O_DIRECT is not in effect.
253 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
254 uint8_t *buf
, int count
)
256 BDRVRawState
*s
= bs
->opaque
;
263 ret
= pread(s
->fd
, buf
, count
, offset
);
267 /* Allow reads beyond the end (needed for pwrite) */
268 if ((ret
== 0) && bs
->growable
) {
269 int64_t size
= raw_getlength(bs
);
270 if (offset
>= size
) {
271 memset(buf
, 0, count
);
276 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
277 "] read failed %d : %d = %s\n",
278 s
->fd
, bs
->filename
, offset
, buf
, count
,
279 bs
->total_sectors
, ret
, errno
, strerror(errno
));
281 /* Try harder for CDrom. */
282 if (s
->type
!= FTYPE_FILE
) {
283 ret
= pread(s
->fd
, buf
, count
, offset
);
286 ret
= pread(s
->fd
, buf
, count
, offset
);
290 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
291 "] retry read failed %d : %d = %s\n",
292 s
->fd
, bs
->filename
, offset
, buf
, count
,
293 bs
->total_sectors
, ret
, errno
, strerror(errno
));
296 return (ret
< 0) ? -errno
: ret
;
300 * offset and count are in bytes, but must be multiples of the sector size
301 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
304 * This function may be called without alignment if the caller ensures
305 * that O_DIRECT is not in effect.
307 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
308 const uint8_t *buf
, int count
)
310 BDRVRawState
*s
= bs
->opaque
;
317 ret
= pwrite(s
->fd
, buf
, count
, offset
);
321 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
322 "] write failed %d : %d = %s\n",
323 s
->fd
, bs
->filename
, offset
, buf
, count
,
324 bs
->total_sectors
, ret
, errno
, strerror(errno
));
326 return (ret
< 0) ? -errno
: ret
;
331 * offset and count are in bytes and possibly not aligned. For files opened
332 * with O_DIRECT, necessary alignments are ensured before calling
333 * raw_pread_aligned to do the actual read.
335 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
336 uint8_t *buf
, int count
)
338 BDRVRawState
*s
= bs
->opaque
;
339 unsigned sector_mask
= bs
->buffer_alignment
- 1;
340 int size
, ret
, shift
, sum
;
344 if (s
->aligned_buf
!= NULL
) {
346 if (offset
& sector_mask
) {
347 /* align offset on a sector size bytes boundary */
349 shift
= offset
& sector_mask
;
350 size
= (shift
+ count
+ sector_mask
) & ~sector_mask
;
351 if (size
> s
->aligned_buf_size
)
352 size
= s
->aligned_buf_size
;
353 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
357 size
= bs
->buffer_alignment
- shift
;
360 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
370 if (count
& sector_mask
|| (uintptr_t) buf
& sector_mask
) {
372 /* read on aligned buffer */
376 size
= (count
+ sector_mask
) & ~sector_mask
;
377 if (size
> s
->aligned_buf_size
)
378 size
= s
->aligned_buf_size
;
380 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
383 } else if (ret
== 0) {
384 fprintf(stderr
, "raw_pread: read beyond end of file\n");
392 memcpy(buf
, s
->aligned_buf
, size
);
404 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
407 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
408 uint8_t *buf
, int nb_sectors
)
412 ret
= raw_pread(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
413 nb_sectors
* BDRV_SECTOR_SIZE
);
414 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
420 * offset and count are in bytes and possibly not aligned. For files opened
421 * with O_DIRECT, necessary alignments are ensured before calling
422 * raw_pwrite_aligned to do the actual write.
424 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
425 const uint8_t *buf
, int count
)
427 BDRVRawState
*s
= bs
->opaque
;
428 unsigned sector_mask
= bs
->buffer_alignment
- 1;
429 int size
, ret
, shift
, sum
;
433 if (s
->aligned_buf
!= NULL
) {
435 if (offset
& sector_mask
) {
436 /* align offset on a sector size bytes boundary */
437 shift
= offset
& sector_mask
;
438 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
,
439 bs
->buffer_alignment
);
443 size
= bs
->buffer_alignment
- shift
;
446 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
448 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
,
449 bs
->buffer_alignment
);
461 if (count
& sector_mask
|| (uintptr_t) buf
& sector_mask
) {
463 while ((size
= (count
& ~sector_mask
)) != 0) {
465 if (size
> s
->aligned_buf_size
)
466 size
= s
->aligned_buf_size
;
468 memcpy(s
->aligned_buf
, buf
, size
);
470 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
479 /* here, count < sector_size because (count & ~sector_mask) == 0 */
481 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
,
482 bs
->buffer_alignment
);
485 memcpy(s
->aligned_buf
, buf
, count
);
487 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
,
488 bs
->buffer_alignment
);
499 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
502 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
503 const uint8_t *buf
, int nb_sectors
)
506 ret
= raw_pwrite(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
507 nb_sectors
* BDRV_SECTOR_SIZE
);
508 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
514 * Check if all memory in this vector is sector aligned.
516 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
520 for (i
= 0; i
< qiov
->niov
; i
++) {
521 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
529 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
530 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
531 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
533 BDRVRawState
*s
= bs
->opaque
;
539 * If O_DIRECT is used the buffer needs to be aligned on a sector
540 * boundary. Check if this is the case or telll the low-level
541 * driver that it needs to copy the buffer.
543 if (s
->aligned_buf
) {
544 if (!qiov_is_aligned(bs
, qiov
)) {
545 type
|= QEMU_AIO_MISALIGNED
;
546 #ifdef CONFIG_LINUX_AIO
547 } else if (s
->use_aio
) {
548 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
549 nb_sectors
, cb
, opaque
, type
);
554 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
558 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
559 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
560 BlockDriverCompletionFunc
*cb
, void *opaque
)
562 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
563 cb
, opaque
, QEMU_AIO_READ
);
566 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
567 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
568 BlockDriverCompletionFunc
*cb
, void *opaque
)
570 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
571 cb
, opaque
, QEMU_AIO_WRITE
);
574 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
575 BlockDriverCompletionFunc
*cb
, void *opaque
)
577 BDRVRawState
*s
= bs
->opaque
;
582 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
585 static void raw_close(BlockDriverState
*bs
)
587 BDRVRawState
*s
= bs
->opaque
;
591 if (s
->aligned_buf
!= NULL
)
592 qemu_vfree(s
->aligned_buf
);
596 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
598 BDRVRawState
*s
= bs
->opaque
;
599 if (s
->type
!= FTYPE_FILE
)
601 if (ftruncate(s
->fd
, offset
) < 0)
607 static int64_t raw_getlength(BlockDriverState
*bs
)
609 BDRVRawState
*s
= bs
->opaque
;
615 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
618 if (ioctl(fd
, DIOCGDINFO
, &dl
))
620 return (uint64_t)dl
.d_secsize
*
621 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
625 #elif defined(__sun__)
626 static int64_t raw_getlength(BlockDriverState
*bs
)
628 BDRVRawState
*s
= bs
->opaque
;
629 struct dk_minfo minfo
;
638 * Use the DKIOCGMEDIAINFO ioctl to read the size.
640 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
642 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
646 * There are reports that lseek on some devices fails, but
647 * irc discussion said that contingency on contingency was overkill.
649 return lseek(s
->fd
, 0, SEEK_END
);
651 #elif defined(CONFIG_BSD)
652 static int64_t raw_getlength(BlockDriverState
*bs
)
654 BDRVRawState
*s
= bs
->opaque
;
658 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
667 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
670 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
671 #ifdef DIOCGMEDIASIZE
672 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
673 #elif defined(DIOCGPART)
676 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
677 size
= pi
.media_size
;
684 size
= LONG_LONG_MAX
;
686 size
= lseek(fd
, 0LL, SEEK_END
);
688 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
691 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
692 if (size
== 2048LL * (unsigned)-1)
694 /* XXX no disc? maybe we need to reopen... */
695 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
702 size
= lseek(fd
, 0, SEEK_END
);
707 static int64_t raw_getlength(BlockDriverState
*bs
)
709 BDRVRawState
*s
= bs
->opaque
;
717 return lseek(s
->fd
, 0, SEEK_END
);
721 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
725 int64_t total_size
= 0;
727 /* Read out options */
728 while (options
&& options
->name
) {
729 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
730 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
735 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
740 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
743 if (close(fd
) != 0) {
750 static int raw_flush(BlockDriverState
*bs
)
752 BDRVRawState
*s
= bs
->opaque
;
753 return qemu_fdatasync(s
->fd
);
757 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
759 struct xfs_flock64 fl
;
761 memset(&fl
, 0, sizeof(fl
));
762 fl
.l_whence
= SEEK_SET
;
763 fl
.l_start
= sector_num
<< 9;
764 fl
.l_len
= (int64_t)nb_sectors
<< 9;
766 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
767 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
775 static int raw_discard(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
)
778 BDRVRawState
*s
= bs
->opaque
;
781 return xfs_discard(s
, sector_num
, nb_sectors
);
788 static QEMUOptionParameter raw_create_options
[] = {
790 .name
= BLOCK_OPT_SIZE
,
792 .help
= "Virtual disk size"
797 static BlockDriver bdrv_file
= {
798 .format_name
= "file",
799 .protocol_name
= "file",
800 .instance_size
= sizeof(BDRVRawState
),
801 .bdrv_probe
= NULL
, /* no probe for protocols */
802 .bdrv_file_open
= raw_open
,
803 .bdrv_read
= raw_read
,
804 .bdrv_write
= raw_write
,
805 .bdrv_close
= raw_close
,
806 .bdrv_create
= raw_create
,
807 .bdrv_flush
= raw_flush
,
808 .bdrv_discard
= raw_discard
,
810 .bdrv_aio_readv
= raw_aio_readv
,
811 .bdrv_aio_writev
= raw_aio_writev
,
812 .bdrv_aio_flush
= raw_aio_flush
,
814 .bdrv_truncate
= raw_truncate
,
815 .bdrv_getlength
= raw_getlength
,
817 .create_options
= raw_create_options
,
820 /***********************************************/
824 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
825 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
827 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
829 kern_return_t kernResult
;
830 mach_port_t masterPort
;
831 CFMutableDictionaryRef classesToMatch
;
833 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
834 if ( KERN_SUCCESS
!= kernResult
) {
835 printf( "IOMasterPort returned %d\n", kernResult
);
838 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
839 if ( classesToMatch
== NULL
) {
840 printf( "IOServiceMatching returned a NULL dictionary.\n" );
842 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
844 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
845 if ( KERN_SUCCESS
!= kernResult
)
847 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
853 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
855 io_object_t nextMedia
;
856 kern_return_t kernResult
= KERN_FAILURE
;
858 nextMedia
= IOIteratorNext( mediaIterator
);
861 CFTypeRef bsdPathAsCFString
;
862 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
863 if ( bsdPathAsCFString
) {
864 size_t devPathLength
;
865 strcpy( bsdPath
, _PATH_DEV
);
866 strcat( bsdPath
, "r" );
867 devPathLength
= strlen( bsdPath
);
868 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
869 kernResult
= KERN_SUCCESS
;
871 CFRelease( bsdPathAsCFString
);
873 IOObjectRelease( nextMedia
);
881 static int hdev_probe_device(const char *filename
)
885 /* allow a dedicated CD-ROM driver to match with a higher priority */
886 if (strstart(filename
, "/dev/cdrom", NULL
))
889 if (stat(filename
, &st
) >= 0 &&
890 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
897 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
899 BDRVRawState
*s
= bs
->opaque
;
902 if (strstart(filename
, "/dev/cdrom", NULL
)) {
903 kern_return_t kernResult
;
904 io_iterator_t mediaIterator
;
905 char bsdPath
[ MAXPATHLEN
];
908 kernResult
= FindEjectableCDMedia( &mediaIterator
);
909 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
911 if ( bsdPath
[ 0 ] != '\0' ) {
912 strcat(bsdPath
,"s0");
913 /* some CDs don't have a partition 0 */
914 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
916 bsdPath
[strlen(bsdPath
)-1] = '1';
924 IOObjectRelease( mediaIterator
);
928 s
->type
= FTYPE_FILE
;
929 #if defined(__linux__)
931 char resolved_path
[ MAXPATHLEN
], *temp
;
933 temp
= realpath(filename
, resolved_path
);
934 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
940 return raw_open_common(bs
, filename
, flags
, 0);
943 #if defined(__linux__)
944 /* Note: we do not have a reliable method to detect if the floppy is
945 present. The current method is to try to open the floppy at every
946 I/O and to keep it opened during a few hundreds of ms. */
947 static int fd_open(BlockDriverState
*bs
)
949 BDRVRawState
*s
= bs
->opaque
;
950 int last_media_present
;
952 if (s
->type
!= FTYPE_FD
)
954 last_media_present
= (s
->fd
>= 0);
956 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
960 printf("Floppy closed\n");
964 if (s
->fd_got_error
&&
965 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
967 printf("No floppy (open delayed)\n");
971 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
973 s
->fd_error_time
= get_clock();
975 if (last_media_present
)
976 s
->fd_media_changed
= 1;
978 printf("No floppy\n");
983 printf("Floppy opened\n");
986 if (!last_media_present
)
987 s
->fd_media_changed
= 1;
988 s
->fd_open_time
= get_clock();
993 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
995 BDRVRawState
*s
= bs
->opaque
;
997 return ioctl(s
->fd
, req
, buf
);
1000 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1001 unsigned long int req
, void *buf
,
1002 BlockDriverCompletionFunc
*cb
, void *opaque
)
1004 BDRVRawState
*s
= bs
->opaque
;
1006 if (fd_open(bs
) < 0)
1008 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
1011 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1012 static int fd_open(BlockDriverState
*bs
)
1014 BDRVRawState
*s
= bs
->opaque
;
1016 /* this is just to ensure s->fd is sane (its called by io ops) */
1021 #else /* !linux && !FreeBSD */
1023 static int fd_open(BlockDriverState
*bs
)
1028 #endif /* !linux && !FreeBSD */
1030 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1034 struct stat stat_buf
;
1035 int64_t total_size
= 0;
1037 /* Read out options */
1038 while (options
&& options
->name
) {
1039 if (!strcmp(options
->name
, "size")) {
1040 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1045 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1049 if (fstat(fd
, &stat_buf
) < 0)
1051 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1053 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1060 static int hdev_has_zero_init(BlockDriverState
*bs
)
1065 static BlockDriver bdrv_host_device
= {
1066 .format_name
= "host_device",
1067 .protocol_name
= "host_device",
1068 .instance_size
= sizeof(BDRVRawState
),
1069 .bdrv_probe_device
= hdev_probe_device
,
1070 .bdrv_file_open
= hdev_open
,
1071 .bdrv_close
= raw_close
,
1072 .bdrv_create
= hdev_create
,
1073 .create_options
= raw_create_options
,
1074 .bdrv_has_zero_init
= hdev_has_zero_init
,
1075 .bdrv_flush
= raw_flush
,
1077 .bdrv_aio_readv
= raw_aio_readv
,
1078 .bdrv_aio_writev
= raw_aio_writev
,
1079 .bdrv_aio_flush
= raw_aio_flush
,
1081 .bdrv_read
= raw_read
,
1082 .bdrv_write
= raw_write
,
1083 .bdrv_getlength
= raw_getlength
,
1085 /* generic scsi device */
1087 .bdrv_ioctl
= hdev_ioctl
,
1088 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1093 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1095 BDRVRawState
*s
= bs
->opaque
;
1100 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1101 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1105 /* close fd so that we can reopen it as needed */
1108 s
->fd_media_changed
= 1;
1113 static int floppy_probe_device(const char *filename
)
1117 struct floppy_struct fdparam
;
1119 if (strstart(filename
, "/dev/fd", NULL
))
1122 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1127 /* Attempt to detect via a floppy specific ioctl */
1128 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1138 static int floppy_is_inserted(BlockDriverState
*bs
)
1140 return fd_open(bs
) >= 0;
1143 static int floppy_media_changed(BlockDriverState
*bs
)
1145 BDRVRawState
*s
= bs
->opaque
;
1149 * XXX: we do not have a true media changed indication.
1150 * It does not work if the floppy is changed without trying to read it.
1153 ret
= s
->fd_media_changed
;
1154 s
->fd_media_changed
= 0;
1156 printf("Floppy changed=%d\n", ret
);
1161 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1163 BDRVRawState
*s
= bs
->opaque
;
1170 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1172 if (ioctl(fd
, FDEJECT
, 0) < 0)
1180 static BlockDriver bdrv_host_floppy
= {
1181 .format_name
= "host_floppy",
1182 .protocol_name
= "host_floppy",
1183 .instance_size
= sizeof(BDRVRawState
),
1184 .bdrv_probe_device
= floppy_probe_device
,
1185 .bdrv_file_open
= floppy_open
,
1186 .bdrv_close
= raw_close
,
1187 .bdrv_create
= hdev_create
,
1188 .create_options
= raw_create_options
,
1189 .bdrv_has_zero_init
= hdev_has_zero_init
,
1190 .bdrv_flush
= raw_flush
,
1192 .bdrv_aio_readv
= raw_aio_readv
,
1193 .bdrv_aio_writev
= raw_aio_writev
,
1194 .bdrv_aio_flush
= raw_aio_flush
,
1196 .bdrv_read
= raw_read
,
1197 .bdrv_write
= raw_write
,
1198 .bdrv_getlength
= raw_getlength
,
1200 /* removable device support */
1201 .bdrv_is_inserted
= floppy_is_inserted
,
1202 .bdrv_media_changed
= floppy_media_changed
,
1203 .bdrv_eject
= floppy_eject
,
1206 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1208 BDRVRawState
*s
= bs
->opaque
;
1212 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1213 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1216 static int cdrom_probe_device(const char *filename
)
1221 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1226 /* Attempt to detect via a CDROM specific ioctl */
1227 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1236 static int cdrom_is_inserted(BlockDriverState
*bs
)
1238 BDRVRawState
*s
= bs
->opaque
;
1241 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1242 if (ret
== CDS_DISC_OK
)
1247 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1249 BDRVRawState
*s
= bs
->opaque
;
1252 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1253 perror("CDROMEJECT");
1255 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1256 perror("CDROMEJECT");
1262 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1264 BDRVRawState
*s
= bs
->opaque
;
1266 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1268 * Note: an error can happen if the distribution automatically
1271 /* perror("CDROM_LOCKDOOR"); */
1277 static BlockDriver bdrv_host_cdrom
= {
1278 .format_name
= "host_cdrom",
1279 .protocol_name
= "host_cdrom",
1280 .instance_size
= sizeof(BDRVRawState
),
1281 .bdrv_probe_device
= cdrom_probe_device
,
1282 .bdrv_file_open
= cdrom_open
,
1283 .bdrv_close
= raw_close
,
1284 .bdrv_create
= hdev_create
,
1285 .create_options
= raw_create_options
,
1286 .bdrv_has_zero_init
= hdev_has_zero_init
,
1287 .bdrv_flush
= raw_flush
,
1289 .bdrv_aio_readv
= raw_aio_readv
,
1290 .bdrv_aio_writev
= raw_aio_writev
,
1291 .bdrv_aio_flush
= raw_aio_flush
,
1293 .bdrv_read
= raw_read
,
1294 .bdrv_write
= raw_write
,
1295 .bdrv_getlength
= raw_getlength
,
1297 /* removable device support */
1298 .bdrv_is_inserted
= cdrom_is_inserted
,
1299 .bdrv_eject
= cdrom_eject
,
1300 .bdrv_set_locked
= cdrom_set_locked
,
1302 /* generic scsi device */
1303 .bdrv_ioctl
= hdev_ioctl
,
1304 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1306 #endif /* __linux__ */
1308 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1309 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1311 BDRVRawState
*s
= bs
->opaque
;
1316 ret
= raw_open_common(bs
, filename
, flags
, 0);
1320 /* make sure the door isnt locked at this time */
1321 ioctl(s
->fd
, CDIOCALLOW
);
1325 static int cdrom_probe_device(const char *filename
)
1327 if (strstart(filename
, "/dev/cd", NULL
) ||
1328 strstart(filename
, "/dev/acd", NULL
))
1333 static int cdrom_reopen(BlockDriverState
*bs
)
1335 BDRVRawState
*s
= bs
->opaque
;
1339 * Force reread of possibly changed/newly loaded disc,
1340 * FreeBSD seems to not notice sometimes...
1344 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1351 /* make sure the door isnt locked at this time */
1352 ioctl(s
->fd
, CDIOCALLOW
);
1356 static int cdrom_is_inserted(BlockDriverState
*bs
)
1358 return raw_getlength(bs
) > 0;
1361 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1363 BDRVRawState
*s
= bs
->opaque
;
1368 (void) ioctl(s
->fd
, CDIOCALLOW
);
1371 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1372 perror("CDIOCEJECT");
1374 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1375 perror("CDIOCCLOSE");
1378 if (cdrom_reopen(bs
) < 0)
1383 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1385 BDRVRawState
*s
= bs
->opaque
;
1389 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1391 * Note: an error can happen if the distribution automatically
1394 /* perror("CDROM_LOCKDOOR"); */
1400 static BlockDriver bdrv_host_cdrom
= {
1401 .format_name
= "host_cdrom",
1402 .protocol_name
= "host_cdrom",
1403 .instance_size
= sizeof(BDRVRawState
),
1404 .bdrv_probe_device
= cdrom_probe_device
,
1405 .bdrv_file_open
= cdrom_open
,
1406 .bdrv_close
= raw_close
,
1407 .bdrv_create
= hdev_create
,
1408 .create_options
= raw_create_options
,
1409 .bdrv_has_zero_init
= hdev_has_zero_init
,
1410 .bdrv_flush
= raw_flush
,
1412 .bdrv_aio_readv
= raw_aio_readv
,
1413 .bdrv_aio_writev
= raw_aio_writev
,
1414 .bdrv_aio_flush
= raw_aio_flush
,
1416 .bdrv_read
= raw_read
,
1417 .bdrv_write
= raw_write
,
1418 .bdrv_getlength
= raw_getlength
,
1420 /* removable device support */
1421 .bdrv_is_inserted
= cdrom_is_inserted
,
1422 .bdrv_eject
= cdrom_eject
,
1423 .bdrv_set_locked
= cdrom_set_locked
,
1425 #endif /* __FreeBSD__ */
1427 static void bdrv_file_init(void)
1430 * Register all the drivers. Note that order is important, the driver
1431 * registered last will get probed first.
1433 bdrv_register(&bdrv_file
);
1434 bdrv_register(&bdrv_host_device
);
1436 bdrv_register(&bdrv_host_floppy
);
1437 bdrv_register(&bdrv_host_cdrom
);
1439 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1440 bdrv_register(&bdrv_host_cdrom
);
1444 block_init(bdrv_file_init
);