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 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)
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)
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_fd = -1;
441 static int aio_sig_num = SIGUSR2;
442 static RawAIOCB *first_aio; /* AIO issued */
443 static int aio_initialized = 0;
445 static void qemu_aio_poll(void *opaque)
447 RawAIOCB *acb, **pacb;
451 struct qemu_signalfd_siginfo siginfo;
455 /* try to read from signalfd, don't freak out if we can't read anything */
457 while (offset < 128) {
460 len = read(aio_sig_fd, sig.buf + offset, 128 - offset);
461 if (len == -1 && errno == EINTR)
463 if (len == -1 && errno == EAGAIN) {
464 /* there is no natural reason for this to happen,
465 * so we'll spin hard until we get everything just
466 * to be on the safe side. */
480 ret = aio_error(&acb->aiocb);
481 if (ret == ECANCELED) {
482 /* remove the request */
484 qemu_aio_release(acb);
485 } else if (ret != EINPROGRESS) {
488 ret = aio_return(&acb->aiocb);
489 if (ret == acb->aiocb.aio_nbytes)
496 /* remove the request */
498 /* call the callback */
499 acb->common.cb(acb->common.opaque, ret);
500 qemu_aio_release(acb);
510 void qemu_aio_init(void)
516 /* Make sure to block AIO signal */
518 sigaddset(&mask, aio_sig_num);
519 sigprocmask(SIG_BLOCK, &mask, NULL);
521 aio_sig_fd = qemu_signalfd(&mask);
523 fcntl(aio_sig_fd, F_SETFL, O_NONBLOCK);
525 qemu_set_fd_handler2(aio_sig_fd, NULL, qemu_aio_poll, NULL, NULL);
527 #if defined(__GLIBC__) && defined(__linux__)
529 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
530 seems to fix the problem. */
532 memset(&ai, 0, sizeof(ai));
535 ai.aio_idle_time = 365 * 100000;
541 /* Wait for all IO requests to complete. */
542 void qemu_aio_flush(void)
550 void qemu_aio_wait(void)
564 FD_SET(aio_sig_fd, &rdfds);
566 ret = select(aio_sig_fd + 1, &rdfds, NULL, NULL, NULL);
567 if (ret == -1 && errno == EINTR)
574 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
575 int64_t sector_num, uint8_t *buf, int nb_sectors,
576 BlockDriverCompletionFunc *cb, void *opaque)
578 BDRVRawState *s = bs->opaque;
584 acb = qemu_aio_get(bs, cb, opaque);
587 acb->aiocb.aio_fildes = s->fd;
588 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
589 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
590 acb->aiocb.aio_buf = buf;
592 acb->aiocb.aio_nbytes = -nb_sectors;
594 acb->aiocb.aio_nbytes = nb_sectors * 512;
595 acb->aiocb.aio_offset = sector_num * 512;
596 acb->next = first_aio;
601 static void raw_aio_em_cb(void* opaque)
603 RawAIOCB *acb = opaque;
604 acb->common.cb(acb->common.opaque, acb->ret);
605 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)
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)
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_flush(void)
711 void qemu_aio_wait(void)
716 #endif /* CONFIG_AIO */
718 static void raw_close(BlockDriverState *bs)
720 BDRVRawState *s = bs->opaque;
724 #if defined(O_DIRECT)
725 if (s->aligned_buf != NULL)
726 qemu_free(s->aligned_buf);
731 static int raw_truncate(BlockDriverState *bs, int64_t offset)
733 BDRVRawState *s = bs->opaque;
734 if (s->type != FTYPE_FILE)
736 if (ftruncate(s->fd, offset) < 0)
742 static int64_t raw_getlength(BlockDriverState *bs)
744 BDRVRawState *s = bs->opaque;
750 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
753 if (ioctl(fd, DIOCGDINFO, &dl))
755 return (uint64_t)dl.d_secsize *
756 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
760 #else /* !__OpenBSD__ */
761 static int64_t raw_getlength(BlockDriverState *bs)
763 BDRVRawState *s = bs->opaque;
770 struct dk_minfo minfo;
780 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
781 #ifdef DIOCGMEDIASIZE
782 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
785 size = LONG_LONG_MAX;
787 size = lseek(fd, 0LL, SEEK_END);
793 * use the DKIOCGMEDIAINFO ioctl to read the size.
795 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
797 size = minfo.dki_lbsize * minfo.dki_capacity;
798 } else /* there are reports that lseek on some devices
799 fails, but irc discussion said that contingency
800 on contingency was overkill */
803 size = lseek(fd, 0, SEEK_END);
809 static int raw_create(const char *filename, int64_t total_size,
810 const char *backing_file, int flags)
814 if (flags || backing_file)
817 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
821 ftruncate(fd, total_size * 512);
826 static void raw_flush(BlockDriverState *bs)
828 BDRVRawState *s = bs->opaque;
832 BlockDriver bdrv_raw = {
834 sizeof(BDRVRawState),
835 NULL, /* no probe for protocols */
844 .bdrv_aio_read = raw_aio_read,
845 .bdrv_aio_write = raw_aio_write,
846 .bdrv_aio_cancel = raw_aio_cancel,
847 .aiocb_size = sizeof(RawAIOCB),
849 .bdrv_pread = raw_pread,
850 .bdrv_pwrite = raw_pwrite,
851 .bdrv_truncate = raw_truncate,
852 .bdrv_getlength = raw_getlength,
855 /***********************************************/
859 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
860 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
862 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
864 kern_return_t kernResult;
865 mach_port_t masterPort;
866 CFMutableDictionaryRef classesToMatch;
868 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
869 if ( KERN_SUCCESS != kernResult ) {
870 printf( "IOMasterPort returned %d\n", kernResult );
873 classesToMatch = IOServiceMatching( kIOCDMediaClass );
874 if ( classesToMatch == NULL ) {
875 printf( "IOServiceMatching returned a NULL dictionary.\n" );
877 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
879 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
880 if ( KERN_SUCCESS != kernResult )
882 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
888 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
890 io_object_t nextMedia;
891 kern_return_t kernResult = KERN_FAILURE;
893 nextMedia = IOIteratorNext( mediaIterator );
896 CFTypeRef bsdPathAsCFString;
897 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
898 if ( bsdPathAsCFString ) {
899 size_t devPathLength;
900 strcpy( bsdPath, _PATH_DEV );
901 strcat( bsdPath, "r" );
902 devPathLength = strlen( bsdPath );
903 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
904 kernResult = KERN_SUCCESS;
906 CFRelease( bsdPathAsCFString );
908 IOObjectRelease( nextMedia );
916 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
918 BDRVRawState *s = bs->opaque;
919 int fd, open_flags, ret;
922 if (strstart(filename, "/dev/cdrom", NULL)) {
923 kern_return_t kernResult;
924 io_iterator_t mediaIterator;
925 char bsdPath[ MAXPATHLEN ];
928 kernResult = FindEjectableCDMedia( &mediaIterator );
929 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
931 if ( bsdPath[ 0 ] != '\0' ) {
932 strcat(bsdPath,"s0");
933 /* some CDs don't have a partition 0 */
934 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
936 bsdPath[strlen(bsdPath)-1] = '1';
944 IOObjectRelease( mediaIterator );
947 open_flags = O_BINARY;
948 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
949 open_flags |= O_RDWR;
951 open_flags |= O_RDONLY;
955 if (flags & BDRV_O_DIRECT)
956 open_flags |= O_DIRECT;
959 s->type = FTYPE_FILE;
960 #if defined(__linux__)
961 if (strstart(filename, "/dev/cd", NULL)) {
962 /* open will not fail even if no CD is inserted */
963 open_flags |= O_NONBLOCK;
965 } else if (strstart(filename, "/dev/fd", NULL)) {
967 s->fd_open_flags = open_flags;
968 /* open will not fail even if no floppy is inserted */
969 open_flags |= O_NONBLOCK;
970 } else if (strstart(filename, "/dev/sg", NULL)) {
974 fd = open(filename, open_flags, 0644);
982 #if defined(__linux__)
983 /* close fd so that we can reopen it as needed */
984 if (s->type == FTYPE_FD) {
987 s->fd_media_changed = 1;
993 #if defined(__linux__)
995 /* Note: we do not have a reliable method to detect if the floppy is
996 present. The current method is to try to open the floppy at every
997 I/O and to keep it opened during a few hundreds of ms. */
998 static int fd_open(BlockDriverState *bs)
1000 BDRVRawState *s = bs->opaque;
1001 int last_media_present;
1003 if (s->type != FTYPE_FD)
1005 last_media_present = (s->fd >= 0);
1007 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1011 printf("Floppy closed\n");
1015 if (s->fd_got_error &&
1016 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1018 printf("No floppy (open delayed)\n");
1022 s->fd = open(bs->filename, s->fd_open_flags);
1024 s->fd_error_time = qemu_get_clock(rt_clock);
1025 s->fd_got_error = 1;
1026 if (last_media_present)
1027 s->fd_media_changed = 1;
1029 printf("No floppy\n");
1034 printf("Floppy opened\n");
1037 if (!last_media_present)
1038 s->fd_media_changed = 1;
1039 s->fd_open_time = qemu_get_clock(rt_clock);
1040 s->fd_got_error = 0;
1044 static int raw_is_inserted(BlockDriverState *bs)
1046 BDRVRawState *s = bs->opaque;
1051 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1052 if (ret == CDS_DISC_OK)
1065 /* currently only used by fdc.c, but a CD version would be good too */
1066 static int raw_media_changed(BlockDriverState *bs)
1068 BDRVRawState *s = bs->opaque;
1074 /* XXX: we do not have a true media changed indication. It
1075 does not work if the floppy is changed without trying
1078 ret = s->fd_media_changed;
1079 s->fd_media_changed = 0;
1081 printf("Floppy changed=%d\n", ret);
1090 static int raw_eject(BlockDriverState *bs, int eject_flag)
1092 BDRVRawState *s = bs->opaque;
1097 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1098 perror("CDROMEJECT");
1100 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1101 perror("CDROMEJECT");
1111 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1113 if (ioctl(fd, FDEJECT, 0) < 0)
1125 static int raw_set_locked(BlockDriverState *bs, int locked)
1127 BDRVRawState *s = bs->opaque;
1131 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1132 /* Note: an error can happen if the distribution automatically
1133 mounts the CD-ROM */
1134 // perror("CDROM_LOCKDOOR");
1143 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1145 BDRVRawState *s = bs->opaque;
1147 return ioctl(s->fd, req, buf);
1151 static int fd_open(BlockDriverState *bs)
1156 static int raw_is_inserted(BlockDriverState *bs)
1161 static int raw_media_changed(BlockDriverState *bs)
1166 static int raw_eject(BlockDriverState *bs, int eject_flag)
1171 static int raw_set_locked(BlockDriverState *bs, int locked)
1176 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1182 BlockDriver bdrv_host_device = {
1184 sizeof(BDRVRawState),
1185 NULL, /* no probe for protocols */
1194 .bdrv_aio_read = raw_aio_read,
1195 .bdrv_aio_write = raw_aio_write,
1196 .bdrv_aio_cancel = raw_aio_cancel,
1197 .aiocb_size = sizeof(RawAIOCB),
1199 .bdrv_pread = raw_pread,
1200 .bdrv_pwrite = raw_pwrite,
1201 .bdrv_getlength = raw_getlength,
1203 /* removable device support */
1204 .bdrv_is_inserted = raw_is_inserted,
1205 .bdrv_media_changed = raw_media_changed,
1206 .bdrv_eject = raw_eject,
1207 .bdrv_set_locked = raw_set_locked,
1208 /* generic scsi device */
1209 .bdrv_ioctl = raw_ioctl,