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

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