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"
32 #if defined(__APPLE__) && (__MACH__)
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>
58 #include <linux/fiemap.h>
60 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
66 #include <sys/ioctl.h>
67 #include <sys/disklabel.h>
72 #include <sys/ioctl.h>
73 #include <sys/disklabel.h>
79 #include <sys/ioctl.h>
80 #include <sys/diskslice.h>
87 //#define DEBUG_FLOPPY
90 #if defined(DEBUG_BLOCK)
91 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
92 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
94 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
97 /* OS X does not have O_DSYNC */
100 #define O_DSYNC O_SYNC
101 #elif defined(O_FSYNC)
102 #define O_DSYNC O_FSYNC
106 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
108 #define O_DIRECT O_DSYNC
115 /* if the FD is not accessed during that time (in ns), we try to
116 reopen it to see if the disk has been changed */
117 #define FD_OPEN_TIMEOUT (1000000000)
119 #define MAX_BLOCKSIZE 4096
121 typedef struct BDRVRawState
{
125 #if defined(__linux__)
126 /* linux floppy specific */
127 int64_t fd_open_time
;
128 int64_t fd_error_time
;
130 int fd_media_changed
;
132 #ifdef CONFIG_LINUX_AIO
141 typedef struct BDRVRawReopenState
{
144 #ifdef CONFIG_LINUX_AIO
147 } BDRVRawReopenState
;
149 static int fd_open(BlockDriverState
*bs
);
150 static int64_t raw_getlength(BlockDriverState
*bs
);
152 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
153 static int cdrom_reopen(BlockDriverState
*bs
);
156 #if defined(__NetBSD__)
157 static int raw_normalize_devicepath(const char **filename
)
159 static char namebuf
[PATH_MAX
];
160 const char *dp
, *fname
;
164 dp
= strrchr(fname
, '/');
165 if (lstat(fname
, &sb
) < 0) {
166 fprintf(stderr
, "%s: stat failed: %s\n",
167 fname
, strerror(errno
));
171 if (!S_ISBLK(sb
.st_mode
)) {
176 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
178 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
179 (int)(dp
- fname
), fname
, dp
+ 1);
181 fprintf(stderr
, "%s is a block device", fname
);
183 fprintf(stderr
, ", using %s\n", *filename
);
188 static int raw_normalize_devicepath(const char **filename
)
194 static void raw_parse_flags(int bdrv_flags
, int *open_flags
)
196 assert(open_flags
!= NULL
);
198 *open_flags
|= O_BINARY
;
199 *open_flags
&= ~O_ACCMODE
;
200 if (bdrv_flags
& BDRV_O_RDWR
) {
201 *open_flags
|= O_RDWR
;
203 *open_flags
|= O_RDONLY
;
206 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
207 * and O_DIRECT for no caching. */
208 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
209 *open_flags
|= O_DIRECT
;
213 #ifdef CONFIG_LINUX_AIO
214 static int raw_set_aio(void **aio_ctx
, int *use_aio
, int bdrv_flags
)
217 assert(aio_ctx
!= NULL
);
218 assert(use_aio
!= NULL
);
220 * Currently Linux do AIO only for files opened with O_DIRECT
221 * specified so check NOCACHE flag too
223 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
224 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
226 /* if non-NULL, laio_init() has already been run */
227 if (*aio_ctx
== NULL
) {
228 *aio_ctx
= laio_init();
245 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
246 int bdrv_flags
, int open_flags
)
248 BDRVRawState
*s
= bs
->opaque
;
251 ret
= raw_normalize_devicepath(&filename
);
256 s
->open_flags
= open_flags
;
257 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
260 fd
= qemu_open(filename
, s
->open_flags
, 0644);
269 /* We're falling back to POSIX AIO in some cases so init always */
270 if (paio_init() < 0) {
274 #ifdef CONFIG_LINUX_AIO
275 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
281 if (platform_test_xfs_fd(s
->fd
)) {
293 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
295 BDRVRawState
*s
= bs
->opaque
;
297 s
->type
= FTYPE_FILE
;
298 return raw_open_common(bs
, filename
, flags
, 0);
301 static int raw_reopen_prepare(BDRVReopenState
*state
,
302 BlockReopenQueue
*queue
, Error
**errp
)
305 BDRVRawReopenState
*raw_s
;
308 assert(state
!= NULL
);
309 assert(state
->bs
!= NULL
);
311 s
= state
->bs
->opaque
;
313 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
314 raw_s
= state
->opaque
;
316 #ifdef CONFIG_LINUX_AIO
317 raw_s
->use_aio
= s
->use_aio
;
319 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
320 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
321 * won't override aio_ctx if aio_ctx is non-NULL */
322 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
327 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
331 int fcntl_flags
= O_APPEND
| O_ASYNC
| O_NONBLOCK
;
333 fcntl_flags
|= O_NOATIME
;
336 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
337 /* dup the original fd */
338 /* TODO: use qemu fcntl wrapper */
339 #ifdef F_DUPFD_CLOEXEC
340 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
342 raw_s
->fd
= dup(s
->fd
);
343 if (raw_s
->fd
!= -1) {
344 qemu_set_cloexec(raw_s
->fd
);
347 if (raw_s
->fd
>= 0) {
348 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
350 qemu_close(raw_s
->fd
);
356 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
357 if (raw_s
->fd
== -1) {
358 assert(!(raw_s
->open_flags
& O_CREAT
));
359 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
360 if (raw_s
->fd
== -1) {
368 static void raw_reopen_commit(BDRVReopenState
*state
)
370 BDRVRawReopenState
*raw_s
= state
->opaque
;
371 BDRVRawState
*s
= state
->bs
->opaque
;
373 s
->open_flags
= raw_s
->open_flags
;
377 #ifdef CONFIG_LINUX_AIO
378 s
->use_aio
= raw_s
->use_aio
;
381 g_free(state
->opaque
);
382 state
->opaque
= NULL
;
386 static void raw_reopen_abort(BDRVReopenState
*state
)
388 BDRVRawReopenState
*raw_s
= state
->opaque
;
390 /* nothing to do if NULL, we didn't get far enough */
395 if (raw_s
->fd
>= 0) {
396 qemu_close(raw_s
->fd
);
399 g_free(state
->opaque
);
400 state
->opaque
= NULL
;
404 /* XXX: use host sector size if necessary with:
405 #ifdef DIOCGSECTORSIZE
407 unsigned int sectorsize = 512;
408 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
409 sectorsize > bufsize)
410 bufsize = sectorsize;
414 uint32_t blockSize = 512;
415 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
422 * Check if all memory in this vector is sector aligned.
424 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
428 for (i
= 0; i
< qiov
->niov
; i
++) {
429 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
437 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
438 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
439 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
441 BDRVRawState
*s
= bs
->opaque
;
447 * If O_DIRECT is used the buffer needs to be aligned on a sector
448 * boundary. Check if this is the case or tell the low-level
449 * driver that it needs to copy the buffer.
451 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
452 if (!qiov_is_aligned(bs
, qiov
)) {
453 type
|= QEMU_AIO_MISALIGNED
;
454 #ifdef CONFIG_LINUX_AIO
455 } else if (s
->use_aio
) {
456 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
457 nb_sectors
, cb
, opaque
, type
);
462 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
466 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
467 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
468 BlockDriverCompletionFunc
*cb
, void *opaque
)
470 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
471 cb
, opaque
, QEMU_AIO_READ
);
474 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
475 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
476 BlockDriverCompletionFunc
*cb
, void *opaque
)
478 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
479 cb
, opaque
, QEMU_AIO_WRITE
);
482 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
483 BlockDriverCompletionFunc
*cb
, void *opaque
)
485 BDRVRawState
*s
= bs
->opaque
;
490 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
493 static void raw_close(BlockDriverState
*bs
)
495 BDRVRawState
*s
= bs
->opaque
;
502 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
504 BDRVRawState
*s
= bs
->opaque
;
507 if (fstat(s
->fd
, &st
)) {
511 if (S_ISREG(st
.st_mode
)) {
512 if (ftruncate(s
->fd
, offset
) < 0) {
515 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
516 if (offset
> raw_getlength(bs
)) {
527 static int64_t raw_getlength(BlockDriverState
*bs
)
529 BDRVRawState
*s
= bs
->opaque
;
535 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
538 if (ioctl(fd
, DIOCGDINFO
, &dl
))
540 return (uint64_t)dl
.d_secsize
*
541 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
545 #elif defined(__NetBSD__)
546 static int64_t raw_getlength(BlockDriverState
*bs
)
548 BDRVRawState
*s
= bs
->opaque
;
554 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
555 struct dkwedge_info dkw
;
557 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
558 return dkw
.dkw_size
* 512;
562 if (ioctl(fd
, DIOCGDINFO
, &dl
))
564 return (uint64_t)dl
.d_secsize
*
565 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
570 #elif defined(__sun__)
571 static int64_t raw_getlength(BlockDriverState
*bs
)
573 BDRVRawState
*s
= bs
->opaque
;
574 struct dk_minfo minfo
;
583 * Use the DKIOCGMEDIAINFO ioctl to read the size.
585 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
587 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
591 * There are reports that lseek on some devices fails, but
592 * irc discussion said that contingency on contingency was overkill.
594 return lseek(s
->fd
, 0, SEEK_END
);
596 #elif defined(CONFIG_BSD)
597 static int64_t raw_getlength(BlockDriverState
*bs
)
599 BDRVRawState
*s
= bs
->opaque
;
603 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
612 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
615 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
616 #ifdef DIOCGMEDIASIZE
617 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
618 #elif defined(DIOCGPART)
621 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
622 size
= pi
.media_size
;
628 #if defined(__APPLE__) && defined(__MACH__)
629 size
= LONG_LONG_MAX
;
631 size
= lseek(fd
, 0LL, SEEK_END
);
633 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
636 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
637 if (size
== 2048LL * (unsigned)-1)
639 /* XXX no disc? maybe we need to reopen... */
640 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
647 size
= lseek(fd
, 0, SEEK_END
);
652 static int64_t raw_getlength(BlockDriverState
*bs
)
654 BDRVRawState
*s
= bs
->opaque
;
662 return lseek(s
->fd
, 0, SEEK_END
);
666 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
669 BDRVRawState
*s
= bs
->opaque
;
671 if (fstat(s
->fd
, &st
) < 0) {
674 return (int64_t)st
.st_blocks
* 512;
677 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
681 int64_t total_size
= 0;
683 /* Read out options */
684 while (options
&& options
->name
) {
685 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
686 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
691 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
696 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
699 if (qemu_close(fd
) != 0) {
707 * Returns true iff the specified sector is present in the disk image. Drivers
708 * not implementing the functionality are assumed to not support backing files,
709 * hence all their sectors are reported as allocated.
711 * If 'sector_num' is beyond the end of the disk image the return value is 0
712 * and 'pnum' is set to 0.
714 * 'pnum' is set to the number of sectors (including and immediately following
715 * the specified sector) that are known to be in the same
716 * allocated/unallocated state.
718 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
719 * beyond the end of the disk image it will be clamped.
721 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
723 int nb_sectors
, int *pnum
)
725 off_t start
, data
, hole
;
733 start
= sector_num
* BDRV_SECTOR_SIZE
;
737 BDRVRawState
*s
= bs
->opaque
;
740 struct fiemap_extent fe
;
743 f
.fm
.fm_start
= start
;
744 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
746 f
.fm
.fm_extent_count
= 1;
747 f
.fm
.fm_reserved
= 0;
748 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
749 /* Assume everything is allocated. */
754 if (f
.fm
.fm_mapped_extents
== 0) {
755 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
756 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
758 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
759 hole
= f
.fm
.fm_start
;
760 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
762 data
= f
.fe
.fe_logical
;
763 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
766 #elif defined SEEK_HOLE && defined SEEK_DATA
768 BDRVRawState
*s
= bs
->opaque
;
770 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
772 /* -ENXIO indicates that sector_num was past the end of the file.
773 * There is a virtual hole there. */
774 assert(errno
!= -ENXIO
);
776 /* Most likely EINVAL. Assume everything is allocated. */
784 /* On a hole. We need another syscall to find its end. */
785 data
= lseek(s
->fd
, start
, SEEK_DATA
);
787 data
= lseek(s
->fd
, 0, SEEK_END
);
796 /* On a data extent, compute sectors to the end of the extent. */
797 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
800 /* On a hole, compute sectors to the beginning of the next extent. */
801 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
807 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
809 struct xfs_flock64 fl
;
811 memset(&fl
, 0, sizeof(fl
));
812 fl
.l_whence
= SEEK_SET
;
813 fl
.l_start
= sector_num
<< 9;
814 fl
.l_len
= (int64_t)nb_sectors
<< 9;
816 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
817 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
825 static coroutine_fn
int raw_co_discard(BlockDriverState
*bs
,
826 int64_t sector_num
, int nb_sectors
)
829 BDRVRawState
*s
= bs
->opaque
;
832 return xfs_discard(s
, sector_num
, nb_sectors
);
839 static QEMUOptionParameter raw_create_options
[] = {
841 .name
= BLOCK_OPT_SIZE
,
843 .help
= "Virtual disk size"
848 static BlockDriver bdrv_file
= {
849 .format_name
= "file",
850 .protocol_name
= "file",
851 .instance_size
= sizeof(BDRVRawState
),
852 .bdrv_probe
= NULL
, /* no probe for protocols */
853 .bdrv_file_open
= raw_open
,
854 .bdrv_reopen_prepare
= raw_reopen_prepare
,
855 .bdrv_reopen_commit
= raw_reopen_commit
,
856 .bdrv_reopen_abort
= raw_reopen_abort
,
857 .bdrv_close
= raw_close
,
858 .bdrv_create
= raw_create
,
859 .bdrv_co_discard
= raw_co_discard
,
860 .bdrv_co_is_allocated
= raw_co_is_allocated
,
862 .bdrv_aio_readv
= raw_aio_readv
,
863 .bdrv_aio_writev
= raw_aio_writev
,
864 .bdrv_aio_flush
= raw_aio_flush
,
866 .bdrv_truncate
= raw_truncate
,
867 .bdrv_getlength
= raw_getlength
,
868 .bdrv_get_allocated_file_size
869 = raw_get_allocated_file_size
,
871 .create_options
= raw_create_options
,
874 /***********************************************/
877 #if defined(__APPLE__) && defined(__MACH__)
878 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
879 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
881 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
883 kern_return_t kernResult
;
884 mach_port_t masterPort
;
885 CFMutableDictionaryRef classesToMatch
;
887 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
888 if ( KERN_SUCCESS
!= kernResult
) {
889 printf( "IOMasterPort returned %d\n", kernResult
);
892 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
893 if ( classesToMatch
== NULL
) {
894 printf( "IOServiceMatching returned a NULL dictionary.\n" );
896 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
898 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
899 if ( KERN_SUCCESS
!= kernResult
)
901 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
907 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
909 io_object_t nextMedia
;
910 kern_return_t kernResult
= KERN_FAILURE
;
912 nextMedia
= IOIteratorNext( mediaIterator
);
915 CFTypeRef bsdPathAsCFString
;
916 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
917 if ( bsdPathAsCFString
) {
918 size_t devPathLength
;
919 strcpy( bsdPath
, _PATH_DEV
);
920 strcat( bsdPath
, "r" );
921 devPathLength
= strlen( bsdPath
);
922 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
923 kernResult
= KERN_SUCCESS
;
925 CFRelease( bsdPathAsCFString
);
927 IOObjectRelease( nextMedia
);
935 static int hdev_probe_device(const char *filename
)
939 /* allow a dedicated CD-ROM driver to match with a higher priority */
940 if (strstart(filename
, "/dev/cdrom", NULL
))
943 if (stat(filename
, &st
) >= 0 &&
944 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
951 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
953 BDRVRawState
*s
= bs
->opaque
;
955 #if defined(__APPLE__) && defined(__MACH__)
956 if (strstart(filename
, "/dev/cdrom", NULL
)) {
957 kern_return_t kernResult
;
958 io_iterator_t mediaIterator
;
959 char bsdPath
[ MAXPATHLEN
];
962 kernResult
= FindEjectableCDMedia( &mediaIterator
);
963 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
965 if ( bsdPath
[ 0 ] != '\0' ) {
966 strcat(bsdPath
,"s0");
967 /* some CDs don't have a partition 0 */
968 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
970 bsdPath
[strlen(bsdPath
)-1] = '1';
978 IOObjectRelease( mediaIterator
);
982 s
->type
= FTYPE_FILE
;
983 #if defined(__linux__)
985 char resolved_path
[ MAXPATHLEN
], *temp
;
987 temp
= realpath(filename
, resolved_path
);
988 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
994 return raw_open_common(bs
, filename
, flags
, 0);
997 #if defined(__linux__)
998 /* Note: we do not have a reliable method to detect if the floppy is
999 present. The current method is to try to open the floppy at every
1000 I/O and to keep it opened during a few hundreds of ms. */
1001 static int fd_open(BlockDriverState
*bs
)
1003 BDRVRawState
*s
= bs
->opaque
;
1004 int last_media_present
;
1006 if (s
->type
!= FTYPE_FD
)
1008 last_media_present
= (s
->fd
>= 0);
1010 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1014 printf("Floppy closed\n");
1018 if (s
->fd_got_error
&&
1019 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1021 printf("No floppy (open delayed)\n");
1025 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1027 s
->fd_error_time
= get_clock();
1028 s
->fd_got_error
= 1;
1029 if (last_media_present
)
1030 s
->fd_media_changed
= 1;
1032 printf("No floppy\n");
1037 printf("Floppy opened\n");
1040 if (!last_media_present
)
1041 s
->fd_media_changed
= 1;
1042 s
->fd_open_time
= get_clock();
1043 s
->fd_got_error
= 0;
1047 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1049 BDRVRawState
*s
= bs
->opaque
;
1051 return ioctl(s
->fd
, req
, buf
);
1054 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1055 unsigned long int req
, void *buf
,
1056 BlockDriverCompletionFunc
*cb
, void *opaque
)
1058 BDRVRawState
*s
= bs
->opaque
;
1060 if (fd_open(bs
) < 0)
1062 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
1065 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1066 static int fd_open(BlockDriverState
*bs
)
1068 BDRVRawState
*s
= bs
->opaque
;
1070 /* this is just to ensure s->fd is sane (its called by io ops) */
1075 #else /* !linux && !FreeBSD */
1077 static int fd_open(BlockDriverState
*bs
)
1082 #endif /* !linux && !FreeBSD */
1084 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1088 struct stat stat_buf
;
1089 int64_t total_size
= 0;
1091 /* Read out options */
1092 while (options
&& options
->name
) {
1093 if (!strcmp(options
->name
, "size")) {
1094 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1099 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1103 if (fstat(fd
, &stat_buf
) < 0)
1105 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1107 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1114 static int hdev_has_zero_init(BlockDriverState
*bs
)
1119 static BlockDriver bdrv_host_device
= {
1120 .format_name
= "host_device",
1121 .protocol_name
= "host_device",
1122 .instance_size
= sizeof(BDRVRawState
),
1123 .bdrv_probe_device
= hdev_probe_device
,
1124 .bdrv_file_open
= hdev_open
,
1125 .bdrv_close
= raw_close
,
1126 .bdrv_create
= hdev_create
,
1127 .create_options
= raw_create_options
,
1128 .bdrv_has_zero_init
= hdev_has_zero_init
,
1130 .bdrv_aio_readv
= raw_aio_readv
,
1131 .bdrv_aio_writev
= raw_aio_writev
,
1132 .bdrv_aio_flush
= raw_aio_flush
,
1134 .bdrv_truncate
= raw_truncate
,
1135 .bdrv_getlength
= raw_getlength
,
1136 .bdrv_get_allocated_file_size
1137 = raw_get_allocated_file_size
,
1139 /* generic scsi device */
1141 .bdrv_ioctl
= hdev_ioctl
,
1142 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1147 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1149 BDRVRawState
*s
= bs
->opaque
;
1154 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1155 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1159 /* close fd so that we can reopen it as needed */
1162 s
->fd_media_changed
= 1;
1167 static int floppy_probe_device(const char *filename
)
1171 struct floppy_struct fdparam
;
1174 if (strstart(filename
, "/dev/fd", NULL
) &&
1175 !strstart(filename
, "/dev/fdset/", NULL
)) {
1179 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1183 ret
= fstat(fd
, &st
);
1184 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1188 /* Attempt to detect via a floppy specific ioctl */
1189 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1200 static int floppy_is_inserted(BlockDriverState
*bs
)
1202 return fd_open(bs
) >= 0;
1205 static int floppy_media_changed(BlockDriverState
*bs
)
1207 BDRVRawState
*s
= bs
->opaque
;
1211 * XXX: we do not have a true media changed indication.
1212 * It does not work if the floppy is changed without trying to read it.
1215 ret
= s
->fd_media_changed
;
1216 s
->fd_media_changed
= 0;
1218 printf("Floppy changed=%d\n", ret
);
1223 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1225 BDRVRawState
*s
= bs
->opaque
;
1232 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1234 if (ioctl(fd
, FDEJECT
, 0) < 0)
1240 static BlockDriver bdrv_host_floppy
= {
1241 .format_name
= "host_floppy",
1242 .protocol_name
= "host_floppy",
1243 .instance_size
= sizeof(BDRVRawState
),
1244 .bdrv_probe_device
= floppy_probe_device
,
1245 .bdrv_file_open
= floppy_open
,
1246 .bdrv_close
= raw_close
,
1247 .bdrv_create
= hdev_create
,
1248 .create_options
= raw_create_options
,
1249 .bdrv_has_zero_init
= hdev_has_zero_init
,
1251 .bdrv_aio_readv
= raw_aio_readv
,
1252 .bdrv_aio_writev
= raw_aio_writev
,
1253 .bdrv_aio_flush
= raw_aio_flush
,
1255 .bdrv_truncate
= raw_truncate
,
1256 .bdrv_getlength
= raw_getlength
,
1257 .bdrv_get_allocated_file_size
1258 = raw_get_allocated_file_size
,
1260 /* removable device support */
1261 .bdrv_is_inserted
= floppy_is_inserted
,
1262 .bdrv_media_changed
= floppy_media_changed
,
1263 .bdrv_eject
= floppy_eject
,
1266 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1268 BDRVRawState
*s
= bs
->opaque
;
1272 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1273 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1276 static int cdrom_probe_device(const char *filename
)
1282 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1286 ret
= fstat(fd
, &st
);
1287 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1291 /* Attempt to detect via a CDROM specific ioctl */
1292 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1302 static int cdrom_is_inserted(BlockDriverState
*bs
)
1304 BDRVRawState
*s
= bs
->opaque
;
1307 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1308 if (ret
== CDS_DISC_OK
)
1313 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1315 BDRVRawState
*s
= bs
->opaque
;
1318 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1319 perror("CDROMEJECT");
1321 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1322 perror("CDROMEJECT");
1326 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1328 BDRVRawState
*s
= bs
->opaque
;
1330 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1332 * Note: an error can happen if the distribution automatically
1335 /* perror("CDROM_LOCKDOOR"); */
1339 static BlockDriver bdrv_host_cdrom
= {
1340 .format_name
= "host_cdrom",
1341 .protocol_name
= "host_cdrom",
1342 .instance_size
= sizeof(BDRVRawState
),
1343 .bdrv_probe_device
= cdrom_probe_device
,
1344 .bdrv_file_open
= cdrom_open
,
1345 .bdrv_close
= raw_close
,
1346 .bdrv_create
= hdev_create
,
1347 .create_options
= raw_create_options
,
1348 .bdrv_has_zero_init
= hdev_has_zero_init
,
1350 .bdrv_aio_readv
= raw_aio_readv
,
1351 .bdrv_aio_writev
= raw_aio_writev
,
1352 .bdrv_aio_flush
= raw_aio_flush
,
1354 .bdrv_truncate
= raw_truncate
,
1355 .bdrv_getlength
= raw_getlength
,
1356 .bdrv_get_allocated_file_size
1357 = raw_get_allocated_file_size
,
1359 /* removable device support */
1360 .bdrv_is_inserted
= cdrom_is_inserted
,
1361 .bdrv_eject
= cdrom_eject
,
1362 .bdrv_lock_medium
= cdrom_lock_medium
,
1364 /* generic scsi device */
1365 .bdrv_ioctl
= hdev_ioctl
,
1366 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1368 #endif /* __linux__ */
1370 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1371 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1373 BDRVRawState
*s
= bs
->opaque
;
1378 ret
= raw_open_common(bs
, filename
, flags
, 0);
1382 /* make sure the door isn't locked at this time */
1383 ioctl(s
->fd
, CDIOCALLOW
);
1387 static int cdrom_probe_device(const char *filename
)
1389 if (strstart(filename
, "/dev/cd", NULL
) ||
1390 strstart(filename
, "/dev/acd", NULL
))
1395 static int cdrom_reopen(BlockDriverState
*bs
)
1397 BDRVRawState
*s
= bs
->opaque
;
1401 * Force reread of possibly changed/newly loaded disc,
1402 * FreeBSD seems to not notice sometimes...
1406 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1413 /* make sure the door isn't locked at this time */
1414 ioctl(s
->fd
, CDIOCALLOW
);
1418 static int cdrom_is_inserted(BlockDriverState
*bs
)
1420 return raw_getlength(bs
) > 0;
1423 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1425 BDRVRawState
*s
= bs
->opaque
;
1430 (void) ioctl(s
->fd
, CDIOCALLOW
);
1433 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1434 perror("CDIOCEJECT");
1436 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1437 perror("CDIOCCLOSE");
1443 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1445 BDRVRawState
*s
= bs
->opaque
;
1449 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1451 * Note: an error can happen if the distribution automatically
1454 /* perror("CDROM_LOCKDOOR"); */
1458 static BlockDriver bdrv_host_cdrom
= {
1459 .format_name
= "host_cdrom",
1460 .protocol_name
= "host_cdrom",
1461 .instance_size
= sizeof(BDRVRawState
),
1462 .bdrv_probe_device
= cdrom_probe_device
,
1463 .bdrv_file_open
= cdrom_open
,
1464 .bdrv_close
= raw_close
,
1465 .bdrv_create
= hdev_create
,
1466 .create_options
= raw_create_options
,
1467 .bdrv_has_zero_init
= hdev_has_zero_init
,
1469 .bdrv_aio_readv
= raw_aio_readv
,
1470 .bdrv_aio_writev
= raw_aio_writev
,
1471 .bdrv_aio_flush
= raw_aio_flush
,
1473 .bdrv_truncate
= raw_truncate
,
1474 .bdrv_getlength
= raw_getlength
,
1475 .bdrv_get_allocated_file_size
1476 = raw_get_allocated_file_size
,
1478 /* removable device support */
1479 .bdrv_is_inserted
= cdrom_is_inserted
,
1480 .bdrv_eject
= cdrom_eject
,
1481 .bdrv_lock_medium
= cdrom_lock_medium
,
1483 #endif /* __FreeBSD__ */
1485 static void bdrv_file_init(void)
1488 * Register all the drivers. Note that order is important, the driver
1489 * registered last will get probed first.
1491 bdrv_register(&bdrv_file
);
1492 bdrv_register(&bdrv_host_device
);
1494 bdrv_register(&bdrv_host_floppy
);
1495 bdrv_register(&bdrv_host_cdrom
);
1497 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1498 bdrv_register(&bdrv_host_cdrom
);
1502 block_init(bdrv_file_init
);