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"
27 #include "block_int.h"
30 #include "posix-aio-compat.h"
35 #include <sys/param.h>
36 #include <IOKit/IOKitLib.h>
37 #include <IOKit/IOBSD.h>
38 #include <IOKit/storage/IOMediaBSDClient.h>
39 #include <IOKit/storage/IOMedia.h>
40 #include <IOKit/storage/IOCDMedia.h>
41 //#include <IOKit/storage/IOCDTypes.h>
42 #include <CoreFoundation/CoreFoundation.h>
46 #define _POSIX_PTHREAD_SEMANTICS 1
51 #include <sys/ioctl.h>
52 #include <linux/cdrom.h>
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
68 #include <sys/ioctl.h>
69 #include <sys/diskslice.h>
72 //#define DEBUG_FLOPPY
75 #if defined(DEBUG_BLOCK)
76 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
77 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
79 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
82 /* OS X does not have O_DSYNC */
84 #define O_DSYNC O_SYNC
87 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
89 #define O_DIRECT O_DSYNC
96 #define ALIGNED_BUFFER_SIZE (32 * 512)
98 /* if the FD is not accessed during that time (in ms), we try to
99 reopen it to see if the disk has been changed */
100 #define FD_OPEN_TIMEOUT 1000
102 typedef struct BDRVRawState
{
105 unsigned int lseek_err_cnt
;
107 #if defined(__linux__)
108 /* linux floppy specific */
109 int64_t fd_open_time
;
110 int64_t fd_error_time
;
112 int fd_media_changed
;
114 uint8_t* aligned_buf
;
117 static int posix_aio_init(void);
119 static int fd_open(BlockDriverState
*bs
);
121 #if defined(__FreeBSD__)
122 static int cdrom_reopen(BlockDriverState
*bs
);
125 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
128 BDRVRawState
*s
= bs
->opaque
;
133 s
->lseek_err_cnt
= 0;
135 s
->open_flags
|= O_BINARY
;
136 s
->open_flags
&= ~O_ACCMODE
;
137 if ((flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
138 s
->open_flags
|= O_RDWR
;
140 s
->open_flags
|= O_RDONLY
;
144 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
145 * and O_DIRECT for no caching. */
146 if ((flags
& BDRV_O_NOCACHE
))
147 s
->open_flags
|= O_DIRECT
;
148 else if (!(flags
& BDRV_O_CACHE_WB
))
149 s
->open_flags
|= O_DSYNC
;
152 fd
= open(filename
, s
->open_flags
, 0644);
160 s
->aligned_buf
= NULL
;
161 if ((flags
& BDRV_O_NOCACHE
)) {
162 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
163 if (s
->aligned_buf
== NULL
) {
172 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
174 BDRVRawState
*s
= bs
->opaque
;
176 s
->type
= FTYPE_FILE
;
177 if (flags
& BDRV_O_CREAT
)
178 s
->open_flags
|= O_CREAT
| O_TRUNC
;
180 return raw_open_common(bs
, filename
, flags
);
183 /* XXX: use host sector size if necessary with:
184 #ifdef DIOCGSECTORSIZE
186 unsigned int sectorsize = 512;
187 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
188 sectorsize > bufsize)
189 bufsize = sectorsize;
193 u_int32_t blockSize = 512;
194 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
201 * offset and count are in bytes, but must be multiples of 512 for files
202 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
204 * This function may be called without alignment if the caller ensures
205 * that O_DIRECT is not in effect.
207 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
208 uint8_t *buf
, int count
)
210 BDRVRawState
*s
= bs
->opaque
;
217 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
218 ++(s
->lseek_err_cnt
);
219 if(s
->lseek_err_cnt
<= 10) {
220 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
221 "] lseek failed : %d = %s\n",
222 s
->fd
, bs
->filename
, offset
, buf
, count
,
223 bs
->total_sectors
, errno
, strerror(errno
));
229 ret
= read(s
->fd
, buf
, count
);
231 goto label__raw_read__success
;
233 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
234 "] read failed %d : %d = %s\n",
235 s
->fd
, bs
->filename
, offset
, buf
, count
,
236 bs
->total_sectors
, ret
, errno
, strerror(errno
));
238 /* Try harder for CDrom. */
239 if (bs
->type
== BDRV_TYPE_CDROM
) {
240 lseek(s
->fd
, offset
, SEEK_SET
);
241 ret
= read(s
->fd
, buf
, count
);
243 goto label__raw_read__success
;
244 lseek(s
->fd
, offset
, SEEK_SET
);
245 ret
= read(s
->fd
, buf
, count
);
247 goto label__raw_read__success
;
249 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
250 "] retry read failed %d : %d = %s\n",
251 s
->fd
, bs
->filename
, offset
, buf
, count
,
252 bs
->total_sectors
, ret
, errno
, strerror(errno
));
255 label__raw_read__success
:
257 return (ret
< 0) ? -errno
: ret
;
261 * offset and count are in bytes, but must be multiples of 512 for files
262 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
264 * This function may be called without alignment if the caller ensures
265 * that O_DIRECT is not in effect.
267 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
268 const uint8_t *buf
, int count
)
270 BDRVRawState
*s
= bs
->opaque
;
277 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
278 ++(s
->lseek_err_cnt
);
279 if(s
->lseek_err_cnt
) {
280 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
281 PRId64
"] lseek failed : %d = %s\n",
282 s
->fd
, bs
->filename
, offset
, buf
, count
,
283 bs
->total_sectors
, errno
, strerror(errno
));
287 s
->lseek_err_cnt
= 0;
289 ret
= write(s
->fd
, buf
, count
);
291 goto label__raw_write__success
;
293 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
294 "] write failed %d : %d = %s\n",
295 s
->fd
, bs
->filename
, offset
, buf
, count
,
296 bs
->total_sectors
, ret
, errno
, strerror(errno
));
298 label__raw_write__success
:
300 return (ret
< 0) ? -errno
: ret
;
305 * offset and count are in bytes and possibly not aligned. For files opened
306 * with O_DIRECT, necessary alignments are ensured before calling
307 * raw_pread_aligned to do the actual read.
309 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
310 uint8_t *buf
, int count
)
312 BDRVRawState
*s
= bs
->opaque
;
313 int size
, ret
, shift
, sum
;
317 if (s
->aligned_buf
!= NULL
) {
319 if (offset
& 0x1ff) {
320 /* align offset on a 512 bytes boundary */
322 shift
= offset
& 0x1ff;
323 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
324 if (size
> ALIGNED_BUFFER_SIZE
)
325 size
= ALIGNED_BUFFER_SIZE
;
326 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
333 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
343 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
345 /* read on aligned buffer */
349 size
= (count
+ 0x1ff) & ~0x1ff;
350 if (size
> ALIGNED_BUFFER_SIZE
)
351 size
= ALIGNED_BUFFER_SIZE
;
353 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
361 memcpy(buf
, s
->aligned_buf
, size
);
373 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
376 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
377 uint8_t *buf
, int nb_sectors
)
381 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
382 if (ret
== (nb_sectors
* 512))
388 * offset and count are in bytes and possibly not aligned. For files opened
389 * with O_DIRECT, necessary alignments are ensured before calling
390 * raw_pwrite_aligned to do the actual write.
392 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
393 const uint8_t *buf
, int count
)
395 BDRVRawState
*s
= bs
->opaque
;
396 int size
, ret
, shift
, sum
;
400 if (s
->aligned_buf
!= NULL
) {
402 if (offset
& 0x1ff) {
403 /* align offset on a 512 bytes boundary */
404 shift
= offset
& 0x1ff;
405 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
412 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
414 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
426 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
428 while ((size
= (count
& ~0x1ff)) != 0) {
430 if (size
> ALIGNED_BUFFER_SIZE
)
431 size
= ALIGNED_BUFFER_SIZE
;
433 memcpy(s
->aligned_buf
, buf
, size
);
435 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
444 /* here, count < 512 because (count & ~0x1ff) == 0 */
446 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
449 memcpy(s
->aligned_buf
, buf
, count
);
451 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
462 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
465 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
466 const uint8_t *buf
, int nb_sectors
)
469 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
470 if (ret
== (nb_sectors
* 512))
476 /***********************************************************/
477 /* Unix AIO using POSIX AIO */
479 typedef struct RawAIOCB
{
480 BlockDriverAIOCB common
;
481 struct qemu_paiocb aiocb
;
482 struct RawAIOCB
*next
;
486 typedef struct PosixAioState
492 static void posix_aio_read(void *opaque
)
494 PosixAioState
*s
= opaque
;
495 RawAIOCB
*acb
, **pacb
;
499 /* read all bytes from signal pipe */
503 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
504 if (len
== -1 && errno
== EINTR
)
505 continue; /* try again */
506 if (len
== sizeof(bytes
))
507 continue; /* more to read */
512 pacb
= &s
->first_aio
;
517 ret
= qemu_paio_error(&acb
->aiocb
);
518 if (ret
== ECANCELED
) {
519 /* remove the request */
521 qemu_aio_release(acb
);
522 } else if (ret
!= EINPROGRESS
) {
525 ret
= qemu_paio_return(&acb
->aiocb
);
526 if (ret
== acb
->aiocb
.aio_nbytes
)
533 /* remove the request */
535 /* call the callback */
536 acb
->common
.cb(acb
->common
.opaque
, ret
);
537 qemu_aio_release(acb
);
547 static int posix_aio_flush(void *opaque
)
549 PosixAioState
*s
= opaque
;
550 return !!s
->first_aio
;
553 static PosixAioState
*posix_aio_state
;
555 static void aio_signal_handler(int signum
)
557 if (posix_aio_state
) {
560 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
566 static int posix_aio_init(void)
568 struct sigaction act
;
571 struct qemu_paioinit ai
;
576 s
= qemu_malloc(sizeof(PosixAioState
));
578 sigfillset(&act
.sa_mask
);
579 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
580 act
.sa_handler
= aio_signal_handler
;
581 sigaction(SIGUSR2
, &act
, NULL
);
584 if (pipe(fds
) == -1) {
585 fprintf(stderr
, "failed to create pipe\n");
592 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
593 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
595 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
597 memset(&ai
, 0, sizeof(ai
));
607 static void raw_aio_remove(RawAIOCB
*acb
)
611 /* remove the callback from the queue */
612 pacb
= &posix_aio_state
->first_aio
;
615 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
617 } else if (*pacb
== acb
) {
619 qemu_aio_release(acb
);
622 pacb
= &(*pacb
)->next
;
626 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
629 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
631 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
632 if (ret
== QEMU_PAIO_NOTCANCELED
) {
633 /* fail safe: if the aio could not be canceled, we wait for
635 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
641 static AIOPool raw_aio_pool
= {
642 .aiocb_size
= sizeof(RawAIOCB
),
643 .cancel
= raw_aio_cancel
,
646 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
647 QEMUIOVector
*qiov
, int nb_sectors
,
648 BlockDriverCompletionFunc
*cb
, void *opaque
)
650 BDRVRawState
*s
= bs
->opaque
;
656 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
659 acb
->aiocb
.aio_fildes
= s
->fd
;
660 acb
->aiocb
.ev_signo
= SIGUSR2
;
661 acb
->aiocb
.aio_iov
= qiov
->iov
;
662 acb
->aiocb
.aio_niov
= qiov
->niov
;
663 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
664 acb
->aiocb
.aio_offset
= sector_num
* 512;
665 acb
->aiocb
.aio_flags
= 0;
668 * If O_DIRECT is used the buffer needs to be aligned on a sector
669 * boundary. Tell the low level code to ensure that in case it's
673 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
675 acb
->next
= posix_aio_state
->first_aio
;
676 posix_aio_state
->first_aio
= acb
;
680 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
681 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
682 BlockDriverCompletionFunc
*cb
, void *opaque
)
686 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
689 if (qemu_paio_read(&acb
->aiocb
) < 0) {
696 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
697 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
698 BlockDriverCompletionFunc
*cb
, void *opaque
)
702 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
705 if (qemu_paio_write(&acb
->aiocb
) < 0) {
711 #else /* CONFIG_AIO */
712 static int posix_aio_init(void)
716 #endif /* CONFIG_AIO */
719 static void raw_close(BlockDriverState
*bs
)
721 BDRVRawState
*s
= bs
->opaque
;
725 if (s
->aligned_buf
!= NULL
)
726 qemu_free(s
->aligned_buf
);
730 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
732 BDRVRawState
*s
= bs
->opaque
;
733 if (s
->type
!= FTYPE_FILE
)
735 if (ftruncate(s
->fd
, offset
) < 0)
741 static int64_t raw_getlength(BlockDriverState
*bs
)
743 BDRVRawState
*s
= bs
->opaque
;
749 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
752 if (ioctl(fd
, DIOCGDINFO
, &dl
))
754 return (uint64_t)dl
.d_secsize
*
755 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
759 #else /* !__OpenBSD__ */
760 static int64_t raw_getlength(BlockDriverState
*bs
)
762 BDRVRawState
*s
= bs
->opaque
;
772 struct dk_minfo minfo
;
785 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
786 #ifdef DIOCGMEDIASIZE
787 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
788 #elif defined(DIOCGPART)
791 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
792 size
= pi
.media_size
;
799 size
= LONG_LONG_MAX
;
801 size
= lseek(fd
, 0LL, SEEK_END
);
806 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
807 if (size
== 2048LL * (unsigned)-1)
809 /* XXX no disc? maybe we need to reopen... */
810 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
820 * use the DKIOCGMEDIAINFO ioctl to read the size.
822 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
824 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
825 } else /* there are reports that lseek on some devices
826 fails, but irc discussion said that contingency
827 on contingency was overkill */
830 size
= lseek(fd
, 0, SEEK_END
);
836 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
839 int64_t total_size
= 0;
841 /* Read out options */
842 while (options
&& options
->name
) {
843 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
844 total_size
= options
->value
.n
/ 512;
849 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
853 ftruncate(fd
, total_size
* 512);
858 static void raw_flush(BlockDriverState
*bs
)
860 BDRVRawState
*s
= bs
->opaque
;
865 static QEMUOptionParameter raw_create_options
[] = {
867 .name
= BLOCK_OPT_SIZE
,
869 .help
= "Virtual disk size"
874 static BlockDriver bdrv_raw
= {
875 .format_name
= "raw",
876 .instance_size
= sizeof(BDRVRawState
),
877 .bdrv_probe
= NULL
, /* no probe for protocols */
878 .bdrv_open
= raw_open
,
879 .bdrv_read
= raw_read
,
880 .bdrv_write
= raw_write
,
881 .bdrv_close
= raw_close
,
882 .bdrv_create
= raw_create
,
883 .bdrv_flush
= raw_flush
,
886 .bdrv_aio_readv
= raw_aio_readv
,
887 .bdrv_aio_writev
= raw_aio_writev
,
890 .bdrv_truncate
= raw_truncate
,
891 .bdrv_getlength
= raw_getlength
,
893 .create_options
= raw_create_options
,
896 /***********************************************/
900 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
901 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
903 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
905 kern_return_t kernResult
;
906 mach_port_t masterPort
;
907 CFMutableDictionaryRef classesToMatch
;
909 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
910 if ( KERN_SUCCESS
!= kernResult
) {
911 printf( "IOMasterPort returned %d\n", kernResult
);
914 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
915 if ( classesToMatch
== NULL
) {
916 printf( "IOServiceMatching returned a NULL dictionary.\n" );
918 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
920 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
921 if ( KERN_SUCCESS
!= kernResult
)
923 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
929 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
931 io_object_t nextMedia
;
932 kern_return_t kernResult
= KERN_FAILURE
;
934 nextMedia
= IOIteratorNext( mediaIterator
);
937 CFTypeRef bsdPathAsCFString
;
938 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
939 if ( bsdPathAsCFString
) {
940 size_t devPathLength
;
941 strcpy( bsdPath
, _PATH_DEV
);
942 strcat( bsdPath
, "r" );
943 devPathLength
= strlen( bsdPath
);
944 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
945 kernResult
= KERN_SUCCESS
;
947 CFRelease( bsdPathAsCFString
);
949 IOObjectRelease( nextMedia
);
957 static int hdev_probe_device(const char *filename
)
961 /* allow a dedicated CD-ROM driver to match with a higher priority */
962 if (strstart(filename
, "/dev/cdrom", NULL
))
965 if (stat(filename
, &st
) >= 0 &&
966 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
973 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
975 BDRVRawState
*s
= bs
->opaque
;
978 if (strstart(filename
, "/dev/cdrom", NULL
)) {
979 kern_return_t kernResult
;
980 io_iterator_t mediaIterator
;
981 char bsdPath
[ MAXPATHLEN
];
984 kernResult
= FindEjectableCDMedia( &mediaIterator
);
985 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
987 if ( bsdPath
[ 0 ] != '\0' ) {
988 strcat(bsdPath
,"s0");
989 /* some CDs don't have a partition 0 */
990 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
992 bsdPath
[strlen(bsdPath
)-1] = '1';
1000 IOObjectRelease( mediaIterator
);
1004 s
->type
= FTYPE_FILE
;
1005 #if defined(__linux__) && defined(CONFIG_AIO)
1006 if (strstart(filename
, "/dev/sg", NULL
)) {
1011 return raw_open_common(bs
, filename
, flags
);
1014 #if defined(__linux__)
1015 /* Note: we do not have a reliable method to detect if the floppy is
1016 present. The current method is to try to open the floppy at every
1017 I/O and to keep it opened during a few hundreds of ms. */
1018 static int fd_open(BlockDriverState
*bs
)
1020 BDRVRawState
*s
= bs
->opaque
;
1021 int last_media_present
;
1023 if (s
->type
!= FTYPE_FD
)
1025 last_media_present
= (s
->fd
>= 0);
1027 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1031 printf("Floppy closed\n");
1035 if (s
->fd_got_error
&&
1036 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1038 printf("No floppy (open delayed)\n");
1042 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1044 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1045 s
->fd_got_error
= 1;
1046 if (last_media_present
)
1047 s
->fd_media_changed
= 1;
1049 printf("No floppy\n");
1054 printf("Floppy opened\n");
1057 if (!last_media_present
)
1058 s
->fd_media_changed
= 1;
1059 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1060 s
->fd_got_error
= 0;
1064 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1066 BDRVRawState
*s
= bs
->opaque
;
1068 return ioctl(s
->fd
, req
, buf
);
1072 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1073 unsigned long int req
, void *buf
,
1074 BlockDriverCompletionFunc
*cb
, void *opaque
)
1076 BDRVRawState
*s
= bs
->opaque
;
1079 if (fd_open(bs
) < 0)
1082 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1085 acb
->aiocb
.aio_fildes
= s
->fd
;
1086 acb
->aiocb
.ev_signo
= SIGUSR2
;
1087 acb
->aiocb
.aio_offset
= 0;
1088 acb
->aiocb
.aio_flags
= 0;
1090 acb
->next
= posix_aio_state
->first_aio
;
1091 posix_aio_state
->first_aio
= acb
;
1093 acb
->aiocb
.aio_ioctl_buf
= buf
;
1094 acb
->aiocb
.aio_ioctl_cmd
= req
;
1095 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1096 raw_aio_remove(acb
);
1100 return &acb
->common
;
1104 #elif defined(__FreeBSD__)
1105 static int fd_open(BlockDriverState
*bs
)
1107 BDRVRawState
*s
= bs
->opaque
;
1109 /* this is just to ensure s->fd is sane (its called by io ops) */
1114 #else /* !linux && !FreeBSD */
1116 static int fd_open(BlockDriverState
*bs
)
1121 #endif /* !linux && !FreeBSD */
1123 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1127 struct stat stat_buf
;
1128 int64_t total_size
= 0;
1130 /* Read out options */
1131 while (options
&& options
->name
) {
1132 if (!strcmp(options
->name
, "size")) {
1133 total_size
= options
->value
.n
/ 512;
1138 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1142 if (fstat(fd
, &stat_buf
) < 0)
1144 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1146 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1153 static BlockDriver bdrv_host_device
= {
1154 .format_name
= "host_device",
1155 .instance_size
= sizeof(BDRVRawState
),
1156 .bdrv_probe_device
= hdev_probe_device
,
1157 .bdrv_open
= hdev_open
,
1158 .bdrv_close
= raw_close
,
1159 .bdrv_create
= hdev_create
,
1160 .bdrv_flush
= raw_flush
,
1163 .bdrv_aio_readv
= raw_aio_readv
,
1164 .bdrv_aio_writev
= raw_aio_writev
,
1167 .bdrv_read
= raw_read
,
1168 .bdrv_write
= raw_write
,
1169 .bdrv_getlength
= raw_getlength
,
1171 /* generic scsi device */
1173 .bdrv_ioctl
= hdev_ioctl
,
1175 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1181 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1183 BDRVRawState
*s
= bs
->opaque
;
1189 /* open will not fail even if no floppy is inserted */
1190 s
->open_flags
|= O_NONBLOCK
;
1192 ret
= raw_open_common(bs
, filename
, flags
);
1196 /* close fd so that we can reopen it as needed */
1199 s
->fd_media_changed
= 1;
1204 static int floppy_probe_device(const char *filename
)
1206 if (strstart(filename
, "/dev/fd", NULL
))
1212 static int floppy_is_inserted(BlockDriverState
*bs
)
1214 return fd_open(bs
) >= 0;
1217 static int floppy_media_changed(BlockDriverState
*bs
)
1219 BDRVRawState
*s
= bs
->opaque
;
1223 * XXX: we do not have a true media changed indication.
1224 * It does not work if the floppy is changed without trying to read it.
1227 ret
= s
->fd_media_changed
;
1228 s
->fd_media_changed
= 0;
1230 printf("Floppy changed=%d\n", ret
);
1235 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1237 BDRVRawState
*s
= bs
->opaque
;
1244 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1246 if (ioctl(fd
, FDEJECT
, 0) < 0)
1254 static BlockDriver bdrv_host_floppy
= {
1255 .format_name
= "host_floppy",
1256 .instance_size
= sizeof(BDRVRawState
),
1257 .bdrv_probe_device
= floppy_probe_device
,
1258 .bdrv_open
= floppy_open
,
1259 .bdrv_close
= raw_close
,
1260 .bdrv_create
= hdev_create
,
1261 .bdrv_flush
= raw_flush
,
1264 .bdrv_aio_readv
= raw_aio_readv
,
1265 .bdrv_aio_writev
= raw_aio_writev
,
1268 .bdrv_read
= raw_read
,
1269 .bdrv_write
= raw_write
,
1270 .bdrv_getlength
= raw_getlength
,
1272 /* removable device support */
1273 .bdrv_is_inserted
= floppy_is_inserted
,
1274 .bdrv_media_changed
= floppy_media_changed
,
1275 .bdrv_eject
= floppy_eject
,
1278 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1280 BDRVRawState
*s
= bs
->opaque
;
1282 /* open will not fail even if no CD is inserted */
1283 s
->open_flags
|= O_NONBLOCK
;
1286 return raw_open_common(bs
, filename
, flags
);
1289 static int cdrom_probe_device(const char *filename
)
1291 if (strstart(filename
, "/dev/cd", NULL
))
1296 static int cdrom_is_inserted(BlockDriverState
*bs
)
1298 BDRVRawState
*s
= bs
->opaque
;
1301 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1302 if (ret
== CDS_DISC_OK
)
1307 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1309 BDRVRawState
*s
= bs
->opaque
;
1312 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1313 perror("CDROMEJECT");
1315 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1316 perror("CDROMEJECT");
1322 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1324 BDRVRawState
*s
= bs
->opaque
;
1326 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1328 * Note: an error can happen if the distribution automatically
1331 /* perror("CDROM_LOCKDOOR"); */
1337 static BlockDriver bdrv_host_cdrom
= {
1338 .format_name
= "host_cdrom",
1339 .instance_size
= sizeof(BDRVRawState
),
1340 .bdrv_probe_device
= cdrom_probe_device
,
1341 .bdrv_open
= cdrom_open
,
1342 .bdrv_close
= raw_close
,
1343 .bdrv_create
= hdev_create
,
1344 .bdrv_flush
= raw_flush
,
1347 .bdrv_aio_readv
= raw_aio_readv
,
1348 .bdrv_aio_writev
= raw_aio_writev
,
1351 .bdrv_read
= raw_read
,
1352 .bdrv_write
= raw_write
,
1353 .bdrv_getlength
= raw_getlength
,
1355 /* removable device support */
1356 .bdrv_is_inserted
= cdrom_is_inserted
,
1357 .bdrv_eject
= cdrom_eject
,
1358 .bdrv_set_locked
= cdrom_set_locked
,
1360 /* generic scsi device */
1361 .bdrv_ioctl
= hdev_ioctl
,
1363 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1366 #endif /* __linux__ */
1369 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1371 BDRVRawState
*s
= bs
->opaque
;
1376 ret
= raw_open_common(bs
, filename
, flags
);
1380 /* make sure the door isnt locked at this time */
1381 ioctl(s
->fd
, CDIOCALLOW
);
1385 static int cdrom_probe_device(const char *filename
)
1387 if (strstart(filename
, "/dev/cd", NULL
) ||
1388 strstart(filename
, "/dev/acd", NULL
))
1393 static int cdrom_reopen(BlockDriverState
*bs
)
1395 BDRVRawState
*s
= bs
->opaque
;
1399 * Force reread of possibly changed/newly loaded disc,
1400 * FreeBSD seems to not notice sometimes...
1404 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1411 /* make sure the door isnt locked at this time */
1412 ioctl(s
->fd
, CDIOCALLOW
);
1416 static int cdrom_is_inserted(BlockDriverState
*bs
)
1418 return raw_getlength(bs
) > 0;
1421 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1423 BDRVRawState
*s
= bs
->opaque
;
1428 (void) ioctl(s
->fd
, CDIOCALLOW
);
1431 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1432 perror("CDIOCEJECT");
1434 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1435 perror("CDIOCCLOSE");
1438 if (cdrom_reopen(bs
) < 0)
1443 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1445 BDRVRawState
*s
= bs
->opaque
;
1449 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1451 * Note: an error can happen if the distribution automatically
1454 /* perror("CDROM_LOCKDOOR"); */
1460 static BlockDriver bdrv_host_cdrom
= {
1461 .format_name
= "host_cdrom",
1462 .instance_size
= sizeof(BDRVRawState
),
1463 .bdrv_probe_device
= cdrom_probe_device
,
1464 .bdrv_open
= cdrom_open
,
1465 .bdrv_close
= raw_close
,
1466 .bdrv_create
= hdev_create
,
1467 .bdrv_flush
= raw_flush
,
1470 .bdrv_aio_readv
= raw_aio_readv
,
1471 .bdrv_aio_writev
= raw_aio_writev
,
1474 .bdrv_read
= raw_read
,
1475 .bdrv_write
= raw_write
,
1476 .bdrv_getlength
= raw_getlength
,
1478 /* removable device support */
1479 .bdrv_is_inserted
= cdrom_is_inserted
,
1480 .bdrv_eject
= cdrom_eject
,
1481 .bdrv_set_locked
= cdrom_set_locked
,
1483 #endif /* __FreeBSD__ */
1485 static void bdrv_raw_init(void)
1488 * Register all the drivers. Note that order is important, the driver
1489 * registered last will get probed first.
1491 bdrv_register(&bdrv_raw
);
1492 bdrv_register(&bdrv_host_device
);
1494 bdrv_register(&bdrv_host_floppy
);
1495 bdrv_register(&bdrv_host_cdrom
);
1498 bdrv_register(&bdrv_host_cdrom
);
1502 block_init(bdrv_raw_init
);