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
,
126 int bdrv_flags
, int open_flags
)
128 BDRVRawState
*s
= bs
->opaque
;
133 s
->lseek_err_cnt
= 0;
135 s
->open_flags
= open_flags
| O_BINARY
;
136 s
->open_flags
&= ~O_ACCMODE
;
137 if ((bdrv_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 ((bdrv_flags
& BDRV_O_NOCACHE
))
147 s
->open_flags
|= O_DIRECT
;
148 else if (!(bdrv_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 ((bdrv_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
;
177 s
->type
= FTYPE_FILE
;
178 if (flags
& BDRV_O_CREAT
)
179 open_flags
= O_CREAT
| O_TRUNC
;
181 return raw_open_common(bs
, filename
, flags
, open_flags
);
184 /* XXX: use host sector size if necessary with:
185 #ifdef DIOCGSECTORSIZE
187 unsigned int sectorsize = 512;
188 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
189 sectorsize > bufsize)
190 bufsize = sectorsize;
194 u_int32_t blockSize = 512;
195 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
202 * offset and count are in bytes, but must be multiples of 512 for files
203 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
205 * This function may be called without alignment if the caller ensures
206 * that O_DIRECT is not in effect.
208 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
209 uint8_t *buf
, int count
)
211 BDRVRawState
*s
= bs
->opaque
;
218 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
219 ++(s
->lseek_err_cnt
);
220 if(s
->lseek_err_cnt
<= 10) {
221 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
222 "] lseek failed : %d = %s\n",
223 s
->fd
, bs
->filename
, offset
, buf
, count
,
224 bs
->total_sectors
, errno
, strerror(errno
));
230 ret
= read(s
->fd
, buf
, count
);
232 goto label__raw_read__success
;
234 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
235 "] read failed %d : %d = %s\n",
236 s
->fd
, bs
->filename
, offset
, buf
, count
,
237 bs
->total_sectors
, ret
, errno
, strerror(errno
));
239 /* Try harder for CDrom. */
240 if (bs
->type
== BDRV_TYPE_CDROM
) {
241 lseek(s
->fd
, offset
, SEEK_SET
);
242 ret
= read(s
->fd
, buf
, count
);
244 goto label__raw_read__success
;
245 lseek(s
->fd
, offset
, SEEK_SET
);
246 ret
= read(s
->fd
, buf
, count
);
248 goto label__raw_read__success
;
250 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
251 "] retry read failed %d : %d = %s\n",
252 s
->fd
, bs
->filename
, offset
, buf
, count
,
253 bs
->total_sectors
, ret
, errno
, strerror(errno
));
256 label__raw_read__success
:
258 return (ret
< 0) ? -errno
: ret
;
262 * offset and count are in bytes, but must be multiples of 512 for files
263 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
265 * This function may be called without alignment if the caller ensures
266 * that O_DIRECT is not in effect.
268 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
269 const uint8_t *buf
, int count
)
271 BDRVRawState
*s
= bs
->opaque
;
278 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
279 ++(s
->lseek_err_cnt
);
280 if(s
->lseek_err_cnt
) {
281 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
282 PRId64
"] lseek failed : %d = %s\n",
283 s
->fd
, bs
->filename
, offset
, buf
, count
,
284 bs
->total_sectors
, errno
, strerror(errno
));
288 s
->lseek_err_cnt
= 0;
290 ret
= write(s
->fd
, buf
, count
);
292 goto label__raw_write__success
;
294 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
295 "] write failed %d : %d = %s\n",
296 s
->fd
, bs
->filename
, offset
, buf
, count
,
297 bs
->total_sectors
, ret
, errno
, strerror(errno
));
299 label__raw_write__success
:
301 return (ret
< 0) ? -errno
: ret
;
306 * offset and count are in bytes and possibly not aligned. For files opened
307 * with O_DIRECT, necessary alignments are ensured before calling
308 * raw_pread_aligned to do the actual read.
310 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
311 uint8_t *buf
, int count
)
313 BDRVRawState
*s
= bs
->opaque
;
314 int size
, ret
, shift
, sum
;
318 if (s
->aligned_buf
!= NULL
) {
320 if (offset
& 0x1ff) {
321 /* align offset on a 512 bytes boundary */
323 shift
= offset
& 0x1ff;
324 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
325 if (size
> ALIGNED_BUFFER_SIZE
)
326 size
= ALIGNED_BUFFER_SIZE
;
327 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
334 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
344 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
346 /* read on aligned buffer */
350 size
= (count
+ 0x1ff) & ~0x1ff;
351 if (size
> ALIGNED_BUFFER_SIZE
)
352 size
= ALIGNED_BUFFER_SIZE
;
354 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
362 memcpy(buf
, s
->aligned_buf
, size
);
374 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
377 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
378 uint8_t *buf
, int nb_sectors
)
382 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
383 if (ret
== (nb_sectors
* 512))
389 * offset and count are in bytes and possibly not aligned. For files opened
390 * with O_DIRECT, necessary alignments are ensured before calling
391 * raw_pwrite_aligned to do the actual write.
393 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
394 const uint8_t *buf
, int count
)
396 BDRVRawState
*s
= bs
->opaque
;
397 int size
, ret
, shift
, sum
;
401 if (s
->aligned_buf
!= NULL
) {
403 if (offset
& 0x1ff) {
404 /* align offset on a 512 bytes boundary */
405 shift
= offset
& 0x1ff;
406 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
413 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
415 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
427 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
429 while ((size
= (count
& ~0x1ff)) != 0) {
431 if (size
> ALIGNED_BUFFER_SIZE
)
432 size
= ALIGNED_BUFFER_SIZE
;
434 memcpy(s
->aligned_buf
, buf
, size
);
436 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
445 /* here, count < 512 because (count & ~0x1ff) == 0 */
447 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
450 memcpy(s
->aligned_buf
, buf
, count
);
452 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
463 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
466 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
467 const uint8_t *buf
, int nb_sectors
)
470 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
471 if (ret
== (nb_sectors
* 512))
477 /***********************************************************/
478 /* Unix AIO using POSIX AIO */
480 typedef struct RawAIOCB
{
481 BlockDriverAIOCB common
;
482 struct qemu_paiocb aiocb
;
483 struct RawAIOCB
*next
;
487 typedef struct PosixAioState
493 static void posix_aio_read(void *opaque
)
495 PosixAioState
*s
= opaque
;
496 RawAIOCB
*acb
, **pacb
;
500 /* read all bytes from signal pipe */
504 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
505 if (len
== -1 && errno
== EINTR
)
506 continue; /* try again */
507 if (len
== sizeof(bytes
))
508 continue; /* more to read */
513 pacb
= &s
->first_aio
;
518 ret
= qemu_paio_error(&acb
->aiocb
);
519 if (ret
== ECANCELED
) {
520 /* remove the request */
522 qemu_aio_release(acb
);
523 } else if (ret
!= EINPROGRESS
) {
526 ret
= qemu_paio_return(&acb
->aiocb
);
527 if (ret
== acb
->aiocb
.aio_nbytes
)
534 /* remove the request */
536 /* call the callback */
537 acb
->common
.cb(acb
->common
.opaque
, ret
);
538 qemu_aio_release(acb
);
548 static int posix_aio_flush(void *opaque
)
550 PosixAioState
*s
= opaque
;
551 return !!s
->first_aio
;
554 static PosixAioState
*posix_aio_state
;
556 static void aio_signal_handler(int signum
)
558 if (posix_aio_state
) {
561 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
567 static int posix_aio_init(void)
569 struct sigaction act
;
572 struct qemu_paioinit ai
;
577 s
= qemu_malloc(sizeof(PosixAioState
));
579 sigfillset(&act
.sa_mask
);
580 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
581 act
.sa_handler
= aio_signal_handler
;
582 sigaction(SIGUSR2
, &act
, NULL
);
585 if (pipe(fds
) == -1) {
586 fprintf(stderr
, "failed to create pipe\n");
593 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
594 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
596 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
598 memset(&ai
, 0, sizeof(ai
));
608 static void raw_aio_remove(RawAIOCB
*acb
)
612 /* remove the callback from the queue */
613 pacb
= &posix_aio_state
->first_aio
;
616 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
618 } else if (*pacb
== acb
) {
620 qemu_aio_release(acb
);
623 pacb
= &(*pacb
)->next
;
627 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
630 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
632 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
633 if (ret
== QEMU_PAIO_NOTCANCELED
) {
634 /* fail safe: if the aio could not be canceled, we wait for
636 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
642 static AIOPool raw_aio_pool
= {
643 .aiocb_size
= sizeof(RawAIOCB
),
644 .cancel
= raw_aio_cancel
,
647 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
648 QEMUIOVector
*qiov
, int nb_sectors
,
649 BlockDriverCompletionFunc
*cb
, void *opaque
)
651 BDRVRawState
*s
= bs
->opaque
;
657 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
660 acb
->aiocb
.aio_fildes
= s
->fd
;
661 acb
->aiocb
.ev_signo
= SIGUSR2
;
662 acb
->aiocb
.aio_iov
= qiov
->iov
;
663 acb
->aiocb
.aio_niov
= qiov
->niov
;
664 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
665 acb
->aiocb
.aio_offset
= sector_num
* 512;
666 acb
->aiocb
.aio_flags
= 0;
669 * If O_DIRECT is used the buffer needs to be aligned on a sector
670 * boundary. Tell the low level code to ensure that in case it's
674 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
676 acb
->next
= posix_aio_state
->first_aio
;
677 posix_aio_state
->first_aio
= acb
;
681 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
682 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
683 BlockDriverCompletionFunc
*cb
, void *opaque
)
687 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
690 if (qemu_paio_read(&acb
->aiocb
) < 0) {
697 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
698 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
699 BlockDriverCompletionFunc
*cb
, void *opaque
)
703 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
706 if (qemu_paio_write(&acb
->aiocb
) < 0) {
712 #else /* CONFIG_AIO */
713 static int posix_aio_init(void)
717 #endif /* CONFIG_AIO */
720 static void raw_close(BlockDriverState
*bs
)
722 BDRVRawState
*s
= bs
->opaque
;
726 if (s
->aligned_buf
!= NULL
)
727 qemu_free(s
->aligned_buf
);
731 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
733 BDRVRawState
*s
= bs
->opaque
;
734 if (s
->type
!= FTYPE_FILE
)
736 if (ftruncate(s
->fd
, offset
) < 0)
742 static int64_t raw_getlength(BlockDriverState
*bs
)
744 BDRVRawState
*s
= bs
->opaque
;
750 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
753 if (ioctl(fd
, DIOCGDINFO
, &dl
))
755 return (uint64_t)dl
.d_secsize
*
756 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
760 #else /* !__OpenBSD__ */
761 static int64_t raw_getlength(BlockDriverState
*bs
)
763 BDRVRawState
*s
= bs
->opaque
;
773 struct dk_minfo minfo
;
786 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
787 #ifdef DIOCGMEDIASIZE
788 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
789 #elif defined(DIOCGPART)
792 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
793 size
= pi
.media_size
;
800 size
= LONG_LONG_MAX
;
802 size
= lseek(fd
, 0LL, SEEK_END
);
807 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
808 if (size
== 2048LL * (unsigned)-1)
810 /* XXX no disc? maybe we need to reopen... */
811 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
821 * use the DKIOCGMEDIAINFO ioctl to read the size.
823 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
825 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
826 } else /* there are reports that lseek on some devices
827 fails, but irc discussion said that contingency
828 on contingency was overkill */
831 size
= lseek(fd
, 0, SEEK_END
);
837 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
840 int64_t total_size
= 0;
842 /* Read out options */
843 while (options
&& options
->name
) {
844 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
845 total_size
= options
->value
.n
/ 512;
850 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
854 ftruncate(fd
, total_size
* 512);
859 static void raw_flush(BlockDriverState
*bs
)
861 BDRVRawState
*s
= bs
->opaque
;
866 static QEMUOptionParameter raw_create_options
[] = {
868 .name
= BLOCK_OPT_SIZE
,
870 .help
= "Virtual disk size"
875 static BlockDriver bdrv_raw
= {
876 .format_name
= "raw",
877 .instance_size
= sizeof(BDRVRawState
),
878 .bdrv_probe
= NULL
, /* no probe for protocols */
879 .bdrv_open
= raw_open
,
880 .bdrv_read
= raw_read
,
881 .bdrv_write
= raw_write
,
882 .bdrv_close
= raw_close
,
883 .bdrv_create
= raw_create
,
884 .bdrv_flush
= raw_flush
,
887 .bdrv_aio_readv
= raw_aio_readv
,
888 .bdrv_aio_writev
= raw_aio_writev
,
891 .bdrv_truncate
= raw_truncate
,
892 .bdrv_getlength
= raw_getlength
,
894 .create_options
= raw_create_options
,
897 /***********************************************/
901 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
902 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
904 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
906 kern_return_t kernResult
;
907 mach_port_t masterPort
;
908 CFMutableDictionaryRef classesToMatch
;
910 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
911 if ( KERN_SUCCESS
!= kernResult
) {
912 printf( "IOMasterPort returned %d\n", kernResult
);
915 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
916 if ( classesToMatch
== NULL
) {
917 printf( "IOServiceMatching returned a NULL dictionary.\n" );
919 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
921 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
922 if ( KERN_SUCCESS
!= kernResult
)
924 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
930 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
932 io_object_t nextMedia
;
933 kern_return_t kernResult
= KERN_FAILURE
;
935 nextMedia
= IOIteratorNext( mediaIterator
);
938 CFTypeRef bsdPathAsCFString
;
939 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
940 if ( bsdPathAsCFString
) {
941 size_t devPathLength
;
942 strcpy( bsdPath
, _PATH_DEV
);
943 strcat( bsdPath
, "r" );
944 devPathLength
= strlen( bsdPath
);
945 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
946 kernResult
= KERN_SUCCESS
;
948 CFRelease( bsdPathAsCFString
);
950 IOObjectRelease( nextMedia
);
958 static int hdev_probe_device(const char *filename
)
962 /* allow a dedicated CD-ROM driver to match with a higher priority */
963 if (strstart(filename
, "/dev/cdrom", NULL
))
966 if (stat(filename
, &st
) >= 0 &&
967 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
974 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
976 BDRVRawState
*s
= bs
->opaque
;
979 if (strstart(filename
, "/dev/cdrom", NULL
)) {
980 kern_return_t kernResult
;
981 io_iterator_t mediaIterator
;
982 char bsdPath
[ MAXPATHLEN
];
985 kernResult
= FindEjectableCDMedia( &mediaIterator
);
986 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
988 if ( bsdPath
[ 0 ] != '\0' ) {
989 strcat(bsdPath
,"s0");
990 /* some CDs don't have a partition 0 */
991 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
993 bsdPath
[strlen(bsdPath
)-1] = '1';
1000 if ( mediaIterator
)
1001 IOObjectRelease( mediaIterator
);
1005 s
->type
= FTYPE_FILE
;
1006 #if defined(__linux__) && defined(CONFIG_AIO)
1007 if (strstart(filename
, "/dev/sg", NULL
)) {
1012 return raw_open_common(bs
, filename
, flags
, 0);
1015 #if defined(__linux__)
1016 /* Note: we do not have a reliable method to detect if the floppy is
1017 present. The current method is to try to open the floppy at every
1018 I/O and to keep it opened during a few hundreds of ms. */
1019 static int fd_open(BlockDriverState
*bs
)
1021 BDRVRawState
*s
= bs
->opaque
;
1022 int last_media_present
;
1024 if (s
->type
!= FTYPE_FD
)
1026 last_media_present
= (s
->fd
>= 0);
1028 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1032 printf("Floppy closed\n");
1036 if (s
->fd_got_error
&&
1037 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1039 printf("No floppy (open delayed)\n");
1043 s
->fd
= open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1045 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1046 s
->fd_got_error
= 1;
1047 if (last_media_present
)
1048 s
->fd_media_changed
= 1;
1050 printf("No floppy\n");
1055 printf("Floppy opened\n");
1058 if (!last_media_present
)
1059 s
->fd_media_changed
= 1;
1060 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1061 s
->fd_got_error
= 0;
1065 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1067 BDRVRawState
*s
= bs
->opaque
;
1069 return ioctl(s
->fd
, req
, buf
);
1073 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1074 unsigned long int req
, void *buf
,
1075 BlockDriverCompletionFunc
*cb
, void *opaque
)
1077 BDRVRawState
*s
= bs
->opaque
;
1080 if (fd_open(bs
) < 0)
1083 acb
= qemu_aio_get(&raw_aio_pool
, bs
, cb
, opaque
);
1086 acb
->aiocb
.aio_fildes
= s
->fd
;
1087 acb
->aiocb
.ev_signo
= SIGUSR2
;
1088 acb
->aiocb
.aio_offset
= 0;
1089 acb
->aiocb
.aio_flags
= 0;
1091 acb
->next
= posix_aio_state
->first_aio
;
1092 posix_aio_state
->first_aio
= acb
;
1094 acb
->aiocb
.aio_ioctl_buf
= buf
;
1095 acb
->aiocb
.aio_ioctl_cmd
= req
;
1096 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1097 raw_aio_remove(acb
);
1101 return &acb
->common
;
1105 #elif defined(__FreeBSD__)
1106 static int fd_open(BlockDriverState
*bs
)
1108 BDRVRawState
*s
= bs
->opaque
;
1110 /* this is just to ensure s->fd is sane (its called by io ops) */
1115 #else /* !linux && !FreeBSD */
1117 static int fd_open(BlockDriverState
*bs
)
1122 #endif /* !linux && !FreeBSD */
1124 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1128 struct stat stat_buf
;
1129 int64_t total_size
= 0;
1131 /* Read out options */
1132 while (options
&& options
->name
) {
1133 if (!strcmp(options
->name
, "size")) {
1134 total_size
= options
->value
.n
/ 512;
1139 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1143 if (fstat(fd
, &stat_buf
) < 0)
1145 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1147 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1154 static BlockDriver bdrv_host_device
= {
1155 .format_name
= "host_device",
1156 .instance_size
= sizeof(BDRVRawState
),
1157 .bdrv_probe_device
= hdev_probe_device
,
1158 .bdrv_open
= hdev_open
,
1159 .bdrv_close
= raw_close
,
1160 .bdrv_create
= hdev_create
,
1161 .bdrv_flush
= raw_flush
,
1164 .bdrv_aio_readv
= raw_aio_readv
,
1165 .bdrv_aio_writev
= raw_aio_writev
,
1168 .bdrv_read
= raw_read
,
1169 .bdrv_write
= raw_write
,
1170 .bdrv_getlength
= raw_getlength
,
1172 /* generic scsi device */
1174 .bdrv_ioctl
= hdev_ioctl
,
1176 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1182 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1184 BDRVRawState
*s
= bs
->opaque
;
1191 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1192 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
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
;
1284 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1285 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1288 static int cdrom_probe_device(const char *filename
)
1290 if (strstart(filename
, "/dev/cd", NULL
))
1295 static int cdrom_is_inserted(BlockDriverState
*bs
)
1297 BDRVRawState
*s
= bs
->opaque
;
1300 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1301 if (ret
== CDS_DISC_OK
)
1306 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1308 BDRVRawState
*s
= bs
->opaque
;
1311 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1312 perror("CDROMEJECT");
1314 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1315 perror("CDROMEJECT");
1321 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1323 BDRVRawState
*s
= bs
->opaque
;
1325 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1327 * Note: an error can happen if the distribution automatically
1330 /* perror("CDROM_LOCKDOOR"); */
1336 static BlockDriver bdrv_host_cdrom
= {
1337 .format_name
= "host_cdrom",
1338 .instance_size
= sizeof(BDRVRawState
),
1339 .bdrv_probe_device
= cdrom_probe_device
,
1340 .bdrv_open
= cdrom_open
,
1341 .bdrv_close
= raw_close
,
1342 .bdrv_create
= hdev_create
,
1343 .bdrv_flush
= raw_flush
,
1346 .bdrv_aio_readv
= raw_aio_readv
,
1347 .bdrv_aio_writev
= raw_aio_writev
,
1350 .bdrv_read
= raw_read
,
1351 .bdrv_write
= raw_write
,
1352 .bdrv_getlength
= raw_getlength
,
1354 /* removable device support */
1355 .bdrv_is_inserted
= cdrom_is_inserted
,
1356 .bdrv_eject
= cdrom_eject
,
1357 .bdrv_set_locked
= cdrom_set_locked
,
1359 /* generic scsi device */
1360 .bdrv_ioctl
= hdev_ioctl
,
1362 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1365 #endif /* __linux__ */
1368 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1370 BDRVRawState
*s
= bs
->opaque
;
1375 ret
= raw_open_common(bs
, filename
, flags
, 0);
1379 /* make sure the door isnt locked at this time */
1380 ioctl(s
->fd
, CDIOCALLOW
);
1384 static int cdrom_probe_device(const char *filename
)
1386 if (strstart(filename
, "/dev/cd", NULL
) ||
1387 strstart(filename
, "/dev/acd", NULL
))
1392 static int cdrom_reopen(BlockDriverState
*bs
)
1394 BDRVRawState
*s
= bs
->opaque
;
1398 * Force reread of possibly changed/newly loaded disc,
1399 * FreeBSD seems to not notice sometimes...
1403 fd
= open(bs
->filename
, s
->open_flags
, 0644);
1410 /* make sure the door isnt locked at this time */
1411 ioctl(s
->fd
, CDIOCALLOW
);
1415 static int cdrom_is_inserted(BlockDriverState
*bs
)
1417 return raw_getlength(bs
) > 0;
1420 static int cdrom_eject(BlockDriverState
*bs
, int eject_flag
)
1422 BDRVRawState
*s
= bs
->opaque
;
1427 (void) ioctl(s
->fd
, CDIOCALLOW
);
1430 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1431 perror("CDIOCEJECT");
1433 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1434 perror("CDIOCCLOSE");
1437 if (cdrom_reopen(bs
) < 0)
1442 static int cdrom_set_locked(BlockDriverState
*bs
, int locked
)
1444 BDRVRawState
*s
= bs
->opaque
;
1448 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1450 * Note: an error can happen if the distribution automatically
1453 /* perror("CDROM_LOCKDOOR"); */
1459 static BlockDriver bdrv_host_cdrom
= {
1460 .format_name
= "host_cdrom",
1461 .instance_size
= sizeof(BDRVRawState
),
1462 .bdrv_probe_device
= cdrom_probe_device
,
1463 .bdrv_open
= cdrom_open
,
1464 .bdrv_close
= raw_close
,
1465 .bdrv_create
= hdev_create
,
1466 .bdrv_flush
= raw_flush
,
1469 .bdrv_aio_readv
= raw_aio_readv
,
1470 .bdrv_aio_writev
= raw_aio_writev
,
1473 .bdrv_read
= raw_read
,
1474 .bdrv_write
= raw_write
,
1475 .bdrv_getlength
= raw_getlength
,
1477 /* removable device support */
1478 .bdrv_is_inserted
= cdrom_is_inserted
,
1479 .bdrv_eject
= cdrom_eject
,
1480 .bdrv_set_locked
= cdrom_set_locked
,
1482 #endif /* __FreeBSD__ */
1484 static void bdrv_raw_init(void)
1487 * Register all the drivers. Note that order is important, the driver
1488 * registered last will get probed first.
1490 bdrv_register(&bdrv_raw
);
1491 bdrv_register(&bdrv_host_device
);
1493 bdrv_register(&bdrv_host_floppy
);
1494 bdrv_register(&bdrv_host_cdrom
);
1497 bdrv_register(&bdrv_host_cdrom
);
1501 block_init(bdrv_raw_init
);