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"
32 #include "posix-aio-compat.h"
37 #include <sys/param.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/IOBSD.h>
40 #include <IOKit/storage/IOMediaBSDClient.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <IOKit/storage/IOCDMedia.h>
43 //#include <IOKit/storage/IOCDTypes.h>
44 #include <CoreFoundation/CoreFoundation.h>
48 #define _POSIX_PTHREAD_SEMANTICS 1
53 #include <sys/ioctl.h>
54 #include <linux/cdrom.h>
64 #include <sys/ioctl.h>
65 #include <sys/disklabel.h>
70 #include <sys/ioctl.h>
71 #include <sys/diskslice.h>
74 //#define DEBUG_FLOPPY
77 #if defined(DEBUG_BLOCK)
78 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
79 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
81 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
84 /* OS X does not have O_DSYNC */
87 #define O_DSYNC O_SYNC
88 #elif defined(O_FSYNC)
89 #define O_DSYNC O_FSYNC
93 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
95 #define O_DIRECT O_DSYNC
102 #define ALIGNED_BUFFER_SIZE (32 * 512)
104 /* if the FD is not accessed during that time (in ms), we try to
105 reopen it to see if the disk has been changed */
106 #define FD_OPEN_TIMEOUT 1000
108 typedef struct BDRVRawState
{
111 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 uint8_t* aligned_buf
;
123 static int posix_aio_init(void);
125 static int fd_open(BlockDriverState
*bs
);
126 static int64_t raw_getlength(BlockDriverState
*bs
);
128 #if defined(__FreeBSD__)
129 static int cdrom_reopen(BlockDriverState
*bs
);
132 static int raw_open_common(BlockDriverState
*bs
, const char *filename
,
133 int bdrv_flags
, int open_flags
)
135 BDRVRawState
*s
= bs
->opaque
;
140 s
->lseek_err_cnt
= 0;
142 s
->open_flags
= open_flags
| O_BINARY
;
143 s
->open_flags
&= ~O_ACCMODE
;
144 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
145 s
->open_flags
|= O_RDWR
;
147 s
->open_flags
|= O_RDONLY
;
151 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
152 * and O_DIRECT for no caching. */
153 if ((bdrv_flags
& BDRV_O_NOCACHE
))
154 s
->open_flags
|= O_DIRECT
;
155 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
156 s
->open_flags
|= O_DSYNC
;
159 fd
= open(filename
, s
->open_flags
, 0644);
167 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
) {
179 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
181 BDRVRawState
*s
= bs
->opaque
;
184 s
->type
= FTYPE_FILE
;
185 if (flags
& BDRV_O_CREAT
)
186 open_flags
= O_CREAT
| O_TRUNC
;
188 return raw_open_common(bs
, filename
, flags
, open_flags
);
191 /* XXX: use host sector size if necessary with:
192 #ifdef DIOCGSECTORSIZE
194 unsigned int sectorsize = 512;
195 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
196 sectorsize > bufsize)
197 bufsize = sectorsize;
201 u_int32_t blockSize = 512;
202 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
209 * offset and count are in bytes, but must be multiples of 512 for files
210 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
212 * This function may be called without alignment if the caller ensures
213 * that O_DIRECT is not in effect.
215 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
216 uint8_t *buf
, int count
)
218 BDRVRawState
*s
= bs
->opaque
;
225 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
226 ++(s
->lseek_err_cnt
);
227 if(s
->lseek_err_cnt
<= 10) {
228 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
229 "] lseek failed : %d = %s\n",
230 s
->fd
, bs
->filename
, offset
, buf
, count
,
231 bs
->total_sectors
, errno
, strerror(errno
));
237 ret
= read(s
->fd
, buf
, count
);
239 goto label__raw_read__success
;
241 /* Allow reads beyond the end (needed for pwrite) */
242 if ((ret
== 0) && bs
->growable
) {
243 int64_t size
= raw_getlength(bs
);
244 if (offset
>= size
) {
245 memset(buf
, 0, count
);
247 goto label__raw_read__success
;
251 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
252 "] read failed %d : %d = %s\n",
253 s
->fd
, bs
->filename
, offset
, buf
, count
,
254 bs
->total_sectors
, ret
, errno
, strerror(errno
));
256 /* Try harder for CDrom. */
257 if (bs
->type
== BDRV_TYPE_CDROM
) {
258 lseek(s
->fd
, offset
, SEEK_SET
);
259 ret
= read(s
->fd
, buf
, count
);
261 goto label__raw_read__success
;
262 lseek(s
->fd
, offset
, SEEK_SET
);
263 ret
= read(s
->fd
, buf
, count
);
265 goto label__raw_read__success
;
267 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
268 "] retry read failed %d : %d = %s\n",
269 s
->fd
, bs
->filename
, offset
, buf
, count
,
270 bs
->total_sectors
, ret
, errno
, strerror(errno
));
273 label__raw_read__success
:
275 return (ret
< 0) ? -errno
: ret
;
279 * offset and count are in bytes, but must be multiples of 512 for files
280 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
282 * This function may be called without alignment if the caller ensures
283 * that O_DIRECT is not in effect.
285 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
286 const uint8_t *buf
, int count
)
288 BDRVRawState
*s
= bs
->opaque
;
295 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
296 ++(s
->lseek_err_cnt
);
297 if(s
->lseek_err_cnt
) {
298 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
299 PRId64
"] lseek failed : %d = %s\n",
300 s
->fd
, bs
->filename
, offset
, buf
, count
,
301 bs
->total_sectors
, errno
, strerror(errno
));
305 s
->lseek_err_cnt
= 0;
307 ret
= write(s
->fd
, buf
, count
);
309 goto label__raw_write__success
;
311 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
312 "] write failed %d : %d = %s\n",
313 s
->fd
, bs
->filename
, offset
, buf
, count
,
314 bs
->total_sectors
, ret
, errno
, strerror(errno
));
316 label__raw_write__success
:
318 return (ret
< 0) ? -errno
: ret
;
323 * offset and count are in bytes and possibly not aligned. For files opened
324 * with O_DIRECT, necessary alignments are ensured before calling
325 * raw_pread_aligned to do the actual read.
327 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
328 uint8_t *buf
, int count
)
330 BDRVRawState
*s
= bs
->opaque
;
331 int size
, ret
, shift
, sum
;
335 if (s
->aligned_buf
!= NULL
) {
337 if (offset
& 0x1ff) {
338 /* align offset on a 512 bytes boundary */
340 shift
= offset
& 0x1ff;
341 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
342 if (size
> ALIGNED_BUFFER_SIZE
)
343 size
= ALIGNED_BUFFER_SIZE
;
344 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
351 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
361 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
363 /* read on aligned buffer */
367 size
= (count
+ 0x1ff) & ~0x1ff;
368 if (size
> ALIGNED_BUFFER_SIZE
)
369 size
= ALIGNED_BUFFER_SIZE
;
371 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
379 memcpy(buf
, s
->aligned_buf
, size
);
391 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
394 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
395 uint8_t *buf
, int nb_sectors
)
399 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
400 if (ret
== (nb_sectors
* 512))
406 * offset and count are in bytes and possibly not aligned. For files opened
407 * with O_DIRECT, necessary alignments are ensured before calling
408 * raw_pwrite_aligned to do the actual write.
410 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
411 const uint8_t *buf
, int count
)
413 BDRVRawState
*s
= bs
->opaque
;
414 int size
, ret
, shift
, sum
;
418 if (s
->aligned_buf
!= NULL
) {
420 if (offset
& 0x1ff) {
421 /* align offset on a 512 bytes boundary */
422 shift
= offset
& 0x1ff;
423 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
430 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
432 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
444 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
446 while ((size
= (count
& ~0x1ff)) != 0) {
448 if (size
> ALIGNED_BUFFER_SIZE
)
449 size
= ALIGNED_BUFFER_SIZE
;
451 memcpy(s
->aligned_buf
, buf
, size
);
453 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
462 /* here, count < 512 because (count & ~0x1ff) == 0 */
464 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
467 memcpy(s
->aligned_buf
, buf
, count
);
469 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
480 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
483 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
484 const uint8_t *buf
, int nb_sectors
)
487 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
488 if (ret
== (nb_sectors
* 512))
494 /***********************************************************/
495 /* Unix AIO using POSIX AIO */
497 typedef struct RawAIOCB
{
498 BlockDriverAIOCB common
;
499 struct qemu_paiocb aiocb
;
500 struct RawAIOCB
*next
;
504 typedef struct PosixAioState
510 static void posix_aio_read(void *opaque
)
512 PosixAioState
*s
= opaque
;
513 RawAIOCB
*acb
, **pacb
;
517 struct qemu_signalfd_siginfo siginfo
;
521 /* try to read from signalfd, don't freak out if we can't read anything */
523 while (offset
< 128) {
526 len
= read(s
->fd
, sig
.buf
+ offset
, 128 - offset
);
527 if (len
== -1 && errno
== EINTR
)
529 if (len
== -1 && errno
== EAGAIN
) {
530 /* there is no natural reason for this to happen,
531 * so we'll spin hard until we get everything just
532 * to be on the safe side. */
541 pacb
= &s
->first_aio
;
546 ret
= qemu_paio_error(&acb
->aiocb
);
547 if (ret
== ECANCELED
) {
548 /* remove the request */
550 qemu_aio_release(acb
);
551 } else if (ret
!= EINPROGRESS
) {
554 ret
= qemu_paio_return(&acb
->aiocb
);
555 if (ret
== acb
->aiocb
.aio_nbytes
)
562 /* remove the request */
564 /* call the callback */
565 acb
->common
.cb(acb
->common
.opaque
, ret
);
566 qemu_aio_release(acb
);
576 static int posix_aio_flush(void *opaque
)
578 PosixAioState
*s
= opaque
;
579 return !!s
->first_aio
;
582 static PosixAioState
*posix_aio_state
;
584 static int posix_aio_init(void)
588 struct qemu_paioinit ai
;
593 s
= qemu_malloc(sizeof(PosixAioState
));
595 /* Make sure to block AIO signal */
597 sigaddset(&mask
, SIGUSR2
);
598 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
601 s
->fd
= qemu_signalfd(&mask
);
603 fprintf(stderr
, "failed to create signalfd\n");
607 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
609 qemu_aio_set_fd_handler(s
->fd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
611 memset(&ai
, 0, sizeof(ai
));
621 static void raw_aio_remove(RawAIOCB
*acb
)
625 /* remove the callback from the queue */
626 pacb
= &posix_aio_state
->first_aio
;
629 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
631 } else if (*pacb
== acb
) {
633 qemu_aio_release(acb
);
636 pacb
= &(*pacb
)->next
;
640 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
643 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
645 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
646 if (ret
== QEMU_PAIO_NOTCANCELED
) {
647 /* fail safe: if the aio could not be canceled, we wait for
649 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
655 static AIOPool raw_aio_pool
= {
656 .aiocb_size
= sizeof(RawAIOCB
),
657 .cancel
= raw_aio_cancel
,
660 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
661 QEMUIOVector
*qiov
, int nb_sectors
,
662 BlockDriverCompletionFunc
*cb
, void *opaque
)
664 BDRVRawState
*s
= bs
->opaque
;
670 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
673 acb
->aiocb
.aio_fildes
= s
->fd
;
674 acb
->aiocb
.ev_signo
= SIGUSR2
;
675 acb
->aiocb
.aio_iov
= qiov
->iov
;
676 acb
->aiocb
.aio_niov
= qiov
->niov
;
677 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
678 acb
->aiocb
.aio_offset
= sector_num
* 512;
679 acb
->aiocb
.aio_flags
= 0;
682 * If O_DIRECT is used the buffer needs to be aligned on a sector
683 * boundary. Tell the low level code to ensure that in case it's
687 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
689 acb
->next
= posix_aio_state
->first_aio
;
690 posix_aio_state
->first_aio
= acb
;
694 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
695 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
696 BlockDriverCompletionFunc
*cb
, void *opaque
)
700 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
703 if (qemu_paio_read(&acb
->aiocb
) < 0) {
710 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
711 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
712 BlockDriverCompletionFunc
*cb
, void *opaque
)
716 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
719 if (qemu_paio_write(&acb
->aiocb
) < 0) {
725 #else /* CONFIG_AIO */
726 static int posix_aio_init(void)
730 #endif /* CONFIG_AIO */
733 static void raw_close(BlockDriverState
*bs
)
735 BDRVRawState
*s
= bs
->opaque
;
739 if (s
->aligned_buf
!= NULL
)
740 qemu_free(s
->aligned_buf
);
744 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
746 BDRVRawState
*s
= bs
->opaque
;
747 if (s
->type
!= FTYPE_FILE
)
749 if (ftruncate(s
->fd
, offset
) < 0)
755 static int64_t raw_getlength(BlockDriverState
*bs
)
757 BDRVRawState
*s
= bs
->opaque
;
763 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
766 if (ioctl(fd
, DIOCGDINFO
, &dl
))
768 return (uint64_t)dl
.d_secsize
*
769 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
773 #else /* !__OpenBSD__ */
774 static int64_t raw_getlength(BlockDriverState
*bs
)
776 BDRVRawState
*s
= bs
->opaque
;
786 struct dk_minfo minfo
;
799 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
800 #ifdef DIOCGMEDIASIZE
801 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
802 #elif defined(DIOCGPART)
805 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
806 size
= pi
.media_size
;
813 size
= LONG_LONG_MAX
;
815 size
= lseek(fd
, 0LL, SEEK_END
);
820 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
821 if (size
== 2048LL * (unsigned)-1)
823 /* XXX no disc? maybe we need to reopen... */
824 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
834 * use the DKIOCGMEDIAINFO ioctl to read the size.
836 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
838 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
839 } else /* there are reports that lseek on some devices
840 fails, but irc discussion said that contingency
841 on contingency was overkill */
844 size
= lseek(fd
, 0, SEEK_END
);
850 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
853 int64_t total_size
= 0;
855 /* Read out options */
856 while (options
&& options
->name
) {
857 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
858 total_size
= options
->value
.n
/ 512;
863 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
867 ftruncate(fd
, total_size
* 512);
872 static void raw_flush(BlockDriverState
*bs
)
874 BDRVRawState
*s
= bs
->opaque
;
879 static QEMUOptionParameter raw_create_options
[] = {
881 .name
= BLOCK_OPT_SIZE
,
883 .help
= "Virtual disk size"
888 static BlockDriver bdrv_raw
= {
889 .format_name
= "raw",
890 .instance_size
= sizeof(BDRVRawState
),
891 .bdrv_probe
= NULL
, /* no probe for protocols */
892 .bdrv_open
= raw_open
,
893 .bdrv_read
= raw_read
,
894 .bdrv_write
= raw_write
,
895 .bdrv_close
= raw_close
,
896 .bdrv_create
= raw_create
,
897 .bdrv_flush
= raw_flush
,
900 .bdrv_aio_readv
= raw_aio_readv
,
901 .bdrv_aio_writev
= raw_aio_writev
,
904 .bdrv_truncate
= raw_truncate
,
905 .bdrv_getlength
= raw_getlength
,
907 .create_options
= raw_create_options
,
910 /***********************************************/
914 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
915 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
917 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
919 kern_return_t kernResult
;
920 mach_port_t masterPort
;
921 CFMutableDictionaryRef classesToMatch
;
923 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
924 if ( KERN_SUCCESS
!= kernResult
) {
925 printf( "IOMasterPort returned %d\n", kernResult
);
928 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
929 if ( classesToMatch
== NULL
) {
930 printf( "IOServiceMatching returned a NULL dictionary.\n" );
932 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
934 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
935 if ( KERN_SUCCESS
!= kernResult
)
937 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
943 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
945 io_object_t nextMedia
;
946 kern_return_t kernResult
= KERN_FAILURE
;
948 nextMedia
= IOIteratorNext( mediaIterator
);
951 CFTypeRef bsdPathAsCFString
;
952 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
953 if ( bsdPathAsCFString
) {
954 size_t devPathLength
;
955 strcpy( bsdPath
, _PATH_DEV
);
956 strcat( bsdPath
, "r" );
957 devPathLength
= strlen( bsdPath
);
958 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
959 kernResult
= KERN_SUCCESS
;
961 CFRelease( bsdPathAsCFString
);
963 IOObjectRelease( nextMedia
);
971 static int hdev_probe_device(const char *filename
)
975 /* allow a dedicated CD-ROM driver to match with a higher priority */
976 if (strstart(filename
, "/dev/cdrom", NULL
))
979 if (stat(filename
, &st
) >= 0 &&
980 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
987 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
989 BDRVRawState
*s
= bs
->opaque
;
992 if (strstart(filename
, "/dev/cdrom", NULL
)) {
993 kern_return_t kernResult
;
994 io_iterator_t mediaIterator
;
995 char bsdPath
[ MAXPATHLEN
];
998 kernResult
= FindEjectableCDMedia( &mediaIterator
);
999 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1001 if ( bsdPath
[ 0 ] != '\0' ) {
1002 strcat(bsdPath
,"s0");
1003 /* some CDs don't have a partition 0 */
1004 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1006 bsdPath
[strlen(bsdPath
)-1] = '1';
1013 if ( mediaIterator
)
1014 IOObjectRelease( mediaIterator
);
1018 s
->type
= FTYPE_FILE
;
1019 #if defined(__linux__) && defined(CONFIG_AIO)
1020 if (strstart(filename
, "/dev/sg", NULL
)) {
1025 return raw_open_common(bs
, filename
, flags
, 0);
1028 #if defined(__linux__)
1029 /* Note: we do not have a reliable method to detect if the floppy is
1030 present. The current method is to try to open the floppy at every
1031 I/O and to keep it opened during a few hundreds of ms. */
1032 static int fd_open(BlockDriverState
*bs
)
1034 BDRVRawState
*s
= bs
->opaque
;
1035 int last_media_present
;
1037 if (s
->type
!= FTYPE_FD
)
1039 last_media_present
= (s
->fd
>= 0);
1041 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1045 printf("Floppy closed\n");
1049 if (s
->fd_got_error
&&
1050 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1052 printf("No floppy (open delayed)\n");
1056 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1058 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1059 s
->fd_got_error
= 1;
1060 if (last_media_present
)
1061 s
->fd_media_changed
= 1;
1063 printf("No floppy\n");
1068 printf("Floppy opened\n");
1071 if (!last_media_present
)
1072 s
->fd_media_changed
= 1;
1073 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1074 s
->fd_got_error
= 0;
1078 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1080 BDRVRawState
*s
= bs
->opaque
;
1082 return ioctl(s
->fd
, req
, buf
);
1086 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1087 unsigned long int req
, void *buf
,
1088 BlockDriverCompletionFunc
*cb
, void *opaque
)
1090 BDRVRawState
*s
= bs
->opaque
;
1093 if (fd_open(bs
) < 0)
1096 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1099 acb
->aiocb
.aio_fildes
= s
->fd
;
1100 acb
->aiocb
.ev_signo
= SIGUSR2
;
1101 acb
->aiocb
.aio_offset
= 0;
1102 acb
->aiocb
.aio_flags
= 0;
1104 acb
->next
= posix_aio_state
->first_aio
;
1105 posix_aio_state
->first_aio
= acb
;
1107 acb
->aiocb
.aio_ioctl_buf
= buf
;
1108 acb
->aiocb
.aio_ioctl_cmd
= req
;
1109 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1110 raw_aio_remove(acb
);
1114 return &acb
->common
;
1118 #elif defined(__FreeBSD__)
1119 static int fd_open(BlockDriverState
*bs
)
1121 BDRVRawState
*s
= bs
->opaque
;
1123 /* this is just to ensure s->fd is sane (its called by io ops) */
1128 #else /* !linux && !FreeBSD */
1130 static int fd_open(BlockDriverState
*bs
)
1135 #endif /* !linux && !FreeBSD */
1137 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1141 struct stat stat_buf
;
1142 int64_t total_size
= 0;
1144 /* Read out options */
1145 while (options
&& options
->name
) {
1146 if (!strcmp(options
->name
, "size")) {
1147 total_size
= options
->value
.n
/ 512;
1152 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1156 if (fstat(fd
, &stat_buf
) < 0)
1158 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1160 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1167 static BlockDriver bdrv_host_device
= {
1168 .format_name
= "host_device",
1169 .instance_size
= sizeof(BDRVRawState
),
1170 .bdrv_probe_device
= hdev_probe_device
,
1171 .bdrv_open
= hdev_open
,
1172 .bdrv_close
= raw_close
,
1173 .bdrv_create
= hdev_create
,
1174 .bdrv_flush
= raw_flush
,
1177 .bdrv_aio_readv
= raw_aio_readv
,
1178 .bdrv_aio_writev
= raw_aio_writev
,
1181 .bdrv_read
= raw_read
,
1182 .bdrv_write
= raw_write
,
1183 .bdrv_getlength
= raw_getlength
,
1185 /* generic scsi device */
1187 .bdrv_ioctl
= hdev_ioctl
,
1189 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1195 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1197 BDRVRawState
*s
= bs
->opaque
;
1204 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1205 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1209 /* close fd so that we can reopen it as needed */
1212 s
->fd_media_changed
= 1;
1217 static int floppy_probe_device(const char *filename
)
1219 if (strstart(filename
, "/dev/fd", NULL
))
1225 static int floppy_is_inserted(BlockDriverState
*bs
)
1227 return fd_open(bs
) >= 0;
1230 static int floppy_media_changed(BlockDriverState
*bs
)
1232 BDRVRawState
*s
= bs
->opaque
;
1236 * XXX: we do not have a true media changed indication.
1237 * It does not work if the floppy is changed without trying to read it.
1240 ret
= s
->fd_media_changed
;
1241 s
->fd_media_changed
= 0;
1243 printf("Floppy changed=%d\n", ret
);
1248 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1250 BDRVRawState
*s
= bs
->opaque
;
1257 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1259 if (ioctl(fd
, FDEJECT
, 0) < 0)
1267 static BlockDriver bdrv_host_floppy
= {
1268 .format_name
= "host_floppy",
1269 .instance_size
= sizeof(BDRVRawState
),
1270 .bdrv_probe_device
= floppy_probe_device
,
1271 .bdrv_open
= floppy_open
,
1272 .bdrv_close
= raw_close
,
1273 .bdrv_create
= hdev_create
,
1274 .bdrv_flush
= raw_flush
,
1277 .bdrv_aio_readv
= raw_aio_readv
,
1278 .bdrv_aio_writev
= raw_aio_writev
,
1281 .bdrv_read
= raw_read
,
1282 .bdrv_write
= raw_write
,
1283 .bdrv_getlength
= raw_getlength
,
1285 /* removable device support */
1286 .bdrv_is_inserted
= floppy_is_inserted
,
1287 .bdrv_media_changed
= floppy_media_changed
,
1288 .bdrv_eject
= floppy_eject
,
1291 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1293 BDRVRawState
*s
= bs
->opaque
;
1297 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1298 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1301 static int cdrom_probe_device(const char *filename
)
1303 if (strstart(filename
, "/dev/cd", NULL
))
1308 static int cdrom_is_inserted(BlockDriverState
*bs
)
1310 BDRVRawState
*s
= bs
->opaque
;
1313 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1314 if (ret
== CDS_DISC_OK
)
1319 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1321 BDRVRawState
*s
= bs
->opaque
;
1324 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1325 perror("CDROMEJECT");
1327 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1328 perror("CDROMEJECT");
1334 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1336 BDRVRawState
*s
= bs
->opaque
;
1338 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1340 * Note: an error can happen if the distribution automatically
1343 /* perror("CDROM_LOCKDOOR"); */
1349 static BlockDriver bdrv_host_cdrom
= {
1350 .format_name
= "host_cdrom",
1351 .instance_size
= sizeof(BDRVRawState
),
1352 .bdrv_probe_device
= cdrom_probe_device
,
1353 .bdrv_open
= cdrom_open
,
1354 .bdrv_close
= raw_close
,
1355 .bdrv_create
= hdev_create
,
1356 .bdrv_flush
= raw_flush
,
1359 .bdrv_aio_readv
= raw_aio_readv
,
1360 .bdrv_aio_writev
= raw_aio_writev
,
1363 .bdrv_read
= raw_read
,
1364 .bdrv_write
= raw_write
,
1365 .bdrv_getlength
= raw_getlength
,
1367 /* removable device support */
1368 .bdrv_is_inserted
= cdrom_is_inserted
,
1369 .bdrv_eject
= cdrom_eject
,
1370 .bdrv_set_locked
= cdrom_set_locked
,
1372 /* generic scsi device */
1373 .bdrv_ioctl
= hdev_ioctl
,
1375 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1378 #endif /* __linux__ */
1381 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1383 BDRVRawState
*s
= bs
->opaque
;
1388 ret
= raw_open_common(bs
, filename
, flags
, 0);
1392 /* make sure the door isnt locked at this time */
1393 ioctl(s
->fd
, CDIOCALLOW
);
1397 static int cdrom_probe_device(const char *filename
)
1399 if (strstart(filename
, "/dev/cd", NULL
) ||
1400 strstart(filename
, "/dev/acd", NULL
))
1405 static int cdrom_reopen(BlockDriverState
*bs
)
1407 BDRVRawState
*s
= bs
->opaque
;
1411 * Force reread of possibly changed/newly loaded disc,
1412 * FreeBSD seems to not notice sometimes...
1416 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1423 /* make sure the door isnt locked at this time */
1424 ioctl(s
->fd
, CDIOCALLOW
);
1428 static int cdrom_is_inserted(BlockDriverState
*bs
)
1430 return raw_getlength(bs
) > 0;
1433 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1435 BDRVRawState
*s
= bs
->opaque
;
1440 (void) ioctl(s
->fd
, CDIOCALLOW
);
1443 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1444 perror("CDIOCEJECT");
1446 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1447 perror("CDIOCCLOSE");
1450 if (cdrom_reopen(bs
) < 0)
1455 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1457 BDRVRawState
*s
= bs
->opaque
;
1461 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1463 * Note: an error can happen if the distribution automatically
1466 /* perror("CDROM_LOCKDOOR"); */
1472 static BlockDriver bdrv_host_cdrom
= {
1473 .format_name
= "host_cdrom",
1474 .instance_size
= sizeof(BDRVRawState
),
1475 .bdrv_probe_device
= cdrom_probe_device
,
1476 .bdrv_open
= cdrom_open
,
1477 .bdrv_close
= raw_close
,
1478 .bdrv_create
= hdev_create
,
1479 .bdrv_flush
= raw_flush
,
1482 .bdrv_aio_readv
= raw_aio_readv
,
1483 .bdrv_aio_writev
= raw_aio_writev
,
1486 .bdrv_read
= raw_read
,
1487 .bdrv_write
= raw_write
,
1488 .bdrv_getlength
= raw_getlength
,
1490 /* removable device support */
1491 .bdrv_is_inserted
= cdrom_is_inserted
,
1492 .bdrv_eject
= cdrom_eject
,
1493 .bdrv_set_locked
= cdrom_set_locked
,
1495 #endif /* __FreeBSD__ */
1497 static void bdrv_raw_init(void)
1500 * Register all the drivers. Note that order is important, the driver
1501 * registered last will get probed first.
1503 bdrv_register(&bdrv_raw
);
1504 bdrv_register(&bdrv_host_device
);
1506 bdrv_register(&bdrv_host_floppy
);
1507 bdrv_register(&bdrv_host_cdrom
);
1510 bdrv_register(&bdrv_host_cdrom
);
1514 block_init(bdrv_raw_init
);