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
{
108 unsigned int lseek_err_cnt
;
110 #if defined(__linux__)
111 /* linux floppy specific */
112 int64_t fd_open_time
;
113 int64_t fd_error_time
;
115 int fd_media_changed
;
117 #ifdef CONFIG_LINUX_AIO
121 uint8_t* aligned_buf
;
124 static int fd_open(BlockDriverState
*bs
);
125 static int64_t raw_getlength(BlockDriverState
*bs
);
127 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
128 static int cdrom_reopen(BlockDriverState
*bs
);
131 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
132 int bdrv_flags
, int open_flags
)
134 BDRVRawState
*s
= bs
->opaque
;
137 s
->lseek_err_cnt
= 0;
139 s
->open_flags
= open_flags
| O_BINARY
;
140 s
->open_flags
&= ~O_ACCMODE
;
141 if (bdrv_flags
& BDRV_O_RDWR
) {
142 s
->open_flags
|= O_RDWR
;
144 s
->open_flags
|= O_RDONLY
;
147 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
148 * and O_DIRECT for no caching. */
149 if ((bdrv_flags
& BDRV_O_NOCACHE
))
150 s
->open_flags
|= O_DIRECT
;
151 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
152 s
->open_flags
|= O_DSYNC
;
155 fd
= qemu_open(filename
, s
->open_flags
, 0644);
163 s
->aligned_buf
= NULL
;
165 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
166 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
167 if (s
->aligned_buf
== NULL
) {
172 #ifdef CONFIG_LINUX_AIO
173 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
174 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
176 /* We're falling back to POSIX AIO in some cases */
179 s
->aio_ctx
= laio_init();
187 if (paio_init() < 0) {
190 #ifdef CONFIG_LINUX_AIO
198 qemu_vfree(s
->aligned_buf
);
204 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
206 BDRVRawState
*s
= bs
->opaque
;
208 s
->type
= FTYPE_FILE
;
209 return raw_open_common(bs
, filename
, flags
, 0);
212 /* XXX: use host sector size if necessary with:
213 #ifdef DIOCGSECTORSIZE
215 unsigned int sectorsize = 512;
216 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
217 sectorsize > bufsize)
218 bufsize = sectorsize;
222 u_int32_t blockSize = 512;
223 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
230 * offset and count are in bytes, but must be multiples of 512 for files
231 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
233 * This function may be called without alignment if the caller ensures
234 * that O_DIRECT is not in effect.
236 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
237 uint8_t *buf
, int count
)
239 BDRVRawState
*s
= bs
->opaque
;
246 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
247 ++(s
->lseek_err_cnt
);
248 if(s
->lseek_err_cnt
<= 10) {
249 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
250 "] lseek failed : %d = %s\n",
251 s
->fd
, bs
->filename
, offset
, buf
, count
,
252 bs
->total_sectors
, errno
, strerror(errno
));
258 ret
= read(s
->fd
, buf
, count
);
260 goto label__raw_read__success
;
262 /* Allow reads beyond the end (needed for pwrite) */
263 if ((ret
== 0) && bs
->growable
) {
264 int64_t size
= raw_getlength(bs
);
265 if (offset
>= size
) {
266 memset(buf
, 0, count
);
268 goto label__raw_read__success
;
272 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
273 "] read failed %d : %d = %s\n",
274 s
->fd
, bs
->filename
, offset
, buf
, count
,
275 bs
->total_sectors
, ret
, errno
, strerror(errno
));
277 /* Try harder for CDrom. */
278 if (bs
->type
== BDRV_TYPE_CDROM
) {
279 lseek(s
->fd
, offset
, SEEK_SET
);
280 ret
= read(s
->fd
, buf
, count
);
282 goto label__raw_read__success
;
283 lseek(s
->fd
, offset
, SEEK_SET
);
284 ret
= read(s
->fd
, buf
, count
);
286 goto label__raw_read__success
;
288 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
289 "] retry read failed %d : %d = %s\n",
290 s
->fd
, bs
->filename
, offset
, buf
, count
,
291 bs
->total_sectors
, ret
, errno
, strerror(errno
));
294 label__raw_read__success
:
296 return (ret
< 0) ? -errno
: ret
;
300 * offset and count are in bytes, but must be multiples of 512 for files
301 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
303 * This function may be called without alignment if the caller ensures
304 * that O_DIRECT is not in effect.
306 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
307 const uint8_t *buf
, int count
)
309 BDRVRawState
*s
= bs
->opaque
;
316 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
317 ++(s
->lseek_err_cnt
);
318 if(s
->lseek_err_cnt
) {
319 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
320 PRId64
"] lseek failed : %d = %s\n",
321 s
->fd
, bs
->filename
, offset
, buf
, count
,
322 bs
->total_sectors
, errno
, strerror(errno
));
326 s
->lseek_err_cnt
= 0;
328 ret
= write(s
->fd
, buf
, count
);
330 goto label__raw_write__success
;
332 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
333 "] write failed %d : %d = %s\n",
334 s
->fd
, bs
->filename
, offset
, buf
, count
,
335 bs
->total_sectors
, ret
, errno
, strerror(errno
));
337 label__raw_write__success
:
339 return (ret
< 0) ? -errno
: ret
;
344 * offset and count are in bytes and possibly not aligned. For files opened
345 * with O_DIRECT, necessary alignments are ensured before calling
346 * raw_pread_aligned to do the actual read.
348 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
349 uint8_t *buf
, int count
)
351 BDRVRawState
*s
= bs
->opaque
;
352 int size
, ret
, shift
, sum
;
356 if (s
->aligned_buf
!= NULL
) {
358 if (offset
& 0x1ff) {
359 /* align offset on a 512 bytes boundary */
361 shift
= offset
& 0x1ff;
362 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
363 if (size
> ALIGNED_BUFFER_SIZE
)
364 size
= ALIGNED_BUFFER_SIZE
;
365 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
372 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
382 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
384 /* read on aligned buffer */
388 size
= (count
+ 0x1ff) & ~0x1ff;
389 if (size
> ALIGNED_BUFFER_SIZE
)
390 size
= ALIGNED_BUFFER_SIZE
;
392 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
395 } else if (ret
== 0) {
396 fprintf(stderr
, "raw_pread: read beyond end of file\n");
404 memcpy(buf
, s
->aligned_buf
, size
);
416 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
419 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
420 uint8_t *buf
, int nb_sectors
)
424 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
425 if (ret
== (nb_sectors
* 512))
431 * offset and count are in bytes and possibly not aligned. For files opened
432 * with O_DIRECT, necessary alignments are ensured before calling
433 * raw_pwrite_aligned to do the actual write.
435 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
436 const uint8_t *buf
, int count
)
438 BDRVRawState
*s
= bs
->opaque
;
439 int size
, ret
, shift
, sum
;
443 if (s
->aligned_buf
!= NULL
) {
445 if (offset
& 0x1ff) {
446 /* align offset on a 512 bytes boundary */
447 shift
= offset
& 0x1ff;
448 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
455 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
457 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
469 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
471 while ((size
= (count
& ~0x1ff)) != 0) {
473 if (size
> ALIGNED_BUFFER_SIZE
)
474 size
= ALIGNED_BUFFER_SIZE
;
476 memcpy(s
->aligned_buf
, buf
, size
);
478 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
487 /* here, count < 512 because (count & ~0x1ff) == 0 */
489 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
492 memcpy(s
->aligned_buf
, buf
, count
);
494 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
505 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
508 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
509 const uint8_t *buf
, int nb_sectors
)
512 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
513 if (ret
== (nb_sectors
* 512))
519 * Check if all memory in this vector is sector aligned.
521 static int qiov_is_aligned(QEMUIOVector
*qiov
)
525 for (i
= 0; i
< qiov
->niov
; i
++) {
526 if ((uintptr_t) qiov
->iov
[i
].iov_base
% 512) {
534 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
535 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
536 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
538 BDRVRawState
*s
= bs
->opaque
;
544 * If O_DIRECT is used the buffer needs to be aligned on a sector
545 * boundary. Check if this is the case or telll the low-level
546 * driver that it needs to copy the buffer.
548 if (s
->aligned_buf
) {
549 if (!qiov_is_aligned(qiov
)) {
550 type
|= QEMU_AIO_MISALIGNED
;
551 #ifdef CONFIG_LINUX_AIO
552 } else if (s
->use_aio
) {
553 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
554 nb_sectors
, cb
, opaque
, type
);
559 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
563 static BlockDriverAIOCB
*raw_aio_readv(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_READ
);
571 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
572 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
573 BlockDriverCompletionFunc
*cb
, void *opaque
)
575 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
576 cb
, opaque
, QEMU_AIO_WRITE
);
579 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
580 BlockDriverCompletionFunc
*cb
, void *opaque
)
582 BDRVRawState
*s
= bs
->opaque
;
587 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
590 static void raw_close(BlockDriverState
*bs
)
592 BDRVRawState
*s
= bs
->opaque
;
596 if (s
->aligned_buf
!= NULL
)
597 qemu_vfree(s
->aligned_buf
);
601 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
603 BDRVRawState
*s
= bs
->opaque
;
604 if (s
->type
!= FTYPE_FILE
)
606 if (ftruncate(s
->fd
, offset
) < 0)
612 static int64_t raw_getlength(BlockDriverState
*bs
)
614 BDRVRawState
*s
= bs
->opaque
;
620 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
623 if (ioctl(fd
, DIOCGDINFO
, &dl
))
625 return (uint64_t)dl
.d_secsize
*
626 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
630 #elif defined(__sun__)
631 static int64_t raw_getlength(BlockDriverState
*bs
)
633 BDRVRawState
*s
= bs
->opaque
;
634 struct dk_minfo minfo
;
643 * Use the DKIOCGMEDIAINFO ioctl to read the size.
645 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
647 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
651 * There are reports that lseek on some devices fails, but
652 * irc discussion said that contingency on contingency was overkill.
654 return lseek(s
->fd
, 0, SEEK_END
);
656 #elif defined(CONFIG_BSD)
657 static int64_t raw_getlength(BlockDriverState
*bs
)
659 BDRVRawState
*s
= bs
->opaque
;
663 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
672 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
675 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
676 #ifdef DIOCGMEDIASIZE
677 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
678 #elif defined(DIOCGPART)
681 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
682 size
= pi
.media_size
;
689 size
= LONG_LONG_MAX
;
691 size
= lseek(fd
, 0LL, SEEK_END
);
693 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
696 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
697 if (size
== 2048LL * (unsigned)-1)
699 /* XXX no disc? maybe we need to reopen... */
700 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
707 size
= lseek(fd
, 0, SEEK_END
);
712 static int64_t raw_getlength(BlockDriverState
*bs
)
714 BDRVRawState
*s
= bs
->opaque
;
722 return lseek(s
->fd
, 0, SEEK_END
);
726 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
730 int64_t total_size
= 0;
732 /* Read out options */
733 while (options
&& options
->name
) {
734 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
735 total_size
= options
->value
.n
/ 512;
740 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
745 if (ftruncate(fd
, total_size
* 512) != 0) {
748 if (close(fd
) != 0) {
755 static void raw_flush(BlockDriverState
*bs
)
757 BDRVRawState
*s
= bs
->opaque
;
758 qemu_fdatasync(s
->fd
);
762 static QEMUOptionParameter raw_create_options
[] = {
764 .name
= BLOCK_OPT_SIZE
,
766 .help
= "Virtual disk size"
771 static BlockDriver bdrv_raw
= {
772 .format_name
= "raw",
773 .instance_size
= sizeof(BDRVRawState
),
774 .bdrv_probe
= NULL
, /* no probe for protocols */
775 .bdrv_open
= raw_open
,
776 .bdrv_read
= raw_read
,
777 .bdrv_write
= raw_write
,
778 .bdrv_close
= raw_close
,
779 .bdrv_create
= raw_create
,
780 .bdrv_flush
= raw_flush
,
782 .bdrv_aio_readv
= raw_aio_readv
,
783 .bdrv_aio_writev
= raw_aio_writev
,
784 .bdrv_aio_flush
= raw_aio_flush
,
786 .bdrv_truncate
= raw_truncate
,
787 .bdrv_getlength
= raw_getlength
,
789 .create_options
= raw_create_options
,
792 /***********************************************/
796 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
797 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
799 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
801 kern_return_t kernResult
;
802 mach_port_t masterPort
;
803 CFMutableDictionaryRef classesToMatch
;
805 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
806 if ( KERN_SUCCESS
!= kernResult
) {
807 printf( "IOMasterPort returned %d\n", kernResult
);
810 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
811 if ( classesToMatch
== NULL
) {
812 printf( "IOServiceMatching returned a NULL dictionary.\n" );
814 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
816 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
817 if ( KERN_SUCCESS
!= kernResult
)
819 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
825 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
827 io_object_t nextMedia
;
828 kern_return_t kernResult
= KERN_FAILURE
;
830 nextMedia
= IOIteratorNext( mediaIterator
);
833 CFTypeRef bsdPathAsCFString
;
834 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
835 if ( bsdPathAsCFString
) {
836 size_t devPathLength
;
837 strcpy( bsdPath
, _PATH_DEV
);
838 strcat( bsdPath
, "r" );
839 devPathLength
= strlen( bsdPath
);
840 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
841 kernResult
= KERN_SUCCESS
;
843 CFRelease( bsdPathAsCFString
);
845 IOObjectRelease( nextMedia
);
853 static int hdev_probe_device(const char *filename
)
857 /* allow a dedicated CD-ROM driver to match with a higher priority */
858 if (strstart(filename
, "/dev/cdrom", NULL
))
861 if (stat(filename
, &st
) >= 0 &&
862 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
869 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
871 BDRVRawState
*s
= bs
->opaque
;
874 if (strstart(filename
, "/dev/cdrom", NULL
)) {
875 kern_return_t kernResult
;
876 io_iterator_t mediaIterator
;
877 char bsdPath
[ MAXPATHLEN
];
880 kernResult
= FindEjectableCDMedia( &mediaIterator
);
881 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
883 if ( bsdPath
[ 0 ] != '\0' ) {
884 strcat(bsdPath
,"s0");
885 /* some CDs don't have a partition 0 */
886 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
888 bsdPath
[strlen(bsdPath
)-1] = '1';
896 IOObjectRelease( mediaIterator
);
900 s
->type
= FTYPE_FILE
;
901 #if defined(__linux__)
902 if (strstart(filename
, "/dev/sg", NULL
)) {
907 return raw_open_common(bs
, filename
, flags
, 0);
910 #if defined(__linux__)
911 /* Note: we do not have a reliable method to detect if the floppy is
912 present. The current method is to try to open the floppy at every
913 I/O and to keep it opened during a few hundreds of ms. */
914 static int fd_open(BlockDriverState
*bs
)
916 BDRVRawState
*s
= bs
->opaque
;
917 int last_media_present
;
919 if (s
->type
!= FTYPE_FD
)
921 last_media_present
= (s
->fd
>= 0);
923 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
927 printf("Floppy closed\n");
931 if (s
->fd_got_error
&&
932 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
934 printf("No floppy (open delayed)\n");
938 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
940 s
->fd_error_time
= qemu_get_clock(rt_clock
);
942 if (last_media_present
)
943 s
->fd_media_changed
= 1;
945 printf("No floppy\n");
950 printf("Floppy opened\n");
953 if (!last_media_present
)
954 s
->fd_media_changed
= 1;
955 s
->fd_open_time
= qemu_get_clock(rt_clock
);
960 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
962 BDRVRawState
*s
= bs
->opaque
;
964 return ioctl(s
->fd
, req
, buf
);
967 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
968 unsigned long int req
, void *buf
,
969 BlockDriverCompletionFunc
*cb
, void *opaque
)
971 BDRVRawState
*s
= bs
->opaque
;
975 return paio_ioctl(bs
, s
->fd
, req
, buf
, cb
, opaque
);
978 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
979 static int fd_open(BlockDriverState
*bs
)
981 BDRVRawState
*s
= bs
->opaque
;
983 /* this is just to ensure s->fd is sane (its called by io ops) */
988 #else /* !linux && !FreeBSD */
990 static int fd_open(BlockDriverState
*bs
)
995 #endif /* !linux && !FreeBSD */
997 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1001 struct stat stat_buf
;
1002 int64_t total_size
= 0;
1004 /* Read out options */
1005 while (options
&& options
->name
) {
1006 if (!strcmp(options
->name
, "size")) {
1007 total_size
= options
->value
.n
/ 512;
1012 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1016 if (fstat(fd
, &stat_buf
) < 0)
1018 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1020 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1027 static BlockDriver bdrv_host_device
= {
1028 .format_name
= "host_device",
1029 .instance_size
= sizeof(BDRVRawState
),
1030 .bdrv_probe_device
= hdev_probe_device
,
1031 .bdrv_open
= hdev_open
,
1032 .bdrv_close
= raw_close
,
1033 .bdrv_create
= hdev_create
,
1034 .create_options
= raw_create_options
,
1036 .bdrv_flush
= raw_flush
,
1038 .bdrv_aio_readv
= raw_aio_readv
,
1039 .bdrv_aio_writev
= raw_aio_writev
,
1040 .bdrv_aio_flush
= raw_aio_flush
,
1042 .bdrv_read
= raw_read
,
1043 .bdrv_write
= raw_write
,
1044 .bdrv_getlength
= raw_getlength
,
1046 /* generic scsi device */
1048 .bdrv_ioctl
= hdev_ioctl
,
1049 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1054 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1056 BDRVRawState
*s
= bs
->opaque
;
1061 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1062 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1066 /* close fd so that we can reopen it as needed */
1069 s
->fd_media_changed
= 1;
1074 static int floppy_probe_device(const char *filename
)
1078 struct floppy_struct fdparam
;
1080 if (strstart(filename
, "/dev/fd", NULL
))
1083 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1088 /* Attempt to detect via a floppy specific ioctl */
1089 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1099 static int floppy_is_inserted(BlockDriverState
*bs
)
1101 return fd_open(bs
) >= 0;
1104 static int floppy_media_changed(BlockDriverState
*bs
)
1106 BDRVRawState
*s
= bs
->opaque
;
1110 * XXX: we do not have a true media changed indication.
1111 * It does not work if the floppy is changed without trying to read it.
1114 ret
= s
->fd_media_changed
;
1115 s
->fd_media_changed
= 0;
1117 printf("Floppy changed=%d\n", ret
);
1122 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1124 BDRVRawState
*s
= bs
->opaque
;
1131 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1133 if (ioctl(fd
, FDEJECT
, 0) < 0)
1141 static BlockDriver bdrv_host_floppy
= {
1142 .format_name
= "host_floppy",
1143 .instance_size
= sizeof(BDRVRawState
),
1144 .bdrv_probe_device
= floppy_probe_device
,
1145 .bdrv_open
= floppy_open
,
1146 .bdrv_close
= raw_close
,
1147 .bdrv_create
= hdev_create
,
1148 .create_options
= raw_create_options
,
1150 .bdrv_flush
= raw_flush
,
1152 .bdrv_aio_readv
= raw_aio_readv
,
1153 .bdrv_aio_writev
= raw_aio_writev
,
1154 .bdrv_aio_flush
= raw_aio_flush
,
1156 .bdrv_read
= raw_read
,
1157 .bdrv_write
= raw_write
,
1158 .bdrv_getlength
= raw_getlength
,
1160 /* removable device support */
1161 .bdrv_is_inserted
= floppy_is_inserted
,
1162 .bdrv_media_changed
= floppy_media_changed
,
1163 .bdrv_eject
= floppy_eject
,
1166 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1168 BDRVRawState
*s
= bs
->opaque
;
1172 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1173 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1176 static int cdrom_probe_device(const char *filename
)
1181 if (strstart(filename
, "/dev/cd", NULL
))
1184 fd
= open(filename
, O_RDONLY
| O_NONBLOCK
);
1189 /* Attempt to detect via a CDROM specific ioctl */
1190 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1199 static int cdrom_is_inserted(BlockDriverState
*bs
)
1201 BDRVRawState
*s
= bs
->opaque
;
1204 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1205 if (ret
== CDS_DISC_OK
)
1210 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1212 BDRVRawState
*s
= bs
->opaque
;
1215 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1216 perror("CDROMEJECT");
1218 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1219 perror("CDROMEJECT");
1225 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1227 BDRVRawState
*s
= bs
->opaque
;
1229 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1231 * Note: an error can happen if the distribution automatically
1234 /* perror("CDROM_LOCKDOOR"); */
1240 static BlockDriver bdrv_host_cdrom
= {
1241 .format_name
= "host_cdrom",
1242 .instance_size
= sizeof(BDRVRawState
),
1243 .bdrv_probe_device
= cdrom_probe_device
,
1244 .bdrv_open
= cdrom_open
,
1245 .bdrv_close
= raw_close
,
1246 .bdrv_create
= hdev_create
,
1247 .create_options
= raw_create_options
,
1249 .bdrv_flush
= raw_flush
,
1251 .bdrv_aio_readv
= raw_aio_readv
,
1252 .bdrv_aio_writev
= raw_aio_writev
,
1253 .bdrv_aio_flush
= raw_aio_flush
,
1255 .bdrv_read
= raw_read
,
1256 .bdrv_write
= raw_write
,
1257 .bdrv_getlength
= raw_getlength
,
1259 /* removable device support */
1260 .bdrv_is_inserted
= cdrom_is_inserted
,
1261 .bdrv_eject
= cdrom_eject
,
1262 .bdrv_set_locked
= cdrom_set_locked
,
1264 /* generic scsi device */
1265 .bdrv_ioctl
= hdev_ioctl
,
1266 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1268 #endif /* __linux__ */
1270 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1271 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1273 BDRVRawState
*s
= bs
->opaque
;
1278 ret
= raw_open_common(bs
, filename
, flags
, 0);
1282 /* make sure the door isnt locked at this time */
1283 ioctl(s
->fd
, CDIOCALLOW
);
1287 static int cdrom_probe_device(const char *filename
)
1289 if (strstart(filename
, "/dev/cd", NULL
) ||
1290 strstart(filename
, "/dev/acd", NULL
))
1295 static int cdrom_reopen(BlockDriverState
*bs
)
1297 BDRVRawState
*s
= bs
->opaque
;
1301 * Force reread of possibly changed/newly loaded disc,
1302 * FreeBSD seems to not notice sometimes...
1306 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1313 /* make sure the door isnt locked at this time */
1314 ioctl(s
->fd
, CDIOCALLOW
);
1318 static int cdrom_is_inserted(BlockDriverState
*bs
)
1320 return raw_getlength(bs
) > 0;
1323 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1325 BDRVRawState
*s
= bs
->opaque
;
1330 (void) ioctl(s
->fd
, CDIOCALLOW
);
1333 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1334 perror("CDIOCEJECT");
1336 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1337 perror("CDIOCCLOSE");
1340 if (cdrom_reopen(bs
) < 0)
1345 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1347 BDRVRawState
*s
= bs
->opaque
;
1351 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1353 * Note: an error can happen if the distribution automatically
1356 /* perror("CDROM_LOCKDOOR"); */
1362 static BlockDriver bdrv_host_cdrom
= {
1363 .format_name
= "host_cdrom",
1364 .instance_size
= sizeof(BDRVRawState
),
1365 .bdrv_probe_device
= cdrom_probe_device
,
1366 .bdrv_open
= cdrom_open
,
1367 .bdrv_close
= raw_close
,
1368 .bdrv_create
= hdev_create
,
1369 .create_options
= raw_create_options
,
1371 .bdrv_flush
= raw_flush
,
1373 .bdrv_aio_readv
= raw_aio_readv
,
1374 .bdrv_aio_writev
= raw_aio_writev
,
1375 .bdrv_aio_flush
= raw_aio_flush
,
1377 .bdrv_read
= raw_read
,
1378 .bdrv_write
= raw_write
,
1379 .bdrv_getlength
= raw_getlength
,
1381 /* removable device support */
1382 .bdrv_is_inserted
= cdrom_is_inserted
,
1383 .bdrv_eject
= cdrom_eject
,
1384 .bdrv_set_locked
= cdrom_set_locked
,
1386 #endif /* __FreeBSD__ */
1388 static void bdrv_raw_init(void)
1391 * Register all the drivers. Note that order is important, the driver
1392 * registered last will get probed first.
1394 bdrv_register(&bdrv_raw
);
1395 bdrv_register(&bdrv_host_device
);
1397 bdrv_register(&bdrv_host_floppy
);
1398 bdrv_register(&bdrv_host_cdrom
);
1400 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1401 bdrv_register(&bdrv_host_cdrom
);
1405 block_init(bdrv_raw_init
);