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

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