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);
317 #ifdef CONFIG_LINUX_AIO
318 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
321 error_setg_errno(errp
, -ret
, "Could not set AIO state");
328 if (platform_test_xfs_fd(s
->fd
)) {
339 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
342 BDRVRawState
*s
= bs
->opaque
;
343 Error
*local_err
= NULL
;
346 s
->type
= FTYPE_FILE
;
347 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
348 if (error_is_set(&local_err
)) {
349 error_propagate(errp
, local_err
);
354 static int raw_reopen_prepare(BDRVReopenState
*state
,
355 BlockReopenQueue
*queue
, Error
**errp
)
358 BDRVRawReopenState
*raw_s
;
361 assert(state
!= NULL
);
362 assert(state
->bs
!= NULL
);
364 s
= state
->bs
->opaque
;
366 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
367 raw_s
= state
->opaque
;
369 #ifdef CONFIG_LINUX_AIO
370 raw_s
->use_aio
= s
->use_aio
;
372 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
373 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
374 * won't override aio_ctx if aio_ctx is non-NULL */
375 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
376 error_setg(errp
, "Could not set AIO state");
381 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
382 raw_s
->open_flags
|= O_NONBLOCK
;
385 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
389 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
391 fcntl_flags
|= O_NOATIME
;
395 /* Not all operating systems have O_ASYNC, and those that don't
396 * will not let us track the state into raw_s->open_flags (typically
397 * you achieve the same effect with an ioctl, for example I_SETSIG
398 * on Solaris). But we do not use O_ASYNC, so that's fine.
400 assert((s
->open_flags
& O_ASYNC
) == 0);
403 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
404 /* dup the original fd */
405 /* TODO: use qemu fcntl wrapper */
406 #ifdef F_DUPFD_CLOEXEC
407 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
409 raw_s
->fd
= dup(s
->fd
);
410 if (raw_s
->fd
!= -1) {
411 qemu_set_cloexec(raw_s
->fd
);
414 if (raw_s
->fd
>= 0) {
415 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
417 qemu_close(raw_s
->fd
);
423 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
424 if (raw_s
->fd
== -1) {
425 assert(!(raw_s
->open_flags
& O_CREAT
));
426 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
427 if (raw_s
->fd
== -1) {
428 error_setg_errno(errp
, errno
, "Could not reopen file");
436 static void raw_reopen_commit(BDRVReopenState
*state
)
438 BDRVRawReopenState
*raw_s
= state
->opaque
;
439 BDRVRawState
*s
= state
->bs
->opaque
;
441 s
->open_flags
= raw_s
->open_flags
;
445 #ifdef CONFIG_LINUX_AIO
446 s
->use_aio
= raw_s
->use_aio
;
449 g_free(state
->opaque
);
450 state
->opaque
= NULL
;
454 static void raw_reopen_abort(BDRVReopenState
*state
)
456 BDRVRawReopenState
*raw_s
= state
->opaque
;
458 /* nothing to do if NULL, we didn't get far enough */
463 if (raw_s
->fd
>= 0) {
464 qemu_close(raw_s
->fd
);
467 g_free(state
->opaque
);
468 state
->opaque
= NULL
;
472 /* XXX: use host sector size if necessary with:
473 #ifdef DIOCGSECTORSIZE
475 unsigned int sectorsize = 512;
476 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
477 sectorsize > bufsize)
478 bufsize = sectorsize;
482 uint32_t blockSize = 512;
483 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
489 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
493 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
501 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
505 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
514 static bool preadv_present
= true;
517 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
519 return preadv(fd
, iov
, nr_iov
, offset
);
523 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
525 return pwritev(fd
, iov
, nr_iov
, offset
);
530 static bool preadv_present
= false;
533 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
539 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
546 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
551 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
552 len
= qemu_pwritev(aiocb
->aio_fildes
,
557 len
= qemu_preadv(aiocb
->aio_fildes
,
561 } while (len
== -1 && errno
== EINTR
);
570 * Read/writes the data to/from a given linear buffer.
572 * Returns the number of bytes handles or -errno in case of an error. Short
573 * reads are only returned if the end of the file is reached.
575 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
580 while (offset
< aiocb
->aio_nbytes
) {
581 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
582 len
= pwrite(aiocb
->aio_fildes
,
583 (const char *)buf
+ offset
,
584 aiocb
->aio_nbytes
- offset
,
585 aiocb
->aio_offset
+ offset
);
587 len
= pread(aiocb
->aio_fildes
,
589 aiocb
->aio_nbytes
- offset
,
590 aiocb
->aio_offset
+ offset
);
592 if (len
== -1 && errno
== EINTR
) {
594 } else if (len
== -1) {
597 } else if (len
== 0) {
606 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
611 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
613 * If there is just a single buffer, and it is properly aligned
614 * we can just use plain pread/pwrite without any problems.
616 if (aiocb
->aio_niov
== 1) {
617 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
620 * We have more than one iovec, and all are properly aligned.
622 * Try preadv/pwritev first and fall back to linearizing the
623 * buffer if it's not supported.
625 if (preadv_present
) {
626 nbytes
= handle_aiocb_rw_vector(aiocb
);
627 if (nbytes
== aiocb
->aio_nbytes
||
628 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
631 preadv_present
= false;
635 * XXX(hch): short read/write. no easy way to handle the reminder
636 * using these interfaces. For now retry using plain
642 * Ok, we have to do it the hard way, copy all segments into
643 * a single aligned buffer.
645 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
646 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
650 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
651 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
652 p
+= aiocb
->aio_iov
[i
].iov_len
;
656 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
657 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
659 size_t count
= aiocb
->aio_nbytes
, copy
;
662 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
664 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
665 copy
= aiocb
->aio_iov
[i
].iov_len
;
667 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
678 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
680 struct xfs_flock64 fl
;
682 memset(&fl
, 0, sizeof(fl
));
683 fl
.l_whence
= SEEK_SET
;
687 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
688 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
696 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
698 int ret
= -EOPNOTSUPP
;
699 BDRVRawState
*s
= aiocb
->bs
->opaque
;
701 if (s
->has_discard
== 0) {
705 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
708 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
709 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
712 } while (errno
== EINTR
);
719 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
723 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
725 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
726 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
729 } while (errno
== EINTR
);
735 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
743 static int aio_worker(void *arg
)
745 RawPosixAIOData
*aiocb
= arg
;
748 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
750 ret
= handle_aiocb_rw(aiocb
);
751 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
752 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
753 0, aiocb
->aio_nbytes
- ret
);
755 ret
= aiocb
->aio_nbytes
;
757 if (ret
== aiocb
->aio_nbytes
) {
759 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
764 ret
= handle_aiocb_rw(aiocb
);
765 if (ret
== aiocb
->aio_nbytes
) {
767 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
772 ret
= handle_aiocb_flush(aiocb
);
775 ret
= handle_aiocb_ioctl(aiocb
);
777 case QEMU_AIO_DISCARD
:
778 ret
= handle_aiocb_discard(aiocb
);
781 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
786 g_slice_free(RawPosixAIOData
, aiocb
);
790 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
791 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
792 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
794 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
798 acb
->aio_type
= type
;
799 acb
->aio_fildes
= fd
;
802 acb
->aio_iov
= qiov
->iov
;
803 acb
->aio_niov
= qiov
->niov
;
805 acb
->aio_nbytes
= nb_sectors
* 512;
806 acb
->aio_offset
= sector_num
* 512;
808 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
809 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
810 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
813 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
814 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
815 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
817 BDRVRawState
*s
= bs
->opaque
;
823 * If O_DIRECT is used the buffer needs to be aligned on a sector
824 * boundary. Check if this is the case or tell the low-level
825 * driver that it needs to copy the buffer.
827 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
828 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
829 type
|= QEMU_AIO_MISALIGNED
;
830 #ifdef CONFIG_LINUX_AIO
831 } else if (s
->use_aio
) {
832 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
833 nb_sectors
, cb
, opaque
, type
);
838 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
842 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
843 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
844 BlockDriverCompletionFunc
*cb
, void *opaque
)
846 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
847 cb
, opaque
, QEMU_AIO_READ
);
850 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
851 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
852 BlockDriverCompletionFunc
*cb
, void *opaque
)
854 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
855 cb
, opaque
, QEMU_AIO_WRITE
);
858 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
859 BlockDriverCompletionFunc
*cb
, void *opaque
)
861 BDRVRawState
*s
= bs
->opaque
;
866 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
869 static void raw_close(BlockDriverState
*bs
)
871 BDRVRawState
*s
= bs
->opaque
;
878 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
880 BDRVRawState
*s
= bs
->opaque
;
883 if (fstat(s
->fd
, &st
)) {
887 if (S_ISREG(st
.st_mode
)) {
888 if (ftruncate(s
->fd
, offset
) < 0) {
891 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
892 if (offset
> raw_getlength(bs
)) {
903 static int64_t raw_getlength(BlockDriverState
*bs
)
905 BDRVRawState
*s
= bs
->opaque
;
911 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
914 if (ioctl(fd
, DIOCGDINFO
, &dl
))
916 return (uint64_t)dl
.d_secsize
*
917 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
921 #elif defined(__NetBSD__)
922 static int64_t raw_getlength(BlockDriverState
*bs
)
924 BDRVRawState
*s
= bs
->opaque
;
930 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
931 struct dkwedge_info dkw
;
933 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
934 return dkw
.dkw_size
* 512;
938 if (ioctl(fd
, DIOCGDINFO
, &dl
))
940 return (uint64_t)dl
.d_secsize
*
941 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
946 #elif defined(__sun__)
947 static int64_t raw_getlength(BlockDriverState
*bs
)
949 BDRVRawState
*s
= bs
->opaque
;
950 struct dk_minfo minfo
;
959 * Use the DKIOCGMEDIAINFO ioctl to read the size.
961 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
963 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
967 * There are reports that lseek on some devices fails, but
968 * irc discussion said that contingency on contingency was overkill.
970 return lseek(s
->fd
, 0, SEEK_END
);
972 #elif defined(CONFIG_BSD)
973 static int64_t raw_getlength(BlockDriverState
*bs
)
975 BDRVRawState
*s
= bs
->opaque
;
979 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
988 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
991 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
992 #ifdef DIOCGMEDIASIZE
993 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
994 #elif defined(DIOCGPART)
997 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
998 size
= pi
.media_size
;
1004 #if defined(__APPLE__) && defined(__MACH__)
1005 size
= LONG_LONG_MAX
;
1007 size
= lseek(fd
, 0LL, SEEK_END
);
1009 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1012 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1013 if (size
== 2048LL * (unsigned)-1)
1015 /* XXX no disc? maybe we need to reopen... */
1016 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
1023 size
= lseek(fd
, 0, SEEK_END
);
1028 static int64_t raw_getlength(BlockDriverState
*bs
)
1030 BDRVRawState
*s
= bs
->opaque
;
1038 return lseek(s
->fd
, 0, SEEK_END
);
1042 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1045 BDRVRawState
*s
= bs
->opaque
;
1047 if (fstat(s
->fd
, &st
) < 0) {
1050 return (int64_t)st
.st_blocks
* 512;
1053 static int raw_create(const char *filename
, QEMUOptionParameter
*options
,
1058 int64_t total_size
= 0;
1060 /* Read out options */
1061 while (options
&& options
->name
) {
1062 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1063 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1068 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1072 error_setg_errno(errp
, -result
, "Could not create file");
1074 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1076 error_setg_errno(errp
, -result
, "Could not resize file");
1078 if (qemu_close(fd
) != 0) {
1080 error_setg_errno(errp
, -result
, "Could not close the new file");
1087 * Returns true iff the specified sector is present in the disk image. Drivers
1088 * not implementing the functionality are assumed to not support backing files,
1089 * hence all their sectors are reported as allocated.
1091 * If 'sector_num' is beyond the end of the disk image the return value is 0
1092 * and 'pnum' is set to 0.
1094 * 'pnum' is set to the number of sectors (including and immediately following
1095 * the specified sector) that are known to be in the same
1096 * allocated/unallocated state.
1098 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1099 * beyond the end of the disk image it will be clamped.
1101 static int64_t coroutine_fn
raw_co_get_block_status(BlockDriverState
*bs
,
1103 int nb_sectors
, int *pnum
)
1105 off_t start
, data
, hole
;
1113 start
= sector_num
* BDRV_SECTOR_SIZE
;
1114 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
1116 #ifdef CONFIG_FIEMAP
1118 BDRVRawState
*s
= bs
->opaque
;
1121 struct fiemap_extent fe
;
1124 f
.fm
.fm_start
= start
;
1125 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1127 f
.fm
.fm_extent_count
= 1;
1128 f
.fm
.fm_reserved
= 0;
1129 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1130 /* Assume everything is allocated. */
1135 if (f
.fm
.fm_mapped_extents
== 0) {
1136 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1137 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1139 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1140 hole
= f
.fm
.fm_start
;
1141 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1143 data
= f
.fe
.fe_logical
;
1144 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1145 if (f
.fe
.fe_flags
& FIEMAP_EXTENT_UNWRITTEN
) {
1146 ret
|= BDRV_BLOCK_ZERO
;
1150 #elif defined SEEK_HOLE && defined SEEK_DATA
1152 BDRVRawState
*s
= bs
->opaque
;
1154 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1156 /* -ENXIO indicates that sector_num was past the end of the file.
1157 * There is a virtual hole there. */
1158 assert(errno
!= -ENXIO
);
1160 /* Most likely EINVAL. Assume everything is allocated. */
1168 /* On a hole. We need another syscall to find its end. */
1169 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1171 data
= lseek(s
->fd
, 0, SEEK_END
);
1176 hole
= start
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1179 if (data
<= start
) {
1180 /* On a data extent, compute sectors to the end of the extent. */
1181 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1183 /* On a hole, compute sectors to the beginning of the next extent. */
1184 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1185 ret
&= ~BDRV_BLOCK_DATA
;
1186 ret
|= BDRV_BLOCK_ZERO
;
1192 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1193 int64_t sector_num
, int nb_sectors
,
1194 BlockDriverCompletionFunc
*cb
, void *opaque
)
1196 BDRVRawState
*s
= bs
->opaque
;
1198 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1199 cb
, opaque
, QEMU_AIO_DISCARD
);
1202 static QEMUOptionParameter raw_create_options
[] = {
1204 .name
= BLOCK_OPT_SIZE
,
1206 .help
= "Virtual disk size"
1211 static BlockDriver bdrv_file
= {
1212 .format_name
= "file",
1213 .protocol_name
= "file",
1214 .instance_size
= sizeof(BDRVRawState
),
1215 .bdrv_needs_filename
= true,
1216 .bdrv_probe
= NULL
, /* no probe for protocols */
1217 .bdrv_file_open
= raw_open
,
1218 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1219 .bdrv_reopen_commit
= raw_reopen_commit
,
1220 .bdrv_reopen_abort
= raw_reopen_abort
,
1221 .bdrv_close
= raw_close
,
1222 .bdrv_create
= raw_create
,
1223 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1224 .bdrv_co_get_block_status
= raw_co_get_block_status
,
1226 .bdrv_aio_readv
= raw_aio_readv
,
1227 .bdrv_aio_writev
= raw_aio_writev
,
1228 .bdrv_aio_flush
= raw_aio_flush
,
1229 .bdrv_aio_discard
= raw_aio_discard
,
1231 .bdrv_truncate
= raw_truncate
,
1232 .bdrv_getlength
= raw_getlength
,
1233 .bdrv_get_allocated_file_size
1234 = raw_get_allocated_file_size
,
1236 .create_options
= raw_create_options
,
1239 /***********************************************/
1242 #if defined(__APPLE__) && defined(__MACH__)
1243 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1244 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1246 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1248 kern_return_t kernResult
;
1249 mach_port_t masterPort
;
1250 CFMutableDictionaryRef classesToMatch
;
1252 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1253 if ( KERN_SUCCESS
!= kernResult
) {
1254 printf( "IOMasterPort returned %d\n", kernResult
);
1257 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1258 if ( classesToMatch
== NULL
) {
1259 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1261 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1263 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1264 if ( KERN_SUCCESS
!= kernResult
)
1266 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1272 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1274 io_object_t nextMedia
;
1275 kern_return_t kernResult
= KERN_FAILURE
;
1277 nextMedia
= IOIteratorNext( mediaIterator
);
1280 CFTypeRef bsdPathAsCFString
;
1281 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1282 if ( bsdPathAsCFString
) {
1283 size_t devPathLength
;
1284 strcpy( bsdPath
, _PATH_DEV
);
1285 strcat( bsdPath
, "r" );
1286 devPathLength
= strlen( bsdPath
);
1287 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1288 kernResult
= KERN_SUCCESS
;
1290 CFRelease( bsdPathAsCFString
);
1292 IOObjectRelease( nextMedia
);
1300 static int hdev_probe_device(const char *filename
)
1304 /* allow a dedicated CD-ROM driver to match with a higher priority */
1305 if (strstart(filename
, "/dev/cdrom", NULL
))
1308 if (stat(filename
, &st
) >= 0 &&
1309 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1316 static int check_hdev_writable(BDRVRawState
*s
)
1318 #if defined(BLKROGET)
1319 /* Linux block devices can be configured "read-only" using blockdev(8).
1320 * This is independent of device node permissions and therefore open(2)
1321 * with O_RDWR succeeds. Actual writes fail with EPERM.
1323 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1324 * check for read-only block devices so that Linux block devices behave
1330 if (fstat(s
->fd
, &st
)) {
1334 if (!S_ISBLK(st
.st_mode
)) {
1338 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1345 #endif /* defined(BLKROGET) */
1349 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1352 BDRVRawState
*s
= bs
->opaque
;
1353 Error
*local_err
= NULL
;
1355 const char *filename
= qdict_get_str(options
, "filename");
1357 #if defined(__APPLE__) && defined(__MACH__)
1358 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1359 kern_return_t kernResult
;
1360 io_iterator_t mediaIterator
;
1361 char bsdPath
[ MAXPATHLEN
];
1364 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1365 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1367 if ( bsdPath
[ 0 ] != '\0' ) {
1368 strcat(bsdPath
,"s0");
1369 /* some CDs don't have a partition 0 */
1370 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1372 bsdPath
[strlen(bsdPath
)-1] = '1';
1377 qdict_put(options
, "filename", qstring_from_str(filename
));
1380 if ( mediaIterator
)
1381 IOObjectRelease( mediaIterator
);
1385 s
->type
= FTYPE_FILE
;
1386 #if defined(__linux__)
1388 char resolved_path
[ MAXPATHLEN
], *temp
;
1390 temp
= realpath(filename
, resolved_path
);
1391 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1397 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
1399 if (error_is_set(&local_err
)) {
1400 error_propagate(errp
, local_err
);
1405 if (flags
& BDRV_O_RDWR
) {
1406 ret
= check_hdev_writable(s
);
1409 error_setg_errno(errp
, -ret
, "The device is not writable");
1417 #if defined(__linux__)
1418 /* Note: we do not have a reliable method to detect if the floppy is
1419 present. The current method is to try to open the floppy at every
1420 I/O and to keep it opened during a few hundreds of ms. */
1421 static int fd_open(BlockDriverState
*bs
)
1423 BDRVRawState
*s
= bs
->opaque
;
1424 int last_media_present
;
1426 if (s
->type
!= FTYPE_FD
)
1428 last_media_present
= (s
->fd
>= 0);
1430 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1434 printf("Floppy closed\n");
1438 if (s
->fd_got_error
&&
1439 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1441 printf("No floppy (open delayed)\n");
1445 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1447 s
->fd_error_time
= get_clock();
1448 s
->fd_got_error
= 1;
1449 if (last_media_present
)
1450 s
->fd_media_changed
= 1;
1452 printf("No floppy\n");
1457 printf("Floppy opened\n");
1460 if (!last_media_present
)
1461 s
->fd_media_changed
= 1;
1462 s
->fd_open_time
= get_clock();
1463 s
->fd_got_error
= 0;
1467 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1469 BDRVRawState
*s
= bs
->opaque
;
1471 return ioctl(s
->fd
, req
, buf
);
1474 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1475 unsigned long int req
, void *buf
,
1476 BlockDriverCompletionFunc
*cb
, void *opaque
)
1478 BDRVRawState
*s
= bs
->opaque
;
1479 RawPosixAIOData
*acb
;
1482 if (fd_open(bs
) < 0)
1485 acb
= g_slice_new(RawPosixAIOData
);
1487 acb
->aio_type
= QEMU_AIO_IOCTL
;
1488 acb
->aio_fildes
= s
->fd
;
1489 acb
->aio_offset
= 0;
1490 acb
->aio_ioctl_buf
= buf
;
1491 acb
->aio_ioctl_cmd
= req
;
1492 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1493 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1496 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1497 static int fd_open(BlockDriverState
*bs
)
1499 BDRVRawState
*s
= bs
->opaque
;
1501 /* this is just to ensure s->fd is sane (its called by io ops) */
1506 #else /* !linux && !FreeBSD */
1508 static int fd_open(BlockDriverState
*bs
)
1513 #endif /* !linux && !FreeBSD */
1515 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1516 int64_t sector_num
, int nb_sectors
,
1517 BlockDriverCompletionFunc
*cb
, void *opaque
)
1519 BDRVRawState
*s
= bs
->opaque
;
1521 if (fd_open(bs
) < 0) {
1524 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1525 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1528 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
,
1533 struct stat stat_buf
;
1534 int64_t total_size
= 0;
1536 /* Read out options */
1537 while (options
&& options
->name
) {
1538 if (!strcmp(options
->name
, "size")) {
1539 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1544 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1547 error_setg_errno(errp
, -ret
, "Could not open device");
1551 if (fstat(fd
, &stat_buf
) < 0) {
1553 error_setg_errno(errp
, -ret
, "Could not stat device");
1554 } else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
)) {
1556 "The given file is neither a block nor a character device");
1558 } else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
) {
1559 error_setg(errp
, "Device is too small");
1567 static BlockDriver bdrv_host_device
= {
1568 .format_name
= "host_device",
1569 .protocol_name
= "host_device",
1570 .instance_size
= sizeof(BDRVRawState
),
1571 .bdrv_needs_filename
= true,
1572 .bdrv_probe_device
= hdev_probe_device
,
1573 .bdrv_file_open
= hdev_open
,
1574 .bdrv_close
= raw_close
,
1575 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1576 .bdrv_reopen_commit
= raw_reopen_commit
,
1577 .bdrv_reopen_abort
= raw_reopen_abort
,
1578 .bdrv_create
= hdev_create
,
1579 .create_options
= raw_create_options
,
1581 .bdrv_aio_readv
= raw_aio_readv
,
1582 .bdrv_aio_writev
= raw_aio_writev
,
1583 .bdrv_aio_flush
= raw_aio_flush
,
1584 .bdrv_aio_discard
= hdev_aio_discard
,
1586 .bdrv_truncate
= raw_truncate
,
1587 .bdrv_getlength
= raw_getlength
,
1588 .bdrv_get_allocated_file_size
1589 = raw_get_allocated_file_size
,
1591 /* generic scsi device */
1593 .bdrv_ioctl
= hdev_ioctl
,
1594 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1599 static int floppy_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1602 BDRVRawState
*s
= bs
->opaque
;
1603 Error
*local_err
= NULL
;
1608 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1609 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
1611 if (error_is_set(&local_err
)) {
1612 error_propagate(errp
, local_err
);
1617 /* close fd so that we can reopen it as needed */
1620 s
->fd_media_changed
= 1;
1625 static int floppy_probe_device(const char *filename
)
1629 struct floppy_struct fdparam
;
1632 if (strstart(filename
, "/dev/fd", NULL
) &&
1633 !strstart(filename
, "/dev/fdset/", NULL
)) {
1637 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1641 ret
= fstat(fd
, &st
);
1642 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1646 /* Attempt to detect via a floppy specific ioctl */
1647 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1658 static int floppy_is_inserted(BlockDriverState
*bs
)
1660 return fd_open(bs
) >= 0;
1663 static int floppy_media_changed(BlockDriverState
*bs
)
1665 BDRVRawState
*s
= bs
->opaque
;
1669 * XXX: we do not have a true media changed indication.
1670 * It does not work if the floppy is changed without trying to read it.
1673 ret
= s
->fd_media_changed
;
1674 s
->fd_media_changed
= 0;
1676 printf("Floppy changed=%d\n", ret
);
1681 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1683 BDRVRawState
*s
= bs
->opaque
;
1690 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1692 if (ioctl(fd
, FDEJECT
, 0) < 0)
1698 static BlockDriver bdrv_host_floppy
= {
1699 .format_name
= "host_floppy",
1700 .protocol_name
= "host_floppy",
1701 .instance_size
= sizeof(BDRVRawState
),
1702 .bdrv_needs_filename
= true,
1703 .bdrv_probe_device
= floppy_probe_device
,
1704 .bdrv_file_open
= floppy_open
,
1705 .bdrv_close
= raw_close
,
1706 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1707 .bdrv_reopen_commit
= raw_reopen_commit
,
1708 .bdrv_reopen_abort
= raw_reopen_abort
,
1709 .bdrv_create
= hdev_create
,
1710 .create_options
= raw_create_options
,
1712 .bdrv_aio_readv
= raw_aio_readv
,
1713 .bdrv_aio_writev
= raw_aio_writev
,
1714 .bdrv_aio_flush
= raw_aio_flush
,
1716 .bdrv_truncate
= raw_truncate
,
1717 .bdrv_getlength
= raw_getlength
,
1718 .has_variable_length
= true,
1719 .bdrv_get_allocated_file_size
1720 = raw_get_allocated_file_size
,
1722 /* removable device support */
1723 .bdrv_is_inserted
= floppy_is_inserted
,
1724 .bdrv_media_changed
= floppy_media_changed
,
1725 .bdrv_eject
= floppy_eject
,
1728 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1731 BDRVRawState
*s
= bs
->opaque
;
1732 Error
*local_err
= NULL
;
1737 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1738 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
1739 if (error_is_set(&local_err
)) {
1740 error_propagate(errp
, local_err
);
1745 static int cdrom_probe_device(const char *filename
)
1751 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1755 ret
= fstat(fd
, &st
);
1756 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1760 /* Attempt to detect via a CDROM specific ioctl */
1761 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1771 static int cdrom_is_inserted(BlockDriverState
*bs
)
1773 BDRVRawState
*s
= bs
->opaque
;
1776 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1777 if (ret
== CDS_DISC_OK
)
1782 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1784 BDRVRawState
*s
= bs
->opaque
;
1787 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1788 perror("CDROMEJECT");
1790 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1791 perror("CDROMEJECT");
1795 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1797 BDRVRawState
*s
= bs
->opaque
;
1799 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1801 * Note: an error can happen if the distribution automatically
1804 /* perror("CDROM_LOCKDOOR"); */
1808 static BlockDriver bdrv_host_cdrom
= {
1809 .format_name
= "host_cdrom",
1810 .protocol_name
= "host_cdrom",
1811 .instance_size
= sizeof(BDRVRawState
),
1812 .bdrv_needs_filename
= true,
1813 .bdrv_probe_device
= cdrom_probe_device
,
1814 .bdrv_file_open
= cdrom_open
,
1815 .bdrv_close
= raw_close
,
1816 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1817 .bdrv_reopen_commit
= raw_reopen_commit
,
1818 .bdrv_reopen_abort
= raw_reopen_abort
,
1819 .bdrv_create
= hdev_create
,
1820 .create_options
= raw_create_options
,
1822 .bdrv_aio_readv
= raw_aio_readv
,
1823 .bdrv_aio_writev
= raw_aio_writev
,
1824 .bdrv_aio_flush
= raw_aio_flush
,
1826 .bdrv_truncate
= raw_truncate
,
1827 .bdrv_getlength
= raw_getlength
,
1828 .has_variable_length
= true,
1829 .bdrv_get_allocated_file_size
1830 = raw_get_allocated_file_size
,
1832 /* removable device support */
1833 .bdrv_is_inserted
= cdrom_is_inserted
,
1834 .bdrv_eject
= cdrom_eject
,
1835 .bdrv_lock_medium
= cdrom_lock_medium
,
1837 /* generic scsi device */
1838 .bdrv_ioctl
= hdev_ioctl
,
1839 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1841 #endif /* __linux__ */
1843 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1844 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
);