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_ASYNC
| O_NONBLOCK
;
350 fcntl_flags
|= O_NOATIME
;
353 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
354 /* dup the original fd */
355 /* TODO: use qemu fcntl wrapper */
356 #ifdef F_DUPFD_CLOEXEC
357 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
359 raw_s
->fd
= dup(s
->fd
);
360 if (raw_s
->fd
!= -1) {
361 qemu_set_cloexec(raw_s
->fd
);
364 if (raw_s
->fd
>= 0) {
365 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
367 qemu_close(raw_s
->fd
);
373 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
374 if (raw_s
->fd
== -1) {
375 assert(!(raw_s
->open_flags
& O_CREAT
));
376 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
377 if (raw_s
->fd
== -1) {
385 static void raw_reopen_commit(BDRVReopenState
*state
)
387 BDRVRawReopenState
*raw_s
= state
->opaque
;
388 BDRVRawState
*s
= state
->bs
->opaque
;
390 s
->open_flags
= raw_s
->open_flags
;
394 #ifdef CONFIG_LINUX_AIO
395 s
->use_aio
= raw_s
->use_aio
;
398 g_free(state
->opaque
);
399 state
->opaque
= NULL
;
403 static void raw_reopen_abort(BDRVReopenState
*state
)
405 BDRVRawReopenState
*raw_s
= state
->opaque
;
407 /* nothing to do if NULL, we didn't get far enough */
412 if (raw_s
->fd
>= 0) {
413 qemu_close(raw_s
->fd
);
416 g_free(state
->opaque
);
417 state
->opaque
= NULL
;
421 /* XXX: use host sector size if necessary with:
422 #ifdef DIOCGSECTORSIZE
424 unsigned int sectorsize = 512;
425 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
426 sectorsize > bufsize)
427 bufsize = sectorsize;
431 uint32_t blockSize = 512;
432 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
438 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
442 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
450 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
454 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
463 static bool preadv_present
= true;
466 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
468 return preadv(fd
, iov
, nr_iov
, offset
);
472 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
474 return pwritev(fd
, iov
, nr_iov
, offset
);
479 static bool preadv_present
= false;
482 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
488 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
495 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
500 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
501 len
= qemu_pwritev(aiocb
->aio_fildes
,
506 len
= qemu_preadv(aiocb
->aio_fildes
,
510 } while (len
== -1 && errno
== EINTR
);
519 * Read/writes the data to/from a given linear buffer.
521 * Returns the number of bytes handles or -errno in case of an error. Short
522 * reads are only returned if the end of the file is reached.
524 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
529 while (offset
< aiocb
->aio_nbytes
) {
530 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
531 len
= pwrite(aiocb
->aio_fildes
,
532 (const char *)buf
+ offset
,
533 aiocb
->aio_nbytes
- offset
,
534 aiocb
->aio_offset
+ offset
);
536 len
= pread(aiocb
->aio_fildes
,
538 aiocb
->aio_nbytes
- offset
,
539 aiocb
->aio_offset
+ offset
);
541 if (len
== -1 && errno
== EINTR
) {
543 } else if (len
== -1) {
546 } else if (len
== 0) {
555 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
560 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
562 * If there is just a single buffer, and it is properly aligned
563 * we can just use plain pread/pwrite without any problems.
565 if (aiocb
->aio_niov
== 1) {
566 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
569 * We have more than one iovec, and all are properly aligned.
571 * Try preadv/pwritev first and fall back to linearizing the
572 * buffer if it's not supported.
574 if (preadv_present
) {
575 nbytes
= handle_aiocb_rw_vector(aiocb
);
576 if (nbytes
== aiocb
->aio_nbytes
||
577 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
580 preadv_present
= false;
584 * XXX(hch): short read/write. no easy way to handle the reminder
585 * using these interfaces. For now retry using plain
591 * Ok, we have to do it the hard way, copy all segments into
592 * a single aligned buffer.
594 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
595 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
599 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
600 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
601 p
+= aiocb
->aio_iov
[i
].iov_len
;
605 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
606 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
608 size_t count
= aiocb
->aio_nbytes
, copy
;
611 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
613 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
614 copy
= aiocb
->aio_iov
[i
].iov_len
;
616 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
627 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
629 struct xfs_flock64 fl
;
631 memset(&fl
, 0, sizeof(fl
));
632 fl
.l_whence
= SEEK_SET
;
636 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
637 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
645 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
647 int ret
= -EOPNOTSUPP
;
648 BDRVRawState
*s
= aiocb
->bs
->opaque
;
650 if (s
->has_discard
== 0) {
654 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
657 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
658 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
661 } while (errno
== EINTR
);
668 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
672 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
674 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
675 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
678 } while (errno
== EINTR
);
684 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
692 static int aio_worker(void *arg
)
694 RawPosixAIOData
*aiocb
= arg
;
697 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
699 ret
= handle_aiocb_rw(aiocb
);
700 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
701 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
702 0, aiocb
->aio_nbytes
- ret
);
704 ret
= aiocb
->aio_nbytes
;
706 if (ret
== aiocb
->aio_nbytes
) {
708 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
713 ret
= handle_aiocb_rw(aiocb
);
714 if (ret
== aiocb
->aio_nbytes
) {
716 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
721 ret
= handle_aiocb_flush(aiocb
);
724 ret
= handle_aiocb_ioctl(aiocb
);
726 case QEMU_AIO_DISCARD
:
727 ret
= handle_aiocb_discard(aiocb
);
730 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
735 g_slice_free(RawPosixAIOData
, aiocb
);
739 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
740 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
741 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
743 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
746 acb
->aio_type
= type
;
747 acb
->aio_fildes
= fd
;
750 acb
->aio_iov
= qiov
->iov
;
751 acb
->aio_niov
= qiov
->niov
;
753 acb
->aio_nbytes
= nb_sectors
* 512;
754 acb
->aio_offset
= sector_num
* 512;
756 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
757 return thread_pool_submit_aio(aio_worker
, acb
, cb
, opaque
);
760 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
761 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
762 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
764 BDRVRawState
*s
= bs
->opaque
;
770 * If O_DIRECT is used the buffer needs to be aligned on a sector
771 * boundary. Check if this is the case or tell the low-level
772 * driver that it needs to copy the buffer.
774 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
775 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
776 type
|= QEMU_AIO_MISALIGNED
;
777 #ifdef CONFIG_LINUX_AIO
778 } else if (s
->use_aio
) {
779 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
780 nb_sectors
, cb
, opaque
, type
);
785 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
789 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
790 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
791 BlockDriverCompletionFunc
*cb
, void *opaque
)
793 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
794 cb
, opaque
, QEMU_AIO_READ
);
797 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
798 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
799 BlockDriverCompletionFunc
*cb
, void *opaque
)
801 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
802 cb
, opaque
, QEMU_AIO_WRITE
);
805 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
806 BlockDriverCompletionFunc
*cb
, void *opaque
)
808 BDRVRawState
*s
= bs
->opaque
;
813 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
816 static void raw_close(BlockDriverState
*bs
)
818 BDRVRawState
*s
= bs
->opaque
;
825 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
827 BDRVRawState
*s
= bs
->opaque
;
830 if (fstat(s
->fd
, &st
)) {
834 if (S_ISREG(st
.st_mode
)) {
835 if (ftruncate(s
->fd
, offset
) < 0) {
838 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
839 if (offset
> raw_getlength(bs
)) {
850 static int64_t raw_getlength(BlockDriverState
*bs
)
852 BDRVRawState
*s
= bs
->opaque
;
858 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
861 if (ioctl(fd
, DIOCGDINFO
, &dl
))
863 return (uint64_t)dl
.d_secsize
*
864 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
868 #elif defined(__NetBSD__)
869 static int64_t raw_getlength(BlockDriverState
*bs
)
871 BDRVRawState
*s
= bs
->opaque
;
877 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
878 struct dkwedge_info dkw
;
880 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
881 return dkw
.dkw_size
* 512;
885 if (ioctl(fd
, DIOCGDINFO
, &dl
))
887 return (uint64_t)dl
.d_secsize
*
888 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
893 #elif defined(__sun__)
894 static int64_t raw_getlength(BlockDriverState
*bs
)
896 BDRVRawState
*s
= bs
->opaque
;
897 struct dk_minfo minfo
;
906 * Use the DKIOCGMEDIAINFO ioctl to read the size.
908 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
910 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
914 * There are reports that lseek on some devices fails, but
915 * irc discussion said that contingency on contingency was overkill.
917 return lseek(s
->fd
, 0, SEEK_END
);
919 #elif defined(CONFIG_BSD)
920 static int64_t raw_getlength(BlockDriverState
*bs
)
922 BDRVRawState
*s
= bs
->opaque
;
926 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
935 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
938 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
939 #ifdef DIOCGMEDIASIZE
940 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
941 #elif defined(DIOCGPART)
944 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
945 size
= pi
.media_size
;
951 #if defined(__APPLE__) && defined(__MACH__)
952 size
= LONG_LONG_MAX
;
954 size
= lseek(fd
, 0LL, SEEK_END
);
956 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
959 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
960 if (size
== 2048LL * (unsigned)-1)
962 /* XXX no disc? maybe we need to reopen... */
963 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
970 size
= lseek(fd
, 0, SEEK_END
);
975 static int64_t raw_getlength(BlockDriverState
*bs
)
977 BDRVRawState
*s
= bs
->opaque
;
985 return lseek(s
->fd
, 0, SEEK_END
);
989 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
992 BDRVRawState
*s
= bs
->opaque
;
994 if (fstat(s
->fd
, &st
) < 0) {
997 return (int64_t)st
.st_blocks
* 512;
1000 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
1004 int64_t total_size
= 0;
1006 /* Read out options */
1007 while (options
&& options
->name
) {
1008 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1009 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1014 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1019 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1022 if (qemu_close(fd
) != 0) {
1030 * Returns true iff the specified sector is present in the disk image. Drivers
1031 * not implementing the functionality are assumed to not support backing files,
1032 * hence all their sectors are reported as allocated.
1034 * If 'sector_num' is beyond the end of the disk image the return value is 0
1035 * and 'pnum' is set to 0.
1037 * 'pnum' is set to the number of sectors (including and immediately following
1038 * the specified sector) that are known to be in the same
1039 * allocated/unallocated state.
1041 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1042 * beyond the end of the disk image it will be clamped.
1044 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
1046 int nb_sectors
, int *pnum
)
1048 off_t start
, data
, hole
;
1056 start
= sector_num
* BDRV_SECTOR_SIZE
;
1058 #ifdef CONFIG_FIEMAP
1060 BDRVRawState
*s
= bs
->opaque
;
1063 struct fiemap_extent fe
;
1066 f
.fm
.fm_start
= start
;
1067 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1069 f
.fm
.fm_extent_count
= 1;
1070 f
.fm
.fm_reserved
= 0;
1071 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1072 /* Assume everything is allocated. */
1077 if (f
.fm
.fm_mapped_extents
== 0) {
1078 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1079 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1081 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1082 hole
= f
.fm
.fm_start
;
1083 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1085 data
= f
.fe
.fe_logical
;
1086 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1089 #elif defined SEEK_HOLE && defined SEEK_DATA
1091 BDRVRawState
*s
= bs
->opaque
;
1093 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1095 /* -ENXIO indicates that sector_num was past the end of the file.
1096 * There is a virtual hole there. */
1097 assert(errno
!= -ENXIO
);
1099 /* Most likely EINVAL. Assume everything is allocated. */
1107 /* On a hole. We need another syscall to find its end. */
1108 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1110 data
= lseek(s
->fd
, 0, SEEK_END
);
1118 if (data
<= start
) {
1119 /* On a data extent, compute sectors to the end of the extent. */
1120 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1123 /* On a hole, compute sectors to the beginning of the next extent. */
1124 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1129 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1130 int64_t sector_num
, int nb_sectors
,
1131 BlockDriverCompletionFunc
*cb
, void *opaque
)
1133 BDRVRawState
*s
= bs
->opaque
;
1135 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1136 cb
, opaque
, QEMU_AIO_DISCARD
);
1139 static QEMUOptionParameter raw_create_options
[] = {
1141 .name
= BLOCK_OPT_SIZE
,
1143 .help
= "Virtual disk size"
1148 static BlockDriver bdrv_file
= {
1149 .format_name
= "file",
1150 .protocol_name
= "file",
1151 .instance_size
= sizeof(BDRVRawState
),
1152 .bdrv_probe
= NULL
, /* no probe for protocols */
1153 .bdrv_file_open
= raw_open
,
1154 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1155 .bdrv_reopen_commit
= raw_reopen_commit
,
1156 .bdrv_reopen_abort
= raw_reopen_abort
,
1157 .bdrv_close
= raw_close
,
1158 .bdrv_create
= raw_create
,
1159 .bdrv_co_is_allocated
= raw_co_is_allocated
,
1161 .bdrv_aio_readv
= raw_aio_readv
,
1162 .bdrv_aio_writev
= raw_aio_writev
,
1163 .bdrv_aio_flush
= raw_aio_flush
,
1164 .bdrv_aio_discard
= raw_aio_discard
,
1166 .bdrv_truncate
= raw_truncate
,
1167 .bdrv_getlength
= raw_getlength
,
1168 .bdrv_get_allocated_file_size
1169 = raw_get_allocated_file_size
,
1171 .create_options
= raw_create_options
,
1174 /***********************************************/
1177 #if defined(__APPLE__) && defined(__MACH__)
1178 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1179 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1181 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1183 kern_return_t kernResult
;
1184 mach_port_t masterPort
;
1185 CFMutableDictionaryRef classesToMatch
;
1187 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1188 if ( KERN_SUCCESS
!= kernResult
) {
1189 printf( "IOMasterPort returned %d\n", kernResult
);
1192 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1193 if ( classesToMatch
== NULL
) {
1194 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1196 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1198 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1199 if ( KERN_SUCCESS
!= kernResult
)
1201 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1207 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1209 io_object_t nextMedia
;
1210 kern_return_t kernResult
= KERN_FAILURE
;
1212 nextMedia
= IOIteratorNext( mediaIterator
);
1215 CFTypeRef bsdPathAsCFString
;
1216 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1217 if ( bsdPathAsCFString
) {
1218 size_t devPathLength
;
1219 strcpy( bsdPath
, _PATH_DEV
);
1220 strcat( bsdPath
, "r" );
1221 devPathLength
= strlen( bsdPath
);
1222 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1223 kernResult
= KERN_SUCCESS
;
1225 CFRelease( bsdPathAsCFString
);
1227 IOObjectRelease( nextMedia
);
1235 static int hdev_probe_device(const char *filename
)
1239 /* allow a dedicated CD-ROM driver to match with a higher priority */
1240 if (strstart(filename
, "/dev/cdrom", NULL
))
1243 if (stat(filename
, &st
) >= 0 &&
1244 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1251 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1253 BDRVRawState
*s
= bs
->opaque
;
1255 #if defined(__APPLE__) && defined(__MACH__)
1256 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1257 kern_return_t kernResult
;
1258 io_iterator_t mediaIterator
;
1259 char bsdPath
[ MAXPATHLEN
];
1262 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1263 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1265 if ( bsdPath
[ 0 ] != '\0' ) {
1266 strcat(bsdPath
,"s0");
1267 /* some CDs don't have a partition 0 */
1268 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1270 bsdPath
[strlen(bsdPath
)-1] = '1';
1277 if ( mediaIterator
)
1278 IOObjectRelease( mediaIterator
);
1282 s
->type
= FTYPE_FILE
;
1283 #if defined(__linux__)
1285 char resolved_path
[ MAXPATHLEN
], *temp
;
1287 temp
= realpath(filename
, resolved_path
);
1288 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1294 return raw_open_common(bs
, filename
, flags
, 0);
1297 #if defined(__linux__)
1298 /* Note: we do not have a reliable method to detect if the floppy is
1299 present. The current method is to try to open the floppy at every
1300 I/O and to keep it opened during a few hundreds of ms. */
1301 static int fd_open(BlockDriverState
*bs
)
1303 BDRVRawState
*s
= bs
->opaque
;
1304 int last_media_present
;
1306 if (s
->type
!= FTYPE_FD
)
1308 last_media_present
= (s
->fd
>= 0);
1310 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1314 printf("Floppy closed\n");
1318 if (s
->fd_got_error
&&
1319 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1321 printf("No floppy (open delayed)\n");
1325 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1327 s
->fd_error_time
= get_clock();
1328 s
->fd_got_error
= 1;
1329 if (last_media_present
)
1330 s
->fd_media_changed
= 1;
1332 printf("No floppy\n");
1337 printf("Floppy opened\n");
1340 if (!last_media_present
)
1341 s
->fd_media_changed
= 1;
1342 s
->fd_open_time
= get_clock();
1343 s
->fd_got_error
= 0;
1347 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1349 BDRVRawState
*s
= bs
->opaque
;
1351 return ioctl(s
->fd
, req
, buf
);
1354 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1355 unsigned long int req
, void *buf
,
1356 BlockDriverCompletionFunc
*cb
, void *opaque
)
1358 BDRVRawState
*s
= bs
->opaque
;
1359 RawPosixAIOData
*acb
;
1361 if (fd_open(bs
) < 0)
1364 acb
= g_slice_new(RawPosixAIOData
);
1366 acb
->aio_type
= QEMU_AIO_IOCTL
;
1367 acb
->aio_fildes
= s
->fd
;
1368 acb
->aio_offset
= 0;
1369 acb
->aio_ioctl_buf
= buf
;
1370 acb
->aio_ioctl_cmd
= req
;
1371 return thread_pool_submit_aio(aio_worker
, acb
, cb
, opaque
);
1374 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1375 static int fd_open(BlockDriverState
*bs
)
1377 BDRVRawState
*s
= bs
->opaque
;
1379 /* this is just to ensure s->fd is sane (its called by io ops) */
1384 #else /* !linux && !FreeBSD */
1386 static int fd_open(BlockDriverState
*bs
)
1391 #endif /* !linux && !FreeBSD */
1393 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1394 int64_t sector_num
, int nb_sectors
,
1395 BlockDriverCompletionFunc
*cb
, void *opaque
)
1397 BDRVRawState
*s
= bs
->opaque
;
1399 if (fd_open(bs
) < 0) {
1402 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1403 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1406 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
)
1410 struct stat stat_buf
;
1411 int64_t total_size
= 0;
1413 /* Read out options */
1414 while (options
&& options
->name
) {
1415 if (!strcmp(options
->name
, "size")) {
1416 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1421 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1425 if (fstat(fd
, &stat_buf
) < 0)
1427 else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
))
1429 else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
)
1436 static int hdev_has_zero_init(BlockDriverState
*bs
)
1441 static BlockDriver bdrv_host_device
= {
1442 .format_name
= "host_device",
1443 .protocol_name
= "host_device",
1444 .instance_size
= sizeof(BDRVRawState
),
1445 .bdrv_probe_device
= hdev_probe_device
,
1446 .bdrv_file_open
= hdev_open
,
1447 .bdrv_close
= raw_close
,
1448 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1449 .bdrv_reopen_commit
= raw_reopen_commit
,
1450 .bdrv_reopen_abort
= raw_reopen_abort
,
1451 .bdrv_create
= hdev_create
,
1452 .create_options
= raw_create_options
,
1453 .bdrv_has_zero_init
= hdev_has_zero_init
,
1455 .bdrv_aio_readv
= raw_aio_readv
,
1456 .bdrv_aio_writev
= raw_aio_writev
,
1457 .bdrv_aio_flush
= raw_aio_flush
,
1458 .bdrv_aio_discard
= hdev_aio_discard
,
1460 .bdrv_truncate
= raw_truncate
,
1461 .bdrv_getlength
= raw_getlength
,
1462 .bdrv_get_allocated_file_size
1463 = raw_get_allocated_file_size
,
1465 /* generic scsi device */
1467 .bdrv_ioctl
= hdev_ioctl
,
1468 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1473 static int floppy_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1475 BDRVRawState
*s
= bs
->opaque
;
1480 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1481 ret
= raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1485 /* close fd so that we can reopen it as needed */
1488 s
->fd_media_changed
= 1;
1493 static int floppy_probe_device(const char *filename
)
1497 struct floppy_struct fdparam
;
1500 if (strstart(filename
, "/dev/fd", NULL
) &&
1501 !strstart(filename
, "/dev/fdset/", NULL
)) {
1505 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1509 ret
= fstat(fd
, &st
);
1510 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1514 /* Attempt to detect via a floppy specific ioctl */
1515 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1526 static int floppy_is_inserted(BlockDriverState
*bs
)
1528 return fd_open(bs
) >= 0;
1531 static int floppy_media_changed(BlockDriverState
*bs
)
1533 BDRVRawState
*s
= bs
->opaque
;
1537 * XXX: we do not have a true media changed indication.
1538 * It does not work if the floppy is changed without trying to read it.
1541 ret
= s
->fd_media_changed
;
1542 s
->fd_media_changed
= 0;
1544 printf("Floppy changed=%d\n", ret
);
1549 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1551 BDRVRawState
*s
= bs
->opaque
;
1558 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1560 if (ioctl(fd
, FDEJECT
, 0) < 0)
1566 static BlockDriver bdrv_host_floppy
= {
1567 .format_name
= "host_floppy",
1568 .protocol_name
= "host_floppy",
1569 .instance_size
= sizeof(BDRVRawState
),
1570 .bdrv_probe_device
= floppy_probe_device
,
1571 .bdrv_file_open
= floppy_open
,
1572 .bdrv_close
= raw_close
,
1573 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1574 .bdrv_reopen_commit
= raw_reopen_commit
,
1575 .bdrv_reopen_abort
= raw_reopen_abort
,
1576 .bdrv_create
= hdev_create
,
1577 .create_options
= raw_create_options
,
1578 .bdrv_has_zero_init
= hdev_has_zero_init
,
1580 .bdrv_aio_readv
= raw_aio_readv
,
1581 .bdrv_aio_writev
= raw_aio_writev
,
1582 .bdrv_aio_flush
= raw_aio_flush
,
1584 .bdrv_truncate
= raw_truncate
,
1585 .bdrv_getlength
= raw_getlength
,
1586 .bdrv_get_allocated_file_size
1587 = raw_get_allocated_file_size
,
1589 /* removable device support */
1590 .bdrv_is_inserted
= floppy_is_inserted
,
1591 .bdrv_media_changed
= floppy_media_changed
,
1592 .bdrv_eject
= floppy_eject
,
1595 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1597 BDRVRawState
*s
= bs
->opaque
;
1601 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1602 return raw_open_common(bs
, filename
, flags
, O_NONBLOCK
);
1605 static int cdrom_probe_device(const char *filename
)
1611 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1615 ret
= fstat(fd
, &st
);
1616 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1620 /* Attempt to detect via a CDROM specific ioctl */
1621 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1631 static int cdrom_is_inserted(BlockDriverState
*bs
)
1633 BDRVRawState
*s
= bs
->opaque
;
1636 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1637 if (ret
== CDS_DISC_OK
)
1642 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1644 BDRVRawState
*s
= bs
->opaque
;
1647 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1648 perror("CDROMEJECT");
1650 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1651 perror("CDROMEJECT");
1655 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1657 BDRVRawState
*s
= bs
->opaque
;
1659 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1661 * Note: an error can happen if the distribution automatically
1664 /* perror("CDROM_LOCKDOOR"); */
1668 static BlockDriver bdrv_host_cdrom
= {
1669 .format_name
= "host_cdrom",
1670 .protocol_name
= "host_cdrom",
1671 .instance_size
= sizeof(BDRVRawState
),
1672 .bdrv_probe_device
= cdrom_probe_device
,
1673 .bdrv_file_open
= cdrom_open
,
1674 .bdrv_close
= raw_close
,
1675 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1676 .bdrv_reopen_commit
= raw_reopen_commit
,
1677 .bdrv_reopen_abort
= raw_reopen_abort
,
1678 .bdrv_create
= hdev_create
,
1679 .create_options
= raw_create_options
,
1680 .bdrv_has_zero_init
= hdev_has_zero_init
,
1682 .bdrv_aio_readv
= raw_aio_readv
,
1683 .bdrv_aio_writev
= raw_aio_writev
,
1684 .bdrv_aio_flush
= raw_aio_flush
,
1686 .bdrv_truncate
= raw_truncate
,
1687 .bdrv_getlength
= raw_getlength
,
1688 .bdrv_get_allocated_file_size
1689 = raw_get_allocated_file_size
,
1691 /* removable device support */
1692 .bdrv_is_inserted
= cdrom_is_inserted
,
1693 .bdrv_eject
= cdrom_eject
,
1694 .bdrv_lock_medium
= cdrom_lock_medium
,
1696 /* generic scsi device */
1697 .bdrv_ioctl
= hdev_ioctl
,
1698 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1700 #endif /* __linux__ */
1702 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1703 static int cdrom_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1705 BDRVRawState
*s
= bs
->opaque
;
1710 ret
= raw_open_common(bs
, filename
, flags
, 0);
1714 /* make sure the door isn't locked at this time */
1715 ioctl(s
->fd
, CDIOCALLOW
);
1719 static int cdrom_probe_device(const char *filename
)
1721 if (strstart(filename
, "/dev/cd", NULL
) ||
1722 strstart(filename
, "/dev/acd", NULL
))
1727 static int cdrom_reopen(BlockDriverState
*bs
)
1729 BDRVRawState
*s
= bs
->opaque
;
1733 * Force reread of possibly changed/newly loaded disc,
1734 * FreeBSD seems to not notice sometimes...
1738 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
1745 /* make sure the door isn't locked at this time */
1746 ioctl(s
->fd
, CDIOCALLOW
);
1750 static int cdrom_is_inserted(BlockDriverState
*bs
)
1752 return raw_getlength(bs
) > 0;
1755 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1757 BDRVRawState
*s
= bs
->opaque
;
1762 (void) ioctl(s
->fd
, CDIOCALLOW
);
1765 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
1766 perror("CDIOCEJECT");
1768 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
1769 perror("CDIOCCLOSE");
1775 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1777 BDRVRawState
*s
= bs
->opaque
;
1781 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
1783 * Note: an error can happen if the distribution automatically
1786 /* perror("CDROM_LOCKDOOR"); */
1790 static BlockDriver bdrv_host_cdrom
= {
1791 .format_name
= "host_cdrom",
1792 .protocol_name
= "host_cdrom",
1793 .instance_size
= sizeof(BDRVRawState
),
1794 .bdrv_probe_device
= cdrom_probe_device
,
1795 .bdrv_file_open
= cdrom_open
,
1796 .bdrv_close
= raw_close
,
1797 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1798 .bdrv_reopen_commit
= raw_reopen_commit
,
1799 .bdrv_reopen_abort
= raw_reopen_abort
,
1800 .bdrv_create
= hdev_create
,
1801 .create_options
= raw_create_options
,
1802 .bdrv_has_zero_init
= hdev_has_zero_init
,
1804 .bdrv_aio_readv
= raw_aio_readv
,
1805 .bdrv_aio_writev
= raw_aio_writev
,
1806 .bdrv_aio_flush
= raw_aio_flush
,
1808 .bdrv_truncate
= raw_truncate
,
1809 .bdrv_getlength
= raw_getlength
,
1810 .bdrv_get_allocated_file_size
1811 = raw_get_allocated_file_size
,
1813 /* removable device support */
1814 .bdrv_is_inserted
= cdrom_is_inserted
,
1815 .bdrv_eject
= cdrom_eject
,
1816 .bdrv_lock_medium
= cdrom_lock_medium
,
1818 #endif /* __FreeBSD__ */
1820 #ifdef CONFIG_LINUX_AIO
1822 * Return the file descriptor for Linux AIO
1824 * This function is a layering violation and should be removed when it becomes
1825 * possible to call the block layer outside the global mutex. It allows the
1826 * caller to hijack the file descriptor so I/O can be performed outside the
1829 int raw_get_aio_fd(BlockDriverState
*bs
)
1837 if (bs
->drv
== bdrv_find_format("raw")) {
1841 /* raw-posix has several protocols so just check for raw_aio_readv */
1842 if (bs
->drv
->bdrv_aio_readv
!= raw_aio_readv
) {
1852 #endif /* CONFIG_LINUX_AIO */
1854 static void bdrv_file_init(void)
1857 * Register all the drivers. Note that order is important, the driver
1858 * registered last will get probed first.
1860 bdrv_register(&bdrv_file
);
1861 bdrv_register(&bdrv_host_device
);
1863 bdrv_register(&bdrv_host_floppy
);
1864 bdrv_register(&bdrv_host_cdrom
);
1866 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1867 bdrv_register(&bdrv_host_cdrom
);
1871 block_init(bdrv_file_init
);