cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-raw-posix-Fail-gracefully-if-no-working-alignment-is.patch

05bba0
From ea0e0a4820d0dfa1227bd8351d89e9d55b25da44 Mon Sep 17 00:00:00 2001
05bba0
From: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
Date: Tue, 17 Mar 2015 12:59:01 +0100
05bba0
Subject: [PATCH 12/16] raw-posix: Fail gracefully if no working alignment is
05bba0
 found
05bba0
05bba0
Message-id: <1424365599-9801-3-git-send-email-stefanha@redhat.com>
05bba0
Patchwork-id: 63913
05bba0
O-Subject: [RHEL-7.1 qemu-kvm PATCH 2/2] raw-posix: Fail gracefully if no working alignment is found
05bba0
Bugzilla: 1184363
05bba0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
05bba0
RH-Acked-by: John Snow <jsnow@redhat.com>
05bba0
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
05bba0
05bba0
From: Kevin Wolf <kwolf@redhat.com>
05bba0
05bba0
If qemu couldn't find out what O_DIRECT alignment to use with a given
05bba0
file, it would run into assert(bdrv_opt_mem_align(bs) != 0); in block.c
05bba0
and confuse users. This adds a more descriptive error message for such
05bba0
cases.
05bba0
05bba0
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
05bba0
Reviewed-by: Eric Blake <eblake@redhat.com>
05bba0
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
(cherry picked from commit df26a35025427f34c1d4e5a8e51152371a5e231e)
05bba0
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
---
05bba0
 block/raw-posix.c | 35 +++++++++++++++++++++++++++--------
05bba0
 1 file changed, 27 insertions(+), 8 deletions(-)
05bba0
05bba0
diff --git a/block/raw-posix.c b/block/raw-posix.c
05bba0
index 6a50856..af526ca 100644
05bba0
--- a/block/raw-posix.c
05bba0
+++ b/block/raw-posix.c
05bba0
@@ -213,7 +213,7 @@ static int raw_normalize_devicepath(const char **filename)
05bba0
 }
05bba0
 #endif
05bba0
 
05bba0
-static void raw_probe_alignment(BlockDriverState *bs)
05bba0
+static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
05bba0
 {
05bba0
     BDRVRawState *s = bs->opaque;
05bba0
     char *buf;
05bba0
@@ -232,24 +232,24 @@ static void raw_probe_alignment(BlockDriverState *bs)
05bba0
     s->buf_align = 0;
05bba0
 
05bba0
 #ifdef BLKSSZGET
05bba0
-    if (ioctl(s->fd, BLKSSZGET, &sector_size) >= 0) {
05bba0
+    if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
05bba0
         bs->request_alignment = sector_size;
05bba0
     }
05bba0
 #endif
05bba0
 #ifdef DKIOCGETBLOCKSIZE
05bba0
-    if (ioctl(s->fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
05bba0
+    if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
05bba0
         bs->request_alignment = sector_size;
05bba0
     }
05bba0
 #endif
05bba0
 #ifdef DIOCGSECTORSIZE
05bba0
-    if (ioctl(s->fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
05bba0
+    if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
05bba0
         bs->request_alignment = sector_size;
05bba0
     }
05bba0
 #endif
05bba0
 #ifdef CONFIG_XFS
05bba0
     if (s->is_xfs) {
05bba0
         struct dioattr da;
05bba0
-        if (xfsctl(NULL, s->fd, XFS_IOC_DIOINFO, &da) >= 0) {
05bba0
+        if (xfsctl(NULL, fd, XFS_IOC_DIOINFO, &da) >= 0) {
05bba0
             bs->request_alignment = da.d_miniosz;
05bba0
             /* The kernel returns wrong information for d_mem */
05bba0
             /* s->buf_align = da.d_mem; */
05bba0
@@ -262,7 +262,7 @@ static void raw_probe_alignment(BlockDriverState *bs)
05bba0
         size_t align;
05bba0
         buf = qemu_memalign(MAX_BLOCKSIZE, 2 * MAX_BLOCKSIZE);
05bba0
         for (align = 512; align <= MAX_BLOCKSIZE; align <<= 1) {
05bba0
-            if (pread(s->fd, buf + align, MAX_BLOCKSIZE, 0) >= 0) {
05bba0
+            if (pread(fd, buf + align, MAX_BLOCKSIZE, 0) >= 0) {
05bba0
                 s->buf_align = align;
05bba0
                 break;
05bba0
             }
05bba0
@@ -274,13 +274,18 @@ static void raw_probe_alignment(BlockDriverState *bs)
05bba0
         size_t align;
05bba0
         buf = qemu_memalign(s->buf_align, MAX_BLOCKSIZE);
05bba0
         for (align = 512; align <= MAX_BLOCKSIZE; align <<= 1) {
05bba0
-            if (pread(s->fd, buf, align, 0) >= 0) {
05bba0
+            if (pread(fd, buf, align, 0) >= 0) {
05bba0
                 bs->request_alignment = align;
05bba0
                 break;
05bba0
             }
05bba0
         }
05bba0
         qemu_vfree(buf);
05bba0
     }
05bba0
+
05bba0
+    if (!s->buf_align || !bs->request_alignment) {
05bba0
+        error_setg(errp, "Could not find working O_DIRECT alignment. "
05bba0
+                         "Try cache.direct=off.");
05bba0
+    }
05bba0
 }
05bba0
 
05bba0
 static void raw_parse_flags(int bdrv_flags, int *open_flags)
05bba0
@@ -459,6 +464,7 @@ static int raw_reopen_prepare(BDRVReopenState *state,
05bba0
     BDRVRawState *s;
05bba0
     BDRVRawReopenState *raw_s;
05bba0
     int ret = 0;
05bba0
+    Error *local_err = NULL;
05bba0
 
05bba0
     assert(state != NULL);
05bba0
     assert(state->bs != NULL);
05bba0
@@ -531,6 +537,19 @@ static int raw_reopen_prepare(BDRVReopenState *state,
05bba0
             ret = -1;
05bba0
         }
05bba0
     }
05bba0
+
05bba0
+    /* Fail already reopen_prepare() if we can't get a working O_DIRECT
05bba0
+     * alignment with the new fd. */
05bba0
+    if (raw_s->fd != -1) {
05bba0
+        raw_probe_alignment(state->bs, raw_s->fd, &local_err);
05bba0
+        if (local_err) {
05bba0
+            qemu_close(raw_s->fd);
05bba0
+            raw_s->fd = -1;
05bba0
+            error_propagate(errp, local_err);
05bba0
+            ret = -EINVAL;
05bba0
+        }
05bba0
+    }
05bba0
+
05bba0
     return ret;
05bba0
 }
05bba0
 
05bba0
@@ -573,7 +592,7 @@ static int raw_refresh_limits(BlockDriverState *bs)
05bba0
 {
05bba0
     BDRVRawState *s = bs->opaque;
05bba0
 
05bba0
-    raw_probe_alignment(bs);
05bba0
+    raw_probe_alignment(bs, s->fd, errp);
05bba0
     bs->bl.opt_mem_alignment = s->buf_align;
05bba0
 
05bba0
     return 0;
05bba0
-- 
05bba0
1.8.3.1
05bba0