2 * Block driver for RAW files (posix)
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "qemu/timer.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
30 #include "block/thread-pool.h"
33 #include "qapi/util.h"
35 #if defined(__APPLE__) && (__MACH__)
37 #include <sys/param.h>
38 #include <IOKit/IOKitLib.h>
39 #include <IOKit/IOBSD.h>
40 #include <IOKit/storage/IOMediaBSDClient.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <IOKit/storage/IOCDMedia.h>
43 //#include <IOKit/storage/IOCDTypes.h>
44 #include <CoreFoundation/CoreFoundation.h>
48 #define _POSIX_PTHREAD_SEMANTICS 1
52 #include <sys/types.h>
54 #include <sys/ioctl.h>
55 #include <sys/param.h>
56 #include <linux/cdrom.h>
60 #define FS_NOCOW_FL 0x00800000 /* Do not cow file */
64 #include <linux/fiemap.h>
66 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
67 #include <linux/falloc.h>
69 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
75 #include <sys/ioctl.h>
76 #include <sys/disklabel.h>
81 #include <sys/ioctl.h>
82 #include <sys/disklabel.h>
88 #include <sys/ioctl.h>
89 #include <sys/diskslice.h>
96 //#define DEBUG_FLOPPY
99 #if defined(DEBUG_BLOCK)
100 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
101 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
103 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
106 /* OS X does not have O_DSYNC */
109 #define O_DSYNC O_SYNC
110 #elif defined(O_FSYNC)
111 #define O_DSYNC O_FSYNC
115 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
117 #define O_DIRECT O_DSYNC
124 /* if the FD is not accessed during that time (in ns), we try to
125 reopen it to see if the disk has been changed */
126 #define FD_OPEN_TIMEOUT (1000000000)
128 #define MAX_BLOCKSIZE 4096
130 typedef struct BDRVRawState
{
136 #if defined(__linux__)
137 /* linux floppy specific */
138 int64_t fd_open_time
;
139 int64_t fd_error_time
;
141 int fd_media_changed
;
143 #ifdef CONFIG_LINUX_AIO
151 bool has_write_zeroes
:1;
152 bool discard_zeroes
:1;
153 bool needs_alignment
;
159 typedef struct BDRVRawReopenState
{
162 #ifdef CONFIG_LINUX_AIO
165 } BDRVRawReopenState
;
167 static int fd_open(BlockDriverState
*bs
);
168 static int64_t raw_getlength(BlockDriverState
*bs
);
170 typedef struct RawPosixAIOData
{
171 BlockDriverState
*bs
;
174 struct iovec
*aio_iov
;
179 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
184 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
185 static int cdrom_reopen(BlockDriverState
*bs
);
188 #if defined(__NetBSD__)
189 static int raw_normalize_devicepath(const char **filename
)
191 static char namebuf
[PATH_MAX
];
192 const char *dp
, *fname
;
196 dp
= strrchr(fname
, '/');
197 if (lstat(fname
, &sb
) < 0) {
198 fprintf(stderr
, "%s: stat failed: %s\n",
199 fname
, strerror(errno
));
203 if (!S_ISBLK(sb
.st_mode
)) {
208 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
210 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
211 (int)(dp
- fname
), fname
, dp
+ 1);
213 fprintf(stderr
, "%s is a block device", fname
);
215 fprintf(stderr
, ", using %s\n", *filename
);
220 static int raw_normalize_devicepath(const char **filename
)
226 static void raw_probe_alignment(BlockDriverState
*bs
, int fd
, Error
**errp
)
228 BDRVRawState
*s
= bs
->opaque
;
230 unsigned int sector_size
;
232 /* For /dev/sg devices the alignment is not really used.
233 With buffered I/O, we don't have any restrictions. */
234 if (bs
->sg
|| !s
->needs_alignment
) {
235 bs
->request_alignment
= 1;
240 /* Try a few ioctls to get the right size */
241 bs
->request_alignment
= 0;
245 if (ioctl(fd
, BLKSSZGET
, §or_size
) >= 0) {
246 bs
->request_alignment
= sector_size
;
249 #ifdef DKIOCGETBLOCKSIZE
250 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, §or_size
) >= 0) {
251 bs
->request_alignment
= sector_size
;
254 #ifdef DIOCGSECTORSIZE
255 if (ioctl(fd
, DIOCGSECTORSIZE
, §or_size
) >= 0) {
256 bs
->request_alignment
= sector_size
;
262 if (xfsctl(NULL
, fd
, XFS_IOC_DIOINFO
, &da
) >= 0) {
263 bs
->request_alignment
= da
.d_miniosz
;
264 /* The kernel returns wrong information for d_mem */
265 /* s->buf_align = da.d_mem; */
270 /* If we could not get the sizes so far, we can only guess them */
273 buf
= qemu_memalign(MAX_BLOCKSIZE
, 2 * MAX_BLOCKSIZE
);
274 for (align
= 512; align
<= MAX_BLOCKSIZE
; align
<<= 1) {
275 if (pread(fd
, buf
+ align
, MAX_BLOCKSIZE
, 0) >= 0) {
276 s
->buf_align
= align
;
283 if (!bs
->request_alignment
) {
285 buf
= qemu_memalign(s
->buf_align
, MAX_BLOCKSIZE
);
286 for (align
= 512; align
<= MAX_BLOCKSIZE
; align
<<= 1) {
287 if (pread(fd
, buf
, align
, 0) >= 0) {
288 bs
->request_alignment
= align
;
295 if (!s
->buf_align
|| !bs
->request_alignment
) {
296 error_setg(errp
, "Could not find working O_DIRECT alignment. "
297 "Try cache.direct=off.");
301 static void raw_parse_flags(int bdrv_flags
, int *open_flags
)
303 assert(open_flags
!= NULL
);
305 *open_flags
|= O_BINARY
;
306 *open_flags
&= ~O_ACCMODE
;
307 if (bdrv_flags
& BDRV_O_RDWR
) {
308 *open_flags
|= O_RDWR
;
310 *open_flags
|= O_RDONLY
;
313 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
314 * and O_DIRECT for no caching. */
315 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
316 *open_flags
|= O_DIRECT
;
320 static void raw_detach_aio_context(BlockDriverState
*bs
)
322 #ifdef CONFIG_LINUX_AIO
323 BDRVRawState
*s
= bs
->opaque
;
326 laio_detach_aio_context(s
->aio_ctx
, bdrv_get_aio_context(bs
));
331 static void raw_attach_aio_context(BlockDriverState
*bs
,
332 AioContext
*new_context
)
334 #ifdef CONFIG_LINUX_AIO
335 BDRVRawState
*s
= bs
->opaque
;
338 laio_attach_aio_context(s
->aio_ctx
, new_context
);
343 #ifdef CONFIG_LINUX_AIO
344 static int raw_set_aio(void **aio_ctx
, int *use_aio
, int bdrv_flags
)
347 assert(aio_ctx
!= NULL
);
348 assert(use_aio
!= NULL
);
350 * Currently Linux do AIO only for files opened with O_DIRECT
351 * specified so check NOCACHE flag too
353 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
354 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
356 /* if non-NULL, laio_init() has already been run */
357 if (*aio_ctx
== NULL
) {
358 *aio_ctx
= laio_init();
375 static void raw_parse_filename(const char *filename
, QDict
*options
,
378 /* The filename does not have to be prefixed by the protocol name, since
379 * "file" is the default protocol; therefore, the return value of this
380 * function call can be ignored. */
381 strstart(filename
, "file:", &filename
);
383 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
386 static QemuOptsList raw_runtime_opts
= {
388 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
392 .type
= QEMU_OPT_STRING
,
393 .help
= "File name of the image",
395 { /* end of list */ }
399 static int raw_open_common(BlockDriverState
*bs
, QDict
*options
,
400 int bdrv_flags
, int open_flags
, Error
**errp
)
402 BDRVRawState
*s
= bs
->opaque
;
404 Error
*local_err
= NULL
;
405 const char *filename
= NULL
;
409 opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0, &error_abort
);
410 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
412 error_propagate(errp
, local_err
);
417 filename
= qemu_opt_get(opts
, "filename");
419 ret
= raw_normalize_devicepath(&filename
);
421 error_setg_errno(errp
, -ret
, "Could not normalize device path");
425 s
->open_flags
= open_flags
;
426 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
429 fd
= qemu_open(filename
, s
->open_flags
, 0644);
439 #ifdef CONFIG_LINUX_AIO
440 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
443 error_setg_errno(errp
, -ret
, "Could not set AIO state");
448 s
->has_discard
= true;
449 s
->has_write_zeroes
= true;
450 if ((bs
->open_flags
& BDRV_O_NOCACHE
) != 0) {
451 s
->needs_alignment
= true;
454 if (fstat(s
->fd
, &st
) < 0) {
455 error_setg_errno(errp
, errno
, "Could not stat file");
458 if (S_ISREG(st
.st_mode
)) {
459 s
->discard_zeroes
= true;
461 if (S_ISBLK(st
.st_mode
)) {
462 #ifdef BLKDISCARDZEROES
464 if (ioctl(s
->fd
, BLKDISCARDZEROES
, &arg
) == 0 && arg
) {
465 s
->discard_zeroes
= true;
469 /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do
470 * not rely on the contents of discarded blocks unless using O_DIRECT.
471 * Same for BLKZEROOUT.
473 if (!(bs
->open_flags
& BDRV_O_NOCACHE
)) {
474 s
->discard_zeroes
= false;
475 s
->has_write_zeroes
= false;
480 if (S_ISCHR(st
.st_mode
)) {
482 * The file is a char device (disk), which on FreeBSD isn't behind
483 * a pager, so force all requests to be aligned. This is needed
484 * so QEMU makes sure all IO operations on the device are aligned
485 * to sector size, or else FreeBSD will reject them with EINVAL.
487 s
->needs_alignment
= true;
492 if (platform_test_xfs_fd(s
->fd
)) {
497 raw_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
501 if (filename
&& (bdrv_flags
& BDRV_O_TEMPORARY
)) {
508 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
511 BDRVRawState
*s
= bs
->opaque
;
512 Error
*local_err
= NULL
;
515 s
->type
= FTYPE_FILE
;
516 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
518 error_propagate(errp
, local_err
);
523 static int raw_reopen_prepare(BDRVReopenState
*state
,
524 BlockReopenQueue
*queue
, Error
**errp
)
527 BDRVRawReopenState
*raw_s
;
529 Error
*local_err
= NULL
;
531 assert(state
!= NULL
);
532 assert(state
->bs
!= NULL
);
534 s
= state
->bs
->opaque
;
536 state
->opaque
= g_new0(BDRVRawReopenState
, 1);
537 raw_s
= state
->opaque
;
539 #ifdef CONFIG_LINUX_AIO
540 raw_s
->use_aio
= s
->use_aio
;
542 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
543 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
544 * won't override aio_ctx if aio_ctx is non-NULL */
545 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
546 error_setg(errp
, "Could not set AIO state");
551 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
552 raw_s
->open_flags
|= O_NONBLOCK
;
555 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
559 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
561 fcntl_flags
|= O_NOATIME
;
565 /* Not all operating systems have O_ASYNC, and those that don't
566 * will not let us track the state into raw_s->open_flags (typically
567 * you achieve the same effect with an ioctl, for example I_SETSIG
568 * on Solaris). But we do not use O_ASYNC, so that's fine.
570 assert((s
->open_flags
& O_ASYNC
) == 0);
573 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
574 /* dup the original fd */
575 /* TODO: use qemu fcntl wrapper */
576 #ifdef F_DUPFD_CLOEXEC
577 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
579 raw_s
->fd
= dup(s
->fd
);
580 if (raw_s
->fd
!= -1) {
581 qemu_set_cloexec(raw_s
->fd
);
584 if (raw_s
->fd
>= 0) {
585 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
587 qemu_close(raw_s
->fd
);
593 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
594 if (raw_s
->fd
== -1) {
595 assert(!(raw_s
->open_flags
& O_CREAT
));
596 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
597 if (raw_s
->fd
== -1) {
598 error_setg_errno(errp
, errno
, "Could not reopen file");
603 /* Fail already reopen_prepare() if we can't get a working O_DIRECT
604 * alignment with the new fd. */
605 if (raw_s
->fd
!= -1) {
606 raw_probe_alignment(state
->bs
, raw_s
->fd
, &local_err
);
608 qemu_close(raw_s
->fd
);
610 error_propagate(errp
, local_err
);
618 static void raw_reopen_commit(BDRVReopenState
*state
)
620 BDRVRawReopenState
*raw_s
= state
->opaque
;
621 BDRVRawState
*s
= state
->bs
->opaque
;
623 s
->open_flags
= raw_s
->open_flags
;
627 #ifdef CONFIG_LINUX_AIO
628 s
->use_aio
= raw_s
->use_aio
;
631 g_free(state
->opaque
);
632 state
->opaque
= NULL
;
636 static void raw_reopen_abort(BDRVReopenState
*state
)
638 BDRVRawReopenState
*raw_s
= state
->opaque
;
640 /* nothing to do if NULL, we didn't get far enough */
645 if (raw_s
->fd
>= 0) {
646 qemu_close(raw_s
->fd
);
649 g_free(state
->opaque
);
650 state
->opaque
= NULL
;
653 static void raw_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
655 BDRVRawState
*s
= bs
->opaque
;
657 raw_probe_alignment(bs
, s
->fd
, errp
);
658 bs
->bl
.opt_mem_alignment
= s
->buf_align
;
661 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
665 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
673 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
677 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
686 static bool preadv_present
= true;
689 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
691 return preadv(fd
, iov
, nr_iov
, offset
);
695 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
697 return pwritev(fd
, iov
, nr_iov
, offset
);
702 static bool preadv_present
= false;
705 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
711 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
718 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
723 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
724 len
= qemu_pwritev(aiocb
->aio_fildes
,
729 len
= qemu_preadv(aiocb
->aio_fildes
,
733 } while (len
== -1 && errno
== EINTR
);
742 * Read/writes the data to/from a given linear buffer.
744 * Returns the number of bytes handles or -errno in case of an error. Short
745 * reads are only returned if the end of the file is reached.
747 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
752 while (offset
< aiocb
->aio_nbytes
) {
753 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
754 len
= pwrite(aiocb
->aio_fildes
,
755 (const char *)buf
+ offset
,
756 aiocb
->aio_nbytes
- offset
,
757 aiocb
->aio_offset
+ offset
);
759 len
= pread(aiocb
->aio_fildes
,
761 aiocb
->aio_nbytes
- offset
,
762 aiocb
->aio_offset
+ offset
);
764 if (len
== -1 && errno
== EINTR
) {
766 } else if (len
== -1 && errno
== EINVAL
&&
767 (aiocb
->bs
->open_flags
& BDRV_O_NOCACHE
) &&
768 !(aiocb
->aio_type
& QEMU_AIO_WRITE
) &&
770 /* O_DIRECT pread() may fail with EINVAL when offset is unaligned
771 * after a short read. Assume that O_DIRECT short reads only occur
772 * at EOF. Therefore this is a short read, not an I/O error.
775 } else if (len
== -1) {
778 } else if (len
== 0) {
787 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
792 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
794 * If there is just a single buffer, and it is properly aligned
795 * we can just use plain pread/pwrite without any problems.
797 if (aiocb
->aio_niov
== 1) {
798 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
801 * We have more than one iovec, and all are properly aligned.
803 * Try preadv/pwritev first and fall back to linearizing the
804 * buffer if it's not supported.
806 if (preadv_present
) {
807 nbytes
= handle_aiocb_rw_vector(aiocb
);
808 if (nbytes
== aiocb
->aio_nbytes
||
809 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
812 preadv_present
= false;
816 * XXX(hch): short read/write. no easy way to handle the reminder
817 * using these interfaces. For now retry using plain
823 * Ok, we have to do it the hard way, copy all segments into
824 * a single aligned buffer.
826 buf
= qemu_try_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
831 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
835 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
836 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
837 p
+= aiocb
->aio_iov
[i
].iov_len
;
839 assert(p
- buf
== aiocb
->aio_nbytes
);
842 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
843 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
845 size_t count
= aiocb
->aio_nbytes
, copy
;
848 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
850 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
851 copy
= aiocb
->aio_iov
[i
].iov_len
;
853 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
854 assert(count
>= copy
);
866 static int xfs_write_zeroes(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
868 struct xfs_flock64 fl
;
870 memset(&fl
, 0, sizeof(fl
));
871 fl
.l_whence
= SEEK_SET
;
875 if (xfsctl(NULL
, s
->fd
, XFS_IOC_ZERO_RANGE
, &fl
) < 0) {
876 DEBUG_BLOCK_PRINT("cannot write zero range (%s)\n", strerror(errno
));
883 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
885 struct xfs_flock64 fl
;
887 memset(&fl
, 0, sizeof(fl
));
888 fl
.l_whence
= SEEK_SET
;
892 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
893 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
901 static ssize_t
handle_aiocb_write_zeroes(RawPosixAIOData
*aiocb
)
903 int ret
= -EOPNOTSUPP
;
904 BDRVRawState
*s
= aiocb
->bs
->opaque
;
906 if (s
->has_write_zeroes
== 0) {
910 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
913 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
914 if (ioctl(aiocb
->aio_fildes
, BLKZEROOUT
, range
) == 0) {
917 } while (errno
== EINTR
);
924 return xfs_write_zeroes(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
929 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
931 s
->has_write_zeroes
= false;
937 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
939 int ret
= -EOPNOTSUPP
;
940 BDRVRawState
*s
= aiocb
->bs
->opaque
;
942 if (!s
->has_discard
) {
946 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
949 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
950 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
953 } while (errno
== EINTR
);
960 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
964 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
966 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
967 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
970 } while (errno
== EINTR
);
976 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
978 s
->has_discard
= false;
984 static int aio_worker(void *arg
)
986 RawPosixAIOData
*aiocb
= arg
;
989 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
991 ret
= handle_aiocb_rw(aiocb
);
992 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
993 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
994 0, aiocb
->aio_nbytes
- ret
);
996 ret
= aiocb
->aio_nbytes
;
998 if (ret
== aiocb
->aio_nbytes
) {
1000 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
1004 case QEMU_AIO_WRITE
:
1005 ret
= handle_aiocb_rw(aiocb
);
1006 if (ret
== aiocb
->aio_nbytes
) {
1008 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
1012 case QEMU_AIO_FLUSH
:
1013 ret
= handle_aiocb_flush(aiocb
);
1015 case QEMU_AIO_IOCTL
:
1016 ret
= handle_aiocb_ioctl(aiocb
);
1018 case QEMU_AIO_DISCARD
:
1019 ret
= handle_aiocb_discard(aiocb
);
1021 case QEMU_AIO_WRITE_ZEROES
:
1022 ret
= handle_aiocb_write_zeroes(aiocb
);
1025 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
1030 g_slice_free(RawPosixAIOData
, aiocb
);
1034 static int paio_submit_co(BlockDriverState
*bs
, int fd
,
1035 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1038 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
1042 acb
->aio_type
= type
;
1043 acb
->aio_fildes
= fd
;
1045 acb
->aio_nbytes
= nb_sectors
* BDRV_SECTOR_SIZE
;
1046 acb
->aio_offset
= sector_num
* BDRV_SECTOR_SIZE
;
1049 acb
->aio_iov
= qiov
->iov
;
1050 acb
->aio_niov
= qiov
->niov
;
1051 assert(qiov
->size
== acb
->aio_nbytes
);
1054 trace_paio_submit_co(sector_num
, nb_sectors
, type
);
1055 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1056 return thread_pool_submit_co(pool
, aio_worker
, acb
);
1059 static BlockAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
1060 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1061 BlockCompletionFunc
*cb
, void *opaque
, int type
)
1063 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
1067 acb
->aio_type
= type
;
1068 acb
->aio_fildes
= fd
;
1070 acb
->aio_nbytes
= nb_sectors
* BDRV_SECTOR_SIZE
;
1071 acb
->aio_offset
= sector_num
* BDRV_SECTOR_SIZE
;
1074 acb
->aio_iov
= qiov
->iov
;
1075 acb
->aio_niov
= qiov
->niov
;
1076 assert(qiov
->size
== acb
->aio_nbytes
);
1079 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
1080 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1081 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1084 static BlockAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
1085 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1086 BlockCompletionFunc
*cb
, void *opaque
, int type
)
1088 BDRVRawState
*s
= bs
->opaque
;
1090 if (fd_open(bs
) < 0)
1094 * Check if the underlying device requires requests to be aligned,
1095 * and if the request we are trying to submit is aligned or not.
1096 * If this is the case tell the low-level driver that it needs
1097 * to copy the buffer.
1099 if (s
->needs_alignment
) {
1100 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
1101 type
|= QEMU_AIO_MISALIGNED
;
1102 #ifdef CONFIG_LINUX_AIO
1103 } else if (s
->use_aio
) {
1104 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
1105 nb_sectors
, cb
, opaque
, type
);
1110 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
1114 static void raw_aio_plug(BlockDriverState
*bs
)
1116 #ifdef CONFIG_LINUX_AIO
1117 BDRVRawState
*s
= bs
->opaque
;
1119 laio_io_plug(bs
, s
->aio_ctx
);
1124 static void raw_aio_unplug(BlockDriverState
*bs
)
1126 #ifdef CONFIG_LINUX_AIO
1127 BDRVRawState
*s
= bs
->opaque
;
1129 laio_io_unplug(bs
, s
->aio_ctx
, true);
1134 static void raw_aio_flush_io_queue(BlockDriverState
*bs
)
1136 #ifdef CONFIG_LINUX_AIO
1137 BDRVRawState
*s
= bs
->opaque
;
1139 laio_io_unplug(bs
, s
->aio_ctx
, false);
1144 static BlockAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
1145 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1146 BlockCompletionFunc
*cb
, void *opaque
)
1148 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
1149 cb
, opaque
, QEMU_AIO_READ
);
1152 static BlockAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
1153 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1154 BlockCompletionFunc
*cb
, void *opaque
)
1156 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
1157 cb
, opaque
, QEMU_AIO_WRITE
);
1160 static BlockAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
1161 BlockCompletionFunc
*cb
, void *opaque
)
1163 BDRVRawState
*s
= bs
->opaque
;
1165 if (fd_open(bs
) < 0)
1168 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
1171 static void raw_close(BlockDriverState
*bs
)
1173 BDRVRawState
*s
= bs
->opaque
;
1175 raw_detach_aio_context(bs
);
1177 #ifdef CONFIG_LINUX_AIO
1179 laio_cleanup(s
->aio_ctx
);
1188 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
1190 BDRVRawState
*s
= bs
->opaque
;
1193 if (fstat(s
->fd
, &st
)) {
1197 if (S_ISREG(st
.st_mode
)) {
1198 if (ftruncate(s
->fd
, offset
) < 0) {
1201 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1202 if (offset
> raw_getlength(bs
)) {
1213 static int64_t raw_getlength(BlockDriverState
*bs
)
1215 BDRVRawState
*s
= bs
->opaque
;
1221 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1222 struct disklabel dl
;
1224 if (ioctl(fd
, DIOCGDINFO
, &dl
))
1226 return (uint64_t)dl
.d_secsize
*
1227 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
1231 #elif defined(__NetBSD__)
1232 static int64_t raw_getlength(BlockDriverState
*bs
)
1234 BDRVRawState
*s
= bs
->opaque
;
1240 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1241 struct dkwedge_info dkw
;
1243 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
1244 return dkw
.dkw_size
* 512;
1246 struct disklabel dl
;
1248 if (ioctl(fd
, DIOCGDINFO
, &dl
))
1250 return (uint64_t)dl
.d_secsize
*
1251 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
1256 #elif defined(__sun__)
1257 static int64_t raw_getlength(BlockDriverState
*bs
)
1259 BDRVRawState
*s
= bs
->opaque
;
1260 struct dk_minfo minfo
;
1270 * Use the DKIOCGMEDIAINFO ioctl to read the size.
1272 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
1274 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
1278 * There are reports that lseek on some devices fails, but
1279 * irc discussion said that contingency on contingency was overkill.
1281 size
= lseek(s
->fd
, 0, SEEK_END
);
1287 #elif defined(CONFIG_BSD)
1288 static int64_t raw_getlength(BlockDriverState
*bs
)
1290 BDRVRawState
*s
= bs
->opaque
;
1294 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1303 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1306 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
1307 #ifdef DIOCGMEDIASIZE
1308 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
1309 #elif defined(DIOCGPART)
1312 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
1313 size
= pi
.media_size
;
1319 #if defined(__APPLE__) && defined(__MACH__)
1322 size
= lseek(fd
, 0LL, SEEK_END
);
1327 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1330 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1331 if (size
== 2048LL * (unsigned)-1)
1333 /* XXX no disc? maybe we need to reopen... */
1334 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
1341 size
= lseek(fd
, 0, SEEK_END
);
1349 static int64_t raw_getlength(BlockDriverState
*bs
)
1351 BDRVRawState
*s
= bs
->opaque
;
1360 size
= lseek(s
->fd
, 0, SEEK_END
);
1368 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1371 BDRVRawState
*s
= bs
->opaque
;
1373 if (fstat(s
->fd
, &st
) < 0) {
1376 return (int64_t)st
.st_blocks
* 512;
1379 static int raw_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1383 int64_t total_size
= 0;
1385 PreallocMode prealloc
;
1387 Error
*local_err
= NULL
;
1389 strstart(filename
, "file:", &filename
);
1391 /* Read out options */
1392 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1394 nocow
= qemu_opt_get_bool(opts
, BLOCK_OPT_NOCOW
, false);
1395 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
1396 prealloc
= qapi_enum_parse(PreallocMode_lookup
, buf
,
1397 PREALLOC_MODE_MAX
, PREALLOC_MODE_OFF
,
1401 error_propagate(errp
, local_err
);
1406 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1410 error_setg_errno(errp
, -result
, "Could not create file");
1416 /* Set NOCOW flag to solve performance issue on fs like btrfs.
1417 * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
1418 * will be ignored since any failure of this operation should not
1419 * block the left work.
1422 if (ioctl(fd
, FS_IOC_GETFLAGS
, &attr
) == 0) {
1423 attr
|= FS_NOCOW_FL
;
1424 ioctl(fd
, FS_IOC_SETFLAGS
, &attr
);
1429 if (ftruncate(fd
, total_size
) != 0) {
1431 error_setg_errno(errp
, -result
, "Could not resize file");
1436 #ifdef CONFIG_POSIX_FALLOCATE
1437 case PREALLOC_MODE_FALLOC
:
1438 /* posix_fallocate() doesn't set errno. */
1439 result
= -posix_fallocate(fd
, 0, total_size
);
1441 error_setg_errno(errp
, -result
,
1442 "Could not preallocate data for the new file");
1446 case PREALLOC_MODE_FULL
:
1448 int64_t num
= 0, left
= total_size
;
1449 buf
= g_malloc0(65536);
1452 num
= MIN(left
, 65536);
1453 result
= write(fd
, buf
, num
);
1456 error_setg_errno(errp
, -result
,
1457 "Could not write to the new file");
1466 case PREALLOC_MODE_OFF
:
1470 error_setg(errp
, "Unsupported preallocation mode: %s",
1471 PreallocMode_lookup
[prealloc
]);
1476 if (qemu_close(fd
) != 0 && result
== 0) {
1478 error_setg_errno(errp
, -result
, "Could not close the new file");
1484 static int64_t try_fiemap(BlockDriverState
*bs
, off_t start
, off_t
*data
,
1485 off_t
*hole
, int nb_sectors
, int *pnum
)
1487 #ifdef CONFIG_FIEMAP
1488 BDRVRawState
*s
= bs
->opaque
;
1489 int64_t ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
1492 struct fiemap_extent fe
;
1495 if (s
->skip_fiemap
) {
1499 f
.fm
.fm_start
= start
;
1500 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1501 f
.fm
.fm_flags
= FIEMAP_FLAG_SYNC
;
1502 f
.fm
.fm_extent_count
= 1;
1503 f
.fm
.fm_reserved
= 0;
1504 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1505 s
->skip_fiemap
= true;
1509 if (f
.fm
.fm_mapped_extents
== 0) {
1510 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1511 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1513 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1514 *hole
= f
.fm
.fm_start
;
1515 *data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1517 *data
= f
.fe
.fe_logical
;
1518 *hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1519 if (f
.fe
.fe_flags
& FIEMAP_EXTENT_UNWRITTEN
) {
1520 ret
|= BDRV_BLOCK_ZERO
;
1530 static int64_t try_seek_hole(BlockDriverState
*bs
, off_t start
, off_t
*data
,
1531 off_t
*hole
, int *pnum
)
1533 #if defined SEEK_HOLE && defined SEEK_DATA
1534 BDRVRawState
*s
= bs
->opaque
;
1536 *hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1538 /* -ENXIO indicates that sector_num was past the end of the file.
1539 * There is a virtual hole there. */
1540 assert(errno
!= -ENXIO
);
1545 if (*hole
> start
) {
1548 /* On a hole. We need another syscall to find its end. */
1549 *data
= lseek(s
->fd
, start
, SEEK_DATA
);
1551 *data
= lseek(s
->fd
, 0, SEEK_END
);
1555 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
1562 * Returns true iff the specified sector is present in the disk image. Drivers
1563 * not implementing the functionality are assumed to not support backing files,
1564 * hence all their sectors are reported as allocated.
1566 * If 'sector_num' is beyond the end of the disk image the return value is 0
1567 * and 'pnum' is set to 0.
1569 * 'pnum' is set to the number of sectors (including and immediately following
1570 * the specified sector) that are known to be in the same
1571 * allocated/unallocated state.
1573 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1574 * beyond the end of the disk image it will be clamped.
1576 static int64_t coroutine_fn
raw_co_get_block_status(BlockDriverState
*bs
,
1578 int nb_sectors
, int *pnum
)
1580 off_t start
, data
= 0, hole
= 0;
1588 start
= sector_num
* BDRV_SECTOR_SIZE
;
1590 ret
= try_seek_hole(bs
, start
, &data
, &hole
, pnum
);
1592 ret
= try_fiemap(bs
, start
, &data
, &hole
, nb_sectors
, pnum
);
1594 /* Assume everything is allocated. */
1596 hole
= start
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1597 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
1601 if (data
<= start
) {
1602 /* On a data extent, compute sectors to the end of the extent. */
1603 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1605 /* On a hole, compute sectors to the beginning of the next extent. */
1606 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1607 ret
&= ~BDRV_BLOCK_DATA
;
1608 ret
|= BDRV_BLOCK_ZERO
;
1614 static coroutine_fn BlockAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1615 int64_t sector_num
, int nb_sectors
,
1616 BlockCompletionFunc
*cb
, void *opaque
)
1618 BDRVRawState
*s
= bs
->opaque
;
1620 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1621 cb
, opaque
, QEMU_AIO_DISCARD
);
1624 static int coroutine_fn
raw_co_write_zeroes(
1625 BlockDriverState
*bs
, int64_t sector_num
,
1626 int nb_sectors
, BdrvRequestFlags flags
)
1628 BDRVRawState
*s
= bs
->opaque
;
1630 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1631 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1632 QEMU_AIO_WRITE_ZEROES
);
1633 } else if (s
->discard_zeroes
) {
1634 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1640 static int raw_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1642 BDRVRawState
*s
= bs
->opaque
;
1644 bdi
->unallocated_blocks_are_zero
= s
->discard_zeroes
;
1645 bdi
->can_write_zeroes_with_unmap
= s
->discard_zeroes
;
1649 static QemuOptsList raw_create_opts
= {
1650 .name
= "raw-create-opts",
1651 .head
= QTAILQ_HEAD_INITIALIZER(raw_create_opts
.head
),
1654 .name
= BLOCK_OPT_SIZE
,
1655 .type
= QEMU_OPT_SIZE
,
1656 .help
= "Virtual disk size"
1659 .name
= BLOCK_OPT_NOCOW
,
1660 .type
= QEMU_OPT_BOOL
,
1661 .help
= "Turn off copy-on-write (valid only on btrfs)"
1664 .name
= BLOCK_OPT_PREALLOC
,
1665 .type
= QEMU_OPT_STRING
,
1666 .help
= "Preallocation mode (allowed values: off, falloc, full)"
1668 { /* end of list */ }
1672 static BlockDriver bdrv_file
= {
1673 .format_name
= "file",
1674 .protocol_name
= "file",
1675 .instance_size
= sizeof(BDRVRawState
),
1676 .bdrv_needs_filename
= true,
1677 .bdrv_probe
= NULL
, /* no probe for protocols */
1678 .bdrv_parse_filename
= raw_parse_filename
,
1679 .bdrv_file_open
= raw_open
,
1680 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1681 .bdrv_reopen_commit
= raw_reopen_commit
,
1682 .bdrv_reopen_abort
= raw_reopen_abort
,
1683 .bdrv_close
= raw_close
,
1684 .bdrv_create
= raw_create
,
1685 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1686 .bdrv_co_get_block_status
= raw_co_get_block_status
,
1687 .bdrv_co_write_zeroes
= raw_co_write_zeroes
,
1689 .bdrv_aio_readv
= raw_aio_readv
,
1690 .bdrv_aio_writev
= raw_aio_writev
,
1691 .bdrv_aio_flush
= raw_aio_flush
,
1692 .bdrv_aio_discard
= raw_aio_discard
,
1693 .bdrv_refresh_limits
= raw_refresh_limits
,
1694 .bdrv_io_plug
= raw_aio_plug
,
1695 .bdrv_io_unplug
= raw_aio_unplug
,
1696 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
1698 .bdrv_truncate
= raw_truncate
,
1699 .bdrv_getlength
= raw_getlength
,
1700 .bdrv_get_info
= raw_get_info
,
1701 .bdrv_get_allocated_file_size
1702 = raw_get_allocated_file_size
,
1704 .bdrv_detach_aio_context
= raw_detach_aio_context
,
1705 .bdrv_attach_aio_context
= raw_attach_aio_context
,
1707 .create_opts
= &raw_create_opts
,
1710 /***********************************************/
1713 #if defined(__APPLE__) && defined(__MACH__)
1714 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1715 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1717 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1719 kern_return_t kernResult
;
1720 mach_port_t masterPort
;
1721 CFMutableDictionaryRef classesToMatch
;
1723 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1724 if ( KERN_SUCCESS
!= kernResult
) {
1725 printf( "IOMasterPort returned %d\n", kernResult
);
1728 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1729 if ( classesToMatch
== NULL
) {
1730 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1732 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1734 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1735 if ( KERN_SUCCESS
!= kernResult
)
1737 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1743 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1745 io_object_t nextMedia
;
1746 kern_return_t kernResult
= KERN_FAILURE
;
1748 nextMedia
= IOIteratorNext( mediaIterator
);
1751 CFTypeRef bsdPathAsCFString
;
1752 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1753 if ( bsdPathAsCFString
) {
1754 size_t devPathLength
;
1755 strcpy( bsdPath
, _PATH_DEV
);
1756 strcat( bsdPath
, "r" );
1757 devPathLength
= strlen( bsdPath
);
1758 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1759 kernResult
= KERN_SUCCESS
;
1761 CFRelease( bsdPathAsCFString
);
1763 IOObjectRelease( nextMedia
);
1771 static int hdev_probe_device(const char *filename
)
1775 /* allow a dedicated CD-ROM driver to match with a higher priority */
1776 if (strstart(filename
, "/dev/cdrom", NULL
))
1779 if (stat(filename
, &st
) >= 0 &&
1780 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1787 static int check_hdev_writable(BDRVRawState
*s
)
1789 #if defined(BLKROGET)
1790 /* Linux block devices can be configured "read-only" using blockdev(8).
1791 * This is independent of device node permissions and therefore open(2)
1792 * with O_RDWR succeeds. Actual writes fail with EPERM.
1794 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1795 * check for read-only block devices so that Linux block devices behave
1801 if (fstat(s
->fd
, &st
)) {
1805 if (!S_ISBLK(st
.st_mode
)) {
1809 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1816 #endif /* defined(BLKROGET) */
1820 static void hdev_parse_filename(const char *filename
, QDict
*options
,
1823 /* The prefix is optional, just as for "file". */
1824 strstart(filename
, "host_device:", &filename
);
1826 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
1829 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1832 BDRVRawState
*s
= bs
->opaque
;
1833 Error
*local_err
= NULL
;
1835 const char *filename
= qdict_get_str(options
, "filename");
1837 #if defined(__APPLE__) && defined(__MACH__)
1838 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1839 kern_return_t kernResult
;
1840 io_iterator_t mediaIterator
;
1841 char bsdPath
[ MAXPATHLEN
];
1844 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1845 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1847 if ( bsdPath
[ 0 ] != '\0' ) {
1848 strcat(bsdPath
,"s0");
1849 /* some CDs don't have a partition 0 */
1850 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1852 bsdPath
[strlen(bsdPath
)-1] = '1';
1857 qdict_put(options
, "filename", qstring_from_str(filename
));
1860 if ( mediaIterator
)
1861 IOObjectRelease( mediaIterator
);
1865 s
->type
= FTYPE_FILE
;
1866 #if defined(__linux__)
1868 char resolved_path
[ MAXPATHLEN
], *temp
;
1870 temp
= realpath(filename
, resolved_path
);
1871 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1877 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
1880 error_propagate(errp
, local_err
);
1885 if (flags
& BDRV_O_RDWR
) {
1886 ret
= check_hdev_writable(s
);
1889 error_setg_errno(errp
, -ret
, "The device is not writable");
1897 #if defined(__linux__)
1898 /* Note: we do not have a reliable method to detect if the floppy is
1899 present. The current method is to try to open the floppy at every
1900 I/O and to keep it opened during a few hundreds of ms. */
1901 static int fd_open(BlockDriverState
*bs
)
1903 BDRVRawState
*s
= bs
->opaque
;
1904 int last_media_present
;
1906 if (s
->type
!= FTYPE_FD
)
1908 last_media_present
= (s
->fd
>= 0);
1910 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1914 printf("Floppy closed\n");
1918 if (s
->fd_got_error
&&
1919 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1921 printf("No floppy (open delayed)\n");
1925 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1927 s
->fd_error_time
= get_clock();
1928 s
->fd_got_error
= 1;
1929 if (last_media_present
)
1930 s
->fd_media_changed
= 1;
1932 printf("No floppy\n");
1937 printf("Floppy opened\n");
1940 if (!last_media_present
)
1941 s
->fd_media_changed
= 1;
1942 s
->fd_open_time
= get_clock();
1943 s
->fd_got_error
= 0;
1947 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1949 BDRVRawState
*s
= bs
->opaque
;
1951 return ioctl(s
->fd
, req
, buf
);
1954 static BlockAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1955 unsigned long int req
, void *buf
,
1956 BlockCompletionFunc
*cb
, void *opaque
)
1958 BDRVRawState
*s
= bs
->opaque
;
1959 RawPosixAIOData
*acb
;
1962 if (fd_open(bs
) < 0)
1965 acb
= g_slice_new(RawPosixAIOData
);
1967 acb
->aio_type
= QEMU_AIO_IOCTL
;
1968 acb
->aio_fildes
= s
->fd
;
1969 acb
->aio_offset
= 0;
1970 acb
->aio_ioctl_buf
= buf
;
1971 acb
->aio_ioctl_cmd
= req
;
1972 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1973 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1976 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1977 static int fd_open(BlockDriverState
*bs
)
1979 BDRVRawState
*s
= bs
->opaque
;
1981 /* this is just to ensure s->fd is sane (its called by io ops) */
1986 #else /* !linux && !FreeBSD */
1988 static int fd_open(BlockDriverState
*bs
)
1993 #endif /* !linux && !FreeBSD */
1995 static coroutine_fn BlockAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1996 int64_t sector_num
, int nb_sectors
,
1997 BlockCompletionFunc
*cb
, void *opaque
)
1999 BDRVRawState
*s
= bs
->opaque
;
2001 if (fd_open(bs
) < 0) {
2004 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
2005 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
2008 static coroutine_fn
int hdev_co_write_zeroes(BlockDriverState
*bs
,
2009 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
2011 BDRVRawState
*s
= bs
->opaque
;
2018 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
2019 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
2020 QEMU_AIO_WRITE_ZEROES
|QEMU_AIO_BLKDEV
);
2021 } else if (s
->discard_zeroes
) {
2022 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
2023 QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
2028 static int hdev_create(const char *filename
, QemuOpts
*opts
,
2033 struct stat stat_buf
;
2034 int64_t total_size
= 0;
2037 /* This function is used by all three protocol block drivers and therefore
2038 * any of these three prefixes may be given.
2039 * The return value has to be stored somewhere, otherwise this is an error
2040 * due to -Werror=unused-value. */
2042 strstart(filename
, "host_device:", &filename
) ||
2043 strstart(filename
, "host_cdrom:" , &filename
) ||
2044 strstart(filename
, "host_floppy:", &filename
);
2048 /* Read out options */
2049 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2052 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
2055 error_setg_errno(errp
, -ret
, "Could not open device");
2059 if (fstat(fd
, &stat_buf
) < 0) {
2061 error_setg_errno(errp
, -ret
, "Could not stat device");
2062 } else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
)) {
2064 "The given file is neither a block nor a character device");
2066 } else if (lseek(fd
, 0, SEEK_END
) < total_size
) {
2067 error_setg(errp
, "Device is too small");
2075 static BlockDriver bdrv_host_device
= {
2076 .format_name
= "host_device",
2077 .protocol_name
= "host_device",
2078 .instance_size
= sizeof(BDRVRawState
),
2079 .bdrv_needs_filename
= true,
2080 .bdrv_probe_device
= hdev_probe_device
,
2081 .bdrv_parse_filename
= hdev_parse_filename
,
2082 .bdrv_file_open
= hdev_open
,
2083 .bdrv_close
= raw_close
,
2084 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2085 .bdrv_reopen_commit
= raw_reopen_commit
,
2086 .bdrv_reopen_abort
= raw_reopen_abort
,
2087 .bdrv_create
= hdev_create
,
2088 .create_opts
= &raw_create_opts
,
2089 .bdrv_co_write_zeroes
= hdev_co_write_zeroes
,
2091 .bdrv_aio_readv
= raw_aio_readv
,
2092 .bdrv_aio_writev
= raw_aio_writev
,
2093 .bdrv_aio_flush
= raw_aio_flush
,
2094 .bdrv_aio_discard
= hdev_aio_discard
,
2095 .bdrv_refresh_limits
= raw_refresh_limits
,
2096 .bdrv_io_plug
= raw_aio_plug
,
2097 .bdrv_io_unplug
= raw_aio_unplug
,
2098 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2100 .bdrv_truncate
= raw_truncate
,
2101 .bdrv_getlength
= raw_getlength
,
2102 .bdrv_get_info
= raw_get_info
,
2103 .bdrv_get_allocated_file_size
2104 = raw_get_allocated_file_size
,
2106 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2107 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2109 /* generic scsi device */
2111 .bdrv_ioctl
= hdev_ioctl
,
2112 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
2117 static void floppy_parse_filename(const char *filename
, QDict
*options
,
2120 /* The prefix is optional, just as for "file". */
2121 strstart(filename
, "host_floppy:", &filename
);
2123 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
2126 static int floppy_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2129 BDRVRawState
*s
= bs
->opaque
;
2130 Error
*local_err
= NULL
;
2135 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
2136 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
2139 error_propagate(errp
, local_err
);
2144 /* close fd so that we can reopen it as needed */
2147 s
->fd_media_changed
= 1;
2152 static int floppy_probe_device(const char *filename
)
2156 struct floppy_struct fdparam
;
2159 if (strstart(filename
, "/dev/fd", NULL
) &&
2160 !strstart(filename
, "/dev/fdset/", NULL
)) {
2164 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
2168 ret
= fstat(fd
, &st
);
2169 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
2173 /* Attempt to detect via a floppy specific ioctl */
2174 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
2185 static int floppy_is_inserted(BlockDriverState
*bs
)
2187 return fd_open(bs
) >= 0;
2190 static int floppy_media_changed(BlockDriverState
*bs
)
2192 BDRVRawState
*s
= bs
->opaque
;
2196 * XXX: we do not have a true media changed indication.
2197 * It does not work if the floppy is changed without trying to read it.
2200 ret
= s
->fd_media_changed
;
2201 s
->fd_media_changed
= 0;
2203 printf("Floppy changed=%d\n", ret
);
2208 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
2210 BDRVRawState
*s
= bs
->opaque
;
2217 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
2219 if (ioctl(fd
, FDEJECT
, 0) < 0)
2225 static BlockDriver bdrv_host_floppy
= {
2226 .format_name
= "host_floppy",
2227 .protocol_name
= "host_floppy",
2228 .instance_size
= sizeof(BDRVRawState
),
2229 .bdrv_needs_filename
= true,
2230 .bdrv_probe_device
= floppy_probe_device
,
2231 .bdrv_parse_filename
= floppy_parse_filename
,
2232 .bdrv_file_open
= floppy_open
,
2233 .bdrv_close
= raw_close
,
2234 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2235 .bdrv_reopen_commit
= raw_reopen_commit
,
2236 .bdrv_reopen_abort
= raw_reopen_abort
,
2237 .bdrv_create
= hdev_create
,
2238 .create_opts
= &raw_create_opts
,
2240 .bdrv_aio_readv
= raw_aio_readv
,
2241 .bdrv_aio_writev
= raw_aio_writev
,
2242 .bdrv_aio_flush
= raw_aio_flush
,
2243 .bdrv_refresh_limits
= raw_refresh_limits
,
2244 .bdrv_io_plug
= raw_aio_plug
,
2245 .bdrv_io_unplug
= raw_aio_unplug
,
2246 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2248 .bdrv_truncate
= raw_truncate
,
2249 .bdrv_getlength
= raw_getlength
,
2250 .has_variable_length
= true,
2251 .bdrv_get_allocated_file_size
2252 = raw_get_allocated_file_size
,
2254 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2255 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2257 /* removable device support */
2258 .bdrv_is_inserted
= floppy_is_inserted
,
2259 .bdrv_media_changed
= floppy_media_changed
,
2260 .bdrv_eject
= floppy_eject
,
2264 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2265 static void cdrom_parse_filename(const char *filename
, QDict
*options
,
2268 /* The prefix is optional, just as for "file". */
2269 strstart(filename
, "host_cdrom:", &filename
);
2271 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
2276 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2279 BDRVRawState
*s
= bs
->opaque
;
2280 Error
*local_err
= NULL
;
2285 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
2286 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
2288 error_propagate(errp
, local_err
);
2293 static int cdrom_probe_device(const char *filename
)
2299 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
2303 ret
= fstat(fd
, &st
);
2304 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
2308 /* Attempt to detect via a CDROM specific ioctl */
2309 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
2319 static int cdrom_is_inserted(BlockDriverState
*bs
)
2321 BDRVRawState
*s
= bs
->opaque
;
2324 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
2325 if (ret
== CDS_DISC_OK
)
2330 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
2332 BDRVRawState
*s
= bs
->opaque
;
2335 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
2336 perror("CDROMEJECT");
2338 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
2339 perror("CDROMEJECT");
2343 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
2345 BDRVRawState
*s
= bs
->opaque
;
2347 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
2349 * Note: an error can happen if the distribution automatically
2352 /* perror("CDROM_LOCKDOOR"); */
2356 static BlockDriver bdrv_host_cdrom
= {
2357 .format_name
= "host_cdrom",
2358 .protocol_name
= "host_cdrom",
2359 .instance_size
= sizeof(BDRVRawState
),
2360 .bdrv_needs_filename
= true,
2361 .bdrv_probe_device
= cdrom_probe_device
,
2362 .bdrv_parse_filename
= cdrom_parse_filename
,
2363 .bdrv_file_open
= cdrom_open
,
2364 .bdrv_close
= raw_close
,
2365 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2366 .bdrv_reopen_commit
= raw_reopen_commit
,
2367 .bdrv_reopen_abort
= raw_reopen_abort
,
2368 .bdrv_create
= hdev_create
,
2369 .create_opts
= &raw_create_opts
,
2371 .bdrv_aio_readv
= raw_aio_readv
,
2372 .bdrv_aio_writev
= raw_aio_writev
,
2373 .bdrv_aio_flush
= raw_aio_flush
,
2374 .bdrv_refresh_limits
= raw_refresh_limits
,
2375 .bdrv_io_plug
= raw_aio_plug
,
2376 .bdrv_io_unplug
= raw_aio_unplug
,
2377 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2379 .bdrv_truncate
= raw_truncate
,
2380 .bdrv_getlength
= raw_getlength
,
2381 .has_variable_length
= true,
2382 .bdrv_get_allocated_file_size
2383 = raw_get_allocated_file_size
,
2385 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2386 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2388 /* removable device support */
2389 .bdrv_is_inserted
= cdrom_is_inserted
,
2390 .bdrv_eject
= cdrom_eject
,
2391 .bdrv_lock_medium
= cdrom_lock_medium
,
2393 /* generic scsi device */
2394 .bdrv_ioctl
= hdev_ioctl
,
2395 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
2397 #endif /* __linux__ */
2399 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2400 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2403 BDRVRawState
*s
= bs
->opaque
;
2404 Error
*local_err
= NULL
;
2409 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
2412 error_propagate(errp
, local_err
);
2417 /* make sure the door isn't locked at this time */
2418 ioctl(s
->fd
, CDIOCALLOW
);
2422 static int cdrom_probe_device(const char *filename
)
2424 if (strstart(filename
, "/dev/cd", NULL
) ||
2425 strstart(filename
, "/dev/acd", NULL
))
2430 static int cdrom_reopen(BlockDriverState
*bs
)
2432 BDRVRawState
*s
= bs
->opaque
;
2436 * Force reread of possibly changed/newly loaded disc,
2437 * FreeBSD seems to not notice sometimes...
2441 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
2448 /* make sure the door isn't locked at this time */
2449 ioctl(s
->fd
, CDIOCALLOW
);
2453 static int cdrom_is_inserted(BlockDriverState
*bs
)
2455 return raw_getlength(bs
) > 0;
2458 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
2460 BDRVRawState
*s
= bs
->opaque
;
2465 (void) ioctl(s
->fd
, CDIOCALLOW
);
2468 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
2469 perror("CDIOCEJECT");
2471 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
2472 perror("CDIOCCLOSE");
2478 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
2480 BDRVRawState
*s
= bs
->opaque
;
2484 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
2486 * Note: an error can happen if the distribution automatically
2489 /* perror("CDROM_LOCKDOOR"); */
2493 static BlockDriver bdrv_host_cdrom
= {
2494 .format_name
= "host_cdrom",
2495 .protocol_name
= "host_cdrom",
2496 .instance_size
= sizeof(BDRVRawState
),
2497 .bdrv_needs_filename
= true,
2498 .bdrv_probe_device
= cdrom_probe_device
,
2499 .bdrv_parse_filename
= cdrom_parse_filename
,
2500 .bdrv_file_open
= cdrom_open
,
2501 .bdrv_close
= raw_close
,
2502 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2503 .bdrv_reopen_commit
= raw_reopen_commit
,
2504 .bdrv_reopen_abort
= raw_reopen_abort
,
2505 .bdrv_create
= hdev_create
,
2506 .create_opts
= &raw_create_opts
,
2508 .bdrv_aio_readv
= raw_aio_readv
,
2509 .bdrv_aio_writev
= raw_aio_writev
,
2510 .bdrv_aio_flush
= raw_aio_flush
,
2511 .bdrv_refresh_limits
= raw_refresh_limits
,
2512 .bdrv_io_plug
= raw_aio_plug
,
2513 .bdrv_io_unplug
= raw_aio_unplug
,
2514 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2516 .bdrv_truncate
= raw_truncate
,
2517 .bdrv_getlength
= raw_getlength
,
2518 .has_variable_length
= true,
2519 .bdrv_get_allocated_file_size
2520 = raw_get_allocated_file_size
,
2522 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2523 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2525 /* removable device support */
2526 .bdrv_is_inserted
= cdrom_is_inserted
,
2527 .bdrv_eject
= cdrom_eject
,
2528 .bdrv_lock_medium
= cdrom_lock_medium
,
2530 #endif /* __FreeBSD__ */
2532 static void bdrv_file_init(void)
2535 * Register all the drivers. Note that order is important, the driver
2536 * registered last will get probed first.
2538 bdrv_register(&bdrv_file
);
2539 bdrv_register(&bdrv_host_device
);
2541 bdrv_register(&bdrv_host_floppy
);
2542 bdrv_register(&bdrv_host_cdrom
);
2544 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2545 bdrv_register(&bdrv_host_cdrom
);
2549 block_init(bdrv_file_init
);