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"
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
47 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
67 //#define DEBUG_FLOPPY
70 #if defined(DEBUG_BLOCK)
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;
100 uint8_t* aligned_buf;
104 static int posix_aio_init(void);
106 static int fd_open(BlockDriverState *bs);
108 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
110 BDRVRawState *s = bs->opaque;
111 int fd, open_flags, ret;
115 s->lseek_err_cnt = 0;
117 open_flags = O_BINARY;
118 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
119 open_flags |= O_RDWR;
121 open_flags |= O_RDONLY;
124 if (flags & BDRV_O_CREAT)
125 open_flags |= O_CREAT | O_TRUNC;
127 if (flags & BDRV_O_DIRECT)
128 open_flags |= O_DIRECT;
131 s->type = FTYPE_FILE;
133 fd = open(filename, open_flags, 0644);
141 #if defined(O_DIRECT)
142 s->aligned_buf = NULL;
143 if (flags & BDRV_O_DIRECT) {
144 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
145 if (s->aligned_buf == NULL) {
155 /* XXX: use host sector size if necessary with:
156 #ifdef DIOCGSECTORSIZE
158 unsigned int sectorsize = 512;
159 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
160 sectorsize > bufsize)
161 bufsize = sectorsize;
165 u_int32_t blockSize = 512;
166 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
173 * offset and count are in bytes, but must be multiples of 512 for files
174 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
176 * This function may be called without alignment if the caller ensures
177 * that O_DIRECT is not in effect.
179 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
180 uint8_t *buf, int count)
182 BDRVRawState *s = bs->opaque;
189 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
190 ++(s->lseek_err_cnt);
191 if(s->lseek_err_cnt <= 10) {
192 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
193 "] lseek failed : %d = %s\n",
194 s->fd, bs->filename, offset, buf, count,
195 bs->total_sectors, errno, strerror(errno));
201 ret = read(s->fd, buf, count);
203 goto label__raw_read__success;
205 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
206 "] read failed %d : %d = %s\n",
207 s->fd, bs->filename, offset, buf, count,
208 bs->total_sectors, ret, errno, strerror(errno));
210 /* Try harder for CDrom. */
211 if (bs->type == BDRV_TYPE_CDROM) {
212 lseek(s->fd, offset, SEEK_SET);
213 ret = read(s->fd, buf, count);
215 goto label__raw_read__success;
216 lseek(s->fd, offset, SEEK_SET);
217 ret = read(s->fd, buf, count);
219 goto label__raw_read__success;
221 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
222 "] retry read failed %d : %d = %s\n",
223 s->fd, bs->filename, offset, buf, count,
224 bs->total_sectors, ret, errno, strerror(errno));
227 label__raw_read__success:
233 * offset and count are in bytes, but must be multiples of 512 for files
234 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
236 * This function may be called without alignment if the caller ensures
237 * that O_DIRECT is not in effect.
239 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
240 const uint8_t *buf, int count)
242 BDRVRawState *s = bs->opaque;
249 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
250 ++(s->lseek_err_cnt);
251 if(s->lseek_err_cnt) {
252 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
253 PRId64 "] lseek failed : %d = %s\n",
254 s->fd, bs->filename, offset, buf, count,
255 bs->total_sectors, errno, strerror(errno));
259 s->lseek_err_cnt = 0;
261 ret = write(s->fd, buf, count);
263 goto label__raw_write__success;
265 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
266 "] write failed %d : %d = %s\n",
267 s->fd, bs->filename, offset, buf, count,
268 bs->total_sectors, ret, errno, strerror(errno));
270 label__raw_write__success:
276 #if defined(O_DIRECT)
278 * offset and count are in bytes and possibly not aligned. For files opened
279 * with O_DIRECT, necessary alignments are ensured before calling
280 * raw_pread_aligned to do the actual read.
282 static int raw_pread(BlockDriverState *bs, int64_t offset,
283 uint8_t *buf, int count)
285 BDRVRawState *s = bs->opaque;
286 int size, ret, shift, sum;
290 if (s->aligned_buf != NULL) {
292 if (offset & 0x1ff) {
293 /* align offset on a 512 bytes boundary */
295 shift = offset & 0x1ff;
296 size = (shift + count + 0x1ff) & ~0x1ff;
297 if (size > ALIGNED_BUFFER_SIZE)
298 size = ALIGNED_BUFFER_SIZE;
299 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
306 memcpy(buf, s->aligned_buf + shift, size);
316 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
318 /* read on aligned buffer */
322 size = (count + 0x1ff) & ~0x1ff;
323 if (size > ALIGNED_BUFFER_SIZE)
324 size = ALIGNED_BUFFER_SIZE;
326 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
334 memcpy(buf, s->aligned_buf, size);
346 return raw_pread_aligned(bs, offset, buf, count) + sum;
350 * offset and count are in bytes and possibly not aligned. For files opened
351 * with O_DIRECT, necessary alignments are ensured before calling
352 * raw_pwrite_aligned to do the actual write.
354 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
355 const uint8_t *buf, int count)
357 BDRVRawState *s = bs->opaque;
358 int size, ret, shift, sum;
362 if (s->aligned_buf != NULL) {
364 if (offset & 0x1ff) {
365 /* align offset on a 512 bytes boundary */
366 shift = offset & 0x1ff;
367 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
374 memcpy(s->aligned_buf + shift, buf, size);
376 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
388 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
390 while ((size = (count & ~0x1ff)) != 0) {
392 if (size > ALIGNED_BUFFER_SIZE)
393 size = ALIGNED_BUFFER_SIZE;
395 memcpy(s->aligned_buf, buf, size);
397 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
406 /* here, count < 512 because (count & ~0x1ff) == 0 */
408 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
411 memcpy(s->aligned_buf, buf, count);
413 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
424 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
428 #define raw_pread raw_pread_aligned
429 #define raw_pwrite raw_pwrite_aligned
434 /***********************************************************/
435 /* Unix AIO using POSIX AIO */
437 typedef struct RawAIOCB {
438 BlockDriverAIOCB common;
440 struct RawAIOCB *next;
444 typedef struct PosixAioState
450 static void posix_aio_read(void *opaque)
452 PosixAioState *s = opaque;
453 RawAIOCB *acb, **pacb;
457 struct qemu_signalfd_siginfo siginfo;
461 /* try to read from signalfd, don't freak out if we can't read anything */
463 while (offset < 128) {
466 len = read(s->fd, sig.buf + offset, 128 - offset);
467 if (len == -1 && errno == EINTR)
469 if (len == -1 && errno == EAGAIN) {
470 /* there is no natural reason for this to happen,
471 * so we'll spin hard until we get everything just
472 * to be on the safe side. */
481 pacb = &s->first_aio;
486 ret = aio_error(&acb->aiocb);
487 if (ret == ECANCELED) {
488 /* remove the request */
490 qemu_aio_release(acb);
491 } else if (ret != EINPROGRESS) {
494 ret = aio_return(&acb->aiocb);
495 if (ret == acb->aiocb.aio_nbytes)
502 /* remove the request */
504 /* call the callback */
505 acb->common.cb(acb->common.opaque, ret);
506 qemu_aio_release(acb);
516 static int posix_aio_flush(void *opaque)
518 PosixAioState *s = opaque;
519 return !!s->first_aio;
522 static PosixAioState *posix_aio_state;
524 static int posix_aio_init(void)
532 s = qemu_malloc(sizeof(PosixAioState));
536 /* Make sure to block AIO signal */
538 sigaddset(&mask, SIGUSR2);
539 sigprocmask(SIG_BLOCK, &mask, NULL);
542 s->fd = qemu_signalfd(&mask);
544 fcntl(s->fd, F_SETFL, O_NONBLOCK);
546 qemu_aio_set_fd_handler(s->fd, posix_aio_read, NULL, posix_aio_flush, s);
548 #if defined(__GLIBC__) && defined(__linux__)
550 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
551 seems to fix the problem. */
553 memset(&ai, 0, sizeof(ai));
556 ai.aio_idle_time = 365 * 100000;
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 = SIGUSR2;
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 = posix_aio_state->first_aio;
588 posix_aio_state->first_aio = acb;
592 static void raw_aio_em_cb(void* opaque)
594 RawAIOCB *acb = opaque;
595 acb->common.cb(acb->common.opaque, acb->ret);
596 qemu_aio_release(acb);
599 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
600 int64_t sector_num, uint8_t *buf, int nb_sectors,
601 BlockDriverCompletionFunc *cb, void *opaque)
606 * If O_DIRECT is used and the buffer is not aligned fall back
609 #if defined(O_DIRECT)
610 BDRVRawState *s = bs->opaque;
612 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
614 acb = qemu_aio_get(bs, cb, opaque);
615 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
616 bh = qemu_bh_new(raw_aio_em_cb, acb);
617 qemu_bh_schedule(bh);
622 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
625 if (aio_read(&acb->aiocb) < 0) {
626 qemu_aio_release(acb);
632 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
633 int64_t sector_num, const uint8_t *buf, int nb_sectors,
634 BlockDriverCompletionFunc *cb, void *opaque)
639 * If O_DIRECT is used and the buffer is not aligned fall back
642 #if defined(O_DIRECT)
643 BDRVRawState *s = bs->opaque;
645 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
647 acb = qemu_aio_get(bs, cb, opaque);
648 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
649 bh = qemu_bh_new(raw_aio_em_cb, acb);
650 qemu_bh_schedule(bh);
655 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
658 if (aio_write(&acb->aiocb) < 0) {
659 qemu_aio_release(acb);
665 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
668 RawAIOCB *acb = (RawAIOCB *)blockacb;
671 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
672 if (ret == AIO_NOTCANCELED) {
673 /* fail safe: if the aio could not be canceled, we wait for
675 while (aio_error(&acb->aiocb) == EINPROGRESS);
678 /* remove the callback from the queue */
679 pacb = &posix_aio_state->first_aio;
683 } else if (*pacb == acb) {
685 qemu_aio_release(acb);
692 #else /* CONFIG_AIO */
693 static int posix_aio_init(void)
696 #endif /* CONFIG_AIO */
698 static void raw_close(BlockDriverState *bs)
700 BDRVRawState *s = bs->opaque;
704 #if defined(O_DIRECT)
705 if (s->aligned_buf != NULL)
706 qemu_free(s->aligned_buf);
711 static int raw_truncate(BlockDriverState *bs, int64_t offset)
713 BDRVRawState *s = bs->opaque;
714 if (s->type != FTYPE_FILE)
716 if (ftruncate(s->fd, offset) < 0)
722 static int64_t raw_getlength(BlockDriverState *bs)
724 BDRVRawState *s = bs->opaque;
730 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
733 if (ioctl(fd, DIOCGDINFO, &dl))
735 return (uint64_t)dl.d_secsize *
736 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
740 #else /* !__OpenBSD__ */
741 static int64_t raw_getlength(BlockDriverState *bs)
743 BDRVRawState *s = bs->opaque;
750 struct dk_minfo minfo;
760 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
761 #ifdef DIOCGMEDIASIZE
762 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
765 size = LONG_LONG_MAX;
767 size = lseek(fd, 0LL, SEEK_END);
773 * use the DKIOCGMEDIAINFO ioctl to read the size.
775 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
777 size = minfo.dki_lbsize * minfo.dki_capacity;
778 } else /* there are reports that lseek on some devices
779 fails, but irc discussion said that contingency
780 on contingency was overkill */
783 size = lseek(fd, 0, SEEK_END);
789 static int raw_create(const char *filename, int64_t total_size,
790 const char *backing_file, int flags)
794 if (flags || backing_file)
797 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
801 ftruncate(fd, total_size * 512);
806 static void raw_flush(BlockDriverState *bs)
808 BDRVRawState *s = bs->opaque;
812 BlockDriver bdrv_raw = {
814 sizeof(BDRVRawState),
815 NULL, /* no probe for protocols */
824 .bdrv_aio_read = raw_aio_read,
825 .bdrv_aio_write = raw_aio_write,
826 .bdrv_aio_cancel = raw_aio_cancel,
827 .aiocb_size = sizeof(RawAIOCB),
829 .bdrv_pread = raw_pread,
830 .bdrv_pwrite = raw_pwrite,
831 .bdrv_truncate = raw_truncate,
832 .bdrv_getlength = raw_getlength,
835 /***********************************************/
839 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
840 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
842 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
844 kern_return_t kernResult;
845 mach_port_t masterPort;
846 CFMutableDictionaryRef classesToMatch;
848 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
849 if ( KERN_SUCCESS != kernResult ) {
850 printf( "IOMasterPort returned %d\n", kernResult );
853 classesToMatch = IOServiceMatching( kIOCDMediaClass );
854 if ( classesToMatch == NULL ) {
855 printf( "IOServiceMatching returned a NULL dictionary.\n" );
857 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
859 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
860 if ( KERN_SUCCESS != kernResult )
862 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
868 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
870 io_object_t nextMedia;
871 kern_return_t kernResult = KERN_FAILURE;
873 nextMedia = IOIteratorNext( mediaIterator );
876 CFTypeRef bsdPathAsCFString;
877 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
878 if ( bsdPathAsCFString ) {
879 size_t devPathLength;
880 strcpy( bsdPath, _PATH_DEV );
881 strcat( bsdPath, "r" );
882 devPathLength = strlen( bsdPath );
883 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
884 kernResult = KERN_SUCCESS;
886 CFRelease( bsdPathAsCFString );
888 IOObjectRelease( nextMedia );
896 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
898 BDRVRawState *s = bs->opaque;
899 int fd, open_flags, ret;
904 if (strstart(filename, "/dev/cdrom", NULL)) {
905 kern_return_t kernResult;
906 io_iterator_t mediaIterator;
907 char bsdPath[ MAXPATHLEN ];
910 kernResult = FindEjectableCDMedia( &mediaIterator );
911 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
913 if ( bsdPath[ 0 ] != '\0' ) {
914 strcat(bsdPath,"s0");
915 /* some CDs don't have a partition 0 */
916 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
918 bsdPath[strlen(bsdPath)-1] = '1';
926 IOObjectRelease( mediaIterator );
929 open_flags = O_BINARY;
930 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
931 open_flags |= O_RDWR;
933 open_flags |= O_RDONLY;
937 if (flags & BDRV_O_DIRECT)
938 open_flags |= O_DIRECT;
941 s->type = FTYPE_FILE;
942 #if defined(__linux__)
943 if (strstart(filename, "/dev/cd", NULL)) {
944 /* open will not fail even if no CD is inserted */
945 open_flags |= O_NONBLOCK;
947 } else if (strstart(filename, "/dev/fd", NULL)) {
949 s->fd_open_flags = open_flags;
950 /* open will not fail even if no floppy is inserted */
951 open_flags |= O_NONBLOCK;
952 } else if (strstart(filename, "/dev/sg", NULL)) {
956 fd = open(filename, open_flags, 0644);
964 #if defined(__linux__)
965 /* close fd so that we can reopen it as needed */
966 if (s->type == FTYPE_FD) {
969 s->fd_media_changed = 1;
975 #if defined(__linux__)
977 /* Note: we do not have a reliable method to detect if the floppy is
978 present. The current method is to try to open the floppy at every
979 I/O and to keep it opened during a few hundreds of ms. */
980 static int fd_open(BlockDriverState *bs)
982 BDRVRawState *s = bs->opaque;
983 int last_media_present;
985 if (s->type != FTYPE_FD)
987 last_media_present = (s->fd >= 0);
989 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
993 printf("Floppy closed\n");
997 if (s->fd_got_error &&
998 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1000 printf("No floppy (open delayed)\n");
1004 s->fd = open(bs->filename, s->fd_open_flags);
1006 s->fd_error_time = qemu_get_clock(rt_clock);
1007 s->fd_got_error = 1;
1008 if (last_media_present)
1009 s->fd_media_changed = 1;
1011 printf("No floppy\n");
1016 printf("Floppy opened\n");
1019 if (!last_media_present)
1020 s->fd_media_changed = 1;
1021 s->fd_open_time = qemu_get_clock(rt_clock);
1022 s->fd_got_error = 0;
1026 static int raw_is_inserted(BlockDriverState *bs)
1028 BDRVRawState *s = bs->opaque;
1033 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1034 if (ret == CDS_DISC_OK)
1047 /* currently only used by fdc.c, but a CD version would be good too */
1048 static int raw_media_changed(BlockDriverState *bs)
1050 BDRVRawState *s = bs->opaque;
1056 /* XXX: we do not have a true media changed indication. It
1057 does not work if the floppy is changed without trying
1060 ret = s->fd_media_changed;
1061 s->fd_media_changed = 0;
1063 printf("Floppy changed=%d\n", ret);
1072 static int raw_eject(BlockDriverState *bs, int eject_flag)
1074 BDRVRawState *s = bs->opaque;
1079 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1080 perror("CDROMEJECT");
1082 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1083 perror("CDROMEJECT");
1093 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1095 if (ioctl(fd, FDEJECT, 0) < 0)
1107 static int raw_set_locked(BlockDriverState *bs, int locked)
1109 BDRVRawState *s = bs->opaque;
1113 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1114 /* Note: an error can happen if the distribution automatically
1115 mounts the CD-ROM */
1116 // perror("CDROM_LOCKDOOR");
1125 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1127 BDRVRawState *s = bs->opaque;
1129 return ioctl(s->fd, req, buf);
1133 static int fd_open(BlockDriverState *bs)
1138 static int raw_is_inserted(BlockDriverState *bs)
1143 static int raw_media_changed(BlockDriverState *bs)
1148 static int raw_eject(BlockDriverState *bs, int eject_flag)
1153 static int raw_set_locked(BlockDriverState *bs, int locked)
1158 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1164 BlockDriver bdrv_host_device = {
1166 sizeof(BDRVRawState),
1167 NULL, /* no probe for protocols */
1176 .bdrv_aio_read = raw_aio_read,
1177 .bdrv_aio_write = raw_aio_write,
1178 .bdrv_aio_cancel = raw_aio_cancel,
1179 .aiocb_size = sizeof(RawAIOCB),
1181 .bdrv_pread = raw_pread,
1182 .bdrv_pwrite = raw_pwrite,
1183 .bdrv_getlength = raw_getlength,
1185 /* removable device support */
1186 .bdrv_is_inserted = raw_is_inserted,
1187 .bdrv_media_changed = raw_media_changed,
1188 .bdrv_eject = raw_eject,
1189 .bdrv_set_locked = raw_set_locked,
1190 /* generic scsi device */
1191 .bdrv_ioctl = raw_ioctl,