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
,
600 int64_t sector_num
, uint8_t *buf
, 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_buf
= buf
;
616 acb
->aiocb
.aio_nbytes
= -nb_sectors
;
618 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
619 acb
->aiocb
.aio_offset
= sector_num
* 512;
620 acb
->next
= posix_aio_state
->first_aio
;
621 posix_aio_state
->first_aio
= acb
;
625 static void raw_aio_em_cb(void* opaque
)
627 RawAIOCB
*acb
= opaque
;
628 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
629 qemu_aio_release(acb
);
632 static void raw_aio_remove(RawAIOCB
*acb
)
636 /* remove the callback from the queue */
637 pacb
= &posix_aio_state
->first_aio
;
640 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
642 } else if (*pacb
== acb
) {
644 qemu_aio_release(acb
);
647 pacb
= &(*pacb
)->next
;
651 static BlockDriverAIOCB
*raw_aio_read(BlockDriverState
*bs
,
652 int64_t sector_num
, uint8_t *buf
, int nb_sectors
,
653 BlockDriverCompletionFunc
*cb
, void *opaque
)
658 * If O_DIRECT is used and the buffer is not aligned fall back
661 BDRVRawState
*s
= bs
->opaque
;
663 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
665 acb
= qemu_aio_get(bs
, cb
, opaque
);
666 acb
->ret
= raw_pread(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
667 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
668 qemu_bh_schedule(bh
);
672 acb
= raw_aio_setup(bs
, sector_num
, buf
, nb_sectors
, cb
, opaque
);
675 if (qemu_paio_read(&acb
->aiocb
) < 0) {
682 static BlockDriverAIOCB
*raw_aio_write(BlockDriverState
*bs
,
683 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
,
684 BlockDriverCompletionFunc
*cb
, void *opaque
)
689 * If O_DIRECT is used and the buffer is not aligned fall back
692 BDRVRawState
*s
= bs
->opaque
;
694 if (unlikely(s
->aligned_buf
!= NULL
&& ((uintptr_t) buf
% 512))) {
696 acb
= qemu_aio_get(bs
, cb
, opaque
);
697 acb
->ret
= raw_pwrite(bs
, 512 * sector_num
, buf
, 512 * nb_sectors
);
698 bh
= qemu_bh_new(raw_aio_em_cb
, acb
);
699 qemu_bh_schedule(bh
);
703 acb
= raw_aio_setup(bs
, sector_num
, (uint8_t*)buf
, nb_sectors
, cb
, opaque
);
706 if (qemu_paio_write(&acb
->aiocb
) < 0) {
713 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
716 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
718 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
719 if (ret
== QEMU_PAIO_NOTCANCELED
) {
720 /* fail safe: if the aio could not be canceled, we wait for
722 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
727 #else /* CONFIG_AIO */
728 static int posix_aio_init(void)
732 #endif /* CONFIG_AIO */
735 static void raw_close(BlockDriverState
*bs
)
737 BDRVRawState
*s
= bs
->opaque
;
741 if (s
->aligned_buf
!= NULL
)
742 qemu_free(s
->aligned_buf
);
746 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
748 BDRVRawState
*s
= bs
->opaque
;
749 if (s
->type
!= FTYPE_FILE
)
751 if (ftruncate(s
->fd
, offset
) < 0)
757 static int64_t raw_getlength(BlockDriverState
*bs
)
759 BDRVRawState
*s
= bs
->opaque
;
765 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
768 if (ioctl(fd
, DIOCGDINFO
, &dl
))
770 return (uint64_t)dl
.d_secsize
*
771 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
775 #else /* !__OpenBSD__ */
776 static int64_t raw_getlength(BlockDriverState
*bs
)
778 BDRVRawState
*s
= bs
->opaque
;
788 struct dk_minfo minfo
;
801 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
802 #ifdef DIOCGMEDIASIZE
803 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
804 #elif defined(DIOCGPART)
807 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
808 size
= pi
.media_size
;
815 size
= LONG_LONG_MAX
;
817 size
= lseek(fd
, 0LL, SEEK_END
);
822 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
823 if (size
== 2048LL * (unsigned)-1)
825 /* XXX no disc? maybe we need to reopen... */
826 if (size
<= 0 && !reopened
&& cd_open(bs
) >= 0) {
836 * use the DKIOCGMEDIAINFO ioctl to read the size.
838 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
840 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
841 } else /* there are reports that lseek on some devices
842 fails, but irc discussion said that contingency
843 on contingency was overkill */
846 size
= lseek(fd
, 0, SEEK_END
);
852 static int raw_create(const char *filename
, int64_t total_size
,
853 const char *backing_file
, int flags
)
857 if (flags
|| backing_file
)
860 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
864 ftruncate(fd
, total_size
* 512);
869 static void raw_flush(BlockDriverState
*bs
)
871 BDRVRawState
*s
= bs
->opaque
;
875 BlockDriver bdrv_raw
= {
877 sizeof(BDRVRawState
),
878 NULL
, /* no probe for protocols */
887 .bdrv_aio_read
= raw_aio_read
,
888 .bdrv_aio_write
= raw_aio_write
,
889 .bdrv_aio_cancel
= raw_aio_cancel
,
890 .aiocb_size
= sizeof(RawAIOCB
),
893 .bdrv_read
= raw_read
,
894 .bdrv_write
= raw_write
,
895 .bdrv_truncate
= raw_truncate
,
896 .bdrv_getlength
= raw_getlength
,
899 /***********************************************/
903 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
904 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
906 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
908 kern_return_t kernResult
;
909 mach_port_t masterPort
;
910 CFMutableDictionaryRef classesToMatch
;
912 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
913 if ( KERN_SUCCESS
!= kernResult
) {
914 printf( "IOMasterPort returned %d\n", kernResult
);
917 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
918 if ( classesToMatch
== NULL
) {
919 printf( "IOServiceMatching returned a NULL dictionary.\n" );
921 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
923 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
924 if ( KERN_SUCCESS
!= kernResult
)
926 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
932 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
934 io_object_t nextMedia
;
935 kern_return_t kernResult
= KERN_FAILURE
;
937 nextMedia
= IOIteratorNext( mediaIterator
);
940 CFTypeRef bsdPathAsCFString
;
941 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
942 if ( bsdPathAsCFString
) {
943 size_t devPathLength
;
944 strcpy( bsdPath
, _PATH_DEV
);
945 strcat( bsdPath
, "r" );
946 devPathLength
= strlen( bsdPath
);
947 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
948 kernResult
= KERN_SUCCESS
;
950 CFRelease( bsdPathAsCFString
);
952 IOObjectRelease( nextMedia
);
960 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
962 BDRVRawState
*s
= bs
->opaque
;
963 int fd
, open_flags
, ret
;
968 if (strstart(filename
, "/dev/cdrom", NULL
)) {
969 kern_return_t kernResult
;
970 io_iterator_t mediaIterator
;
971 char bsdPath
[ MAXPATHLEN
];
974 kernResult
= FindEjectableCDMedia( &mediaIterator
);
975 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
977 if ( bsdPath
[ 0 ] != '\0' ) {
978 strcat(bsdPath
,"s0");
979 /* some CDs don't have a partition 0 */
980 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
982 bsdPath
[strlen(bsdPath
)-1] = '1';
990 IOObjectRelease( mediaIterator
);
993 open_flags
= O_BINARY
;
994 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
995 open_flags
|= O_RDWR
;
997 open_flags
|= O_RDONLY
;
1000 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
1001 * and O_DIRECT for no caching. */
1002 if ((flags
& BDRV_O_NOCACHE
))
1003 open_flags
|= O_DIRECT
;
1004 else if (!(flags
& BDRV_O_CACHE_WB
))
1005 open_flags
|= O_DSYNC
;
1007 s
->type
= FTYPE_FILE
;
1008 #if defined(__linux__)
1009 if (strstart(filename
, "/dev/cd", NULL
)) {
1010 /* open will not fail even if no CD is inserted */
1011 open_flags
|= O_NONBLOCK
;
1013 } else if (strstart(filename
, "/dev/fd", NULL
)) {
1015 s
->fd_open_flags
= open_flags
;
1016 /* open will not fail even if no floppy is inserted */
1017 open_flags
|= O_NONBLOCK
;
1019 } else if (strstart(filename
, "/dev/sg", NULL
)) {
1024 #if defined(__FreeBSD__)
1025 if (strstart(filename
, "/dev/cd", NULL
) ||
1026 strstart(filename
, "/dev/acd", NULL
)) {
1028 s
->cd_open_flags
= open_flags
;
1032 fd
= open(filename
, open_flags
, 0644);
1040 #if defined(__FreeBSD__)
1041 /* make sure the door isnt locked at this time */
1042 if (s
->type
== FTYPE_CD
)
1043 ioctl (s
->fd
, CDIOCALLOW
);
1045 #if defined(__linux__)
1046 /* close fd so that we can reopen it as needed */
1047 if (s
->type
== FTYPE_FD
) {
1050 s
->fd_media_changed
= 1;
1056 #if defined(__linux__)
1057 /* Note: we do not have a reliable method to detect if the floppy is
1058 present. The current method is to try to open the floppy at every
1059 I/O and to keep it opened during a few hundreds of ms. */
1060 static int fd_open(BlockDriverState
*bs
)
1062 BDRVRawState
*s
= bs
->opaque
;
1063 int last_media_present
;
1065 if (s
->type
!= FTYPE_FD
)
1067 last_media_present
= (s
->fd
>= 0);
1069 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1073 printf("Floppy closed\n");
1077 if (s
->fd_got_error
&&
1078 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1080 printf("No floppy (open delayed)\n");
1084 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1086 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1087 s
->fd_got_error
= 1;
1088 if (last_media_present
)
1089 s
->fd_media_changed
= 1;
1091 printf("No floppy\n");
1096 printf("Floppy opened\n");
1099 if (!last_media_present
)
1100 s
->fd_media_changed
= 1;
1101 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1102 s
->fd_got_error
= 0;
1106 static int raw_is_inserted(BlockDriverState
*bs
)
1108 BDRVRawState
*s
= bs
->opaque
;
1113 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1114 if (ret
== CDS_DISC_OK
)
1127 /* currently only used by fdc.c, but a CD version would be good too */
1128 static int raw_media_changed(BlockDriverState
*bs
)
1130 BDRVRawState
*s
= bs
->opaque
;
1136 /* XXX: we do not have a true media changed indication. It
1137 does not work if the floppy is changed without trying
1140 ret
= s
->fd_media_changed
;
1141 s
->fd_media_changed
= 0;
1143 printf("Floppy changed=%d\n", ret
);
1152 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1154 BDRVRawState
*s
= bs
->opaque
;
1159 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1160 perror("CDROMEJECT");
1162 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1163 perror("CDROMEJECT");
1173 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1175 if (ioctl(fd
, FDEJECT
, 0) < 0)
1187 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1189 BDRVRawState
*s
= bs
->opaque
;
1193 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1194 /* Note: an error can happen if the distribution automatically
1195 mounts the CD-ROM */
1196 // perror("CDROM_LOCKDOOR");
1205 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1207 BDRVRawState
*s
= bs
->opaque
;
1209 return ioctl(s
->fd
, req
, buf
);
1213 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1214 unsigned long int req
, void *buf
,
1215 BlockDriverCompletionFunc
*cb
, void *opaque
)
1219 acb
= raw_aio_setup(bs
, 0, buf
, 0, cb
, opaque
);
1223 acb
->aiocb
.aio_ioctl_cmd
= req
;
1224 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1225 raw_aio_remove(acb
);
1229 return &acb
->common
;
1233 #elif defined(__FreeBSD__)
1235 static int fd_open(BlockDriverState
*bs
)
1237 BDRVRawState
*s
= bs
->opaque
;
1239 /* this is just to ensure s->fd is sane (its called by io ops) */
1245 static int cd_open(BlockDriverState
*bs
)
1247 #if defined(__FreeBSD__)
1248 BDRVRawState
*s
= bs
->opaque
;
1253 /* XXX force reread of possibly changed/newly loaded disc,
1254 * FreeBSD seems to not notice sometimes... */
1257 fd
= open(bs
->filename
, s
->cd_open_flags
, 0644);
1263 /* make sure the door isnt locked at this time */
1264 ioctl (s
->fd
, CDIOCALLOW
);
1270 static int raw_is_inserted(BlockDriverState
*bs
)
1272 BDRVRawState
*s
= bs
->opaque
;
1276 return (raw_getlength(bs
) > 0);
1278 /* XXX handle this */
1285 static int raw_media_changed(BlockDriverState
*bs
)
1290 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1292 BDRVRawState
*s
= bs
->opaque
;
1298 (void) ioctl (s
->fd
, CDIOCALLOW
);
1300 if (ioctl (s
->fd
, CDIOCEJECT
) < 0)
1301 perror("CDIOCEJECT");
1303 if (ioctl (s
->fd
, CDIOCCLOSE
) < 0)
1304 perror("CDIOCCLOSE");
1306 if (cd_open(bs
) < 0)
1310 /* XXX handle this */
1318 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1320 BDRVRawState
*s
= bs
->opaque
;
1326 if (ioctl (s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1327 /* Note: an error can happen if the distribution automatically
1328 mounts the CD-ROM */
1329 // perror("CDROM_LOCKDOOR");
1338 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1342 #else /* !linux && !FreeBSD */
1344 static int fd_open(BlockDriverState
*bs
)
1349 static int raw_is_inserted(BlockDriverState
*bs
)
1354 static int raw_media_changed(BlockDriverState
*bs
)
1359 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1364 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1369 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1374 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1375 unsigned long int req
, void *buf
,
1376 BlockDriverCompletionFunc
*cb
, void *opaque
)
1380 #endif /* !linux && !FreeBSD */
1382 #if defined(__linux__) || defined(__FreeBSD__)
1383 static int hdev_create(const char *filename
, int64_t total_size
,
1384 const char *backing_file
, int flags
)
1388 struct stat stat_buf
;
1390 if (flags
|| backing_file
)
1393 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1397 if (fstat(fd
, &stat_buf
) < 0)
1399 else if (!S_ISBLK(stat_buf
.st_mode
))
1401 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1408 #else /* !(linux || freebsd) */
1410 static int hdev_create(const char *filename
, int64_t total_size
,
1411 const char *backing_file
, int flags
)
1417 BlockDriver bdrv_host_device
= {
1418 .format_name
= "host_device",
1419 .instance_size
= sizeof(BDRVRawState
),
1420 .bdrv_open
= hdev_open
,
1421 .bdrv_close
= raw_close
,
1422 .bdrv_create
= hdev_create
,
1423 .bdrv_flush
= raw_flush
,
1426 .bdrv_aio_read
= raw_aio_read
,
1427 .bdrv_aio_write
= raw_aio_write
,
1428 .bdrv_aio_cancel
= raw_aio_cancel
,
1429 .aiocb_size
= sizeof(RawAIOCB
),
1432 .bdrv_read
= raw_read
,
1433 .bdrv_write
= raw_write
,
1434 .bdrv_getlength
= raw_getlength
,
1436 /* removable device support */
1437 .bdrv_is_inserted
= raw_is_inserted
,
1438 .bdrv_media_changed
= raw_media_changed
,
1439 .bdrv_eject
= raw_eject
,
1440 .bdrv_set_locked
= raw_set_locked
,
1441 /* generic scsi device */
1442 .bdrv_ioctl
= raw_ioctl
,
1444 .bdrv_aio_ioctl
= raw_aio_ioctl
,