thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-file-posix-try-BLKSECTGET-on-block-devices-too-do-no.patch

a83cc2
From 9c8493d3a6d2e4d879d1ef67ff1abebd532c87a0 Mon Sep 17 00:00:00 2001
a83cc2
From: Paolo Bonzini <pbonzini@redhat.com>
a83cc2
Date: Fri, 16 Jul 2021 16:51:34 -0400
a83cc2
Subject: [PATCH 18/43] file-posix: try BLKSECTGET on block devices too, do not
a83cc2
 round to power of 2
a83cc2
a83cc2
RH-Author: Miroslav Rezanina <mrezanin@redhat.com>
a83cc2
RH-Bugzilla: 1957194
a83cc2
a83cc2
bs->sg is only true for character devices, but block devices can also
a83cc2
be used with scsi-block and scsi-generic.  Unfortunately BLKSECTGET
a83cc2
returns bytes in an int for /dev/sgN devices, and sectors in a short
a83cc2
for block devices, so account for that in the code.
a83cc2
a83cc2
The maximum transfer also need not be a power of 2 (for example I have
a83cc2
seen disks with 1280 KiB maximum transfer) so there's no need to pass
a83cc2
the result through pow2floor.
a83cc2
a83cc2
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
a83cc2
(cherry picked from commit 18473467d55a20d643b6c9b3a52de42f705b4d35)
a83cc2
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
a83cc2
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
a83cc2
---
a83cc2
 block/file-posix.c | 57 +++++++++++++++++++++++++++-------------------
a83cc2
 1 file changed, 33 insertions(+), 24 deletions(-)
a83cc2
a83cc2
diff --git a/block/file-posix.c b/block/file-posix.c
a83cc2
index 44325a635d..7b4ebf65d5 100644
a83cc2
--- a/block/file-posix.c
a83cc2
+++ b/block/file-posix.c
a83cc2
@@ -1173,22 +1173,27 @@ static void raw_reopen_abort(BDRVReopenState *state)
a83cc2
     s->reopen_state = NULL;
a83cc2
 }
a83cc2
 
a83cc2
-static int sg_get_max_transfer_length(int fd)
a83cc2
+static int hdev_get_max_hw_transfer(int fd, struct stat *st)
a83cc2
 {
a83cc2
 #ifdef BLKSECTGET
a83cc2
-    int max_bytes = 0;
a83cc2
-
a83cc2
-    if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
a83cc2
-        return max_bytes;
a83cc2
+    if (S_ISBLK(st->st_mode)) {
a83cc2
+        unsigned short max_sectors = 0;
a83cc2
+        if (ioctl(fd, BLKSECTGET, &max_sectors) == 0) {
a83cc2
+            return max_sectors * 512;
a83cc2
+        }
a83cc2
     } else {
a83cc2
-        return -errno;
a83cc2
+        int max_bytes = 0;
a83cc2
+        if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
a83cc2
+            return max_bytes;
a83cc2
+        }
a83cc2
     }
a83cc2
+    return -errno;
a83cc2
 #else
a83cc2
     return -ENOSYS;
a83cc2
 #endif
a83cc2
 }
a83cc2
 
a83cc2
-static int sg_get_max_segments(int fd)
a83cc2
+static int hdev_get_max_segments(int fd, struct stat *st)
a83cc2
 {
a83cc2
 #ifdef CONFIG_LINUX
a83cc2
     char buf[32];
a83cc2
@@ -1197,26 +1202,20 @@ static int sg_get_max_segments(int fd)
a83cc2
     int ret;
a83cc2
     int sysfd = -1;
a83cc2
     long max_segments;
a83cc2
-    struct stat st;
a83cc2
-
a83cc2
-    if (fstat(fd, &st)) {
a83cc2
-        ret = -errno;
a83cc2
-        goto out;
a83cc2
-    }
a83cc2
 
a83cc2
-    if (S_ISCHR(st.st_mode)) {
a83cc2
+    if (S_ISCHR(st->st_mode)) {
a83cc2
         if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
a83cc2
             return ret;
a83cc2
         }
a83cc2
         return -ENOTSUP;
a83cc2
     }
a83cc2
 
a83cc2
-    if (!S_ISBLK(st.st_mode)) {
a83cc2
+    if (!S_ISBLK(st->st_mode)) {
a83cc2
         return -ENOTSUP;
a83cc2
     }
a83cc2
 
a83cc2
     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
a83cc2
-                                major(st.st_rdev), minor(st.st_rdev));
a83cc2
+                                major(st->st_rdev), minor(st->st_rdev));
a83cc2
     sysfd = open(sysfspath, O_RDONLY);
a83cc2
     if (sysfd == -1) {
a83cc2
         ret = -errno;
a83cc2
@@ -1253,23 +1252,33 @@ out:
a83cc2
 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
a83cc2
 {
a83cc2
     BDRVRawState *s = bs->opaque;
a83cc2
+    struct stat st;
a83cc2
 
a83cc2
-    if (bs->sg) {
a83cc2
-        int ret = sg_get_max_transfer_length(s->fd);
a83cc2
+    raw_probe_alignment(bs, s->fd, errp);
a83cc2
+    bs->bl.min_mem_alignment = s->buf_align;
a83cc2
+    bs->bl.opt_mem_alignment = MAX(s->buf_align, qemu_real_host_page_size);
a83cc2
+
a83cc2
+    /*
a83cc2
+     * Maximum transfers are best effort, so it is okay to ignore any
a83cc2
+     * errors.  That said, based on the man page errors in fstat would be
a83cc2
+     * very much unexpected; the only possible case seems to be ENOMEM.
a83cc2
+     */
a83cc2
+    if (fstat(s->fd, &st)) {
a83cc2
+        return;
a83cc2
+    }
a83cc2
+
a83cc2
+    if (bs->sg || S_ISBLK(st.st_mode)) {
a83cc2
+        int ret = hdev_get_max_hw_transfer(s->fd, &st);
a83cc2
 
a83cc2
         if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
a83cc2
-            bs->bl.max_hw_transfer = pow2floor(ret);
a83cc2
+            bs->bl.max_hw_transfer = ret;
a83cc2
         }
a83cc2
 
a83cc2
-        ret = sg_get_max_segments(s->fd);
a83cc2
+        ret = hdev_get_max_segments(s->fd, &st);
a83cc2
         if (ret > 0) {
a83cc2
             bs->bl.max_iov = ret;
a83cc2
         }
a83cc2
     }
a83cc2
-
a83cc2
-    raw_probe_alignment(bs, s->fd, errp);
a83cc2
-    bs->bl.min_mem_alignment = s->buf_align;
a83cc2
-    bs->bl.opt_mem_alignment = MAX(s->buf_align, qemu_real_host_page_size);
a83cc2
 }
a83cc2
 
a83cc2
 static int check_for_dasd(int fd)
a83cc2
-- 
a83cc2
2.27.0
a83cc2