e3c68b
From 0c485548b4126ed907dec9941209b1b1312d0b5d Mon Sep 17 00:00:00 2001
e3c68b
From: Sheetal Pamecha <spamecha@redhat.com>
e3c68b
Date: Wed, 19 Jun 2019 15:08:58 +0530
e3c68b
Subject: [PATCH 215/221] posix: modify storage.reserve option to take size and
e3c68b
 percent
e3c68b
e3c68b
* reverting changes made in
e3c68b
https://review.gluster.org/#/c/glusterfs/+/21686/
e3c68b
e3c68b
* Now storage.reserve can take value in percent or bytes
e3c68b
e3c68b
> fixes: bz#1651445
e3c68b
> Change-Id: Id4826210ec27991c55b17d1fecd90356bff3e036
e3c68b
> Signed-off-by: Sheetal Pamecha <spamecha@redhat.com>
e3c68b
> Cherry pick from commit 5cbc87d8b8f1287e81c38b793b8d13b057208c62
e3c68b
> Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/22900/
e3c68b
e3c68b
BUG: 1573077
e3c68b
Change-Id: Id4826210ec27991c55b17d1fecd90356bff3e036
e3c68b
Signed-off-by: Sheetal Pamecha <spamecha@redhat.com>
e3c68b
Reviewed-on: https://code.engineering.redhat.com/gerrit/174744
e3c68b
Tested-by: RHGS Build Bot <nigelb@redhat.com>
e3c68b
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
e3c68b
---
e3c68b
 tests/bugs/posix/bug-1651445.t                  | 29 +++++++++-------------
e3c68b
 xlators/mgmt/glusterd/src/glusterd-volume-set.c | 33 -------------------------
e3c68b
 xlators/storage/posix/src/posix-common.c        | 33 +++++++++++--------------
e3c68b
 xlators/storage/posix/src/posix-helpers.c       | 26 +++++++++----------
e3c68b
 xlators/storage/posix/src/posix-inode-fd-ops.c  | 15 ++++++-----
e3c68b
 xlators/storage/posix/src/posix.h               |  4 +--
e3c68b
 6 files changed, 51 insertions(+), 89 deletions(-)
e3c68b
e3c68b
diff --git a/tests/bugs/posix/bug-1651445.t b/tests/bugs/posix/bug-1651445.t
e3c68b
index f6f1833..5248d47 100644
e3c68b
--- a/tests/bugs/posix/bug-1651445.t
e3c68b
+++ b/tests/bugs/posix/bug-1651445.t
e3c68b
@@ -17,39 +17,34 @@ TEST $CLI volume start $V0
e3c68b
 
e3c68b
 TEST glusterfs --volfile-id=/$V0 --volfile-server=$H0 $M0
e3c68b
 
e3c68b
-TEST $CLI volume set $V0 storage.reserve-size 10MB
e3c68b
+#Setting the size in bytes
e3c68b
+TEST $CLI volume set $V0 storage.reserve 40MB
e3c68b
 
e3c68b
-#No effect as priority to reserve-size
e3c68b
-TEST $CLI volume set $V0 storage.reserve 20
e3c68b
+#wait 5s to reset disk_space_full flag
e3c68b
+sleep 5
e3c68b
 
e3c68b
 TEST dd if=/dev/zero of=$M0/a bs=100M count=1
e3c68b
-sleep 5
e3c68b
+TEST dd if=/dev/zero of=$M0/b bs=10M count=1
e3c68b
 
e3c68b
-#Below dd confirms posix is giving priority to reserve-size
e3c68b
-TEST dd if=/dev/zero of=$M0/b bs=40M count=1
e3c68b
+# Wait 5s to update disk_space_full flag because thread check disk space
e3c68b
+# after every 5s
e3c68b
 
e3c68b
 sleep 5
e3c68b
+# setup_lvm create lvm partition of 150M and 40M are reserve so after
e3c68b
+# consuming more than 110M next dd should fail
e3c68b
 TEST ! dd if=/dev/zero of=$M0/c bs=5M count=1
e3c68b
 
e3c68b
 rm -rf $M0/*
e3c68b
-#Size will reserve from the previously set reserve option = 20%
e3c68b
-TEST $CLI volume set $V0 storage.reserve-size 0
e3c68b
 
e3c68b
-#Overwrite reserve option
e3c68b
-TEST $CLI volume set $V0 storage.reserve-size 40MB
e3c68b
+#Setting the size in percent and repeating the above steps
e3c68b
+TEST $CLI volume set $V0 storage.reserve 40
e3c68b
 
e3c68b
-#wait 5s to reset disk_space_full flag
e3c68b
 sleep 5
e3c68b
 
e3c68b
-TEST dd if=/dev/zero of=$M0/a bs=100M count=1
e3c68b
+TEST dd if=/dev/zero of=$M0/a bs=80M count=1
e3c68b
 TEST dd if=/dev/zero of=$M0/b bs=10M count=1
e3c68b
 
e3c68b
-# Wait 5s to update disk_space_full flag because thread check disk space
e3c68b
-# after every 5s
e3c68b
-
e3c68b
 sleep 5
e3c68b
-# setup_lvm create lvm partition of 150M and 40M are reserve so after
e3c68b
-# consuming more than 110M next dd should fail
e3c68b
 TEST ! dd if=/dev/zero of=$M0/c bs=5M count=1
e3c68b
 
e3c68b
 TEST $CLI volume stop $V0
e3c68b
diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-set.c b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
e3c68b
index 3a7ab83..7a83124 100644
e3c68b
--- a/xlators/mgmt/glusterd/src/glusterd-volume-set.c
e3c68b
+++ b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
e3c68b
@@ -1231,30 +1231,6 @@ out:
e3c68b
 
e3c68b
     return ret;
e3c68b
 }
e3c68b
-static int
e3c68b
-validate_size(glusterd_volinfo_t *volinfo, dict_t *dict, char *key, char *value,
e3c68b
-              char **op_errstr)
e3c68b
-{
e3c68b
-    xlator_t *this = NULL;
e3c68b
-    uint64_t size = 0;
e3c68b
-    int ret = -1;
e3c68b
-
e3c68b
-    this = THIS;
e3c68b
-    GF_VALIDATE_OR_GOTO("glusterd", this, out);
e3c68b
-    ret = gf_string2bytesize_uint64(value, &size);
e3c68b
-    if (ret < 0) {
e3c68b
-        gf_asprintf(op_errstr,
e3c68b
-                    "%s is not a valid size. %s "
e3c68b
-                    "expects a valid value in bytes",
e3c68b
-                    value, key);
e3c68b
-        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_INVALID_ENTRY, "%s",
e3c68b
-               *op_errstr);
e3c68b
-    }
e3c68b
-out:
e3c68b
-    gf_msg_debug("glusterd", 0, "Returning %d", ret);
e3c68b
-
e3c68b
-    return ret;
e3c68b
-}
e3c68b
 
e3c68b
 /* dispatch table for VOLUME SET
e3c68b
  * -----------------------------
e3c68b
@@ -2854,15 +2830,6 @@ struct volopt_map_entry glusterd_volopt_map[] = {
e3c68b
         .op_version = GD_OP_VERSION_3_13_0,
e3c68b
     },
e3c68b
     {
e3c68b
-        .key = "storage.reserve-size",
e3c68b
-        .voltype = "storage/posix",
e3c68b
-        .value = "0",
e3c68b
-        .validate_fn = validate_size,
e3c68b
-        .description = "If set, priority will be given to "
e3c68b
-                       "storage.reserve-size over storage.reserve",
e3c68b
-        .op_version = GD_OP_VERSION_7_0,
e3c68b
-    },
e3c68b
-    {
e3c68b
         .option = "health-check-timeout",
e3c68b
         .key = "storage.health-check-timeout",
e3c68b
         .type = NO_DOC,
e3c68b
diff --git a/xlators/storage/posix/src/posix-common.c b/xlators/storage/posix/src/posix-common.c
e3c68b
index 0f70af5..bfe2cb0 100644
e3c68b
--- a/xlators/storage/posix/src/posix-common.c
e3c68b
+++ b/xlators/storage/posix/src/posix-common.c
e3c68b
@@ -345,12 +345,14 @@ posix_reconfigure(xlator_t *this, dict_t *options)
e3c68b
                " fallback to <hostname>:<export>");
e3c68b
     }
e3c68b
 
e3c68b
-    GF_OPTION_RECONF("reserve-size", priv->disk_reserve_size, options, size,
e3c68b
+    GF_OPTION_RECONF("reserve", priv->disk_reserve, options, percent_or_size,
e3c68b
                      out);
e3c68b
+    /* option can be any one of percent or bytes */
e3c68b
+    priv->disk_unit = 0;
e3c68b
+    if (priv->disk_reserve < 100.0)
e3c68b
+        priv->disk_unit = 'p';
e3c68b
 
e3c68b
-    GF_OPTION_RECONF("reserve", priv->disk_reserve_percent, options, uint32,
e3c68b
-                     out);
e3c68b
-    if (priv->disk_reserve_size || priv->disk_reserve_percent) {
e3c68b
+    if (priv->disk_reserve) {
e3c68b
         ret = posix_spawn_disk_space_check_thread(this);
e3c68b
         if (ret) {
e3c68b
             gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_DISK_SPACE_CHECK_FAILED,
e3c68b
@@ -975,11 +977,15 @@ posix_init(xlator_t *this)
e3c68b
 
e3c68b
     _private->disk_space_check_active = _gf_false;
e3c68b
     _private->disk_space_full = 0;
e3c68b
-    GF_OPTION_INIT("reserve-size", _private->disk_reserve_size, size, out);
e3c68b
 
e3c68b
-    GF_OPTION_INIT("reserve", _private->disk_reserve_percent, uint32, out);
e3c68b
+    GF_OPTION_INIT("reserve", _private->disk_reserve, percent_or_size, out);
e3c68b
+
e3c68b
+    /* option can be any one of percent or bytes */
e3c68b
+    _private->disk_unit = 0;
e3c68b
+    if (_private->disk_reserve < 100.0)
e3c68b
+        _private->disk_unit = 'p';
e3c68b
 
e3c68b
-    if (_private->disk_reserve_size || _private->disk_reserve_percent) {
e3c68b
+    if (_private->disk_reserve) {
e3c68b
         ret = posix_spawn_disk_space_check_thread(this);
e3c68b
         if (ret) {
e3c68b
             gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_DISK_SPACE_CHECK_FAILED,
e3c68b
@@ -1221,23 +1227,14 @@ struct volume_options posix_options[] = {
e3c68b
      .op_version = {GD_OP_VERSION_4_0_0},
e3c68b
      .flags = OPT_FLAG_SETTABLE | OPT_FLAG_DOC},
e3c68b
     {.key = {"reserve"},
e3c68b
-     .type = GF_OPTION_TYPE_INT,
e3c68b
+     .type = GF_OPTION_TYPE_PERCENT_OR_SIZET,
e3c68b
      .min = 0,
e3c68b
      .default_value = "1",
e3c68b
      .validate = GF_OPT_VALIDATE_MIN,
e3c68b
-     .description = "Percentage of disk space to be reserved."
e3c68b
+     .description = "Percentage/Size of disk space to be reserved."
e3c68b
                     " Set to 0 to disable",
e3c68b
      .op_version = {GD_OP_VERSION_3_13_0},
e3c68b
      .flags = OPT_FLAG_SETTABLE | OPT_FLAG_DOC},
e3c68b
-    {.key = {"reserve-size"},
e3c68b
-     .type = GF_OPTION_TYPE_SIZET,
e3c68b
-     .min = 0,
e3c68b
-     .default_value = "0",
e3c68b
-     .validate = GF_OPT_VALIDATE_MIN,
e3c68b
-     .description = "size in megabytes to be reserved for disk space."
e3c68b
-                    " Set to 0 to disable",
e3c68b
-     .op_version = {GD_OP_VERSION_7_0},
e3c68b
-     .flags = OPT_FLAG_SETTABLE | OPT_FLAG_DOC},
e3c68b
     {.key = {"batch-fsync-mode"},
e3c68b
      .type = GF_OPTION_TYPE_STR,
e3c68b
      .default_value = "reverse-fsync",
e3c68b
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
e3c68b
index 849db3d..07169b5 100644
e3c68b
--- a/xlators/storage/posix/src/posix-helpers.c
e3c68b
+++ b/xlators/storage/posix/src/posix-helpers.c
e3c68b
@@ -2246,11 +2246,11 @@ posix_disk_space_check(xlator_t *this)
e3c68b
     struct posix_private *priv = NULL;
e3c68b
     char *subvol_path = NULL;
e3c68b
     int op_ret = 0;
e3c68b
-    uint64_t size = 0;
e3c68b
-    int percent = 0;
e3c68b
+    double size = 0;
e3c68b
+    double percent = 0;
e3c68b
     struct statvfs buf = {0};
e3c68b
-    uint64_t totsz = 0;
e3c68b
-    uint64_t freesz = 0;
e3c68b
+    double totsz = 0;
e3c68b
+    double freesz = 0;
e3c68b
 
e3c68b
     GF_VALIDATE_OR_GOTO(this->name, this, out);
e3c68b
     priv = this->private;
e3c68b
@@ -2258,14 +2258,6 @@ posix_disk_space_check(xlator_t *this)
e3c68b
 
e3c68b
     subvol_path = priv->base_path;
e3c68b
 
e3c68b
-    if (priv->disk_reserve_size) {
e3c68b
-        size = priv->disk_reserve_size;
e3c68b
-    } else {
e3c68b
-        percent = priv->disk_reserve_percent;
e3c68b
-        totsz = (buf.f_blocks * buf.f_bsize);
e3c68b
-        size = ((totsz * percent) / 100);
e3c68b
-    }
e3c68b
-
e3c68b
     op_ret = sys_statvfs(subvol_path, &buf;;
e3c68b
 
e3c68b
     if (op_ret == -1) {
e3c68b
@@ -2273,8 +2265,16 @@ posix_disk_space_check(xlator_t *this)
e3c68b
                "statvfs failed on %s", subvol_path);
e3c68b
         goto out;
e3c68b
     }
e3c68b
-    freesz = (buf.f_bfree * buf.f_bsize);
e3c68b
 
e3c68b
+    if (priv->disk_unit == 'p') {
e3c68b
+        percent = priv->disk_reserve;
e3c68b
+        totsz = (buf.f_blocks * buf.f_bsize);
e3c68b
+        size = ((totsz * percent) / 100);
e3c68b
+    } else {
e3c68b
+        size = priv->disk_reserve;
e3c68b
+    }
e3c68b
+
e3c68b
+    freesz = (buf.f_bfree * buf.f_bsize);
e3c68b
     if (freesz <= size) {
e3c68b
         priv->disk_space_full = 1;
e3c68b
     } else {
e3c68b
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
e3c68b
index b92c411..fc847d6 100644
e3c68b
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
e3c68b
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
e3c68b
@@ -720,7 +720,7 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
e3c68b
        thread after every 5 sec sleep to working correctly storage.reserve
e3c68b
        option behaviour
e3c68b
     */
e3c68b
-    if (priv->disk_reserve_size || priv->disk_reserve_percent)
e3c68b
+    if (priv->disk_reserve)
e3c68b
         posix_disk_space_check(this);
e3c68b
 
e3c68b
     DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, ret, ret, out);
e3c68b
@@ -2306,7 +2306,7 @@ posix_statfs(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
e3c68b
     };
e3c68b
     struct posix_private *priv = NULL;
e3c68b
     int shared_by = 1;
e3c68b
-    int percent = 0;
e3c68b
+    double percent = 0;
e3c68b
     uint64_t reserved_blocks = 0;
e3c68b
 
e3c68b
     VALIDATE_OR_GOTO(frame, out);
e3c68b
@@ -2332,11 +2332,14 @@ posix_statfs(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
e3c68b
         goto out;
e3c68b
     }
e3c68b
 
e3c68b
-    if (priv->disk_reserve_size) {
e3c68b
-        reserved_blocks = priv->disk_reserve_size / buf.f_bsize;
e3c68b
+    if (priv->disk_unit == 'p') {
e3c68b
+        percent = priv->disk_reserve;
e3c68b
+        reserved_blocks = (((buf.f_blocks * percent) / 100) + 0.5);
e3c68b
     } else {
e3c68b
-        percent = priv->disk_reserve_percent;
e3c68b
-        reserved_blocks = (buf.f_blocks * percent) / 100;
e3c68b
+        if (buf.f_bsize) {
e3c68b
+            reserved_blocks = (priv->disk_reserve + buf.f_bsize - 1) /
e3c68b
+                              buf.f_bsize;
e3c68b
+        }
e3c68b
     }
e3c68b
 
e3c68b
     if (buf.f_bfree > reserved_blocks) {
e3c68b
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
e3c68b
index 4364b96..b0935a7 100644
e3c68b
--- a/xlators/storage/posix/src/posix.h
e3c68b
+++ b/xlators/storage/posix/src/posix.h
e3c68b
@@ -225,8 +225,8 @@ struct posix_private {
e3c68b
     pthread_t health_check;
e3c68b
     gf_boolean_t health_check_active;
e3c68b
 
e3c68b
-    uint32_t disk_reserve_percent;
e3c68b
-    uint64_t disk_reserve_size;
e3c68b
+    double disk_reserve;
e3c68b
+    char disk_unit;
e3c68b
     uint32_t disk_space_full;
e3c68b
     pthread_t disk_space_check;
e3c68b
     gf_boolean_t disk_space_check_active;
e3c68b
-- 
e3c68b
1.8.3.1
e3c68b