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