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"
34 #if defined(__APPLE__) && (__MACH__)
36 #include <sys/param.h>
37 #include <IOKit/IOKitLib.h>
38 #include <IOKit/IOBSD.h>
39 #include <IOKit/storage/IOMediaBSDClient.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <IOKit/storage/IOCDMedia.h>
42 //#include <IOKit/storage/IOCDTypes.h>
43 #include <CoreFoundation/CoreFoundation.h>
47 #define _POSIX_PTHREAD_SEMANTICS 1
51 #include <sys/types.h>
53 #include <sys/ioctl.h>
54 #include <sys/param.h>
55 #include <linux/cdrom.h>
60 #include <linux/fiemap.h>
62 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
63 #include <linux/falloc.h>
65 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
71 #include <sys/ioctl.h>
72 #include <sys/disklabel.h>
77 #include <sys/ioctl.h>
78 #include <sys/disklabel.h>
84 #include <sys/ioctl.h>
85 #include <sys/diskslice.h>
92 //#define DEBUG_FLOPPY
95 #if defined(DEBUG_BLOCK)
96 #define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
97 { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
99 #define DEBUG_BLOCK_PRINT(formatCstr, ...)
102 /* OS X does not have O_DSYNC */
105 #define O_DSYNC O_SYNC
106 #elif defined(O_FSYNC)
107 #define O_DSYNC O_FSYNC
111 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
113 #define O_DIRECT O_DSYNC
120 /* if the FD is not accessed during that time (in ns), we try to
121 reopen it to see if the disk has been changed */
122 #define FD_OPEN_TIMEOUT (1000000000)
124 #define MAX_BLOCKSIZE 4096
126 typedef struct BDRVRawState
{
130 #if defined(__linux__)
131 /* linux floppy specific */
132 int64_t fd_open_time
;
133 int64_t fd_error_time
;
135 int fd_media_changed
;
137 #ifdef CONFIG_LINUX_AIO
144 bool has_discard
: 1;
147 typedef struct BDRVRawReopenState
{
150 #ifdef CONFIG_LINUX_AIO
153 } BDRVRawReopenState
;
155 static int fd_open(BlockDriverState
*bs
);
156 static int64_t raw_getlength(BlockDriverState
*bs
);
158 typedef struct RawPosixAIOData
{
159 BlockDriverState
*bs
;
162 struct iovec
*aio_iov
;
167 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
172 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
173 static int cdrom_reopen(BlockDriverState
*bs
);
176 #if defined(__NetBSD__)
177 static int raw_normalize_devicepath(const char **filename
)
179 static char namebuf
[PATH_MAX
];
180 const char *dp
, *fname
;
184 dp
= strrchr(fname
, '/');
185 if (lstat(fname
, &sb
) < 0) {
186 fprintf(stderr
, "%s: stat failed: %s\n",
187 fname
, strerror(errno
));
191 if (!S_ISBLK(sb
.st_mode
)) {
196 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
198 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
199 (int)(dp
- fname
), fname
, dp
+ 1);
201 fprintf(stderr
, "%s is a block device", fname
);
203 fprintf(stderr
, ", using %s\n", *filename
);
208 static int raw_normalize_devicepath(const char **filename
)
214 static void raw_parse_flags(int bdrv_flags
, int *open_flags
)
216 assert(open_flags
!= NULL
);
218 *open_flags
|= O_BINARY
;
219 *open_flags
&= ~O_ACCMODE
;
220 if (bdrv_flags
& BDRV_O_RDWR
) {
221 *open_flags
|= O_RDWR
;
223 *open_flags
|= O_RDONLY
;
226 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
227 * and O_DIRECT for no caching. */
228 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
229 *open_flags
|= O_DIRECT
;
233 #ifdef CONFIG_LINUX_AIO
234 static int raw_set_aio(void **aio_ctx
, int *use_aio
, int bdrv_flags
)
237 assert(aio_ctx
!= NULL
);
238 assert(use_aio
!= NULL
);
240 * Currently Linux do AIO only for files opened with O_DIRECT
241 * specified so check NOCACHE flag too
243 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
244 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
246 /* if non-NULL, laio_init() has already been run */
247 if (*aio_ctx
== NULL
) {
248 *aio_ctx
= laio_init();
265 static QemuOptsList raw_runtime_opts
= {
267 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
271 .type
= QEMU_OPT_STRING
,
272 .help
= "File name of the image",
274 { /* end of list */ }
278 static int raw_open_common(BlockDriverState
*bs
, QDict
*options
,
279 int bdrv_flags
, int open_flags
)
281 BDRVRawState
*s
= bs
->opaque
;
283 Error
*local_err
= NULL
;
284 const char *filename
;
287 opts
= qemu_opts_create_nofail(&raw_runtime_opts
);
288 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
289 if (error_is_set(&local_err
)) {
290 qerror_report_err(local_err
);
291 error_free(local_err
);
296 filename
= qemu_opt_get(opts
, "filename");
298 ret
= raw_normalize_devicepath(&filename
);
303 s
->open_flags
= open_flags
;
304 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
307 fd
= qemu_open(filename
, s
->open_flags
, 0644);
317 #ifdef CONFIG_LINUX_AIO
318 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
327 if (platform_test_xfs_fd(s
->fd
)) {
338 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
340 BDRVRawState
*s
= bs
->opaque
;
342 s
->type
= FTYPE_FILE
;
343 return raw_open_common(bs
, options
, flags
, 0);
346 static int raw_reopen_prepare(BDRVReopenState
*state
,
347 BlockReopenQueue
*queue
, Error
**errp
)
350 BDRVRawReopenState
*raw_s
;
353 assert(state
!= NULL
);
354 assert(state
->bs
!= NULL
);
356 s
= state
->bs
->opaque
;
358 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
359 raw_s
= state
->opaque
;
361 #ifdef CONFIG_LINUX_AIO
362 raw_s
->use_aio
= s
->use_aio
;
364 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
365 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
366 * won't override aio_ctx if aio_ctx is non-NULL */
367 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
372 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
373 raw_s
->open_flags
|= O_NONBLOCK
;
376 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
380 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
382 fcntl_flags
|= O_NOATIME
;
386 /* Not all operating systems have O_ASYNC, and those that don't
387 * will not let us track the state into raw_s->open_flags (typically
388 * you achieve the same effect with an ioctl, for example I_SETSIG
389 * on Solaris). But we do not use O_ASYNC, so that's fine.
391 assert((s
->open_flags
& O_ASYNC
) == 0);
394 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
395 /* dup the original fd */
396 /* TODO: use qemu fcntl wrapper */
397 #ifdef F_DUPFD_CLOEXEC
398 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
400 raw_s
->fd
= dup(s
->fd
);
401 if (raw_s
->fd
!= -1) {
402 qemu_set_cloexec(raw_s
->fd
);
405 if (raw_s
->fd
>= 0) {
406 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
408 qemu_close(raw_s
->fd
);
414 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
415 if (raw_s
->fd
== -1) {
416 assert(!(raw_s
->open_flags
& O_CREAT
));
417 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
418 if (raw_s
->fd
== -1) {
426 static void raw_reopen_commit(BDRVReopenState
*state
)
428 BDRVRawReopenState
*raw_s
= state
->opaque
;
429 BDRVRawState
*s
= state
->bs
->opaque
;
431 s
->open_flags
= raw_s
->open_flags
;
435 #ifdef CONFIG_LINUX_AIO
436 s
->use_aio
= raw_s
->use_aio
;
439 g_free(state
->opaque
);
440 state
->opaque
= NULL
;
444 static void raw_reopen_abort(BDRVReopenState
*state
)
446 BDRVRawReopenState
*raw_s
= state
->opaque
;
448 /* nothing to do if NULL, we didn't get far enough */
453 if (raw_s
->fd
>= 0) {
454 qemu_close(raw_s
->fd
);
457 g_free(state
->opaque
);
458 state
->opaque
= NULL
;
462 /* XXX: use host sector size if necessary with:
463 #ifdef DIOCGSECTORSIZE
465 unsigned int sectorsize = 512;
466 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
467 sectorsize > bufsize)
468 bufsize = sectorsize;
472 uint32_t blockSize = 512;
473 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
479 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
483 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
491 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
495 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
504 static bool preadv_present
= true;
507 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
509 return preadv(fd
, iov
, nr_iov
, offset
);
513 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
515 return pwritev(fd
, iov
, nr_iov
, offset
);
520 static bool preadv_present
= false;
523 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
529 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
536 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
541 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
542 len
= qemu_pwritev(aiocb
->aio_fildes
,
547 len
= qemu_preadv(aiocb
->aio_fildes
,
551 } while (len
== -1 && errno
== EINTR
);
560 * Read/writes the data to/from a given linear buffer.
562 * Returns the number of bytes handles or -errno in case of an error. Short
563 * reads are only returned if the end of the file is reached.
565 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
570 while (offset
< aiocb
->aio_nbytes
) {
571 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
572 len
= pwrite(aiocb
->aio_fildes
,
573 (const char *)buf
+ offset
,
574 aiocb
->aio_nbytes
- offset
,
575 aiocb
->aio_offset
+ offset
);
577 len
= pread(aiocb
->aio_fildes
,
579 aiocb
->aio_nbytes
- offset
,
580 aiocb
->aio_offset
+ offset
);
582 if (len
== -1 && errno
== EINTR
) {
584 } else if (len
== -1) {
587 } else if (len
== 0) {
596 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
601 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
603 * If there is just a single buffer, and it is properly aligned
604 * we can just use plain pread/pwrite without any problems.
606 if (aiocb
->aio_niov
== 1) {
607 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
610 * We have more than one iovec, and all are properly aligned.
612 * Try preadv/pwritev first and fall back to linearizing the
613 * buffer if it's not supported.
615 if (preadv_present
) {
616 nbytes
= handle_aiocb_rw_vector(aiocb
);
617 if (nbytes
== aiocb
->aio_nbytes
||
618 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
621 preadv_present
= false;
625 * XXX(hch): short read/write. no easy way to handle the reminder
626 * using these interfaces. For now retry using plain
632 * Ok, we have to do it the hard way, copy all segments into
633 * a single aligned buffer.
635 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
636 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
640 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
641 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
642 p
+= aiocb
->aio_iov
[i
].iov_len
;
646 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
647 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
649 size_t count
= aiocb
->aio_nbytes
, copy
;
652 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
654 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
655 copy
= aiocb
->aio_iov
[i
].iov_len
;
657 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
668 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
670 struct xfs_flock64 fl
;
672 memset(&fl
, 0, sizeof(fl
));
673 fl
.l_whence
= SEEK_SET
;
677 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
678 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
686 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
688 int ret
= -EOPNOTSUPP
;
689 BDRVRawState
*s
= aiocb
->bs
->opaque
;
691 if (s
->has_discard
== 0) {
695 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
698 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
699 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
702 } while (errno
== EINTR
);
709 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
713 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
715 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
716 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
719 } while (errno
== EINTR
);
725 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
733 static int aio_worker(void *arg
)
735 RawPosixAIOData
*aiocb
= arg
;
738 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
740 ret
= handle_aiocb_rw(aiocb
);
741 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
742 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
743 0, aiocb
->aio_nbytes
- ret
);
745 ret
= aiocb
->aio_nbytes
;
747 if (ret
== aiocb
->aio_nbytes
) {
749 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
754 ret
= handle_aiocb_rw(aiocb
);
755 if (ret
== aiocb
->aio_nbytes
) {
757 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
762 ret
= handle_aiocb_flush(aiocb
);
765 ret
= handle_aiocb_ioctl(aiocb
);
767 case QEMU_AIO_DISCARD
:
768 ret
= handle_aiocb_discard(aiocb
);
771 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
776 g_slice_free(RawPosixAIOData
, aiocb
);
780 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
781 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
782 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
784 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
788 acb
->aio_type
= type
;
789 acb
->aio_fildes
= fd
;
792 acb
->aio_iov
= qiov
->iov
;
793 acb
->aio_niov
= qiov
->niov
;
795 acb
->aio_nbytes
= nb_sectors
* 512;
796 acb
->aio_offset
= sector_num
* 512;
798 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
799 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
800 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
803 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
804 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
805 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
807 BDRVRawState
*s
= bs
->opaque
;
813 * If O_DIRECT is used the buffer needs to be aligned on a sector
814 * boundary. Check if this is the case or tell the low-level
815 * driver that it needs to copy the buffer.
817 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
818 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
819 type
|= QEMU_AIO_MISALIGNED
;
820 #ifdef CONFIG_LINUX_AIO
821 } else if (s
->use_aio
) {
822 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
823 nb_sectors
, cb
, opaque
, type
);
828 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
832 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
833 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
834 BlockDriverCompletionFunc
*cb
, void *opaque
)
836 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
837 cb
, opaque
, QEMU_AIO_READ
);
840 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
841 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
842 BlockDriverCompletionFunc
*cb
, void *opaque
)
844 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
845 cb
, opaque
, QEMU_AIO_WRITE
);
848 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
849 BlockDriverCompletionFunc
*cb
, void *opaque
)
851 BDRVRawState
*s
= bs
->opaque
;
856 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
859 static void raw_close(BlockDriverState
*bs
)
861 BDRVRawState
*s
= bs
->opaque
;
868 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
870 BDRVRawState
*s
= bs
->opaque
;
873 if (fstat(s
->fd
, &st
)) {
877 if (S_ISREG(st
.st_mode
)) {
878 if (ftruncate(s
->fd
, offset
) < 0) {
881 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
882 if (offset
> raw_getlength(bs
)) {
893 static int64_t raw_getlength(BlockDriverState
*bs
)
895 BDRVRawState
*s
= bs
->opaque
;
901 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
904 if (ioctl(fd
, DIOCGDINFO
, &dl
))
906 return (uint64_t)dl
.d_secsize
*
907 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
911 #elif defined(__NetBSD__)
912 static int64_t raw_getlength(BlockDriverState
*bs
)
914 BDRVRawState
*s
= bs
->opaque
;
920 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
921 struct dkwedge_info dkw
;
923 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
924 return dkw
.dkw_size
* 512;
928 if (ioctl(fd
, DIOCGDINFO
, &dl
))
930 return (uint64_t)dl
.d_secsize
*
931 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
936 #elif defined(__sun__)
937 static int64_t raw_getlength(BlockDriverState
*bs
)
939 BDRVRawState
*s
= bs
->opaque
;
940 struct dk_minfo minfo
;
949 * Use the DKIOCGMEDIAINFO ioctl to read the size.
951 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
953 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
957 * There are reports that lseek on some devices fails, but
958 * irc discussion said that contingency on contingency was overkill.
960 return lseek(s
->fd
, 0, SEEK_END
);
962 #elif defined(CONFIG_BSD)
963 static int64_t raw_getlength(BlockDriverState
*bs
)
965 BDRVRawState
*s
= bs
->opaque
;
969 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
978 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
981 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
982 #ifdef DIOCGMEDIASIZE
983 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
984 #elif defined(DIOCGPART)
987 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
988 size
= pi
.media_size
;
994 #if defined(__APPLE__) && defined(__MACH__)
995 size
= LONG_LONG_MAX
;
997 size
= lseek(fd
, 0LL, SEEK_END
);
999 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1002 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1003 if (size
== 2048LL * (unsigned)-1)
1005 /* XXX no disc? maybe we need to reopen... */
1006 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
1013 size
= lseek(fd
, 0, SEEK_END
);
1018 static int64_t raw_getlength(BlockDriverState
*bs
)
1020 BDRVRawState
*s
= bs
->opaque
;
1028 return lseek(s
->fd
, 0, SEEK_END
);
1032 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1035 BDRVRawState
*s
= bs
->opaque
;
1037 if (fstat(s
->fd
, &st
) < 0) {
1040 return (int64_t)st
.st_blocks
* 512;
1043 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
1047 int64_t total_size
= 0;
1049 /* Read out options */
1050 while (options
&& options
->name
) {
1051 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1052 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1057 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1062 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1065 if (qemu_close(fd
) != 0) {
1073 * Returns true iff the specified sector is present in the disk image. Drivers
1074 * not implementing the functionality are assumed to not support backing files,
1075 * hence all their sectors are reported as allocated.
1077 * If 'sector_num' is beyond the end of the disk image the return value is 0
1078 * and 'pnum' is set to 0.
1080 * 'pnum' is set to the number of sectors (including and immediately following
1081 * the specified sector) that are known to be in the same
1082 * allocated/unallocated state.
1084 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1085 * beyond the end of the disk image it will be clamped.
1087 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
1089 int nb_sectors
, int *pnum
)
1091 off_t start
, data
, hole
;
1099 start
= sector_num
* BDRV_SECTOR_SIZE
;
1101 #ifdef CONFIG_FIEMAP
1103 BDRVRawState
*s
= bs
->opaque
;
1106 struct fiemap_extent fe
;
1109 f
.fm
.fm_start
= start
;
1110 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1112 f
.fm
.fm_extent_count
= 1;
1113 f
.fm
.fm_reserved
= 0;
1114 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1115 /* Assume everything is allocated. */
1120 if (f
.fm
.fm_mapped_extents
== 0) {
1121 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1122 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1124 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1125 hole
= f
.fm
.fm_start
;
1126 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1128 data
= f
.fe
.fe_logical
;
1129 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1132 #elif defined SEEK_HOLE && defined SEEK_DATA
1134 BDRVRawState
*s
= bs
->opaque
;
1136 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1138 /* -ENXIO indicates that sector_num was past the end of the file.
1139 * There is a virtual hole there. */
1140 assert(errno
!= -ENXIO
);
1142 /* Most likely EINVAL. Assume everything is allocated. */
1150 /* On a hole. We need another syscall to find its end. */
1151 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1153 data
= lseek(s
->fd
, 0, SEEK_END
);
1161 if (data
<= start
) {
1162 /* On a data extent, compute sectors to the end of the extent. */
1163 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1166 /* On a hole, compute sectors to the beginning of the next extent. */
1167 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1172 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1173 int64_t sector_num
, int nb_sectors
,
1174 BlockDriverCompletionFunc
*cb
, void *opaque
)
1176 BDRVRawState
*s
= bs
->opaque
;
1178 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1179 cb
, opaque
, QEMU_AIO_DISCARD
);
1182 static QEMUOptionParameter raw_create_options
[] = {
1184 .name
= BLOCK_OPT_SIZE
,
1186 .help
= "Virtual disk size"
1191 static BlockDriver bdrv_file
= {
1192 .format_name
= "file",
1193 .protocol_name
= "file",
1194 .instance_size
= sizeof(BDRVRawState
),
1195 .bdrv_probe
= NULL
, /* no probe for protocols */
1196 .bdrv_file_open
= raw_open
,
1197 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1198 .bdrv_reopen_commit
= raw_reopen_commit
,
1199 .bdrv_reopen_abort
= raw_reopen_abort
,
1200 .bdrv_close
= raw_close
,
1201 .bdrv_create
= raw_create
,
1202 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1203 .bdrv_co_is_allocated
= raw_co_is_allocated
,
1205 .bdrv_aio_readv
= raw_aio_readv
,
1206 .bdrv_aio_writev
= raw_aio_writev
,
1207 .bdrv_aio_flush
= raw_aio_flush
,
1208 .bdrv_aio_discard
= raw_aio_discard
,
1210 .bdrv_truncate
= raw_truncate
,
1211 .bdrv_getlength
= raw_getlength
,
1212 .bdrv_get_allocated_file_size
1213 = raw_get_allocated_file_size
,
1215 .create_options
= raw_create_options
,
1218 /***********************************************/
1221 #if defined(__APPLE__) && defined(__MACH__)
1222 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1223 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1225 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1227 kern_return_t kernResult
;
1228 mach_port_t masterPort
;
1229 CFMutableDictionaryRef classesToMatch
;
1231 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1232 if ( KERN_SUCCESS
!= kernResult
) {
1233 printf( "IOMasterPort returned %d\n", kernResult
);
1236 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1237 if ( classesToMatch
== NULL
) {
1238 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1240 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1242 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1243 if ( KERN_SUCCESS
!= kernResult
)
1245 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1251 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1253 io_object_t nextMedia
;
1254 kern_return_t kernResult
= KERN_FAILURE
;
1256 nextMedia
= IOIteratorNext( mediaIterator
);
1259 CFTypeRef bsdPathAsCFString
;
1260 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1261 if ( bsdPathAsCFString
) {
1262 size_t devPathLength
;
1263 strcpy( bsdPath
, _PATH_DEV
);
1264 strcat( bsdPath
, "r" );
1265 devPathLength
= strlen( bsdPath
);
1266 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1267 kernResult
= KERN_SUCCESS
;
1269 CFRelease( bsdPathAsCFString
);
1271 IOObjectRelease( nextMedia
);
1279 static int hdev_probe_device(const char *filename
)
1283 /* allow a dedicated CD-ROM driver to match with a higher priority */
1284 if (strstart(filename
, "/dev/cdrom", NULL
))
1287 if (stat(filename
, &st
) >= 0 &&
1288 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1295 static int check_hdev_writable(BDRVRawState
*s
)
1297 #if defined(BLKROGET)
1298 /* Linux block devices can be configured "read-only" using blockdev(8).
1299 * This is independent of device node permissions and therefore open(2)
1300 * with O_RDWR succeeds. Actual writes fail with EPERM.
1302 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1303 * check for read-only block devices so that Linux block devices behave
1309 if (fstat(s
->fd
, &st
)) {
1313 if (!S_ISBLK(st
.st_mode
)) {
1317 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1324 #endif /* defined(BLKROGET) */
1328 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1330 BDRVRawState
*s
= bs
->opaque
;
1332 const char *filename
= qdict_get_str(options
, "filename");
1334 #if defined(__APPLE__) && defined(__MACH__)
1335 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1336 kern_return_t kernResult
;
1337 io_iterator_t mediaIterator
;
1338 char bsdPath
[ MAXPATHLEN
];
1341 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1342 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1344 if ( bsdPath
[ 0 ] != '\0' ) {
1345 strcat(bsdPath
,"s0");
1346 /* some CDs don't have a partition 0 */
1347 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1349 bsdPath
[strlen(bsdPath
)-1] = '1';
1354 qdict_put(options
, "filename", qstring_from_str(filename
));
1357 if ( mediaIterator
)
1358 IOObjectRelease( mediaIterator
);
1362 s
->type
= FTYPE_FILE
;
1363 #if defined(__linux__)
1365 char resolved_path
[ MAXPATHLEN
], *temp
;
1367 temp
= realpath(filename
, resolved_path
);
1368 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1374 ret
= raw_open_common(bs
, options
, flags
, 0);
1379 if (flags
& BDRV_O_RDWR
) {
1380 ret
= check_hdev_writable(s
);
1390 #if defined(__linux__)
1391 /* Note: we do not have a reliable method to detect if the floppy is
1392 present. The current method is to try to open the floppy at every
1393 I/O and to keep it opened during a few hundreds of ms. */
1394 static int fd_open(BlockDriverState
*bs
)
1396 BDRVRawState
*s
= bs
->opaque
;
1397 int last_media_present
;
1399 if (s
->type
!= FTYPE_FD
)
1401 last_media_present
= (s
->fd
>= 0);
1403 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1407 printf("Floppy closed\n");
1411 if (s
->fd_got_error
&&
1412 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1414 printf("No floppy (open delayed)\n");
1418 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1420 s
->fd_error_time
= get_clock();
1421 s
->fd_got_error
= 1;
1422 if (last_media_present
)
1423 s
->fd_media_changed
= 1;
1425 printf("No floppy\n");
1430 printf("Floppy opened\n");
1433 if (!last_media_present
)
1434 s
->fd_media_changed
= 1;
1435 s
->fd_open_time
= get_clock();
1436 s
->fd_got_error
= 0;
1440 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1442 BDRVRawState
*s
= bs
->opaque
;
1444 return ioctl(s
->fd
, req
, buf
);
1447 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1448 unsigned long int req
, void *buf
,
1449 BlockDriverCompletionFunc
*cb
, void *opaque
)
1451 BDRVRawState
*s
= bs
->opaque
;
1452 RawPosixAIOData
*acb
;
1455 if (fd_open(bs
) < 0)
1458 acb
= g_slice_new(RawPosixAIOData
);
1460 acb
->aio_type
= QEMU_AIO_IOCTL
;
1461 acb
->aio_fildes
= s
->fd
;
1462 acb
->aio_offset
= 0;
1463 acb
->aio_ioctl_buf
= buf
;
1464 acb
->aio_ioctl_cmd
= req
;
1465 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1466 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1469 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1470 static int fd_open(BlockDriverState
*bs
)
1472 BDRVRawState
*s
= bs
->opaque
;
1474 /* this is just to ensure s->fd is sane (its called by io ops) */
1479 #else /* !linux && !FreeBSD */
1481 static int fd_open(BlockDriverState
*bs
)
1486 #endif /* !linux && !FreeBSD */
1488 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1489 int64_t sector_num
, int nb_sectors
,
1490 BlockDriverCompletionFunc
*cb
, void *opaque
)
1492 BDRVRawState
*s
= bs
->opaque
;
1494 if (fd_open(bs
) < 0) {
1497 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1498 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1501 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1505 struct stat stat_buf
;
1506 int64_t total_size
= 0;
1508 /* Read out options */
1509 while (options
&& options
->name
) {
1510 if (!strcmp(options
->name
, "size")) {
1511 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1516 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1520 if (fstat(fd
, &stat_buf
) < 0)
1522 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1524 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1531 static BlockDriver bdrv_host_device
= {
1532 .format_name
= "host_device",
1533 .protocol_name
= "host_device",
1534 .instance_size
= sizeof(BDRVRawState
),
1535 .bdrv_probe_device
= hdev_probe_device
,
1536 .bdrv_file_open
= hdev_open
,
1537 .bdrv_close
= raw_close
,
1538 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1539 .bdrv_reopen_commit
= raw_reopen_commit
,
1540 .bdrv_reopen_abort
= raw_reopen_abort
,
1541 .bdrv_create
= hdev_create
,
1542 .create_options
= raw_create_options
,
1544 .bdrv_aio_readv
= raw_aio_readv
,
1545 .bdrv_aio_writev
= raw_aio_writev
,
1546 .bdrv_aio_flush
= raw_aio_flush
,
1547 .bdrv_aio_discard
= hdev_aio_discard
,
1549 .bdrv_truncate
= raw_truncate
,
1550 .bdrv_getlength
= raw_getlength
,
1551 .bdrv_get_allocated_file_size
1552 = raw_get_allocated_file_size
,
1554 /* generic scsi device */
1556 .bdrv_ioctl
= hdev_ioctl
,
1557 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1562 static int floppy_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1564 BDRVRawState
*s
= bs
->opaque
;
1569 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1570 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
);
1574 /* close fd so that we can reopen it as needed */
1577 s
->fd_media_changed
= 1;
1582 static int floppy_probe_device(const char *filename
)
1586 struct floppy_struct fdparam
;
1589 if (strstart(filename
, "/dev/fd", NULL
) &&
1590 !strstart(filename
, "/dev/fdset/", NULL
)) {
1594 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1598 ret
= fstat(fd
, &st
);
1599 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1603 /* Attempt to detect via a floppy specific ioctl */
1604 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1615 static int floppy_is_inserted(BlockDriverState
*bs
)
1617 return fd_open(bs
) >= 0;
1620 static int floppy_media_changed(BlockDriverState
*bs
)
1622 BDRVRawState
*s
= bs
->opaque
;
1626 * XXX: we do not have a true media changed indication.
1627 * It does not work if the floppy is changed without trying to read it.
1630 ret
= s
->fd_media_changed
;
1631 s
->fd_media_changed
= 0;
1633 printf("Floppy changed=%d\n", ret
);
1638 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1640 BDRVRawState
*s
= bs
->opaque
;
1647 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1649 if (ioctl(fd
, FDEJECT
, 0) < 0)
1655 static BlockDriver bdrv_host_floppy
= {
1656 .format_name
= "host_floppy",
1657 .protocol_name
= "host_floppy",
1658 .instance_size
= sizeof(BDRVRawState
),
1659 .bdrv_probe_device
= floppy_probe_device
,
1660 .bdrv_file_open
= floppy_open
,
1661 .bdrv_close
= raw_close
,
1662 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1663 .bdrv_reopen_commit
= raw_reopen_commit
,
1664 .bdrv_reopen_abort
= raw_reopen_abort
,
1665 .bdrv_create
= hdev_create
,
1666 .create_options
= raw_create_options
,
1668 .bdrv_aio_readv
= raw_aio_readv
,
1669 .bdrv_aio_writev
= raw_aio_writev
,
1670 .bdrv_aio_flush
= raw_aio_flush
,
1672 .bdrv_truncate
= raw_truncate
,
1673 .bdrv_getlength
= raw_getlength
,
1674 .bdrv_get_allocated_file_size
1675 = raw_get_allocated_file_size
,
1677 /* removable device support */
1678 .bdrv_is_inserted
= floppy_is_inserted
,
1679 .bdrv_media_changed
= floppy_media_changed
,
1680 .bdrv_eject
= floppy_eject
,
1683 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1685 BDRVRawState
*s
= bs
->opaque
;
1689 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1690 return raw_open_common(bs
, options
, flags
, O_NONBLOCK
);
1693 static int cdrom_probe_device(const char *filename
)
1699 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1703 ret
= fstat(fd
, &st
);
1704 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1708 /* Attempt to detect via a CDROM specific ioctl */
1709 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1719 static int cdrom_is_inserted(BlockDriverState
*bs
)
1721 BDRVRawState
*s
= bs
->opaque
;
1724 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1725 if (ret
== CDS_DISC_OK
)
1730 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1732 BDRVRawState
*s
= bs
->opaque
;
1735 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1736 perror("CDROMEJECT");
1738 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1739 perror("CDROMEJECT");
1743 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1745 BDRVRawState
*s
= bs
->opaque
;
1747 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1749 * Note: an error can happen if the distribution automatically
1752 /* perror("CDROM_LOCKDOOR"); */
1756 static BlockDriver bdrv_host_cdrom
= {
1757 .format_name
= "host_cdrom",
1758 .protocol_name
= "host_cdrom",
1759 .instance_size
= sizeof(BDRVRawState
),
1760 .bdrv_probe_device
= cdrom_probe_device
,
1761 .bdrv_file_open
= cdrom_open
,
1762 .bdrv_close
= raw_close
,
1763 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1764 .bdrv_reopen_commit
= raw_reopen_commit
,
1765 .bdrv_reopen_abort
= raw_reopen_abort
,
1766 .bdrv_create
= hdev_create
,
1767 .create_options
= raw_create_options
,
1769 .bdrv_aio_readv
= raw_aio_readv
,
1770 .bdrv_aio_writev
= raw_aio_writev
,
1771 .bdrv_aio_flush
= raw_aio_flush
,
1773 .bdrv_truncate
= raw_truncate
,
1774 .bdrv_getlength
= raw_getlength
,
1775 .bdrv_get_allocated_file_size
1776 = raw_get_allocated_file_size
,
1778 /* removable device support */
1779 .bdrv_is_inserted
= cdrom_is_inserted
,
1780 .bdrv_eject
= cdrom_eject
,
1781 .bdrv_lock_medium
= cdrom_lock_medium
,
1783 /* generic scsi device */
1784 .bdrv_ioctl
= hdev_ioctl
,
1785 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1787 #endif /* __linux__ */
1789 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1790 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1792 BDRVRawState
*s
= bs
->opaque
;
1797 ret
= raw_open_common(bs
, options
, flags
, 0);
1801 /* make sure the door isn't locked at this time */
1802 ioctl(s
->fd
, CDIOCALLOW
);
1806 static int cdrom_probe_device(const char *filename
)
1808 if (strstart(filename
, "/dev/cd", NULL
) ||
1809 strstart(filename
, "/dev/acd", NULL
))
1814 static int cdrom_reopen(BlockDriverState
*bs
)
1816 BDRVRawState
*s
= bs
->opaque
;
1820 * Force reread of possibly changed/newly loaded disc,
1821 * FreeBSD seems to not notice sometimes...
1825 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1832 /* make sure the door isn't locked at this time */
1833 ioctl(s
->fd
, CDIOCALLOW
);
1837 static int cdrom_is_inserted(BlockDriverState
*bs
)
1839 return raw_getlength(bs
) > 0;
1842 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1844 BDRVRawState
*s
= bs
->opaque
;
1849 (void) ioctl(s
->fd
, CDIOCALLOW
);
1852 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1853 perror("CDIOCEJECT");
1855 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1856 perror("CDIOCCLOSE");
1862 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1864 BDRVRawState
*s
= bs
->opaque
;
1868 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1870 * Note: an error can happen if the distribution automatically
1873 /* perror("CDROM_LOCKDOOR"); */
1877 static BlockDriver bdrv_host_cdrom
= {
1878 .format_name
= "host_cdrom",
1879 .protocol_name
= "host_cdrom",
1880 .instance_size
= sizeof(BDRVRawState
),
1881 .bdrv_probe_device
= cdrom_probe_device
,
1882 .bdrv_file_open
= cdrom_open
,
1883 .bdrv_close
= raw_close
,
1884 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1885 .bdrv_reopen_commit
= raw_reopen_commit
,
1886 .bdrv_reopen_abort
= raw_reopen_abort
,
1887 .bdrv_create
= hdev_create
,
1888 .create_options
= raw_create_options
,
1890 .bdrv_aio_readv
= raw_aio_readv
,
1891 .bdrv_aio_writev
= raw_aio_writev
,
1892 .bdrv_aio_flush
= raw_aio_flush
,
1894 .bdrv_truncate
= raw_truncate
,
1895 .bdrv_getlength
= raw_getlength
,
1896 .bdrv_get_allocated_file_size
1897 = raw_get_allocated_file_size
,
1899 /* removable device support */
1900 .bdrv_is_inserted
= cdrom_is_inserted
,
1901 .bdrv_eject
= cdrom_eject
,
1902 .bdrv_lock_medium
= cdrom_lock_medium
,
1904 #endif /* __FreeBSD__ */
1906 #ifdef CONFIG_LINUX_AIO
1908 * Return the file descriptor for Linux AIO
1910 * This function is a layering violation and should be removed when it becomes
1911 * possible to call the block layer outside the global mutex. It allows the
1912 * caller to hijack the file descriptor so I/O can be performed outside the
1915 int raw_get_aio_fd(BlockDriverState
*bs
)
1923 if (bs
->drv
== bdrv_find_format("raw")) {
1927 /* raw-posix has several protocols so just check for raw_aio_readv */
1928 if (bs
->drv
->bdrv_aio_readv
!= raw_aio_readv
) {
1938 #endif /* CONFIG_LINUX_AIO */
1940 static void bdrv_file_init(void)
1943 * Register all the drivers. Note that order is important, the driver
1944 * registered last will get probed first.
1946 bdrv_register(&bdrv_file
);
1947 bdrv_register(&bdrv_host_device
);
1949 bdrv_register(&bdrv_host_floppy
);
1950 bdrv_register(&bdrv_host_cdrom
);
1952 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1953 bdrv_register(&bdrv_host_cdrom
);
1957 block_init(bdrv_file_init
);