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"
33 #include "posix-aio-compat.h"
38 #include <sys/param.h>
39 #include <IOKit/IOKitLib.h>
40 #include <IOKit/IOBSD.h>
41 #include <IOKit/storage/IOMediaBSDClient.h>
42 #include <IOKit/storage/IOMedia.h>
43 #include <IOKit/storage/IOCDMedia.h>
44 //#include <IOKit/storage/IOCDTypes.h>
45 #include <CoreFoundation/CoreFoundation.h>
49 #define _POSIX_PTHREAD_SEMANTICS 1
54 #include <sys/ioctl.h>
55 #include <linux/cdrom.h>
65 #include <sys/ioctl.h>
66 #include <sys/disklabel.h>
71 #include <sys/ioctl.h>
72 #include <sys/diskslice.h>
75 //#define DEBUG_FLOPPY
78 #if defined(DEBUG_BLOCK)
79 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
80 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
82 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
85 /* OS X does not have O_DSYNC */
88 #define O_DSYNC O_SYNC
89 #elif defined(O_FSYNC)
90 #define O_DSYNC O_FSYNC
94 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
96 #define O_DIRECT O_DSYNC
103 #define ALIGNED_BUFFER_SIZE (32 * 512)
105 /* if the FD is not accessed during that time (in ms), we try to
106 reopen it to see if the disk has been changed */
107 #define FD_OPEN_TIMEOUT 1000
109 typedef struct BDRVRawState
{
112 unsigned int lseek_err_cnt
;
114 #if defined(__linux__)
115 /* linux floppy specific */
116 int64_t fd_open_time
;
117 int64_t fd_error_time
;
119 int fd_media_changed
;
121 uint8_t* aligned_buf
;
124 static int posix_aio_init(void);
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
;
141 s
->lseek_err_cnt
= 0;
143 s
->open_flags
= open_flags
| O_BINARY
;
144 s
->open_flags
&= ~O_ACCMODE
;
145 if ((bdrv_flags
& BDRV_O_ACCESS
) == BDRV_O_RDWR
) {
146 s
->open_flags
|= O_RDWR
;
148 s
->open_flags
|= O_RDONLY
;
152 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
153 * and O_DIRECT for no caching. */
154 if ((bdrv_flags
& BDRV_O_NOCACHE
))
155 s
->open_flags
|= O_DIRECT
;
156 else if (!(bdrv_flags
& BDRV_O_CACHE_WB
))
157 s
->open_flags
|= O_DSYNC
;
160 fd
= open(filename
, s
->open_flags
, 0644);
168 s
->aligned_buf
= NULL
;
169 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
170 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
171 if (s
->aligned_buf
== NULL
) {
180 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
182 BDRVRawState
*s
= bs
->opaque
;
185 s
->type
= FTYPE_FILE
;
186 if (flags
& BDRV_O_CREAT
)
187 open_flags
= O_CREAT
| O_TRUNC
;
189 return raw_open_common(bs
, filename
, flags
, open_flags
);
192 /* XXX: use host sector size if necessary with:
193 #ifdef DIOCGSECTORSIZE
195 unsigned int sectorsize = 512;
196 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
197 sectorsize > bufsize)
198 bufsize = sectorsize;
202 u_int32_t blockSize = 512;
203 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
210 * offset and count are in bytes, but must be multiples of 512 for files
211 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
213 * This function may be called without alignment if the caller ensures
214 * that O_DIRECT is not in effect.
216 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
217 uint8_t *buf
, int count
)
219 BDRVRawState
*s
= bs
->opaque
;
226 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
227 ++(s
->lseek_err_cnt
);
228 if(s
->lseek_err_cnt
<= 10) {
229 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
230 "] lseek failed : %d = %s\n",
231 s
->fd
, bs
->filename
, offset
, buf
, count
,
232 bs
->total_sectors
, errno
, strerror(errno
));
238 ret
= read(s
->fd
, buf
, count
);
240 goto label__raw_read__success
;
242 /* Allow reads beyond the end (needed for pwrite) */
243 if ((ret
== 0) && bs
->growable
) {
244 int64_t size
= raw_getlength(bs
);
245 if (offset
>= size
) {
246 memset(buf
, 0, count
);
248 goto label__raw_read__success
;
252 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
253 "] read failed %d : %d = %s\n",
254 s
->fd
, bs
->filename
, offset
, buf
, count
,
255 bs
->total_sectors
, ret
, errno
, strerror(errno
));
257 /* Try harder for CDrom. */
258 if (bs
->type
== BDRV_TYPE_CDROM
) {
259 lseek(s
->fd
, offset
, SEEK_SET
);
260 ret
= read(s
->fd
, buf
, count
);
262 goto label__raw_read__success
;
263 lseek(s
->fd
, offset
, SEEK_SET
);
264 ret
= read(s
->fd
, buf
, count
);
266 goto label__raw_read__success
;
268 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
269 "] retry read failed %d : %d = %s\n",
270 s
->fd
, bs
->filename
, offset
, buf
, count
,
271 bs
->total_sectors
, ret
, errno
, strerror(errno
));
274 label__raw_read__success
:
276 return (ret
< 0) ? -errno
: ret
;
280 * offset and count are in bytes, but must be multiples of 512 for files
281 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
283 * This function may be called without alignment if the caller ensures
284 * that O_DIRECT is not in effect.
286 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
287 const uint8_t *buf
, int count
)
289 BDRVRawState
*s
= bs
->opaque
;
296 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
297 ++(s
->lseek_err_cnt
);
298 if(s
->lseek_err_cnt
) {
299 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
300 PRId64
"] lseek failed : %d = %s\n",
301 s
->fd
, bs
->filename
, offset
, buf
, count
,
302 bs
->total_sectors
, errno
, strerror(errno
));
306 s
->lseek_err_cnt
= 0;
308 ret
= write(s
->fd
, buf
, count
);
310 goto label__raw_write__success
;
312 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
313 "] write failed %d : %d = %s\n",
314 s
->fd
, bs
->filename
, offset
, buf
, count
,
315 bs
->total_sectors
, ret
, errno
, strerror(errno
));
317 label__raw_write__success
:
319 return (ret
< 0) ? -errno
: ret
;
324 * offset and count are in bytes and possibly not aligned. For files opened
325 * with O_DIRECT, necessary alignments are ensured before calling
326 * raw_pread_aligned to do the actual read.
328 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
329 uint8_t *buf
, int count
)
331 BDRVRawState
*s
= bs
->opaque
;
332 int size
, ret
, shift
, sum
;
336 if (s
->aligned_buf
!= NULL
) {
338 if (offset
& 0x1ff) {
339 /* align offset on a 512 bytes boundary */
341 shift
= offset
& 0x1ff;
342 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
343 if (size
> ALIGNED_BUFFER_SIZE
)
344 size
= ALIGNED_BUFFER_SIZE
;
345 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
352 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
362 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
364 /* read on aligned buffer */
368 size
= (count
+ 0x1ff) & ~0x1ff;
369 if (size
> ALIGNED_BUFFER_SIZE
)
370 size
= ALIGNED_BUFFER_SIZE
;
372 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
380 memcpy(buf
, s
->aligned_buf
, size
);
392 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
395 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
396 uint8_t *buf
, int nb_sectors
)
400 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
401 if (ret
== (nb_sectors
* 512))
407 * offset and count are in bytes and possibly not aligned. For files opened
408 * with O_DIRECT, necessary alignments are ensured before calling
409 * raw_pwrite_aligned to do the actual write.
411 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
412 const uint8_t *buf
, int count
)
414 BDRVRawState
*s
= bs
->opaque
;
415 int size
, ret
, shift
, sum
;
419 if (s
->aligned_buf
!= NULL
) {
421 if (offset
& 0x1ff) {
422 /* align offset on a 512 bytes boundary */
423 shift
= offset
& 0x1ff;
424 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
431 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
433 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
445 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
447 while ((size
= (count
& ~0x1ff)) != 0) {
449 if (size
> ALIGNED_BUFFER_SIZE
)
450 size
= ALIGNED_BUFFER_SIZE
;
452 memcpy(s
->aligned_buf
, buf
, size
);
454 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
463 /* here, count < 512 because (count & ~0x1ff) == 0 */
465 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
468 memcpy(s
->aligned_buf
, buf
, count
);
470 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
481 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
484 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
485 const uint8_t *buf
, int nb_sectors
)
488 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
489 if (ret
== (nb_sectors
* 512))
495 /***********************************************************/
496 /* Unix AIO using POSIX AIO */
498 typedef struct RawAIOCB
{
499 BlockDriverAIOCB common
;
500 struct qemu_paiocb aiocb
;
501 struct RawAIOCB
*next
;
505 typedef struct PosixAioState
511 static void posix_aio_read(void *opaque
)
513 PosixAioState
*s
= opaque
;
514 RawAIOCB
*acb
, **pacb
;
518 struct qemu_signalfd_siginfo siginfo
;
522 /* try to read from signalfd, don't freak out if we can't read anything */
524 while (offset
< 128) {
527 len
= read(s
->fd
, sig
.buf
+ offset
, 128 - offset
);
528 if (len
== -1 && errno
== EINTR
)
530 if (len
== -1 && errno
== EAGAIN
) {
531 /* there is no natural reason for this to happen,
532 * so we'll spin hard until we get everything just
533 * to be on the safe side. */
542 pacb
= &s
->first_aio
;
547 ret
= qemu_paio_error(&acb
->aiocb
);
548 if (ret
== ECANCELED
) {
549 /* remove the request */
551 qemu_aio_release(acb
);
552 } else if (ret
!= EINPROGRESS
) {
555 ret
= qemu_paio_return(&acb
->aiocb
);
556 if (ret
== acb
->aiocb
.aio_nbytes
)
563 /* remove the request */
565 /* call the callback */
566 acb
->common
.cb(acb
->common
.opaque
, ret
);
567 qemu_aio_release(acb
);
577 static int posix_aio_flush(void *opaque
)
579 PosixAioState
*s
= opaque
;
580 return !!s
->first_aio
;
583 static PosixAioState
*posix_aio_state
;
585 static int posix_aio_init(void)
589 struct qemu_paioinit ai
;
594 s
= qemu_malloc(sizeof(PosixAioState
));
596 /* Make sure to block AIO signal */
598 sigaddset(&mask
, SIGUSR2
);
599 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
602 s
->fd
= qemu_signalfd(&mask
);
604 fprintf(stderr
, "failed to create signalfd\n");
608 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
610 qemu_aio_set_fd_handler(s
->fd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
612 memset(&ai
, 0, sizeof(ai
));
622 static void raw_aio_remove(RawAIOCB
*acb
)
626 /* remove the callback from the queue */
627 pacb
= &posix_aio_state
->first_aio
;
630 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
632 } else if (*pacb
== acb
) {
634 qemu_aio_release(acb
);
637 pacb
= &(*pacb
)->next
;
641 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
644 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
646 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
647 if (ret
== QEMU_PAIO_NOTCANCELED
) {
648 /* fail safe: if the aio could not be canceled, we wait for
650 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
656 static AIOPool raw_aio_pool
= {
657 .aiocb_size
= sizeof(RawAIOCB
),
658 .cancel
= raw_aio_cancel
,
661 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
662 QEMUIOVector
*qiov
, int nb_sectors
,
663 BlockDriverCompletionFunc
*cb
, void *opaque
)
665 BDRVRawState
*s
= bs
->opaque
;
671 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
674 acb
->aiocb
.aio_fildes
= s
->fd
;
675 acb
->aiocb
.ev_signo
= SIGUSR2
;
676 acb
->aiocb
.aio_iov
= qiov
->iov
;
677 acb
->aiocb
.aio_niov
= qiov
->niov
;
678 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
679 acb
->aiocb
.aio_offset
= sector_num
* 512;
680 acb
->aiocb
.aio_flags
= 0;
683 * If O_DIRECT is used the buffer needs to be aligned on a sector
684 * boundary. Tell the low level code to ensure that in case it's
688 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
690 acb
->next
= posix_aio_state
->first_aio
;
691 posix_aio_state
->first_aio
= acb
;
695 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
696 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
697 BlockDriverCompletionFunc
*cb
, void *opaque
)
701 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
704 if (qemu_paio_read(&acb
->aiocb
) < 0) {
711 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
712 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
713 BlockDriverCompletionFunc
*cb
, void *opaque
)
717 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
720 if (qemu_paio_write(&acb
->aiocb
) < 0) {
726 #else /* CONFIG_AIO */
727 static int posix_aio_init(void)
731 #endif /* CONFIG_AIO */
734 static void raw_close(BlockDriverState
*bs
)
736 BDRVRawState
*s
= bs
->opaque
;
740 if (s
->aligned_buf
!= NULL
)
741 qemu_free(s
->aligned_buf
);
745 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
747 BDRVRawState
*s
= bs
->opaque
;
748 if (s
->type
!= FTYPE_FILE
)
750 if (ftruncate(s
->fd
, offset
) < 0)
756 static int64_t raw_getlength(BlockDriverState
*bs
)
758 BDRVRawState
*s
= bs
->opaque
;
764 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
767 if (ioctl(fd
, DIOCGDINFO
, &dl
))
769 return (uint64_t)dl
.d_secsize
*
770 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
774 #else /* !__OpenBSD__ */
775 static int64_t raw_getlength(BlockDriverState
*bs
)
777 BDRVRawState
*s
= bs
->opaque
;
787 struct dk_minfo minfo
;
800 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
801 #ifdef DIOCGMEDIASIZE
802 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
803 #elif defined(DIOCGPART)
806 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
807 size
= pi
.media_size
;
814 size
= LONG_LONG_MAX
;
816 size
= lseek(fd
, 0LL, SEEK_END
);
821 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
822 if (size
== 2048LL * (unsigned)-1)
824 /* XXX no disc? maybe we need to reopen... */
825 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
835 * use the DKIOCGMEDIAINFO ioctl to read the size.
837 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
839 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
840 } else /* there are reports that lseek on some devices
841 fails, but irc discussion said that contingency
842 on contingency was overkill */
845 size
= lseek(fd
, 0, SEEK_END
);
851 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
855 int64_t total_size
= 0;
857 /* Read out options */
858 while (options
&& options
->name
) {
859 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
860 total_size
= options
->value
.n
/ 512;
865 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
870 if (ftruncate(fd
, total_size
* 512) != 0) {
873 if (close(fd
) != 0) {
880 static void raw_flush(BlockDriverState
*bs
)
882 BDRVRawState
*s
= bs
->opaque
;
887 static QEMUOptionParameter raw_create_options
[] = {
889 .name
= BLOCK_OPT_SIZE
,
891 .help
= "Virtual disk size"
896 static BlockDriver bdrv_raw
= {
897 .format_name
= "raw",
898 .instance_size
= sizeof(BDRVRawState
),
899 .bdrv_probe
= NULL
, /* no probe for protocols */
900 .bdrv_open
= raw_open
,
901 .bdrv_read
= raw_read
,
902 .bdrv_write
= raw_write
,
903 .bdrv_close
= raw_close
,
904 .bdrv_create
= raw_create
,
905 .bdrv_flush
= raw_flush
,
908 .bdrv_aio_readv
= raw_aio_readv
,
909 .bdrv_aio_writev
= raw_aio_writev
,
912 .bdrv_truncate
= raw_truncate
,
913 .bdrv_getlength
= raw_getlength
,
915 .create_options
= raw_create_options
,
918 /***********************************************/
922 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
923 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
925 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
927 kern_return_t kernResult
;
928 mach_port_t masterPort
;
929 CFMutableDictionaryRef classesToMatch
;
931 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
932 if ( KERN_SUCCESS
!= kernResult
) {
933 printf( "IOMasterPort returned %d\n", kernResult
);
936 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
937 if ( classesToMatch
== NULL
) {
938 printf( "IOServiceMatching returned a NULL dictionary.\n" );
940 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
942 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
943 if ( KERN_SUCCESS
!= kernResult
)
945 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
951 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
953 io_object_t nextMedia
;
954 kern_return_t kernResult
= KERN_FAILURE
;
956 nextMedia
= IOIteratorNext( mediaIterator
);
959 CFTypeRef bsdPathAsCFString
;
960 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
961 if ( bsdPathAsCFString
) {
962 size_t devPathLength
;
963 strcpy( bsdPath
, _PATH_DEV
);
964 strcat( bsdPath
, "r" );
965 devPathLength
= strlen( bsdPath
);
966 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
967 kernResult
= KERN_SUCCESS
;
969 CFRelease( bsdPathAsCFString
);
971 IOObjectRelease( nextMedia
);
979 static int hdev_probe_device(const char *filename
)
983 /* allow a dedicated CD-ROM driver to match with a higher priority */
984 if (strstart(filename
, "/dev/cdrom", NULL
))
987 if (stat(filename
, &st
) >= 0 &&
988 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
995 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
997 BDRVRawState
*s
= bs
->opaque
;
1000 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1001 kern_return_t kernResult
;
1002 io_iterator_t mediaIterator
;
1003 char bsdPath
[ MAXPATHLEN
];
1006 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1007 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1009 if ( bsdPath
[ 0 ] != '\0' ) {
1010 strcat(bsdPath
,"s0");
1011 /* some CDs don't have a partition 0 */
1012 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1014 bsdPath
[strlen(bsdPath
)-1] = '1';
1021 if ( mediaIterator
)
1022 IOObjectRelease( mediaIterator
);
1026 s
->type
= FTYPE_FILE
;
1027 #if defined(__linux__) && defined(CONFIG_AIO)
1028 if (strstart(filename
, "/dev/sg", NULL
)) {
1033 return raw_open_common(bs
, filename
, flags
, 0);
1036 #if defined(__linux__)
1037 /* Note: we do not have a reliable method to detect if the floppy is
1038 present. The current method is to try to open the floppy at every
1039 I/O and to keep it opened during a few hundreds of ms. */
1040 static int fd_open(BlockDriverState
*bs
)
1042 BDRVRawState
*s
= bs
->opaque
;
1043 int last_media_present
;
1045 if (s
->type
!= FTYPE_FD
)
1047 last_media_present
= (s
->fd
>= 0);
1049 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1053 printf("Floppy closed\n");
1057 if (s
->fd_got_error
&&
1058 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1060 printf("No floppy (open delayed)\n");
1064 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1066 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1067 s
->fd_got_error
= 1;
1068 if (last_media_present
)
1069 s
->fd_media_changed
= 1;
1071 printf("No floppy\n");
1076 printf("Floppy opened\n");
1079 if (!last_media_present
)
1080 s
->fd_media_changed
= 1;
1081 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1082 s
->fd_got_error
= 0;
1086 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1088 BDRVRawState
*s
= bs
->opaque
;
1090 return ioctl(s
->fd
, req
, buf
);
1094 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1095 unsigned long int req
, void *buf
,
1096 BlockDriverCompletionFunc
*cb
, void *opaque
)
1098 BDRVRawState
*s
= bs
->opaque
;
1101 if (fd_open(bs
) < 0)
1104 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1107 acb
->aiocb
.aio_fildes
= s
->fd
;
1108 acb
->aiocb
.ev_signo
= SIGUSR2
;
1109 acb
->aiocb
.aio_offset
= 0;
1110 acb
->aiocb
.aio_flags
= 0;
1112 acb
->next
= posix_aio_state
->first_aio
;
1113 posix_aio_state
->first_aio
= acb
;
1115 acb
->aiocb
.aio_ioctl_buf
= buf
;
1116 acb
->aiocb
.aio_ioctl_cmd
= req
;
1117 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1118 raw_aio_remove(acb
);
1122 return &acb
->common
;
1126 #elif defined(__FreeBSD__)
1127 static int fd_open(BlockDriverState
*bs
)
1129 BDRVRawState
*s
= bs
->opaque
;
1131 /* this is just to ensure s->fd is sane (its called by io ops) */
1136 #else /* !linux && !FreeBSD */
1138 static int fd_open(BlockDriverState
*bs
)
1143 #endif /* !linux && !FreeBSD */
1145 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1149 struct stat stat_buf
;
1150 int64_t total_size
= 0;
1152 /* Read out options */
1153 while (options
&& options
->name
) {
1154 if (!strcmp(options
->name
, "size")) {
1155 total_size
= options
->value
.n
/ 512;
1160 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1164 if (fstat(fd
, &stat_buf
) < 0)
1166 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1168 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1175 static BlockDriver bdrv_host_device
= {
1176 .format_name
= "host_device",
1177 .instance_size
= sizeof(BDRVRawState
),
1178 .bdrv_probe_device
= hdev_probe_device
,
1179 .bdrv_open
= hdev_open
,
1180 .bdrv_close
= raw_close
,
1181 .bdrv_create
= hdev_create
,
1182 .bdrv_flush
= raw_flush
,
1185 .bdrv_aio_readv
= raw_aio_readv
,
1186 .bdrv_aio_writev
= raw_aio_writev
,
1189 .bdrv_read
= raw_read
,
1190 .bdrv_write
= raw_write
,
1191 .bdrv_getlength
= raw_getlength
,
1193 /* generic scsi device */
1195 .bdrv_ioctl
= hdev_ioctl
,
1197 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1203 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1205 BDRVRawState
*s
= bs
->opaque
;
1212 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1213 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1217 /* close fd so that we can reopen it as needed */
1220 s
->fd_media_changed
= 1;
1225 static int floppy_probe_device(const char *filename
)
1227 if (strstart(filename
, "/dev/fd", NULL
))
1233 static int floppy_is_inserted(BlockDriverState
*bs
)
1235 return fd_open(bs
) >= 0;
1238 static int floppy_media_changed(BlockDriverState
*bs
)
1240 BDRVRawState
*s
= bs
->opaque
;
1244 * XXX: we do not have a true media changed indication.
1245 * It does not work if the floppy is changed without trying to read it.
1248 ret
= s
->fd_media_changed
;
1249 s
->fd_media_changed
= 0;
1251 printf("Floppy changed=%d\n", ret
);
1256 static int floppy_eject(BlockDriverState
*bs
, int eject_flag
)
1258 BDRVRawState
*s
= bs
->opaque
;
1265 fd
= open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1267 if (ioctl(fd
, FDEJECT
, 0) < 0)
1275 static BlockDriver bdrv_host_floppy
= {
1276 .format_name
= "host_floppy",
1277 .instance_size
= sizeof(BDRVRawState
),
1278 .bdrv_probe_device
= floppy_probe_device
,
1279 .bdrv_open
= floppy_open
,
1280 .bdrv_close
= raw_close
,
1281 .bdrv_create
= hdev_create
,
1282 .bdrv_flush
= raw_flush
,
1285 .bdrv_aio_readv
= raw_aio_readv
,
1286 .bdrv_aio_writev
= raw_aio_writev
,
1289 .bdrv_read
= raw_read
,
1290 .bdrv_write
= raw_write
,
1291 .bdrv_getlength
= raw_getlength
,
1293 /* removable device support */
1294 .bdrv_is_inserted
= floppy_is_inserted
,
1295 .bdrv_media_changed
= floppy_media_changed
,
1296 .bdrv_eject
= floppy_eject
,
1299 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1301 BDRVRawState
*s
= bs
->opaque
;
1305 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1306 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1309 static int cdrom_probe_device(const char *filename
)
1311 if (strstart(filename
, "/dev/cd", NULL
))
1316 static int cdrom_is_inserted(BlockDriverState
*bs
)
1318 BDRVRawState
*s
= bs
->opaque
;
1321 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1322 if (ret
== CDS_DISC_OK
)
1327 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1329 BDRVRawState
*s
= bs
->opaque
;
1332 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1333 perror("CDROMEJECT");
1335 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1336 perror("CDROMEJECT");
1342 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1344 BDRVRawState
*s
= bs
->opaque
;
1346 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1348 * Note: an error can happen if the distribution automatically
1351 /* perror("CDROM_LOCKDOOR"); */
1357 static BlockDriver bdrv_host_cdrom
= {
1358 .format_name
= "host_cdrom",
1359 .instance_size
= sizeof(BDRVRawState
),
1360 .bdrv_probe_device
= cdrom_probe_device
,
1361 .bdrv_open
= cdrom_open
,
1362 .bdrv_close
= raw_close
,
1363 .bdrv_create
= hdev_create
,
1364 .bdrv_flush
= raw_flush
,
1367 .bdrv_aio_readv
= raw_aio_readv
,
1368 .bdrv_aio_writev
= raw_aio_writev
,
1371 .bdrv_read
= raw_read
,
1372 .bdrv_write
= raw_write
,
1373 .bdrv_getlength
= raw_getlength
,
1375 /* removable device support */
1376 .bdrv_is_inserted
= cdrom_is_inserted
,
1377 .bdrv_eject
= cdrom_eject
,
1378 .bdrv_set_locked
= cdrom_set_locked
,
1380 /* generic scsi device */
1381 .bdrv_ioctl
= hdev_ioctl
,
1383 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1386 #endif /* __linux__ */
1389 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1391 BDRVRawState
*s
= bs
->opaque
;
1396 ret
= raw_open_common(bs
, filename
, flags
, 0);
1400 /* make sure the door isnt locked at this time */
1401 ioctl(s
->fd
, CDIOCALLOW
);
1405 static int cdrom_probe_device(const char *filename
)
1407 if (strstart(filename
, "/dev/cd", NULL
) ||
1408 strstart(filename
, "/dev/acd", NULL
))
1413 static int cdrom_reopen(BlockDriverState
*bs
)
1415 BDRVRawState
*s
= bs
->opaque
;
1419 * Force reread of possibly changed/newly loaded disc,
1420 * FreeBSD seems to not notice sometimes...
1424 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1431 /* make sure the door isnt locked at this time */
1432 ioctl(s
->fd
, CDIOCALLOW
);
1436 static int cdrom_is_inserted(BlockDriverState
*bs
)
1438 return raw_getlength(bs
) > 0;
1441 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1443 BDRVRawState
*s
= bs
->opaque
;
1448 (void) ioctl(s
->fd
, CDIOCALLOW
);
1451 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1452 perror("CDIOCEJECT");
1454 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1455 perror("CDIOCCLOSE");
1458 if (cdrom_reopen(bs
) < 0)
1463 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1465 BDRVRawState
*s
= bs
->opaque
;
1469 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1471 * Note: an error can happen if the distribution automatically
1474 /* perror("CDROM_LOCKDOOR"); */
1480 static BlockDriver bdrv_host_cdrom
= {
1481 .format_name
= "host_cdrom",
1482 .instance_size
= sizeof(BDRVRawState
),
1483 .bdrv_probe_device
= cdrom_probe_device
,
1484 .bdrv_open
= cdrom_open
,
1485 .bdrv_close
= raw_close
,
1486 .bdrv_create
= hdev_create
,
1487 .bdrv_flush
= raw_flush
,
1490 .bdrv_aio_readv
= raw_aio_readv
,
1491 .bdrv_aio_writev
= raw_aio_writev
,
1494 .bdrv_read
= raw_read
,
1495 .bdrv_write
= raw_write
,
1496 .bdrv_getlength
= raw_getlength
,
1498 /* removable device support */
1499 .bdrv_is_inserted
= cdrom_is_inserted
,
1500 .bdrv_eject
= cdrom_eject
,
1501 .bdrv_set_locked
= cdrom_set_locked
,
1503 #endif /* __FreeBSD__ */
1505 static void bdrv_raw_init(void)
1508 * Register all the drivers. Note that order is important, the driver
1509 * registered last will get probed first.
1511 bdrv_register(&bdrv_raw
);
1512 bdrv_register(&bdrv_host_device
);
1514 bdrv_register(&bdrv_host_floppy
);
1515 bdrv_register(&bdrv_host_cdrom
);
1518 bdrv_register(&bdrv_host_cdrom
);
1522 block_init(bdrv_raw_init
);