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 * offset and count are in bytes, but must be multiples of 512 for files
301 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
303 * This function may be called without alignment if the caller ensures
304 * that O_DIRECT is not in effect.
306 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
307 uint8_t *buf, int count)
309 BDRVRawState *s = bs->opaque;
316 ret = pread(s->fd, buf, count, offset);
320 /* Allow reads beyond the end (needed for pwrite) */
321 if ((ret == 0) && bs->growable) {
322 int64_t size = raw_getlength(bs);
323 if (offset >= size) {
324 memset(buf, 0, count);
329 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
330 "] read failed %d : %d = %s\n",
331 s->fd, bs->filename, offset, buf, count,
332 bs->total_sectors, ret, errno, strerror(errno));
334 /* Try harder for CDrom. */
335 if (s->type != FTYPE_FILE) {
336 ret = pread(s->fd, buf, count, offset);
339 ret = pread(s->fd, buf, count, offset);
343 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
344 "] retry read failed %d : %d = %s\n",
345 s->fd, bs->filename, offset, buf, count,
346 bs->total_sectors, ret, errno, strerror(errno));
349 return (ret < 0) ? -errno : ret;
353 * offset and count are in bytes, but must be multiples of the sector size
354 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
357 * This function may be called without alignment if the caller ensures
358 * that O_DIRECT is not in effect.
360 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
361 const uint8_t *buf, int count)
363 BDRVRawState *s = bs->opaque;
370 ret = pwrite(s->fd, buf, count, offset);
374 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
375 "] write failed %d : %d = %s\n",
376 s->fd, bs->filename, offset, buf, count,
377 bs->total_sectors, ret, errno, strerror(errno));
379 return (ret < 0) ? -errno : ret;
384 * offset and count are in bytes and possibly not aligned. For files opened
385 * with O_DIRECT, necessary alignments are ensured before calling
386 * raw_pread_aligned to do the actual read.
388 static int raw_pread(BlockDriverState *bs, int64_t offset,
389 uint8_t *buf, int count)
391 BDRVRawState *s = bs->opaque;
392 unsigned sector_mask = bs->buffer_alignment - 1;
393 int size, ret, shift, sum;
397 if (s->aligned_buf != NULL) {
399 if (offset & sector_mask) {
400 /* align offset on a sector size bytes boundary */
402 shift = offset & sector_mask;
403 size = (shift + count + sector_mask) & ~sector_mask;
404 if (size > s->aligned_buf_size)
405 size = s->aligned_buf_size;
406 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
410 size = bs->buffer_alignment - shift;
413 memcpy(buf, s->aligned_buf + shift, size);
423 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
425 /* read on aligned buffer */
429 size = (count + sector_mask) & ~sector_mask;
430 if (size > s->aligned_buf_size)
431 size = s->aligned_buf_size;
433 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
436 } else if (ret == 0) {
437 fprintf(stderr, "raw_pread: read beyond end of file\n");
445 memcpy(buf, s->aligned_buf, size);
457 return raw_pread_aligned(bs, offset, buf, count) + sum;
460 static int raw_read(BlockDriverState *bs, int64_t sector_num,
461 uint8_t *buf, int nb_sectors)
465 ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf,
466 nb_sectors * BDRV_SECTOR_SIZE);
467 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
473 * offset and count are in bytes and possibly not aligned. For files opened
474 * with O_DIRECT, necessary alignments are ensured before calling
475 * raw_pwrite_aligned to do the actual write.
477 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
478 const uint8_t *buf, int count)
480 BDRVRawState *s = bs->opaque;
481 unsigned sector_mask = bs->buffer_alignment - 1;
482 int size, ret, shift, sum;
486 if (s->aligned_buf != NULL) {
488 if (offset & sector_mask) {
489 /* align offset on a sector size bytes boundary */
490 shift = offset & sector_mask;
491 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf,
492 bs->buffer_alignment);
496 size = bs->buffer_alignment - shift;
499 memcpy(s->aligned_buf + shift, buf, size);
501 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf,
502 bs->buffer_alignment);
514 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
516 while ((size = (count & ~sector_mask)) != 0) {
518 if (size > s->aligned_buf_size)
519 size = s->aligned_buf_size;
521 memcpy(s->aligned_buf, buf, size);
523 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
532 /* here, count < sector_size because (count & ~sector_mask) == 0 */
534 ret = raw_pread_aligned(bs, offset, s->aligned_buf,
535 bs->buffer_alignment);
538 memcpy(s->aligned_buf, buf, count);
540 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf,
541 bs->buffer_alignment);
552 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
555 static int raw_write(BlockDriverState *bs, int64_t sector_num,
556 const uint8_t *buf, int nb_sectors)
559 ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf,
560 nb_sectors * BDRV_SECTOR_SIZE);
561 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
567 * Check if all memory in this vector is sector aligned.
569 static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
573 for (i = 0; i < qiov->niov; i++) {
574 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
582 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
583 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
584 BlockDriverCompletionFunc *cb, void *opaque, int type)
586 BDRVRawState *s = bs->opaque;
592 * If O_DIRECT is used the buffer needs to be aligned on a sector
593 * boundary. Check if this is the case or tell the low-level
594 * driver that it needs to copy the buffer.
596 if (s->aligned_buf) {
597 if (!qiov_is_aligned(bs, qiov)) {
598 type |= QEMU_AIO_MISALIGNED;
599 #ifdef CONFIG_LINUX_AIO
600 } else if (s->use_aio) {
601 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
602 nb_sectors, cb, opaque, type);
607 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
611 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
612 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
613 BlockDriverCompletionFunc *cb, void *opaque)
615 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
616 cb, opaque, QEMU_AIO_READ);
619 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
620 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
621 BlockDriverCompletionFunc *cb, void *opaque)
623 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
624 cb, opaque, QEMU_AIO_WRITE);
627 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
628 BlockDriverCompletionFunc *cb, void *opaque)
630 BDRVRawState *s = bs->opaque;
635 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
638 static void raw_close(BlockDriverState *bs)
640 BDRVRawState *s = bs->opaque;
644 if (s->aligned_buf != NULL)
645 qemu_vfree(s->aligned_buf);
649 static int raw_truncate(BlockDriverState *bs, int64_t offset)
651 BDRVRawState *s = bs->opaque;
652 if (s->type != FTYPE_FILE)
654 if (ftruncate(s->fd, offset) < 0)
660 static int64_t raw_getlength(BlockDriverState *bs)
662 BDRVRawState *s = bs->opaque;
668 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
671 if (ioctl(fd, DIOCGDINFO, &dl))
673 return (uint64_t)dl.d_secsize *
674 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
678 #elif defined(__NetBSD__)
679 static int64_t raw_getlength(BlockDriverState *bs)
681 BDRVRawState *s = bs->opaque;
687 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
688 struct dkwedge_info dkw;
690 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
691 return dkw.dkw_size * 512;
695 if (ioctl(fd, DIOCGDINFO, &dl))
697 return (uint64_t)dl.d_secsize *
698 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
703 #elif defined(__sun__)
704 static int64_t raw_getlength(BlockDriverState *bs)
706 BDRVRawState *s = bs->opaque;
707 struct dk_minfo minfo;
716 * Use the DKIOCGMEDIAINFO ioctl to read the size.
718 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
720 return minfo.dki_lbsize * minfo.dki_capacity;
724 * There are reports that lseek on some devices fails, but
725 * irc discussion said that contingency on contingency was overkill.
727 return lseek(s->fd, 0, SEEK_END);
729 #elif defined(CONFIG_BSD)
730 static int64_t raw_getlength(BlockDriverState *bs)
732 BDRVRawState *s = bs->opaque;
736 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
745 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
748 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
749 #ifdef DIOCGMEDIASIZE
750 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
751 #elif defined(DIOCGPART)
754 if (ioctl(fd, DIOCGPART, &pi) == 0)
755 size = pi.media_size;
762 size = LONG_LONG_MAX;
764 size = lseek(fd, 0LL, SEEK_END);
766 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
769 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
770 if (size == 2048LL * (unsigned)-1)
772 /* XXX no disc? maybe we need to reopen... */
773 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
780 size = lseek(fd, 0, SEEK_END);
785 static int64_t raw_getlength(BlockDriverState *bs)
787 BDRVRawState *s = bs->opaque;
795 return lseek(s->fd, 0, SEEK_END);
799 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
802 BDRVRawState *s = bs->opaque;
804 if (fstat(s->fd, &st) < 0) {
807 return (int64_t)st.st_blocks * 512;
810 static int raw_create(const char *filename, QEMUOptionParameter *options)
814 int64_t total_size = 0;
816 /* Read out options */
817 while (options && options->name) {
818 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
819 total_size = options->value.n / BDRV_SECTOR_SIZE;
824 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
829 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
832 if (close(fd) != 0) {
839 static int raw_flush(BlockDriverState *bs)
841 BDRVRawState *s = bs->opaque;
842 return qemu_fdatasync(s->fd);
846 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
848 struct xfs_flock64 fl;
850 memset(&fl, 0, sizeof(fl));
851 fl.l_whence = SEEK_SET;
852 fl.l_start = sector_num << 9;
853 fl.l_len = (int64_t)nb_sectors << 9;
855 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
856 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
864 static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
867 BDRVRawState *s = bs->opaque;
870 return xfs_discard(s, sector_num, nb_sectors);
877 static QEMUOptionParameter raw_create_options[] = {
879 .name = BLOCK_OPT_SIZE,
881 .help = "Virtual disk size"
886 static BlockDriver bdrv_file = {
887 .format_name = "file",
888 .protocol_name = "file",
889 .instance_size = sizeof(BDRVRawState),
890 .bdrv_probe = NULL, /* no probe for protocols */
891 .bdrv_file_open = raw_open,
892 .bdrv_read = raw_read,
893 .bdrv_write = raw_write,
894 .bdrv_close = raw_close,
895 .bdrv_create = raw_create,
896 .bdrv_flush = raw_flush,
897 .bdrv_discard = raw_discard,
899 .bdrv_aio_readv = raw_aio_readv,
900 .bdrv_aio_writev = raw_aio_writev,
901 .bdrv_aio_flush = raw_aio_flush,
903 .bdrv_truncate = raw_truncate,
904 .bdrv_getlength = raw_getlength,
905 .bdrv_get_allocated_file_size
906 = raw_get_allocated_file_size,
908 .create_options = raw_create_options,
911 /***********************************************/
915 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
916 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
918 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
920 kern_return_t kernResult;
921 mach_port_t masterPort;
922 CFMutableDictionaryRef classesToMatch;
924 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
925 if ( KERN_SUCCESS != kernResult ) {
926 printf( "IOMasterPort returned %d\n", kernResult );
929 classesToMatch = IOServiceMatching( kIOCDMediaClass );
930 if ( classesToMatch == NULL ) {
931 printf( "IOServiceMatching returned a NULL dictionary.\n" );
933 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
935 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
936 if ( KERN_SUCCESS != kernResult )
938 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
944 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
946 io_object_t nextMedia;
947 kern_return_t kernResult = KERN_FAILURE;
949 nextMedia = IOIteratorNext( mediaIterator );
952 CFTypeRef bsdPathAsCFString;
953 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
954 if ( bsdPathAsCFString ) {
955 size_t devPathLength;
956 strcpy( bsdPath, _PATH_DEV );
957 strcat( bsdPath, "r" );
958 devPathLength = strlen( bsdPath );
959 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
960 kernResult = KERN_SUCCESS;
962 CFRelease( bsdPathAsCFString );
964 IOObjectRelease( nextMedia );
972 static int hdev_probe_device(const char *filename)
976 /* allow a dedicated CD-ROM driver to match with a higher priority */
977 if (strstart(filename, "/dev/cdrom", NULL))
980 if (stat(filename, &st) >= 0 &&
981 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
988 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
990 BDRVRawState *s = bs->opaque;
993 if (strstart(filename, "/dev/cdrom", NULL)) {
994 kern_return_t kernResult;
995 io_iterator_t mediaIterator;
996 char bsdPath[ MAXPATHLEN ];
999 kernResult = FindEjectableCDMedia( &mediaIterator );
1000 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1002 if ( bsdPath[ 0 ] != '\0' ) {
1003 strcat(bsdPath,"s0");
1004 /* some CDs don't have a partition 0 */
1005 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1007 bsdPath[strlen(bsdPath)-1] = '1';
1014 if ( mediaIterator )
1015 IOObjectRelease( mediaIterator );
1019 s->type = FTYPE_FILE;
1020 #if defined(__linux__)
1022 char resolved_path[ MAXPATHLEN ], *temp;
1024 temp = realpath(filename, resolved_path);
1025 if (temp && strstart(temp, "/dev/sg", NULL)) {
1031 return raw_open_common(bs, filename, flags, 0);
1034 #if defined(__linux__)
1035 /* Note: we do not have a reliable method to detect if the floppy is
1036 present. The current method is to try to open the floppy at every
1037 I/O and to keep it opened during a few hundreds of ms. */
1038 static int fd_open(BlockDriverState *bs)
1040 BDRVRawState *s = bs->opaque;
1041 int last_media_present;
1043 if (s->type != FTYPE_FD)
1045 last_media_present = (s->fd >= 0);
1047 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1051 printf("Floppy closed\n");
1055 if (s->fd_got_error &&
1056 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1058 printf("No floppy (open delayed)\n");
1062 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
1064 s->fd_error_time = get_clock();
1065 s->fd_got_error = 1;
1066 if (last_media_present)
1067 s->fd_media_changed = 1;
1069 printf("No floppy\n");
1074 printf("Floppy opened\n");
1077 if (!last_media_present)
1078 s->fd_media_changed = 1;
1079 s->fd_open_time = get_clock();
1080 s->fd_got_error = 0;
1084 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1086 BDRVRawState *s = bs->opaque;
1088 return ioctl(s->fd, req, buf);
1091 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1092 unsigned long int req, void *buf,
1093 BlockDriverCompletionFunc *cb, void *opaque)
1095 BDRVRawState *s = bs->opaque;
1097 if (fd_open(bs) < 0)
1099 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
1102 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1103 static int fd_open(BlockDriverState *bs)
1105 BDRVRawState *s = bs->opaque;
1107 /* this is just to ensure s->fd is sane (its called by io ops) */
1112 #else /* !linux && !FreeBSD */
1114 static int fd_open(BlockDriverState *bs)
1119 #endif /* !linux && !FreeBSD */
1121 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1125 struct stat stat_buf;
1126 int64_t total_size = 0;
1128 /* Read out options */
1129 while (options && options->name) {
1130 if (!strcmp(options->name, "size")) {
1131 total_size = options->value.n / BDRV_SECTOR_SIZE;
1136 fd = open(filename, O_WRONLY | O_BINARY);
1140 if (fstat(fd, &stat_buf) < 0)
1142 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1144 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1151 static int hdev_has_zero_init(BlockDriverState *bs)
1156 static BlockDriver bdrv_host_device = {
1157 .format_name = "host_device",
1158 .protocol_name = "host_device",
1159 .instance_size = sizeof(BDRVRawState),
1160 .bdrv_probe_device = hdev_probe_device,
1161 .bdrv_file_open = hdev_open,
1162 .bdrv_close = raw_close,
1163 .bdrv_create = hdev_create,
1164 .create_options = raw_create_options,
1165 .bdrv_has_zero_init = hdev_has_zero_init,
1166 .bdrv_flush = raw_flush,
1168 .bdrv_aio_readv = raw_aio_readv,
1169 .bdrv_aio_writev = raw_aio_writev,
1170 .bdrv_aio_flush = raw_aio_flush,
1172 .bdrv_read = raw_read,
1173 .bdrv_write = raw_write,
1174 .bdrv_getlength = raw_getlength,
1175 .bdrv_get_allocated_file_size
1176 = raw_get_allocated_file_size,
1178 /* generic scsi device */
1180 .bdrv_ioctl = hdev_ioctl,
1181 .bdrv_aio_ioctl = hdev_aio_ioctl,
1186 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1188 BDRVRawState *s = bs->opaque;
1193 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1194 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1198 /* close fd so that we can reopen it as needed */
1201 s->fd_media_changed = 1;
1206 static int floppy_probe_device(const char *filename)
1210 struct floppy_struct fdparam;
1213 if (strstart(filename, "/dev/fd", NULL))
1216 fd = open(filename, O_RDONLY | O_NONBLOCK);
1220 ret = fstat(fd, &st);
1221 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1225 /* Attempt to detect via a floppy specific ioctl */
1226 ret = ioctl(fd, FDGETPRM, &fdparam);
1237 static int floppy_is_inserted(BlockDriverState *bs)
1239 return fd_open(bs) >= 0;
1242 static int floppy_media_changed(BlockDriverState *bs)
1244 BDRVRawState *s = bs->opaque;
1248 * XXX: we do not have a true media changed indication.
1249 * It does not work if the floppy is changed without trying to read it.
1252 ret = s->fd_media_changed;
1253 s->fd_media_changed = 0;
1255 printf("Floppy changed=%d\n", ret);
1260 static void floppy_eject(BlockDriverState *bs, int eject_flag)
1262 BDRVRawState *s = bs->opaque;
1269 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1271 if (ioctl(fd, FDEJECT, 0) < 0)
1277 static BlockDriver bdrv_host_floppy = {
1278 .format_name = "host_floppy",
1279 .protocol_name = "host_floppy",
1280 .instance_size = sizeof(BDRVRawState),
1281 .bdrv_probe_device = floppy_probe_device,
1282 .bdrv_file_open = floppy_open,
1283 .bdrv_close = raw_close,
1284 .bdrv_create = hdev_create,
1285 .create_options = raw_create_options,
1286 .bdrv_has_zero_init = hdev_has_zero_init,
1287 .bdrv_flush = raw_flush,
1289 .bdrv_aio_readv = raw_aio_readv,
1290 .bdrv_aio_writev = raw_aio_writev,
1291 .bdrv_aio_flush = raw_aio_flush,
1293 .bdrv_read = raw_read,
1294 .bdrv_write = raw_write,
1295 .bdrv_getlength = raw_getlength,
1296 .bdrv_get_allocated_file_size
1297 = raw_get_allocated_file_size,
1299 /* removable device support */
1300 .bdrv_is_inserted = floppy_is_inserted,
1301 .bdrv_media_changed = floppy_media_changed,
1302 .bdrv_eject = floppy_eject,
1305 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1307 BDRVRawState *s = bs->opaque;
1311 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1312 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1315 static int cdrom_probe_device(const char *filename)
1321 fd = open(filename, O_RDONLY | O_NONBLOCK);
1325 ret = fstat(fd, &st);
1326 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1330 /* Attempt to detect via a CDROM specific ioctl */
1331 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1341 static int cdrom_is_inserted(BlockDriverState *bs)
1343 BDRVRawState *s = bs->opaque;
1346 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1347 if (ret == CDS_DISC_OK)
1352 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1354 BDRVRawState *s = bs->opaque;
1357 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1358 perror("CDROMEJECT");
1360 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1361 perror("CDROMEJECT");
1365 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1367 BDRVRawState *s = bs->opaque;
1369 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1371 * Note: an error can happen if the distribution automatically
1374 /* perror("CDROM_LOCKDOOR"); */
1378 static BlockDriver bdrv_host_cdrom = {
1379 .format_name = "host_cdrom",
1380 .protocol_name = "host_cdrom",
1381 .instance_size = sizeof(BDRVRawState),
1382 .bdrv_probe_device = cdrom_probe_device,
1383 .bdrv_file_open = cdrom_open,
1384 .bdrv_close = raw_close,
1385 .bdrv_create = hdev_create,
1386 .create_options = raw_create_options,
1387 .bdrv_has_zero_init = hdev_has_zero_init,
1388 .bdrv_flush = raw_flush,
1390 .bdrv_aio_readv = raw_aio_readv,
1391 .bdrv_aio_writev = raw_aio_writev,
1392 .bdrv_aio_flush = raw_aio_flush,
1394 .bdrv_read = raw_read,
1395 .bdrv_write = raw_write,
1396 .bdrv_getlength = raw_getlength,
1397 .bdrv_get_allocated_file_size
1398 = raw_get_allocated_file_size,
1400 /* removable device support */
1401 .bdrv_is_inserted = cdrom_is_inserted,
1402 .bdrv_eject = cdrom_eject,
1403 .bdrv_lock_medium = cdrom_lock_medium,
1405 /* generic scsi device */
1406 .bdrv_ioctl = hdev_ioctl,
1407 .bdrv_aio_ioctl = hdev_aio_ioctl,
1409 #endif /* __linux__ */
1411 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1412 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1414 BDRVRawState *s = bs->opaque;
1419 ret = raw_open_common(bs, filename, flags, 0);
1423 /* make sure the door isnt locked at this time */
1424 ioctl(s->fd, CDIOCALLOW);
1428 static int cdrom_probe_device(const char *filename)
1430 if (strstart(filename, "/dev/cd", NULL) ||
1431 strstart(filename, "/dev/acd", NULL))
1436 static int cdrom_reopen(BlockDriverState *bs)
1438 BDRVRawState *s = bs->opaque;
1442 * Force reread of possibly changed/newly loaded disc,
1443 * FreeBSD seems to not notice sometimes...
1447 fd = open(bs->filename, s->open_flags, 0644);
1454 /* make sure the door isnt locked at this time */
1455 ioctl(s->fd, CDIOCALLOW);
1459 static int cdrom_is_inserted(BlockDriverState *bs)
1461 return raw_getlength(bs) > 0;
1464 static void cdrom_eject(BlockDriverState *bs, int eject_flag)
1466 BDRVRawState *s = bs->opaque;
1471 (void) ioctl(s->fd, CDIOCALLOW);
1474 if (ioctl(s->fd, CDIOCEJECT) < 0)
1475 perror("CDIOCEJECT");
1477 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1478 perror("CDIOCCLOSE");
1484 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1486 BDRVRawState *s = bs->opaque;
1490 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1492 * Note: an error can happen if the distribution automatically
1495 /* perror("CDROM_LOCKDOOR"); */
1499 static BlockDriver bdrv_host_cdrom = {
1500 .format_name = "host_cdrom",
1501 .protocol_name = "host_cdrom",
1502 .instance_size = sizeof(BDRVRawState),
1503 .bdrv_probe_device = cdrom_probe_device,
1504 .bdrv_file_open = cdrom_open,
1505 .bdrv_close = raw_close,
1506 .bdrv_create = hdev_create,
1507 .create_options = raw_create_options,
1508 .bdrv_has_zero_init = hdev_has_zero_init,
1509 .bdrv_flush = raw_flush,
1511 .bdrv_aio_readv = raw_aio_readv,
1512 .bdrv_aio_writev = raw_aio_writev,
1513 .bdrv_aio_flush = raw_aio_flush,
1515 .bdrv_read = raw_read,
1516 .bdrv_write = raw_write,
1517 .bdrv_getlength = raw_getlength,
1518 .bdrv_get_allocated_file_size
1519 = raw_get_allocated_file_size,
1521 /* removable device support */
1522 .bdrv_is_inserted = cdrom_is_inserted,
1523 .bdrv_eject = cdrom_eject,
1524 .bdrv_lock_medium = cdrom_lock_medium,
1526 #endif /* __FreeBSD__ */
1528 static void bdrv_file_init(void)
1531 * Register all the drivers. Note that order is important, the driver
1532 * registered last will get probed first.
1534 bdrv_register(&bdrv_file);
1535 bdrv_register(&bdrv_host_device);
1537 bdrv_register(&bdrv_host_floppy);
1538 bdrv_register(&bdrv_host_cdrom);
1540 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1541 bdrv_register(&bdrv_host_cdrom);
1545 block_init(bdrv_file_init);