block: file-posix queue
Subsystems (2)
v5 cover block: file-posix queue
New patches:
- 3/4 (for review comments),
- 9 (split for ease of review),
- 11 (new bugfix)
v1->v2: add missing patch
v2->v3: add max_hw_transfer to BlockLimits
v3->v4: fix compilation after patch 1, tweak commit messages according
to Vladimir's review
v4->v5: round down max_transfer and max_hw_transfer to request alignment
checkpatch fixes
return -ENOTSUP, -not -EIO if block limits ioctls fail handle host_cdrom like host_device in QAPI
split "block: try BSD disk size ioctls one after another" new bugfix patch "file-posix: handle EINTR during ioctl"
Joelle van Dyne (3):
block: feature detection for host block support
block: check for sys/disk.h
block: detect DKIOCGETBLOCKCOUNT/SIZE before use
Paolo Bonzini (8):
file-posix: fix max_iov for /dev/sg devices
scsi-generic: pass max_segments via max_iov field in BlockLimits
osdep: provide ROUND_DOWN macro
block-backend: align max_transfer to request alignment
block: add max_hw_transfer to BlockLimits
file-posix: try BLKSECTGET on block devices too, do not round to power of 2
block: try BSD disk size ioctls one after another
file-posix: handle EINTR during ioctl
block.c | 2 +- block/block-backend.c | 19 ++++- block/file-posix.c | 144 ++++++++++++++++++++------------- block/io.c | 2 + hw/scsi/scsi-generic.c | 6 +- include/block/block_int.h | 7 ++ include/qemu/osdep.h | 28 +++++-- include/sysemu/block-backend.h | 1 + meson.build | 7 +- qapi/block-core.json | 14 +++- 10 files changed, 156 insertions(+), 74 deletions(-)
Signature · 1 line
2.31.1
PATCH v5 1/11 file-posix: fix max_iov for /dev/sg devices
Even though it was only called for devices that have bs->sg set (which must be character devices), sg_get_max_segments looked at /sys/dev/block which only works for block devices.
On Linux the sg driver has its own way to provide the maximum number of iovecs in a scatter/gather list, so add support for it. The block device path is kept because it will be reinstated in the next patches.
--- block/file-posix.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
block/file-posix.c +11 -0
@@ -1178,6 +1178,17 @@ static int sg_get_max_segments(int fd) goto out; } +if (S_ISCHR (st.st_mode)) { +if (ioctl (fd,SG_GET_SG_TABLESIZE , &ret) ==0 ) { +return ret; + } +return -ENOTSUP ; + } + +if (!S_ISBLK (st.st_mode)) { +return -ENOTSUP ; + } + sysfspath =g_strdup_printf ("/sys/dev/block/%u:%u/queue/max_segments" ,major (st.st_rdev),minor (st.st_rdev)); sysfd =open (sysfspath,O_RDONLY );
Signature · 1 line
2.31.1
PATCH v5 2/11 scsi-generic: pass max_segments via max_iov field in BlockLimits
In reply to Paolo BonziniI/O to a disk via read/write is not limited by the number of segments allowed by the host adapter; the kernel can split requests if needed, and the limit imposed by the host adapter can be very low (256k or so) to avoid that SG_IO returns EINVAL if memory is heavily fragmented.
Since this value is only interesting for SG_IO-based I/O, do not include it in the max_transfer and only take it into account when patching the block limits VPD page in the scsi-generic device.
--- block/file-posix.c | 3 +-- hw/scsi/scsi-generic.c | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-)
block/file-posix.c +1 -2
@@ -1237,8 +1237,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp) ret =sg_get_max_segments (s->fd);if (ret >0 ) { - bs->bl.max_transfer =MIN (bs->bl.max_transfer, - ret * qemu_real_host_page_size); + bs->bl.max_iov = ret; } }
hw/scsi/scsi-generic.c +4 -2
@@ -179,10 +179,12 @@ static int scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s, int len) (r->req.cmd.buf[1] & 0x01)) { page = r->req.cmd.buf[2 ];if (page ==0xb0 ) { -uint32_t max_transfer = -blk_get_max_transfer (s->conf.blk) / s->blocksize; +uint32_t max_transfer =blk_get_max_transfer (s->conf.blk); +uint32_t max_iov =blk_get_max_iov (s->conf.blk);assert (max_transfer); + max_transfer =MIN_NON_ZERO (max_transfer, max_iov * qemu_real_host_page_size) + / s->blocksize;stl_be_p (&r->buf[8 ], max_transfer);/* Also take care of the opt xfer len. */ stl_be_p(&r->buf[12],
Signature · 1 line
2.31.1
PATCH v5 3/11 osdep: provide ROUND_DOWN macro
In reply to Paolo Bonziniosdep.h provides a ROUND_UP macro to hide bitwise operations for the purpose of rounding a number up to a power of two; add a ROUND_DOWN macro that does the same with truncation towards zero.
While at it, change the formatting of some comments.
--- include/qemu/osdep.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
include/qemu/osdep.h +22 -6
@@ -319,11 +319,16 @@ extern "C" { })#endif -/* Round number down to multiple */ +/* +* Round number down to multiple. Safe when m is not a power of 2 (see +* ROUND_DOWN for a faster version when a power of 2 is guaranteed). +*/ #define QEMU_ALIGN_DOWN (n, m) ((n) / (m) * (m)) -/* Round number up to multiple. Safe when m is not a power of 2 (see -* ROUND_UP for a faster version when a power of 2 is guaranteed) */ +/* +* Round number up to multiple. Safe when m is not a power of 2 (see +* ROUND_UP for a faster version when a power of 2 is guaranteed). +*/ #define QEMU_ALIGN_UP (n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))/* Check if n is a multiple of m */
@@ -340,11 +345,22 @@ extern "C" {/* Check if pointer p is n-bytes aligned */ #define QEMU_PTR_IS_ALIGNED (p, n) QEMU_IS_ALIGNED((uintptr_t)(p), (n)) -/* Round number up to multiple. Requires that d be a power of 2 (see +/* +* Round number down to multiple. Requires that d be a power of 2 (see * QEMU_ALIGN_UP for a safer but slower version on arbitrary -* numbers); works even if d is a smaller type than n. */ +* numbers); works even if d is a smaller type than n. +*/ +#ifndef ROUND_DOWN +#define ROUND_DOWN (n, d) ((n) & -(0 ? (n) : (d))) +#endif + +/* +* Round number up to multiple. Requires that d be a power of 2 (see +* QEMU_ALIGN_UP for a safer but slower version on arbitrary +* numbers); works even if d is a smaller type than n. +*/ #ifndef ROUND_UP -#define ROUND_UP (n, d) (((n) + (d) - 1) & -(0 ? (n) : (d))) +#define ROUND_UP (n, d) ROUND_DOWN((n) + (d) - 1, (d))#endif #ifndef DIV_ROUND_UP
Signature · 1 line
2.31.1
Re: [PATCH 03/11] osdep: provide ROUND_DOWN macro
Quoted 10 lines · Paolo Bonzini
osdep.hprovides aROUND_UPmacro to hide bitwise operations for the purpose of rounding a number up to a power of two; add aROUND_DOWNmacro that does the same with truncation towards zero.While at it, change the formatting of some comments.
Signed-off-by Paolo Bonzini <[email protected]>--- include/qemu/osdep.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
What a nice thing to have.
Re: [PATCH 03/11] osdep: provide ROUND_DOWN macro
In reply to Paolo BonziniQuoted 10 lines · Paolo Bonzini
osdep.hprovides aROUND_UPmacro to hide bitwise operations for the purpose of rounding a number up to a power of two; add aROUND_DOWNmacro that does the same with truncation towards zero.While at it, change the formatting of some comments.
Signed-off-by Paolo Bonzini <[email protected]>--- include/qemu/osdep.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
PATCH v5 4/11 block-backend: align max_transfer to request alignment
In reply to Paolo BonziniBlock device requests must be aligned to bs->bl.request_alignment. It makes sense for drivers to align bs->bl.max_transfer the same way; however when there is no specified limit, blk_get_max_transfer just returns INT_MAX. Since the contract of the function does not specify that INT_MAX means "no maximum", just align the outcome of the function (whether INT_MAX or bs->bl.max_transfer) before returning it.
--- block/block-backend.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
block/block-backend.c +3 -3
@@ -1957,12 +1957,12 @@ uint32_t blk_get_request_alignment(BlockBackend *blk)uint32_t blk_get_max_transfer (BlockBackend *blk) {BlockDriverState *bs =blk_bs (blk); -uint32_t max =0 ; +uint32_t max =INT_MAX ;if (bs) { - max = bs->bl.max_transfer; + max =MIN_NON_ZERO (max, bs->bl.max_transfer); } -return MIN_NON_ZERO (max,INT_MAX ); +return ROUND_DOWN (max,blk_get_request_alignment (blk)); }int blk_get_max_iov (BlockBackend *blk)
Signature · 1 line
2.31.1
Re: [PATCH 04/11] block-backend: align max_transfer to request alignment
Quoted 8 lines · Paolo Bonzini
Block device requests must be aligned to
bs->bl.request_alignment. It makes sense for drivers to alignbs->bl.max_transferthe same way; however when there is no specified limit,blk_get_max_transferjust returnsINT_MAX. Since the contract of the function does not specify thatINT_MAXmeans "no maximum", just align the outcome of the function (whetherINT_MAXorbs->bl.max_transfer) before returning it.
Quoted 4 lines
Signed-off-by Paolo Bonzini <[email protected]>--- block/block-backend.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
PATCH v5 5/11 block: add max_hw_transfer to BlockLimits
In reply to Paolo BonziniFor block host devices, I/O can happen through either the kernel file descriptor I/O system calls (preadv/pwritev, io_submit, io_uring) or the SCSI passthrough ioctl SG_IO.
In the latter case, the size of each transfer can be limited by the HBA, while for file descriptor I/O the kernel is able to split and merge I/O in smaller pieces as needed. Applying the HBA limits to file descriptor I/O results in more system calls and suboptimal performance, so this patch splits the max_transfer limit in two:max_transfer remains valid and is used in general, while max_hw_transfer is limited to the maximum hardware size. max_hw_transfer can then be included by the scsi-generic driver in the block limits page, to ensure that the stricter hardware limit is used.
--- block/block-backend.c | 13 +++++++++++++ block/file-posix.c | 2 +- block/io.c | 2 ++ hw/scsi/scsi-generic.c | 2 +- include/block/block_int.h | 7 +++++++ include/sysemu/block-backend.h | 1 + 6 files changed, 25 insertions(+), 2 deletions(-)
block/block-backend.c +13 -0
@@ -1953,6 +1953,19 @@ uint32_t blk_get_request_alignment(BlockBackend *blk)return bs ? bs->bl.request_alignment :BDRV_SECTOR_SIZE ; } +/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */ +uint64_t blk_get_max_hw_transfer (BlockBackend *blk) +{ +BlockDriverState *bs =blk_bs (blk); +uint64_t max =INT_MAX ; + +if (bs) { + max =MIN_NON_ZERO (max, bs->bl.max_hw_transfer); + max =MIN_NON_ZERO (max, bs->bl.max_transfer); + } +return ROUND_DOWN (max,blk_get_request_alignment (blk)); +} +/* Returns the maximum transfer length, in bytes; guaranteed nonzero */ uint32_t blk_get_max_transfer (BlockBackend *blk) {
block/file-posix.c +1 -1
@@ -1232,7 +1232,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)int ret =sg_get_max_transfer_length (s->fd);if (ret >0 && ret <=BDRV_REQUEST_MAX_BYTES ) { - bs->bl.max_transfer =pow2floor (ret); + bs->bl.max_hw_transfer =pow2floor (ret); } ret =sg_get_max_segments (s->fd);
block/io.c +2 -0
@@ -127,6 +127,8 @@ static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src) { dst->opt_transfer =MAX (dst->opt_transfer, src->opt_transfer); dst->max_transfer =MIN_NON_ZERO (dst->max_transfer, src->max_transfer); + dst->max_hw_transfer =MIN_NON_ZERO (dst->max_hw_transfer, + src->max_hw_transfer); dst->opt_mem_alignment =MAX (dst->opt_mem_alignment, src->opt_mem_alignment); dst->min_mem_alignment = MAX(dst->min_mem_alignment,
hw/scsi/scsi-generic.c +1 -1
@@ -179,7 +179,7 @@ static int scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s, int len) (r->req.cmd.buf[1] & 0x01)) { page = r->req.cmd.buf[2 ];if (page ==0xb0 ) { -uint32_t max_transfer =blk_get_max_transfer (s->conf.blk); +uint64_t max_transfer =blk_get_max_hw_transfer (s->conf.blk);uint32_t max_iov =blk_get_max_iov (s->conf.blk);assert (max_transfer);
include/block/block_int.h +7 -0
@@ -695,6 +695,13 @@ typedef struct BlockLimits {* clamped down. */ uint32_t max_transfer; +/* Maximal hardware transfer length in bytes. Applies whenever +* transfers to the device bypass the kernel I/O scheduler, for +* example with SG_IO. If larger than max_transfer or if zero, +* blk_get_max_hw_transfer will fall back to max_transfer. +*/ +uint64_t max_hw_transfer; +/* memory alignment, in bytes so that no bounce buffer is needed */ size_t min_mem_alignment;
include/sysemu/block-backend.h +1 -0
@@ -208,6 +208,7 @@ void blk_eject(BlockBackend *blk, bool eject_flag);int blk_get_flags (BlockBackend *blk);uint32_t blk_get_request_alignment (BlockBackend *blk);uint32_t blk_get_max_transfer (BlockBackend *blk); +uint64_t blk_get_max_hw_transfer (BlockBackend *blk);int blk_get_max_iov (BlockBackend *blk);void blk_set_guest_block_size (BlockBackend *blk,int align);void *blk_try_blockalign (BlockBackend *blk,size_t size);
Signature · 1 line
2.31.1
Re: [PATCH 05/11] block: add max_hw_transfer to BlockLimits
Quoted 15 lines · Paolo Bonzini
For block host devices, I/O can happen through either the kernel file descriptor I/O system calls (preadv/pwritev,
io_submit,io_uring) or the SCSI passthrough ioctlSG_IO.In the latter case, the size of each transfer can be limited by the HBA, while for file descriptor I/O the kernel is able to split and merge I/O in smaller pieces as needed. Applying the HBA limits to file descriptor I/O results in more system calls and suboptimal performance, so this patch splits the
max_transferlimit in two:max_transferremains valid and is used in general, whilemax_hw_transferis limited to the maximum hardware size.max_hw_transfercan then be included by the scsi-generic driver in the block limits page, to ensure that the stricter hardware limit is used.Signed-off-by Paolo Bonzini <[email protected]>
Quoted 8 lines
--- block/block-backend.c | 13 +++++++++++++ block/file-posix.c | 2 +- block/io.c | 2 ++ hw/scsi/scsi-generic.c | 2 +- include/block/block_int.h | 7 +++++++ include/sysemu/block-backend.h | 1 + 6 files changed, 25 insertions(+), 2 deletions(-)
PATCH v5 6/11 file-posix: try BLKSECTGET on block devices too, do not round to power of 2
In reply to Paolo Bonzinibs->sg is only true for character devices, but block devices can also be used with scsi-block and scsi-generic. Unfortunately BLKSECTGET returns bytes in an int for /dev/sgN devices, and sectors in a short for block devices, so account for that in the code.
The maximum transfer also need not be a power of 2 (for example I have seen disks with 1280 KiB maximum transfer) so there's no need to pass the result through pow2floor.
--- block/file-posix.c | 67 ++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 29 deletions(-)
block/file-posix.c +38 -29
@@ -1147,22 +1147,27 @@ static void raw_reopen_abort(BDRVReopenState *state) s->reopen_state =NULL ; } -static int sg_get_max_transfer_length (int fd) +static int hdev_get_max_hw_transfer (int fd,struct stat *st) {#ifdef BLKSECTGET -int max_bytes =0 ; - -if (ioctl (fd,BLKSECTGET , &max_bytes) ==0 ) { -return max_bytes; +if (S_ISBLK (st->st_mode)) { +unsigned short max_sectors =0 ; +if (ioctl (fd,BLKSECTGET , &max_sectors) ==0 ) { +return max_sectors *512 ; + } }else { -return -errno; +int max_bytes =0 ; +if (ioctl (fd,BLKSECTGET , &max_bytes) ==0 ) { +return max_bytes; + } } +return -errno;#else return -ENOSYS ;#endif } -static int sg_get_max_segments (int fd) +static int hdev_get_max_segments (int fd,struct stat *st) {#ifdef CONFIG_LINUX char buf[32 ];
@@ -1171,26 +1176,20 @@ static int sg_get_max_segments(int fd)int ret;int sysfd =-1 ;long max_segments; -struct stat st; -if (fstat (fd, &st)) { - ret = -errno; - goto out; - } - -if (S_ISCHR (st.st_mode)) { +if (S_ISCHR (st->st_mode)) {if (ioctl (fd,SG_GET_SG_TABLESIZE , &ret) ==0 ) {return ret; }return -ENOTSUP ; } -if (!S_ISBLK (st.st_mode)) { +if (!S_ISBLK (st->st_mode)) {return -ENOTSUP ; } sysfspath =g_strdup_printf ("/sys/dev/block/%u:%u/queue/max_segments" , -major (st.st_rdev),minor (st.st_rdev)); +major (st->st_rdev),minor (st->st_rdev)); sysfd =open (sysfspath,O_RDONLY );if (sysfd ==-1 ) { ret = -errno;
@@ -1227,23 +1226,33 @@ out:static void raw_refresh_limits (BlockDriverState *bs,Error **errp) {BDRVRawState *s = bs->opaque; - -if (bs->sg) { -int ret =sg_get_max_transfer_length (s->fd); - -if (ret >0 && ret <=BDRV_REQUEST_MAX_BYTES ) { - bs->bl.max_hw_transfer =pow2floor (ret); - } - - ret =sg_get_max_segments (s->fd); -if (ret >0 ) { - bs->bl.max_iov = ret; - } - } +struct stat st;raw_probe_alignment (bs, s->fd, errp); bs->bl.min_mem_alignment = s->buf_align; bs->bl.opt_mem_alignment =MAX (s->buf_align, qemu_real_host_page_size); + +/* +* Maximum transfers are best effort, so it is okay to ignore any +* errors. That said, based on the man page errors in fstat would be +* very much unexpected; the only possible case seems to be ENOMEM. +*/ +if (fstat (s->fd, &st)) { +return ; + } + +if (bs->sg ||S_ISBLK (st.st_mode)) { +int ret =hdev_get_max_hw_transfer (s->fd, &st); + +if (ret >0 && ret <=BDRV_REQUEST_MAX_BYTES ) { + bs->bl.max_hw_transfer = ret; + } + + ret =hdev_get_max_segments (s->fd, &st); +if (ret >0 ) { + bs->bl.max_iov = ret; + } + } }static int check_for_dasd (int fd)
Signature · 1 line
2.31.1
Re: [PATCH 06/11] file-posix: try BLKSECTGET on block devices too, do not round to power of 2
Quoted 5 lines · Paolo Bonzini
bs->sgis only true for character devices, but block devices can also be used with scsi-block and scsi-generic. Unfortunately BLKSECTGET returns bytes in an int for/dev/sgNdevices, and sectors in a short for block devices, so account for that in the code.
Quoted 8 lines
The maximum transfer also need not be a power of 2 (for example I have seen disks with 1280 KiB maximum transfer) so there's no need to pass the result through pow2floor.
Signed-off-by Paolo Bonzini <[email protected]>--- block/file-posix.c | 67 ++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 29 deletions(-)
PATCH v5 7/11 block: feature detection for host block support
In reply to Paolo BonziniFrom: Joelle van Dyne <[email protected]>
On Darwin (iOS), there are no system level APIs for directly accessing host block devices. We detect this at configure time.
Message-Id: <[email protected]>
--- block/file-posix.c | 33 ++++++++++++++++++++++----------- meson.build | 6 +++++- qapi/block-core.json | 14 ++++++++++---- 3 files changed, 37 insertions(+), 16 deletions(-)
block/file-posix.c +22 -11
@@ -42,6 +42,8 @@#include "scsi/constants.h" #if defined(__APPLE__) && (__MACH__) +#include <sys/ioctl.h> +#if defined(HAVE_HOST_BLOCK_DEVICE )#include <paths.h> #include <sys/param.h> #include <IOKit/IOKitLib.h>
@@ -52,6 +54,7 @@//#include <IOKit/storage/IOCDTypes.h> #include <IOKit/storage/IODVDMedia.h> #include <CoreFoundation/CoreFoundation.h> +#endif /* defined(HAVE_HOST_BLOCK_DEVICE) */ #endif #ifdef __sun__
@@ -178,7 +181,17 @@ typedef struct BDRVRawReopenState {bool check_cache_dropped; } BDRVRawReopenState; -static int fd_open (BlockDriverState *bs); +static int fd_open (BlockDriverState *bs) +{ +BDRVRawState *s = bs->opaque; + +/* this is just to ensure s->fd is sane (its called by io ops) */ +if (s->fd >=0 ) { +return 0 ; + } +return -EIO ; +} +static int64_t raw_getlength (BlockDriverState *bs); typedef struct RawPosixAIOData {
@@ -3033,6 +3046,7 @@ static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)return stats; } +#if defined(HAVE_HOST_BLOCK_DEVICE )static BlockStatsSpecific *hdev_get_specific_stats (BlockDriverState *bs) {BlockStatsSpecific *stats =g_new (BlockStatsSpecific,1 );
@@ -3042,6 +3056,7 @@ static BlockStatsSpecific *hdev_get_specific_stats(BlockDriverState *bs)return stats; } +#endif /* HAVE_HOST_BLOCK_DEVICE */ static QemuOptsList raw_create_opts = { .name ="raw-create-opts" ,
@@ -3257,6 +3272,8 @@ BlockDriver bdrv_file = {/***********************************************/ /* host device */ +#if defined(HAVE_HOST_BLOCK_DEVICE ) +#if defined(__APPLE__) && defined(__MACH__)static kern_return_t GetBSDPath (io_iterator_t mediaIterator,char *bsdPath,CFIndex maxPathSize,int flags);
@@ -3549,16 +3566,6 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) }#endif /* linux */ -static int fd_open (BlockDriverState *bs) -{ -BDRVRawState *s = bs->opaque; - -/* this is just to ensure s->fd is sane (its called by io ops) */ -if (s->fd >=0 ) -return 0 ; -return -EIO ; -} - static coroutine_fn inthdev_co_pdiscard (BlockDriverState *bs,int64_t offset,int bytes) {
@@ -3882,6 +3889,8 @@ static BlockDriver bdrv_host_cdrom = { };#endif /* __FreeBSD__ */ +#endif /* HAVE_HOST_BLOCK_DEVICE */ +static void bdrv_file_init (void ) {/*
@@ -3889,6 +3898,7 @@ static void bdrv_file_init(void)* registered last will get probed first. */ bdrv_register (&bdrv_file); +#if defined(HAVE_HOST_BLOCK_DEVICE )bdrv_register (&bdrv_host_device);#ifdef __linux__bdrv_register (&bdrv_host_cdrom);
@@ -3896,6 +3906,7 @@ static void bdrv_file_init(void)#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)bdrv_register (&bdrv_host_cdrom);#endif +#endif /* HAVE_HOST_BLOCK_DEVICE */ }block_init (bdrv_file_init);
meson.build +5 -1
@@ -183,7 +183,7 @@ if targetos == 'windows' include_directories: include_directories('.')) elif targetos == 'darwin' coref = dependency('appleframeworks', modules: 'CoreFoundation') - iokit = dependency('appleframeworks', modules: 'IOKit') + iokit = dependency('appleframeworks', modules: 'IOKit', required: false) elif targetos == 'sunos' socket = [cc.find_library('socket'), cc.find_library('nsl'),
@@ -1147,6 +1147,9 @@ if get_option('cfi') add_global_link_arguments(cfi_flags, native: false, language: ['c', 'cpp', 'objc']) endif +have_host_block_device = (targetos != 'darwin' or + cc.has_header('IOKit/storage/IOMedia.h')) + ################# # config-host.h # #################
@@ -1246,6 +1249,7 @@ config_host_data.set('HAVE_PTY_H', cc.has_header('pty.h')) config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h')) config_host_data.set('HAVE_SYS_KCOV_H', cc.has_header('sys/kcov.h')) config_host_data.set('HAVE_SYSTEM_FUNCTION', cc.has_function('system', prefix: '#include <stdlib.h>')) +config_host_data.set('HAVE_HOST_BLOCK_DEVICE', have_host_block_device) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>'))
qapi/block-core.json +10 -4
@@ -897,7 +897,8 @@ 'discriminator': 'driver', 'data': { 'file': 'BlockStatsSpecificFile', - 'host_device': 'BlockStatsSpecificFile', + 'host_device': { 'type': 'BlockStatsSpecificFile', + 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' }, 'nvme': 'BlockStatsSpecificNvme' } } ##
@@ -2814,7 +2815,10 @@ { 'enum': 'BlockdevDriver', 'data': [ 'blkdebug', 'blklogwrites', 'blkreplay', 'blkverify', 'bochs', 'cloop', 'compress', 'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', - 'gluster', 'host_cdrom', 'host_device', 'http', 'https', 'iscsi', + 'gluster', + {'name': 'host_cdrom', 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' }, + {'name': 'host_device', 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' }, + 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'preallocate', 'qcow', 'qcow2', 'qed', 'quorum', 'raw', 'rbd', { 'name': 'replication', 'if': 'defined(CONFIG_REPLICATION)' },
@@ -3995,8 +3999,10 @@ 'ftp': 'BlockdevOptionsCurlFtp', 'ftps': 'BlockdevOptionsCurlFtps', 'gluster': 'BlockdevOptionsGluster', - 'host_cdrom': 'BlockdevOptionsFile', - 'host_device':'BlockdevOptionsFile', + 'host_cdrom': { 'type': 'BlockdevOptionsFile', + 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' }, + 'host_device': { 'type': 'BlockdevOptionsFile', + 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' }, 'http': 'BlockdevOptionsCurlHttp', 'https': 'BlockdevOptionsCurlHttps', 'iscsi': 'BlockdevOptionsIscsi',
Signature · 1 line
2.31.1
Re: [PATCH 07/11] block: feature detection for host block support
Quoted 5 lines · Paolo Bonzini
From: Joelle van Dyne <[email protected]>
On Darwin (iOS), there are no system level APIs for directly accessing host block devices. We detect this at configure time.
Quoted 8 lines
Signed-off-by Joelle van Dyne <[email protected]>Message-Id: <[email protected]>
Signed-off-by Paolo Bonzini <[email protected]>--- block/file-posix.c | 33 ++++++++++++++++++++++----------- meson.build | 6 +++++- qapi/block-core.json | 14 ++++++++++---- 3 files changed, 37 insertions(+), 16 deletions(-)
PATCH v5 8/11 block: check for sys/disk.h
In reply to Paolo BonziniFrom: Joelle van Dyne <[email protected]>
Some BSD platforms do not have this header.
Message-Id: <[email protected]>
--- block.c | 2 +- meson.build | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
block.c +1 -1
@@ -54,7 +54,7 @@#ifdef CONFIG_BSD #include <sys/ioctl.h> #include <sys/queue.h> -#ifndef __DragonFly__ +#if defined(HAVE_SYS_DISK_H )#include <sys/disk.h> #endif #endif
meson.build +1 -0
@@ -1250,6 +1250,7 @@ config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h')) config_host_data.set('HAVE_SYS_KCOV_H', cc.has_header('sys/kcov.h')) config_host_data.set('HAVE_SYSTEM_FUNCTION', cc.has_function('system', prefix: '#include <stdlib.h>')) config_host_data.set('HAVE_HOST_BLOCK_DEVICE', have_host_block_device) +config_host_data.set('HAVE_SYS_DISK_H', cc.has_header('sys/disk.h')) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>'))
Signature · 1 line
2.31.1
PATCH v5 9/11 block: try BSD disk size ioctls one after another
In reply to Paolo BonziniTry all the possible ioctls for disk size as long as they are supported, to keep the #if ladder simple.
Extracted and cleaned up from a patch by Joelle van Dyne and Warner Losh.
--- block/file-posix.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)
block/file-posix.c +16 -18
@@ -2327,39 +2327,37 @@ static int64_t raw_getlength(BlockDriverState *bs) again: #endifif (!fstat (fd, &sb) && (S_IFCHR & sb.st_mode)) { + size =0 ;#ifdef DIOCGMEDIASIZE - if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) -#elif defined(DIOCGPART ) - { -struct partinfo pi; -if (ioctl (fd,DIOCGPART , &pi) ==0 ) - size = pi.media_size; -else - size =0 ; +if (ioctl (fd,DIOCGMEDIASIZE , (off_t *)&size)) { + size =0 ; + } +#endif +#ifdef DIOCGPART +if (size ==0 ) { +struct partinfo pi; +if (ioctl (fd,DIOCGPART , &pi) ==0 ) { + size = pi.media_size; + } } - if (size == 0)#endif #if defined(__APPLE__) && defined(__MACH__) - { +if (size ==0 ) {uint64_t sectors =0 ;uint32_t sector_size =0 ;if (ioctl (fd,DKIOCGETBLOCKCOUNT , §ors) ==0 &&ioctl (fd,DKIOCGETBLOCKSIZE , §or_size) ==0 ) { size = sectors * sector_size; - }else { - size =lseek (fd,0LL ,SEEK_END ); -if (size <0 ) { -return -errno; - } } } -#else - size =lseek (fd,0LL ,SEEK_END ); +#endif +if (size ==0 ) { + size =lseek (fd,0LL ,SEEK_END ); + }if (size <0 ) {return -errno; } -#endif #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)switch (s->type) {case FTYPE_CD :
Signature · 1 line
2.31.1
Re: [PATCH 09/11] block: try BSD disk size ioctls one after another
Quoted 10 lines · Paolo Bonzini
Try all the possible ioctls for disk size as long as they are supported, to keep the #if ladder simple.
Extracted and cleaned up from a patch by Joelle van Dyne and Warner Losh.
Signed-off-by Paolo Bonzini <[email protected]>--- block/file-posix.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)
Thanks, that’s much better.
PATCH v5 10/11 block: detect DKIOCGETBLOCKCOUNT/SIZE before use
In reply to Paolo BonziniFrom: Joelle van Dyne <[email protected]>
iOS hosts do not have these defined so we fallback to the default behaviour.
--- block/file-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/file-posix.c +1 -1
@@ -2341,7 +2341,7 @@ again: } }#endif -#if defined(__APPLE__) && defined(__MACH__) +#if defined(DKIOCGETBLOCKCOUNT ) && defined(DKIOCGETBLOCKSIZE )if (size ==0 ) {uint64_t sectors =0 ;uint32_t sector_size =0 ;
Signature · 1 line
2.31.1
Re: [PATCH 10/11] block: detect DKIOCGETBLOCKCOUNT/SIZE before use
Quoted 11 lines · Paolo Bonzini
From: Joelle van Dyne <[email protected]>
iOS hosts do not have these defined so we fallback to the default behaviour.
Co-authored-by Warner Losh <[email protected]>Signed-off-by Joelle van Dyne <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- block/file-posix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH v5 11/11 file-posix: handle EINTR during ioctl
In reply to Paolo BonziniSimilar to other handle_aiocb_* functions, handle_aiocb_ioctl needs to cater for the possibility that ioctl is interrupted by a signal. Otherwise, the I/O is incorrectly reported as a failure to the guest.
--- block/file-posix.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
block/file-posix.c +3 -1
@@ -1347,7 +1347,9 @@ static int handle_aiocb_ioctl(void *opaque)RawPosixAIOData *aiocb = opaque;int ret; - ret =ioctl (aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); +do { + ret =ioctl (aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); + }while (ret ==-1 && errno ==EINTR );if (ret ==-1 ) {return -errno; }
Signature · 1 line
2.31.1
Re: [PATCH 11/11] file-posix: handle EINTR during ioctl
Quoted 15 lines · Paolo Bonzini
Similar to other handle_aiocb_* functions,
handle_aiocb_ioctlneeds to cater for the possibility that ioctl is interrupted by a signal. Otherwise, the I/O is incorrectly reported as a failure to the guest.Reported-by Gordon Watson <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- block/file-posix.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)block/file-posix.c +0
-0
block/file-posix.c static int handle_aiocb_ioctl(void *opaque) · line 1352
Quoted 7 lines
block/file-posix.c +3
-1@@ -1347,7 +1347,9 @@ static int handle_aiocb_ioctl(void *opaque)RawPosixAIOData *aiocb = opaque;int ret; - ret =ioctl (aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); +do { + ret =ioctl (aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); + }while (ret ==-1 && errno ==EINTR );
Shouldn't this use the TFR macro instead?
Quoted 4 lines
block/file-posix.c +0
-0if (ret ==-1 ) {return -errno; }
Re: [PATCH 11/11] file-posix: handle EINTR during ioctl
In reply to Paolo BonziniQuoted 15 lines · Paolo Bonzini
Similar to other handle_aiocb_* functions,
handle_aiocb_ioctlneeds to cater for the possibility that ioctl is interrupted by a signal. Otherwise, the I/O is incorrectly reported as a failure to the guest.Reported-by Gordon Watson <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- block/file-posix.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)block/file-posix.c +0
-0
block/file-posix.c static int handle_aiocb_ioctl(void *opaque) · line 1355
Quoted 10 lines
block/file-posix.c +3
-1@@ -1347,7 +1347,9 @@ static int handle_aiocb_ioctl(void *opaque)RawPosixAIOData *aiocb = opaque;int ret; - ret =ioctl (aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); +do { + ret =ioctl (aiocb->aio_fildes, aiocb->ioctl.cmd, aiocb->ioctl.buf); + }while (ret ==-1 && errno ==EINTR );if (ret ==-1 ) {return -errno; }
I think the macro TFR() from qemu-common.h could be applied here, probably like TFR(ret = ioctl(...));.
No matter:
Quoted 12 lines · Paolo Bonzini
New patches:
- 3/4 (for review comments),
- 9 (split for ease of review),
- 11 (new bugfix)
v1->v2: add missing patch
v2->v3: addmax_hw_transferto BlockLimits
v3->v4: fix compilation after patch 1, tweak commit messages according
to Vladimir's review
Quoted 6 lines
v4->v5: round downmax_transferandmax_hw_transferto request alignment
checkpatch fixes
return-ENOTSUP, -not-EIOif block limits ioctls fail handlehost_cdromlikehost_devicein QAPI
split "block: try BSD disk size ioctls one after another" new bugfix patch "file-posix: handle EINTR during ioctl"
Thanks, looks good to me, though I’m afraid I’ll be on PTO the next two weeks so I can’t take this series through my tree... (I don’t really want to send a pull request today when I probably wouldn’t be able to deal with potential problems)
Max
Quoted 1 line · Max Reitz
Thanks, looks good to me, though I’m afraid I’ll be on PTO the next two weeks so I can’t take this series through my tree... (I don’t really want to send a pull request today when I probably wouldn’t be able to deal with potential problems)
I can take it too, if it's okay for you.
Paolo
Quoted 4 lines · Paolo Bonzini
Quoted 1 line · Max Reitz
Thanks, looks good to me, though I’m afraid I’ll be on PTO the next two weeks so I can’t take this series through my tree... (I don’t really want to send a pull request today when I probably wouldn’t be able to deal with potential problems)
I can take it too, if it's okay for you.
Sure!