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 int raw_open_common(BlockDriverState
*bs
, const char *filename
,
266 int bdrv_flags
, int open_flags
)
268 BDRVRawState
*s
= bs
->opaque
;
271 ret
= raw_normalize_devicepath(&filename
);
276 s
->open_flags
= open_flags
;
277 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
280 fd
= qemu_open(filename
, s
->open_flags
, 0644);
289 #ifdef CONFIG_LINUX_AIO
290 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
298 if (platform_test_xfs_fd(s
->fd
)) {
306 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
308 BDRVRawState
*s
= bs
->opaque
;
310 s
->type
= FTYPE_FILE
;
311 return raw_open_common(bs
, filename
, flags
, 0);
314 static int raw_reopen_prepare(BDRVReopenState
*state
,
315 BlockReopenQueue
*queue
, Error
**errp
)
318 BDRVRawReopenState
*raw_s
;
321 assert(state
!= NULL
);
322 assert(state
->bs
!= NULL
);
324 s
= state
->bs
->opaque
;
326 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
327 raw_s
= state
->opaque
;
329 #ifdef CONFIG_LINUX_AIO
330 raw_s
->use_aio
= s
->use_aio
;
332 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
333 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
334 * won't override aio_ctx if aio_ctx is non-NULL */
335 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
340 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
341 raw_s
->open_flags
|= O_NONBLOCK
;
344 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
348 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
350 fcntl_flags
|= O_NOATIME
;
354 /* Not all operating systems have O_ASYNC, and those that don't
355 * will not let us track the state into raw_s->open_flags (typically
356 * you achieve the same effect with an ioctl, for example I_SETSIG
357 * on Solaris). But we do not use O_ASYNC, so that's fine.
359 assert((s
->open_flags
& O_ASYNC
) == 0);
362 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
363 /* dup the original fd */
364 /* TODO: use qemu fcntl wrapper */
365 #ifdef F_DUPFD_CLOEXEC
366 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
368 raw_s
->fd
= dup(s
->fd
);
369 if (raw_s
->fd
!= -1) {
370 qemu_set_cloexec(raw_s
->fd
);
373 if (raw_s
->fd
>= 0) {
374 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
376 qemu_close(raw_s
->fd
);
382 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
383 if (raw_s
->fd
== -1) {
384 assert(!(raw_s
->open_flags
& O_CREAT
));
385 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
386 if (raw_s
->fd
== -1) {
394 static void raw_reopen_commit(BDRVReopenState
*state
)
396 BDRVRawReopenState
*raw_s
= state
->opaque
;
397 BDRVRawState
*s
= state
->bs
->opaque
;
399 s
->open_flags
= raw_s
->open_flags
;
403 #ifdef CONFIG_LINUX_AIO
404 s
->use_aio
= raw_s
->use_aio
;
407 g_free(state
->opaque
);
408 state
->opaque
= NULL
;
412 static void raw_reopen_abort(BDRVReopenState
*state
)
414 BDRVRawReopenState
*raw_s
= state
->opaque
;
416 /* nothing to do if NULL, we didn't get far enough */
421 if (raw_s
->fd
>= 0) {
422 qemu_close(raw_s
->fd
);
425 g_free(state
->opaque
);
426 state
->opaque
= NULL
;
430 /* XXX: use host sector size if necessary with:
431 #ifdef DIOCGSECTORSIZE
433 unsigned int sectorsize = 512;
434 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
435 sectorsize > bufsize)
436 bufsize = sectorsize;
440 uint32_t blockSize = 512;
441 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
447 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
451 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
459 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
463 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
472 static bool preadv_present
= true;
475 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
477 return preadv(fd
, iov
, nr_iov
, offset
);
481 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
483 return pwritev(fd
, iov
, nr_iov
, offset
);
488 static bool preadv_present
= false;
491 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
497 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
504 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
509 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
510 len
= qemu_pwritev(aiocb
->aio_fildes
,
515 len
= qemu_preadv(aiocb
->aio_fildes
,
519 } while (len
== -1 && errno
== EINTR
);
528 * Read/writes the data to/from a given linear buffer.
530 * Returns the number of bytes handles or -errno in case of an error. Short
531 * reads are only returned if the end of the file is reached.
533 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
538 while (offset
< aiocb
->aio_nbytes
) {
539 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
540 len
= pwrite(aiocb
->aio_fildes
,
541 (const char *)buf
+ offset
,
542 aiocb
->aio_nbytes
- offset
,
543 aiocb
->aio_offset
+ offset
);
545 len
= pread(aiocb
->aio_fildes
,
547 aiocb
->aio_nbytes
- offset
,
548 aiocb
->aio_offset
+ offset
);
550 if (len
== -1 && errno
== EINTR
) {
552 } else if (len
== -1) {
555 } else if (len
== 0) {
564 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
569 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
571 * If there is just a single buffer, and it is properly aligned
572 * we can just use plain pread/pwrite without any problems.
574 if (aiocb
->aio_niov
== 1) {
575 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
578 * We have more than one iovec, and all are properly aligned.
580 * Try preadv/pwritev first and fall back to linearizing the
581 * buffer if it's not supported.
583 if (preadv_present
) {
584 nbytes
= handle_aiocb_rw_vector(aiocb
);
585 if (nbytes
== aiocb
->aio_nbytes
||
586 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
589 preadv_present
= false;
593 * XXX(hch): short read/write. no easy way to handle the reminder
594 * using these interfaces. For now retry using plain
600 * Ok, we have to do it the hard way, copy all segments into
601 * a single aligned buffer.
603 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
604 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
608 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
609 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
610 p
+= aiocb
->aio_iov
[i
].iov_len
;
614 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
615 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
617 size_t count
= aiocb
->aio_nbytes
, copy
;
620 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
622 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
623 copy
= aiocb
->aio_iov
[i
].iov_len
;
625 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
636 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
638 struct xfs_flock64 fl
;
640 memset(&fl
, 0, sizeof(fl
));
641 fl
.l_whence
= SEEK_SET
;
645 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
646 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
654 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
656 int ret
= -EOPNOTSUPP
;
657 BDRVRawState
*s
= aiocb
->bs
->opaque
;
659 if (s
->has_discard
== 0) {
663 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
666 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
667 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
670 } while (errno
== EINTR
);
677 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
681 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
683 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
684 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
687 } while (errno
== EINTR
);
693 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
701 static int aio_worker(void *arg
)
703 RawPosixAIOData
*aiocb
= arg
;
706 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
708 ret
= handle_aiocb_rw(aiocb
);
709 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
710 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
711 0, aiocb
->aio_nbytes
- ret
);
713 ret
= aiocb
->aio_nbytes
;
715 if (ret
== aiocb
->aio_nbytes
) {
717 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
722 ret
= handle_aiocb_rw(aiocb
);
723 if (ret
== aiocb
->aio_nbytes
) {
725 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
730 ret
= handle_aiocb_flush(aiocb
);
733 ret
= handle_aiocb_ioctl(aiocb
);
735 case QEMU_AIO_DISCARD
:
736 ret
= handle_aiocb_discard(aiocb
);
739 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
744 g_slice_free(RawPosixAIOData
, aiocb
);
748 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
749 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
750 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
752 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
755 acb
->aio_type
= type
;
756 acb
->aio_fildes
= fd
;
759 acb
->aio_iov
= qiov
->iov
;
760 acb
->aio_niov
= qiov
->niov
;
762 acb
->aio_nbytes
= nb_sectors
* 512;
763 acb
->aio_offset
= sector_num
* 512;
765 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
766 return thread_pool_submit_aio(aio_worker
, acb
, cb
, opaque
);
769 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
770 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
771 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
773 BDRVRawState
*s
= bs
->opaque
;
779 * If O_DIRECT is used the buffer needs to be aligned on a sector
780 * boundary. Check if this is the case or tell the low-level
781 * driver that it needs to copy the buffer.
783 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
784 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
785 type
|= QEMU_AIO_MISALIGNED
;
786 #ifdef CONFIG_LINUX_AIO
787 } else if (s
->use_aio
) {
788 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
789 nb_sectors
, cb
, opaque
, type
);
794 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
798 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
799 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
800 BlockDriverCompletionFunc
*cb
, void *opaque
)
802 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
803 cb
, opaque
, QEMU_AIO_READ
);
806 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
807 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
808 BlockDriverCompletionFunc
*cb
, void *opaque
)
810 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
811 cb
, opaque
, QEMU_AIO_WRITE
);
814 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
815 BlockDriverCompletionFunc
*cb
, void *opaque
)
817 BDRVRawState
*s
= bs
->opaque
;
822 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
825 static void raw_close(BlockDriverState
*bs
)
827 BDRVRawState
*s
= bs
->opaque
;
834 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
836 BDRVRawState
*s
= bs
->opaque
;
839 if (fstat(s
->fd
, &st
)) {
843 if (S_ISREG(st
.st_mode
)) {
844 if (ftruncate(s
->fd
, offset
) < 0) {
847 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
848 if (offset
> raw_getlength(bs
)) {
859 static int64_t raw_getlength(BlockDriverState
*bs
)
861 BDRVRawState
*s
= bs
->opaque
;
867 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
870 if (ioctl(fd
, DIOCGDINFO
, &dl
))
872 return (uint64_t)dl
.d_secsize
*
873 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
877 #elif defined(__NetBSD__)
878 static int64_t raw_getlength(BlockDriverState
*bs
)
880 BDRVRawState
*s
= bs
->opaque
;
886 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
887 struct dkwedge_info dkw
;
889 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
890 return dkw
.dkw_size
* 512;
894 if (ioctl(fd
, DIOCGDINFO
, &dl
))
896 return (uint64_t)dl
.d_secsize
*
897 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
902 #elif defined(__sun__)
903 static int64_t raw_getlength(BlockDriverState
*bs
)
905 BDRVRawState
*s
= bs
->opaque
;
906 struct dk_minfo minfo
;
915 * Use the DKIOCGMEDIAINFO ioctl to read the size.
917 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
919 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
923 * There are reports that lseek on some devices fails, but
924 * irc discussion said that contingency on contingency was overkill.
926 return lseek(s
->fd
, 0, SEEK_END
);
928 #elif defined(CONFIG_BSD)
929 static int64_t raw_getlength(BlockDriverState
*bs
)
931 BDRVRawState
*s
= bs
->opaque
;
935 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
944 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
947 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
948 #ifdef DIOCGMEDIASIZE
949 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
950 #elif defined(DIOCGPART)
953 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
954 size
= pi
.media_size
;
960 #if defined(__APPLE__) && defined(__MACH__)
961 size
= LONG_LONG_MAX
;
963 size
= lseek(fd
, 0LL, SEEK_END
);
965 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
968 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
969 if (size
== 2048LL * (unsigned)-1)
971 /* XXX no disc? maybe we need to reopen... */
972 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
979 size
= lseek(fd
, 0, SEEK_END
);
984 static int64_t raw_getlength(BlockDriverState
*bs
)
986 BDRVRawState
*s
= bs
->opaque
;
994 return lseek(s
->fd
, 0, SEEK_END
);
998 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1001 BDRVRawState
*s
= bs
->opaque
;
1003 if (fstat(s
->fd
, &st
) < 0) {
1006 return (int64_t)st
.st_blocks
* 512;
1009 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
1013 int64_t total_size
= 0;
1015 /* Read out options */
1016 while (options
&& options
->name
) {
1017 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1018 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1023 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1028 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1031 if (qemu_close(fd
) != 0) {
1039 * Returns true iff the specified sector is present in the disk image. Drivers
1040 * not implementing the functionality are assumed to not support backing files,
1041 * hence all their sectors are reported as allocated.
1043 * If 'sector_num' is beyond the end of the disk image the return value is 0
1044 * and 'pnum' is set to 0.
1046 * 'pnum' is set to the number of sectors (including and immediately following
1047 * the specified sector) that are known to be in the same
1048 * allocated/unallocated state.
1050 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1051 * beyond the end of the disk image it will be clamped.
1053 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
1055 int nb_sectors
, int *pnum
)
1057 off_t start
, data
, hole
;
1065 start
= sector_num
* BDRV_SECTOR_SIZE
;
1067 #ifdef CONFIG_FIEMAP
1069 BDRVRawState
*s
= bs
->opaque
;
1072 struct fiemap_extent fe
;
1075 f
.fm
.fm_start
= start
;
1076 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1078 f
.fm
.fm_extent_count
= 1;
1079 f
.fm
.fm_reserved
= 0;
1080 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1081 /* Assume everything is allocated. */
1086 if (f
.fm
.fm_mapped_extents
== 0) {
1087 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1088 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1090 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1091 hole
= f
.fm
.fm_start
;
1092 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1094 data
= f
.fe
.fe_logical
;
1095 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1098 #elif defined SEEK_HOLE && defined SEEK_DATA
1100 BDRVRawState
*s
= bs
->opaque
;
1102 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1104 /* -ENXIO indicates that sector_num was past the end of the file.
1105 * There is a virtual hole there. */
1106 assert(errno
!= -ENXIO
);
1108 /* Most likely EINVAL. Assume everything is allocated. */
1116 /* On a hole. We need another syscall to find its end. */
1117 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1119 data
= lseek(s
->fd
, 0, SEEK_END
);
1127 if (data
<= start
) {
1128 /* On a data extent, compute sectors to the end of the extent. */
1129 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1132 /* On a hole, compute sectors to the beginning of the next extent. */
1133 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1138 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1139 int64_t sector_num
, int nb_sectors
,
1140 BlockDriverCompletionFunc
*cb
, void *opaque
)
1142 BDRVRawState
*s
= bs
->opaque
;
1144 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1145 cb
, opaque
, QEMU_AIO_DISCARD
);
1148 static QEMUOptionParameter raw_create_options
[] = {
1150 .name
= BLOCK_OPT_SIZE
,
1152 .help
= "Virtual disk size"
1157 static BlockDriver bdrv_file
= {
1158 .format_name
= "file",
1159 .protocol_name
= "file",
1160 .instance_size
= sizeof(BDRVRawState
),
1161 .bdrv_probe
= NULL
, /* no probe for protocols */
1162 .bdrv_file_open
= raw_open
,
1163 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1164 .bdrv_reopen_commit
= raw_reopen_commit
,
1165 .bdrv_reopen_abort
= raw_reopen_abort
,
1166 .bdrv_close
= raw_close
,
1167 .bdrv_create
= raw_create
,
1168 .bdrv_co_is_allocated
= raw_co_is_allocated
,
1170 .bdrv_aio_readv
= raw_aio_readv
,
1171 .bdrv_aio_writev
= raw_aio_writev
,
1172 .bdrv_aio_flush
= raw_aio_flush
,
1173 .bdrv_aio_discard
= raw_aio_discard
,
1175 .bdrv_truncate
= raw_truncate
,
1176 .bdrv_getlength
= raw_getlength
,
1177 .bdrv_get_allocated_file_size
1178 = raw_get_allocated_file_size
,
1180 .create_options
= raw_create_options
,
1183 /***********************************************/
1186 #if defined(__APPLE__) && defined(__MACH__)
1187 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1188 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1190 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1192 kern_return_t kernResult
;
1193 mach_port_t masterPort
;
1194 CFMutableDictionaryRef classesToMatch
;
1196 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1197 if ( KERN_SUCCESS
!= kernResult
) {
1198 printf( "IOMasterPort returned %d\n", kernResult
);
1201 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1202 if ( classesToMatch
== NULL
) {
1203 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1205 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1207 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1208 if ( KERN_SUCCESS
!= kernResult
)
1210 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1216 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1218 io_object_t nextMedia
;
1219 kern_return_t kernResult
= KERN_FAILURE
;
1221 nextMedia
= IOIteratorNext( mediaIterator
);
1224 CFTypeRef bsdPathAsCFString
;
1225 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1226 if ( bsdPathAsCFString
) {
1227 size_t devPathLength
;
1228 strcpy( bsdPath
, _PATH_DEV
);
1229 strcat( bsdPath
, "r" );
1230 devPathLength
= strlen( bsdPath
);
1231 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1232 kernResult
= KERN_SUCCESS
;
1234 CFRelease( bsdPathAsCFString
);
1236 IOObjectRelease( nextMedia
);
1244 static int hdev_probe_device(const char *filename
)
1248 /* allow a dedicated CD-ROM driver to match with a higher priority */
1249 if (strstart(filename
, "/dev/cdrom", NULL
))
1252 if (stat(filename
, &st
) >= 0 &&
1253 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1260 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1262 BDRVRawState
*s
= bs
->opaque
;
1264 #if defined(__APPLE__) && defined(__MACH__)
1265 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1266 kern_return_t kernResult
;
1267 io_iterator_t mediaIterator
;
1268 char bsdPath
[ MAXPATHLEN
];
1271 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1272 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1274 if ( bsdPath
[ 0 ] != '\0' ) {
1275 strcat(bsdPath
,"s0");
1276 /* some CDs don't have a partition 0 */
1277 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1279 bsdPath
[strlen(bsdPath
)-1] = '1';
1286 if ( mediaIterator
)
1287 IOObjectRelease( mediaIterator
);
1291 s
->type
= FTYPE_FILE
;
1292 #if defined(__linux__)
1294 char resolved_path
[ MAXPATHLEN
], *temp
;
1296 temp
= realpath(filename
, resolved_path
);
1297 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1303 return raw_open_common(bs
, filename
, flags
, 0);
1306 #if defined(__linux__)
1307 /* Note: we do not have a reliable method to detect if the floppy is
1308 present. The current method is to try to open the floppy at every
1309 I/O and to keep it opened during a few hundreds of ms. */
1310 static int fd_open(BlockDriverState
*bs
)
1312 BDRVRawState
*s
= bs
->opaque
;
1313 int last_media_present
;
1315 if (s
->type
!= FTYPE_FD
)
1317 last_media_present
= (s
->fd
>= 0);
1319 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1323 printf("Floppy closed\n");
1327 if (s
->fd_got_error
&&
1328 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1330 printf("No floppy (open delayed)\n");
1334 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1336 s
->fd_error_time
= get_clock();
1337 s
->fd_got_error
= 1;
1338 if (last_media_present
)
1339 s
->fd_media_changed
= 1;
1341 printf("No floppy\n");
1346 printf("Floppy opened\n");
1349 if (!last_media_present
)
1350 s
->fd_media_changed
= 1;
1351 s
->fd_open_time
= get_clock();
1352 s
->fd_got_error
= 0;
1356 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1358 BDRVRawState
*s
= bs
->opaque
;
1360 return ioctl(s
->fd
, req
, buf
);
1363 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1364 unsigned long int req
, void *buf
,
1365 BlockDriverCompletionFunc
*cb
, void *opaque
)
1367 BDRVRawState
*s
= bs
->opaque
;
1368 RawPosixAIOData
*acb
;
1370 if (fd_open(bs
) < 0)
1373 acb
= g_slice_new(RawPosixAIOData
);
1375 acb
->aio_type
= QEMU_AIO_IOCTL
;
1376 acb
->aio_fildes
= s
->fd
;
1377 acb
->aio_offset
= 0;
1378 acb
->aio_ioctl_buf
= buf
;
1379 acb
->aio_ioctl_cmd
= req
;
1380 return thread_pool_submit_aio(aio_worker
, acb
, cb
, opaque
);
1383 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1384 static int fd_open(BlockDriverState
*bs
)
1386 BDRVRawState
*s
= bs
->opaque
;
1388 /* this is just to ensure s->fd is sane (its called by io ops) */
1393 #else /* !linux && !FreeBSD */
1395 static int fd_open(BlockDriverState
*bs
)
1400 #endif /* !linux && !FreeBSD */
1402 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1403 int64_t sector_num
, int nb_sectors
,
1404 BlockDriverCompletionFunc
*cb
, void *opaque
)
1406 BDRVRawState
*s
= bs
->opaque
;
1408 if (fd_open(bs
) < 0) {
1411 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1412 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1415 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1419 struct stat stat_buf
;
1420 int64_t total_size
= 0;
1422 /* Read out options */
1423 while (options
&& options
->name
) {
1424 if (!strcmp(options
->name
, "size")) {
1425 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1430 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1434 if (fstat(fd
, &stat_buf
) < 0)
1436 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1438 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1445 static int hdev_has_zero_init(BlockDriverState
*bs
)
1450 static BlockDriver bdrv_host_device
= {
1451 .format_name
= "host_device",
1452 .protocol_name
= "host_device",
1453 .instance_size
= sizeof(BDRVRawState
),
1454 .bdrv_probe_device
= hdev_probe_device
,
1455 .bdrv_file_open
= hdev_open
,
1456 .bdrv_close
= raw_close
,
1457 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1458 .bdrv_reopen_commit
= raw_reopen_commit
,
1459 .bdrv_reopen_abort
= raw_reopen_abort
,
1460 .bdrv_create
= hdev_create
,
1461 .create_options
= raw_create_options
,
1462 .bdrv_has_zero_init
= hdev_has_zero_init
,
1464 .bdrv_aio_readv
= raw_aio_readv
,
1465 .bdrv_aio_writev
= raw_aio_writev
,
1466 .bdrv_aio_flush
= raw_aio_flush
,
1467 .bdrv_aio_discard
= hdev_aio_discard
,
1469 .bdrv_truncate
= raw_truncate
,
1470 .bdrv_getlength
= raw_getlength
,
1471 .bdrv_get_allocated_file_size
1472 = raw_get_allocated_file_size
,
1474 /* generic scsi device */
1476 .bdrv_ioctl
= hdev_ioctl
,
1477 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1482 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1484 BDRVRawState
*s
= bs
->opaque
;
1489 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1490 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1494 /* close fd so that we can reopen it as needed */
1497 s
->fd_media_changed
= 1;
1502 static int floppy_probe_device(const char *filename
)
1506 struct floppy_struct fdparam
;
1509 if (strstart(filename
, "/dev/fd", NULL
) &&
1510 !strstart(filename
, "/dev/fdset/", NULL
)) {
1514 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1518 ret
= fstat(fd
, &st
);
1519 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1523 /* Attempt to detect via a floppy specific ioctl */
1524 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1535 static int floppy_is_inserted(BlockDriverState
*bs
)
1537 return fd_open(bs
) >= 0;
1540 static int floppy_media_changed(BlockDriverState
*bs
)
1542 BDRVRawState
*s
= bs
->opaque
;
1546 * XXX: we do not have a true media changed indication.
1547 * It does not work if the floppy is changed without trying to read it.
1550 ret
= s
->fd_media_changed
;
1551 s
->fd_media_changed
= 0;
1553 printf("Floppy changed=%d\n", ret
);
1558 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1560 BDRVRawState
*s
= bs
->opaque
;
1567 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1569 if (ioctl(fd
, FDEJECT
, 0) < 0)
1575 static BlockDriver bdrv_host_floppy
= {
1576 .format_name
= "host_floppy",
1577 .protocol_name
= "host_floppy",
1578 .instance_size
= sizeof(BDRVRawState
),
1579 .bdrv_probe_device
= floppy_probe_device
,
1580 .bdrv_file_open
= floppy_open
,
1581 .bdrv_close
= raw_close
,
1582 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1583 .bdrv_reopen_commit
= raw_reopen_commit
,
1584 .bdrv_reopen_abort
= raw_reopen_abort
,
1585 .bdrv_create
= hdev_create
,
1586 .create_options
= raw_create_options
,
1587 .bdrv_has_zero_init
= hdev_has_zero_init
,
1589 .bdrv_aio_readv
= raw_aio_readv
,
1590 .bdrv_aio_writev
= raw_aio_writev
,
1591 .bdrv_aio_flush
= raw_aio_flush
,
1593 .bdrv_truncate
= raw_truncate
,
1594 .bdrv_getlength
= raw_getlength
,
1595 .bdrv_get_allocated_file_size
1596 = raw_get_allocated_file_size
,
1598 /* removable device support */
1599 .bdrv_is_inserted
= floppy_is_inserted
,
1600 .bdrv_media_changed
= floppy_media_changed
,
1601 .bdrv_eject
= floppy_eject
,
1604 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1606 BDRVRawState
*s
= bs
->opaque
;
1610 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1611 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1614 static int cdrom_probe_device(const char *filename
)
1620 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1624 ret
= fstat(fd
, &st
);
1625 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1629 /* Attempt to detect via a CDROM specific ioctl */
1630 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1640 static int cdrom_is_inserted(BlockDriverState
*bs
)
1642 BDRVRawState
*s
= bs
->opaque
;
1645 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1646 if (ret
== CDS_DISC_OK
)
1651 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1653 BDRVRawState
*s
= bs
->opaque
;
1656 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1657 perror("CDROMEJECT");
1659 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1660 perror("CDROMEJECT");
1664 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1666 BDRVRawState
*s
= bs
->opaque
;
1668 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1670 * Note: an error can happen if the distribution automatically
1673 /* perror("CDROM_LOCKDOOR"); */
1677 static BlockDriver bdrv_host_cdrom
= {
1678 .format_name
= "host_cdrom",
1679 .protocol_name
= "host_cdrom",
1680 .instance_size
= sizeof(BDRVRawState
),
1681 .bdrv_probe_device
= cdrom_probe_device
,
1682 .bdrv_file_open
= cdrom_open
,
1683 .bdrv_close
= raw_close
,
1684 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1685 .bdrv_reopen_commit
= raw_reopen_commit
,
1686 .bdrv_reopen_abort
= raw_reopen_abort
,
1687 .bdrv_create
= hdev_create
,
1688 .create_options
= raw_create_options
,
1689 .bdrv_has_zero_init
= hdev_has_zero_init
,
1691 .bdrv_aio_readv
= raw_aio_readv
,
1692 .bdrv_aio_writev
= raw_aio_writev
,
1693 .bdrv_aio_flush
= raw_aio_flush
,
1695 .bdrv_truncate
= raw_truncate
,
1696 .bdrv_getlength
= raw_getlength
,
1697 .bdrv_get_allocated_file_size
1698 = raw_get_allocated_file_size
,
1700 /* removable device support */
1701 .bdrv_is_inserted
= cdrom_is_inserted
,
1702 .bdrv_eject
= cdrom_eject
,
1703 .bdrv_lock_medium
= cdrom_lock_medium
,
1705 /* generic scsi device */
1706 .bdrv_ioctl
= hdev_ioctl
,
1707 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1709 #endif /* __linux__ */
1711 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1712 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1714 BDRVRawState
*s
= bs
->opaque
;
1719 ret
= raw_open_common(bs
, filename
, flags
, 0);
1723 /* make sure the door isn't locked at this time */
1724 ioctl(s
->fd
, CDIOCALLOW
);
1728 static int cdrom_probe_device(const char *filename
)
1730 if (strstart(filename
, "/dev/cd", NULL
) ||
1731 strstart(filename
, "/dev/acd", NULL
))
1736 static int cdrom_reopen(BlockDriverState
*bs
)
1738 BDRVRawState
*s
= bs
->opaque
;
1742 * Force reread of possibly changed/newly loaded disc,
1743 * FreeBSD seems to not notice sometimes...
1747 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1754 /* make sure the door isn't locked at this time */
1755 ioctl(s
->fd
, CDIOCALLOW
);
1759 static int cdrom_is_inserted(BlockDriverState
*bs
)
1761 return raw_getlength(bs
) > 0;
1764 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1766 BDRVRawState
*s
= bs
->opaque
;
1771 (void) ioctl(s
->fd
, CDIOCALLOW
);
1774 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1775 perror("CDIOCEJECT");
1777 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1778 perror("CDIOCCLOSE");
1784 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1786 BDRVRawState
*s
= bs
->opaque
;
1790 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1792 * Note: an error can happen if the distribution automatically
1795 /* perror("CDROM_LOCKDOOR"); */
1799 static BlockDriver bdrv_host_cdrom
= {
1800 .format_name
= "host_cdrom",
1801 .protocol_name
= "host_cdrom",
1802 .instance_size
= sizeof(BDRVRawState
),
1803 .bdrv_probe_device
= cdrom_probe_device
,
1804 .bdrv_file_open
= cdrom_open
,
1805 .bdrv_close
= raw_close
,
1806 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1807 .bdrv_reopen_commit
= raw_reopen_commit
,
1808 .bdrv_reopen_abort
= raw_reopen_abort
,
1809 .bdrv_create
= hdev_create
,
1810 .create_options
= raw_create_options
,
1811 .bdrv_has_zero_init
= hdev_has_zero_init
,
1813 .bdrv_aio_readv
= raw_aio_readv
,
1814 .bdrv_aio_writev
= raw_aio_writev
,
1815 .bdrv_aio_flush
= raw_aio_flush
,
1817 .bdrv_truncate
= raw_truncate
,
1818 .bdrv_getlength
= raw_getlength
,
1819 .bdrv_get_allocated_file_size
1820 = raw_get_allocated_file_size
,
1822 /* removable device support */
1823 .bdrv_is_inserted
= cdrom_is_inserted
,
1824 .bdrv_eject
= cdrom_eject
,
1825 .bdrv_lock_medium
= cdrom_lock_medium
,
1827 #endif /* __FreeBSD__ */
1829 #ifdef CONFIG_LINUX_AIO
1831 * Return the file descriptor for Linux AIO
1833 * This function is a layering violation and should be removed when it becomes
1834 * possible to call the block layer outside the global mutex. It allows the
1835 * caller to hijack the file descriptor so I/O can be performed outside the
1838 int raw_get_aio_fd(BlockDriverState
*bs
)
1846 if (bs
->drv
== bdrv_find_format("raw")) {
1850 /* raw-posix has several protocols so just check for raw_aio_readv */
1851 if (bs
->drv
->bdrv_aio_readv
!= raw_aio_readv
) {
1861 #endif /* CONFIG_LINUX_AIO */
1863 static void bdrv_file_init(void)
1866 * Register all the drivers. Note that order is important, the driver
1867 * registered last will get probed first.
1869 bdrv_register(&bdrv_file
);
1870 bdrv_register(&bdrv_host_device
);
1872 bdrv_register(&bdrv_host_floppy
);
1873 bdrv_register(&bdrv_host_cdrom
);
1875 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1876 bdrv_register(&bdrv_host_cdrom
);
1880 block_init(bdrv_file_init
);