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 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
26 #include "qemu-timer.h"
29 #include "block_int.h"
37 #include <sys/param.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/IOBSD.h>
40 #include <IOKit/storage/IOMediaBSDClient.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <IOKit/storage/IOCDMedia.h>
43 //#include <IOKit/storage/IOCDTypes.h>
44 #include <CoreFoundation/CoreFoundation.h>
48 #define _POSIX_PTHREAD_SEMANTICS 1
53 #include <sys/ioctl.h>
54 #include <linux/cdrom.h>
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
67 //#define DEBUG_FLOPPY
70 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
71 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
72 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
74 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
81 #define ALIGNED_BUFFER_SIZE (32 * 512)
83 /* if the FD is not accessed during that time (in ms), we try to
84 reopen it to see if the disk has been changed */
85 #define FD_OPEN_TIMEOUT 1000
87 typedef struct BDRVRawState {
90 unsigned int lseek_err_cnt;
91 #if defined(__linux__)
92 /* linux floppy specific */
95 int64_t fd_error_time;
99 #if defined(O_DIRECT) && !defined(QEMU_IMG)
100 uint8_t* aligned_buf;
104 static int fd_open(BlockDriverState *bs);
106 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
108 BDRVRawState *s = bs->opaque;
109 int fd, open_flags, ret;
111 s->lseek_err_cnt = 0;
113 open_flags = O_BINARY;
114 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
115 open_flags |= O_RDWR;
117 open_flags |= O_RDONLY;
120 if (flags & BDRV_O_CREAT)
121 open_flags |= O_CREAT | O_TRUNC;
123 if (flags & BDRV_O_DIRECT)
124 open_flags |= O_DIRECT;
127 s->type = FTYPE_FILE;
129 fd = open(filename, open_flags, 0644);
137 #if defined(O_DIRECT) && !defined(QEMU_IMG)
138 s->aligned_buf = NULL;
139 if (flags & BDRV_O_DIRECT) {
140 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
141 if (s->aligned_buf == NULL) {
151 /* XXX: use host sector size if necessary with:
152 #ifdef DIOCGSECTORSIZE
154 unsigned int sectorsize = 512;
155 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
156 sectorsize > bufsize)
157 bufsize = sectorsize;
161 u_int32_t blockSize = 512;
162 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
169 * offset and count are in bytes, but must be multiples of 512 for files
170 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
172 * This function may be called without alignment if the caller ensures
173 * that O_DIRECT is not in effect.
175 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
176 uint8_t *buf, int count)
178 BDRVRawState *s = bs->opaque;
185 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
186 ++(s->lseek_err_cnt);
187 if(s->lseek_err_cnt <= 10) {
188 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
189 "] lseek failed : %d = %s\n",
190 s->fd, bs->filename, offset, buf, count,
191 bs->total_sectors, errno, strerror(errno));
197 ret = read(s->fd, buf, count);
199 goto label__raw_read__success;
201 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
202 "] read failed %d : %d = %s\n",
203 s->fd, bs->filename, offset, buf, count,
204 bs->total_sectors, ret, errno, strerror(errno));
206 /* Try harder for CDrom. */
207 if (bs->type == BDRV_TYPE_CDROM) {
208 lseek(s->fd, offset, SEEK_SET);
209 ret = read(s->fd, buf, count);
211 goto label__raw_read__success;
212 lseek(s->fd, offset, SEEK_SET);
213 ret = read(s->fd, buf, count);
215 goto label__raw_read__success;
217 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
218 "] retry read failed %d : %d = %s\n",
219 s->fd, bs->filename, offset, buf, count,
220 bs->total_sectors, ret, errno, strerror(errno));
223 label__raw_read__success:
229 * offset and count are in bytes, but must be multiples of 512 for files
230 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
232 * This function may be called without alignment if the caller ensures
233 * that O_DIRECT is not in effect.
235 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
236 const uint8_t *buf, int count)
238 BDRVRawState *s = bs->opaque;
245 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
246 ++(s->lseek_err_cnt);
247 if(s->lseek_err_cnt) {
248 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
249 PRId64 "] lseek failed : %d = %s\n",
250 s->fd, bs->filename, offset, buf, count,
251 bs->total_sectors, errno, strerror(errno));
255 s->lseek_err_cnt = 0;
257 ret = write(s->fd, buf, count);
259 goto label__raw_write__success;
261 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
262 "] write failed %d : %d = %s\n",
263 s->fd, bs->filename, offset, buf, count,
264 bs->total_sectors, ret, errno, strerror(errno));
266 label__raw_write__success:
272 #if defined(O_DIRECT) && !defined(QEMU_IMG)
274 * offset and count are in bytes and possibly not aligned. For files opened
275 * with O_DIRECT, necessary alignments are ensured before calling
276 * raw_pread_aligned to do the actual read.
278 static int raw_pread(BlockDriverState *bs, int64_t offset,
279 uint8_t *buf, int count)
281 BDRVRawState *s = bs->opaque;
282 int size, ret, shift, sum;
286 if (s->aligned_buf != NULL) {
288 if (offset & 0x1ff) {
289 /* align offset on a 512 bytes boundary */
291 shift = offset & 0x1ff;
292 size = (shift + count + 0x1ff) & ~0x1ff;
293 if (size > ALIGNED_BUFFER_SIZE)
294 size = ALIGNED_BUFFER_SIZE;
295 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
302 memcpy(buf, s->aligned_buf + shift, size);
312 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
314 /* read on aligned buffer */
318 size = (count + 0x1ff) & ~0x1ff;
319 if (size > ALIGNED_BUFFER_SIZE)
320 size = ALIGNED_BUFFER_SIZE;
322 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
330 memcpy(buf, s->aligned_buf, size);
342 return raw_pread_aligned(bs, offset, buf, count) + sum;
346 * offset and count are in bytes and possibly not aligned. For files opened
347 * with O_DIRECT, necessary alignments are ensured before calling
348 * raw_pwrite_aligned to do the actual write.
350 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
351 const uint8_t *buf, int count)
353 BDRVRawState *s = bs->opaque;
354 int size, ret, shift, sum;
358 if (s->aligned_buf != NULL) {
360 if (offset & 0x1ff) {
361 /* align offset on a 512 bytes boundary */
362 shift = offset & 0x1ff;
363 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
370 memcpy(s->aligned_buf + shift, buf, size);
372 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
384 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
386 while ((size = (count & ~0x1ff)) != 0) {
388 if (size > ALIGNED_BUFFER_SIZE)
389 size = ALIGNED_BUFFER_SIZE;
391 memcpy(s->aligned_buf, buf, size);
393 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
402 /* here, count < 512 because (count & ~0x1ff) == 0 */
404 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
407 memcpy(s->aligned_buf, buf, count);
409 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
420 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
424 #define raw_pread raw_pread_aligned
425 #define raw_pwrite raw_pwrite_aligned
430 /***********************************************************/
431 /* Unix AIO using POSIX AIO */
433 typedef struct RawAIOCB {
434 BlockDriverAIOCB common;
436 struct RawAIOCB *next;
440 static int aio_sig_num = SIGUSR2;
441 static RawAIOCB *first_aio; /* AIO issued */
442 static int aio_initialized = 0;
444 static void aio_signal_handler(int signum)
446 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
447 CPUState *env = cpu_single_env;
449 /* stop the currently executing cpu because a timer occured */
450 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
452 if (env->kqemu_enabled) {
453 kqemu_cpu_interrupt(env);
460 void qemu_aio_init(void)
462 struct sigaction act;
466 sigfillset(&act.sa_mask);
467 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
468 act.sa_handler = aio_signal_handler;
469 sigaction(aio_sig_num, &act, NULL);
471 #if defined(__GLIBC__) && defined(__linux__)
473 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
474 seems to fix the problem. */
476 memset(&ai, 0, sizeof(ai));
479 ai.aio_idle_time = 365 * 100000;
485 void qemu_aio_poll(void)
487 RawAIOCB *acb, **pacb;
496 ret = aio_error(&acb->aiocb);
497 if (ret == ECANCELED) {
498 /* remove the request */
500 qemu_aio_release(acb);
501 } else if (ret != EINPROGRESS) {
504 ret = aio_return(&acb->aiocb);
505 if (ret == acb->aiocb.aio_nbytes)
512 /* remove the request */
514 /* call the callback */
515 acb->common.cb(acb->common.opaque, ret);
516 qemu_aio_release(acb);
526 /* Wait for all IO requests to complete. */
527 void qemu_aio_flush(void)
529 qemu_aio_wait_start();
537 /* wait until at least one AIO was handled */
538 static sigset_t wait_oset;
540 void qemu_aio_wait_start(void)
544 if (!aio_initialized)
547 sigaddset(&set, aio_sig_num);
548 sigprocmask(SIG_BLOCK, &set, &wait_oset);
551 void qemu_aio_wait(void)
556 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
561 sigaddset(&set, aio_sig_num);
562 sigwait(&set, &nb_sigs);
566 void qemu_aio_wait_end(void)
568 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
571 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
572 int64_t sector_num, uint8_t *buf, int nb_sectors,
573 BlockDriverCompletionFunc *cb, void *opaque)
575 BDRVRawState *s = bs->opaque;
581 acb = qemu_aio_get(bs, cb, opaque);
584 acb->aiocb.aio_fildes = s->fd;
585 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
586 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
587 acb->aiocb.aio_buf = buf;
589 acb->aiocb.aio_nbytes = -nb_sectors;
591 acb->aiocb.aio_nbytes = nb_sectors * 512;
592 acb->aiocb.aio_offset = sector_num * 512;
593 acb->next = first_aio;
598 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
599 static void raw_aio_em_cb(void* opaque)
601 RawAIOCB *acb = opaque;
602 acb->common.cb(acb->common.opaque, acb->ret);
603 qemu_aio_release(acb);
607 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
608 int64_t sector_num, uint8_t *buf, int nb_sectors,
609 BlockDriverCompletionFunc *cb, void *opaque)
614 * If O_DIRECT is used and the buffer is not aligned fall back
617 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
618 BDRVRawState *s = bs->opaque;
620 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
622 acb = qemu_aio_get(bs, cb, opaque);
623 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
624 bh = qemu_bh_new(raw_aio_em_cb, acb);
625 qemu_bh_schedule(bh);
630 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
633 if (aio_read(&acb->aiocb) < 0) {
634 qemu_aio_release(acb);
640 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
641 int64_t sector_num, const uint8_t *buf, int nb_sectors,
642 BlockDriverCompletionFunc *cb, void *opaque)
647 * If O_DIRECT is used and the buffer is not aligned fall back
650 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
651 BDRVRawState *s = bs->opaque;
653 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
655 acb = qemu_aio_get(bs, cb, opaque);
656 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
657 bh = qemu_bh_new(raw_aio_em_cb, acb);
658 qemu_bh_schedule(bh);
663 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
666 if (aio_write(&acb->aiocb) < 0) {
667 qemu_aio_release(acb);
673 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
676 RawAIOCB *acb = (RawAIOCB *)blockacb;
679 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
680 if (ret == AIO_NOTCANCELED) {
681 /* fail safe: if the aio could not be canceled, we wait for
683 while (aio_error(&acb->aiocb) == EINPROGRESS);
686 /* remove the callback from the queue */
691 } else if (*pacb == acb) {
693 qemu_aio_release(acb);
700 # else /* CONFIG_AIO */
702 void qemu_aio_init(void)
706 void qemu_aio_poll(void)
710 void qemu_aio_flush(void)
714 void qemu_aio_wait_start(void)
718 void qemu_aio_wait(void)
720 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
725 void qemu_aio_wait_end(void)
729 #endif /* CONFIG_AIO */
731 static void raw_close(BlockDriverState *bs)
733 BDRVRawState *s = bs->opaque;
737 #if defined(O_DIRECT) && !defined(QEMU_IMG)
738 if (s->aligned_buf != NULL)
739 qemu_free(s->aligned_buf);
744 static int raw_truncate(BlockDriverState *bs, int64_t offset)
746 BDRVRawState *s = bs->opaque;
747 if (s->type != FTYPE_FILE)
749 if (ftruncate(s->fd, offset) < 0)
755 static int64_t raw_getlength(BlockDriverState *bs)
757 BDRVRawState *s = bs->opaque;
763 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
766 if (ioctl(fd, DIOCGDINFO, &dl))
768 return (uint64_t)dl.d_secsize *
769 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
773 #else /* !__OpenBSD__ */
774 static int64_t raw_getlength(BlockDriverState *bs)
776 BDRVRawState *s = bs->opaque;
783 struct dk_minfo minfo;
793 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
794 #ifdef DIOCGMEDIASIZE
795 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
798 size = LONG_LONG_MAX;
800 size = lseek(fd, 0LL, SEEK_END);
806 * use the DKIOCGMEDIAINFO ioctl to read the size.
808 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
810 size = minfo.dki_lbsize * minfo.dki_capacity;
811 } else /* there are reports that lseek on some devices
812 fails, but irc discussion said that contingency
813 on contingency was overkill */
816 size = lseek(fd, 0, SEEK_END);
822 static int raw_create(const char *filename, int64_t total_size,
823 const char *backing_file, int flags)
827 if (flags || backing_file)
830 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
834 ftruncate(fd, total_size * 512);
839 static void raw_flush(BlockDriverState *bs)
841 BDRVRawState *s = bs->opaque;
845 BlockDriver bdrv_raw = {
847 sizeof(BDRVRawState),
848 NULL, /* no probe for protocols */
857 .bdrv_aio_read = raw_aio_read,
858 .bdrv_aio_write = raw_aio_write,
859 .bdrv_aio_cancel = raw_aio_cancel,
860 .aiocb_size = sizeof(RawAIOCB),
862 .protocol_name = "file",
863 .bdrv_pread = raw_pread,
864 .bdrv_pwrite = raw_pwrite,
865 .bdrv_truncate = raw_truncate,
866 .bdrv_getlength = raw_getlength,
869 /***********************************************/
873 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
874 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
876 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
878 kern_return_t kernResult;
879 mach_port_t masterPort;
880 CFMutableDictionaryRef classesToMatch;
882 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
883 if ( KERN_SUCCESS != kernResult ) {
884 printf( "IOMasterPort returned %d\n", kernResult );
887 classesToMatch = IOServiceMatching( kIOCDMediaClass );
888 if ( classesToMatch == NULL ) {
889 printf( "IOServiceMatching returned a NULL dictionary.\n" );
891 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
893 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
894 if ( KERN_SUCCESS != kernResult )
896 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
902 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
904 io_object_t nextMedia;
905 kern_return_t kernResult = KERN_FAILURE;
907 nextMedia = IOIteratorNext( mediaIterator );
910 CFTypeRef bsdPathAsCFString;
911 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
912 if ( bsdPathAsCFString ) {
913 size_t devPathLength;
914 strcpy( bsdPath, _PATH_DEV );
915 strcat( bsdPath, "r" );
916 devPathLength = strlen( bsdPath );
917 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
918 kernResult = KERN_SUCCESS;
920 CFRelease( bsdPathAsCFString );
922 IOObjectRelease( nextMedia );
930 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
932 BDRVRawState *s = bs->opaque;
933 int fd, open_flags, ret;
936 if (strstart(filename, "/dev/cdrom", NULL)) {
937 kern_return_t kernResult;
938 io_iterator_t mediaIterator;
939 char bsdPath[ MAXPATHLEN ];
942 kernResult = FindEjectableCDMedia( &mediaIterator );
943 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
945 if ( bsdPath[ 0 ] != '\0' ) {
946 strcat(bsdPath,"s0");
947 /* some CDs don't have a partition 0 */
948 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
950 bsdPath[strlen(bsdPath)-1] = '1';
958 IOObjectRelease( mediaIterator );
961 open_flags = O_BINARY;
962 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
963 open_flags |= O_RDWR;
965 open_flags |= O_RDONLY;
969 if (flags & BDRV_O_DIRECT)
970 open_flags |= O_DIRECT;
973 s->type = FTYPE_FILE;
974 #if defined(__linux__)
975 if (strstart(filename, "/dev/cd", NULL)) {
976 /* open will not fail even if no CD is inserted */
977 open_flags |= O_NONBLOCK;
979 } else if (strstart(filename, "/dev/fd", NULL)) {
981 s->fd_open_flags = open_flags;
982 /* open will not fail even if no floppy is inserted */
983 open_flags |= O_NONBLOCK;
984 } else if (strstart(filename, "/dev/sg", NULL)) {
988 fd = open(filename, open_flags, 0644);
996 #if defined(__linux__)
997 /* close fd so that we can reopen it as needed */
998 if (s->type == FTYPE_FD) {
1001 s->fd_media_changed = 1;
1007 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1009 /* Note: we do not have a reliable method to detect if the floppy is
1010 present. The current method is to try to open the floppy at every
1011 I/O and to keep it opened during a few hundreds of ms. */
1012 static int fd_open(BlockDriverState *bs)
1014 BDRVRawState *s = bs->opaque;
1015 int last_media_present;
1017 if (s->type != FTYPE_FD)
1019 last_media_present = (s->fd >= 0);
1021 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1025 printf("Floppy closed\n");
1029 if (s->fd_got_error &&
1030 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1032 printf("No floppy (open delayed)\n");
1036 s->fd = open(bs->filename, s->fd_open_flags);
1038 s->fd_error_time = qemu_get_clock(rt_clock);
1039 s->fd_got_error = 1;
1040 if (last_media_present)
1041 s->fd_media_changed = 1;
1043 printf("No floppy\n");
1048 printf("Floppy opened\n");
1051 if (!last_media_present)
1052 s->fd_media_changed = 1;
1053 s->fd_open_time = qemu_get_clock(rt_clock);
1054 s->fd_got_error = 0;
1058 static int fd_open(BlockDriverState *bs)
1064 #if defined(__linux__)
1066 static int raw_is_inserted(BlockDriverState *bs)
1068 BDRVRawState *s = bs->opaque;
1073 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1074 if (ret == CDS_DISC_OK)
1087 /* currently only used by fdc.c, but a CD version would be good too */
1088 static int raw_media_changed(BlockDriverState *bs)
1090 BDRVRawState *s = bs->opaque;
1096 /* XXX: we do not have a true media changed indication. It
1097 does not work if the floppy is changed without trying
1100 ret = s->fd_media_changed;
1101 s->fd_media_changed = 0;
1103 printf("Floppy changed=%d\n", ret);
1112 static int raw_eject(BlockDriverState *bs, int eject_flag)
1114 BDRVRawState *s = bs->opaque;
1119 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1120 perror("CDROMEJECT");
1122 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1123 perror("CDROMEJECT");
1133 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1135 if (ioctl(fd, FDEJECT, 0) < 0)
1147 static int raw_set_locked(BlockDriverState *bs, int locked)
1149 BDRVRawState *s = bs->opaque;
1153 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1154 /* Note: an error can happen if the distribution automatically
1155 mounts the CD-ROM */
1156 // perror("CDROM_LOCKDOOR");
1165 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1167 BDRVRawState *s = bs->opaque;
1169 return ioctl(s->fd, req, buf);
1173 static int raw_is_inserted(BlockDriverState *bs)
1178 static int raw_media_changed(BlockDriverState *bs)
1183 static int raw_eject(BlockDriverState *bs, int eject_flag)
1188 static int raw_set_locked(BlockDriverState *bs, int locked)
1193 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1199 BlockDriver bdrv_host_device = {
1201 sizeof(BDRVRawState),
1202 NULL, /* no probe for protocols */
1211 .bdrv_aio_read = raw_aio_read,
1212 .bdrv_aio_write = raw_aio_write,
1213 .bdrv_aio_cancel = raw_aio_cancel,
1214 .aiocb_size = sizeof(RawAIOCB),
1216 .bdrv_pread = raw_pread,
1217 .bdrv_pwrite = raw_pwrite,
1218 .bdrv_getlength = raw_getlength,
1220 /* removable device support */
1221 .bdrv_is_inserted = raw_is_inserted,
1222 .bdrv_media_changed = raw_media_changed,
1223 .bdrv_eject = raw_eject,
1224 .bdrv_set_locked = raw_set_locked,
1225 /* generic scsi device */
1226 .bdrv_ioctl = raw_ioctl,