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"
29 #include "posix-aio-compat.h"
34 #include <sys/param.h>
35 #include <IOKit/IOKitLib.h>
36 #include <IOKit/IOBSD.h>
37 #include <IOKit/storage/IOMediaBSDClient.h>
38 #include <IOKit/storage/IOMedia.h>
39 #include <IOKit/storage/IOCDMedia.h>
40 //#include <IOKit/storage/IOCDTypes.h>
41 #include <CoreFoundation/CoreFoundation.h>
45 #define _POSIX_PTHREAD_SEMANTICS 1
50 #include <sys/ioctl.h>
51 #include <linux/cdrom.h>
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
67 #include <sys/ioctl.h>
68 #include <sys/diskslice.h>
71 //#define DEBUG_FLOPPY
74 #if defined(DEBUG_BLOCK)
75 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
76 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
78 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
81 /* OS X does not have O_DSYNC */
83 #define O_DSYNC O_SYNC
86 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
88 #define O_DIRECT O_DSYNC
95 #define ALIGNED_BUFFER_SIZE (32 * 512)
97 /* if the FD is not accessed during that time (in ms), we try to
98 reopen it to see if the disk has been changed */
99 #define FD_OPEN_TIMEOUT 1000
101 typedef struct BDRVRawState
{
104 unsigned int lseek_err_cnt
;
105 #if defined(__linux__)
106 /* linux floppy specific */
108 int64_t fd_open_time
;
109 int64_t fd_error_time
;
111 int fd_media_changed
;
113 #if defined(__FreeBSD__)
116 uint8_t* aligned_buf
;
119 static int posix_aio_init(void);
121 static int fd_open(BlockDriverState
*bs
);
123 #if defined(__FreeBSD__)
124 static int cd_open(BlockDriverState
*bs
);
127 static int raw_is_inserted(BlockDriverState
*bs
);
129 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
131 BDRVRawState
*s
= bs
->opaque
;
132 int fd
, open_flags
, ret
;
136 s
->lseek_err_cnt
= 0;
138 open_flags
= O_BINARY
;
139 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
140 open_flags
|= O_RDWR
;
142 open_flags
|= O_RDONLY
;
145 if (flags
& BDRV_O_CREAT
)
146 open_flags
|= O_CREAT
| O_TRUNC
;
148 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
149 * and O_DIRECT for no caching. */
150 if ((flags
& BDRV_O_NOCACHE
))
151 open_flags
|= O_DIRECT
;
152 else if (!(flags
& BDRV_O_CACHE_WB
))
153 open_flags
|= O_DSYNC
;
155 s
->type
= FTYPE_FILE
;
157 fd
= open(filename
, open_flags
, 0644);
165 s
->aligned_buf
= NULL
;
166 if ((flags
& BDRV_O_NOCACHE
)) {
167 s
->aligned_buf
= qemu_blockalign(bs
, ALIGNED_BUFFER_SIZE
);
168 if (s
->aligned_buf
== NULL
) {
177 /* XXX: use host sector size if necessary with:
178 #ifdef DIOCGSECTORSIZE
180 unsigned int sectorsize = 512;
181 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
182 sectorsize > bufsize)
183 bufsize = sectorsize;
187 u_int32_t blockSize = 512;
188 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
195 * offset and count are in bytes, but must be multiples of 512 for files
196 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
198 * This function may be called without alignment if the caller ensures
199 * that O_DIRECT is not in effect.
201 static int raw_pread_aligned(BlockDriverState
*bs
, int64_t offset
,
202 uint8_t *buf
, int count
)
204 BDRVRawState
*s
= bs
->opaque
;
211 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
212 ++(s
->lseek_err_cnt
);
213 if(s
->lseek_err_cnt
<= 10) {
214 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
215 "] lseek failed : %d = %s\n",
216 s
->fd
, bs
->filename
, offset
, buf
, count
,
217 bs
->total_sectors
, errno
, strerror(errno
));
223 ret
= read(s
->fd
, buf
, count
);
225 goto label__raw_read__success
;
227 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
228 "] read failed %d : %d = %s\n",
229 s
->fd
, bs
->filename
, offset
, buf
, count
,
230 bs
->total_sectors
, ret
, errno
, strerror(errno
));
232 /* Try harder for CDrom. */
233 if (bs
->type
== BDRV_TYPE_CDROM
) {
234 lseek(s
->fd
, offset
, SEEK_SET
);
235 ret
= read(s
->fd
, buf
, count
);
237 goto label__raw_read__success
;
238 lseek(s
->fd
, offset
, SEEK_SET
);
239 ret
= read(s
->fd
, buf
, count
);
241 goto label__raw_read__success
;
243 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64
", %p, %d) [%" PRId64
244 "] retry read failed %d : %d = %s\n",
245 s
->fd
, bs
->filename
, offset
, buf
, count
,
246 bs
->total_sectors
, ret
, errno
, strerror(errno
));
249 label__raw_read__success
:
255 * offset and count are in bytes, but must be multiples of 512 for files
256 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
258 * This function may be called without alignment if the caller ensures
259 * that O_DIRECT is not in effect.
261 static int raw_pwrite_aligned(BlockDriverState
*bs
, int64_t offset
,
262 const uint8_t *buf
, int count
)
264 BDRVRawState
*s
= bs
->opaque
;
271 if (offset
>= 0 && lseek(s
->fd
, offset
, SEEK_SET
) == (off_t
)-1) {
272 ++(s
->lseek_err_cnt
);
273 if(s
->lseek_err_cnt
) {
274 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%"
275 PRId64
"] lseek failed : %d = %s\n",
276 s
->fd
, bs
->filename
, offset
, buf
, count
,
277 bs
->total_sectors
, errno
, strerror(errno
));
281 s
->lseek_err_cnt
= 0;
283 ret
= write(s
->fd
, buf
, count
);
285 goto label__raw_write__success
;
287 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64
", %p, %d) [%" PRId64
288 "] write failed %d : %d = %s\n",
289 s
->fd
, bs
->filename
, offset
, buf
, count
,
290 bs
->total_sectors
, ret
, errno
, strerror(errno
));
292 label__raw_write__success
:
294 return (ret
< 0) ? -errno
: ret
;
299 * offset and count are in bytes and possibly not aligned. For files opened
300 * with O_DIRECT, necessary alignments are ensured before calling
301 * raw_pread_aligned to do the actual read.
303 static int raw_pread(BlockDriverState
*bs
, int64_t offset
,
304 uint8_t *buf
, int count
)
306 BDRVRawState
*s
= bs
->opaque
;
307 int size
, ret
, shift
, sum
;
311 if (s
->aligned_buf
!= NULL
) {
313 if (offset
& 0x1ff) {
314 /* align offset on a 512 bytes boundary */
316 shift
= offset
& 0x1ff;
317 size
= (shift
+ count
+ 0x1ff) & ~0x1ff;
318 if (size
> ALIGNED_BUFFER_SIZE
)
319 size
= ALIGNED_BUFFER_SIZE
;
320 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, size
);
327 memcpy(buf
, s
->aligned_buf
+ shift
, size
);
337 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
339 /* read on aligned buffer */
343 size
= (count
+ 0x1ff) & ~0x1ff;
344 if (size
> ALIGNED_BUFFER_SIZE
)
345 size
= ALIGNED_BUFFER_SIZE
;
347 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, size
);
355 memcpy(buf
, s
->aligned_buf
, size
);
367 return raw_pread_aligned(bs
, offset
, buf
, count
) + sum
;
370 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
371 uint8_t *buf
, int nb_sectors
)
375 ret
= raw_pread(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
376 if (ret
== (nb_sectors
* 512))
382 * offset and count are in bytes and possibly not aligned. For files opened
383 * with O_DIRECT, necessary alignments are ensured before calling
384 * raw_pwrite_aligned to do the actual write.
386 static int raw_pwrite(BlockDriverState
*bs
, int64_t offset
,
387 const uint8_t *buf
, int count
)
389 BDRVRawState
*s
= bs
->opaque
;
390 int size
, ret
, shift
, sum
;
394 if (s
->aligned_buf
!= NULL
) {
396 if (offset
& 0x1ff) {
397 /* align offset on a 512 bytes boundary */
398 shift
= offset
& 0x1ff;
399 ret
= raw_pread_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
406 memcpy(s
->aligned_buf
+ shift
, buf
, size
);
408 ret
= raw_pwrite_aligned(bs
, offset
- shift
, s
->aligned_buf
, 512);
420 if (count
& 0x1ff || (uintptr_t) buf
& 0x1ff) {
422 while ((size
= (count
& ~0x1ff)) != 0) {
424 if (size
> ALIGNED_BUFFER_SIZE
)
425 size
= ALIGNED_BUFFER_SIZE
;
427 memcpy(s
->aligned_buf
, buf
, size
);
429 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, size
);
438 /* here, count < 512 because (count & ~0x1ff) == 0 */
440 ret
= raw_pread_aligned(bs
, offset
, s
->aligned_buf
, 512);
443 memcpy(s
->aligned_buf
, buf
, count
);
445 ret
= raw_pwrite_aligned(bs
, offset
, s
->aligned_buf
, 512);
456 return raw_pwrite_aligned(bs
, offset
, buf
, count
) + sum
;
459 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
460 const uint8_t *buf
, int nb_sectors
)
463 ret
= raw_pwrite(bs
, sector_num
* 512, buf
, nb_sectors
* 512);
464 if (ret
== (nb_sectors
* 512))
470 /***********************************************************/
471 /* Unix AIO using POSIX AIO */
473 typedef struct RawAIOCB
{
474 BlockDriverAIOCB common
;
475 struct qemu_paiocb aiocb
;
476 struct RawAIOCB
*next
;
480 typedef struct PosixAioState
486 static void posix_aio_read(void *opaque
)
488 PosixAioState
*s
= opaque
;
489 RawAIOCB
*acb
, **pacb
;
493 /* read all bytes from signal pipe */
497 len
= read(s
->rfd
, bytes
, sizeof(bytes
));
498 if (len
== -1 && errno
== EINTR
)
499 continue; /* try again */
500 if (len
== sizeof(bytes
))
501 continue; /* more to read */
506 pacb
= &s
->first_aio
;
511 ret
= qemu_paio_error(&acb
->aiocb
);
512 if (ret
== ECANCELED
) {
513 /* remove the request */
515 qemu_aio_release(acb
);
516 } else if (ret
!= EINPROGRESS
) {
519 ret
= qemu_paio_return(&acb
->aiocb
);
520 if (ret
== acb
->aiocb
.aio_nbytes
)
527 /* remove the request */
529 /* call the callback */
530 acb
->common
.cb(acb
->common
.opaque
, ret
);
531 qemu_aio_release(acb
);
541 static int posix_aio_flush(void *opaque
)
543 PosixAioState
*s
= opaque
;
544 return !!s
->first_aio
;
547 static PosixAioState
*posix_aio_state
;
549 static void aio_signal_handler(int signum
)
551 if (posix_aio_state
) {
554 write(posix_aio_state
->wfd
, &byte
, sizeof(byte
));
560 static int posix_aio_init(void)
562 struct sigaction act
;
565 struct qemu_paioinit ai
;
570 s
= qemu_malloc(sizeof(PosixAioState
));
572 sigfillset(&act
.sa_mask
);
573 act
.sa_flags
= 0; /* do not restart syscalls to interrupt select() */
574 act
.sa_handler
= aio_signal_handler
;
575 sigaction(SIGUSR2
, &act
, NULL
);
578 if (pipe(fds
) == -1) {
579 fprintf(stderr
, "failed to create pipe\n");
586 fcntl(s
->rfd
, F_SETFL
, O_NONBLOCK
);
587 fcntl(s
->wfd
, F_SETFL
, O_NONBLOCK
);
589 qemu_aio_set_fd_handler(s
->rfd
, posix_aio_read
, NULL
, posix_aio_flush
, s
);
591 memset(&ai
, 0, sizeof(ai
));
601 static RawAIOCB
*raw_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
602 QEMUIOVector
*qiov
, int nb_sectors
,
603 BlockDriverCompletionFunc
*cb
, void *opaque
)
605 BDRVRawState
*s
= bs
->opaque
;
611 acb
= qemu_aio_get(bs
, cb
, opaque
);
614 acb
->aiocb
.aio_fildes
= s
->fd
;
615 acb
->aiocb
.ev_signo
= SIGUSR2
;
616 acb
->aiocb
.aio_iov
= qiov
->iov
;
617 acb
->aiocb
.aio_niov
= qiov
->niov
;
618 acb
->aiocb
.aio_nbytes
= nb_sectors
* 512;
619 acb
->aiocb
.aio_offset
= sector_num
* 512;
620 acb
->aiocb
.aio_flags
= 0;
623 * If O_DIRECT is used the buffer needs to be aligned on a sector
624 * boundary. Tell the low level code to ensure that in case it's
628 acb
->aiocb
.aio_flags
|= QEMU_AIO_SECTOR_ALIGNED
;
630 acb
->next
= posix_aio_state
->first_aio
;
631 posix_aio_state
->first_aio
= acb
;
635 static void raw_aio_remove(RawAIOCB
*acb
)
639 /* remove the callback from the queue */
640 pacb
= &posix_aio_state
->first_aio
;
643 fprintf(stderr
, "raw_aio_remove: aio request not found!\n");
645 } else if (*pacb
== acb
) {
647 qemu_aio_release(acb
);
650 pacb
= &(*pacb
)->next
;
654 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
655 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
656 BlockDriverCompletionFunc
*cb
, void *opaque
)
660 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
663 if (qemu_paio_read(&acb
->aiocb
) < 0) {
670 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
671 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
672 BlockDriverCompletionFunc
*cb
, void *opaque
)
676 acb
= raw_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
);
679 if (qemu_paio_write(&acb
->aiocb
) < 0) {
686 static void raw_aio_cancel(BlockDriverAIOCB
*blockacb
)
689 RawAIOCB
*acb
= (RawAIOCB
*)blockacb
;
691 ret
= qemu_paio_cancel(acb
->aiocb
.aio_fildes
, &acb
->aiocb
);
692 if (ret
== QEMU_PAIO_NOTCANCELED
) {
693 /* fail safe: if the aio could not be canceled, we wait for
695 while (qemu_paio_error(&acb
->aiocb
) == EINPROGRESS
);
700 #else /* CONFIG_AIO */
701 static int posix_aio_init(void)
705 #endif /* CONFIG_AIO */
708 static void raw_close(BlockDriverState
*bs
)
710 BDRVRawState
*s
= bs
->opaque
;
714 if (s
->aligned_buf
!= NULL
)
715 qemu_free(s
->aligned_buf
);
719 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
721 BDRVRawState
*s
= bs
->opaque
;
722 if (s
->type
!= FTYPE_FILE
)
724 if (ftruncate(s
->fd
, offset
) < 0)
730 static int64_t raw_getlength(BlockDriverState
*bs
)
732 BDRVRawState
*s
= bs
->opaque
;
738 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
741 if (ioctl(fd
, DIOCGDINFO
, &dl
))
743 return (uint64_t)dl
.d_secsize
*
744 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
748 #else /* !__OpenBSD__ */
749 static int64_t raw_getlength(BlockDriverState
*bs
)
751 BDRVRawState
*s
= bs
->opaque
;
761 struct dk_minfo minfo
;
774 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
775 #ifdef DIOCGMEDIASIZE
776 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
777 #elif defined(DIOCGPART)
780 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
781 size
= pi
.media_size
;
788 size
= LONG_LONG_MAX
;
790 size
= lseek(fd
, 0LL, SEEK_END
);
795 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
796 if (size
== 2048LL * (unsigned)-1)
798 /* XXX no disc? maybe we need to reopen... */
799 if (size
<= 0 && !reopened
&& cd_open(bs
) >= 0) {
809 * use the DKIOCGMEDIAINFO ioctl to read the size.
811 rv
= ioctl ( fd
, DKIOCGMEDIAINFO
, &minfo
);
813 size
= minfo
.dki_lbsize
* minfo
.dki_capacity
;
814 } else /* there are reports that lseek on some devices
815 fails, but irc discussion said that contingency
816 on contingency was overkill */
819 size
= lseek(fd
, 0, SEEK_END
);
825 static int raw_create(const char *filename
, int64_t total_size
,
826 const char *backing_file
, int flags
)
830 if (flags
|| backing_file
)
833 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
837 ftruncate(fd
, total_size
* 512);
842 static void raw_flush(BlockDriverState
*bs
)
844 BDRVRawState
*s
= bs
->opaque
;
848 BlockDriver bdrv_raw
= {
849 .format_name
= "raw",
850 .instance_size
= sizeof(BDRVRawState
),
851 .bdrv_probe
= NULL
, /* no probe for protocols */
852 .bdrv_open
= raw_open
,
853 .bdrv_read
= raw_read
,
854 .bdrv_write
= raw_write
,
855 .bdrv_close
= raw_close
,
856 .bdrv_create
= raw_create
,
857 .bdrv_flush
= raw_flush
,
860 .bdrv_aio_readv
= raw_aio_readv
,
861 .bdrv_aio_writev
= raw_aio_writev
,
862 .bdrv_aio_cancel
= raw_aio_cancel
,
863 .aiocb_size
= sizeof(RawAIOCB
),
866 .bdrv_truncate
= raw_truncate
,
867 .bdrv_getlength
= raw_getlength
,
870 /***********************************************/
874 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
875 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
877 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
879 kern_return_t kernResult
;
880 mach_port_t masterPort
;
881 CFMutableDictionaryRef classesToMatch
;
883 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
884 if ( KERN_SUCCESS
!= kernResult
) {
885 printf( "IOMasterPort returned %d\n", kernResult
);
888 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
889 if ( classesToMatch
== NULL
) {
890 printf( "IOServiceMatching returned a NULL dictionary.\n" );
892 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
894 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
895 if ( KERN_SUCCESS
!= kernResult
)
897 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
903 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
905 io_object_t nextMedia
;
906 kern_return_t kernResult
= KERN_FAILURE
;
908 nextMedia
= IOIteratorNext( mediaIterator
);
911 CFTypeRef bsdPathAsCFString
;
912 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
913 if ( bsdPathAsCFString
) {
914 size_t devPathLength
;
915 strcpy( bsdPath
, _PATH_DEV
);
916 strcat( bsdPath
, "r" );
917 devPathLength
= strlen( bsdPath
);
918 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
919 kernResult
= KERN_SUCCESS
;
921 CFRelease( bsdPathAsCFString
);
923 IOObjectRelease( nextMedia
);
931 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
933 BDRVRawState
*s
= bs
->opaque
;
934 int fd
, open_flags
, ret
;
939 if (strstart(filename
, "/dev/cdrom", NULL
)) {
940 kern_return_t kernResult
;
941 io_iterator_t mediaIterator
;
942 char bsdPath
[ MAXPATHLEN
];
945 kernResult
= FindEjectableCDMedia( &mediaIterator
);
946 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
948 if ( bsdPath
[ 0 ] != '\0' ) {
949 strcat(bsdPath
,"s0");
950 /* some CDs don't have a partition 0 */
951 fd
= open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
953 bsdPath
[strlen(bsdPath
)-1] = '1';
961 IOObjectRelease( mediaIterator
);
964 open_flags
= O_BINARY
;
965 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
966 open_flags
|= O_RDWR
;
968 open_flags
|= O_RDONLY
;
971 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
972 * and O_DIRECT for no caching. */
973 if ((flags
& BDRV_O_NOCACHE
))
974 open_flags
|= O_DIRECT
;
975 else if (!(flags
& BDRV_O_CACHE_WB
))
976 open_flags
|= O_DSYNC
;
978 s
->type
= FTYPE_FILE
;
979 #if defined(__linux__)
980 if (strstart(filename
, "/dev/cd", NULL
)) {
981 /* open will not fail even if no CD is inserted */
982 open_flags
|= O_NONBLOCK
;
984 } else if (strstart(filename
, "/dev/fd", NULL
)) {
986 s
->fd_open_flags
= open_flags
;
987 /* open will not fail even if no floppy is inserted */
988 open_flags
|= O_NONBLOCK
;
990 } else if (strstart(filename
, "/dev/sg", NULL
)) {
995 #if defined(__FreeBSD__)
996 if (strstart(filename
, "/dev/cd", NULL
) ||
997 strstart(filename
, "/dev/acd", NULL
)) {
999 s
->cd_open_flags
= open_flags
;
1003 fd
= open(filename
, open_flags
, 0644);
1011 #if defined(__FreeBSD__)
1012 /* make sure the door isnt locked at this time */
1013 if (s
->type
== FTYPE_CD
)
1014 ioctl (s
->fd
, CDIOCALLOW
);
1016 #if defined(__linux__)
1017 /* close fd so that we can reopen it as needed */
1018 if (s
->type
== FTYPE_FD
) {
1021 s
->fd_media_changed
= 1;
1027 #if defined(__linux__)
1028 /* Note: we do not have a reliable method to detect if the floppy is
1029 present. The current method is to try to open the floppy at every
1030 I/O and to keep it opened during a few hundreds of ms. */
1031 static int fd_open(BlockDriverState
*bs
)
1033 BDRVRawState
*s
= bs
->opaque
;
1034 int last_media_present
;
1036 if (s
->type
!= FTYPE_FD
)
1038 last_media_present
= (s
->fd
>= 0);
1040 (qemu_get_clock(rt_clock
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1044 printf("Floppy closed\n");
1048 if (s
->fd_got_error
&&
1049 (qemu_get_clock(rt_clock
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1051 printf("No floppy (open delayed)\n");
1055 s
->fd
= open(bs
->filename
, s
->fd_open_flags
);
1057 s
->fd_error_time
= qemu_get_clock(rt_clock
);
1058 s
->fd_got_error
= 1;
1059 if (last_media_present
)
1060 s
->fd_media_changed
= 1;
1062 printf("No floppy\n");
1067 printf("Floppy opened\n");
1070 if (!last_media_present
)
1071 s
->fd_media_changed
= 1;
1072 s
->fd_open_time
= qemu_get_clock(rt_clock
);
1073 s
->fd_got_error
= 0;
1077 static int raw_is_inserted(BlockDriverState
*bs
)
1079 BDRVRawState
*s
= bs
->opaque
;
1084 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1085 if (ret
== CDS_DISC_OK
)
1098 /* currently only used by fdc.c, but a CD version would be good too */
1099 static int raw_media_changed(BlockDriverState
*bs
)
1101 BDRVRawState
*s
= bs
->opaque
;
1107 /* XXX: we do not have a true media changed indication. It
1108 does not work if the floppy is changed without trying
1111 ret
= s
->fd_media_changed
;
1112 s
->fd_media_changed
= 0;
1114 printf("Floppy changed=%d\n", ret
);
1123 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1125 BDRVRawState
*s
= bs
->opaque
;
1130 if (ioctl (s
->fd
, CDROMEJECT
, NULL
) < 0)
1131 perror("CDROMEJECT");
1133 if (ioctl (s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1134 perror("CDROMEJECT");
1144 fd
= open(bs
->filename
, s
->fd_open_flags
| O_NONBLOCK
);
1146 if (ioctl(fd
, FDEJECT
, 0) < 0)
1158 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1160 BDRVRawState
*s
= bs
->opaque
;
1164 if (ioctl (s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1165 /* Note: an error can happen if the distribution automatically
1166 mounts the CD-ROM */
1167 // perror("CDROM_LOCKDOOR");
1176 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1178 BDRVRawState
*s
= bs
->opaque
;
1180 return ioctl(s
->fd
, req
, buf
);
1184 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1185 unsigned long int req
, void *buf
,
1186 BlockDriverCompletionFunc
*cb
, void *opaque
)
1188 BDRVRawState
*s
= bs
->opaque
;
1191 if (fd_open(bs
) < 0)
1194 acb
= qemu_aio_get(bs
, cb
, opaque
);
1197 acb
->aiocb
.aio_fildes
= s
->fd
;
1198 acb
->aiocb
.ev_signo
= SIGUSR2
;
1199 acb
->aiocb
.aio_offset
= 0;
1200 acb
->aiocb
.aio_flags
= 0;
1202 acb
->next
= posix_aio_state
->first_aio
;
1203 posix_aio_state
->first_aio
= acb
;
1205 acb
->aiocb
.aio_ioctl_buf
= buf
;
1206 acb
->aiocb
.aio_ioctl_cmd
= req
;
1207 if (qemu_paio_ioctl(&acb
->aiocb
) < 0) {
1208 raw_aio_remove(acb
);
1212 return &acb
->common
;
1216 #elif defined(__FreeBSD__)
1218 static int fd_open(BlockDriverState
*bs
)
1220 BDRVRawState
*s
= bs
->opaque
;
1222 /* this is just to ensure s->fd is sane (its called by io ops) */
1228 static int cd_open(BlockDriverState
*bs
)
1230 #if defined(__FreeBSD__)
1231 BDRVRawState
*s
= bs
->opaque
;
1236 /* XXX force reread of possibly changed/newly loaded disc,
1237 * FreeBSD seems to not notice sometimes... */
1240 fd
= open(bs
->filename
, s
->cd_open_flags
, 0644);
1246 /* make sure the door isnt locked at this time */
1247 ioctl (s
->fd
, CDIOCALLOW
);
1253 static int raw_is_inserted(BlockDriverState
*bs
)
1255 BDRVRawState
*s
= bs
->opaque
;
1259 return (raw_getlength(bs
) > 0);
1261 /* XXX handle this */
1268 static int raw_media_changed(BlockDriverState
*bs
)
1273 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1275 BDRVRawState
*s
= bs
->opaque
;
1281 (void) ioctl (s
->fd
, CDIOCALLOW
);
1283 if (ioctl (s
->fd
, CDIOCEJECT
) < 0)
1284 perror("CDIOCEJECT");
1286 if (ioctl (s
->fd
, CDIOCCLOSE
) < 0)
1287 perror("CDIOCCLOSE");
1289 if (cd_open(bs
) < 0)
1293 /* XXX handle this */
1301 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1303 BDRVRawState
*s
= bs
->opaque
;
1309 if (ioctl (s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1310 /* Note: an error can happen if the distribution automatically
1311 mounts the CD-ROM */
1312 // perror("CDROM_LOCKDOOR");
1321 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1325 #else /* !linux && !FreeBSD */
1327 static int fd_open(BlockDriverState
*bs
)
1332 static int raw_is_inserted(BlockDriverState
*bs
)
1337 static int raw_media_changed(BlockDriverState
*bs
)
1342 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
1347 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
1352 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1357 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
1358 unsigned long int req
, void *buf
,
1359 BlockDriverCompletionFunc
*cb
, void *opaque
)
1363 #endif /* !linux && !FreeBSD */
1365 #if defined(__linux__) || defined(__FreeBSD__)
1366 static int hdev_create(const char *filename
, int64_t total_size
,
1367 const char *backing_file
, int flags
)
1371 struct stat stat_buf
;
1373 if (flags
|| backing_file
)
1376 fd
= open(filename
, O_WRONLY
| O_BINARY
);
1380 if (fstat(fd
, &stat_buf
) < 0)
1382 else if (!S_ISBLK(stat_buf
.st_mode
))
1384 else if (lseek(fd
, 0, SEEK_END
) < total_size
* 512)
1391 #else /* !(linux || freebsd) */
1393 static int hdev_create(const char *filename
, int64_t total_size
,
1394 const char *backing_file
, int flags
)
1400 BlockDriver bdrv_host_device
= {
1401 .format_name
= "host_device",
1402 .instance_size
= sizeof(BDRVRawState
),
1403 .bdrv_open
= hdev_open
,
1404 .bdrv_close
= raw_close
,
1405 .bdrv_create
= hdev_create
,
1406 .bdrv_flush
= raw_flush
,
1409 .bdrv_aio_readv
= raw_aio_readv
,
1410 .bdrv_aio_writev
= raw_aio_writev
,
1411 .bdrv_aio_cancel
= raw_aio_cancel
,
1412 .aiocb_size
= sizeof(RawAIOCB
),
1415 .bdrv_read
= raw_read
,
1416 .bdrv_write
= raw_write
,
1417 .bdrv_getlength
= raw_getlength
,
1419 /* removable device support */
1420 .bdrv_is_inserted
= raw_is_inserted
,
1421 .bdrv_media_changed
= raw_media_changed
,
1422 .bdrv_eject
= raw_eject
,
1423 .bdrv_set_locked
= raw_set_locked
,
1424 /* generic scsi device */
1425 .bdrv_ioctl
= raw_ioctl
,
1427 .bdrv_aio_ioctl
= raw_aio_ioctl
,