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 void raw_parse_flags(int bdrv_flags
, int *open_flags
)
190 assert(open_flags
!= NULL
);
192 *open_flags
|= O_BINARY
;
193 *open_flags
&= ~O_ACCMODE
;
194 if (bdrv_flags
& BDRV_O_RDWR
) {
195 *open_flags
|= O_RDWR
;
197 *open_flags
|= O_RDONLY
;
200 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
201 * and O_DIRECT for no caching. */
202 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
203 *open_flags
|= O_DIRECT
;
207 #ifdef CONFIG_LINUX_AIO
208 static int raw_set_aio(void **aio_ctx
, int *use_aio
, int bdrv_flags
)
211 assert(aio_ctx
!= NULL
);
212 assert(use_aio
!= NULL
);
214 * Currently Linux do AIO only for files opened with O_DIRECT
215 * specified so check NOCACHE flag too
217 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
218 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
220 /* if non-NULL, laio_init() has already been run */
221 if (*aio_ctx
== NULL
) {
222 *aio_ctx
= laio_init();
239 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
240 int bdrv_flags
, int open_flags
)
242 BDRVRawState
*s
= bs
->opaque
;
245 ret
= raw_normalize_devicepath(&filename
);
250 s
->open_flags
= open_flags
;
251 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
254 fd
= qemu_open(filename
, s
->open_flags
, 0644);
262 s
->aligned_buf
= NULL
;
264 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
266 * Allocate a buffer for read/modify/write cycles. Chose the size
267 * pessimistically as we don't know the block size yet.
269 s
->aligned_buf_size
= 32 * MAX_BLOCKSIZE
;
270 s
->aligned_buf
= qemu_memalign(MAX_BLOCKSIZE
, s
->aligned_buf_size
);
271 if (s
->aligned_buf
== NULL
) {
276 /* We're falling back to POSIX AIO in some cases so init always */
277 if (paio_init() < 0) {
281 #ifdef CONFIG_LINUX_AIO
282 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
288 if (platform_test_xfs_fd(s
->fd
)) {
296 qemu_vfree(s
->aligned_buf
);
302 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
304 BDRVRawState
*s
= bs
->opaque
;
306 s
->type
= FTYPE_FILE
;
307 return raw_open_common(bs
, filename
, flags
, 0);
310 /* XXX: use host sector size if necessary with:
311 #ifdef DIOCGSECTORSIZE
313 unsigned int sectorsize = 512;
314 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
315 sectorsize > bufsize)
316 bufsize = sectorsize;
320 uint32_t blockSize = 512;
321 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
328 * Check if all memory in this vector is sector aligned.
330 static int qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
334 for (i
= 0; i
< qiov
->niov
; i
++) {
335 if ((uintptr_t) qiov
->iov
[i
].iov_base
% bs
->buffer_alignment
) {
343 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
344 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
345 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
347 BDRVRawState
*s
= bs
->opaque
;
353 * If O_DIRECT is used the buffer needs to be aligned on a sector
354 * boundary. Check if this is the case or tell the low-level
355 * driver that it needs to copy the buffer.
357 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
358 if (!qiov_is_aligned(bs
, qiov
)) {
359 type
|= QEMU_AIO_MISALIGNED
;
360 #ifdef CONFIG_LINUX_AIO
361 } else if (s
->use_aio
) {
362 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
363 nb_sectors
, cb
, opaque
, type
);
368 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
372 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
373 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
374 BlockDriverCompletionFunc
*cb
, void *opaque
)
376 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
377 cb
, opaque
, QEMU_AIO_READ
);
380 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
381 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
382 BlockDriverCompletionFunc
*cb
, void *opaque
)
384 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
385 cb
, opaque
, QEMU_AIO_WRITE
);
388 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
389 BlockDriverCompletionFunc
*cb
, void *opaque
)
391 BDRVRawState
*s
= bs
->opaque
;
396 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
399 static void raw_close(BlockDriverState
*bs
)
401 BDRVRawState
*s
= bs
->opaque
;
405 if (s
->aligned_buf
!= NULL
)
406 qemu_vfree(s
->aligned_buf
);
410 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
412 BDRVRawState
*s
= bs
->opaque
;
415 if (fstat(s
->fd
, &st
)) {
419 if (S_ISREG(st
.st_mode
)) {
420 if (ftruncate(s
->fd
, offset
) < 0) {
423 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
424 if (offset
> raw_getlength(bs
)) {
435 static int64_t raw_getlength(BlockDriverState
*bs
)
437 BDRVRawState
*s
= bs
->opaque
;
443 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
446 if (ioctl(fd
, DIOCGDINFO
, &dl
))
448 return (uint64_t)dl
.d_secsize
*
449 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
453 #elif defined(__NetBSD__)
454 static int64_t raw_getlength(BlockDriverState
*bs
)
456 BDRVRawState
*s
= bs
->opaque
;
462 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
463 struct dkwedge_info dkw
;
465 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
466 return dkw
.dkw_size
* 512;
470 if (ioctl(fd
, DIOCGDINFO
, &dl
))
472 return (uint64_t)dl
.d_secsize
*
473 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
478 #elif defined(__sun__)
479 static int64_t raw_getlength(BlockDriverState
*bs
)
481 BDRVRawState
*s
= bs
->opaque
;
482 struct dk_minfo minfo
;
491 * Use the DKIOCGMEDIAINFO ioctl to read the size.
493 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
495 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
499 * There are reports that lseek on some devices fails, but
500 * irc discussion said that contingency on contingency was overkill.
502 return lseek(s
->fd
, 0, SEEK_END
);
504 #elif defined(CONFIG_BSD)
505 static int64_t raw_getlength(BlockDriverState
*bs
)
507 BDRVRawState
*s
= bs
->opaque
;
511 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
520 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
523 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
524 #ifdef DIOCGMEDIASIZE
525 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
526 #elif defined(DIOCGPART)
529 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
530 size
= pi
.media_size
;
536 #if defined(__APPLE__) && defined(__MACH__)
537 size
= LONG_LONG_MAX
;
539 size
= lseek(fd
, 0LL, SEEK_END
);
541 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
544 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
545 if (size
== 2048LL * (unsigned)-1)
547 /* XXX no disc? maybe we need to reopen... */
548 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
555 size
= lseek(fd
, 0, SEEK_END
);
560 static int64_t raw_getlength(BlockDriverState
*bs
)
562 BDRVRawState
*s
= bs
->opaque
;
570 return lseek(s
->fd
, 0, SEEK_END
);
574 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
577 BDRVRawState
*s
= bs
->opaque
;
579 if (fstat(s
->fd
, &st
) < 0) {
582 return (int64_t)st
.st_blocks
* 512;
585 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
589 int64_t total_size
= 0;
591 /* Read out options */
592 while (options
&& options
->name
) {
593 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
594 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
599 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
604 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
607 if (qemu_close(fd
) != 0) {
615 * Returns true iff the specified sector is present in the disk image. Drivers
616 * not implementing the functionality are assumed to not support backing files,
617 * hence all their sectors are reported as allocated.
619 * If 'sector_num' is beyond the end of the disk image the return value is 0
620 * and 'pnum' is set to 0.
622 * 'pnum' is set to the number of sectors (including and immediately following
623 * the specified sector) that are known to be in the same
624 * allocated/unallocated state.
626 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
627 * beyond the end of the disk image it will be clamped.
629 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
631 int nb_sectors
, int *pnum
)
633 off_t start
, data
, hole
;
641 start
= sector_num
* BDRV_SECTOR_SIZE
;
645 BDRVRawState
*s
= bs
->opaque
;
648 struct fiemap_extent fe
;
651 f
.fm
.fm_start
= start
;
652 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
654 f
.fm
.fm_extent_count
= 1;
655 f
.fm
.fm_reserved
= 0;
656 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
657 /* Assume everything is allocated. */
662 if (f
.fm
.fm_mapped_extents
== 0) {
663 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
664 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
666 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
667 hole
= f
.fm
.fm_start
;
668 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
670 data
= f
.fe
.fe_logical
;
671 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
674 #elif defined SEEK_HOLE && defined SEEK_DATA
676 BDRVRawState
*s
= bs
->opaque
;
678 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
680 /* -ENXIO indicates that sector_num was past the end of the file.
681 * There is a virtual hole there. */
682 assert(errno
!= -ENXIO
);
684 /* Most likely EINVAL. Assume everything is allocated. */
692 /* On a hole. We need another syscall to find its end. */
693 data
= lseek(s
->fd
, start
, SEEK_DATA
);
695 data
= lseek(s
->fd
, 0, SEEK_END
);
704 /* On a data extent, compute sectors to the end of the extent. */
705 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
708 /* On a hole, compute sectors to the beginning of the next extent. */
709 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
715 static int xfs_discard(BDRVRawState
*s
, int64_t sector_num
, int nb_sectors
)
717 struct xfs_flock64 fl
;
719 memset(&fl
, 0, sizeof(fl
));
720 fl
.l_whence
= SEEK_SET
;
721 fl
.l_start
= sector_num
<< 9;
722 fl
.l_len
= (int64_t)nb_sectors
<< 9;
724 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
725 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
733 static coroutine_fn
int raw_co_discard(BlockDriverState
*bs
,
734 int64_t sector_num
, int nb_sectors
)
737 BDRVRawState
*s
= bs
->opaque
;
740 return xfs_discard(s
, sector_num
, nb_sectors
);
747 static QEMUOptionParameter raw_create_options
[] = {
749 .name
= BLOCK_OPT_SIZE
,
751 .help
= "Virtual disk size"
756 static BlockDriver bdrv_file
= {
757 .format_name
= "file",
758 .protocol_name
= "file",
759 .instance_size
= sizeof(BDRVRawState
),
760 .bdrv_probe
= NULL
, /* no probe for protocols */
761 .bdrv_file_open
= raw_open
,
762 .bdrv_close
= raw_close
,
763 .bdrv_create
= raw_create
,
764 .bdrv_co_discard
= raw_co_discard
,
765 .bdrv_co_is_allocated
= raw_co_is_allocated
,
767 .bdrv_aio_readv
= raw_aio_readv
,
768 .bdrv_aio_writev
= raw_aio_writev
,
769 .bdrv_aio_flush
= raw_aio_flush
,
771 .bdrv_truncate
= raw_truncate
,
772 .bdrv_getlength
= raw_getlength
,
773 .bdrv_get_allocated_file_size
774 = raw_get_allocated_file_size
,
776 .create_options
= raw_create_options
,
779 /***********************************************/
782 #if defined(__APPLE__) && defined(__MACH__)
783 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
784 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
786 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
788 kern_return_t kernResult
;
789 mach_port_t masterPort
;
790 CFMutableDictionaryRef classesToMatch
;
792 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
793 if ( KERN_SUCCESS
!= kernResult
) {
794 printf( "IOMasterPort returned %d\n", kernResult
);
797 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
798 if ( classesToMatch
== NULL
) {
799 printf( "IOServiceMatching returned a NULL dictionary.\n" );
801 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
803 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
804 if ( KERN_SUCCESS
!= kernResult
)
806 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
812 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
814 io_object_t nextMedia
;
815 kern_return_t kernResult
= KERN_FAILURE
;
817 nextMedia
= IOIteratorNext( mediaIterator
);
820 CFTypeRef bsdPathAsCFString
;
821 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
822 if ( bsdPathAsCFString
) {
823 size_t devPathLength
;
824 strcpy( bsdPath
, _PATH_DEV
);
825 strcat( bsdPath
, "r" );
826 devPathLength
= strlen( bsdPath
);
827 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
828 kernResult
= KERN_SUCCESS
;
830 CFRelease( bsdPathAsCFString
);
832 IOObjectRelease( nextMedia
);
840 static int hdev_probe_device(const char *filename
)
844 /* allow a dedicated CD-ROM driver to match with a higher priority */
845 if (strstart(filename
, "/dev/cdrom", NULL
))
848 if (stat(filename
, &st
) >= 0 &&
849 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
856 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
858 BDRVRawState
*s
= bs
->opaque
;
860 #if defined(__APPLE__) && defined(__MACH__)
861 if (strstart(filename
, "/dev/cdrom", NULL
)) {
862 kern_return_t kernResult
;
863 io_iterator_t mediaIterator
;
864 char bsdPath
[ MAXPATHLEN
];
867 kernResult
= FindEjectableCDMedia( &mediaIterator
);
868 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
870 if ( bsdPath
[ 0 ] != '\0' ) {
871 strcat(bsdPath
,"s0");
872 /* some CDs don't have a partition 0 */
873 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
875 bsdPath
[strlen(bsdPath
)-1] = '1';
883 IOObjectRelease( mediaIterator
);
887 s
->type
= FTYPE_FILE
;
888 #if defined(__linux__)
890 char resolved_path
[ MAXPATHLEN
], *temp
;
892 temp
= realpath(filename
, resolved_path
);
893 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
899 return raw_open_common(bs
, filename
, flags
, 0);
902 #if defined(__linux__)
903 /* Note: we do not have a reliable method to detect if the floppy is
904 present. The current method is to try to open the floppy at every
905 I/O and to keep it opened during a few hundreds of ms. */
906 static int fd_open(BlockDriverState
*bs
)
908 BDRVRawState
*s
= bs
->opaque
;
909 int last_media_present
;
911 if (s
->type
!= FTYPE_FD
)
913 last_media_present
= (s
->fd
>= 0);
915 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
919 printf("Floppy closed\n");
923 if (s
->fd_got_error
&&
924 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
926 printf("No floppy (open delayed)\n");
930 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
932 s
->fd_error_time
= get_clock();
934 if (last_media_present
)
935 s
->fd_media_changed
= 1;
937 printf("No floppy\n");
942 printf("Floppy opened\n");
945 if (!last_media_present
)
946 s
->fd_media_changed
= 1;
947 s
->fd_open_time
= get_clock();
952 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
954 BDRVRawState
*s
= bs
->opaque
;
956 return ioctl(s
->fd
, req
, buf
);
959 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
960 unsigned long int req
, void *buf
,
961 BlockDriverCompletionFunc
*cb
, void *opaque
)
963 BDRVRawState
*s
= bs
->opaque
;
967 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
970 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
971 static int fd_open(BlockDriverState
*bs
)
973 BDRVRawState
*s
= bs
->opaque
;
975 /* this is just to ensure s->fd is sane (its called by io ops) */
980 #else /* !linux && !FreeBSD */
982 static int fd_open(BlockDriverState
*bs
)
987 #endif /* !linux && !FreeBSD */
989 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
993 struct stat stat_buf
;
994 int64_t total_size
= 0;
996 /* Read out options */
997 while (options
&& options
->name
) {
998 if (!strcmp(options
->name
, "size")) {
999 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1004 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1008 if (fstat(fd
, &stat_buf
) < 0)
1010 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1012 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1019 static int hdev_has_zero_init(BlockDriverState
*bs
)
1024 static BlockDriver bdrv_host_device
= {
1025 .format_name
= "host_device",
1026 .protocol_name
= "host_device",
1027 .instance_size
= sizeof(BDRVRawState
),
1028 .bdrv_probe_device
= hdev_probe_device
,
1029 .bdrv_file_open
= hdev_open
,
1030 .bdrv_close
= raw_close
,
1031 .bdrv_create
= hdev_create
,
1032 .create_options
= raw_create_options
,
1033 .bdrv_has_zero_init
= hdev_has_zero_init
,
1035 .bdrv_aio_readv
= raw_aio_readv
,
1036 .bdrv_aio_writev
= raw_aio_writev
,
1037 .bdrv_aio_flush
= raw_aio_flush
,
1039 .bdrv_truncate
= raw_truncate
,
1040 .bdrv_getlength
= raw_getlength
,
1041 .bdrv_get_allocated_file_size
1042 = raw_get_allocated_file_size
,
1044 /* generic scsi device */
1046 .bdrv_ioctl
= hdev_ioctl
,
1047 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1052 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1054 BDRVRawState
*s
= bs
->opaque
;
1059 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1060 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1064 /* close fd so that we can reopen it as needed */
1067 s
->fd_media_changed
= 1;
1072 static int floppy_probe_device(const char *filename
)
1076 struct floppy_struct fdparam
;
1079 if (strstart(filename
, "/dev/fd", NULL
) &&
1080 !strstart(filename
, "/dev/fdset/", NULL
)) {
1084 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1088 ret
= fstat(fd
, &st
);
1089 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1093 /* Attempt to detect via a floppy specific ioctl */
1094 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1105 static int floppy_is_inserted(BlockDriverState
*bs
)
1107 return fd_open(bs
) >= 0;
1110 static int floppy_media_changed(BlockDriverState
*bs
)
1112 BDRVRawState
*s
= bs
->opaque
;
1116 * XXX: we do not have a true media changed indication.
1117 * It does not work if the floppy is changed without trying to read it.
1120 ret
= s
->fd_media_changed
;
1121 s
->fd_media_changed
= 0;
1123 printf("Floppy changed=%d\n", ret
);
1128 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1130 BDRVRawState
*s
= bs
->opaque
;
1137 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1139 if (ioctl(fd
, FDEJECT
, 0) < 0)
1145 static BlockDriver bdrv_host_floppy
= {
1146 .format_name
= "host_floppy",
1147 .protocol_name
= "host_floppy",
1148 .instance_size
= sizeof(BDRVRawState
),
1149 .bdrv_probe_device
= floppy_probe_device
,
1150 .bdrv_file_open
= floppy_open
,
1151 .bdrv_close
= raw_close
,
1152 .bdrv_create
= hdev_create
,
1153 .create_options
= raw_create_options
,
1154 .bdrv_has_zero_init
= hdev_has_zero_init
,
1156 .bdrv_aio_readv
= raw_aio_readv
,
1157 .bdrv_aio_writev
= raw_aio_writev
,
1158 .bdrv_aio_flush
= raw_aio_flush
,
1160 .bdrv_truncate
= raw_truncate
,
1161 .bdrv_getlength
= raw_getlength
,
1162 .bdrv_get_allocated_file_size
1163 = raw_get_allocated_file_size
,
1165 /* removable device support */
1166 .bdrv_is_inserted
= floppy_is_inserted
,
1167 .bdrv_media_changed
= floppy_media_changed
,
1168 .bdrv_eject
= floppy_eject
,
1171 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1173 BDRVRawState
*s
= bs
->opaque
;
1177 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1178 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1181 static int cdrom_probe_device(const char *filename
)
1187 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1191 ret
= fstat(fd
, &st
);
1192 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1196 /* Attempt to detect via a CDROM specific ioctl */
1197 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1207 static int cdrom_is_inserted(BlockDriverState
*bs
)
1209 BDRVRawState
*s
= bs
->opaque
;
1212 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1213 if (ret
== CDS_DISC_OK
)
1218 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1220 BDRVRawState
*s
= bs
->opaque
;
1223 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1224 perror("CDROMEJECT");
1226 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1227 perror("CDROMEJECT");
1231 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1233 BDRVRawState
*s
= bs
->opaque
;
1235 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1237 * Note: an error can happen if the distribution automatically
1240 /* perror("CDROM_LOCKDOOR"); */
1244 static BlockDriver bdrv_host_cdrom
= {
1245 .format_name
= "host_cdrom",
1246 .protocol_name
= "host_cdrom",
1247 .instance_size
= sizeof(BDRVRawState
),
1248 .bdrv_probe_device
= cdrom_probe_device
,
1249 .bdrv_file_open
= cdrom_open
,
1250 .bdrv_close
= raw_close
,
1251 .bdrv_create
= hdev_create
,
1252 .create_options
= raw_create_options
,
1253 .bdrv_has_zero_init
= hdev_has_zero_init
,
1255 .bdrv_aio_readv
= raw_aio_readv
,
1256 .bdrv_aio_writev
= raw_aio_writev
,
1257 .bdrv_aio_flush
= raw_aio_flush
,
1259 .bdrv_truncate
= raw_truncate
,
1260 .bdrv_getlength
= raw_getlength
,
1261 .bdrv_get_allocated_file_size
1262 = raw_get_allocated_file_size
,
1264 /* removable device support */
1265 .bdrv_is_inserted
= cdrom_is_inserted
,
1266 .bdrv_eject
= cdrom_eject
,
1267 .bdrv_lock_medium
= cdrom_lock_medium
,
1269 /* generic scsi device */
1270 .bdrv_ioctl
= hdev_ioctl
,
1271 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1273 #endif /* __linux__ */
1275 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1276 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1278 BDRVRawState
*s
= bs
->opaque
;
1283 ret
= raw_open_common(bs
, filename
, flags
, 0);
1287 /* make sure the door isn't locked at this time */
1288 ioctl(s
->fd
, CDIOCALLOW
);
1292 static int cdrom_probe_device(const char *filename
)
1294 if (strstart(filename
, "/dev/cd", NULL
) ||
1295 strstart(filename
, "/dev/acd", NULL
))
1300 static int cdrom_reopen(BlockDriverState
*bs
)
1302 BDRVRawState
*s
= bs
->opaque
;
1306 * Force reread of possibly changed/newly loaded disc,
1307 * FreeBSD seems to not notice sometimes...
1311 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1318 /* make sure the door isn't locked at this time */
1319 ioctl(s
->fd
, CDIOCALLOW
);
1323 static int cdrom_is_inserted(BlockDriverState
*bs
)
1325 return raw_getlength(bs
) > 0;
1328 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1330 BDRVRawState
*s
= bs
->opaque
;
1335 (void) ioctl(s
->fd
, CDIOCALLOW
);
1338 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1339 perror("CDIOCEJECT");
1341 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1342 perror("CDIOCCLOSE");
1348 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1350 BDRVRawState
*s
= bs
->opaque
;
1354 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1356 * Note: an error can happen if the distribution automatically
1359 /* perror("CDROM_LOCKDOOR"); */
1363 static BlockDriver bdrv_host_cdrom
= {
1364 .format_name
= "host_cdrom",
1365 .protocol_name
= "host_cdrom",
1366 .instance_size
= sizeof(BDRVRawState
),
1367 .bdrv_probe_device
= cdrom_probe_device
,
1368 .bdrv_file_open
= cdrom_open
,
1369 .bdrv_close
= raw_close
,
1370 .bdrv_create
= hdev_create
,
1371 .create_options
= raw_create_options
,
1372 .bdrv_has_zero_init
= hdev_has_zero_init
,
1374 .bdrv_aio_readv
= raw_aio_readv
,
1375 .bdrv_aio_writev
= raw_aio_writev
,
1376 .bdrv_aio_flush
= raw_aio_flush
,
1378 .bdrv_truncate
= raw_truncate
,
1379 .bdrv_getlength
= raw_getlength
,
1380 .bdrv_get_allocated_file_size
1381 = raw_get_allocated_file_size
,
1383 /* removable device support */
1384 .bdrv_is_inserted
= cdrom_is_inserted
,
1385 .bdrv_eject
= cdrom_eject
,
1386 .bdrv_lock_medium
= cdrom_lock_medium
,
1388 #endif /* __FreeBSD__ */
1390 static void bdrv_file_init(void)
1393 * Register all the drivers. Note that order is important, the driver
1394 * registered last will get probed first.
1396 bdrv_register(&bdrv_file
);
1397 bdrv_register(&bdrv_host_device
);
1399 bdrv_register(&bdrv_host_floppy
);
1400 bdrv_register(&bdrv_host_cdrom
);
1402 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1403 bdrv_register(&bdrv_host_cdrom
);
1407 block_init(bdrv_file_init
);