Test coroutine execution order
[qemu/cris-port.git] / block / raw-posix.c
blobf836c8e745c4c37631ca50c11b98e3ecadc1708d
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 QemuOptsList raw_runtime_opts = {
266 .name = "raw",
267 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
268 .desc = {
270 .name = "filename",
271 .type = QEMU_OPT_STRING,
272 .help = "File name of the image",
274 { /* end of list */ }
278 static int raw_open_common(BlockDriverState *bs, QDict *options,
279 int bdrv_flags, int open_flags, Error **errp)
281 BDRVRawState *s = bs->opaque;
282 QemuOpts *opts;
283 Error *local_err = NULL;
284 const char *filename;
285 int fd, ret;
287 opts = qemu_opts_create_nofail(&raw_runtime_opts);
288 qemu_opts_absorb_qdict(opts, options, &local_err);
289 if (error_is_set(&local_err)) {
290 error_propagate(errp, local_err);
291 ret = -EINVAL;
292 goto fail;
295 filename = qemu_opt_get(opts, "filename");
297 ret = raw_normalize_devicepath(&filename);
298 if (ret != 0) {
299 error_setg_errno(errp, -ret, "Could not normalize device path");
300 goto fail;
303 s->open_flags = open_flags;
304 raw_parse_flags(bdrv_flags, &s->open_flags);
306 s->fd = -1;
307 fd = qemu_open(filename, s->open_flags, 0644);
308 if (fd < 0) {
309 ret = -errno;
310 if (ret == -EROFS) {
311 ret = -EACCES;
313 goto fail;
315 s->fd = fd;
317 #ifdef CONFIG_LINUX_AIO
318 if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
319 qemu_close(fd);
320 ret = -errno;
321 error_setg_errno(errp, -ret, "Could not set AIO state");
322 goto fail;
324 #endif
326 s->has_discard = 1;
327 #ifdef CONFIG_XFS
328 if (platform_test_xfs_fd(s->fd)) {
329 s->is_xfs = 1;
331 #endif
333 ret = 0;
334 fail:
335 qemu_opts_del(opts);
336 return ret;
339 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
340 Error **errp)
342 BDRVRawState *s = bs->opaque;
343 Error *local_err = NULL;
344 int ret;
346 s->type = FTYPE_FILE;
347 ret = raw_open_common(bs, options, flags, 0, &local_err);
348 if (error_is_set(&local_err)) {
349 error_propagate(errp, local_err);
351 return ret;
354 static int raw_reopen_prepare(BDRVReopenState *state,
355 BlockReopenQueue *queue, Error **errp)
357 BDRVRawState *s;
358 BDRVRawReopenState *raw_s;
359 int ret = 0;
361 assert(state != NULL);
362 assert(state->bs != NULL);
364 s = state->bs->opaque;
366 state->opaque = g_malloc0(sizeof(BDRVRawReopenState));
367 raw_s = state->opaque;
369 #ifdef CONFIG_LINUX_AIO
370 raw_s->use_aio = s->use_aio;
372 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
373 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
374 * won't override aio_ctx if aio_ctx is non-NULL */
375 if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
376 error_setg(errp, "Could not set AIO state");
377 return -1;
379 #endif
381 if (s->type == FTYPE_FD || s->type == FTYPE_CD) {
382 raw_s->open_flags |= O_NONBLOCK;
385 raw_parse_flags(state->flags, &raw_s->open_flags);
387 raw_s->fd = -1;
389 int fcntl_flags = O_APPEND | O_NONBLOCK;
390 #ifdef O_NOATIME
391 fcntl_flags |= O_NOATIME;
392 #endif
394 #ifdef O_ASYNC
395 /* Not all operating systems have O_ASYNC, and those that don't
396 * will not let us track the state into raw_s->open_flags (typically
397 * you achieve the same effect with an ioctl, for example I_SETSIG
398 * on Solaris). But we do not use O_ASYNC, so that's fine.
400 assert((s->open_flags & O_ASYNC) == 0);
401 #endif
403 if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
404 /* dup the original fd */
405 /* TODO: use qemu fcntl wrapper */
406 #ifdef F_DUPFD_CLOEXEC
407 raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
408 #else
409 raw_s->fd = dup(s->fd);
410 if (raw_s->fd != -1) {
411 qemu_set_cloexec(raw_s->fd);
413 #endif
414 if (raw_s->fd >= 0) {
415 ret = fcntl_setfl(raw_s->fd, raw_s->open_flags);
416 if (ret) {
417 qemu_close(raw_s->fd);
418 raw_s->fd = -1;
423 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
424 if (raw_s->fd == -1) {
425 assert(!(raw_s->open_flags & O_CREAT));
426 raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
427 if (raw_s->fd == -1) {
428 error_setg_errno(errp, errno, "Could not reopen file");
429 ret = -1;
432 return ret;
436 static void raw_reopen_commit(BDRVReopenState *state)
438 BDRVRawReopenState *raw_s = state->opaque;
439 BDRVRawState *s = state->bs->opaque;
441 s->open_flags = raw_s->open_flags;
443 qemu_close(s->fd);
444 s->fd = raw_s->fd;
445 #ifdef CONFIG_LINUX_AIO
446 s->use_aio = raw_s->use_aio;
447 #endif
449 g_free(state->opaque);
450 state->opaque = NULL;
454 static void raw_reopen_abort(BDRVReopenState *state)
456 BDRVRawReopenState *raw_s = state->opaque;
458 /* nothing to do if NULL, we didn't get far enough */
459 if (raw_s == NULL) {
460 return;
463 if (raw_s->fd >= 0) {
464 qemu_close(raw_s->fd);
465 raw_s->fd = -1;
467 g_free(state->opaque);
468 state->opaque = NULL;
472 /* XXX: use host sector size if necessary with:
473 #ifdef DIOCGSECTORSIZE
475 unsigned int sectorsize = 512;
476 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
477 sectorsize > bufsize)
478 bufsize = sectorsize;
480 #endif
481 #ifdef CONFIG_COCOA
482 uint32_t blockSize = 512;
483 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
484 bufsize = blockSize;
486 #endif
489 static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
491 int ret;
493 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
494 if (ret == -1) {
495 return -errno;
498 return 0;
501 static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
503 int ret;
505 ret = qemu_fdatasync(aiocb->aio_fildes);
506 if (ret == -1) {
507 return -errno;
509 return 0;
512 #ifdef CONFIG_PREADV
514 static bool preadv_present = true;
516 static ssize_t
517 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
519 return preadv(fd, iov, nr_iov, offset);
522 static ssize_t
523 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
525 return pwritev(fd, iov, nr_iov, offset);
528 #else
530 static bool preadv_present = false;
532 static ssize_t
533 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
535 return -ENOSYS;
538 static ssize_t
539 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
541 return -ENOSYS;
544 #endif
546 static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
548 ssize_t len;
550 do {
551 if (aiocb->aio_type & QEMU_AIO_WRITE)
552 len = qemu_pwritev(aiocb->aio_fildes,
553 aiocb->aio_iov,
554 aiocb->aio_niov,
555 aiocb->aio_offset);
556 else
557 len = qemu_preadv(aiocb->aio_fildes,
558 aiocb->aio_iov,
559 aiocb->aio_niov,
560 aiocb->aio_offset);
561 } while (len == -1 && errno == EINTR);
563 if (len == -1) {
564 return -errno;
566 return len;
570 * Read/writes the data to/from a given linear buffer.
572 * Returns the number of bytes handles or -errno in case of an error. Short
573 * reads are only returned if the end of the file is reached.
575 static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
577 ssize_t offset = 0;
578 ssize_t len;
580 while (offset < aiocb->aio_nbytes) {
581 if (aiocb->aio_type & QEMU_AIO_WRITE) {
582 len = pwrite(aiocb->aio_fildes,
583 (const char *)buf + offset,
584 aiocb->aio_nbytes - offset,
585 aiocb->aio_offset + offset);
586 } else {
587 len = pread(aiocb->aio_fildes,
588 buf + offset,
589 aiocb->aio_nbytes - offset,
590 aiocb->aio_offset + offset);
592 if (len == -1 && errno == EINTR) {
593 continue;
594 } else if (len == -1) {
595 offset = -errno;
596 break;
597 } else if (len == 0) {
598 break;
600 offset += len;
603 return offset;
606 static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
608 ssize_t nbytes;
609 char *buf;
611 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
613 * If there is just a single buffer, and it is properly aligned
614 * we can just use plain pread/pwrite without any problems.
616 if (aiocb->aio_niov == 1) {
617 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
620 * We have more than one iovec, and all are properly aligned.
622 * Try preadv/pwritev first and fall back to linearizing the
623 * buffer if it's not supported.
625 if (preadv_present) {
626 nbytes = handle_aiocb_rw_vector(aiocb);
627 if (nbytes == aiocb->aio_nbytes ||
628 (nbytes < 0 && nbytes != -ENOSYS)) {
629 return nbytes;
631 preadv_present = false;
635 * XXX(hch): short read/write. no easy way to handle the reminder
636 * using these interfaces. For now retry using plain
637 * pread/pwrite?
642 * Ok, we have to do it the hard way, copy all segments into
643 * a single aligned buffer.
645 buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes);
646 if (aiocb->aio_type & QEMU_AIO_WRITE) {
647 char *p = buf;
648 int i;
650 for (i = 0; i < aiocb->aio_niov; ++i) {
651 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
652 p += aiocb->aio_iov[i].iov_len;
656 nbytes = handle_aiocb_rw_linear(aiocb, buf);
657 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
658 char *p = buf;
659 size_t count = aiocb->aio_nbytes, copy;
660 int i;
662 for (i = 0; i < aiocb->aio_niov && count; ++i) {
663 copy = count;
664 if (copy > aiocb->aio_iov[i].iov_len) {
665 copy = aiocb->aio_iov[i].iov_len;
667 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
668 p += copy;
669 count -= copy;
672 qemu_vfree(buf);
674 return nbytes;
677 #ifdef CONFIG_XFS
678 static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
680 struct xfs_flock64 fl;
682 memset(&fl, 0, sizeof(fl));
683 fl.l_whence = SEEK_SET;
684 fl.l_start = offset;
685 fl.l_len = bytes;
687 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
688 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
689 return -errno;
692 return 0;
694 #endif
696 static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
698 int ret = -EOPNOTSUPP;
699 BDRVRawState *s = aiocb->bs->opaque;
701 if (s->has_discard == 0) {
702 return 0;
705 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
706 #ifdef BLKDISCARD
707 do {
708 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
709 if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
710 return 0;
712 } while (errno == EINTR);
714 ret = -errno;
715 #endif
716 } else {
717 #ifdef CONFIG_XFS
718 if (s->is_xfs) {
719 return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
721 #endif
723 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
724 do {
725 if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
726 aiocb->aio_offset, aiocb->aio_nbytes) == 0) {
727 return 0;
729 } while (errno == EINTR);
731 ret = -errno;
732 #endif
735 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
736 ret == -ENOTTY) {
737 s->has_discard = 0;
738 ret = 0;
740 return ret;
743 static int aio_worker(void *arg)
745 RawPosixAIOData *aiocb = arg;
746 ssize_t ret = 0;
748 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
749 case QEMU_AIO_READ:
750 ret = handle_aiocb_rw(aiocb);
751 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
752 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
753 0, aiocb->aio_nbytes - ret);
755 ret = aiocb->aio_nbytes;
757 if (ret == aiocb->aio_nbytes) {
758 ret = 0;
759 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
760 ret = -EINVAL;
762 break;
763 case QEMU_AIO_WRITE:
764 ret = handle_aiocb_rw(aiocb);
765 if (ret == aiocb->aio_nbytes) {
766 ret = 0;
767 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
768 ret = -EINVAL;
770 break;
771 case QEMU_AIO_FLUSH:
772 ret = handle_aiocb_flush(aiocb);
773 break;
774 case QEMU_AIO_IOCTL:
775 ret = handle_aiocb_ioctl(aiocb);
776 break;
777 case QEMU_AIO_DISCARD:
778 ret = handle_aiocb_discard(aiocb);
779 break;
780 default:
781 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
782 ret = -EINVAL;
783 break;
786 g_slice_free(RawPosixAIOData, aiocb);
787 return ret;
790 static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
791 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
792 BlockDriverCompletionFunc *cb, void *opaque, int type)
794 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
795 ThreadPool *pool;
797 acb->bs = bs;
798 acb->aio_type = type;
799 acb->aio_fildes = fd;
801 if (qiov) {
802 acb->aio_iov = qiov->iov;
803 acb->aio_niov = qiov->niov;
805 acb->aio_nbytes = nb_sectors * 512;
806 acb->aio_offset = sector_num * 512;
808 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
809 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
810 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
813 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
814 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
815 BlockDriverCompletionFunc *cb, void *opaque, int type)
817 BDRVRawState *s = bs->opaque;
819 if (fd_open(bs) < 0)
820 return NULL;
823 * If O_DIRECT is used the buffer needs to be aligned on a sector
824 * boundary. Check if this is the case or tell the low-level
825 * driver that it needs to copy the buffer.
827 if ((bs->open_flags & BDRV_O_NOCACHE)) {
828 if (!bdrv_qiov_is_aligned(bs, qiov)) {
829 type |= QEMU_AIO_MISALIGNED;
830 #ifdef CONFIG_LINUX_AIO
831 } else if (s->use_aio) {
832 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
833 nb_sectors, cb, opaque, type);
834 #endif
838 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
839 cb, opaque, type);
842 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
843 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
844 BlockDriverCompletionFunc *cb, void *opaque)
846 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
847 cb, opaque, QEMU_AIO_READ);
850 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
851 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
852 BlockDriverCompletionFunc *cb, void *opaque)
854 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
855 cb, opaque, QEMU_AIO_WRITE);
858 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
859 BlockDriverCompletionFunc *cb, void *opaque)
861 BDRVRawState *s = bs->opaque;
863 if (fd_open(bs) < 0)
864 return NULL;
866 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
869 static void raw_close(BlockDriverState *bs)
871 BDRVRawState *s = bs->opaque;
872 if (s->fd >= 0) {
873 qemu_close(s->fd);
874 s->fd = -1;
878 static int raw_truncate(BlockDriverState *bs, int64_t offset)
880 BDRVRawState *s = bs->opaque;
881 struct stat st;
883 if (fstat(s->fd, &st)) {
884 return -errno;
887 if (S_ISREG(st.st_mode)) {
888 if (ftruncate(s->fd, offset) < 0) {
889 return -errno;
891 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
892 if (offset > raw_getlength(bs)) {
893 return -EINVAL;
895 } else {
896 return -ENOTSUP;
899 return 0;
902 #ifdef __OpenBSD__
903 static int64_t raw_getlength(BlockDriverState *bs)
905 BDRVRawState *s = bs->opaque;
906 int fd = s->fd;
907 struct stat st;
909 if (fstat(fd, &st))
910 return -1;
911 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
912 struct disklabel dl;
914 if (ioctl(fd, DIOCGDINFO, &dl))
915 return -1;
916 return (uint64_t)dl.d_secsize *
917 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
918 } else
919 return st.st_size;
921 #elif defined(__NetBSD__)
922 static int64_t raw_getlength(BlockDriverState *bs)
924 BDRVRawState *s = bs->opaque;
925 int fd = s->fd;
926 struct stat st;
928 if (fstat(fd, &st))
929 return -1;
930 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
931 struct dkwedge_info dkw;
933 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
934 return dkw.dkw_size * 512;
935 } else {
936 struct disklabel dl;
938 if (ioctl(fd, DIOCGDINFO, &dl))
939 return -1;
940 return (uint64_t)dl.d_secsize *
941 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
943 } else
944 return st.st_size;
946 #elif defined(__sun__)
947 static int64_t raw_getlength(BlockDriverState *bs)
949 BDRVRawState *s = bs->opaque;
950 struct dk_minfo minfo;
951 int ret;
953 ret = fd_open(bs);
954 if (ret < 0) {
955 return ret;
959 * Use the DKIOCGMEDIAINFO ioctl to read the size.
961 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
962 if (ret != -1) {
963 return minfo.dki_lbsize * minfo.dki_capacity;
967 * There are reports that lseek on some devices fails, but
968 * irc discussion said that contingency on contingency was overkill.
970 return lseek(s->fd, 0, SEEK_END);
972 #elif defined(CONFIG_BSD)
973 static int64_t raw_getlength(BlockDriverState *bs)
975 BDRVRawState *s = bs->opaque;
976 int fd = s->fd;
977 int64_t size;
978 struct stat sb;
979 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
980 int reopened = 0;
981 #endif
982 int ret;
984 ret = fd_open(bs);
985 if (ret < 0)
986 return ret;
988 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
989 again:
990 #endif
991 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
992 #ifdef DIOCGMEDIASIZE
993 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
994 #elif defined(DIOCGPART)
996 struct partinfo pi;
997 if (ioctl(fd, DIOCGPART, &pi) == 0)
998 size = pi.media_size;
999 else
1000 size = 0;
1002 if (size == 0)
1003 #endif
1004 #if defined(__APPLE__) && defined(__MACH__)
1005 size = LONG_LONG_MAX;
1006 #else
1007 size = lseek(fd, 0LL, SEEK_END);
1008 #endif
1009 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1010 switch(s->type) {
1011 case FTYPE_CD:
1012 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1013 if (size == 2048LL * (unsigned)-1)
1014 size = 0;
1015 /* XXX no disc? maybe we need to reopen... */
1016 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
1017 reopened = 1;
1018 goto again;
1021 #endif
1022 } else {
1023 size = lseek(fd, 0, SEEK_END);
1025 return size;
1027 #else
1028 static int64_t raw_getlength(BlockDriverState *bs)
1030 BDRVRawState *s = bs->opaque;
1031 int ret;
1033 ret = fd_open(bs);
1034 if (ret < 0) {
1035 return ret;
1038 return lseek(s->fd, 0, SEEK_END);
1040 #endif
1042 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1044 struct stat st;
1045 BDRVRawState *s = bs->opaque;
1047 if (fstat(s->fd, &st) < 0) {
1048 return -errno;
1050 return (int64_t)st.st_blocks * 512;
1053 static int raw_create(const char *filename, QEMUOptionParameter *options,
1054 Error **errp)
1056 int fd;
1057 int result = 0;
1058 int64_t total_size = 0;
1060 /* Read out options */
1061 while (options && options->name) {
1062 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1063 total_size = options->value.n / BDRV_SECTOR_SIZE;
1065 options++;
1068 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1069 0644);
1070 if (fd < 0) {
1071 result = -errno;
1072 error_setg_errno(errp, -result, "Could not create file");
1073 } else {
1074 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
1075 result = -errno;
1076 error_setg_errno(errp, -result, "Could not resize file");
1078 if (qemu_close(fd) != 0) {
1079 result = -errno;
1080 error_setg_errno(errp, -result, "Could not close the new file");
1083 return result;
1087 * Returns true iff the specified sector is present in the disk image. Drivers
1088 * not implementing the functionality are assumed to not support backing files,
1089 * hence all their sectors are reported as allocated.
1091 * If 'sector_num' is beyond the end of the disk image the return value is 0
1092 * and 'pnum' is set to 0.
1094 * 'pnum' is set to the number of sectors (including and immediately following
1095 * the specified sector) that are known to be in the same
1096 * allocated/unallocated state.
1098 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1099 * beyond the end of the disk image it will be clamped.
1101 static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
1102 int64_t sector_num,
1103 int nb_sectors, int *pnum)
1105 off_t start, data, hole;
1106 int64_t ret;
1108 ret = fd_open(bs);
1109 if (ret < 0) {
1110 return ret;
1113 start = sector_num * BDRV_SECTOR_SIZE;
1114 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
1116 #ifdef CONFIG_FIEMAP
1118 BDRVRawState *s = bs->opaque;
1119 struct {
1120 struct fiemap fm;
1121 struct fiemap_extent fe;
1122 } f;
1124 f.fm.fm_start = start;
1125 f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
1126 f.fm.fm_flags = 0;
1127 f.fm.fm_extent_count = 1;
1128 f.fm.fm_reserved = 0;
1129 if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
1130 /* Assume everything is allocated. */
1131 *pnum = nb_sectors;
1132 return ret;
1135 if (f.fm.fm_mapped_extents == 0) {
1136 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1137 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1139 off_t length = lseek(s->fd, 0, SEEK_END);
1140 hole = f.fm.fm_start;
1141 data = MIN(f.fm.fm_start + f.fm.fm_length, length);
1142 } else {
1143 data = f.fe.fe_logical;
1144 hole = f.fe.fe_logical + f.fe.fe_length;
1145 if (f.fe.fe_flags & FIEMAP_EXTENT_UNWRITTEN) {
1146 ret |= BDRV_BLOCK_ZERO;
1150 #elif defined SEEK_HOLE && defined SEEK_DATA
1152 BDRVRawState *s = bs->opaque;
1154 hole = lseek(s->fd, start, SEEK_HOLE);
1155 if (hole == -1) {
1156 /* -ENXIO indicates that sector_num was past the end of the file.
1157 * There is a virtual hole there. */
1158 assert(errno != -ENXIO);
1160 /* Most likely EINVAL. Assume everything is allocated. */
1161 *pnum = nb_sectors;
1162 return ret;
1165 if (hole > start) {
1166 data = start;
1167 } else {
1168 /* On a hole. We need another syscall to find its end. */
1169 data = lseek(s->fd, start, SEEK_DATA);
1170 if (data == -1) {
1171 data = lseek(s->fd, 0, SEEK_END);
1174 #else
1175 data = 0;
1176 hole = start + nb_sectors * BDRV_SECTOR_SIZE;
1177 #endif
1179 if (data <= start) {
1180 /* On a data extent, compute sectors to the end of the extent. */
1181 *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
1182 } else {
1183 /* On a hole, compute sectors to the beginning of the next extent. */
1184 *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
1185 ret &= ~BDRV_BLOCK_DATA;
1186 ret |= BDRV_BLOCK_ZERO;
1189 return ret;
1192 static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
1193 int64_t sector_num, int nb_sectors,
1194 BlockDriverCompletionFunc *cb, void *opaque)
1196 BDRVRawState *s = bs->opaque;
1198 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1199 cb, opaque, QEMU_AIO_DISCARD);
1202 static QEMUOptionParameter raw_create_options[] = {
1204 .name = BLOCK_OPT_SIZE,
1205 .type = OPT_SIZE,
1206 .help = "Virtual disk size"
1208 { NULL }
1211 static BlockDriver bdrv_file = {
1212 .format_name = "file",
1213 .protocol_name = "file",
1214 .instance_size = sizeof(BDRVRawState),
1215 .bdrv_needs_filename = true,
1216 .bdrv_probe = NULL, /* no probe for protocols */
1217 .bdrv_file_open = raw_open,
1218 .bdrv_reopen_prepare = raw_reopen_prepare,
1219 .bdrv_reopen_commit = raw_reopen_commit,
1220 .bdrv_reopen_abort = raw_reopen_abort,
1221 .bdrv_close = raw_close,
1222 .bdrv_create = raw_create,
1223 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1224 .bdrv_co_get_block_status = raw_co_get_block_status,
1226 .bdrv_aio_readv = raw_aio_readv,
1227 .bdrv_aio_writev = raw_aio_writev,
1228 .bdrv_aio_flush = raw_aio_flush,
1229 .bdrv_aio_discard = raw_aio_discard,
1231 .bdrv_truncate = raw_truncate,
1232 .bdrv_getlength = raw_getlength,
1233 .bdrv_get_allocated_file_size
1234 = raw_get_allocated_file_size,
1236 .create_options = raw_create_options,
1239 /***********************************************/
1240 /* host device */
1242 #if defined(__APPLE__) && defined(__MACH__)
1243 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
1244 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
1246 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
1248 kern_return_t kernResult;
1249 mach_port_t masterPort;
1250 CFMutableDictionaryRef classesToMatch;
1252 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1253 if ( KERN_SUCCESS != kernResult ) {
1254 printf( "IOMasterPort returned %d\n", kernResult );
1257 classesToMatch = IOServiceMatching( kIOCDMediaClass );
1258 if ( classesToMatch == NULL ) {
1259 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1260 } else {
1261 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
1263 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
1264 if ( KERN_SUCCESS != kernResult )
1266 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
1269 return kernResult;
1272 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
1274 io_object_t nextMedia;
1275 kern_return_t kernResult = KERN_FAILURE;
1276 *bsdPath = '\0';
1277 nextMedia = IOIteratorNext( mediaIterator );
1278 if ( nextMedia )
1280 CFTypeRef bsdPathAsCFString;
1281 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1282 if ( bsdPathAsCFString ) {
1283 size_t devPathLength;
1284 strcpy( bsdPath, _PATH_DEV );
1285 strcat( bsdPath, "r" );
1286 devPathLength = strlen( bsdPath );
1287 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1288 kernResult = KERN_SUCCESS;
1290 CFRelease( bsdPathAsCFString );
1292 IOObjectRelease( nextMedia );
1295 return kernResult;
1298 #endif
1300 static int hdev_probe_device(const char *filename)
1302 struct stat st;
1304 /* allow a dedicated CD-ROM driver to match with a higher priority */
1305 if (strstart(filename, "/dev/cdrom", NULL))
1306 return 50;
1308 if (stat(filename, &st) >= 0 &&
1309 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1310 return 100;
1313 return 0;
1316 static int check_hdev_writable(BDRVRawState *s)
1318 #if defined(BLKROGET)
1319 /* Linux block devices can be configured "read-only" using blockdev(8).
1320 * This is independent of device node permissions and therefore open(2)
1321 * with O_RDWR succeeds. Actual writes fail with EPERM.
1323 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1324 * check for read-only block devices so that Linux block devices behave
1325 * properly.
1327 struct stat st;
1328 int readonly = 0;
1330 if (fstat(s->fd, &st)) {
1331 return -errno;
1334 if (!S_ISBLK(st.st_mode)) {
1335 return 0;
1338 if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
1339 return -errno;
1342 if (readonly) {
1343 return -EACCES;
1345 #endif /* defined(BLKROGET) */
1346 return 0;
1349 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
1350 Error **errp)
1352 BDRVRawState *s = bs->opaque;
1353 Error *local_err = NULL;
1354 int ret;
1355 const char *filename = qdict_get_str(options, "filename");
1357 #if defined(__APPLE__) && defined(__MACH__)
1358 if (strstart(filename, "/dev/cdrom", NULL)) {
1359 kern_return_t kernResult;
1360 io_iterator_t mediaIterator;
1361 char bsdPath[ MAXPATHLEN ];
1362 int fd;
1364 kernResult = FindEjectableCDMedia( &mediaIterator );
1365 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1367 if ( bsdPath[ 0 ] != '\0' ) {
1368 strcat(bsdPath,"s0");
1369 /* some CDs don't have a partition 0 */
1370 fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1371 if (fd < 0) {
1372 bsdPath[strlen(bsdPath)-1] = '1';
1373 } else {
1374 qemu_close(fd);
1376 filename = bsdPath;
1377 qdict_put(options, "filename", qstring_from_str(filename));
1380 if ( mediaIterator )
1381 IOObjectRelease( mediaIterator );
1383 #endif
1385 s->type = FTYPE_FILE;
1386 #if defined(__linux__)
1388 char resolved_path[ MAXPATHLEN ], *temp;
1390 temp = realpath(filename, resolved_path);
1391 if (temp && strstart(temp, "/dev/sg", NULL)) {
1392 bs->sg = 1;
1395 #endif
1397 ret = raw_open_common(bs, options, flags, 0, &local_err);
1398 if (ret < 0) {
1399 if (error_is_set(&local_err)) {
1400 error_propagate(errp, local_err);
1402 return ret;
1405 if (flags & BDRV_O_RDWR) {
1406 ret = check_hdev_writable(s);
1407 if (ret < 0) {
1408 raw_close(bs);
1409 error_setg_errno(errp, -ret, "The device is not writable");
1410 return ret;
1414 return ret;
1417 #if defined(__linux__)
1418 /* Note: we do not have a reliable method to detect if the floppy is
1419 present. The current method is to try to open the floppy at every
1420 I/O and to keep it opened during a few hundreds of ms. */
1421 static int fd_open(BlockDriverState *bs)
1423 BDRVRawState *s = bs->opaque;
1424 int last_media_present;
1426 if (s->type != FTYPE_FD)
1427 return 0;
1428 last_media_present = (s->fd >= 0);
1429 if (s->fd >= 0 &&
1430 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1431 qemu_close(s->fd);
1432 s->fd = -1;
1433 #ifdef DEBUG_FLOPPY
1434 printf("Floppy closed\n");
1435 #endif
1437 if (s->fd < 0) {
1438 if (s->fd_got_error &&
1439 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1440 #ifdef DEBUG_FLOPPY
1441 printf("No floppy (open delayed)\n");
1442 #endif
1443 return -EIO;
1445 s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
1446 if (s->fd < 0) {
1447 s->fd_error_time = get_clock();
1448 s->fd_got_error = 1;
1449 if (last_media_present)
1450 s->fd_media_changed = 1;
1451 #ifdef DEBUG_FLOPPY
1452 printf("No floppy\n");
1453 #endif
1454 return -EIO;
1456 #ifdef DEBUG_FLOPPY
1457 printf("Floppy opened\n");
1458 #endif
1460 if (!last_media_present)
1461 s->fd_media_changed = 1;
1462 s->fd_open_time = get_clock();
1463 s->fd_got_error = 0;
1464 return 0;
1467 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1469 BDRVRawState *s = bs->opaque;
1471 return ioctl(s->fd, req, buf);
1474 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1475 unsigned long int req, void *buf,
1476 BlockDriverCompletionFunc *cb, void *opaque)
1478 BDRVRawState *s = bs->opaque;
1479 RawPosixAIOData *acb;
1480 ThreadPool *pool;
1482 if (fd_open(bs) < 0)
1483 return NULL;
1485 acb = g_slice_new(RawPosixAIOData);
1486 acb->bs = bs;
1487 acb->aio_type = QEMU_AIO_IOCTL;
1488 acb->aio_fildes = s->fd;
1489 acb->aio_offset = 0;
1490 acb->aio_ioctl_buf = buf;
1491 acb->aio_ioctl_cmd = req;
1492 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1493 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
1496 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1497 static int fd_open(BlockDriverState *bs)
1499 BDRVRawState *s = bs->opaque;
1501 /* this is just to ensure s->fd is sane (its called by io ops) */
1502 if (s->fd >= 0)
1503 return 0;
1504 return -EIO;
1506 #else /* !linux && !FreeBSD */
1508 static int fd_open(BlockDriverState *bs)
1510 return 0;
1513 #endif /* !linux && !FreeBSD */
1515 static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
1516 int64_t sector_num, int nb_sectors,
1517 BlockDriverCompletionFunc *cb, void *opaque)
1519 BDRVRawState *s = bs->opaque;
1521 if (fd_open(bs) < 0) {
1522 return NULL;
1524 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1525 cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1528 static int hdev_create(const char *filename, QEMUOptionParameter *options,
1529 Error **errp)
1531 int fd;
1532 int ret = 0;
1533 struct stat stat_buf;
1534 int64_t total_size = 0;
1536 /* Read out options */
1537 while (options && options->name) {
1538 if (!strcmp(options->name, "size")) {
1539 total_size = options->value.n / BDRV_SECTOR_SIZE;
1541 options++;
1544 fd = qemu_open(filename, O_WRONLY | O_BINARY);
1545 if (fd < 0) {
1546 ret = -errno;
1547 error_setg_errno(errp, -ret, "Could not open device");
1548 return ret;
1551 if (fstat(fd, &stat_buf) < 0) {
1552 ret = -errno;
1553 error_setg_errno(errp, -ret, "Could not stat device");
1554 } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) {
1555 error_setg(errp,
1556 "The given file is neither a block nor a character device");
1557 ret = -ENODEV;
1558 } else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) {
1559 error_setg(errp, "Device is too small");
1560 ret = -ENOSPC;
1563 qemu_close(fd);
1564 return ret;
1567 static BlockDriver bdrv_host_device = {
1568 .format_name = "host_device",
1569 .protocol_name = "host_device",
1570 .instance_size = sizeof(BDRVRawState),
1571 .bdrv_needs_filename = true,
1572 .bdrv_probe_device = hdev_probe_device,
1573 .bdrv_file_open = hdev_open,
1574 .bdrv_close = raw_close,
1575 .bdrv_reopen_prepare = raw_reopen_prepare,
1576 .bdrv_reopen_commit = raw_reopen_commit,
1577 .bdrv_reopen_abort = raw_reopen_abort,
1578 .bdrv_create = hdev_create,
1579 .create_options = raw_create_options,
1581 .bdrv_aio_readv = raw_aio_readv,
1582 .bdrv_aio_writev = raw_aio_writev,
1583 .bdrv_aio_flush = raw_aio_flush,
1584 .bdrv_aio_discard = hdev_aio_discard,
1586 .bdrv_truncate = raw_truncate,
1587 .bdrv_getlength = raw_getlength,
1588 .bdrv_get_allocated_file_size
1589 = raw_get_allocated_file_size,
1591 /* generic scsi device */
1592 #ifdef __linux__
1593 .bdrv_ioctl = hdev_ioctl,
1594 .bdrv_aio_ioctl = hdev_aio_ioctl,
1595 #endif
1598 #ifdef __linux__
1599 static int floppy_open(BlockDriverState *bs, QDict *options, int flags,
1600 Error **errp)
1602 BDRVRawState *s = bs->opaque;
1603 Error *local_err = NULL;
1604 int ret;
1606 s->type = FTYPE_FD;
1608 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1609 ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
1610 if (ret) {
1611 if (error_is_set(&local_err)) {
1612 error_propagate(errp, local_err);
1614 return ret;
1617 /* close fd so that we can reopen it as needed */
1618 qemu_close(s->fd);
1619 s->fd = -1;
1620 s->fd_media_changed = 1;
1622 return 0;
1625 static int floppy_probe_device(const char *filename)
1627 int fd, ret;
1628 int prio = 0;
1629 struct floppy_struct fdparam;
1630 struct stat st;
1632 if (strstart(filename, "/dev/fd", NULL) &&
1633 !strstart(filename, "/dev/fdset/", NULL)) {
1634 prio = 50;
1637 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1638 if (fd < 0) {
1639 goto out;
1641 ret = fstat(fd, &st);
1642 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1643 goto outc;
1646 /* Attempt to detect via a floppy specific ioctl */
1647 ret = ioctl(fd, FDGETPRM, &fdparam);
1648 if (ret >= 0)
1649 prio = 100;
1651 outc:
1652 qemu_close(fd);
1653 out:
1654 return prio;
1658 static int floppy_is_inserted(BlockDriverState *bs)
1660 return fd_open(bs) >= 0;
1663 static int floppy_media_changed(BlockDriverState *bs)
1665 BDRVRawState *s = bs->opaque;
1666 int ret;
1669 * XXX: we do not have a true media changed indication.
1670 * It does not work if the floppy is changed without trying to read it.
1672 fd_open(bs);
1673 ret = s->fd_media_changed;
1674 s->fd_media_changed = 0;
1675 #ifdef DEBUG_FLOPPY
1676 printf("Floppy changed=%d\n", ret);
1677 #endif
1678 return ret;
1681 static void floppy_eject(BlockDriverState *bs, bool eject_flag)
1683 BDRVRawState *s = bs->opaque;
1684 int fd;
1686 if (s->fd >= 0) {
1687 qemu_close(s->fd);
1688 s->fd = -1;
1690 fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK);
1691 if (fd >= 0) {
1692 if (ioctl(fd, FDEJECT, 0) < 0)
1693 perror("FDEJECT");
1694 qemu_close(fd);
1698 static BlockDriver bdrv_host_floppy = {
1699 .format_name = "host_floppy",
1700 .protocol_name = "host_floppy",
1701 .instance_size = sizeof(BDRVRawState),
1702 .bdrv_needs_filename = true,
1703 .bdrv_probe_device = floppy_probe_device,
1704 .bdrv_file_open = floppy_open,
1705 .bdrv_close = raw_close,
1706 .bdrv_reopen_prepare = raw_reopen_prepare,
1707 .bdrv_reopen_commit = raw_reopen_commit,
1708 .bdrv_reopen_abort = raw_reopen_abort,
1709 .bdrv_create = hdev_create,
1710 .create_options = raw_create_options,
1712 .bdrv_aio_readv = raw_aio_readv,
1713 .bdrv_aio_writev = raw_aio_writev,
1714 .bdrv_aio_flush = raw_aio_flush,
1716 .bdrv_truncate = raw_truncate,
1717 .bdrv_getlength = raw_getlength,
1718 .has_variable_length = true,
1719 .bdrv_get_allocated_file_size
1720 = raw_get_allocated_file_size,
1722 /* removable device support */
1723 .bdrv_is_inserted = floppy_is_inserted,
1724 .bdrv_media_changed = floppy_media_changed,
1725 .bdrv_eject = floppy_eject,
1728 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
1729 Error **errp)
1731 BDRVRawState *s = bs->opaque;
1732 Error *local_err = NULL;
1733 int ret;
1735 s->type = FTYPE_CD;
1737 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1738 ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
1739 if (error_is_set(&local_err)) {
1740 error_propagate(errp, local_err);
1742 return ret;
1745 static int cdrom_probe_device(const char *filename)
1747 int fd, ret;
1748 int prio = 0;
1749 struct stat st;
1751 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1752 if (fd < 0) {
1753 goto out;
1755 ret = fstat(fd, &st);
1756 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1757 goto outc;
1760 /* Attempt to detect via a CDROM specific ioctl */
1761 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1762 if (ret >= 0)
1763 prio = 100;
1765 outc:
1766 qemu_close(fd);
1767 out:
1768 return prio;
1771 static int cdrom_is_inserted(BlockDriverState *bs)
1773 BDRVRawState *s = bs->opaque;
1774 int ret;
1776 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1777 if (ret == CDS_DISC_OK)
1778 return 1;
1779 return 0;
1782 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1784 BDRVRawState *s = bs->opaque;
1786 if (eject_flag) {
1787 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1788 perror("CDROMEJECT");
1789 } else {
1790 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1791 perror("CDROMEJECT");
1795 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1797 BDRVRawState *s = bs->opaque;
1799 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1801 * Note: an error can happen if the distribution automatically
1802 * mounts the CD-ROM
1804 /* perror("CDROM_LOCKDOOR"); */
1808 static BlockDriver bdrv_host_cdrom = {
1809 .format_name = "host_cdrom",
1810 .protocol_name = "host_cdrom",
1811 .instance_size = sizeof(BDRVRawState),
1812 .bdrv_needs_filename = true,
1813 .bdrv_probe_device = cdrom_probe_device,
1814 .bdrv_file_open = cdrom_open,
1815 .bdrv_close = raw_close,
1816 .bdrv_reopen_prepare = raw_reopen_prepare,
1817 .bdrv_reopen_commit = raw_reopen_commit,
1818 .bdrv_reopen_abort = raw_reopen_abort,
1819 .bdrv_create = hdev_create,
1820 .create_options = raw_create_options,
1822 .bdrv_aio_readv = raw_aio_readv,
1823 .bdrv_aio_writev = raw_aio_writev,
1824 .bdrv_aio_flush = raw_aio_flush,
1826 .bdrv_truncate = raw_truncate,
1827 .bdrv_getlength = raw_getlength,
1828 .has_variable_length = true,
1829 .bdrv_get_allocated_file_size
1830 = raw_get_allocated_file_size,
1832 /* removable device support */
1833 .bdrv_is_inserted = cdrom_is_inserted,
1834 .bdrv_eject = cdrom_eject,
1835 .bdrv_lock_medium = cdrom_lock_medium,
1837 /* generic scsi device */
1838 .bdrv_ioctl = hdev_ioctl,
1839 .bdrv_aio_ioctl = hdev_aio_ioctl,
1841 #endif /* __linux__ */
1843 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1844 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
1845 Error **errp)
1847 BDRVRawState *s = bs->opaque;
1848 Error *local_err = NULL;
1849 int ret;
1851 s->type = FTYPE_CD;
1853 ret = raw_open_common(bs, options, flags, 0, &local_err);
1854 if (ret) {
1855 if (error_is_set(&local_err)) {
1856 error_propagate(errp, local_err);
1858 return ret;
1861 /* make sure the door isn't locked at this time */
1862 ioctl(s->fd, CDIOCALLOW);
1863 return 0;
1866 static int cdrom_probe_device(const char *filename)
1868 if (strstart(filename, "/dev/cd", NULL) ||
1869 strstart(filename, "/dev/acd", NULL))
1870 return 100;
1871 return 0;
1874 static int cdrom_reopen(BlockDriverState *bs)
1876 BDRVRawState *s = bs->opaque;
1877 int fd;
1880 * Force reread of possibly changed/newly loaded disc,
1881 * FreeBSD seems to not notice sometimes...
1883 if (s->fd >= 0)
1884 qemu_close(s->fd);
1885 fd = qemu_open(bs->filename, s->open_flags, 0644);
1886 if (fd < 0) {
1887 s->fd = -1;
1888 return -EIO;
1890 s->fd = fd;
1892 /* make sure the door isn't locked at this time */
1893 ioctl(s->fd, CDIOCALLOW);
1894 return 0;
1897 static int cdrom_is_inserted(BlockDriverState *bs)
1899 return raw_getlength(bs) > 0;
1902 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1904 BDRVRawState *s = bs->opaque;
1906 if (s->fd < 0)
1907 return;
1909 (void) ioctl(s->fd, CDIOCALLOW);
1911 if (eject_flag) {
1912 if (ioctl(s->fd, CDIOCEJECT) < 0)
1913 perror("CDIOCEJECT");
1914 } else {
1915 if (ioctl(s->fd, CDIOCCLOSE) < 0)
1916 perror("CDIOCCLOSE");
1919 cdrom_reopen(bs);
1922 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1924 BDRVRawState *s = bs->opaque;
1926 if (s->fd < 0)
1927 return;
1928 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
1930 * Note: an error can happen if the distribution automatically
1931 * mounts the CD-ROM
1933 /* perror("CDROM_LOCKDOOR"); */
1937 static BlockDriver bdrv_host_cdrom = {
1938 .format_name = "host_cdrom",
1939 .protocol_name = "host_cdrom",
1940 .instance_size = sizeof(BDRVRawState),
1941 .bdrv_needs_filename = true,
1942 .bdrv_probe_device = cdrom_probe_device,
1943 .bdrv_file_open = cdrom_open,
1944 .bdrv_close = raw_close,
1945 .bdrv_reopen_prepare = raw_reopen_prepare,
1946 .bdrv_reopen_commit = raw_reopen_commit,
1947 .bdrv_reopen_abort = raw_reopen_abort,
1948 .bdrv_create = hdev_create,
1949 .create_options = raw_create_options,
1951 .bdrv_aio_readv = raw_aio_readv,
1952 .bdrv_aio_writev = raw_aio_writev,
1953 .bdrv_aio_flush = raw_aio_flush,
1955 .bdrv_truncate = raw_truncate,
1956 .bdrv_getlength = raw_getlength,
1957 .has_variable_length = true,
1958 .bdrv_get_allocated_file_size
1959 = raw_get_allocated_file_size,
1961 /* removable device support */
1962 .bdrv_is_inserted = cdrom_is_inserted,
1963 .bdrv_eject = cdrom_eject,
1964 .bdrv_lock_medium = cdrom_lock_medium,
1966 #endif /* __FreeBSD__ */
1968 #ifdef CONFIG_LINUX_AIO
1970 * Return the file descriptor for Linux AIO
1972 * This function is a layering violation and should be removed when it becomes
1973 * possible to call the block layer outside the global mutex. It allows the
1974 * caller to hijack the file descriptor so I/O can be performed outside the
1975 * block layer.
1977 int raw_get_aio_fd(BlockDriverState *bs)
1979 BDRVRawState *s;
1981 if (!bs->drv) {
1982 return -ENOMEDIUM;
1985 if (bs->drv == bdrv_find_format("raw")) {
1986 bs = bs->file;
1989 /* raw-posix has several protocols so just check for raw_aio_readv */
1990 if (bs->drv->bdrv_aio_readv != raw_aio_readv) {
1991 return -ENOTSUP;
1994 s = bs->opaque;
1995 if (!s->use_aio) {
1996 return -ENOTSUP;
1998 return s->fd;
2000 #endif /* CONFIG_LINUX_AIO */
2002 static void bdrv_file_init(void)
2005 * Register all the drivers. Note that order is important, the driver
2006 * registered last will get probed first.
2008 bdrv_register(&bdrv_file);
2009 bdrv_register(&bdrv_host_device);
2010 #ifdef __linux__
2011 bdrv_register(&bdrv_host_floppy);
2012 bdrv_register(&bdrv_host_cdrom);
2013 #endif
2014 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2015 bdrv_register(&bdrv_host_cdrom);
2016 #endif
2019 block_init(bdrv_file_init);