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 #ifdef CONFIG_LINUX_AIO
189 static int raw_set_aio(void **aio_ctx
, int *use_aio
, int bdrv_flags
)
192 assert(aio_ctx
!= NULL
);
193 assert(use_aio
!= NULL
);
195 * Currently Linux do AIO only for files opened with O_DIRECT
196 * specified so check NOCACHE flag too
198 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
199 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
201 /* if non-NULL, laio_init() has already been run */
202 if (*aio_ctx
== NULL
) {
203 *aio_ctx
= laio_init();
220 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
221 int bdrv_flags
, int open_flags
)
223 BDRVRawState
*s
= bs
->opaque
;
226 ret
= raw_normalize_devicepath(&filename
);
231 s
->open_flags
= open_flags
| O_BINARY
;
232 s
->open_flags
&= ~O_ACCMODE
;
233 if (bdrv_flags
& BDRV_O_RDWR
) {
234 s
->open_flags
|= O_RDWR
;
236 s
->open_flags
|= O_RDONLY
;
239 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
240 * and O_DIRECT for no caching. */
241 if ((bdrv_flags
& BDRV_O_NOCACHE
))
242 s
->open_flags
|= O_DIRECT
;
243 if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
244 s
->open_flags
|= O_DSYNC
;
247 fd
= qemu_open(filename
, s
->open_flags
, 0644);
255 s
->aligned_buf
= NULL
;
257 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
259 * Allocate a buffer for read/modify/write cycles. Chose the size
260 * pessimistically as we don't know the block size yet.
262 s
->aligned_buf_size
= 32 * MAX_BLOCKSIZE
;
263 s
->aligned_buf
= qemu_memalign(MAX_BLOCKSIZE
, s
->aligned_buf_size
);
264 if (s
->aligned_buf
== NULL
) {
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
)) {
289 qemu_vfree(s
->aligned_buf
);
295 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
297 BDRVRawState
*s
= bs
->opaque
;
299 s
->type
= FTYPE_FILE
;
300 return raw_open_common(bs
, filename
, flags
, 0);
303 /* XXX: use host sector size if necessary with:
304 #ifdef DIOCGSECTORSIZE
306 unsigned int sectorsize = 512;
307 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
308 sectorsize > bufsize)
309 bufsize = sectorsize;
313 uint32_t blockSize = 512;
314 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
321 * Check if all memory in this vector is sector aligned.
323 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
327 for (i
= 0; i
< qiov
->niov
; i
++) {
328 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
336 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
337 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
338 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
340 BDRVRawState
*s
= bs
->opaque
;
346 * If O_DIRECT is used the buffer needs to be aligned on a sector
347 * boundary. Check if this is the case or tell the low-level
348 * driver that it needs to copy the buffer.
350 if (s
->aligned_buf
) {
351 if (!qiov_is_aligned(bs
, qiov
)) {
352 type
|= QEMU_AIO_MISALIGNED
;
353 #ifdef CONFIG_LINUX_AIO
354 } else if (s
->use_aio
) {
355 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
356 nb_sectors
, cb
, opaque
, type
);
361 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
365 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
366 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
367 BlockDriverCompletionFunc
*cb
, void *opaque
)
369 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
370 cb
, opaque
, QEMU_AIO_READ
);
373 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
374 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
375 BlockDriverCompletionFunc
*cb
, void *opaque
)
377 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
378 cb
, opaque
, QEMU_AIO_WRITE
);
381 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
382 BlockDriverCompletionFunc
*cb
, void *opaque
)
384 BDRVRawState
*s
= bs
->opaque
;
389 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
392 static void raw_close(BlockDriverState
*bs
)
394 BDRVRawState
*s
= bs
->opaque
;
398 if (s
->aligned_buf
!= NULL
)
399 qemu_vfree(s
->aligned_buf
);
403 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
405 BDRVRawState
*s
= bs
->opaque
;
408 if (fstat(s
->fd
, &st
)) {
412 if (S_ISREG(st
.st_mode
)) {
413 if (ftruncate(s
->fd
, offset
) < 0) {
416 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
417 if (offset
> raw_getlength(bs
)) {
428 static int64_t raw_getlength(BlockDriverState
*bs
)
430 BDRVRawState
*s
= bs
->opaque
;
436 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
439 if (ioctl(fd
, DIOCGDINFO
, &dl
))
441 return (uint64_t)dl
.d_secsize
*
442 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
446 #elif defined(__NetBSD__)
447 static int64_t raw_getlength(BlockDriverState
*bs
)
449 BDRVRawState
*s
= bs
->opaque
;
455 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
456 struct dkwedge_info dkw
;
458 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
459 return dkw
.dkw_size
* 512;
463 if (ioctl(fd
, DIOCGDINFO
, &dl
))
465 return (uint64_t)dl
.d_secsize
*
466 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
471 #elif defined(__sun__)
472 static int64_t raw_getlength(BlockDriverState
*bs
)
474 BDRVRawState
*s
= bs
->opaque
;
475 struct dk_minfo minfo
;
484 * Use the DKIOCGMEDIAINFO ioctl to read the size.
486 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
488 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
492 * There are reports that lseek on some devices fails, but
493 * irc discussion said that contingency on contingency was overkill.
495 return lseek(s
->fd
, 0, SEEK_END
);
497 #elif defined(CONFIG_BSD)
498 static int64_t raw_getlength(BlockDriverState
*bs
)
500 BDRVRawState
*s
= bs
->opaque
;
504 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
513 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
516 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
517 #ifdef DIOCGMEDIASIZE
518 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
519 #elif defined(DIOCGPART)
522 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
523 size
= pi
.media_size
;
529 #if defined(__APPLE__) && defined(__MACH__)
530 size
= LONG_LONG_MAX
;
532 size
= lseek(fd
, 0LL, SEEK_END
);
534 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
537 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
538 if (size
== 2048LL * (unsigned)-1)
540 /* XXX no disc? maybe we need to reopen... */
541 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
548 size
= lseek(fd
, 0, SEEK_END
);
553 static int64_t raw_getlength(BlockDriverState
*bs
)
555 BDRVRawState
*s
= bs
->opaque
;
563 return lseek(s
->fd
, 0, SEEK_END
);
567 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
570 BDRVRawState
*s
= bs
->opaque
;
572 if (fstat(s
->fd
, &st
) < 0) {
575 return (int64_t)st
.st_blocks
* 512;
578 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
582 int64_t total_size
= 0;
584 /* Read out options */
585 while (options
&& options
->name
) {
586 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
587 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
592 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
597 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
600 if (qemu_close(fd
) != 0) {
608 * Returns true iff the specified sector is present in the disk image. Drivers
609 * not implementing the functionality are assumed to not support backing files,
610 * hence all their sectors are reported as allocated.
612 * If 'sector_num' is beyond the end of the disk image the return value is 0
613 * and 'pnum' is set to 0.
615 * 'pnum' is set to the number of sectors (including and immediately following
616 * the specified sector) that are known to be in the same
617 * allocated/unallocated state.
619 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
620 * beyond the end of the disk image it will be clamped.
622 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
624 int nb_sectors
, int *pnum
)
626 off_t start
, data
, hole
;
634 start
= sector_num
* BDRV_SECTOR_SIZE
;
638 BDRVRawState
*s
= bs
->opaque
;
641 struct fiemap_extent fe
;
644 f
.fm
.fm_start
= start
;
645 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
647 f
.fm
.fm_extent_count
= 1;
648 f
.fm
.fm_reserved
= 0;
649 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
650 /* Assume everything is allocated. */
655 if (f
.fm
.fm_mapped_extents
== 0) {
656 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
657 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
659 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
660 hole
= f
.fm
.fm_start
;
661 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
663 data
= f
.fe
.fe_logical
;
664 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
667 #elif defined SEEK_HOLE && defined SEEK_DATA
669 BDRVRawState
*s
= bs
->opaque
;
671 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
673 /* -ENXIO indicates that sector_num was past the end of the file.
674 * There is a virtual hole there. */
675 assert(errno
!= -ENXIO
);
677 /* Most likely EINVAL. Assume everything is allocated. */
685 /* On a hole. We need another syscall to find its end. */
686 data
= lseek(s
->fd
, start
, SEEK_DATA
);
688 data
= lseek(s
->fd
, 0, SEEK_END
);
697 /* On a data extent, compute sectors to the end of the extent. */
698 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
701 /* On a hole, compute sectors to the beginning of the next extent. */
702 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
708 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
710 struct xfs_flock64 fl
;
712 memset(&fl
, 0, sizeof(fl
));
713 fl
.l_whence
= SEEK_SET
;
714 fl
.l_start
= sector_num
<< 9;
715 fl
.l_len
= (int64_t)nb_sectors
<< 9;
717 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
718 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
726 static coroutine_fn
int raw_co_discard(BlockDriverState
*bs
,
727 int64_t sector_num
, int nb_sectors
)
730 BDRVRawState
*s
= bs
->opaque
;
733 return xfs_discard(s
, sector_num
, nb_sectors
);
740 static QEMUOptionParameter raw_create_options
[] = {
742 .name
= BLOCK_OPT_SIZE
,
744 .help
= "Virtual disk size"
749 static BlockDriver bdrv_file
= {
750 .format_name
= "file",
751 .protocol_name
= "file",
752 .instance_size
= sizeof(BDRVRawState
),
753 .bdrv_probe
= NULL
, /* no probe for protocols */
754 .bdrv_file_open
= raw_open
,
755 .bdrv_close
= raw_close
,
756 .bdrv_create
= raw_create
,
757 .bdrv_co_discard
= raw_co_discard
,
758 .bdrv_co_is_allocated
= raw_co_is_allocated
,
760 .bdrv_aio_readv
= raw_aio_readv
,
761 .bdrv_aio_writev
= raw_aio_writev
,
762 .bdrv_aio_flush
= raw_aio_flush
,
764 .bdrv_truncate
= raw_truncate
,
765 .bdrv_getlength
= raw_getlength
,
766 .bdrv_get_allocated_file_size
767 = raw_get_allocated_file_size
,
769 .create_options
= raw_create_options
,
772 /***********************************************/
775 #if defined(__APPLE__) && defined(__MACH__)
776 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
777 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
779 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
781 kern_return_t kernResult
;
782 mach_port_t masterPort
;
783 CFMutableDictionaryRef classesToMatch
;
785 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
786 if ( KERN_SUCCESS
!= kernResult
) {
787 printf( "IOMasterPort returned %d\n", kernResult
);
790 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
791 if ( classesToMatch
== NULL
) {
792 printf( "IOServiceMatching returned a NULL dictionary.\n" );
794 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
796 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
797 if ( KERN_SUCCESS
!= kernResult
)
799 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
805 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
807 io_object_t nextMedia
;
808 kern_return_t kernResult
= KERN_FAILURE
;
810 nextMedia
= IOIteratorNext( mediaIterator
);
813 CFTypeRef bsdPathAsCFString
;
814 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
815 if ( bsdPathAsCFString
) {
816 size_t devPathLength
;
817 strcpy( bsdPath
, _PATH_DEV
);
818 strcat( bsdPath
, "r" );
819 devPathLength
= strlen( bsdPath
);
820 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
821 kernResult
= KERN_SUCCESS
;
823 CFRelease( bsdPathAsCFString
);
825 IOObjectRelease( nextMedia
);
833 static int hdev_probe_device(const char *filename
)
837 /* allow a dedicated CD-ROM driver to match with a higher priority */
838 if (strstart(filename
, "/dev/cdrom", NULL
))
841 if (stat(filename
, &st
) >= 0 &&
842 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
849 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
851 BDRVRawState
*s
= bs
->opaque
;
853 #if defined(__APPLE__) && defined(__MACH__)
854 if (strstart(filename
, "/dev/cdrom", NULL
)) {
855 kern_return_t kernResult
;
856 io_iterator_t mediaIterator
;
857 char bsdPath
[ MAXPATHLEN
];
860 kernResult
= FindEjectableCDMedia( &mediaIterator
);
861 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
863 if ( bsdPath
[ 0 ] != '\0' ) {
864 strcat(bsdPath
,"s0");
865 /* some CDs don't have a partition 0 */
866 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
868 bsdPath
[strlen(bsdPath
)-1] = '1';
876 IOObjectRelease( mediaIterator
);
880 s
->type
= FTYPE_FILE
;
881 #if defined(__linux__)
883 char resolved_path
[ MAXPATHLEN
], *temp
;
885 temp
= realpath(filename
, resolved_path
);
886 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
892 return raw_open_common(bs
, filename
, flags
, 0);
895 #if defined(__linux__)
896 /* Note: we do not have a reliable method to detect if the floppy is
897 present. The current method is to try to open the floppy at every
898 I/O and to keep it opened during a few hundreds of ms. */
899 static int fd_open(BlockDriverState
*bs
)
901 BDRVRawState
*s
= bs
->opaque
;
902 int last_media_present
;
904 if (s
->type
!= FTYPE_FD
)
906 last_media_present
= (s
->fd
>= 0);
908 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
912 printf("Floppy closed\n");
916 if (s
->fd_got_error
&&
917 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
919 printf("No floppy (open delayed)\n");
923 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
925 s
->fd_error_time
= get_clock();
927 if (last_media_present
)
928 s
->fd_media_changed
= 1;
930 printf("No floppy\n");
935 printf("Floppy opened\n");
938 if (!last_media_present
)
939 s
->fd_media_changed
= 1;
940 s
->fd_open_time
= get_clock();
945 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
947 BDRVRawState
*s
= bs
->opaque
;
949 return ioctl(s
->fd
, req
, buf
);
952 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
953 unsigned long int req
, void *buf
,
954 BlockDriverCompletionFunc
*cb
, void *opaque
)
956 BDRVRawState
*s
= bs
->opaque
;
960 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
963 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
964 static int fd_open(BlockDriverState
*bs
)
966 BDRVRawState
*s
= bs
->opaque
;
968 /* this is just to ensure s->fd is sane (its called by io ops) */
973 #else /* !linux && !FreeBSD */
975 static int fd_open(BlockDriverState
*bs
)
980 #endif /* !linux && !FreeBSD */
982 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
986 struct stat stat_buf
;
987 int64_t total_size
= 0;
989 /* Read out options */
990 while (options
&& options
->name
) {
991 if (!strcmp(options
->name
, "size")) {
992 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
997 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1001 if (fstat(fd
, &stat_buf
) < 0)
1003 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1005 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1012 static int hdev_has_zero_init(BlockDriverState
*bs
)
1017 static BlockDriver bdrv_host_device
= {
1018 .format_name
= "host_device",
1019 .protocol_name
= "host_device",
1020 .instance_size
= sizeof(BDRVRawState
),
1021 .bdrv_probe_device
= hdev_probe_device
,
1022 .bdrv_file_open
= hdev_open
,
1023 .bdrv_close
= raw_close
,
1024 .bdrv_create
= hdev_create
,
1025 .create_options
= raw_create_options
,
1026 .bdrv_has_zero_init
= hdev_has_zero_init
,
1028 .bdrv_aio_readv
= raw_aio_readv
,
1029 .bdrv_aio_writev
= raw_aio_writev
,
1030 .bdrv_aio_flush
= raw_aio_flush
,
1032 .bdrv_truncate
= raw_truncate
,
1033 .bdrv_getlength
= raw_getlength
,
1034 .bdrv_get_allocated_file_size
1035 = raw_get_allocated_file_size
,
1037 /* generic scsi device */
1039 .bdrv_ioctl
= hdev_ioctl
,
1040 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1045 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1047 BDRVRawState
*s
= bs
->opaque
;
1052 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1053 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1057 /* close fd so that we can reopen it as needed */
1060 s
->fd_media_changed
= 1;
1065 static int floppy_probe_device(const char *filename
)
1069 struct floppy_struct fdparam
;
1072 if (strstart(filename
, "/dev/fd", NULL
) &&
1073 !strstart(filename
, "/dev/fdset/", NULL
)) {
1077 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1081 ret
= fstat(fd
, &st
);
1082 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1086 /* Attempt to detect via a floppy specific ioctl */
1087 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1098 static int floppy_is_inserted(BlockDriverState
*bs
)
1100 return fd_open(bs
) >= 0;
1103 static int floppy_media_changed(BlockDriverState
*bs
)
1105 BDRVRawState
*s
= bs
->opaque
;
1109 * XXX: we do not have a true media changed indication.
1110 * It does not work if the floppy is changed without trying to read it.
1113 ret
= s
->fd_media_changed
;
1114 s
->fd_media_changed
= 0;
1116 printf("Floppy changed=%d\n", ret
);
1121 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1123 BDRVRawState
*s
= bs
->opaque
;
1130 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1132 if (ioctl(fd
, FDEJECT
, 0) < 0)
1138 static BlockDriver bdrv_host_floppy
= {
1139 .format_name
= "host_floppy",
1140 .protocol_name
= "host_floppy",
1141 .instance_size
= sizeof(BDRVRawState
),
1142 .bdrv_probe_device
= floppy_probe_device
,
1143 .bdrv_file_open
= floppy_open
,
1144 .bdrv_close
= raw_close
,
1145 .bdrv_create
= hdev_create
,
1146 .create_options
= raw_create_options
,
1147 .bdrv_has_zero_init
= hdev_has_zero_init
,
1149 .bdrv_aio_readv
= raw_aio_readv
,
1150 .bdrv_aio_writev
= raw_aio_writev
,
1151 .bdrv_aio_flush
= raw_aio_flush
,
1153 .bdrv_truncate
= raw_truncate
,
1154 .bdrv_getlength
= raw_getlength
,
1155 .bdrv_get_allocated_file_size
1156 = raw_get_allocated_file_size
,
1158 /* removable device support */
1159 .bdrv_is_inserted
= floppy_is_inserted
,
1160 .bdrv_media_changed
= floppy_media_changed
,
1161 .bdrv_eject
= floppy_eject
,
1164 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1166 BDRVRawState
*s
= bs
->opaque
;
1170 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1171 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1174 static int cdrom_probe_device(const char *filename
)
1180 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1184 ret
= fstat(fd
, &st
);
1185 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1189 /* Attempt to detect via a CDROM specific ioctl */
1190 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1200 static int cdrom_is_inserted(BlockDriverState
*bs
)
1202 BDRVRawState
*s
= bs
->opaque
;
1205 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1206 if (ret
== CDS_DISC_OK
)
1211 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1213 BDRVRawState
*s
= bs
->opaque
;
1216 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1217 perror("CDROMEJECT");
1219 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1220 perror("CDROMEJECT");
1224 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1226 BDRVRawState
*s
= bs
->opaque
;
1228 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1230 * Note: an error can happen if the distribution automatically
1233 /* perror("CDROM_LOCKDOOR"); */
1237 static BlockDriver bdrv_host_cdrom
= {
1238 .format_name
= "host_cdrom",
1239 .protocol_name
= "host_cdrom",
1240 .instance_size
= sizeof(BDRVRawState
),
1241 .bdrv_probe_device
= cdrom_probe_device
,
1242 .bdrv_file_open
= cdrom_open
,
1243 .bdrv_close
= raw_close
,
1244 .bdrv_create
= hdev_create
,
1245 .create_options
= raw_create_options
,
1246 .bdrv_has_zero_init
= hdev_has_zero_init
,
1248 .bdrv_aio_readv
= raw_aio_readv
,
1249 .bdrv_aio_writev
= raw_aio_writev
,
1250 .bdrv_aio_flush
= raw_aio_flush
,
1252 .bdrv_truncate
= raw_truncate
,
1253 .bdrv_getlength
= raw_getlength
,
1254 .bdrv_get_allocated_file_size
1255 = raw_get_allocated_file_size
,
1257 /* removable device support */
1258 .bdrv_is_inserted
= cdrom_is_inserted
,
1259 .bdrv_eject
= cdrom_eject
,
1260 .bdrv_lock_medium
= cdrom_lock_medium
,
1262 /* generic scsi device */
1263 .bdrv_ioctl
= hdev_ioctl
,
1264 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1266 #endif /* __linux__ */
1268 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1269 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1271 BDRVRawState
*s
= bs
->opaque
;
1276 ret
= raw_open_common(bs
, filename
, flags
, 0);
1280 /* make sure the door isn't locked at this time */
1281 ioctl(s
->fd
, CDIOCALLOW
);
1285 static int cdrom_probe_device(const char *filename
)
1287 if (strstart(filename
, "/dev/cd", NULL
) ||
1288 strstart(filename
, "/dev/acd", NULL
))
1293 static int cdrom_reopen(BlockDriverState
*bs
)
1295 BDRVRawState
*s
= bs
->opaque
;
1299 * Force reread of possibly changed/newly loaded disc,
1300 * FreeBSD seems to not notice sometimes...
1304 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1311 /* make sure the door isn't locked at this time */
1312 ioctl(s
->fd
, CDIOCALLOW
);
1316 static int cdrom_is_inserted(BlockDriverState
*bs
)
1318 return raw_getlength(bs
) > 0;
1321 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1323 BDRVRawState
*s
= bs
->opaque
;
1328 (void) ioctl(s
->fd
, CDIOCALLOW
);
1331 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1332 perror("CDIOCEJECT");
1334 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1335 perror("CDIOCCLOSE");
1341 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1343 BDRVRawState
*s
= bs
->opaque
;
1347 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1349 * Note: an error can happen if the distribution automatically
1352 /* perror("CDROM_LOCKDOOR"); */
1356 static BlockDriver bdrv_host_cdrom
= {
1357 .format_name
= "host_cdrom",
1358 .protocol_name
= "host_cdrom",
1359 .instance_size
= sizeof(BDRVRawState
),
1360 .bdrv_probe_device
= cdrom_probe_device
,
1361 .bdrv_file_open
= cdrom_open
,
1362 .bdrv_close
= raw_close
,
1363 .bdrv_create
= hdev_create
,
1364 .create_options
= raw_create_options
,
1365 .bdrv_has_zero_init
= hdev_has_zero_init
,
1367 .bdrv_aio_readv
= raw_aio_readv
,
1368 .bdrv_aio_writev
= raw_aio_writev
,
1369 .bdrv_aio_flush
= raw_aio_flush
,
1371 .bdrv_truncate
= raw_truncate
,
1372 .bdrv_getlength
= raw_getlength
,
1373 .bdrv_get_allocated_file_size
1374 = raw_get_allocated_file_size
,
1376 /* removable device support */
1377 .bdrv_is_inserted
= cdrom_is_inserted
,
1378 .bdrv_eject
= cdrom_eject
,
1379 .bdrv_lock_medium
= cdrom_lock_medium
,
1381 #endif /* __FreeBSD__ */
1383 static void bdrv_file_init(void)
1386 * Register all the drivers. Note that order is important, the driver
1387 * registered last will get probed first.
1389 bdrv_register(&bdrv_file
);
1390 bdrv_register(&bdrv_host_device
);
1392 bdrv_register(&bdrv_host_floppy
);
1393 bdrv_register(&bdrv_host_cdrom
);
1395 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1396 bdrv_register(&bdrv_host_cdrom
);
1400 block_init(bdrv_file_init
);