virtio-serial-s390: switch to the new API.
[qemu/kevin.git] / block / raw-posix.c
blob99ac86978081214950597748a4c6b37212fc2d8c
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/log.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "trace.h"
30 #include "block/thread-pool.h"
31 #include "qemu/iov.h"
32 #include "raw-aio.h"
34 #if defined(__APPLE__) && (__MACH__)
35 #include <paths.h>
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
44 #endif
46 #ifdef __sun__
47 #define _POSIX_PTHREAD_SEMANTICS 1
48 #include <sys/dkio.h>
49 #endif
50 #ifdef __linux__
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <sys/ioctl.h>
54 #include <sys/param.h>
55 #include <linux/cdrom.h>
56 #include <linux/fd.h>
57 #include <linux/fs.h>
58 #endif
59 #ifdef CONFIG_FIEMAP
60 #include <linux/fiemap.h>
61 #endif
62 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
63 #include <linux/falloc.h>
64 #endif
65 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
66 #include <sys/disk.h>
67 #include <sys/cdio.h>
68 #endif
70 #ifdef __OpenBSD__
71 #include <sys/ioctl.h>
72 #include <sys/disklabel.h>
73 #include <sys/dkio.h>
74 #endif
76 #ifdef __NetBSD__
77 #include <sys/ioctl.h>
78 #include <sys/disklabel.h>
79 #include <sys/dkio.h>
80 #include <sys/disk.h>
81 #endif
83 #ifdef __DragonFly__
84 #include <sys/ioctl.h>
85 #include <sys/diskslice.h>
86 #endif
88 #ifdef CONFIG_XFS
89 #include <xfs/xfs.h>
90 #endif
92 //#define DEBUG_FLOPPY
94 //#define DEBUG_BLOCK
95 #if defined(DEBUG_BLOCK)
96 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
97 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
98 #else
99 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
100 #endif
102 /* OS X does not have O_DSYNC */
103 #ifndef O_DSYNC
104 #ifdef O_SYNC
105 #define O_DSYNC O_SYNC
106 #elif defined(O_FSYNC)
107 #define O_DSYNC O_FSYNC
108 #endif
109 #endif
111 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
112 #ifndef O_DIRECT
113 #define O_DIRECT O_DSYNC
114 #endif
116 #define FTYPE_FILE 0
117 #define FTYPE_CD 1
118 #define FTYPE_FD 2
120 /* if the FD is not accessed during that time (in ns), we try to
121 reopen it to see if the disk has been changed */
122 #define FD_OPEN_TIMEOUT (1000000000)
124 #define MAX_BLOCKSIZE 4096
126 typedef struct BDRVRawState {
127 int fd;
128 int type;
129 int open_flags;
130 #if defined(__linux__)
131 /* linux floppy specific */
132 int64_t fd_open_time;
133 int64_t fd_error_time;
134 int fd_got_error;
135 int fd_media_changed;
136 #endif
137 #ifdef CONFIG_LINUX_AIO
138 int use_aio;
139 void *aio_ctx;
140 #endif
141 #ifdef CONFIG_XFS
142 bool is_xfs : 1;
143 #endif
144 bool has_discard : 1;
145 } BDRVRawState;
147 typedef struct BDRVRawReopenState {
148 int fd;
149 int open_flags;
150 #ifdef CONFIG_LINUX_AIO
151 int use_aio;
152 #endif
153 } BDRVRawReopenState;
155 static int fd_open(BlockDriverState *bs);
156 static int64_t raw_getlength(BlockDriverState *bs);
158 typedef struct RawPosixAIOData {
159 BlockDriverState *bs;
160 int aio_fildes;
161 union {
162 struct iovec *aio_iov;
163 void *aio_ioctl_buf;
165 int aio_niov;
166 uint64_t aio_nbytes;
167 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
168 off_t aio_offset;
169 int aio_type;
170 } RawPosixAIOData;
172 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
173 static int cdrom_reopen(BlockDriverState *bs);
174 #endif
176 #if defined(__NetBSD__)
177 static int raw_normalize_devicepath(const char **filename)
179 static char namebuf[PATH_MAX];
180 const char *dp, *fname;
181 struct stat sb;
183 fname = *filename;
184 dp = strrchr(fname, '/');
185 if (lstat(fname, &sb) < 0) {
186 fprintf(stderr, "%s: stat failed: %s\n",
187 fname, strerror(errno));
188 return -errno;
191 if (!S_ISBLK(sb.st_mode)) {
192 return 0;
195 if (dp == NULL) {
196 snprintf(namebuf, PATH_MAX, "r%s", fname);
197 } else {
198 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
199 (int)(dp - fname), fname, dp + 1);
201 fprintf(stderr, "%s is a block device", fname);
202 *filename = namebuf;
203 fprintf(stderr, ", using %s\n", *filename);
205 return 0;
207 #else
208 static int raw_normalize_devicepath(const char **filename)
210 return 0;
212 #endif
214 static void raw_parse_flags(int bdrv_flags, int *open_flags)
216 assert(open_flags != NULL);
218 *open_flags |= O_BINARY;
219 *open_flags &= ~O_ACCMODE;
220 if (bdrv_flags & BDRV_O_RDWR) {
221 *open_flags |= O_RDWR;
222 } else {
223 *open_flags |= O_RDONLY;
226 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
227 * and O_DIRECT for no caching. */
228 if ((bdrv_flags & BDRV_O_NOCACHE)) {
229 *open_flags |= O_DIRECT;
233 #ifdef CONFIG_LINUX_AIO
234 static int raw_set_aio(void **aio_ctx, int *use_aio, int bdrv_flags)
236 int ret = -1;
237 assert(aio_ctx != NULL);
238 assert(use_aio != NULL);
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 /* if non-NULL, laio_init() has already been run */
247 if (*aio_ctx == NULL) {
248 *aio_ctx = laio_init();
249 if (!*aio_ctx) {
250 goto error;
253 *use_aio = 1;
254 } else {
255 *use_aio = 0;
258 ret = 0;
260 error:
261 return ret;
263 #endif
265 static int raw_open_common(BlockDriverState *bs, const char *filename,
266 int bdrv_flags, int open_flags)
268 BDRVRawState *s = bs->opaque;
269 int fd, ret;
271 ret = raw_normalize_devicepath(&filename);
272 if (ret != 0) {
273 return ret;
276 s->open_flags = open_flags;
277 raw_parse_flags(bdrv_flags, &s->open_flags);
279 s->fd = -1;
280 fd = qemu_open(filename, s->open_flags, 0644);
281 if (fd < 0) {
282 ret = -errno;
283 if (ret == -EROFS)
284 ret = -EACCES;
285 return ret;
287 s->fd = fd;
289 #ifdef CONFIG_LINUX_AIO
290 if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
291 qemu_close(fd);
292 return -errno;
294 #endif
296 s->has_discard = 1;
297 #ifdef CONFIG_XFS
298 if (platform_test_xfs_fd(s->fd)) {
299 s->is_xfs = 1;
301 #endif
303 return 0;
306 static int raw_open(BlockDriverState *bs, const char *filename,
307 QDict *options, int flags)
309 BDRVRawState *s = bs->opaque;
311 s->type = FTYPE_FILE;
312 return raw_open_common(bs, filename, flags, 0);
315 static int raw_reopen_prepare(BDRVReopenState *state,
316 BlockReopenQueue *queue, Error **errp)
318 BDRVRawState *s;
319 BDRVRawReopenState *raw_s;
320 int ret = 0;
322 assert(state != NULL);
323 assert(state->bs != NULL);
325 s = state->bs->opaque;
327 state->opaque = g_malloc0(sizeof(BDRVRawReopenState));
328 raw_s = state->opaque;
330 #ifdef CONFIG_LINUX_AIO
331 raw_s->use_aio = s->use_aio;
333 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
334 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
335 * won't override aio_ctx if aio_ctx is non-NULL */
336 if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
337 return -1;
339 #endif
341 if (s->type == FTYPE_FD || s->type == FTYPE_CD) {
342 raw_s->open_flags |= O_NONBLOCK;
345 raw_parse_flags(state->flags, &raw_s->open_flags);
347 raw_s->fd = -1;
349 int fcntl_flags = O_APPEND | O_NONBLOCK;
350 #ifdef O_NOATIME
351 fcntl_flags |= O_NOATIME;
352 #endif
354 #ifdef O_ASYNC
355 /* Not all operating systems have O_ASYNC, and those that don't
356 * will not let us track the state into raw_s->open_flags (typically
357 * you achieve the same effect with an ioctl, for example I_SETSIG
358 * on Solaris). But we do not use O_ASYNC, so that's fine.
360 assert((s->open_flags & O_ASYNC) == 0);
361 #endif
363 if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
364 /* dup the original fd */
365 /* TODO: use qemu fcntl wrapper */
366 #ifdef F_DUPFD_CLOEXEC
367 raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
368 #else
369 raw_s->fd = dup(s->fd);
370 if (raw_s->fd != -1) {
371 qemu_set_cloexec(raw_s->fd);
373 #endif
374 if (raw_s->fd >= 0) {
375 ret = fcntl_setfl(raw_s->fd, raw_s->open_flags);
376 if (ret) {
377 qemu_close(raw_s->fd);
378 raw_s->fd = -1;
383 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
384 if (raw_s->fd == -1) {
385 assert(!(raw_s->open_flags & O_CREAT));
386 raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
387 if (raw_s->fd == -1) {
388 ret = -1;
391 return ret;
395 static void raw_reopen_commit(BDRVReopenState *state)
397 BDRVRawReopenState *raw_s = state->opaque;
398 BDRVRawState *s = state->bs->opaque;
400 s->open_flags = raw_s->open_flags;
402 qemu_close(s->fd);
403 s->fd = raw_s->fd;
404 #ifdef CONFIG_LINUX_AIO
405 s->use_aio = raw_s->use_aio;
406 #endif
408 g_free(state->opaque);
409 state->opaque = NULL;
413 static void raw_reopen_abort(BDRVReopenState *state)
415 BDRVRawReopenState *raw_s = state->opaque;
417 /* nothing to do if NULL, we didn't get far enough */
418 if (raw_s == NULL) {
419 return;
422 if (raw_s->fd >= 0) {
423 qemu_close(raw_s->fd);
424 raw_s->fd = -1;
426 g_free(state->opaque);
427 state->opaque = NULL;
431 /* XXX: use host sector size if necessary with:
432 #ifdef DIOCGSECTORSIZE
434 unsigned int sectorsize = 512;
435 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
436 sectorsize > bufsize)
437 bufsize = sectorsize;
439 #endif
440 #ifdef CONFIG_COCOA
441 uint32_t blockSize = 512;
442 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
443 bufsize = blockSize;
445 #endif
448 static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
450 int ret;
452 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
453 if (ret == -1) {
454 return -errno;
457 return 0;
460 static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
462 int ret;
464 ret = qemu_fdatasync(aiocb->aio_fildes);
465 if (ret == -1) {
466 return -errno;
468 return 0;
471 #ifdef CONFIG_PREADV
473 static bool preadv_present = true;
475 static ssize_t
476 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
478 return preadv(fd, iov, nr_iov, offset);
481 static ssize_t
482 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
484 return pwritev(fd, iov, nr_iov, offset);
487 #else
489 static bool preadv_present = false;
491 static ssize_t
492 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
494 return -ENOSYS;
497 static ssize_t
498 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
500 return -ENOSYS;
503 #endif
505 static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
507 ssize_t len;
509 do {
510 if (aiocb->aio_type & QEMU_AIO_WRITE)
511 len = qemu_pwritev(aiocb->aio_fildes,
512 aiocb->aio_iov,
513 aiocb->aio_niov,
514 aiocb->aio_offset);
515 else
516 len = qemu_preadv(aiocb->aio_fildes,
517 aiocb->aio_iov,
518 aiocb->aio_niov,
519 aiocb->aio_offset);
520 } while (len == -1 && errno == EINTR);
522 if (len == -1) {
523 return -errno;
525 return len;
529 * Read/writes the data to/from a given linear buffer.
531 * Returns the number of bytes handles or -errno in case of an error. Short
532 * reads are only returned if the end of the file is reached.
534 static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
536 ssize_t offset = 0;
537 ssize_t len;
539 while (offset < aiocb->aio_nbytes) {
540 if (aiocb->aio_type & QEMU_AIO_WRITE) {
541 len = pwrite(aiocb->aio_fildes,
542 (const char *)buf + offset,
543 aiocb->aio_nbytes - offset,
544 aiocb->aio_offset + offset);
545 } else {
546 len = pread(aiocb->aio_fildes,
547 buf + offset,
548 aiocb->aio_nbytes - offset,
549 aiocb->aio_offset + offset);
551 if (len == -1 && errno == EINTR) {
552 continue;
553 } else if (len == -1) {
554 offset = -errno;
555 break;
556 } else if (len == 0) {
557 break;
559 offset += len;
562 return offset;
565 static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
567 ssize_t nbytes;
568 char *buf;
570 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
572 * If there is just a single buffer, and it is properly aligned
573 * we can just use plain pread/pwrite without any problems.
575 if (aiocb->aio_niov == 1) {
576 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
579 * We have more than one iovec, and all are properly aligned.
581 * Try preadv/pwritev first and fall back to linearizing the
582 * buffer if it's not supported.
584 if (preadv_present) {
585 nbytes = handle_aiocb_rw_vector(aiocb);
586 if (nbytes == aiocb->aio_nbytes ||
587 (nbytes < 0 && nbytes != -ENOSYS)) {
588 return nbytes;
590 preadv_present = false;
594 * XXX(hch): short read/write. no easy way to handle the reminder
595 * using these interfaces. For now retry using plain
596 * pread/pwrite?
601 * Ok, we have to do it the hard way, copy all segments into
602 * a single aligned buffer.
604 buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes);
605 if (aiocb->aio_type & QEMU_AIO_WRITE) {
606 char *p = buf;
607 int i;
609 for (i = 0; i < aiocb->aio_niov; ++i) {
610 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
611 p += aiocb->aio_iov[i].iov_len;
615 nbytes = handle_aiocb_rw_linear(aiocb, buf);
616 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
617 char *p = buf;
618 size_t count = aiocb->aio_nbytes, copy;
619 int i;
621 for (i = 0; i < aiocb->aio_niov && count; ++i) {
622 copy = count;
623 if (copy > aiocb->aio_iov[i].iov_len) {
624 copy = aiocb->aio_iov[i].iov_len;
626 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
627 p += copy;
628 count -= copy;
631 qemu_vfree(buf);
633 return nbytes;
636 #ifdef CONFIG_XFS
637 static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
639 struct xfs_flock64 fl;
641 memset(&fl, 0, sizeof(fl));
642 fl.l_whence = SEEK_SET;
643 fl.l_start = offset;
644 fl.l_len = bytes;
646 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
647 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
648 return -errno;
651 return 0;
653 #endif
655 static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
657 int ret = -EOPNOTSUPP;
658 BDRVRawState *s = aiocb->bs->opaque;
660 if (s->has_discard == 0) {
661 return 0;
664 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
665 #ifdef BLKDISCARD
666 do {
667 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
668 if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
669 return 0;
671 } while (errno == EINTR);
673 ret = -errno;
674 #endif
675 } else {
676 #ifdef CONFIG_XFS
677 if (s->is_xfs) {
678 return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
680 #endif
682 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
683 do {
684 if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
685 aiocb->aio_offset, aiocb->aio_nbytes) == 0) {
686 return 0;
688 } while (errno == EINTR);
690 ret = -errno;
691 #endif
694 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
695 ret == -ENOTTY) {
696 s->has_discard = 0;
697 ret = 0;
699 return ret;
702 static int aio_worker(void *arg)
704 RawPosixAIOData *aiocb = arg;
705 ssize_t ret = 0;
707 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
708 case QEMU_AIO_READ:
709 ret = handle_aiocb_rw(aiocb);
710 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
711 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
712 0, aiocb->aio_nbytes - ret);
714 ret = aiocb->aio_nbytes;
716 if (ret == aiocb->aio_nbytes) {
717 ret = 0;
718 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
719 ret = -EINVAL;
721 break;
722 case QEMU_AIO_WRITE:
723 ret = handle_aiocb_rw(aiocb);
724 if (ret == aiocb->aio_nbytes) {
725 ret = 0;
726 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
727 ret = -EINVAL;
729 break;
730 case QEMU_AIO_FLUSH:
731 ret = handle_aiocb_flush(aiocb);
732 break;
733 case QEMU_AIO_IOCTL:
734 ret = handle_aiocb_ioctl(aiocb);
735 break;
736 case QEMU_AIO_DISCARD:
737 ret = handle_aiocb_discard(aiocb);
738 break;
739 default:
740 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
741 ret = -EINVAL;
742 break;
745 g_slice_free(RawPosixAIOData, aiocb);
746 return ret;
749 static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
750 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
751 BlockDriverCompletionFunc *cb, void *opaque, int type)
753 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
754 ThreadPool *pool;
756 acb->bs = bs;
757 acb->aio_type = type;
758 acb->aio_fildes = fd;
760 if (qiov) {
761 acb->aio_iov = qiov->iov;
762 acb->aio_niov = qiov->niov;
764 acb->aio_nbytes = nb_sectors * 512;
765 acb->aio_offset = sector_num * 512;
767 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
768 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
769 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
772 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
773 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
774 BlockDriverCompletionFunc *cb, void *opaque, int type)
776 BDRVRawState *s = bs->opaque;
778 if (fd_open(bs) < 0)
779 return NULL;
782 * If O_DIRECT is used the buffer needs to be aligned on a sector
783 * boundary. Check if this is the case or tell the low-level
784 * driver that it needs to copy the buffer.
786 if ((bs->open_flags & BDRV_O_NOCACHE)) {
787 if (!bdrv_qiov_is_aligned(bs, qiov)) {
788 type |= QEMU_AIO_MISALIGNED;
789 #ifdef CONFIG_LINUX_AIO
790 } else if (s->use_aio) {
791 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
792 nb_sectors, cb, opaque, type);
793 #endif
797 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
798 cb, opaque, type);
801 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
802 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
803 BlockDriverCompletionFunc *cb, void *opaque)
805 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
806 cb, opaque, QEMU_AIO_READ);
809 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
810 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
811 BlockDriverCompletionFunc *cb, void *opaque)
813 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
814 cb, opaque, QEMU_AIO_WRITE);
817 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
818 BlockDriverCompletionFunc *cb, void *opaque)
820 BDRVRawState *s = bs->opaque;
822 if (fd_open(bs) < 0)
823 return NULL;
825 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
828 static void raw_close(BlockDriverState *bs)
830 BDRVRawState *s = bs->opaque;
831 if (s->fd >= 0) {
832 qemu_close(s->fd);
833 s->fd = -1;
837 static int raw_truncate(BlockDriverState *bs, int64_t offset)
839 BDRVRawState *s = bs->opaque;
840 struct stat st;
842 if (fstat(s->fd, &st)) {
843 return -errno;
846 if (S_ISREG(st.st_mode)) {
847 if (ftruncate(s->fd, offset) < 0) {
848 return -errno;
850 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
851 if (offset > raw_getlength(bs)) {
852 return -EINVAL;
854 } else {
855 return -ENOTSUP;
858 return 0;
861 #ifdef __OpenBSD__
862 static int64_t raw_getlength(BlockDriverState *bs)
864 BDRVRawState *s = bs->opaque;
865 int fd = s->fd;
866 struct stat st;
868 if (fstat(fd, &st))
869 return -1;
870 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
871 struct disklabel dl;
873 if (ioctl(fd, DIOCGDINFO, &dl))
874 return -1;
875 return (uint64_t)dl.d_secsize *
876 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
877 } else
878 return st.st_size;
880 #elif defined(__NetBSD__)
881 static int64_t raw_getlength(BlockDriverState *bs)
883 BDRVRawState *s = bs->opaque;
884 int fd = s->fd;
885 struct stat st;
887 if (fstat(fd, &st))
888 return -1;
889 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
890 struct dkwedge_info dkw;
892 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
893 return dkw.dkw_size * 512;
894 } else {
895 struct disklabel dl;
897 if (ioctl(fd, DIOCGDINFO, &dl))
898 return -1;
899 return (uint64_t)dl.d_secsize *
900 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
902 } else
903 return st.st_size;
905 #elif defined(__sun__)
906 static int64_t raw_getlength(BlockDriverState *bs)
908 BDRVRawState *s = bs->opaque;
909 struct dk_minfo minfo;
910 int ret;
912 ret = fd_open(bs);
913 if (ret < 0) {
914 return ret;
918 * Use the DKIOCGMEDIAINFO ioctl to read the size.
920 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
921 if (ret != -1) {
922 return minfo.dki_lbsize * minfo.dki_capacity;
926 * There are reports that lseek on some devices fails, but
927 * irc discussion said that contingency on contingency was overkill.
929 return lseek(s->fd, 0, SEEK_END);
931 #elif defined(CONFIG_BSD)
932 static int64_t raw_getlength(BlockDriverState *bs)
934 BDRVRawState *s = bs->opaque;
935 int fd = s->fd;
936 int64_t size;
937 struct stat sb;
938 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
939 int reopened = 0;
940 #endif
941 int ret;
943 ret = fd_open(bs);
944 if (ret < 0)
945 return ret;
947 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
948 again:
949 #endif
950 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
951 #ifdef DIOCGMEDIASIZE
952 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
953 #elif defined(DIOCGPART)
955 struct partinfo pi;
956 if (ioctl(fd, DIOCGPART, &pi) == 0)
957 size = pi.media_size;
958 else
959 size = 0;
961 if (size == 0)
962 #endif
963 #if defined(__APPLE__) && defined(__MACH__)
964 size = LONG_LONG_MAX;
965 #else
966 size = lseek(fd, 0LL, SEEK_END);
967 #endif
968 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
969 switch(s->type) {
970 case FTYPE_CD:
971 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
972 if (size == 2048LL * (unsigned)-1)
973 size = 0;
974 /* XXX no disc? maybe we need to reopen... */
975 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
976 reopened = 1;
977 goto again;
980 #endif
981 } else {
982 size = lseek(fd, 0, SEEK_END);
984 return size;
986 #else
987 static int64_t raw_getlength(BlockDriverState *bs)
989 BDRVRawState *s = bs->opaque;
990 int ret;
992 ret = fd_open(bs);
993 if (ret < 0) {
994 return ret;
997 return lseek(s->fd, 0, SEEK_END);
999 #endif
1001 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1003 struct stat st;
1004 BDRVRawState *s = bs->opaque;
1006 if (fstat(s->fd, &st) < 0) {
1007 return -errno;
1009 return (int64_t)st.st_blocks * 512;
1012 static int raw_create(const char *filename, QEMUOptionParameter *options)
1014 int fd;
1015 int result = 0;
1016 int64_t total_size = 0;
1018 /* Read out options */
1019 while (options && options->name) {
1020 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1021 total_size = options->value.n / BDRV_SECTOR_SIZE;
1023 options++;
1026 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1027 0644);
1028 if (fd < 0) {
1029 result = -errno;
1030 } else {
1031 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
1032 result = -errno;
1034 if (qemu_close(fd) != 0) {
1035 result = -errno;
1038 return result;
1042 * Returns true iff the specified sector is present in the disk image. Drivers
1043 * not implementing the functionality are assumed to not support backing files,
1044 * hence all their sectors are reported as allocated.
1046 * If 'sector_num' is beyond the end of the disk image the return value is 0
1047 * and 'pnum' is set to 0.
1049 * 'pnum' is set to the number of sectors (including and immediately following
1050 * the specified sector) that are known to be in the same
1051 * allocated/unallocated state.
1053 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1054 * beyond the end of the disk image it will be clamped.
1056 static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
1057 int64_t sector_num,
1058 int nb_sectors, int *pnum)
1060 off_t start, data, hole;
1061 int ret;
1063 ret = fd_open(bs);
1064 if (ret < 0) {
1065 return ret;
1068 start = sector_num * BDRV_SECTOR_SIZE;
1070 #ifdef CONFIG_FIEMAP
1072 BDRVRawState *s = bs->opaque;
1073 struct {
1074 struct fiemap fm;
1075 struct fiemap_extent fe;
1076 } f;
1078 f.fm.fm_start = start;
1079 f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
1080 f.fm.fm_flags = 0;
1081 f.fm.fm_extent_count = 1;
1082 f.fm.fm_reserved = 0;
1083 if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
1084 /* Assume everything is allocated. */
1085 *pnum = nb_sectors;
1086 return 1;
1089 if (f.fm.fm_mapped_extents == 0) {
1090 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1091 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1093 off_t length = lseek(s->fd, 0, SEEK_END);
1094 hole = f.fm.fm_start;
1095 data = MIN(f.fm.fm_start + f.fm.fm_length, length);
1096 } else {
1097 data = f.fe.fe_logical;
1098 hole = f.fe.fe_logical + f.fe.fe_length;
1101 #elif defined SEEK_HOLE && defined SEEK_DATA
1103 BDRVRawState *s = bs->opaque;
1105 hole = lseek(s->fd, start, SEEK_HOLE);
1106 if (hole == -1) {
1107 /* -ENXIO indicates that sector_num was past the end of the file.
1108 * There is a virtual hole there. */
1109 assert(errno != -ENXIO);
1111 /* Most likely EINVAL. Assume everything is allocated. */
1112 *pnum = nb_sectors;
1113 return 1;
1116 if (hole > start) {
1117 data = start;
1118 } else {
1119 /* On a hole. We need another syscall to find its end. */
1120 data = lseek(s->fd, start, SEEK_DATA);
1121 if (data == -1) {
1122 data = lseek(s->fd, 0, SEEK_END);
1125 #else
1126 *pnum = nb_sectors;
1127 return 1;
1128 #endif
1130 if (data <= start) {
1131 /* On a data extent, compute sectors to the end of the extent. */
1132 *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
1133 return 1;
1134 } else {
1135 /* On a hole, compute sectors to the beginning of the next extent. */
1136 *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
1137 return 0;
1141 static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
1142 int64_t sector_num, int nb_sectors,
1143 BlockDriverCompletionFunc *cb, void *opaque)
1145 BDRVRawState *s = bs->opaque;
1147 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1148 cb, opaque, QEMU_AIO_DISCARD);
1151 static QEMUOptionParameter raw_create_options[] = {
1153 .name = BLOCK_OPT_SIZE,
1154 .type = OPT_SIZE,
1155 .help = "Virtual disk size"
1157 { NULL }
1160 static BlockDriver bdrv_file = {
1161 .format_name = "file",
1162 .protocol_name = "file",
1163 .instance_size = sizeof(BDRVRawState),
1164 .bdrv_probe = NULL, /* no probe for protocols */
1165 .bdrv_file_open = raw_open,
1166 .bdrv_reopen_prepare = raw_reopen_prepare,
1167 .bdrv_reopen_commit = raw_reopen_commit,
1168 .bdrv_reopen_abort = raw_reopen_abort,
1169 .bdrv_close = raw_close,
1170 .bdrv_create = raw_create,
1171 .bdrv_co_is_allocated = raw_co_is_allocated,
1173 .bdrv_aio_readv = raw_aio_readv,
1174 .bdrv_aio_writev = raw_aio_writev,
1175 .bdrv_aio_flush = raw_aio_flush,
1176 .bdrv_aio_discard = raw_aio_discard,
1178 .bdrv_truncate = raw_truncate,
1179 .bdrv_getlength = raw_getlength,
1180 .bdrv_get_allocated_file_size
1181 = raw_get_allocated_file_size,
1183 .create_options = raw_create_options,
1186 /***********************************************/
1187 /* host device */
1189 #if defined(__APPLE__) && defined(__MACH__)
1190 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
1191 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
1193 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
1195 kern_return_t kernResult;
1196 mach_port_t masterPort;
1197 CFMutableDictionaryRef classesToMatch;
1199 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1200 if ( KERN_SUCCESS != kernResult ) {
1201 printf( "IOMasterPort returned %d\n", kernResult );
1204 classesToMatch = IOServiceMatching( kIOCDMediaClass );
1205 if ( classesToMatch == NULL ) {
1206 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1207 } else {
1208 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
1210 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
1211 if ( KERN_SUCCESS != kernResult )
1213 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
1216 return kernResult;
1219 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
1221 io_object_t nextMedia;
1222 kern_return_t kernResult = KERN_FAILURE;
1223 *bsdPath = '\0';
1224 nextMedia = IOIteratorNext( mediaIterator );
1225 if ( nextMedia )
1227 CFTypeRef bsdPathAsCFString;
1228 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1229 if ( bsdPathAsCFString ) {
1230 size_t devPathLength;
1231 strcpy( bsdPath, _PATH_DEV );
1232 strcat( bsdPath, "r" );
1233 devPathLength = strlen( bsdPath );
1234 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1235 kernResult = KERN_SUCCESS;
1237 CFRelease( bsdPathAsCFString );
1239 IOObjectRelease( nextMedia );
1242 return kernResult;
1245 #endif
1247 static int hdev_probe_device(const char *filename)
1249 struct stat st;
1251 /* allow a dedicated CD-ROM driver to match with a higher priority */
1252 if (strstart(filename, "/dev/cdrom", NULL))
1253 return 50;
1255 if (stat(filename, &st) >= 0 &&
1256 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1257 return 100;
1260 return 0;
1263 static int check_hdev_writable(BDRVRawState *s)
1265 #if defined(BLKROGET)
1266 /* Linux block devices can be configured "read-only" using blockdev(8).
1267 * This is independent of device node permissions and therefore open(2)
1268 * with O_RDWR succeeds. Actual writes fail with EPERM.
1270 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1271 * check for read-only block devices so that Linux block devices behave
1272 * properly.
1274 struct stat st;
1275 int readonly = 0;
1277 if (fstat(s->fd, &st)) {
1278 return -errno;
1281 if (!S_ISBLK(st.st_mode)) {
1282 return 0;
1285 if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
1286 return -errno;
1289 if (readonly) {
1290 return -EACCES;
1292 #endif /* defined(BLKROGET) */
1293 return 0;
1296 static int hdev_open(BlockDriverState *bs, const char *filename,
1297 QDict *options, int flags)
1299 BDRVRawState *s = bs->opaque;
1300 int ret;
1302 #if defined(__APPLE__) && defined(__MACH__)
1303 if (strstart(filename, "/dev/cdrom", NULL)) {
1304 kern_return_t kernResult;
1305 io_iterator_t mediaIterator;
1306 char bsdPath[ MAXPATHLEN ];
1307 int fd;
1309 kernResult = FindEjectableCDMedia( &mediaIterator );
1310 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1312 if ( bsdPath[ 0 ] != '\0' ) {
1313 strcat(bsdPath,"s0");
1314 /* some CDs don't have a partition 0 */
1315 fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1316 if (fd < 0) {
1317 bsdPath[strlen(bsdPath)-1] = '1';
1318 } else {
1319 qemu_close(fd);
1321 filename = bsdPath;
1324 if ( mediaIterator )
1325 IOObjectRelease( mediaIterator );
1327 #endif
1329 s->type = FTYPE_FILE;
1330 #if defined(__linux__)
1332 char resolved_path[ MAXPATHLEN ], *temp;
1334 temp = realpath(filename, resolved_path);
1335 if (temp && strstart(temp, "/dev/sg", NULL)) {
1336 bs->sg = 1;
1339 #endif
1341 ret = raw_open_common(bs, filename, flags, 0);
1342 if (ret < 0) {
1343 return ret;
1346 if (flags & BDRV_O_RDWR) {
1347 ret = check_hdev_writable(s);
1348 if (ret < 0) {
1349 raw_close(bs);
1350 return ret;
1354 return ret;
1357 #if defined(__linux__)
1358 /* Note: we do not have a reliable method to detect if the floppy is
1359 present. The current method is to try to open the floppy at every
1360 I/O and to keep it opened during a few hundreds of ms. */
1361 static int fd_open(BlockDriverState *bs)
1363 BDRVRawState *s = bs->opaque;
1364 int last_media_present;
1366 if (s->type != FTYPE_FD)
1367 return 0;
1368 last_media_present = (s->fd >= 0);
1369 if (s->fd >= 0 &&
1370 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1371 qemu_close(s->fd);
1372 s->fd = -1;
1373 #ifdef DEBUG_FLOPPY
1374 printf("Floppy closed\n");
1375 #endif
1377 if (s->fd < 0) {
1378 if (s->fd_got_error &&
1379 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1380 #ifdef DEBUG_FLOPPY
1381 printf("No floppy (open delayed)\n");
1382 #endif
1383 return -EIO;
1385 s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
1386 if (s->fd < 0) {
1387 s->fd_error_time = get_clock();
1388 s->fd_got_error = 1;
1389 if (last_media_present)
1390 s->fd_media_changed = 1;
1391 #ifdef DEBUG_FLOPPY
1392 printf("No floppy\n");
1393 #endif
1394 return -EIO;
1396 #ifdef DEBUG_FLOPPY
1397 printf("Floppy opened\n");
1398 #endif
1400 if (!last_media_present)
1401 s->fd_media_changed = 1;
1402 s->fd_open_time = get_clock();
1403 s->fd_got_error = 0;
1404 return 0;
1407 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1409 BDRVRawState *s = bs->opaque;
1411 return ioctl(s->fd, req, buf);
1414 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1415 unsigned long int req, void *buf,
1416 BlockDriverCompletionFunc *cb, void *opaque)
1418 BDRVRawState *s = bs->opaque;
1419 RawPosixAIOData *acb;
1420 ThreadPool *pool;
1422 if (fd_open(bs) < 0)
1423 return NULL;
1425 acb = g_slice_new(RawPosixAIOData);
1426 acb->bs = bs;
1427 acb->aio_type = QEMU_AIO_IOCTL;
1428 acb->aio_fildes = s->fd;
1429 acb->aio_offset = 0;
1430 acb->aio_ioctl_buf = buf;
1431 acb->aio_ioctl_cmd = req;
1432 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1433 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
1436 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1437 static int fd_open(BlockDriverState *bs)
1439 BDRVRawState *s = bs->opaque;
1441 /* this is just to ensure s->fd is sane (its called by io ops) */
1442 if (s->fd >= 0)
1443 return 0;
1444 return -EIO;
1446 #else /* !linux && !FreeBSD */
1448 static int fd_open(BlockDriverState *bs)
1450 return 0;
1453 #endif /* !linux && !FreeBSD */
1455 static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
1456 int64_t sector_num, int nb_sectors,
1457 BlockDriverCompletionFunc *cb, void *opaque)
1459 BDRVRawState *s = bs->opaque;
1461 if (fd_open(bs) < 0) {
1462 return NULL;
1464 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1465 cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1468 static int hdev_create(const char *filename, QEMUOptionParameter *options)
1470 int fd;
1471 int ret = 0;
1472 struct stat stat_buf;
1473 int64_t total_size = 0;
1475 /* Read out options */
1476 while (options && options->name) {
1477 if (!strcmp(options->name, "size")) {
1478 total_size = options->value.n / BDRV_SECTOR_SIZE;
1480 options++;
1483 fd = qemu_open(filename, O_WRONLY | O_BINARY);
1484 if (fd < 0)
1485 return -errno;
1487 if (fstat(fd, &stat_buf) < 0)
1488 ret = -errno;
1489 else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode))
1490 ret = -ENODEV;
1491 else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE)
1492 ret = -ENOSPC;
1494 qemu_close(fd);
1495 return ret;
1498 static int hdev_has_zero_init(BlockDriverState *bs)
1500 return 0;
1503 static BlockDriver bdrv_host_device = {
1504 .format_name = "host_device",
1505 .protocol_name = "host_device",
1506 .instance_size = sizeof(BDRVRawState),
1507 .bdrv_probe_device = hdev_probe_device,
1508 .bdrv_file_open = hdev_open,
1509 .bdrv_close = raw_close,
1510 .bdrv_reopen_prepare = raw_reopen_prepare,
1511 .bdrv_reopen_commit = raw_reopen_commit,
1512 .bdrv_reopen_abort = raw_reopen_abort,
1513 .bdrv_create = hdev_create,
1514 .create_options = raw_create_options,
1515 .bdrv_has_zero_init = hdev_has_zero_init,
1517 .bdrv_aio_readv = raw_aio_readv,
1518 .bdrv_aio_writev = raw_aio_writev,
1519 .bdrv_aio_flush = raw_aio_flush,
1520 .bdrv_aio_discard = hdev_aio_discard,
1522 .bdrv_truncate = raw_truncate,
1523 .bdrv_getlength = raw_getlength,
1524 .bdrv_get_allocated_file_size
1525 = raw_get_allocated_file_size,
1527 /* generic scsi device */
1528 #ifdef __linux__
1529 .bdrv_ioctl = hdev_ioctl,
1530 .bdrv_aio_ioctl = hdev_aio_ioctl,
1531 #endif
1534 #ifdef __linux__
1535 static int floppy_open(BlockDriverState *bs, const char *filename,
1536 QDict *options, int flags)
1538 BDRVRawState *s = bs->opaque;
1539 int ret;
1541 s->type = FTYPE_FD;
1543 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1544 ret = raw_open_common(bs, filename, flags, O_NONBLOCK);
1545 if (ret)
1546 return ret;
1548 /* close fd so that we can reopen it as needed */
1549 qemu_close(s->fd);
1550 s->fd = -1;
1551 s->fd_media_changed = 1;
1553 return 0;
1556 static int floppy_probe_device(const char *filename)
1558 int fd, ret;
1559 int prio = 0;
1560 struct floppy_struct fdparam;
1561 struct stat st;
1563 if (strstart(filename, "/dev/fd", NULL) &&
1564 !strstart(filename, "/dev/fdset/", NULL)) {
1565 prio = 50;
1568 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1569 if (fd < 0) {
1570 goto out;
1572 ret = fstat(fd, &st);
1573 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1574 goto outc;
1577 /* Attempt to detect via a floppy specific ioctl */
1578 ret = ioctl(fd, FDGETPRM, &fdparam);
1579 if (ret >= 0)
1580 prio = 100;
1582 outc:
1583 qemu_close(fd);
1584 out:
1585 return prio;
1589 static int floppy_is_inserted(BlockDriverState *bs)
1591 return fd_open(bs) >= 0;
1594 static int floppy_media_changed(BlockDriverState *bs)
1596 BDRVRawState *s = bs->opaque;
1597 int ret;
1600 * XXX: we do not have a true media changed indication.
1601 * It does not work if the floppy is changed without trying to read it.
1603 fd_open(bs);
1604 ret = s->fd_media_changed;
1605 s->fd_media_changed = 0;
1606 #ifdef DEBUG_FLOPPY
1607 printf("Floppy changed=%d\n", ret);
1608 #endif
1609 return ret;
1612 static void floppy_eject(BlockDriverState *bs, bool eject_flag)
1614 BDRVRawState *s = bs->opaque;
1615 int fd;
1617 if (s->fd >= 0) {
1618 qemu_close(s->fd);
1619 s->fd = -1;
1621 fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK);
1622 if (fd >= 0) {
1623 if (ioctl(fd, FDEJECT, 0) < 0)
1624 perror("FDEJECT");
1625 qemu_close(fd);
1629 static BlockDriver bdrv_host_floppy = {
1630 .format_name = "host_floppy",
1631 .protocol_name = "host_floppy",
1632 .instance_size = sizeof(BDRVRawState),
1633 .bdrv_probe_device = floppy_probe_device,
1634 .bdrv_file_open = floppy_open,
1635 .bdrv_close = raw_close,
1636 .bdrv_reopen_prepare = raw_reopen_prepare,
1637 .bdrv_reopen_commit = raw_reopen_commit,
1638 .bdrv_reopen_abort = raw_reopen_abort,
1639 .bdrv_create = hdev_create,
1640 .create_options = raw_create_options,
1641 .bdrv_has_zero_init = hdev_has_zero_init,
1643 .bdrv_aio_readv = raw_aio_readv,
1644 .bdrv_aio_writev = raw_aio_writev,
1645 .bdrv_aio_flush = raw_aio_flush,
1647 .bdrv_truncate = raw_truncate,
1648 .bdrv_getlength = raw_getlength,
1649 .bdrv_get_allocated_file_size
1650 = raw_get_allocated_file_size,
1652 /* removable device support */
1653 .bdrv_is_inserted = floppy_is_inserted,
1654 .bdrv_media_changed = floppy_media_changed,
1655 .bdrv_eject = floppy_eject,
1658 static int cdrom_open(BlockDriverState *bs, const char *filename,
1659 QDict *options, int flags)
1661 BDRVRawState *s = bs->opaque;
1663 s->type = FTYPE_CD;
1665 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1666 return raw_open_common(bs, filename, flags, O_NONBLOCK);
1669 static int cdrom_probe_device(const char *filename)
1671 int fd, ret;
1672 int prio = 0;
1673 struct stat st;
1675 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1676 if (fd < 0) {
1677 goto out;
1679 ret = fstat(fd, &st);
1680 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1681 goto outc;
1684 /* Attempt to detect via a CDROM specific ioctl */
1685 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1686 if (ret >= 0)
1687 prio = 100;
1689 outc:
1690 qemu_close(fd);
1691 out:
1692 return prio;
1695 static int cdrom_is_inserted(BlockDriverState *bs)
1697 BDRVRawState *s = bs->opaque;
1698 int ret;
1700 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1701 if (ret == CDS_DISC_OK)
1702 return 1;
1703 return 0;
1706 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1708 BDRVRawState *s = bs->opaque;
1710 if (eject_flag) {
1711 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1712 perror("CDROMEJECT");
1713 } else {
1714 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1715 perror("CDROMEJECT");
1719 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1721 BDRVRawState *s = bs->opaque;
1723 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1725 * Note: an error can happen if the distribution automatically
1726 * mounts the CD-ROM
1728 /* perror("CDROM_LOCKDOOR"); */
1732 static BlockDriver bdrv_host_cdrom = {
1733 .format_name = "host_cdrom",
1734 .protocol_name = "host_cdrom",
1735 .instance_size = sizeof(BDRVRawState),
1736 .bdrv_probe_device = cdrom_probe_device,
1737 .bdrv_file_open = cdrom_open,
1738 .bdrv_close = raw_close,
1739 .bdrv_reopen_prepare = raw_reopen_prepare,
1740 .bdrv_reopen_commit = raw_reopen_commit,
1741 .bdrv_reopen_abort = raw_reopen_abort,
1742 .bdrv_create = hdev_create,
1743 .create_options = raw_create_options,
1744 .bdrv_has_zero_init = hdev_has_zero_init,
1746 .bdrv_aio_readv = raw_aio_readv,
1747 .bdrv_aio_writev = raw_aio_writev,
1748 .bdrv_aio_flush = raw_aio_flush,
1750 .bdrv_truncate = raw_truncate,
1751 .bdrv_getlength = raw_getlength,
1752 .bdrv_get_allocated_file_size
1753 = raw_get_allocated_file_size,
1755 /* removable device support */
1756 .bdrv_is_inserted = cdrom_is_inserted,
1757 .bdrv_eject = cdrom_eject,
1758 .bdrv_lock_medium = cdrom_lock_medium,
1760 /* generic scsi device */
1761 .bdrv_ioctl = hdev_ioctl,
1762 .bdrv_aio_ioctl = hdev_aio_ioctl,
1764 #endif /* __linux__ */
1766 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1767 static int cdrom_open(BlockDriverState *bs, const char *filename,
1768 QDict *options, int flags)
1770 BDRVRawState *s = bs->opaque;
1771 int ret;
1773 s->type = FTYPE_CD;
1775 ret = raw_open_common(bs, filename, flags, 0);
1776 if (ret)
1777 return ret;
1779 /* make sure the door isn't locked at this time */
1780 ioctl(s->fd, CDIOCALLOW);
1781 return 0;
1784 static int cdrom_probe_device(const char *filename)
1786 if (strstart(filename, "/dev/cd", NULL) ||
1787 strstart(filename, "/dev/acd", NULL))
1788 return 100;
1789 return 0;
1792 static int cdrom_reopen(BlockDriverState *bs)
1794 BDRVRawState *s = bs->opaque;
1795 int fd;
1798 * Force reread of possibly changed/newly loaded disc,
1799 * FreeBSD seems to not notice sometimes...
1801 if (s->fd >= 0)
1802 qemu_close(s->fd);
1803 fd = qemu_open(bs->filename, s->open_flags, 0644);
1804 if (fd < 0) {
1805 s->fd = -1;
1806 return -EIO;
1808 s->fd = fd;
1810 /* make sure the door isn't locked at this time */
1811 ioctl(s->fd, CDIOCALLOW);
1812 return 0;
1815 static int cdrom_is_inserted(BlockDriverState *bs)
1817 return raw_getlength(bs) > 0;
1820 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1822 BDRVRawState *s = bs->opaque;
1824 if (s->fd < 0)
1825 return;
1827 (void) ioctl(s->fd, CDIOCALLOW);
1829 if (eject_flag) {
1830 if (ioctl(s->fd, CDIOCEJECT) < 0)
1831 perror("CDIOCEJECT");
1832 } else {
1833 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1834 perror("CDIOCCLOSE");
1837 cdrom_reopen(bs);
1840 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1842 BDRVRawState *s = bs->opaque;
1844 if (s->fd < 0)
1845 return;
1846 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1848 * Note: an error can happen if the distribution automatically
1849 * mounts the CD-ROM
1851 /* perror("CDROM_LOCKDOOR"); */
1855 static BlockDriver bdrv_host_cdrom = {
1856 .format_name = "host_cdrom",
1857 .protocol_name = "host_cdrom",
1858 .instance_size = sizeof(BDRVRawState),
1859 .bdrv_probe_device = cdrom_probe_device,
1860 .bdrv_file_open = cdrom_open,
1861 .bdrv_close = raw_close,
1862 .bdrv_reopen_prepare = raw_reopen_prepare,
1863 .bdrv_reopen_commit = raw_reopen_commit,
1864 .bdrv_reopen_abort = raw_reopen_abort,
1865 .bdrv_create = hdev_create,
1866 .create_options = raw_create_options,
1867 .bdrv_has_zero_init = hdev_has_zero_init,
1869 .bdrv_aio_readv = raw_aio_readv,
1870 .bdrv_aio_writev = raw_aio_writev,
1871 .bdrv_aio_flush = raw_aio_flush,
1873 .bdrv_truncate = raw_truncate,
1874 .bdrv_getlength = raw_getlength,
1875 .bdrv_get_allocated_file_size
1876 = raw_get_allocated_file_size,
1878 /* removable device support */
1879 .bdrv_is_inserted = cdrom_is_inserted,
1880 .bdrv_eject = cdrom_eject,
1881 .bdrv_lock_medium = cdrom_lock_medium,
1883 #endif /* __FreeBSD__ */
1885 #ifdef CONFIG_LINUX_AIO
1887 * Return the file descriptor for Linux AIO
1889 * This function is a layering violation and should be removed when it becomes
1890 * possible to call the block layer outside the global mutex. It allows the
1891 * caller to hijack the file descriptor so I/O can be performed outside the
1892 * block layer.
1894 int raw_get_aio_fd(BlockDriverState *bs)
1896 BDRVRawState *s;
1898 if (!bs->drv) {
1899 return -ENOMEDIUM;
1902 if (bs->drv == bdrv_find_format("raw")) {
1903 bs = bs->file;
1906 /* raw-posix has several protocols so just check for raw_aio_readv */
1907 if (bs->drv->bdrv_aio_readv != raw_aio_readv) {
1908 return -ENOTSUP;
1911 s = bs->opaque;
1912 if (!s->use_aio) {
1913 return -ENOTSUP;
1915 return s->fd;
1917 #endif /* CONFIG_LINUX_AIO */
1919 static void bdrv_file_init(void)
1922 * Register all the drivers. Note that order is important, the driver
1923 * registered last will get probed first.
1925 bdrv_register(&bdrv_file);
1926 bdrv_register(&bdrv_host_device);
1927 #ifdef __linux__
1928 bdrv_register(&bdrv_host_floppy);
1929 bdrv_register(&bdrv_host_cdrom);
1930 #endif
1931 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1932 bdrv_register(&bdrv_host_cdrom);
1933 #endif
1936 block_init(bdrv_file_init);