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
,
341 BDRVRawState
*s
= bs
->opaque
;
343 s
->type
= FTYPE_FILE
;
344 return raw_open_common(bs
, options
, flags
, 0);
347 static int raw_reopen_prepare(BDRVReopenState
*state
,
348 BlockReopenQueue
*queue
, Error
**errp
)
351 BDRVRawReopenState
*raw_s
;
354 assert(state
!= NULL
);
355 assert(state
->bs
!= NULL
);
357 s
= state
->bs
->opaque
;
359 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
360 raw_s
= state
->opaque
;
362 #ifdef CONFIG_LINUX_AIO
363 raw_s
->use_aio
= s
->use_aio
;
365 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
366 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
367 * won't override aio_ctx if aio_ctx is non-NULL */
368 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
373 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
374 raw_s
->open_flags
|= O_NONBLOCK
;
377 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
381 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
383 fcntl_flags
|= O_NOATIME
;
387 /* Not all operating systems have O_ASYNC, and those that don't
388 * will not let us track the state into raw_s->open_flags (typically
389 * you achieve the same effect with an ioctl, for example I_SETSIG
390 * on Solaris). But we do not use O_ASYNC, so that's fine.
392 assert((s
->open_flags
& O_ASYNC
) == 0);
395 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
396 /* dup the original fd */
397 /* TODO: use qemu fcntl wrapper */
398 #ifdef F_DUPFD_CLOEXEC
399 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
401 raw_s
->fd
= dup(s
->fd
);
402 if (raw_s
->fd
!= -1) {
403 qemu_set_cloexec(raw_s
->fd
);
406 if (raw_s
->fd
>= 0) {
407 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
409 qemu_close(raw_s
->fd
);
415 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
416 if (raw_s
->fd
== -1) {
417 assert(!(raw_s
->open_flags
& O_CREAT
));
418 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
419 if (raw_s
->fd
== -1) {
427 static void raw_reopen_commit(BDRVReopenState
*state
)
429 BDRVRawReopenState
*raw_s
= state
->opaque
;
430 BDRVRawState
*s
= state
->bs
->opaque
;
432 s
->open_flags
= raw_s
->open_flags
;
436 #ifdef CONFIG_LINUX_AIO
437 s
->use_aio
= raw_s
->use_aio
;
440 g_free(state
->opaque
);
441 state
->opaque
= NULL
;
445 static void raw_reopen_abort(BDRVReopenState
*state
)
447 BDRVRawReopenState
*raw_s
= state
->opaque
;
449 /* nothing to do if NULL, we didn't get far enough */
454 if (raw_s
->fd
>= 0) {
455 qemu_close(raw_s
->fd
);
458 g_free(state
->opaque
);
459 state
->opaque
= NULL
;
463 /* XXX: use host sector size if necessary with:
464 #ifdef DIOCGSECTORSIZE
466 unsigned int sectorsize = 512;
467 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
468 sectorsize > bufsize)
469 bufsize = sectorsize;
473 uint32_t blockSize = 512;
474 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
480 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
484 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
492 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
496 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
505 static bool preadv_present
= true;
508 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
510 return preadv(fd
, iov
, nr_iov
, offset
);
514 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
516 return pwritev(fd
, iov
, nr_iov
, offset
);
521 static bool preadv_present
= false;
524 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
530 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
537 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
542 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
543 len
= qemu_pwritev(aiocb
->aio_fildes
,
548 len
= qemu_preadv(aiocb
->aio_fildes
,
552 } while (len
== -1 && errno
== EINTR
);
561 * Read/writes the data to/from a given linear buffer.
563 * Returns the number of bytes handles or -errno in case of an error. Short
564 * reads are only returned if the end of the file is reached.
566 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
571 while (offset
< aiocb
->aio_nbytes
) {
572 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
573 len
= pwrite(aiocb
->aio_fildes
,
574 (const char *)buf
+ offset
,
575 aiocb
->aio_nbytes
- offset
,
576 aiocb
->aio_offset
+ offset
);
578 len
= pread(aiocb
->aio_fildes
,
580 aiocb
->aio_nbytes
- offset
,
581 aiocb
->aio_offset
+ offset
);
583 if (len
== -1 && errno
== EINTR
) {
585 } else if (len
== -1) {
588 } else if (len
== 0) {
597 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
602 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
604 * If there is just a single buffer, and it is properly aligned
605 * we can just use plain pread/pwrite without any problems.
607 if (aiocb
->aio_niov
== 1) {
608 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
611 * We have more than one iovec, and all are properly aligned.
613 * Try preadv/pwritev first and fall back to linearizing the
614 * buffer if it's not supported.
616 if (preadv_present
) {
617 nbytes
= handle_aiocb_rw_vector(aiocb
);
618 if (nbytes
== aiocb
->aio_nbytes
||
619 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
622 preadv_present
= false;
626 * XXX(hch): short read/write. no easy way to handle the reminder
627 * using these interfaces. For now retry using plain
633 * Ok, we have to do it the hard way, copy all segments into
634 * a single aligned buffer.
636 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
637 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
641 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
642 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
643 p
+= aiocb
->aio_iov
[i
].iov_len
;
647 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
648 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
650 size_t count
= aiocb
->aio_nbytes
, copy
;
653 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
655 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
656 copy
= aiocb
->aio_iov
[i
].iov_len
;
658 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
669 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
671 struct xfs_flock64 fl
;
673 memset(&fl
, 0, sizeof(fl
));
674 fl
.l_whence
= SEEK_SET
;
678 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
679 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
687 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
689 int ret
= -EOPNOTSUPP
;
690 BDRVRawState
*s
= aiocb
->bs
->opaque
;
692 if (s
->has_discard
== 0) {
696 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
699 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
700 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
703 } while (errno
== EINTR
);
710 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
714 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
716 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
717 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
720 } while (errno
== EINTR
);
726 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
734 static int aio_worker(void *arg
)
736 RawPosixAIOData
*aiocb
= arg
;
739 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
741 ret
= handle_aiocb_rw(aiocb
);
742 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
743 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
744 0, aiocb
->aio_nbytes
- ret
);
746 ret
= aiocb
->aio_nbytes
;
748 if (ret
== aiocb
->aio_nbytes
) {
750 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
755 ret
= handle_aiocb_rw(aiocb
);
756 if (ret
== aiocb
->aio_nbytes
) {
758 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
763 ret
= handle_aiocb_flush(aiocb
);
766 ret
= handle_aiocb_ioctl(aiocb
);
768 case QEMU_AIO_DISCARD
:
769 ret
= handle_aiocb_discard(aiocb
);
772 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
777 g_slice_free(RawPosixAIOData
, aiocb
);
781 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
782 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
783 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
785 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
789 acb
->aio_type
= type
;
790 acb
->aio_fildes
= fd
;
793 acb
->aio_iov
= qiov
->iov
;
794 acb
->aio_niov
= qiov
->niov
;
796 acb
->aio_nbytes
= nb_sectors
* 512;
797 acb
->aio_offset
= sector_num
* 512;
799 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
800 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
801 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
804 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
805 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
806 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
808 BDRVRawState
*s
= bs
->opaque
;
814 * If O_DIRECT is used the buffer needs to be aligned on a sector
815 * boundary. Check if this is the case or tell the low-level
816 * driver that it needs to copy the buffer.
818 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
819 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
820 type
|= QEMU_AIO_MISALIGNED
;
821 #ifdef CONFIG_LINUX_AIO
822 } else if (s
->use_aio
) {
823 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
824 nb_sectors
, cb
, opaque
, type
);
829 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
833 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
834 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
835 BlockDriverCompletionFunc
*cb
, void *opaque
)
837 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
838 cb
, opaque
, QEMU_AIO_READ
);
841 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
842 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
843 BlockDriverCompletionFunc
*cb
, void *opaque
)
845 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
846 cb
, opaque
, QEMU_AIO_WRITE
);
849 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
850 BlockDriverCompletionFunc
*cb
, void *opaque
)
852 BDRVRawState
*s
= bs
->opaque
;
857 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
860 static void raw_close(BlockDriverState
*bs
)
862 BDRVRawState
*s
= bs
->opaque
;
869 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
871 BDRVRawState
*s
= bs
->opaque
;
874 if (fstat(s
->fd
, &st
)) {
878 if (S_ISREG(st
.st_mode
)) {
879 if (ftruncate(s
->fd
, offset
) < 0) {
882 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
883 if (offset
> raw_getlength(bs
)) {
894 static int64_t raw_getlength(BlockDriverState
*bs
)
896 BDRVRawState
*s
= bs
->opaque
;
902 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
905 if (ioctl(fd
, DIOCGDINFO
, &dl
))
907 return (uint64_t)dl
.d_secsize
*
908 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
912 #elif defined(__NetBSD__)
913 static int64_t raw_getlength(BlockDriverState
*bs
)
915 BDRVRawState
*s
= bs
->opaque
;
921 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
922 struct dkwedge_info dkw
;
924 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
925 return dkw
.dkw_size
* 512;
929 if (ioctl(fd
, DIOCGDINFO
, &dl
))
931 return (uint64_t)dl
.d_secsize
*
932 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
937 #elif defined(__sun__)
938 static int64_t raw_getlength(BlockDriverState
*bs
)
940 BDRVRawState
*s
= bs
->opaque
;
941 struct dk_minfo minfo
;
950 * Use the DKIOCGMEDIAINFO ioctl to read the size.
952 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
954 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
958 * There are reports that lseek on some devices fails, but
959 * irc discussion said that contingency on contingency was overkill.
961 return lseek(s
->fd
, 0, SEEK_END
);
963 #elif defined(CONFIG_BSD)
964 static int64_t raw_getlength(BlockDriverState
*bs
)
966 BDRVRawState
*s
= bs
->opaque
;
970 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
979 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
982 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
983 #ifdef DIOCGMEDIASIZE
984 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
985 #elif defined(DIOCGPART)
988 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
989 size
= pi
.media_size
;
995 #if defined(__APPLE__) && defined(__MACH__)
996 size
= LONG_LONG_MAX
;
998 size
= lseek(fd
, 0LL, SEEK_END
);
1000 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1003 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1004 if (size
== 2048LL * (unsigned)-1)
1006 /* XXX no disc? maybe we need to reopen... */
1007 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
1014 size
= lseek(fd
, 0, SEEK_END
);
1019 static int64_t raw_getlength(BlockDriverState
*bs
)
1021 BDRVRawState
*s
= bs
->opaque
;
1029 return lseek(s
->fd
, 0, SEEK_END
);
1033 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1036 BDRVRawState
*s
= bs
->opaque
;
1038 if (fstat(s
->fd
, &st
) < 0) {
1041 return (int64_t)st
.st_blocks
* 512;
1044 static int raw_create(const char *filename
, QEMUOptionParameter
*options
,
1049 int64_t total_size
= 0;
1051 /* Read out options */
1052 while (options
&& options
->name
) {
1053 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1054 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1059 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1064 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1067 if (qemu_close(fd
) != 0) {
1075 * Returns true iff the specified sector is present in the disk image. Drivers
1076 * not implementing the functionality are assumed to not support backing files,
1077 * hence all their sectors are reported as allocated.
1079 * If 'sector_num' is beyond the end of the disk image the return value is 0
1080 * and 'pnum' is set to 0.
1082 * 'pnum' is set to the number of sectors (including and immediately following
1083 * the specified sector) that are known to be in the same
1084 * allocated/unallocated state.
1086 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1087 * beyond the end of the disk image it will be clamped.
1089 static int64_t coroutine_fn
raw_co_get_block_status(BlockDriverState
*bs
,
1091 int nb_sectors
, int *pnum
)
1093 off_t start
, data
, hole
;
1101 start
= sector_num
* BDRV_SECTOR_SIZE
;
1102 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
1104 #ifdef CONFIG_FIEMAP
1106 BDRVRawState
*s
= bs
->opaque
;
1109 struct fiemap_extent fe
;
1112 f
.fm
.fm_start
= start
;
1113 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1115 f
.fm
.fm_extent_count
= 1;
1116 f
.fm
.fm_reserved
= 0;
1117 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1118 /* Assume everything is allocated. */
1123 if (f
.fm
.fm_mapped_extents
== 0) {
1124 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1125 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1127 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1128 hole
= f
.fm
.fm_start
;
1129 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1131 data
= f
.fe
.fe_logical
;
1132 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1133 if (f
.fe
.fe_flags
& FIEMAP_EXTENT_UNWRITTEN
) {
1134 ret
|= BDRV_BLOCK_ZERO
;
1138 #elif defined SEEK_HOLE && defined SEEK_DATA
1140 BDRVRawState
*s
= bs
->opaque
;
1142 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1144 /* -ENXIO indicates that sector_num was past the end of the file.
1145 * There is a virtual hole there. */
1146 assert(errno
!= -ENXIO
);
1148 /* Most likely EINVAL. Assume everything is allocated. */
1156 /* On a hole. We need another syscall to find its end. */
1157 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1159 data
= lseek(s
->fd
, 0, SEEK_END
);
1164 hole
= start
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1167 if (data
<= start
) {
1168 /* On a data extent, compute sectors to the end of the extent. */
1169 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1171 /* On a hole, compute sectors to the beginning of the next extent. */
1172 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1173 ret
&= ~BDRV_BLOCK_DATA
;
1174 ret
|= BDRV_BLOCK_ZERO
;
1180 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1181 int64_t sector_num
, int nb_sectors
,
1182 BlockDriverCompletionFunc
*cb
, void *opaque
)
1184 BDRVRawState
*s
= bs
->opaque
;
1186 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1187 cb
, opaque
, QEMU_AIO_DISCARD
);
1190 static QEMUOptionParameter raw_create_options
[] = {
1192 .name
= BLOCK_OPT_SIZE
,
1194 .help
= "Virtual disk size"
1199 static BlockDriver bdrv_file
= {
1200 .format_name
= "file",
1201 .protocol_name
= "file",
1202 .instance_size
= sizeof(BDRVRawState
),
1203 .bdrv_needs_filename
= true,
1204 .bdrv_probe
= NULL
, /* no probe for protocols */
1205 .bdrv_file_open
= raw_open
,
1206 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1207 .bdrv_reopen_commit
= raw_reopen_commit
,
1208 .bdrv_reopen_abort
= raw_reopen_abort
,
1209 .bdrv_close
= raw_close
,
1210 .bdrv_create
= raw_create
,
1211 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1212 .bdrv_co_get_block_status
= raw_co_get_block_status
,
1214 .bdrv_aio_readv
= raw_aio_readv
,
1215 .bdrv_aio_writev
= raw_aio_writev
,
1216 .bdrv_aio_flush
= raw_aio_flush
,
1217 .bdrv_aio_discard
= raw_aio_discard
,
1219 .bdrv_truncate
= raw_truncate
,
1220 .bdrv_getlength
= raw_getlength
,
1221 .bdrv_get_allocated_file_size
1222 = raw_get_allocated_file_size
,
1224 .create_options
= raw_create_options
,
1227 /***********************************************/
1230 #if defined(__APPLE__) && defined(__MACH__)
1231 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1232 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1234 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1236 kern_return_t kernResult
;
1237 mach_port_t masterPort
;
1238 CFMutableDictionaryRef classesToMatch
;
1240 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1241 if ( KERN_SUCCESS
!= kernResult
) {
1242 printf( "IOMasterPort returned %d\n", kernResult
);
1245 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1246 if ( classesToMatch
== NULL
) {
1247 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1249 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1251 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1252 if ( KERN_SUCCESS
!= kernResult
)
1254 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1260 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1262 io_object_t nextMedia
;
1263 kern_return_t kernResult
= KERN_FAILURE
;
1265 nextMedia
= IOIteratorNext( mediaIterator
);
1268 CFTypeRef bsdPathAsCFString
;
1269 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1270 if ( bsdPathAsCFString
) {
1271 size_t devPathLength
;
1272 strcpy( bsdPath
, _PATH_DEV
);
1273 strcat( bsdPath
, "r" );
1274 devPathLength
= strlen( bsdPath
);
1275 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1276 kernResult
= KERN_SUCCESS
;
1278 CFRelease( bsdPathAsCFString
);
1280 IOObjectRelease( nextMedia
);
1288 static int hdev_probe_device(const char *filename
)
1292 /* allow a dedicated CD-ROM driver to match with a higher priority */
1293 if (strstart(filename
, "/dev/cdrom", NULL
))
1296 if (stat(filename
, &st
) >= 0 &&
1297 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1304 static int check_hdev_writable(BDRVRawState
*s
)
1306 #if defined(BLKROGET)
1307 /* Linux block devices can be configured "read-only" using blockdev(8).
1308 * This is independent of device node permissions and therefore open(2)
1309 * with O_RDWR succeeds. Actual writes fail with EPERM.
1311 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1312 * check for read-only block devices so that Linux block devices behave
1318 if (fstat(s
->fd
, &st
)) {
1322 if (!S_ISBLK(st
.st_mode
)) {
1326 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1333 #endif /* defined(BLKROGET) */
1337 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1340 BDRVRawState
*s
= bs
->opaque
;
1342 const char *filename
= qdict_get_str(options
, "filename");
1344 #if defined(__APPLE__) && defined(__MACH__)
1345 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1346 kern_return_t kernResult
;
1347 io_iterator_t mediaIterator
;
1348 char bsdPath
[ MAXPATHLEN
];
1351 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1352 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1354 if ( bsdPath
[ 0 ] != '\0' ) {
1355 strcat(bsdPath
,"s0");
1356 /* some CDs don't have a partition 0 */
1357 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1359 bsdPath
[strlen(bsdPath
)-1] = '1';
1364 qdict_put(options
, "filename", qstring_from_str(filename
));
1367 if ( mediaIterator
)
1368 IOObjectRelease( mediaIterator
);
1372 s
->type
= FTYPE_FILE
;
1373 #if defined(__linux__)
1375 char resolved_path
[ MAXPATHLEN
], *temp
;
1377 temp
= realpath(filename
, resolved_path
);
1378 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1384 ret
= raw_open_common(bs
, options
, flags
, 0);
1389 if (flags
& BDRV_O_RDWR
) {
1390 ret
= check_hdev_writable(s
);
1400 #if defined(__linux__)
1401 /* Note: we do not have a reliable method to detect if the floppy is
1402 present. The current method is to try to open the floppy at every
1403 I/O and to keep it opened during a few hundreds of ms. */
1404 static int fd_open(BlockDriverState
*bs
)
1406 BDRVRawState
*s
= bs
->opaque
;
1407 int last_media_present
;
1409 if (s
->type
!= FTYPE_FD
)
1411 last_media_present
= (s
->fd
>= 0);
1413 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1417 printf("Floppy closed\n");
1421 if (s
->fd_got_error
&&
1422 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1424 printf("No floppy (open delayed)\n");
1428 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1430 s
->fd_error_time
= get_clock();
1431 s
->fd_got_error
= 1;
1432 if (last_media_present
)
1433 s
->fd_media_changed
= 1;
1435 printf("No floppy\n");
1440 printf("Floppy opened\n");
1443 if (!last_media_present
)
1444 s
->fd_media_changed
= 1;
1445 s
->fd_open_time
= get_clock();
1446 s
->fd_got_error
= 0;
1450 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1452 BDRVRawState
*s
= bs
->opaque
;
1454 return ioctl(s
->fd
, req
, buf
);
1457 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1458 unsigned long int req
, void *buf
,
1459 BlockDriverCompletionFunc
*cb
, void *opaque
)
1461 BDRVRawState
*s
= bs
->opaque
;
1462 RawPosixAIOData
*acb
;
1465 if (fd_open(bs
) < 0)
1468 acb
= g_slice_new(RawPosixAIOData
);
1470 acb
->aio_type
= QEMU_AIO_IOCTL
;
1471 acb
->aio_fildes
= s
->fd
;
1472 acb
->aio_offset
= 0;
1473 acb
->aio_ioctl_buf
= buf
;
1474 acb
->aio_ioctl_cmd
= req
;
1475 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1476 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1479 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1480 static int fd_open(BlockDriverState
*bs
)
1482 BDRVRawState
*s
= bs
->opaque
;
1484 /* this is just to ensure s->fd is sane (its called by io ops) */
1489 #else /* !linux && !FreeBSD */
1491 static int fd_open(BlockDriverState
*bs
)
1496 #endif /* !linux && !FreeBSD */
1498 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1499 int64_t sector_num
, int nb_sectors
,
1500 BlockDriverCompletionFunc
*cb
, void *opaque
)
1502 BDRVRawState
*s
= bs
->opaque
;
1504 if (fd_open(bs
) < 0) {
1507 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1508 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1511 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
,
1516 struct stat stat_buf
;
1517 int64_t total_size
= 0;
1519 /* Read out options */
1520 while (options
&& options
->name
) {
1521 if (!strcmp(options
->name
, "size")) {
1522 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1527 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1531 if (fstat(fd
, &stat_buf
) < 0)
1533 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1535 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1542 static BlockDriver bdrv_host_device
= {
1543 .format_name
= "host_device",
1544 .protocol_name
= "host_device",
1545 .instance_size
= sizeof(BDRVRawState
),
1546 .bdrv_needs_filename
= true,
1547 .bdrv_probe_device
= hdev_probe_device
,
1548 .bdrv_file_open
= hdev_open
,
1549 .bdrv_close
= raw_close
,
1550 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1551 .bdrv_reopen_commit
= raw_reopen_commit
,
1552 .bdrv_reopen_abort
= raw_reopen_abort
,
1553 .bdrv_create
= hdev_create
,
1554 .create_options
= raw_create_options
,
1556 .bdrv_aio_readv
= raw_aio_readv
,
1557 .bdrv_aio_writev
= raw_aio_writev
,
1558 .bdrv_aio_flush
= raw_aio_flush
,
1559 .bdrv_aio_discard
= hdev_aio_discard
,
1561 .bdrv_truncate
= raw_truncate
,
1562 .bdrv_getlength
= raw_getlength
,
1563 .bdrv_get_allocated_file_size
1564 = raw_get_allocated_file_size
,
1566 /* generic scsi device */
1568 .bdrv_ioctl
= hdev_ioctl
,
1569 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1574 static int floppy_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1577 BDRVRawState
*s
= bs
->opaque
;
1582 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1583 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
);
1587 /* close fd so that we can reopen it as needed */
1590 s
->fd_media_changed
= 1;
1595 static int floppy_probe_device(const char *filename
)
1599 struct floppy_struct fdparam
;
1602 if (strstart(filename
, "/dev/fd", NULL
) &&
1603 !strstart(filename
, "/dev/fdset/", NULL
)) {
1607 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1611 ret
= fstat(fd
, &st
);
1612 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1616 /* Attempt to detect via a floppy specific ioctl */
1617 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1628 static int floppy_is_inserted(BlockDriverState
*bs
)
1630 return fd_open(bs
) >= 0;
1633 static int floppy_media_changed(BlockDriverState
*bs
)
1635 BDRVRawState
*s
= bs
->opaque
;
1639 * XXX: we do not have a true media changed indication.
1640 * It does not work if the floppy is changed without trying to read it.
1643 ret
= s
->fd_media_changed
;
1644 s
->fd_media_changed
= 0;
1646 printf("Floppy changed=%d\n", ret
);
1651 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1653 BDRVRawState
*s
= bs
->opaque
;
1660 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1662 if (ioctl(fd
, FDEJECT
, 0) < 0)
1668 static BlockDriver bdrv_host_floppy
= {
1669 .format_name
= "host_floppy",
1670 .protocol_name
= "host_floppy",
1671 .instance_size
= sizeof(BDRVRawState
),
1672 .bdrv_needs_filename
= true,
1673 .bdrv_probe_device
= floppy_probe_device
,
1674 .bdrv_file_open
= floppy_open
,
1675 .bdrv_close
= raw_close
,
1676 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1677 .bdrv_reopen_commit
= raw_reopen_commit
,
1678 .bdrv_reopen_abort
= raw_reopen_abort
,
1679 .bdrv_create
= hdev_create
,
1680 .create_options
= raw_create_options
,
1682 .bdrv_aio_readv
= raw_aio_readv
,
1683 .bdrv_aio_writev
= raw_aio_writev
,
1684 .bdrv_aio_flush
= raw_aio_flush
,
1686 .bdrv_truncate
= raw_truncate
,
1687 .bdrv_getlength
= raw_getlength
,
1688 .bdrv_get_allocated_file_size
1689 = raw_get_allocated_file_size
,
1691 /* removable device support */
1692 .bdrv_is_inserted
= floppy_is_inserted
,
1693 .bdrv_media_changed
= floppy_media_changed
,
1694 .bdrv_eject
= floppy_eject
,
1697 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1700 BDRVRawState
*s
= bs
->opaque
;
1704 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1705 return raw_open_common(bs
, options
, flags
, O_NONBLOCK
);
1708 static int cdrom_probe_device(const char *filename
)
1714 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1718 ret
= fstat(fd
, &st
);
1719 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1723 /* Attempt to detect via a CDROM specific ioctl */
1724 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1734 static int cdrom_is_inserted(BlockDriverState
*bs
)
1736 BDRVRawState
*s
= bs
->opaque
;
1739 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1740 if (ret
== CDS_DISC_OK
)
1745 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1747 BDRVRawState
*s
= bs
->opaque
;
1750 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1751 perror("CDROMEJECT");
1753 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1754 perror("CDROMEJECT");
1758 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1760 BDRVRawState
*s
= bs
->opaque
;
1762 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1764 * Note: an error can happen if the distribution automatically
1767 /* perror("CDROM_LOCKDOOR"); */
1771 static BlockDriver bdrv_host_cdrom
= {
1772 .format_name
= "host_cdrom",
1773 .protocol_name
= "host_cdrom",
1774 .instance_size
= sizeof(BDRVRawState
),
1775 .bdrv_needs_filename
= true,
1776 .bdrv_probe_device
= cdrom_probe_device
,
1777 .bdrv_file_open
= cdrom_open
,
1778 .bdrv_close
= raw_close
,
1779 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1780 .bdrv_reopen_commit
= raw_reopen_commit
,
1781 .bdrv_reopen_abort
= raw_reopen_abort
,
1782 .bdrv_create
= hdev_create
,
1783 .create_options
= raw_create_options
,
1785 .bdrv_aio_readv
= raw_aio_readv
,
1786 .bdrv_aio_writev
= raw_aio_writev
,
1787 .bdrv_aio_flush
= raw_aio_flush
,
1789 .bdrv_truncate
= raw_truncate
,
1790 .bdrv_getlength
= raw_getlength
,
1791 .bdrv_get_allocated_file_size
1792 = raw_get_allocated_file_size
,
1794 /* removable device support */
1795 .bdrv_is_inserted
= cdrom_is_inserted
,
1796 .bdrv_eject
= cdrom_eject
,
1797 .bdrv_lock_medium
= cdrom_lock_medium
,
1799 /* generic scsi device */
1800 .bdrv_ioctl
= hdev_ioctl
,
1801 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1803 #endif /* __linux__ */
1805 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1806 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1808 BDRVRawState
*s
= bs
->opaque
;
1813 ret
= raw_open_common(bs
, options
, flags
, 0);
1817 /* make sure the door isn't locked at this time */
1818 ioctl(s
->fd
, CDIOCALLOW
);
1822 static int cdrom_probe_device(const char *filename
)
1824 if (strstart(filename
, "/dev/cd", NULL
) ||
1825 strstart(filename
, "/dev/acd", NULL
))
1830 static int cdrom_reopen(BlockDriverState
*bs
)
1832 BDRVRawState
*s
= bs
->opaque
;
1836 * Force reread of possibly changed/newly loaded disc,
1837 * FreeBSD seems to not notice sometimes...
1841 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1848 /* make sure the door isn't locked at this time */
1849 ioctl(s
->fd
, CDIOCALLOW
);
1853 static int cdrom_is_inserted(BlockDriverState
*bs
)
1855 return raw_getlength(bs
) > 0;
1858 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1860 BDRVRawState
*s
= bs
->opaque
;
1865 (void) ioctl(s
->fd
, CDIOCALLOW
);
1868 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1869 perror("CDIOCEJECT");
1871 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1872 perror("CDIOCCLOSE");
1878 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1880 BDRVRawState
*s
= bs
->opaque
;
1884 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1886 * Note: an error can happen if the distribution automatically
1889 /* perror("CDROM_LOCKDOOR"); */
1893 static BlockDriver bdrv_host_cdrom
= {
1894 .format_name
= "host_cdrom",
1895 .protocol_name
= "host_cdrom",
1896 .instance_size
= sizeof(BDRVRawState
),
1897 .bdrv_needs_filename
= true,
1898 .bdrv_probe_device
= cdrom_probe_device
,
1899 .bdrv_file_open
= cdrom_open
,
1900 .bdrv_close
= raw_close
,
1901 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1902 .bdrv_reopen_commit
= raw_reopen_commit
,
1903 .bdrv_reopen_abort
= raw_reopen_abort
,
1904 .bdrv_create
= hdev_create
,
1905 .create_options
= raw_create_options
,
1907 .bdrv_aio_readv
= raw_aio_readv
,
1908 .bdrv_aio_writev
= raw_aio_writev
,
1909 .bdrv_aio_flush
= raw_aio_flush
,
1911 .bdrv_truncate
= raw_truncate
,
1912 .bdrv_getlength
= raw_getlength
,
1913 .bdrv_get_allocated_file_size
1914 = raw_get_allocated_file_size
,
1916 /* removable device support */
1917 .bdrv_is_inserted
= cdrom_is_inserted
,
1918 .bdrv_eject
= cdrom_eject
,
1919 .bdrv_lock_medium
= cdrom_lock_medium
,
1921 #endif /* __FreeBSD__ */
1923 #ifdef CONFIG_LINUX_AIO
1925 * Return the file descriptor for Linux AIO
1927 * This function is a layering violation and should be removed when it becomes
1928 * possible to call the block layer outside the global mutex. It allows the
1929 * caller to hijack the file descriptor so I/O can be performed outside the
1932 int raw_get_aio_fd(BlockDriverState
*bs
)
1940 if (bs
->drv
== bdrv_find_format("raw")) {
1944 /* raw-posix has several protocols so just check for raw_aio_readv */
1945 if (bs
->drv
->bdrv_aio_readv
!= raw_aio_readv
) {
1955 #endif /* CONFIG_LINUX_AIO */
1957 static void bdrv_file_init(void)
1960 * Register all the drivers. Note that order is important, the driver
1961 * registered last will get probed first.
1963 bdrv_register(&bdrv_file
);
1964 bdrv_register(&bdrv_host_device
);
1966 bdrv_register(&bdrv_host_floppy
);
1967 bdrv_register(&bdrv_host_cdrom
);
1969 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1970 bdrv_register(&bdrv_host_cdrom
);
1974 block_init(bdrv_file_init
);