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
,
307 QDict
*options
, int flags
)
309 BDRVRawState
*s
= bs
->opaque
;
311 s
->type
= FTYPE_FILE
;
312 return raw_open_common(bs
, filename
, flags
, 0);
315 static int raw_reopen_prepare(BDRVReopenState
*state
,
316 BlockReopenQueue
*queue
, Error
**errp
)
319 BDRVRawReopenState
*raw_s
;
322 assert(state
!= NULL
);
323 assert(state
->bs
!= NULL
);
325 s
= state
->bs
->opaque
;
327 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
328 raw_s
= state
->opaque
;
330 #ifdef CONFIG_LINUX_AIO
331 raw_s
->use_aio
= s
->use_aio
;
333 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
334 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
335 * won't override aio_ctx if aio_ctx is non-NULL */
336 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
341 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
342 raw_s
->open_flags
|= O_NONBLOCK
;
345 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
349 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
351 fcntl_flags
|= O_NOATIME
;
355 /* Not all operating systems have O_ASYNC, and those that don't
356 * will not let us track the state into raw_s->open_flags (typically
357 * you achieve the same effect with an ioctl, for example I_SETSIG
358 * on Solaris). But we do not use O_ASYNC, so that's fine.
360 assert((s
->open_flags
& O_ASYNC
) == 0);
363 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
364 /* dup the original fd */
365 /* TODO: use qemu fcntl wrapper */
366 #ifdef F_DUPFD_CLOEXEC
367 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
369 raw_s
->fd
= dup(s
->fd
);
370 if (raw_s
->fd
!= -1) {
371 qemu_set_cloexec(raw_s
->fd
);
374 if (raw_s
->fd
>= 0) {
375 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
377 qemu_close(raw_s
->fd
);
383 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
384 if (raw_s
->fd
== -1) {
385 assert(!(raw_s
->open_flags
& O_CREAT
));
386 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
387 if (raw_s
->fd
== -1) {
395 static void raw_reopen_commit(BDRVReopenState
*state
)
397 BDRVRawReopenState
*raw_s
= state
->opaque
;
398 BDRVRawState
*s
= state
->bs
->opaque
;
400 s
->open_flags
= raw_s
->open_flags
;
404 #ifdef CONFIG_LINUX_AIO
405 s
->use_aio
= raw_s
->use_aio
;
408 g_free(state
->opaque
);
409 state
->opaque
= NULL
;
413 static void raw_reopen_abort(BDRVReopenState
*state
)
415 BDRVRawReopenState
*raw_s
= state
->opaque
;
417 /* nothing to do if NULL, we didn't get far enough */
422 if (raw_s
->fd
>= 0) {
423 qemu_close(raw_s
->fd
);
426 g_free(state
->opaque
);
427 state
->opaque
= NULL
;
431 /* XXX: use host sector size if necessary with:
432 #ifdef DIOCGSECTORSIZE
434 unsigned int sectorsize = 512;
435 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
436 sectorsize > bufsize)
437 bufsize = sectorsize;
441 uint32_t blockSize = 512;
442 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
448 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
452 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
460 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
464 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
473 static bool preadv_present
= true;
476 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
478 return preadv(fd
, iov
, nr_iov
, offset
);
482 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
484 return pwritev(fd
, iov
, nr_iov
, offset
);
489 static bool preadv_present
= false;
492 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
498 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
505 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
510 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
511 len
= qemu_pwritev(aiocb
->aio_fildes
,
516 len
= qemu_preadv(aiocb
->aio_fildes
,
520 } while (len
== -1 && errno
== EINTR
);
529 * Read/writes the data to/from a given linear buffer.
531 * Returns the number of bytes handles or -errno in case of an error. Short
532 * reads are only returned if the end of the file is reached.
534 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
539 while (offset
< aiocb
->aio_nbytes
) {
540 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
541 len
= pwrite(aiocb
->aio_fildes
,
542 (const char *)buf
+ offset
,
543 aiocb
->aio_nbytes
- offset
,
544 aiocb
->aio_offset
+ offset
);
546 len
= pread(aiocb
->aio_fildes
,
548 aiocb
->aio_nbytes
- offset
,
549 aiocb
->aio_offset
+ offset
);
551 if (len
== -1 && errno
== EINTR
) {
553 } else if (len
== -1) {
556 } else if (len
== 0) {
565 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
570 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
572 * If there is just a single buffer, and it is properly aligned
573 * we can just use plain pread/pwrite without any problems.
575 if (aiocb
->aio_niov
== 1) {
576 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
579 * We have more than one iovec, and all are properly aligned.
581 * Try preadv/pwritev first and fall back to linearizing the
582 * buffer if it's not supported.
584 if (preadv_present
) {
585 nbytes
= handle_aiocb_rw_vector(aiocb
);
586 if (nbytes
== aiocb
->aio_nbytes
||
587 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
590 preadv_present
= false;
594 * XXX(hch): short read/write. no easy way to handle the reminder
595 * using these interfaces. For now retry using plain
601 * Ok, we have to do it the hard way, copy all segments into
602 * a single aligned buffer.
604 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
605 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
609 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
610 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
611 p
+= aiocb
->aio_iov
[i
].iov_len
;
615 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
616 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
618 size_t count
= aiocb
->aio_nbytes
, copy
;
621 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
623 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
624 copy
= aiocb
->aio_iov
[i
].iov_len
;
626 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
637 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
639 struct xfs_flock64 fl
;
641 memset(&fl
, 0, sizeof(fl
));
642 fl
.l_whence
= SEEK_SET
;
646 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
647 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
655 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
657 int ret
= -EOPNOTSUPP
;
658 BDRVRawState
*s
= aiocb
->bs
->opaque
;
660 if (s
->has_discard
== 0) {
664 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
667 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
668 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
671 } while (errno
== EINTR
);
678 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
682 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
684 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
685 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
688 } while (errno
== EINTR
);
694 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
702 static int aio_worker(void *arg
)
704 RawPosixAIOData
*aiocb
= arg
;
707 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
709 ret
= handle_aiocb_rw(aiocb
);
710 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
711 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
712 0, aiocb
->aio_nbytes
- ret
);
714 ret
= aiocb
->aio_nbytes
;
716 if (ret
== aiocb
->aio_nbytes
) {
718 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
723 ret
= handle_aiocb_rw(aiocb
);
724 if (ret
== aiocb
->aio_nbytes
) {
726 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
731 ret
= handle_aiocb_flush(aiocb
);
734 ret
= handle_aiocb_ioctl(aiocb
);
736 case QEMU_AIO_DISCARD
:
737 ret
= handle_aiocb_discard(aiocb
);
740 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
745 g_slice_free(RawPosixAIOData
, aiocb
);
749 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
750 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
751 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
753 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
757 acb
->aio_type
= type
;
758 acb
->aio_fildes
= fd
;
761 acb
->aio_iov
= qiov
->iov
;
762 acb
->aio_niov
= qiov
->niov
;
764 acb
->aio_nbytes
= nb_sectors
* 512;
765 acb
->aio_offset
= sector_num
* 512;
767 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
768 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
769 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
772 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
773 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
774 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
776 BDRVRawState
*s
= bs
->opaque
;
782 * If O_DIRECT is used the buffer needs to be aligned on a sector
783 * boundary. Check if this is the case or tell the low-level
784 * driver that it needs to copy the buffer.
786 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
787 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
788 type
|= QEMU_AIO_MISALIGNED
;
789 #ifdef CONFIG_LINUX_AIO
790 } else if (s
->use_aio
) {
791 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
792 nb_sectors
, cb
, opaque
, type
);
797 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
801 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
802 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
803 BlockDriverCompletionFunc
*cb
, void *opaque
)
805 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
806 cb
, opaque
, QEMU_AIO_READ
);
809 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
810 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
811 BlockDriverCompletionFunc
*cb
, void *opaque
)
813 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
814 cb
, opaque
, QEMU_AIO_WRITE
);
817 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
818 BlockDriverCompletionFunc
*cb
, void *opaque
)
820 BDRVRawState
*s
= bs
->opaque
;
825 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
828 static void raw_close(BlockDriverState
*bs
)
830 BDRVRawState
*s
= bs
->opaque
;
837 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
839 BDRVRawState
*s
= bs
->opaque
;
842 if (fstat(s
->fd
, &st
)) {
846 if (S_ISREG(st
.st_mode
)) {
847 if (ftruncate(s
->fd
, offset
) < 0) {
850 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
851 if (offset
> raw_getlength(bs
)) {
862 static int64_t raw_getlength(BlockDriverState
*bs
)
864 BDRVRawState
*s
= bs
->opaque
;
870 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
873 if (ioctl(fd
, DIOCGDINFO
, &dl
))
875 return (uint64_t)dl
.d_secsize
*
876 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
880 #elif defined(__NetBSD__)
881 static int64_t raw_getlength(BlockDriverState
*bs
)
883 BDRVRawState
*s
= bs
->opaque
;
889 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
890 struct dkwedge_info dkw
;
892 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
893 return dkw
.dkw_size
* 512;
897 if (ioctl(fd
, DIOCGDINFO
, &dl
))
899 return (uint64_t)dl
.d_secsize
*
900 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
905 #elif defined(__sun__)
906 static int64_t raw_getlength(BlockDriverState
*bs
)
908 BDRVRawState
*s
= bs
->opaque
;
909 struct dk_minfo minfo
;
918 * Use the DKIOCGMEDIAINFO ioctl to read the size.
920 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
922 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
926 * There are reports that lseek on some devices fails, but
927 * irc discussion said that contingency on contingency was overkill.
929 return lseek(s
->fd
, 0, SEEK_END
);
931 #elif defined(CONFIG_BSD)
932 static int64_t raw_getlength(BlockDriverState
*bs
)
934 BDRVRawState
*s
= bs
->opaque
;
938 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
947 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
950 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
951 #ifdef DIOCGMEDIASIZE
952 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
953 #elif defined(DIOCGPART)
956 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
957 size
= pi
.media_size
;
963 #if defined(__APPLE__) && defined(__MACH__)
964 size
= LONG_LONG_MAX
;
966 size
= lseek(fd
, 0LL, SEEK_END
);
968 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
971 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
972 if (size
== 2048LL * (unsigned)-1)
974 /* XXX no disc? maybe we need to reopen... */
975 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
982 size
= lseek(fd
, 0, SEEK_END
);
987 static int64_t raw_getlength(BlockDriverState
*bs
)
989 BDRVRawState
*s
= bs
->opaque
;
997 return lseek(s
->fd
, 0, SEEK_END
);
1001 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1004 BDRVRawState
*s
= bs
->opaque
;
1006 if (fstat(s
->fd
, &st
) < 0) {
1009 return (int64_t)st
.st_blocks
* 512;
1012 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
1016 int64_t total_size
= 0;
1018 /* Read out options */
1019 while (options
&& options
->name
) {
1020 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1021 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1026 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1031 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1034 if (qemu_close(fd
) != 0) {
1042 * Returns true iff the specified sector is present in the disk image. Drivers
1043 * not implementing the functionality are assumed to not support backing files,
1044 * hence all their sectors are reported as allocated.
1046 * If 'sector_num' is beyond the end of the disk image the return value is 0
1047 * and 'pnum' is set to 0.
1049 * 'pnum' is set to the number of sectors (including and immediately following
1050 * the specified sector) that are known to be in the same
1051 * allocated/unallocated state.
1053 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1054 * beyond the end of the disk image it will be clamped.
1056 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
1058 int nb_sectors
, int *pnum
)
1060 off_t start
, data
, hole
;
1068 start
= sector_num
* BDRV_SECTOR_SIZE
;
1070 #ifdef CONFIG_FIEMAP
1072 BDRVRawState
*s
= bs
->opaque
;
1075 struct fiemap_extent fe
;
1078 f
.fm
.fm_start
= start
;
1079 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1081 f
.fm
.fm_extent_count
= 1;
1082 f
.fm
.fm_reserved
= 0;
1083 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1084 /* Assume everything is allocated. */
1089 if (f
.fm
.fm_mapped_extents
== 0) {
1090 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1091 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1093 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1094 hole
= f
.fm
.fm_start
;
1095 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1097 data
= f
.fe
.fe_logical
;
1098 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1101 #elif defined SEEK_HOLE && defined SEEK_DATA
1103 BDRVRawState
*s
= bs
->opaque
;
1105 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1107 /* -ENXIO indicates that sector_num was past the end of the file.
1108 * There is a virtual hole there. */
1109 assert(errno
!= -ENXIO
);
1111 /* Most likely EINVAL. Assume everything is allocated. */
1119 /* On a hole. We need another syscall to find its end. */
1120 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1122 data
= lseek(s
->fd
, 0, SEEK_END
);
1130 if (data
<= start
) {
1131 /* On a data extent, compute sectors to the end of the extent. */
1132 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1135 /* On a hole, compute sectors to the beginning of the next extent. */
1136 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1141 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1142 int64_t sector_num
, int nb_sectors
,
1143 BlockDriverCompletionFunc
*cb
, void *opaque
)
1145 BDRVRawState
*s
= bs
->opaque
;
1147 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1148 cb
, opaque
, QEMU_AIO_DISCARD
);
1151 static QEMUOptionParameter raw_create_options
[] = {
1153 .name
= BLOCK_OPT_SIZE
,
1155 .help
= "Virtual disk size"
1160 static BlockDriver bdrv_file
= {
1161 .format_name
= "file",
1162 .protocol_name
= "file",
1163 .instance_size
= sizeof(BDRVRawState
),
1164 .bdrv_probe
= NULL
, /* no probe for protocols */
1165 .bdrv_file_open
= raw_open
,
1166 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1167 .bdrv_reopen_commit
= raw_reopen_commit
,
1168 .bdrv_reopen_abort
= raw_reopen_abort
,
1169 .bdrv_close
= raw_close
,
1170 .bdrv_create
= raw_create
,
1171 .bdrv_co_is_allocated
= raw_co_is_allocated
,
1173 .bdrv_aio_readv
= raw_aio_readv
,
1174 .bdrv_aio_writev
= raw_aio_writev
,
1175 .bdrv_aio_flush
= raw_aio_flush
,
1176 .bdrv_aio_discard
= raw_aio_discard
,
1178 .bdrv_truncate
= raw_truncate
,
1179 .bdrv_getlength
= raw_getlength
,
1180 .bdrv_get_allocated_file_size
1181 = raw_get_allocated_file_size
,
1183 .create_options
= raw_create_options
,
1186 /***********************************************/
1189 #if defined(__APPLE__) && defined(__MACH__)
1190 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1191 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1193 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1195 kern_return_t kernResult
;
1196 mach_port_t masterPort
;
1197 CFMutableDictionaryRef classesToMatch
;
1199 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1200 if ( KERN_SUCCESS
!= kernResult
) {
1201 printf( "IOMasterPort returned %d\n", kernResult
);
1204 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1205 if ( classesToMatch
== NULL
) {
1206 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1208 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1210 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1211 if ( KERN_SUCCESS
!= kernResult
)
1213 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1219 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1221 io_object_t nextMedia
;
1222 kern_return_t kernResult
= KERN_FAILURE
;
1224 nextMedia
= IOIteratorNext( mediaIterator
);
1227 CFTypeRef bsdPathAsCFString
;
1228 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1229 if ( bsdPathAsCFString
) {
1230 size_t devPathLength
;
1231 strcpy( bsdPath
, _PATH_DEV
);
1232 strcat( bsdPath
, "r" );
1233 devPathLength
= strlen( bsdPath
);
1234 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1235 kernResult
= KERN_SUCCESS
;
1237 CFRelease( bsdPathAsCFString
);
1239 IOObjectRelease( nextMedia
);
1247 static int hdev_probe_device(const char *filename
)
1251 /* allow a dedicated CD-ROM driver to match with a higher priority */
1252 if (strstart(filename
, "/dev/cdrom", NULL
))
1255 if (stat(filename
, &st
) >= 0 &&
1256 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1263 static int check_hdev_writable(BDRVRawState
*s
)
1265 #if defined(BLKROGET)
1266 /* Linux block devices can be configured "read-only" using blockdev(8).
1267 * This is independent of device node permissions and therefore open(2)
1268 * with O_RDWR succeeds. Actual writes fail with EPERM.
1270 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1271 * check for read-only block devices so that Linux block devices behave
1277 if (fstat(s
->fd
, &st
)) {
1281 if (!S_ISBLK(st
.st_mode
)) {
1285 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1292 #endif /* defined(BLKROGET) */
1296 static int hdev_open(BlockDriverState
*bs
, const char *filename
,
1297 QDict
*options
, int flags
)
1299 BDRVRawState
*s
= bs
->opaque
;
1302 #if defined(__APPLE__) && defined(__MACH__)
1303 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1304 kern_return_t kernResult
;
1305 io_iterator_t mediaIterator
;
1306 char bsdPath
[ MAXPATHLEN
];
1309 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1310 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1312 if ( bsdPath
[ 0 ] != '\0' ) {
1313 strcat(bsdPath
,"s0");
1314 /* some CDs don't have a partition 0 */
1315 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1317 bsdPath
[strlen(bsdPath
)-1] = '1';
1324 if ( mediaIterator
)
1325 IOObjectRelease( mediaIterator
);
1329 s
->type
= FTYPE_FILE
;
1330 #if defined(__linux__)
1332 char resolved_path
[ MAXPATHLEN
], *temp
;
1334 temp
= realpath(filename
, resolved_path
);
1335 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1341 ret
= raw_open_common(bs
, filename
, flags
, 0);
1346 if (flags
& BDRV_O_RDWR
) {
1347 ret
= check_hdev_writable(s
);
1357 #if defined(__linux__)
1358 /* Note: we do not have a reliable method to detect if the floppy is
1359 present. The current method is to try to open the floppy at every
1360 I/O and to keep it opened during a few hundreds of ms. */
1361 static int fd_open(BlockDriverState
*bs
)
1363 BDRVRawState
*s
= bs
->opaque
;
1364 int last_media_present
;
1366 if (s
->type
!= FTYPE_FD
)
1368 last_media_present
= (s
->fd
>= 0);
1370 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1374 printf("Floppy closed\n");
1378 if (s
->fd_got_error
&&
1379 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1381 printf("No floppy (open delayed)\n");
1385 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1387 s
->fd_error_time
= get_clock();
1388 s
->fd_got_error
= 1;
1389 if (last_media_present
)
1390 s
->fd_media_changed
= 1;
1392 printf("No floppy\n");
1397 printf("Floppy opened\n");
1400 if (!last_media_present
)
1401 s
->fd_media_changed
= 1;
1402 s
->fd_open_time
= get_clock();
1403 s
->fd_got_error
= 0;
1407 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1409 BDRVRawState
*s
= bs
->opaque
;
1411 return ioctl(s
->fd
, req
, buf
);
1414 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1415 unsigned long int req
, void *buf
,
1416 BlockDriverCompletionFunc
*cb
, void *opaque
)
1418 BDRVRawState
*s
= bs
->opaque
;
1419 RawPosixAIOData
*acb
;
1422 if (fd_open(bs
) < 0)
1425 acb
= g_slice_new(RawPosixAIOData
);
1427 acb
->aio_type
= QEMU_AIO_IOCTL
;
1428 acb
->aio_fildes
= s
->fd
;
1429 acb
->aio_offset
= 0;
1430 acb
->aio_ioctl_buf
= buf
;
1431 acb
->aio_ioctl_cmd
= req
;
1432 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1433 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1436 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1437 static int fd_open(BlockDriverState
*bs
)
1439 BDRVRawState
*s
= bs
->opaque
;
1441 /* this is just to ensure s->fd is sane (its called by io ops) */
1446 #else /* !linux && !FreeBSD */
1448 static int fd_open(BlockDriverState
*bs
)
1453 #endif /* !linux && !FreeBSD */
1455 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1456 int64_t sector_num
, int nb_sectors
,
1457 BlockDriverCompletionFunc
*cb
, void *opaque
)
1459 BDRVRawState
*s
= bs
->opaque
;
1461 if (fd_open(bs
) < 0) {
1464 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1465 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1468 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1472 struct stat stat_buf
;
1473 int64_t total_size
= 0;
1475 /* Read out options */
1476 while (options
&& options
->name
) {
1477 if (!strcmp(options
->name
, "size")) {
1478 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1483 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1487 if (fstat(fd
, &stat_buf
) < 0)
1489 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1491 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1498 static int hdev_has_zero_init(BlockDriverState
*bs
)
1503 static BlockDriver bdrv_host_device
= {
1504 .format_name
= "host_device",
1505 .protocol_name
= "host_device",
1506 .instance_size
= sizeof(BDRVRawState
),
1507 .bdrv_probe_device
= hdev_probe_device
,
1508 .bdrv_file_open
= hdev_open
,
1509 .bdrv_close
= raw_close
,
1510 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1511 .bdrv_reopen_commit
= raw_reopen_commit
,
1512 .bdrv_reopen_abort
= raw_reopen_abort
,
1513 .bdrv_create
= hdev_create
,
1514 .create_options
= raw_create_options
,
1515 .bdrv_has_zero_init
= hdev_has_zero_init
,
1517 .bdrv_aio_readv
= raw_aio_readv
,
1518 .bdrv_aio_writev
= raw_aio_writev
,
1519 .bdrv_aio_flush
= raw_aio_flush
,
1520 .bdrv_aio_discard
= hdev_aio_discard
,
1522 .bdrv_truncate
= raw_truncate
,
1523 .bdrv_getlength
= raw_getlength
,
1524 .bdrv_get_allocated_file_size
1525 = raw_get_allocated_file_size
,
1527 /* generic scsi device */
1529 .bdrv_ioctl
= hdev_ioctl
,
1530 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1535 static int floppy_open(BlockDriverState
*bs
, const char *filename
,
1536 QDict
*options
, int flags
)
1538 BDRVRawState
*s
= bs
->opaque
;
1543 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1544 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1548 /* close fd so that we can reopen it as needed */
1551 s
->fd_media_changed
= 1;
1556 static int floppy_probe_device(const char *filename
)
1560 struct floppy_struct fdparam
;
1563 if (strstart(filename
, "/dev/fd", NULL
) &&
1564 !strstart(filename
, "/dev/fdset/", NULL
)) {
1568 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1572 ret
= fstat(fd
, &st
);
1573 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1577 /* Attempt to detect via a floppy specific ioctl */
1578 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1589 static int floppy_is_inserted(BlockDriverState
*bs
)
1591 return fd_open(bs
) >= 0;
1594 static int floppy_media_changed(BlockDriverState
*bs
)
1596 BDRVRawState
*s
= bs
->opaque
;
1600 * XXX: we do not have a true media changed indication.
1601 * It does not work if the floppy is changed without trying to read it.
1604 ret
= s
->fd_media_changed
;
1605 s
->fd_media_changed
= 0;
1607 printf("Floppy changed=%d\n", ret
);
1612 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1614 BDRVRawState
*s
= bs
->opaque
;
1621 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1623 if (ioctl(fd
, FDEJECT
, 0) < 0)
1629 static BlockDriver bdrv_host_floppy
= {
1630 .format_name
= "host_floppy",
1631 .protocol_name
= "host_floppy",
1632 .instance_size
= sizeof(BDRVRawState
),
1633 .bdrv_probe_device
= floppy_probe_device
,
1634 .bdrv_file_open
= floppy_open
,
1635 .bdrv_close
= raw_close
,
1636 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1637 .bdrv_reopen_commit
= raw_reopen_commit
,
1638 .bdrv_reopen_abort
= raw_reopen_abort
,
1639 .bdrv_create
= hdev_create
,
1640 .create_options
= raw_create_options
,
1641 .bdrv_has_zero_init
= hdev_has_zero_init
,
1643 .bdrv_aio_readv
= raw_aio_readv
,
1644 .bdrv_aio_writev
= raw_aio_writev
,
1645 .bdrv_aio_flush
= raw_aio_flush
,
1647 .bdrv_truncate
= raw_truncate
,
1648 .bdrv_getlength
= raw_getlength
,
1649 .bdrv_get_allocated_file_size
1650 = raw_get_allocated_file_size
,
1652 /* removable device support */
1653 .bdrv_is_inserted
= floppy_is_inserted
,
1654 .bdrv_media_changed
= floppy_media_changed
,
1655 .bdrv_eject
= floppy_eject
,
1658 static int cdrom_open(BlockDriverState
*bs
, const char *filename
,
1659 QDict
*options
, int flags
)
1661 BDRVRawState
*s
= bs
->opaque
;
1665 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1666 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1669 static int cdrom_probe_device(const char *filename
)
1675 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1679 ret
= fstat(fd
, &st
);
1680 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1684 /* Attempt to detect via a CDROM specific ioctl */
1685 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1695 static int cdrom_is_inserted(BlockDriverState
*bs
)
1697 BDRVRawState
*s
= bs
->opaque
;
1700 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1701 if (ret
== CDS_DISC_OK
)
1706 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1708 BDRVRawState
*s
= bs
->opaque
;
1711 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1712 perror("CDROMEJECT");
1714 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1715 perror("CDROMEJECT");
1719 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1721 BDRVRawState
*s
= bs
->opaque
;
1723 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1725 * Note: an error can happen if the distribution automatically
1728 /* perror("CDROM_LOCKDOOR"); */
1732 static BlockDriver bdrv_host_cdrom
= {
1733 .format_name
= "host_cdrom",
1734 .protocol_name
= "host_cdrom",
1735 .instance_size
= sizeof(BDRVRawState
),
1736 .bdrv_probe_device
= cdrom_probe_device
,
1737 .bdrv_file_open
= cdrom_open
,
1738 .bdrv_close
= raw_close
,
1739 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1740 .bdrv_reopen_commit
= raw_reopen_commit
,
1741 .bdrv_reopen_abort
= raw_reopen_abort
,
1742 .bdrv_create
= hdev_create
,
1743 .create_options
= raw_create_options
,
1744 .bdrv_has_zero_init
= hdev_has_zero_init
,
1746 .bdrv_aio_readv
= raw_aio_readv
,
1747 .bdrv_aio_writev
= raw_aio_writev
,
1748 .bdrv_aio_flush
= raw_aio_flush
,
1750 .bdrv_truncate
= raw_truncate
,
1751 .bdrv_getlength
= raw_getlength
,
1752 .bdrv_get_allocated_file_size
1753 = raw_get_allocated_file_size
,
1755 /* removable device support */
1756 .bdrv_is_inserted
= cdrom_is_inserted
,
1757 .bdrv_eject
= cdrom_eject
,
1758 .bdrv_lock_medium
= cdrom_lock_medium
,
1760 /* generic scsi device */
1761 .bdrv_ioctl
= hdev_ioctl
,
1762 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1764 #endif /* __linux__ */
1766 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1767 static int cdrom_open(BlockDriverState
*bs
, const char *filename
,
1768 QDict
*options
, int flags
)
1770 BDRVRawState
*s
= bs
->opaque
;
1775 ret
= raw_open_common(bs
, filename
, flags
, 0);
1779 /* make sure the door isn't locked at this time */
1780 ioctl(s
->fd
, CDIOCALLOW
);
1784 static int cdrom_probe_device(const char *filename
)
1786 if (strstart(filename
, "/dev/cd", NULL
) ||
1787 strstart(filename
, "/dev/acd", NULL
))
1792 static int cdrom_reopen(BlockDriverState
*bs
)
1794 BDRVRawState
*s
= bs
->opaque
;
1798 * Force reread of possibly changed/newly loaded disc,
1799 * FreeBSD seems to not notice sometimes...
1803 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1810 /* make sure the door isn't locked at this time */
1811 ioctl(s
->fd
, CDIOCALLOW
);
1815 static int cdrom_is_inserted(BlockDriverState
*bs
)
1817 return raw_getlength(bs
) > 0;
1820 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1822 BDRVRawState
*s
= bs
->opaque
;
1827 (void) ioctl(s
->fd
, CDIOCALLOW
);
1830 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1831 perror("CDIOCEJECT");
1833 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1834 perror("CDIOCCLOSE");
1840 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1842 BDRVRawState
*s
= bs
->opaque
;
1846 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1848 * Note: an error can happen if the distribution automatically
1851 /* perror("CDROM_LOCKDOOR"); */
1855 static BlockDriver bdrv_host_cdrom
= {
1856 .format_name
= "host_cdrom",
1857 .protocol_name
= "host_cdrom",
1858 .instance_size
= sizeof(BDRVRawState
),
1859 .bdrv_probe_device
= cdrom_probe_device
,
1860 .bdrv_file_open
= cdrom_open
,
1861 .bdrv_close
= raw_close
,
1862 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1863 .bdrv_reopen_commit
= raw_reopen_commit
,
1864 .bdrv_reopen_abort
= raw_reopen_abort
,
1865 .bdrv_create
= hdev_create
,
1866 .create_options
= raw_create_options
,
1867 .bdrv_has_zero_init
= hdev_has_zero_init
,
1869 .bdrv_aio_readv
= raw_aio_readv
,
1870 .bdrv_aio_writev
= raw_aio_writev
,
1871 .bdrv_aio_flush
= raw_aio_flush
,
1873 .bdrv_truncate
= raw_truncate
,
1874 .bdrv_getlength
= raw_getlength
,
1875 .bdrv_get_allocated_file_size
1876 = raw_get_allocated_file_size
,
1878 /* removable device support */
1879 .bdrv_is_inserted
= cdrom_is_inserted
,
1880 .bdrv_eject
= cdrom_eject
,
1881 .bdrv_lock_medium
= cdrom_lock_medium
,
1883 #endif /* __FreeBSD__ */
1885 #ifdef CONFIG_LINUX_AIO
1887 * Return the file descriptor for Linux AIO
1889 * This function is a layering violation and should be removed when it becomes
1890 * possible to call the block layer outside the global mutex. It allows the
1891 * caller to hijack the file descriptor so I/O can be performed outside the
1894 int raw_get_aio_fd(BlockDriverState
*bs
)
1902 if (bs
->drv
== bdrv_find_format("raw")) {
1906 /* raw-posix has several protocols so just check for raw_aio_readv */
1907 if (bs
->drv
->bdrv_aio_readv
!= raw_aio_readv
) {
1917 #endif /* CONFIG_LINUX_AIO */
1919 static void bdrv_file_init(void)
1922 * Register all the drivers. Note that order is important, the driver
1923 * registered last will get probed first.
1925 bdrv_register(&bdrv_file
);
1926 bdrv_register(&bdrv_host_device
);
1928 bdrv_register(&bdrv_host_floppy
);
1929 bdrv_register(&bdrv_host_cdrom
);
1931 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1932 bdrv_register(&bdrv_host_cdrom
);
1936 block_init(bdrv_file_init
);