char: Prevent multiple devices opening same chardev
[qemu/agraf.git] / block / raw-posix.c
blob6b724705994d0600c471fb4c897c8a3c16d05910
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 #include "qemu-timer.h"
26 #include "qemu-char.h"
27 #include "qemu-log.h"
28 #include "block_int.h"
29 #include "module.h"
30 #include "block/raw-posix-aio.h"
32 #ifdef CONFIG_COCOA
33 #include <paths.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>
42 #endif
44 #ifdef __sun__
45 #define _POSIX_PTHREAD_SEMANTICS 1
46 #include <signal.h>
47 #include <sys/dkio.h>
48 #endif
49 #ifdef __linux__
50 #include <sys/ioctl.h>
51 #include <sys/param.h>
52 #include <linux/cdrom.h>
53 #include <linux/fd.h>
54 #endif
55 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
56 #include <signal.h>
57 #include <sys/disk.h>
58 #include <sys/cdio.h>
59 #endif
61 #ifdef __OpenBSD__
62 #include <sys/ioctl.h>
63 #include <sys/disklabel.h>
64 #include <sys/dkio.h>
65 #endif
67 #ifdef __DragonFly__
68 #include <sys/ioctl.h>
69 #include <sys/diskslice.h>
70 #endif
72 #ifdef CONFIG_XFS
73 #include <xfs/xfs.h>
74 #endif
76 //#define DEBUG_FLOPPY
78 //#define DEBUG_BLOCK
79 #if defined(DEBUG_BLOCK)
80 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
81 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
82 #else
83 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
84 #endif
86 /* OS X does not have O_DSYNC */
87 #ifndef O_DSYNC
88 #ifdef O_SYNC
89 #define O_DSYNC O_SYNC
90 #elif defined(O_FSYNC)
91 #define O_DSYNC O_FSYNC
92 #endif
93 #endif
95 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
96 #ifndef O_DIRECT
97 #define O_DIRECT O_DSYNC
98 #endif
100 #define FTYPE_FILE 0
101 #define FTYPE_CD 1
102 #define FTYPE_FD 2
104 /* if the FD is not accessed during that time (in ns), we try to
105 reopen it to see if the disk has been changed */
106 #define FD_OPEN_TIMEOUT (1000000000)
108 #define MAX_BLOCKSIZE 4096
110 typedef struct BDRVRawState {
111 int fd;
112 int type;
113 int open_flags;
114 #if defined(__linux__)
115 /* linux floppy specific */
116 int64_t fd_open_time;
117 int64_t fd_error_time;
118 int fd_got_error;
119 int fd_media_changed;
120 #endif
121 #ifdef CONFIG_LINUX_AIO
122 int use_aio;
123 void *aio_ctx;
124 #endif
125 uint8_t *aligned_buf;
126 unsigned aligned_buf_size;
127 #ifdef CONFIG_XFS
128 bool is_xfs : 1;
129 #endif
130 } BDRVRawState;
132 static int fd_open(BlockDriverState *bs);
133 static int64_t raw_getlength(BlockDriverState *bs);
135 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
136 static int cdrom_reopen(BlockDriverState *bs);
137 #endif
139 static int raw_open_common(BlockDriverState *bs, const char *filename,
140 int bdrv_flags, int open_flags)
142 BDRVRawState *s = bs->opaque;
143 int fd, ret;
145 s->open_flags = open_flags | O_BINARY;
146 s->open_flags &= ~O_ACCMODE;
147 if (bdrv_flags & BDRV_O_RDWR) {
148 s->open_flags |= O_RDWR;
149 } else {
150 s->open_flags |= O_RDONLY;
153 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
154 * and O_DIRECT for no caching. */
155 if ((bdrv_flags & BDRV_O_NOCACHE))
156 s->open_flags |= O_DIRECT;
157 else if (!(bdrv_flags & BDRV_O_CACHE_WB))
158 s->open_flags |= O_DSYNC;
160 s->fd = -1;
161 fd = qemu_open(filename, s->open_flags, 0644);
162 if (fd < 0) {
163 ret = -errno;
164 if (ret == -EROFS)
165 ret = -EACCES;
166 return ret;
168 s->fd = fd;
169 s->aligned_buf = NULL;
171 if ((bdrv_flags & BDRV_O_NOCACHE)) {
173 * Allocate a buffer for read/modify/write cycles. Chose the size
174 * pessimistically as we don't know the block size yet.
176 s->aligned_buf_size = 32 * MAX_BLOCKSIZE;
177 s->aligned_buf = qemu_memalign(MAX_BLOCKSIZE, s->aligned_buf_size);
178 if (s->aligned_buf == NULL) {
179 goto out_close;
183 #ifdef CONFIG_LINUX_AIO
184 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
185 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
187 /* We're falling back to POSIX AIO in some cases */
188 paio_init();
190 s->aio_ctx = laio_init();
191 if (!s->aio_ctx) {
192 goto out_free_buf;
194 s->use_aio = 1;
195 } else
196 #endif
198 if (paio_init() < 0) {
199 goto out_free_buf;
201 #ifdef CONFIG_LINUX_AIO
202 s->use_aio = 0;
203 #endif
206 #ifdef CONFIG_XFS
207 if (platform_test_xfs_fd(s->fd)) {
208 s->is_xfs = 1;
210 #endif
212 return 0;
214 out_free_buf:
215 qemu_vfree(s->aligned_buf);
216 out_close:
217 close(fd);
218 return -errno;
221 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
223 BDRVRawState *s = bs->opaque;
225 s->type = FTYPE_FILE;
226 return raw_open_common(bs, filename, flags, 0);
229 /* XXX: use host sector size if necessary with:
230 #ifdef DIOCGSECTORSIZE
232 unsigned int sectorsize = 512;
233 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
234 sectorsize > bufsize)
235 bufsize = sectorsize;
237 #endif
238 #ifdef CONFIG_COCOA
239 uint32_t blockSize = 512;
240 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
241 bufsize = blockSize;
243 #endif
247 * offset and count are in bytes, but must be multiples of 512 for files
248 * opened with O_DIRECT. buf must be aligned to 512 bytes then.
250 * This function may be called without alignment if the caller ensures
251 * that O_DIRECT is not in effect.
253 static int raw_pread_aligned(BlockDriverState *bs, int64_t offset,
254 uint8_t *buf, int count)
256 BDRVRawState *s = bs->opaque;
257 int ret;
259 ret = fd_open(bs);
260 if (ret < 0)
261 return ret;
263 ret = pread(s->fd, buf, count, offset);
264 if (ret == count)
265 return ret;
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);
272 return count;
276 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
277 "] read failed %d : %d = %s\n",
278 s->fd, bs->filename, offset, buf, count,
279 bs->total_sectors, ret, errno, strerror(errno));
281 /* Try harder for CDrom. */
282 if (s->type != FTYPE_FILE) {
283 ret = pread(s->fd, buf, count, offset);
284 if (ret == count)
285 return ret;
286 ret = pread(s->fd, buf, count, offset);
287 if (ret == count)
288 return ret;
290 DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
291 "] retry read failed %d : %d = %s\n",
292 s->fd, bs->filename, offset, buf, count,
293 bs->total_sectors, ret, errno, strerror(errno));
296 return (ret < 0) ? -errno : ret;
300 * offset and count are in bytes, but must be multiples of the sector size
301 * for files opened with O_DIRECT. buf must be aligned to sector size bytes
302 * then.
304 * This function may be called without alignment if the caller ensures
305 * that O_DIRECT is not in effect.
307 static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset,
308 const uint8_t *buf, int count)
310 BDRVRawState *s = bs->opaque;
311 int ret;
313 ret = fd_open(bs);
314 if (ret < 0)
315 return -errno;
317 ret = pwrite(s->fd, buf, count, offset);
318 if (ret == count)
319 return ret;
321 DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
322 "] write failed %d : %d = %s\n",
323 s->fd, bs->filename, offset, buf, count,
324 bs->total_sectors, ret, errno, strerror(errno));
326 return (ret < 0) ? -errno : ret;
331 * offset and count are in bytes and possibly not aligned. For files opened
332 * with O_DIRECT, necessary alignments are ensured before calling
333 * raw_pread_aligned to do the actual read.
335 static int raw_pread(BlockDriverState *bs, int64_t offset,
336 uint8_t *buf, int count)
338 BDRVRawState *s = bs->opaque;
339 unsigned sector_mask = bs->buffer_alignment - 1;
340 int size, ret, shift, sum;
342 sum = 0;
344 if (s->aligned_buf != NULL) {
346 if (offset & sector_mask) {
347 /* align offset on a sector size bytes boundary */
349 shift = offset & sector_mask;
350 size = (shift + count + sector_mask) & ~sector_mask;
351 if (size > s->aligned_buf_size)
352 size = s->aligned_buf_size;
353 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size);
354 if (ret < 0)
355 return ret;
357 size = bs->buffer_alignment - shift;
358 if (size > count)
359 size = count;
360 memcpy(buf, s->aligned_buf + shift, size);
362 buf += size;
363 offset += size;
364 count -= size;
365 sum += size;
367 if (count == 0)
368 return sum;
370 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
372 /* read on aligned buffer */
374 while (count) {
376 size = (count + sector_mask) & ~sector_mask;
377 if (size > s->aligned_buf_size)
378 size = s->aligned_buf_size;
380 ret = raw_pread_aligned(bs, offset, s->aligned_buf, size);
381 if (ret < 0) {
382 return ret;
383 } else if (ret == 0) {
384 fprintf(stderr, "raw_pread: read beyond end of file\n");
385 abort();
388 size = ret;
389 if (size > count)
390 size = count;
392 memcpy(buf, s->aligned_buf, size);
394 buf += size;
395 offset += size;
396 count -= size;
397 sum += size;
400 return sum;
404 return raw_pread_aligned(bs, offset, buf, count) + sum;
407 static int raw_read(BlockDriverState *bs, int64_t sector_num,
408 uint8_t *buf, int nb_sectors)
410 int ret;
412 ret = raw_pread(bs, sector_num * BDRV_SECTOR_SIZE, buf,
413 nb_sectors * BDRV_SECTOR_SIZE);
414 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
415 ret = 0;
416 return ret;
420 * offset and count are in bytes and possibly not aligned. For files opened
421 * with O_DIRECT, necessary alignments are ensured before calling
422 * raw_pwrite_aligned to do the actual write.
424 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
425 const uint8_t *buf, int count)
427 BDRVRawState *s = bs->opaque;
428 unsigned sector_mask = bs->buffer_alignment - 1;
429 int size, ret, shift, sum;
431 sum = 0;
433 if (s->aligned_buf != NULL) {
435 if (offset & sector_mask) {
436 /* align offset on a sector size bytes boundary */
437 shift = offset & sector_mask;
438 ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf,
439 bs->buffer_alignment);
440 if (ret < 0)
441 return ret;
443 size = bs->buffer_alignment - shift;
444 if (size > count)
445 size = count;
446 memcpy(s->aligned_buf + shift, buf, size);
448 ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf,
449 bs->buffer_alignment);
450 if (ret < 0)
451 return ret;
453 buf += size;
454 offset += size;
455 count -= size;
456 sum += size;
458 if (count == 0)
459 return sum;
461 if (count & sector_mask || (uintptr_t) buf & sector_mask) {
463 while ((size = (count & ~sector_mask)) != 0) {
465 if (size > s->aligned_buf_size)
466 size = s->aligned_buf_size;
468 memcpy(s->aligned_buf, buf, size);
470 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);
471 if (ret < 0)
472 return ret;
474 buf += ret;
475 offset += ret;
476 count -= ret;
477 sum += ret;
479 /* here, count < sector_size because (count & ~sector_mask) == 0 */
480 if (count) {
481 ret = raw_pread_aligned(bs, offset, s->aligned_buf,
482 bs->buffer_alignment);
483 if (ret < 0)
484 return ret;
485 memcpy(s->aligned_buf, buf, count);
487 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf,
488 bs->buffer_alignment);
489 if (ret < 0)
490 return ret;
491 if (count < ret)
492 ret = count;
494 sum += ret;
496 return sum;
499 return raw_pwrite_aligned(bs, offset, buf, count) + sum;
502 static int raw_write(BlockDriverState *bs, int64_t sector_num,
503 const uint8_t *buf, int nb_sectors)
505 int ret;
506 ret = raw_pwrite(bs, sector_num * BDRV_SECTOR_SIZE, buf,
507 nb_sectors * BDRV_SECTOR_SIZE);
508 if (ret == (nb_sectors * BDRV_SECTOR_SIZE))
509 ret = 0;
510 return ret;
514 * Check if all memory in this vector is sector aligned.
516 static int qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
518 int i;
520 for (i = 0; i < qiov->niov; i++) {
521 if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
522 return 0;
526 return 1;
529 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
530 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
531 BlockDriverCompletionFunc *cb, void *opaque, int type)
533 BDRVRawState *s = bs->opaque;
535 if (fd_open(bs) < 0)
536 return NULL;
539 * If O_DIRECT is used the buffer needs to be aligned on a sector
540 * boundary. Check if this is the case or telll the low-level
541 * driver that it needs to copy the buffer.
543 if (s->aligned_buf) {
544 if (!qiov_is_aligned(bs, qiov)) {
545 type |= QEMU_AIO_MISALIGNED;
546 #ifdef CONFIG_LINUX_AIO
547 } else if (s->use_aio) {
548 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
549 nb_sectors, cb, opaque, type);
550 #endif
554 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
555 cb, opaque, type);
558 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
559 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
560 BlockDriverCompletionFunc *cb, void *opaque)
562 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
563 cb, opaque, QEMU_AIO_READ);
566 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
567 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
568 BlockDriverCompletionFunc *cb, void *opaque)
570 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
571 cb, opaque, QEMU_AIO_WRITE);
574 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
575 BlockDriverCompletionFunc *cb, void *opaque)
577 BDRVRawState *s = bs->opaque;
579 if (fd_open(bs) < 0)
580 return NULL;
582 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
585 static void raw_close(BlockDriverState *bs)
587 BDRVRawState *s = bs->opaque;
588 if (s->fd >= 0) {
589 close(s->fd);
590 s->fd = -1;
591 if (s->aligned_buf != NULL)
592 qemu_vfree(s->aligned_buf);
596 static int raw_truncate(BlockDriverState *bs, int64_t offset)
598 BDRVRawState *s = bs->opaque;
599 if (s->type != FTYPE_FILE)
600 return -ENOTSUP;
601 if (ftruncate(s->fd, offset) < 0)
602 return -errno;
603 return 0;
606 #ifdef __OpenBSD__
607 static int64_t raw_getlength(BlockDriverState *bs)
609 BDRVRawState *s = bs->opaque;
610 int fd = s->fd;
611 struct stat st;
613 if (fstat(fd, &st))
614 return -1;
615 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
616 struct disklabel dl;
618 if (ioctl(fd, DIOCGDINFO, &dl))
619 return -1;
620 return (uint64_t)dl.d_secsize *
621 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
622 } else
623 return st.st_size;
625 #elif defined(__sun__)
626 static int64_t raw_getlength(BlockDriverState *bs)
628 BDRVRawState *s = bs->opaque;
629 struct dk_minfo minfo;
630 int ret;
632 ret = fd_open(bs);
633 if (ret < 0) {
634 return ret;
638 * Use the DKIOCGMEDIAINFO ioctl to read the size.
640 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
641 if (ret != -1) {
642 return minfo.dki_lbsize * minfo.dki_capacity;
646 * There are reports that lseek on some devices fails, but
647 * irc discussion said that contingency on contingency was overkill.
649 return lseek(s->fd, 0, SEEK_END);
651 #elif defined(CONFIG_BSD)
652 static int64_t raw_getlength(BlockDriverState *bs)
654 BDRVRawState *s = bs->opaque;
655 int fd = s->fd;
656 int64_t size;
657 struct stat sb;
658 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
659 int reopened = 0;
660 #endif
661 int ret;
663 ret = fd_open(bs);
664 if (ret < 0)
665 return ret;
667 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
668 again:
669 #endif
670 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
671 #ifdef DIOCGMEDIASIZE
672 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
673 #elif defined(DIOCGPART)
675 struct partinfo pi;
676 if (ioctl(fd, DIOCGPART, &pi) == 0)
677 size = pi.media_size;
678 else
679 size = 0;
681 if (size == 0)
682 #endif
683 #ifdef CONFIG_COCOA
684 size = LONG_LONG_MAX;
685 #else
686 size = lseek(fd, 0LL, SEEK_END);
687 #endif
688 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
689 switch(s->type) {
690 case FTYPE_CD:
691 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
692 if (size == 2048LL * (unsigned)-1)
693 size = 0;
694 /* XXX no disc? maybe we need to reopen... */
695 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
696 reopened = 1;
697 goto again;
700 #endif
701 } else {
702 size = lseek(fd, 0, SEEK_END);
704 return size;
706 #else
707 static int64_t raw_getlength(BlockDriverState *bs)
709 BDRVRawState *s = bs->opaque;
710 int ret;
712 ret = fd_open(bs);
713 if (ret < 0) {
714 return ret;
717 return lseek(s->fd, 0, SEEK_END);
719 #endif
721 static int raw_create(const char *filename, QEMUOptionParameter *options)
723 int fd;
724 int result = 0;
725 int64_t total_size = 0;
727 /* Read out options */
728 while (options && options->name) {
729 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
730 total_size = options->value.n / BDRV_SECTOR_SIZE;
732 options++;
735 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
736 0644);
737 if (fd < 0) {
738 result = -errno;
739 } else {
740 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
741 result = -errno;
743 if (close(fd) != 0) {
744 result = -errno;
747 return result;
750 static int raw_flush(BlockDriverState *bs)
752 BDRVRawState *s = bs->opaque;
753 return qemu_fdatasync(s->fd);
756 #ifdef CONFIG_XFS
757 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
759 struct xfs_flock64 fl;
761 memset(&fl, 0, sizeof(fl));
762 fl.l_whence = SEEK_SET;
763 fl.l_start = sector_num << 9;
764 fl.l_len = (int64_t)nb_sectors << 9;
766 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
767 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
768 return -errno;
771 return 0;
773 #endif
775 static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
777 #ifdef CONFIG_XFS
778 BDRVRawState *s = bs->opaque;
780 if (s->is_xfs) {
781 return xfs_discard(s, sector_num, nb_sectors);
783 #endif
785 return 0;
788 static QEMUOptionParameter raw_create_options[] = {
790 .name = BLOCK_OPT_SIZE,
791 .type = OPT_SIZE,
792 .help = "Virtual disk size"
794 { NULL }
797 static BlockDriver bdrv_file = {
798 .format_name = "file",
799 .protocol_name = "file",
800 .instance_size = sizeof(BDRVRawState),
801 .bdrv_probe = NULL, /* no probe for protocols */
802 .bdrv_file_open = raw_open,
803 .bdrv_read = raw_read,
804 .bdrv_write = raw_write,
805 .bdrv_close = raw_close,
806 .bdrv_create = raw_create,
807 .bdrv_flush = raw_flush,
808 .bdrv_discard = raw_discard,
810 .bdrv_aio_readv = raw_aio_readv,
811 .bdrv_aio_writev = raw_aio_writev,
812 .bdrv_aio_flush = raw_aio_flush,
814 .bdrv_truncate = raw_truncate,
815 .bdrv_getlength = raw_getlength,
817 .create_options = raw_create_options,
820 /***********************************************/
821 /* host device */
823 #ifdef CONFIG_COCOA
824 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
825 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
827 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
829 kern_return_t kernResult;
830 mach_port_t masterPort;
831 CFMutableDictionaryRef classesToMatch;
833 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
834 if ( KERN_SUCCESS != kernResult ) {
835 printf( "IOMasterPort returned %d\n", kernResult );
838 classesToMatch = IOServiceMatching( kIOCDMediaClass );
839 if ( classesToMatch == NULL ) {
840 printf( "IOServiceMatching returned a NULL dictionary.\n" );
841 } else {
842 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
844 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
845 if ( KERN_SUCCESS != kernResult )
847 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
850 return kernResult;
853 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
855 io_object_t nextMedia;
856 kern_return_t kernResult = KERN_FAILURE;
857 *bsdPath = '\0';
858 nextMedia = IOIteratorNext( mediaIterator );
859 if ( nextMedia )
861 CFTypeRef bsdPathAsCFString;
862 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
863 if ( bsdPathAsCFString ) {
864 size_t devPathLength;
865 strcpy( bsdPath, _PATH_DEV );
866 strcat( bsdPath, "r" );
867 devPathLength = strlen( bsdPath );
868 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
869 kernResult = KERN_SUCCESS;
871 CFRelease( bsdPathAsCFString );
873 IOObjectRelease( nextMedia );
876 return kernResult;
879 #endif
881 static int hdev_probe_device(const char *filename)
883 struct stat st;
885 /* allow a dedicated CD-ROM driver to match with a higher priority */
886 if (strstart(filename, "/dev/cdrom", NULL))
887 return 50;
889 if (stat(filename, &st) >= 0 &&
890 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
891 return 100;
894 return 0;
897 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
899 BDRVRawState *s = bs->opaque;
901 #ifdef CONFIG_COCOA
902 if (strstart(filename, "/dev/cdrom", NULL)) {
903 kern_return_t kernResult;
904 io_iterator_t mediaIterator;
905 char bsdPath[ MAXPATHLEN ];
906 int fd;
908 kernResult = FindEjectableCDMedia( &mediaIterator );
909 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
911 if ( bsdPath[ 0 ] != '\0' ) {
912 strcat(bsdPath,"s0");
913 /* some CDs don't have a partition 0 */
914 fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
915 if (fd < 0) {
916 bsdPath[strlen(bsdPath)-1] = '1';
917 } else {
918 close(fd);
920 filename = bsdPath;
923 if ( mediaIterator )
924 IOObjectRelease( mediaIterator );
926 #endif
928 s->type = FTYPE_FILE;
929 #if defined(__linux__)
931 char resolved_path[ MAXPATHLEN ], *temp;
933 temp = realpath(filename, resolved_path);
934 if (temp && strstart(temp, "/dev/sg", NULL)) {
935 bs->sg = 1;
938 #endif
940 return raw_open_common(bs, filename, flags, 0);
943 #if defined(__linux__)
944 /* Note: we do not have a reliable method to detect if the floppy is
945 present. The current method is to try to open the floppy at every
946 I/O and to keep it opened during a few hundreds of ms. */
947 static int fd_open(BlockDriverState *bs)
949 BDRVRawState *s = bs->opaque;
950 int last_media_present;
952 if (s->type != FTYPE_FD)
953 return 0;
954 last_media_present = (s->fd >= 0);
955 if (s->fd >= 0 &&
956 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
957 close(s->fd);
958 s->fd = -1;
959 #ifdef DEBUG_FLOPPY
960 printf("Floppy closed\n");
961 #endif
963 if (s->fd < 0) {
964 if (s->fd_got_error &&
965 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
966 #ifdef DEBUG_FLOPPY
967 printf("No floppy (open delayed)\n");
968 #endif
969 return -EIO;
971 s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
972 if (s->fd < 0) {
973 s->fd_error_time = get_clock();
974 s->fd_got_error = 1;
975 if (last_media_present)
976 s->fd_media_changed = 1;
977 #ifdef DEBUG_FLOPPY
978 printf("No floppy\n");
979 #endif
980 return -EIO;
982 #ifdef DEBUG_FLOPPY
983 printf("Floppy opened\n");
984 #endif
986 if (!last_media_present)
987 s->fd_media_changed = 1;
988 s->fd_open_time = get_clock();
989 s->fd_got_error = 0;
990 return 0;
993 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
995 BDRVRawState *s = bs->opaque;
997 return ioctl(s->fd, req, buf);
1000 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1001 unsigned long int req, void *buf,
1002 BlockDriverCompletionFunc *cb, void *opaque)
1004 BDRVRawState *s = bs->opaque;
1006 if (fd_open(bs) < 0)
1007 return NULL;
1008 return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
1011 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1012 static int fd_open(BlockDriverState *bs)
1014 BDRVRawState *s = bs->opaque;
1016 /* this is just to ensure s->fd is sane (its called by io ops) */
1017 if (s->fd >= 0)
1018 return 0;
1019 return -EIO;
1021 #else /* !linux && !FreeBSD */
1023 static int fd_open(BlockDriverState *bs)
1025 return 0;
1028 #endif /* !linux && !FreeBSD */
1030 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1032 int fd;
1033 int ret = 0;
1034 struct stat stat_buf;
1035 int64_t total_size = 0;
1037 /* Read out options */
1038 while (options && options->name) {
1039 if (!strcmp(options->name, "size")) {
1040 total_size = options->value.n / BDRV_SECTOR_SIZE;
1042 options++;
1045 fd = open(filename, O_WRONLY | O_BINARY);
1046 if (fd < 0)
1047 return -errno;
1049 if (fstat(fd, &stat_buf) < 0)
1050 ret = -errno;
1051 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1052 ret = -ENODEV;
1053 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1054 ret = -ENOSPC;
1056 close(fd);
1057 return ret;
1060 static int hdev_has_zero_init(BlockDriverState *bs)
1062 return 0;
1065 static BlockDriver bdrv_host_device = {
1066 .format_name = "host_device",
1067 .protocol_name = "host_device",
1068 .instance_size = sizeof(BDRVRawState),
1069 .bdrv_probe_device = hdev_probe_device,
1070 .bdrv_file_open = hdev_open,
1071 .bdrv_close = raw_close,
1072 .bdrv_create = hdev_create,
1073 .create_options = raw_create_options,
1074 .bdrv_has_zero_init = hdev_has_zero_init,
1075 .bdrv_flush = raw_flush,
1077 .bdrv_aio_readv = raw_aio_readv,
1078 .bdrv_aio_writev = raw_aio_writev,
1079 .bdrv_aio_flush = raw_aio_flush,
1081 .bdrv_read = raw_read,
1082 .bdrv_write = raw_write,
1083 .bdrv_getlength = raw_getlength,
1085 /* generic scsi device */
1086 #ifdef __linux__
1087 .bdrv_ioctl = hdev_ioctl,
1088 .bdrv_aio_ioctl = hdev_aio_ioctl,
1089 #endif
1092 #ifdef __linux__
1093 static int floppy_open(BlockDriverState *bs, const char *filename, int flags)
1095 BDRVRawState *s = bs->opaque;
1096 int ret;
1098 s->type = FTYPE_FD;
1100 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1101 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1102 if (ret)
1103 return ret;
1105 /* close fd so that we can reopen it as needed */
1106 close(s->fd);
1107 s->fd = -1;
1108 s->fd_media_changed = 1;
1110 return 0;
1113 static int floppy_probe_device(const char *filename)
1115 int fd, ret;
1116 int prio = 0;
1117 struct floppy_struct fdparam;
1119 if (strstart(filename, "/dev/fd", NULL))
1120 prio = 50;
1122 fd = open(filename, O_RDONLY | O_NONBLOCK);
1123 if (fd < 0) {
1124 goto out;
1127 /* Attempt to detect via a floppy specific ioctl */
1128 ret = ioctl(fd, FDGETPRM, &fdparam);
1129 if (ret >= 0)
1130 prio = 100;
1132 close(fd);
1133 out:
1134 return prio;
1138 static int floppy_is_inserted(BlockDriverState *bs)
1140 return fd_open(bs) >= 0;
1143 static int floppy_media_changed(BlockDriverState *bs)
1145 BDRVRawState *s = bs->opaque;
1146 int ret;
1149 * XXX: we do not have a true media changed indication.
1150 * It does not work if the floppy is changed without trying to read it.
1152 fd_open(bs);
1153 ret = s->fd_media_changed;
1154 s->fd_media_changed = 0;
1155 #ifdef DEBUG_FLOPPY
1156 printf("Floppy changed=%d\n", ret);
1157 #endif
1158 return ret;
1161 static int floppy_eject(BlockDriverState *bs, int eject_flag)
1163 BDRVRawState *s = bs->opaque;
1164 int fd;
1166 if (s->fd >= 0) {
1167 close(s->fd);
1168 s->fd = -1;
1170 fd = open(bs->filename, s->open_flags | O_NONBLOCK);
1171 if (fd >= 0) {
1172 if (ioctl(fd, FDEJECT, 0) < 0)
1173 perror("FDEJECT");
1174 close(fd);
1177 return 0;
1180 static BlockDriver bdrv_host_floppy = {
1181 .format_name = "host_floppy",
1182 .protocol_name = "host_floppy",
1183 .instance_size = sizeof(BDRVRawState),
1184 .bdrv_probe_device = floppy_probe_device,
1185 .bdrv_file_open = floppy_open,
1186 .bdrv_close = raw_close,
1187 .bdrv_create = hdev_create,
1188 .create_options = raw_create_options,
1189 .bdrv_has_zero_init = hdev_has_zero_init,
1190 .bdrv_flush = raw_flush,
1192 .bdrv_aio_readv = raw_aio_readv,
1193 .bdrv_aio_writev = raw_aio_writev,
1194 .bdrv_aio_flush = raw_aio_flush,
1196 .bdrv_read = raw_read,
1197 .bdrv_write = raw_write,
1198 .bdrv_getlength = raw_getlength,
1200 /* removable device support */
1201 .bdrv_is_inserted = floppy_is_inserted,
1202 .bdrv_media_changed = floppy_media_changed,
1203 .bdrv_eject = floppy_eject,
1206 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1208 BDRVRawState *s = bs->opaque;
1210 s->type = FTYPE_CD;
1212 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1213 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1216 static int cdrom_probe_device(const char *filename)
1218 int fd, ret;
1219 int prio = 0;
1221 fd = open(filename, O_RDONLY | O_NONBLOCK);
1222 if (fd < 0) {
1223 goto out;
1226 /* Attempt to detect via a CDROM specific ioctl */
1227 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1228 if (ret >= 0)
1229 prio = 100;
1231 close(fd);
1232 out:
1233 return prio;
1236 static int cdrom_is_inserted(BlockDriverState *bs)
1238 BDRVRawState *s = bs->opaque;
1239 int ret;
1241 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1242 if (ret == CDS_DISC_OK)
1243 return 1;
1244 return 0;
1247 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1249 BDRVRawState *s = bs->opaque;
1251 if (eject_flag) {
1252 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1253 perror("CDROMEJECT");
1254 } else {
1255 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1256 perror("CDROMEJECT");
1259 return 0;
1262 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1264 BDRVRawState *s = bs->opaque;
1266 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1268 * Note: an error can happen if the distribution automatically
1269 * mounts the CD-ROM
1271 /* perror("CDROM_LOCKDOOR"); */
1274 return 0;
1277 static BlockDriver bdrv_host_cdrom = {
1278 .format_name = "host_cdrom",
1279 .protocol_name = "host_cdrom",
1280 .instance_size = sizeof(BDRVRawState),
1281 .bdrv_probe_device = cdrom_probe_device,
1282 .bdrv_file_open = cdrom_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,
1297 /* removable device support */
1298 .bdrv_is_inserted = cdrom_is_inserted,
1299 .bdrv_eject = cdrom_eject,
1300 .bdrv_set_locked = cdrom_set_locked,
1302 /* generic scsi device */
1303 .bdrv_ioctl = hdev_ioctl,
1304 .bdrv_aio_ioctl = hdev_aio_ioctl,
1306 #endif /* __linux__ */
1308 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1309 static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
1311 BDRVRawState *s = bs->opaque;
1312 int ret;
1314 s->type = FTYPE_CD;
1316 ret = raw_open_common(bs, filename, flags, 0);
1317 if (ret)
1318 return ret;
1320 /* make sure the door isnt locked at this time */
1321 ioctl(s->fd, CDIOCALLOW);
1322 return 0;
1325 static int cdrom_probe_device(const char *filename)
1327 if (strstart(filename, "/dev/cd", NULL) ||
1328 strstart(filename, "/dev/acd", NULL))
1329 return 100;
1330 return 0;
1333 static int cdrom_reopen(BlockDriverState *bs)
1335 BDRVRawState *s = bs->opaque;
1336 int fd;
1339 * Force reread of possibly changed/newly loaded disc,
1340 * FreeBSD seems to not notice sometimes...
1342 if (s->fd >= 0)
1343 close(s->fd);
1344 fd = open(bs->filename, s->open_flags, 0644);
1345 if (fd < 0) {
1346 s->fd = -1;
1347 return -EIO;
1349 s->fd = fd;
1351 /* make sure the door isnt locked at this time */
1352 ioctl(s->fd, CDIOCALLOW);
1353 return 0;
1356 static int cdrom_is_inserted(BlockDriverState *bs)
1358 return raw_getlength(bs) > 0;
1361 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
1363 BDRVRawState *s = bs->opaque;
1365 if (s->fd < 0)
1366 return -ENOTSUP;
1368 (void) ioctl(s->fd, CDIOCALLOW);
1370 if (eject_flag) {
1371 if (ioctl(s->fd, CDIOCEJECT) < 0)
1372 perror("CDIOCEJECT");
1373 } else {
1374 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1375 perror("CDIOCCLOSE");
1378 if (cdrom_reopen(bs) < 0)
1379 return -ENOTSUP;
1380 return 0;
1383 static int cdrom_set_locked(BlockDriverState *bs, int locked)
1385 BDRVRawState *s = bs->opaque;
1387 if (s->fd < 0)
1388 return -ENOTSUP;
1389 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1391 * Note: an error can happen if the distribution automatically
1392 * mounts the CD-ROM
1394 /* perror("CDROM_LOCKDOOR"); */
1397 return 0;
1400 static BlockDriver bdrv_host_cdrom = {
1401 .format_name = "host_cdrom",
1402 .protocol_name = "host_cdrom",
1403 .instance_size = sizeof(BDRVRawState),
1404 .bdrv_probe_device = cdrom_probe_device,
1405 .bdrv_file_open = cdrom_open,
1406 .bdrv_close = raw_close,
1407 .bdrv_create = hdev_create,
1408 .create_options = raw_create_options,
1409 .bdrv_has_zero_init = hdev_has_zero_init,
1410 .bdrv_flush = raw_flush,
1412 .bdrv_aio_readv = raw_aio_readv,
1413 .bdrv_aio_writev = raw_aio_writev,
1414 .bdrv_aio_flush = raw_aio_flush,
1416 .bdrv_read = raw_read,
1417 .bdrv_write = raw_write,
1418 .bdrv_getlength = raw_getlength,
1420 /* removable device support */
1421 .bdrv_is_inserted = cdrom_is_inserted,
1422 .bdrv_eject = cdrom_eject,
1423 .bdrv_set_locked = cdrom_set_locked,
1425 #endif /* __FreeBSD__ */
1427 static void bdrv_file_init(void)
1430 * Register all the drivers. Note that order is important, the driver
1431 * registered last will get probed first.
1433 bdrv_register(&bdrv_file);
1434 bdrv_register(&bdrv_host_device);
1435 #ifdef __linux__
1436 bdrv_register(&bdrv_host_floppy);
1437 bdrv_register(&bdrv_host_cdrom);
1438 #endif
1439 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1440 bdrv_register(&bdrv_host_cdrom);
1441 #endif
1444 block_init(bdrv_file_init);