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