* [PATCH 1/5] file-posix: try BLKSECTGET on block devices too, do not round to power of 2
2021-05-18 8:38 [PATCH 0/5] block: file-posix queue Paolo Bonzini
@ 2021-05-18 8:38 ` Paolo Bonzini
2021-05-18 8:38 ` [PATCH 2/5] file-posix: fix max_iov for /dev/sg devices Paolo Bonzini
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2021-05-18 8:38 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block
bs->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.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
block/file-posix.c | 40 ++++++++++++++++++++++------------------
1 file changed, 22 insertions(+), 18 deletions(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index 59c889d5a7..e5ef006aee 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -1149,22 +1149,27 @@ static void raw_reopen_abort(BDRVReopenState *state)
s->reopen_state = NULL;
}
-static int sg_get_max_transfer_length(int fd)
+static int sg_get_max_transfer_length(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 sg_get_max_segments(int fd, struct stat *st)
{
#ifdef CONFIG_LINUX
char buf[32];
@@ -1173,15 +1178,9 @@ 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;
- }
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;
@@ -1218,15 +1217,20 @@ out:
static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
{
BDRVRawState *s = bs->opaque;
+ struct stat st;
+
+ if (fstat(s->fd, &st)) {
+ return;
+ }
- if (bs->sg) {
- int ret = sg_get_max_transfer_length(s->fd);
+ if (bs->sg || S_ISBLK(st.st_mode)) {
+ int ret = sg_get_max_transfer_length(s->fd, &st);
if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
- bs->bl.max_transfer = pow2floor(ret);
+ bs->bl.max_transfer = ret;
}
- ret = sg_get_max_segments(s->fd);
+ ret = sg_get_max_segments(s->fd, &st);
if (ret > 0) {
bs->bl.max_iov = ret;
}
--
2.31.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/5] block: feature detection for host block support
2021-05-18 8:38 [PATCH 0/5] block: file-posix queue Paolo Bonzini
2021-05-18 8:38 ` [PATCH 1/5] file-posix: try BLKSECTGET on block devices too, do not round to power of 2 Paolo Bonzini
2021-05-18 8:38 ` [PATCH 2/5] file-posix: fix max_iov for /dev/sg devices Paolo Bonzini
@ 2021-05-18 8:38 ` Paolo Bonzini
2021-05-18 8:39 ` [PATCH 4/5] block: check for sys/disk.h Paolo Bonzini
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2021-05-18 8:38 UTC (permalink / raw)
To: qemu-devel; +Cc: Joelle van Dyne, qemu-block
From: Joelle van Dyne <j@getutm.app>
On Darwin (iOS), there are no system level APIs for directly accessing
host block devices. We detect this at configure time.
Signed-off-by: Joelle van Dyne <j@getutm.app>
Message-Id: <20210315180341.31638-2-j@getutm.app>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
block/file-posix.c | 33 ++++++++++++++++++++++-----------
meson.build | 6 +++++-
qapi/block-core.json | 10 +++++++---
3 files changed, 34 insertions(+), 15 deletions(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index 77a45083a6..e83f34a960 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -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__
@@ -180,7 +183,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 {
@@ -3017,6 +3030,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);
@@ -3026,6 +3040,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",
@@ -3241,6 +3256,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);
@@ -3533,16 +3550,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 int
hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
{
@@ -3866,6 +3873,8 @@ static BlockDriver bdrv_host_cdrom = {
};
#endif /* __FreeBSD__ */
+#endif /* HAVE_HOST_BLOCK_DEVICE */
+
static void bdrv_file_init(void)
{
/*
@@ -3873,6 +3882,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);
@@ -3880,6 +3890,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);
diff --git a/meson.build b/meson.build
index 5cf0dce695..b3cc45c41d 100644
--- a/meson.build
+++ b/meson.build
@@ -182,7 +182,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'),
@@ -1067,6 +1067,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 #
#################
@@ -1160,6 +1163,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>'))
diff --git a/qapi/block-core.json b/qapi/block-core.json
index 2ea294129e..2dd41be156 100644
--- a/qapi/block-core.json
+++ b/qapi/block-core.json
@@ -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,9 @@
{ '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', 'host_cdrom',
+ {'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)' },
@@ -3996,7 +3999,8 @@
'ftps': 'BlockdevOptionsCurlFtps',
'gluster': 'BlockdevOptionsGluster',
'host_cdrom': 'BlockdevOptionsFile',
- 'host_device':'BlockdevOptionsFile',
+ 'host_device': { 'type': 'BlockdevOptionsFile',
+ 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' },
'http': 'BlockdevOptionsCurlHttp',
'https': 'BlockdevOptionsCurlHttps',
'iscsi': 'BlockdevOptionsIscsi',
--
2.31.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 5/5] block: detect DKIOCGETBLOCKCOUNT/SIZE before use
2021-05-18 8:38 [PATCH 0/5] block: file-posix queue Paolo Bonzini
` (3 preceding siblings ...)
2021-05-18 8:39 ` [PATCH 4/5] block: check for sys/disk.h Paolo Bonzini
@ 2021-05-18 8:39 ` Paolo Bonzini
2021-05-18 13:23 ` [PATCH 0/5] block: file-posix queue Kevin Wolf
5 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2021-05-18 8:39 UTC (permalink / raw)
To: qemu-devel; +Cc: Warner Losh, Joelle van Dyne, qemu-block, Peter Maydell
From: Joelle van Dyne <j@getutm.app>
iOS hosts do not have these defined so we fallback to the
default behaviour.
Co-authored-by: Warner Losh <imp@bsdimp.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Joelle van Dyne <j@getutm.app>
Message-Id: <20210315180341.31638-4-j@getutm.app>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
block/file-posix.c | 21 +++++++++------------
1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index e83f34a960..34bd413553 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -2311,8 +2311,11 @@ static int64_t raw_getlength(BlockDriverState *bs)
again:
#endif
if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+ size = 0;
#ifdef DIOCGMEDIASIZE
- if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+ if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) {
+ size = 0;
+ }
#elif defined(DIOCGPART)
{
struct partinfo pi;
@@ -2321,9 +2324,7 @@ again:
else
size = 0;
}
- if (size == 0)
-#endif
-#if defined(__APPLE__) && defined(__MACH__)
+#elif defined(DKIOCGETBLOCKCOUNT) && defined(DKIOCGETBLOCKSIZE)
{
uint64_t sectors = 0;
uint32_t sector_size = 0;
@@ -2331,19 +2332,15 @@ again:
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:
--
2.31.1
^ permalink raw reply related [flat|nested] 8+ messages in thread