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

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