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"
35 #include <sys/param.h>
36 #include <IOKit/IOKitLib.h>
37 #include <IOKit/IOBSD.h>
38 #include <IOKit/storage/IOMediaBSDClient.h>
39 #include <IOKit/storage/IOMedia.h>
40 #include <IOKit/storage/IOCDMedia.h>
41 //#include <IOKit/storage/IOCDTypes.h>
42 #include <CoreFoundation/CoreFoundation.h>
46 #define _POSIX_PTHREAD_SEMANTICS 1
51 #include <sys/ioctl.h>
52 #include <linux/cdrom.h>
59 //#define DEBUG_FLOPPY
62 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
63 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
64 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
66 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
73 #define ALIGNED_BUFFER_SIZE (32 * 512)
75 /* if the FD is not accessed during that time (in ms), we try to
76 reopen it to see if the disk has been changed */
77 #define FD_OPEN_TIMEOUT 1000
79 typedef struct BDRVRawState {
82 unsigned int lseek_err_cnt;
83 #if defined(__linux__)
84 /* linux floppy specific */
87 int64_t fd_error_time;
91 #if defined(O_DIRECT) && !defined(QEMU_IMG)
96 static int fd_open(BlockDriverState *bs);
98 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
100 BDRVRawState *s = bs->opaque;
101 int fd, open_flags, ret;
103 s->lseek_err_cnt = 0;
105 open_flags = O_BINARY;
106 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
107 open_flags |= O_RDWR;
109 open_flags |= O_RDONLY;
112 if (flags & BDRV_O_CREAT)
113 open_flags |= O_CREAT | O_TRUNC;
115 if (flags & BDRV_O_DIRECT)
116 open_flags |= O_DIRECT;
119 s->type = FTYPE_FILE;
121 fd = open(filename, open_flags, 0644);
129 #if defined(O_DIRECT) && !defined(QEMU_IMG)
130 s->aligned_buf = NULL;
131 if (flags & BDRV_O_DIRECT) {
132 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
133 if (s->aligned_buf == NULL) {
143 /* XXX: use host sector size if necessary with:
144 #ifdef DIOCGSECTORSIZE
146 unsigned int sectorsize = 512;
147 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
148 sectorsize > bufsize)
149 bufsize = sectorsize;
153 u_int32_t blockSize = 512;
154 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
161 * offset and count are in bytes, but must be multiples of 512 for files
162 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
164 * This function may be called without alignment if the caller ensures
165 * that O_DIRECT is not in effect.
167 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
168 uint8_t *buf, int count)
170 BDRVRawState *s = bs->opaque;
177 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
178 ++(s->lseek_err_cnt);
179 if(s->lseek_err_cnt <= 10) {
180 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
181 "] lseek failed : %d = %s\n",
182 s->fd, bs->filename, offset, buf, count,
183 bs->total_sectors, errno, strerror(errno));
189 ret = read(s->fd, buf, count);
191 goto label__raw_read__success;
193 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
194 "] read failed %d : %d = %s\n",
195 s->fd, bs->filename, offset, buf, count,
196 bs->total_sectors, ret, errno, strerror(errno));
198 /* Try harder for CDrom. */
199 if (bs->type == BDRV_TYPE_CDROM) {
200 lseek(s->fd, offset, SEEK_SET);
201 ret = read(s->fd, buf, count);
203 goto label__raw_read__success;
204 lseek(s->fd, offset, SEEK_SET);
205 ret = read(s->fd, buf, count);
207 goto label__raw_read__success;
209 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
210 "] retry read failed %d : %d = %s\n",
211 s->fd, bs->filename, offset, buf, count,
212 bs->total_sectors, ret, errno, strerror(errno));
215 label__raw_read__success:
221 * offset and count are in bytes, but must be multiples of 512 for files
222 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
224 * This function may be called without alignment if the caller ensures
225 * that O_DIRECT is not in effect.
227 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
228 const uint8_t *buf, int count)
230 BDRVRawState *s = bs->opaque;
237 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
238 ++(s->lseek_err_cnt);
239 if(s->lseek_err_cnt) {
240 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
241 PRId64 "] lseek failed : %d = %s\n",
242 s->fd, bs->filename, offset, buf, count,
243 bs->total_sectors, errno, strerror(errno));
247 s->lseek_err_cnt = 0;
249 ret = write(s->fd, buf, count);
251 goto label__raw_write__success;
253 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
254 "] write failed %d : %d = %s\n",
255 s->fd, bs->filename, offset, buf, count,
256 bs->total_sectors, ret, errno, strerror(errno));
258 label__raw_write__success:
264 #if defined(O_DIRECT) && !defined(QEMU_IMG)
266 * offset and count are in bytes and possibly not aligned. For files opened
267 * with O_DIRECT, necessary alignments are ensured before calling
268 * raw_pread_aligned to do the actual read.
270 static int raw_pread(BlockDriverState *bs, int64_t offset,
271 uint8_t *buf, int count)
273 BDRVRawState *s = bs->opaque;
274 int size, ret, shift, sum;
278 if (s->aligned_buf != NULL) {
280 if (offset & 0x1ff) {
281 /* align offset on a 512 bytes boundary */
283 shift = offset & 0x1ff;
284 size = (shift + count + 0x1ff) & ~0x1ff;
285 if (size > ALIGNED_BUFFER_SIZE)
286 size = ALIGNED_BUFFER_SIZE;
287 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
294 memcpy(buf, s->aligned_buf + shift, size);
304 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
306 /* read on aligned buffer */
310 size = (count + 0x1ff) & ~0x1ff;
311 if (size > ALIGNED_BUFFER_SIZE)
312 size = ALIGNED_BUFFER_SIZE;
314 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
322 memcpy(buf, s->aligned_buf, size);
334 return raw_pread_aligned(bs, offset, buf, count) + sum;
338 * offset and count are in bytes and possibly not aligned. For files opened
339 * with O_DIRECT, necessary alignments are ensured before calling
340 * raw_pwrite_aligned to do the actual write.
342 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
343 const uint8_t *buf, int count)
345 BDRVRawState *s = bs->opaque;
346 int size, ret, shift, sum;
350 if (s->aligned_buf != NULL) {
352 if (offset & 0x1ff) {
353 /* align offset on a 512 bytes boundary */
354 shift = offset & 0x1ff;
355 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
362 memcpy(s->aligned_buf + shift, buf, size);
364 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
376 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
378 while ((size = (count & ~0x1ff)) != 0) {
380 if (size > ALIGNED_BUFFER_SIZE)
381 size = ALIGNED_BUFFER_SIZE;
383 memcpy(s->aligned_buf, buf, size);
385 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
394 /* here, count < 512 because (count & ~0x1ff) == 0 */
396 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
399 memcpy(s->aligned_buf, buf, count);
401 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
412 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
416 #define raw_pread raw_pread_aligned
417 #define raw_pwrite raw_pwrite_aligned
421 /***********************************************************/
422 /* Unix AIO using POSIX AIO */
424 typedef struct RawAIOCB {
425 BlockDriverAIOCB common;
427 struct RawAIOCB *next;
431 static int aio_sig_num = SIGUSR2;
432 static RawAIOCB *first_aio; /* AIO issued */
433 static int aio_initialized = 0;
435 static void aio_signal_handler(int signum)
437 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
438 CPUState *env = cpu_single_env;
440 /* stop the currently executing cpu because a timer occured */
441 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
443 if (env->kqemu_enabled) {
444 kqemu_cpu_interrupt(env);
451 void qemu_aio_init(void)
453 struct sigaction act;
457 sigfillset(&act.sa_mask);
458 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
459 act.sa_handler = aio_signal_handler;
460 sigaction(aio_sig_num, &act, NULL);
462 #if defined(__GLIBC__) && defined(__linux__)
464 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
465 seems to fix the problem. */
467 memset(&ai, 0, sizeof(ai));
470 ai.aio_idle_time = 365 * 100000;
476 void qemu_aio_poll(void)
478 RawAIOCB *acb, **pacb;
487 ret = aio_error(&acb->aiocb);
488 if (ret == ECANCELED) {
489 /* remove the request */
491 qemu_aio_release(acb);
492 } else if (ret != EINPROGRESS) {
495 ret = aio_return(&acb->aiocb);
496 if (ret == acb->aiocb.aio_nbytes)
503 /* remove the request */
505 /* call the callback */
506 acb->common.cb(acb->common.opaque, ret);
507 qemu_aio_release(acb);
517 /* Wait for all IO requests to complete. */
518 void qemu_aio_flush(void)
520 qemu_aio_wait_start();
528 /* wait until at least one AIO was handled */
529 static sigset_t wait_oset;
531 void qemu_aio_wait_start(void)
535 if (!aio_initialized)
538 sigaddset(&set, aio_sig_num);
539 sigprocmask(SIG_BLOCK, &set, &wait_oset);
542 void qemu_aio_wait(void)
547 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
552 sigaddset(&set, aio_sig_num);
553 sigwait(&set, &nb_sigs);
557 void qemu_aio_wait_end(void)
559 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
562 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
563 int64_t sector_num, uint8_t *buf, int nb_sectors,
564 BlockDriverCompletionFunc *cb, void *opaque)
566 BDRVRawState *s = bs->opaque;
572 acb = qemu_aio_get(bs, cb, opaque);
575 acb->aiocb.aio_fildes = s->fd;
576 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
577 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
578 acb->aiocb.aio_buf = buf;
580 acb->aiocb.aio_nbytes = -nb_sectors;
582 acb->aiocb.aio_nbytes = nb_sectors * 512;
583 acb->aiocb.aio_offset = sector_num * 512;
584 acb->next = first_aio;
589 #if !defined(QEMU_IMG) && !defined(QEMU_NBD)
590 static void raw_aio_em_cb(void* opaque)
592 RawAIOCB *acb = opaque;
593 acb->common.cb(acb->common.opaque, acb->ret);
594 qemu_aio_release(acb);
598 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
599 int64_t sector_num, uint8_t *buf, int nb_sectors,
600 BlockDriverCompletionFunc *cb, void *opaque)
605 * If O_DIRECT is used and the buffer is not aligned fall back
608 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
609 BDRVRawState *s = bs->opaque;
611 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
613 acb = qemu_aio_get(bs, cb, opaque);
614 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
615 bh = qemu_bh_new(raw_aio_em_cb, acb);
616 qemu_bh_schedule(bh);
621 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
624 if (aio_read(&acb->aiocb) < 0) {
625 qemu_aio_release(acb);
631 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
632 int64_t sector_num, const uint8_t *buf, int nb_sectors,
633 BlockDriverCompletionFunc *cb, void *opaque)
638 * If O_DIRECT is used and the buffer is not aligned fall back
641 #if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
642 BDRVRawState *s = bs->opaque;
644 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
646 acb = qemu_aio_get(bs, cb, opaque);
647 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
648 bh = qemu_bh_new(raw_aio_em_cb, acb);
649 qemu_bh_schedule(bh);
654 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
657 if (aio_write(&acb->aiocb) < 0) {
658 qemu_aio_release(acb);
664 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
667 RawAIOCB *acb = (RawAIOCB *)blockacb;
670 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
671 if (ret == AIO_NOTCANCELED) {
672 /* fail safe: if the aio could not be canceled, we wait for
674 while (aio_error(&acb->aiocb) == EINPROGRESS);
677 /* remove the callback from the queue */
682 } else if (*pacb == acb) {
684 qemu_aio_release(acb);
691 static void raw_close(BlockDriverState *bs)
693 BDRVRawState *s = bs->opaque;
697 #if defined(O_DIRECT) && !defined(QEMU_IMG)
698 if (s->aligned_buf != NULL)
699 qemu_free(s->aligned_buf);
704 static int raw_truncate(BlockDriverState *bs, int64_t offset)
706 BDRVRawState *s = bs->opaque;
707 if (s->type != FTYPE_FILE)
709 if (ftruncate(s->fd, offset) < 0)
714 static int64_t raw_getlength(BlockDriverState *bs)
716 BDRVRawState *s = bs->opaque;
723 struct dk_minfo minfo;
733 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
734 #ifdef DIOCGMEDIASIZE
735 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
738 size = LONG_LONG_MAX;
740 size = lseek(fd, 0LL, SEEK_END);
746 * use the DKIOCGMEDIAINFO ioctl to read the size.
748 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
750 size = minfo.dki_lbsize * minfo.dki_capacity;
751 } else /* there are reports that lseek on some devices
752 fails, but irc discussion said that contingency
753 on contingency was overkill */
756 size = lseek(fd, 0, SEEK_END);
761 static int raw_create(const char *filename, int64_t total_size,
762 const char *backing_file, int flags)
766 if (flags || backing_file)
769 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
773 ftruncate(fd, total_size * 512);
778 static void raw_flush(BlockDriverState *bs)
780 BDRVRawState *s = bs->opaque;
784 BlockDriver bdrv_raw = {
786 sizeof(BDRVRawState),
787 NULL, /* no probe for protocols */
795 .bdrv_aio_read = raw_aio_read,
796 .bdrv_aio_write = raw_aio_write,
797 .bdrv_aio_cancel = raw_aio_cancel,
798 .aiocb_size = sizeof(RawAIOCB),
799 .protocol_name = "file",
800 .bdrv_pread = raw_pread,
801 .bdrv_pwrite = raw_pwrite,
802 .bdrv_truncate = raw_truncate,
803 .bdrv_getlength = raw_getlength,
806 /***********************************************/
810 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
811 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
813 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
815 kern_return_t kernResult;
816 mach_port_t masterPort;
817 CFMutableDictionaryRef classesToMatch;
819 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
820 if ( KERN_SUCCESS != kernResult ) {
821 printf( "IOMasterPort returned %d\n", kernResult );
824 classesToMatch = IOServiceMatching( kIOCDMediaClass );
825 if ( classesToMatch == NULL ) {
826 printf( "IOServiceMatching returned a NULL dictionary.\n" );
828 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
830 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
831 if ( KERN_SUCCESS != kernResult )
833 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
839 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
841 io_object_t nextMedia;
842 kern_return_t kernResult = KERN_FAILURE;
844 nextMedia = IOIteratorNext( mediaIterator );
847 CFTypeRef bsdPathAsCFString;
848 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
849 if ( bsdPathAsCFString ) {
850 size_t devPathLength;
851 strcpy( bsdPath, _PATH_DEV );
852 strcat( bsdPath, "r" );
853 devPathLength = strlen( bsdPath );
854 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
855 kernResult = KERN_SUCCESS;
857 CFRelease( bsdPathAsCFString );
859 IOObjectRelease( nextMedia );
867 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
869 BDRVRawState *s = bs->opaque;
870 int fd, open_flags, ret;
873 if (strstart(filename, "/dev/cdrom", NULL)) {
874 kern_return_t kernResult;
875 io_iterator_t mediaIterator;
876 char bsdPath[ MAXPATHLEN ];
879 kernResult = FindEjectableCDMedia( &mediaIterator );
880 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
882 if ( bsdPath[ 0 ] != '\0' ) {
883 strcat(bsdPath,"s0");
884 /* some CDs don't have a partition 0 */
885 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
887 bsdPath[strlen(bsdPath)-1] = '1';
895 IOObjectRelease( mediaIterator );
898 open_flags = O_BINARY;
899 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
900 open_flags |= O_RDWR;
902 open_flags |= O_RDONLY;
906 if (flags & BDRV_O_DIRECT)
907 open_flags |= O_DIRECT;
910 s->type = FTYPE_FILE;
911 #if defined(__linux__)
912 if (strstart(filename, "/dev/cd", NULL)) {
913 /* open will not fail even if no CD is inserted */
914 open_flags |= O_NONBLOCK;
916 } else if (strstart(filename, "/dev/fd", NULL)) {
918 s->fd_open_flags = open_flags;
919 /* open will not fail even if no floppy is inserted */
920 open_flags |= O_NONBLOCK;
921 } else if (strstart(filename, "/dev/sg", NULL)) {
925 fd = open(filename, open_flags, 0644);
933 #if defined(__linux__)
934 /* close fd so that we can reopen it as needed */
935 if (s->type == FTYPE_FD) {
938 s->fd_media_changed = 1;
944 #if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
946 /* Note: we do not have a reliable method to detect if the floppy is
947 present. The current method is to try to open the floppy at every
948 I/O and to keep it opened during a few hundreds of ms. */
949 static int fd_open(BlockDriverState *bs)
951 BDRVRawState *s = bs->opaque;
952 int last_media_present;
954 if (s->type != FTYPE_FD)
956 last_media_present = (s->fd >= 0);
958 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
962 printf("Floppy closed\n");
966 if (s->fd_got_error &&
967 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
969 printf("No floppy (open delayed)\n");
973 s->fd = open(bs->filename, s->fd_open_flags);
975 s->fd_error_time = qemu_get_clock(rt_clock);
977 if (last_media_present)
978 s->fd_media_changed = 1;
980 printf("No floppy\n");
985 printf("Floppy opened\n");
988 if (!last_media_present)
989 s->fd_media_changed = 1;
990 s->fd_open_time = qemu_get_clock(rt_clock);
995 static int fd_open(BlockDriverState *bs)
1001 #if defined(__linux__)
1003 static int raw_is_inserted(BlockDriverState *bs)
1005 BDRVRawState *s = bs->opaque;
1010 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1011 if (ret == CDS_DISC_OK)
1024 /* currently only used by fdc.c, but a CD version would be good too */
1025 static int raw_media_changed(BlockDriverState *bs)
1027 BDRVRawState *s = bs->opaque;
1033 /* XXX: we do not have a true media changed indication. It
1034 does not work if the floppy is changed without trying
1037 ret = s->fd_media_changed;
1038 s->fd_media_changed = 0;
1040 printf("Floppy changed=%d\n", ret);
1049 static int raw_eject(BlockDriverState *bs, int eject_flag)
1051 BDRVRawState *s = bs->opaque;
1056 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1057 perror("CDROMEJECT");
1059 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1060 perror("CDROMEJECT");
1070 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1072 if (ioctl(fd, FDEJECT, 0) < 0)
1084 static int raw_set_locked(BlockDriverState *bs, int locked)
1086 BDRVRawState *s = bs->opaque;
1090 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1091 /* Note: an error can happen if the distribution automatically
1092 mounts the CD-ROM */
1093 // perror("CDROM_LOCKDOOR");
1102 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1104 BDRVRawState *s = bs->opaque;
1106 return ioctl(s->fd, req, buf);
1110 static int raw_is_inserted(BlockDriverState *bs)
1115 static int raw_media_changed(BlockDriverState *bs)
1120 static int raw_eject(BlockDriverState *bs, int eject_flag)
1125 static int raw_set_locked(BlockDriverState *bs, int locked)
1130 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1136 BlockDriver bdrv_host_device = {
1138 sizeof(BDRVRawState),
1139 NULL, /* no probe for protocols */
1147 .bdrv_aio_read = raw_aio_read,
1148 .bdrv_aio_write = raw_aio_write,
1149 .bdrv_aio_cancel = raw_aio_cancel,
1150 .aiocb_size = sizeof(RawAIOCB),
1151 .bdrv_pread = raw_pread,
1152 .bdrv_pwrite = raw_pwrite,
1153 .bdrv_getlength = raw_getlength,
1155 /* removable device support */
1156 .bdrv_is_inserted = raw_is_inserted,
1157 .bdrv_media_changed = raw_media_changed,
1158 .bdrv_eject = raw_eject,
1159 .bdrv_set_locked = raw_set_locked,
1160 /* generic scsi device */
1161 .bdrv_ioctl = raw_ioctl,