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>
61 //#define DEBUG_FLOPPY
64 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
65 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
66 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
68 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
75 #define ALIGNED_BUFFER_SIZE (32 * 512)
77 /* if the FD is not accessed during that time (in ms), we try to
78 reopen it to see if the disk has been changed */
79 #define FD_OPEN_TIMEOUT 1000
81 typedef struct BDRVRawState {
84 unsigned int lseek_err_cnt;
85 #if defined(__linux__)
86 /* linux floppy specific */
89 int64_t fd_error_time;
93 #if defined(O_DIRECT) && !defined(QEMU_IMG)
98 static int fd_open(BlockDriverState *bs);
100 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
102 BDRVRawState *s = bs->opaque;
103 int fd, open_flags, ret;
105 s->lseek_err_cnt = 0;
107 open_flags = O_BINARY;
108 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
109 open_flags |= O_RDWR;
111 open_flags |= O_RDONLY;
114 if (flags & BDRV_O_CREAT)
115 open_flags |= O_CREAT | O_TRUNC;
117 if (flags & BDRV_O_DIRECT)
118 open_flags |= O_DIRECT;
121 s->type = FTYPE_FILE;
123 fd = open(filename, open_flags, 0644);
131 #if defined(O_DIRECT) && !defined(QEMU_IMG)
132 s->aligned_buf = NULL;
133 if (flags & BDRV_O_DIRECT) {
134 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
135 if (s->aligned_buf == NULL) {
145 /* XXX: use host sector size if necessary with:
146 #ifdef DIOCGSECTORSIZE
148 unsigned int sectorsize = 512;
149 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
150 sectorsize > bufsize)
151 bufsize = sectorsize;
155 u_int32_t blockSize = 512;
156 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
163 * offset and count are in bytes, but must be multiples of 512 for files
164 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
166 * This function may be called without alignment if the caller ensures
167 * that O_DIRECT is not in effect.
169 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
170 uint8_t *buf, int count)
172 BDRVRawState *s = bs->opaque;
179 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
180 ++(s->lseek_err_cnt);
181 if(s->lseek_err_cnt <= 10) {
182 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
183 "] lseek failed : %d = %s\n",
184 s->fd, bs->filename, offset, buf, count,
185 bs->total_sectors, errno, strerror(errno));
191 ret = read(s->fd, buf, count);
193 goto label__raw_read__success;
195 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
196 "] read failed %d : %d = %s\n",
197 s->fd, bs->filename, offset, buf, count,
198 bs->total_sectors, ret, errno, strerror(errno));
200 /* Try harder for CDrom. */
201 if (bs->type == BDRV_TYPE_CDROM) {
202 lseek(s->fd, offset, SEEK_SET);
203 ret = read(s->fd, buf, count);
205 goto label__raw_read__success;
206 lseek(s->fd, offset, SEEK_SET);
207 ret = read(s->fd, buf, count);
209 goto label__raw_read__success;
211 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
212 "] retry read failed %d : %d = %s\n",
213 s->fd, bs->filename, offset, buf, count,
214 bs->total_sectors, ret, errno, strerror(errno));
217 label__raw_read__success:
223 * offset and count are in bytes, but must be multiples of 512 for files
224 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
226 * This function may be called without alignment if the caller ensures
227 * that O_DIRECT is not in effect.
229 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
230 const uint8_t *buf, int count)
232 BDRVRawState *s = bs->opaque;
239 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
240 ++(s->lseek_err_cnt);
241 if(s->lseek_err_cnt) {
242 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
243 PRId64 "] lseek failed : %d = %s\n",
244 s->fd, bs->filename, offset, buf, count,
245 bs->total_sectors, errno, strerror(errno));
249 s->lseek_err_cnt = 0;
251 ret = write(s->fd, buf, count);
253 goto label__raw_write__success;
255 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
256 "] write failed %d : %d = %s\n",
257 s->fd, bs->filename, offset, buf, count,
258 bs->total_sectors, ret, errno, strerror(errno));
260 label__raw_write__success:
266 #if defined(O_DIRECT) && !defined(QEMU_IMG)
268 * offset and count are in bytes and possibly not aligned. For files opened
269 * with O_DIRECT, necessary alignments are ensured before calling
270 * raw_pread_aligned to do the actual read.
272 static int raw_pread(BlockDriverState *bs, int64_t offset,
273 uint8_t *buf, int count)
275 BDRVRawState *s = bs->opaque;
276 int size, ret, shift, sum;
280 if (s->aligned_buf != NULL) {
282 if (offset & 0x1ff) {
283 /* align offset on a 512 bytes boundary */
285 shift = offset & 0x1ff;
286 size = (shift + count + 0x1ff) & ~0x1ff;
287 if (size > ALIGNED_BUFFER_SIZE)
288 size = ALIGNED_BUFFER_SIZE;
289 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
296 memcpy(buf, s->aligned_buf + shift, size);
306 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
308 /* read on aligned buffer */
312 size = (count + 0x1ff) & ~0x1ff;
313 if (size > ALIGNED_BUFFER_SIZE)
314 size = ALIGNED_BUFFER_SIZE;
316 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
324 memcpy(buf, s->aligned_buf, size);
336 return raw_pread_aligned(bs, offset, buf, count) + sum;
340 * offset and count are in bytes and possibly not aligned. For files opened
341 * with O_DIRECT, necessary alignments are ensured before calling
342 * raw_pwrite_aligned to do the actual write.
344 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
345 const uint8_t *buf, int count)
347 BDRVRawState *s = bs->opaque;
348 int size, ret, shift, sum;
352 if (s->aligned_buf != NULL) {
354 if (offset & 0x1ff) {
355 /* align offset on a 512 bytes boundary */
356 shift = offset & 0x1ff;
357 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
364 memcpy(s->aligned_buf + shift, buf, size);
366 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
378 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
380 while ((size = (count & ~0x1ff)) != 0) {
382 if (size > ALIGNED_BUFFER_SIZE)
383 size = ALIGNED_BUFFER_SIZE;
385 memcpy(s->aligned_buf, buf, size);
387 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
396 /* here, count < 512 because (count & ~0x1ff) == 0 */
398 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
401 memcpy(s->aligned_buf, buf, count);
403 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
414 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
418 #define raw_pread raw_pread_aligned
419 #define raw_pwrite raw_pwrite_aligned
424 /***********************************************************/
425 /* Unix AIO using POSIX AIO */
427 typedef struct RawAIOCB {
428 BlockDriverAIOCB common;
430 struct RawAIOCB *next;
434 static int aio_sig_num = SIGUSR2;
435 static RawAIOCB *first_aio; /* AIO issued */
436 static int aio_initialized = 0;
438 static void aio_signal_handler(int signum)
440 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
441 CPUState *env = cpu_single_env;
443 /* stop the currently executing cpu because a timer occured */
444 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
446 if (env->kqemu_enabled) {
447 kqemu_cpu_interrupt(env);
454 void qemu_aio_init(void)
456 struct sigaction act;
460 sigfillset(&act.sa_mask);
461 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
462 act.sa_handler = aio_signal_handler;
463 sigaction(aio_sig_num, &act, NULL);
465 #if defined(__GLIBC__) && defined(__linux__)
467 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
468 seems to fix the problem. */
470 memset(&ai, 0, sizeof(ai));
473 ai.aio_idle_time = 365 * 100000;
479 void qemu_aio_poll(void)
481 RawAIOCB *acb, **pacb;
490 ret = aio_error(&acb->aiocb);
491 if (ret == ECANCELED) {
492 /* remove the request */
494 qemu_aio_release(acb);
495 } else if (ret != EINPROGRESS) {
498 ret = aio_return(&acb->aiocb);
499 if (ret == acb->aiocb.aio_nbytes)
506 /* remove the request */
508 /* call the callback */
509 acb->common.cb(acb->common.opaque, ret);
510 qemu_aio_release(acb);
520 /* Wait for all IO requests to complete. */
521 void qemu_aio_flush(void)
523 qemu_aio_wait_start();
531 /* wait until at least one AIO was handled */
532 static sigset_t wait_oset;
534 void qemu_aio_wait_start(void)
538 if (!aio_initialized)
541 sigaddset(&set, aio_sig_num);
542 sigprocmask(SIG_BLOCK, &set, &wait_oset);
545 void qemu_aio_wait(void)
550 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
555 sigaddset(&set, aio_sig_num);
556 sigwait(&set, &nb_sigs);
560 void qemu_aio_wait_end(void)
562 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
565 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
566 int64_t sector_num, uint8_t *buf, int nb_sectors,
567 BlockDriverCompletionFunc *cb, void *opaque)
569 BDRVRawState *s = bs->opaque;
575 acb = qemu_aio_get(bs, cb, opaque);
578 acb->aiocb.aio_fildes = s->fd;
579 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
580 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
581 acb->aiocb.aio_buf = buf;
583 acb->aiocb.aio_nbytes = -nb_sectors;
585 acb->aiocb.aio_nbytes = nb_sectors * 512;
586 acb->aiocb.aio_offset = sector_num * 512;
587 acb->next = first_aio;
592 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
593 static void raw_aio_em_cb(void* opaque)
595 RawAIOCB *acb = opaque;
596 acb->common.cb(acb->common.opaque, acb->ret);
597 qemu_aio_release(acb);
601 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
602 int64_t sector_num, uint8_t *buf, int nb_sectors,
603 BlockDriverCompletionFunc *cb, void *opaque)
608 * If O_DIRECT is used and the buffer is not aligned fall back
611 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
612 BDRVRawState *s = bs->opaque;
614 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
616 acb = qemu_aio_get(bs, cb, opaque);
617 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
618 bh = qemu_bh_new(raw_aio_em_cb, acb);
619 qemu_bh_schedule(bh);
624 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
627 if (aio_read(&acb->aiocb) < 0) {
628 qemu_aio_release(acb);
634 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
635 int64_t sector_num, const uint8_t *buf, int nb_sectors,
636 BlockDriverCompletionFunc *cb, void *opaque)
641 * If O_DIRECT is used and the buffer is not aligned fall back
644 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
645 BDRVRawState *s = bs->opaque;
647 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
649 acb = qemu_aio_get(bs, cb, opaque);
650 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
651 bh = qemu_bh_new(raw_aio_em_cb, acb);
652 qemu_bh_schedule(bh);
657 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
660 if (aio_write(&acb->aiocb) < 0) {
661 qemu_aio_release(acb);
667 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
670 RawAIOCB *acb = (RawAIOCB *)blockacb;
673 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
674 if (ret == AIO_NOTCANCELED) {
675 /* fail safe: if the aio could not be canceled, we wait for
677 while (aio_error(&acb->aiocb) == EINPROGRESS);
680 /* remove the callback from the queue */
685 } else if (*pacb == acb) {
687 qemu_aio_release(acb);
694 # else /* CONFIG_AIO */
696 void qemu_aio_init(void)
700 void qemu_aio_poll(void)
704 void qemu_aio_flush(void)
708 void qemu_aio_wait_start(void)
712 void qemu_aio_wait(void)
714 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
719 void qemu_aio_wait_end(void)
723 #endif /* CONFIG_AIO */
725 static void raw_close(BlockDriverState *bs)
727 BDRVRawState *s = bs->opaque;
731 #if defined(O_DIRECT) && !defined(QEMU_IMG)
732 if (s->aligned_buf != NULL)
733 qemu_free(s->aligned_buf);
738 static int raw_truncate(BlockDriverState *bs, int64_t offset)
740 BDRVRawState *s = bs->opaque;
741 if (s->type != FTYPE_FILE)
743 if (ftruncate(s->fd, offset) < 0)
748 static int64_t raw_getlength(BlockDriverState *bs)
750 BDRVRawState *s = bs->opaque;
757 struct dk_minfo minfo;
767 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
768 #ifdef DIOCGMEDIASIZE
769 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
772 size = LONG_LONG_MAX;
774 size = lseek(fd, 0LL, SEEK_END);
780 * use the DKIOCGMEDIAINFO ioctl to read the size.
782 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
784 size = minfo.dki_lbsize * minfo.dki_capacity;
785 } else /* there are reports that lseek on some devices
786 fails, but irc discussion said that contingency
787 on contingency was overkill */
790 size = lseek(fd, 0, SEEK_END);
795 static int raw_create(const char *filename, int64_t total_size,
796 const char *backing_file, int flags)
800 if (flags || backing_file)
803 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
807 ftruncate(fd, total_size * 512);
812 static void raw_flush(BlockDriverState *bs)
814 BDRVRawState *s = bs->opaque;
818 BlockDriver bdrv_raw = {
820 sizeof(BDRVRawState),
821 NULL, /* no probe for protocols */
830 .bdrv_aio_read = raw_aio_read,
831 .bdrv_aio_write = raw_aio_write,
832 .bdrv_aio_cancel = raw_aio_cancel,
833 .aiocb_size = sizeof(RawAIOCB),
835 .protocol_name = "file",
836 .bdrv_pread = raw_pread,
837 .bdrv_pwrite = raw_pwrite,
838 .bdrv_truncate = raw_truncate,
839 .bdrv_getlength = raw_getlength,
842 /***********************************************/
846 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
847 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
849 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
851 kern_return_t kernResult;
852 mach_port_t masterPort;
853 CFMutableDictionaryRef classesToMatch;
855 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
856 if ( KERN_SUCCESS != kernResult ) {
857 printf( "IOMasterPort returned %d\n", kernResult );
860 classesToMatch = IOServiceMatching( kIOCDMediaClass );
861 if ( classesToMatch == NULL ) {
862 printf( "IOServiceMatching returned a NULL dictionary.\n" );
864 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
866 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
867 if ( KERN_SUCCESS != kernResult )
869 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
875 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
877 io_object_t nextMedia;
878 kern_return_t kernResult = KERN_FAILURE;
880 nextMedia = IOIteratorNext( mediaIterator );
883 CFTypeRef bsdPathAsCFString;
884 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
885 if ( bsdPathAsCFString ) {
886 size_t devPathLength;
887 strcpy( bsdPath, _PATH_DEV );
888 strcat( bsdPath, "r" );
889 devPathLength = strlen( bsdPath );
890 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
891 kernResult = KERN_SUCCESS;
893 CFRelease( bsdPathAsCFString );
895 IOObjectRelease( nextMedia );
903 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
905 BDRVRawState *s = bs->opaque;
906 int fd, open_flags, ret;
909 if (strstart(filename, "/dev/cdrom", NULL)) {
910 kern_return_t kernResult;
911 io_iterator_t mediaIterator;
912 char bsdPath[ MAXPATHLEN ];
915 kernResult = FindEjectableCDMedia( &mediaIterator );
916 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
918 if ( bsdPath[ 0 ] != '\0' ) {
919 strcat(bsdPath,"s0");
920 /* some CDs don't have a partition 0 */
921 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
923 bsdPath[strlen(bsdPath)-1] = '1';
931 IOObjectRelease( mediaIterator );
934 open_flags = O_BINARY;
935 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
936 open_flags |= O_RDWR;
938 open_flags |= O_RDONLY;
942 if (flags & BDRV_O_DIRECT)
943 open_flags |= O_DIRECT;
946 s->type = FTYPE_FILE;
947 #if defined(__linux__)
948 if (strstart(filename, "/dev/cd", NULL)) {
949 /* open will not fail even if no CD is inserted */
950 open_flags |= O_NONBLOCK;
952 } else if (strstart(filename, "/dev/fd", NULL)) {
954 s->fd_open_flags = open_flags;
955 /* open will not fail even if no floppy is inserted */
956 open_flags |= O_NONBLOCK;
957 } else if (strstart(filename, "/dev/sg", NULL)) {
961 fd = open(filename, open_flags, 0644);
969 #if defined(__linux__)
970 /* close fd so that we can reopen it as needed */
971 if (s->type == FTYPE_FD) {
974 s->fd_media_changed = 1;
980 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
982 /* Note: we do not have a reliable method to detect if the floppy is
983 present. The current method is to try to open the floppy at every
984 I/O and to keep it opened during a few hundreds of ms. */
985 static int fd_open(BlockDriverState *bs)
987 BDRVRawState *s = bs->opaque;
988 int last_media_present;
990 if (s->type != FTYPE_FD)
992 last_media_present = (s->fd >= 0);
994 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
998 printf("Floppy closed\n");
1002 if (s->fd_got_error &&
1003 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1005 printf("No floppy (open delayed)\n");
1009 s->fd = open(bs->filename, s->fd_open_flags);
1011 s->fd_error_time = qemu_get_clock(rt_clock);
1012 s->fd_got_error = 1;
1013 if (last_media_present)
1014 s->fd_media_changed = 1;
1016 printf("No floppy\n");
1021 printf("Floppy opened\n");
1024 if (!last_media_present)
1025 s->fd_media_changed = 1;
1026 s->fd_open_time = qemu_get_clock(rt_clock);
1027 s->fd_got_error = 0;
1031 static int fd_open(BlockDriverState *bs)
1037 #if defined(__linux__)
1039 static int raw_is_inserted(BlockDriverState *bs)
1041 BDRVRawState *s = bs->opaque;
1046 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1047 if (ret == CDS_DISC_OK)
1060 /* currently only used by fdc.c, but a CD version would be good too */
1061 static int raw_media_changed(BlockDriverState *bs)
1063 BDRVRawState *s = bs->opaque;
1069 /* XXX: we do not have a true media changed indication. It
1070 does not work if the floppy is changed without trying
1073 ret = s->fd_media_changed;
1074 s->fd_media_changed = 0;
1076 printf("Floppy changed=%d\n", ret);
1085 static int raw_eject(BlockDriverState *bs, int eject_flag)
1087 BDRVRawState *s = bs->opaque;
1092 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1093 perror("CDROMEJECT");
1095 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1096 perror("CDROMEJECT");
1106 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1108 if (ioctl(fd, FDEJECT, 0) < 0)
1120 static int raw_set_locked(BlockDriverState *bs, int locked)
1122 BDRVRawState *s = bs->opaque;
1126 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1127 /* Note: an error can happen if the distribution automatically
1128 mounts the CD-ROM */
1129 // perror("CDROM_LOCKDOOR");
1138 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1140 BDRVRawState *s = bs->opaque;
1142 return ioctl(s->fd, req, buf);
1146 static int raw_is_inserted(BlockDriverState *bs)
1151 static int raw_media_changed(BlockDriverState *bs)
1156 static int raw_eject(BlockDriverState *bs, int eject_flag)
1161 static int raw_set_locked(BlockDriverState *bs, int locked)
1166 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1172 BlockDriver bdrv_host_device = {
1174 sizeof(BDRVRawState),
1175 NULL, /* no probe for protocols */
1184 .bdrv_aio_read = raw_aio_read,
1185 .bdrv_aio_write = raw_aio_write,
1186 .bdrv_aio_cancel = raw_aio_cancel,
1187 .aiocb_size = sizeof(RawAIOCB),
1189 .bdrv_pread = raw_pread,
1190 .bdrv_pwrite = raw_pwrite,
1191 .bdrv_getlength = raw_getlength,
1193 /* removable device support */
1194 .bdrv_is_inserted = raw_is_inserted,
1195 .bdrv_media_changed = raw_media_changed,
1196 .bdrv_eject = raw_eject,
1197 .bdrv_set_locked = raw_set_locked,
1198 /* generic scsi device */
1199 .bdrv_ioctl = raw_ioctl,