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
;
118 #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__)
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
->lseek_err_cnt
= 0;
139 s
->open_flags
= open_flags
| O_BINARY
;
140 s
->open_flags
&= ~O_ACCMODE
;
141 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
142 s
->open_flags
|= O_RDWR
;
144 s
->open_flags
|= O_RDONLY
;
148 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
149 * and O_DIRECT for no caching. */
150 if ((bdrv_flags
& BDRV_O_NOCACHE
))
151 s
->open_flags
|= O_DIRECT
;
152 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
153 s
->open_flags
|= O_DSYNC
;
156 fd
= open(filename
, s
->open_flags
, 0644);
164 s
->aligned_buf
= NULL
;
166 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
167 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
168 if (s
->aligned_buf
== NULL
) {
173 #ifdef CONFIG_LINUX_AIO
174 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
175 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
176 s
->aio_ctx
= laio_init();
184 s
->aio_ctx
= paio_init();
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
;
207 s
->type
= FTYPE_FILE
;
208 if (flags
& BDRV_O_CREAT
)
209 open_flags
= O_CREAT
| O_TRUNC
;
211 return raw_open_common(bs
, filename
, flags
, open_flags
);
214 /* XXX: use host sector size if necessary with:
215 #ifdef DIOCGSECTORSIZE
217 unsigned int sectorsize = 512;
218 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
219 sectorsize > bufsize)
220 bufsize = sectorsize;
224 u_int32_t blockSize = 512;
225 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
232 * offset and count are in bytes, but must be multiples of 512 for files
233 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
235 * This function may be called without alignment if the caller ensures
236 * that O_DIRECT is not in effect.
238 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
239 uint8_t *buf
, int count
)
241 BDRVRawState
*s
= bs
->opaque
;
248 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
249 ++(s
->lseek_err_cnt
);
250 if(s
->lseek_err_cnt
<= 10) {
251 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
252 "] lseek failed : %d = %s\n",
253 s
->fd
, bs
->filename
, offset
, buf
, count
,
254 bs
->total_sectors
, errno
, strerror(errno
));
260 ret
= read(s
->fd
, buf
, count
);
262 goto label__raw_read__success
;
264 /* Allow reads beyond the end (needed for pwrite) */
265 if ((ret
== 0) && bs
->growable
) {
266 int64_t size
= raw_getlength(bs
);
267 if (offset
>= size
) {
268 memset(buf
, 0, count
);
270 goto label__raw_read__success
;
274 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
275 "] read failed %d : %d = %s\n",
276 s
->fd
, bs
->filename
, offset
, buf
, count
,
277 bs
->total_sectors
, ret
, errno
, strerror(errno
));
279 /* Try harder for CDrom. */
280 if (bs
->type
== BDRV_TYPE_CDROM
) {
281 lseek(s
->fd
, offset
, SEEK_SET
);
282 ret
= read(s
->fd
, buf
, count
);
284 goto label__raw_read__success
;
285 lseek(s
->fd
, offset
, SEEK_SET
);
286 ret
= read(s
->fd
, buf
, count
);
288 goto label__raw_read__success
;
290 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
291 "] retry read failed %d : %d = %s\n",
292 s
->fd
, bs
->filename
, offset
, buf
, count
,
293 bs
->total_sectors
, ret
, errno
, strerror(errno
));
296 label__raw_read__success
:
298 return (ret
< 0) ? -errno
: ret
;
302 * offset and count are in bytes, but must be multiples of 512 for files
303 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
305 * This function may be called without alignment if the caller ensures
306 * that O_DIRECT is not in effect.
308 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
309 const uint8_t *buf
, int count
)
311 BDRVRawState
*s
= bs
->opaque
;
318 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
319 ++(s
->lseek_err_cnt
);
320 if(s
->lseek_err_cnt
) {
321 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
322 PRId64
"] lseek failed : %d = %s\n",
323 s
->fd
, bs
->filename
, offset
, buf
, count
,
324 bs
->total_sectors
, errno
, strerror(errno
));
328 s
->lseek_err_cnt
= 0;
330 ret
= write(s
->fd
, buf
, count
);
332 goto label__raw_write__success
;
334 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
335 "] write failed %d : %d = %s\n",
336 s
->fd
, bs
->filename
, offset
, buf
, count
,
337 bs
->total_sectors
, ret
, errno
, strerror(errno
));
339 label__raw_write__success
:
341 return (ret
< 0) ? -errno
: ret
;
346 * offset and count are in bytes and possibly not aligned. For files opened
347 * with O_DIRECT, necessary alignments are ensured before calling
348 * raw_pread_aligned to do the actual read.
350 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
351 uint8_t *buf
, int count
)
353 BDRVRawState
*s
= bs
->opaque
;
354 int size
, ret
, shift
, sum
;
358 if (s
->aligned_buf
!= NULL
) {
360 if (offset
& 0x1ff) {
361 /* align offset on a 512 bytes boundary */
363 shift
= offset
& 0x1ff;
364 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
365 if (size
> ALIGNED_BUFFER_SIZE
)
366 size
= ALIGNED_BUFFER_SIZE
;
367 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
374 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
384 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
386 /* read on aligned buffer */
390 size
= (count
+ 0x1ff) & ~0x1ff;
391 if (size
> ALIGNED_BUFFER_SIZE
)
392 size
= ALIGNED_BUFFER_SIZE
;
394 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
402 memcpy(buf
, s
->aligned_buf
, size
);
414 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
417 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
418 uint8_t *buf
, int nb_sectors
)
422 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
423 if (ret
== (nb_sectors
* 512))
429 * offset and count are in bytes and possibly not aligned. For files opened
430 * with O_DIRECT, necessary alignments are ensured before calling
431 * raw_pwrite_aligned to do the actual write.
433 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
434 const uint8_t *buf
, int count
)
436 BDRVRawState
*s
= bs
->opaque
;
437 int size
, ret
, shift
, sum
;
441 if (s
->aligned_buf
!= NULL
) {
443 if (offset
& 0x1ff) {
444 /* align offset on a 512 bytes boundary */
445 shift
= offset
& 0x1ff;
446 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
453 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
455 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
467 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
469 while ((size
= (count
& ~0x1ff)) != 0) {
471 if (size
> ALIGNED_BUFFER_SIZE
)
472 size
= ALIGNED_BUFFER_SIZE
;
474 memcpy(s
->aligned_buf
, buf
, size
);
476 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
485 /* here, count < 512 because (count & ~0x1ff) == 0 */
487 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
490 memcpy(s
->aligned_buf
, buf
, count
);
492 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
503 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
506 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
507 const uint8_t *buf
, int nb_sectors
)
510 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
511 if (ret
== (nb_sectors
* 512))
517 * Check if all memory in this vector is sector aligned.
519 static int qiov_is_aligned(QEMUIOVector
*qiov
)
523 for (i
= 0; i
< qiov
->niov
; i
++) {
524 if ((uintptr_t) qiov
->iov
[i
].iov_base
% 512) {
532 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
533 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
534 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
536 BDRVRawState
*s
= bs
->opaque
;
542 * If O_DIRECT is used the buffer needs to be aligned on a sector
543 * boundary. Check if this is the case or telll the low-level
544 * driver that it needs to copy the buffer.
546 if (s
->aligned_buf
) {
547 if (!qiov_is_aligned(qiov
)) {
548 type
|= QEMU_AIO_MISALIGNED
;
549 #ifdef CONFIG_LINUX_AIO
550 } else if (s
->use_aio
) {
551 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
552 nb_sectors
, cb
, opaque
, type
);
557 return paio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
, nb_sectors
,
561 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
562 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
563 BlockDriverCompletionFunc
*cb
, void *opaque
)
565 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
566 cb
, opaque
, QEMU_AIO_READ
);
569 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
570 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
571 BlockDriverCompletionFunc
*cb
, void *opaque
)
573 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
574 cb
, opaque
, QEMU_AIO_WRITE
);
577 static void raw_close(BlockDriverState
*bs
)
579 BDRVRawState
*s
= bs
->opaque
;
583 if (s
->aligned_buf
!= NULL
)
584 qemu_free(s
->aligned_buf
);
588 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
590 BDRVRawState
*s
= bs
->opaque
;
591 if (s
->type
!= FTYPE_FILE
)
593 if (ftruncate(s
->fd
, offset
) < 0)
599 static int64_t raw_getlength(BlockDriverState
*bs
)
601 BDRVRawState
*s
= bs
->opaque
;
607 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
610 if (ioctl(fd
, DIOCGDINFO
, &dl
))
612 return (uint64_t)dl
.d_secsize
*
613 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
617 #else /* !__OpenBSD__ */
618 static int64_t raw_getlength(BlockDriverState
*bs
)
620 BDRVRawState
*s
= bs
->opaque
;
630 struct dk_minfo minfo
;
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
);
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) {
678 * use the DKIOCGMEDIAINFO ioctl to read the size.
680 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
682 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
683 } else /* there are reports that lseek on some devices
684 fails, but irc discussion said that contingency
685 on contingency was overkill */
688 size
= lseek(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
/ 512;
708 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
713 if (ftruncate(fd
, total_size
* 512) != 0) {
716 if (close(fd
) != 0) {
723 static void raw_flush(BlockDriverState
*bs
)
725 BDRVRawState
*s
= bs
->opaque
;
730 static QEMUOptionParameter raw_create_options
[] = {
732 .name
= BLOCK_OPT_SIZE
,
734 .help
= "Virtual disk size"
739 static BlockDriver bdrv_raw
= {
740 .format_name
= "raw",
741 .instance_size
= sizeof(BDRVRawState
),
742 .bdrv_probe
= NULL
, /* no probe for protocols */
743 .bdrv_open
= raw_open
,
744 .bdrv_read
= raw_read
,
745 .bdrv_write
= raw_write
,
746 .bdrv_close
= raw_close
,
747 .bdrv_create
= raw_create
,
748 .bdrv_flush
= raw_flush
,
750 .bdrv_aio_readv
= raw_aio_readv
,
751 .bdrv_aio_writev
= raw_aio_writev
,
753 .bdrv_truncate
= raw_truncate
,
754 .bdrv_getlength
= raw_getlength
,
756 .create_options
= raw_create_options
,
759 /***********************************************/
763 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
764 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
766 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
768 kern_return_t kernResult
;
769 mach_port_t masterPort
;
770 CFMutableDictionaryRef classesToMatch
;
772 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
773 if ( KERN_SUCCESS
!= kernResult
) {
774 printf( "IOMasterPort returned %d\n", kernResult
);
777 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
778 if ( classesToMatch
== NULL
) {
779 printf( "IOServiceMatching returned a NULL dictionary.\n" );
781 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
783 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
784 if ( KERN_SUCCESS
!= kernResult
)
786 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
792 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
794 io_object_t nextMedia
;
795 kern_return_t kernResult
= KERN_FAILURE
;
797 nextMedia
= IOIteratorNext( mediaIterator
);
800 CFTypeRef bsdPathAsCFString
;
801 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
802 if ( bsdPathAsCFString
) {
803 size_t devPathLength
;
804 strcpy( bsdPath
, _PATH_DEV
);
805 strcat( bsdPath
, "r" );
806 devPathLength
= strlen( bsdPath
);
807 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
808 kernResult
= KERN_SUCCESS
;
810 CFRelease( bsdPathAsCFString
);
812 IOObjectRelease( nextMedia
);
820 static int hdev_probe_device(const char *filename
)
824 /* allow a dedicated CD-ROM driver to match with a higher priority */
825 if (strstart(filename
, "/dev/cdrom", NULL
))
828 if (stat(filename
, &st
) >= 0 &&
829 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
836 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
838 BDRVRawState
*s
= bs
->opaque
;
841 if (strstart(filename
, "/dev/cdrom", NULL
)) {
842 kern_return_t kernResult
;
843 io_iterator_t mediaIterator
;
844 char bsdPath
[ MAXPATHLEN
];
847 kernResult
= FindEjectableCDMedia( &mediaIterator
);
848 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
850 if ( bsdPath
[ 0 ] != '\0' ) {
851 strcat(bsdPath
,"s0");
852 /* some CDs don't have a partition 0 */
853 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
855 bsdPath
[strlen(bsdPath
)-1] = '1';
863 IOObjectRelease( mediaIterator
);
867 s
->type
= FTYPE_FILE
;
868 #if defined(__linux__)
869 if (strstart(filename
, "/dev/sg", NULL
)) {
874 return raw_open_common(bs
, filename
, flags
, 0);
877 #if defined(__linux__)
878 /* Note: we do not have a reliable method to detect if the floppy is
879 present. The current method is to try to open the floppy at every
880 I/O and to keep it opened during a few hundreds of ms. */
881 static int fd_open(BlockDriverState
*bs
)
883 BDRVRawState
*s
= bs
->opaque
;
884 int last_media_present
;
886 if (s
->type
!= FTYPE_FD
)
888 last_media_present
= (s
->fd
>= 0);
890 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
894 printf("Floppy closed\n");
898 if (s
->fd_got_error
&&
899 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
901 printf("No floppy (open delayed)\n");
905 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
907 s
->fd_error_time
= qemu_get_clock(rt_clock
);
909 if (last_media_present
)
910 s
->fd_media_changed
= 1;
912 printf("No floppy\n");
917 printf("Floppy opened\n");
920 if (!last_media_present
)
921 s
->fd_media_changed
= 1;
922 s
->fd_open_time
= qemu_get_clock(rt_clock
);
927 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
929 BDRVRawState
*s
= bs
->opaque
;
931 return ioctl(s
->fd
, req
, buf
);
934 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
935 unsigned long int req
, void *buf
,
936 BlockDriverCompletionFunc
*cb
, void *opaque
)
938 BDRVRawState
*s
= bs
->opaque
;
942 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
945 #elif defined(__FreeBSD__)
946 static int fd_open(BlockDriverState
*bs
)
948 BDRVRawState
*s
= bs
->opaque
;
950 /* this is just to ensure s->fd is sane (its called by io ops) */
955 #else /* !linux && !FreeBSD */
957 static int fd_open(BlockDriverState
*bs
)
962 #endif /* !linux && !FreeBSD */
964 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
968 struct stat stat_buf
;
969 int64_t total_size
= 0;
971 /* Read out options */
972 while (options
&& options
->name
) {
973 if (!strcmp(options
->name
, "size")) {
974 total_size
= options
->value
.n
/ 512;
979 fd
= open(filename
, O_WRONLY
| O_BINARY
);
983 if (fstat(fd
, &stat_buf
) < 0)
985 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
987 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
994 static BlockDriver bdrv_host_device
= {
995 .format_name
= "host_device",
996 .instance_size
= sizeof(BDRVRawState
),
997 .bdrv_probe_device
= hdev_probe_device
,
998 .bdrv_open
= hdev_open
,
999 .bdrv_close
= raw_close
,
1000 .bdrv_create
= hdev_create
,
1001 .bdrv_flush
= raw_flush
,
1003 .bdrv_aio_readv
= raw_aio_readv
,
1004 .bdrv_aio_writev
= raw_aio_writev
,
1006 .bdrv_read
= raw_read
,
1007 .bdrv_write
= raw_write
,
1008 .bdrv_getlength
= raw_getlength
,
1010 /* generic scsi device */
1012 .bdrv_ioctl
= hdev_ioctl
,
1013 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1018 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1020 BDRVRawState
*s
= bs
->opaque
;
1025 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1026 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1030 /* close fd so that we can reopen it as needed */
1033 s
->fd_media_changed
= 1;
1038 static int floppy_probe_device(const char *filename
)
1040 if (strstart(filename
, "/dev/fd", NULL
))
1046 static int floppy_is_inserted(BlockDriverState
*bs
)
1048 return fd_open(bs
) >= 0;
1051 static int floppy_media_changed(BlockDriverState
*bs
)
1053 BDRVRawState
*s
= bs
->opaque
;
1057 * XXX: we do not have a true media changed indication.
1058 * It does not work if the floppy is changed without trying to read it.
1061 ret
= s
->fd_media_changed
;
1062 s
->fd_media_changed
= 0;
1064 printf("Floppy changed=%d\n", ret
);
1069 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1071 BDRVRawState
*s
= bs
->opaque
;
1078 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1080 if (ioctl(fd
, FDEJECT
, 0) < 0)
1088 static BlockDriver bdrv_host_floppy
= {
1089 .format_name
= "host_floppy",
1090 .instance_size
= sizeof(BDRVRawState
),
1091 .bdrv_probe_device
= floppy_probe_device
,
1092 .bdrv_open
= floppy_open
,
1093 .bdrv_close
= raw_close
,
1094 .bdrv_create
= hdev_create
,
1095 .bdrv_flush
= raw_flush
,
1097 .bdrv_aio_readv
= raw_aio_readv
,
1098 .bdrv_aio_writev
= raw_aio_writev
,
1100 .bdrv_read
= raw_read
,
1101 .bdrv_write
= raw_write
,
1102 .bdrv_getlength
= raw_getlength
,
1104 /* removable device support */
1105 .bdrv_is_inserted
= floppy_is_inserted
,
1106 .bdrv_media_changed
= floppy_media_changed
,
1107 .bdrv_eject
= floppy_eject
,
1110 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1112 BDRVRawState
*s
= bs
->opaque
;
1116 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1117 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1120 static int cdrom_probe_device(const char *filename
)
1122 if (strstart(filename
, "/dev/cd", NULL
))
1127 static int cdrom_is_inserted(BlockDriverState
*bs
)
1129 BDRVRawState
*s
= bs
->opaque
;
1132 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1133 if (ret
== CDS_DISC_OK
)
1138 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1140 BDRVRawState
*s
= bs
->opaque
;
1143 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1144 perror("CDROMEJECT");
1146 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1147 perror("CDROMEJECT");
1153 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1155 BDRVRawState
*s
= bs
->opaque
;
1157 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1159 * Note: an error can happen if the distribution automatically
1162 /* perror("CDROM_LOCKDOOR"); */
1168 static BlockDriver bdrv_host_cdrom
= {
1169 .format_name
= "host_cdrom",
1170 .instance_size
= sizeof(BDRVRawState
),
1171 .bdrv_probe_device
= cdrom_probe_device
,
1172 .bdrv_open
= cdrom_open
,
1173 .bdrv_close
= raw_close
,
1174 .bdrv_create
= hdev_create
,
1175 .bdrv_flush
= raw_flush
,
1177 .bdrv_aio_readv
= raw_aio_readv
,
1178 .bdrv_aio_writev
= raw_aio_writev
,
1180 .bdrv_read
= raw_read
,
1181 .bdrv_write
= raw_write
,
1182 .bdrv_getlength
= raw_getlength
,
1184 /* removable device support */
1185 .bdrv_is_inserted
= cdrom_is_inserted
,
1186 .bdrv_eject
= cdrom_eject
,
1187 .bdrv_set_locked
= cdrom_set_locked
,
1189 /* generic scsi device */
1190 .bdrv_ioctl
= hdev_ioctl
,
1191 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1193 #endif /* __linux__ */
1196 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1198 BDRVRawState
*s
= bs
->opaque
;
1203 ret
= raw_open_common(bs
, filename
, flags
, 0);
1207 /* make sure the door isnt locked at this time */
1208 ioctl(s
->fd
, CDIOCALLOW
);
1212 static int cdrom_probe_device(const char *filename
)
1214 if (strstart(filename
, "/dev/cd", NULL
) ||
1215 strstart(filename
, "/dev/acd", NULL
))
1220 static int cdrom_reopen(BlockDriverState
*bs
)
1222 BDRVRawState
*s
= bs
->opaque
;
1226 * Force reread of possibly changed/newly loaded disc,
1227 * FreeBSD seems to not notice sometimes...
1231 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1238 /* make sure the door isnt locked at this time */
1239 ioctl(s
->fd
, CDIOCALLOW
);
1243 static int cdrom_is_inserted(BlockDriverState
*bs
)
1245 return raw_getlength(bs
) > 0;
1248 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1250 BDRVRawState
*s
= bs
->opaque
;
1255 (void) ioctl(s
->fd
, CDIOCALLOW
);
1258 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1259 perror("CDIOCEJECT");
1261 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1262 perror("CDIOCCLOSE");
1265 if (cdrom_reopen(bs
) < 0)
1270 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1272 BDRVRawState
*s
= bs
->opaque
;
1276 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1278 * Note: an error can happen if the distribution automatically
1281 /* perror("CDROM_LOCKDOOR"); */
1287 static BlockDriver bdrv_host_cdrom
= {
1288 .format_name
= "host_cdrom",
1289 .instance_size
= sizeof(BDRVRawState
),
1290 .bdrv_probe_device
= cdrom_probe_device
,
1291 .bdrv_open
= cdrom_open
,
1292 .bdrv_close
= raw_close
,
1293 .bdrv_create
= hdev_create
,
1294 .bdrv_flush
= raw_flush
,
1296 .bdrv_aio_readv
= raw_aio_readv
,
1297 .bdrv_aio_writev
= raw_aio_writev
,
1299 .bdrv_read
= raw_read
,
1300 .bdrv_write
= raw_write
,
1301 .bdrv_getlength
= raw_getlength
,
1303 /* removable device support */
1304 .bdrv_is_inserted
= cdrom_is_inserted
,
1305 .bdrv_eject
= cdrom_eject
,
1306 .bdrv_set_locked
= cdrom_set_locked
,
1308 #endif /* __FreeBSD__ */
1310 static void bdrv_raw_init(void)
1313 * Register all the drivers. Note that order is important, the driver
1314 * registered last will get probed first.
1316 bdrv_register(&bdrv_raw
);
1317 bdrv_register(&bdrv_host_device
);
1319 bdrv_register(&bdrv_host_floppy
);
1320 bdrv_register(&bdrv_host_cdrom
);
1323 bdrv_register(&bdrv_host_cdrom
);
1327 block_init(bdrv_raw_init
);