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"
32 #include "block/raw-posix-aio.h"
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
47 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
63 #include <sys/ioctl.h>
64 #include <sys/disklabel.h>
69 #include <sys/ioctl.h>
70 #include <sys/diskslice.h>
73 //#define DEBUG_FLOPPY
76 #if defined(DEBUG_BLOCK)
77 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
78 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
80 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
83 /* OS X does not have O_DSYNC */
86 #define O_DSYNC O_SYNC
87 #elif defined(O_FSYNC)
88 #define O_DSYNC O_FSYNC
92 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
94 #define O_DIRECT O_DSYNC
101 #define ALIGNED_BUFFER_SIZE (32 * 512)
103 /* if the FD is not accessed during that time (in ms), we try to
104 reopen it to see if the disk has been changed */
105 #define FD_OPEN_TIMEOUT 1000
107 typedef struct BDRVRawState
{
110 unsigned int lseek_err_cnt
;
113 #if defined(__linux__)
114 /* linux floppy specific */
115 int64_t fd_open_time
;
116 int64_t fd_error_time
;
118 int fd_media_changed
;
120 #ifdef CONFIG_LINUX_AIO
123 uint8_t* aligned_buf
;
126 static int fd_open(BlockDriverState
*bs
);
127 static int64_t raw_getlength(BlockDriverState
*bs
);
129 #if defined(__FreeBSD__)
130 static int cdrom_reopen(BlockDriverState
*bs
);
133 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
134 int bdrv_flags
, int open_flags
)
136 BDRVRawState
*s
= bs
->opaque
;
139 s
->lseek_err_cnt
= 0;
141 s
->open_flags
= open_flags
| O_BINARY
;
142 s
->open_flags
&= ~O_ACCMODE
;
143 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
144 s
->open_flags
|= O_RDWR
;
146 s
->open_flags
|= O_RDONLY
;
150 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
151 * and O_DIRECT for no caching. */
152 if ((bdrv_flags
& BDRV_O_NOCACHE
))
153 s
->open_flags
|= O_DIRECT
;
154 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
155 s
->open_flags
|= O_DSYNC
;
158 fd
= open(filename
, s
->open_flags
, 0644);
166 s
->aligned_buf
= NULL
;
168 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
169 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
170 if (s
->aligned_buf
== NULL
) {
175 #ifdef CONFIG_LINUX_AIO
176 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
177 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
179 /* We're falling back to POSIX AIO in some cases */
182 s
->aio_ctx
= laio_init();
190 s
->aio_ctx
= paio_init();
194 #ifdef CONFIG_LINUX_AIO
202 qemu_vfree(s
->aligned_buf
);
208 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
210 BDRVRawState
*s
= bs
->opaque
;
213 s
->type
= FTYPE_FILE
;
214 if (flags
& BDRV_O_CREAT
)
215 open_flags
= O_CREAT
| O_TRUNC
;
217 return raw_open_common(bs
, filename
, flags
, open_flags
);
220 /* XXX: use host sector size if necessary with:
221 #ifdef DIOCGSECTORSIZE
223 unsigned int sectorsize = 512;
224 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
225 sectorsize > bufsize)
226 bufsize = sectorsize;
230 u_int32_t blockSize = 512;
231 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
238 * offset and count are in bytes, but must be multiples of 512 for files
239 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
241 * This function may be called without alignment if the caller ensures
242 * that O_DIRECT is not in effect.
244 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
245 uint8_t *buf
, int count
)
247 BDRVRawState
*s
= bs
->opaque
;
254 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
255 ++(s
->lseek_err_cnt
);
256 if(s
->lseek_err_cnt
<= 10) {
257 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
258 "] lseek failed : %d = %s\n",
259 s
->fd
, bs
->filename
, offset
, buf
, count
,
260 bs
->total_sectors
, errno
, strerror(errno
));
266 ret
= read(s
->fd
, buf
, count
);
268 goto label__raw_read__success
;
270 /* Allow reads beyond the end (needed for pwrite) */
271 if ((ret
== 0) && bs
->growable
) {
272 int64_t size
= raw_getlength(bs
);
273 if (offset
>= size
) {
274 memset(buf
, 0, count
);
276 goto label__raw_read__success
;
280 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
281 "] read failed %d : %d = %s\n",
282 s
->fd
, bs
->filename
, offset
, buf
, count
,
283 bs
->total_sectors
, ret
, errno
, strerror(errno
));
285 /* Try harder for CDrom. */
286 if (bs
->type
== BDRV_TYPE_CDROM
) {
287 lseek(s
->fd
, offset
, SEEK_SET
);
288 ret
= read(s
->fd
, buf
, count
);
290 goto label__raw_read__success
;
291 lseek(s
->fd
, offset
, SEEK_SET
);
292 ret
= read(s
->fd
, buf
, count
);
294 goto label__raw_read__success
;
296 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
297 "] retry read failed %d : %d = %s\n",
298 s
->fd
, bs
->filename
, offset
, buf
, count
,
299 bs
->total_sectors
, ret
, errno
, strerror(errno
));
302 label__raw_read__success
:
304 return (ret
< 0) ? -errno
: ret
;
308 * offset and count are in bytes, but must be multiples of 512 for files
309 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
311 * This function may be called without alignment if the caller ensures
312 * that O_DIRECT is not in effect.
314 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
315 const uint8_t *buf
, int count
)
317 BDRVRawState
*s
= bs
->opaque
;
324 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
325 ++(s
->lseek_err_cnt
);
326 if(s
->lseek_err_cnt
) {
327 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
328 PRId64
"] lseek failed : %d = %s\n",
329 s
->fd
, bs
->filename
, offset
, buf
, count
,
330 bs
->total_sectors
, errno
, strerror(errno
));
334 s
->lseek_err_cnt
= 0;
336 ret
= write(s
->fd
, buf
, count
);
338 goto label__raw_write__success
;
340 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
341 "] write failed %d : %d = %s\n",
342 s
->fd
, bs
->filename
, offset
, buf
, count
,
343 bs
->total_sectors
, ret
, errno
, strerror(errno
));
345 label__raw_write__success
:
347 return (ret
< 0) ? -errno
: ret
;
352 * offset and count are in bytes and possibly not aligned. For files opened
353 * with O_DIRECT, necessary alignments are ensured before calling
354 * raw_pread_aligned to do the actual read.
356 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
357 uint8_t *buf
, int count
)
359 BDRVRawState
*s
= bs
->opaque
;
360 int size
, ret
, shift
, sum
;
364 if (s
->aligned_buf
!= NULL
) {
366 if (offset
& 0x1ff) {
367 /* align offset on a 512 bytes boundary */
369 shift
= offset
& 0x1ff;
370 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
371 if (size
> ALIGNED_BUFFER_SIZE
)
372 size
= ALIGNED_BUFFER_SIZE
;
373 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
380 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
390 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
392 /* read on aligned buffer */
396 size
= (count
+ 0x1ff) & ~0x1ff;
397 if (size
> ALIGNED_BUFFER_SIZE
)
398 size
= ALIGNED_BUFFER_SIZE
;
400 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
408 memcpy(buf
, s
->aligned_buf
, size
);
420 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
423 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
424 uint8_t *buf
, int nb_sectors
)
428 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
429 if (ret
== (nb_sectors
* 512))
435 * offset and count are in bytes and possibly not aligned. For files opened
436 * with O_DIRECT, necessary alignments are ensured before calling
437 * raw_pwrite_aligned to do the actual write.
439 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
440 const uint8_t *buf
, int count
)
442 BDRVRawState
*s
= bs
->opaque
;
443 int size
, ret
, shift
, sum
;
447 if (s
->aligned_buf
!= NULL
) {
449 if (offset
& 0x1ff) {
450 /* align offset on a 512 bytes boundary */
451 shift
= offset
& 0x1ff;
452 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
459 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
461 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
473 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
475 while ((size
= (count
& ~0x1ff)) != 0) {
477 if (size
> ALIGNED_BUFFER_SIZE
)
478 size
= ALIGNED_BUFFER_SIZE
;
480 memcpy(s
->aligned_buf
, buf
, size
);
482 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
491 /* here, count < 512 because (count & ~0x1ff) == 0 */
493 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
496 memcpy(s
->aligned_buf
, buf
, count
);
498 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
509 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
512 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
513 const uint8_t *buf
, int nb_sectors
)
516 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
517 if (ret
== (nb_sectors
* 512))
523 * Check if all memory in this vector is sector aligned.
525 static int qiov_is_aligned(QEMUIOVector
*qiov
)
529 for (i
= 0; i
< qiov
->niov
; i
++) {
530 if ((uintptr_t) qiov
->iov
[i
].iov_base
% 512) {
538 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
539 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
540 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
542 BDRVRawState
*s
= bs
->opaque
;
548 * If O_DIRECT is used the buffer needs to be aligned on a sector
549 * boundary. Check if this is the case or telll the low-level
550 * driver that it needs to copy the buffer.
552 if (s
->aligned_buf
) {
553 if (!qiov_is_aligned(qiov
)) {
554 type
|= QEMU_AIO_MISALIGNED
;
555 #ifdef CONFIG_LINUX_AIO
556 } else if (s
->use_aio
) {
557 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
558 nb_sectors
, cb
, opaque
, type
);
563 return paio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
, nb_sectors
,
567 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
568 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
569 BlockDriverCompletionFunc
*cb
, void *opaque
)
571 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
572 cb
, opaque
, QEMU_AIO_READ
);
575 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
576 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
577 BlockDriverCompletionFunc
*cb
, void *opaque
)
579 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
580 cb
, opaque
, QEMU_AIO_WRITE
);
583 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
584 BlockDriverCompletionFunc
*cb
, void *opaque
)
586 BDRVRawState
*s
= bs
->opaque
;
591 return paio_submit(bs
, s
->aio_ctx
, s
->fd
, 0, NULL
, 0,
592 cb
, opaque
, QEMU_AIO_FLUSH
);
595 static void raw_close(BlockDriverState
*bs
)
597 BDRVRawState
*s
= bs
->opaque
;
601 if (s
->aligned_buf
!= NULL
)
602 qemu_free(s
->aligned_buf
);
606 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
608 BDRVRawState
*s
= bs
->opaque
;
609 if (s
->type
!= FTYPE_FILE
)
611 if (ftruncate(s
->fd
, offset
) < 0)
617 static int64_t raw_getlength(BlockDriverState
*bs
)
619 BDRVRawState
*s
= bs
->opaque
;
625 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
628 if (ioctl(fd
, DIOCGDINFO
, &dl
))
630 return (uint64_t)dl
.d_secsize
*
631 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
635 #else /* !__OpenBSD__ */
636 static int64_t raw_getlength(BlockDriverState
*bs
)
638 BDRVRawState
*s
= bs
->opaque
;
648 struct dk_minfo minfo
;
661 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
662 #ifdef DIOCGMEDIASIZE
663 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
664 #elif defined(DIOCGPART)
667 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
668 size
= pi
.media_size
;
675 size
= LONG_LONG_MAX
;
677 size
= lseek(fd
, 0LL, SEEK_END
);
682 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
683 if (size
== 2048LL * (unsigned)-1)
685 /* XXX no disc? maybe we need to reopen... */
686 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
696 * use the DKIOCGMEDIAINFO ioctl to read the size.
698 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
700 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
701 } else /* there are reports that lseek on some devices
702 fails, but irc discussion said that contingency
703 on contingency was overkill */
706 size
= lseek(fd
, 0, SEEK_END
);
712 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
716 int64_t total_size
= 0;
718 /* Read out options */
719 while (options
&& options
->name
) {
720 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
721 total_size
= options
->value
.n
/ 512;
726 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
731 if (ftruncate(fd
, total_size
* 512) != 0) {
734 if (close(fd
) != 0) {
741 static void raw_flush(BlockDriverState
*bs
)
743 BDRVRawState
*s
= bs
->opaque
;
744 qemu_fdatasync(s
->fd
);
748 static QEMUOptionParameter raw_create_options
[] = {
750 .name
= BLOCK_OPT_SIZE
,
752 .help
= "Virtual disk size"
757 static BlockDriver bdrv_raw
= {
758 .format_name
= "raw",
759 .instance_size
= sizeof(BDRVRawState
),
760 .bdrv_probe
= NULL
, /* no probe for protocols */
761 .bdrv_open
= raw_open
,
762 .bdrv_read
= raw_read
,
763 .bdrv_write
= raw_write
,
764 .bdrv_close
= raw_close
,
765 .bdrv_create
= raw_create
,
766 .bdrv_flush
= raw_flush
,
768 .bdrv_aio_readv
= raw_aio_readv
,
769 .bdrv_aio_writev
= raw_aio_writev
,
770 .bdrv_aio_flush
= raw_aio_flush
,
772 .bdrv_truncate
= raw_truncate
,
773 .bdrv_getlength
= raw_getlength
,
775 .create_options
= raw_create_options
,
778 /***********************************************/
782 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
783 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
785 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
787 kern_return_t kernResult
;
788 mach_port_t masterPort
;
789 CFMutableDictionaryRef classesToMatch
;
791 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
792 if ( KERN_SUCCESS
!= kernResult
) {
793 printf( "IOMasterPort returned %d\n", kernResult
);
796 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
797 if ( classesToMatch
== NULL
) {
798 printf( "IOServiceMatching returned a NULL dictionary.\n" );
800 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
802 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
803 if ( KERN_SUCCESS
!= kernResult
)
805 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
811 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
813 io_object_t nextMedia
;
814 kern_return_t kernResult
= KERN_FAILURE
;
816 nextMedia
= IOIteratorNext( mediaIterator
);
819 CFTypeRef bsdPathAsCFString
;
820 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
821 if ( bsdPathAsCFString
) {
822 size_t devPathLength
;
823 strcpy( bsdPath
, _PATH_DEV
);
824 strcat( bsdPath
, "r" );
825 devPathLength
= strlen( bsdPath
);
826 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
827 kernResult
= KERN_SUCCESS
;
829 CFRelease( bsdPathAsCFString
);
831 IOObjectRelease( nextMedia
);
839 static int hdev_probe_device(const char *filename
)
843 /* allow a dedicated CD-ROM driver to match with a higher priority */
844 if (strstart(filename
, "/dev/cdrom", NULL
))
847 if (stat(filename
, &st
) >= 0 &&
848 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
855 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
857 BDRVRawState
*s
= bs
->opaque
;
860 if (strstart(filename
, "/dev/cdrom", NULL
)) {
861 kern_return_t kernResult
;
862 io_iterator_t mediaIterator
;
863 char bsdPath
[ MAXPATHLEN
];
866 kernResult
= FindEjectableCDMedia( &mediaIterator
);
867 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
869 if ( bsdPath
[ 0 ] != '\0' ) {
870 strcat(bsdPath
,"s0");
871 /* some CDs don't have a partition 0 */
872 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
874 bsdPath
[strlen(bsdPath
)-1] = '1';
882 IOObjectRelease( mediaIterator
);
886 s
->type
= FTYPE_FILE
;
887 #if defined(__linux__)
888 if (strstart(filename
, "/dev/sg", NULL
)) {
893 return raw_open_common(bs
, filename
, flags
, 0);
896 #if defined(__linux__)
897 /* Note: we do not have a reliable method to detect if the floppy is
898 present. The current method is to try to open the floppy at every
899 I/O and to keep it opened during a few hundreds of ms. */
900 static int fd_open(BlockDriverState
*bs
)
902 BDRVRawState
*s
= bs
->opaque
;
903 int last_media_present
;
905 if (s
->type
!= FTYPE_FD
)
907 last_media_present
= (s
->fd
>= 0);
909 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
913 printf("Floppy closed\n");
917 if (s
->fd_got_error
&&
918 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
920 printf("No floppy (open delayed)\n");
924 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
926 s
->fd_error_time
= qemu_get_clock(rt_clock
);
928 if (last_media_present
)
929 s
->fd_media_changed
= 1;
931 printf("No floppy\n");
936 printf("Floppy opened\n");
939 if (!last_media_present
)
940 s
->fd_media_changed
= 1;
941 s
->fd_open_time
= qemu_get_clock(rt_clock
);
946 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
948 BDRVRawState
*s
= bs
->opaque
;
950 return ioctl(s
->fd
, req
, buf
);
953 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
954 unsigned long int req
, void *buf
,
955 BlockDriverCompletionFunc
*cb
, void *opaque
)
957 BDRVRawState
*s
= bs
->opaque
;
961 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
964 #elif defined(__FreeBSD__)
965 static int fd_open(BlockDriverState
*bs
)
967 BDRVRawState
*s
= bs
->opaque
;
969 /* this is just to ensure s->fd is sane (its called by io ops) */
974 #else /* !linux && !FreeBSD */
976 static int fd_open(BlockDriverState
*bs
)
981 #endif /* !linux && !FreeBSD */
983 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
987 struct stat stat_buf
;
988 int64_t total_size
= 0;
990 /* Read out options */
991 while (options
&& options
->name
) {
992 if (!strcmp(options
->name
, "size")) {
993 total_size
= options
->value
.n
/ 512;
998 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1002 if (fstat(fd
, &stat_buf
) < 0)
1004 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1006 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1013 static BlockDriver bdrv_host_device
= {
1014 .format_name
= "host_device",
1015 .instance_size
= sizeof(BDRVRawState
),
1016 .bdrv_probe_device
= hdev_probe_device
,
1017 .bdrv_open
= hdev_open
,
1018 .bdrv_close
= raw_close
,
1019 .bdrv_create
= hdev_create
,
1020 .create_options
= raw_create_options
,
1021 .bdrv_flush
= raw_flush
,
1023 .bdrv_aio_readv
= raw_aio_readv
,
1024 .bdrv_aio_writev
= raw_aio_writev
,
1025 .bdrv_aio_flush
= raw_aio_flush
,
1027 .bdrv_read
= raw_read
,
1028 .bdrv_write
= raw_write
,
1029 .bdrv_getlength
= raw_getlength
,
1031 /* generic scsi device */
1033 .bdrv_ioctl
= hdev_ioctl
,
1034 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1039 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1041 BDRVRawState
*s
= bs
->opaque
;
1046 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1047 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1051 /* close fd so that we can reopen it as needed */
1054 s
->fd_media_changed
= 1;
1059 static int floppy_probe_device(const char *filename
)
1061 if (strstart(filename
, "/dev/fd", NULL
))
1067 static int floppy_is_inserted(BlockDriverState
*bs
)
1069 return fd_open(bs
) >= 0;
1072 static int floppy_media_changed(BlockDriverState
*bs
)
1074 BDRVRawState
*s
= bs
->opaque
;
1078 * XXX: we do not have a true media changed indication.
1079 * It does not work if the floppy is changed without trying to read it.
1082 ret
= s
->fd_media_changed
;
1083 s
->fd_media_changed
= 0;
1085 printf("Floppy changed=%d\n", ret
);
1090 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1092 BDRVRawState
*s
= bs
->opaque
;
1099 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1101 if (ioctl(fd
, FDEJECT
, 0) < 0)
1109 static BlockDriver bdrv_host_floppy
= {
1110 .format_name
= "host_floppy",
1111 .instance_size
= sizeof(BDRVRawState
),
1112 .bdrv_probe_device
= floppy_probe_device
,
1113 .bdrv_open
= floppy_open
,
1114 .bdrv_close
= raw_close
,
1115 .bdrv_create
= hdev_create
,
1116 .create_options
= raw_create_options
,
1117 .bdrv_flush
= raw_flush
,
1119 .bdrv_aio_readv
= raw_aio_readv
,
1120 .bdrv_aio_writev
= raw_aio_writev
,
1121 .bdrv_aio_flush
= raw_aio_flush
,
1123 .bdrv_read
= raw_read
,
1124 .bdrv_write
= raw_write
,
1125 .bdrv_getlength
= raw_getlength
,
1127 /* removable device support */
1128 .bdrv_is_inserted
= floppy_is_inserted
,
1129 .bdrv_media_changed
= floppy_media_changed
,
1130 .bdrv_eject
= floppy_eject
,
1133 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1135 BDRVRawState
*s
= bs
->opaque
;
1139 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1140 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1143 static int cdrom_probe_device(const char *filename
)
1145 if (strstart(filename
, "/dev/cd", NULL
))
1150 static int cdrom_is_inserted(BlockDriverState
*bs
)
1152 BDRVRawState
*s
= bs
->opaque
;
1155 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1156 if (ret
== CDS_DISC_OK
)
1161 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1163 BDRVRawState
*s
= bs
->opaque
;
1166 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1167 perror("CDROMEJECT");
1169 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1170 perror("CDROMEJECT");
1176 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1178 BDRVRawState
*s
= bs
->opaque
;
1180 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1182 * Note: an error can happen if the distribution automatically
1185 /* perror("CDROM_LOCKDOOR"); */
1191 static BlockDriver bdrv_host_cdrom
= {
1192 .format_name
= "host_cdrom",
1193 .instance_size
= sizeof(BDRVRawState
),
1194 .bdrv_probe_device
= cdrom_probe_device
,
1195 .bdrv_open
= cdrom_open
,
1196 .bdrv_close
= raw_close
,
1197 .bdrv_create
= hdev_create
,
1198 .create_options
= raw_create_options
,
1199 .bdrv_flush
= raw_flush
,
1201 .bdrv_aio_readv
= raw_aio_readv
,
1202 .bdrv_aio_writev
= raw_aio_writev
,
1203 .bdrv_aio_flush
= raw_aio_flush
,
1205 .bdrv_read
= raw_read
,
1206 .bdrv_write
= raw_write
,
1207 .bdrv_getlength
= raw_getlength
,
1209 /* removable device support */
1210 .bdrv_is_inserted
= cdrom_is_inserted
,
1211 .bdrv_eject
= cdrom_eject
,
1212 .bdrv_set_locked
= cdrom_set_locked
,
1214 /* generic scsi device */
1215 .bdrv_ioctl
= hdev_ioctl
,
1216 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1218 #endif /* __linux__ */
1221 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1223 BDRVRawState
*s
= bs
->opaque
;
1228 ret
= raw_open_common(bs
, filename
, flags
, 0);
1232 /* make sure the door isnt locked at this time */
1233 ioctl(s
->fd
, CDIOCALLOW
);
1237 static int cdrom_probe_device(const char *filename
)
1239 if (strstart(filename
, "/dev/cd", NULL
) ||
1240 strstart(filename
, "/dev/acd", NULL
))
1245 static int cdrom_reopen(BlockDriverState
*bs
)
1247 BDRVRawState
*s
= bs
->opaque
;
1251 * Force reread of possibly changed/newly loaded disc,
1252 * FreeBSD seems to not notice sometimes...
1256 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1263 /* make sure the door isnt locked at this time */
1264 ioctl(s
->fd
, CDIOCALLOW
);
1268 static int cdrom_is_inserted(BlockDriverState
*bs
)
1270 return raw_getlength(bs
) > 0;
1273 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1275 BDRVRawState
*s
= bs
->opaque
;
1280 (void) ioctl(s
->fd
, CDIOCALLOW
);
1283 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1284 perror("CDIOCEJECT");
1286 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1287 perror("CDIOCCLOSE");
1290 if (cdrom_reopen(bs
) < 0)
1295 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1297 BDRVRawState
*s
= bs
->opaque
;
1301 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1303 * Note: an error can happen if the distribution automatically
1306 /* perror("CDROM_LOCKDOOR"); */
1312 static BlockDriver bdrv_host_cdrom
= {
1313 .format_name
= "host_cdrom",
1314 .instance_size
= sizeof(BDRVRawState
),
1315 .bdrv_probe_device
= cdrom_probe_device
,
1316 .bdrv_open
= cdrom_open
,
1317 .bdrv_close
= raw_close
,
1318 .bdrv_create
= hdev_create
,
1319 .create_options
= raw_create_options
,
1320 .bdrv_flush
= raw_flush
,
1322 .bdrv_aio_readv
= raw_aio_readv
,
1323 .bdrv_aio_writev
= raw_aio_writev
,
1324 .bdrv_aio_flush
= raw_aio_flush
,
1326 .bdrv_read
= raw_read
,
1327 .bdrv_write
= raw_write
,
1328 .bdrv_getlength
= raw_getlength
,
1330 /* removable device support */
1331 .bdrv_is_inserted
= cdrom_is_inserted
,
1332 .bdrv_eject
= cdrom_eject
,
1333 .bdrv_set_locked
= cdrom_set_locked
,
1335 #endif /* __FreeBSD__ */
1337 static void bdrv_raw_init(void)
1340 * Register all the drivers. Note that order is important, the driver
1341 * registered last will get probed first.
1343 bdrv_register(&bdrv_raw
);
1344 bdrv_register(&bdrv_host_device
);
1346 bdrv_register(&bdrv_host_floppy
);
1347 bdrv_register(&bdrv_host_cdrom
);
1350 bdrv_register(&bdrv_host_cdrom
);
1354 block_init(bdrv_raw_init
);