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"
31 #include "posix-aio-compat.h"
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
47 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
63 #include <sys/ioctl.h>
64 #include <sys/disklabel.h>
69 #include <sys/ioctl.h>
70 #include <sys/diskslice.h>
73 //#define DEBUG_FLOPPY
76 #if defined(DEBUG_BLOCK)
77 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled()) \
78 { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0)
80 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
83 /* OS X does not have O_DSYNC */
85 #define O_DSYNC O_SYNC
88 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
90 #define O_DIRECT O_DSYNC
97 #define ALIGNED_BUFFER_SIZE (32 * 512)
99 /* if the FD is not accessed during that time (in ms), we try to
100 reopen it to see if the disk has been changed */
101 #define FD_OPEN_TIMEOUT 1000
103 typedef struct BDRVRawState
{
106 unsigned int lseek_err_cnt
;
107 #if defined(__linux__)
108 /* linux floppy specific */
110 int64_t fd_open_time
;
111 int64_t fd_error_time
;
113 int fd_media_changed
;
115 #if defined(__FreeBSD__)
118 uint8_t* aligned_buf
;
121 static int posix_aio_init(void);
123 static int fd_open(BlockDriverState
*bs
);
125 #if defined(__FreeBSD__)
126 static int cd_open(BlockDriverState
*bs
);
129 static int raw_is_inserted(BlockDriverState
*bs
);
131 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
133 BDRVRawState
*s
= bs
->opaque
;
134 int fd
, open_flags
, ret
;
138 s
->lseek_err_cnt
= 0;
140 open_flags
= O_BINARY
;
141 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
142 open_flags
|= O_RDWR
;
144 open_flags
|= O_RDONLY
;
147 if (flags
& BDRV_O_CREAT
)
148 open_flags
|= O_CREAT
| O_TRUNC
;
150 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
151 * and O_DIRECT for no caching. */
152 if ((flags
& BDRV_O_NOCACHE
))
153 open_flags
|= O_DIRECT
;
154 else if (!(flags
& BDRV_O_CACHE_WB
))
155 open_flags
|= O_DSYNC
;
157 s
->type
= FTYPE_FILE
;
159 fd
= open(filename
, open_flags
, 0644);
167 s
->aligned_buf
= NULL
;
168 if ((flags
& BDRV_O_NOCACHE
)) {
169 s
->aligned_buf
= qemu_memalign(512, ALIGNED_BUFFER_SIZE
);
170 if (s
->aligned_buf
== NULL
) {
179 /* XXX: use host sector size if necessary with:
180 #ifdef DIOCGSECTORSIZE
182 unsigned int sectorsize = 512;
183 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
184 sectorsize > bufsize)
185 bufsize = sectorsize;
189 u_int32_t blockSize = 512;
190 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
197 * offset and count are in bytes, but must be multiples of 512 for files
198 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
200 * This function may be called without alignment if the caller ensures
201 * that O_DIRECT is not in effect.
203 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
204 uint8_t *buf
, int count
)
206 BDRVRawState
*s
= bs
->opaque
;
213 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
214 ++(s
->lseek_err_cnt
);
215 if(s
->lseek_err_cnt
<= 10) {
216 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
217 "] lseek failed : %d = %s\n",
218 s
->fd
, bs
->filename
, offset
, buf
, count
,
219 bs
->total_sectors
, errno
, strerror(errno
));
225 ret
= read(s
->fd
, buf
, count
);
227 goto label__raw_read__success
;
229 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
230 "] read failed %d : %d = %s\n",
231 s
->fd
, bs
->filename
, offset
, buf
, count
,
232 bs
->total_sectors
, ret
, errno
, strerror(errno
));
234 /* Try harder for CDrom. */
235 if (bs
->type
== BDRV_TYPE_CDROM
) {
236 lseek(s
->fd
, offset
, SEEK_SET
);
237 ret
= read(s
->fd
, buf
, count
);
239 goto label__raw_read__success
;
240 lseek(s
->fd
, offset
, SEEK_SET
);
241 ret
= read(s
->fd
, buf
, count
);
243 goto label__raw_read__success
;
245 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
246 "] retry read failed %d : %d = %s\n",
247 s
->fd
, bs
->filename
, offset
, buf
, count
,
248 bs
->total_sectors
, ret
, errno
, strerror(errno
));
251 label__raw_read__success
:
257 * offset and count are in bytes, but must be multiples of 512 for files
258 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
260 * This function may be called without alignment if the caller ensures
261 * that O_DIRECT is not in effect.
263 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
264 const uint8_t *buf
, int count
)
266 BDRVRawState
*s
= bs
->opaque
;
273 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
274 ++(s
->lseek_err_cnt
);
275 if(s
->lseek_err_cnt
) {
276 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
277 PRId64
"] lseek failed : %d = %s\n",
278 s
->fd
, bs
->filename
, offset
, buf
, count
,
279 bs
->total_sectors
, errno
, strerror(errno
));
283 s
->lseek_err_cnt
= 0;
285 ret
= write(s
->fd
, buf
, count
);
287 goto label__raw_write__success
;
289 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
290 "] write failed %d : %d = %s\n",
291 s
->fd
, bs
->filename
, offset
, buf
, count
,
292 bs
->total_sectors
, ret
, errno
, strerror(errno
));
294 label__raw_write__success
:
296 return (ret
< 0) ? -errno
: ret
;
301 * offset and count are in bytes and possibly not aligned. For files opened
302 * with O_DIRECT, necessary alignments are ensured before calling
303 * raw_pread_aligned to do the actual read.
305 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
306 uint8_t *buf
, int count
)
308 BDRVRawState
*s
= bs
->opaque
;
309 int size
, ret
, shift
, sum
;
313 if (s
->aligned_buf
!= NULL
) {
315 if (offset
& 0x1ff) {
316 /* align offset on a 512 bytes boundary */
318 shift
= offset
& 0x1ff;
319 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
320 if (size
> ALIGNED_BUFFER_SIZE
)
321 size
= ALIGNED_BUFFER_SIZE
;
322 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
329 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
339 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
341 /* read on aligned buffer */
345 size
= (count
+ 0x1ff) & ~0x1ff;
346 if (size
> ALIGNED_BUFFER_SIZE
)
347 size
= ALIGNED_BUFFER_SIZE
;
349 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
357 memcpy(buf
, s
->aligned_buf
, size
);
369 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
372 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
373 uint8_t *buf
, int nb_sectors
)
377 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
378 if (ret
== (nb_sectors
* 512))
384 * offset and count are in bytes and possibly not aligned. For files opened
385 * with O_DIRECT, necessary alignments are ensured before calling
386 * raw_pwrite_aligned to do the actual write.
388 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
389 const uint8_t *buf
, int count
)
391 BDRVRawState
*s
= bs
->opaque
;
392 int size
, ret
, shift
, sum
;
396 if (s
->aligned_buf
!= NULL
) {
398 if (offset
& 0x1ff) {
399 /* align offset on a 512 bytes boundary */
400 shift
= offset
& 0x1ff;
401 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
408 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
410 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
422 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
424 while ((size
= (count
& ~0x1ff)) != 0) {
426 if (size
> ALIGNED_BUFFER_SIZE
)
427 size
= ALIGNED_BUFFER_SIZE
;
429 memcpy(s
->aligned_buf
, buf
, size
);
431 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
440 /* here, count < 512 because (count & ~0x1ff) == 0 */
442 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
445 memcpy(s
->aligned_buf
, buf
, count
);
447 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
458 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
461 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
462 const uint8_t *buf
, int nb_sectors
)
465 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
466 if (ret
== (nb_sectors
* 512))
472 /***********************************************************/
473 /* Unix AIO using POSIX AIO */
475 typedef struct RawAIOCB
{
476 BlockDriverAIOCB common
;
477 struct qemu_paiocb aiocb
;
478 struct RawAIOCB
*next
;
482 typedef struct PosixAioState
488 static void posix_aio_read(void *opaque
)
490 PosixAioState
*s
= opaque
;
491 RawAIOCB
*acb
, **pacb
;
495 struct qemu_signalfd_siginfo siginfo
;
499 /* try to read from signalfd, don't freak out if we can't read anything */
501 while (offset
< 128) {
504 len
= read(s
->fd
, sig
.buf
+ offset
, 128 - offset
);
505 if (len
== -1 && errno
== EINTR
)
507 if (len
== -1 && errno
== EAGAIN
) {
508 /* there is no natural reason for this to happen,
509 * so we'll spin hard until we get everything just
510 * to be on the safe side. */
519 pacb
= &s
->first_aio
;
524 ret
= qemu_paio_error(&acb
->aiocb
);
525 if (ret
== ECANCELED
) {
526 /* remove the request */
528 qemu_aio_release(acb
);
529 } else if (ret
!= EINPROGRESS
) {
532 ret
= qemu_paio_return(&acb
->aiocb
);
533 if (ret
== acb
->aiocb
.aio_nbytes
)
540 /* remove the request */
542 /* call the callback */
543 acb
->common
.cb(acb
->common
.opaque
, ret
);
544 qemu_aio_release(acb
);
554 static int posix_aio_flush(void *opaque
)
556 PosixAioState
*s
= opaque
;
557 return !!s
->first_aio
;
560 static PosixAioState
*posix_aio_state
;
562 static int posix_aio_init(void)
566 struct qemu_paioinit ai
;
571 s
= qemu_malloc(sizeof(PosixAioState
));
573 /* Make sure to block AIO signal */
575 sigaddset(&mask
, SIGUSR2
);
576 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
579 s
->fd
= qemu_signalfd(&mask
);
581 fprintf(stderr
, "failed to create signalfd\n");
585 fcntl(s
->fd
, F_SETFL
, O_NONBLOCK
);
587 qemu_aio_set_fd_handler(s
->fd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
589 memset(&ai
, 0, sizeof(ai
));
599 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
600 QEMUIOVector
*qiov
, int nb_sectors
,
601 BlockDriverCompletionFunc
*cb
, void *opaque
)
603 BDRVRawState
*s
= bs
->opaque
;
609 acb
= qemu_aio_get(bs
, cb
, opaque
);
612 acb
->aiocb
.aio_fildes
= s
->fd
;
613 acb
->aiocb
.ev_signo
= SIGUSR2
;
614 acb
->aiocb
.aio_iov
= qiov
->iov
;
615 acb
->aiocb
.aio_niov
= qiov
->niov
;
616 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
617 acb
->aiocb
.aio_offset
= sector_num
* 512;
618 acb
->aiocb
.aio_flags
= 0;
621 * If O_DIRECT is used the buffer needs to be aligned on a sector
622 * boundary. Tell the low level code to ensure that in case it's
626 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
628 acb
->next
= posix_aio_state
->first_aio
;
629 posix_aio_state
->first_aio
= acb
;
633 static void raw_aio_remove(RawAIOCB
*acb
)
637 /* remove the callback from the queue */
638 pacb
= &posix_aio_state
->first_aio
;
641 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
643 } else if (*pacb
== acb
) {
645 qemu_aio_release(acb
);
648 pacb
= &(*pacb
)->next
;
652 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
653 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
654 BlockDriverCompletionFunc
*cb
, void *opaque
)
658 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
661 if (qemu_paio_read(&acb
->aiocb
) < 0) {
668 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
669 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
670 BlockDriverCompletionFunc
*cb
, void *opaque
)
674 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
677 if (qemu_paio_write(&acb
->aiocb
) < 0) {
684 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
687 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
689 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
690 if (ret
== QEMU_PAIO_NOTCANCELED
) {
691 /* fail safe: if the aio could not be canceled, we wait for
693 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
698 #else /* CONFIG_AIO */
699 static int posix_aio_init(void)
703 #endif /* CONFIG_AIO */
706 static void raw_close(BlockDriverState
*bs
)
708 BDRVRawState
*s
= bs
->opaque
;
712 if (s
->aligned_buf
!= NULL
)
713 qemu_free(s
->aligned_buf
);
717 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
719 BDRVRawState
*s
= bs
->opaque
;
720 if (s
->type
!= FTYPE_FILE
)
722 if (ftruncate(s
->fd
, offset
) < 0)
728 static int64_t raw_getlength(BlockDriverState
*bs
)
730 BDRVRawState
*s
= bs
->opaque
;
736 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
739 if (ioctl(fd
, DIOCGDINFO
, &dl
))
741 return (uint64_t)dl
.d_secsize
*
742 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
746 #else /* !__OpenBSD__ */
747 static int64_t raw_getlength(BlockDriverState
*bs
)
749 BDRVRawState
*s
= bs
->opaque
;
759 struct dk_minfo minfo
;
772 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
773 #ifdef DIOCGMEDIASIZE
774 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
775 #elif defined(DIOCGPART)
778 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
779 size
= pi
.media_size
;
786 size
= LONG_LONG_MAX
;
788 size
= lseek(fd
, 0LL, SEEK_END
);
793 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
794 if (size
== 2048LL * (unsigned)-1)
796 /* XXX no disc? maybe we need to reopen... */
797 if (size
<= 0 && !reopened
&& cd_open(bs
) >= 0) {
807 * use the DKIOCGMEDIAINFO ioctl to read the size.
809 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
811 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
812 } else /* there are reports that lseek on some devices
813 fails, but irc discussion said that contingency
814 on contingency was overkill */
817 size
= lseek(fd
, 0, SEEK_END
);
823 static int raw_create(const char *filename
, int64_t total_size
,
824 const char *backing_file
, int flags
)
828 if (flags
|| backing_file
)
831 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
835 ftruncate(fd
, total_size
* 512);
840 static void raw_flush(BlockDriverState
*bs
)
842 BDRVRawState
*s
= bs
->opaque
;
846 BlockDriver bdrv_raw
= {
847 .format_name
= "raw",
848 .instance_size
= sizeof(BDRVRawState
),
849 .bdrv_probe
= NULL
, /* no probe for protocols */
850 .bdrv_open
= raw_open
,
851 .bdrv_read
= raw_read
,
852 .bdrv_write
= raw_write
,
853 .bdrv_close
= raw_close
,
854 .bdrv_create
= raw_create
,
855 .bdrv_flush
= raw_flush
,
858 .bdrv_aio_readv
= raw_aio_readv
,
859 .bdrv_aio_writev
= raw_aio_writev
,
860 .bdrv_aio_cancel
= raw_aio_cancel
,
861 .aiocb_size
= sizeof(RawAIOCB
),
864 .bdrv_truncate
= raw_truncate
,
865 .bdrv_getlength
= raw_getlength
,
868 /***********************************************/
872 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
873 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
875 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
877 kern_return_t kernResult
;
878 mach_port_t masterPort
;
879 CFMutableDictionaryRef classesToMatch
;
881 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
882 if ( KERN_SUCCESS
!= kernResult
) {
883 printf( "IOMasterPort returned %d\n", kernResult
);
886 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
887 if ( classesToMatch
== NULL
) {
888 printf( "IOServiceMatching returned a NULL dictionary.\n" );
890 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
892 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
893 if ( KERN_SUCCESS
!= kernResult
)
895 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
901 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
903 io_object_t nextMedia
;
904 kern_return_t kernResult
= KERN_FAILURE
;
906 nextMedia
= IOIteratorNext( mediaIterator
);
909 CFTypeRef bsdPathAsCFString
;
910 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
911 if ( bsdPathAsCFString
) {
912 size_t devPathLength
;
913 strcpy( bsdPath
, _PATH_DEV
);
914 strcat( bsdPath
, "r" );
915 devPathLength
= strlen( bsdPath
);
916 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
917 kernResult
= KERN_SUCCESS
;
919 CFRelease( bsdPathAsCFString
);
921 IOObjectRelease( nextMedia
);
929 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
931 BDRVRawState
*s
= bs
->opaque
;
932 int fd
, open_flags
, ret
;
937 if (strstart(filename
, "/dev/cdrom", NULL
)) {
938 kern_return_t kernResult
;
939 io_iterator_t mediaIterator
;
940 char bsdPath
[ MAXPATHLEN
];
943 kernResult
= FindEjectableCDMedia( &mediaIterator
);
944 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
946 if ( bsdPath
[ 0 ] != '\0' ) {
947 strcat(bsdPath
,"s0");
948 /* some CDs don't have a partition 0 */
949 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
951 bsdPath
[strlen(bsdPath
)-1] = '1';
959 IOObjectRelease( mediaIterator
);
962 open_flags
= O_BINARY
;
963 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
964 open_flags
|= O_RDWR
;
966 open_flags
|= O_RDONLY
;
969 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
970 * and O_DIRECT for no caching. */
971 if ((flags
& BDRV_O_NOCACHE
))
972 open_flags
|= O_DIRECT
;
973 else if (!(flags
& BDRV_O_CACHE_WB
))
974 open_flags
|= O_DSYNC
;
976 s
->type
= FTYPE_FILE
;
977 #if defined(__linux__)
978 if (strstart(filename
, "/dev/cd", NULL
)) {
979 /* open will not fail even if no CD is inserted */
980 open_flags
|= O_NONBLOCK
;
982 } else if (strstart(filename
, "/dev/fd", NULL
)) {
984 s
->fd_open_flags
= open_flags
;
985 /* open will not fail even if no floppy is inserted */
986 open_flags
|= O_NONBLOCK
;
988 } else if (strstart(filename
, "/dev/sg", NULL
)) {
993 #if defined(__FreeBSD__)
994 if (strstart(filename
, "/dev/cd", NULL
) ||
995 strstart(filename
, "/dev/acd", NULL
)) {
997 s
->cd_open_flags
= open_flags
;
1001 fd
= open(filename
, open_flags
, 0644);
1009 #if defined(__FreeBSD__)
1010 /* make sure the door isnt locked at this time */
1011 if (s
->type
== FTYPE_CD
)
1012 ioctl (s
->fd
, CDIOCALLOW
);
1014 #if defined(__linux__)
1015 /* close fd so that we can reopen it as needed */
1016 if (s
->type
== FTYPE_FD
) {
1019 s
->fd_media_changed
= 1;
1025 #if defined(__linux__)
1026 /* Note: we do not have a reliable method to detect if the floppy is
1027 present. The current method is to try to open the floppy at every
1028 I/O and to keep it opened during a few hundreds of ms. */
1029 static int fd_open(BlockDriverState
*bs
)
1031 BDRVRawState
*s
= bs
->opaque
;
1032 int last_media_present
;
1034 if (s
->type
!= FTYPE_FD
)
1036 last_media_present
= (s
->fd
>= 0);
1038 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1042 printf("Floppy closed\n");
1046 if (s
->fd_got_error
&&
1047 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1049 printf("No floppy (open delayed)\n");
1053 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1055 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1056 s
->fd_got_error
= 1;
1057 if (last_media_present
)
1058 s
->fd_media_changed
= 1;
1060 printf("No floppy\n");
1065 printf("Floppy opened\n");
1068 if (!last_media_present
)
1069 s
->fd_media_changed
= 1;
1070 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1071 s
->fd_got_error
= 0;
1075 static int raw_is_inserted(BlockDriverState
*bs
)
1077 BDRVRawState
*s
= bs
->opaque
;
1082 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1083 if (ret
== CDS_DISC_OK
)
1096 /* currently only used by fdc.c, but a CD version would be good too */
1097 static int raw_media_changed(BlockDriverState
*bs
)
1099 BDRVRawState
*s
= bs
->opaque
;
1105 /* XXX: we do not have a true media changed indication. It
1106 does not work if the floppy is changed without trying
1109 ret
= s
->fd_media_changed
;
1110 s
->fd_media_changed
= 0;
1112 printf("Floppy changed=%d\n", ret
);
1121 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1123 BDRVRawState
*s
= bs
->opaque
;
1128 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1129 perror("CDROMEJECT");
1131 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1132 perror("CDROMEJECT");
1142 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1144 if (ioctl(fd
, FDEJECT
, 0) < 0)
1156 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1158 BDRVRawState
*s
= bs
->opaque
;
1162 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1163 /* Note: an error can happen if the distribution automatically
1164 mounts the CD-ROM */
1165 // perror("CDROM_LOCKDOOR");
1174 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1176 BDRVRawState
*s
= bs
->opaque
;
1178 return ioctl(s
->fd
, req
, buf
);
1182 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1183 unsigned long int req
, void *buf
,
1184 BlockDriverCompletionFunc
*cb
, void *opaque
)
1186 BDRVRawState
*s
= bs
->opaque
;
1189 if (fd_open(bs
) < 0)
1192 acb
= qemu_aio_get(bs
, cb
, opaque
);
1195 acb
->aiocb
.aio_fildes
= s
->fd
;
1196 acb
->aiocb
.ev_signo
= SIGUSR2
;
1197 acb
->aiocb
.aio_offset
= 0;
1198 acb
->aiocb
.aio_flags
= 0;
1200 acb
->next
= posix_aio_state
->first_aio
;
1201 posix_aio_state
->first_aio
= acb
;
1203 acb
->aiocb
.aio_ioctl_buf
= buf
;
1204 acb
->aiocb
.aio_ioctl_cmd
= req
;
1205 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1206 raw_aio_remove(acb
);
1210 return &acb
->common
;
1214 #elif defined(__FreeBSD__)
1216 static int fd_open(BlockDriverState
*bs
)
1218 BDRVRawState
*s
= bs
->opaque
;
1220 /* this is just to ensure s->fd is sane (its called by io ops) */
1226 static int cd_open(BlockDriverState
*bs
)
1228 #if defined(__FreeBSD__)
1229 BDRVRawState
*s
= bs
->opaque
;
1234 /* XXX force reread of possibly changed/newly loaded disc,
1235 * FreeBSD seems to not notice sometimes... */
1238 fd
= open(bs
->filename
, s
->cd_open_flags
, 0644);
1244 /* make sure the door isnt locked at this time */
1245 ioctl (s
->fd
, CDIOCALLOW
);
1251 static int raw_is_inserted(BlockDriverState
*bs
)
1253 BDRVRawState
*s
= bs
->opaque
;
1257 return (raw_getlength(bs
) > 0);
1259 /* XXX handle this */
1266 static int raw_media_changed(BlockDriverState
*bs
)
1271 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1273 BDRVRawState
*s
= bs
->opaque
;
1279 (void) ioctl (s
->fd
, CDIOCALLOW
);
1281 if (ioctl (s
->fd
, CDIOCEJECT
) < 0)
1282 perror("CDIOCEJECT");
1284 if (ioctl (s
->fd
, CDIOCCLOSE
) < 0)
1285 perror("CDIOCCLOSE");
1287 if (cd_open(bs
) < 0)
1291 /* XXX handle this */
1299 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1301 BDRVRawState
*s
= bs
->opaque
;
1307 if (ioctl (s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1308 /* Note: an error can happen if the distribution automatically
1309 mounts the CD-ROM */
1310 // perror("CDROM_LOCKDOOR");
1319 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1323 #else /* !linux && !FreeBSD */
1325 static int fd_open(BlockDriverState
*bs
)
1330 static int raw_is_inserted(BlockDriverState
*bs
)
1335 static int raw_media_changed(BlockDriverState
*bs
)
1340 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1345 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1350 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1355 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1356 unsigned long int req
, void *buf
,
1357 BlockDriverCompletionFunc
*cb
, void *opaque
)
1361 #endif /* !linux && !FreeBSD */
1363 #if defined(__linux__) || defined(__FreeBSD__)
1364 static int hdev_create(const char *filename
, int64_t total_size
,
1365 const char *backing_file
, int flags
)
1369 struct stat stat_buf
;
1371 if (flags
|| backing_file
)
1374 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1378 if (fstat(fd
, &stat_buf
) < 0)
1380 else if (!S_ISBLK(stat_buf
.st_mode
))
1382 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1389 #else /* !(linux || freebsd) */
1391 static int hdev_create(const char *filename
, int64_t total_size
,
1392 const char *backing_file
, int flags
)
1398 BlockDriver bdrv_host_device
= {
1399 .format_name
= "host_device",
1400 .instance_size
= sizeof(BDRVRawState
),
1401 .bdrv_open
= hdev_open
,
1402 .bdrv_close
= raw_close
,
1403 .bdrv_create
= hdev_create
,
1404 .bdrv_flush
= raw_flush
,
1407 .bdrv_aio_readv
= raw_aio_readv
,
1408 .bdrv_aio_writev
= raw_aio_writev
,
1409 .bdrv_aio_cancel
= raw_aio_cancel
,
1410 .aiocb_size
= sizeof(RawAIOCB
),
1413 .bdrv_read
= raw_read
,
1414 .bdrv_write
= raw_write
,
1415 .bdrv_getlength
= raw_getlength
,
1417 /* removable device support */
1418 .bdrv_is_inserted
= raw_is_inserted
,
1419 .bdrv_media_changed
= raw_media_changed
,
1420 .bdrv_eject
= raw_eject
,
1421 .bdrv_set_locked
= raw_set_locked
,
1422 /* generic scsi device */
1423 .bdrv_ioctl
= raw_ioctl
,
1425 .bdrv_aio_ioctl
= raw_aio_ioctl
,