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
145 bool has_write_zeroes
:1;
146 bool discard_zeroes
:1;
149 typedef struct BDRVRawReopenState
{
152 #ifdef CONFIG_LINUX_AIO
155 } BDRVRawReopenState
;
157 static int fd_open(BlockDriverState
*bs
);
158 static int64_t raw_getlength(BlockDriverState
*bs
);
160 typedef struct RawPosixAIOData
{
161 BlockDriverState
*bs
;
164 struct iovec
*aio_iov
;
169 #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */
174 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
175 static int cdrom_reopen(BlockDriverState
*bs
);
178 #if defined(__NetBSD__)
179 static int raw_normalize_devicepath(const char **filename
)
181 static char namebuf
[PATH_MAX
];
182 const char *dp
, *fname
;
186 dp
= strrchr(fname
, '/');
187 if (lstat(fname
, &sb
) < 0) {
188 fprintf(stderr
, "%s: stat failed: %s\n",
189 fname
, strerror(errno
));
193 if (!S_ISBLK(sb
.st_mode
)) {
198 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
200 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
201 (int)(dp
- fname
), fname
, dp
+ 1);
203 fprintf(stderr
, "%s is a block device", fname
);
205 fprintf(stderr
, ", using %s\n", *filename
);
210 static int raw_normalize_devicepath(const char **filename
)
216 static void raw_parse_flags(int bdrv_flags
, int *open_flags
)
218 assert(open_flags
!= NULL
);
220 *open_flags
|= O_BINARY
;
221 *open_flags
&= ~O_ACCMODE
;
222 if (bdrv_flags
& BDRV_O_RDWR
) {
223 *open_flags
|= O_RDWR
;
225 *open_flags
|= O_RDONLY
;
228 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
229 * and O_DIRECT for no caching. */
230 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
231 *open_flags
|= O_DIRECT
;
235 #ifdef CONFIG_LINUX_AIO
236 static int raw_set_aio(void **aio_ctx
, int *use_aio
, int bdrv_flags
)
239 assert(aio_ctx
!= NULL
);
240 assert(use_aio
!= NULL
);
242 * Currently Linux do AIO only for files opened with O_DIRECT
243 * specified so check NOCACHE flag too
245 if ((bdrv_flags
& (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) ==
246 (BDRV_O_NOCACHE
|BDRV_O_NATIVE_AIO
)) {
248 /* if non-NULL, laio_init() has already been run */
249 if (*aio_ctx
== NULL
) {
250 *aio_ctx
= laio_init();
267 static QemuOptsList raw_runtime_opts
= {
269 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
273 .type
= QEMU_OPT_STRING
,
274 .help
= "File name of the image",
276 { /* end of list */ }
280 static int raw_open_common(BlockDriverState
*bs
, QDict
*options
,
281 int bdrv_flags
, int open_flags
, Error
**errp
)
283 BDRVRawState
*s
= bs
->opaque
;
285 Error
*local_err
= NULL
;
286 const char *filename
;
290 opts
= qemu_opts_create_nofail(&raw_runtime_opts
);
291 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
292 if (error_is_set(&local_err
)) {
293 error_propagate(errp
, local_err
);
298 filename
= qemu_opt_get(opts
, "filename");
300 ret
= raw_normalize_devicepath(&filename
);
302 error_setg_errno(errp
, -ret
, "Could not normalize device path");
306 s
->open_flags
= open_flags
;
307 raw_parse_flags(bdrv_flags
, &s
->open_flags
);
310 fd
= qemu_open(filename
, s
->open_flags
, 0644);
320 #ifdef CONFIG_LINUX_AIO
321 if (raw_set_aio(&s
->aio_ctx
, &s
->use_aio
, bdrv_flags
)) {
324 error_setg_errno(errp
, -ret
, "Could not set AIO state");
329 s
->has_discard
= true;
330 s
->has_write_zeroes
= true;
332 if (fstat(s
->fd
, &st
) < 0) {
333 error_setg_errno(errp
, errno
, "Could not stat file");
336 if (S_ISREG(st
.st_mode
)) {
337 s
->discard_zeroes
= true;
339 if (S_ISBLK(st
.st_mode
)) {
340 #ifdef BLKDISCARDZEROES
342 if (ioctl(s
->fd
, BLKDISCARDZEROES
, &arg
) == 0 && arg
) {
343 s
->discard_zeroes
= true;
347 /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do
348 * not rely on the contents of discarded blocks unless using O_DIRECT.
349 * Same for BLKZEROOUT.
351 if (!(bs
->open_flags
& BDRV_O_NOCACHE
)) {
352 s
->discard_zeroes
= false;
353 s
->has_write_zeroes
= false;
359 if (platform_test_xfs_fd(s
->fd
)) {
370 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
373 BDRVRawState
*s
= bs
->opaque
;
374 Error
*local_err
= NULL
;
377 s
->type
= FTYPE_FILE
;
378 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
379 if (error_is_set(&local_err
)) {
380 error_propagate(errp
, local_err
);
385 static int raw_reopen_prepare(BDRVReopenState
*state
,
386 BlockReopenQueue
*queue
, Error
**errp
)
389 BDRVRawReopenState
*raw_s
;
392 assert(state
!= NULL
);
393 assert(state
->bs
!= NULL
);
395 s
= state
->bs
->opaque
;
397 state
->opaque
= g_malloc0(sizeof(BDRVRawReopenState
));
398 raw_s
= state
->opaque
;
400 #ifdef CONFIG_LINUX_AIO
401 raw_s
->use_aio
= s
->use_aio
;
403 /* we can use s->aio_ctx instead of a copy, because the use_aio flag is
404 * valid in the 'false' condition even if aio_ctx is set, and raw_set_aio()
405 * won't override aio_ctx if aio_ctx is non-NULL */
406 if (raw_set_aio(&s
->aio_ctx
, &raw_s
->use_aio
, state
->flags
)) {
407 error_setg(errp
, "Could not set AIO state");
412 if (s
->type
== FTYPE_FD
|| s
->type
== FTYPE_CD
) {
413 raw_s
->open_flags
|= O_NONBLOCK
;
416 raw_parse_flags(state
->flags
, &raw_s
->open_flags
);
420 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
422 fcntl_flags
|= O_NOATIME
;
426 /* Not all operating systems have O_ASYNC, and those that don't
427 * will not let us track the state into raw_s->open_flags (typically
428 * you achieve the same effect with an ioctl, for example I_SETSIG
429 * on Solaris). But we do not use O_ASYNC, so that's fine.
431 assert((s
->open_flags
& O_ASYNC
) == 0);
434 if ((raw_s
->open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
435 /* dup the original fd */
436 /* TODO: use qemu fcntl wrapper */
437 #ifdef F_DUPFD_CLOEXEC
438 raw_s
->fd
= fcntl(s
->fd
, F_DUPFD_CLOEXEC
, 0);
440 raw_s
->fd
= dup(s
->fd
);
441 if (raw_s
->fd
!= -1) {
442 qemu_set_cloexec(raw_s
->fd
);
445 if (raw_s
->fd
>= 0) {
446 ret
= fcntl_setfl(raw_s
->fd
, raw_s
->open_flags
);
448 qemu_close(raw_s
->fd
);
454 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
455 if (raw_s
->fd
== -1) {
456 assert(!(raw_s
->open_flags
& O_CREAT
));
457 raw_s
->fd
= qemu_open(state
->bs
->filename
, raw_s
->open_flags
);
458 if (raw_s
->fd
== -1) {
459 error_setg_errno(errp
, errno
, "Could not reopen file");
467 static void raw_reopen_commit(BDRVReopenState
*state
)
469 BDRVRawReopenState
*raw_s
= state
->opaque
;
470 BDRVRawState
*s
= state
->bs
->opaque
;
472 s
->open_flags
= raw_s
->open_flags
;
476 #ifdef CONFIG_LINUX_AIO
477 s
->use_aio
= raw_s
->use_aio
;
480 g_free(state
->opaque
);
481 state
->opaque
= NULL
;
485 static void raw_reopen_abort(BDRVReopenState
*state
)
487 BDRVRawReopenState
*raw_s
= state
->opaque
;
489 /* nothing to do if NULL, we didn't get far enough */
494 if (raw_s
->fd
>= 0) {
495 qemu_close(raw_s
->fd
);
498 g_free(state
->opaque
);
499 state
->opaque
= NULL
;
503 /* XXX: use host sector size if necessary with:
504 #ifdef DIOCGSECTORSIZE
506 unsigned int sectorsize = 512;
507 if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
508 sectorsize > bufsize)
509 bufsize = sectorsize;
513 uint32_t blockSize = 512;
514 if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
520 static ssize_t
handle_aiocb_ioctl(RawPosixAIOData
*aiocb
)
524 ret
= ioctl(aiocb
->aio_fildes
, aiocb
->aio_ioctl_cmd
, aiocb
->aio_ioctl_buf
);
532 static ssize_t
handle_aiocb_flush(RawPosixAIOData
*aiocb
)
536 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
545 static bool preadv_present
= true;
548 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
550 return preadv(fd
, iov
, nr_iov
, offset
);
554 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
556 return pwritev(fd
, iov
, nr_iov
, offset
);
561 static bool preadv_present
= false;
564 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
570 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
577 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
582 if (aiocb
->aio_type
& QEMU_AIO_WRITE
)
583 len
= qemu_pwritev(aiocb
->aio_fildes
,
588 len
= qemu_preadv(aiocb
->aio_fildes
,
592 } while (len
== -1 && errno
== EINTR
);
601 * Read/writes the data to/from a given linear buffer.
603 * Returns the number of bytes handles or -errno in case of an error. Short
604 * reads are only returned if the end of the file is reached.
606 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
611 while (offset
< aiocb
->aio_nbytes
) {
612 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
613 len
= pwrite(aiocb
->aio_fildes
,
614 (const char *)buf
+ offset
,
615 aiocb
->aio_nbytes
- offset
,
616 aiocb
->aio_offset
+ offset
);
618 len
= pread(aiocb
->aio_fildes
,
620 aiocb
->aio_nbytes
- offset
,
621 aiocb
->aio_offset
+ offset
);
623 if (len
== -1 && errno
== EINTR
) {
625 } else if (len
== -1) {
628 } else if (len
== 0) {
637 static ssize_t
handle_aiocb_rw(RawPosixAIOData
*aiocb
)
642 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
644 * If there is just a single buffer, and it is properly aligned
645 * we can just use plain pread/pwrite without any problems.
647 if (aiocb
->aio_niov
== 1) {
648 return handle_aiocb_rw_linear(aiocb
, aiocb
->aio_iov
->iov_base
);
651 * We have more than one iovec, and all are properly aligned.
653 * Try preadv/pwritev first and fall back to linearizing the
654 * buffer if it's not supported.
656 if (preadv_present
) {
657 nbytes
= handle_aiocb_rw_vector(aiocb
);
658 if (nbytes
== aiocb
->aio_nbytes
||
659 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
662 preadv_present
= false;
666 * XXX(hch): short read/write. no easy way to handle the reminder
667 * using these interfaces. For now retry using plain
673 * Ok, we have to do it the hard way, copy all segments into
674 * a single aligned buffer.
676 buf
= qemu_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
677 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
681 for (i
= 0; i
< aiocb
->aio_niov
; ++i
) {
682 memcpy(p
, aiocb
->aio_iov
[i
].iov_base
, aiocb
->aio_iov
[i
].iov_len
);
683 p
+= aiocb
->aio_iov
[i
].iov_len
;
687 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
688 if (!(aiocb
->aio_type
& QEMU_AIO_WRITE
)) {
690 size_t count
= aiocb
->aio_nbytes
, copy
;
693 for (i
= 0; i
< aiocb
->aio_niov
&& count
; ++i
) {
695 if (copy
> aiocb
->aio_iov
[i
].iov_len
) {
696 copy
= aiocb
->aio_iov
[i
].iov_len
;
698 memcpy(aiocb
->aio_iov
[i
].iov_base
, p
, copy
);
709 static int xfs_write_zeroes(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
711 struct xfs_flock64 fl
;
713 memset(&fl
, 0, sizeof(fl
));
714 fl
.l_whence
= SEEK_SET
;
718 if (xfsctl(NULL
, s
->fd
, XFS_IOC_ZERO_RANGE
, &fl
) < 0) {
719 DEBUG_BLOCK_PRINT("cannot write zero range (%s)\n", strerror(errno
));
726 static int xfs_discard(BDRVRawState
*s
, int64_t offset
, uint64_t bytes
)
728 struct xfs_flock64 fl
;
730 memset(&fl
, 0, sizeof(fl
));
731 fl
.l_whence
= SEEK_SET
;
735 if (xfsctl(NULL
, s
->fd
, XFS_IOC_UNRESVSP64
, &fl
) < 0) {
736 DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno
));
744 static ssize_t
handle_aiocb_write_zeroes(RawPosixAIOData
*aiocb
)
746 int ret
= -EOPNOTSUPP
;
747 BDRVRawState
*s
= aiocb
->bs
->opaque
;
749 if (s
->has_write_zeroes
== 0) {
753 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
756 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
757 if (ioctl(aiocb
->aio_fildes
, BLKZEROOUT
, range
) == 0) {
760 } while (errno
== EINTR
);
767 return xfs_write_zeroes(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
772 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
774 s
->has_write_zeroes
= false;
780 static ssize_t
handle_aiocb_discard(RawPosixAIOData
*aiocb
)
782 int ret
= -EOPNOTSUPP
;
783 BDRVRawState
*s
= aiocb
->bs
->opaque
;
785 if (!s
->has_discard
) {
789 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
792 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
793 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
796 } while (errno
== EINTR
);
803 return xfs_discard(s
, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
807 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
809 if (fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
810 aiocb
->aio_offset
, aiocb
->aio_nbytes
) == 0) {
813 } while (errno
== EINTR
);
819 if (ret
== -ENODEV
|| ret
== -ENOSYS
|| ret
== -EOPNOTSUPP
||
821 s
->has_discard
= false;
827 static int aio_worker(void *arg
)
829 RawPosixAIOData
*aiocb
= arg
;
832 switch (aiocb
->aio_type
& QEMU_AIO_TYPE_MASK
) {
834 ret
= handle_aiocb_rw(aiocb
);
835 if (ret
>= 0 && ret
< aiocb
->aio_nbytes
&& aiocb
->bs
->growable
) {
836 iov_memset(aiocb
->aio_iov
, aiocb
->aio_niov
, ret
,
837 0, aiocb
->aio_nbytes
- ret
);
839 ret
= aiocb
->aio_nbytes
;
841 if (ret
== aiocb
->aio_nbytes
) {
843 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
848 ret
= handle_aiocb_rw(aiocb
);
849 if (ret
== aiocb
->aio_nbytes
) {
851 } else if (ret
>= 0 && ret
< aiocb
->aio_nbytes
) {
856 ret
= handle_aiocb_flush(aiocb
);
859 ret
= handle_aiocb_ioctl(aiocb
);
861 case QEMU_AIO_DISCARD
:
862 ret
= handle_aiocb_discard(aiocb
);
864 case QEMU_AIO_WRITE_ZEROES
:
865 ret
= handle_aiocb_write_zeroes(aiocb
);
868 fprintf(stderr
, "invalid aio request (0x%x)\n", aiocb
->aio_type
);
873 g_slice_free(RawPosixAIOData
, aiocb
);
877 static int paio_submit_co(BlockDriverState
*bs
, int fd
,
878 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
881 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
885 acb
->aio_type
= type
;
886 acb
->aio_fildes
= fd
;
889 acb
->aio_iov
= qiov
->iov
;
890 acb
->aio_niov
= qiov
->niov
;
892 acb
->aio_nbytes
= nb_sectors
* 512;
893 acb
->aio_offset
= sector_num
* 512;
895 trace_paio_submit_co(sector_num
, nb_sectors
, type
);
896 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
897 return thread_pool_submit_co(pool
, aio_worker
, acb
);
900 static BlockDriverAIOCB
*paio_submit(BlockDriverState
*bs
, int fd
,
901 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
902 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
904 RawPosixAIOData
*acb
= g_slice_new(RawPosixAIOData
);
908 acb
->aio_type
= type
;
909 acb
->aio_fildes
= fd
;
912 acb
->aio_iov
= qiov
->iov
;
913 acb
->aio_niov
= qiov
->niov
;
915 acb
->aio_nbytes
= nb_sectors
* 512;
916 acb
->aio_offset
= sector_num
* 512;
918 trace_paio_submit(acb
, opaque
, sector_num
, nb_sectors
, type
);
919 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
920 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
923 static BlockDriverAIOCB
*raw_aio_submit(BlockDriverState
*bs
,
924 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
925 BlockDriverCompletionFunc
*cb
, void *opaque
, int type
)
927 BDRVRawState
*s
= bs
->opaque
;
933 * If O_DIRECT is used the buffer needs to be aligned on a sector
934 * boundary. Check if this is the case or tell the low-level
935 * driver that it needs to copy the buffer.
937 if ((bs
->open_flags
& BDRV_O_NOCACHE
)) {
938 if (!bdrv_qiov_is_aligned(bs
, qiov
)) {
939 type
|= QEMU_AIO_MISALIGNED
;
940 #ifdef CONFIG_LINUX_AIO
941 } else if (s
->use_aio
) {
942 return laio_submit(bs
, s
->aio_ctx
, s
->fd
, sector_num
, qiov
,
943 nb_sectors
, cb
, opaque
, type
);
948 return paio_submit(bs
, s
->fd
, sector_num
, qiov
, nb_sectors
,
952 static BlockDriverAIOCB
*raw_aio_readv(BlockDriverState
*bs
,
953 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
954 BlockDriverCompletionFunc
*cb
, void *opaque
)
956 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
957 cb
, opaque
, QEMU_AIO_READ
);
960 static BlockDriverAIOCB
*raw_aio_writev(BlockDriverState
*bs
,
961 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
962 BlockDriverCompletionFunc
*cb
, void *opaque
)
964 return raw_aio_submit(bs
, sector_num
, qiov
, nb_sectors
,
965 cb
, opaque
, QEMU_AIO_WRITE
);
968 static BlockDriverAIOCB
*raw_aio_flush(BlockDriverState
*bs
,
969 BlockDriverCompletionFunc
*cb
, void *opaque
)
971 BDRVRawState
*s
= bs
->opaque
;
976 return paio_submit(bs
, s
->fd
, 0, NULL
, 0, cb
, opaque
, QEMU_AIO_FLUSH
);
979 static void raw_close(BlockDriverState
*bs
)
981 BDRVRawState
*s
= bs
->opaque
;
988 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
990 BDRVRawState
*s
= bs
->opaque
;
993 if (fstat(s
->fd
, &st
)) {
997 if (S_ISREG(st
.st_mode
)) {
998 if (ftruncate(s
->fd
, offset
) < 0) {
1001 } else if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1002 if (offset
> raw_getlength(bs
)) {
1013 static int64_t raw_getlength(BlockDriverState
*bs
)
1015 BDRVRawState
*s
= bs
->opaque
;
1021 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1022 struct disklabel dl
;
1024 if (ioctl(fd
, DIOCGDINFO
, &dl
))
1026 return (uint64_t)dl
.d_secsize
*
1027 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
1031 #elif defined(__NetBSD__)
1032 static int64_t raw_getlength(BlockDriverState
*bs
)
1034 BDRVRawState
*s
= bs
->opaque
;
1040 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
1041 struct dkwedge_info dkw
;
1043 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
1044 return dkw
.dkw_size
* 512;
1046 struct disklabel dl
;
1048 if (ioctl(fd
, DIOCGDINFO
, &dl
))
1050 return (uint64_t)dl
.d_secsize
*
1051 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
1056 #elif defined(__sun__)
1057 static int64_t raw_getlength(BlockDriverState
*bs
)
1059 BDRVRawState
*s
= bs
->opaque
;
1060 struct dk_minfo minfo
;
1069 * Use the DKIOCGMEDIAINFO ioctl to read the size.
1071 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
1073 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
1077 * There are reports that lseek on some devices fails, but
1078 * irc discussion said that contingency on contingency was overkill.
1080 return lseek(s
->fd
, 0, SEEK_END
);
1082 #elif defined(CONFIG_BSD)
1083 static int64_t raw_getlength(BlockDriverState
*bs
)
1085 BDRVRawState
*s
= bs
->opaque
;
1089 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1098 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
1101 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
1102 #ifdef DIOCGMEDIASIZE
1103 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
))
1104 #elif defined(DIOCGPART)
1107 if (ioctl(fd
, DIOCGPART
, &pi
) == 0)
1108 size
= pi
.media_size
;
1114 #if defined(__APPLE__) && defined(__MACH__)
1115 size
= LONG_LONG_MAX
;
1117 size
= lseek(fd
, 0LL, SEEK_END
);
1119 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1122 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
1123 if (size
== 2048LL * (unsigned)-1)
1125 /* XXX no disc? maybe we need to reopen... */
1126 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
1133 size
= lseek(fd
, 0, SEEK_END
);
1138 static int64_t raw_getlength(BlockDriverState
*bs
)
1140 BDRVRawState
*s
= bs
->opaque
;
1148 return lseek(s
->fd
, 0, SEEK_END
);
1152 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
1155 BDRVRawState
*s
= bs
->opaque
;
1157 if (fstat(s
->fd
, &st
) < 0) {
1160 return (int64_t)st
.st_blocks
* 512;
1163 static int raw_create(const char *filename
, QEMUOptionParameter
*options
,
1168 int64_t total_size
= 0;
1170 /* Read out options */
1171 while (options
&& options
->name
) {
1172 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1173 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1178 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
1182 error_setg_errno(errp
, -result
, "Could not create file");
1184 if (ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
1186 error_setg_errno(errp
, -result
, "Could not resize file");
1188 if (qemu_close(fd
) != 0) {
1190 error_setg_errno(errp
, -result
, "Could not close the new file");
1197 * Returns true iff the specified sector is present in the disk image. Drivers
1198 * not implementing the functionality are assumed to not support backing files,
1199 * hence all their sectors are reported as allocated.
1201 * If 'sector_num' is beyond the end of the disk image the return value is 0
1202 * and 'pnum' is set to 0.
1204 * 'pnum' is set to the number of sectors (including and immediately following
1205 * the specified sector) that are known to be in the same
1206 * allocated/unallocated state.
1208 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1209 * beyond the end of the disk image it will be clamped.
1211 static int64_t coroutine_fn
raw_co_get_block_status(BlockDriverState
*bs
,
1213 int nb_sectors
, int *pnum
)
1215 off_t start
, data
, hole
;
1223 start
= sector_num
* BDRV_SECTOR_SIZE
;
1224 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
1226 #ifdef CONFIG_FIEMAP
1228 BDRVRawState
*s
= bs
->opaque
;
1231 struct fiemap_extent fe
;
1234 f
.fm
.fm_start
= start
;
1235 f
.fm
.fm_length
= (int64_t)nb_sectors
* BDRV_SECTOR_SIZE
;
1237 f
.fm
.fm_extent_count
= 1;
1238 f
.fm
.fm_reserved
= 0;
1239 if (ioctl(s
->fd
, FS_IOC_FIEMAP
, &f
) == -1) {
1240 /* Assume everything is allocated. */
1245 if (f
.fm
.fm_mapped_extents
== 0) {
1246 /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
1247 * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
1249 off_t length
= lseek(s
->fd
, 0, SEEK_END
);
1250 hole
= f
.fm
.fm_start
;
1251 data
= MIN(f
.fm
.fm_start
+ f
.fm
.fm_length
, length
);
1253 data
= f
.fe
.fe_logical
;
1254 hole
= f
.fe
.fe_logical
+ f
.fe
.fe_length
;
1255 if (f
.fe
.fe_flags
& FIEMAP_EXTENT_UNWRITTEN
) {
1256 ret
|= BDRV_BLOCK_ZERO
;
1260 #elif defined SEEK_HOLE && defined SEEK_DATA
1262 BDRVRawState
*s
= bs
->opaque
;
1264 hole
= lseek(s
->fd
, start
, SEEK_HOLE
);
1266 /* -ENXIO indicates that sector_num was past the end of the file.
1267 * There is a virtual hole there. */
1268 assert(errno
!= -ENXIO
);
1270 /* Most likely EINVAL. Assume everything is allocated. */
1278 /* On a hole. We need another syscall to find its end. */
1279 data
= lseek(s
->fd
, start
, SEEK_DATA
);
1281 data
= lseek(s
->fd
, 0, SEEK_END
);
1286 hole
= start
+ nb_sectors
* BDRV_SECTOR_SIZE
;
1289 if (data
<= start
) {
1290 /* On a data extent, compute sectors to the end of the extent. */
1291 *pnum
= MIN(nb_sectors
, (hole
- start
) / BDRV_SECTOR_SIZE
);
1293 /* On a hole, compute sectors to the beginning of the next extent. */
1294 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1295 ret
&= ~BDRV_BLOCK_DATA
;
1296 ret
|= BDRV_BLOCK_ZERO
;
1302 static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState
*bs
,
1303 int64_t sector_num
, int nb_sectors
,
1304 BlockDriverCompletionFunc
*cb
, void *opaque
)
1306 BDRVRawState
*s
= bs
->opaque
;
1308 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1309 cb
, opaque
, QEMU_AIO_DISCARD
);
1312 static int coroutine_fn
raw_co_write_zeroes(
1313 BlockDriverState
*bs
, int64_t sector_num
,
1314 int nb_sectors
, BdrvRequestFlags flags
)
1316 BDRVRawState
*s
= bs
->opaque
;
1318 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1319 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1320 QEMU_AIO_WRITE_ZEROES
);
1321 } else if (s
->discard_zeroes
) {
1322 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1328 static int raw_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1330 BDRVRawState
*s
= bs
->opaque
;
1332 bdi
->unallocated_blocks_are_zero
= s
->discard_zeroes
;
1333 bdi
->can_write_zeroes_with_unmap
= s
->discard_zeroes
;
1337 static QEMUOptionParameter raw_create_options
[] = {
1339 .name
= BLOCK_OPT_SIZE
,
1341 .help
= "Virtual disk size"
1346 static BlockDriver bdrv_file
= {
1347 .format_name
= "file",
1348 .protocol_name
= "file",
1349 .instance_size
= sizeof(BDRVRawState
),
1350 .bdrv_needs_filename
= true,
1351 .bdrv_probe
= NULL
, /* no probe for protocols */
1352 .bdrv_file_open
= raw_open
,
1353 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1354 .bdrv_reopen_commit
= raw_reopen_commit
,
1355 .bdrv_reopen_abort
= raw_reopen_abort
,
1356 .bdrv_close
= raw_close
,
1357 .bdrv_create
= raw_create
,
1358 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1359 .bdrv_co_get_block_status
= raw_co_get_block_status
,
1360 .bdrv_co_write_zeroes
= raw_co_write_zeroes
,
1362 .bdrv_aio_readv
= raw_aio_readv
,
1363 .bdrv_aio_writev
= raw_aio_writev
,
1364 .bdrv_aio_flush
= raw_aio_flush
,
1365 .bdrv_aio_discard
= raw_aio_discard
,
1367 .bdrv_truncate
= raw_truncate
,
1368 .bdrv_getlength
= raw_getlength
,
1369 .bdrv_get_info
= raw_get_info
,
1370 .bdrv_get_allocated_file_size
1371 = raw_get_allocated_file_size
,
1373 .create_options
= raw_create_options
,
1376 /***********************************************/
1379 #if defined(__APPLE__) && defined(__MACH__)
1380 static kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
);
1381 static kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
);
1383 kern_return_t
FindEjectableCDMedia( io_iterator_t
*mediaIterator
)
1385 kern_return_t kernResult
;
1386 mach_port_t masterPort
;
1387 CFMutableDictionaryRef classesToMatch
;
1389 kernResult
= IOMasterPort( MACH_PORT_NULL
, &masterPort
);
1390 if ( KERN_SUCCESS
!= kernResult
) {
1391 printf( "IOMasterPort returned %d\n", kernResult
);
1394 classesToMatch
= IOServiceMatching( kIOCDMediaClass
);
1395 if ( classesToMatch
== NULL
) {
1396 printf( "IOServiceMatching returned a NULL dictionary.\n" );
1398 CFDictionarySetValue( classesToMatch
, CFSTR( kIOMediaEjectableKey
), kCFBooleanTrue
);
1400 kernResult
= IOServiceGetMatchingServices( masterPort
, classesToMatch
, mediaIterator
);
1401 if ( KERN_SUCCESS
!= kernResult
)
1403 printf( "IOServiceGetMatchingServices returned %d\n", kernResult
);
1409 kern_return_t
GetBSDPath( io_iterator_t mediaIterator
, char *bsdPath
, CFIndex maxPathSize
)
1411 io_object_t nextMedia
;
1412 kern_return_t kernResult
= KERN_FAILURE
;
1414 nextMedia
= IOIteratorNext( mediaIterator
);
1417 CFTypeRef bsdPathAsCFString
;
1418 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
1419 if ( bsdPathAsCFString
) {
1420 size_t devPathLength
;
1421 strcpy( bsdPath
, _PATH_DEV
);
1422 strcat( bsdPath
, "r" );
1423 devPathLength
= strlen( bsdPath
);
1424 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
1425 kernResult
= KERN_SUCCESS
;
1427 CFRelease( bsdPathAsCFString
);
1429 IOObjectRelease( nextMedia
);
1437 static int hdev_probe_device(const char *filename
)
1441 /* allow a dedicated CD-ROM driver to match with a higher priority */
1442 if (strstart(filename
, "/dev/cdrom", NULL
))
1445 if (stat(filename
, &st
) >= 0 &&
1446 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
1453 static int check_hdev_writable(BDRVRawState
*s
)
1455 #if defined(BLKROGET)
1456 /* Linux block devices can be configured "read-only" using blockdev(8).
1457 * This is independent of device node permissions and therefore open(2)
1458 * with O_RDWR succeeds. Actual writes fail with EPERM.
1460 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
1461 * check for read-only block devices so that Linux block devices behave
1467 if (fstat(s
->fd
, &st
)) {
1471 if (!S_ISBLK(st
.st_mode
)) {
1475 if (ioctl(s
->fd
, BLKROGET
, &readonly
) < 0) {
1482 #endif /* defined(BLKROGET) */
1486 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1489 BDRVRawState
*s
= bs
->opaque
;
1490 Error
*local_err
= NULL
;
1492 const char *filename
= qdict_get_str(options
, "filename");
1494 #if defined(__APPLE__) && defined(__MACH__)
1495 if (strstart(filename
, "/dev/cdrom", NULL
)) {
1496 kern_return_t kernResult
;
1497 io_iterator_t mediaIterator
;
1498 char bsdPath
[ MAXPATHLEN
];
1501 kernResult
= FindEjectableCDMedia( &mediaIterator
);
1502 kernResult
= GetBSDPath( mediaIterator
, bsdPath
, sizeof( bsdPath
) );
1504 if ( bsdPath
[ 0 ] != '\0' ) {
1505 strcat(bsdPath
,"s0");
1506 /* some CDs don't have a partition 0 */
1507 fd
= qemu_open(bsdPath
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
1509 bsdPath
[strlen(bsdPath
)-1] = '1';
1514 qdict_put(options
, "filename", qstring_from_str(filename
));
1517 if ( mediaIterator
)
1518 IOObjectRelease( mediaIterator
);
1522 s
->type
= FTYPE_FILE
;
1523 #if defined(__linux__)
1525 char resolved_path
[ MAXPATHLEN
], *temp
;
1527 temp
= realpath(filename
, resolved_path
);
1528 if (temp
&& strstart(temp
, "/dev/sg", NULL
)) {
1534 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
1536 if (error_is_set(&local_err
)) {
1537 error_propagate(errp
, local_err
);
1542 if (flags
& BDRV_O_RDWR
) {
1543 ret
= check_hdev_writable(s
);
1546 error_setg_errno(errp
, -ret
, "The device is not writable");
1554 #if defined(__linux__)
1555 /* Note: we do not have a reliable method to detect if the floppy is
1556 present. The current method is to try to open the floppy at every
1557 I/O and to keep it opened during a few hundreds of ms. */
1558 static int fd_open(BlockDriverState
*bs
)
1560 BDRVRawState
*s
= bs
->opaque
;
1561 int last_media_present
;
1563 if (s
->type
!= FTYPE_FD
)
1565 last_media_present
= (s
->fd
>= 0);
1567 (get_clock() - s
->fd_open_time
) >= FD_OPEN_TIMEOUT
) {
1571 printf("Floppy closed\n");
1575 if (s
->fd_got_error
&&
1576 (get_clock() - s
->fd_error_time
) < FD_OPEN_TIMEOUT
) {
1578 printf("No floppy (open delayed)\n");
1582 s
->fd
= qemu_open(bs
->filename
, s
->open_flags
& ~O_NONBLOCK
);
1584 s
->fd_error_time
= get_clock();
1585 s
->fd_got_error
= 1;
1586 if (last_media_present
)
1587 s
->fd_media_changed
= 1;
1589 printf("No floppy\n");
1594 printf("Floppy opened\n");
1597 if (!last_media_present
)
1598 s
->fd_media_changed
= 1;
1599 s
->fd_open_time
= get_clock();
1600 s
->fd_got_error
= 0;
1604 static int hdev_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
1606 BDRVRawState
*s
= bs
->opaque
;
1608 return ioctl(s
->fd
, req
, buf
);
1611 static BlockDriverAIOCB
*hdev_aio_ioctl(BlockDriverState
*bs
,
1612 unsigned long int req
, void *buf
,
1613 BlockDriverCompletionFunc
*cb
, void *opaque
)
1615 BDRVRawState
*s
= bs
->opaque
;
1616 RawPosixAIOData
*acb
;
1619 if (fd_open(bs
) < 0)
1622 acb
= g_slice_new(RawPosixAIOData
);
1624 acb
->aio_type
= QEMU_AIO_IOCTL
;
1625 acb
->aio_fildes
= s
->fd
;
1626 acb
->aio_offset
= 0;
1627 acb
->aio_ioctl_buf
= buf
;
1628 acb
->aio_ioctl_cmd
= req
;
1629 pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
1630 return thread_pool_submit_aio(pool
, aio_worker
, acb
, cb
, opaque
);
1633 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1634 static int fd_open(BlockDriverState
*bs
)
1636 BDRVRawState
*s
= bs
->opaque
;
1638 /* this is just to ensure s->fd is sane (its called by io ops) */
1643 #else /* !linux && !FreeBSD */
1645 static int fd_open(BlockDriverState
*bs
)
1650 #endif /* !linux && !FreeBSD */
1652 static coroutine_fn BlockDriverAIOCB
*hdev_aio_discard(BlockDriverState
*bs
,
1653 int64_t sector_num
, int nb_sectors
,
1654 BlockDriverCompletionFunc
*cb
, void *opaque
)
1656 BDRVRawState
*s
= bs
->opaque
;
1658 if (fd_open(bs
) < 0) {
1661 return paio_submit(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1662 cb
, opaque
, QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1665 static coroutine_fn
int hdev_co_write_zeroes(BlockDriverState
*bs
,
1666 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
1668 BDRVRawState
*s
= bs
->opaque
;
1675 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1676 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1677 QEMU_AIO_WRITE_ZEROES
|QEMU_AIO_BLKDEV
);
1678 } else if (s
->discard_zeroes
) {
1679 return paio_submit_co(bs
, s
->fd
, sector_num
, NULL
, nb_sectors
,
1680 QEMU_AIO_DISCARD
|QEMU_AIO_BLKDEV
);
1685 static int hdev_create(const char *filename
, QEMUOptionParameter
*options
,
1690 struct stat stat_buf
;
1691 int64_t total_size
= 0;
1693 /* Read out options */
1694 while (options
&& options
->name
) {
1695 if (!strcmp(options
->name
, "size")) {
1696 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1701 fd
= qemu_open(filename
, O_WRONLY
| O_BINARY
);
1704 error_setg_errno(errp
, -ret
, "Could not open device");
1708 if (fstat(fd
, &stat_buf
) < 0) {
1710 error_setg_errno(errp
, -ret
, "Could not stat device");
1711 } else if (!S_ISBLK(stat_buf
.st_mode
) && !S_ISCHR(stat_buf
.st_mode
)) {
1713 "The given file is neither a block nor a character device");
1715 } else if (lseek(fd
, 0, SEEK_END
) < total_size
* BDRV_SECTOR_SIZE
) {
1716 error_setg(errp
, "Device is too small");
1724 static BlockDriver bdrv_host_device
= {
1725 .format_name
= "host_device",
1726 .protocol_name
= "host_device",
1727 .instance_size
= sizeof(BDRVRawState
),
1728 .bdrv_needs_filename
= true,
1729 .bdrv_probe_device
= hdev_probe_device
,
1730 .bdrv_file_open
= hdev_open
,
1731 .bdrv_close
= raw_close
,
1732 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1733 .bdrv_reopen_commit
= raw_reopen_commit
,
1734 .bdrv_reopen_abort
= raw_reopen_abort
,
1735 .bdrv_create
= hdev_create
,
1736 .create_options
= raw_create_options
,
1737 .bdrv_co_write_zeroes
= hdev_co_write_zeroes
,
1739 .bdrv_aio_readv
= raw_aio_readv
,
1740 .bdrv_aio_writev
= raw_aio_writev
,
1741 .bdrv_aio_flush
= raw_aio_flush
,
1742 .bdrv_aio_discard
= hdev_aio_discard
,
1744 .bdrv_truncate
= raw_truncate
,
1745 .bdrv_getlength
= raw_getlength
,
1746 .bdrv_get_info
= raw_get_info
,
1747 .bdrv_get_allocated_file_size
1748 = raw_get_allocated_file_size
,
1750 /* generic scsi device */
1752 .bdrv_ioctl
= hdev_ioctl
,
1753 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
1758 static int floppy_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1761 BDRVRawState
*s
= bs
->opaque
;
1762 Error
*local_err
= NULL
;
1767 /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */
1768 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
1770 if (error_is_set(&local_err
)) {
1771 error_propagate(errp
, local_err
);
1776 /* close fd so that we can reopen it as needed */
1779 s
->fd_media_changed
= 1;
1784 static int floppy_probe_device(const char *filename
)
1788 struct floppy_struct fdparam
;
1791 if (strstart(filename
, "/dev/fd", NULL
) &&
1792 !strstart(filename
, "/dev/fdset/", NULL
)) {
1796 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1800 ret
= fstat(fd
, &st
);
1801 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1805 /* Attempt to detect via a floppy specific ioctl */
1806 ret
= ioctl(fd
, FDGETPRM
, &fdparam
);
1817 static int floppy_is_inserted(BlockDriverState
*bs
)
1819 return fd_open(bs
) >= 0;
1822 static int floppy_media_changed(BlockDriverState
*bs
)
1824 BDRVRawState
*s
= bs
->opaque
;
1828 * XXX: we do not have a true media changed indication.
1829 * It does not work if the floppy is changed without trying to read it.
1832 ret
= s
->fd_media_changed
;
1833 s
->fd_media_changed
= 0;
1835 printf("Floppy changed=%d\n", ret
);
1840 static void floppy_eject(BlockDriverState
*bs
, bool eject_flag
)
1842 BDRVRawState
*s
= bs
->opaque
;
1849 fd
= qemu_open(bs
->filename
, s
->open_flags
| O_NONBLOCK
);
1851 if (ioctl(fd
, FDEJECT
, 0) < 0)
1857 static BlockDriver bdrv_host_floppy
= {
1858 .format_name
= "host_floppy",
1859 .protocol_name
= "host_floppy",
1860 .instance_size
= sizeof(BDRVRawState
),
1861 .bdrv_needs_filename
= true,
1862 .bdrv_probe_device
= floppy_probe_device
,
1863 .bdrv_file_open
= floppy_open
,
1864 .bdrv_close
= raw_close
,
1865 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1866 .bdrv_reopen_commit
= raw_reopen_commit
,
1867 .bdrv_reopen_abort
= raw_reopen_abort
,
1868 .bdrv_create
= hdev_create
,
1869 .create_options
= raw_create_options
,
1871 .bdrv_aio_readv
= raw_aio_readv
,
1872 .bdrv_aio_writev
= raw_aio_writev
,
1873 .bdrv_aio_flush
= raw_aio_flush
,
1875 .bdrv_truncate
= raw_truncate
,
1876 .bdrv_getlength
= raw_getlength
,
1877 .has_variable_length
= true,
1878 .bdrv_get_allocated_file_size
1879 = raw_get_allocated_file_size
,
1881 /* removable device support */
1882 .bdrv_is_inserted
= floppy_is_inserted
,
1883 .bdrv_media_changed
= floppy_media_changed
,
1884 .bdrv_eject
= floppy_eject
,
1887 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1890 BDRVRawState
*s
= bs
->opaque
;
1891 Error
*local_err
= NULL
;
1896 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
1897 ret
= raw_open_common(bs
, options
, flags
, O_NONBLOCK
, &local_err
);
1898 if (error_is_set(&local_err
)) {
1899 error_propagate(errp
, local_err
);
1904 static int cdrom_probe_device(const char *filename
)
1910 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
);
1914 ret
= fstat(fd
, &st
);
1915 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
1919 /* Attempt to detect via a CDROM specific ioctl */
1920 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1930 static int cdrom_is_inserted(BlockDriverState
*bs
)
1932 BDRVRawState
*s
= bs
->opaque
;
1935 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
1936 if (ret
== CDS_DISC_OK
)
1941 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
1943 BDRVRawState
*s
= bs
->opaque
;
1946 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
1947 perror("CDROMEJECT");
1949 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
1950 perror("CDROMEJECT");
1954 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
1956 BDRVRawState
*s
= bs
->opaque
;
1958 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
1960 * Note: an error can happen if the distribution automatically
1963 /* perror("CDROM_LOCKDOOR"); */
1967 static BlockDriver bdrv_host_cdrom
= {
1968 .format_name
= "host_cdrom",
1969 .protocol_name
= "host_cdrom",
1970 .instance_size
= sizeof(BDRVRawState
),
1971 .bdrv_needs_filename
= true,
1972 .bdrv_probe_device
= cdrom_probe_device
,
1973 .bdrv_file_open
= cdrom_open
,
1974 .bdrv_close
= raw_close
,
1975 .bdrv_reopen_prepare
= raw_reopen_prepare
,
1976 .bdrv_reopen_commit
= raw_reopen_commit
,
1977 .bdrv_reopen_abort
= raw_reopen_abort
,
1978 .bdrv_create
= hdev_create
,
1979 .create_options
= raw_create_options
,
1981 .bdrv_aio_readv
= raw_aio_readv
,
1982 .bdrv_aio_writev
= raw_aio_writev
,
1983 .bdrv_aio_flush
= raw_aio_flush
,
1985 .bdrv_truncate
= raw_truncate
,
1986 .bdrv_getlength
= raw_getlength
,
1987 .has_variable_length
= true,
1988 .bdrv_get_allocated_file_size
1989 = raw_get_allocated_file_size
,
1991 /* removable device support */
1992 .bdrv_is_inserted
= cdrom_is_inserted
,
1993 .bdrv_eject
= cdrom_eject
,
1994 .bdrv_lock_medium
= cdrom_lock_medium
,
1996 /* generic scsi device */
1997 .bdrv_ioctl
= hdev_ioctl
,
1998 .bdrv_aio_ioctl
= hdev_aio_ioctl
,
2000 #endif /* __linux__ */
2002 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2003 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2006 BDRVRawState
*s
= bs
->opaque
;
2007 Error
*local_err
= NULL
;
2012 ret
= raw_open_common(bs
, options
, flags
, 0, &local_err
);
2014 if (error_is_set(&local_err
)) {
2015 error_propagate(errp
, local_err
);
2020 /* make sure the door isn't locked at this time */
2021 ioctl(s
->fd
, CDIOCALLOW
);
2025 static int cdrom_probe_device(const char *filename
)
2027 if (strstart(filename
, "/dev/cd", NULL
) ||
2028 strstart(filename
, "/dev/acd", NULL
))
2033 static int cdrom_reopen(BlockDriverState
*bs
)
2035 BDRVRawState
*s
= bs
->opaque
;
2039 * Force reread of possibly changed/newly loaded disc,
2040 * FreeBSD seems to not notice sometimes...
2044 fd
= qemu_open(bs
->filename
, s
->open_flags
, 0644);
2051 /* make sure the door isn't locked at this time */
2052 ioctl(s
->fd
, CDIOCALLOW
);
2056 static int cdrom_is_inserted(BlockDriverState
*bs
)
2058 return raw_getlength(bs
) > 0;
2061 static void cdrom_eject(BlockDriverState
*bs
, bool eject_flag
)
2063 BDRVRawState
*s
= bs
->opaque
;
2068 (void) ioctl(s
->fd
, CDIOCALLOW
);
2071 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
2072 perror("CDIOCEJECT");
2074 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
2075 perror("CDIOCCLOSE");
2081 static void cdrom_lock_medium(BlockDriverState
*bs
, bool locked
)
2083 BDRVRawState
*s
= bs
->opaque
;
2087 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
2089 * Note: an error can happen if the distribution automatically
2092 /* perror("CDROM_LOCKDOOR"); */
2096 static BlockDriver bdrv_host_cdrom
= {
2097 .format_name
= "host_cdrom",
2098 .protocol_name
= "host_cdrom",
2099 .instance_size
= sizeof(BDRVRawState
),
2100 .bdrv_needs_filename
= true,
2101 .bdrv_probe_device
= cdrom_probe_device
,
2102 .bdrv_file_open
= cdrom_open
,
2103 .bdrv_close
= raw_close
,
2104 .bdrv_reopen_prepare
= raw_reopen_prepare
,
2105 .bdrv_reopen_commit
= raw_reopen_commit
,
2106 .bdrv_reopen_abort
= raw_reopen_abort
,
2107 .bdrv_create
= hdev_create
,
2108 .create_options
= raw_create_options
,
2110 .bdrv_aio_readv
= raw_aio_readv
,
2111 .bdrv_aio_writev
= raw_aio_writev
,
2112 .bdrv_aio_flush
= raw_aio_flush
,
2114 .bdrv_truncate
= raw_truncate
,
2115 .bdrv_getlength
= raw_getlength
,
2116 .has_variable_length
= true,
2117 .bdrv_get_allocated_file_size
2118 = raw_get_allocated_file_size
,
2120 /* removable device support */
2121 .bdrv_is_inserted
= cdrom_is_inserted
,
2122 .bdrv_eject
= cdrom_eject
,
2123 .bdrv_lock_medium
= cdrom_lock_medium
,
2125 #endif /* __FreeBSD__ */
2127 #ifdef CONFIG_LINUX_AIO
2129 * Return the file descriptor for Linux AIO
2131 * This function is a layering violation and should be removed when it becomes
2132 * possible to call the block layer outside the global mutex. It allows the
2133 * caller to hijack the file descriptor so I/O can be performed outside the
2136 int raw_get_aio_fd(BlockDriverState
*bs
)
2144 if (bs
->drv
== bdrv_find_format("raw")) {
2148 /* raw-posix has several protocols so just check for raw_aio_readv */
2149 if (bs
->drv
->bdrv_aio_readv
!= raw_aio_readv
) {
2159 #endif /* CONFIG_LINUX_AIO */
2161 static void bdrv_file_init(void)
2164 * Register all the drivers. Note that order is important, the driver
2165 * registered last will get probed first.
2167 bdrv_register(&bdrv_file
);
2168 bdrv_register(&bdrv_host_device
);
2170 bdrv_register(&bdrv_host_floppy
);
2171 bdrv_register(&bdrv_host_cdrom
);
2173 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2174 bdrv_register(&bdrv_host_cdrom
);
2178 block_init(bdrv_file_init
);