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 */
63 #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE)
64 #include <linux/falloc.h>
66 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
72 #include <sys/ioctl.h>
73 #include <sys/disklabel.h>
78 #include <sys/ioctl.h>
79 #include <sys/disklabel.h>
85 #include <sys/ioctl.h>
86 #include <sys/diskslice.h>
93 //#define DEBUG_FLOPPY
96 #if defined(DEBUG_BLOCK)
97 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
98 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
100 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
103 /* OS X does not have O_DSYNC */
106 #define O_DSYNC O_SYNC
107 #elif defined(O_FSYNC)
108 #define O_DSYNC O_FSYNC
112 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
114 #define O_DIRECT O_DSYNC
121 /* if the FD is not accessed during that time (in ns), we try to
122 reopen it to see if the disk has been changed */
123 #define FD_OPEN_TIMEOUT (1000000000)
125 #define MAX_BLOCKSIZE 4096
127 typedef struct BDRVRawState
{
133 #if defined(__linux__)
134 /* linux floppy specific */
135 int64_t fd_open_time
;
136 int64_t fd_error_time
;
138 int fd_media_changed
;
140 #ifdef CONFIG_LINUX_AIO
148 bool has_write_zeroes
:1;
149 bool discard_zeroes
:1;
151 bool needs_alignment
;
154 typedef struct BDRVRawReopenState
{
157 #ifdef CONFIG_LINUX_AIO
160 } BDRVRawReopenState
;
162 static int fd_open(BlockDriverState
*bs
);
163 static int64_t raw_getlength(BlockDriverState
*bs
);
165 typedef struct RawPosixAIOData
{
166 BlockDriverState
*bs
;
169 struct iovec
*aio_iov
;
174 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
179 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
180 static int cdrom_reopen(BlockDriverState
*bs
);
183 #if defined(__NetBSD__)
184 static int raw_normalize_devicepath(const char **filename
)
186 static char namebuf
[PATH_MAX
];
187 const char *dp
, *fname
;
191 dp
= strrchr(fname
, '/');
192 if (lstat(fname
, &sb
) < 0) {
193 fprintf(stderr
, "%s: stat failed: %s\n",
194 fname
, strerror(errno
));
198 if (!S_ISBLK(sb
.st_mode
)) {
203 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
205 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
206 (int)(dp
- fname
), fname
, dp
+ 1);
208 fprintf(stderr
, "%s is a block device", fname
);
210 fprintf(stderr
, ", using %s\n", *filename
);
215 static int raw_normalize_devicepath(const char **filename
)
221 static void raw_probe_alignment(BlockDriverState
*bs
, int fd
, Error
**errp
)
223 BDRVRawState
*s
= bs
->opaque
;
225 unsigned int sector_size
;
227 /* For /dev/sg devices the alignment is not really used.
228 With buffered I/O, we don't have any restrictions. */
229 if (bs
->sg
|| !s
->needs_alignment
) {
230 bs
->request_alignment
= 1;
235 /* Try a few ioctls to get the right size */
236 bs
->request_alignment
= 0;
240 if (ioctl(fd
, BLKSSZGET
, §or_size
) >= 0) {
241 bs
->request_alignment
= sector_size
;
244 #ifdef DKIOCGETBLOCKSIZE
245 if (ioctl(fd
, DKIOCGETBLOCKSIZE
, §or_size
) >= 0) {
246 bs
->request_alignment
= sector_size
;
249 #ifdef DIOCGSECTORSIZE
250 if (ioctl(fd
, DIOCGSECTORSIZE
, §or_size
) >= 0) {
251 bs
->request_alignment
= sector_size
;
257 if (xfsctl(NULL
, fd
, XFS_IOC_DIOINFO
, &da
) >= 0) {
258 bs
->request_alignment
= da
.d_miniosz
;
259 /* The kernel returns wrong information for d_mem */
260 /* s->buf_align = da.d_mem; */
265 /* If we could not get the sizes so far, we can only guess them */
268 buf
= qemu_memalign(MAX_BLOCKSIZE
, 2 * MAX_BLOCKSIZE
);
269 for (align
= 512; align
<= MAX_BLOCKSIZE
; align
<<= 1) {
270 if (pread(fd
, buf
+ align
, MAX_BLOCKSIZE
, 0) >= 0) {
271 s
->buf_align
= align
;
278 if (!bs
->request_alignment
) {
280 buf
= qemu_memalign(s
->buf_align
, MAX_BLOCKSIZE
);
281 for (align
= 512; align
<= MAX_BLOCKSIZE
; align
<<= 1) {
282 if (pread(fd
, buf
, align
, 0) >= 0) {
283 bs
->request_alignment
= align
;
290 if (!s
->buf_align
|| !bs
->request_alignment
) {
291 error_setg(errp
, "Could not find working O_DIRECT alignment. "
292 "Try cache.direct=off.");
296 static void raw_parse_flags(int bdrv_flags
, int *open_flags
)
298 assert(open_flags
!= NULL
);
300 *open_flags
|= O_BINARY
;
301 *open_flags
&= ~O_ACCMODE
;
302 if (bdrv_flags
& BDRV_O_RDWR
) {
303 *open_flags
|= O_RDWR
;
305 *open_flags
|= O_RDONLY
;
308 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
309 * and O_DIRECT for no caching. */
310 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
311 *open_flags
|= O_DIRECT
;
315 static void raw_detach_aio_context(BlockDriverState
*bs
)
317 #ifdef CONFIG_LINUX_AIO
318 BDRVRawState
*s
= bs
->opaque
;
321 laio_detach_aio_context(s
->aio_ctx
, bdrv_get_aio_context(bs
));
326 static void raw_attach_aio_context(BlockDriverState
*bs
,
327 AioContext
*new_context
)
329 #ifdef CONFIG_LINUX_AIO
330 BDRVRawState
*s
= bs
->opaque
;
333 laio_attach_aio_context(s
->aio_ctx
, new_context
);
338 #ifdef CONFIG_LINUX_AIO
339 static int raw_set_aio(void **aio_ctx
, int *use_aio
, int bdrv_flags
)
342 assert(aio_ctx
!= NULL
);
343 assert(use_aio
!= NULL
);
345 * Currently Linux do AIO only for files opened with O_DIRECT
346 * specified so check NOCACHE flag too
348 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
349 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
351 /* if non-NULL, laio_init() has already been run */
352 if (*aio_ctx
== NULL
) {
353 *aio_ctx
= laio_init();
370 static void raw_parse_filename(const char *filename
, QDict
*options
,
373 /* The filename does not have to be prefixed by the protocol name, since
374 * "file" is the default protocol; therefore, the return value of this
375 * function call can be ignored. */
376 strstart(filename
, "file:", &filename
);
378 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
381 static QemuOptsList raw_runtime_opts
= {
383 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
387 .type
= QEMU_OPT_STRING
,
388 .help
= "File name of the image",
390 { /* end of list */ }
394 static int raw_open_common(BlockDriverState
*bs
, QDict
*options
,
395 int bdrv_flags
, int open_flags
, Error
**errp
)
397 BDRVRawState
*s
= bs
->opaque
;
399 Error
*local_err
= NULL
;
400 const char *filename
= NULL
;
404 opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0, &error_abort
);
405 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
407 error_propagate(errp
, local_err
);
412 filename
= qemu_opt_get(opts
, "filename");
414 ret
= raw_normalize_devicepath(&filename
);
416 error_setg_errno(errp
, -ret
, "Could not normalize device path");
420 s
->open_flags
= open_flags
;
421 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
424 fd
= qemu_open(filename
, s
->open_flags
, 0644);
434 #ifdef CONFIG_LINUX_AIO
435 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
438 error_setg_errno(errp
, -ret
, "Could not set AIO state");
443 s
->has_discard
= true;
444 s
->has_write_zeroes
= true;
445 if ((bs
->open_flags
& BDRV_O_NOCACHE
) != 0) {
446 s
->needs_alignment
= true;
449 if (fstat(s
->fd
, &st
) < 0) {
451 error_setg_errno(errp
, errno
, "Could not stat file");
454 if (S_ISREG(st
.st_mode
)) {
455 s
->discard_zeroes
= true;
456 s
->has_fallocate
= true;
458 if (S_ISBLK(st
.st_mode
)) {
459 #ifdef BLKDISCARDZEROES
461 if (ioctl(s
->fd
, BLKDISCARDZEROES
, &arg
) == 0 && arg
) {
462 s
->discard_zeroes
= true;
466 /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do
467 * not rely on the contents of discarded blocks unless using O_DIRECT.
468 * Same for BLKZEROOUT.
470 if (!(bs
->open_flags
& BDRV_O_NOCACHE
)) {
471 s
->discard_zeroes
= false;
472 s
->has_write_zeroes
= false;
477 if (S_ISCHR(st
.st_mode
)) {
479 * The file is a char device (disk), which on FreeBSD isn't behind
480 * a pager, so force all requests to be aligned. This is needed
481 * so QEMU makes sure all IO operations on the device are aligned
482 * to sector size, or else FreeBSD will reject them with EINVAL.
484 s
->needs_alignment
= true;
489 if (platform_test_xfs_fd(s
->fd
)) {
494 raw_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
498 if (filename
&& (bdrv_flags
& BDRV_O_TEMPORARY
)) {
505 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
508 BDRVRawState
*s
= bs
->opaque
;
509 Error
*local_err
= NULL
;
512 s
->type
= FTYPE_FILE
;
513 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
515 error_propagate(errp
, local_err
);
520 static int raw_reopen_prepare(BDRVReopenState
*state
,
521 BlockReopenQueue
*queue
, Error
**errp
)
524 BDRVRawReopenState
*raw_s
;
526 Error
*local_err
= NULL
;
528 assert(state
!= NULL
);
529 assert(state
->bs
!= NULL
);
531 s
= state
->bs
->opaque
;
533 state
->opaque
= g_new0(BDRVRawReopenState
, 1);
534 raw_s
= state
->opaque
;
536 #ifdef CONFIG_LINUX_AIO
537 raw_s
->use_aio
= s
->use_aio
;
539 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
540 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
541 * won't override aio_ctx if aio_ctx is non-NULL */
542 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
543 error_setg(errp
, "Could not set AIO state");
548 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
549 raw_s
->open_flags
|= O_NONBLOCK
;
552 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
556 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
558 fcntl_flags
|= O_NOATIME
;
562 /* Not all operating systems have O_ASYNC, and those that don't
563 * will not let us track the state into raw_s->open_flags (typically
564 * you achieve the same effect with an ioctl, for example I_SETSIG
565 * on Solaris). But we do not use O_ASYNC, so that's fine.
567 assert((s
->open_flags
& O_ASYNC
) == 0);
570 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
571 /* dup the original fd */
572 /* TODO: use qemu fcntl wrapper */
573 #ifdef F_DUPFD_CLOEXEC
574 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
576 raw_s
->fd
= dup(s
->fd
);
577 if (raw_s
->fd
!= -1) {
578 qemu_set_cloexec(raw_s
->fd
);
581 if (raw_s
->fd
>= 0) {
582 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
584 qemu_close(raw_s
->fd
);
590 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
591 if (raw_s
->fd
== -1) {
592 assert(!(raw_s
->open_flags
& O_CREAT
));
593 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
594 if (raw_s
->fd
== -1) {
595 error_setg_errno(errp
, errno
, "Could not reopen file");
600 /* Fail already reopen_prepare() if we can't get a working O_DIRECT
601 * alignment with the new fd. */
602 if (raw_s
->fd
!= -1) {
603 raw_probe_alignment(state
->bs
, raw_s
->fd
, &local_err
);
605 qemu_close(raw_s
->fd
);
607 error_propagate(errp
, local_err
);
615 static void raw_reopen_commit(BDRVReopenState
*state
)
617 BDRVRawReopenState
*raw_s
= state
->opaque
;
618 BDRVRawState
*s
= state
->bs
->opaque
;
620 s
->open_flags
= raw_s
->open_flags
;
624 #ifdef CONFIG_LINUX_AIO
625 s
->use_aio
= raw_s
->use_aio
;
628 g_free(state
->opaque
);
629 state
->opaque
= NULL
;
633 static void raw_reopen_abort(BDRVReopenState
*state
)
635 BDRVRawReopenState
*raw_s
= state
->opaque
;
637 /* nothing to do if NULL, we didn't get far enough */
642 if (raw_s
->fd
>= 0) {
643 qemu_close(raw_s
->fd
);
646 g_free(state
->opaque
);
647 state
->opaque
= NULL
;
650 static void raw_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
652 BDRVRawState
*s
= bs
->opaque
;
654 raw_probe_alignment(bs
, s
->fd
, errp
);
655 bs
->bl
.opt_mem_alignment
= s
->buf_align
;
658 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
662 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
670 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
674 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
683 static bool preadv_present
= true;
686 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
688 return preadv(fd
, iov
, nr_iov
, offset
);
692 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
694 return pwritev(fd
, iov
, nr_iov
, offset
);
699 static bool preadv_present
= false;
702 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
708 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
715 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
720 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
721 len
= qemu_pwritev(aiocb
->aio_fildes
,
726 len
= qemu_preadv(aiocb
->aio_fildes
,
730 } while (len
== -1 && errno
== EINTR
);
739 * Read/writes the data to/from a given linear buffer.
741 * Returns the number of bytes handles or -errno in case of an error. Short
742 * reads are only returned if the end of the file is reached.
744 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
749 while (offset
< aiocb
->aio_nbytes
) {
750 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
751 len
= pwrite(aiocb
->aio_fildes
,
752 (const char *)buf
+ offset
,
753 aiocb
->aio_nbytes
- offset
,
754 aiocb
->aio_offset
+ offset
);
756 len
= pread(aiocb
->aio_fildes
,
758 aiocb
->aio_nbytes
- offset
,
759 aiocb
->aio_offset
+ offset
);
761 if (len
== -1 && errno
== EINTR
) {
763 } else if (len
== -1 && errno
== EINVAL
&&
764 (aiocb
->bs
->open_flags
& BDRV_O_NOCACHE
) &&
765 !(aiocb
->aio_type
& QEMU_AIO_WRITE
) &&
767 /* O_DIRECT pread() may fail with EINVAL when offset is unaligned
768 * after a short read. Assume that O_DIRECT short reads only occur
769 * at EOF. Therefore this is a short read, not an I/O error.
772 } else if (len
== -1) {
775 } else if (len
== 0) {
784 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
789 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
791 * If there is just a single buffer, and it is properly aligned
792 * we can just use plain pread/pwrite without any problems.
794 if (aiocb
->aio_niov
== 1) {
795 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
798 * We have more than one iovec, and all are properly aligned.
800 * Try preadv/pwritev first and fall back to linearizing the
801 * buffer if it's not supported.
803 if (preadv_present
) {
804 nbytes
= handle_aiocb_rw_vector(aiocb
);
805 if (nbytes
== aiocb
->aio_nbytes
||
806 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
809 preadv_present
= false;
813 * XXX(hch): short read/write. no easy way to handle the reminder
814 * using these interfaces. For now retry using plain
820 * Ok, we have to do it the hard way, copy all segments into
821 * a single aligned buffer.
823 buf
= qemu_try_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
828 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
832 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
833 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
834 p
+= aiocb
->aio_iov
[i
].iov_len
;
836 assert(p
- buf
== aiocb
->aio_nbytes
);
839 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
840 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
842 size_t count
= aiocb
->aio_nbytes
, copy
;
845 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
847 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
848 copy
= aiocb
->aio_iov
[i
].iov_len
;
850 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
851 assert(count
>= copy
);
863 static int xfs_write_zeroes(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
865 struct xfs_flock64 fl
;
867 memset(&fl
, 0, sizeof(fl
));
868 fl
.l_whence
= SEEK_SET
;
872 if (xfsctl(NULL
, s
->fd
, XFS_IOC_ZERO_RANGE
, &fl
) < 0) {
873 DEBUG_BLOCK_PRINT("cannot write zero range (%s)\n", strerror(errno
));
880 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
882 struct xfs_flock64 fl
;
884 memset(&fl
, 0, sizeof(fl
));
885 fl
.l_whence
= SEEK_SET
;
889 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
890 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
898 static int translate_err(int err
)
900 if (err
== -ENODEV
|| err
== -ENOSYS
|| err
== -EOPNOTSUPP
||
907 #ifdef CONFIG_FALLOCATE
908 static int do_fallocate(int fd
, int mode
, off_t offset
, off_t len
)
911 if (fallocate(fd
, mode
, offset
, len
) == 0) {
914 } while (errno
== EINTR
);
915 return translate_err(-errno
);
919 static ssize_t
handle_aiocb_write_zeroes_block(RawPosixAIOData
*aiocb
)
922 BDRVRawState
*s
= aiocb
->bs
->opaque
;
924 if (!s
->has_write_zeroes
) {
930 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
931 if (ioctl(aiocb
->aio_fildes
, BLKZEROOUT
, range
) == 0) {
934 } while (errno
== EINTR
);
936 ret
= translate_err(-errno
);
939 if (ret
== -ENOTSUP
) {
940 s
->has_write_zeroes
= false;
945 static ssize_t
handle_aiocb_write_zeroes(RawPosixAIOData
*aiocb
)
947 BDRVRawState
*s
= aiocb
->bs
->opaque
;
949 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
950 return handle_aiocb_write_zeroes_block(aiocb
);
955 return xfs_write_zeroes(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
959 #ifdef CONFIG_FALLOCATE_ZERO_RANGE
960 if (s
->has_write_zeroes
) {
961 int ret
= do_fallocate(s
->fd
, FALLOC_FL_ZERO_RANGE
,
962 aiocb
->aio_offset
, aiocb
->aio_nbytes
);
963 if (ret
== 0 || ret
!= -ENOTSUP
) {
966 s
->has_write_zeroes
= false;
970 #ifdef CONFIG_FALLOCATE
971 if (s
->has_fallocate
&& aiocb
->aio_offset
>= bdrv_getlength(aiocb
->bs
)) {
972 int ret
= do_fallocate(s
->fd
, 0, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
973 if (ret
== 0 || ret
!= -ENOTSUP
) {
976 s
->has_fallocate
= false;
983 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
985 int ret
= -EOPNOTSUPP
;
986 BDRVRawState
*s
= aiocb
->bs
->opaque
;
988 if (!s
->has_discard
) {
992 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
995 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
996 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
999 } while (errno
== EINTR
);
1006 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
1010 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1011 ret
= do_fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
1012 aiocb
->aio_offset
, aiocb
->aio_nbytes
);
1016 ret
= translate_err(ret
);
1017 if (ret
== -ENOTSUP
) {
1018 s
->has_discard
= false;
1023 static int aio_worker(void *arg
)
1025 RawPosixAIOData
*aiocb
= arg
;
1028 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
1030 ret
= handle_aiocb_rw(aiocb
);
1031 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
1032 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
1033 0, aiocb
->aio_nbytes
- ret
);
1035 ret
= aiocb
->aio_nbytes
;
1037 if (ret
== aiocb
->aio_nbytes
) {
1039 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
1043 case QEMU_AIO_WRITE
:
1044 ret
= handle_aiocb_rw(aiocb
);
1045 if (ret
== aiocb
->aio_nbytes
) {
1047 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
1051 case QEMU_AIO_FLUSH
:
1052 ret
= handle_aiocb_flush(aiocb
);
1054 case QEMU_AIO_IOCTL
:
1055 ret
= handle_aiocb_ioctl(aiocb
);
1057 case QEMU_AIO_DISCARD
:
1058 ret
= handle_aiocb_discard(aiocb
);
1060 case QEMU_AIO_WRITE_ZEROES
:
1061 ret
= handle_aiocb_write_zeroes(aiocb
);
1064 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
1069 g_slice_free(RawPosixAIOData
, aiocb
);
1073 static int paio_submit_co(BlockDriverState
*bs
, int fd
,
1074 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1077 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
1081 acb
->aio_type
= type
;
1082 acb
->aio_fildes
= fd
;
1084 acb
->aio_nbytes
= nb_sectors
* BDRV_SECTOR_SIZE
;
1085 acb
->aio_offset
= sector_num
* BDRV_SECTOR_SIZE
;
1088 acb
->aio_iov
= qiov
->iov
;
1089 acb
->aio_niov
= qiov
->niov
;
1090 assert(qiov
->size
== acb
->aio_nbytes
);
1093 trace_paio_submit_co(sector_num
, nb_sectors
, type
);
1094 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1095 return thread_pool_submit_co(pool
, aio_worker
, acb
);
1098 static BlockAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
1099 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1100 BlockCompletionFunc
*cb
, void *opaque
, int type
)
1102 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
1106 acb
->aio_type
= type
;
1107 acb
->aio_fildes
= fd
;
1109 acb
->aio_nbytes
= nb_sectors
* BDRV_SECTOR_SIZE
;
1110 acb
->aio_offset
= sector_num
* BDRV_SECTOR_SIZE
;
1113 acb
->aio_iov
= qiov
->iov
;
1114 acb
->aio_niov
= qiov
->niov
;
1115 assert(qiov
->size
== acb
->aio_nbytes
);
1118 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
1119 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1120 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1123 static BlockAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
1124 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1125 BlockCompletionFunc
*cb
, void *opaque
, int type
)
1127 BDRVRawState
*s
= bs
->opaque
;
1129 if (fd_open(bs
) < 0)
1133 * Check if the underlying device requires requests to be aligned,
1134 * and if the request we are trying to submit is aligned or not.
1135 * If this is the case tell the low-level driver that it needs
1136 * to copy the buffer.
1138 if (s
->needs_alignment
) {
1139 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
1140 type
|= QEMU_AIO_MISALIGNED
;
1141 #ifdef CONFIG_LINUX_AIO
1142 } else if (s
->use_aio
) {
1143 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
1144 nb_sectors
, cb
, opaque
, type
);
1149 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
1153 static void raw_aio_plug(BlockDriverState
*bs
)
1155 #ifdef CONFIG_LINUX_AIO
1156 BDRVRawState
*s
= bs
->opaque
;
1158 laio_io_plug(bs
, s
->aio_ctx
);
1163 static void raw_aio_unplug(BlockDriverState
*bs
)
1165 #ifdef CONFIG_LINUX_AIO
1166 BDRVRawState
*s
= bs
->opaque
;
1168 laio_io_unplug(bs
, s
->aio_ctx
, true);
1173 static void raw_aio_flush_io_queue(BlockDriverState
*bs
)
1175 #ifdef CONFIG_LINUX_AIO
1176 BDRVRawState
*s
= bs
->opaque
;
1178 laio_io_unplug(bs
, s
->aio_ctx
, false);
1183 static BlockAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
1184 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1185 BlockCompletionFunc
*cb
, void *opaque
)
1187 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
1188 cb
, opaque
, QEMU_AIO_READ
);
1191 static BlockAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
1192 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
1193 BlockCompletionFunc
*cb
, void *opaque
)
1195 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
1196 cb
, opaque
, QEMU_AIO_WRITE
);
1199 static BlockAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
1200 BlockCompletionFunc
*cb
, void *opaque
)
1202 BDRVRawState
*s
= bs
->opaque
;
1204 if (fd_open(bs
) < 0)
1207 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
1210 static void raw_close(BlockDriverState
*bs
)
1212 BDRVRawState
*s
= bs
->opaque
;
1214 raw_detach_aio_context(bs
);
1216 #ifdef CONFIG_LINUX_AIO
1218 laio_cleanup(s
->aio_ctx
);
1227 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
1229 BDRVRawState
*s
= bs
->opaque
;
1232 if (fstat(s
->fd
, &st
)) {
1236 if (S_ISREG(st
.st_mode
)) {
1237 if (ftruncate(s
->fd
, offset
) < 0) {
1240 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1241 if (offset
> raw_getlength(bs
)) {
1252 static int64_t raw_getlength(BlockDriverState
*bs
)
1254 BDRVRawState
*s
= bs
->opaque
;
1260 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1261 struct disklabel dl
;
1263 if (ioctl(fd
, DIOCGDINFO
, &dl
))
1265 return (uint64_t)dl
.d_secsize
*
1266 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
1270 #elif defined(__NetBSD__)
1271 static int64_t raw_getlength(BlockDriverState
*bs
)
1273 BDRVRawState
*s
= bs
->opaque
;
1279 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1280 struct dkwedge_info dkw
;
1282 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
1283 return dkw
.dkw_size
* 512;
1285 struct disklabel dl
;
1287 if (ioctl(fd
, DIOCGDINFO
, &dl
))
1289 return (uint64_t)dl
.d_secsize
*
1290 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
1295 #elif defined(__sun__)
1296 static int64_t raw_getlength(BlockDriverState
*bs
)
1298 BDRVRawState
*s
= bs
->opaque
;
1299 struct dk_minfo minfo
;
1309 * Use the DKIOCGMEDIAINFO ioctl to read the size.
1311 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
1313 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
1317 * There are reports that lseek on some devices fails, but
1318 * irc discussion said that contingency on contingency was overkill.
1320 size
= lseek(s
->fd
, 0, SEEK_END
);
1326 #elif defined(CONFIG_BSD)
1327 static int64_t raw_getlength(BlockDriverState
*bs
)
1329 BDRVRawState
*s
= bs
->opaque
;
1333 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1342 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1345 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
1346 #ifdef DIOCGMEDIASIZE
1347 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
1348 #elif defined(DIOCGPART)
1351 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
1352 size
= pi
.media_size
;
1358 #if defined(__APPLE__) && defined(__MACH__)
1361 size
= lseek(fd
, 0LL, SEEK_END
);
1366 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1369 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1370 if (size
== 2048LL * (unsigned)-1)
1372 /* XXX no disc? maybe we need to reopen... */
1373 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
1380 size
= lseek(fd
, 0, SEEK_END
);
1388 static int64_t raw_getlength(BlockDriverState
*bs
)
1390 BDRVRawState
*s
= bs
->opaque
;
1399 size
= lseek(s
->fd
, 0, SEEK_END
);
1407 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1410 BDRVRawState
*s
= bs
->opaque
;
1412 if (fstat(s
->fd
, &st
) < 0) {
1415 return (int64_t)st
.st_blocks
* 512;
1418 static int raw_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1422 int64_t total_size
= 0;
1424 PreallocMode prealloc
;
1426 Error
*local_err
= NULL
;
1428 strstart(filename
, "file:", &filename
);
1430 /* Read out options */
1431 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1433 nocow
= qemu_opt_get_bool(opts
, BLOCK_OPT_NOCOW
, false);
1434 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
1435 prealloc
= qapi_enum_parse(PreallocMode_lookup
, buf
,
1436 PREALLOC_MODE_MAX
, PREALLOC_MODE_OFF
,
1440 error_propagate(errp
, local_err
);
1445 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1449 error_setg_errno(errp
, -result
, "Could not create file");
1455 /* Set NOCOW flag to solve performance issue on fs like btrfs.
1456 * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
1457 * will be ignored since any failure of this operation should not
1458 * block the left work.
1461 if (ioctl(fd
, FS_IOC_GETFLAGS
, &attr
) == 0) {
1462 attr
|= FS_NOCOW_FL
;
1463 ioctl(fd
, FS_IOC_SETFLAGS
, &attr
);
1468 if (ftruncate(fd
, total_size
) != 0) {
1470 error_setg_errno(errp
, -result
, "Could not resize file");
1475 #ifdef CONFIG_POSIX_FALLOCATE
1476 case PREALLOC_MODE_FALLOC
:
1477 /* posix_fallocate() doesn't set errno. */
1478 result
= -posix_fallocate(fd
, 0, total_size
);
1480 error_setg_errno(errp
, -result
,
1481 "Could not preallocate data for the new file");
1485 case PREALLOC_MODE_FULL
:
1487 int64_t num
= 0, left
= total_size
;
1488 buf
= g_malloc0(65536);
1491 num
= MIN(left
, 65536);
1492 result
= write(fd
, buf
, num
);
1495 error_setg_errno(errp
, -result
,
1496 "Could not write to the new file");
1505 error_setg_errno(errp
, -result
,
1506 "Could not flush new file to disk");
1512 case PREALLOC_MODE_OFF
:
1516 error_setg(errp
, "Unsupported preallocation mode: %s",
1517 PreallocMode_lookup
[prealloc
]);
1522 if (qemu_close(fd
) != 0 && result
== 0) {
1524 error_setg_errno(errp
, -result
, "Could not close the new file");
1531 * Find allocation range in @bs around offset @start.
1532 * May change underlying file descriptor's file offset.
1533 * If @start is not in a hole, store @start in @data, and the
1534 * beginning of the next hole in @hole, and return 0.
1535 * If @start is in a non-trailing hole, store @start in @hole and the
1536 * beginning of the next non-hole in @data, and return 0.
1537 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1538 * If we can't find out, return a negative errno other than -ENXIO.
1540 static int find_allocation(BlockDriverState
*bs
, off_t start
,
1541 off_t
*data
, off_t
*hole
)
1543 #if defined SEEK_HOLE && defined SEEK_DATA
1544 BDRVRawState
*s
= bs
->opaque
;
1549 * D1. offs == start: start is in data
1550 * D2. offs > start: start is in a hole, next data at offs
1551 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1552 * or start is beyond EOF
1553 * If the latter happens, the file has been truncated behind
1554 * our back since we opened it. All bets are off then.
1555 * Treating like a trailing hole is simplest.
1556 * D4. offs < 0, errno != ENXIO: we learned nothing
1558 offs
= lseek(s
->fd
, start
, SEEK_DATA
);
1560 return -errno
; /* D3 or D4 */
1562 assert(offs
>= start
);
1565 /* D2: in hole, next data at offs */
1571 /* D1: in data, end not yet known */
1575 * H1. offs == start: start is in a hole
1576 * If this happens here, a hole has been dug behind our back
1577 * since the previous lseek().
1578 * H2. offs > start: either start is in data, next hole at offs,
1579 * or start is in trailing hole, EOF at offs
1580 * Linux treats trailing holes like any other hole: offs ==
1581 * start. Solaris seeks to EOF instead: offs > start (blech).
1582 * If that happens here, a hole has been dug behind our back
1583 * since the previous lseek().
1584 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1585 * If this happens, the file has been truncated behind our
1586 * back since we opened it. Treat it like a trailing hole.
1587 * H4. offs < 0, errno != ENXIO: we learned nothing
1588 * Pretend we know nothing at all, i.e. "forget" about D1.
1590 offs
= lseek(s
->fd
, start
, SEEK_HOLE
);
1592 return -errno
; /* D1 and (H3 or H4) */
1594 assert(offs
>= start
);
1598 * D1 and H2: either in data, next hole at offs, or it was in
1599 * data but is now in a trailing hole. In the latter case,
1600 * all bets are off. Treating it as if it there was data all
1601 * the way to EOF is safe, so simply do that.
1616 * Returns the allocation status of the specified sectors.
1618 * If 'sector_num' is beyond the end of the disk image the return value is 0
1619 * and 'pnum' is set to 0.
1621 * 'pnum' is set to the number of sectors (including and immediately following
1622 * the specified sector) that are known to be in the same
1623 * allocated/unallocated state.
1625 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1626 * beyond the end of the disk image it will be clamped.
1628 static int64_t coroutine_fn
raw_co_get_block_status(BlockDriverState
*bs
,
1630 int nb_sectors
, int *pnum
)
1632 off_t start
, data
= 0, hole
= 0;
1641 start
= sector_num
* BDRV_SECTOR_SIZE
;
1642 total_size
= bdrv_getlength(bs
);
1643 if (total_size
< 0) {
1645 } else if (start
>= total_size
) {
1648 } else if (start
+ nb_sectors
* BDRV_SECTOR_SIZE
> total_size
) {
1649 nb_sectors
= DIV_ROUND_UP(total_size
- start
, BDRV_SECTOR_SIZE
);
1652 ret
= find_allocation(bs
, start
, &data
, &hole
);
1653 if (ret
== -ENXIO
) {
1656 ret
= BDRV_BLOCK_ZERO
;
1657 } else if (ret
< 0) {
1658 /* No info available, so pretend there are no holes */
1660 ret
= BDRV_BLOCK_DATA
;
1661 } else if (data
== start
) {
1662 /* On a data extent, compute sectors to the end of the extent. */
1663 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1664 ret
= BDRV_BLOCK_DATA
;
1666 /* On a hole, compute sectors to the beginning of the next extent. */
1667 assert(hole
== start
);
1668 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1669 ret
= BDRV_BLOCK_ZERO
;
1671 return ret
| BDRV_BLOCK_OFFSET_VALID
| start
;
1674 static coroutine_fn BlockAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1675 int64_t sector_num
, int nb_sectors
,
1676 BlockCompletionFunc
*cb
, void *opaque
)
1678 BDRVRawState
*s
= bs
->opaque
;
1680 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1681 cb
, opaque
, QEMU_AIO_DISCARD
);
1684 static int coroutine_fn
raw_co_write_zeroes(
1685 BlockDriverState
*bs
, int64_t sector_num
,
1686 int nb_sectors
, BdrvRequestFlags flags
)
1688 BDRVRawState
*s
= bs
->opaque
;
1690 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1691 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1692 QEMU_AIO_WRITE_ZEROES
);
1693 } else if (s
->discard_zeroes
) {
1694 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1700 static int raw_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1702 BDRVRawState
*s
= bs
->opaque
;
1704 bdi
->unallocated_blocks_are_zero
= s
->discard_zeroes
;
1705 bdi
->can_write_zeroes_with_unmap
= s
->discard_zeroes
;
1709 static QemuOptsList raw_create_opts
= {
1710 .name
= "raw-create-opts",
1711 .head
= QTAILQ_HEAD_INITIALIZER(raw_create_opts
.head
),
1714 .name
= BLOCK_OPT_SIZE
,
1715 .type
= QEMU_OPT_SIZE
,
1716 .help
= "Virtual disk size"
1719 .name
= BLOCK_OPT_NOCOW
,
1720 .type
= QEMU_OPT_BOOL
,
1721 .help
= "Turn off copy-on-write (valid only on btrfs)"
1724 .name
= BLOCK_OPT_PREALLOC
,
1725 .type
= QEMU_OPT_STRING
,
1726 .help
= "Preallocation mode (allowed values: off, falloc, full)"
1728 { /* end of list */ }
1732 BlockDriver bdrv_file
= {
1733 .format_name
= "file",
1734 .protocol_name
= "file",
1735 .instance_size
= sizeof(BDRVRawState
),
1736 .bdrv_needs_filename
= true,
1737 .bdrv_probe
= NULL
, /* no probe for protocols */
1738 .bdrv_parse_filename
= raw_parse_filename
,
1739 .bdrv_file_open
= raw_open
,
1740 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1741 .bdrv_reopen_commit
= raw_reopen_commit
,
1742 .bdrv_reopen_abort
= raw_reopen_abort
,
1743 .bdrv_close
= raw_close
,
1744 .bdrv_create
= raw_create
,
1745 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1746 .bdrv_co_get_block_status
= raw_co_get_block_status
,
1747 .bdrv_co_write_zeroes
= raw_co_write_zeroes
,
1749 .bdrv_aio_readv
= raw_aio_readv
,
1750 .bdrv_aio_writev
= raw_aio_writev
,
1751 .bdrv_aio_flush
= raw_aio_flush
,
1752 .bdrv_aio_discard
= raw_aio_discard
,
1753 .bdrv_refresh_limits
= raw_refresh_limits
,
1754 .bdrv_io_plug
= raw_aio_plug
,
1755 .bdrv_io_unplug
= raw_aio_unplug
,
1756 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
1758 .bdrv_truncate
= raw_truncate
,
1759 .bdrv_getlength
= raw_getlength
,
1760 .bdrv_get_info
= raw_get_info
,
1761 .bdrv_get_allocated_file_size
1762 = raw_get_allocated_file_size
,
1764 .bdrv_detach_aio_context
= raw_detach_aio_context
,
1765 .bdrv_attach_aio_context
= raw_attach_aio_context
,
1767 .create_opts
= &raw_create_opts
,
1770 /***********************************************/
1773 #if defined(__APPLE__) && defined(__MACH__)
1774 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1775 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1777 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1779 kern_return_t kernResult
;
1780 mach_port_t masterPort
;
1781 CFMutableDictionaryRef classesToMatch
;
1783 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1784 if ( KERN_SUCCESS
!= kernResult
) {
1785 printf( "IOMasterPort returned %d\n", kernResult
);
1788 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1789 if ( classesToMatch
== NULL
) {
1790 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1792 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1794 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1795 if ( KERN_SUCCESS
!= kernResult
)
1797 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1803 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1805 io_object_t nextMedia
;
1806 kern_return_t kernResult
= KERN_FAILURE
;
1808 nextMedia
= IOIteratorNext( mediaIterator
);
1811 CFTypeRef bsdPathAsCFString
;
1812 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1813 if ( bsdPathAsCFString
) {
1814 size_t devPathLength
;
1815 strcpy( bsdPath
, _PATH_DEV
);
1816 strcat( bsdPath
, "r" );
1817 devPathLength
= strlen( bsdPath
);
1818 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1819 kernResult
= KERN_SUCCESS
;
1821 CFRelease( bsdPathAsCFString
);
1823 IOObjectRelease( nextMedia
);
1831 static int hdev_probe_device(const char *filename
)
1835 /* allow a dedicated CD-ROM driver to match with a higher priority */
1836 if (strstart(filename
, "/dev/cdrom", NULL
))
1839 if (stat(filename
, &st
) >= 0 &&
1840 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1847 static int check_hdev_writable(BDRVRawState
*s
)
1849 #if defined(BLKROGET)
1850 /* Linux block devices can be configured "read-only" using blockdev(8).
1851 * This is independent of device node permissions and therefore open(2)
1852 * with O_RDWR succeeds. Actual writes fail with EPERM.
1854 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1855 * check for read-only block devices so that Linux block devices behave
1861 if (fstat(s
->fd
, &st
)) {
1865 if (!S_ISBLK(st
.st_mode
)) {
1869 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1876 #endif /* defined(BLKROGET) */
1880 static void hdev_parse_filename(const char *filename
, QDict
*options
,
1883 /* The prefix is optional, just as for "file". */
1884 strstart(filename
, "host_device:", &filename
);
1886 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
1889 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1892 BDRVRawState
*s
= bs
->opaque
;
1893 Error
*local_err
= NULL
;
1895 const char *filename
= qdict_get_str(options
, "filename");
1897 #if defined(__APPLE__) && defined(__MACH__)
1898 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1899 kern_return_t kernResult
;
1900 io_iterator_t mediaIterator
;
1901 char bsdPath
[ MAXPATHLEN
];
1904 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1905 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1907 if ( bsdPath
[ 0 ] != '\0' ) {
1908 strcat(bsdPath
,"s0");
1909 /* some CDs don't have a partition 0 */
1910 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1912 bsdPath
[strlen(bsdPath
)-1] = '1';
1917 qdict_put(options
, "filename", qstring_from_str(filename
));
1920 if ( mediaIterator
)
1921 IOObjectRelease( mediaIterator
);
1925 s
->type
= FTYPE_FILE
;
1926 #if defined(__linux__)
1928 char resolved_path
[ MAXPATHLEN
], *temp
;
1930 temp
= realpath(filename
, resolved_path
);
1931 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1937 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
1940 error_propagate(errp
, local_err
);
1945 if (flags
& BDRV_O_RDWR
) {
1946 ret
= check_hdev_writable(s
);
1949 error_setg_errno(errp
, -ret
, "The device is not writable");
1957 #if defined(__linux__)
1958 /* Note: we do not have a reliable method to detect if the floppy is
1959 present. The current method is to try to open the floppy at every
1960 I/O and to keep it opened during a few hundreds of ms. */
1961 static int fd_open(BlockDriverState
*bs
)
1963 BDRVRawState
*s
= bs
->opaque
;
1964 int last_media_present
;
1966 if (s
->type
!= FTYPE_FD
)
1968 last_media_present
= (s
->fd
>= 0);
1970 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1974 printf("Floppy closed\n");
1978 if (s
->fd_got_error
&&
1979 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1981 printf("No floppy (open delayed)\n");
1985 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1987 s
->fd_error_time
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
1988 s
->fd_got_error
= 1;
1989 if (last_media_present
)
1990 s
->fd_media_changed
= 1;
1992 printf("No floppy\n");
1997 printf("Floppy opened\n");
2000 if (!last_media_present
)
2001 s
->fd_media_changed
= 1;
2002 s
->fd_open_time
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
2003 s
->fd_got_error
= 0;
2007 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
2009 BDRVRawState
*s
= bs
->opaque
;
2011 return ioctl(s
->fd
, req
, buf
);
2014 static BlockAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
2015 unsigned long int req
, void *buf
,
2016 BlockCompletionFunc
*cb
, void *opaque
)
2018 BDRVRawState
*s
= bs
->opaque
;
2019 RawPosixAIOData
*acb
;
2022 if (fd_open(bs
) < 0)
2025 acb
= g_slice_new(RawPosixAIOData
);
2027 acb
->aio_type
= QEMU_AIO_IOCTL
;
2028 acb
->aio_fildes
= s
->fd
;
2029 acb
->aio_offset
= 0;
2030 acb
->aio_ioctl_buf
= buf
;
2031 acb
->aio_ioctl_cmd
= req
;
2032 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
2033 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
2036 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2037 static int fd_open(BlockDriverState
*bs
)
2039 BDRVRawState
*s
= bs
->opaque
;
2041 /* this is just to ensure s->fd is sane (its called by io ops) */
2046 #else /* !linux && !FreeBSD */
2048 static int fd_open(BlockDriverState
*bs
)
2053 #endif /* !linux && !FreeBSD */
2055 static coroutine_fn BlockAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
2056 int64_t sector_num
, int nb_sectors
,
2057 BlockCompletionFunc
*cb
, void *opaque
)
2059 BDRVRawState
*s
= bs
->opaque
;
2061 if (fd_open(bs
) < 0) {
2064 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
2065 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
2068 static coroutine_fn
int hdev_co_write_zeroes(BlockDriverState
*bs
,
2069 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
2071 BDRVRawState
*s
= bs
->opaque
;
2078 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
2079 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
2080 QEMU_AIO_WRITE_ZEROES
|QEMU_AIO_BLKDEV
);
2081 } else if (s
->discard_zeroes
) {
2082 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
2083 QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
2088 static int hdev_create(const char *filename
, QemuOpts
*opts
,
2093 struct stat stat_buf
;
2094 int64_t total_size
= 0;
2097 /* This function is used by all three protocol block drivers and therefore
2098 * any of these three prefixes may be given.
2099 * The return value has to be stored somewhere, otherwise this is an error
2100 * due to -Werror=unused-value. */
2102 strstart(filename
, "host_device:", &filename
) ||
2103 strstart(filename
, "host_cdrom:" , &filename
) ||
2104 strstart(filename
, "host_floppy:", &filename
);
2108 /* Read out options */
2109 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2112 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
2115 error_setg_errno(errp
, -ret
, "Could not open device");
2119 if (fstat(fd
, &stat_buf
) < 0) {
2121 error_setg_errno(errp
, -ret
, "Could not stat device");
2122 } else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
)) {
2124 "The given file is neither a block nor a character device");
2126 } else if (lseek(fd
, 0, SEEK_END
) < total_size
) {
2127 error_setg(errp
, "Device is too small");
2135 static BlockDriver bdrv_host_device
= {
2136 .format_name
= "host_device",
2137 .protocol_name
= "host_device",
2138 .instance_size
= sizeof(BDRVRawState
),
2139 .bdrv_needs_filename
= true,
2140 .bdrv_probe_device
= hdev_probe_device
,
2141 .bdrv_parse_filename
= hdev_parse_filename
,
2142 .bdrv_file_open
= hdev_open
,
2143 .bdrv_close
= raw_close
,
2144 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2145 .bdrv_reopen_commit
= raw_reopen_commit
,
2146 .bdrv_reopen_abort
= raw_reopen_abort
,
2147 .bdrv_create
= hdev_create
,
2148 .create_opts
= &raw_create_opts
,
2149 .bdrv_co_write_zeroes
= hdev_co_write_zeroes
,
2151 .bdrv_aio_readv
= raw_aio_readv
,
2152 .bdrv_aio_writev
= raw_aio_writev
,
2153 .bdrv_aio_flush
= raw_aio_flush
,
2154 .bdrv_aio_discard
= hdev_aio_discard
,
2155 .bdrv_refresh_limits
= raw_refresh_limits
,
2156 .bdrv_io_plug
= raw_aio_plug
,
2157 .bdrv_io_unplug
= raw_aio_unplug
,
2158 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2160 .bdrv_truncate
= raw_truncate
,
2161 .bdrv_getlength
= raw_getlength
,
2162 .bdrv_get_info
= raw_get_info
,
2163 .bdrv_get_allocated_file_size
2164 = raw_get_allocated_file_size
,
2166 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2167 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2169 /* generic scsi device */
2171 .bdrv_ioctl
= hdev_ioctl
,
2172 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
2177 static void floppy_parse_filename(const char *filename
, QDict
*options
,
2180 /* The prefix is optional, just as for "file". */
2181 strstart(filename
, "host_floppy:", &filename
);
2183 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
2186 static int floppy_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2189 BDRVRawState
*s
= bs
->opaque
;
2190 Error
*local_err
= NULL
;
2195 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
2196 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
2199 error_propagate(errp
, local_err
);
2204 /* close fd so that we can reopen it as needed */
2207 s
->fd_media_changed
= 1;
2212 static int floppy_probe_device(const char *filename
)
2216 struct floppy_struct fdparam
;
2219 if (strstart(filename
, "/dev/fd", NULL
) &&
2220 !strstart(filename
, "/dev/fdset/", NULL
)) {
2224 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
2228 ret
= fstat(fd
, &st
);
2229 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
2233 /* Attempt to detect via a floppy specific ioctl */
2234 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
2245 static int floppy_is_inserted(BlockDriverState
*bs
)
2247 return fd_open(bs
) >= 0;
2250 static int floppy_media_changed(BlockDriverState
*bs
)
2252 BDRVRawState
*s
= bs
->opaque
;
2256 * XXX: we do not have a true media changed indication.
2257 * It does not work if the floppy is changed without trying to read it.
2260 ret
= s
->fd_media_changed
;
2261 s
->fd_media_changed
= 0;
2263 printf("Floppy changed=%d\n", ret
);
2268 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
2270 BDRVRawState
*s
= bs
->opaque
;
2277 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
2279 if (ioctl(fd
, FDEJECT
, 0) < 0)
2285 static BlockDriver bdrv_host_floppy
= {
2286 .format_name
= "host_floppy",
2287 .protocol_name
= "host_floppy",
2288 .instance_size
= sizeof(BDRVRawState
),
2289 .bdrv_needs_filename
= true,
2290 .bdrv_probe_device
= floppy_probe_device
,
2291 .bdrv_parse_filename
= floppy_parse_filename
,
2292 .bdrv_file_open
= floppy_open
,
2293 .bdrv_close
= raw_close
,
2294 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2295 .bdrv_reopen_commit
= raw_reopen_commit
,
2296 .bdrv_reopen_abort
= raw_reopen_abort
,
2297 .bdrv_create
= hdev_create
,
2298 .create_opts
= &raw_create_opts
,
2300 .bdrv_aio_readv
= raw_aio_readv
,
2301 .bdrv_aio_writev
= raw_aio_writev
,
2302 .bdrv_aio_flush
= raw_aio_flush
,
2303 .bdrv_refresh_limits
= raw_refresh_limits
,
2304 .bdrv_io_plug
= raw_aio_plug
,
2305 .bdrv_io_unplug
= raw_aio_unplug
,
2306 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2308 .bdrv_truncate
= raw_truncate
,
2309 .bdrv_getlength
= raw_getlength
,
2310 .has_variable_length
= true,
2311 .bdrv_get_allocated_file_size
2312 = raw_get_allocated_file_size
,
2314 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2315 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2317 /* removable device support */
2318 .bdrv_is_inserted
= floppy_is_inserted
,
2319 .bdrv_media_changed
= floppy_media_changed
,
2320 .bdrv_eject
= floppy_eject
,
2324 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2325 static void cdrom_parse_filename(const char *filename
, QDict
*options
,
2328 /* The prefix is optional, just as for "file". */
2329 strstart(filename
, "host_cdrom:", &filename
);
2331 qdict_put_obj(options
, "filename", QOBJECT(qstring_from_str(filename
)));
2336 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2339 BDRVRawState
*s
= bs
->opaque
;
2340 Error
*local_err
= NULL
;
2345 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
2346 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
2348 error_propagate(errp
, local_err
);
2353 static int cdrom_probe_device(const char *filename
)
2359 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
2363 ret
= fstat(fd
, &st
);
2364 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
2368 /* Attempt to detect via a CDROM specific ioctl */
2369 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
2379 static int cdrom_is_inserted(BlockDriverState
*bs
)
2381 BDRVRawState
*s
= bs
->opaque
;
2384 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
2385 if (ret
== CDS_DISC_OK
)
2390 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
2392 BDRVRawState
*s
= bs
->opaque
;
2395 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
2396 perror("CDROMEJECT");
2398 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
2399 perror("CDROMEJECT");
2403 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
2405 BDRVRawState
*s
= bs
->opaque
;
2407 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
2409 * Note: an error can happen if the distribution automatically
2412 /* perror("CDROM_LOCKDOOR"); */
2416 static BlockDriver bdrv_host_cdrom
= {
2417 .format_name
= "host_cdrom",
2418 .protocol_name
= "host_cdrom",
2419 .instance_size
= sizeof(BDRVRawState
),
2420 .bdrv_needs_filename
= true,
2421 .bdrv_probe_device
= cdrom_probe_device
,
2422 .bdrv_parse_filename
= cdrom_parse_filename
,
2423 .bdrv_file_open
= cdrom_open
,
2424 .bdrv_close
= raw_close
,
2425 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2426 .bdrv_reopen_commit
= raw_reopen_commit
,
2427 .bdrv_reopen_abort
= raw_reopen_abort
,
2428 .bdrv_create
= hdev_create
,
2429 .create_opts
= &raw_create_opts
,
2431 .bdrv_aio_readv
= raw_aio_readv
,
2432 .bdrv_aio_writev
= raw_aio_writev
,
2433 .bdrv_aio_flush
= raw_aio_flush
,
2434 .bdrv_refresh_limits
= raw_refresh_limits
,
2435 .bdrv_io_plug
= raw_aio_plug
,
2436 .bdrv_io_unplug
= raw_aio_unplug
,
2437 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2439 .bdrv_truncate
= raw_truncate
,
2440 .bdrv_getlength
= raw_getlength
,
2441 .has_variable_length
= true,
2442 .bdrv_get_allocated_file_size
2443 = raw_get_allocated_file_size
,
2445 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2446 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2448 /* removable device support */
2449 .bdrv_is_inserted
= cdrom_is_inserted
,
2450 .bdrv_eject
= cdrom_eject
,
2451 .bdrv_lock_medium
= cdrom_lock_medium
,
2453 /* generic scsi device */
2454 .bdrv_ioctl
= hdev_ioctl
,
2455 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
2457 #endif /* __linux__ */
2459 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2460 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2463 BDRVRawState
*s
= bs
->opaque
;
2464 Error
*local_err
= NULL
;
2469 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
2472 error_propagate(errp
, local_err
);
2477 /* make sure the door isn't locked at this time */
2478 ioctl(s
->fd
, CDIOCALLOW
);
2482 static int cdrom_probe_device(const char *filename
)
2484 if (strstart(filename
, "/dev/cd", NULL
) ||
2485 strstart(filename
, "/dev/acd", NULL
))
2490 static int cdrom_reopen(BlockDriverState
*bs
)
2492 BDRVRawState
*s
= bs
->opaque
;
2496 * Force reread of possibly changed/newly loaded disc,
2497 * FreeBSD seems to not notice sometimes...
2501 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
2508 /* make sure the door isn't locked at this time */
2509 ioctl(s
->fd
, CDIOCALLOW
);
2513 static int cdrom_is_inserted(BlockDriverState
*bs
)
2515 return raw_getlength(bs
) > 0;
2518 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
2520 BDRVRawState
*s
= bs
->opaque
;
2525 (void) ioctl(s
->fd
, CDIOCALLOW
);
2528 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
2529 perror("CDIOCEJECT");
2531 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
2532 perror("CDIOCCLOSE");
2538 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
2540 BDRVRawState
*s
= bs
->opaque
;
2544 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
2546 * Note: an error can happen if the distribution automatically
2549 /* perror("CDROM_LOCKDOOR"); */
2553 static BlockDriver bdrv_host_cdrom
= {
2554 .format_name
= "host_cdrom",
2555 .protocol_name
= "host_cdrom",
2556 .instance_size
= sizeof(BDRVRawState
),
2557 .bdrv_needs_filename
= true,
2558 .bdrv_probe_device
= cdrom_probe_device
,
2559 .bdrv_parse_filename
= cdrom_parse_filename
,
2560 .bdrv_file_open
= cdrom_open
,
2561 .bdrv_close
= raw_close
,
2562 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2563 .bdrv_reopen_commit
= raw_reopen_commit
,
2564 .bdrv_reopen_abort
= raw_reopen_abort
,
2565 .bdrv_create
= hdev_create
,
2566 .create_opts
= &raw_create_opts
,
2568 .bdrv_aio_readv
= raw_aio_readv
,
2569 .bdrv_aio_writev
= raw_aio_writev
,
2570 .bdrv_aio_flush
= raw_aio_flush
,
2571 .bdrv_refresh_limits
= raw_refresh_limits
,
2572 .bdrv_io_plug
= raw_aio_plug
,
2573 .bdrv_io_unplug
= raw_aio_unplug
,
2574 .bdrv_flush_io_queue
= raw_aio_flush_io_queue
,
2576 .bdrv_truncate
= raw_truncate
,
2577 .bdrv_getlength
= raw_getlength
,
2578 .has_variable_length
= true,
2579 .bdrv_get_allocated_file_size
2580 = raw_get_allocated_file_size
,
2582 .bdrv_detach_aio_context
= raw_detach_aio_context
,
2583 .bdrv_attach_aio_context
= raw_attach_aio_context
,
2585 /* removable device support */
2586 .bdrv_is_inserted
= cdrom_is_inserted
,
2587 .bdrv_eject
= cdrom_eject
,
2588 .bdrv_lock_medium
= cdrom_lock_medium
,
2590 #endif /* __FreeBSD__ */
2592 static void bdrv_file_init(void)
2595 * Register all the drivers. Note that order is important, the driver
2596 * registered last will get probed first.
2598 bdrv_register(&bdrv_file
);
2599 bdrv_register(&bdrv_host_device
);
2601 bdrv_register(&bdrv_host_floppy
);
2602 bdrv_register(&bdrv_host_cdrom
);
2604 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2605 bdrv_register(&bdrv_host_cdrom
);
2609 block_init(bdrv_file_init
);