char/cadence_uart: Delete redundant rx rst logic
[qemu.git] / block / raw-posix.c
blob10c6b34ba9952dfa0793de29d2ee1c90eb56f93d
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 bool has_write_zeroes:1;
146 bool discard_zeroes:1;
147 } BDRVRawState;
149 typedef struct BDRVRawReopenState {
150 int fd;
151 int open_flags;
152 #ifdef CONFIG_LINUX_AIO
153 int use_aio;
154 #endif
155 } BDRVRawReopenState;
157 static int fd_open(BlockDriverState *bs);
158 static int64_t raw_getlength(BlockDriverState *bs);
160 typedef struct RawPosixAIOData {
161 BlockDriverState *bs;
162 int aio_fildes;
163 union {
164 struct iovec *aio_iov;
165 void *aio_ioctl_buf;
167 int aio_niov;
168 uint64_t aio_nbytes;
169 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
170 off_t aio_offset;
171 int aio_type;
172 } RawPosixAIOData;
174 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
175 static int cdrom_reopen(BlockDriverState *bs);
176 #endif
178 #if defined(__NetBSD__)
179 static int raw_normalize_devicepath(const char **filename)
181 static char namebuf[PATH_MAX];
182 const char *dp, *fname;
183 struct stat sb;
185 fname = *filename;
186 dp = strrchr(fname, '/');
187 if (lstat(fname, &sb) < 0) {
188 fprintf(stderr, "%s: stat failed: %s\n",
189 fname, strerror(errno));
190 return -errno;
193 if (!S_ISBLK(sb.st_mode)) {
194 return 0;
197 if (dp == NULL) {
198 snprintf(namebuf, PATH_MAX, "r%s", fname);
199 } else {
200 snprintf(namebuf, PATH_MAX, "%.*s/r%s",
201 (int)(dp - fname), fname, dp + 1);
203 fprintf(stderr, "%s is a block device", fname);
204 *filename = namebuf;
205 fprintf(stderr, ", using %s\n", *filename);
207 return 0;
209 #else
210 static int raw_normalize_devicepath(const char **filename)
212 return 0;
214 #endif
216 static void raw_parse_flags(int bdrv_flags, int *open_flags)
218 assert(open_flags != NULL);
220 *open_flags |= O_BINARY;
221 *open_flags &= ~O_ACCMODE;
222 if (bdrv_flags & BDRV_O_RDWR) {
223 *open_flags |= O_RDWR;
224 } else {
225 *open_flags |= O_RDONLY;
228 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
229 * and O_DIRECT for no caching. */
230 if ((bdrv_flags & BDRV_O_NOCACHE)) {
231 *open_flags |= O_DIRECT;
235 #ifdef CONFIG_LINUX_AIO
236 static int raw_set_aio(void **aio_ctx, int *use_aio, int bdrv_flags)
238 int ret = -1;
239 assert(aio_ctx != NULL);
240 assert(use_aio != NULL);
242 * Currently Linux do AIO only for files opened with O_DIRECT
243 * specified so check NOCACHE flag too
245 if ((bdrv_flags & (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) ==
246 (BDRV_O_NOCACHE|BDRV_O_NATIVE_AIO)) {
248 /* if non-NULL, laio_init() has already been run */
249 if (*aio_ctx == NULL) {
250 *aio_ctx = laio_init();
251 if (!*aio_ctx) {
252 goto error;
255 *use_aio = 1;
256 } else {
257 *use_aio = 0;
260 ret = 0;
262 error:
263 return ret;
265 #endif
267 static QemuOptsList raw_runtime_opts = {
268 .name = "raw",
269 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
270 .desc = {
272 .name = "filename",
273 .type = QEMU_OPT_STRING,
274 .help = "File name of the image",
276 { /* end of list */ }
280 static int raw_open_common(BlockDriverState *bs, QDict *options,
281 int bdrv_flags, int open_flags, Error **errp)
283 BDRVRawState *s = bs->opaque;
284 QemuOpts *opts;
285 Error *local_err = NULL;
286 const char *filename;
287 int fd, ret;
288 struct stat st;
290 opts = qemu_opts_create_nofail(&raw_runtime_opts);
291 qemu_opts_absorb_qdict(opts, options, &local_err);
292 if (error_is_set(&local_err)) {
293 error_propagate(errp, local_err);
294 ret = -EINVAL;
295 goto fail;
298 filename = qemu_opt_get(opts, "filename");
300 ret = raw_normalize_devicepath(&filename);
301 if (ret != 0) {
302 error_setg_errno(errp, -ret, "Could not normalize device path");
303 goto fail;
306 s->open_flags = open_flags;
307 raw_parse_flags(bdrv_flags, &s->open_flags);
309 s->fd = -1;
310 fd = qemu_open(filename, s->open_flags, 0644);
311 if (fd < 0) {
312 ret = -errno;
313 if (ret == -EROFS) {
314 ret = -EACCES;
316 goto fail;
318 s->fd = fd;
320 #ifdef CONFIG_LINUX_AIO
321 if (raw_set_aio(&s->aio_ctx, &s->use_aio, bdrv_flags)) {
322 qemu_close(fd);
323 ret = -errno;
324 error_setg_errno(errp, -ret, "Could not set AIO state");
325 goto fail;
327 #endif
329 s->has_discard = true;
330 s->has_write_zeroes = true;
332 if (fstat(s->fd, &st) < 0) {
333 error_setg_errno(errp, errno, "Could not stat file");
334 goto fail;
336 if (S_ISREG(st.st_mode)) {
337 s->discard_zeroes = true;
339 if (S_ISBLK(st.st_mode)) {
340 #ifdef BLKDISCARDZEROES
341 unsigned int arg;
342 if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) {
343 s->discard_zeroes = true;
345 #endif
346 #ifdef __linux__
347 /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do
348 * not rely on the contents of discarded blocks unless using O_DIRECT.
349 * Same for BLKZEROOUT.
351 if (!(bs->open_flags & BDRV_O_NOCACHE)) {
352 s->discard_zeroes = false;
353 s->has_write_zeroes = false;
355 #endif
358 #ifdef CONFIG_XFS
359 if (platform_test_xfs_fd(s->fd)) {
360 s->is_xfs = true;
362 #endif
364 ret = 0;
365 fail:
366 qemu_opts_del(opts);
367 return ret;
370 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
371 Error **errp)
373 BDRVRawState *s = bs->opaque;
374 Error *local_err = NULL;
375 int ret;
377 s->type = FTYPE_FILE;
378 ret = raw_open_common(bs, options, flags, 0, &local_err);
379 if (error_is_set(&local_err)) {
380 error_propagate(errp, local_err);
382 return ret;
385 static int raw_reopen_prepare(BDRVReopenState *state,
386 BlockReopenQueue *queue, Error **errp)
388 BDRVRawState *s;
389 BDRVRawReopenState *raw_s;
390 int ret = 0;
392 assert(state != NULL);
393 assert(state->bs != NULL);
395 s = state->bs->opaque;
397 state->opaque = g_malloc0(sizeof(BDRVRawReopenState));
398 raw_s = state->opaque;
400 #ifdef CONFIG_LINUX_AIO
401 raw_s->use_aio = s->use_aio;
403 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
404 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
405 * won't override aio_ctx if aio_ctx is non-NULL */
406 if (raw_set_aio(&s->aio_ctx, &raw_s->use_aio, state->flags)) {
407 error_setg(errp, "Could not set AIO state");
408 return -1;
410 #endif
412 if (s->type == FTYPE_FD || s->type == FTYPE_CD) {
413 raw_s->open_flags |= O_NONBLOCK;
416 raw_parse_flags(state->flags, &raw_s->open_flags);
418 raw_s->fd = -1;
420 int fcntl_flags = O_APPEND | O_NONBLOCK;
421 #ifdef O_NOATIME
422 fcntl_flags |= O_NOATIME;
423 #endif
425 #ifdef O_ASYNC
426 /* Not all operating systems have O_ASYNC, and those that don't
427 * will not let us track the state into raw_s->open_flags (typically
428 * you achieve the same effect with an ioctl, for example I_SETSIG
429 * on Solaris). But we do not use O_ASYNC, so that's fine.
431 assert((s->open_flags & O_ASYNC) == 0);
432 #endif
434 if ((raw_s->open_flags & ~fcntl_flags) == (s->open_flags & ~fcntl_flags)) {
435 /* dup the original fd */
436 /* TODO: use qemu fcntl wrapper */
437 #ifdef F_DUPFD_CLOEXEC
438 raw_s->fd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
439 #else
440 raw_s->fd = dup(s->fd);
441 if (raw_s->fd != -1) {
442 qemu_set_cloexec(raw_s->fd);
444 #endif
445 if (raw_s->fd >= 0) {
446 ret = fcntl_setfl(raw_s->fd, raw_s->open_flags);
447 if (ret) {
448 qemu_close(raw_s->fd);
449 raw_s->fd = -1;
454 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
455 if (raw_s->fd == -1) {
456 assert(!(raw_s->open_flags & O_CREAT));
457 raw_s->fd = qemu_open(state->bs->filename, raw_s->open_flags);
458 if (raw_s->fd == -1) {
459 error_setg_errno(errp, errno, "Could not reopen file");
460 ret = -1;
463 return ret;
467 static void raw_reopen_commit(BDRVReopenState *state)
469 BDRVRawReopenState *raw_s = state->opaque;
470 BDRVRawState *s = state->bs->opaque;
472 s->open_flags = raw_s->open_flags;
474 qemu_close(s->fd);
475 s->fd = raw_s->fd;
476 #ifdef CONFIG_LINUX_AIO
477 s->use_aio = raw_s->use_aio;
478 #endif
480 g_free(state->opaque);
481 state->opaque = NULL;
485 static void raw_reopen_abort(BDRVReopenState *state)
487 BDRVRawReopenState *raw_s = state->opaque;
489 /* nothing to do if NULL, we didn't get far enough */
490 if (raw_s == NULL) {
491 return;
494 if (raw_s->fd >= 0) {
495 qemu_close(raw_s->fd);
496 raw_s->fd = -1;
498 g_free(state->opaque);
499 state->opaque = NULL;
503 /* XXX: use host sector size if necessary with:
504 #ifdef DIOCGSECTORSIZE
506 unsigned int sectorsize = 512;
507 if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
508 sectorsize > bufsize)
509 bufsize = sectorsize;
511 #endif
512 #ifdef CONFIG_COCOA
513 uint32_t blockSize = 512;
514 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
515 bufsize = blockSize;
517 #endif
520 static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
522 int ret;
524 ret = ioctl(aiocb->aio_fildes, aiocb->aio_ioctl_cmd, aiocb->aio_ioctl_buf);
525 if (ret == -1) {
526 return -errno;
529 return 0;
532 static ssize_t handle_aiocb_flush(RawPosixAIOData *aiocb)
534 int ret;
536 ret = qemu_fdatasync(aiocb->aio_fildes);
537 if (ret == -1) {
538 return -errno;
540 return 0;
543 #ifdef CONFIG_PREADV
545 static bool preadv_present = true;
547 static ssize_t
548 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
550 return preadv(fd, iov, nr_iov, offset);
553 static ssize_t
554 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
556 return pwritev(fd, iov, nr_iov, offset);
559 #else
561 static bool preadv_present = false;
563 static ssize_t
564 qemu_preadv(int fd, const struct iovec *iov, int nr_iov, off_t offset)
566 return -ENOSYS;
569 static ssize_t
570 qemu_pwritev(int fd, const struct iovec *iov, int nr_iov, off_t offset)
572 return -ENOSYS;
575 #endif
577 static ssize_t handle_aiocb_rw_vector(RawPosixAIOData *aiocb)
579 ssize_t len;
581 do {
582 if (aiocb->aio_type & QEMU_AIO_WRITE)
583 len = qemu_pwritev(aiocb->aio_fildes,
584 aiocb->aio_iov,
585 aiocb->aio_niov,
586 aiocb->aio_offset);
587 else
588 len = qemu_preadv(aiocb->aio_fildes,
589 aiocb->aio_iov,
590 aiocb->aio_niov,
591 aiocb->aio_offset);
592 } while (len == -1 && errno == EINTR);
594 if (len == -1) {
595 return -errno;
597 return len;
601 * Read/writes the data to/from a given linear buffer.
603 * Returns the number of bytes handles or -errno in case of an error. Short
604 * reads are only returned if the end of the file is reached.
606 static ssize_t handle_aiocb_rw_linear(RawPosixAIOData *aiocb, char *buf)
608 ssize_t offset = 0;
609 ssize_t len;
611 while (offset < aiocb->aio_nbytes) {
612 if (aiocb->aio_type & QEMU_AIO_WRITE) {
613 len = pwrite(aiocb->aio_fildes,
614 (const char *)buf + offset,
615 aiocb->aio_nbytes - offset,
616 aiocb->aio_offset + offset);
617 } else {
618 len = pread(aiocb->aio_fildes,
619 buf + offset,
620 aiocb->aio_nbytes - offset,
621 aiocb->aio_offset + offset);
623 if (len == -1 && errno == EINTR) {
624 continue;
625 } else if (len == -1) {
626 offset = -errno;
627 break;
628 } else if (len == 0) {
629 break;
631 offset += len;
634 return offset;
637 static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
639 ssize_t nbytes;
640 char *buf;
642 if (!(aiocb->aio_type & QEMU_AIO_MISALIGNED)) {
644 * If there is just a single buffer, and it is properly aligned
645 * we can just use plain pread/pwrite without any problems.
647 if (aiocb->aio_niov == 1) {
648 return handle_aiocb_rw_linear(aiocb, aiocb->aio_iov->iov_base);
651 * We have more than one iovec, and all are properly aligned.
653 * Try preadv/pwritev first and fall back to linearizing the
654 * buffer if it's not supported.
656 if (preadv_present) {
657 nbytes = handle_aiocb_rw_vector(aiocb);
658 if (nbytes == aiocb->aio_nbytes ||
659 (nbytes < 0 && nbytes != -ENOSYS)) {
660 return nbytes;
662 preadv_present = false;
666 * XXX(hch): short read/write. no easy way to handle the reminder
667 * using these interfaces. For now retry using plain
668 * pread/pwrite?
673 * Ok, we have to do it the hard way, copy all segments into
674 * a single aligned buffer.
676 buf = qemu_blockalign(aiocb->bs, aiocb->aio_nbytes);
677 if (aiocb->aio_type & QEMU_AIO_WRITE) {
678 char *p = buf;
679 int i;
681 for (i = 0; i < aiocb->aio_niov; ++i) {
682 memcpy(p, aiocb->aio_iov[i].iov_base, aiocb->aio_iov[i].iov_len);
683 p += aiocb->aio_iov[i].iov_len;
687 nbytes = handle_aiocb_rw_linear(aiocb, buf);
688 if (!(aiocb->aio_type & QEMU_AIO_WRITE)) {
689 char *p = buf;
690 size_t count = aiocb->aio_nbytes, copy;
691 int i;
693 for (i = 0; i < aiocb->aio_niov && count; ++i) {
694 copy = count;
695 if (copy > aiocb->aio_iov[i].iov_len) {
696 copy = aiocb->aio_iov[i].iov_len;
698 memcpy(aiocb->aio_iov[i].iov_base, p, copy);
699 p += copy;
700 count -= copy;
703 qemu_vfree(buf);
705 return nbytes;
708 #ifdef CONFIG_XFS
709 static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
711 struct xfs_flock64 fl;
713 memset(&fl, 0, sizeof(fl));
714 fl.l_whence = SEEK_SET;
715 fl.l_start = offset;
716 fl.l_len = bytes;
718 if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) {
719 DEBUG_BLOCK_PRINT("cannot write zero range (%s)\n", strerror(errno));
720 return -errno;
723 return 0;
726 static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
728 struct xfs_flock64 fl;
730 memset(&fl, 0, sizeof(fl));
731 fl.l_whence = SEEK_SET;
732 fl.l_start = offset;
733 fl.l_len = bytes;
735 if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
736 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
737 return -errno;
740 return 0;
742 #endif
744 static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
746 int ret = -EOPNOTSUPP;
747 BDRVRawState *s = aiocb->bs->opaque;
749 if (s->has_write_zeroes == 0) {
750 return -ENOTSUP;
753 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
754 #ifdef BLKZEROOUT
755 do {
756 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
757 if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) {
758 return 0;
760 } while (errno == EINTR);
762 ret = -errno;
763 #endif
764 } else {
765 #ifdef CONFIG_XFS
766 if (s->is_xfs) {
767 return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes);
769 #endif
772 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
773 ret == -ENOTTY) {
774 s->has_write_zeroes = false;
775 ret = -ENOTSUP;
777 return ret;
780 static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
782 int ret = -EOPNOTSUPP;
783 BDRVRawState *s = aiocb->bs->opaque;
785 if (!s->has_discard) {
786 return -ENOTSUP;
789 if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
790 #ifdef BLKDISCARD
791 do {
792 uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
793 if (ioctl(aiocb->aio_fildes, BLKDISCARD, range) == 0) {
794 return 0;
796 } while (errno == EINTR);
798 ret = -errno;
799 #endif
800 } else {
801 #ifdef CONFIG_XFS
802 if (s->is_xfs) {
803 return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
805 #endif
807 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
808 do {
809 if (fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
810 aiocb->aio_offset, aiocb->aio_nbytes) == 0) {
811 return 0;
813 } while (errno == EINTR);
815 ret = -errno;
816 #endif
819 if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
820 ret == -ENOTTY) {
821 s->has_discard = false;
822 ret = -ENOTSUP;
824 return ret;
827 static int aio_worker(void *arg)
829 RawPosixAIOData *aiocb = arg;
830 ssize_t ret = 0;
832 switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
833 case QEMU_AIO_READ:
834 ret = handle_aiocb_rw(aiocb);
835 if (ret >= 0 && ret < aiocb->aio_nbytes && aiocb->bs->growable) {
836 iov_memset(aiocb->aio_iov, aiocb->aio_niov, ret,
837 0, aiocb->aio_nbytes - ret);
839 ret = aiocb->aio_nbytes;
841 if (ret == aiocb->aio_nbytes) {
842 ret = 0;
843 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
844 ret = -EINVAL;
846 break;
847 case QEMU_AIO_WRITE:
848 ret = handle_aiocb_rw(aiocb);
849 if (ret == aiocb->aio_nbytes) {
850 ret = 0;
851 } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
852 ret = -EINVAL;
854 break;
855 case QEMU_AIO_FLUSH:
856 ret = handle_aiocb_flush(aiocb);
857 break;
858 case QEMU_AIO_IOCTL:
859 ret = handle_aiocb_ioctl(aiocb);
860 break;
861 case QEMU_AIO_DISCARD:
862 ret = handle_aiocb_discard(aiocb);
863 break;
864 case QEMU_AIO_WRITE_ZEROES:
865 ret = handle_aiocb_write_zeroes(aiocb);
866 break;
867 default:
868 fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
869 ret = -EINVAL;
870 break;
873 g_slice_free(RawPosixAIOData, aiocb);
874 return ret;
877 static int paio_submit_co(BlockDriverState *bs, int fd,
878 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
879 int type)
881 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
882 ThreadPool *pool;
884 acb->bs = bs;
885 acb->aio_type = type;
886 acb->aio_fildes = fd;
888 if (qiov) {
889 acb->aio_iov = qiov->iov;
890 acb->aio_niov = qiov->niov;
892 acb->aio_nbytes = nb_sectors * 512;
893 acb->aio_offset = sector_num * 512;
895 trace_paio_submit_co(sector_num, nb_sectors, type);
896 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
897 return thread_pool_submit_co(pool, aio_worker, acb);
900 static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
901 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
902 BlockDriverCompletionFunc *cb, void *opaque, int type)
904 RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
905 ThreadPool *pool;
907 acb->bs = bs;
908 acb->aio_type = type;
909 acb->aio_fildes = fd;
911 if (qiov) {
912 acb->aio_iov = qiov->iov;
913 acb->aio_niov = qiov->niov;
915 acb->aio_nbytes = nb_sectors * 512;
916 acb->aio_offset = sector_num * 512;
918 trace_paio_submit(acb, opaque, sector_num, nb_sectors, type);
919 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
920 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
923 static BlockDriverAIOCB *raw_aio_submit(BlockDriverState *bs,
924 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
925 BlockDriverCompletionFunc *cb, void *opaque, int type)
927 BDRVRawState *s = bs->opaque;
929 if (fd_open(bs) < 0)
930 return NULL;
933 * If O_DIRECT is used the buffer needs to be aligned on a sector
934 * boundary. Check if this is the case or tell the low-level
935 * driver that it needs to copy the buffer.
937 if ((bs->open_flags & BDRV_O_NOCACHE)) {
938 if (!bdrv_qiov_is_aligned(bs, qiov)) {
939 type |= QEMU_AIO_MISALIGNED;
940 #ifdef CONFIG_LINUX_AIO
941 } else if (s->use_aio) {
942 return laio_submit(bs, s->aio_ctx, s->fd, sector_num, qiov,
943 nb_sectors, cb, opaque, type);
944 #endif
948 return paio_submit(bs, s->fd, sector_num, qiov, nb_sectors,
949 cb, opaque, type);
952 static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
953 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
954 BlockDriverCompletionFunc *cb, void *opaque)
956 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
957 cb, opaque, QEMU_AIO_READ);
960 static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
961 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
962 BlockDriverCompletionFunc *cb, void *opaque)
964 return raw_aio_submit(bs, sector_num, qiov, nb_sectors,
965 cb, opaque, QEMU_AIO_WRITE);
968 static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
969 BlockDriverCompletionFunc *cb, void *opaque)
971 BDRVRawState *s = bs->opaque;
973 if (fd_open(bs) < 0)
974 return NULL;
976 return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
979 static void raw_close(BlockDriverState *bs)
981 BDRVRawState *s = bs->opaque;
982 if (s->fd >= 0) {
983 qemu_close(s->fd);
984 s->fd = -1;
988 static int raw_truncate(BlockDriverState *bs, int64_t offset)
990 BDRVRawState *s = bs->opaque;
991 struct stat st;
993 if (fstat(s->fd, &st)) {
994 return -errno;
997 if (S_ISREG(st.st_mode)) {
998 if (ftruncate(s->fd, offset) < 0) {
999 return -errno;
1001 } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1002 if (offset > raw_getlength(bs)) {
1003 return -EINVAL;
1005 } else {
1006 return -ENOTSUP;
1009 return 0;
1012 #ifdef __OpenBSD__
1013 static int64_t raw_getlength(BlockDriverState *bs)
1015 BDRVRawState *s = bs->opaque;
1016 int fd = s->fd;
1017 struct stat st;
1019 if (fstat(fd, &st))
1020 return -1;
1021 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1022 struct disklabel dl;
1024 if (ioctl(fd, DIOCGDINFO, &dl))
1025 return -1;
1026 return (uint64_t)dl.d_secsize *
1027 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1028 } else
1029 return st.st_size;
1031 #elif defined(__NetBSD__)
1032 static int64_t raw_getlength(BlockDriverState *bs)
1034 BDRVRawState *s = bs->opaque;
1035 int fd = s->fd;
1036 struct stat st;
1038 if (fstat(fd, &st))
1039 return -1;
1040 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
1041 struct dkwedge_info dkw;
1043 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
1044 return dkw.dkw_size * 512;
1045 } else {
1046 struct disklabel dl;
1048 if (ioctl(fd, DIOCGDINFO, &dl))
1049 return -1;
1050 return (uint64_t)dl.d_secsize *
1051 dl.d_partitions[DISKPART(st.st_rdev)].p_size;
1053 } else
1054 return st.st_size;
1056 #elif defined(__sun__)
1057 static int64_t raw_getlength(BlockDriverState *bs)
1059 BDRVRawState *s = bs->opaque;
1060 struct dk_minfo minfo;
1061 int ret;
1063 ret = fd_open(bs);
1064 if (ret < 0) {
1065 return ret;
1069 * Use the DKIOCGMEDIAINFO ioctl to read the size.
1071 ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
1072 if (ret != -1) {
1073 return minfo.dki_lbsize * minfo.dki_capacity;
1077 * There are reports that lseek on some devices fails, but
1078 * irc discussion said that contingency on contingency was overkill.
1080 return lseek(s->fd, 0, SEEK_END);
1082 #elif defined(CONFIG_BSD)
1083 static int64_t raw_getlength(BlockDriverState *bs)
1085 BDRVRawState *s = bs->opaque;
1086 int fd = s->fd;
1087 int64_t size;
1088 struct stat sb;
1089 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1090 int reopened = 0;
1091 #endif
1092 int ret;
1094 ret = fd_open(bs);
1095 if (ret < 0)
1096 return ret;
1098 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1099 again:
1100 #endif
1101 if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
1102 #ifdef DIOCGMEDIASIZE
1103 if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
1104 #elif defined(DIOCGPART)
1106 struct partinfo pi;
1107 if (ioctl(fd, DIOCGPART, &pi) == 0)
1108 size = pi.media_size;
1109 else
1110 size = 0;
1112 if (size == 0)
1113 #endif
1114 #if defined(__APPLE__) && defined(__MACH__)
1115 size = LONG_LONG_MAX;
1116 #else
1117 size = lseek(fd, 0LL, SEEK_END);
1118 #endif
1119 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1120 switch(s->type) {
1121 case FTYPE_CD:
1122 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1123 if (size == 2048LL * (unsigned)-1)
1124 size = 0;
1125 /* XXX no disc? maybe we need to reopen... */
1126 if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
1127 reopened = 1;
1128 goto again;
1131 #endif
1132 } else {
1133 size = lseek(fd, 0, SEEK_END);
1135 return size;
1137 #else
1138 static int64_t raw_getlength(BlockDriverState *bs)
1140 BDRVRawState *s = bs->opaque;
1141 int ret;
1143 ret = fd_open(bs);
1144 if (ret < 0) {
1145 return ret;
1148 return lseek(s->fd, 0, SEEK_END);
1150 #endif
1152 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
1154 struct stat st;
1155 BDRVRawState *s = bs->opaque;
1157 if (fstat(s->fd, &st) < 0) {
1158 return -errno;
1160 return (int64_t)st.st_blocks * 512;
1163 static int raw_create(const char *filename, QEMUOptionParameter *options,
1164 Error **errp)
1166 int fd;
1167 int result = 0;
1168 int64_t total_size = 0;
1170 /* Read out options */
1171 while (options && options->name) {
1172 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1173 total_size = options->value.n / BDRV_SECTOR_SIZE;
1175 options++;
1178 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1179 0644);
1180 if (fd < 0) {
1181 result = -errno;
1182 error_setg_errno(errp, -result, "Could not create file");
1183 } else {
1184 if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
1185 result = -errno;
1186 error_setg_errno(errp, -result, "Could not resize file");
1188 if (qemu_close(fd) != 0) {
1189 result = -errno;
1190 error_setg_errno(errp, -result, "Could not close the new file");
1193 return result;
1197 * Returns true iff the specified sector is present in the disk image. Drivers
1198 * not implementing the functionality are assumed to not support backing files,
1199 * hence all their sectors are reported as allocated.
1201 * If 'sector_num' is beyond the end of the disk image the return value is 0
1202 * and 'pnum' is set to 0.
1204 * 'pnum' is set to the number of sectors (including and immediately following
1205 * the specified sector) that are known to be in the same
1206 * allocated/unallocated state.
1208 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1209 * beyond the end of the disk image it will be clamped.
1211 static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
1212 int64_t sector_num,
1213 int nb_sectors, int *pnum)
1215 off_t start, data, hole;
1216 int64_t ret;
1218 ret = fd_open(bs);
1219 if (ret < 0) {
1220 return ret;
1223 start = sector_num * BDRV_SECTOR_SIZE;
1224 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
1226 #ifdef CONFIG_FIEMAP
1228 BDRVRawState *s = bs->opaque;
1229 struct {
1230 struct fiemap fm;
1231 struct fiemap_extent fe;
1232 } f;
1234 f.fm.fm_start = start;
1235 f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
1236 f.fm.fm_flags = 0;
1237 f.fm.fm_extent_count = 1;
1238 f.fm.fm_reserved = 0;
1239 if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
1240 /* Assume everything is allocated. */
1241 *pnum = nb_sectors;
1242 return ret;
1245 if (f.fm.fm_mapped_extents == 0) {
1246 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1247 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1249 off_t length = lseek(s->fd, 0, SEEK_END);
1250 hole = f.fm.fm_start;
1251 data = MIN(f.fm.fm_start + f.fm.fm_length, length);
1252 } else {
1253 data = f.fe.fe_logical;
1254 hole = f.fe.fe_logical + f.fe.fe_length;
1255 if (f.fe.fe_flags & FIEMAP_EXTENT_UNWRITTEN) {
1256 ret |= BDRV_BLOCK_ZERO;
1260 #elif defined SEEK_HOLE && defined SEEK_DATA
1262 BDRVRawState *s = bs->opaque;
1264 hole = lseek(s->fd, start, SEEK_HOLE);
1265 if (hole == -1) {
1266 /* -ENXIO indicates that sector_num was past the end of the file.
1267 * There is a virtual hole there. */
1268 assert(errno != -ENXIO);
1270 /* Most likely EINVAL. Assume everything is allocated. */
1271 *pnum = nb_sectors;
1272 return ret;
1275 if (hole > start) {
1276 data = start;
1277 } else {
1278 /* On a hole. We need another syscall to find its end. */
1279 data = lseek(s->fd, start, SEEK_DATA);
1280 if (data == -1) {
1281 data = lseek(s->fd, 0, SEEK_END);
1284 #else
1285 data = 0;
1286 hole = start + nb_sectors * BDRV_SECTOR_SIZE;
1287 #endif
1289 if (data <= start) {
1290 /* On a data extent, compute sectors to the end of the extent. */
1291 *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
1292 } else {
1293 /* On a hole, compute sectors to the beginning of the next extent. */
1294 *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
1295 ret &= ~BDRV_BLOCK_DATA;
1296 ret |= BDRV_BLOCK_ZERO;
1299 return ret;
1302 static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
1303 int64_t sector_num, int nb_sectors,
1304 BlockDriverCompletionFunc *cb, void *opaque)
1306 BDRVRawState *s = bs->opaque;
1308 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1309 cb, opaque, QEMU_AIO_DISCARD);
1312 static int coroutine_fn raw_co_write_zeroes(
1313 BlockDriverState *bs, int64_t sector_num,
1314 int nb_sectors, BdrvRequestFlags flags)
1316 BDRVRawState *s = bs->opaque;
1318 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1319 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1320 QEMU_AIO_WRITE_ZEROES);
1321 } else if (s->discard_zeroes) {
1322 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1323 QEMU_AIO_DISCARD);
1325 return -ENOTSUP;
1328 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1330 BDRVRawState *s = bs->opaque;
1332 bdi->unallocated_blocks_are_zero = s->discard_zeroes;
1333 bdi->can_write_zeroes_with_unmap = s->discard_zeroes;
1334 return 0;
1337 static QEMUOptionParameter raw_create_options[] = {
1339 .name = BLOCK_OPT_SIZE,
1340 .type = OPT_SIZE,
1341 .help = "Virtual disk size"
1343 { NULL }
1346 static BlockDriver bdrv_file = {
1347 .format_name = "file",
1348 .protocol_name = "file",
1349 .instance_size = sizeof(BDRVRawState),
1350 .bdrv_needs_filename = true,
1351 .bdrv_probe = NULL, /* no probe for protocols */
1352 .bdrv_file_open = raw_open,
1353 .bdrv_reopen_prepare = raw_reopen_prepare,
1354 .bdrv_reopen_commit = raw_reopen_commit,
1355 .bdrv_reopen_abort = raw_reopen_abort,
1356 .bdrv_close = raw_close,
1357 .bdrv_create = raw_create,
1358 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1359 .bdrv_co_get_block_status = raw_co_get_block_status,
1360 .bdrv_co_write_zeroes = raw_co_write_zeroes,
1362 .bdrv_aio_readv = raw_aio_readv,
1363 .bdrv_aio_writev = raw_aio_writev,
1364 .bdrv_aio_flush = raw_aio_flush,
1365 .bdrv_aio_discard = raw_aio_discard,
1367 .bdrv_truncate = raw_truncate,
1368 .bdrv_getlength = raw_getlength,
1369 .bdrv_get_info = raw_get_info,
1370 .bdrv_get_allocated_file_size
1371 = raw_get_allocated_file_size,
1373 .create_options = raw_create_options,
1376 /***********************************************/
1377 /* host device */
1379 #if defined(__APPLE__) && defined(__MACH__)
1380 static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
1381 static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
1383 kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
1385 kern_return_t kernResult;
1386 mach_port_t masterPort;
1387 CFMutableDictionaryRef classesToMatch;
1389 kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
1390 if ( KERN_SUCCESS != kernResult ) {
1391 printf( "IOMasterPort returned %d\n", kernResult );
1394 classesToMatch = IOServiceMatching( kIOCDMediaClass );
1395 if ( classesToMatch == NULL ) {
1396 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1397 } else {
1398 CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
1400 kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
1401 if ( KERN_SUCCESS != kernResult )
1403 printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
1406 return kernResult;
1409 kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
1411 io_object_t nextMedia;
1412 kern_return_t kernResult = KERN_FAILURE;
1413 *bsdPath = '\0';
1414 nextMedia = IOIteratorNext( mediaIterator );
1415 if ( nextMedia )
1417 CFTypeRef bsdPathAsCFString;
1418 bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
1419 if ( bsdPathAsCFString ) {
1420 size_t devPathLength;
1421 strcpy( bsdPath, _PATH_DEV );
1422 strcat( bsdPath, "r" );
1423 devPathLength = strlen( bsdPath );
1424 if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
1425 kernResult = KERN_SUCCESS;
1427 CFRelease( bsdPathAsCFString );
1429 IOObjectRelease( nextMedia );
1432 return kernResult;
1435 #endif
1437 static int hdev_probe_device(const char *filename)
1439 struct stat st;
1441 /* allow a dedicated CD-ROM driver to match with a higher priority */
1442 if (strstart(filename, "/dev/cdrom", NULL))
1443 return 50;
1445 if (stat(filename, &st) >= 0 &&
1446 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
1447 return 100;
1450 return 0;
1453 static int check_hdev_writable(BDRVRawState *s)
1455 #if defined(BLKROGET)
1456 /* Linux block devices can be configured "read-only" using blockdev(8).
1457 * This is independent of device node permissions and therefore open(2)
1458 * with O_RDWR succeeds. Actual writes fail with EPERM.
1460 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1461 * check for read-only block devices so that Linux block devices behave
1462 * properly.
1464 struct stat st;
1465 int readonly = 0;
1467 if (fstat(s->fd, &st)) {
1468 return -errno;
1471 if (!S_ISBLK(st.st_mode)) {
1472 return 0;
1475 if (ioctl(s->fd, BLKROGET, &readonly) < 0) {
1476 return -errno;
1479 if (readonly) {
1480 return -EACCES;
1482 #endif /* defined(BLKROGET) */
1483 return 0;
1486 static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
1487 Error **errp)
1489 BDRVRawState *s = bs->opaque;
1490 Error *local_err = NULL;
1491 int ret;
1492 const char *filename = qdict_get_str(options, "filename");
1494 #if defined(__APPLE__) && defined(__MACH__)
1495 if (strstart(filename, "/dev/cdrom", NULL)) {
1496 kern_return_t kernResult;
1497 io_iterator_t mediaIterator;
1498 char bsdPath[ MAXPATHLEN ];
1499 int fd;
1501 kernResult = FindEjectableCDMedia( &mediaIterator );
1502 kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
1504 if ( bsdPath[ 0 ] != '\0' ) {
1505 strcat(bsdPath,"s0");
1506 /* some CDs don't have a partition 0 */
1507 fd = qemu_open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
1508 if (fd < 0) {
1509 bsdPath[strlen(bsdPath)-1] = '1';
1510 } else {
1511 qemu_close(fd);
1513 filename = bsdPath;
1514 qdict_put(options, "filename", qstring_from_str(filename));
1517 if ( mediaIterator )
1518 IOObjectRelease( mediaIterator );
1520 #endif
1522 s->type = FTYPE_FILE;
1523 #if defined(__linux__)
1525 char resolved_path[ MAXPATHLEN ], *temp;
1527 temp = realpath(filename, resolved_path);
1528 if (temp && strstart(temp, "/dev/sg", NULL)) {
1529 bs->sg = 1;
1532 #endif
1534 ret = raw_open_common(bs, options, flags, 0, &local_err);
1535 if (ret < 0) {
1536 if (error_is_set(&local_err)) {
1537 error_propagate(errp, local_err);
1539 return ret;
1542 if (flags & BDRV_O_RDWR) {
1543 ret = check_hdev_writable(s);
1544 if (ret < 0) {
1545 raw_close(bs);
1546 error_setg_errno(errp, -ret, "The device is not writable");
1547 return ret;
1551 return ret;
1554 #if defined(__linux__)
1555 /* Note: we do not have a reliable method to detect if the floppy is
1556 present. The current method is to try to open the floppy at every
1557 I/O and to keep it opened during a few hundreds of ms. */
1558 static int fd_open(BlockDriverState *bs)
1560 BDRVRawState *s = bs->opaque;
1561 int last_media_present;
1563 if (s->type != FTYPE_FD)
1564 return 0;
1565 last_media_present = (s->fd >= 0);
1566 if (s->fd >= 0 &&
1567 (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
1568 qemu_close(s->fd);
1569 s->fd = -1;
1570 #ifdef DEBUG_FLOPPY
1571 printf("Floppy closed\n");
1572 #endif
1574 if (s->fd < 0) {
1575 if (s->fd_got_error &&
1576 (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
1577 #ifdef DEBUG_FLOPPY
1578 printf("No floppy (open delayed)\n");
1579 #endif
1580 return -EIO;
1582 s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
1583 if (s->fd < 0) {
1584 s->fd_error_time = get_clock();
1585 s->fd_got_error = 1;
1586 if (last_media_present)
1587 s->fd_media_changed = 1;
1588 #ifdef DEBUG_FLOPPY
1589 printf("No floppy\n");
1590 #endif
1591 return -EIO;
1593 #ifdef DEBUG_FLOPPY
1594 printf("Floppy opened\n");
1595 #endif
1597 if (!last_media_present)
1598 s->fd_media_changed = 1;
1599 s->fd_open_time = get_clock();
1600 s->fd_got_error = 0;
1601 return 0;
1604 static int hdev_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1606 BDRVRawState *s = bs->opaque;
1608 return ioctl(s->fd, req, buf);
1611 static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
1612 unsigned long int req, void *buf,
1613 BlockDriverCompletionFunc *cb, void *opaque)
1615 BDRVRawState *s = bs->opaque;
1616 RawPosixAIOData *acb;
1617 ThreadPool *pool;
1619 if (fd_open(bs) < 0)
1620 return NULL;
1622 acb = g_slice_new(RawPosixAIOData);
1623 acb->bs = bs;
1624 acb->aio_type = QEMU_AIO_IOCTL;
1625 acb->aio_fildes = s->fd;
1626 acb->aio_offset = 0;
1627 acb->aio_ioctl_buf = buf;
1628 acb->aio_ioctl_cmd = req;
1629 pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
1630 return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
1633 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1634 static int fd_open(BlockDriverState *bs)
1636 BDRVRawState *s = bs->opaque;
1638 /* this is just to ensure s->fd is sane (its called by io ops) */
1639 if (s->fd >= 0)
1640 return 0;
1641 return -EIO;
1643 #else /* !linux && !FreeBSD */
1645 static int fd_open(BlockDriverState *bs)
1647 return 0;
1650 #endif /* !linux && !FreeBSD */
1652 static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
1653 int64_t sector_num, int nb_sectors,
1654 BlockDriverCompletionFunc *cb, void *opaque)
1656 BDRVRawState *s = bs->opaque;
1658 if (fd_open(bs) < 0) {
1659 return NULL;
1661 return paio_submit(bs, s->fd, sector_num, NULL, nb_sectors,
1662 cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1665 static coroutine_fn int hdev_co_write_zeroes(BlockDriverState *bs,
1666 int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
1668 BDRVRawState *s = bs->opaque;
1669 int rc;
1671 rc = fd_open(bs);
1672 if (rc < 0) {
1673 return rc;
1675 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1676 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1677 QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
1678 } else if (s->discard_zeroes) {
1679 return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
1680 QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
1682 return -ENOTSUP;
1685 static int hdev_create(const char *filename, QEMUOptionParameter *options,
1686 Error **errp)
1688 int fd;
1689 int ret = 0;
1690 struct stat stat_buf;
1691 int64_t total_size = 0;
1693 /* Read out options */
1694 while (options && options->name) {
1695 if (!strcmp(options->name, "size")) {
1696 total_size = options->value.n / BDRV_SECTOR_SIZE;
1698 options++;
1701 fd = qemu_open(filename, O_WRONLY | O_BINARY);
1702 if (fd < 0) {
1703 ret = -errno;
1704 error_setg_errno(errp, -ret, "Could not open device");
1705 return ret;
1708 if (fstat(fd, &stat_buf) < 0) {
1709 ret = -errno;
1710 error_setg_errno(errp, -ret, "Could not stat device");
1711 } else if (!S_ISBLK(stat_buf.st_mode) && !S_ISCHR(stat_buf.st_mode)) {
1712 error_setg(errp,
1713 "The given file is neither a block nor a character device");
1714 ret = -ENODEV;
1715 } else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) {
1716 error_setg(errp, "Device is too small");
1717 ret = -ENOSPC;
1720 qemu_close(fd);
1721 return ret;
1724 static BlockDriver bdrv_host_device = {
1725 .format_name = "host_device",
1726 .protocol_name = "host_device",
1727 .instance_size = sizeof(BDRVRawState),
1728 .bdrv_needs_filename = true,
1729 .bdrv_probe_device = hdev_probe_device,
1730 .bdrv_file_open = hdev_open,
1731 .bdrv_close = raw_close,
1732 .bdrv_reopen_prepare = raw_reopen_prepare,
1733 .bdrv_reopen_commit = raw_reopen_commit,
1734 .bdrv_reopen_abort = raw_reopen_abort,
1735 .bdrv_create = hdev_create,
1736 .create_options = raw_create_options,
1737 .bdrv_co_write_zeroes = hdev_co_write_zeroes,
1739 .bdrv_aio_readv = raw_aio_readv,
1740 .bdrv_aio_writev = raw_aio_writev,
1741 .bdrv_aio_flush = raw_aio_flush,
1742 .bdrv_aio_discard = hdev_aio_discard,
1744 .bdrv_truncate = raw_truncate,
1745 .bdrv_getlength = raw_getlength,
1746 .bdrv_get_info = raw_get_info,
1747 .bdrv_get_allocated_file_size
1748 = raw_get_allocated_file_size,
1750 /* generic scsi device */
1751 #ifdef __linux__
1752 .bdrv_ioctl = hdev_ioctl,
1753 .bdrv_aio_ioctl = hdev_aio_ioctl,
1754 #endif
1757 #ifdef __linux__
1758 static int floppy_open(BlockDriverState *bs, QDict *options, int flags,
1759 Error **errp)
1761 BDRVRawState *s = bs->opaque;
1762 Error *local_err = NULL;
1763 int ret;
1765 s->type = FTYPE_FD;
1767 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1768 ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
1769 if (ret) {
1770 if (error_is_set(&local_err)) {
1771 error_propagate(errp, local_err);
1773 return ret;
1776 /* close fd so that we can reopen it as needed */
1777 qemu_close(s->fd);
1778 s->fd = -1;
1779 s->fd_media_changed = 1;
1781 return 0;
1784 static int floppy_probe_device(const char *filename)
1786 int fd, ret;
1787 int prio = 0;
1788 struct floppy_struct fdparam;
1789 struct stat st;
1791 if (strstart(filename, "/dev/fd", NULL) &&
1792 !strstart(filename, "/dev/fdset/", NULL)) {
1793 prio = 50;
1796 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1797 if (fd < 0) {
1798 goto out;
1800 ret = fstat(fd, &st);
1801 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1802 goto outc;
1805 /* Attempt to detect via a floppy specific ioctl */
1806 ret = ioctl(fd, FDGETPRM, &fdparam);
1807 if (ret >= 0)
1808 prio = 100;
1810 outc:
1811 qemu_close(fd);
1812 out:
1813 return prio;
1817 static int floppy_is_inserted(BlockDriverState *bs)
1819 return fd_open(bs) >= 0;
1822 static int floppy_media_changed(BlockDriverState *bs)
1824 BDRVRawState *s = bs->opaque;
1825 int ret;
1828 * XXX: we do not have a true media changed indication.
1829 * It does not work if the floppy is changed without trying to read it.
1831 fd_open(bs);
1832 ret = s->fd_media_changed;
1833 s->fd_media_changed = 0;
1834 #ifdef DEBUG_FLOPPY
1835 printf("Floppy changed=%d\n", ret);
1836 #endif
1837 return ret;
1840 static void floppy_eject(BlockDriverState *bs, bool eject_flag)
1842 BDRVRawState *s = bs->opaque;
1843 int fd;
1845 if (s->fd >= 0) {
1846 qemu_close(s->fd);
1847 s->fd = -1;
1849 fd = qemu_open(bs->filename, s->open_flags | O_NONBLOCK);
1850 if (fd >= 0) {
1851 if (ioctl(fd, FDEJECT, 0) < 0)
1852 perror("FDEJECT");
1853 qemu_close(fd);
1857 static BlockDriver bdrv_host_floppy = {
1858 .format_name = "host_floppy",
1859 .protocol_name = "host_floppy",
1860 .instance_size = sizeof(BDRVRawState),
1861 .bdrv_needs_filename = true,
1862 .bdrv_probe_device = floppy_probe_device,
1863 .bdrv_file_open = floppy_open,
1864 .bdrv_close = raw_close,
1865 .bdrv_reopen_prepare = raw_reopen_prepare,
1866 .bdrv_reopen_commit = raw_reopen_commit,
1867 .bdrv_reopen_abort = raw_reopen_abort,
1868 .bdrv_create = hdev_create,
1869 .create_options = raw_create_options,
1871 .bdrv_aio_readv = raw_aio_readv,
1872 .bdrv_aio_writev = raw_aio_writev,
1873 .bdrv_aio_flush = raw_aio_flush,
1875 .bdrv_truncate = raw_truncate,
1876 .bdrv_getlength = raw_getlength,
1877 .has_variable_length = true,
1878 .bdrv_get_allocated_file_size
1879 = raw_get_allocated_file_size,
1881 /* removable device support */
1882 .bdrv_is_inserted = floppy_is_inserted,
1883 .bdrv_media_changed = floppy_media_changed,
1884 .bdrv_eject = floppy_eject,
1887 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
1888 Error **errp)
1890 BDRVRawState *s = bs->opaque;
1891 Error *local_err = NULL;
1892 int ret;
1894 s->type = FTYPE_CD;
1896 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1897 ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
1898 if (error_is_set(&local_err)) {
1899 error_propagate(errp, local_err);
1901 return ret;
1904 static int cdrom_probe_device(const char *filename)
1906 int fd, ret;
1907 int prio = 0;
1908 struct stat st;
1910 fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
1911 if (fd < 0) {
1912 goto out;
1914 ret = fstat(fd, &st);
1915 if (ret == -1 || !S_ISBLK(st.st_mode)) {
1916 goto outc;
1919 /* Attempt to detect via a CDROM specific ioctl */
1920 ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1921 if (ret >= 0)
1922 prio = 100;
1924 outc:
1925 qemu_close(fd);
1926 out:
1927 return prio;
1930 static int cdrom_is_inserted(BlockDriverState *bs)
1932 BDRVRawState *s = bs->opaque;
1933 int ret;
1935 ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
1936 if (ret == CDS_DISC_OK)
1937 return 1;
1938 return 0;
1941 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
1943 BDRVRawState *s = bs->opaque;
1945 if (eject_flag) {
1946 if (ioctl(s->fd, CDROMEJECT, NULL) < 0)
1947 perror("CDROMEJECT");
1948 } else {
1949 if (ioctl(s->fd, CDROMCLOSETRAY, NULL) < 0)
1950 perror("CDROMEJECT");
1954 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
1956 BDRVRawState *s = bs->opaque;
1958 if (ioctl(s->fd, CDROM_LOCKDOOR, locked) < 0) {
1960 * Note: an error can happen if the distribution automatically
1961 * mounts the CD-ROM
1963 /* perror("CDROM_LOCKDOOR"); */
1967 static BlockDriver bdrv_host_cdrom = {
1968 .format_name = "host_cdrom",
1969 .protocol_name = "host_cdrom",
1970 .instance_size = sizeof(BDRVRawState),
1971 .bdrv_needs_filename = true,
1972 .bdrv_probe_device = cdrom_probe_device,
1973 .bdrv_file_open = cdrom_open,
1974 .bdrv_close = raw_close,
1975 .bdrv_reopen_prepare = raw_reopen_prepare,
1976 .bdrv_reopen_commit = raw_reopen_commit,
1977 .bdrv_reopen_abort = raw_reopen_abort,
1978 .bdrv_create = hdev_create,
1979 .create_options = raw_create_options,
1981 .bdrv_aio_readv = raw_aio_readv,
1982 .bdrv_aio_writev = raw_aio_writev,
1983 .bdrv_aio_flush = raw_aio_flush,
1985 .bdrv_truncate = raw_truncate,
1986 .bdrv_getlength = raw_getlength,
1987 .has_variable_length = true,
1988 .bdrv_get_allocated_file_size
1989 = raw_get_allocated_file_size,
1991 /* removable device support */
1992 .bdrv_is_inserted = cdrom_is_inserted,
1993 .bdrv_eject = cdrom_eject,
1994 .bdrv_lock_medium = cdrom_lock_medium,
1996 /* generic scsi device */
1997 .bdrv_ioctl = hdev_ioctl,
1998 .bdrv_aio_ioctl = hdev_aio_ioctl,
2000 #endif /* __linux__ */
2002 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2003 static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
2004 Error **errp)
2006 BDRVRawState *s = bs->opaque;
2007 Error *local_err = NULL;
2008 int ret;
2010 s->type = FTYPE_CD;
2012 ret = raw_open_common(bs, options, flags, 0, &local_err);
2013 if (ret) {
2014 if (error_is_set(&local_err)) {
2015 error_propagate(errp, local_err);
2017 return ret;
2020 /* make sure the door isn't locked at this time */
2021 ioctl(s->fd, CDIOCALLOW);
2022 return 0;
2025 static int cdrom_probe_device(const char *filename)
2027 if (strstart(filename, "/dev/cd", NULL) ||
2028 strstart(filename, "/dev/acd", NULL))
2029 return 100;
2030 return 0;
2033 static int cdrom_reopen(BlockDriverState *bs)
2035 BDRVRawState *s = bs->opaque;
2036 int fd;
2039 * Force reread of possibly changed/newly loaded disc,
2040 * FreeBSD seems to not notice sometimes...
2042 if (s->fd >= 0)
2043 qemu_close(s->fd);
2044 fd = qemu_open(bs->filename, s->open_flags, 0644);
2045 if (fd < 0) {
2046 s->fd = -1;
2047 return -EIO;
2049 s->fd = fd;
2051 /* make sure the door isn't locked at this time */
2052 ioctl(s->fd, CDIOCALLOW);
2053 return 0;
2056 static int cdrom_is_inserted(BlockDriverState *bs)
2058 return raw_getlength(bs) > 0;
2061 static void cdrom_eject(BlockDriverState *bs, bool eject_flag)
2063 BDRVRawState *s = bs->opaque;
2065 if (s->fd < 0)
2066 return;
2068 (void) ioctl(s->fd, CDIOCALLOW);
2070 if (eject_flag) {
2071 if (ioctl(s->fd, CDIOCEJECT) < 0)
2072 perror("CDIOCEJECT");
2073 } else {
2074 if (ioctl(s->fd, CDIOCCLOSE) < 0)
2075 perror("CDIOCCLOSE");
2078 cdrom_reopen(bs);
2081 static void cdrom_lock_medium(BlockDriverState *bs, bool locked)
2083 BDRVRawState *s = bs->opaque;
2085 if (s->fd < 0)
2086 return;
2087 if (ioctl(s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) {
2089 * Note: an error can happen if the distribution automatically
2090 * mounts the CD-ROM
2092 /* perror("CDROM_LOCKDOOR"); */
2096 static BlockDriver bdrv_host_cdrom = {
2097 .format_name = "host_cdrom",
2098 .protocol_name = "host_cdrom",
2099 .instance_size = sizeof(BDRVRawState),
2100 .bdrv_needs_filename = true,
2101 .bdrv_probe_device = cdrom_probe_device,
2102 .bdrv_file_open = cdrom_open,
2103 .bdrv_close = raw_close,
2104 .bdrv_reopen_prepare = raw_reopen_prepare,
2105 .bdrv_reopen_commit = raw_reopen_commit,
2106 .bdrv_reopen_abort = raw_reopen_abort,
2107 .bdrv_create = hdev_create,
2108 .create_options = raw_create_options,
2110 .bdrv_aio_readv = raw_aio_readv,
2111 .bdrv_aio_writev = raw_aio_writev,
2112 .bdrv_aio_flush = raw_aio_flush,
2114 .bdrv_truncate = raw_truncate,
2115 .bdrv_getlength = raw_getlength,
2116 .has_variable_length = true,
2117 .bdrv_get_allocated_file_size
2118 = raw_get_allocated_file_size,
2120 /* removable device support */
2121 .bdrv_is_inserted = cdrom_is_inserted,
2122 .bdrv_eject = cdrom_eject,
2123 .bdrv_lock_medium = cdrom_lock_medium,
2125 #endif /* __FreeBSD__ */
2127 #ifdef CONFIG_LINUX_AIO
2129 * Return the file descriptor for Linux AIO
2131 * This function is a layering violation and should be removed when it becomes
2132 * possible to call the block layer outside the global mutex. It allows the
2133 * caller to hijack the file descriptor so I/O can be performed outside the
2134 * block layer.
2136 int raw_get_aio_fd(BlockDriverState *bs)
2138 BDRVRawState *s;
2140 if (!bs->drv) {
2141 return -ENOMEDIUM;
2144 if (bs->drv == bdrv_find_format("raw")) {
2145 bs = bs->file;
2148 /* raw-posix has several protocols so just check for raw_aio_readv */
2149 if (bs->drv->bdrv_aio_readv != raw_aio_readv) {
2150 return -ENOTSUP;
2153 s = bs->opaque;
2154 if (!s->use_aio) {
2155 return -ENOTSUP;
2157 return s->fd;
2159 #endif /* CONFIG_LINUX_AIO */
2161 static void bdrv_file_init(void)
2164 * Register all the drivers. Note that order is important, the driver
2165 * registered last will get probed first.
2167 bdrv_register(&bdrv_file);
2168 bdrv_register(&bdrv_host_device);
2169 #ifdef __linux__
2170 bdrv_register(&bdrv_host_floppy);
2171 bdrv_register(&bdrv_host_cdrom);
2172 #endif
2173 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2174 bdrv_register(&bdrv_host_cdrom);
2175 #endif
2178 block_init(bdrv_file_init);