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
136 uint8_t *aligned_buf
;
137 unsigned aligned_buf_size
;
143 static int fd_open(BlockDriverState
*bs
);
144 static int64_t raw_getlength(BlockDriverState
*bs
);
146 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
147 static int cdrom_reopen(BlockDriverState
*bs
);
150 #if defined(__NetBSD__)
151 static int raw_normalize_devicepath(const char **filename
)
153 static char namebuf
[PATH_MAX
];
154 const char *dp
, *fname
;
158 dp
= strrchr(fname
, '/');
159 if (lstat(fname
, &sb
) < 0) {
160 fprintf(stderr
, "%s: stat failed: %s\n",
161 fname
, strerror(errno
));
165 if (!S_ISBLK(sb
.st_mode
)) {
170 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
172 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
173 (int)(dp
- fname
), fname
, dp
+ 1);
175 fprintf(stderr
, "%s is a block device", fname
);
177 fprintf(stderr
, ", using %s\n", *filename
);
182 static int raw_normalize_devicepath(const char **filename
)
188 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
189 int bdrv_flags
, int open_flags
)
191 BDRVRawState
*s
= bs
->opaque
;
194 ret
= raw_normalize_devicepath(&filename
);
199 s
->open_flags
= open_flags
| O_BINARY
;
200 s
->open_flags
&= ~O_ACCMODE
;
201 if (bdrv_flags
& BDRV_O_RDWR
) {
202 s
->open_flags
|= O_RDWR
;
204 s
->open_flags
|= O_RDONLY
;
207 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
208 * and O_DIRECT for no caching. */
209 if ((bdrv_flags
& BDRV_O_NOCACHE
))
210 s
->open_flags
|= O_DIRECT
;
211 if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
212 s
->open_flags
|= O_DSYNC
;
215 fd
= qemu_open(filename
, s
->open_flags
, 0644);
223 s
->aligned_buf
= NULL
;
225 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
227 * Allocate a buffer for read/modify/write cycles. Chose the size
228 * pessimistically as we don't know the block size yet.
230 s
->aligned_buf_size
= 32 * MAX_BLOCKSIZE
;
231 s
->aligned_buf
= qemu_memalign(MAX_BLOCKSIZE
, s
->aligned_buf_size
);
232 if (s
->aligned_buf
== NULL
) {
237 /* We're falling back to POSIX AIO in some cases so init always */
238 if (paio_init() < 0) {
242 #ifdef CONFIG_LINUX_AIO
244 * Currently Linux do AIO only for files opened with O_DIRECT
245 * specified so check NOCACHE flag too
247 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
248 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
250 s
->aio_ctx
= laio_init();
258 #ifdef CONFIG_LINUX_AIO
264 if (platform_test_xfs_fd(s
->fd
)) {
272 qemu_vfree(s
->aligned_buf
);
278 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
280 BDRVRawState
*s
= bs
->opaque
;
282 s
->type
= FTYPE_FILE
;
283 return raw_open_common(bs
, filename
, flags
, 0);
286 /* XXX: use host sector size if necessary with:
287 #ifdef DIOCGSECTORSIZE
289 unsigned int sectorsize = 512;
290 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
291 sectorsize > bufsize)
292 bufsize = sectorsize;
296 uint32_t blockSize = 512;
297 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
304 * Check if all memory in this vector is sector aligned.
306 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
310 for (i
= 0; i
< qiov
->niov
; i
++) {
311 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
319 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
320 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
321 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
323 BDRVRawState
*s
= bs
->opaque
;
329 * If O_DIRECT is used the buffer needs to be aligned on a sector
330 * boundary. Check if this is the case or tell the low-level
331 * driver that it needs to copy the buffer.
333 if (s
->aligned_buf
) {
334 if (!qiov_is_aligned(bs
, qiov
)) {
335 type
|= QEMU_AIO_MISALIGNED
;
336 #ifdef CONFIG_LINUX_AIO
337 } else if (s
->use_aio
) {
338 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
339 nb_sectors
, cb
, opaque
, type
);
344 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
348 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
349 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
350 BlockDriverCompletionFunc
*cb
, void *opaque
)
352 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
353 cb
, opaque
, QEMU_AIO_READ
);
356 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
357 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
358 BlockDriverCompletionFunc
*cb
, void *opaque
)
360 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
361 cb
, opaque
, QEMU_AIO_WRITE
);
364 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
365 BlockDriverCompletionFunc
*cb
, void *opaque
)
367 BDRVRawState
*s
= bs
->opaque
;
372 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
375 static void raw_close(BlockDriverState
*bs
)
377 BDRVRawState
*s
= bs
->opaque
;
381 if (s
->aligned_buf
!= NULL
)
382 qemu_vfree(s
->aligned_buf
);
386 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
388 BDRVRawState
*s
= bs
->opaque
;
391 if (fstat(s
->fd
, &st
)) {
395 if (S_ISREG(st
.st_mode
)) {
396 if (ftruncate(s
->fd
, offset
) < 0) {
399 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
400 if (offset
> raw_getlength(bs
)) {
411 static int64_t raw_getlength(BlockDriverState
*bs
)
413 BDRVRawState
*s
= bs
->opaque
;
419 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
422 if (ioctl(fd
, DIOCGDINFO
, &dl
))
424 return (uint64_t)dl
.d_secsize
*
425 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
429 #elif defined(__NetBSD__)
430 static int64_t raw_getlength(BlockDriverState
*bs
)
432 BDRVRawState
*s
= bs
->opaque
;
438 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
439 struct dkwedge_info dkw
;
441 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
442 return dkw
.dkw_size
* 512;
446 if (ioctl(fd
, DIOCGDINFO
, &dl
))
448 return (uint64_t)dl
.d_secsize
*
449 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
454 #elif defined(__sun__)
455 static int64_t raw_getlength(BlockDriverState
*bs
)
457 BDRVRawState
*s
= bs
->opaque
;
458 struct dk_minfo minfo
;
467 * Use the DKIOCGMEDIAINFO ioctl to read the size.
469 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
471 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
475 * There are reports that lseek on some devices fails, but
476 * irc discussion said that contingency on contingency was overkill.
478 return lseek(s
->fd
, 0, SEEK_END
);
480 #elif defined(CONFIG_BSD)
481 static int64_t raw_getlength(BlockDriverState
*bs
)
483 BDRVRawState
*s
= bs
->opaque
;
487 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
496 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
499 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
500 #ifdef DIOCGMEDIASIZE
501 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
502 #elif defined(DIOCGPART)
505 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
506 size
= pi
.media_size
;
512 #if defined(__APPLE__) && defined(__MACH__)
513 size
= LONG_LONG_MAX
;
515 size
= lseek(fd
, 0LL, SEEK_END
);
517 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
520 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
521 if (size
== 2048LL * (unsigned)-1)
523 /* XXX no disc? maybe we need to reopen... */
524 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
531 size
= lseek(fd
, 0, SEEK_END
);
536 static int64_t raw_getlength(BlockDriverState
*bs
)
538 BDRVRawState
*s
= bs
->opaque
;
546 return lseek(s
->fd
, 0, SEEK_END
);
550 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
553 BDRVRawState
*s
= bs
->opaque
;
555 if (fstat(s
->fd
, &st
) < 0) {
558 return (int64_t)st
.st_blocks
* 512;
561 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
565 int64_t total_size
= 0;
567 /* Read out options */
568 while (options
&& options
->name
) {
569 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
570 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
575 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
580 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
583 if (qemu_close(fd
) != 0) {
591 * Returns true iff the specified sector is present in the disk image. Drivers
592 * not implementing the functionality are assumed to not support backing files,
593 * hence all their sectors are reported as allocated.
595 * If 'sector_num' is beyond the end of the disk image the return value is 0
596 * and 'pnum' is set to 0.
598 * 'pnum' is set to the number of sectors (including and immediately following
599 * the specified sector) that are known to be in the same
600 * allocated/unallocated state.
602 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
603 * beyond the end of the disk image it will be clamped.
605 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
607 int nb_sectors
, int *pnum
)
609 off_t start
, data
, hole
;
617 start
= sector_num
* BDRV_SECTOR_SIZE
;
621 BDRVRawState
*s
= bs
->opaque
;
624 struct fiemap_extent fe
;
627 f
.fm
.fm_start
= start
;
628 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
630 f
.fm
.fm_extent_count
= 1;
631 f
.fm
.fm_reserved
= 0;
632 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
633 /* Assume everything is allocated. */
638 if (f
.fm
.fm_mapped_extents
== 0) {
639 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
640 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
642 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
643 hole
= f
.fm
.fm_start
;
644 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
646 data
= f
.fe
.fe_logical
;
647 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
650 #elif defined SEEK_HOLE && defined SEEK_DATA
652 BDRVRawState
*s
= bs
->opaque
;
654 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
656 /* -ENXIO indicates that sector_num was past the end of the file.
657 * There is a virtual hole there. */
658 assert(errno
!= -ENXIO
);
660 /* Most likely EINVAL. Assume everything is allocated. */
668 /* On a hole. We need another syscall to find its end. */
669 data
= lseek(s
->fd
, start
, SEEK_DATA
);
671 data
= lseek(s
->fd
, 0, SEEK_END
);
680 /* On a data extent, compute sectors to the end of the extent. */
681 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
684 /* On a hole, compute sectors to the beginning of the next extent. */
685 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
691 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
693 struct xfs_flock64 fl
;
695 memset(&fl
, 0, sizeof(fl
));
696 fl
.l_whence
= SEEK_SET
;
697 fl
.l_start
= sector_num
<< 9;
698 fl
.l_len
= (int64_t)nb_sectors
<< 9;
700 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
701 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
709 static coroutine_fn
int raw_co_discard(BlockDriverState
*bs
,
710 int64_t sector_num
, int nb_sectors
)
713 BDRVRawState
*s
= bs
->opaque
;
716 return xfs_discard(s
, sector_num
, nb_sectors
);
723 static QEMUOptionParameter raw_create_options
[] = {
725 .name
= BLOCK_OPT_SIZE
,
727 .help
= "Virtual disk size"
732 static BlockDriver bdrv_file
= {
733 .format_name
= "file",
734 .protocol_name
= "file",
735 .instance_size
= sizeof(BDRVRawState
),
736 .bdrv_probe
= NULL
, /* no probe for protocols */
737 .bdrv_file_open
= raw_open
,
738 .bdrv_close
= raw_close
,
739 .bdrv_create
= raw_create
,
740 .bdrv_co_discard
= raw_co_discard
,
741 .bdrv_co_is_allocated
= raw_co_is_allocated
,
743 .bdrv_aio_readv
= raw_aio_readv
,
744 .bdrv_aio_writev
= raw_aio_writev
,
745 .bdrv_aio_flush
= raw_aio_flush
,
747 .bdrv_truncate
= raw_truncate
,
748 .bdrv_getlength
= raw_getlength
,
749 .bdrv_get_allocated_file_size
750 = raw_get_allocated_file_size
,
752 .create_options
= raw_create_options
,
755 /***********************************************/
758 #if defined(__APPLE__) && defined(__MACH__)
759 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
760 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
762 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
764 kern_return_t kernResult
;
765 mach_port_t masterPort
;
766 CFMutableDictionaryRef classesToMatch
;
768 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
769 if ( KERN_SUCCESS
!= kernResult
) {
770 printf( "IOMasterPort returned %d\n", kernResult
);
773 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
774 if ( classesToMatch
== NULL
) {
775 printf( "IOServiceMatching returned a NULL dictionary.\n" );
777 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
779 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
780 if ( KERN_SUCCESS
!= kernResult
)
782 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
788 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
790 io_object_t nextMedia
;
791 kern_return_t kernResult
= KERN_FAILURE
;
793 nextMedia
= IOIteratorNext( mediaIterator
);
796 CFTypeRef bsdPathAsCFString
;
797 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
798 if ( bsdPathAsCFString
) {
799 size_t devPathLength
;
800 strcpy( bsdPath
, _PATH_DEV
);
801 strcat( bsdPath
, "r" );
802 devPathLength
= strlen( bsdPath
);
803 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
804 kernResult
= KERN_SUCCESS
;
806 CFRelease( bsdPathAsCFString
);
808 IOObjectRelease( nextMedia
);
816 static int hdev_probe_device(const char *filename
)
820 /* allow a dedicated CD-ROM driver to match with a higher priority */
821 if (strstart(filename
, "/dev/cdrom", NULL
))
824 if (stat(filename
, &st
) >= 0 &&
825 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
832 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
834 BDRVRawState
*s
= bs
->opaque
;
836 #if defined(__APPLE__) && defined(__MACH__)
837 if (strstart(filename
, "/dev/cdrom", NULL
)) {
838 kern_return_t kernResult
;
839 io_iterator_t mediaIterator
;
840 char bsdPath
[ MAXPATHLEN
];
843 kernResult
= FindEjectableCDMedia( &mediaIterator
);
844 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
846 if ( bsdPath
[ 0 ] != '\0' ) {
847 strcat(bsdPath
,"s0");
848 /* some CDs don't have a partition 0 */
849 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
851 bsdPath
[strlen(bsdPath
)-1] = '1';
859 IOObjectRelease( mediaIterator
);
863 s
->type
= FTYPE_FILE
;
864 #if defined(__linux__)
866 char resolved_path
[ MAXPATHLEN
], *temp
;
868 temp
= realpath(filename
, resolved_path
);
869 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
875 return raw_open_common(bs
, filename
, flags
, 0);
878 #if defined(__linux__)
879 /* Note: we do not have a reliable method to detect if the floppy is
880 present. The current method is to try to open the floppy at every
881 I/O and to keep it opened during a few hundreds of ms. */
882 static int fd_open(BlockDriverState
*bs
)
884 BDRVRawState
*s
= bs
->opaque
;
885 int last_media_present
;
887 if (s
->type
!= FTYPE_FD
)
889 last_media_present
= (s
->fd
>= 0);
891 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
895 printf("Floppy closed\n");
899 if (s
->fd_got_error
&&
900 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
902 printf("No floppy (open delayed)\n");
906 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
908 s
->fd_error_time
= get_clock();
910 if (last_media_present
)
911 s
->fd_media_changed
= 1;
913 printf("No floppy\n");
918 printf("Floppy opened\n");
921 if (!last_media_present
)
922 s
->fd_media_changed
= 1;
923 s
->fd_open_time
= get_clock();
928 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
930 BDRVRawState
*s
= bs
->opaque
;
932 return ioctl(s
->fd
, req
, buf
);
935 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
936 unsigned long int req
, void *buf
,
937 BlockDriverCompletionFunc
*cb
, void *opaque
)
939 BDRVRawState
*s
= bs
->opaque
;
943 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
946 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
947 static int fd_open(BlockDriverState
*bs
)
949 BDRVRawState
*s
= bs
->opaque
;
951 /* this is just to ensure s->fd is sane (its called by io ops) */
956 #else /* !linux && !FreeBSD */
958 static int fd_open(BlockDriverState
*bs
)
963 #endif /* !linux && !FreeBSD */
965 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
969 struct stat stat_buf
;
970 int64_t total_size
= 0;
972 /* Read out options */
973 while (options
&& options
->name
) {
974 if (!strcmp(options
->name
, "size")) {
975 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
980 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
984 if (fstat(fd
, &stat_buf
) < 0)
986 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
988 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
995 static int hdev_has_zero_init(BlockDriverState
*bs
)
1000 static BlockDriver bdrv_host_device
= {
1001 .format_name
= "host_device",
1002 .protocol_name
= "host_device",
1003 .instance_size
= sizeof(BDRVRawState
),
1004 .bdrv_probe_device
= hdev_probe_device
,
1005 .bdrv_file_open
= hdev_open
,
1006 .bdrv_close
= raw_close
,
1007 .bdrv_create
= hdev_create
,
1008 .create_options
= raw_create_options
,
1009 .bdrv_has_zero_init
= hdev_has_zero_init
,
1011 .bdrv_aio_readv
= raw_aio_readv
,
1012 .bdrv_aio_writev
= raw_aio_writev
,
1013 .bdrv_aio_flush
= raw_aio_flush
,
1015 .bdrv_truncate
= raw_truncate
,
1016 .bdrv_getlength
= raw_getlength
,
1017 .bdrv_get_allocated_file_size
1018 = raw_get_allocated_file_size
,
1020 /* generic scsi device */
1022 .bdrv_ioctl
= hdev_ioctl
,
1023 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1028 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1030 BDRVRawState
*s
= bs
->opaque
;
1035 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1036 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1040 /* close fd so that we can reopen it as needed */
1043 s
->fd_media_changed
= 1;
1048 static int floppy_probe_device(const char *filename
)
1052 struct floppy_struct fdparam
;
1055 if (strstart(filename
, "/dev/fd", NULL
) &&
1056 !strstart(filename
, "/dev/fdset/", NULL
)) {
1060 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1064 ret
= fstat(fd
, &st
);
1065 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1069 /* Attempt to detect via a floppy specific ioctl */
1070 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1081 static int floppy_is_inserted(BlockDriverState
*bs
)
1083 return fd_open(bs
) >= 0;
1086 static int floppy_media_changed(BlockDriverState
*bs
)
1088 BDRVRawState
*s
= bs
->opaque
;
1092 * XXX: we do not have a true media changed indication.
1093 * It does not work if the floppy is changed without trying to read it.
1096 ret
= s
->fd_media_changed
;
1097 s
->fd_media_changed
= 0;
1099 printf("Floppy changed=%d\n", ret
);
1104 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1106 BDRVRawState
*s
= bs
->opaque
;
1113 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1115 if (ioctl(fd
, FDEJECT
, 0) < 0)
1121 static BlockDriver bdrv_host_floppy
= {
1122 .format_name
= "host_floppy",
1123 .protocol_name
= "host_floppy",
1124 .instance_size
= sizeof(BDRVRawState
),
1125 .bdrv_probe_device
= floppy_probe_device
,
1126 .bdrv_file_open
= floppy_open
,
1127 .bdrv_close
= raw_close
,
1128 .bdrv_create
= hdev_create
,
1129 .create_options
= raw_create_options
,
1130 .bdrv_has_zero_init
= hdev_has_zero_init
,
1132 .bdrv_aio_readv
= raw_aio_readv
,
1133 .bdrv_aio_writev
= raw_aio_writev
,
1134 .bdrv_aio_flush
= raw_aio_flush
,
1136 .bdrv_truncate
= raw_truncate
,
1137 .bdrv_getlength
= raw_getlength
,
1138 .bdrv_get_allocated_file_size
1139 = raw_get_allocated_file_size
,
1141 /* removable device support */
1142 .bdrv_is_inserted
= floppy_is_inserted
,
1143 .bdrv_media_changed
= floppy_media_changed
,
1144 .bdrv_eject
= floppy_eject
,
1147 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1149 BDRVRawState
*s
= bs
->opaque
;
1153 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1154 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1157 static int cdrom_probe_device(const char *filename
)
1163 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1167 ret
= fstat(fd
, &st
);
1168 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1172 /* Attempt to detect via a CDROM specific ioctl */
1173 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1183 static int cdrom_is_inserted(BlockDriverState
*bs
)
1185 BDRVRawState
*s
= bs
->opaque
;
1188 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1189 if (ret
== CDS_DISC_OK
)
1194 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1196 BDRVRawState
*s
= bs
->opaque
;
1199 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1200 perror("CDROMEJECT");
1202 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1203 perror("CDROMEJECT");
1207 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1209 BDRVRawState
*s
= bs
->opaque
;
1211 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1213 * Note: an error can happen if the distribution automatically
1216 /* perror("CDROM_LOCKDOOR"); */
1220 static BlockDriver bdrv_host_cdrom
= {
1221 .format_name
= "host_cdrom",
1222 .protocol_name
= "host_cdrom",
1223 .instance_size
= sizeof(BDRVRawState
),
1224 .bdrv_probe_device
= cdrom_probe_device
,
1225 .bdrv_file_open
= cdrom_open
,
1226 .bdrv_close
= raw_close
,
1227 .bdrv_create
= hdev_create
,
1228 .create_options
= raw_create_options
,
1229 .bdrv_has_zero_init
= hdev_has_zero_init
,
1231 .bdrv_aio_readv
= raw_aio_readv
,
1232 .bdrv_aio_writev
= raw_aio_writev
,
1233 .bdrv_aio_flush
= raw_aio_flush
,
1235 .bdrv_truncate
= raw_truncate
,
1236 .bdrv_getlength
= raw_getlength
,
1237 .bdrv_get_allocated_file_size
1238 = raw_get_allocated_file_size
,
1240 /* removable device support */
1241 .bdrv_is_inserted
= cdrom_is_inserted
,
1242 .bdrv_eject
= cdrom_eject
,
1243 .bdrv_lock_medium
= cdrom_lock_medium
,
1245 /* generic scsi device */
1246 .bdrv_ioctl
= hdev_ioctl
,
1247 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1249 #endif /* __linux__ */
1251 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1252 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1254 BDRVRawState
*s
= bs
->opaque
;
1259 ret
= raw_open_common(bs
, filename
, flags
, 0);
1263 /* make sure the door isn't locked at this time */
1264 ioctl(s
->fd
, CDIOCALLOW
);
1268 static int cdrom_probe_device(const char *filename
)
1270 if (strstart(filename
, "/dev/cd", NULL
) ||
1271 strstart(filename
, "/dev/acd", NULL
))
1276 static int cdrom_reopen(BlockDriverState
*bs
)
1278 BDRVRawState
*s
= bs
->opaque
;
1282 * Force reread of possibly changed/newly loaded disc,
1283 * FreeBSD seems to not notice sometimes...
1287 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1294 /* make sure the door isn't locked at this time */
1295 ioctl(s
->fd
, CDIOCALLOW
);
1299 static int cdrom_is_inserted(BlockDriverState
*bs
)
1301 return raw_getlength(bs
) > 0;
1304 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1306 BDRVRawState
*s
= bs
->opaque
;
1311 (void) ioctl(s
->fd
, CDIOCALLOW
);
1314 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1315 perror("CDIOCEJECT");
1317 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1318 perror("CDIOCCLOSE");
1324 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1326 BDRVRawState
*s
= bs
->opaque
;
1330 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 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 #endif /* __FreeBSD__ */
1366 static void bdrv_file_init(void)
1369 * Register all the drivers. Note that order is important, the driver
1370 * registered last will get probed first.
1372 bdrv_register(&bdrv_file
);
1373 bdrv_register(&bdrv_host_device
);
1375 bdrv_register(&bdrv_host_floppy
);
1376 bdrv_register(&bdrv_host_cdrom
);
1378 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1379 bdrv_register(&bdrv_host_cdrom
);
1383 block_init(bdrv_file_init
);