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

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