76daa3
From 50cca99ecdaeb3dd64ff8af220915dc15a2692ec Mon Sep 17 00:00:00 2001
76daa3
From: Jeffrey Cody <jcody@redhat.com>
76daa3
Date: Mon, 5 Jun 2017 16:12:29 +0200
76daa3
Subject: [PATCH 10/13] gluster: add support for PREALLOC_MODE_FALLOC
76daa3
76daa3
RH-Author: Jeffrey Cody <jcody@redhat.com>
76daa3
Message-id: <2e9189b1b1d970dc78a6ad2bcf2fabb3955172d4.1496678872.git.jcody@redhat.com>
76daa3
Patchwork-id: 75484
76daa3
O-Subject: [RHEV-7.4 qemu-kvm-rhev PATCH] gluster: add support for PREALLOC_MODE_FALLOC
76daa3
Bugzilla: 1450759
76daa3
RH-Acked-by: John Snow <jsnow@redhat.com>
76daa3
RH-Acked-by: Niels de Vos <ndevos@redhat.com>
76daa3
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
76daa3
76daa3
From: Niels de Vos <ndevos@redhat.com>
76daa3
76daa3
Add missing support for "preallocation=falloc" to the Gluster block
76daa3
driver. This change bases its logic on that of block/file-posix.c and
76daa3
removed the gluster_supports_zerofill() and qemu_gluster_zerofill()
76daa3
functions in favour of #ifdef checks in an easy to read
76daa3
switch-statement.
76daa3
76daa3
Both glfs_zerofill() and glfs_fallocate() have been introduced with
76daa3
GlusterFS 3.5.0 (pkg-config glusterfs-api = 6). A #define for the
76daa3
availability of glfs_fallocate() has been added to ./configure.
76daa3
76daa3
Reported-by: Satheesaran Sundaramoorthi <sasundar@redhat.com>
76daa3
Signed-off-by: Niels de Vos <ndevos@redhat.com>
76daa3
Message-id: 20170528063114.28691-1-ndevos@redhat.com
76daa3
URL: https://bugzilla.redhat.com/1450759
76daa3
Signed-off-by: Niels de Vos <ndevos@redhat.com>
76daa3
Signed-off-by: Jeff Cody <jcody@redhat.com>
76daa3
(cherry picked from commit df3a429ae82c0f45becdfab105617701d75e0f05)
76daa3
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
76daa3
---
76daa3
 block/gluster.c | 76 ++++++++++++++++++++++++++++++---------------------------
76daa3
 configure       |  6 +++++
76daa3
 2 files changed, 46 insertions(+), 36 deletions(-)
76daa3
76daa3
diff --git a/block/gluster.c b/block/gluster.c
76daa3
index 6ea91ef..52a1c1d 100644
76daa3
--- a/block/gluster.c
76daa3
+++ b/block/gluster.c
76daa3
@@ -964,29 +964,6 @@ static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs,
76daa3
     qemu_coroutine_yield();
76daa3
     return acb.ret;
76daa3
 }
76daa3
-
76daa3
-static inline bool gluster_supports_zerofill(void)
76daa3
-{
76daa3
-    return 1;
76daa3
-}
76daa3
-
76daa3
-static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
76daa3
-                                        int64_t size)
76daa3
-{
76daa3
-    return glfs_zerofill(fd, offset, size);
76daa3
-}
76daa3
-
76daa3
-#else
76daa3
-static inline bool gluster_supports_zerofill(void)
76daa3
-{
76daa3
-    return 0;
76daa3
-}
76daa3
-
76daa3
-static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
76daa3
-                                        int64_t size)
76daa3
-{
76daa3
-    return 0;
76daa3
-}
76daa3
 #endif
76daa3
 
76daa3
 static int qemu_gluster_create(const char *filename,
76daa3
@@ -996,9 +973,10 @@ static int qemu_gluster_create(const char *filename,
76daa3
     struct glfs *glfs;
76daa3
     struct glfs_fd *fd;
76daa3
     int ret = 0;
76daa3
-    int prealloc = 0;
76daa3
+    PreallocMode prealloc;
76daa3
     int64_t total_size = 0;
76daa3
     char *tmp = NULL;
76daa3
+    Error *local_err = NULL;
76daa3
 
76daa3
     gconf = g_new0(BlockdevOptionsGluster, 1);
76daa3
     gconf->debug = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG,
76daa3
@@ -1026,13 +1004,12 @@ static int qemu_gluster_create(const char *filename,
76daa3
                           BDRV_SECTOR_SIZE);
76daa3
 
76daa3
     tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
76daa3
-    if (!tmp || !strcmp(tmp, "off")) {
76daa3
-        prealloc = 0;
76daa3
-    } else if (!strcmp(tmp, "full") && gluster_supports_zerofill()) {
76daa3
-        prealloc = 1;
76daa3
-    } else {
76daa3
-        error_setg(errp, "Invalid preallocation mode: '%s'"
76daa3
-                         " or GlusterFS doesn't support zerofill API", tmp);
76daa3
+    prealloc = qapi_enum_parse(PreallocMode_lookup, tmp,
76daa3
+                               PREALLOC_MODE__MAX, PREALLOC_MODE_OFF,
76daa3
+                               &local_err);
76daa3
+    g_free(tmp);
76daa3
+    if (local_err) {
76daa3
+        error_propagate(errp, local_err);
76daa3
         ret = -EINVAL;
76daa3
         goto out;
76daa3
     }
76daa3
@@ -1041,21 +1018,48 @@ static int qemu_gluster_create(const char *filename,
76daa3
                     O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
76daa3
     if (!fd) {
76daa3
         ret = -errno;
76daa3
-    } else {
76daa3
+        goto out;
76daa3
+    }
76daa3
+
76daa3
+    switch (prealloc) {
76daa3
+#ifdef CONFIG_GLUSTERFS_FALLOCATE
76daa3
+    case PREALLOC_MODE_FALLOC:
76daa3
+        if (glfs_fallocate(fd, 0, 0, total_size)) {
76daa3
+            error_setg(errp, "Could not preallocate data for the new file");
76daa3
+            ret = -errno;
76daa3
+        }
76daa3
+        break;
76daa3
+#endif /* CONFIG_GLUSTERFS_FALLOCATE */
76daa3
+#ifdef CONFIG_GLUSTERFS_ZEROFILL
76daa3
+    case PREALLOC_MODE_FULL:
76daa3
         if (!glfs_ftruncate(fd, total_size)) {
76daa3
-            if (prealloc && qemu_gluster_zerofill(fd, 0, total_size)) {
76daa3
+            if (glfs_zerofill(fd, 0, total_size)) {
76daa3
+                error_setg(errp, "Could not zerofill the new file");
76daa3
                 ret = -errno;
76daa3
             }
76daa3
         } else {
76daa3
+            error_setg(errp, "Could not resize file");
76daa3
             ret = -errno;
76daa3
         }
76daa3
-
76daa3
-        if (glfs_close(fd) != 0) {
76daa3
+        break;
76daa3
+#endif /* CONFIG_GLUSTERFS_ZEROFILL */
76daa3
+    case PREALLOC_MODE_OFF:
76daa3
+        if (glfs_ftruncate(fd, total_size) != 0) {
76daa3
             ret = -errno;
76daa3
+            error_setg(errp, "Could not resize file");
76daa3
         }
76daa3
+        break;
76daa3
+    default:
76daa3
+        ret = -EINVAL;
76daa3
+        error_setg(errp, "Unsupported preallocation mode: %s",
76daa3
+                   PreallocMode_lookup[prealloc]);
76daa3
+        break;
76daa3
+    }
76daa3
+
76daa3
+    if (glfs_close(fd) != 0) {
76daa3
+        ret = -errno;
76daa3
     }
76daa3
 out:
76daa3
-    g_free(tmp);
76daa3
     qapi_free_BlockdevOptionsGluster(gconf);
76daa3
     glfs_clear_preopened(glfs);
76daa3
     return ret;
76daa3
diff --git a/configure b/configure
76daa3
index 2ca95f4..b019b33 100755
76daa3
--- a/configure
76daa3
+++ b/configure
76daa3
@@ -300,6 +300,7 @@ seccomp=""
76daa3
 glusterfs=""
76daa3
 glusterfs_xlator_opt="no"
76daa3
 glusterfs_discard="no"
76daa3
+glusterfs_fallocate="no"
76daa3
 glusterfs_zerofill="no"
76daa3
 gtk=""
76daa3
 gtkabi=""
76daa3
@@ -3543,6 +3544,7 @@ if test "$glusterfs" != "no" ; then
76daa3
       glusterfs_discard="yes"
76daa3
     fi
76daa3
     if $pkg_config --atleast-version=6 glusterfs-api; then
76daa3
+      glusterfs_fallocate="yes"
76daa3
       glusterfs_zerofill="yes"
76daa3
     fi
76daa3
   else
76daa3
@@ -5707,6 +5709,10 @@ if test "$glusterfs_discard" = "yes" ; then
76daa3
   echo "CONFIG_GLUSTERFS_DISCARD=y" >> $config_host_mak
76daa3
 fi
76daa3
 
76daa3
+if test "$glusterfs_fallocate" = "yes" ; then
76daa3
+  echo "CONFIG_GLUSTERFS_FALLOCATE=y" >> $config_host_mak
76daa3
+fi
76daa3
+
76daa3
 if test "$glusterfs_zerofill" = "yes" ; then
76daa3
   echo "CONFIG_GLUSTERFS_ZEROFILL=y" >> $config_host_mak
76daa3
 fi
76daa3
-- 
76daa3
1.8.3.1
76daa3