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>
63 #include <sys/ioctl.h>
64 #include <sys/disklabel.h>
68 //#define DEBUG_FLOPPY
71 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
72 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
73 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
75 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
82 #define ALIGNED_BUFFER_SIZE (32 * 512)
84 /* if the FD is not accessed during that time (in ms), we try to
85 reopen it to see if the disk has been changed */
86 #define FD_OPEN_TIMEOUT 1000
88 typedef struct BDRVRawState {
91 unsigned int lseek_err_cnt;
92 #if defined(__linux__)
93 /* linux floppy specific */
96 int64_t fd_error_time;
100 #if defined(O_DIRECT) && !defined(QEMU_IMG)
101 uint8_t* aligned_buf;
105 static int fd_open(BlockDriverState *bs);
107 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
109 BDRVRawState *s = bs->opaque;
110 int fd, open_flags, ret;
112 s->lseek_err_cnt = 0;
114 open_flags = O_BINARY;
115 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
116 open_flags |= O_RDWR;
118 open_flags |= O_RDONLY;
121 if (flags & BDRV_O_CREAT)
122 open_flags |= O_CREAT | O_TRUNC;
124 if (flags & BDRV_O_DIRECT)
125 open_flags |= O_DIRECT;
128 s->type = FTYPE_FILE;
130 fd = open(filename, open_flags, 0644);
138 #if defined(O_DIRECT) && !defined(QEMU_IMG)
139 s->aligned_buf = NULL;
140 if (flags & BDRV_O_DIRECT) {
141 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
142 if (s->aligned_buf == NULL) {
152 /* XXX: use host sector size if necessary with:
153 #ifdef DIOCGSECTORSIZE
155 unsigned int sectorsize = 512;
156 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
157 sectorsize > bufsize)
158 bufsize = sectorsize;
162 u_int32_t blockSize = 512;
163 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
170 * offset and count are in bytes, but must be multiples of 512 for files
171 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
173 * This function may be called without alignment if the caller ensures
174 * that O_DIRECT is not in effect.
176 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
177 uint8_t *buf, int count)
179 BDRVRawState *s = bs->opaque;
186 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
187 ++(s->lseek_err_cnt);
188 if(s->lseek_err_cnt <= 10) {
189 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
190 "] lseek failed : %d = %s\n",
191 s->fd, bs->filename, offset, buf, count,
192 bs->total_sectors, errno, strerror(errno));
198 ret = read(s->fd, buf, count);
200 goto label__raw_read__success;
202 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
203 "] read failed %d : %d = %s\n",
204 s->fd, bs->filename, offset, buf, count,
205 bs->total_sectors, ret, errno, strerror(errno));
207 /* Try harder for CDrom. */
208 if (bs->type == BDRV_TYPE_CDROM) {
209 lseek(s->fd, offset, SEEK_SET);
210 ret = read(s->fd, buf, count);
212 goto label__raw_read__success;
213 lseek(s->fd, offset, SEEK_SET);
214 ret = read(s->fd, buf, count);
216 goto label__raw_read__success;
218 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
219 "] retry read failed %d : %d = %s\n",
220 s->fd, bs->filename, offset, buf, count,
221 bs->total_sectors, ret, errno, strerror(errno));
224 label__raw_read__success:
230 * offset and count are in bytes, but must be multiples of 512 for files
231 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
233 * This function may be called without alignment if the caller ensures
234 * that O_DIRECT is not in effect.
236 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
237 const uint8_t *buf, int count)
239 BDRVRawState *s = bs->opaque;
246 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
247 ++(s->lseek_err_cnt);
248 if(s->lseek_err_cnt) {
249 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
250 PRId64 "] lseek failed : %d = %s\n",
251 s->fd, bs->filename, offset, buf, count,
252 bs->total_sectors, errno, strerror(errno));
256 s->lseek_err_cnt = 0;
258 ret = write(s->fd, buf, count);
260 goto label__raw_write__success;
262 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
263 "] write failed %d : %d = %s\n",
264 s->fd, bs->filename, offset, buf, count,
265 bs->total_sectors, ret, errno, strerror(errno));
267 label__raw_write__success:
273 #if defined(O_DIRECT) && !defined(QEMU_IMG)
275 * offset and count are in bytes and possibly not aligned. For files opened
276 * with O_DIRECT, necessary alignments are ensured before calling
277 * raw_pread_aligned to do the actual read.
279 static int raw_pread(BlockDriverState *bs, int64_t offset,
280 uint8_t *buf, int count)
282 BDRVRawState *s = bs->opaque;
283 int size, ret, shift, sum;
287 if (s->aligned_buf != NULL) {
289 if (offset & 0x1ff) {
290 /* align offset on a 512 bytes boundary */
292 shift = offset & 0x1ff;
293 size = (shift + count + 0x1ff) & ~0x1ff;
294 if (size > ALIGNED_BUFFER_SIZE)
295 size = ALIGNED_BUFFER_SIZE;
296 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
303 memcpy(buf, s->aligned_buf + shift, size);
313 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
315 /* read on aligned buffer */
319 size = (count + 0x1ff) & ~0x1ff;
320 if (size > ALIGNED_BUFFER_SIZE)
321 size = ALIGNED_BUFFER_SIZE;
323 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
331 memcpy(buf, s->aligned_buf, size);
343 return raw_pread_aligned(bs, offset, buf, count) + sum;
347 * offset and count are in bytes and possibly not aligned. For files opened
348 * with O_DIRECT, necessary alignments are ensured before calling
349 * raw_pwrite_aligned to do the actual write.
351 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
352 const uint8_t *buf, int count)
354 BDRVRawState *s = bs->opaque;
355 int size, ret, shift, sum;
359 if (s->aligned_buf != NULL) {
361 if (offset & 0x1ff) {
362 /* align offset on a 512 bytes boundary */
363 shift = offset & 0x1ff;
364 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
371 memcpy(s->aligned_buf + shift, buf, size);
373 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
385 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
387 while ((size = (count & ~0x1ff)) != 0) {
389 if (size > ALIGNED_BUFFER_SIZE)
390 size = ALIGNED_BUFFER_SIZE;
392 memcpy(s->aligned_buf, buf, size);
394 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
403 /* here, count < 512 because (count & ~0x1ff) == 0 */
405 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
408 memcpy(s->aligned_buf, buf, count);
410 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
421 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
425 #define raw_pread raw_pread_aligned
426 #define raw_pwrite raw_pwrite_aligned
431 /***********************************************************/
432 /* Unix AIO using POSIX AIO */
434 typedef struct RawAIOCB {
435 BlockDriverAIOCB common;
437 struct RawAIOCB *next;
441 static int aio_sig_num = SIGUSR2;
442 static RawAIOCB *first_aio; /* AIO issued */
443 static int aio_initialized = 0;
445 static void aio_signal_handler(int signum)
447 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
448 CPUState *env = cpu_single_env;
450 /* stop the currently executing cpu because a timer occured */
451 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
453 if (env->kqemu_enabled) {
454 kqemu_cpu_interrupt(env);
461 void qemu_aio_init(void)
463 struct sigaction act;
467 sigfillset(&act.sa_mask);
468 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
469 act.sa_handler = aio_signal_handler;
470 sigaction(aio_sig_num, &act, NULL);
472 #if defined(__GLIBC__) && defined(__linux__)
474 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
475 seems to fix the problem. */
477 memset(&ai, 0, sizeof(ai));
480 ai.aio_idle_time = 365 * 100000;
486 void qemu_aio_poll(void)
488 RawAIOCB *acb, **pacb;
497 ret = aio_error(&acb->aiocb);
498 if (ret == ECANCELED) {
499 /* remove the request */
501 qemu_aio_release(acb);
502 } else if (ret != EINPROGRESS) {
505 ret = aio_return(&acb->aiocb);
506 if (ret == acb->aiocb.aio_nbytes)
513 /* remove the request */
515 /* call the callback */
516 acb->common.cb(acb->common.opaque, ret);
517 qemu_aio_release(acb);
527 /* Wait for all IO requests to complete. */
528 void qemu_aio_flush(void)
530 qemu_aio_wait_start();
538 /* wait until at least one AIO was handled */
539 static sigset_t wait_oset;
541 void qemu_aio_wait_start(void)
545 if (!aio_initialized)
548 sigaddset(&set, aio_sig_num);
549 sigprocmask(SIG_BLOCK, &set, &wait_oset);
552 void qemu_aio_wait(void)
557 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
562 sigaddset(&set, aio_sig_num);
563 sigwait(&set, &nb_sigs);
567 void qemu_aio_wait_end(void)
569 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
572 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
573 int64_t sector_num, uint8_t *buf, int nb_sectors,
574 BlockDriverCompletionFunc *cb, void *opaque)
576 BDRVRawState *s = bs->opaque;
582 acb = qemu_aio_get(bs, cb, opaque);
585 acb->aiocb.aio_fildes = s->fd;
586 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
587 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
588 acb->aiocb.aio_buf = buf;
590 acb->aiocb.aio_nbytes = -nb_sectors;
592 acb->aiocb.aio_nbytes = nb_sectors * 512;
593 acb->aiocb.aio_offset = sector_num * 512;
594 acb->next = first_aio;
599 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
600 static void raw_aio_em_cb(void* opaque)
602 RawAIOCB *acb = opaque;
603 acb->common.cb(acb->common.opaque, acb->ret);
604 qemu_aio_release(acb);
608 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
609 int64_t sector_num, uint8_t *buf, int nb_sectors,
610 BlockDriverCompletionFunc *cb, void *opaque)
615 * If O_DIRECT is used and the buffer is not aligned fall back
618 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
619 BDRVRawState *s = bs->opaque;
621 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
623 acb = qemu_aio_get(bs, cb, opaque);
624 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
625 bh = qemu_bh_new(raw_aio_em_cb, acb);
626 qemu_bh_schedule(bh);
631 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
634 if (aio_read(&acb->aiocb) < 0) {
635 qemu_aio_release(acb);
641 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
642 int64_t sector_num, const uint8_t *buf, int nb_sectors,
643 BlockDriverCompletionFunc *cb, void *opaque)
648 * If O_DIRECT is used and the buffer is not aligned fall back
651 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
652 BDRVRawState *s = bs->opaque;
654 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
656 acb = qemu_aio_get(bs, cb, opaque);
657 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
658 bh = qemu_bh_new(raw_aio_em_cb, acb);
659 qemu_bh_schedule(bh);
664 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
667 if (aio_write(&acb->aiocb) < 0) {
668 qemu_aio_release(acb);
674 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
677 RawAIOCB *acb = (RawAIOCB *)blockacb;
680 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
681 if (ret == AIO_NOTCANCELED) {
682 /* fail safe: if the aio could not be canceled, we wait for
684 while (aio_error(&acb->aiocb) == EINPROGRESS);
687 /* remove the callback from the queue */
692 } else if (*pacb == acb) {
694 qemu_aio_release(acb);
701 # else /* CONFIG_AIO */
703 void qemu_aio_init(void)
707 void qemu_aio_poll(void)
711 void qemu_aio_flush(void)
715 void qemu_aio_wait_start(void)
719 void qemu_aio_wait(void)
721 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
726 void qemu_aio_wait_end(void)
730 #endif /* CONFIG_AIO */
732 static void raw_close(BlockDriverState *bs)
734 BDRVRawState *s = bs->opaque;
738 #if defined(O_DIRECT) && !defined(QEMU_IMG)
739 if (s->aligned_buf != NULL)
740 qemu_free(s->aligned_buf);
745 static int raw_truncate(BlockDriverState *bs, int64_t offset)
747 BDRVRawState *s = bs->opaque;
748 if (s->type != FTYPE_FILE)
750 if (ftruncate(s->fd, offset) < 0)
756 static int64_t raw_getlength(BlockDriverState *bs)
758 BDRVRawState *s = bs->opaque;
764 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
767 if (ioctl(fd, DIOCGDINFO, &dl))
769 return (uint64_t)dl.d_secsize *
770 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
774 #else /* !__OpenBSD__ */
775 static int64_t raw_getlength(BlockDriverState *bs)
777 BDRVRawState *s = bs->opaque;
784 struct dk_minfo minfo;
794 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
795 #ifdef DIOCGMEDIASIZE
796 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
799 size = LONG_LONG_MAX;
801 size = lseek(fd, 0LL, SEEK_END);
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 = {
848 sizeof(BDRVRawState),
849 NULL, /* no probe for protocols */
858 .bdrv_aio_read = raw_aio_read,
859 .bdrv_aio_write = raw_aio_write,
860 .bdrv_aio_cancel = raw_aio_cancel,
861 .aiocb_size = sizeof(RawAIOCB),
863 .protocol_name = "file",
864 .bdrv_pread = raw_pread,
865 .bdrv_pwrite = raw_pwrite,
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;
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;
970 if (flags & BDRV_O_DIRECT)
971 open_flags |= O_DIRECT;
974 s->type = FTYPE_FILE;
975 #if defined(__linux__)
976 if (strstart(filename, "/dev/cd", NULL)) {
977 /* open will not fail even if no CD is inserted */
978 open_flags |= O_NONBLOCK;
980 } else if (strstart(filename, "/dev/fd", NULL)) {
982 s->fd_open_flags = open_flags;
983 /* open will not fail even if no floppy is inserted */
984 open_flags |= O_NONBLOCK;
985 } else if (strstart(filename, "/dev/sg", NULL)) {
989 fd = open(filename, open_flags, 0644);
997 #if defined(__linux__)
998 /* close fd so that we can reopen it as needed */
999 if (s->type == FTYPE_FD) {
1002 s->fd_media_changed = 1;
1008 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
1010 /* Note: we do not have a reliable method to detect if the floppy is
1011 present. The current method is to try to open the floppy at every
1012 I/O and to keep it opened during a few hundreds of ms. */
1013 static int fd_open(BlockDriverState *bs)
1015 BDRVRawState *s = bs->opaque;
1016 int last_media_present;
1018 if (s->type != FTYPE_FD)
1020 last_media_present = (s->fd >= 0);
1022 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1026 printf("Floppy closed\n");
1030 if (s->fd_got_error &&
1031 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1033 printf("No floppy (open delayed)\n");
1037 s->fd = open(bs->filename, s->fd_open_flags);
1039 s->fd_error_time = qemu_get_clock(rt_clock);
1040 s->fd_got_error = 1;
1041 if (last_media_present)
1042 s->fd_media_changed = 1;
1044 printf("No floppy\n");
1049 printf("Floppy opened\n");
1052 if (!last_media_present)
1053 s->fd_media_changed = 1;
1054 s->fd_open_time = qemu_get_clock(rt_clock);
1055 s->fd_got_error = 0;
1059 static int fd_open(BlockDriverState *bs)
1065 #if defined(__linux__)
1067 static int raw_is_inserted(BlockDriverState *bs)
1069 BDRVRawState *s = bs->opaque;
1074 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1075 if (ret == CDS_DISC_OK)
1088 /* currently only used by fdc.c, but a CD version would be good too */
1089 static int raw_media_changed(BlockDriverState *bs)
1091 BDRVRawState *s = bs->opaque;
1097 /* XXX: we do not have a true media changed indication. It
1098 does not work if the floppy is changed without trying
1101 ret = s->fd_media_changed;
1102 s->fd_media_changed = 0;
1104 printf("Floppy changed=%d\n", ret);
1113 static int raw_eject(BlockDriverState *bs, int eject_flag)
1115 BDRVRawState *s = bs->opaque;
1120 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1121 perror("CDROMEJECT");
1123 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1124 perror("CDROMEJECT");
1134 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1136 if (ioctl(fd, FDEJECT, 0) < 0)
1148 static int raw_set_locked(BlockDriverState *bs, int locked)
1150 BDRVRawState *s = bs->opaque;
1154 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1155 /* Note: an error can happen if the distribution automatically
1156 mounts the CD-ROM */
1157 // perror("CDROM_LOCKDOOR");
1166 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1168 BDRVRawState *s = bs->opaque;
1170 return ioctl(s->fd, req, buf);
1174 static int raw_is_inserted(BlockDriverState *bs)
1179 static int raw_media_changed(BlockDriverState *bs)
1184 static int raw_eject(BlockDriverState *bs, int eject_flag)
1189 static int raw_set_locked(BlockDriverState *bs, int locked)
1194 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1200 BlockDriver bdrv_host_device = {
1202 sizeof(BDRVRawState),
1203 NULL, /* no probe for protocols */
1212 .bdrv_aio_read = raw_aio_read,
1213 .bdrv_aio_write = raw_aio_write,
1214 .bdrv_aio_cancel = raw_aio_cancel,
1215 .aiocb_size = sizeof(RawAIOCB),
1217 .bdrv_pread = raw_pread,
1218 .bdrv_pwrite = raw_pwrite,
1219 .bdrv_getlength = raw_getlength,
1221 /* removable device support */
1222 .bdrv_is_inserted = raw_is_inserted,
1223 .bdrv_media_changed = raw_media_changed,
1224 .bdrv_eject = raw_eject,
1225 .bdrv_set_locked = raw_set_locked,
1226 /* generic scsi device */
1227 .bdrv_ioctl = raw_ioctl,