Merge branch 'qemu-cvs'
[qemu-kvm/fedora.git] / block-raw-posix.c
blob74657fb658f1d99b6a2dbe4707427449e5fd764b
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #ifndef QEMU_IMG
26 #include "qemu-kvm.h"
27 #include "qemu-timer.h"
28 #include "exec-all.h"
29 #endif
30 #include "block_int.h"
31 #include <assert.h>
32 #include <aio.h>
34 #ifdef CONFIG_COCOA
35 #include <paths.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>
44 #endif
46 #ifdef __sun__
47 #define _POSIX_PTHREAD_SEMANTICS 1
48 #include <signal.h>
49 #include <sys/dkio.h>
50 #endif
51 #ifdef __linux__
52 #include <sys/ioctl.h>
53 #include <linux/cdrom.h>
54 #include <linux/fd.h>
55 #endif
56 #ifdef __FreeBSD__
57 #include <sys/disk.h>
58 #endif
60 //#define DEBUG_FLOPPY
62 //#define DEBUG_BLOCK
63 #if defined(DEBUG_BLOCK) && !defined(QEMU_IMG)
64 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \
65 { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
66 #else
67 #define DEBUG_BLOCK_PRINT(formatCstr, args...)
68 #endif
70 #define FTYPE_FILE 0
71 #define FTYPE_CD 1
72 #define FTYPE_FD 2
74 /* if the FD is not accessed during that time (in ms), we try to
75 reopen it to see if the disk has been changed */
76 #define FD_OPEN_TIMEOUT 1000
78 typedef struct BDRVRawState {
79 int fd;
80 int type;
81 unsigned int lseek_err_cnt;
82 #if defined(__linux__)
83 /* linux floppy specific */
84 int fd_open_flags;
85 int64_t fd_open_time;
86 int64_t fd_error_time;
87 int fd_got_error;
88 int fd_media_changed;
89 #endif
90 } BDRVRawState;
92 static int fd_open(BlockDriverState *bs);
94 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
96 BDRVRawState *s = bs->opaque;
97 int fd, open_flags, ret;
99 s->lseek_err_cnt = 0;
101 open_flags = O_BINARY;
102 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
103 open_flags |= O_RDWR;
104 } else {
105 open_flags |= O_RDONLY;
106 bs->read_only = 1;
108 if (flags & BDRV_O_CREAT)
109 open_flags |= O_CREAT | O_TRUNC;
110 #ifdef O_DIRECT
111 if (flags & BDRV_O_DIRECT)
112 open_flags |= O_DIRECT;
113 #endif
115 s->type = FTYPE_FILE;
117 fd = open(filename, open_flags, 0644);
118 if (fd < 0) {
119 ret = -errno;
120 if (ret == -EROFS)
121 ret = -EACCES;
122 return ret;
124 s->fd = fd;
125 return 0;
128 /* XXX: use host sector size if necessary with:
129 #ifdef DIOCGSECTORSIZE
131 unsigned int sectorsize = 512;
132 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
133 sectorsize > bufsize)
134 bufsize = sectorsize;
136 #endif
137 #ifdef CONFIG_COCOA
138 u_int32_t blockSize = 512;
139 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
140 bufsize = blockSize;
142 #endif
145 static int raw_pread(BlockDriverState *bs, int64_t offset,
146 uint8_t *buf, int count)
148 BDRVRawState *s = bs->opaque;
149 int ret;
151 ret = fd_open(bs);
152 if (ret < 0)
153 return ret;
155 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
156 ++(s->lseek_err_cnt);
157 if(s->lseek_err_cnt <= 10) {
158 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
159 "] lseek failed : %d = %s\n",
160 s->fd, bs->filename, offset, buf, count,
161 bs->total_sectors, errno, strerror(errno));
163 return -1;
165 s->lseek_err_cnt=0;
167 ret = read(s->fd, buf, count);
168 if (ret == count)
169 goto label__raw_read__success;
171 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
172 "] read failed %d : %d = %s\n",
173 s->fd, bs->filename, offset, buf, count,
174 bs->total_sectors, ret, errno, strerror(errno));
176 /* Try harder for CDrom. */
177 if (bs->type == BDRV_TYPE_CDROM) {
178 lseek(s->fd, offset, SEEK_SET);
179 ret = read(s->fd, buf, count);
180 if (ret == count)
181 goto label__raw_read__success;
182 lseek(s->fd, offset, SEEK_SET);
183 ret = read(s->fd, buf, count);
184 if (ret == count)
185 goto label__raw_read__success;
187 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
188 "] retry read failed %d : %d = %s\n",
189 s->fd, bs->filename, offset, buf, count,
190 bs->total_sectors, ret, errno, strerror(errno));
193 label__raw_read__success:
195 return ret;
198 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
199 const uint8_t *buf, int count)
201 BDRVRawState *s = bs->opaque;
202 int ret;
204 ret = fd_open(bs);
205 if (ret < 0)
206 return ret;
208 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
209 ++(s->lseek_err_cnt);
210 if(s->lseek_err_cnt) {
211 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
212 PRId64 "] lseek failed : %d = %s\n",
213 s->fd, bs->filename, offset, buf, count,
214 bs->total_sectors, errno, strerror(errno));
216 return -1;
218 s->lseek_err_cnt = 0;
220 ret = write(s->fd, buf, count);
221 if (ret == count)
222 goto label__raw_write__success;
224 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
225 "] write failed %d : %d = %s\n",
226 s->fd, bs->filename, offset, buf, count,
227 bs->total_sectors, ret, errno, strerror(errno));
229 label__raw_write__success:
231 return ret;
234 /***********************************************************/
235 /* Unix AIO using POSIX AIO */
237 typedef struct RawAIOCB {
238 BlockDriverAIOCB common;
239 struct aiocb aiocb;
240 struct RawAIOCB *next;
241 } RawAIOCB;
243 static int aio_sig_num = SIGUSR2;
244 static RawAIOCB *first_aio; /* AIO issued */
245 static int aio_initialized = 0;
247 static void aio_signal_handler(int signum)
249 #ifndef QEMU_IMG
250 CPUState *env = cpu_single_env;
251 if (env) {
252 /* stop the currently executing cpu because a timer occured */
253 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
254 #ifdef USE_KQEMU
255 if (env->kqemu_enabled) {
256 kqemu_cpu_interrupt(env);
258 #endif
260 #endif
263 void qemu_aio_init(void)
265 struct sigaction act;
267 aio_initialized = 1;
269 sigfillset(&act.sa_mask);
270 act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
271 act.sa_handler = aio_signal_handler;
272 sigaction(aio_sig_num, &act, NULL);
274 #if defined(__GLIBC__) && defined(__linux__)
276 /* XXX: aio thread exit seems to hang on RedHat 9 and this init
277 seems to fix the problem. */
278 struct aioinit ai;
279 memset(&ai, 0, sizeof(ai));
280 ai.aio_threads = 1;
281 ai.aio_num = 1;
282 ai.aio_idle_time = 365 * 100000;
283 aio_init(&ai);
285 #endif
288 void qemu_aio_poll(void)
290 RawAIOCB *acb, **pacb;
291 int ret;
293 for(;;) {
294 pacb = &first_aio;
295 for(;;) {
296 acb = *pacb;
297 if (!acb)
298 goto the_end;
299 ret = aio_error(&acb->aiocb);
300 if (ret == ECANCELED) {
301 /* remove the request */
302 *pacb = acb->next;
303 qemu_aio_release(acb);
304 } else if (ret != EINPROGRESS) {
305 /* end of aio */
306 if (ret == 0) {
307 ret = aio_return(&acb->aiocb);
308 if (ret == acb->aiocb.aio_nbytes)
309 ret = 0;
310 else
311 ret = -EINVAL;
312 } else {
313 ret = -ret;
315 /* remove the request */
316 *pacb = acb->next;
317 /* call the callback */
318 acb->common.cb(acb->common.opaque, ret);
319 qemu_aio_release(acb);
320 break;
321 } else {
322 pacb = &acb->next;
326 the_end: ;
329 /* Wait for all IO requests to complete. */
330 void qemu_aio_flush(void)
332 qemu_aio_wait_start();
333 qemu_aio_poll();
334 while (first_aio) {
335 qemu_aio_wait();
337 qemu_aio_wait_end();
340 /* wait until at least one AIO was handled */
341 static sigset_t wait_oset;
343 void qemu_aio_wait_start(void)
345 sigset_t set;
347 if (!aio_initialized)
348 qemu_aio_init();
349 #ifndef QEMU_IMG
350 if (kvm_enabled()) {
351 qemu_kvm_aio_wait_start();
352 return;
354 #endif
355 sigemptyset(&set);
356 sigaddset(&set, aio_sig_num);
357 sigprocmask(SIG_BLOCK, &set, &wait_oset);
360 void qemu_aio_wait(void)
362 sigset_t set;
363 int nb_sigs;
365 #ifndef QEMU_IMG
366 if (qemu_bh_poll())
367 return;
368 if (kvm_enabled()) {
369 qemu_kvm_aio_wait();
370 qemu_aio_poll();
371 return;
373 #endif
374 sigemptyset(&set);
375 sigaddset(&set, aio_sig_num);
376 sigwait(&set, &nb_sigs);
377 qemu_aio_poll();
380 void qemu_aio_wait_end(void)
382 #ifndef QEMU_IMG
383 if (kvm_enabled()) {
384 qemu_kvm_aio_wait_end();
385 return;
387 #endif
388 sigprocmask(SIG_SETMASK, &wait_oset, NULL);
391 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
392 int64_t sector_num, uint8_t *buf, int nb_sectors,
393 BlockDriverCompletionFunc *cb, void *opaque)
395 BDRVRawState *s = bs->opaque;
396 RawAIOCB *acb;
398 if (fd_open(bs) < 0)
399 return NULL;
401 acb = qemu_aio_get(bs, cb, opaque);
402 if (!acb)
403 return NULL;
404 acb->aiocb.aio_fildes = s->fd;
405 acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
406 acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
407 acb->aiocb.aio_buf = buf;
408 if (nb_sectors < 0)
409 acb->aiocb.aio_nbytes = -nb_sectors;
410 else
411 acb->aiocb.aio_nbytes = nb_sectors * 512;
412 acb->aiocb.aio_offset = sector_num * 512;
413 acb->next = first_aio;
414 first_aio = acb;
415 return acb;
418 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
419 int64_t sector_num, uint8_t *buf, int nb_sectors,
420 BlockDriverCompletionFunc *cb, void *opaque)
422 RawAIOCB *acb;
424 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
425 if (!acb)
426 return NULL;
427 if (aio_read(&acb->aiocb) < 0) {
428 qemu_aio_release(acb);
429 return NULL;
431 return &acb->common;
434 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
435 int64_t sector_num, const uint8_t *buf, int nb_sectors,
436 BlockDriverCompletionFunc *cb, void *opaque)
438 RawAIOCB *acb;
440 acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
441 if (!acb)
442 return NULL;
443 if (aio_write(&acb->aiocb) < 0) {
444 qemu_aio_release(acb);
445 return NULL;
447 return &acb->common;
450 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
452 int ret;
453 RawAIOCB *acb = (RawAIOCB *)blockacb;
454 RawAIOCB **pacb;
456 ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
457 if (ret == AIO_NOTCANCELED) {
458 /* fail safe: if the aio could not be canceled, we wait for
459 it */
460 while (aio_error(&acb->aiocb) == EINPROGRESS);
463 /* remove the callback from the queue */
464 pacb = &first_aio;
465 for(;;) {
466 if (*pacb == NULL) {
467 break;
468 } else if (*pacb == acb) {
469 *pacb = acb->next;
470 qemu_aio_release(acb);
471 break;
473 pacb = &acb->next;
477 static void raw_close(BlockDriverState *bs)
479 BDRVRawState *s = bs->opaque;
480 if (s->fd >= 0) {
481 close(s->fd);
482 s->fd = -1;
486 static int raw_truncate(BlockDriverState *bs, int64_t offset)
488 BDRVRawState *s = bs->opaque;
489 if (s->type != FTYPE_FILE)
490 return -ENOTSUP;
491 if (ftruncate(s->fd, offset) < 0)
492 return -errno;
493 return 0;
496 static int64_t raw_getlength(BlockDriverState *bs)
498 BDRVRawState *s = bs->opaque;
499 int fd = s->fd;
500 int64_t size;
501 #ifdef _BSD
502 struct stat sb;
503 #endif
504 #ifdef __sun__
505 struct dk_minfo minfo;
506 int rv;
507 #endif
508 int ret;
510 ret = fd_open(bs);
511 if (ret < 0)
512 return ret;
514 #ifdef _BSD
515 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
516 #ifdef DIOCGMEDIASIZE
517 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
518 #endif
519 #ifdef CONFIG_COCOA
520 size = LONG_LONG_MAX;
521 #else
522 size = lseek(fd, 0LL, SEEK_END);
523 #endif
524 } else
525 #endif
526 #ifdef __sun__
528 * use the DKIOCGMEDIAINFO ioctl to read the size.
530 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
531 if ( rv != -1 ) {
532 size = minfo.dki_lbsize * minfo.dki_capacity;
533 } else /* there are reports that lseek on some devices
534 fails, but irc discussion said that contingency
535 on contingency was overkill */
536 #endif
538 size = lseek(fd, 0, SEEK_END);
540 return size;
543 static int raw_create(const char *filename, int64_t total_size,
544 const char *backing_file, int flags)
546 int fd;
548 if (flags || backing_file)
549 return -ENOTSUP;
551 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
552 0644);
553 if (fd < 0)
554 return -EIO;
555 ftruncate(fd, total_size * 512);
556 close(fd);
557 return 0;
560 static void raw_flush(BlockDriverState *bs)
562 BDRVRawState *s = bs->opaque;
563 fsync(s->fd);
566 BlockDriver bdrv_raw = {
567 "raw",
568 sizeof(BDRVRawState),
569 NULL, /* no probe for protocols */
570 raw_open,
571 NULL,
572 NULL,
573 raw_close,
574 raw_create,
575 raw_flush,
577 .bdrv_aio_read = raw_aio_read,
578 .bdrv_aio_write = raw_aio_write,
579 .bdrv_aio_cancel = raw_aio_cancel,
580 .aiocb_size = sizeof(RawAIOCB),
581 .protocol_name = "file",
582 .bdrv_pread = raw_pread,
583 .bdrv_pwrite = raw_pwrite,
584 .bdrv_truncate = raw_truncate,
585 .bdrv_getlength = raw_getlength,
588 /***********************************************/
589 /* host device */
591 #ifdef CONFIG_COCOA
592 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
593 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
595 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
597 kern_return_t kernResult;
598 mach_port_t masterPort;
599 CFMutableDictionaryRef classesToMatch;
601 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
602 if ( KERN_SUCCESS != kernResult ) {
603 printf( "IOMasterPort returned %d\n", kernResult );
606 classesToMatch = IOServiceMatching( kIOCDMediaClass );
607 if ( classesToMatch == NULL ) {
608 printf( "IOServiceMatching returned a NULL dictionary.\n" );
609 } else {
610 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
612 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
613 if ( KERN_SUCCESS != kernResult )
615 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
618 return kernResult;
621 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
623 io_object_t nextMedia;
624 kern_return_t kernResult = KERN_FAILURE;
625 *bsdPath = '\0';
626 nextMedia = IOIteratorNext( mediaIterator );
627 if ( nextMedia )
629 CFTypeRef bsdPathAsCFString;
630 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
631 if ( bsdPathAsCFString ) {
632 size_t devPathLength;
633 strcpy( bsdPath, _PATH_DEV );
634 strcat( bsdPath, "r" );
635 devPathLength = strlen( bsdPath );
636 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
637 kernResult = KERN_SUCCESS;
639 CFRelease( bsdPathAsCFString );
641 IOObjectRelease( nextMedia );
644 return kernResult;
647 #endif
649 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
651 BDRVRawState *s = bs->opaque;
652 int fd, open_flags, ret;
654 #ifdef CONFIG_COCOA
655 if (strstart(filename, "/dev/cdrom", NULL)) {
656 kern_return_t kernResult;
657 io_iterator_t mediaIterator;
658 char bsdPath[ MAXPATHLEN ];
659 int fd;
661 kernResult = FindEjectableCDMedia( &mediaIterator );
662 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
664 if ( bsdPath[ 0 ] != '\0' ) {
665 strcat(bsdPath,"s0");
666 /* some CDs don't have a partition 0 */
667 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
668 if (fd < 0) {
669 bsdPath[strlen(bsdPath)-1] = '1';
670 } else {
671 close(fd);
673 filename = bsdPath;
676 if ( mediaIterator )
677 IOObjectRelease( mediaIterator );
679 #endif
680 open_flags = O_BINARY;
681 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
682 open_flags |= O_RDWR;
683 } else {
684 open_flags |= O_RDONLY;
685 bs->read_only = 1;
687 #ifdef O_DIRECT
688 if (flags & BDRV_O_DIRECT)
689 open_flags |= O_DIRECT;
690 #endif
692 s->type = FTYPE_FILE;
693 #if defined(__linux__)
694 if (strstart(filename, "/dev/cd", NULL)) {
695 /* open will not fail even if no CD is inserted */
696 open_flags |= O_NONBLOCK;
697 s->type = FTYPE_CD;
698 } else if (strstart(filename, "/dev/fd", NULL)) {
699 s->type = FTYPE_FD;
700 s->fd_open_flags = open_flags;
701 /* open will not fail even if no floppy is inserted */
702 open_flags |= O_NONBLOCK;
703 } else if (strstart(filename, "/dev/sg", NULL)) {
704 bs->sg = 1;
706 #endif
707 fd = open(filename, open_flags, 0644);
708 if (fd < 0) {
709 ret = -errno;
710 if (ret == -EROFS)
711 ret = -EACCES;
712 return ret;
714 s->fd = fd;
715 #if defined(__linux__)
716 /* close fd so that we can reopen it as needed */
717 if (s->type == FTYPE_FD) {
718 close(s->fd);
719 s->fd = -1;
720 s->fd_media_changed = 1;
722 #endif
723 return 0;
726 #if defined(__linux__) && !defined(QEMU_IMG)
728 /* Note: we do not have a reliable method to detect if the floppy is
729 present. The current method is to try to open the floppy at every
730 I/O and to keep it opened during a few hundreds of ms. */
731 static int fd_open(BlockDriverState *bs)
733 BDRVRawState *s = bs->opaque;
734 int last_media_present;
736 if (s->type != FTYPE_FD)
737 return 0;
738 last_media_present = (s->fd >= 0);
739 if (s->fd >= 0 &&
740 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
741 close(s->fd);
742 s->fd = -1;
743 #ifdef DEBUG_FLOPPY
744 printf("Floppy closed\n");
745 #endif
747 if (s->fd < 0) {
748 if (s->fd_got_error &&
749 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
750 #ifdef DEBUG_FLOPPY
751 printf("No floppy (open delayed)\n");
752 #endif
753 return -EIO;
755 s->fd = open(bs->filename, s->fd_open_flags);
756 if (s->fd < 0) {
757 s->fd_error_time = qemu_get_clock(rt_clock);
758 s->fd_got_error = 1;
759 if (last_media_present)
760 s->fd_media_changed = 1;
761 #ifdef DEBUG_FLOPPY
762 printf("No floppy\n");
763 #endif
764 return -EIO;
766 #ifdef DEBUG_FLOPPY
767 printf("Floppy opened\n");
768 #endif
770 if (!last_media_present)
771 s->fd_media_changed = 1;
772 s->fd_open_time = qemu_get_clock(rt_clock);
773 s->fd_got_error = 0;
774 return 0;
776 #else
777 static int fd_open(BlockDriverState *bs)
779 return 0;
781 #endif
783 #if defined(__linux__)
785 static int raw_is_inserted(BlockDriverState *bs)
787 BDRVRawState *s = bs->opaque;
788 int ret;
790 switch(s->type) {
791 case FTYPE_CD:
792 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
793 if (ret == CDS_DISC_OK)
794 return 1;
795 else
796 return 0;
797 break;
798 case FTYPE_FD:
799 ret = fd_open(bs);
800 return (ret >= 0);
801 default:
802 return 1;
806 /* currently only used by fdc.c, but a CD version would be good too */
807 static int raw_media_changed(BlockDriverState *bs)
809 BDRVRawState *s = bs->opaque;
811 switch(s->type) {
812 case FTYPE_FD:
814 int ret;
815 /* XXX: we do not have a true media changed indication. It
816 does not work if the floppy is changed without trying
817 to read it */
818 fd_open(bs);
819 ret = s->fd_media_changed;
820 s->fd_media_changed = 0;
821 #ifdef DEBUG_FLOPPY
822 printf("Floppy changed=%d\n", ret);
823 #endif
824 return ret;
826 default:
827 return -ENOTSUP;
831 static int raw_eject(BlockDriverState *bs, int eject_flag)
833 BDRVRawState *s = bs->opaque;
835 switch(s->type) {
836 case FTYPE_CD:
837 if (eject_flag) {
838 if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
839 perror("CDROMEJECT");
840 } else {
841 if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
842 perror("CDROMEJECT");
844 break;
845 case FTYPE_FD:
847 int fd;
848 if (s->fd >= 0) {
849 close(s->fd);
850 s->fd = -1;
852 fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
853 if (fd >= 0) {
854 if (ioctl(fd, FDEJECT, 0) < 0)
855 perror("FDEJECT");
856 close(fd);
859 break;
860 default:
861 return -ENOTSUP;
863 return 0;
866 static int raw_set_locked(BlockDriverState *bs, int locked)
868 BDRVRawState *s = bs->opaque;
870 switch(s->type) {
871 case FTYPE_CD:
872 if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
873 /* Note: an error can happen if the distribution automatically
874 mounts the CD-ROM */
875 // perror("CDROM_LOCKDOOR");
877 break;
878 default:
879 return -ENOTSUP;
881 return 0;
884 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
886 BDRVRawState *s = bs->opaque;
888 return ioctl(s->fd, req, buf);
890 #else
892 static int raw_is_inserted(BlockDriverState *bs)
894 return 1;
897 static int raw_media_changed(BlockDriverState *bs)
899 return -ENOTSUP;
902 static int raw_eject(BlockDriverState *bs, int eject_flag)
904 return -ENOTSUP;
907 static int raw_set_locked(BlockDriverState *bs, int locked)
909 return -ENOTSUP;
912 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
914 return -ENOTSUP;
916 #endif /* !linux */
918 BlockDriver bdrv_host_device = {
919 "host_device",
920 sizeof(BDRVRawState),
921 NULL, /* no probe for protocols */
922 hdev_open,
923 NULL,
924 NULL,
925 raw_close,
926 NULL,
927 raw_flush,
929 .bdrv_aio_read = raw_aio_read,
930 .bdrv_aio_write = raw_aio_write,
931 .bdrv_aio_cancel = raw_aio_cancel,
932 .aiocb_size = sizeof(RawAIOCB),
933 .bdrv_pread = raw_pread,
934 .bdrv_pwrite = raw_pwrite,
935 .bdrv_getlength = raw_getlength,
937 /* removable device support */
938 .bdrv_is_inserted = raw_is_inserted,
939 .bdrv_media_changed = raw_media_changed,
940 .bdrv_eject = raw_eject,
941 .bdrv_set_locked = raw_set_locked,
942 /* generic scsi device */
943 .bdrv_ioctl = raw_ioctl,