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"
28 #include "block_int.h"
30 #include "block/raw-posix-aio.h"
34 #include <sys/param.h>
35 #include <IOKit/IOKitLib.h>
36 #include <IOKit/IOBSD.h>
37 #include <IOKit/storage/IOMediaBSDClient.h>
38 #include <IOKit/storage/IOMedia.h>
39 #include <IOKit/storage/IOCDMedia.h>
40 //#include <IOKit/storage/IOCDTypes.h>
41 #include <CoreFoundation/CoreFoundation.h>
45 #define _POSIX_PTHREAD_SEMANTICS 1
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
52 #include <sys/param.h>
53 #include <linux/cdrom.h>
56 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
68 #include <sys/ioctl.h>
69 #include <sys/disklabel.h>
75 #include <sys/ioctl.h>
76 #include <sys/diskslice.h>
83 //#define DEBUG_FLOPPY
86 #if defined(DEBUG_BLOCK)
87 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
88 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
90 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
93 /* OS X does not have O_DSYNC */
96 #define O_DSYNC O_SYNC
97 #elif defined(O_FSYNC)
98 #define O_DSYNC O_FSYNC
102 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
104 #define O_DIRECT O_DSYNC
111 /* if the FD is not accessed during that time (in ns), we try to
112 reopen it to see if the disk has been changed */
113 #define FD_OPEN_TIMEOUT (1000000000)
115 #define MAX_BLOCKSIZE 4096
117 typedef struct BDRVRawState {
121 #if defined(__linux__)
122 /* linux floppy specific */
123 int64_t fd_open_time;
124 int64_t fd_error_time;
126 int fd_media_changed;
128 #ifdef CONFIG_LINUX_AIO
132 uint8_t *aligned_buf;
133 unsigned aligned_buf_size;
139 static int fd_open(BlockDriverState *bs);
140 static int64_t raw_getlength(BlockDriverState *bs);
142 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
143 static int cdrom_reopen(BlockDriverState *bs);
146 #if defined(__NetBSD__)
147 static int raw_normalize_devicepath(const char **filename)
149 static char namebuf[PATH_MAX];
150 const char *dp, *fname;
154 dp = strrchr(fname, '/');
155 if (lstat(fname, &sb) < 0) {
156 fprintf(stderr, "%s: stat failed: %s\n",
157 fname, strerror(errno));
161 if (!S_ISBLK(sb.st_mode)) {
166 snprintf(namebuf, PATH_MAX, "r%s", fname);
168 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
169 (int)(dp - fname), fname, dp + 1);
171 fprintf(stderr, "%s is a block device", fname);
173 fprintf(stderr, ", using %s\n", *filename);
178 static int raw_normalize_devicepath(const char **filename)
184 static int raw_open_common(BlockDriverState *bs, const char *filename,
185 int bdrv_flags, int open_flags)
187 BDRVRawState *s = bs->opaque;
190 ret = raw_normalize_devicepath(&filename);
195 s->open_flags = open_flags | O_BINARY;
196 s->open_flags &= ~O_ACCMODE;
197 if (bdrv_flags & BDRV_O_RDWR) {
198 s->open_flags |= O_RDWR;
200 s->open_flags |= O_RDONLY;
203 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
204 * and O_DIRECT for no caching. */
205 if ((bdrv_flags & BDRV_O_NOCACHE))
206 s->open_flags |= O_DIRECT;
207 if (!(bdrv_flags & BDRV_O_CACHE_WB))
208 s->open_flags |= O_DSYNC;
211 fd = qemu_open(filename, s->open_flags, 0644);
219 s->aligned_buf = NULL;
221 if ((bdrv_flags & BDRV_O_NOCACHE)) {
223 * Allocate a buffer for read/modify/write cycles. Chose the size
224 * pessimistically as we don't know the block size yet.
226 s->aligned_buf_size = 32 * MAX_BLOCKSIZE;
227 s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);
228 if (s->aligned_buf == NULL) {
233 /* We're falling back to POSIX AIO in some cases so init always */
234 if (paio_init() < 0) {
238 #ifdef CONFIG_LINUX_AIO
240 * Currently Linux do AIO only for files opened with O_DIRECT
241 * specified so check NOCACHE flag too
243 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
244 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
246 s->aio_ctx = laio_init();
254 #ifdef CONFIG_LINUX_AIO
260 if (platform_test_xfs_fd(s->fd)) {
268 qemu_vfree(s->aligned_buf);
274 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
276 BDRVRawState *s = bs->opaque;
278 s->type = FTYPE_FILE;
279 return raw_open_common(bs, filename, flags, 0);
282 /* XXX: use host sector size if necessary with:
283 #ifdef DIOCGSECTORSIZE
285 unsigned int sectorsize = 512;
286 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
287 sectorsize > bufsize)
288 bufsize = sectorsize;
292 uint32_t blockSize = 512;
293 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
300 * Check if all memory in this vector is sector aligned.
302 static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
306 for (i = 0; i < qiov->niov; i++) {
307 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
315 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
316 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
317 BlockDriverCompletionFunc *cb, void *opaque, int type)
319 BDRVRawState *s = bs->opaque;
325 * If O_DIRECT is used the buffer needs to be aligned on a sector
326 * boundary. Check if this is the case or tell the low-level
327 * driver that it needs to copy the buffer.
329 if (s->aligned_buf) {
330 if (!qiov_is_aligned(bs, qiov)) {
331 type |= QEMU_AIO_MISALIGNED;
332 #ifdef CONFIG_LINUX_AIO
333 } else if (s->use_aio) {
334 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
335 nb_sectors, cb, opaque, type);
340 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
344 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
345 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
346 BlockDriverCompletionFunc *cb, void *opaque)
348 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
349 cb, opaque, QEMU_AIO_READ);
352 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
353 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
354 BlockDriverCompletionFunc *cb, void *opaque)
356 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
357 cb, opaque, QEMU_AIO_WRITE);
360 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
361 BlockDriverCompletionFunc *cb, void *opaque)
363 BDRVRawState *s = bs->opaque;
368 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
371 static void raw_close(BlockDriverState *bs)
373 BDRVRawState *s = bs->opaque;
377 if (s->aligned_buf != NULL)
378 qemu_vfree(s->aligned_buf);
382 static int raw_truncate(BlockDriverState *bs, int64_t offset)
384 BDRVRawState *s = bs->opaque;
387 if (fstat(s->fd, &st)) {
391 if (S_ISREG(st.st_mode)) {
392 if (ftruncate(s->fd, offset) < 0) {
395 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
396 if (offset > raw_getlength(bs)) {
407 static int64_t raw_getlength(BlockDriverState *bs)
409 BDRVRawState *s = bs->opaque;
415 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
418 if (ioctl(fd, DIOCGDINFO, &dl))
420 return (uint64_t)dl.d_secsize *
421 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
425 #elif defined(__NetBSD__)
426 static int64_t raw_getlength(BlockDriverState *bs)
428 BDRVRawState *s = bs->opaque;
434 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
435 struct dkwedge_info dkw;
437 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
438 return dkw.dkw_size * 512;
442 if (ioctl(fd, DIOCGDINFO, &dl))
444 return (uint64_t)dl.d_secsize *
445 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
450 #elif defined(__sun__)
451 static int64_t raw_getlength(BlockDriverState *bs)
453 BDRVRawState *s = bs->opaque;
454 struct dk_minfo minfo;
463 * Use the DKIOCGMEDIAINFO ioctl to read the size.
465 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
467 return minfo.dki_lbsize * minfo.dki_capacity;
471 * There are reports that lseek on some devices fails, but
472 * irc discussion said that contingency on contingency was overkill.
474 return lseek(s->fd, 0, SEEK_END);
476 #elif defined(CONFIG_BSD)
477 static int64_t raw_getlength(BlockDriverState *bs)
479 BDRVRawState *s = bs->opaque;
483 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
492 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
495 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
496 #ifdef DIOCGMEDIASIZE
497 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
498 #elif defined(DIOCGPART)
501 if (ioctl(fd, DIOCGPART, &pi) == 0)
502 size = pi.media_size;
509 size = LONG_LONG_MAX;
511 size = lseek(fd, 0LL, SEEK_END);
513 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
516 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
517 if (size == 2048LL * (unsigned)-1)
519 /* XXX no disc? maybe we need to reopen... */
520 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
527 size = lseek(fd, 0, SEEK_END);
532 static int64_t raw_getlength(BlockDriverState *bs)
534 BDRVRawState *s = bs->opaque;
542 return lseek(s->fd, 0, SEEK_END);
546 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
549 BDRVRawState *s = bs->opaque;
551 if (fstat(s->fd, &st) < 0) {
554 return (int64_t)st.st_blocks * 512;
557 static int raw_create(const char *filename, QEMUOptionParameter *options)
561 int64_t total_size = 0;
563 /* Read out options */
564 while (options && options->name) {
565 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
566 total_size = options->value.n / BDRV_SECTOR_SIZE;
571 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
576 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
579 if (close(fd) != 0) {
587 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
589 struct xfs_flock64 fl;
591 memset(&fl, 0, sizeof(fl));
592 fl.l_whence = SEEK_SET;
593 fl.l_start = sector_num << 9;
594 fl.l_len = (int64_t)nb_sectors << 9;
596 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
597 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
605 static coroutine_fn int raw_co_discard(BlockDriverState *bs,
606 int64_t sector_num, int nb_sectors)
609 BDRVRawState *s = bs->opaque;
612 return xfs_discard(s, sector_num, nb_sectors);
619 static QEMUOptionParameter raw_create_options[] = {
621 .name = BLOCK_OPT_SIZE,
623 .help = "Virtual disk size"
628 static BlockDriver bdrv_file = {
629 .format_name = "file",
630 .protocol_name = "file",
631 .instance_size = sizeof(BDRVRawState),
632 .bdrv_probe = NULL, /* no probe for protocols */
633 .bdrv_file_open = raw_open,
634 .bdrv_close = raw_close,
635 .bdrv_create = raw_create,
636 .bdrv_co_discard = raw_co_discard,
638 .bdrv_aio_readv = raw_aio_readv,
639 .bdrv_aio_writev = raw_aio_writev,
640 .bdrv_aio_flush = raw_aio_flush,
642 .bdrv_truncate = raw_truncate,
643 .bdrv_getlength = raw_getlength,
644 .bdrv_get_allocated_file_size
645 = raw_get_allocated_file_size,
647 .create_options = raw_create_options,
650 /***********************************************/
654 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
655 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
657 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
659 kern_return_t kernResult;
660 mach_port_t masterPort;
661 CFMutableDictionaryRef classesToMatch;
663 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
664 if ( KERN_SUCCESS != kernResult ) {
665 printf( "IOMasterPort returned %d\n", kernResult );
668 classesToMatch = IOServiceMatching( kIOCDMediaClass );
669 if ( classesToMatch == NULL ) {
670 printf( "IOServiceMatching returned a NULL dictionary.\n" );
672 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
674 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
675 if ( KERN_SUCCESS != kernResult )
677 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
683 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
685 io_object_t nextMedia;
686 kern_return_t kernResult = KERN_FAILURE;
688 nextMedia = IOIteratorNext( mediaIterator );
691 CFTypeRef bsdPathAsCFString;
692 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
693 if ( bsdPathAsCFString ) {
694 size_t devPathLength;
695 strcpy( bsdPath, _PATH_DEV );
696 strcat( bsdPath, "r" );
697 devPathLength = strlen( bsdPath );
698 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
699 kernResult = KERN_SUCCESS;
701 CFRelease( bsdPathAsCFString );
703 IOObjectRelease( nextMedia );
711 static int hdev_probe_device(const char *filename)
715 /* allow a dedicated CD-ROM driver to match with a higher priority */
716 if (strstart(filename, "/dev/cdrom", NULL))
719 if (stat(filename, &st) >= 0 &&
720 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
727 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
729 BDRVRawState *s = bs->opaque;
732 if (strstart(filename, "/dev/cdrom", NULL)) {
733 kern_return_t kernResult;
734 io_iterator_t mediaIterator;
735 char bsdPath[ MAXPATHLEN ];
738 kernResult = FindEjectableCDMedia( &mediaIterator );
739 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
741 if ( bsdPath[ 0 ] != '\0' ) {
742 strcat(bsdPath,"s0");
743 /* some CDs don't have a partition 0 */
744 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
746 bsdPath[strlen(bsdPath)-1] = '1';
754 IOObjectRelease( mediaIterator );
758 s->type = FTYPE_FILE;
759 #if defined(__linux__)
761 char resolved_path[ MAXPATHLEN ], *temp;
763 temp = realpath(filename, resolved_path);
764 if (temp && strstart(temp, "/dev/sg", NULL)) {
770 return raw_open_common(bs, filename, flags, 0);
773 #if defined(__linux__)
774 /* Note: we do not have a reliable method to detect if the floppy is
775 present. The current method is to try to open the floppy at every
776 I/O and to keep it opened during a few hundreds of ms. */
777 static int fd_open(BlockDriverState *bs)
779 BDRVRawState *s = bs->opaque;
780 int last_media_present;
782 if (s->type != FTYPE_FD)
784 last_media_present = (s->fd >= 0);
786 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
790 printf("Floppy closed\n");
794 if (s->fd_got_error &&
795 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
797 printf("No floppy (open delayed)\n");
801 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
803 s->fd_error_time = get_clock();
805 if (last_media_present)
806 s->fd_media_changed = 1;
808 printf("No floppy\n");
813 printf("Floppy opened\n");
816 if (!last_media_present)
817 s->fd_media_changed = 1;
818 s->fd_open_time = get_clock();
823 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
825 BDRVRawState *s = bs->opaque;
827 return ioctl(s->fd, req, buf);
830 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
831 unsigned long int req, void *buf,
832 BlockDriverCompletionFunc *cb, void *opaque)
834 BDRVRawState *s = bs->opaque;
838 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
841 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
842 static int fd_open(BlockDriverState *bs)
844 BDRVRawState *s = bs->opaque;
846 /* this is just to ensure s->fd is sane (its called by io ops) */
851 #else /* !linux && !FreeBSD */
853 static int fd_open(BlockDriverState *bs)
858 #endif /* !linux && !FreeBSD */
860 static int hdev_create(const char *filename, QEMUOptionParameter *options)
864 struct stat stat_buf;
865 int64_t total_size = 0;
867 /* Read out options */
868 while (options && options->name) {
869 if (!strcmp(options->name, "size")) {
870 total_size = options->value.n / BDRV_SECTOR_SIZE;
875 fd = open(filename, O_WRONLY | O_BINARY);
879 if (fstat(fd, &stat_buf) < 0)
881 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
883 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
890 static int hdev_has_zero_init(BlockDriverState *bs)
895 static BlockDriver bdrv_host_device = {
896 .format_name = "host_device",
897 .protocol_name = "host_device",
898 .instance_size = sizeof(BDRVRawState),
899 .bdrv_probe_device = hdev_probe_device,
900 .bdrv_file_open = hdev_open,
901 .bdrv_close = raw_close,
902 .bdrv_create = hdev_create,
903 .create_options = raw_create_options,
904 .bdrv_has_zero_init = hdev_has_zero_init,
906 .bdrv_aio_readv = raw_aio_readv,
907 .bdrv_aio_writev = raw_aio_writev,
908 .bdrv_aio_flush = raw_aio_flush,
910 .bdrv_truncate = raw_truncate,
911 .bdrv_getlength = raw_getlength,
912 .bdrv_get_allocated_file_size
913 = raw_get_allocated_file_size,
915 /* generic scsi device */
917 .bdrv_ioctl = hdev_ioctl,
918 .bdrv_aio_ioctl = hdev_aio_ioctl,
923 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
925 BDRVRawState *s = bs->opaque;
930 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
931 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
935 /* close fd so that we can reopen it as needed */
938 s->fd_media_changed = 1;
943 static int floppy_probe_device(const char *filename)
947 struct floppy_struct fdparam;
950 if (strstart(filename, "/dev/fd", NULL))
953 fd = open(filename, O_RDONLY | O_NONBLOCK);
957 ret = fstat(fd, &st);
958 if (ret == -1 || !S_ISBLK(st.st_mode)) {
962 /* Attempt to detect via a floppy specific ioctl */
963 ret = ioctl(fd, FDGETPRM, &fdparam);
974 static int floppy_is_inserted(BlockDriverState *bs)
976 return fd_open(bs) >= 0;
979 static int floppy_media_changed(BlockDriverState *bs)
981 BDRVRawState *s = bs->opaque;
985 * XXX: we do not have a true media changed indication.
986 * It does not work if the floppy is changed without trying to read it.
989 ret = s->fd_media_changed;
990 s->fd_media_changed = 0;
992 printf("Floppy changed=%d\n", ret);
997 static void floppy_eject(BlockDriverState *bs, int eject_flag)
999 BDRVRawState *s = bs->opaque;
1006 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1008 if (ioctl(fd, FDEJECT, 0) < 0)
1014 static BlockDriver bdrv_host_floppy = {
1015 .format_name = "host_floppy",
1016 .protocol_name = "host_floppy",
1017 .instance_size = sizeof(BDRVRawState),
1018 .bdrv_probe_device = floppy_probe_device,
1019 .bdrv_file_open = floppy_open,
1020 .bdrv_close = raw_close,
1021 .bdrv_create = hdev_create,
1022 .create_options = raw_create_options,
1023 .bdrv_has_zero_init = hdev_has_zero_init,
1025 .bdrv_aio_readv = raw_aio_readv,
1026 .bdrv_aio_writev = raw_aio_writev,
1027 .bdrv_aio_flush = raw_aio_flush,
1029 .bdrv_truncate = raw_truncate,
1030 .bdrv_getlength = raw_getlength,
1031 .bdrv_get_allocated_file_size
1032 = raw_get_allocated_file_size,
1034 /* removable device support */
1035 .bdrv_is_inserted = floppy_is_inserted,
1036 .bdrv_media_changed = floppy_media_changed,
1037 .bdrv_eject = floppy_eject,
1040 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1042 BDRVRawState *s = bs->opaque;
1046 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1047 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1050 static int cdrom_probe_device(const char *filename)
1056 fd = open(filename, O_RDONLY | O_NONBLOCK);
1060 ret = fstat(fd, &st);
1061 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1065 /* Attempt to detect via a CDROM specific ioctl */
1066 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1076 static int cdrom_is_inserted(BlockDriverState *bs)
1078 BDRVRawState *s = bs->opaque;
1081 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1082 if (ret == CDS_DISC_OK)
1087 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1089 BDRVRawState *s = bs->opaque;
1092 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1093 perror("CDROMEJECT");
1095 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1096 perror("CDROMEJECT");
1100 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1102 BDRVRawState *s = bs->opaque;
1104 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1106 * Note: an error can happen if the distribution automatically
1109 /* perror("CDROM_LOCKDOOR"); */
1113 static BlockDriver bdrv_host_cdrom = {
1114 .format_name = "host_cdrom",
1115 .protocol_name = "host_cdrom",
1116 .instance_size = sizeof(BDRVRawState),
1117 .bdrv_probe_device = cdrom_probe_device,
1118 .bdrv_file_open = cdrom_open,
1119 .bdrv_close = raw_close,
1120 .bdrv_create = hdev_create,
1121 .create_options = raw_create_options,
1122 .bdrv_has_zero_init = hdev_has_zero_init,
1124 .bdrv_aio_readv = raw_aio_readv,
1125 .bdrv_aio_writev = raw_aio_writev,
1126 .bdrv_aio_flush = raw_aio_flush,
1128 .bdrv_truncate = raw_truncate,
1129 .bdrv_getlength = raw_getlength,
1130 .bdrv_get_allocated_file_size
1131 = raw_get_allocated_file_size,
1133 /* removable device support */
1134 .bdrv_is_inserted = cdrom_is_inserted,
1135 .bdrv_eject = cdrom_eject,
1136 .bdrv_lock_medium = cdrom_lock_medium,
1138 /* generic scsi device */
1139 .bdrv_ioctl = hdev_ioctl,
1140 .bdrv_aio_ioctl = hdev_aio_ioctl,
1142 #endif /* __linux__ */
1144 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1145 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1147 BDRVRawState *s = bs->opaque;
1152 ret = raw_open_common(bs, filename, flags, 0);
1156 /* make sure the door isnt locked at this time */
1157 ioctl(s->fd, CDIOCALLOW);
1161 static int cdrom_probe_device(const char *filename)
1163 if (strstart(filename, "/dev/cd", NULL) ||
1164 strstart(filename, "/dev/acd", NULL))
1169 static int cdrom_reopen(BlockDriverState *bs)
1171 BDRVRawState *s = bs->opaque;
1175 * Force reread of possibly changed/newly loaded disc,
1176 * FreeBSD seems to not notice sometimes...
1180 fd = open(bs->filename, s->open_flags, 0644);
1187 /* make sure the door isnt locked at this time */
1188 ioctl(s->fd, CDIOCALLOW);
1192 static int cdrom_is_inserted(BlockDriverState *bs)
1194 return raw_getlength(bs) > 0;
1197 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1199 BDRVRawState *s = bs->opaque;
1204 (void) ioctl(s->fd, CDIOCALLOW);
1207 if (ioctl(s->fd, CDIOCEJECT) < 0)
1208 perror("CDIOCEJECT");
1210 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1211 perror("CDIOCCLOSE");
1217 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1219 BDRVRawState *s = bs->opaque;
1223 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1225 * Note: an error can happen if the distribution automatically
1228 /* perror("CDROM_LOCKDOOR"); */
1232 static BlockDriver bdrv_host_cdrom = {
1233 .format_name = "host_cdrom",
1234 .protocol_name = "host_cdrom",
1235 .instance_size = sizeof(BDRVRawState),
1236 .bdrv_probe_device = cdrom_probe_device,
1237 .bdrv_file_open = cdrom_open,
1238 .bdrv_close = raw_close,
1239 .bdrv_create = hdev_create,
1240 .create_options = raw_create_options,
1241 .bdrv_has_zero_init = hdev_has_zero_init,
1243 .bdrv_aio_readv = raw_aio_readv,
1244 .bdrv_aio_writev = raw_aio_writev,
1245 .bdrv_aio_flush = raw_aio_flush,
1247 .bdrv_truncate = raw_truncate,
1248 .bdrv_getlength = raw_getlength,
1249 .bdrv_get_allocated_file_size
1250 = raw_get_allocated_file_size,
1252 /* removable device support */
1253 .bdrv_is_inserted = cdrom_is_inserted,
1254 .bdrv_eject = cdrom_eject,
1255 .bdrv_lock_medium = cdrom_lock_medium,
1257 #endif /* __FreeBSD__ */
1259 static void bdrv_file_init(void)
1262 * Register all the drivers. Note that order is important, the driver
1263 * registered last will get probed first.
1265 bdrv_register(&bdrv_file);
1266 bdrv_register(&bdrv_host_device);
1268 bdrv_register(&bdrv_host_floppy);
1269 bdrv_register(&bdrv_host_cdrom);
1271 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1272 bdrv_register(&bdrv_host_cdrom);
1276 block_init(bdrv_file_init);