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
50 #include <sys/ioctl.h>
51 #include <linux/cdrom.h>
54 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
61 #include <sys/ioctl.h>
62 #include <sys/disklabel.h>
67 #include <sys/ioctl.h>
68 #include <sys/diskslice.h>
71 //#define DEBUG_FLOPPY
74 #if defined(DEBUG_BLOCK)
75 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
76 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
78 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
81 /* OS X does not have O_DSYNC */
84 #define O_DSYNC O_SYNC
85 #elif defined(O_FSYNC)
86 #define O_DSYNC O_FSYNC
90 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
92 #define O_DIRECT O_DSYNC
99 #define ALIGNED_BUFFER_SIZE (32 * 512)
101 /* if the FD is not accessed during that time (in ms), we try to
102 reopen it to see if the disk has been changed */
103 #define FD_OPEN_TIMEOUT 1000
105 typedef struct BDRVRawState {
108 unsigned int lseek_err_cnt;
110 #if defined(__linux__)
111 /* linux floppy specific */
112 int64_t fd_open_time;
113 int64_t fd_error_time;
115 int fd_media_changed;
117 #ifdef CONFIG_LINUX_AIO
121 uint8_t* aligned_buf;
124 static int fd_open(BlockDriverState *bs);
125 static int64_t raw_getlength(BlockDriverState *bs);
127 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
128 static int cdrom_reopen(BlockDriverState *bs);
131 static int raw_open_common(BlockDriverState *bs, const char *filename,
132 int bdrv_flags, int open_flags)
134 BDRVRawState *s = bs->opaque;
137 s->lseek_err_cnt = 0;
139 s->open_flags = open_flags | O_BINARY;
140 s->open_flags &= ~O_ACCMODE;
141 if ((bdrv_flags & BDRV_O_ACCESS) == BDRV_O_RDWR) {
142 s->open_flags |= O_RDWR;
144 s->open_flags |= O_RDONLY;
148 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
149 * and O_DIRECT for no caching. */
150 if ((bdrv_flags & BDRV_O_NOCACHE))
151 s->open_flags |= O_DIRECT;
152 else if (!(bdrv_flags & BDRV_O_CACHE_WB))
153 s->open_flags |= O_DSYNC;
156 fd = qemu_open(filename, s->open_flags, 0644);
164 s->aligned_buf = NULL;
166 if ((bdrv_flags & BDRV_O_NOCACHE)) {
167 s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE);
168 if (s->aligned_buf == NULL) {
173 #ifdef CONFIG_LINUX_AIO
174 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
175 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
177 /* We're falling back to POSIX AIO in some cases */
180 s->aio_ctx = laio_init();
188 if (paio_init() < 0) {
191 #ifdef CONFIG_LINUX_AIO
199 qemu_vfree(s->aligned_buf);
205 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
207 BDRVRawState *s = bs->opaque;
210 s->type = FTYPE_FILE;
211 if (flags & BDRV_O_CREAT)
212 open_flags = O_CREAT | O_TRUNC;
214 return raw_open_common(bs, filename, flags, open_flags);
217 /* XXX: use host sector size if necessary with:
218 #ifdef DIOCGSECTORSIZE
220 unsigned int sectorsize = 512;
221 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
222 sectorsize > bufsize)
223 bufsize = sectorsize;
227 u_int32_t blockSize = 512;
228 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
235 * offset and count are in bytes, but must be multiples of 512 for files
236 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
238 * This function may be called without alignment if the caller ensures
239 * that O_DIRECT is not in effect.
241 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
242 uint8_t *buf, int count)
244 BDRVRawState *s = bs->opaque;
251 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
252 ++(s->lseek_err_cnt);
253 if(s->lseek_err_cnt <= 10) {
254 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
255 "] lseek failed : %d = %s\n",
256 s->fd, bs->filename, offset, buf, count,
257 bs->total_sectors, errno, strerror(errno));
263 ret = read(s->fd, buf, count);
265 goto label__raw_read__success;
267 /* Allow reads beyond the end (needed for pwrite) */
268 if ((ret == 0) && bs->growable) {
269 int64_t size = raw_getlength(bs);
270 if (offset >= size) {
271 memset(buf, 0, count);
273 goto label__raw_read__success;
277 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
278 "] read failed %d : %d = %s\n",
279 s->fd, bs->filename, offset, buf, count,
280 bs->total_sectors, ret, errno, strerror(errno));
282 /* Try harder for CDrom. */
283 if (bs->type == BDRV_TYPE_CDROM) {
284 lseek(s->fd, offset, SEEK_SET);
285 ret = read(s->fd, buf, count);
287 goto label__raw_read__success;
288 lseek(s->fd, offset, SEEK_SET);
289 ret = read(s->fd, buf, count);
291 goto label__raw_read__success;
293 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
294 "] retry read failed %d : %d = %s\n",
295 s->fd, bs->filename, offset, buf, count,
296 bs->total_sectors, ret, errno, strerror(errno));
299 label__raw_read__success:
301 return (ret < 0) ? -errno : ret;
305 * offset and count are in bytes, but must be multiples of 512 for files
306 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
308 * This function may be called without alignment if the caller ensures
309 * that O_DIRECT is not in effect.
311 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
312 const uint8_t *buf, int count)
314 BDRVRawState *s = bs->opaque;
321 if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) {
322 ++(s->lseek_err_cnt);
323 if(s->lseek_err_cnt) {
324 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%"
325 PRId64 "] lseek failed : %d = %s\n",
326 s->fd, bs->filename, offset, buf, count,
327 bs->total_sectors, errno, strerror(errno));
331 s->lseek_err_cnt = 0;
333 ret = write(s->fd, buf, count);
335 goto label__raw_write__success;
337 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
338 "] write failed %d : %d = %s\n",
339 s->fd, bs->filename, offset, buf, count,
340 bs->total_sectors, ret, errno, strerror(errno));
342 label__raw_write__success:
344 return (ret < 0) ? -errno : ret;
349 * offset and count are in bytes and possibly not aligned. For files opened
350 * with O_DIRECT, necessary alignments are ensured before calling
351 * raw_pread_aligned to do the actual read.
353 static int raw_pread(BlockDriverState *bs, int64_t offset,
354 uint8_t *buf, int count)
356 BDRVRawState *s = bs->opaque;
357 int size, ret, shift, sum;
361 if (s->aligned_buf != NULL) {
363 if (offset & 0x1ff) {
364 /* align offset on a 512 bytes boundary */
366 shift = offset & 0x1ff;
367 size = (shift + count + 0x1ff) & ~0x1ff;
368 if (size > ALIGNED_BUFFER_SIZE)
369 size = ALIGNED_BUFFER_SIZE;
370 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
377 memcpy(buf, s->aligned_buf + shift, size);
387 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
389 /* read on aligned buffer */
393 size = (count + 0x1ff) & ~0x1ff;
394 if (size > ALIGNED_BUFFER_SIZE)
395 size = ALIGNED_BUFFER_SIZE;
397 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
405 memcpy(buf, s->aligned_buf, size);
417 return raw_pread_aligned(bs, offset, buf, count) + sum;
420 static int raw_read(BlockDriverState *bs, int64_t sector_num,
421 uint8_t *buf, int nb_sectors)
425 ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512);
426 if (ret == (nb_sectors * 512))
432 * offset and count are in bytes and possibly not aligned. For files opened
433 * with O_DIRECT, necessary alignments are ensured before calling
434 * raw_pwrite_aligned to do the actual write.
436 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
437 const uint8_t *buf, int count)
439 BDRVRawState *s = bs->opaque;
440 int size, ret, shift, sum;
444 if (s->aligned_buf != NULL) {
446 if (offset & 0x1ff) {
447 /* align offset on a 512 bytes boundary */
448 shift = offset & 0x1ff;
449 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
456 memcpy(s->aligned_buf + shift, buf, size);
458 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
470 if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {
472 while ((size = (count & ~0x1ff)) != 0) {
474 if (size > ALIGNED_BUFFER_SIZE)
475 size = ALIGNED_BUFFER_SIZE;
477 memcpy(s->aligned_buf, buf, size);
479 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
488 /* here, count < 512 because (count & ~0x1ff) == 0 */
490 ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
493 memcpy(s->aligned_buf, buf, count);
495 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
506 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
509 static int raw_write(BlockDriverState *bs, int64_t sector_num,
510 const uint8_t *buf, int nb_sectors)
513 ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512);
514 if (ret == (nb_sectors * 512))
520 * Check if all memory in this vector is sector aligned.
522 static int qiov_is_aligned(QEMUIOVector *qiov)
526 for (i = 0; i < qiov->niov; i++) {
527 if ((uintptr_t) qiov->iov[i].iov_base % 512) {
535 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
536 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
537 BlockDriverCompletionFunc *cb, void *opaque, int type)
539 BDRVRawState *s = bs->opaque;
545 * If O_DIRECT is used the buffer needs to be aligned on a sector
546 * boundary. Check if this is the case or telll the low-level
547 * driver that it needs to copy the buffer.
549 if (s->aligned_buf) {
550 if (!qiov_is_aligned(qiov)) {
551 type |= QEMU_AIO_MISALIGNED;
552 #ifdef CONFIG_LINUX_AIO
553 } else if (s->use_aio) {
554 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
555 nb_sectors, cb, opaque, type);
560 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
564 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
565 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
566 BlockDriverCompletionFunc *cb, void *opaque)
568 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
569 cb, opaque, QEMU_AIO_READ);
572 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
573 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
574 BlockDriverCompletionFunc *cb, void *opaque)
576 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
577 cb, opaque, QEMU_AIO_WRITE);
580 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
581 BlockDriverCompletionFunc *cb, void *opaque)
583 BDRVRawState *s = bs->opaque;
588 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
591 static void raw_close(BlockDriverState *bs)
593 BDRVRawState *s = bs->opaque;
597 if (s->aligned_buf != NULL)
598 qemu_free(s->aligned_buf);
602 static int raw_truncate(BlockDriverState *bs, int64_t offset)
604 BDRVRawState *s = bs->opaque;
605 if (s->type != FTYPE_FILE)
607 if (ftruncate(s->fd, offset) < 0)
613 static int64_t raw_getlength(BlockDriverState *bs)
615 BDRVRawState *s = bs->opaque;
621 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
624 if (ioctl(fd, DIOCGDINFO, &dl))
626 return (uint64_t)dl.d_secsize *
627 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
631 #else /* !__OpenBSD__ */
632 static int64_t raw_getlength(BlockDriverState *bs)
634 BDRVRawState *s = bs->opaque;
639 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
644 struct dk_minfo minfo;
654 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
657 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
658 #ifdef DIOCGMEDIASIZE
659 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
660 #elif defined(DIOCGPART)
663 if (ioctl(fd, DIOCGPART, &pi) == 0)
664 size = pi.media_size;
671 size = LONG_LONG_MAX;
673 size = lseek(fd, 0LL, SEEK_END);
675 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
678 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
679 if (size == 2048LL * (unsigned)-1)
681 /* XXX no disc? maybe we need to reopen... */
682 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
692 * use the DKIOCGMEDIAINFO ioctl to read the size.
694 rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
696 size = minfo.dki_lbsize * minfo.dki_capacity;
697 } else /* there are reports that lseek on some devices
698 fails, but irc discussion said that contingency
699 on contingency was overkill */
702 size = lseek(fd, 0, SEEK_END);
708 static int raw_create(const char *filename, QEMUOptionParameter *options)
712 int64_t total_size = 0;
714 /* Read out options */
715 while (options && options->name) {
716 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
717 total_size = options->value.n / 512;
722 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
727 if (ftruncate(fd, total_size * 512) != 0) {
730 if (close(fd) != 0) {
737 static void raw_flush(BlockDriverState *bs)
739 BDRVRawState *s = bs->opaque;
740 qemu_fdatasync(s->fd);
744 static QEMUOptionParameter raw_create_options[] = {
746 .name = BLOCK_OPT_SIZE,
748 .help = "Virtual disk size"
753 static BlockDriver bdrv_raw = {
754 .format_name = "raw",
755 .instance_size = sizeof(BDRVRawState),
756 .bdrv_probe = NULL, /* no probe for protocols */
757 .bdrv_open = raw_open,
758 .bdrv_read = raw_read,
759 .bdrv_write = raw_write,
760 .bdrv_close = raw_close,
761 .bdrv_create = raw_create,
762 .bdrv_flush = raw_flush,
764 .bdrv_aio_readv = raw_aio_readv,
765 .bdrv_aio_writev = raw_aio_writev,
766 .bdrv_aio_flush = raw_aio_flush,
768 .bdrv_truncate = raw_truncate,
769 .bdrv_getlength = raw_getlength,
771 .create_options = raw_create_options,
774 /***********************************************/
778 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
779 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
781 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
783 kern_return_t kernResult;
784 mach_port_t masterPort;
785 CFMutableDictionaryRef classesToMatch;
787 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
788 if ( KERN_SUCCESS != kernResult ) {
789 printf( "IOMasterPort returned %d\n", kernResult );
792 classesToMatch = IOServiceMatching( kIOCDMediaClass );
793 if ( classesToMatch == NULL ) {
794 printf( "IOServiceMatching returned a NULL dictionary.\n" );
796 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
798 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
799 if ( KERN_SUCCESS != kernResult )
801 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
807 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
809 io_object_t nextMedia;
810 kern_return_t kernResult = KERN_FAILURE;
812 nextMedia = IOIteratorNext( mediaIterator );
815 CFTypeRef bsdPathAsCFString;
816 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
817 if ( bsdPathAsCFString ) {
818 size_t devPathLength;
819 strcpy( bsdPath, _PATH_DEV );
820 strcat( bsdPath, "r" );
821 devPathLength = strlen( bsdPath );
822 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
823 kernResult = KERN_SUCCESS;
825 CFRelease( bsdPathAsCFString );
827 IOObjectRelease( nextMedia );
835 static int hdev_probe_device(const char *filename)
839 /* allow a dedicated CD-ROM driver to match with a higher priority */
840 if (strstart(filename, "/dev/cdrom", NULL))
843 if (stat(filename, &st) >= 0 &&
844 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
851 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
853 BDRVRawState *s = bs->opaque;
856 if (strstart(filename, "/dev/cdrom", NULL)) {
857 kern_return_t kernResult;
858 io_iterator_t mediaIterator;
859 char bsdPath[ MAXPATHLEN ];
862 kernResult = FindEjectableCDMedia( &mediaIterator );
863 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
865 if ( bsdPath[ 0 ] != '\0' ) {
866 strcat(bsdPath,"s0");
867 /* some CDs don't have a partition 0 */
868 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
870 bsdPath[strlen(bsdPath)-1] = '1';
878 IOObjectRelease( mediaIterator );
882 s->type = FTYPE_FILE;
883 #if defined(__linux__)
884 if (strstart(filename, "/dev/sg", NULL)) {
889 return raw_open_common(bs, filename, flags, 0);
892 #if defined(__linux__)
893 /* Note: we do not have a reliable method to detect if the floppy is
894 present. The current method is to try to open the floppy at every
895 I/O and to keep it opened during a few hundreds of ms. */
896 static int fd_open(BlockDriverState *bs)
898 BDRVRawState *s = bs->opaque;
899 int last_media_present;
901 if (s->type != FTYPE_FD)
903 last_media_present = (s->fd >= 0);
905 (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
909 printf("Floppy closed\n");
913 if (s->fd_got_error &&
914 (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
916 printf("No floppy (open delayed)\n");
920 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
922 s->fd_error_time = qemu_get_clock(rt_clock);
924 if (last_media_present)
925 s->fd_media_changed = 1;
927 printf("No floppy\n");
932 printf("Floppy opened\n");
935 if (!last_media_present)
936 s->fd_media_changed = 1;
937 s->fd_open_time = qemu_get_clock(rt_clock);
942 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
944 BDRVRawState *s = bs->opaque;
946 return ioctl(s->fd, req, buf);
949 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
950 unsigned long int req, void *buf,
951 BlockDriverCompletionFunc *cb, void *opaque)
953 BDRVRawState *s = bs->opaque;
957 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
960 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
961 static int fd_open(BlockDriverState *bs)
963 BDRVRawState *s = bs->opaque;
965 /* this is just to ensure s->fd is sane (its called by io ops) */
970 #else /* !linux && !FreeBSD */
972 static int fd_open(BlockDriverState *bs)
977 #endif /* !linux && !FreeBSD */
979 static int hdev_create(const char *filename, QEMUOptionParameter *options)
983 struct stat stat_buf;
984 int64_t total_size = 0;
986 /* Read out options */
987 while (options && options->name) {
988 if (!strcmp(options->name, "size")) {
989 total_size = options->value.n / 512;
994 fd = open(filename, O_WRONLY | O_BINARY);
998 if (fstat(fd, &stat_buf) < 0)
1000 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1002 else if (lseek(fd, 0, SEEK_END) < total_size * 512)
1009 static BlockDriver bdrv_host_device = {
1010 .format_name = "host_device",
1011 .instance_size = sizeof(BDRVRawState),
1012 .bdrv_probe_device = hdev_probe_device,
1013 .bdrv_open = hdev_open,
1014 .bdrv_close = raw_close,
1015 .bdrv_create = hdev_create,
1016 .create_options = raw_create_options,
1018 .bdrv_flush = raw_flush,
1020 .bdrv_aio_readv = raw_aio_readv,
1021 .bdrv_aio_writev = raw_aio_writev,
1022 .bdrv_aio_flush = raw_aio_flush,
1024 .bdrv_read = raw_read,
1025 .bdrv_write = raw_write,
1026 .bdrv_getlength = raw_getlength,
1028 /* generic scsi device */
1030 .bdrv_ioctl = hdev_ioctl,
1031 .bdrv_aio_ioctl = hdev_aio_ioctl,
1036 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1038 BDRVRawState *s = bs->opaque;
1043 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1044 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1048 /* close fd so that we can reopen it as needed */
1051 s->fd_media_changed = 1;
1056 static int floppy_probe_device(const char *filename)
1058 if (strstart(filename, "/dev/fd", NULL))
1064 static int floppy_is_inserted(BlockDriverState *bs)
1066 return fd_open(bs) >= 0;
1069 static int floppy_media_changed(BlockDriverState *bs)
1071 BDRVRawState *s = bs->opaque;
1075 * XXX: we do not have a true media changed indication.
1076 * It does not work if the floppy is changed without trying to read it.
1079 ret = s->fd_media_changed;
1080 s->fd_media_changed = 0;
1082 printf("Floppy changed=%d\n", ret);
1087 static int floppy_eject(BlockDriverState *bs, int eject_flag)
1089 BDRVRawState *s = bs->opaque;
1096 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1098 if (ioctl(fd, FDEJECT, 0) < 0)
1106 static BlockDriver bdrv_host_floppy = {
1107 .format_name = "host_floppy",
1108 .instance_size = sizeof(BDRVRawState),
1109 .bdrv_probe_device = floppy_probe_device,
1110 .bdrv_open = floppy_open,
1111 .bdrv_close = raw_close,
1112 .bdrv_create = hdev_create,
1113 .create_options = raw_create_options,
1115 .bdrv_flush = raw_flush,
1117 .bdrv_aio_readv = raw_aio_readv,
1118 .bdrv_aio_writev = raw_aio_writev,
1119 .bdrv_aio_flush = raw_aio_flush,
1121 .bdrv_read = raw_read,
1122 .bdrv_write = raw_write,
1123 .bdrv_getlength = raw_getlength,
1125 /* removable device support */
1126 .bdrv_is_inserted = floppy_is_inserted,
1127 .bdrv_media_changed = floppy_media_changed,
1128 .bdrv_eject = floppy_eject,
1131 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1133 BDRVRawState *s = bs->opaque;
1137 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1138 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1141 static int cdrom_probe_device(const char *filename)
1143 if (strstart(filename, "/dev/cd", NULL))
1148 static int cdrom_is_inserted(BlockDriverState *bs)
1150 BDRVRawState *s = bs->opaque;
1153 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1154 if (ret == CDS_DISC_OK)
1159 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1161 BDRVRawState *s = bs->opaque;
1164 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1165 perror("CDROMEJECT");
1167 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1168 perror("CDROMEJECT");
1174 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1176 BDRVRawState *s = bs->opaque;
1178 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1180 * Note: an error can happen if the distribution automatically
1183 /* perror("CDROM_LOCKDOOR"); */
1189 static BlockDriver bdrv_host_cdrom = {
1190 .format_name = "host_cdrom",
1191 .instance_size = sizeof(BDRVRawState),
1192 .bdrv_probe_device = cdrom_probe_device,
1193 .bdrv_open = cdrom_open,
1194 .bdrv_close = raw_close,
1195 .bdrv_create = hdev_create,
1196 .create_options = raw_create_options,
1198 .bdrv_flush = raw_flush,
1200 .bdrv_aio_readv = raw_aio_readv,
1201 .bdrv_aio_writev = raw_aio_writev,
1202 .bdrv_aio_flush = raw_aio_flush,
1204 .bdrv_read = raw_read,
1205 .bdrv_write = raw_write,
1206 .bdrv_getlength = raw_getlength,
1208 /* removable device support */
1209 .bdrv_is_inserted = cdrom_is_inserted,
1210 .bdrv_eject = cdrom_eject,
1211 .bdrv_set_locked = cdrom_set_locked,
1213 /* generic scsi device */
1214 .bdrv_ioctl = hdev_ioctl,
1215 .bdrv_aio_ioctl = hdev_aio_ioctl,
1217 #endif /* __linux__ */
1219 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1220 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1222 BDRVRawState *s = bs->opaque;
1227 ret = raw_open_common(bs, filename, flags, 0);
1231 /* make sure the door isnt locked at this time */
1232 ioctl(s->fd, CDIOCALLOW);
1236 static int cdrom_probe_device(const char *filename)
1238 if (strstart(filename, "/dev/cd", NULL) ||
1239 strstart(filename, "/dev/acd", NULL))
1244 static int cdrom_reopen(BlockDriverState *bs)
1246 BDRVRawState *s = bs->opaque;
1250 * Force reread of possibly changed/newly loaded disc,
1251 * FreeBSD seems to not notice sometimes...
1255 fd = open(bs->filename, s->open_flags, 0644);
1262 /* make sure the door isnt locked at this time */
1263 ioctl(s->fd, CDIOCALLOW);
1267 static int cdrom_is_inserted(BlockDriverState *bs)
1269 return raw_getlength(bs) > 0;
1272 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1274 BDRVRawState *s = bs->opaque;
1279 (void) ioctl(s->fd, CDIOCALLOW);
1282 if (ioctl(s->fd, CDIOCEJECT) < 0)
1283 perror("CDIOCEJECT");
1285 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1286 perror("CDIOCCLOSE");
1289 if (cdrom_reopen(bs) < 0)
1294 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1296 BDRVRawState *s = bs->opaque;
1300 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1302 * Note: an error can happen if the distribution automatically
1305 /* perror("CDROM_LOCKDOOR"); */
1311 static BlockDriver bdrv_host_cdrom = {
1312 .format_name = "host_cdrom",
1313 .instance_size = sizeof(BDRVRawState),
1314 .bdrv_probe_device = cdrom_probe_device,
1315 .bdrv_open = cdrom_open,
1316 .bdrv_close = raw_close,
1317 .bdrv_create = hdev_create,
1318 .create_options = raw_create_options,
1320 .bdrv_flush = raw_flush,
1322 .bdrv_aio_readv = raw_aio_readv,
1323 .bdrv_aio_writev = raw_aio_writev,
1324 .bdrv_aio_flush = raw_aio_flush,
1326 .bdrv_read = raw_read,
1327 .bdrv_write = raw_write,
1328 .bdrv_getlength = raw_getlength,
1330 /* removable device support */
1331 .bdrv_is_inserted = cdrom_is_inserted,
1332 .bdrv_eject = cdrom_eject,
1333 .bdrv_set_locked = cdrom_set_locked,
1335 #endif /* __FreeBSD__ */
1337 static void bdrv_raw_init(void)
1340 * Register all the drivers. Note that order is important, the driver
1341 * registered last will get probed first.
1343 bdrv_register(&bdrv_raw);
1344 bdrv_register(&bdrv_host_device);
1346 bdrv_register(&bdrv_host_floppy);
1347 bdrv_register(&bdrv_host_cdrom);
1349 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1350 bdrv_register(&bdrv_host_cdrom);
1354 block_init(bdrv_raw_init);