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 <linux/cdrom.h>
54 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
67 #include <sys/ioctl.h>
68 #include <sys/diskslice.h>
71 //#define DEBUG_FLOPPY
74 #if defined(DEBUG_BLOCK)
75 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
76 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
78 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
81 /* OS X does not have O_DSYNC */
84 #define O_DSYNC O_SYNC
85 #elif defined(O_FSYNC)
86 #define O_DSYNC O_FSYNC
90 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
92 #define O_DIRECT O_DSYNC
99 #define ALIGNED_BUFFER_SIZE (32 * 512)
101 /* if the FD is not accessed during that time (in ms), we try to
102 reopen it to see if the disk has been changed */
103 #define FD_OPEN_TIMEOUT 1000
105 typedef struct BDRVRawState
{
109 #if defined(__linux__)
110 /* linux floppy specific */
111 int64_t fd_open_time
;
112 int64_t fd_error_time
;
114 int fd_media_changed
;
116 #ifdef CONFIG_LINUX_AIO
120 uint8_t* aligned_buf
;
123 static int fd_open(BlockDriverState
*bs
);
124 static int64_t raw_getlength(BlockDriverState
*bs
);
126 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
127 static int cdrom_reopen(BlockDriverState
*bs
);
130 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
131 int bdrv_flags
, int open_flags
)
133 BDRVRawState
*s
= bs
->opaque
;
136 s
->open_flags
= open_flags
| O_BINARY
;
137 s
->open_flags
&= ~O_ACCMODE
;
138 if (bdrv_flags
& BDRV_O_RDWR
) {
139 s
->open_flags
|= O_RDWR
;
141 s
->open_flags
|= O_RDONLY
;
144 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
145 * and O_DIRECT for no caching. */
146 if ((bdrv_flags
& BDRV_O_NOCACHE
))
147 s
->open_flags
|= O_DIRECT
;
148 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
149 s
->open_flags
|= O_DSYNC
;
152 fd
= qemu_open(filename
, s
->open_flags
, 0644);
160 s
->aligned_buf
= NULL
;
162 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
163 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
164 if (s
->aligned_buf
== NULL
) {
169 #ifdef CONFIG_LINUX_AIO
170 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
171 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
173 /* We're falling back to POSIX AIO in some cases */
176 s
->aio_ctx
= laio_init();
184 if (paio_init() < 0) {
187 #ifdef CONFIG_LINUX_AIO
195 qemu_vfree(s
->aligned_buf
);
201 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
203 BDRVRawState
*s
= bs
->opaque
;
205 s
->type
= FTYPE_FILE
;
206 return raw_open_common(bs
, filename
, flags
, 0);
209 /* XXX: use host sector size if necessary with:
210 #ifdef DIOCGSECTORSIZE
212 unsigned int sectorsize = 512;
213 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
214 sectorsize > bufsize)
215 bufsize = sectorsize;
219 uint32_t blockSize = 512;
220 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
227 * offset and count are in bytes, but must be multiples of 512 for files
228 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
230 * This function may be called without alignment if the caller ensures
231 * that O_DIRECT is not in effect.
233 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
234 uint8_t *buf
, int count
)
236 BDRVRawState
*s
= bs
->opaque
;
243 ret
= pread(s
->fd
, buf
, count
, offset
);
247 /* Allow reads beyond the end (needed for pwrite) */
248 if ((ret
== 0) && bs
->growable
) {
249 int64_t size
= raw_getlength(bs
);
250 if (offset
>= size
) {
251 memset(buf
, 0, count
);
256 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
257 "] read failed %d : %d = %s\n",
258 s
->fd
, bs
->filename
, offset
, buf
, count
,
259 bs
->total_sectors
, ret
, errno
, strerror(errno
));
261 /* Try harder for CDrom. */
262 if (s
->type
!= FTYPE_FILE
) {
263 ret
= pread(s
->fd
, buf
, count
, offset
);
266 ret
= pread(s
->fd
, buf
, count
, offset
);
270 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
271 "] retry read failed %d : %d = %s\n",
272 s
->fd
, bs
->filename
, offset
, buf
, count
,
273 bs
->total_sectors
, ret
, errno
, strerror(errno
));
276 return (ret
< 0) ? -errno
: ret
;
280 * offset and count are in bytes, but must be multiples of 512 for files
281 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
283 * This function may be called without alignment if the caller ensures
284 * that O_DIRECT is not in effect.
286 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
287 const uint8_t *buf
, int count
)
289 BDRVRawState
*s
= bs
->opaque
;
296 ret
= pwrite(s
->fd
, buf
, count
, offset
);
300 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
301 "] write failed %d : %d = %s\n",
302 s
->fd
, bs
->filename
, offset
, buf
, count
,
303 bs
->total_sectors
, ret
, errno
, strerror(errno
));
305 return (ret
< 0) ? -errno
: ret
;
310 * offset and count are in bytes and possibly not aligned. For files opened
311 * with O_DIRECT, necessary alignments are ensured before calling
312 * raw_pread_aligned to do the actual read.
314 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
315 uint8_t *buf
, int count
)
317 BDRVRawState
*s
= bs
->opaque
;
318 int size
, ret
, shift
, sum
;
322 if (s
->aligned_buf
!= NULL
) {
324 if (offset
& 0x1ff) {
325 /* align offset on a 512 bytes boundary */
327 shift
= offset
& 0x1ff;
328 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
329 if (size
> ALIGNED_BUFFER_SIZE
)
330 size
= ALIGNED_BUFFER_SIZE
;
331 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
338 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
348 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
350 /* read on aligned buffer */
354 size
= (count
+ 0x1ff) & ~0x1ff;
355 if (size
> ALIGNED_BUFFER_SIZE
)
356 size
= ALIGNED_BUFFER_SIZE
;
358 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
361 } else if (ret
== 0) {
362 fprintf(stderr
, "raw_pread: read beyond end of file\n");
370 memcpy(buf
, s
->aligned_buf
, size
);
382 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
385 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
386 uint8_t *buf
, int nb_sectors
)
390 ret
= raw_pread(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
391 nb_sectors
* BDRV_SECTOR_SIZE
);
392 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
398 * offset and count are in bytes and possibly not aligned. For files opened
399 * with O_DIRECT, necessary alignments are ensured before calling
400 * raw_pwrite_aligned to do the actual write.
402 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
403 const uint8_t *buf
, int count
)
405 BDRVRawState
*s
= bs
->opaque
;
406 int size
, ret
, shift
, sum
;
410 if (s
->aligned_buf
!= NULL
) {
412 if (offset
& 0x1ff) {
413 /* align offset on a 512 bytes boundary */
414 shift
= offset
& 0x1ff;
415 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
422 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
424 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
436 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
438 while ((size
= (count
& ~0x1ff)) != 0) {
440 if (size
> ALIGNED_BUFFER_SIZE
)
441 size
= ALIGNED_BUFFER_SIZE
;
443 memcpy(s
->aligned_buf
, buf
, size
);
445 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
454 /* here, count < 512 because (count & ~0x1ff) == 0 */
456 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
459 memcpy(s
->aligned_buf
, buf
, count
);
461 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
472 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
475 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
476 const uint8_t *buf
, int nb_sectors
)
479 ret
= raw_pwrite(bs
, sector_num
* BDRV_SECTOR_SIZE
, buf
,
480 nb_sectors
* BDRV_SECTOR_SIZE
);
481 if (ret
== (nb_sectors
* BDRV_SECTOR_SIZE
))
487 * Check if all memory in this vector is sector aligned.
489 static int qiov_is_aligned(QEMUIOVector
*qiov
)
493 for (i
= 0; i
< qiov
->niov
; i
++) {
494 if ((uintptr_t) qiov
->iov
[i
].iov_base
% BDRV_SECTOR_SIZE
) {
502 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
503 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
504 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
506 BDRVRawState
*s
= bs
->opaque
;
512 * If O_DIRECT is used the buffer needs to be aligned on a sector
513 * boundary. Check if this is the case or telll the low-level
514 * driver that it needs to copy the buffer.
516 if (s
->aligned_buf
) {
517 if (!qiov_is_aligned(qiov
)) {
518 type
|= QEMU_AIO_MISALIGNED
;
519 #ifdef CONFIG_LINUX_AIO
520 } else if (s
->use_aio
) {
521 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
522 nb_sectors
, cb
, opaque
, type
);
527 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
531 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
532 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
533 BlockDriverCompletionFunc
*cb
, void *opaque
)
535 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
536 cb
, opaque
, QEMU_AIO_READ
);
539 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
540 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
541 BlockDriverCompletionFunc
*cb
, void *opaque
)
543 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
544 cb
, opaque
, QEMU_AIO_WRITE
);
547 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
548 BlockDriverCompletionFunc
*cb
, void *opaque
)
550 BDRVRawState
*s
= bs
->opaque
;
555 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
558 static void raw_close(BlockDriverState
*bs
)
560 BDRVRawState
*s
= bs
->opaque
;
564 if (s
->aligned_buf
!= NULL
)
565 qemu_vfree(s
->aligned_buf
);
569 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
571 BDRVRawState
*s
= bs
->opaque
;
572 if (s
->type
!= FTYPE_FILE
)
574 if (ftruncate(s
->fd
, offset
) < 0)
580 static int64_t raw_getlength(BlockDriverState
*bs
)
582 BDRVRawState
*s
= bs
->opaque
;
588 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
591 if (ioctl(fd
, DIOCGDINFO
, &dl
))
593 return (uint64_t)dl
.d_secsize
*
594 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
598 #elif defined(__sun__)
599 static int64_t raw_getlength(BlockDriverState
*bs
)
601 BDRVRawState
*s
= bs
->opaque
;
602 struct dk_minfo minfo
;
611 * Use the DKIOCGMEDIAINFO ioctl to read the size.
613 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
615 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
619 * There are reports that lseek on some devices fails, but
620 * irc discussion said that contingency on contingency was overkill.
622 return lseek(s
->fd
, 0, SEEK_END
);
624 #elif defined(CONFIG_BSD)
625 static int64_t raw_getlength(BlockDriverState
*bs
)
627 BDRVRawState
*s
= bs
->opaque
;
631 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
640 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
643 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
644 #ifdef DIOCGMEDIASIZE
645 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
646 #elif defined(DIOCGPART)
649 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
650 size
= pi
.media_size
;
657 size
= LONG_LONG_MAX
;
659 size
= lseek(fd
, 0LL, SEEK_END
);
661 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
664 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
665 if (size
== 2048LL * (unsigned)-1)
667 /* XXX no disc? maybe we need to reopen... */
668 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
675 size
= lseek(fd
, 0, SEEK_END
);
680 static int64_t raw_getlength(BlockDriverState
*bs
)
682 BDRVRawState
*s
= bs
->opaque
;
690 return lseek(s
->fd
, 0, SEEK_END
);
694 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
698 int64_t total_size
= 0;
700 /* Read out options */
701 while (options
&& options
->name
) {
702 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
703 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
708 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
713 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
716 if (close(fd
) != 0) {
723 static void raw_flush(BlockDriverState
*bs
)
725 BDRVRawState
*s
= bs
->opaque
;
726 qemu_fdatasync(s
->fd
);
730 static QEMUOptionParameter raw_create_options
[] = {
732 .name
= BLOCK_OPT_SIZE
,
734 .help
= "Virtual disk size"
739 static BlockDriver bdrv_file
= {
740 .format_name
= "file",
741 .protocol_name
= "file",
742 .instance_size
= sizeof(BDRVRawState
),
743 .bdrv_probe
= NULL
, /* no probe for protocols */
744 .bdrv_file_open
= raw_open
,
745 .bdrv_read
= raw_read
,
746 .bdrv_write
= raw_write
,
747 .bdrv_close
= raw_close
,
748 .bdrv_create
= raw_create
,
749 .bdrv_flush
= raw_flush
,
751 .bdrv_aio_readv
= raw_aio_readv
,
752 .bdrv_aio_writev
= raw_aio_writev
,
753 .bdrv_aio_flush
= raw_aio_flush
,
755 .bdrv_truncate
= raw_truncate
,
756 .bdrv_getlength
= raw_getlength
,
758 .create_options
= raw_create_options
,
761 /***********************************************/
765 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
766 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
768 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
770 kern_return_t kernResult
;
771 mach_port_t masterPort
;
772 CFMutableDictionaryRef classesToMatch
;
774 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
775 if ( KERN_SUCCESS
!= kernResult
) {
776 printf( "IOMasterPort returned %d\n", kernResult
);
779 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
780 if ( classesToMatch
== NULL
) {
781 printf( "IOServiceMatching returned a NULL dictionary.\n" );
783 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
785 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
786 if ( KERN_SUCCESS
!= kernResult
)
788 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
794 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
796 io_object_t nextMedia
;
797 kern_return_t kernResult
= KERN_FAILURE
;
799 nextMedia
= IOIteratorNext( mediaIterator
);
802 CFTypeRef bsdPathAsCFString
;
803 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
804 if ( bsdPathAsCFString
) {
805 size_t devPathLength
;
806 strcpy( bsdPath
, _PATH_DEV
);
807 strcat( bsdPath
, "r" );
808 devPathLength
= strlen( bsdPath
);
809 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
810 kernResult
= KERN_SUCCESS
;
812 CFRelease( bsdPathAsCFString
);
814 IOObjectRelease( nextMedia
);
822 static int hdev_probe_device(const char *filename
)
826 /* allow a dedicated CD-ROM driver to match with a higher priority */
827 if (strstart(filename
, "/dev/cdrom", NULL
))
830 if (stat(filename
, &st
) >= 0 &&
831 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
838 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
840 BDRVRawState
*s
= bs
->opaque
;
843 if (strstart(filename
, "/dev/cdrom", NULL
)) {
844 kern_return_t kernResult
;
845 io_iterator_t mediaIterator
;
846 char bsdPath
[ MAXPATHLEN
];
849 kernResult
= FindEjectableCDMedia( &mediaIterator
);
850 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
852 if ( bsdPath
[ 0 ] != '\0' ) {
853 strcat(bsdPath
,"s0");
854 /* some CDs don't have a partition 0 */
855 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
857 bsdPath
[strlen(bsdPath
)-1] = '1';
865 IOObjectRelease( mediaIterator
);
869 s
->type
= FTYPE_FILE
;
870 #if defined(__linux__)
871 if (strstart(filename
, "/dev/sg", NULL
)) {
876 return raw_open_common(bs
, filename
, flags
, 0);
879 #if defined(__linux__)
880 /* Note: we do not have a reliable method to detect if the floppy is
881 present. The current method is to try to open the floppy at every
882 I/O and to keep it opened during a few hundreds of ms. */
883 static int fd_open(BlockDriverState
*bs
)
885 BDRVRawState
*s
= bs
->opaque
;
886 int last_media_present
;
888 if (s
->type
!= FTYPE_FD
)
890 last_media_present
= (s
->fd
>= 0);
892 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
896 printf("Floppy closed\n");
900 if (s
->fd_got_error
&&
901 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
903 printf("No floppy (open delayed)\n");
907 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
909 s
->fd_error_time
= qemu_get_clock(rt_clock
);
911 if (last_media_present
)
912 s
->fd_media_changed
= 1;
914 printf("No floppy\n");
919 printf("Floppy opened\n");
922 if (!last_media_present
)
923 s
->fd_media_changed
= 1;
924 s
->fd_open_time
= qemu_get_clock(rt_clock
);
929 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
931 BDRVRawState
*s
= bs
->opaque
;
933 return ioctl(s
->fd
, req
, buf
);
936 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
937 unsigned long int req
, void *buf
,
938 BlockDriverCompletionFunc
*cb
, void *opaque
)
940 BDRVRawState
*s
= bs
->opaque
;
944 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
947 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
948 static int fd_open(BlockDriverState
*bs
)
950 BDRVRawState
*s
= bs
->opaque
;
952 /* this is just to ensure s->fd is sane (its called by io ops) */
957 #else /* !linux && !FreeBSD */
959 static int fd_open(BlockDriverState
*bs
)
964 #endif /* !linux && !FreeBSD */
966 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
970 struct stat stat_buf
;
971 int64_t total_size
= 0;
973 /* Read out options */
974 while (options
&& options
->name
) {
975 if (!strcmp(options
->name
, "size")) {
976 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
981 fd
= open(filename
, O_WRONLY
| O_BINARY
);
985 if (fstat(fd
, &stat_buf
) < 0)
987 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
989 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
996 static int hdev_has_zero_init(BlockDriverState
*bs
)
1001 static BlockDriver bdrv_host_device
= {
1002 .format_name
= "host_device",
1003 .protocol_name
= "host_device",
1004 .instance_size
= sizeof(BDRVRawState
),
1005 .bdrv_probe_device
= hdev_probe_device
,
1006 .bdrv_file_open
= hdev_open
,
1007 .bdrv_close
= raw_close
,
1008 .bdrv_create
= hdev_create
,
1009 .create_options
= raw_create_options
,
1010 .bdrv_has_zero_init
= hdev_has_zero_init
,
1011 .bdrv_flush
= raw_flush
,
1013 .bdrv_aio_readv
= raw_aio_readv
,
1014 .bdrv_aio_writev
= raw_aio_writev
,
1015 .bdrv_aio_flush
= raw_aio_flush
,
1017 .bdrv_read
= raw_read
,
1018 .bdrv_write
= raw_write
,
1019 .bdrv_getlength
= raw_getlength
,
1021 /* generic scsi device */
1023 .bdrv_ioctl
= hdev_ioctl
,
1024 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1029 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1031 BDRVRawState
*s
= bs
->opaque
;
1036 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1037 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1041 /* close fd so that we can reopen it as needed */
1044 s
->fd_media_changed
= 1;
1049 static int floppy_probe_device(const char *filename
)
1053 struct floppy_struct fdparam
;
1055 if (strstart(filename
, "/dev/fd", NULL
))
1058 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1063 /* Attempt to detect via a floppy specific ioctl */
1064 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1074 static int floppy_is_inserted(BlockDriverState
*bs
)
1076 return fd_open(bs
) >= 0;
1079 static int floppy_media_changed(BlockDriverState
*bs
)
1081 BDRVRawState
*s
= bs
->opaque
;
1085 * XXX: we do not have a true media changed indication.
1086 * It does not work if the floppy is changed without trying to read it.
1089 ret
= s
->fd_media_changed
;
1090 s
->fd_media_changed
= 0;
1092 printf("Floppy changed=%d\n", ret
);
1097 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1099 BDRVRawState
*s
= bs
->opaque
;
1106 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1108 if (ioctl(fd
, FDEJECT
, 0) < 0)
1116 static BlockDriver bdrv_host_floppy
= {
1117 .format_name
= "host_floppy",
1118 .protocol_name
= "host_floppy",
1119 .instance_size
= sizeof(BDRVRawState
),
1120 .bdrv_probe_device
= floppy_probe_device
,
1121 .bdrv_file_open
= floppy_open
,
1122 .bdrv_close
= raw_close
,
1123 .bdrv_create
= hdev_create
,
1124 .create_options
= raw_create_options
,
1125 .bdrv_has_zero_init
= hdev_has_zero_init
,
1126 .bdrv_flush
= raw_flush
,
1128 .bdrv_aio_readv
= raw_aio_readv
,
1129 .bdrv_aio_writev
= raw_aio_writev
,
1130 .bdrv_aio_flush
= raw_aio_flush
,
1132 .bdrv_read
= raw_read
,
1133 .bdrv_write
= raw_write
,
1134 .bdrv_getlength
= raw_getlength
,
1136 /* removable device support */
1137 .bdrv_is_inserted
= floppy_is_inserted
,
1138 .bdrv_media_changed
= floppy_media_changed
,
1139 .bdrv_eject
= floppy_eject
,
1142 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1144 BDRVRawState
*s
= bs
->opaque
;
1148 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1149 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1152 static int cdrom_probe_device(const char *filename
)
1157 if (strstart(filename
, "/dev/cd", NULL
))
1160 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1165 /* Attempt to detect via a CDROM specific ioctl */
1166 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1175 static int cdrom_is_inserted(BlockDriverState
*bs
)
1177 BDRVRawState
*s
= bs
->opaque
;
1180 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1181 if (ret
== CDS_DISC_OK
)
1186 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1188 BDRVRawState
*s
= bs
->opaque
;
1191 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1192 perror("CDROMEJECT");
1194 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1195 perror("CDROMEJECT");
1201 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1203 BDRVRawState
*s
= bs
->opaque
;
1205 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1207 * Note: an error can happen if the distribution automatically
1210 /* perror("CDROM_LOCKDOOR"); */
1216 static BlockDriver bdrv_host_cdrom
= {
1217 .format_name
= "host_cdrom",
1218 .protocol_name
= "host_cdrom",
1219 .instance_size
= sizeof(BDRVRawState
),
1220 .bdrv_probe_device
= cdrom_probe_device
,
1221 .bdrv_file_open
= cdrom_open
,
1222 .bdrv_close
= raw_close
,
1223 .bdrv_create
= hdev_create
,
1224 .create_options
= raw_create_options
,
1225 .bdrv_has_zero_init
= hdev_has_zero_init
,
1226 .bdrv_flush
= raw_flush
,
1228 .bdrv_aio_readv
= raw_aio_readv
,
1229 .bdrv_aio_writev
= raw_aio_writev
,
1230 .bdrv_aio_flush
= raw_aio_flush
,
1232 .bdrv_read
= raw_read
,
1233 .bdrv_write
= raw_write
,
1234 .bdrv_getlength
= raw_getlength
,
1236 /* removable device support */
1237 .bdrv_is_inserted
= cdrom_is_inserted
,
1238 .bdrv_eject
= cdrom_eject
,
1239 .bdrv_set_locked
= cdrom_set_locked
,
1241 /* generic scsi device */
1242 .bdrv_ioctl
= hdev_ioctl
,
1243 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1245 #endif /* __linux__ */
1247 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1248 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1250 BDRVRawState
*s
= bs
->opaque
;
1255 ret
= raw_open_common(bs
, filename
, flags
, 0);
1259 /* make sure the door isnt locked at this time */
1260 ioctl(s
->fd
, CDIOCALLOW
);
1264 static int cdrom_probe_device(const char *filename
)
1266 if (strstart(filename
, "/dev/cd", NULL
) ||
1267 strstart(filename
, "/dev/acd", NULL
))
1272 static int cdrom_reopen(BlockDriverState
*bs
)
1274 BDRVRawState
*s
= bs
->opaque
;
1278 * Force reread of possibly changed/newly loaded disc,
1279 * FreeBSD seems to not notice sometimes...
1283 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1290 /* make sure the door isnt locked at this time */
1291 ioctl(s
->fd
, CDIOCALLOW
);
1295 static int cdrom_is_inserted(BlockDriverState
*bs
)
1297 return raw_getlength(bs
) > 0;
1300 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1302 BDRVRawState
*s
= bs
->opaque
;
1307 (void) ioctl(s
->fd
, CDIOCALLOW
);
1310 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1311 perror("CDIOCEJECT");
1313 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1314 perror("CDIOCCLOSE");
1317 if (cdrom_reopen(bs
) < 0)
1322 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1324 BDRVRawState
*s
= bs
->opaque
;
1328 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1330 * Note: an error can happen if the distribution automatically
1333 /* 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
,
1349 .bdrv_flush
= raw_flush
,
1351 .bdrv_aio_readv
= raw_aio_readv
,
1352 .bdrv_aio_writev
= raw_aio_writev
,
1353 .bdrv_aio_flush
= raw_aio_flush
,
1355 .bdrv_read
= raw_read
,
1356 .bdrv_write
= raw_write
,
1357 .bdrv_getlength
= raw_getlength
,
1359 /* removable device support */
1360 .bdrv_is_inserted
= cdrom_is_inserted
,
1361 .bdrv_eject
= cdrom_eject
,
1362 .bdrv_set_locked
= cdrom_set_locked
,
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
);