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 #ifdef CONFIG_LINUX_AIO
270 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
277 if (platform_test_xfs_fd(s
->fd
)) {
285 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
287 BDRVRawState
*s
= bs
->opaque
;
289 s
->type
= FTYPE_FILE
;
290 return raw_open_common(bs
, filename
, flags
, 0);
293 static int raw_reopen_prepare(BDRVReopenState
*state
,
294 BlockReopenQueue
*queue
, Error
**errp
)
297 BDRVRawReopenState
*raw_s
;
300 assert(state
!= NULL
);
301 assert(state
->bs
!= NULL
);
303 s
= state
->bs
->opaque
;
305 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
306 raw_s
= state
->opaque
;
308 #ifdef CONFIG_LINUX_AIO
309 raw_s
->use_aio
= s
->use_aio
;
311 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
312 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
313 * won't override aio_ctx if aio_ctx is non-NULL */
314 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
319 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
323 int fcntl_flags
= O_APPEND
| O_ASYNC
| O_NONBLOCK
;
325 fcntl_flags
|= O_NOATIME
;
328 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
329 /* dup the original fd */
330 /* TODO: use qemu fcntl wrapper */
331 #ifdef F_DUPFD_CLOEXEC
332 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
334 raw_s
->fd
= dup(s
->fd
);
335 if (raw_s
->fd
!= -1) {
336 qemu_set_cloexec(raw_s
->fd
);
339 if (raw_s
->fd
>= 0) {
340 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
342 qemu_close(raw_s
->fd
);
348 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
349 if (raw_s
->fd
== -1) {
350 assert(!(raw_s
->open_flags
& O_CREAT
));
351 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
352 if (raw_s
->fd
== -1) {
360 static void raw_reopen_commit(BDRVReopenState
*state
)
362 BDRVRawReopenState
*raw_s
= state
->opaque
;
363 BDRVRawState
*s
= state
->bs
->opaque
;
365 s
->open_flags
= raw_s
->open_flags
;
369 #ifdef CONFIG_LINUX_AIO
370 s
->use_aio
= raw_s
->use_aio
;
373 g_free(state
->opaque
);
374 state
->opaque
= NULL
;
378 static void raw_reopen_abort(BDRVReopenState
*state
)
380 BDRVRawReopenState
*raw_s
= state
->opaque
;
382 /* nothing to do if NULL, we didn't get far enough */
387 if (raw_s
->fd
>= 0) {
388 qemu_close(raw_s
->fd
);
391 g_free(state
->opaque
);
392 state
->opaque
= NULL
;
396 /* XXX: use host sector size if necessary with:
397 #ifdef DIOCGSECTORSIZE
399 unsigned int sectorsize = 512;
400 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
401 sectorsize > bufsize)
402 bufsize = sectorsize;
406 uint32_t blockSize = 512;
407 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
414 * Check if all memory in this vector is sector aligned.
416 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
420 for (i
= 0; i
< qiov
->niov
; i
++) {
421 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
429 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
430 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
431 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
433 BDRVRawState
*s
= bs
->opaque
;
439 * If O_DIRECT is used the buffer needs to be aligned on a sector
440 * boundary. Check if this is the case or tell the low-level
441 * driver that it needs to copy the buffer.
443 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
444 if (!qiov_is_aligned(bs
, qiov
)) {
445 type
|= QEMU_AIO_MISALIGNED
;
446 #ifdef CONFIG_LINUX_AIO
447 } else if (s
->use_aio
) {
448 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
449 nb_sectors
, cb
, opaque
, type
);
454 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
458 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
459 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
460 BlockDriverCompletionFunc
*cb
, void *opaque
)
462 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
463 cb
, opaque
, QEMU_AIO_READ
);
466 static BlockDriverAIOCB
*raw_aio_writev(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_WRITE
);
474 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
475 BlockDriverCompletionFunc
*cb
, void *opaque
)
477 BDRVRawState
*s
= bs
->opaque
;
482 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
485 static void raw_close(BlockDriverState
*bs
)
487 BDRVRawState
*s
= bs
->opaque
;
494 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
496 BDRVRawState
*s
= bs
->opaque
;
499 if (fstat(s
->fd
, &st
)) {
503 if (S_ISREG(st
.st_mode
)) {
504 if (ftruncate(s
->fd
, offset
) < 0) {
507 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
508 if (offset
> raw_getlength(bs
)) {
519 static int64_t raw_getlength(BlockDriverState
*bs
)
521 BDRVRawState
*s
= bs
->opaque
;
527 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
530 if (ioctl(fd
, DIOCGDINFO
, &dl
))
532 return (uint64_t)dl
.d_secsize
*
533 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
537 #elif defined(__NetBSD__)
538 static int64_t raw_getlength(BlockDriverState
*bs
)
540 BDRVRawState
*s
= bs
->opaque
;
546 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
547 struct dkwedge_info dkw
;
549 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
550 return dkw
.dkw_size
* 512;
554 if (ioctl(fd
, DIOCGDINFO
, &dl
))
556 return (uint64_t)dl
.d_secsize
*
557 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
562 #elif defined(__sun__)
563 static int64_t raw_getlength(BlockDriverState
*bs
)
565 BDRVRawState
*s
= bs
->opaque
;
566 struct dk_minfo minfo
;
575 * Use the DKIOCGMEDIAINFO ioctl to read the size.
577 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
579 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
583 * There are reports that lseek on some devices fails, but
584 * irc discussion said that contingency on contingency was overkill.
586 return lseek(s
->fd
, 0, SEEK_END
);
588 #elif defined(CONFIG_BSD)
589 static int64_t raw_getlength(BlockDriverState
*bs
)
591 BDRVRawState
*s
= bs
->opaque
;
595 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
604 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
607 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
608 #ifdef DIOCGMEDIASIZE
609 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
610 #elif defined(DIOCGPART)
613 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
614 size
= pi
.media_size
;
620 #if defined(__APPLE__) && defined(__MACH__)
621 size
= LONG_LONG_MAX
;
623 size
= lseek(fd
, 0LL, SEEK_END
);
625 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
628 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
629 if (size
== 2048LL * (unsigned)-1)
631 /* XXX no disc? maybe we need to reopen... */
632 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
639 size
= lseek(fd
, 0, SEEK_END
);
644 static int64_t raw_getlength(BlockDriverState
*bs
)
646 BDRVRawState
*s
= bs
->opaque
;
654 return lseek(s
->fd
, 0, SEEK_END
);
658 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
661 BDRVRawState
*s
= bs
->opaque
;
663 if (fstat(s
->fd
, &st
) < 0) {
666 return (int64_t)st
.st_blocks
* 512;
669 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
673 int64_t total_size
= 0;
675 /* Read out options */
676 while (options
&& options
->name
) {
677 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
678 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
683 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
688 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
691 if (qemu_close(fd
) != 0) {
699 * Returns true iff the specified sector is present in the disk image. Drivers
700 * not implementing the functionality are assumed to not support backing files,
701 * hence all their sectors are reported as allocated.
703 * If 'sector_num' is beyond the end of the disk image the return value is 0
704 * and 'pnum' is set to 0.
706 * 'pnum' is set to the number of sectors (including and immediately following
707 * the specified sector) that are known to be in the same
708 * allocated/unallocated state.
710 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
711 * beyond the end of the disk image it will be clamped.
713 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
715 int nb_sectors
, int *pnum
)
717 off_t start
, data
, hole
;
725 start
= sector_num
* BDRV_SECTOR_SIZE
;
729 BDRVRawState
*s
= bs
->opaque
;
732 struct fiemap_extent fe
;
735 f
.fm
.fm_start
= start
;
736 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
738 f
.fm
.fm_extent_count
= 1;
739 f
.fm
.fm_reserved
= 0;
740 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
741 /* Assume everything is allocated. */
746 if (f
.fm
.fm_mapped_extents
== 0) {
747 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
748 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
750 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
751 hole
= f
.fm
.fm_start
;
752 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
754 data
= f
.fe
.fe_logical
;
755 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
758 #elif defined SEEK_HOLE && defined SEEK_DATA
760 BDRVRawState
*s
= bs
->opaque
;
762 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
764 /* -ENXIO indicates that sector_num was past the end of the file.
765 * There is a virtual hole there. */
766 assert(errno
!= -ENXIO
);
768 /* Most likely EINVAL. Assume everything is allocated. */
776 /* On a hole. We need another syscall to find its end. */
777 data
= lseek(s
->fd
, start
, SEEK_DATA
);
779 data
= lseek(s
->fd
, 0, SEEK_END
);
788 /* On a data extent, compute sectors to the end of the extent. */
789 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
792 /* On a hole, compute sectors to the beginning of the next extent. */
793 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
799 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
801 struct xfs_flock64 fl
;
803 memset(&fl
, 0, sizeof(fl
));
804 fl
.l_whence
= SEEK_SET
;
805 fl
.l_start
= sector_num
<< 9;
806 fl
.l_len
= (int64_t)nb_sectors
<< 9;
808 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
809 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
817 static coroutine_fn
int raw_co_discard(BlockDriverState
*bs
,
818 int64_t sector_num
, int nb_sectors
)
821 BDRVRawState
*s
= bs
->opaque
;
824 return xfs_discard(s
, sector_num
, nb_sectors
);
831 static QEMUOptionParameter raw_create_options
[] = {
833 .name
= BLOCK_OPT_SIZE
,
835 .help
= "Virtual disk size"
840 static BlockDriver bdrv_file
= {
841 .format_name
= "file",
842 .protocol_name
= "file",
843 .instance_size
= sizeof(BDRVRawState
),
844 .bdrv_probe
= NULL
, /* no probe for protocols */
845 .bdrv_file_open
= raw_open
,
846 .bdrv_reopen_prepare
= raw_reopen_prepare
,
847 .bdrv_reopen_commit
= raw_reopen_commit
,
848 .bdrv_reopen_abort
= raw_reopen_abort
,
849 .bdrv_close
= raw_close
,
850 .bdrv_create
= raw_create
,
851 .bdrv_co_discard
= raw_co_discard
,
852 .bdrv_co_is_allocated
= raw_co_is_allocated
,
854 .bdrv_aio_readv
= raw_aio_readv
,
855 .bdrv_aio_writev
= raw_aio_writev
,
856 .bdrv_aio_flush
= raw_aio_flush
,
858 .bdrv_truncate
= raw_truncate
,
859 .bdrv_getlength
= raw_getlength
,
860 .bdrv_get_allocated_file_size
861 = raw_get_allocated_file_size
,
863 .create_options
= raw_create_options
,
866 /***********************************************/
869 #if defined(__APPLE__) && defined(__MACH__)
870 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
871 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
873 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
875 kern_return_t kernResult
;
876 mach_port_t masterPort
;
877 CFMutableDictionaryRef classesToMatch
;
879 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
880 if ( KERN_SUCCESS
!= kernResult
) {
881 printf( "IOMasterPort returned %d\n", kernResult
);
884 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
885 if ( classesToMatch
== NULL
) {
886 printf( "IOServiceMatching returned a NULL dictionary.\n" );
888 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
890 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
891 if ( KERN_SUCCESS
!= kernResult
)
893 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
899 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
901 io_object_t nextMedia
;
902 kern_return_t kernResult
= KERN_FAILURE
;
904 nextMedia
= IOIteratorNext( mediaIterator
);
907 CFTypeRef bsdPathAsCFString
;
908 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
909 if ( bsdPathAsCFString
) {
910 size_t devPathLength
;
911 strcpy( bsdPath
, _PATH_DEV
);
912 strcat( bsdPath
, "r" );
913 devPathLength
= strlen( bsdPath
);
914 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
915 kernResult
= KERN_SUCCESS
;
917 CFRelease( bsdPathAsCFString
);
919 IOObjectRelease( nextMedia
);
927 static int hdev_probe_device(const char *filename
)
931 /* allow a dedicated CD-ROM driver to match with a higher priority */
932 if (strstart(filename
, "/dev/cdrom", NULL
))
935 if (stat(filename
, &st
) >= 0 &&
936 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
943 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
945 BDRVRawState
*s
= bs
->opaque
;
947 #if defined(__APPLE__) && defined(__MACH__)
948 if (strstart(filename
, "/dev/cdrom", NULL
)) {
949 kern_return_t kernResult
;
950 io_iterator_t mediaIterator
;
951 char bsdPath
[ MAXPATHLEN
];
954 kernResult
= FindEjectableCDMedia( &mediaIterator
);
955 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
957 if ( bsdPath
[ 0 ] != '\0' ) {
958 strcat(bsdPath
,"s0");
959 /* some CDs don't have a partition 0 */
960 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
962 bsdPath
[strlen(bsdPath
)-1] = '1';
970 IOObjectRelease( mediaIterator
);
974 s
->type
= FTYPE_FILE
;
975 #if defined(__linux__)
977 char resolved_path
[ MAXPATHLEN
], *temp
;
979 temp
= realpath(filename
, resolved_path
);
980 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
986 return raw_open_common(bs
, filename
, flags
, 0);
989 #if defined(__linux__)
990 /* Note: we do not have a reliable method to detect if the floppy is
991 present. The current method is to try to open the floppy at every
992 I/O and to keep it opened during a few hundreds of ms. */
993 static int fd_open(BlockDriverState
*bs
)
995 BDRVRawState
*s
= bs
->opaque
;
996 int last_media_present
;
998 if (s
->type
!= FTYPE_FD
)
1000 last_media_present
= (s
->fd
>= 0);
1002 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1006 printf("Floppy closed\n");
1010 if (s
->fd_got_error
&&
1011 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1013 printf("No floppy (open delayed)\n");
1017 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1019 s
->fd_error_time
= get_clock();
1020 s
->fd_got_error
= 1;
1021 if (last_media_present
)
1022 s
->fd_media_changed
= 1;
1024 printf("No floppy\n");
1029 printf("Floppy opened\n");
1032 if (!last_media_present
)
1033 s
->fd_media_changed
= 1;
1034 s
->fd_open_time
= get_clock();
1035 s
->fd_got_error
= 0;
1039 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1041 BDRVRawState
*s
= bs
->opaque
;
1043 return ioctl(s
->fd
, req
, buf
);
1046 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1047 unsigned long int req
, void *buf
,
1048 BlockDriverCompletionFunc
*cb
, void *opaque
)
1050 BDRVRawState
*s
= bs
->opaque
;
1052 if (fd_open(bs
) < 0)
1054 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
1057 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1058 static int fd_open(BlockDriverState
*bs
)
1060 BDRVRawState
*s
= bs
->opaque
;
1062 /* this is just to ensure s->fd is sane (its called by io ops) */
1067 #else /* !linux && !FreeBSD */
1069 static int fd_open(BlockDriverState
*bs
)
1074 #endif /* !linux && !FreeBSD */
1076 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1080 struct stat stat_buf
;
1081 int64_t total_size
= 0;
1083 /* Read out options */
1084 while (options
&& options
->name
) {
1085 if (!strcmp(options
->name
, "size")) {
1086 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1091 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1095 if (fstat(fd
, &stat_buf
) < 0)
1097 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1099 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1106 static int hdev_has_zero_init(BlockDriverState
*bs
)
1111 static BlockDriver bdrv_host_device
= {
1112 .format_name
= "host_device",
1113 .protocol_name
= "host_device",
1114 .instance_size
= sizeof(BDRVRawState
),
1115 .bdrv_probe_device
= hdev_probe_device
,
1116 .bdrv_file_open
= hdev_open
,
1117 .bdrv_close
= raw_close
,
1118 .bdrv_create
= hdev_create
,
1119 .create_options
= raw_create_options
,
1120 .bdrv_has_zero_init
= hdev_has_zero_init
,
1122 .bdrv_aio_readv
= raw_aio_readv
,
1123 .bdrv_aio_writev
= raw_aio_writev
,
1124 .bdrv_aio_flush
= raw_aio_flush
,
1126 .bdrv_truncate
= raw_truncate
,
1127 .bdrv_getlength
= raw_getlength
,
1128 .bdrv_get_allocated_file_size
1129 = raw_get_allocated_file_size
,
1131 /* generic scsi device */
1133 .bdrv_ioctl
= hdev_ioctl
,
1134 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1139 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1141 BDRVRawState
*s
= bs
->opaque
;
1146 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1147 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1151 /* close fd so that we can reopen it as needed */
1154 s
->fd_media_changed
= 1;
1159 static int floppy_probe_device(const char *filename
)
1163 struct floppy_struct fdparam
;
1166 if (strstart(filename
, "/dev/fd", NULL
) &&
1167 !strstart(filename
, "/dev/fdset/", NULL
)) {
1171 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1175 ret
= fstat(fd
, &st
);
1176 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1180 /* Attempt to detect via a floppy specific ioctl */
1181 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1192 static int floppy_is_inserted(BlockDriverState
*bs
)
1194 return fd_open(bs
) >= 0;
1197 static int floppy_media_changed(BlockDriverState
*bs
)
1199 BDRVRawState
*s
= bs
->opaque
;
1203 * XXX: we do not have a true media changed indication.
1204 * It does not work if the floppy is changed without trying to read it.
1207 ret
= s
->fd_media_changed
;
1208 s
->fd_media_changed
= 0;
1210 printf("Floppy changed=%d\n", ret
);
1215 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1217 BDRVRawState
*s
= bs
->opaque
;
1224 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1226 if (ioctl(fd
, FDEJECT
, 0) < 0)
1232 static BlockDriver bdrv_host_floppy
= {
1233 .format_name
= "host_floppy",
1234 .protocol_name
= "host_floppy",
1235 .instance_size
= sizeof(BDRVRawState
),
1236 .bdrv_probe_device
= floppy_probe_device
,
1237 .bdrv_file_open
= floppy_open
,
1238 .bdrv_close
= raw_close
,
1239 .bdrv_create
= hdev_create
,
1240 .create_options
= raw_create_options
,
1241 .bdrv_has_zero_init
= hdev_has_zero_init
,
1243 .bdrv_aio_readv
= raw_aio_readv
,
1244 .bdrv_aio_writev
= raw_aio_writev
,
1245 .bdrv_aio_flush
= raw_aio_flush
,
1247 .bdrv_truncate
= raw_truncate
,
1248 .bdrv_getlength
= raw_getlength
,
1249 .bdrv_get_allocated_file_size
1250 = raw_get_allocated_file_size
,
1252 /* removable device support */
1253 .bdrv_is_inserted
= floppy_is_inserted
,
1254 .bdrv_media_changed
= floppy_media_changed
,
1255 .bdrv_eject
= floppy_eject
,
1258 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1260 BDRVRawState
*s
= bs
->opaque
;
1264 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1265 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1268 static int cdrom_probe_device(const char *filename
)
1274 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1278 ret
= fstat(fd
, &st
);
1279 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1283 /* Attempt to detect via a CDROM specific ioctl */
1284 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1294 static int cdrom_is_inserted(BlockDriverState
*bs
)
1296 BDRVRawState
*s
= bs
->opaque
;
1299 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1300 if (ret
== CDS_DISC_OK
)
1305 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1307 BDRVRawState
*s
= bs
->opaque
;
1310 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1311 perror("CDROMEJECT");
1313 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1314 perror("CDROMEJECT");
1318 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1320 BDRVRawState
*s
= bs
->opaque
;
1322 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1324 * Note: an error can happen if the distribution automatically
1327 /* perror("CDROM_LOCKDOOR"); */
1331 static BlockDriver bdrv_host_cdrom
= {
1332 .format_name
= "host_cdrom",
1333 .protocol_name
= "host_cdrom",
1334 .instance_size
= sizeof(BDRVRawState
),
1335 .bdrv_probe_device
= cdrom_probe_device
,
1336 .bdrv_file_open
= cdrom_open
,
1337 .bdrv_close
= raw_close
,
1338 .bdrv_create
= hdev_create
,
1339 .create_options
= raw_create_options
,
1340 .bdrv_has_zero_init
= hdev_has_zero_init
,
1342 .bdrv_aio_readv
= raw_aio_readv
,
1343 .bdrv_aio_writev
= raw_aio_writev
,
1344 .bdrv_aio_flush
= raw_aio_flush
,
1346 .bdrv_truncate
= raw_truncate
,
1347 .bdrv_getlength
= raw_getlength
,
1348 .bdrv_get_allocated_file_size
1349 = raw_get_allocated_file_size
,
1351 /* removable device support */
1352 .bdrv_is_inserted
= cdrom_is_inserted
,
1353 .bdrv_eject
= cdrom_eject
,
1354 .bdrv_lock_medium
= cdrom_lock_medium
,
1356 /* generic scsi device */
1357 .bdrv_ioctl
= hdev_ioctl
,
1358 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1360 #endif /* __linux__ */
1362 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1363 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1365 BDRVRawState
*s
= bs
->opaque
;
1370 ret
= raw_open_common(bs
, filename
, flags
, 0);
1374 /* make sure the door isn't locked at this time */
1375 ioctl(s
->fd
, CDIOCALLOW
);
1379 static int cdrom_probe_device(const char *filename
)
1381 if (strstart(filename
, "/dev/cd", NULL
) ||
1382 strstart(filename
, "/dev/acd", NULL
))
1387 static int cdrom_reopen(BlockDriverState
*bs
)
1389 BDRVRawState
*s
= bs
->opaque
;
1393 * Force reread of possibly changed/newly loaded disc,
1394 * FreeBSD seems to not notice sometimes...
1398 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1405 /* make sure the door isn't locked at this time */
1406 ioctl(s
->fd
, CDIOCALLOW
);
1410 static int cdrom_is_inserted(BlockDriverState
*bs
)
1412 return raw_getlength(bs
) > 0;
1415 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1417 BDRVRawState
*s
= bs
->opaque
;
1422 (void) ioctl(s
->fd
, CDIOCALLOW
);
1425 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1426 perror("CDIOCEJECT");
1428 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1429 perror("CDIOCCLOSE");
1435 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1437 BDRVRawState
*s
= bs
->opaque
;
1441 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1443 * Note: an error can happen if the distribution automatically
1446 /* perror("CDROM_LOCKDOOR"); */
1450 static BlockDriver bdrv_host_cdrom
= {
1451 .format_name
= "host_cdrom",
1452 .protocol_name
= "host_cdrom",
1453 .instance_size
= sizeof(BDRVRawState
),
1454 .bdrv_probe_device
= cdrom_probe_device
,
1455 .bdrv_file_open
= cdrom_open
,
1456 .bdrv_close
= raw_close
,
1457 .bdrv_create
= hdev_create
,
1458 .create_options
= raw_create_options
,
1459 .bdrv_has_zero_init
= hdev_has_zero_init
,
1461 .bdrv_aio_readv
= raw_aio_readv
,
1462 .bdrv_aio_writev
= raw_aio_writev
,
1463 .bdrv_aio_flush
= raw_aio_flush
,
1465 .bdrv_truncate
= raw_truncate
,
1466 .bdrv_getlength
= raw_getlength
,
1467 .bdrv_get_allocated_file_size
1468 = raw_get_allocated_file_size
,
1470 /* removable device support */
1471 .bdrv_is_inserted
= cdrom_is_inserted
,
1472 .bdrv_eject
= cdrom_eject
,
1473 .bdrv_lock_medium
= cdrom_lock_medium
,
1475 #endif /* __FreeBSD__ */
1477 static void bdrv_file_init(void)
1480 * Register all the drivers. Note that order is important, the driver
1481 * registered last will get probed first.
1483 bdrv_register(&bdrv_file
);
1484 bdrv_register(&bdrv_host_device
);
1486 bdrv_register(&bdrv_host_floppy
);
1487 bdrv_register(&bdrv_host_cdrom
);
1489 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1490 bdrv_register(&bdrv_host_cdrom
);
1494 block_init(bdrv_file_init
);