thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-file-posix-Make-.bdrv_co_truncate-asynchronous.patch

ae23c9
From f585ea7b67febdd86a2a1c9d53724a5bce625cad Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Tue, 24 Jul 2018 09:23:26 +0200
ae23c9
Subject: [PATCH 213/268] file-posix: Make .bdrv_co_truncate asynchronous
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20180712144258.17303-7-kwolf@redhat.com>
ae23c9
Patchwork-id: 81324
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 6/6] file-posix: Make .bdrv_co_truncate asynchronous
ae23c9
Bugzilla: 1595173
ae23c9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ae23c9
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ae23c9
RH-Acked-by: John Snow <jsnow@redhat.com>
ae23c9
ae23c9
This moves the code to resize an image file to the thread pool to avoid
ae23c9
blocking.
ae23c9
ae23c9
Creating large images with preallocation with blockdev-create is now
ae23c9
actually a background job instead of blocking the monitor (and most
ae23c9
other things) until the preallocation has completed.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
ae23c9
(cherry picked from commit 93f4e2ff4b31205d8bab0856631a52ed442b8b1c)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
---
ae23c9
 block/file-posix.c      | 266 +++++++++++++++++++++++++++---------------------
ae23c9
 include/block/raw-aio.h |   4 +-
ae23c9
 2 files changed, 154 insertions(+), 116 deletions(-)
ae23c9
ae23c9
diff --git a/block/file-posix.c b/block/file-posix.c
ae23c9
index f8488ec..24c2367 100644
ae23c9
--- a/block/file-posix.c
ae23c9
+++ b/block/file-posix.c
ae23c9
@@ -186,8 +186,16 @@ typedef struct RawPosixAIOData {
ae23c9
 #define aio_ioctl_cmd   aio_nbytes /* for QEMU_AIO_IOCTL */
ae23c9
     off_t aio_offset;
ae23c9
     int aio_type;
ae23c9
-    int aio_fd2;
ae23c9
-    off_t aio_offset2;
ae23c9
+    union {
ae23c9
+        struct {
ae23c9
+            int aio_fd2;
ae23c9
+            off_t aio_offset2;
ae23c9
+        };
ae23c9
+        struct {
ae23c9
+            PreallocMode prealloc;
ae23c9
+            Error **errp;
ae23c9
+        };
ae23c9
+    };
ae23c9
 } RawPosixAIOData;
ae23c9
 
ae23c9
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
ae23c9
@@ -1509,6 +1517,122 @@ static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
ae23c9
     return ret;
ae23c9
 }
ae23c9
 
ae23c9
+static int handle_aiocb_truncate(RawPosixAIOData *aiocb)
ae23c9
+{
ae23c9
+    int result = 0;
ae23c9
+    int64_t current_length = 0;
ae23c9
+    char *buf = NULL;
ae23c9
+    struct stat st;
ae23c9
+    int fd = aiocb->aio_fildes;
ae23c9
+    int64_t offset = aiocb->aio_offset;
ae23c9
+    Error **errp = aiocb->errp;
ae23c9
+
ae23c9
+    if (fstat(fd, &st) < 0) {
ae23c9
+        result = -errno;
ae23c9
+        error_setg_errno(errp, -result, "Could not stat file");
ae23c9
+        return result;
ae23c9
+    }
ae23c9
+
ae23c9
+    current_length = st.st_size;
ae23c9
+    if (current_length > offset && aiocb->prealloc != PREALLOC_MODE_OFF) {
ae23c9
+        error_setg(errp, "Cannot use preallocation for shrinking files");
ae23c9
+        return -ENOTSUP;
ae23c9
+    }
ae23c9
+
ae23c9
+    switch (aiocb->prealloc) {
ae23c9
+#ifdef CONFIG_POSIX_FALLOCATE
ae23c9
+    case PREALLOC_MODE_FALLOC:
ae23c9
+        /*
ae23c9
+         * Truncating before posix_fallocate() makes it about twice slower on
ae23c9
+         * file systems that do not support fallocate(), trying to check if a
ae23c9
+         * block is allocated before allocating it, so don't do that here.
ae23c9
+         */
ae23c9
+        if (offset != current_length) {
ae23c9
+            result = -posix_fallocate(fd, current_length,
ae23c9
+                                      offset - current_length);
ae23c9
+            if (result != 0) {
ae23c9
+                /* posix_fallocate() doesn't set errno. */
ae23c9
+                error_setg_errno(errp, -result,
ae23c9
+                                 "Could not preallocate new data");
ae23c9
+            }
ae23c9
+        } else {
ae23c9
+            result = 0;
ae23c9
+        }
ae23c9
+        goto out;
ae23c9
+#endif
ae23c9
+    case PREALLOC_MODE_FULL:
ae23c9
+    {
ae23c9
+        int64_t num = 0, left = offset - current_length;
ae23c9
+        off_t seek_result;
ae23c9
+
ae23c9
+        /*
ae23c9
+         * Knowing the final size from the beginning could allow the file
ae23c9
+         * system driver to do less allocations and possibly avoid
ae23c9
+         * fragmentation of the file.
ae23c9
+         */
ae23c9
+        if (ftruncate(fd, offset) != 0) {
ae23c9
+            result = -errno;
ae23c9
+            error_setg_errno(errp, -result, "Could not resize file");
ae23c9
+            goto out;
ae23c9
+        }
ae23c9
+
ae23c9
+        buf = g_malloc0(65536);
ae23c9
+
ae23c9
+        seek_result = lseek(fd, current_length, SEEK_SET);
ae23c9
+        if (seek_result < 0) {
ae23c9
+            result = -errno;
ae23c9
+            error_setg_errno(errp, -result,
ae23c9
+                             "Failed to seek to the old end of file");
ae23c9
+            goto out;
ae23c9
+        }
ae23c9
+
ae23c9
+        while (left > 0) {
ae23c9
+            num = MIN(left, 65536);
ae23c9
+            result = write(fd, buf, num);
ae23c9
+            if (result < 0) {
ae23c9
+                result = -errno;
ae23c9
+                error_setg_errno(errp, -result,
ae23c9
+                                 "Could not write zeros for preallocation");
ae23c9
+                goto out;
ae23c9
+            }
ae23c9
+            left -= result;
ae23c9
+        }
ae23c9
+        if (result >= 0) {
ae23c9
+            result = fsync(fd);
ae23c9
+            if (result < 0) {
ae23c9
+                result = -errno;
ae23c9
+                error_setg_errno(errp, -result,
ae23c9
+                                 "Could not flush file to disk");
ae23c9
+                goto out;
ae23c9
+            }
ae23c9
+        }
ae23c9
+        goto out;
ae23c9
+    }
ae23c9
+    case PREALLOC_MODE_OFF:
ae23c9
+        if (ftruncate(fd, offset) != 0) {
ae23c9
+            result = -errno;
ae23c9
+            error_setg_errno(errp, -result, "Could not resize file");
ae23c9
+        }
ae23c9
+        return result;
ae23c9
+    default:
ae23c9
+        result = -ENOTSUP;
ae23c9
+        error_setg(errp, "Unsupported preallocation mode: %s",
ae23c9
+                   PreallocMode_str(aiocb->prealloc));
ae23c9
+        return result;
ae23c9
+    }
ae23c9
+
ae23c9
+out:
ae23c9
+    if (result < 0) {
ae23c9
+        if (ftruncate(fd, current_length) < 0) {
ae23c9
+            error_report("Failed to restore old file length: %s",
ae23c9
+                         strerror(errno));
ae23c9
+        }
ae23c9
+    }
ae23c9
+
ae23c9
+    g_free(buf);
ae23c9
+    return result;
ae23c9
+}
ae23c9
+
ae23c9
 static int aio_worker(void *arg)
ae23c9
 {
ae23c9
     RawPosixAIOData *aiocb = arg;
ae23c9
@@ -1552,6 +1676,9 @@ static int aio_worker(void *arg)
ae23c9
     case QEMU_AIO_COPY_RANGE:
ae23c9
         ret = handle_aiocb_copy_range(aiocb);
ae23c9
         break;
ae23c9
+    case QEMU_AIO_TRUNCATE:
ae23c9
+        ret = handle_aiocb_truncate(aiocb);
ae23c9
+        break;
ae23c9
     default:
ae23c9
         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
ae23c9
         ret = -EINVAL;
ae23c9
@@ -1719,117 +1846,25 @@ static void raw_close(BlockDriverState *bs)
ae23c9
  *
ae23c9
  * Returns: 0 on success, -errno on failure.
ae23c9
  */
ae23c9
-static int raw_regular_truncate(int fd, int64_t offset, PreallocMode prealloc,
ae23c9
-                                Error **errp)
ae23c9
+static int coroutine_fn
ae23c9
+raw_regular_truncate(BlockDriverState *bs, int fd, int64_t offset,
ae23c9
+                     PreallocMode prealloc, Error **errp)
ae23c9
 {
ae23c9
-    int result = 0;
ae23c9
-    int64_t current_length = 0;
ae23c9
-    char *buf = NULL;
ae23c9
-    struct stat st;
ae23c9
-
ae23c9
-    if (fstat(fd, &st) < 0) {
ae23c9
-        result = -errno;
ae23c9
-        error_setg_errno(errp, -result, "Could not stat file");
ae23c9
-        return result;
ae23c9
-    }
ae23c9
-
ae23c9
-    current_length = st.st_size;
ae23c9
-    if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
ae23c9
-        error_setg(errp, "Cannot use preallocation for shrinking files");
ae23c9
-        return -ENOTSUP;
ae23c9
-    }
ae23c9
-
ae23c9
-    switch (prealloc) {
ae23c9
-#ifdef CONFIG_POSIX_FALLOCATE
ae23c9
-    case PREALLOC_MODE_FALLOC:
ae23c9
-        /*
ae23c9
-         * Truncating before posix_fallocate() makes it about twice slower on
ae23c9
-         * file systems that do not support fallocate(), trying to check if a
ae23c9
-         * block is allocated before allocating it, so don't do that here.
ae23c9
-         */
ae23c9
-        if (offset != current_length) {
ae23c9
-            result = -posix_fallocate(fd, current_length, offset - current_length);
ae23c9
-            if (result != 0) {
ae23c9
-                /* posix_fallocate() doesn't set errno. */
ae23c9
-                error_setg_errno(errp, -result,
ae23c9
-                                 "Could not preallocate new data");
ae23c9
-            }
ae23c9
-        } else {
ae23c9
-            result = 0;
ae23c9
-        }
ae23c9
-        goto out;
ae23c9
-#endif
ae23c9
-    case PREALLOC_MODE_FULL:
ae23c9
-    {
ae23c9
-        int64_t num = 0, left = offset - current_length;
ae23c9
-        off_t seek_result;
ae23c9
-
ae23c9
-        /*
ae23c9
-         * Knowing the final size from the beginning could allow the file
ae23c9
-         * system driver to do less allocations and possibly avoid
ae23c9
-         * fragmentation of the file.
ae23c9
-         */
ae23c9
-        if (ftruncate(fd, offset) != 0) {
ae23c9
-            result = -errno;
ae23c9
-            error_setg_errno(errp, -result, "Could not resize file");
ae23c9
-            goto out;
ae23c9
-        }
ae23c9
-
ae23c9
-        buf = g_malloc0(65536);
ae23c9
-
ae23c9
-        seek_result = lseek(fd, current_length, SEEK_SET);
ae23c9
-        if (seek_result < 0) {
ae23c9
-            result = -errno;
ae23c9
-            error_setg_errno(errp, -result,
ae23c9
-                             "Failed to seek to the old end of file");
ae23c9
-            goto out;
ae23c9
-        }
ae23c9
-
ae23c9
-        while (left > 0) {
ae23c9
-            num = MIN(left, 65536);
ae23c9
-            result = write(fd, buf, num);
ae23c9
-            if (result < 0) {
ae23c9
-                result = -errno;
ae23c9
-                error_setg_errno(errp, -result,
ae23c9
-                                 "Could not write zeros for preallocation");
ae23c9
-                goto out;
ae23c9
-            }
ae23c9
-            left -= result;
ae23c9
-        }
ae23c9
-        if (result >= 0) {
ae23c9
-            result = fsync(fd);
ae23c9
-            if (result < 0) {
ae23c9
-                result = -errno;
ae23c9
-                error_setg_errno(errp, -result,
ae23c9
-                                 "Could not flush file to disk");
ae23c9
-                goto out;
ae23c9
-            }
ae23c9
-        }
ae23c9
-        goto out;
ae23c9
-    }
ae23c9
-    case PREALLOC_MODE_OFF:
ae23c9
-        if (ftruncate(fd, offset) != 0) {
ae23c9
-            result = -errno;
ae23c9
-            error_setg_errno(errp, -result, "Could not resize file");
ae23c9
-        }
ae23c9
-        return result;
ae23c9
-    default:
ae23c9
-        result = -ENOTSUP;
ae23c9
-        error_setg(errp, "Unsupported preallocation mode: %s",
ae23c9
-                   PreallocMode_str(prealloc));
ae23c9
-        return result;
ae23c9
-    }
ae23c9
+    RawPosixAIOData *acb = g_new(RawPosixAIOData, 1);
ae23c9
+    ThreadPool *pool;
ae23c9
 
ae23c9
-out:
ae23c9
-    if (result < 0) {
ae23c9
-        if (ftruncate(fd, current_length) < 0) {
ae23c9
-            error_report("Failed to restore old file length: %s",
ae23c9
-                         strerror(errno));
ae23c9
-        }
ae23c9
-    }
ae23c9
+    *acb = (RawPosixAIOData) {
ae23c9
+        .bs             = bs,
ae23c9
+        .aio_fildes     = fd,
ae23c9
+        .aio_type       = QEMU_AIO_TRUNCATE,
ae23c9
+        .aio_offset     = offset,
ae23c9
+        .prealloc       = prealloc,
ae23c9
+        .errp           = errp,
ae23c9
+    };
ae23c9
 
ae23c9
-    g_free(buf);
ae23c9
-    return result;
ae23c9
+    /* @bs can be NULL, bdrv_get_aio_context() returns the main context then */
ae23c9
+    pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
ae23c9
+    return thread_pool_submit_co(pool, aio_worker, acb);
ae23c9
 }
ae23c9
 
ae23c9
 static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
ae23c9
@@ -1846,7 +1881,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
ae23c9
     }
ae23c9
 
ae23c9
     if (S_ISREG(st.st_mode)) {
ae23c9
-        return raw_regular_truncate(s->fd, offset, prealloc, errp);
ae23c9
+        return raw_regular_truncate(bs, s->fd, offset, prealloc, errp);
ae23c9
     }
ae23c9
 
ae23c9
     if (prealloc != PREALLOC_MODE_OFF) {
ae23c9
@@ -2048,7 +2083,8 @@ static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
ae23c9
     return (int64_t)st.st_blocks * 512;
ae23c9
 }
ae23c9
 
ae23c9
-static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
+static int coroutine_fn
ae23c9
+raw_co_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
 {
ae23c9
     BlockdevCreateOptionsFile *file_opts;
ae23c9
     Error *local_err = NULL;
ae23c9
@@ -2101,7 +2137,7 @@ static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
     }
ae23c9
 
ae23c9
     /* Clear the file by truncating it to 0 */
ae23c9
-    result = raw_regular_truncate(fd, 0, PREALLOC_MODE_OFF, errp);
ae23c9
+    result = raw_regular_truncate(NULL, fd, 0, PREALLOC_MODE_OFF, errp);
ae23c9
     if (result < 0) {
ae23c9
         goto out_unlock;
ae23c9
     }
ae23c9
@@ -2123,8 +2159,8 @@ static int raw_co_create(BlockdevCreateOptions *options, Error **errp)
ae23c9
 
ae23c9
     /* Resize and potentially preallocate the file to the desired
ae23c9
      * final size */
ae23c9
-    result = raw_regular_truncate(fd, file_opts->size, file_opts->preallocation,
ae23c9
-                                  errp);
ae23c9
+    result = raw_regular_truncate(NULL, fd, file_opts->size,
ae23c9
+                                  file_opts->preallocation, errp);
ae23c9
     if (result < 0) {
ae23c9
         goto out_unlock;
ae23c9
     }
ae23c9
diff --git a/include/block/raw-aio.h b/include/block/raw-aio.h
ae23c9
index 3240530..2ffcd9d 100644
ae23c9
--- a/include/block/raw-aio.h
ae23c9
+++ b/include/block/raw-aio.h
ae23c9
@@ -26,6 +26,7 @@
ae23c9
 #define QEMU_AIO_DISCARD      0x0010
ae23c9
 #define QEMU_AIO_WRITE_ZEROES 0x0020
ae23c9
 #define QEMU_AIO_COPY_RANGE   0x0040
ae23c9
+#define QEMU_AIO_TRUNCATE     0x0080
ae23c9
 #define QEMU_AIO_TYPE_MASK \
ae23c9
         (QEMU_AIO_READ | \
ae23c9
          QEMU_AIO_WRITE | \
ae23c9
@@ -33,7 +34,8 @@
ae23c9
          QEMU_AIO_FLUSH | \
ae23c9
          QEMU_AIO_DISCARD | \
ae23c9
          QEMU_AIO_WRITE_ZEROES | \
ae23c9
-         QEMU_AIO_COPY_RANGE)
ae23c9
+         QEMU_AIO_COPY_RANGE | \
ae23c9
+         QEMU_AIO_TRUNCATE)
ae23c9
 
ae23c9
 /* AIO flags */
ae23c9
 #define QEMU_AIO_MISALIGNED   0x1000
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9