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
, Error
**errp
)
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 error_propagate(errp
, local_err
);
295 filename
= qemu_opt_get(opts
, "filename");
297 ret
= raw_normalize_devicepath(&filename
);
299 error_setg_errno(errp
, -ret
, "Could not normalize device path");
303 s
->open_flags
= open_flags
;
304 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
307 fd
= qemu_open(filename
, s
->open_flags
, 0644);
313 error_setg_errno(errp
, -ret
, "Could not open file");
318 #ifdef CONFIG_LINUX_AIO
319 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
322 error_setg_errno(errp
, -ret
, "Could not set AIO state");
329 if (platform_test_xfs_fd(s
->fd
)) {
340 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
343 BDRVRawState
*s
= bs
->opaque
;
344 Error
*local_err
= NULL
;
347 s
->type
= FTYPE_FILE
;
348 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
349 if (error_is_set(&local_err
)) {
350 error_propagate(errp
, local_err
);
355 static int raw_reopen_prepare(BDRVReopenState
*state
,
356 BlockReopenQueue
*queue
, Error
**errp
)
359 BDRVRawReopenState
*raw_s
;
362 assert(state
!= NULL
);
363 assert(state
->bs
!= NULL
);
365 s
= state
->bs
->opaque
;
367 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
368 raw_s
= state
->opaque
;
370 #ifdef CONFIG_LINUX_AIO
371 raw_s
->use_aio
= s
->use_aio
;
373 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
374 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
375 * won't override aio_ctx if aio_ctx is non-NULL */
376 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
377 error_setg(errp
, "Could not set AIO state");
382 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
383 raw_s
->open_flags
|= O_NONBLOCK
;
386 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
390 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
392 fcntl_flags
|= O_NOATIME
;
396 /* Not all operating systems have O_ASYNC, and those that don't
397 * will not let us track the state into raw_s->open_flags (typically
398 * you achieve the same effect with an ioctl, for example I_SETSIG
399 * on Solaris). But we do not use O_ASYNC, so that's fine.
401 assert((s
->open_flags
& O_ASYNC
) == 0);
404 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
405 /* dup the original fd */
406 /* TODO: use qemu fcntl wrapper */
407 #ifdef F_DUPFD_CLOEXEC
408 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
410 raw_s
->fd
= dup(s
->fd
);
411 if (raw_s
->fd
!= -1) {
412 qemu_set_cloexec(raw_s
->fd
);
415 if (raw_s
->fd
>= 0) {
416 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
418 qemu_close(raw_s
->fd
);
424 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
425 if (raw_s
->fd
== -1) {
426 assert(!(raw_s
->open_flags
& O_CREAT
));
427 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
428 if (raw_s
->fd
== -1) {
429 error_setg_errno(errp
, errno
, "Could not reopen file");
437 static void raw_reopen_commit(BDRVReopenState
*state
)
439 BDRVRawReopenState
*raw_s
= state
->opaque
;
440 BDRVRawState
*s
= state
->bs
->opaque
;
442 s
->open_flags
= raw_s
->open_flags
;
446 #ifdef CONFIG_LINUX_AIO
447 s
->use_aio
= raw_s
->use_aio
;
450 g_free(state
->opaque
);
451 state
->opaque
= NULL
;
455 static void raw_reopen_abort(BDRVReopenState
*state
)
457 BDRVRawReopenState
*raw_s
= state
->opaque
;
459 /* nothing to do if NULL, we didn't get far enough */
464 if (raw_s
->fd
>= 0) {
465 qemu_close(raw_s
->fd
);
468 g_free(state
->opaque
);
469 state
->opaque
= NULL
;
473 /* XXX: use host sector size if necessary with:
474 #ifdef DIOCGSECTORSIZE
476 unsigned int sectorsize = 512;
477 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
478 sectorsize > bufsize)
479 bufsize = sectorsize;
483 uint32_t blockSize = 512;
484 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
490 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
494 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
502 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
506 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
515 static bool preadv_present
= true;
518 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
520 return preadv(fd
, iov
, nr_iov
, offset
);
524 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
526 return pwritev(fd
, iov
, nr_iov
, offset
);
531 static bool preadv_present
= false;
534 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
540 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
547 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
552 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
553 len
= qemu_pwritev(aiocb
->aio_fildes
,
558 len
= qemu_preadv(aiocb
->aio_fildes
,
562 } while (len
== -1 && errno
== EINTR
);
571 * Read/writes the data to/from a given linear buffer.
573 * Returns the number of bytes handles or -errno in case of an error. Short
574 * reads are only returned if the end of the file is reached.
576 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
581 while (offset
< aiocb
->aio_nbytes
) {
582 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
583 len
= pwrite(aiocb
->aio_fildes
,
584 (const char *)buf
+ offset
,
585 aiocb
->aio_nbytes
- offset
,
586 aiocb
->aio_offset
+ offset
);
588 len
= pread(aiocb
->aio_fildes
,
590 aiocb
->aio_nbytes
- offset
,
591 aiocb
->aio_offset
+ offset
);
593 if (len
== -1 && errno
== EINTR
) {
595 } else if (len
== -1) {
598 } else if (len
== 0) {
607 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
612 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
614 * If there is just a single buffer, and it is properly aligned
615 * we can just use plain pread/pwrite without any problems.
617 if (aiocb
->aio_niov
== 1) {
618 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
621 * We have more than one iovec, and all are properly aligned.
623 * Try preadv/pwritev first and fall back to linearizing the
624 * buffer if it's not supported.
626 if (preadv_present
) {
627 nbytes
= handle_aiocb_rw_vector(aiocb
);
628 if (nbytes
== aiocb
->aio_nbytes
||
629 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
632 preadv_present
= false;
636 * XXX(hch): short read/write. no easy way to handle the reminder
637 * using these interfaces. For now retry using plain
643 * Ok, we have to do it the hard way, copy all segments into
644 * a single aligned buffer.
646 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
647 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
651 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
652 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
653 p
+= aiocb
->aio_iov
[i
].iov_len
;
657 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
658 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
660 size_t count
= aiocb
->aio_nbytes
, copy
;
663 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
665 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
666 copy
= aiocb
->aio_iov
[i
].iov_len
;
668 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
679 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
681 struct xfs_flock64 fl
;
683 memset(&fl
, 0, sizeof(fl
));
684 fl
.l_whence
= SEEK_SET
;
688 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
689 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
697 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
699 int ret
= -EOPNOTSUPP
;
700 BDRVRawState
*s
= aiocb
->bs
->opaque
;
702 if (s
->has_discard
== 0) {
706 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
709 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
710 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
713 } while (errno
== EINTR
);
720 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
724 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
726 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
727 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
730 } while (errno
== EINTR
);
736 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
744 static int aio_worker(void *arg
)
746 RawPosixAIOData
*aiocb
= arg
;
749 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
751 ret
= handle_aiocb_rw(aiocb
);
752 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
753 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
754 0, aiocb
->aio_nbytes
- ret
);
756 ret
= aiocb
->aio_nbytes
;
758 if (ret
== aiocb
->aio_nbytes
) {
760 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
765 ret
= handle_aiocb_rw(aiocb
);
766 if (ret
== aiocb
->aio_nbytes
) {
768 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
773 ret
= handle_aiocb_flush(aiocb
);
776 ret
= handle_aiocb_ioctl(aiocb
);
778 case QEMU_AIO_DISCARD
:
779 ret
= handle_aiocb_discard(aiocb
);
782 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
787 g_slice_free(RawPosixAIOData
, aiocb
);
791 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
792 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
793 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
795 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
799 acb
->aio_type
= type
;
800 acb
->aio_fildes
= fd
;
803 acb
->aio_iov
= qiov
->iov
;
804 acb
->aio_niov
= qiov
->niov
;
806 acb
->aio_nbytes
= nb_sectors
* 512;
807 acb
->aio_offset
= sector_num
* 512;
809 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
810 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
811 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
814 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
815 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
816 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
818 BDRVRawState
*s
= bs
->opaque
;
824 * If O_DIRECT is used the buffer needs to be aligned on a sector
825 * boundary. Check if this is the case or tell the low-level
826 * driver that it needs to copy the buffer.
828 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
829 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
830 type
|= QEMU_AIO_MISALIGNED
;
831 #ifdef CONFIG_LINUX_AIO
832 } else if (s
->use_aio
) {
833 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
834 nb_sectors
, cb
, opaque
, type
);
839 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
843 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
844 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
845 BlockDriverCompletionFunc
*cb
, void *opaque
)
847 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
848 cb
, opaque
, QEMU_AIO_READ
);
851 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
852 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
853 BlockDriverCompletionFunc
*cb
, void *opaque
)
855 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
856 cb
, opaque
, QEMU_AIO_WRITE
);
859 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
860 BlockDriverCompletionFunc
*cb
, void *opaque
)
862 BDRVRawState
*s
= bs
->opaque
;
867 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
870 static void raw_close(BlockDriverState
*bs
)
872 BDRVRawState
*s
= bs
->opaque
;
879 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
881 BDRVRawState
*s
= bs
->opaque
;
884 if (fstat(s
->fd
, &st
)) {
888 if (S_ISREG(st
.st_mode
)) {
889 if (ftruncate(s
->fd
, offset
) < 0) {
892 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
893 if (offset
> raw_getlength(bs
)) {
904 static int64_t raw_getlength(BlockDriverState
*bs
)
906 BDRVRawState
*s
= bs
->opaque
;
912 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
915 if (ioctl(fd
, DIOCGDINFO
, &dl
))
917 return (uint64_t)dl
.d_secsize
*
918 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
922 #elif defined(__NetBSD__)
923 static int64_t raw_getlength(BlockDriverState
*bs
)
925 BDRVRawState
*s
= bs
->opaque
;
931 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
932 struct dkwedge_info dkw
;
934 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
935 return dkw
.dkw_size
* 512;
939 if (ioctl(fd
, DIOCGDINFO
, &dl
))
941 return (uint64_t)dl
.d_secsize
*
942 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
947 #elif defined(__sun__)
948 static int64_t raw_getlength(BlockDriverState
*bs
)
950 BDRVRawState
*s
= bs
->opaque
;
951 struct dk_minfo minfo
;
960 * Use the DKIOCGMEDIAINFO ioctl to read the size.
962 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
964 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
968 * There are reports that lseek on some devices fails, but
969 * irc discussion said that contingency on contingency was overkill.
971 return lseek(s
->fd
, 0, SEEK_END
);
973 #elif defined(CONFIG_BSD)
974 static int64_t raw_getlength(BlockDriverState
*bs
)
976 BDRVRawState
*s
= bs
->opaque
;
980 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
989 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
992 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
993 #ifdef DIOCGMEDIASIZE
994 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
995 #elif defined(DIOCGPART)
998 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
999 size
= pi
.media_size
;
1005 #if defined(__APPLE__) && defined(__MACH__)
1006 size
= LONG_LONG_MAX
;
1008 size
= lseek(fd
, 0LL, SEEK_END
);
1010 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1013 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1014 if (size
== 2048LL * (unsigned)-1)
1016 /* XXX no disc? maybe we need to reopen... */
1017 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
1024 size
= lseek(fd
, 0, SEEK_END
);
1029 static int64_t raw_getlength(BlockDriverState
*bs
)
1031 BDRVRawState
*s
= bs
->opaque
;
1039 return lseek(s
->fd
, 0, SEEK_END
);
1043 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1046 BDRVRawState
*s
= bs
->opaque
;
1048 if (fstat(s
->fd
, &st
) < 0) {
1051 return (int64_t)st
.st_blocks
* 512;
1054 static int raw_create(const char *filename
, QEMUOptionParameter
*options
,
1059 int64_t total_size
= 0;
1061 /* Read out options */
1062 while (options
&& options
->name
) {
1063 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1064 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1069 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1073 error_setg_errno(errp
, -result
, "Could not create file");
1075 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1077 error_setg_errno(errp
, -result
, "Could not resize file");
1079 if (qemu_close(fd
) != 0) {
1081 error_setg_errno(errp
, -result
, "Could not close the new file");
1088 * Returns true iff the specified sector is present in the disk image. Drivers
1089 * not implementing the functionality are assumed to not support backing files,
1090 * hence all their sectors are reported as allocated.
1092 * If 'sector_num' is beyond the end of the disk image the return value is 0
1093 * and 'pnum' is set to 0.
1095 * 'pnum' is set to the number of sectors (including and immediately following
1096 * the specified sector) that are known to be in the same
1097 * allocated/unallocated state.
1099 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1100 * beyond the end of the disk image it will be clamped.
1102 static int64_t coroutine_fn
raw_co_get_block_status(BlockDriverState
*bs
,
1104 int nb_sectors
, int *pnum
)
1106 off_t start
, data
, hole
;
1114 start
= sector_num
* BDRV_SECTOR_SIZE
;
1115 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
1117 #ifdef CONFIG_FIEMAP
1119 BDRVRawState
*s
= bs
->opaque
;
1122 struct fiemap_extent fe
;
1125 f
.fm
.fm_start
= start
;
1126 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1128 f
.fm
.fm_extent_count
= 1;
1129 f
.fm
.fm_reserved
= 0;
1130 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1131 /* Assume everything is allocated. */
1136 if (f
.fm
.fm_mapped_extents
== 0) {
1137 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1138 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1140 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1141 hole
= f
.fm
.fm_start
;
1142 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1144 data
= f
.fe
.fe_logical
;
1145 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1146 if (f
.fe
.fe_flags
& FIEMAP_EXTENT_UNWRITTEN
) {
1147 ret
|= BDRV_BLOCK_ZERO
;
1151 #elif defined SEEK_HOLE && defined SEEK_DATA
1153 BDRVRawState
*s
= bs
->opaque
;
1155 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1157 /* -ENXIO indicates that sector_num was past the end of the file.
1158 * There is a virtual hole there. */
1159 assert(errno
!= -ENXIO
);
1161 /* Most likely EINVAL. Assume everything is allocated. */
1169 /* On a hole. We need another syscall to find its end. */
1170 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1172 data
= lseek(s
->fd
, 0, SEEK_END
);
1177 hole
= start
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1180 if (data
<= start
) {
1181 /* On a data extent, compute sectors to the end of the extent. */
1182 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1184 /* On a hole, compute sectors to the beginning of the next extent. */
1185 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1186 ret
&= ~BDRV_BLOCK_DATA
;
1187 ret
|= BDRV_BLOCK_ZERO
;
1193 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1194 int64_t sector_num
, int nb_sectors
,
1195 BlockDriverCompletionFunc
*cb
, void *opaque
)
1197 BDRVRawState
*s
= bs
->opaque
;
1199 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1200 cb
, opaque
, QEMU_AIO_DISCARD
);
1203 static QEMUOptionParameter raw_create_options
[] = {
1205 .name
= BLOCK_OPT_SIZE
,
1207 .help
= "Virtual disk size"
1212 static BlockDriver bdrv_file
= {
1213 .format_name
= "file",
1214 .protocol_name
= "file",
1215 .instance_size
= sizeof(BDRVRawState
),
1216 .bdrv_needs_filename
= true,
1217 .bdrv_probe
= NULL
, /* no probe for protocols */
1218 .bdrv_file_open
= raw_open
,
1219 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1220 .bdrv_reopen_commit
= raw_reopen_commit
,
1221 .bdrv_reopen_abort
= raw_reopen_abort
,
1222 .bdrv_close
= raw_close
,
1223 .bdrv_create
= raw_create
,
1224 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1225 .bdrv_co_get_block_status
= raw_co_get_block_status
,
1227 .bdrv_aio_readv
= raw_aio_readv
,
1228 .bdrv_aio_writev
= raw_aio_writev
,
1229 .bdrv_aio_flush
= raw_aio_flush
,
1230 .bdrv_aio_discard
= raw_aio_discard
,
1232 .bdrv_truncate
= raw_truncate
,
1233 .bdrv_getlength
= raw_getlength
,
1234 .bdrv_get_allocated_file_size
1235 = raw_get_allocated_file_size
,
1237 .create_options
= raw_create_options
,
1240 /***********************************************/
1243 #if defined(__APPLE__) && defined(__MACH__)
1244 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1245 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1247 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1249 kern_return_t kernResult
;
1250 mach_port_t masterPort
;
1251 CFMutableDictionaryRef classesToMatch
;
1253 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1254 if ( KERN_SUCCESS
!= kernResult
) {
1255 printf( "IOMasterPort returned %d\n", kernResult
);
1258 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1259 if ( classesToMatch
== NULL
) {
1260 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1262 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1264 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1265 if ( KERN_SUCCESS
!= kernResult
)
1267 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1273 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1275 io_object_t nextMedia
;
1276 kern_return_t kernResult
= KERN_FAILURE
;
1278 nextMedia
= IOIteratorNext( mediaIterator
);
1281 CFTypeRef bsdPathAsCFString
;
1282 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1283 if ( bsdPathAsCFString
) {
1284 size_t devPathLength
;
1285 strcpy( bsdPath
, _PATH_DEV
);
1286 strcat( bsdPath
, "r" );
1287 devPathLength
= strlen( bsdPath
);
1288 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1289 kernResult
= KERN_SUCCESS
;
1291 CFRelease( bsdPathAsCFString
);
1293 IOObjectRelease( nextMedia
);
1301 static int hdev_probe_device(const char *filename
)
1305 /* allow a dedicated CD-ROM driver to match with a higher priority */
1306 if (strstart(filename
, "/dev/cdrom", NULL
))
1309 if (stat(filename
, &st
) >= 0 &&
1310 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1317 static int check_hdev_writable(BDRVRawState
*s
)
1319 #if defined(BLKROGET)
1320 /* Linux block devices can be configured "read-only" using blockdev(8).
1321 * This is independent of device node permissions and therefore open(2)
1322 * with O_RDWR succeeds. Actual writes fail with EPERM.
1324 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1325 * check for read-only block devices so that Linux block devices behave
1331 if (fstat(s
->fd
, &st
)) {
1335 if (!S_ISBLK(st
.st_mode
)) {
1339 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1346 #endif /* defined(BLKROGET) */
1350 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1353 BDRVRawState
*s
= bs
->opaque
;
1354 Error
*local_err
= NULL
;
1356 const char *filename
= qdict_get_str(options
, "filename");
1358 #if defined(__APPLE__) && defined(__MACH__)
1359 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1360 kern_return_t kernResult
;
1361 io_iterator_t mediaIterator
;
1362 char bsdPath
[ MAXPATHLEN
];
1365 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1366 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1368 if ( bsdPath
[ 0 ] != '\0' ) {
1369 strcat(bsdPath
,"s0");
1370 /* some CDs don't have a partition 0 */
1371 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1373 bsdPath
[strlen(bsdPath
)-1] = '1';
1378 qdict_put(options
, "filename", qstring_from_str(filename
));
1381 if ( mediaIterator
)
1382 IOObjectRelease( mediaIterator
);
1386 s
->type
= FTYPE_FILE
;
1387 #if defined(__linux__)
1389 char resolved_path
[ MAXPATHLEN
], *temp
;
1391 temp
= realpath(filename
, resolved_path
);
1392 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1398 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
1400 if (error_is_set(&local_err
)) {
1401 error_propagate(errp
, local_err
);
1406 if (flags
& BDRV_O_RDWR
) {
1407 ret
= check_hdev_writable(s
);
1410 error_setg_errno(errp
, -ret
, "The device is not writable");
1418 #if defined(__linux__)
1419 /* Note: we do not have a reliable method to detect if the floppy is
1420 present. The current method is to try to open the floppy at every
1421 I/O and to keep it opened during a few hundreds of ms. */
1422 static int fd_open(BlockDriverState
*bs
)
1424 BDRVRawState
*s
= bs
->opaque
;
1425 int last_media_present
;
1427 if (s
->type
!= FTYPE_FD
)
1429 last_media_present
= (s
->fd
>= 0);
1431 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1435 printf("Floppy closed\n");
1439 if (s
->fd_got_error
&&
1440 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1442 printf("No floppy (open delayed)\n");
1446 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1448 s
->fd_error_time
= get_clock();
1449 s
->fd_got_error
= 1;
1450 if (last_media_present
)
1451 s
->fd_media_changed
= 1;
1453 printf("No floppy\n");
1458 printf("Floppy opened\n");
1461 if (!last_media_present
)
1462 s
->fd_media_changed
= 1;
1463 s
->fd_open_time
= get_clock();
1464 s
->fd_got_error
= 0;
1468 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1470 BDRVRawState
*s
= bs
->opaque
;
1472 return ioctl(s
->fd
, req
, buf
);
1475 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1476 unsigned long int req
, void *buf
,
1477 BlockDriverCompletionFunc
*cb
, void *opaque
)
1479 BDRVRawState
*s
= bs
->opaque
;
1480 RawPosixAIOData
*acb
;
1483 if (fd_open(bs
) < 0)
1486 acb
= g_slice_new(RawPosixAIOData
);
1488 acb
->aio_type
= QEMU_AIO_IOCTL
;
1489 acb
->aio_fildes
= s
->fd
;
1490 acb
->aio_offset
= 0;
1491 acb
->aio_ioctl_buf
= buf
;
1492 acb
->aio_ioctl_cmd
= req
;
1493 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1494 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1497 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1498 static int fd_open(BlockDriverState
*bs
)
1500 BDRVRawState
*s
= bs
->opaque
;
1502 /* this is just to ensure s->fd is sane (its called by io ops) */
1507 #else /* !linux && !FreeBSD */
1509 static int fd_open(BlockDriverState
*bs
)
1514 #endif /* !linux && !FreeBSD */
1516 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1517 int64_t sector_num
, int nb_sectors
,
1518 BlockDriverCompletionFunc
*cb
, void *opaque
)
1520 BDRVRawState
*s
= bs
->opaque
;
1522 if (fd_open(bs
) < 0) {
1525 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1526 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1529 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
,
1534 struct stat stat_buf
;
1535 int64_t total_size
= 0;
1537 /* Read out options */
1538 while (options
&& options
->name
) {
1539 if (!strcmp(options
->name
, "size")) {
1540 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1545 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1548 error_setg_errno(errp
, -ret
, "Could not open device");
1552 if (fstat(fd
, &stat_buf
) < 0) {
1554 error_setg_errno(errp
, -ret
, "Could not stat device");
1555 } else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
)) {
1557 "The given file is neither a block nor a character device");
1559 } else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
) {
1560 error_setg(errp
, "Device is too small");
1568 static BlockDriver bdrv_host_device
= {
1569 .format_name
= "host_device",
1570 .protocol_name
= "host_device",
1571 .instance_size
= sizeof(BDRVRawState
),
1572 .bdrv_needs_filename
= true,
1573 .bdrv_probe_device
= hdev_probe_device
,
1574 .bdrv_file_open
= hdev_open
,
1575 .bdrv_close
= raw_close
,
1576 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1577 .bdrv_reopen_commit
= raw_reopen_commit
,
1578 .bdrv_reopen_abort
= raw_reopen_abort
,
1579 .bdrv_create
= hdev_create
,
1580 .create_options
= raw_create_options
,
1582 .bdrv_aio_readv
= raw_aio_readv
,
1583 .bdrv_aio_writev
= raw_aio_writev
,
1584 .bdrv_aio_flush
= raw_aio_flush
,
1585 .bdrv_aio_discard
= hdev_aio_discard
,
1587 .bdrv_truncate
= raw_truncate
,
1588 .bdrv_getlength
= raw_getlength
,
1589 .bdrv_get_allocated_file_size
1590 = raw_get_allocated_file_size
,
1592 /* generic scsi device */
1594 .bdrv_ioctl
= hdev_ioctl
,
1595 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1600 static int floppy_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1603 BDRVRawState
*s
= bs
->opaque
;
1604 Error
*local_err
= NULL
;
1609 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1610 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
1612 if (error_is_set(&local_err
)) {
1613 error_propagate(errp
, local_err
);
1618 /* close fd so that we can reopen it as needed */
1621 s
->fd_media_changed
= 1;
1626 static int floppy_probe_device(const char *filename
)
1630 struct floppy_struct fdparam
;
1633 if (strstart(filename
, "/dev/fd", NULL
) &&
1634 !strstart(filename
, "/dev/fdset/", NULL
)) {
1638 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1642 ret
= fstat(fd
, &st
);
1643 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1647 /* Attempt to detect via a floppy specific ioctl */
1648 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1659 static int floppy_is_inserted(BlockDriverState
*bs
)
1661 return fd_open(bs
) >= 0;
1664 static int floppy_media_changed(BlockDriverState
*bs
)
1666 BDRVRawState
*s
= bs
->opaque
;
1670 * XXX: we do not have a true media changed indication.
1671 * It does not work if the floppy is changed without trying to read it.
1674 ret
= s
->fd_media_changed
;
1675 s
->fd_media_changed
= 0;
1677 printf("Floppy changed=%d\n", ret
);
1682 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1684 BDRVRawState
*s
= bs
->opaque
;
1691 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1693 if (ioctl(fd
, FDEJECT
, 0) < 0)
1699 static BlockDriver bdrv_host_floppy
= {
1700 .format_name
= "host_floppy",
1701 .protocol_name
= "host_floppy",
1702 .instance_size
= sizeof(BDRVRawState
),
1703 .bdrv_needs_filename
= true,
1704 .bdrv_probe_device
= floppy_probe_device
,
1705 .bdrv_file_open
= floppy_open
,
1706 .bdrv_close
= raw_close
,
1707 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1708 .bdrv_reopen_commit
= raw_reopen_commit
,
1709 .bdrv_reopen_abort
= raw_reopen_abort
,
1710 .bdrv_create
= hdev_create
,
1711 .create_options
= raw_create_options
,
1713 .bdrv_aio_readv
= raw_aio_readv
,
1714 .bdrv_aio_writev
= raw_aio_writev
,
1715 .bdrv_aio_flush
= raw_aio_flush
,
1717 .bdrv_truncate
= raw_truncate
,
1718 .bdrv_getlength
= raw_getlength
,
1719 .has_variable_length
= true,
1720 .bdrv_get_allocated_file_size
1721 = raw_get_allocated_file_size
,
1723 /* removable device support */
1724 .bdrv_is_inserted
= floppy_is_inserted
,
1725 .bdrv_media_changed
= floppy_media_changed
,
1726 .bdrv_eject
= floppy_eject
,
1729 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1732 BDRVRawState
*s
= bs
->opaque
;
1733 Error
*local_err
= NULL
;
1738 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1739 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
1740 if (error_is_set(&local_err
)) {
1741 error_propagate(errp
, local_err
);
1746 static int cdrom_probe_device(const char *filename
)
1752 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1756 ret
= fstat(fd
, &st
);
1757 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1761 /* Attempt to detect via a CDROM specific ioctl */
1762 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1772 static int cdrom_is_inserted(BlockDriverState
*bs
)
1774 BDRVRawState
*s
= bs
->opaque
;
1777 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1778 if (ret
== CDS_DISC_OK
)
1783 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1785 BDRVRawState
*s
= bs
->opaque
;
1788 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1789 perror("CDROMEJECT");
1791 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1792 perror("CDROMEJECT");
1796 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1798 BDRVRawState
*s
= bs
->opaque
;
1800 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1802 * Note: an error can happen if the distribution automatically
1805 /* perror("CDROM_LOCKDOOR"); */
1809 static BlockDriver bdrv_host_cdrom
= {
1810 .format_name
= "host_cdrom",
1811 .protocol_name
= "host_cdrom",
1812 .instance_size
= sizeof(BDRVRawState
),
1813 .bdrv_needs_filename
= true,
1814 .bdrv_probe_device
= cdrom_probe_device
,
1815 .bdrv_file_open
= cdrom_open
,
1816 .bdrv_close
= raw_close
,
1817 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1818 .bdrv_reopen_commit
= raw_reopen_commit
,
1819 .bdrv_reopen_abort
= raw_reopen_abort
,
1820 .bdrv_create
= hdev_create
,
1821 .create_options
= raw_create_options
,
1823 .bdrv_aio_readv
= raw_aio_readv
,
1824 .bdrv_aio_writev
= raw_aio_writev
,
1825 .bdrv_aio_flush
= raw_aio_flush
,
1827 .bdrv_truncate
= raw_truncate
,
1828 .bdrv_getlength
= raw_getlength
,
1829 .has_variable_length
= true,
1830 .bdrv_get_allocated_file_size
1831 = raw_get_allocated_file_size
,
1833 /* removable device support */
1834 .bdrv_is_inserted
= cdrom_is_inserted
,
1835 .bdrv_eject
= cdrom_eject
,
1836 .bdrv_lock_medium
= cdrom_lock_medium
,
1838 /* generic scsi device */
1839 .bdrv_ioctl
= hdev_ioctl
,
1840 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1842 #endif /* __linux__ */
1844 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1845 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1847 BDRVRawState
*s
= bs
->opaque
;
1848 Error
*local_err
= NULL
;
1853 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
1855 if (error_is_set(&local_err
)) {
1856 error_propagate(errp
, local_err
);
1861 /* make sure the door isn't locked at this time */
1862 ioctl(s
->fd
, CDIOCALLOW
);
1866 static int cdrom_probe_device(const char *filename
)
1868 if (strstart(filename
, "/dev/cd", NULL
) ||
1869 strstart(filename
, "/dev/acd", NULL
))
1874 static int cdrom_reopen(BlockDriverState
*bs
)
1876 BDRVRawState
*s
= bs
->opaque
;
1880 * Force reread of possibly changed/newly loaded disc,
1881 * FreeBSD seems to not notice sometimes...
1885 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1892 /* make sure the door isn't locked at this time */
1893 ioctl(s
->fd
, CDIOCALLOW
);
1897 static int cdrom_is_inserted(BlockDriverState
*bs
)
1899 return raw_getlength(bs
) > 0;
1902 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1904 BDRVRawState
*s
= bs
->opaque
;
1909 (void) ioctl(s
->fd
, CDIOCALLOW
);
1912 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1913 perror("CDIOCEJECT");
1915 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1916 perror("CDIOCCLOSE");
1922 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1924 BDRVRawState
*s
= bs
->opaque
;
1928 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1930 * Note: an error can happen if the distribution automatically
1933 /* perror("CDROM_LOCKDOOR"); */
1937 static BlockDriver bdrv_host_cdrom
= {
1938 .format_name
= "host_cdrom",
1939 .protocol_name
= "host_cdrom",
1940 .instance_size
= sizeof(BDRVRawState
),
1941 .bdrv_needs_filename
= true,
1942 .bdrv_probe_device
= cdrom_probe_device
,
1943 .bdrv_file_open
= cdrom_open
,
1944 .bdrv_close
= raw_close
,
1945 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1946 .bdrv_reopen_commit
= raw_reopen_commit
,
1947 .bdrv_reopen_abort
= raw_reopen_abort
,
1948 .bdrv_create
= hdev_create
,
1949 .create_options
= raw_create_options
,
1951 .bdrv_aio_readv
= raw_aio_readv
,
1952 .bdrv_aio_writev
= raw_aio_writev
,
1953 .bdrv_aio_flush
= raw_aio_flush
,
1955 .bdrv_truncate
= raw_truncate
,
1956 .bdrv_getlength
= raw_getlength
,
1957 .has_variable_length
= true,
1958 .bdrv_get_allocated_file_size
1959 = raw_get_allocated_file_size
,
1961 /* removable device support */
1962 .bdrv_is_inserted
= cdrom_is_inserted
,
1963 .bdrv_eject
= cdrom_eject
,
1964 .bdrv_lock_medium
= cdrom_lock_medium
,
1966 #endif /* __FreeBSD__ */
1968 #ifdef CONFIG_LINUX_AIO
1970 * Return the file descriptor for Linux AIO
1972 * This function is a layering violation and should be removed when it becomes
1973 * possible to call the block layer outside the global mutex. It allows the
1974 * caller to hijack the file descriptor so I/O can be performed outside the
1977 int raw_get_aio_fd(BlockDriverState
*bs
)
1985 if (bs
->drv
== bdrv_find_format("raw")) {
1989 /* raw-posix has several protocols so just check for raw_aio_readv */
1990 if (bs
->drv
->bdrv_aio_readv
!= raw_aio_readv
) {
2000 #endif /* CONFIG_LINUX_AIO */
2002 static void bdrv_file_init(void)
2005 * Register all the drivers. Note that order is important, the driver
2006 * registered last will get probed first.
2008 bdrv_register(&bdrv_file
);
2009 bdrv_register(&bdrv_host_device
);
2011 bdrv_register(&bdrv_host_floppy
);
2012 bdrv_register(&bdrv_host_cdrom
);
2014 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2015 bdrv_register(&bdrv_host_cdrom
);
2019 block_init(bdrv_file_init
);