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>
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
{
108 unsigned int lseek_err_cnt
;
111 #if defined(__linux__)
112 /* linux floppy specific */
113 int64_t fd_open_time
;
114 int64_t fd_error_time
;
116 int fd_media_changed
;
119 uint8_t* aligned_buf
;
122 static int fd_open(BlockDriverState
*bs
);
123 static int64_t raw_getlength(BlockDriverState
*bs
);
125 #if defined(__FreeBSD__)
126 static int cdrom_reopen(BlockDriverState
*bs
);
129 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
130 int bdrv_flags
, int open_flags
)
132 BDRVRawState
*s
= bs
->opaque
;
135 s
->lseek_err_cnt
= 0;
137 s
->open_flags
= open_flags
| O_BINARY
;
138 s
->open_flags
&= ~O_ACCMODE
;
139 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
140 s
->open_flags
|= O_RDWR
;
142 s
->open_flags
|= O_RDONLY
;
146 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
147 * and O_DIRECT for no caching. */
148 if ((bdrv_flags
& BDRV_O_NOCACHE
))
149 s
->open_flags
|= O_DIRECT
;
150 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
151 s
->open_flags
|= O_DSYNC
;
154 fd
= open(filename
, s
->open_flags
, 0644);
162 s
->aligned_buf
= NULL
;
164 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
165 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
166 if (s
->aligned_buf
== NULL
) {
171 #ifdef CONFIG_LINUX_AIO
172 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
173 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
174 s
->aio_ctx
= laio_init();
182 s
->aio_ctx
= paio_init();
192 qemu_vfree(s
->aligned_buf
);
198 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
200 BDRVRawState
*s
= bs
->opaque
;
203 s
->type
= FTYPE_FILE
;
204 if (flags
& BDRV_O_CREAT
)
205 open_flags
= O_CREAT
| O_TRUNC
;
207 return raw_open_common(bs
, filename
, flags
, open_flags
);
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 u_int32_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 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
245 ++(s
->lseek_err_cnt
);
246 if(s
->lseek_err_cnt
<= 10) {
247 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
248 "] lseek failed : %d = %s\n",
249 s
->fd
, bs
->filename
, offset
, buf
, count
,
250 bs
->total_sectors
, errno
, strerror(errno
));
256 ret
= read(s
->fd
, buf
, count
);
258 goto label__raw_read__success
;
260 /* Allow reads beyond the end (needed for pwrite) */
261 if ((ret
== 0) && bs
->growable
) {
262 int64_t size
= raw_getlength(bs
);
263 if (offset
>= size
) {
264 memset(buf
, 0, count
);
266 goto label__raw_read__success
;
270 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
271 "] read failed %d : %d = %s\n",
272 s
->fd
, bs
->filename
, offset
, buf
, count
,
273 bs
->total_sectors
, ret
, errno
, strerror(errno
));
275 /* Try harder for CDrom. */
276 if (bs
->type
== BDRV_TYPE_CDROM
) {
277 lseek(s
->fd
, offset
, SEEK_SET
);
278 ret
= read(s
->fd
, buf
, count
);
280 goto label__raw_read__success
;
281 lseek(s
->fd
, offset
, SEEK_SET
);
282 ret
= read(s
->fd
, buf
, count
);
284 goto label__raw_read__success
;
286 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
287 "] retry read failed %d : %d = %s\n",
288 s
->fd
, bs
->filename
, offset
, buf
, count
,
289 bs
->total_sectors
, ret
, errno
, strerror(errno
));
292 label__raw_read__success
:
294 return (ret
< 0) ? -errno
: ret
;
298 * offset and count are in bytes, but must be multiples of 512 for files
299 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
301 * This function may be called without alignment if the caller ensures
302 * that O_DIRECT is not in effect.
304 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
305 const uint8_t *buf
, int count
)
307 BDRVRawState
*s
= bs
->opaque
;
314 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
315 ++(s
->lseek_err_cnt
);
316 if(s
->lseek_err_cnt
) {
317 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
318 PRId64
"] lseek failed : %d = %s\n",
319 s
->fd
, bs
->filename
, offset
, buf
, count
,
320 bs
->total_sectors
, errno
, strerror(errno
));
324 s
->lseek_err_cnt
= 0;
326 ret
= write(s
->fd
, buf
, count
);
328 goto label__raw_write__success
;
330 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
331 "] write failed %d : %d = %s\n",
332 s
->fd
, bs
->filename
, offset
, buf
, count
,
333 bs
->total_sectors
, ret
, errno
, strerror(errno
));
335 label__raw_write__success
:
337 return (ret
< 0) ? -errno
: ret
;
342 * offset and count are in bytes and possibly not aligned. For files opened
343 * with O_DIRECT, necessary alignments are ensured before calling
344 * raw_pread_aligned to do the actual read.
346 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
347 uint8_t *buf
, int count
)
349 BDRVRawState
*s
= bs
->opaque
;
350 int size
, ret
, shift
, sum
;
354 if (s
->aligned_buf
!= NULL
) {
356 if (offset
& 0x1ff) {
357 /* align offset on a 512 bytes boundary */
359 shift
= offset
& 0x1ff;
360 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
361 if (size
> ALIGNED_BUFFER_SIZE
)
362 size
= ALIGNED_BUFFER_SIZE
;
363 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
370 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
380 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
382 /* read on aligned buffer */
386 size
= (count
+ 0x1ff) & ~0x1ff;
387 if (size
> ALIGNED_BUFFER_SIZE
)
388 size
= ALIGNED_BUFFER_SIZE
;
390 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
398 memcpy(buf
, s
->aligned_buf
, size
);
410 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
413 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
414 uint8_t *buf
, int nb_sectors
)
418 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
419 if (ret
== (nb_sectors
* 512))
425 * offset and count are in bytes and possibly not aligned. For files opened
426 * with O_DIRECT, necessary alignments are ensured before calling
427 * raw_pwrite_aligned to do the actual write.
429 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
430 const uint8_t *buf
, int count
)
432 BDRVRawState
*s
= bs
->opaque
;
433 int size
, ret
, shift
, sum
;
437 if (s
->aligned_buf
!= NULL
) {
439 if (offset
& 0x1ff) {
440 /* align offset on a 512 bytes boundary */
441 shift
= offset
& 0x1ff;
442 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
449 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
451 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
463 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
465 while ((size
= (count
& ~0x1ff)) != 0) {
467 if (size
> ALIGNED_BUFFER_SIZE
)
468 size
= ALIGNED_BUFFER_SIZE
;
470 memcpy(s
->aligned_buf
, buf
, size
);
472 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
481 /* here, count < 512 because (count & ~0x1ff) == 0 */
483 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
486 memcpy(s
->aligned_buf
, buf
, count
);
488 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
499 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
502 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
503 const uint8_t *buf
, int nb_sectors
)
506 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
507 if (ret
== (nb_sectors
* 512))
513 * Check if all memory in this vector is sector aligned.
515 static int qiov_is_aligned(QEMUIOVector
*qiov
)
519 for (i
= 0; i
< qiov
->niov
; i
++) {
520 if ((uintptr_t) qiov
->iov
[i
].iov_base
% 512) {
528 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
529 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
530 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
532 BDRVRawState
*s
= bs
->opaque
;
538 * If O_DIRECT is used the buffer needs to be aligned on a sector
539 * boundary. Check if this is the case or telll the low-level
540 * driver that it needs to copy the buffer.
542 if (s
->aligned_buf
) {
543 if (!qiov_is_aligned(qiov
)) {
544 type
|= QEMU_AIO_MISALIGNED
;
545 } else if (s
->use_aio
) {
546 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
547 nb_sectors
, cb
, opaque
, type
);
551 return paio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
, nb_sectors
,
555 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
556 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
557 BlockDriverCompletionFunc
*cb
, void *opaque
)
559 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
560 cb
, opaque
, QEMU_AIO_READ
);
563 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
564 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
565 BlockDriverCompletionFunc
*cb
, void *opaque
)
567 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
568 cb
, opaque
, QEMU_AIO_WRITE
);
571 static void raw_close(BlockDriverState
*bs
)
573 BDRVRawState
*s
= bs
->opaque
;
577 if (s
->aligned_buf
!= NULL
)
578 qemu_free(s
->aligned_buf
);
582 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
584 BDRVRawState
*s
= bs
->opaque
;
585 if (s
->type
!= FTYPE_FILE
)
587 if (ftruncate(s
->fd
, offset
) < 0)
593 static int64_t raw_getlength(BlockDriverState
*bs
)
595 BDRVRawState
*s
= bs
->opaque
;
601 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
604 if (ioctl(fd
, DIOCGDINFO
, &dl
))
606 return (uint64_t)dl
.d_secsize
*
607 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
611 #else /* !__OpenBSD__ */
612 static int64_t raw_getlength(BlockDriverState
*bs
)
614 BDRVRawState
*s
= bs
->opaque
;
624 struct dk_minfo minfo
;
637 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
638 #ifdef DIOCGMEDIASIZE
639 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
640 #elif defined(DIOCGPART)
643 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
644 size
= pi
.media_size
;
651 size
= LONG_LONG_MAX
;
653 size
= lseek(fd
, 0LL, SEEK_END
);
658 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
659 if (size
== 2048LL * (unsigned)-1)
661 /* XXX no disc? maybe we need to reopen... */
662 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
672 * use the DKIOCGMEDIAINFO ioctl to read the size.
674 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
676 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
677 } else /* there are reports that lseek on some devices
678 fails, but irc discussion said that contingency
679 on contingency was overkill */
682 size
= lseek(fd
, 0, SEEK_END
);
688 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
692 int64_t total_size
= 0;
694 /* Read out options */
695 while (options
&& options
->name
) {
696 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
697 total_size
= options
->value
.n
/ 512;
702 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
707 if (ftruncate(fd
, total_size
* 512) != 0) {
710 if (close(fd
) != 0) {
717 static void raw_flush(BlockDriverState
*bs
)
719 BDRVRawState
*s
= bs
->opaque
;
724 static QEMUOptionParameter raw_create_options
[] = {
726 .name
= BLOCK_OPT_SIZE
,
728 .help
= "Virtual disk size"
733 static BlockDriver bdrv_raw
= {
734 .format_name
= "raw",
735 .instance_size
= sizeof(BDRVRawState
),
736 .bdrv_probe
= NULL
, /* no probe for protocols */
737 .bdrv_open
= raw_open
,
738 .bdrv_read
= raw_read
,
739 .bdrv_write
= raw_write
,
740 .bdrv_close
= raw_close
,
741 .bdrv_create
= raw_create
,
742 .bdrv_flush
= raw_flush
,
744 .bdrv_aio_readv
= raw_aio_readv
,
745 .bdrv_aio_writev
= raw_aio_writev
,
747 .bdrv_truncate
= raw_truncate
,
748 .bdrv_getlength
= raw_getlength
,
750 .create_options
= raw_create_options
,
753 /***********************************************/
757 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
758 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
760 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
762 kern_return_t kernResult
;
763 mach_port_t masterPort
;
764 CFMutableDictionaryRef classesToMatch
;
766 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
767 if ( KERN_SUCCESS
!= kernResult
) {
768 printf( "IOMasterPort returned %d\n", kernResult
);
771 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
772 if ( classesToMatch
== NULL
) {
773 printf( "IOServiceMatching returned a NULL dictionary.\n" );
775 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
777 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
778 if ( KERN_SUCCESS
!= kernResult
)
780 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
786 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
788 io_object_t nextMedia
;
789 kern_return_t kernResult
= KERN_FAILURE
;
791 nextMedia
= IOIteratorNext( mediaIterator
);
794 CFTypeRef bsdPathAsCFString
;
795 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
796 if ( bsdPathAsCFString
) {
797 size_t devPathLength
;
798 strcpy( bsdPath
, _PATH_DEV
);
799 strcat( bsdPath
, "r" );
800 devPathLength
= strlen( bsdPath
);
801 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
802 kernResult
= KERN_SUCCESS
;
804 CFRelease( bsdPathAsCFString
);
806 IOObjectRelease( nextMedia
);
814 static int hdev_probe_device(const char *filename
)
818 /* allow a dedicated CD-ROM driver to match with a higher priority */
819 if (strstart(filename
, "/dev/cdrom", NULL
))
822 if (stat(filename
, &st
) >= 0 &&
823 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
830 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
832 BDRVRawState
*s
= bs
->opaque
;
835 if (strstart(filename
, "/dev/cdrom", NULL
)) {
836 kern_return_t kernResult
;
837 io_iterator_t mediaIterator
;
838 char bsdPath
[ MAXPATHLEN
];
841 kernResult
= FindEjectableCDMedia( &mediaIterator
);
842 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
844 if ( bsdPath
[ 0 ] != '\0' ) {
845 strcat(bsdPath
,"s0");
846 /* some CDs don't have a partition 0 */
847 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
849 bsdPath
[strlen(bsdPath
)-1] = '1';
857 IOObjectRelease( mediaIterator
);
861 s
->type
= FTYPE_FILE
;
862 #if defined(__linux__)
863 if (strstart(filename
, "/dev/sg", NULL
)) {
868 return raw_open_common(bs
, filename
, flags
, 0);
871 #if defined(__linux__)
872 /* Note: we do not have a reliable method to detect if the floppy is
873 present. The current method is to try to open the floppy at every
874 I/O and to keep it opened during a few hundreds of ms. */
875 static int fd_open(BlockDriverState
*bs
)
877 BDRVRawState
*s
= bs
->opaque
;
878 int last_media_present
;
880 if (s
->type
!= FTYPE_FD
)
882 last_media_present
= (s
->fd
>= 0);
884 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
888 printf("Floppy closed\n");
892 if (s
->fd_got_error
&&
893 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
895 printf("No floppy (open delayed)\n");
899 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
901 s
->fd_error_time
= qemu_get_clock(rt_clock
);
903 if (last_media_present
)
904 s
->fd_media_changed
= 1;
906 printf("No floppy\n");
911 printf("Floppy opened\n");
914 if (!last_media_present
)
915 s
->fd_media_changed
= 1;
916 s
->fd_open_time
= qemu_get_clock(rt_clock
);
921 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
923 BDRVRawState
*s
= bs
->opaque
;
925 return ioctl(s
->fd
, req
, buf
);
928 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
929 unsigned long int req
, void *buf
,
930 BlockDriverCompletionFunc
*cb
, void *opaque
)
932 BDRVRawState
*s
= bs
->opaque
;
936 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
939 #elif defined(__FreeBSD__)
940 static int fd_open(BlockDriverState
*bs
)
942 BDRVRawState
*s
= bs
->opaque
;
944 /* this is just to ensure s->fd is sane (its called by io ops) */
949 #else /* !linux && !FreeBSD */
951 static int fd_open(BlockDriverState
*bs
)
956 #endif /* !linux && !FreeBSD */
958 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
962 struct stat stat_buf
;
963 int64_t total_size
= 0;
965 /* Read out options */
966 while (options
&& options
->name
) {
967 if (!strcmp(options
->name
, "size")) {
968 total_size
= options
->value
.n
/ 512;
973 fd
= open(filename
, O_WRONLY
| O_BINARY
);
977 if (fstat(fd
, &stat_buf
) < 0)
979 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
981 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
988 static BlockDriver bdrv_host_device
= {
989 .format_name
= "host_device",
990 .instance_size
= sizeof(BDRVRawState
),
991 .bdrv_probe_device
= hdev_probe_device
,
992 .bdrv_open
= hdev_open
,
993 .bdrv_close
= raw_close
,
994 .bdrv_create
= hdev_create
,
995 .bdrv_flush
= raw_flush
,
997 .bdrv_aio_readv
= raw_aio_readv
,
998 .bdrv_aio_writev
= raw_aio_writev
,
1000 .bdrv_read
= raw_read
,
1001 .bdrv_write
= raw_write
,
1002 .bdrv_getlength
= raw_getlength
,
1004 /* generic scsi device */
1006 .bdrv_ioctl
= hdev_ioctl
,
1007 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1012 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1014 BDRVRawState
*s
= bs
->opaque
;
1019 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1020 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1024 /* close fd so that we can reopen it as needed */
1027 s
->fd_media_changed
= 1;
1032 static int floppy_probe_device(const char *filename
)
1034 if (strstart(filename
, "/dev/fd", NULL
))
1040 static int floppy_is_inserted(BlockDriverState
*bs
)
1042 return fd_open(bs
) >= 0;
1045 static int floppy_media_changed(BlockDriverState
*bs
)
1047 BDRVRawState
*s
= bs
->opaque
;
1051 * XXX: we do not have a true media changed indication.
1052 * It does not work if the floppy is changed without trying to read it.
1055 ret
= s
->fd_media_changed
;
1056 s
->fd_media_changed
= 0;
1058 printf("Floppy changed=%d\n", ret
);
1063 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1065 BDRVRawState
*s
= bs
->opaque
;
1072 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1074 if (ioctl(fd
, FDEJECT
, 0) < 0)
1082 static BlockDriver bdrv_host_floppy
= {
1083 .format_name
= "host_floppy",
1084 .instance_size
= sizeof(BDRVRawState
),
1085 .bdrv_probe_device
= floppy_probe_device
,
1086 .bdrv_open
= floppy_open
,
1087 .bdrv_close
= raw_close
,
1088 .bdrv_create
= hdev_create
,
1089 .bdrv_flush
= raw_flush
,
1091 .bdrv_aio_readv
= raw_aio_readv
,
1092 .bdrv_aio_writev
= raw_aio_writev
,
1094 .bdrv_read
= raw_read
,
1095 .bdrv_write
= raw_write
,
1096 .bdrv_getlength
= raw_getlength
,
1098 /* removable device support */
1099 .bdrv_is_inserted
= floppy_is_inserted
,
1100 .bdrv_media_changed
= floppy_media_changed
,
1101 .bdrv_eject
= floppy_eject
,
1104 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1106 BDRVRawState
*s
= bs
->opaque
;
1110 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1111 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1114 static int cdrom_probe_device(const char *filename
)
1116 if (strstart(filename
, "/dev/cd", NULL
))
1121 static int cdrom_is_inserted(BlockDriverState
*bs
)
1123 BDRVRawState
*s
= bs
->opaque
;
1126 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1127 if (ret
== CDS_DISC_OK
)
1132 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1134 BDRVRawState
*s
= bs
->opaque
;
1137 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1138 perror("CDROMEJECT");
1140 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1141 perror("CDROMEJECT");
1147 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1149 BDRVRawState
*s
= bs
->opaque
;
1151 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1153 * Note: an error can happen if the distribution automatically
1156 /* perror("CDROM_LOCKDOOR"); */
1162 static BlockDriver bdrv_host_cdrom
= {
1163 .format_name
= "host_cdrom",
1164 .instance_size
= sizeof(BDRVRawState
),
1165 .bdrv_probe_device
= cdrom_probe_device
,
1166 .bdrv_open
= cdrom_open
,
1167 .bdrv_close
= raw_close
,
1168 .bdrv_create
= hdev_create
,
1169 .bdrv_flush
= raw_flush
,
1171 .bdrv_aio_readv
= raw_aio_readv
,
1172 .bdrv_aio_writev
= raw_aio_writev
,
1174 .bdrv_read
= raw_read
,
1175 .bdrv_write
= raw_write
,
1176 .bdrv_getlength
= raw_getlength
,
1178 /* removable device support */
1179 .bdrv_is_inserted
= cdrom_is_inserted
,
1180 .bdrv_eject
= cdrom_eject
,
1181 .bdrv_set_locked
= cdrom_set_locked
,
1183 /* generic scsi device */
1184 .bdrv_ioctl
= hdev_ioctl
,
1185 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1187 #endif /* __linux__ */
1190 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1192 BDRVRawState
*s
= bs
->opaque
;
1197 ret
= raw_open_common(bs
, filename
, flags
, 0);
1201 /* make sure the door isnt locked at this time */
1202 ioctl(s
->fd
, CDIOCALLOW
);
1206 static int cdrom_probe_device(const char *filename
)
1208 if (strstart(filename
, "/dev/cd", NULL
) ||
1209 strstart(filename
, "/dev/acd", NULL
))
1214 static int cdrom_reopen(BlockDriverState
*bs
)
1216 BDRVRawState
*s
= bs
->opaque
;
1220 * Force reread of possibly changed/newly loaded disc,
1221 * FreeBSD seems to not notice sometimes...
1225 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1232 /* make sure the door isnt locked at this time */
1233 ioctl(s
->fd
, CDIOCALLOW
);
1237 static int cdrom_is_inserted(BlockDriverState
*bs
)
1239 return raw_getlength(bs
) > 0;
1242 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1244 BDRVRawState
*s
= bs
->opaque
;
1249 (void) ioctl(s
->fd
, CDIOCALLOW
);
1252 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1253 perror("CDIOCEJECT");
1255 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1256 perror("CDIOCCLOSE");
1259 if (cdrom_reopen(bs
) < 0)
1264 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1266 BDRVRawState
*s
= bs
->opaque
;
1270 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1272 * Note: an error can happen if the distribution automatically
1275 /* perror("CDROM_LOCKDOOR"); */
1281 static BlockDriver bdrv_host_cdrom
= {
1282 .format_name
= "host_cdrom",
1283 .instance_size
= sizeof(BDRVRawState
),
1284 .bdrv_probe_device
= cdrom_probe_device
,
1285 .bdrv_open
= cdrom_open
,
1286 .bdrv_close
= raw_close
,
1287 .bdrv_create
= hdev_create
,
1288 .bdrv_flush
= raw_flush
,
1290 .bdrv_aio_readv
= raw_aio_readv
,
1291 .bdrv_aio_writev
= raw_aio_writev
,
1293 .bdrv_read
= raw_read
,
1294 .bdrv_write
= raw_write
,
1295 .bdrv_getlength
= raw_getlength
,
1297 /* removable device support */
1298 .bdrv_is_inserted
= cdrom_is_inserted
,
1299 .bdrv_eject
= cdrom_eject
,
1300 .bdrv_set_locked
= cdrom_set_locked
,
1302 #endif /* __FreeBSD__ */
1304 static void bdrv_raw_init(void)
1307 * Register all the drivers. Note that order is important, the driver
1308 * registered last will get probed first.
1310 bdrv_register(&bdrv_raw
);
1311 bdrv_register(&bdrv_host_device
);
1313 bdrv_register(&bdrv_host_floppy
);
1314 bdrv_register(&bdrv_host_cdrom
);
1317 bdrv_register(&bdrv_host_cdrom
);
1321 block_init(bdrv_raw_init
);