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"
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
50 #include <sys/ioctl.h>
51 #include <sys/param.h>
52 #include <linux/cdrom.h>
55 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
68 #include <sys/ioctl.h>
69 #include <sys/diskslice.h>
72 //#define DEBUG_FLOPPY
75 #if defined(DEBUG_BLOCK)
76 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
77 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
79 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
82 /* OS X does not have O_DSYNC */
85 #define O_DSYNC O_SYNC
86 #elif defined(O_FSYNC)
87 #define O_DSYNC O_FSYNC
91 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
93 #define O_DIRECT O_DSYNC
100 #define ALIGNED_BUFFER_SIZE (32 * 512)
102 /* if the FD is not accessed during that time (in ms), we try to
103 reopen it to see if the disk has been changed */
104 #define FD_OPEN_TIMEOUT 1000
106 typedef struct BDRVRawState
{
110 #if defined(__linux__)
111 /* linux floppy specific */
112 int64_t fd_open_time
;
113 int64_t fd_error_time
;
115 int fd_media_changed
;
117 #ifdef CONFIG_LINUX_AIO
121 uint8_t* aligned_buf
;
124 static int fd_open(BlockDriverState
*bs
);
125 static int64_t raw_getlength(BlockDriverState
*bs
);
127 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
128 static int cdrom_reopen(BlockDriverState
*bs
);
131 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
132 int bdrv_flags
, int open_flags
)
134 BDRVRawState
*s
= bs
->opaque
;
137 s
->open_flags
= open_flags
| O_BINARY
;
138 s
->open_flags
&= ~O_ACCMODE
;
139 if (bdrv_flags
& BDRV_O_RDWR
) {
140 s
->open_flags
|= O_RDWR
;
142 s
->open_flags
|= O_RDONLY
;
145 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
146 * and O_DIRECT for no caching. */
147 if ((bdrv_flags
& BDRV_O_NOCACHE
))
148 s
->open_flags
|= O_DIRECT
;
149 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
150 s
->open_flags
|= O_DSYNC
;
153 fd
= qemu_open(filename
, s
->open_flags
, 0644);
161 s
->aligned_buf
= NULL
;
163 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
164 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
165 if (s
->aligned_buf
== NULL
) {
170 #ifdef CONFIG_LINUX_AIO
171 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
172 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
174 /* We're falling back to POSIX AIO in some cases */
177 s
->aio_ctx
= laio_init();
185 if (paio_init() < 0) {
188 #ifdef CONFIG_LINUX_AIO
196 qemu_vfree(s
->aligned_buf
);
202 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
204 BDRVRawState
*s
= bs
->opaque
;
206 s
->type
= FTYPE_FILE
;
207 return raw_open_common(bs
, filename
, flags
, 0);
210 /* XXX: use host sector size if necessary with:
211 #ifdef DIOCGSECTORSIZE
213 unsigned int sectorsize = 512;
214 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
215 sectorsize > bufsize)
216 bufsize = sectorsize;
220 uint32_t blockSize = 512;
221 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
228 * offset and count are in bytes, but must be multiples of 512 for files
229 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
231 * This function may be called without alignment if the caller ensures
232 * that O_DIRECT is not in effect.
234 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
235 uint8_t *buf
, int count
)
237 BDRVRawState
*s
= bs
->opaque
;
244 ret
= pread(s
->fd
, buf
, count
, offset
);
248 /* Allow reads beyond the end (needed for pwrite) */
249 if ((ret
== 0) && bs
->growable
) {
250 int64_t size
= raw_getlength(bs
);
251 if (offset
>= size
) {
252 memset(buf
, 0, count
);
257 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
258 "] read failed %d : %d = %s\n",
259 s
->fd
, bs
->filename
, offset
, buf
, count
,
260 bs
->total_sectors
, ret
, errno
, strerror(errno
));
262 /* Try harder for CDrom. */
263 if (s
->type
!= FTYPE_FILE
) {
264 ret
= pread(s
->fd
, buf
, count
, offset
);
267 ret
= pread(s
->fd
, buf
, count
, offset
);
271 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
272 "] retry read failed %d : %d = %s\n",
273 s
->fd
, bs
->filename
, offset
, buf
, count
,
274 bs
->total_sectors
, ret
, errno
, strerror(errno
));
277 return (ret
< 0) ? -errno
: ret
;
281 * offset and count are in bytes, but must be multiples of 512 for files
282 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
284 * This function may be called without alignment if the caller ensures
285 * that O_DIRECT is not in effect.
287 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
288 const uint8_t *buf
, int count
)
290 BDRVRawState
*s
= bs
->opaque
;
297 ret
= pwrite(s
->fd
, buf
, count
, offset
);
301 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
302 "] write failed %d : %d = %s\n",
303 s
->fd
, bs
->filename
, offset
, buf
, count
,
304 bs
->total_sectors
, ret
, errno
, strerror(errno
));
306 return (ret
< 0) ? -errno
: ret
;
311 * offset and count are in bytes and possibly not aligned. For files opened
312 * with O_DIRECT, necessary alignments are ensured before calling
313 * raw_pread_aligned to do the actual read.
315 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
316 uint8_t *buf
, int count
)
318 BDRVRawState
*s
= bs
->opaque
;
319 int size
, ret
, shift
, sum
;
323 if (s
->aligned_buf
!= NULL
) {
325 if (offset
& 0x1ff) {
326 /* align offset on a 512 bytes boundary */
328 shift
= offset
& 0x1ff;
329 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
330 if (size
> ALIGNED_BUFFER_SIZE
)
331 size
= ALIGNED_BUFFER_SIZE
;
332 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
339 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
349 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
351 /* read on aligned buffer */
355 size
= (count
+ 0x1ff) & ~0x1ff;
356 if (size
> ALIGNED_BUFFER_SIZE
)
357 size
= ALIGNED_BUFFER_SIZE
;
359 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
362 } else if (ret
== 0) {
363 fprintf(stderr
, "raw_pread: read beyond end of file\n");
371 memcpy(buf
, s
->aligned_buf
, size
);
383 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
386 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
387 uint8_t *buf
, int nb_sectors
)
391 ret
= raw_pread(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
392 nb_sectors
* BDRV_SECTOR_SIZE
);
393 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
399 * offset and count are in bytes and possibly not aligned. For files opened
400 * with O_DIRECT, necessary alignments are ensured before calling
401 * raw_pwrite_aligned to do the actual write.
403 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
404 const uint8_t *buf
, int count
)
406 BDRVRawState
*s
= bs
->opaque
;
407 int size
, ret
, shift
, sum
;
411 if (s
->aligned_buf
!= NULL
) {
413 if (offset
& 0x1ff) {
414 /* align offset on a 512 bytes boundary */
415 shift
= offset
& 0x1ff;
416 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
423 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
425 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
437 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
439 while ((size
= (count
& ~0x1ff)) != 0) {
441 if (size
> ALIGNED_BUFFER_SIZE
)
442 size
= ALIGNED_BUFFER_SIZE
;
444 memcpy(s
->aligned_buf
, buf
, size
);
446 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
455 /* here, count < 512 because (count & ~0x1ff) == 0 */
457 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
460 memcpy(s
->aligned_buf
, buf
, count
);
462 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
473 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
476 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
477 const uint8_t *buf
, int nb_sectors
)
480 ret
= raw_pwrite(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
481 nb_sectors
* BDRV_SECTOR_SIZE
);
482 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
488 * Check if all memory in this vector is sector aligned.
490 static int qiov_is_aligned(QEMUIOVector
*qiov
)
494 for (i
= 0; i
< qiov
->niov
; i
++) {
495 if ((uintptr_t) qiov
->iov
[i
].iov_base
% BDRV_SECTOR_SIZE
) {
503 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
504 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
505 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
507 BDRVRawState
*s
= bs
->opaque
;
513 * If O_DIRECT is used the buffer needs to be aligned on a sector
514 * boundary. Check if this is the case or telll the low-level
515 * driver that it needs to copy the buffer.
517 if (s
->aligned_buf
) {
518 if (!qiov_is_aligned(qiov
)) {
519 type
|= QEMU_AIO_MISALIGNED
;
520 #ifdef CONFIG_LINUX_AIO
521 } else if (s
->use_aio
) {
522 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
523 nb_sectors
, cb
, opaque
, type
);
528 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
532 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
533 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
534 BlockDriverCompletionFunc
*cb
, void *opaque
)
536 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
537 cb
, opaque
, QEMU_AIO_READ
);
540 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
541 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
542 BlockDriverCompletionFunc
*cb
, void *opaque
)
544 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
545 cb
, opaque
, QEMU_AIO_WRITE
);
548 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
549 BlockDriverCompletionFunc
*cb
, void *opaque
)
551 BDRVRawState
*s
= bs
->opaque
;
556 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
559 static void raw_close(BlockDriverState
*bs
)
561 BDRVRawState
*s
= bs
->opaque
;
565 if (s
->aligned_buf
!= NULL
)
566 qemu_vfree(s
->aligned_buf
);
570 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
572 BDRVRawState
*s
= bs
->opaque
;
573 if (s
->type
!= FTYPE_FILE
)
575 if (ftruncate(s
->fd
, offset
) < 0)
581 static int64_t raw_getlength(BlockDriverState
*bs
)
583 BDRVRawState
*s
= bs
->opaque
;
589 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
592 if (ioctl(fd
, DIOCGDINFO
, &dl
))
594 return (uint64_t)dl
.d_secsize
*
595 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
599 #elif defined(__sun__)
600 static int64_t raw_getlength(BlockDriverState
*bs
)
602 BDRVRawState
*s
= bs
->opaque
;
603 struct dk_minfo minfo
;
612 * Use the DKIOCGMEDIAINFO ioctl to read the size.
614 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
616 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
620 * There are reports that lseek on some devices fails, but
621 * irc discussion said that contingency on contingency was overkill.
623 return lseek(s
->fd
, 0, SEEK_END
);
625 #elif defined(CONFIG_BSD)
626 static int64_t raw_getlength(BlockDriverState
*bs
)
628 BDRVRawState
*s
= bs
->opaque
;
632 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
641 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
644 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
645 #ifdef DIOCGMEDIASIZE
646 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
647 #elif defined(DIOCGPART)
650 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
651 size
= pi
.media_size
;
658 size
= LONG_LONG_MAX
;
660 size
= lseek(fd
, 0LL, SEEK_END
);
662 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
665 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
666 if (size
== 2048LL * (unsigned)-1)
668 /* XXX no disc? maybe we need to reopen... */
669 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
676 size
= lseek(fd
, 0, SEEK_END
);
681 static int64_t raw_getlength(BlockDriverState
*bs
)
683 BDRVRawState
*s
= bs
->opaque
;
691 return lseek(s
->fd
, 0, SEEK_END
);
695 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
699 int64_t total_size
= 0;
701 /* Read out options */
702 while (options
&& options
->name
) {
703 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
704 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
709 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
714 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
717 if (close(fd
) != 0) {
724 static void raw_flush(BlockDriverState
*bs
)
726 BDRVRawState
*s
= bs
->opaque
;
727 qemu_fdatasync(s
->fd
);
731 static QEMUOptionParameter raw_create_options
[] = {
733 .name
= BLOCK_OPT_SIZE
,
735 .help
= "Virtual disk size"
740 static BlockDriver bdrv_file
= {
741 .format_name
= "file",
742 .protocol_name
= "file",
743 .instance_size
= sizeof(BDRVRawState
),
744 .bdrv_probe
= NULL
, /* no probe for protocols */
745 .bdrv_file_open
= raw_open
,
746 .bdrv_read
= raw_read
,
747 .bdrv_write
= raw_write
,
748 .bdrv_close
= raw_close
,
749 .bdrv_create
= raw_create
,
750 .bdrv_flush
= raw_flush
,
752 .bdrv_aio_readv
= raw_aio_readv
,
753 .bdrv_aio_writev
= raw_aio_writev
,
754 .bdrv_aio_flush
= raw_aio_flush
,
756 .bdrv_truncate
= raw_truncate
,
757 .bdrv_getlength
= raw_getlength
,
759 .create_options
= raw_create_options
,
762 /***********************************************/
766 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
767 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
769 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
771 kern_return_t kernResult
;
772 mach_port_t masterPort
;
773 CFMutableDictionaryRef classesToMatch
;
775 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
776 if ( KERN_SUCCESS
!= kernResult
) {
777 printf( "IOMasterPort returned %d\n", kernResult
);
780 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
781 if ( classesToMatch
== NULL
) {
782 printf( "IOServiceMatching returned a NULL dictionary.\n" );
784 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
786 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
787 if ( KERN_SUCCESS
!= kernResult
)
789 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
795 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
797 io_object_t nextMedia
;
798 kern_return_t kernResult
= KERN_FAILURE
;
800 nextMedia
= IOIteratorNext( mediaIterator
);
803 CFTypeRef bsdPathAsCFString
;
804 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
805 if ( bsdPathAsCFString
) {
806 size_t devPathLength
;
807 strcpy( bsdPath
, _PATH_DEV
);
808 strcat( bsdPath
, "r" );
809 devPathLength
= strlen( bsdPath
);
810 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
811 kernResult
= KERN_SUCCESS
;
813 CFRelease( bsdPathAsCFString
);
815 IOObjectRelease( nextMedia
);
823 static int hdev_probe_device(const char *filename
)
827 /* allow a dedicated CD-ROM driver to match with a higher priority */
828 if (strstart(filename
, "/dev/cdrom", NULL
))
831 if (stat(filename
, &st
) >= 0 &&
832 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
839 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
841 BDRVRawState
*s
= bs
->opaque
;
844 if (strstart(filename
, "/dev/cdrom", NULL
)) {
845 kern_return_t kernResult
;
846 io_iterator_t mediaIterator
;
847 char bsdPath
[ MAXPATHLEN
];
850 kernResult
= FindEjectableCDMedia( &mediaIterator
);
851 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
853 if ( bsdPath
[ 0 ] != '\0' ) {
854 strcat(bsdPath
,"s0");
855 /* some CDs don't have a partition 0 */
856 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
858 bsdPath
[strlen(bsdPath
)-1] = '1';
866 IOObjectRelease( mediaIterator
);
870 s
->type
= FTYPE_FILE
;
871 #if defined(__linux__)
873 char resolved_path
[ MAXPATHLEN
], *temp
;
875 temp
= realpath(filename
, resolved_path
);
876 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
882 return raw_open_common(bs
, filename
, flags
, 0);
885 #if defined(__linux__)
886 /* Note: we do not have a reliable method to detect if the floppy is
887 present. The current method is to try to open the floppy at every
888 I/O and to keep it opened during a few hundreds of ms. */
889 static int fd_open(BlockDriverState
*bs
)
891 BDRVRawState
*s
= bs
->opaque
;
892 int last_media_present
;
894 if (s
->type
!= FTYPE_FD
)
896 last_media_present
= (s
->fd
>= 0);
898 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
902 printf("Floppy closed\n");
906 if (s
->fd_got_error
&&
907 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
909 printf("No floppy (open delayed)\n");
913 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
915 s
->fd_error_time
= qemu_get_clock(rt_clock
);
917 if (last_media_present
)
918 s
->fd_media_changed
= 1;
920 printf("No floppy\n");
925 printf("Floppy opened\n");
928 if (!last_media_present
)
929 s
->fd_media_changed
= 1;
930 s
->fd_open_time
= qemu_get_clock(rt_clock
);
935 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
937 BDRVRawState
*s
= bs
->opaque
;
939 return ioctl(s
->fd
, req
, buf
);
942 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
943 unsigned long int req
, void *buf
,
944 BlockDriverCompletionFunc
*cb
, void *opaque
)
946 BDRVRawState
*s
= bs
->opaque
;
950 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
953 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
954 static int fd_open(BlockDriverState
*bs
)
956 BDRVRawState
*s
= bs
->opaque
;
958 /* this is just to ensure s->fd is sane (its called by io ops) */
963 #else /* !linux && !FreeBSD */
965 static int fd_open(BlockDriverState
*bs
)
970 #endif /* !linux && !FreeBSD */
972 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
976 struct stat stat_buf
;
977 int64_t total_size
= 0;
979 /* Read out options */
980 while (options
&& options
->name
) {
981 if (!strcmp(options
->name
, "size")) {
982 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
987 fd
= open(filename
, O_WRONLY
| O_BINARY
);
991 if (fstat(fd
, &stat_buf
) < 0)
993 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
995 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1002 static int hdev_has_zero_init(BlockDriverState
*bs
)
1007 static BlockDriver bdrv_host_device
= {
1008 .format_name
= "host_device",
1009 .protocol_name
= "host_device",
1010 .instance_size
= sizeof(BDRVRawState
),
1011 .bdrv_probe_device
= hdev_probe_device
,
1012 .bdrv_file_open
= hdev_open
,
1013 .bdrv_close
= raw_close
,
1014 .bdrv_create
= hdev_create
,
1015 .create_options
= raw_create_options
,
1016 .bdrv_has_zero_init
= hdev_has_zero_init
,
1017 .bdrv_flush
= raw_flush
,
1019 .bdrv_aio_readv
= raw_aio_readv
,
1020 .bdrv_aio_writev
= raw_aio_writev
,
1021 .bdrv_aio_flush
= raw_aio_flush
,
1023 .bdrv_read
= raw_read
,
1024 .bdrv_write
= raw_write
,
1025 .bdrv_getlength
= raw_getlength
,
1027 /* generic scsi device */
1029 .bdrv_ioctl
= hdev_ioctl
,
1030 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1035 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1037 BDRVRawState
*s
= bs
->opaque
;
1042 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1043 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1047 /* close fd so that we can reopen it as needed */
1050 s
->fd_media_changed
= 1;
1055 static int floppy_probe_device(const char *filename
)
1059 struct floppy_struct fdparam
;
1061 if (strstart(filename
, "/dev/fd", NULL
))
1064 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1069 /* Attempt to detect via a floppy specific ioctl */
1070 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1080 static int floppy_is_inserted(BlockDriverState
*bs
)
1082 return fd_open(bs
) >= 0;
1085 static int floppy_media_changed(BlockDriverState
*bs
)
1087 BDRVRawState
*s
= bs
->opaque
;
1091 * XXX: we do not have a true media changed indication.
1092 * It does not work if the floppy is changed without trying to read it.
1095 ret
= s
->fd_media_changed
;
1096 s
->fd_media_changed
= 0;
1098 printf("Floppy changed=%d\n", ret
);
1103 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1105 BDRVRawState
*s
= bs
->opaque
;
1112 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1114 if (ioctl(fd
, FDEJECT
, 0) < 0)
1122 static BlockDriver bdrv_host_floppy
= {
1123 .format_name
= "host_floppy",
1124 .protocol_name
= "host_floppy",
1125 .instance_size
= sizeof(BDRVRawState
),
1126 .bdrv_probe_device
= floppy_probe_device
,
1127 .bdrv_file_open
= floppy_open
,
1128 .bdrv_close
= raw_close
,
1129 .bdrv_create
= hdev_create
,
1130 .create_options
= raw_create_options
,
1131 .bdrv_has_zero_init
= hdev_has_zero_init
,
1132 .bdrv_flush
= raw_flush
,
1134 .bdrv_aio_readv
= raw_aio_readv
,
1135 .bdrv_aio_writev
= raw_aio_writev
,
1136 .bdrv_aio_flush
= raw_aio_flush
,
1138 .bdrv_read
= raw_read
,
1139 .bdrv_write
= raw_write
,
1140 .bdrv_getlength
= raw_getlength
,
1142 /* removable device support */
1143 .bdrv_is_inserted
= floppy_is_inserted
,
1144 .bdrv_media_changed
= floppy_media_changed
,
1145 .bdrv_eject
= floppy_eject
,
1148 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1150 BDRVRawState
*s
= bs
->opaque
;
1154 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1155 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1158 static int cdrom_probe_device(const char *filename
)
1163 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1168 /* Attempt to detect via a CDROM specific ioctl */
1169 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1178 static int cdrom_is_inserted(BlockDriverState
*bs
)
1180 BDRVRawState
*s
= bs
->opaque
;
1183 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1184 if (ret
== CDS_DISC_OK
)
1189 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1191 BDRVRawState
*s
= bs
->opaque
;
1194 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1195 perror("CDROMEJECT");
1197 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1198 perror("CDROMEJECT");
1204 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1206 BDRVRawState
*s
= bs
->opaque
;
1208 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1210 * Note: an error can happen if the distribution automatically
1213 /* perror("CDROM_LOCKDOOR"); */
1219 static BlockDriver bdrv_host_cdrom
= {
1220 .format_name
= "host_cdrom",
1221 .protocol_name
= "host_cdrom",
1222 .instance_size
= sizeof(BDRVRawState
),
1223 .bdrv_probe_device
= cdrom_probe_device
,
1224 .bdrv_file_open
= cdrom_open
,
1225 .bdrv_close
= raw_close
,
1226 .bdrv_create
= hdev_create
,
1227 .create_options
= raw_create_options
,
1228 .bdrv_has_zero_init
= hdev_has_zero_init
,
1229 .bdrv_flush
= raw_flush
,
1231 .bdrv_aio_readv
= raw_aio_readv
,
1232 .bdrv_aio_writev
= raw_aio_writev
,
1233 .bdrv_aio_flush
= raw_aio_flush
,
1235 .bdrv_read
= raw_read
,
1236 .bdrv_write
= raw_write
,
1237 .bdrv_getlength
= raw_getlength
,
1239 /* removable device support */
1240 .bdrv_is_inserted
= cdrom_is_inserted
,
1241 .bdrv_eject
= cdrom_eject
,
1242 .bdrv_set_locked
= cdrom_set_locked
,
1244 /* generic scsi device */
1245 .bdrv_ioctl
= hdev_ioctl
,
1246 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1248 #endif /* __linux__ */
1250 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1251 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1253 BDRVRawState
*s
= bs
->opaque
;
1258 ret
= raw_open_common(bs
, filename
, flags
, 0);
1262 /* make sure the door isnt locked at this time */
1263 ioctl(s
->fd
, CDIOCALLOW
);
1267 static int cdrom_probe_device(const char *filename
)
1269 if (strstart(filename
, "/dev/cd", NULL
) ||
1270 strstart(filename
, "/dev/acd", NULL
))
1275 static int cdrom_reopen(BlockDriverState
*bs
)
1277 BDRVRawState
*s
= bs
->opaque
;
1281 * Force reread of possibly changed/newly loaded disc,
1282 * FreeBSD seems to not notice sometimes...
1286 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1293 /* make sure the door isnt locked at this time */
1294 ioctl(s
->fd
, CDIOCALLOW
);
1298 static int cdrom_is_inserted(BlockDriverState
*bs
)
1300 return raw_getlength(bs
) > 0;
1303 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1305 BDRVRawState
*s
= bs
->opaque
;
1310 (void) ioctl(s
->fd
, CDIOCALLOW
);
1313 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1314 perror("CDIOCEJECT");
1316 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1317 perror("CDIOCCLOSE");
1320 if (cdrom_reopen(bs
) < 0)
1325 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1327 BDRVRawState
*s
= bs
->opaque
;
1331 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1333 * Note: an error can happen if the distribution automatically
1336 /* perror("CDROM_LOCKDOOR"); */
1342 static BlockDriver bdrv_host_cdrom
= {
1343 .format_name
= "host_cdrom",
1344 .protocol_name
= "host_cdrom",
1345 .instance_size
= sizeof(BDRVRawState
),
1346 .bdrv_probe_device
= cdrom_probe_device
,
1347 .bdrv_file_open
= cdrom_open
,
1348 .bdrv_close
= raw_close
,
1349 .bdrv_create
= hdev_create
,
1350 .create_options
= raw_create_options
,
1351 .bdrv_has_zero_init
= hdev_has_zero_init
,
1352 .bdrv_flush
= raw_flush
,
1354 .bdrv_aio_readv
= raw_aio_readv
,
1355 .bdrv_aio_writev
= raw_aio_writev
,
1356 .bdrv_aio_flush
= raw_aio_flush
,
1358 .bdrv_read
= raw_read
,
1359 .bdrv_write
= raw_write
,
1360 .bdrv_getlength
= raw_getlength
,
1362 /* removable device support */
1363 .bdrv_is_inserted
= cdrom_is_inserted
,
1364 .bdrv_eject
= cdrom_eject
,
1365 .bdrv_set_locked
= cdrom_set_locked
,
1367 #endif /* __FreeBSD__ */
1369 static void bdrv_file_init(void)
1372 * Register all the drivers. Note that order is important, the driver
1373 * registered last will get probed first.
1375 bdrv_register(&bdrv_file
);
1376 bdrv_register(&bdrv_host_device
);
1378 bdrv_register(&bdrv_host_floppy
);
1379 bdrv_register(&bdrv_host_cdrom
);
1381 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1382 bdrv_register(&bdrv_host_cdrom
);
1386 block_init(bdrv_file_init
);