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"
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>
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
66 //#define DEBUG_FLOPPY
69 #if defined(DEBUG_BLOCK)
70 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
71 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
73 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
76 /* OS X does not have O_DSYNC */
78 #define O_DSYNC O_SYNC
81 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
83 #define O_DIRECT O_DSYNC
90 #define ALIGNED_BUFFER_SIZE (32 * 512)
92 /* if the FD is not accessed during that time (in ms), we try to
93 reopen it to see if the disk has been changed */
94 #define FD_OPEN_TIMEOUT 1000
96 /* posix-aio doesn't allow multiple outstanding requests to a single file
97 * descriptor. we implement a pool of dup()'d file descriptors to work
99 #define RAW_FD_POOL_SIZE 64
101 typedef struct BDRVRawState {
104 unsigned int lseek_err_cnt;
105 int fd_pool[RAW_FD_POOL_SIZE];
106 #if defined(__linux__)
107 /* linux floppy specific */
109 int64_t fd_open_time;
110 int64_t fd_error_time;
112 int fd_media_changed;
114 uint8_t* aligned_buf;
117 static int posix_aio_init(void);
119 static int fd_open(BlockDriverState *bs);
121 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
123 BDRVRawState *s = bs->opaque;
124 int fd, open_flags, ret;
129 s->lseek_err_cnt = 0;
131 open_flags = O_BINARY;
132 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
133 open_flags |= O_RDWR;
135 open_flags |= O_RDONLY;
138 if (flags & BDRV_O_CREAT)
139 open_flags |= O_CREAT | O_TRUNC;
141 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
142 * and O_DIRECT for no caching. */
143 if ((flags & BDRV_O_NOCACHE))
144 open_flags |= O_DIRECT;
145 else if (!(flags & BDRV_O_CACHE_WB))
146 open_flags |= O_DSYNC;
148 s->type = FTYPE_FILE;
150 fd = open(filename, open_flags, 0644);
158 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
160 s->aligned_buf = NULL;
161 if ((flags & BDRV_O_NOCACHE)) {
162 s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
163 if (s->aligned_buf == NULL) {
172 /* XXX: use host sector size if necessary with:
173 #ifdef DIOCGSECTORSIZE
175 unsigned int sectorsize = 512;
176 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
177 sectorsize > bufsize)
178 bufsize = sectorsize;
182 u_int32_t blockSize = 512;
183 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
190 * offset and count are in bytes, but must be multiples of 512 for files
191 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
193 * This function may be called without alignment if the caller ensures
194 * that O_DIRECT is not in effect.
196 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
197 uint8_t *buf, int count)
199 BDRVRawState *s = bs->opaque;
206 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
207 ++(s->lseek_err_cnt);
208 if(s->lseek_err_cnt <= 10) {
209 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
210 "] lseek failed : %d = %s\n",
211 s->fd, bs->filename, offset, buf, count,
212 bs->total_sectors, errno, strerror(errno));
218 ret = read(s->fd, buf, count);
220 goto label__raw_read__success;
222 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
223 "] read failed %d : %d = %s\n",
224 s->fd, bs->filename, offset, buf, count,
225 bs->total_sectors, ret, errno, strerror(errno));
227 /* Try harder for CDrom. */
228 if (bs->type == BDRV_TYPE_CDROM) {
229 lseek(s->fd, offset, SEEK_SET);
230 ret = read(s->fd, buf, count);
232 goto label__raw_read__success;
233 lseek(s->fd, offset, SEEK_SET);
234 ret = read(s->fd, buf, count);
236 goto label__raw_read__success;
238 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
239 "] retry read failed %d : %d = %s\n",
240 s->fd, bs->filename, offset, buf, count,
241 bs->total_sectors, ret, errno, strerror(errno));
244 label__raw_read__success:
250 * offset and count are in bytes, but must be multiples of 512 for files
251 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
253 * This function may be called without alignment if the caller ensures
254 * that O_DIRECT is not in effect.
256 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
257 const uint8_t *buf, int count)
259 BDRVRawState *s = bs->opaque;
266 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
267 ++(s->lseek_err_cnt);
268 if(s->lseek_err_cnt) {
269 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
270 PRId64 "] lseek failed : %d = %s\n",
271 s->fd, bs->filename, offset, buf, count,
272 bs->total_sectors, errno, strerror(errno));
276 s->lseek_err_cnt = 0;
278 ret = write(s->fd, buf, count);
280 goto label__raw_write__success;
282 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
283 "] write failed %d : %d = %s\n",
284 s->fd, bs->filename, offset, buf, count,
285 bs->total_sectors, ret, errno, strerror(errno));
287 label__raw_write__success:
294 * offset and count are in bytes and possibly not aligned. For files opened
295 * with O_DIRECT, necessary alignments are ensured before calling
296 * raw_pread_aligned to do the actual read.
298 static int raw_pread(BlockDriverState *bs, int64_t offset,
299 uint8_t *buf, int count)
301 BDRVRawState *s = bs->opaque;
302 int size, ret, shift, sum;
306 if (s->aligned_buf != NULL) {
308 if (offset & 0x1ff) {
309 /* align offset on a 512 bytes boundary */
311 shift = offset & 0x1ff;
312 size = (shift + count + 0x1ff) & ~0x1ff;
313 if (size > ALIGNED_BUFFER_SIZE)
314 size = ALIGNED_BUFFER_SIZE;
315 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
322 memcpy(buf, s->aligned_buf + shift, size);
332 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
334 /* read on aligned buffer */
338 size = (count + 0x1ff) & ~0x1ff;
339 if (size > ALIGNED_BUFFER_SIZE)
340 size = ALIGNED_BUFFER_SIZE;
342 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
350 memcpy(buf, s->aligned_buf, size);
362 return raw_pread_aligned(bs, offset, buf, count) + sum;
366 * offset and count are in bytes and possibly not aligned. For files opened
367 * with O_DIRECT, necessary alignments are ensured before calling
368 * raw_pwrite_aligned to do the actual write.
370 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
371 const uint8_t *buf, int count)
373 BDRVRawState *s = bs->opaque;
374 int size, ret, shift, sum;
378 if (s->aligned_buf != NULL) {
380 if (offset & 0x1ff) {
381 /* align offset on a 512 bytes boundary */
382 shift = offset & 0x1ff;
383 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
390 memcpy(s->aligned_buf + shift, buf, size);
392 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
404 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
406 while ((size = (count & ~0x1ff)) != 0) {
408 if (size > ALIGNED_BUFFER_SIZE)
409 size = ALIGNED_BUFFER_SIZE;
411 memcpy(s->aligned_buf, buf, size);
413 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
422 /* here, count < 512 because (count & ~0x1ff) == 0 */
424 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
427 memcpy(s->aligned_buf, buf, count);
429 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
440 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
444 /***********************************************************/
445 /* Unix AIO using POSIX AIO */
447 typedef struct RawAIOCB {
448 BlockDriverAIOCB common;
451 struct RawAIOCB *next;
455 typedef struct PosixAioState
461 static int raw_fd_pool_get(BDRVRawState *s)
465 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
467 if (s->fd_pool[i] != -1)
470 /* try to dup file descriptor */
471 s->fd_pool[i] = dup(s->fd);
472 if (s->fd_pool[i] != -1)
473 return s->fd_pool[i];
476 /* we couldn't dup the file descriptor so just use the main one */
480 static void raw_fd_pool_put(RawAIOCB *acb)
482 BDRVRawState *s = acb->common.bs->opaque;
485 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
486 if (s->fd_pool[i] == acb->fd) {
487 close(s->fd_pool[i]);
493 static void posix_aio_read(void *opaque)
495 PosixAioState *s = opaque;
496 RawAIOCB *acb, **pacb;
500 /* read all bytes from signal pipe */
504 len = read(s->rfd, bytes, sizeof(bytes));
505 if (len == -1 && errno == EINTR)
506 continue; /* try again */
507 if (len == sizeof(bytes))
508 continue; /* more to read */
513 pacb = &s->first_aio;
518 ret = aio_error(&acb->aiocb);
519 if (ret == ECANCELED) {
520 /* remove the request */
522 raw_fd_pool_put(acb);
523 qemu_aio_release(acb);
524 } else if (ret != EINPROGRESS) {
527 ret = aio_return(&acb->aiocb);
528 if (ret == acb->aiocb.aio_nbytes)
535 /* remove the request */
537 /* call the callback */
538 acb->common.cb(acb->common.opaque, ret);
539 raw_fd_pool_put(acb);
540 qemu_aio_release(acb);
550 static int posix_aio_flush(void *opaque)
552 PosixAioState *s = opaque;
553 return !!s->first_aio;
556 static PosixAioState *posix_aio_state;
558 static void aio_signal_handler(int signum)
560 if (posix_aio_state) {
563 write(posix_aio_state->wfd, &byte, sizeof(byte));
569 static int posix_aio_init(void)
571 struct sigaction act;
578 s = qemu_malloc(sizeof(PosixAioState));
582 sigfillset(&act.sa_mask);
583 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
584 act.sa_handler = aio_signal_handler;
585 sigaction(SIGUSR2, &act, NULL);
588 if (pipe(fds) == -1) {
589 fprintf(stderr, "failed to create pipe\n");
596 fcntl(s->rfd, F_SETFL, O_NONBLOCK);
597 fcntl(s->wfd, F_SETFL, O_NONBLOCK);
599 qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
601 #if defined(__linux__)
605 memset(&ai, 0, sizeof(ai));
606 #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 4)
610 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
611 seems to fix the problem. */
614 ai.aio_idle_time = 365 * 100000;
624 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
625 int64_t sector_num, uint8_t *buf, int nb_sectors,
626 BlockDriverCompletionFunc *cb, void *opaque)
628 BDRVRawState *s = bs->opaque;
634 acb = qemu_aio_get(bs, cb, opaque);
637 acb->fd = raw_fd_pool_get(s);
638 acb->aiocb.aio_fildes = acb->fd;
639 acb->aiocb.aio_sigevent.sigev_signo = SIGUSR2;
640 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
641 acb->aiocb.aio_buf = buf;
643 acb->aiocb.aio_nbytes = -nb_sectors;
645 acb->aiocb.aio_nbytes = nb_sectors * 512;
646 acb->aiocb.aio_offset = sector_num * 512;
647 acb->next = posix_aio_state->first_aio;
648 posix_aio_state->first_aio = acb;
652 static void raw_aio_em_cb(void* opaque)
654 RawAIOCB *acb = opaque;
655 acb->common.cb(acb->common.opaque, acb->ret);
656 qemu_aio_release(acb);
659 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
660 int64_t sector_num, uint8_t *buf, int nb_sectors,
661 BlockDriverCompletionFunc *cb, void *opaque)
666 * If O_DIRECT is used and the buffer is not aligned fall back
669 BDRVRawState *s = bs->opaque;
671 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
673 acb = qemu_aio_get(bs, cb, opaque);
674 acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors);
675 bh = qemu_bh_new(raw_aio_em_cb, acb);
676 qemu_bh_schedule(bh);
680 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
683 if (aio_read(&acb->aiocb) < 0) {
684 qemu_aio_release(acb);
690 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
691 int64_t sector_num, const uint8_t *buf, int nb_sectors,
692 BlockDriverCompletionFunc *cb, void *opaque)
697 * If O_DIRECT is used and the buffer is not aligned fall back
700 BDRVRawState *s = bs->opaque;
702 if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
704 acb = qemu_aio_get(bs, cb, opaque);
705 acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors);
706 bh = qemu_bh_new(raw_aio_em_cb, acb);
707 qemu_bh_schedule(bh);
711 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
714 if (aio_write(&acb->aiocb) < 0) {
715 qemu_aio_release(acb);
721 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
724 RawAIOCB *acb = (RawAIOCB *)blockacb;
727 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
728 if (ret == AIO_NOTCANCELED) {
729 /* fail safe: if the aio could not be canceled, we wait for
731 while (aio_error(&acb->aiocb) == EINPROGRESS);
734 /* remove the callback from the queue */
735 pacb = &posix_aio_state->first_aio;
739 } else if (*pacb == acb) {
741 raw_fd_pool_put(acb);
742 qemu_aio_release(acb);
749 #else /* CONFIG_AIO */
750 static int posix_aio_init(void)
754 #endif /* CONFIG_AIO */
756 static void raw_close_fd_pool(BDRVRawState *s)
760 for (i = 0; i < RAW_FD_POOL_SIZE; i++) {
761 if (s->fd_pool[i] != -1) {
762 close(s->fd_pool[i]);
768 static void raw_close(BlockDriverState *bs)
770 BDRVRawState *s = bs->opaque;
774 if (s->aligned_buf != NULL)
775 qemu_free(s->aligned_buf);
777 raw_close_fd_pool(s);
780 static int raw_truncate(BlockDriverState *bs, int64_t offset)
782 BDRVRawState *s = bs->opaque;
783 if (s->type != FTYPE_FILE)
785 if (ftruncate(s->fd, offset) < 0)
791 static int64_t raw_getlength(BlockDriverState *bs)
793 BDRVRawState *s = bs->opaque;
799 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
802 if (ioctl(fd, DIOCGDINFO, &dl))
804 return (uint64_t)dl.d_secsize *
805 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
809 #else /* !__OpenBSD__ */
810 static int64_t raw_getlength(BlockDriverState *bs)
812 BDRVRawState *s = bs->opaque;
819 struct dk_minfo minfo;
829 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
830 #ifdef DIOCGMEDIASIZE
831 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
834 size = LONG_LONG_MAX;
836 size = lseek(fd, 0LL, SEEK_END);
842 * use the DKIOCGMEDIAINFO ioctl to read the size.
844 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
846 size = minfo.dki_lbsize * minfo.dki_capacity;
847 } else /* there are reports that lseek on some devices
848 fails, but irc discussion said that contingency
849 on contingency was overkill */
852 size = lseek(fd, 0, SEEK_END);
858 static int raw_create(const char *filename, int64_t total_size,
859 const char *backing_file, int flags)
863 if (flags || backing_file)
866 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
870 ftruncate(fd, total_size * 512);
875 static void raw_flush(BlockDriverState *bs)
877 BDRVRawState *s = bs->opaque;
881 BlockDriver bdrv_raw = {
883 sizeof(BDRVRawState),
884 NULL, /* no probe for protocols */
893 .bdrv_aio_read = raw_aio_read,
894 .bdrv_aio_write = raw_aio_write,
895 .bdrv_aio_cancel = raw_aio_cancel,
896 .aiocb_size = sizeof(RawAIOCB),
898 .bdrv_pread = raw_pread,
899 .bdrv_pwrite = raw_pwrite,
900 .bdrv_truncate = raw_truncate,
901 .bdrv_getlength = raw_getlength,
904 /***********************************************/
908 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
909 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
911 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
913 kern_return_t kernResult;
914 mach_port_t masterPort;
915 CFMutableDictionaryRef classesToMatch;
917 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
918 if ( KERN_SUCCESS != kernResult ) {
919 printf( "IOMasterPort returned %d\n", kernResult );
922 classesToMatch = IOServiceMatching( kIOCDMediaClass );
923 if ( classesToMatch == NULL ) {
924 printf( "IOServiceMatching returned a NULL dictionary.\n" );
926 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
928 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
929 if ( KERN_SUCCESS != kernResult )
931 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
937 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
939 io_object_t nextMedia;
940 kern_return_t kernResult = KERN_FAILURE;
942 nextMedia = IOIteratorNext( mediaIterator );
945 CFTypeRef bsdPathAsCFString;
946 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
947 if ( bsdPathAsCFString ) {
948 size_t devPathLength;
949 strcpy( bsdPath, _PATH_DEV );
950 strcat( bsdPath, "r" );
951 devPathLength = strlen( bsdPath );
952 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
953 kernResult = KERN_SUCCESS;
955 CFRelease( bsdPathAsCFString );
957 IOObjectRelease( nextMedia );
965 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
967 BDRVRawState *s = bs->opaque;
968 int fd, open_flags, ret, i;
973 if (strstart(filename, "/dev/cdrom", NULL)) {
974 kern_return_t kernResult;
975 io_iterator_t mediaIterator;
976 char bsdPath[ MAXPATHLEN ];
979 kernResult = FindEjectableCDMedia( &mediaIterator );
980 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
982 if ( bsdPath[ 0 ] != '\0' ) {
983 strcat(bsdPath,"s0");
984 /* some CDs don't have a partition 0 */
985 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
987 bsdPath[strlen(bsdPath)-1] = '1';
995 IOObjectRelease( mediaIterator );
998 open_flags = O_BINARY;
999 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1000 open_flags |= O_RDWR;
1002 open_flags |= O_RDONLY;
1005 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
1006 * and O_DIRECT for no caching. */
1007 if ((flags & BDRV_O_NOCACHE))
1008 open_flags |= O_DIRECT;
1009 else if (!(flags & BDRV_O_CACHE_WB))
1010 open_flags |= O_DSYNC;
1012 s->type = FTYPE_FILE;
1013 #if defined(__linux__)
1014 if (strstart(filename, "/dev/cd", NULL)) {
1015 /* open will not fail even if no CD is inserted */
1016 open_flags |= O_NONBLOCK;
1018 } else if (strstart(filename, "/dev/fd", NULL)) {
1020 s->fd_open_flags = open_flags;
1021 /* open will not fail even if no floppy is inserted */
1022 open_flags |= O_NONBLOCK;
1023 } else if (strstart(filename, "/dev/sg", NULL)) {
1027 fd = open(filename, open_flags, 0644);
1035 for (i = 0; i < RAW_FD_POOL_SIZE; i++)
1037 #if defined(__linux__)
1038 /* close fd so that we can reopen it as needed */
1039 if (s->type == FTYPE_FD) {
1042 s->fd_media_changed = 1;
1048 #if defined(__linux__)
1049 /* Note: we do not have a reliable method to detect if the floppy is
1050 present. The current method is to try to open the floppy at every
1051 I/O and to keep it opened during a few hundreds of ms. */
1052 static int fd_open(BlockDriverState *bs)
1054 BDRVRawState *s = bs->opaque;
1055 int last_media_present;
1057 if (s->type != FTYPE_FD)
1059 last_media_present = (s->fd >= 0);
1061 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1064 raw_close_fd_pool(s);
1066 printf("Floppy closed\n");
1070 if (s->fd_got_error &&
1071 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1073 printf("No floppy (open delayed)\n");
1077 s->fd = open(bs->filename, s->fd_open_flags);
1079 s->fd_error_time = qemu_get_clock(rt_clock);
1080 s->fd_got_error = 1;
1081 if (last_media_present)
1082 s->fd_media_changed = 1;
1084 printf("No floppy\n");
1089 printf("Floppy opened\n");
1092 if (!last_media_present)
1093 s->fd_media_changed = 1;
1094 s->fd_open_time = qemu_get_clock(rt_clock);
1095 s->fd_got_error = 0;
1099 static int raw_is_inserted(BlockDriverState *bs)
1101 BDRVRawState *s = bs->opaque;
1106 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1107 if (ret == CDS_DISC_OK)
1120 /* currently only used by fdc.c, but a CD version would be good too */
1121 static int raw_media_changed(BlockDriverState *bs)
1123 BDRVRawState *s = bs->opaque;
1129 /* XXX: we do not have a true media changed indication. It
1130 does not work if the floppy is changed without trying
1133 ret = s->fd_media_changed;
1134 s->fd_media_changed = 0;
1136 printf("Floppy changed=%d\n", ret);
1145 static int raw_eject(BlockDriverState *bs, int eject_flag)
1147 BDRVRawState *s = bs->opaque;
1152 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
1153 perror("CDROMEJECT");
1155 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
1156 perror("CDROMEJECT");
1165 raw_close_fd_pool(s);
1167 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
1169 if (ioctl(fd, FDEJECT, 0) < 0)
1181 static int raw_set_locked(BlockDriverState *bs, int locked)
1183 BDRVRawState *s = bs->opaque;
1187 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
1188 /* Note: an error can happen if the distribution automatically
1189 mounts the CD-ROM */
1190 // perror("CDROM_LOCKDOOR");
1199 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1201 BDRVRawState *s = bs->opaque;
1203 return ioctl(s->fd, req, buf);
1207 static int fd_open(BlockDriverState *bs)
1212 static int raw_is_inserted(BlockDriverState *bs)
1217 static int raw_media_changed(BlockDriverState *bs)
1222 static int raw_eject(BlockDriverState *bs, int eject_flag)
1227 static int raw_set_locked(BlockDriverState *bs, int locked)
1232 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1238 BlockDriver bdrv_host_device = {
1240 sizeof(BDRVRawState),
1241 NULL, /* no probe for protocols */
1250 .bdrv_aio_read = raw_aio_read,
1251 .bdrv_aio_write = raw_aio_write,
1252 .bdrv_aio_cancel = raw_aio_cancel,
1253 .aiocb_size = sizeof(RawAIOCB),
1255 .bdrv_pread = raw_pread,
1256 .bdrv_pwrite = raw_pwrite,
1257 .bdrv_getlength = raw_getlength,
1259 /* removable device support */
1260 .bdrv_is_inserted = raw_is_inserted,
1261 .bdrv_media_changed = raw_media_changed,
1262 .bdrv_eject = raw_eject,
1263 .bdrv_set_locked = raw_set_locked,
1264 /* generic scsi device */
1265 .bdrv_ioctl = raw_ioctl,