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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/cutils.h"
28 #include "qemu/error-report.h"
29 #include "block/block-io.h"
30 #include "block/block_int.h"
31 #include "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qemu/units.h"
34 #include "qemu/memalign.h"
36 #include "block/thread-pool.h"
38 #include "block/raw-aio.h"
39 #include "qapi/qmp/qdict.h"
40 #include "qapi/qmp/qstring.h"
42 #include "scsi/pr-manager.h"
43 #include "scsi/constants.h"
45 #if defined(__APPLE__) && (__MACH__)
46 #include <sys/ioctl.h>
47 #if defined(HAVE_HOST_BLOCK_DEVICE)
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <IOKit/IOKitLib.h>
52 #include <IOKit/IOBSD.h>
53 #include <IOKit/storage/IOMediaBSDClient.h>
54 #include <IOKit/storage/IOMedia.h>
55 #include <IOKit/storage/IOCDMedia.h>
56 //#include <IOKit/storage/IOCDTypes.h>
57 #include <IOKit/storage/IODVDMedia.h>
58 #include <CoreFoundation/CoreFoundation.h>
59 #endif /* defined(HAVE_HOST_BLOCK_DEVICE) */
63 #define _POSIX_PTHREAD_SEMANTICS 1
67 #include <sys/ioctl.h>
68 #include <sys/param.h>
69 #include <sys/syscall.h>
71 #if defined(CONFIG_BLKZONED)
72 #include <linux/blkzoned.h>
74 #include <linux/cdrom.h>
77 #include <linux/hdreg.h>
78 #include <linux/magic.h>
84 #define FS_NOCOW_FL 0x00800000 /* Do not cow file */
87 #if defined(CONFIG_FALLOCATE_PUNCH_HOLE) || defined(CONFIG_FALLOCATE_ZERO_RANGE)
88 #include <linux/falloc.h>
90 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
96 #include <sys/ioctl.h>
97 #include <sys/disklabel.h>
102 #include <sys/ioctl.h>
103 #include <sys/disklabel.h>
104 #include <sys/dkio.h>
105 #include <sys/disk.h>
109 #include <sys/ioctl.h>
110 #include <sys/diskslice.h>
113 /* OS X does not have O_DSYNC */
116 #define O_DSYNC O_SYNC
117 #elif defined(O_FSYNC)
118 #define O_DSYNC O_FSYNC
122 /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
124 #define O_DIRECT O_DSYNC
130 #define MAX_BLOCKSIZE 4096
132 /* Posix file locking bytes. Libvirt takes byte 0, we start from higher bytes,
133 * leaving a few more bytes for its future use. */
134 #define RAW_LOCK_PERM_BASE 100
135 #define RAW_LOCK_SHARED_BASE 200
137 typedef struct BDRVRawState
{
144 /* The current permissions. */
146 uint64_t shared_perm
;
148 /* The perms bits whose corresponding bytes are already locked in
150 uint64_t locked_perm
;
151 uint64_t locked_shared_perm
;
153 uint64_t aio_max_batch
;
156 int perm_change_flags
;
157 BDRVReopenState
*reopen_state
;
160 bool has_write_zeroes
:1;
161 bool use_linux_aio
:1;
162 bool use_linux_io_uring
:1;
163 int page_cache_inconsistent
; /* errno from fdatasync failure */
165 bool needs_alignment
;
166 bool force_alignment
;
168 bool check_cache_dropped
;
170 uint64_t discard_nb_ok
;
171 uint64_t discard_nb_failed
;
172 uint64_t discard_bytes_ok
;
178 typedef struct BDRVRawReopenState
{
181 bool check_cache_dropped
;
182 } BDRVRawReopenState
;
184 static int fd_open(BlockDriverState
*bs
)
186 BDRVRawState
*s
= bs
->opaque
;
188 /* this is just to ensure s->fd is sane (its called by io ops) */
195 static int64_t raw_getlength(BlockDriverState
*bs
);
197 typedef struct RawPosixAIOData
{
198 BlockDriverState
*bs
;
219 PreallocMode prealloc
;
223 unsigned int *nr_zones
;
224 BlockZoneDescriptor
*zones
;
232 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
233 static int cdrom_reopen(BlockDriverState
*bs
);
237 * Elide EAGAIN and EACCES details when failing to lock, as this
238 * indicates that the specified file region is already locked by
239 * another process, which is considered a common scenario.
241 #define raw_lock_error_setg_errno(errp, err, fmt, ...) \
243 if ((err) == EAGAIN || (err) == EACCES) { \
244 error_setg((errp), (fmt), ## __VA_ARGS__); \
246 error_setg_errno((errp), (err), (fmt), ## __VA_ARGS__); \
250 #if defined(__NetBSD__)
251 static int raw_normalize_devicepath(const char **filename
, Error
**errp
)
253 static char namebuf
[PATH_MAX
];
254 const char *dp
, *fname
;
258 dp
= strrchr(fname
, '/');
259 if (lstat(fname
, &sb
) < 0) {
260 error_setg_file_open(errp
, errno
, fname
);
264 if (!S_ISBLK(sb
.st_mode
)) {
269 snprintf(namebuf
, PATH_MAX
, "r%s", fname
);
271 snprintf(namebuf
, PATH_MAX
, "%.*s/r%s",
272 (int)(dp
- fname
), fname
, dp
+ 1);
275 warn_report("%s is a block device, using %s", fname
, *filename
);
280 static int raw_normalize_devicepath(const char **filename
, Error
**errp
)
287 * Get logical block size via ioctl. On success store it in @sector_size_p.
289 static int probe_logical_blocksize(int fd
, unsigned int *sector_size_p
)
291 unsigned int sector_size
;
292 bool success
= false;
296 static const unsigned long ioctl_list
[] = {
300 #ifdef DKIOCGETBLOCKSIZE
303 #ifdef DIOCGSECTORSIZE
308 /* Try a few ioctls to get the right size */
309 for (i
= 0; i
< (int)ARRAY_SIZE(ioctl_list
); i
++) {
310 if (ioctl(fd
, ioctl_list
[i
], §or_size
) >= 0) {
311 *sector_size_p
= sector_size
;
316 return success
? 0 : -errno
;
320 * Get physical block size of @fd.
321 * On success, store it in @blk_size and return 0.
322 * On failure, return -errno.
324 static int probe_physical_blocksize(int fd
, unsigned int *blk_size
)
327 if (ioctl(fd
, BLKPBSZGET
, blk_size
) < 0) {
337 * Returns true if no alignment restrictions are necessary even for files
338 * opened with O_DIRECT.
340 * raw_probe_alignment() probes the required alignment and assume that 1 means
341 * the probing failed, so it falls back to a safe default of 4k. This can be
342 * avoided if we know that byte alignment is okay for the file.
344 static bool dio_byte_aligned(int fd
)
350 ret
= fstatfs(fd
, &buf
);
351 if (ret
== 0 && buf
.f_type
== NFS_SUPER_MAGIC
) {
358 static bool raw_needs_alignment(BlockDriverState
*bs
)
360 BDRVRawState
*s
= bs
->opaque
;
362 if ((bs
->open_flags
& BDRV_O_NOCACHE
) != 0 && !dio_byte_aligned(s
->fd
)) {
366 return s
->force_alignment
;
369 /* Check if read is allowed with given memory buffer and length.
371 * This function is used to check O_DIRECT memory buffer and request alignment.
373 static bool raw_is_io_aligned(int fd
, void *buf
, size_t len
)
375 ssize_t ret
= pread(fd
, buf
, len
, 0);
382 /* The Linux kernel returns EINVAL for misaligned O_DIRECT reads. Ignore
383 * other errors (e.g. real I/O error), which could happen on a failed
384 * drive, since we only care about probing alignment.
386 if (errno
!= EINVAL
) {
394 static void raw_probe_alignment(BlockDriverState
*bs
, int fd
, Error
**errp
)
396 BDRVRawState
*s
= bs
->opaque
;
398 size_t max_align
= MAX(MAX_BLOCKSIZE
, qemu_real_host_page_size());
399 size_t alignments
[] = {1, 512, 1024, 2048, 4096};
401 /* For SCSI generic devices the alignment is not really used.
402 With buffered I/O, we don't have any restrictions. */
403 if (bdrv_is_sg(bs
) || !s
->needs_alignment
) {
404 bs
->bl
.request_alignment
= 1;
409 bs
->bl
.request_alignment
= 0;
411 /* Let's try to use the logical blocksize for the alignment. */
412 if (probe_logical_blocksize(fd
, &bs
->bl
.request_alignment
) < 0) {
413 bs
->bl
.request_alignment
= 0;
418 * The XFS ioctl definitions are shipped in extra packages that might
419 * not always be available. Since we just need the XFS_IOC_DIOINFO ioctl
420 * here, we simply use our own definition instead:
427 if (ioctl(fd
, _IOR('X', 30, struct xfs_dioattr
), &da
) >= 0) {
428 bs
->bl
.request_alignment
= da
.d_miniosz
;
429 /* The kernel returns wrong information for d_mem */
430 /* s->buf_align = da.d_mem; */
435 * If we could not get the sizes so far, we can only guess them. First try
436 * to detect request alignment, since it is more likely to succeed. Then
437 * try to detect buf_align, which cannot be detected in some cases (e.g.
438 * Gluster). If buf_align cannot be detected, we fallback to the value of
442 if (!bs
->bl
.request_alignment
) {
445 buf
= qemu_memalign(max_align
, max_align
);
446 for (i
= 0; i
< ARRAY_SIZE(alignments
); i
++) {
447 align
= alignments
[i
];
448 if (raw_is_io_aligned(fd
, buf
, align
)) {
449 /* Fallback to safe value. */
450 bs
->bl
.request_alignment
= (align
!= 1) ? align
: max_align
;
460 buf
= qemu_memalign(max_align
, 2 * max_align
);
461 for (i
= 0; i
< ARRAY_SIZE(alignments
); i
++) {
462 align
= alignments
[i
];
463 if (raw_is_io_aligned(fd
, buf
+ align
, max_align
)) {
464 /* Fallback to request_alignment. */
465 s
->buf_align
= (align
!= 1) ? align
: bs
->bl
.request_alignment
;
472 if (!s
->buf_align
|| !bs
->bl
.request_alignment
) {
473 error_setg(errp
, "Could not find working O_DIRECT alignment");
474 error_append_hint(errp
, "Try cache.direct=off\n");
478 static int check_hdev_writable(int fd
)
480 #if defined(BLKROGET)
481 /* Linux block devices can be configured "read-only" using blockdev(8).
482 * This is independent of device node permissions and therefore open(2)
483 * with O_RDWR succeeds. Actual writes fail with EPERM.
485 * bdrv_open() is supposed to fail if the disk is read-only. Explicitly
486 * check for read-only block devices so that Linux block devices behave
492 if (fstat(fd
, &st
)) {
496 if (!S_ISBLK(st
.st_mode
)) {
500 if (ioctl(fd
, BLKROGET
, &readonly
) < 0) {
507 #endif /* defined(BLKROGET) */
511 static void raw_parse_flags(int bdrv_flags
, int *open_flags
, bool has_writers
)
513 bool read_write
= false;
514 assert(open_flags
!= NULL
);
516 *open_flags
|= O_BINARY
;
517 *open_flags
&= ~O_ACCMODE
;
519 if (bdrv_flags
& BDRV_O_AUTO_RDONLY
) {
520 read_write
= has_writers
;
521 } else if (bdrv_flags
& BDRV_O_RDWR
) {
526 *open_flags
|= O_RDWR
;
528 *open_flags
|= O_RDONLY
;
531 /* Use O_DSYNC for write-through caching, no flags for write-back caching,
532 * and O_DIRECT for no caching. */
533 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
534 *open_flags
|= O_DIRECT
;
538 static void raw_parse_filename(const char *filename
, QDict
*options
,
541 bdrv_parse_filename_strip_prefix(filename
, "file:", options
);
544 static QemuOptsList raw_runtime_opts
= {
546 .head
= QTAILQ_HEAD_INITIALIZER(raw_runtime_opts
.head
),
550 .type
= QEMU_OPT_STRING
,
551 .help
= "File name of the image",
555 .type
= QEMU_OPT_STRING
,
556 .help
= "host AIO implementation (threads, native, io_uring)",
559 .name
= "aio-max-batch",
560 .type
= QEMU_OPT_NUMBER
,
561 .help
= "AIO max batch size (0 = auto handled by AIO backend, default: 0)",
565 .type
= QEMU_OPT_STRING
,
566 .help
= "file locking mode (on/off/auto, default: auto)",
569 .name
= "pr-manager",
570 .type
= QEMU_OPT_STRING
,
571 .help
= "id of persistent reservation manager object (default: none)",
573 #if defined(__linux__)
575 .name
= "drop-cache",
576 .type
= QEMU_OPT_BOOL
,
577 .help
= "invalidate page cache during live migration (default: on)",
581 .name
= "x-check-cache-dropped",
582 .type
= QEMU_OPT_BOOL
,
583 .help
= "check that page cache was dropped on live migration (default: off)"
585 { /* end of list */ }
589 static const char *const mutable_opts
[] = { "x-check-cache-dropped", NULL
};
591 static int raw_open_common(BlockDriverState
*bs
, QDict
*options
,
592 int bdrv_flags
, int open_flags
,
593 bool device
, Error
**errp
)
595 BDRVRawState
*s
= bs
->opaque
;
597 Error
*local_err
= NULL
;
598 const char *filename
= NULL
;
600 BlockdevAioOptions aio
, aio_default
;
605 opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0, &error_abort
);
606 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
611 filename
= qemu_opt_get(opts
, "filename");
613 ret
= raw_normalize_devicepath(&filename
, errp
);
618 if (bdrv_flags
& BDRV_O_NATIVE_AIO
) {
619 aio_default
= BLOCKDEV_AIO_OPTIONS_NATIVE
;
620 #ifdef CONFIG_LINUX_IO_URING
621 } else if (bdrv_flags
& BDRV_O_IO_URING
) {
622 aio_default
= BLOCKDEV_AIO_OPTIONS_IO_URING
;
625 aio_default
= BLOCKDEV_AIO_OPTIONS_THREADS
;
628 aio
= qapi_enum_parse(&BlockdevAioOptions_lookup
,
629 qemu_opt_get(opts
, "aio"),
630 aio_default
, &local_err
);
632 error_propagate(errp
, local_err
);
637 s
->use_linux_aio
= (aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
);
638 #ifdef CONFIG_LINUX_IO_URING
639 s
->use_linux_io_uring
= (aio
== BLOCKDEV_AIO_OPTIONS_IO_URING
);
642 s
->aio_max_batch
= qemu_opt_get_number(opts
, "aio-max-batch", 0);
644 locking
= qapi_enum_parse(&OnOffAuto_lookup
,
645 qemu_opt_get(opts
, "locking"),
646 ON_OFF_AUTO_AUTO
, &local_err
);
648 error_propagate(errp
, local_err
);
655 if (!qemu_has_ofd_lock()) {
656 warn_report("File lock requested but OFD locking syscall is "
657 "unavailable, falling back to POSIX file locks");
658 error_printf("Due to the implementation, locks can be lost "
662 case ON_OFF_AUTO_OFF
:
665 case ON_OFF_AUTO_AUTO
:
666 s
->use_lock
= qemu_has_ofd_lock();
672 str
= qemu_opt_get(opts
, "pr-manager");
674 s
->pr_mgr
= pr_manager_lookup(str
, &local_err
);
676 error_propagate(errp
, local_err
);
682 s
->drop_cache
= qemu_opt_get_bool(opts
, "drop-cache", true);
683 s
->check_cache_dropped
= qemu_opt_get_bool(opts
, "x-check-cache-dropped",
686 s
->open_flags
= open_flags
;
687 raw_parse_flags(bdrv_flags
, &s
->open_flags
, false);
690 fd
= qemu_open(filename
, s
->open_flags
, errp
);
691 ret
= fd
< 0 ? -errno
: 0;
701 /* Check s->open_flags rather than bdrv_flags due to auto-read-only */
702 if (s
->open_flags
& O_RDWR
) {
703 ret
= check_hdev_writable(s
->fd
);
705 error_setg_errno(errp
, -ret
, "The device is not writable");
711 s
->shared_perm
= BLK_PERM_ALL
;
713 #ifdef CONFIG_LINUX_AIO
714 /* Currently Linux does AIO only for files opened with O_DIRECT */
715 if (s
->use_linux_aio
) {
716 if (!(s
->open_flags
& O_DIRECT
)) {
717 error_setg(errp
, "aio=native was specified, but it requires "
718 "cache.direct=on, which was not specified.");
722 if (!aio_setup_linux_aio(bdrv_get_aio_context(bs
), errp
)) {
723 error_prepend(errp
, "Unable to use native AIO: ");
728 if (s
->use_linux_aio
) {
729 error_setg(errp
, "aio=native was specified, but is not supported "
734 #endif /* !defined(CONFIG_LINUX_AIO) */
736 #ifdef CONFIG_LINUX_IO_URING
737 if (s
->use_linux_io_uring
) {
738 if (!aio_setup_linux_io_uring(bdrv_get_aio_context(bs
), errp
)) {
739 error_prepend(errp
, "Unable to use io_uring: ");
744 if (s
->use_linux_io_uring
) {
745 error_setg(errp
, "aio=io_uring was specified, but is not supported "
750 #endif /* !defined(CONFIG_LINUX_IO_URING) */
752 s
->has_discard
= true;
753 s
->has_write_zeroes
= true;
755 if (fstat(s
->fd
, &st
) < 0) {
757 error_setg_errno(errp
, errno
, "Could not stat file");
762 if (!S_ISREG(st
.st_mode
)) {
763 error_setg(errp
, "'%s' driver requires '%s' to be a regular file",
764 bs
->drv
->format_name
, bs
->filename
);
768 s
->has_fallocate
= true;
771 if (!(S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
772 error_setg(errp
, "'%s' driver requires '%s' to be either "
773 "a character or block device",
774 bs
->drv
->format_name
, bs
->filename
);
779 #ifdef CONFIG_BLKZONED
781 * The kernel page cache does not reliably work for writes to SWR zones
782 * of zoned block device because it can not guarantee the order of writes.
784 if ((bs
->bl
.zoned
!= BLK_Z_NONE
) &&
785 (!(s
->open_flags
& O_DIRECT
))) {
786 error_setg(errp
, "The driver supports zoned devices, and it requires "
787 "cache.direct=on, which was not specified.");
788 return -EINVAL
; /* No host kernel page cache */
792 if (S_ISBLK(st
.st_mode
)) {
794 /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache. Do
795 * not rely on the contents of discarded blocks unless using O_DIRECT.
796 * Same for BLKZEROOUT.
798 if (!(bs
->open_flags
& BDRV_O_NOCACHE
)) {
799 s
->has_write_zeroes
= false;
804 if (S_ISCHR(st
.st_mode
)) {
806 * The file is a char device (disk), which on FreeBSD isn't behind
807 * a pager, so force all requests to be aligned. This is needed
808 * so QEMU makes sure all IO operations on the device are aligned
809 * to sector size, or else FreeBSD will reject them with EINVAL.
811 s
->force_alignment
= true;
814 s
->needs_alignment
= raw_needs_alignment(bs
);
816 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
;
817 if (S_ISREG(st
.st_mode
)) {
818 /* When extending regular files, we get zeros from the OS */
819 bs
->supported_truncate_flags
= BDRV_REQ_ZERO_WRITE
;
823 if (ret
< 0 && s
->fd
!= -1) {
826 if (filename
&& (bdrv_flags
& BDRV_O_TEMPORARY
)) {
833 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
836 BDRVRawState
*s
= bs
->opaque
;
838 s
->type
= FTYPE_FILE
;
839 return raw_open_common(bs
, options
, flags
, 0, false, errp
);
848 #define PERM_FOREACH(i) \
849 for ((i) = 0; (1ULL << (i)) <= BLK_PERM_ALL; i++)
851 /* Lock bytes indicated by @perm_lock_bits and @shared_perm_lock_bits in the
852 * file; if @unlock == true, also unlock the unneeded bytes.
853 * @shared_perm_lock_bits is the mask of all permissions that are NOT shared.
855 static int raw_apply_lock_bytes(BDRVRawState
*s
, int fd
,
856 uint64_t perm_lock_bits
,
857 uint64_t shared_perm_lock_bits
,
858 bool unlock
, Error
**errp
)
862 uint64_t locked_perm
, locked_shared_perm
;
865 locked_perm
= s
->locked_perm
;
866 locked_shared_perm
= s
->locked_shared_perm
;
869 * We don't have the previous bits, just lock/unlock for each of the
873 locked_perm
= BLK_PERM_ALL
;
874 locked_shared_perm
= BLK_PERM_ALL
;
877 locked_shared_perm
= 0;
882 int off
= RAW_LOCK_PERM_BASE
+ i
;
883 uint64_t bit
= (1ULL << i
);
884 if ((perm_lock_bits
& bit
) && !(locked_perm
& bit
)) {
885 ret
= qemu_lock_fd(fd
, off
, 1, false);
887 raw_lock_error_setg_errno(errp
, -ret
, "Failed to lock byte %d",
891 s
->locked_perm
|= bit
;
893 } else if (unlock
&& (locked_perm
& bit
) && !(perm_lock_bits
& bit
)) {
894 ret
= qemu_unlock_fd(fd
, off
, 1);
896 error_setg_errno(errp
, -ret
, "Failed to unlock byte %d", off
);
899 s
->locked_perm
&= ~bit
;
904 int off
= RAW_LOCK_SHARED_BASE
+ i
;
905 uint64_t bit
= (1ULL << i
);
906 if ((shared_perm_lock_bits
& bit
) && !(locked_shared_perm
& bit
)) {
907 ret
= qemu_lock_fd(fd
, off
, 1, false);
909 raw_lock_error_setg_errno(errp
, -ret
, "Failed to lock byte %d",
913 s
->locked_shared_perm
|= bit
;
915 } else if (unlock
&& (locked_shared_perm
& bit
) &&
916 !(shared_perm_lock_bits
& bit
)) {
917 ret
= qemu_unlock_fd(fd
, off
, 1);
919 error_setg_errno(errp
, -ret
, "Failed to unlock byte %d", off
);
922 s
->locked_shared_perm
&= ~bit
;
929 /* Check "unshared" bytes implied by @perm and ~@shared_perm in the file. */
930 static int raw_check_lock_bytes(int fd
, uint64_t perm
, uint64_t shared_perm
,
937 int off
= RAW_LOCK_SHARED_BASE
+ i
;
938 uint64_t p
= 1ULL << i
;
940 ret
= qemu_lock_fd_test(fd
, off
, 1, true);
942 char *perm_name
= bdrv_perm_names(p
);
944 raw_lock_error_setg_errno(errp
, -ret
,
945 "Failed to get \"%s\" lock",
953 int off
= RAW_LOCK_PERM_BASE
+ i
;
954 uint64_t p
= 1ULL << i
;
955 if (!(shared_perm
& p
)) {
956 ret
= qemu_lock_fd_test(fd
, off
, 1, true);
958 char *perm_name
= bdrv_perm_names(p
);
960 raw_lock_error_setg_errno(errp
, -ret
,
961 "Failed to get shared \"%s\" lock",
971 static int raw_handle_perm_lock(BlockDriverState
*bs
,
973 uint64_t new_perm
, uint64_t new_shared
,
976 BDRVRawState
*s
= bs
->opaque
;
978 Error
*local_err
= NULL
;
984 if (bdrv_get_flags(bs
) & BDRV_O_INACTIVE
) {
990 if ((s
->perm
| new_perm
) == s
->perm
&&
991 (s
->shared_perm
& new_shared
) == s
->shared_perm
)
994 * We are going to unlock bytes, it should not fail. If it fail due
995 * to some fs-dependent permission-unrelated reasons (which occurs
996 * sometimes on NFS and leads to abort in bdrv_replace_child) we
997 * can't prevent such errors by any check here. And we ignore them
998 * anyway in ABORT and COMMIT.
1002 ret
= raw_apply_lock_bytes(s
, s
->fd
, s
->perm
| new_perm
,
1003 ~s
->shared_perm
| ~new_shared
,
1006 ret
= raw_check_lock_bytes(s
->fd
, new_perm
, new_shared
, errp
);
1010 error_append_hint(errp
,
1011 "Is another process using the image [%s]?\n",
1014 /* fall through to unlock bytes. */
1016 raw_apply_lock_bytes(s
, s
->fd
, s
->perm
, ~s
->shared_perm
,
1019 /* Theoretically the above call only unlocks bytes and it cannot
1020 * fail. Something weird happened, report it.
1022 warn_report_err(local_err
);
1026 raw_apply_lock_bytes(s
, s
->fd
, new_perm
, ~new_shared
,
1029 /* Theoretically the above call only unlocks bytes and it cannot
1030 * fail. Something weird happened, report it.
1032 warn_report_err(local_err
);
1039 /* Sets a specific flag */
1040 static int fcntl_setfl(int fd
, int flag
)
1044 flags
= fcntl(fd
, F_GETFL
);
1048 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1) {
1054 static int raw_reconfigure_getfd(BlockDriverState
*bs
, int flags
,
1055 int *open_flags
, uint64_t perm
, bool force_dup
,
1058 BDRVRawState
*s
= bs
->opaque
;
1061 bool has_writers
= perm
&
1062 (BLK_PERM_WRITE
| BLK_PERM_WRITE_UNCHANGED
| BLK_PERM_RESIZE
);
1063 int fcntl_flags
= O_APPEND
| O_NONBLOCK
;
1065 fcntl_flags
|= O_NOATIME
;
1069 if (s
->type
== FTYPE_CD
) {
1070 *open_flags
|= O_NONBLOCK
;
1073 raw_parse_flags(flags
, open_flags
, has_writers
);
1076 /* Not all operating systems have O_ASYNC, and those that don't
1077 * will not let us track the state into rs->open_flags (typically
1078 * you achieve the same effect with an ioctl, for example I_SETSIG
1079 * on Solaris). But we do not use O_ASYNC, so that's fine.
1081 assert((s
->open_flags
& O_ASYNC
) == 0);
1084 if (!force_dup
&& *open_flags
== s
->open_flags
) {
1085 /* We're lucky, the existing fd is fine */
1089 if ((*open_flags
& ~fcntl_flags
) == (s
->open_flags
& ~fcntl_flags
)) {
1090 /* dup the original fd */
1091 fd
= qemu_dup(s
->fd
);
1093 ret
= fcntl_setfl(fd
, *open_flags
);
1101 /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */
1103 const char *normalized_filename
= bs
->filename
;
1104 ret
= raw_normalize_devicepath(&normalized_filename
, errp
);
1106 fd
= qemu_open(normalized_filename
, *open_flags
, errp
);
1113 if (fd
!= -1 && (*open_flags
& O_RDWR
)) {
1114 ret
= check_hdev_writable(fd
);
1117 error_setg_errno(errp
, -ret
, "The device is not writable");
1125 static int raw_reopen_prepare(BDRVReopenState
*state
,
1126 BlockReopenQueue
*queue
, Error
**errp
)
1129 BDRVRawReopenState
*rs
;
1133 assert(state
!= NULL
);
1134 assert(state
->bs
!= NULL
);
1136 s
= state
->bs
->opaque
;
1138 state
->opaque
= g_new0(BDRVRawReopenState
, 1);
1141 /* Handle options changes */
1142 opts
= qemu_opts_create(&raw_runtime_opts
, NULL
, 0, &error_abort
);
1143 if (!qemu_opts_absorb_qdict(opts
, state
->options
, errp
)) {
1148 rs
->drop_cache
= qemu_opt_get_bool_del(opts
, "drop-cache", true);
1149 rs
->check_cache_dropped
=
1150 qemu_opt_get_bool_del(opts
, "x-check-cache-dropped", false);
1152 /* This driver's reopen function doesn't currently allow changing
1153 * other options, so let's put them back in the original QDict and
1154 * bdrv_reopen_prepare() will detect changes and complain. */
1155 qemu_opts_to_qdict(opts
, state
->options
);
1158 * As part of reopen prepare we also want to create new fd by
1159 * raw_reconfigure_getfd(). But it wants updated "perm", when in
1160 * bdrv_reopen_multiple() .bdrv_reopen_prepare() callback called prior to
1161 * permission update. Happily, permission update is always a part
1162 * (a separate stage) of bdrv_reopen_multiple() so we can rely on this
1163 * fact and reconfigure fd in raw_check_perm().
1166 s
->reopen_state
= state
;
1170 qemu_opts_del(opts
);
1174 static void raw_reopen_commit(BDRVReopenState
*state
)
1176 BDRVRawReopenState
*rs
= state
->opaque
;
1177 BDRVRawState
*s
= state
->bs
->opaque
;
1179 s
->drop_cache
= rs
->drop_cache
;
1180 s
->check_cache_dropped
= rs
->check_cache_dropped
;
1181 s
->open_flags
= rs
->open_flags
;
1182 g_free(state
->opaque
);
1183 state
->opaque
= NULL
;
1185 assert(s
->reopen_state
== state
);
1186 s
->reopen_state
= NULL
;
1190 static void raw_reopen_abort(BDRVReopenState
*state
)
1192 BDRVRawReopenState
*rs
= state
->opaque
;
1193 BDRVRawState
*s
= state
->bs
->opaque
;
1195 /* nothing to do if NULL, we didn't get far enough */
1200 g_free(state
->opaque
);
1201 state
->opaque
= NULL
;
1203 assert(s
->reopen_state
== state
);
1204 s
->reopen_state
= NULL
;
1207 static int hdev_get_max_hw_transfer(int fd
, struct stat
*st
)
1210 if (S_ISBLK(st
->st_mode
)) {
1211 unsigned short max_sectors
= 0;
1212 if (ioctl(fd
, BLKSECTGET
, &max_sectors
) == 0) {
1213 return max_sectors
* 512;
1217 if (ioctl(fd
, BLKSECTGET
, &max_bytes
) == 0) {
1228 * Get a sysfs attribute value as character string.
1231 static int get_sysfs_str_val(struct stat
*st
, const char *attribute
,
1233 g_autofree
char *sysfspath
= NULL
;
1236 if (!S_ISBLK(st
->st_mode
)) {
1240 sysfspath
= g_strdup_printf("/sys/dev/block/%u:%u/queue/%s",
1241 major(st
->st_rdev
), minor(st
->st_rdev
),
1243 if (!g_file_get_contents(sysfspath
, val
, &len
, NULL
)) {
1247 /* The file is ended with '\n' */
1250 if (*(p
+ len
- 1) == '\n') {
1251 *(p
+ len
- 1) = '\0';
1257 #if defined(CONFIG_BLKZONED)
1258 static int get_sysfs_zoned_model(struct stat
*st
, BlockZoneModel
*zoned
)
1260 g_autofree
char *val
= NULL
;
1263 ret
= get_sysfs_str_val(st
, "zoned", &val
);
1268 if (strcmp(val
, "host-managed") == 0) {
1270 } else if (strcmp(val
, "host-aware") == 0) {
1272 } else if (strcmp(val
, "none") == 0) {
1273 *zoned
= BLK_Z_NONE
;
1279 #endif /* defined(CONFIG_BLKZONED) */
1282 * Get a sysfs attribute value as a long integer.
1285 static long get_sysfs_long_val(struct stat
*st
, const char *attribute
)
1287 g_autofree
char *str
= NULL
;
1292 ret
= get_sysfs_str_val(st
, attribute
, &str
);
1297 /* The file is ended with '\n', pass 'end' to accept that. */
1298 ret
= qemu_strtol(str
, &end
, 10, &val
);
1299 if (ret
== 0 && end
&& *end
== '\0') {
1306 static int hdev_get_max_segments(int fd
, struct stat
*st
)
1311 if (S_ISCHR(st
->st_mode
)) {
1312 if (ioctl(fd
, SG_GET_SG_TABLESIZE
, &ret
) == 0) {
1317 return get_sysfs_long_val(st
, "max_segments");
1323 #if defined(CONFIG_BLKZONED)
1325 * If the reset_all flag is true, then the wps of zone whose state is
1326 * not readonly or offline should be all reset to the start sector.
1327 * Else, take the real wp of the device.
1329 static int get_zones_wp(BlockDriverState
*bs
, int fd
, int64_t offset
,
1330 unsigned int nrz
, bool reset_all
)
1332 struct blk_zone
*blkz
;
1334 uint64_t sector
= offset
>> BDRV_SECTOR_BITS
;
1335 BlockZoneWps
*wps
= bs
->wps
;
1336 unsigned int j
= offset
/ bs
->bl
.zone_size
;
1337 unsigned int n
= 0, i
= 0;
1339 rep_size
= sizeof(struct blk_zone_report
) + nrz
* sizeof(struct blk_zone
);
1340 g_autofree
struct blk_zone_report
*rep
= NULL
;
1342 rep
= g_malloc(rep_size
);
1343 blkz
= (struct blk_zone
*)(rep
+ 1);
1345 memset(rep
, 0, rep_size
);
1346 rep
->sector
= sector
;
1347 rep
->nr_zones
= nrz
- n
;
1350 ret
= ioctl(fd
, BLKREPORTZONE
, rep
);
1351 } while (ret
!= 0 && errno
== EINTR
);
1353 error_report("%d: ioctl BLKREPORTZONE at %" PRId64
" failed %d",
1358 if (!rep
->nr_zones
) {
1362 for (i
= 0; i
< rep
->nr_zones
; ++i
, ++n
, ++j
) {
1364 * The wp tracking cares only about sequential writes required and
1365 * sequential write preferred zones so that the wp can advance to
1366 * the right location.
1367 * Use the most significant bit of the wp location to indicate the
1368 * zone type: 0 for SWR/SWP zones and 1 for conventional zones.
1370 if (blkz
[i
].type
== BLK_ZONE_TYPE_CONVENTIONAL
) {
1371 wps
->wp
[j
] |= 1ULL << 63;
1373 switch(blkz
[i
].cond
) {
1374 case BLK_ZONE_COND_FULL
:
1375 case BLK_ZONE_COND_READONLY
:
1376 /* Zone not writable */
1377 wps
->wp
[j
] = (blkz
[i
].start
+ blkz
[i
].len
) << BDRV_SECTOR_BITS
;
1379 case BLK_ZONE_COND_OFFLINE
:
1380 /* Zone not writable nor readable */
1381 wps
->wp
[j
] = (blkz
[i
].start
) << BDRV_SECTOR_BITS
;
1385 wps
->wp
[j
] = blkz
[i
].start
<< BDRV_SECTOR_BITS
;
1387 wps
->wp
[j
] = blkz
[i
].wp
<< BDRV_SECTOR_BITS
;
1393 sector
= blkz
[i
- 1].start
+ blkz
[i
- 1].len
;
1399 static void update_zones_wp(BlockDriverState
*bs
, int fd
, int64_t offset
,
1402 if (get_zones_wp(bs
, fd
, offset
, nrz
, 0) < 0) {
1403 error_report("update zone wp failed");
1407 static void raw_refresh_zoned_limits(BlockDriverState
*bs
, struct stat
*st
,
1410 BDRVRawState
*s
= bs
->opaque
;
1411 BlockZoneModel zoned
;
1414 ret
= get_sysfs_zoned_model(st
, &zoned
);
1415 if (ret
< 0 || zoned
== BLK_Z_NONE
) {
1418 bs
->bl
.zoned
= zoned
;
1420 ret
= get_sysfs_long_val(st
, "max_open_zones");
1422 bs
->bl
.max_open_zones
= ret
;
1425 ret
= get_sysfs_long_val(st
, "max_active_zones");
1427 bs
->bl
.max_active_zones
= ret
;
1431 * The zoned device must at least have zone size and nr_zones fields.
1433 ret
= get_sysfs_long_val(st
, "chunk_sectors");
1435 error_setg_errno(errp
, -ret
, "Unable to read chunk_sectors "
1439 error_setg(errp
, "Read 0 from chunk_sectors sysfs attribute");
1442 bs
->bl
.zone_size
= ret
<< BDRV_SECTOR_BITS
;
1444 ret
= get_sysfs_long_val(st
, "nr_zones");
1446 error_setg_errno(errp
, -ret
, "Unable to read nr_zones "
1450 error_setg(errp
, "Read 0 from nr_zones sysfs attribute");
1453 bs
->bl
.nr_zones
= ret
;
1455 ret
= get_sysfs_long_val(st
, "zone_append_max_bytes");
1457 bs
->bl
.max_append_sectors
= ret
>> BDRV_SECTOR_BITS
;
1460 ret
= get_sysfs_long_val(st
, "physical_block_size");
1462 bs
->bl
.write_granularity
= ret
;
1465 /* The refresh_limits() function can be called multiple times. */
1467 bs
->wps
= g_malloc(sizeof(BlockZoneWps
) +
1468 sizeof(int64_t) * bs
->bl
.nr_zones
);
1469 ret
= get_zones_wp(bs
, s
->fd
, 0, bs
->bl
.nr_zones
, 0);
1471 error_setg_errno(errp
, -ret
, "report wps failed");
1474 qemu_co_mutex_init(&bs
->wps
->colock
);
1478 bs
->bl
.zoned
= BLK_Z_NONE
;
1482 #else /* !defined(CONFIG_BLKZONED) */
1483 static void raw_refresh_zoned_limits(BlockDriverState
*bs
, struct stat
*st
,
1486 bs
->bl
.zoned
= BLK_Z_NONE
;
1488 #endif /* !defined(CONFIG_BLKZONED) */
1490 static void raw_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1492 BDRVRawState
*s
= bs
->opaque
;
1495 s
->needs_alignment
= raw_needs_alignment(bs
);
1496 raw_probe_alignment(bs
, s
->fd
, errp
);
1498 bs
->bl
.min_mem_alignment
= s
->buf_align
;
1499 bs
->bl
.opt_mem_alignment
= MAX(s
->buf_align
, qemu_real_host_page_size());
1502 * Maximum transfers are best effort, so it is okay to ignore any
1503 * errors. That said, based on the man page errors in fstat would be
1504 * very much unexpected; the only possible case seems to be ENOMEM.
1506 if (fstat(s
->fd
, &st
)) {
1510 #if defined(__APPLE__) && (__MACH__)
1513 if (!fstatfs(s
->fd
, &buf
)) {
1514 bs
->bl
.opt_transfer
= buf
.f_iosize
;
1515 bs
->bl
.pdiscard_alignment
= buf
.f_bsize
;
1519 if (bdrv_is_sg(bs
) || S_ISBLK(st
.st_mode
)) {
1520 int ret
= hdev_get_max_hw_transfer(s
->fd
, &st
);
1522 if (ret
> 0 && ret
<= BDRV_REQUEST_MAX_BYTES
) {
1523 bs
->bl
.max_hw_transfer
= ret
;
1526 ret
= hdev_get_max_segments(s
->fd
, &st
);
1528 bs
->bl
.max_hw_iov
= ret
;
1532 raw_refresh_zoned_limits(bs
, &st
, errp
);
1535 static int check_for_dasd(int fd
)
1538 struct dasd_information2_t info
= {0};
1540 return ioctl(fd
, BIODASDINFO2
, &info
);
1547 * Try to get @bs's logical and physical block size.
1548 * On success, store them in @bsz and return zero.
1549 * On failure, return negative errno.
1551 static int hdev_probe_blocksizes(BlockDriverState
*bs
, BlockSizes
*bsz
)
1553 BDRVRawState
*s
= bs
->opaque
;
1556 /* If DASD or zoned devices, get blocksizes */
1557 if (check_for_dasd(s
->fd
) < 0) {
1558 /* zoned devices are not DASD */
1559 if (bs
->bl
.zoned
== BLK_Z_NONE
) {
1563 ret
= probe_logical_blocksize(s
->fd
, &bsz
->log
);
1567 return probe_physical_blocksize(s
->fd
, &bsz
->phys
);
1571 * Try to get @bs's geometry: cyls, heads, sectors.
1572 * On success, store them in @geo and return 0.
1573 * On failure return -errno.
1574 * (Allows block driver to assign default geometry values that guest sees)
1577 static int hdev_probe_geometry(BlockDriverState
*bs
, HDGeometry
*geo
)
1579 BDRVRawState
*s
= bs
->opaque
;
1580 struct hd_geometry ioctl_geo
= {0};
1582 /* If DASD, get its geometry */
1583 if (check_for_dasd(s
->fd
) < 0) {
1586 if (ioctl(s
->fd
, HDIO_GETGEO
, &ioctl_geo
) < 0) {
1589 /* HDIO_GETGEO may return success even though geo contains zeros
1590 (e.g. certain multipath setups) */
1591 if (!ioctl_geo
.heads
|| !ioctl_geo
.sectors
|| !ioctl_geo
.cylinders
) {
1594 /* Do not return a geometry for partition */
1595 if (ioctl_geo
.start
!= 0) {
1598 geo
->heads
= ioctl_geo
.heads
;
1599 geo
->sectors
= ioctl_geo
.sectors
;
1600 geo
->cylinders
= ioctl_geo
.cylinders
;
1604 #else /* __linux__ */
1605 static int hdev_probe_geometry(BlockDriverState
*bs
, HDGeometry
*geo
)
1611 #if defined(__linux__)
1612 static int handle_aiocb_ioctl(void *opaque
)
1614 RawPosixAIOData
*aiocb
= opaque
;
1617 ret
= RETRY_ON_EINTR(
1618 ioctl(aiocb
->aio_fildes
, aiocb
->ioctl
.cmd
, aiocb
->ioctl
.buf
)
1628 static int handle_aiocb_flush(void *opaque
)
1630 RawPosixAIOData
*aiocb
= opaque
;
1631 BDRVRawState
*s
= aiocb
->bs
->opaque
;
1634 if (s
->page_cache_inconsistent
) {
1635 return -s
->page_cache_inconsistent
;
1638 ret
= qemu_fdatasync(aiocb
->aio_fildes
);
1640 trace_file_flush_fdatasync_failed(errno
);
1642 /* There is no clear definition of the semantics of a failing fsync(),
1643 * so we may have to assume the worst. The sad truth is that this
1644 * assumption is correct for Linux. Some pages are now probably marked
1645 * clean in the page cache even though they are inconsistent with the
1646 * on-disk contents. The next fdatasync() call would succeed, but no
1647 * further writeback attempt will be made. We can't get back to a state
1648 * in which we know what is on disk (we would have to rewrite
1649 * everything that was touched since the last fdatasync() at least), so
1650 * make bdrv_flush() fail permanently. Given that the behaviour isn't
1651 * really defined, I have little hope that other OSes are doing better.
1653 * Obviously, this doesn't affect O_DIRECT, which bypasses the page
1655 if ((s
->open_flags
& O_DIRECT
) == 0) {
1656 s
->page_cache_inconsistent
= errno
;
1663 #ifdef CONFIG_PREADV
1665 static bool preadv_present
= true;
1668 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
1670 return preadv(fd
, iov
, nr_iov
, offset
);
1674 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
1676 return pwritev(fd
, iov
, nr_iov
, offset
);
1681 static bool preadv_present
= false;
1684 qemu_preadv(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
1690 qemu_pwritev(int fd
, const struct iovec
*iov
, int nr_iov
, off_t offset
)
1697 static ssize_t
handle_aiocb_rw_vector(RawPosixAIOData
*aiocb
)
1701 len
= RETRY_ON_EINTR(
1702 (aiocb
->aio_type
& (QEMU_AIO_WRITE
| QEMU_AIO_ZONE_APPEND
)) ?
1703 qemu_pwritev(aiocb
->aio_fildes
,
1706 aiocb
->aio_offset
) :
1707 qemu_preadv(aiocb
->aio_fildes
,
1720 * Read/writes the data to/from a given linear buffer.
1722 * Returns the number of bytes handles or -errno in case of an error. Short
1723 * reads are only returned if the end of the file is reached.
1725 static ssize_t
handle_aiocb_rw_linear(RawPosixAIOData
*aiocb
, char *buf
)
1730 while (offset
< aiocb
->aio_nbytes
) {
1731 if (aiocb
->aio_type
& (QEMU_AIO_WRITE
| QEMU_AIO_ZONE_APPEND
)) {
1732 len
= pwrite(aiocb
->aio_fildes
,
1733 (const char *)buf
+ offset
,
1734 aiocb
->aio_nbytes
- offset
,
1735 aiocb
->aio_offset
+ offset
);
1737 len
= pread(aiocb
->aio_fildes
,
1739 aiocb
->aio_nbytes
- offset
,
1740 aiocb
->aio_offset
+ offset
);
1742 if (len
== -1 && errno
== EINTR
) {
1744 } else if (len
== -1 && errno
== EINVAL
&&
1745 (aiocb
->bs
->open_flags
& BDRV_O_NOCACHE
) &&
1746 !(aiocb
->aio_type
& QEMU_AIO_WRITE
) &&
1748 /* O_DIRECT pread() may fail with EINVAL when offset is unaligned
1749 * after a short read. Assume that O_DIRECT short reads only occur
1750 * at EOF. Therefore this is a short read, not an I/O error.
1753 } else if (len
== -1) {
1756 } else if (len
== 0) {
1765 static int handle_aiocb_rw(void *opaque
)
1767 RawPosixAIOData
*aiocb
= opaque
;
1771 if (!(aiocb
->aio_type
& QEMU_AIO_MISALIGNED
)) {
1773 * If there is just a single buffer, and it is properly aligned
1774 * we can just use plain pread/pwrite without any problems.
1776 if (aiocb
->io
.niov
== 1) {
1777 nbytes
= handle_aiocb_rw_linear(aiocb
, aiocb
->io
.iov
->iov_base
);
1781 * We have more than one iovec, and all are properly aligned.
1783 * Try preadv/pwritev first and fall back to linearizing the
1784 * buffer if it's not supported.
1786 if (preadv_present
) {
1787 nbytes
= handle_aiocb_rw_vector(aiocb
);
1788 if (nbytes
== aiocb
->aio_nbytes
||
1789 (nbytes
< 0 && nbytes
!= -ENOSYS
)) {
1792 preadv_present
= false;
1796 * XXX(hch): short read/write. no easy way to handle the reminder
1797 * using these interfaces. For now retry using plain
1803 * Ok, we have to do it the hard way, copy all segments into
1804 * a single aligned buffer.
1806 buf
= qemu_try_blockalign(aiocb
->bs
, aiocb
->aio_nbytes
);
1812 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
1816 for (i
= 0; i
< aiocb
->io
.niov
; ++i
) {
1817 memcpy(p
, aiocb
->io
.iov
[i
].iov_base
, aiocb
->io
.iov
[i
].iov_len
);
1818 p
+= aiocb
->io
.iov
[i
].iov_len
;
1820 assert(p
- buf
== aiocb
->aio_nbytes
);
1823 nbytes
= handle_aiocb_rw_linear(aiocb
, buf
);
1824 if (!(aiocb
->aio_type
& (QEMU_AIO_WRITE
| QEMU_AIO_ZONE_APPEND
))) {
1826 size_t count
= aiocb
->aio_nbytes
, copy
;
1829 for (i
= 0; i
< aiocb
->io
.niov
&& count
; ++i
) {
1831 if (copy
> aiocb
->io
.iov
[i
].iov_len
) {
1832 copy
= aiocb
->io
.iov
[i
].iov_len
;
1834 memcpy(aiocb
->io
.iov
[i
].iov_base
, p
, copy
);
1835 assert(count
>= copy
);
1844 if (nbytes
== aiocb
->aio_nbytes
) {
1846 } else if (nbytes
>= 0 && nbytes
< aiocb
->aio_nbytes
) {
1847 if (aiocb
->aio_type
& QEMU_AIO_WRITE
) {
1850 iov_memset(aiocb
->io
.iov
, aiocb
->io
.niov
, nbytes
,
1851 0, aiocb
->aio_nbytes
- nbytes
);
1860 #if defined(CONFIG_FALLOCATE) || defined(BLKZEROOUT) || defined(BLKDISCARD)
1861 static int translate_err(int err
)
1863 if (err
== -ENODEV
|| err
== -ENOSYS
|| err
== -EOPNOTSUPP
||
1871 #ifdef CONFIG_FALLOCATE
1872 static int do_fallocate(int fd
, int mode
, off_t offset
, off_t len
)
1875 if (fallocate(fd
, mode
, offset
, len
) == 0) {
1878 } while (errno
== EINTR
);
1879 return translate_err(-errno
);
1883 static ssize_t
handle_aiocb_write_zeroes_block(RawPosixAIOData
*aiocb
)
1886 BDRVRawState
*s
= aiocb
->bs
->opaque
;
1888 if (!s
->has_write_zeroes
) {
1893 /* The BLKZEROOUT implementation in the kernel doesn't set
1894 * BLKDEV_ZERO_NOFALLBACK, so we can't call this if we have to avoid slow
1896 if (!(aiocb
->aio_type
& QEMU_AIO_NO_FALLBACK
)) {
1898 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
1899 if (ioctl(aiocb
->aio_fildes
, BLKZEROOUT
, range
) == 0) {
1902 } while (errno
== EINTR
);
1904 ret
= translate_err(-errno
);
1905 if (ret
== -ENOTSUP
) {
1906 s
->has_write_zeroes
= false;
1914 static int handle_aiocb_write_zeroes(void *opaque
)
1916 RawPosixAIOData
*aiocb
= opaque
;
1917 #ifdef CONFIG_FALLOCATE
1918 BDRVRawState
*s
= aiocb
->bs
->opaque
;
1922 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
1923 return handle_aiocb_write_zeroes_block(aiocb
);
1926 #ifdef CONFIG_FALLOCATE_ZERO_RANGE
1927 if (s
->has_write_zeroes
) {
1928 int ret
= do_fallocate(s
->fd
, FALLOC_FL_ZERO_RANGE
,
1929 aiocb
->aio_offset
, aiocb
->aio_nbytes
);
1930 if (ret
== -ENOTSUP
) {
1931 s
->has_write_zeroes
= false;
1932 } else if (ret
== 0 || ret
!= -EINVAL
) {
1936 * Note: Some file systems do not like unaligned byte ranges, and
1937 * return EINVAL in such a case, though they should not do it according
1938 * to the man-page of fallocate(). Thus we simply ignore this return
1939 * value and try the other fallbacks instead.
1944 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1945 if (s
->has_discard
&& s
->has_fallocate
) {
1946 int ret
= do_fallocate(s
->fd
,
1947 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
1948 aiocb
->aio_offset
, aiocb
->aio_nbytes
);
1950 ret
= do_fallocate(s
->fd
, 0, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
1951 if (ret
== 0 || ret
!= -ENOTSUP
) {
1954 s
->has_fallocate
= false;
1955 } else if (ret
== -EINVAL
) {
1957 * Some file systems like older versions of GPFS do not like un-
1958 * aligned byte ranges, and return EINVAL in such a case, though
1959 * they should not do it according to the man-page of fallocate().
1960 * Warn about the bad filesystem and try the final fallback instead.
1962 warn_report_once("Your file system is misbehaving: "
1963 "fallocate(FALLOC_FL_PUNCH_HOLE) returned EINVAL. "
1964 "Please report this bug to your file system "
1966 } else if (ret
!= -ENOTSUP
) {
1969 s
->has_discard
= false;
1974 #ifdef CONFIG_FALLOCATE
1975 /* Last resort: we are trying to extend the file with zeroed data. This
1976 * can be done via fallocate(fd, 0) */
1977 len
= raw_getlength(aiocb
->bs
);
1978 if (s
->has_fallocate
&& len
>= 0 && aiocb
->aio_offset
>= len
) {
1979 int ret
= do_fallocate(s
->fd
, 0, aiocb
->aio_offset
, aiocb
->aio_nbytes
);
1980 if (ret
== 0 || ret
!= -ENOTSUP
) {
1983 s
->has_fallocate
= false;
1990 static int handle_aiocb_write_zeroes_unmap(void *opaque
)
1992 RawPosixAIOData
*aiocb
= opaque
;
1993 BDRVRawState
*s G_GNUC_UNUSED
= aiocb
->bs
->opaque
;
1995 /* First try to write zeros and unmap at the same time */
1997 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1998 int ret
= do_fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
1999 aiocb
->aio_offset
, aiocb
->aio_nbytes
);
2010 /* If we couldn't manage to unmap while guaranteed that the area reads as
2011 * all-zero afterwards, just write zeroes without unmapping */
2012 return handle_aiocb_write_zeroes(aiocb
);
2015 #ifndef HAVE_COPY_FILE_RANGE
2016 static off_t
copy_file_range(int in_fd
, off_t
*in_off
, int out_fd
,
2017 off_t
*out_off
, size_t len
, unsigned int flags
)
2019 #ifdef __NR_copy_file_range
2020 return syscall(__NR_copy_file_range
, in_fd
, in_off
, out_fd
,
2021 out_off
, len
, flags
);
2030 * parse_zone - Fill a zone descriptor
2032 #if defined(CONFIG_BLKZONED)
2033 static inline int parse_zone(struct BlockZoneDescriptor
*zone
,
2034 const struct blk_zone
*blkz
) {
2035 zone
->start
= blkz
->start
<< BDRV_SECTOR_BITS
;
2036 zone
->length
= blkz
->len
<< BDRV_SECTOR_BITS
;
2037 zone
->wp
= blkz
->wp
<< BDRV_SECTOR_BITS
;
2039 #ifdef HAVE_BLK_ZONE_REP_CAPACITY
2040 zone
->cap
= blkz
->capacity
<< BDRV_SECTOR_BITS
;
2042 zone
->cap
= blkz
->len
<< BDRV_SECTOR_BITS
;
2045 switch (blkz
->type
) {
2046 case BLK_ZONE_TYPE_SEQWRITE_REQ
:
2047 zone
->type
= BLK_ZT_SWR
;
2049 case BLK_ZONE_TYPE_SEQWRITE_PREF
:
2050 zone
->type
= BLK_ZT_SWP
;
2052 case BLK_ZONE_TYPE_CONVENTIONAL
:
2053 zone
->type
= BLK_ZT_CONV
;
2056 error_report("Unsupported zone type: 0x%x", blkz
->type
);
2060 switch (blkz
->cond
) {
2061 case BLK_ZONE_COND_NOT_WP
:
2062 zone
->state
= BLK_ZS_NOT_WP
;
2064 case BLK_ZONE_COND_EMPTY
:
2065 zone
->state
= BLK_ZS_EMPTY
;
2067 case BLK_ZONE_COND_IMP_OPEN
:
2068 zone
->state
= BLK_ZS_IOPEN
;
2070 case BLK_ZONE_COND_EXP_OPEN
:
2071 zone
->state
= BLK_ZS_EOPEN
;
2073 case BLK_ZONE_COND_CLOSED
:
2074 zone
->state
= BLK_ZS_CLOSED
;
2076 case BLK_ZONE_COND_READONLY
:
2077 zone
->state
= BLK_ZS_RDONLY
;
2079 case BLK_ZONE_COND_FULL
:
2080 zone
->state
= BLK_ZS_FULL
;
2082 case BLK_ZONE_COND_OFFLINE
:
2083 zone
->state
= BLK_ZS_OFFLINE
;
2086 error_report("Unsupported zone state: 0x%x", blkz
->cond
);
2093 #if defined(CONFIG_BLKZONED)
2094 static int handle_aiocb_zone_report(void *opaque
)
2096 RawPosixAIOData
*aiocb
= opaque
;
2097 int fd
= aiocb
->aio_fildes
;
2098 unsigned int *nr_zones
= aiocb
->zone_report
.nr_zones
;
2099 BlockZoneDescriptor
*zones
= aiocb
->zone_report
.zones
;
2100 /* zoned block devices use 512-byte sectors */
2101 uint64_t sector
= aiocb
->aio_offset
/ 512;
2103 struct blk_zone
*blkz
;
2107 unsigned int n
= 0, i
= 0;
2110 rep_size
= sizeof(struct blk_zone_report
) + nrz
* sizeof(struct blk_zone
);
2111 g_autofree
struct blk_zone_report
*rep
= NULL
;
2112 rep
= g_malloc(rep_size
);
2114 blkz
= (struct blk_zone
*)(rep
+ 1);
2116 memset(rep
, 0, rep_size
);
2117 rep
->sector
= sector
;
2118 rep
->nr_zones
= nrz
- n
;
2121 ret
= ioctl(fd
, BLKREPORTZONE
, rep
);
2122 } while (ret
!= 0 && errno
== EINTR
);
2124 error_report("%d: ioctl BLKREPORTZONE at %" PRId64
" failed %d",
2129 if (!rep
->nr_zones
) {
2133 for (i
= 0; i
< rep
->nr_zones
; i
++, n
++) {
2134 ret
= parse_zone(&zones
[n
], &blkz
[i
]);
2139 /* The next report should start after the last zone reported */
2140 sector
= blkz
[i
].start
+ blkz
[i
].len
;
2149 #if defined(CONFIG_BLKZONED)
2150 static int handle_aiocb_zone_mgmt(void *opaque
)
2152 RawPosixAIOData
*aiocb
= opaque
;
2153 int fd
= aiocb
->aio_fildes
;
2154 uint64_t sector
= aiocb
->aio_offset
/ 512;
2155 int64_t nr_sectors
= aiocb
->aio_nbytes
/ 512;
2156 struct blk_zone_range range
;
2159 /* Execute the operation */
2160 range
.sector
= sector
;
2161 range
.nr_sectors
= nr_sectors
;
2163 ret
= ioctl(fd
, aiocb
->zone_mgmt
.op
, &range
);
2164 } while (ret
!= 0 && errno
== EINTR
);
2166 return ret
< 0 ? -errno
: ret
;
2170 static int handle_aiocb_copy_range(void *opaque
)
2172 RawPosixAIOData
*aiocb
= opaque
;
2173 uint64_t bytes
= aiocb
->aio_nbytes
;
2174 off_t in_off
= aiocb
->aio_offset
;
2175 off_t out_off
= aiocb
->copy_range
.aio_offset2
;
2178 ssize_t ret
= copy_file_range(aiocb
->aio_fildes
, &in_off
,
2179 aiocb
->copy_range
.aio_fd2
, &out_off
,
2181 trace_file_copy_file_range(aiocb
->bs
, aiocb
->aio_fildes
, in_off
,
2182 aiocb
->copy_range
.aio_fd2
, out_off
, bytes
,
2185 /* No progress (e.g. when beyond EOF), let the caller fall back to
2204 static int handle_aiocb_discard(void *opaque
)
2206 RawPosixAIOData
*aiocb
= opaque
;
2208 BDRVRawState
*s
= aiocb
->bs
->opaque
;
2210 if (!s
->has_discard
) {
2214 if (aiocb
->aio_type
& QEMU_AIO_BLKDEV
) {
2217 uint64_t range
[2] = { aiocb
->aio_offset
, aiocb
->aio_nbytes
};
2218 if (ioctl(aiocb
->aio_fildes
, BLKDISCARD
, range
) == 0) {
2221 } while (errno
== EINTR
);
2223 ret
= translate_err(-errno
);
2226 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
2227 ret
= do_fallocate(s
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
2228 aiocb
->aio_offset
, aiocb
->aio_nbytes
);
2229 ret
= translate_err(ret
);
2230 #elif defined(__APPLE__) && (__MACH__)
2231 fpunchhole_t fpunchhole
;
2232 fpunchhole
.fp_flags
= 0;
2233 fpunchhole
.reserved
= 0;
2234 fpunchhole
.fp_offset
= aiocb
->aio_offset
;
2235 fpunchhole
.fp_length
= aiocb
->aio_nbytes
;
2236 if (fcntl(s
->fd
, F_PUNCHHOLE
, &fpunchhole
) == -1) {
2237 ret
= errno
== ENODEV
? -ENOTSUP
: -errno
;
2244 if (ret
== -ENOTSUP
) {
2245 s
->has_discard
= false;
2251 * Help alignment probing by allocating the first block.
2253 * When reading with direct I/O from unallocated area on Gluster backed by XFS,
2254 * reading succeeds regardless of request length. In this case we fallback to
2255 * safe alignment which is not optimal. Allocating the first block avoids this
2258 * fd may be opened with O_DIRECT, but we don't know the buffer alignment or
2259 * request alignment, so we use safe values.
2261 * Returns: 0 on success, -errno on failure. Since this is an optimization,
2262 * caller may ignore failures.
2264 static int allocate_first_block(int fd
, size_t max_size
)
2266 size_t write_size
= (max_size
< MAX_BLOCKSIZE
)
2269 size_t max_align
= MAX(MAX_BLOCKSIZE
, qemu_real_host_page_size());
2274 buf
= qemu_memalign(max_align
, write_size
);
2275 memset(buf
, 0, write_size
);
2277 n
= RETRY_ON_EINTR(pwrite(fd
, buf
, write_size
, 0));
2279 ret
= (n
== -1) ? -errno
: 0;
2285 static int handle_aiocb_truncate(void *opaque
)
2287 RawPosixAIOData
*aiocb
= opaque
;
2289 int64_t current_length
= 0;
2292 int fd
= aiocb
->aio_fildes
;
2293 int64_t offset
= aiocb
->aio_offset
;
2294 PreallocMode prealloc
= aiocb
->truncate
.prealloc
;
2295 Error
**errp
= aiocb
->truncate
.errp
;
2297 if (fstat(fd
, &st
) < 0) {
2299 error_setg_errno(errp
, -result
, "Could not stat file");
2303 current_length
= st
.st_size
;
2304 if (current_length
> offset
&& prealloc
!= PREALLOC_MODE_OFF
) {
2305 error_setg(errp
, "Cannot use preallocation for shrinking files");
2310 #ifdef CONFIG_POSIX_FALLOCATE
2311 case PREALLOC_MODE_FALLOC
:
2313 * Truncating before posix_fallocate() makes it about twice slower on
2314 * file systems that do not support fallocate(), trying to check if a
2315 * block is allocated before allocating it, so don't do that here.
2317 if (offset
!= current_length
) {
2318 result
= -posix_fallocate(fd
, current_length
,
2319 offset
- current_length
);
2321 /* posix_fallocate() doesn't set errno. */
2322 error_setg_errno(errp
, -result
,
2323 "Could not preallocate new data");
2324 } else if (current_length
== 0) {
2326 * posix_fallocate() uses fallocate() if the filesystem
2327 * supports it, or fallback to manually writing zeroes. If
2328 * fallocate() was used, unaligned reads from the fallocated
2329 * area in raw_probe_alignment() will succeed, hence we need to
2330 * allocate the first block.
2332 * Optimize future alignment probing; ignore failures.
2334 allocate_first_block(fd
, offset
);
2341 case PREALLOC_MODE_FULL
:
2343 int64_t num
= 0, left
= offset
- current_length
;
2347 * Knowing the final size from the beginning could allow the file
2348 * system driver to do less allocations and possibly avoid
2349 * fragmentation of the file.
2351 if (ftruncate(fd
, offset
) != 0) {
2353 error_setg_errno(errp
, -result
, "Could not resize file");
2357 buf
= g_malloc0(65536);
2359 seek_result
= lseek(fd
, current_length
, SEEK_SET
);
2360 if (seek_result
< 0) {
2362 error_setg_errno(errp
, -result
,
2363 "Failed to seek to the old end of file");
2368 num
= MIN(left
, 65536);
2369 result
= write(fd
, buf
, num
);
2371 if (errno
== EINTR
) {
2375 error_setg_errno(errp
, -result
,
2376 "Could not write zeros for preallocation");
2385 error_setg_errno(errp
, -result
,
2386 "Could not flush file to disk");
2392 case PREALLOC_MODE_OFF
:
2393 if (ftruncate(fd
, offset
) != 0) {
2395 error_setg_errno(errp
, -result
, "Could not resize file");
2396 } else if (current_length
== 0 && offset
> current_length
) {
2397 /* Optimize future alignment probing; ignore failures. */
2398 allocate_first_block(fd
, offset
);
2403 error_setg(errp
, "Unsupported preallocation mode: %s",
2404 PreallocMode_str(prealloc
));
2410 if (ftruncate(fd
, current_length
) < 0) {
2411 error_report("Failed to restore old file length: %s",
2420 static int coroutine_fn
raw_thread_pool_submit(ThreadPoolFunc func
, void *arg
)
2422 return thread_pool_submit_co(func
, arg
);
2426 * Check if all memory in this vector is sector aligned.
2428 static bool bdrv_qiov_is_aligned(BlockDriverState
*bs
, QEMUIOVector
*qiov
)
2431 size_t alignment
= bdrv_min_mem_align(bs
);
2432 size_t len
= bs
->bl
.request_alignment
;
2435 for (i
= 0; i
< qiov
->niov
; i
++) {
2436 if ((uintptr_t) qiov
->iov
[i
].iov_base
% alignment
) {
2439 if (qiov
->iov
[i
].iov_len
% len
) {
2447 static int coroutine_fn
raw_co_prw(BlockDriverState
*bs
, int64_t *offset_ptr
,
2448 uint64_t bytes
, QEMUIOVector
*qiov
, int type
)
2450 BDRVRawState
*s
= bs
->opaque
;
2451 RawPosixAIOData acb
;
2453 uint64_t offset
= *offset_ptr
;
2455 if (fd_open(bs
) < 0)
2457 #if defined(CONFIG_BLKZONED)
2458 if ((type
& (QEMU_AIO_WRITE
| QEMU_AIO_ZONE_APPEND
)) &&
2459 bs
->bl
.zoned
!= BLK_Z_NONE
) {
2460 qemu_co_mutex_lock(&bs
->wps
->colock
);
2461 if (type
& QEMU_AIO_ZONE_APPEND
) {
2462 int index
= offset
/ bs
->bl
.zone_size
;
2463 offset
= bs
->wps
->wp
[index
];
2469 * When using O_DIRECT, the request must be aligned to be able to use
2470 * either libaio or io_uring interface. If not fail back to regular thread
2471 * pool read/write code which emulates this for us if we
2472 * set QEMU_AIO_MISALIGNED.
2474 if (s
->needs_alignment
&& !bdrv_qiov_is_aligned(bs
, qiov
)) {
2475 type
|= QEMU_AIO_MISALIGNED
;
2476 #ifdef CONFIG_LINUX_IO_URING
2477 } else if (s
->use_linux_io_uring
) {
2478 assert(qiov
->size
== bytes
);
2479 ret
= luring_co_submit(bs
, s
->fd
, offset
, qiov
, type
);
2482 #ifdef CONFIG_LINUX_AIO
2483 } else if (s
->use_linux_aio
) {
2484 assert(qiov
->size
== bytes
);
2485 ret
= laio_co_submit(s
->fd
, offset
, qiov
, type
,
2491 acb
= (RawPosixAIOData
) {
2493 .aio_fildes
= s
->fd
,
2495 .aio_offset
= offset
,
2496 .aio_nbytes
= bytes
,
2503 assert(qiov
->size
== bytes
);
2504 ret
= raw_thread_pool_submit(handle_aiocb_rw
, &acb
);
2505 goto out
; /* Avoid the compiler err of unused label */
2508 #if defined(CONFIG_BLKZONED)
2509 if ((type
& (QEMU_AIO_WRITE
| QEMU_AIO_ZONE_APPEND
)) &&
2510 bs
->bl
.zoned
!= BLK_Z_NONE
) {
2511 BlockZoneWps
*wps
= bs
->wps
;
2513 uint64_t *wp
= &wps
->wp
[offset
/ bs
->bl
.zone_size
];
2514 if (!BDRV_ZT_IS_CONV(*wp
)) {
2515 if (type
& QEMU_AIO_ZONE_APPEND
) {
2517 trace_zbd_zone_append_complete(bs
, *offset_ptr
2518 >> BDRV_SECTOR_BITS
);
2520 /* Advance the wp if needed */
2521 if (offset
+ bytes
> *wp
) {
2522 *wp
= offset
+ bytes
;
2527 * write and append write are not allowed to cross zone boundaries
2529 update_zones_wp(bs
, s
->fd
, offset
, 1);
2532 qemu_co_mutex_unlock(&wps
->colock
);
2538 static int coroutine_fn
raw_co_preadv(BlockDriverState
*bs
, int64_t offset
,
2539 int64_t bytes
, QEMUIOVector
*qiov
,
2540 BdrvRequestFlags flags
)
2542 return raw_co_prw(bs
, &offset
, bytes
, qiov
, QEMU_AIO_READ
);
2545 static int coroutine_fn
raw_co_pwritev(BlockDriverState
*bs
, int64_t offset
,
2546 int64_t bytes
, QEMUIOVector
*qiov
,
2547 BdrvRequestFlags flags
)
2549 return raw_co_prw(bs
, &offset
, bytes
, qiov
, QEMU_AIO_WRITE
);
2552 static int coroutine_fn
raw_co_flush_to_disk(BlockDriverState
*bs
)
2554 BDRVRawState
*s
= bs
->opaque
;
2555 RawPosixAIOData acb
;
2563 acb
= (RawPosixAIOData
) {
2565 .aio_fildes
= s
->fd
,
2566 .aio_type
= QEMU_AIO_FLUSH
,
2569 #ifdef CONFIG_LINUX_IO_URING
2570 if (s
->use_linux_io_uring
) {
2571 return luring_co_submit(bs
, s
->fd
, 0, NULL
, QEMU_AIO_FLUSH
);
2574 return raw_thread_pool_submit(handle_aiocb_flush
, &acb
);
2577 static void raw_aio_attach_aio_context(BlockDriverState
*bs
,
2578 AioContext
*new_context
)
2580 BDRVRawState
__attribute__((unused
)) *s
= bs
->opaque
;
2581 #ifdef CONFIG_LINUX_AIO
2582 if (s
->use_linux_aio
) {
2583 Error
*local_err
= NULL
;
2584 if (!aio_setup_linux_aio(new_context
, &local_err
)) {
2585 error_reportf_err(local_err
, "Unable to use native AIO, "
2586 "falling back to thread pool: ");
2587 s
->use_linux_aio
= false;
2591 #ifdef CONFIG_LINUX_IO_URING
2592 if (s
->use_linux_io_uring
) {
2593 Error
*local_err
= NULL
;
2594 if (!aio_setup_linux_io_uring(new_context
, &local_err
)) {
2595 error_reportf_err(local_err
, "Unable to use linux io_uring, "
2596 "falling back to thread pool: ");
2597 s
->use_linux_io_uring
= false;
2603 static void raw_close(BlockDriverState
*bs
)
2605 BDRVRawState
*s
= bs
->opaque
;
2608 #if defined(CONFIG_BLKZONED)
2617 * Truncates the given regular file @fd to @offset and, when growing, fills the
2618 * new space according to @prealloc.
2620 * Returns: 0 on success, -errno on failure.
2622 static int coroutine_fn
2623 raw_regular_truncate(BlockDriverState
*bs
, int fd
, int64_t offset
,
2624 PreallocMode prealloc
, Error
**errp
)
2626 RawPosixAIOData acb
;
2628 acb
= (RawPosixAIOData
) {
2631 .aio_type
= QEMU_AIO_TRUNCATE
,
2632 .aio_offset
= offset
,
2634 .prealloc
= prealloc
,
2639 return raw_thread_pool_submit(handle_aiocb_truncate
, &acb
);
2642 static int coroutine_fn
raw_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2643 bool exact
, PreallocMode prealloc
,
2644 BdrvRequestFlags flags
, Error
**errp
)
2646 BDRVRawState
*s
= bs
->opaque
;
2650 if (fstat(s
->fd
, &st
)) {
2652 error_setg_errno(errp
, -ret
, "Failed to fstat() the file");
2656 if (S_ISREG(st
.st_mode
)) {
2657 /* Always resizes to the exact @offset */
2658 return raw_regular_truncate(bs
, s
->fd
, offset
, prealloc
, errp
);
2661 if (prealloc
!= PREALLOC_MODE_OFF
) {
2662 error_setg(errp
, "Preallocation mode '%s' unsupported for this "
2663 "non-regular file", PreallocMode_str(prealloc
));
2667 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
2668 int64_t cur_length
= raw_getlength(bs
);
2670 if (offset
!= cur_length
&& exact
) {
2671 error_setg(errp
, "Cannot resize device files");
2673 } else if (offset
> cur_length
) {
2674 error_setg(errp
, "Cannot grow device files");
2678 error_setg(errp
, "Resizing this file is not supported");
2686 static int64_t raw_getlength(BlockDriverState
*bs
)
2688 BDRVRawState
*s
= bs
->opaque
;
2694 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
2695 struct disklabel dl
;
2697 if (ioctl(fd
, DIOCGDINFO
, &dl
))
2699 return (uint64_t)dl
.d_secsize
*
2700 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
2704 #elif defined(__NetBSD__)
2705 static int64_t raw_getlength(BlockDriverState
*bs
)
2707 BDRVRawState
*s
= bs
->opaque
;
2713 if (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
)) {
2714 struct dkwedge_info dkw
;
2716 if (ioctl(fd
, DIOCGWEDGEINFO
, &dkw
) != -1) {
2717 return dkw
.dkw_size
* 512;
2719 struct disklabel dl
;
2721 if (ioctl(fd
, DIOCGDINFO
, &dl
))
2723 return (uint64_t)dl
.d_secsize
*
2724 dl
.d_partitions
[DISKPART(st
.st_rdev
)].p_size
;
2729 #elif defined(__sun__)
2730 static int64_t raw_getlength(BlockDriverState
*bs
)
2732 BDRVRawState
*s
= bs
->opaque
;
2733 struct dk_minfo minfo
;
2743 * Use the DKIOCGMEDIAINFO ioctl to read the size.
2745 ret
= ioctl(s
->fd
, DKIOCGMEDIAINFO
, &minfo
);
2747 return minfo
.dki_lbsize
* minfo
.dki_capacity
;
2751 * There are reports that lseek on some devices fails, but
2752 * irc discussion said that contingency on contingency was overkill.
2754 size
= lseek(s
->fd
, 0, SEEK_END
);
2760 #elif defined(CONFIG_BSD)
2761 static int64_t raw_getlength(BlockDriverState
*bs
)
2763 BDRVRawState
*s
= bs
->opaque
;
2767 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2776 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
2779 if (!fstat(fd
, &sb
) && (S_IFCHR
& sb
.st_mode
)) {
2781 #ifdef DIOCGMEDIASIZE
2782 if (ioctl(fd
, DIOCGMEDIASIZE
, (off_t
*)&size
)) {
2789 if (ioctl(fd
, DIOCGPART
, &pi
) == 0) {
2790 size
= pi
.media_size
;
2794 #if defined(DKIOCGETBLOCKCOUNT) && defined(DKIOCGETBLOCKSIZE)
2796 uint64_t sectors
= 0;
2797 uint32_t sector_size
= 0;
2799 if (ioctl(fd
, DKIOCGETBLOCKCOUNT
, §ors
) == 0
2800 && ioctl(fd
, DKIOCGETBLOCKSIZE
, §or_size
) == 0) {
2801 size
= sectors
* sector_size
;
2806 size
= lseek(fd
, 0LL, SEEK_END
);
2811 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
2814 /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
2815 if (size
== 2048LL * (unsigned)-1)
2817 /* XXX no disc? maybe we need to reopen... */
2818 if (size
<= 0 && !reopened
&& cdrom_reopen(bs
) >= 0) {
2825 size
= lseek(fd
, 0, SEEK_END
);
2833 static int64_t raw_getlength(BlockDriverState
*bs
)
2835 BDRVRawState
*s
= bs
->opaque
;
2844 size
= lseek(s
->fd
, 0, SEEK_END
);
2852 static int64_t coroutine_fn
raw_co_getlength(BlockDriverState
*bs
)
2854 return raw_getlength(bs
);
2857 static int64_t coroutine_fn
raw_co_get_allocated_file_size(BlockDriverState
*bs
)
2860 BDRVRawState
*s
= bs
->opaque
;
2862 if (fstat(s
->fd
, &st
) < 0) {
2865 return (int64_t)st
.st_blocks
* 512;
2868 static int coroutine_fn
2869 raw_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
2871 BlockdevCreateOptionsFile
*file_opts
;
2872 Error
*local_err
= NULL
;
2874 uint64_t perm
, shared
;
2877 /* Validate options and set default values */
2878 assert(options
->driver
== BLOCKDEV_DRIVER_FILE
);
2879 file_opts
= &options
->u
.file
;
2881 if (!file_opts
->has_nocow
) {
2882 file_opts
->nocow
= false;
2884 if (!file_opts
->has_preallocation
) {
2885 file_opts
->preallocation
= PREALLOC_MODE_OFF
;
2887 if (!file_opts
->has_extent_size_hint
) {
2888 file_opts
->extent_size_hint
= 1 * MiB
;
2890 if (file_opts
->extent_size_hint
> UINT32_MAX
) {
2892 error_setg(errp
, "Extent size hint is too large");
2897 fd
= qemu_create(file_opts
->filename
, O_RDWR
| O_BINARY
, 0644, errp
);
2903 /* Take permissions: We want to discard everything, so we need
2904 * BLK_PERM_WRITE; and truncation to the desired size requires
2906 * On the other hand, we cannot share the RESIZE permission
2907 * because we promise that after this function, the file has the
2908 * size given in the options. If someone else were to resize it
2909 * concurrently, we could not guarantee that.
2910 * Note that after this function, we can no longer guarantee that
2911 * the file is not touched by a third party, so it may be resized
2913 perm
= BLK_PERM_WRITE
| BLK_PERM_RESIZE
;
2914 shared
= BLK_PERM_ALL
& ~BLK_PERM_RESIZE
;
2916 /* Step one: Take locks */
2917 result
= raw_apply_lock_bytes(NULL
, fd
, perm
, ~shared
, false, errp
);
2922 /* Step two: Check that nobody else has taken conflicting locks */
2923 result
= raw_check_lock_bytes(fd
, perm
, shared
, errp
);
2925 error_append_hint(errp
,
2926 "Is another process using the image [%s]?\n",
2927 file_opts
->filename
);
2931 /* Clear the file by truncating it to 0 */
2932 result
= raw_regular_truncate(NULL
, fd
, 0, PREALLOC_MODE_OFF
, errp
);
2937 if (file_opts
->nocow
) {
2939 /* Set NOCOW flag to solve performance issue on fs like btrfs.
2940 * This is an optimisation. The FS_IOC_SETFLAGS ioctl return value
2941 * will be ignored since any failure of this operation should not
2942 * block the left work.
2945 if (ioctl(fd
, FS_IOC_GETFLAGS
, &attr
) == 0) {
2946 attr
|= FS_NOCOW_FL
;
2947 ioctl(fd
, FS_IOC_SETFLAGS
, &attr
);
2951 #ifdef FS_IOC_FSSETXATTR
2953 * Try to set the extent size hint. Failure is not fatal, and a warning is
2954 * only printed if the option was explicitly specified.
2957 struct fsxattr attr
;
2958 result
= ioctl(fd
, FS_IOC_FSGETXATTR
, &attr
);
2960 attr
.fsx_xflags
|= FS_XFLAG_EXTSIZE
;
2961 attr
.fsx_extsize
= file_opts
->extent_size_hint
;
2962 result
= ioctl(fd
, FS_IOC_FSSETXATTR
, &attr
);
2964 if (result
< 0 && file_opts
->has_extent_size_hint
&&
2965 file_opts
->extent_size_hint
)
2967 warn_report("Failed to set extent size hint: %s",
2973 /* Resize and potentially preallocate the file to the desired
2975 result
= raw_regular_truncate(NULL
, fd
, file_opts
->size
,
2976 file_opts
->preallocation
, errp
);
2982 raw_apply_lock_bytes(NULL
, fd
, 0, 0, true, &local_err
);
2984 /* The above call should not fail, and if it does, that does
2985 * not mean the whole creation operation has failed. So
2986 * report it the user for their convenience, but do not report
2987 * it to the caller. */
2988 warn_report_err(local_err
);
2992 if (qemu_close(fd
) != 0 && result
== 0) {
2994 error_setg_errno(errp
, -result
, "Could not close the new file");
3000 static int coroutine_fn GRAPH_RDLOCK
3001 raw_co_create_opts(BlockDriver
*drv
, const char *filename
,
3002 QemuOpts
*opts
, Error
**errp
)
3004 BlockdevCreateOptions options
;
3005 int64_t total_size
= 0;
3006 int64_t extent_size_hint
= 0;
3007 bool has_extent_size_hint
= false;
3009 PreallocMode prealloc
;
3011 Error
*local_err
= NULL
;
3013 /* Skip file: protocol prefix */
3014 strstart(filename
, "file:", &filename
);
3016 /* Read out options */
3017 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
3019 if (qemu_opt_get(opts
, BLOCK_OPT_EXTENT_SIZE_HINT
)) {
3020 has_extent_size_hint
= true;
3022 qemu_opt_get_size_del(opts
, BLOCK_OPT_EXTENT_SIZE_HINT
, -1);
3024 nocow
= qemu_opt_get_bool(opts
, BLOCK_OPT_NOCOW
, false);
3025 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
3026 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, buf
,
3027 PREALLOC_MODE_OFF
, &local_err
);
3030 error_propagate(errp
, local_err
);
3034 options
= (BlockdevCreateOptions
) {
3035 .driver
= BLOCKDEV_DRIVER_FILE
,
3037 .filename
= (char *) filename
,
3039 .has_preallocation
= true,
3040 .preallocation
= prealloc
,
3043 .has_extent_size_hint
= has_extent_size_hint
,
3044 .extent_size_hint
= extent_size_hint
,
3047 return raw_co_create(&options
, errp
);
3050 static int coroutine_fn
raw_co_delete_file(BlockDriverState
*bs
,
3056 if (!(stat(bs
->filename
, &st
) == 0) || !S_ISREG(st
.st_mode
)) {
3057 error_setg_errno(errp
, ENOENT
, "%s is not a regular file",
3062 ret
= unlink(bs
->filename
);
3065 error_setg_errno(errp
, -ret
, "Error when deleting file %s",
3073 * Find allocation range in @bs around offset @start.
3074 * May change underlying file descriptor's file offset.
3075 * If @start is not in a hole, store @start in @data, and the
3076 * beginning of the next hole in @hole, and return 0.
3077 * If @start is in a non-trailing hole, store @start in @hole and the
3078 * beginning of the next non-hole in @data, and return 0.
3079 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
3080 * If we can't find out, return a negative errno other than -ENXIO.
3082 static int find_allocation(BlockDriverState
*bs
, off_t start
,
3083 off_t
*data
, off_t
*hole
)
3085 #if defined SEEK_HOLE && defined SEEK_DATA
3086 BDRVRawState
*s
= bs
->opaque
;
3091 * D1. offs == start: start is in data
3092 * D2. offs > start: start is in a hole, next data at offs
3093 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
3094 * or start is beyond EOF
3095 * If the latter happens, the file has been truncated behind
3096 * our back since we opened it. All bets are off then.
3097 * Treating like a trailing hole is simplest.
3098 * D4. offs < 0, errno != ENXIO: we learned nothing
3100 offs
= lseek(s
->fd
, start
, SEEK_DATA
);
3102 return -errno
; /* D3 or D4 */
3106 /* This is not a valid return by lseek(). We are safe to just return
3107 * -EIO in this case, and we'll treat it like D4. */
3112 /* D2: in hole, next data at offs */
3118 /* D1: in data, end not yet known */
3122 * H1. offs == start: start is in a hole
3123 * If this happens here, a hole has been dug behind our back
3124 * since the previous lseek().
3125 * H2. offs > start: either start is in data, next hole at offs,
3126 * or start is in trailing hole, EOF at offs
3127 * Linux treats trailing holes like any other hole: offs ==
3128 * start. Solaris seeks to EOF instead: offs > start (blech).
3129 * If that happens here, a hole has been dug behind our back
3130 * since the previous lseek().
3131 * H3. offs < 0, errno = ENXIO: start is beyond EOF
3132 * If this happens, the file has been truncated behind our
3133 * back since we opened it. Treat it like a trailing hole.
3134 * H4. offs < 0, errno != ENXIO: we learned nothing
3135 * Pretend we know nothing at all, i.e. "forget" about D1.
3137 offs
= lseek(s
->fd
, start
, SEEK_HOLE
);
3139 return -errno
; /* D1 and (H3 or H4) */
3143 /* This is not a valid return by lseek(). We are safe to just return
3144 * -EIO in this case, and we'll treat it like H4. */
3150 * D1 and H2: either in data, next hole at offs, or it was in
3151 * data but is now in a trailing hole. In the latter case,
3152 * all bets are off. Treating it as if it there was data all
3153 * the way to EOF is safe, so simply do that.
3168 * Returns the allocation status of the specified offset.
3170 * The block layer guarantees 'offset' and 'bytes' are within bounds.
3172 * 'pnum' is set to the number of bytes (including and immediately following
3173 * the specified offset) that are known to be in the same
3174 * allocated/unallocated state.
3176 * 'bytes' is a soft cap for 'pnum'. If the information is free, 'pnum' may
3179 static int coroutine_fn
raw_co_block_status(BlockDriverState
*bs
,
3182 int64_t bytes
, int64_t *pnum
,
3184 BlockDriverState
**file
)
3186 off_t data
= 0, hole
= 0;
3189 assert(QEMU_IS_ALIGNED(offset
| bytes
, bs
->bl
.request_alignment
));
3200 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
3203 ret
= find_allocation(bs
, offset
, &data
, &hole
);
3204 if (ret
== -ENXIO
) {
3207 ret
= BDRV_BLOCK_ZERO
;
3208 } else if (ret
< 0) {
3209 /* No info available, so pretend there are no holes */
3211 ret
= BDRV_BLOCK_DATA
;
3212 } else if (data
== offset
) {
3213 /* On a data extent, compute bytes to the end of the extent,
3214 * possibly including a partial sector at EOF. */
3215 *pnum
= hole
- offset
;
3218 * We are not allowed to return partial sectors, though, so
3219 * round up if necessary.
3221 if (!QEMU_IS_ALIGNED(*pnum
, bs
->bl
.request_alignment
)) {
3222 int64_t file_length
= raw_getlength(bs
);
3223 if (file_length
> 0) {
3224 /* Ignore errors, this is just a safeguard */
3225 assert(hole
== file_length
);
3227 *pnum
= ROUND_UP(*pnum
, bs
->bl
.request_alignment
);
3230 ret
= BDRV_BLOCK_DATA
;
3232 /* On a hole, compute bytes to the beginning of the next extent. */
3233 assert(hole
== offset
);
3234 *pnum
= data
- offset
;
3235 ret
= BDRV_BLOCK_ZERO
;
3239 return ret
| BDRV_BLOCK_OFFSET_VALID
;
3242 #if defined(__linux__)
3243 /* Verify that the file is not in the page cache */
3244 static void check_cache_dropped(BlockDriverState
*bs
, Error
**errp
)
3246 const size_t window_size
= 128 * 1024 * 1024;
3247 BDRVRawState
*s
= bs
->opaque
;
3248 void *window
= NULL
;
3255 /* mincore(2) page status information requires 1 byte per page */
3256 page_size
= sysconf(_SC_PAGESIZE
);
3257 vec
= g_malloc(DIV_ROUND_UP(window_size
, page_size
));
3259 end
= raw_getlength(bs
);
3261 for (offset
= 0; offset
< end
; offset
+= window_size
) {
3268 /* Unmap previous window if size has changed */
3269 new_length
= MIN(end
- offset
, window_size
);
3270 if (new_length
!= length
) {
3271 munmap(window
, length
);
3276 new_window
= mmap(window
, new_length
, PROT_NONE
, MAP_PRIVATE
,
3278 if (new_window
== MAP_FAILED
) {
3279 error_setg_errno(errp
, errno
, "mmap failed");
3283 window
= new_window
;
3284 length
= new_length
;
3286 ret
= mincore(window
, length
, vec
);
3288 error_setg_errno(errp
, errno
, "mincore failed");
3292 vec_end
= DIV_ROUND_UP(length
, page_size
);
3293 for (i
= 0; i
< vec_end
; i
++) {
3299 error_setg(errp
, "page cache still in use!");
3305 munmap(window
, length
);
3310 #endif /* __linux__ */
3312 static void coroutine_fn GRAPH_RDLOCK
3313 raw_co_invalidate_cache(BlockDriverState
*bs
, Error
**errp
)
3315 BDRVRawState
*s
= bs
->opaque
;
3320 error_setg_errno(errp
, -ret
, "The file descriptor is not open");
3324 if (!s
->drop_cache
) {
3328 if (s
->open_flags
& O_DIRECT
) {
3329 return; /* No host kernel page cache */
3332 #if defined(__linux__)
3333 /* This sets the scene for the next syscall... */
3334 ret
= bdrv_co_flush(bs
);
3336 error_setg_errno(errp
, -ret
, "flush failed");
3340 /* Linux does not invalidate pages that are dirty, locked, or mmapped by a
3341 * process. These limitations are okay because we just fsynced the file,
3342 * we don't use mmap, and the file should not be in use by other processes.
3344 ret
= posix_fadvise(s
->fd
, 0, 0, POSIX_FADV_DONTNEED
);
3345 if (ret
!= 0) { /* the return value is a positive errno */
3346 error_setg_errno(errp
, ret
, "fadvise failed");
3350 if (s
->check_cache_dropped
) {
3351 check_cache_dropped(bs
, errp
);
3353 #else /* __linux__ */
3354 /* Do nothing. Live migration to a remote host with cache.direct=off is
3355 * unsupported on other host operating systems. Cache consistency issues
3356 * may occur but no error is reported here, partly because that's the
3357 * historical behavior and partly because it's hard to differentiate valid
3358 * configurations that should not cause errors.
3360 #endif /* !__linux__ */
3363 static void raw_account_discard(BDRVRawState
*s
, uint64_t nbytes
, int ret
)
3366 s
->stats
.discard_nb_failed
++;
3368 s
->stats
.discard_nb_ok
++;
3369 s
->stats
.discard_bytes_ok
+= nbytes
;
3374 * zone report - Get a zone block device's information in the form
3375 * of an array of zone descriptors.
3376 * zones is an array of zone descriptors to hold zone information on reply;
3377 * offset can be any byte within the entire size of the device;
3378 * nr_zones is the maximum number of sectors the command should operate on.
3380 #if defined(CONFIG_BLKZONED)
3381 static int coroutine_fn
raw_co_zone_report(BlockDriverState
*bs
, int64_t offset
,
3382 unsigned int *nr_zones
,
3383 BlockZoneDescriptor
*zones
) {
3384 BDRVRawState
*s
= bs
->opaque
;
3385 RawPosixAIOData acb
= (RawPosixAIOData
) {
3387 .aio_fildes
= s
->fd
,
3388 .aio_type
= QEMU_AIO_ZONE_REPORT
,
3389 .aio_offset
= offset
,
3391 .nr_zones
= nr_zones
,
3396 trace_zbd_zone_report(bs
, *nr_zones
, offset
>> BDRV_SECTOR_BITS
);
3397 return raw_thread_pool_submit(handle_aiocb_zone_report
, &acb
);
3402 * zone management operations - Execute an operation on a zone
3404 #if defined(CONFIG_BLKZONED)
3405 static int coroutine_fn
raw_co_zone_mgmt(BlockDriverState
*bs
, BlockZoneOp op
,
3406 int64_t offset
, int64_t len
) {
3407 BDRVRawState
*s
= bs
->opaque
;
3408 RawPosixAIOData acb
;
3409 int64_t zone_size
, zone_size_mask
;
3410 const char *op_name
;
3413 BlockZoneWps
*wps
= bs
->wps
;
3414 int64_t capacity
= bs
->total_sectors
<< BDRV_SECTOR_BITS
;
3416 zone_size
= bs
->bl
.zone_size
;
3417 zone_size_mask
= zone_size
- 1;
3418 if (offset
& zone_size_mask
) {
3419 error_report("sector offset %" PRId64
" is not aligned to zone size "
3420 "%" PRId64
"", offset
/ 512, zone_size
/ 512);
3424 if (((offset
+ len
) < capacity
&& len
& zone_size_mask
) ||
3425 offset
+ len
> capacity
) {
3426 error_report("number of sectors %" PRId64
" is not aligned to zone size"
3427 " %" PRId64
"", len
/ 512, zone_size
/ 512);
3431 uint32_t i
= offset
/ bs
->bl
.zone_size
;
3432 uint32_t nrz
= len
/ bs
->bl
.zone_size
;
3433 uint64_t *wp
= &wps
->wp
[i
];
3434 if (BDRV_ZT_IS_CONV(*wp
) && len
!= capacity
) {
3435 error_report("zone mgmt operations are not allowed for conventional zones");
3441 op_name
= "BLKOPENZONE";
3445 op_name
= "BLKCLOSEZONE";
3449 op_name
= "BLKFINISHZONE";
3453 op_name
= "BLKRESETZONE";
3457 error_report("Unsupported zone op: 0x%x", op
);
3461 acb
= (RawPosixAIOData
) {
3463 .aio_fildes
= s
->fd
,
3464 .aio_type
= QEMU_AIO_ZONE_MGMT
,
3465 .aio_offset
= offset
,
3472 trace_zbd_zone_mgmt(bs
, op_name
, offset
>> BDRV_SECTOR_BITS
,
3473 len
>> BDRV_SECTOR_BITS
);
3474 ret
= raw_thread_pool_submit(handle_aiocb_zone_mgmt
, &acb
);
3476 update_zones_wp(bs
, s
->fd
, offset
, nrz
);
3477 error_report("ioctl %s failed %d", op_name
, ret
);
3481 if (zo
== BLKRESETZONE
&& len
== capacity
) {
3482 ret
= get_zones_wp(bs
, s
->fd
, 0, bs
->bl
.nr_zones
, 1);
3484 error_report("reporting single wp failed");
3487 } else if (zo
== BLKRESETZONE
) {
3488 for (unsigned int j
= 0; j
< nrz
; ++j
) {
3489 wp
[j
] = offset
+ j
* zone_size
;
3491 } else if (zo
== BLKFINISHZONE
) {
3492 for (unsigned int j
= 0; j
< nrz
; ++j
) {
3493 /* The zoned device allows the last zone smaller that the
3495 wp
[j
] = MIN(offset
+ (j
+ 1) * zone_size
, offset
+ len
);
3503 #if defined(CONFIG_BLKZONED)
3504 static int coroutine_fn
raw_co_zone_append(BlockDriverState
*bs
,
3507 BdrvRequestFlags flags
) {
3509 int64_t zone_size_mask
= bs
->bl
.zone_size
- 1;
3510 int64_t iov_len
= 0;
3513 if (*offset
& zone_size_mask
) {
3514 error_report("sector offset %" PRId64
" is not aligned to zone size "
3515 "%" PRId32
"", *offset
/ 512, bs
->bl
.zone_size
/ 512);
3519 int64_t wg
= bs
->bl
.write_granularity
;
3520 int64_t wg_mask
= wg
- 1;
3521 for (int i
= 0; i
< qiov
->niov
; i
++) {
3522 iov_len
= qiov
->iov
[i
].iov_len
;
3523 if (iov_len
& wg_mask
) {
3524 error_report("len of IOVector[%d] %" PRId64
" is not aligned to "
3525 "block size %" PRId64
"", i
, iov_len
, wg
);
3531 trace_zbd_zone_append(bs
, *offset
>> BDRV_SECTOR_BITS
);
3532 return raw_co_prw(bs
, offset
, len
, qiov
, QEMU_AIO_ZONE_APPEND
);
3536 static coroutine_fn
int
3537 raw_do_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
3540 BDRVRawState
*s
= bs
->opaque
;
3541 RawPosixAIOData acb
;
3544 acb
= (RawPosixAIOData
) {
3546 .aio_fildes
= s
->fd
,
3547 .aio_type
= QEMU_AIO_DISCARD
,
3548 .aio_offset
= offset
,
3549 .aio_nbytes
= bytes
,
3553 acb
.aio_type
|= QEMU_AIO_BLKDEV
;
3556 ret
= raw_thread_pool_submit(handle_aiocb_discard
, &acb
);
3557 raw_account_discard(s
, bytes
, ret
);
3561 static coroutine_fn
int
3562 raw_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
3564 return raw_do_pdiscard(bs
, offset
, bytes
, false);
3567 static int coroutine_fn
3568 raw_do_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
3569 BdrvRequestFlags flags
, bool blkdev
)
3571 BDRVRawState
*s
= bs
->opaque
;
3572 RawPosixAIOData acb
;
3573 ThreadPoolFunc
*handler
;
3575 #ifdef CONFIG_FALLOCATE
3576 if (offset
+ bytes
> bs
->total_sectors
* BDRV_SECTOR_SIZE
) {
3577 BdrvTrackedRequest
*req
;
3580 * This is a workaround for a bug in the Linux XFS driver,
3581 * where writes submitted through the AIO interface will be
3582 * discarded if they happen beyond a concurrently running
3583 * fallocate() that increases the file length (i.e., both the
3584 * write and the fallocate() happen beyond the EOF).
3586 * To work around it, we extend the tracked request for this
3587 * zero write until INT64_MAX (effectively infinity), and mark
3588 * it as serializing.
3590 * We have to enable this workaround for all filesystems and
3591 * AIO modes (not just XFS with aio=native), because for
3592 * remote filesystems we do not know the host configuration.
3595 req
= bdrv_co_get_self_request(bs
);
3597 assert(req
->type
== BDRV_TRACKED_WRITE
);
3598 assert(req
->offset
<= offset
);
3599 assert(req
->offset
+ req
->bytes
>= offset
+ bytes
);
3601 req
->bytes
= BDRV_MAX_LENGTH
- req
->offset
;
3603 bdrv_check_request(req
->offset
, req
->bytes
, &error_abort
);
3605 bdrv_make_request_serialising(req
, bs
->bl
.request_alignment
);
3609 acb
= (RawPosixAIOData
) {
3611 .aio_fildes
= s
->fd
,
3612 .aio_type
= QEMU_AIO_WRITE_ZEROES
,
3613 .aio_offset
= offset
,
3614 .aio_nbytes
= bytes
,
3618 acb
.aio_type
|= QEMU_AIO_BLKDEV
;
3620 if (flags
& BDRV_REQ_NO_FALLBACK
) {
3621 acb
.aio_type
|= QEMU_AIO_NO_FALLBACK
;
3624 if (flags
& BDRV_REQ_MAY_UNMAP
) {
3625 acb
.aio_type
|= QEMU_AIO_DISCARD
;
3626 handler
= handle_aiocb_write_zeroes_unmap
;
3628 handler
= handle_aiocb_write_zeroes
;
3631 return raw_thread_pool_submit(handler
, &acb
);
3634 static int coroutine_fn
raw_co_pwrite_zeroes(
3635 BlockDriverState
*bs
, int64_t offset
,
3636 int64_t bytes
, BdrvRequestFlags flags
)
3638 return raw_do_pwrite_zeroes(bs
, offset
, bytes
, flags
, false);
3641 static int coroutine_fn
3642 raw_co_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
3647 static ImageInfoSpecific
*raw_get_specific_info(BlockDriverState
*bs
,
3650 ImageInfoSpecificFile
*file_info
= g_new0(ImageInfoSpecificFile
, 1);
3651 ImageInfoSpecific
*spec_info
= g_new(ImageInfoSpecific
, 1);
3653 *spec_info
= (ImageInfoSpecific
){
3654 .type
= IMAGE_INFO_SPECIFIC_KIND_FILE
,
3655 .u
.file
.data
= file_info
,
3658 #ifdef FS_IOC_FSGETXATTR
3660 BDRVRawState
*s
= bs
->opaque
;
3661 struct fsxattr attr
;
3664 ret
= ioctl(s
->fd
, FS_IOC_FSGETXATTR
, &attr
);
3665 if (!ret
&& attr
.fsx_extsize
!= 0) {
3666 file_info
->has_extent_size_hint
= true;
3667 file_info
->extent_size_hint
= attr
.fsx_extsize
;
3675 static BlockStatsSpecificFile
get_blockstats_specific_file(BlockDriverState
*bs
)
3677 BDRVRawState
*s
= bs
->opaque
;
3678 return (BlockStatsSpecificFile
) {
3679 .discard_nb_ok
= s
->stats
.discard_nb_ok
,
3680 .discard_nb_failed
= s
->stats
.discard_nb_failed
,
3681 .discard_bytes_ok
= s
->stats
.discard_bytes_ok
,
3685 static BlockStatsSpecific
*raw_get_specific_stats(BlockDriverState
*bs
)
3687 BlockStatsSpecific
*stats
= g_new(BlockStatsSpecific
, 1);
3689 stats
->driver
= BLOCKDEV_DRIVER_FILE
;
3690 stats
->u
.file
= get_blockstats_specific_file(bs
);
3695 #if defined(HAVE_HOST_BLOCK_DEVICE)
3696 static BlockStatsSpecific
*hdev_get_specific_stats(BlockDriverState
*bs
)
3698 BlockStatsSpecific
*stats
= g_new(BlockStatsSpecific
, 1);
3700 stats
->driver
= BLOCKDEV_DRIVER_HOST_DEVICE
;
3701 stats
->u
.host_device
= get_blockstats_specific_file(bs
);
3705 #endif /* HAVE_HOST_BLOCK_DEVICE */
3707 static QemuOptsList raw_create_opts
= {
3708 .name
= "raw-create-opts",
3709 .head
= QTAILQ_HEAD_INITIALIZER(raw_create_opts
.head
),
3712 .name
= BLOCK_OPT_SIZE
,
3713 .type
= QEMU_OPT_SIZE
,
3714 .help
= "Virtual disk size"
3717 .name
= BLOCK_OPT_NOCOW
,
3718 .type
= QEMU_OPT_BOOL
,
3719 .help
= "Turn off copy-on-write (valid only on btrfs)"
3722 .name
= BLOCK_OPT_PREALLOC
,
3723 .type
= QEMU_OPT_STRING
,
3724 .help
= "Preallocation mode (allowed values: off"
3725 #ifdef CONFIG_POSIX_FALLOCATE
3731 .name
= BLOCK_OPT_EXTENT_SIZE_HINT
,
3732 .type
= QEMU_OPT_SIZE
,
3733 .help
= "Extent size hint for the image file, 0 to disable"
3735 { /* end of list */ }
3739 static int raw_check_perm(BlockDriverState
*bs
, uint64_t perm
, uint64_t shared
,
3742 BDRVRawState
*s
= bs
->opaque
;
3743 int input_flags
= s
->reopen_state
? s
->reopen_state
->flags
: bs
->open_flags
;
3747 /* We may need a new fd if auto-read-only switches the mode */
3748 ret
= raw_reconfigure_getfd(bs
, input_flags
, &open_flags
, perm
,
3752 } else if (ret
!= s
->fd
) {
3753 Error
*local_err
= NULL
;
3756 * Fail already check_perm() if we can't get a working O_DIRECT
3757 * alignment with the new fd.
3759 raw_probe_alignment(bs
, ret
, &local_err
);
3761 error_propagate(errp
, local_err
);
3765 s
->perm_change_fd
= ret
;
3766 s
->perm_change_flags
= open_flags
;
3769 /* Prepare permissions on old fd to avoid conflicts between old and new,
3770 * but keep everything locked that new will need. */
3771 ret
= raw_handle_perm_lock(bs
, RAW_PL_PREPARE
, perm
, shared
, errp
);
3776 /* Copy locks to the new fd */
3777 if (s
->perm_change_fd
&& s
->use_lock
) {
3778 ret
= raw_apply_lock_bytes(NULL
, s
->perm_change_fd
, perm
, ~shared
,
3781 raw_handle_perm_lock(bs
, RAW_PL_ABORT
, 0, 0, NULL
);
3788 if (s
->perm_change_fd
) {
3789 qemu_close(s
->perm_change_fd
);
3791 s
->perm_change_fd
= 0;
3795 static void raw_set_perm(BlockDriverState
*bs
, uint64_t perm
, uint64_t shared
)
3797 BDRVRawState
*s
= bs
->opaque
;
3799 /* For reopen, we have already switched to the new fd (.bdrv_set_perm is
3800 * called after .bdrv_reopen_commit) */
3801 if (s
->perm_change_fd
&& s
->fd
!= s
->perm_change_fd
) {
3803 s
->fd
= s
->perm_change_fd
;
3804 s
->open_flags
= s
->perm_change_flags
;
3806 s
->perm_change_fd
= 0;
3808 raw_handle_perm_lock(bs
, RAW_PL_COMMIT
, perm
, shared
, NULL
);
3810 s
->shared_perm
= shared
;
3813 static void raw_abort_perm_update(BlockDriverState
*bs
)
3815 BDRVRawState
*s
= bs
->opaque
;
3817 /* For reopen, .bdrv_reopen_abort is called afterwards and will close
3818 * the file descriptor. */
3819 if (s
->perm_change_fd
) {
3820 qemu_close(s
->perm_change_fd
);
3822 s
->perm_change_fd
= 0;
3824 raw_handle_perm_lock(bs
, RAW_PL_ABORT
, 0, 0, NULL
);
3827 static int coroutine_fn GRAPH_RDLOCK
raw_co_copy_range_from(
3828 BlockDriverState
*bs
, BdrvChild
*src
, int64_t src_offset
,
3829 BdrvChild
*dst
, int64_t dst_offset
, int64_t bytes
,
3830 BdrvRequestFlags read_flags
, BdrvRequestFlags write_flags
)
3832 return bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
3833 read_flags
, write_flags
);
3836 static int coroutine_fn GRAPH_RDLOCK
3837 raw_co_copy_range_to(BlockDriverState
*bs
,
3838 BdrvChild
*src
, int64_t src_offset
,
3839 BdrvChild
*dst
, int64_t dst_offset
,
3840 int64_t bytes
, BdrvRequestFlags read_flags
,
3841 BdrvRequestFlags write_flags
)
3843 RawPosixAIOData acb
;
3844 BDRVRawState
*s
= bs
->opaque
;
3845 BDRVRawState
*src_s
;
3847 assert(dst
->bs
== bs
);
3848 if (src
->bs
->drv
->bdrv_co_copy_range_to
!= raw_co_copy_range_to
) {
3852 src_s
= src
->bs
->opaque
;
3853 if (fd_open(src
->bs
) < 0 || fd_open(dst
->bs
) < 0) {
3857 acb
= (RawPosixAIOData
) {
3859 .aio_type
= QEMU_AIO_COPY_RANGE
,
3860 .aio_fildes
= src_s
->fd
,
3861 .aio_offset
= src_offset
,
3862 .aio_nbytes
= bytes
,
3865 .aio_offset2
= dst_offset
,
3869 return raw_thread_pool_submit(handle_aiocb_copy_range
, &acb
);
3872 BlockDriver bdrv_file
= {
3873 .format_name
= "file",
3874 .protocol_name
= "file",
3875 .instance_size
= sizeof(BDRVRawState
),
3876 .bdrv_needs_filename
= true,
3877 .bdrv_probe
= NULL
, /* no probe for protocols */
3878 .bdrv_parse_filename
= raw_parse_filename
,
3879 .bdrv_file_open
= raw_open
,
3880 .bdrv_reopen_prepare
= raw_reopen_prepare
,
3881 .bdrv_reopen_commit
= raw_reopen_commit
,
3882 .bdrv_reopen_abort
= raw_reopen_abort
,
3883 .bdrv_close
= raw_close
,
3884 .bdrv_co_create
= raw_co_create
,
3885 .bdrv_co_create_opts
= raw_co_create_opts
,
3886 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3887 .bdrv_co_block_status
= raw_co_block_status
,
3888 .bdrv_co_invalidate_cache
= raw_co_invalidate_cache
,
3889 .bdrv_co_pwrite_zeroes
= raw_co_pwrite_zeroes
,
3890 .bdrv_co_delete_file
= raw_co_delete_file
,
3892 .bdrv_co_preadv
= raw_co_preadv
,
3893 .bdrv_co_pwritev
= raw_co_pwritev
,
3894 .bdrv_co_flush_to_disk
= raw_co_flush_to_disk
,
3895 .bdrv_co_pdiscard
= raw_co_pdiscard
,
3896 .bdrv_co_copy_range_from
= raw_co_copy_range_from
,
3897 .bdrv_co_copy_range_to
= raw_co_copy_range_to
,
3898 .bdrv_refresh_limits
= raw_refresh_limits
,
3899 .bdrv_attach_aio_context
= raw_aio_attach_aio_context
,
3901 .bdrv_co_truncate
= raw_co_truncate
,
3902 .bdrv_co_getlength
= raw_co_getlength
,
3903 .bdrv_co_get_info
= raw_co_get_info
,
3904 .bdrv_get_specific_info
= raw_get_specific_info
,
3905 .bdrv_co_get_allocated_file_size
= raw_co_get_allocated_file_size
,
3906 .bdrv_get_specific_stats
= raw_get_specific_stats
,
3907 .bdrv_check_perm
= raw_check_perm
,
3908 .bdrv_set_perm
= raw_set_perm
,
3909 .bdrv_abort_perm_update
= raw_abort_perm_update
,
3910 .create_opts
= &raw_create_opts
,
3911 .mutable_opts
= mutable_opts
,
3914 /***********************************************/
3917 #if defined(HAVE_HOST_BLOCK_DEVICE)
3919 #if defined(__APPLE__) && defined(__MACH__)
3920 static kern_return_t
GetBSDPath(io_iterator_t mediaIterator
, char *bsdPath
,
3921 CFIndex maxPathSize
, int flags
);
3923 #if !defined(MAC_OS_VERSION_12_0) \
3924 || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_VERSION_12_0)
3925 #define IOMainPort IOMasterPort
3928 static char *FindEjectableOpticalMedia(io_iterator_t
*mediaIterator
)
3930 kern_return_t kernResult
= KERN_FAILURE
;
3931 mach_port_t mainPort
;
3932 CFMutableDictionaryRef classesToMatch
;
3933 const char *matching_array
[] = {kIODVDMediaClass
, kIOCDMediaClass
};
3934 char *mediaType
= NULL
;
3936 kernResult
= IOMainPort(MACH_PORT_NULL
, &mainPort
);
3937 if ( KERN_SUCCESS
!= kernResult
) {
3938 printf("IOMainPort returned %d\n", kernResult
);
3942 for (index
= 0; index
< ARRAY_SIZE(matching_array
); index
++) {
3943 classesToMatch
= IOServiceMatching(matching_array
[index
]);
3944 if (classesToMatch
== NULL
) {
3945 error_report("IOServiceMatching returned NULL for %s",
3946 matching_array
[index
]);
3949 CFDictionarySetValue(classesToMatch
, CFSTR(kIOMediaEjectableKey
),
3951 kernResult
= IOServiceGetMatchingServices(mainPort
, classesToMatch
,
3953 if (kernResult
!= KERN_SUCCESS
) {
3954 error_report("Note: IOServiceGetMatchingServices returned %d",
3959 /* If a match was found, leave the loop */
3960 if (*mediaIterator
!= 0) {
3961 trace_file_FindEjectableOpticalMedia(matching_array
[index
]);
3962 mediaType
= g_strdup(matching_array
[index
]);
3969 kern_return_t
GetBSDPath(io_iterator_t mediaIterator
, char *bsdPath
,
3970 CFIndex maxPathSize
, int flags
)
3972 io_object_t nextMedia
;
3973 kern_return_t kernResult
= KERN_FAILURE
;
3975 nextMedia
= IOIteratorNext( mediaIterator
);
3978 CFTypeRef bsdPathAsCFString
;
3979 bsdPathAsCFString
= IORegistryEntryCreateCFProperty( nextMedia
, CFSTR( kIOBSDNameKey
), kCFAllocatorDefault
, 0 );
3980 if ( bsdPathAsCFString
) {
3981 size_t devPathLength
;
3982 strcpy( bsdPath
, _PATH_DEV
);
3983 if (flags
& BDRV_O_NOCACHE
) {
3984 strcat(bsdPath
, "r");
3986 devPathLength
= strlen( bsdPath
);
3987 if ( CFStringGetCString( bsdPathAsCFString
, bsdPath
+ devPathLength
, maxPathSize
- devPathLength
, kCFStringEncodingASCII
) ) {
3988 kernResult
= KERN_SUCCESS
;
3990 CFRelease( bsdPathAsCFString
);
3992 IOObjectRelease( nextMedia
);
3998 /* Sets up a real cdrom for use in QEMU */
3999 static bool setup_cdrom(char *bsd_path
, Error
**errp
)
4001 int index
, num_of_test_partitions
= 2, fd
;
4002 char test_partition
[MAXPATHLEN
];
4003 bool partition_found
= false;
4005 /* look for a working partition */
4006 for (index
= 0; index
< num_of_test_partitions
; index
++) {
4007 snprintf(test_partition
, sizeof(test_partition
), "%ss%d", bsd_path
,
4009 fd
= qemu_open(test_partition
, O_RDONLY
| O_BINARY
| O_LARGEFILE
, NULL
);
4011 partition_found
= true;
4017 /* if a working partition on the device was not found */
4018 if (partition_found
== false) {
4019 error_setg(errp
, "Failed to find a working partition on disc");
4021 trace_file_setup_cdrom(test_partition
);
4022 pstrcpy(bsd_path
, MAXPATHLEN
, test_partition
);
4024 return partition_found
;
4027 /* Prints directions on mounting and unmounting a device */
4028 static void print_unmounting_directions(const char *file_name
)
4030 error_report("If device %s is mounted on the desktop, unmount"
4031 " it first before using it in QEMU", file_name
);
4032 error_report("Command to unmount device: diskutil unmountDisk %s",
4034 error_report("Command to mount device: diskutil mountDisk %s", file_name
);
4037 #endif /* defined(__APPLE__) && defined(__MACH__) */
4039 static int hdev_probe_device(const char *filename
)
4043 /* allow a dedicated CD-ROM driver to match with a higher priority */
4044 if (strstart(filename
, "/dev/cdrom", NULL
))
4047 if (stat(filename
, &st
) >= 0 &&
4048 (S_ISCHR(st
.st_mode
) || S_ISBLK(st
.st_mode
))) {
4055 static void hdev_parse_filename(const char *filename
, QDict
*options
,
4058 bdrv_parse_filename_strip_prefix(filename
, "host_device:", options
);
4061 static bool hdev_is_sg(BlockDriverState
*bs
)
4064 #if defined(__linux__)
4066 BDRVRawState
*s
= bs
->opaque
;
4068 struct sg_scsi_id scsiid
;
4072 if (stat(bs
->filename
, &st
) < 0 || !S_ISCHR(st
.st_mode
)) {
4076 ret
= ioctl(s
->fd
, SG_GET_VERSION_NUM
, &sg_version
);
4081 ret
= ioctl(s
->fd
, SG_GET_SCSI_ID
, &scsiid
);
4083 trace_file_hdev_is_sg(scsiid
.scsi_type
, sg_version
);
4092 static int hdev_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
4095 BDRVRawState
*s
= bs
->opaque
;
4098 #if defined(__APPLE__) && defined(__MACH__)
4100 * Caution: while qdict_get_str() is fine, getting non-string types
4101 * would require more care. When @options come from -blockdev or
4102 * blockdev_add, its members are typed according to the QAPI
4103 * schema, but when they come from -drive, they're all QString.
4105 const char *filename
= qdict_get_str(options
, "filename");
4106 char bsd_path
[MAXPATHLEN
] = "";
4107 bool error_occurred
= false;
4109 /* If using a real cdrom */
4110 if (strcmp(filename
, "/dev/cdrom") == 0) {
4111 char *mediaType
= NULL
;
4112 kern_return_t ret_val
;
4113 io_iterator_t mediaIterator
= 0;
4115 mediaType
= FindEjectableOpticalMedia(&mediaIterator
);
4116 if (mediaType
== NULL
) {
4117 error_setg(errp
, "Please make sure your CD/DVD is in the optical"
4119 error_occurred
= true;
4120 goto hdev_open_Mac_error
;
4123 ret_val
= GetBSDPath(mediaIterator
, bsd_path
, sizeof(bsd_path
), flags
);
4124 if (ret_val
!= KERN_SUCCESS
) {
4125 error_setg(errp
, "Could not get BSD path for optical drive");
4126 error_occurred
= true;
4127 goto hdev_open_Mac_error
;
4130 /* If a real optical drive was not found */
4131 if (bsd_path
[0] == '\0') {
4132 error_setg(errp
, "Failed to obtain bsd path for optical drive");
4133 error_occurred
= true;
4134 goto hdev_open_Mac_error
;
4137 /* If using a cdrom disc and finding a partition on the disc failed */
4138 if (strncmp(mediaType
, kIOCDMediaClass
, 9) == 0 &&
4139 setup_cdrom(bsd_path
, errp
) == false) {
4140 print_unmounting_directions(bsd_path
);
4141 error_occurred
= true;
4142 goto hdev_open_Mac_error
;
4145 qdict_put_str(options
, "filename", bsd_path
);
4147 hdev_open_Mac_error
:
4149 if (mediaIterator
) {
4150 IOObjectRelease(mediaIterator
);
4152 if (error_occurred
) {
4156 #endif /* defined(__APPLE__) && defined(__MACH__) */
4158 s
->type
= FTYPE_FILE
;
4160 ret
= raw_open_common(bs
, options
, flags
, 0, true, errp
);
4162 #if defined(__APPLE__) && defined(__MACH__)
4164 filename
= bsd_path
;
4166 /* if a physical device experienced an error while being opened */
4167 if (strncmp(filename
, "/dev/", 5) == 0) {
4168 print_unmounting_directions(filename
);
4170 #endif /* defined(__APPLE__) && defined(__MACH__) */
4174 /* Since this does ioctl the device must be already opened */
4175 bs
->sg
= hdev_is_sg(bs
);
4180 #if defined(__linux__)
4181 static int coroutine_fn
4182 hdev_co_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
4184 BDRVRawState
*s
= bs
->opaque
;
4185 RawPosixAIOData acb
;
4193 if (req
== SG_IO
&& s
->pr_mgr
) {
4194 struct sg_io_hdr
*io_hdr
= buf
;
4195 if (io_hdr
->cmdp
[0] == PERSISTENT_RESERVE_OUT
||
4196 io_hdr
->cmdp
[0] == PERSISTENT_RESERVE_IN
) {
4197 return pr_manager_execute(s
->pr_mgr
, qemu_get_current_aio_context(),
4202 acb
= (RawPosixAIOData
) {
4204 .aio_type
= QEMU_AIO_IOCTL
,
4205 .aio_fildes
= s
->fd
,
4213 return raw_thread_pool_submit(handle_aiocb_ioctl
, &acb
);
4217 static coroutine_fn
int
4218 hdev_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
4220 BDRVRawState
*s
= bs
->opaque
;
4225 raw_account_discard(s
, bytes
, ret
);
4228 return raw_do_pdiscard(bs
, offset
, bytes
, true);
4231 static coroutine_fn
int hdev_co_pwrite_zeroes(BlockDriverState
*bs
,
4232 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
)
4241 return raw_do_pwrite_zeroes(bs
, offset
, bytes
, flags
, true);
4244 static BlockDriver bdrv_host_device
= {
4245 .format_name
= "host_device",
4246 .protocol_name
= "host_device",
4247 .instance_size
= sizeof(BDRVRawState
),
4248 .bdrv_needs_filename
= true,
4249 .bdrv_probe_device
= hdev_probe_device
,
4250 .bdrv_parse_filename
= hdev_parse_filename
,
4251 .bdrv_file_open
= hdev_open
,
4252 .bdrv_close
= raw_close
,
4253 .bdrv_reopen_prepare
= raw_reopen_prepare
,
4254 .bdrv_reopen_commit
= raw_reopen_commit
,
4255 .bdrv_reopen_abort
= raw_reopen_abort
,
4256 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
4257 .create_opts
= &bdrv_create_opts_simple
,
4258 .mutable_opts
= mutable_opts
,
4259 .bdrv_co_invalidate_cache
= raw_co_invalidate_cache
,
4260 .bdrv_co_pwrite_zeroes
= hdev_co_pwrite_zeroes
,
4262 .bdrv_co_preadv
= raw_co_preadv
,
4263 .bdrv_co_pwritev
= raw_co_pwritev
,
4264 .bdrv_co_flush_to_disk
= raw_co_flush_to_disk
,
4265 .bdrv_co_pdiscard
= hdev_co_pdiscard
,
4266 .bdrv_co_copy_range_from
= raw_co_copy_range_from
,
4267 .bdrv_co_copy_range_to
= raw_co_copy_range_to
,
4268 .bdrv_refresh_limits
= raw_refresh_limits
,
4269 .bdrv_attach_aio_context
= raw_aio_attach_aio_context
,
4271 .bdrv_co_truncate
= raw_co_truncate
,
4272 .bdrv_co_getlength
= raw_co_getlength
,
4273 .bdrv_co_get_info
= raw_co_get_info
,
4274 .bdrv_get_specific_info
= raw_get_specific_info
,
4275 .bdrv_co_get_allocated_file_size
= raw_co_get_allocated_file_size
,
4276 .bdrv_get_specific_stats
= hdev_get_specific_stats
,
4277 .bdrv_check_perm
= raw_check_perm
,
4278 .bdrv_set_perm
= raw_set_perm
,
4279 .bdrv_abort_perm_update
= raw_abort_perm_update
,
4280 .bdrv_probe_blocksizes
= hdev_probe_blocksizes
,
4281 .bdrv_probe_geometry
= hdev_probe_geometry
,
4283 /* generic scsi device */
4285 .bdrv_co_ioctl
= hdev_co_ioctl
,
4289 #if defined(CONFIG_BLKZONED)
4290 /* zone management operations */
4291 .bdrv_co_zone_report
= raw_co_zone_report
,
4292 .bdrv_co_zone_mgmt
= raw_co_zone_mgmt
,
4293 .bdrv_co_zone_append
= raw_co_zone_append
,
4297 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
4298 static void cdrom_parse_filename(const char *filename
, QDict
*options
,
4301 bdrv_parse_filename_strip_prefix(filename
, "host_cdrom:", options
);
4304 static void cdrom_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
4306 bs
->bl
.has_variable_length
= true;
4307 raw_refresh_limits(bs
, errp
);
4312 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
4315 BDRVRawState
*s
= bs
->opaque
;
4319 /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
4320 return raw_open_common(bs
, options
, flags
, O_NONBLOCK
, true, errp
);
4323 static int cdrom_probe_device(const char *filename
)
4329 fd
= qemu_open(filename
, O_RDONLY
| O_NONBLOCK
, NULL
);
4333 ret
= fstat(fd
, &st
);
4334 if (ret
== -1 || !S_ISBLK(st
.st_mode
)) {
4338 /* Attempt to detect via a CDROM specific ioctl */
4339 ret
= ioctl(fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
4349 static bool coroutine_fn
cdrom_co_is_inserted(BlockDriverState
*bs
)
4351 BDRVRawState
*s
= bs
->opaque
;
4354 ret
= ioctl(s
->fd
, CDROM_DRIVE_STATUS
, CDSL_CURRENT
);
4355 return ret
== CDS_DISC_OK
;
4358 static void coroutine_fn
cdrom_co_eject(BlockDriverState
*bs
, bool eject_flag
)
4360 BDRVRawState
*s
= bs
->opaque
;
4363 if (ioctl(s
->fd
, CDROMEJECT
, NULL
) < 0)
4364 perror("CDROMEJECT");
4366 if (ioctl(s
->fd
, CDROMCLOSETRAY
, NULL
) < 0)
4367 perror("CDROMEJECT");
4371 static void coroutine_fn
cdrom_co_lock_medium(BlockDriverState
*bs
, bool locked
)
4373 BDRVRawState
*s
= bs
->opaque
;
4375 if (ioctl(s
->fd
, CDROM_LOCKDOOR
, locked
) < 0) {
4377 * Note: an error can happen if the distribution automatically
4380 /* perror("CDROM_LOCKDOOR"); */
4384 static BlockDriver bdrv_host_cdrom
= {
4385 .format_name
= "host_cdrom",
4386 .protocol_name
= "host_cdrom",
4387 .instance_size
= sizeof(BDRVRawState
),
4388 .bdrv_needs_filename
= true,
4389 .bdrv_probe_device
= cdrom_probe_device
,
4390 .bdrv_parse_filename
= cdrom_parse_filename
,
4391 .bdrv_file_open
= cdrom_open
,
4392 .bdrv_close
= raw_close
,
4393 .bdrv_reopen_prepare
= raw_reopen_prepare
,
4394 .bdrv_reopen_commit
= raw_reopen_commit
,
4395 .bdrv_reopen_abort
= raw_reopen_abort
,
4396 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
4397 .create_opts
= &bdrv_create_opts_simple
,
4398 .mutable_opts
= mutable_opts
,
4399 .bdrv_co_invalidate_cache
= raw_co_invalidate_cache
,
4401 .bdrv_co_preadv
= raw_co_preadv
,
4402 .bdrv_co_pwritev
= raw_co_pwritev
,
4403 .bdrv_co_flush_to_disk
= raw_co_flush_to_disk
,
4404 .bdrv_refresh_limits
= cdrom_refresh_limits
,
4405 .bdrv_attach_aio_context
= raw_aio_attach_aio_context
,
4407 .bdrv_co_truncate
= raw_co_truncate
,
4408 .bdrv_co_getlength
= raw_co_getlength
,
4409 .bdrv_co_get_allocated_file_size
= raw_co_get_allocated_file_size
,
4411 /* removable device support */
4412 .bdrv_co_is_inserted
= cdrom_co_is_inserted
,
4413 .bdrv_co_eject
= cdrom_co_eject
,
4414 .bdrv_co_lock_medium
= cdrom_co_lock_medium
,
4416 /* generic scsi device */
4417 .bdrv_co_ioctl
= hdev_co_ioctl
,
4419 #endif /* __linux__ */
4421 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
4422 static int cdrom_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
4425 BDRVRawState
*s
= bs
->opaque
;
4430 ret
= raw_open_common(bs
, options
, flags
, 0, true, errp
);
4435 /* make sure the door isn't locked at this time */
4436 ioctl(s
->fd
, CDIOCALLOW
);
4440 static int cdrom_probe_device(const char *filename
)
4442 if (strstart(filename
, "/dev/cd", NULL
) ||
4443 strstart(filename
, "/dev/acd", NULL
))
4448 static int cdrom_reopen(BlockDriverState
*bs
)
4450 BDRVRawState
*s
= bs
->opaque
;
4454 * Force reread of possibly changed/newly loaded disc,
4455 * FreeBSD seems to not notice sometimes...
4459 fd
= qemu_open(bs
->filename
, s
->open_flags
, NULL
);
4466 /* make sure the door isn't locked at this time */
4467 ioctl(s
->fd
, CDIOCALLOW
);
4471 static bool coroutine_fn
cdrom_co_is_inserted(BlockDriverState
*bs
)
4473 return raw_getlength(bs
) > 0;
4476 static void coroutine_fn
cdrom_co_eject(BlockDriverState
*bs
, bool eject_flag
)
4478 BDRVRawState
*s
= bs
->opaque
;
4483 (void) ioctl(s
->fd
, CDIOCALLOW
);
4486 if (ioctl(s
->fd
, CDIOCEJECT
) < 0)
4487 perror("CDIOCEJECT");
4489 if (ioctl(s
->fd
, CDIOCCLOSE
) < 0)
4490 perror("CDIOCCLOSE");
4496 static void coroutine_fn
cdrom_co_lock_medium(BlockDriverState
*bs
, bool locked
)
4498 BDRVRawState
*s
= bs
->opaque
;
4502 if (ioctl(s
->fd
, (locked
? CDIOCPREVENT
: CDIOCALLOW
)) < 0) {
4504 * Note: an error can happen if the distribution automatically
4507 /* perror("CDROM_LOCKDOOR"); */
4511 static BlockDriver bdrv_host_cdrom
= {
4512 .format_name
= "host_cdrom",
4513 .protocol_name
= "host_cdrom",
4514 .instance_size
= sizeof(BDRVRawState
),
4515 .bdrv_needs_filename
= true,
4516 .bdrv_probe_device
= cdrom_probe_device
,
4517 .bdrv_parse_filename
= cdrom_parse_filename
,
4518 .bdrv_file_open
= cdrom_open
,
4519 .bdrv_close
= raw_close
,
4520 .bdrv_reopen_prepare
= raw_reopen_prepare
,
4521 .bdrv_reopen_commit
= raw_reopen_commit
,
4522 .bdrv_reopen_abort
= raw_reopen_abort
,
4523 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
4524 .create_opts
= &bdrv_create_opts_simple
,
4525 .mutable_opts
= mutable_opts
,
4527 .bdrv_co_preadv
= raw_co_preadv
,
4528 .bdrv_co_pwritev
= raw_co_pwritev
,
4529 .bdrv_co_flush_to_disk
= raw_co_flush_to_disk
,
4530 .bdrv_refresh_limits
= cdrom_refresh_limits
,
4531 .bdrv_attach_aio_context
= raw_aio_attach_aio_context
,
4533 .bdrv_co_truncate
= raw_co_truncate
,
4534 .bdrv_co_getlength
= raw_co_getlength
,
4535 .bdrv_co_get_allocated_file_size
= raw_co_get_allocated_file_size
,
4537 /* removable device support */
4538 .bdrv_co_is_inserted
= cdrom_co_is_inserted
,
4539 .bdrv_co_eject
= cdrom_co_eject
,
4540 .bdrv_co_lock_medium
= cdrom_co_lock_medium
,
4542 #endif /* __FreeBSD__ */
4544 #endif /* HAVE_HOST_BLOCK_DEVICE */
4546 static void bdrv_file_init(void)
4549 * Register all the drivers. Note that order is important, the driver
4550 * registered last will get probed first.
4552 bdrv_register(&bdrv_file
);
4553 #if defined(HAVE_HOST_BLOCK_DEVICE)
4554 bdrv_register(&bdrv_host_device
);
4556 bdrv_register(&bdrv_host_cdrom
);
4558 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
4559 bdrv_register(&bdrv_host_cdrom
);
4561 #endif /* HAVE_HOST_BLOCK_DEVICE */
4564 block_init(bdrv_file_init
);