17b94a
From 4d82c7879387e6f7963b4d9c84c4ff8a1788055d Mon Sep 17 00:00:00 2001
17b94a
From: Sheetal Pamecha <sheetal.pamecha08@gmail.com>
17b94a
Date: Mon, 19 Nov 2018 22:15:25 +0530
17b94a
Subject: [PATCH 171/178] posix: add storage.reserve-size option
17b94a
17b94a
storage.reserve-size option will take size as input
17b94a
instead of percentage. If set, priority will be given to
17b94a
storage.reserve-size over storage.reserve. Default value
17b94a
of this option is 0.
17b94a
17b94a
> fixes: bz#1651445
17b94a
> Change-Id: I7a7342c68e436e8bf65bd39c567512ee04abbcea
17b94a
> Signed-off-by: Sheetal Pamecha <sheetal.pamecha08@gmail.com>
17b94a
> Cherry pick from commit 950726dfc8e3171bef625b563c0c6dbba1ec2928
17b94a
> Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/21686/
17b94a
17b94a
BUG: 1573077
17b94a
Change-Id: I7a7342c68e436e8bf65bd39c567512ee04abbcea
17b94a
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
17b94a
Reviewed-on: https://code.engineering.redhat.com/gerrit/172709
17b94a
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
17b94a
Tested-by: RHGS Build Bot <nigelb@redhat.com>
17b94a
---
17b94a
 tests/bugs/posix/bug-1651445.t                  | 58 +++++++++++++++++++++++++
17b94a
 xlators/mgmt/glusterd/src/glusterd-volume-set.c | 33 ++++++++++++++
17b94a
 xlators/storage/posix/src/posix-common.c        | 34 ++++++++++++---
17b94a
 xlators/storage/posix/src/posix-helpers.c       | 13 ++++--
17b94a
 xlators/storage/posix/src/posix-inode-fd-ops.c  | 10 +++--
17b94a
 xlators/storage/posix/src/posix.h               |  3 +-
17b94a
 6 files changed, 138 insertions(+), 13 deletions(-)
17b94a
 create mode 100644 tests/bugs/posix/bug-1651445.t
17b94a
17b94a
diff --git a/tests/bugs/posix/bug-1651445.t b/tests/bugs/posix/bug-1651445.t
17b94a
new file mode 100644
17b94a
index 0000000..f6f1833
17b94a
--- /dev/null
17b94a
+++ b/tests/bugs/posix/bug-1651445.t
17b94a
@@ -0,0 +1,58 @@
17b94a
+#!/bin/bash
17b94a
+
17b94a
+. $(dirname $0)/../../include.rc
17b94a
+. $(dirname $0)/../../volume.rc
17b94a
+. $(dirname $0)/../../snapshot.rc
17b94a
+
17b94a
+cleanup
17b94a
+
17b94a
+TEST verify_lvm_version
17b94a
+TEST glusterd
17b94a
+TEST pidof glusterd
17b94a
+TEST init_n_bricks 3
17b94a
+TEST setup_lvm 3
17b94a
+
17b94a
+TEST $CLI volume create $V0 replica 3 $H0:$L{1,2,3}
17b94a
+TEST $CLI volume start $V0
17b94a
+
17b94a
+TEST glusterfs --volfile-id=/$V0 --volfile-server=$H0 $M0
17b94a
+
17b94a
+TEST $CLI volume set $V0 storage.reserve-size 10MB
17b94a
+
17b94a
+#No effect as priority to reserve-size
17b94a
+TEST $CLI volume set $V0 storage.reserve 20
17b94a
+
17b94a
+TEST dd if=/dev/zero of=$M0/a bs=100M count=1
17b94a
+sleep 5
17b94a
+
17b94a
+#Below dd confirms posix is giving priority to reserve-size
17b94a
+TEST dd if=/dev/zero of=$M0/b bs=40M count=1
17b94a
+
17b94a
+sleep 5
17b94a
+TEST ! dd if=/dev/zero of=$M0/c bs=5M count=1
17b94a
+
17b94a
+rm -rf $M0/*
17b94a
+#Size will reserve from the previously set reserve option = 20%
17b94a
+TEST $CLI volume set $V0 storage.reserve-size 0
17b94a
+
17b94a
+#Overwrite reserve option
17b94a
+TEST $CLI volume set $V0 storage.reserve-size 40MB
17b94a
+
17b94a
+#wait 5s to reset disk_space_full flag
17b94a
+sleep 5
17b94a
+
17b94a
+TEST dd if=/dev/zero of=$M0/a bs=100M count=1
17b94a
+TEST dd if=/dev/zero of=$M0/b bs=10M count=1
17b94a
+
17b94a
+# Wait 5s to update disk_space_full flag because thread check disk space
17b94a
+# after every 5s
17b94a
+
17b94a
+sleep 5
17b94a
+# setup_lvm create lvm partition of 150M and 40M are reserve so after
17b94a
+# consuming more than 110M next dd should fail
17b94a
+TEST ! dd if=/dev/zero of=$M0/c bs=5M count=1
17b94a
+
17b94a
+TEST $CLI volume stop $V0
17b94a
+TEST $CLI volume delete $V0
17b94a
+
17b94a
+cleanup
17b94a
diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-set.c b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
17b94a
index 7a83124..3a7ab83 100644
17b94a
--- a/xlators/mgmt/glusterd/src/glusterd-volume-set.c
17b94a
+++ b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
17b94a
@@ -1231,6 +1231,30 @@ out:
17b94a
 
17b94a
     return ret;
17b94a
 }
17b94a
+static int
17b94a
+validate_size(glusterd_volinfo_t *volinfo, dict_t *dict, char *key, char *value,
17b94a
+              char **op_errstr)
17b94a
+{
17b94a
+    xlator_t *this = NULL;
17b94a
+    uint64_t size = 0;
17b94a
+    int ret = -1;
17b94a
+
17b94a
+    this = THIS;
17b94a
+    GF_VALIDATE_OR_GOTO("glusterd", this, out);
17b94a
+    ret = gf_string2bytesize_uint64(value, &size);
17b94a
+    if (ret < 0) {
17b94a
+        gf_asprintf(op_errstr,
17b94a
+                    "%s is not a valid size. %s "
17b94a
+                    "expects a valid value in bytes",
17b94a
+                    value, key);
17b94a
+        gf_msg(this->name, GF_LOG_ERROR, 0, GD_MSG_INVALID_ENTRY, "%s",
17b94a
+               *op_errstr);
17b94a
+    }
17b94a
+out:
17b94a
+    gf_msg_debug("glusterd", 0, "Returning %d", ret);
17b94a
+
17b94a
+    return ret;
17b94a
+}
17b94a
 
17b94a
 /* dispatch table for VOLUME SET
17b94a
  * -----------------------------
17b94a
@@ -2830,6 +2854,15 @@ struct volopt_map_entry glusterd_volopt_map[] = {
17b94a
         .op_version = GD_OP_VERSION_3_13_0,
17b94a
     },
17b94a
     {
17b94a
+        .key = "storage.reserve-size",
17b94a
+        .voltype = "storage/posix",
17b94a
+        .value = "0",
17b94a
+        .validate_fn = validate_size,
17b94a
+        .description = "If set, priority will be given to "
17b94a
+                       "storage.reserve-size over storage.reserve",
17b94a
+        .op_version = GD_OP_VERSION_7_0,
17b94a
+    },
17b94a
+    {
17b94a
         .option = "health-check-timeout",
17b94a
         .key = "storage.health-check-timeout",
17b94a
         .type = NO_DOC,
17b94a
diff --git a/xlators/storage/posix/src/posix-common.c b/xlators/storage/posix/src/posix-common.c
17b94a
index ed82e35..0f70af5 100644
17b94a
--- a/xlators/storage/posix/src/posix-common.c
17b94a
+++ b/xlators/storage/posix/src/posix-common.c
17b94a
@@ -345,11 +345,18 @@ posix_reconfigure(xlator_t *this, dict_t *options)
17b94a
                " fallback to <hostname>:<export>");
17b94a
     }
17b94a
 
17b94a
-    GF_OPTION_RECONF("reserve", priv->disk_reserve, options, uint32, out);
17b94a
-    if (priv->disk_reserve) {
17b94a
+    GF_OPTION_RECONF("reserve-size", priv->disk_reserve_size, options, size,
17b94a
+                     out);
17b94a
+
17b94a
+    GF_OPTION_RECONF("reserve", priv->disk_reserve_percent, options, uint32,
17b94a
+                     out);
17b94a
+    if (priv->disk_reserve_size || priv->disk_reserve_percent) {
17b94a
         ret = posix_spawn_disk_space_check_thread(this);
17b94a
-        if (ret)
17b94a
+        if (ret) {
17b94a
+            gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_DISK_SPACE_CHECK_FAILED,
17b94a
+                   "Getting disk space check from thread failed");
17b94a
             goto out;
17b94a
+        }
17b94a
     }
17b94a
 
17b94a
     GF_OPTION_RECONF("health-check-interval", priv->health_check_interval,
17b94a
@@ -968,11 +975,17 @@ posix_init(xlator_t *this)
17b94a
 
17b94a
     _private->disk_space_check_active = _gf_false;
17b94a
     _private->disk_space_full = 0;
17b94a
-    GF_OPTION_INIT("reserve", _private->disk_reserve, uint32, out);
17b94a
-    if (_private->disk_reserve) {
17b94a
+    GF_OPTION_INIT("reserve-size", _private->disk_reserve_size, size, out);
17b94a
+
17b94a
+    GF_OPTION_INIT("reserve", _private->disk_reserve_percent, uint32, out);
17b94a
+
17b94a
+    if (_private->disk_reserve_size || _private->disk_reserve_percent) {
17b94a
         ret = posix_spawn_disk_space_check_thread(this);
17b94a
-        if (ret)
17b94a
+        if (ret) {
17b94a
+            gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_DISK_SPACE_CHECK_FAILED,
17b94a
+                   "Getting disk space check from thread failed ");
17b94a
             goto out;
17b94a
+        }
17b94a
     }
17b94a
 
17b94a
     _private->health_check_active = _gf_false;
17b94a
@@ -1216,6 +1229,15 @@ struct volume_options posix_options[] = {
17b94a
                     " Set to 0 to disable",
17b94a
      .op_version = {GD_OP_VERSION_3_13_0},
17b94a
      .flags = OPT_FLAG_SETTABLE | OPT_FLAG_DOC},
17b94a
+    {.key = {"reserve-size"},
17b94a
+     .type = GF_OPTION_TYPE_SIZET,
17b94a
+     .min = 0,
17b94a
+     .default_value = "0",
17b94a
+     .validate = GF_OPT_VALIDATE_MIN,
17b94a
+     .description = "size in megabytes to be reserved for disk space."
17b94a
+                    " Set to 0 to disable",
17b94a
+     .op_version = {GD_OP_VERSION_7_0},
17b94a
+     .flags = OPT_FLAG_SETTABLE | OPT_FLAG_DOC},
17b94a
     {.key = {"batch-fsync-mode"},
17b94a
      .type = GF_OPTION_TYPE_STR,
17b94a
      .default_value = "reverse-fsync",
17b94a
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
17b94a
index d0fd45a..aecf4f8 100644
17b94a
--- a/xlators/storage/posix/src/posix-helpers.c
17b94a
+++ b/xlators/storage/posix/src/posix-helpers.c
17b94a
@@ -2246,6 +2246,7 @@ posix_disk_space_check(xlator_t *this)
17b94a
     struct posix_private *priv = NULL;
17b94a
     char *subvol_path = NULL;
17b94a
     int op_ret = 0;
17b94a
+    uint64_t size = 0;
17b94a
     int percent = 0;
17b94a
     struct statvfs buf = {0};
17b94a
     uint64_t totsz = 0;
17b94a
@@ -2256,7 +2257,14 @@ posix_disk_space_check(xlator_t *this)
17b94a
     GF_VALIDATE_OR_GOTO(this->name, priv, out);
17b94a
 
17b94a
     subvol_path = priv->base_path;
17b94a
-    percent = priv->disk_reserve;
17b94a
+
17b94a
+    if (priv->disk_reserve_size) {
17b94a
+        size = priv->disk_reserve_size;
17b94a
+    } else {
17b94a
+        percent = priv->disk_reserve_percent;
17b94a
+        totsz = (buf.f_blocks * buf.f_bsize);
17b94a
+        size = ((totsz * percent) / 100);
17b94a
+    }
17b94a
 
17b94a
     op_ret = sys_statvfs(subvol_path, &buf;;
17b94a
 
17b94a
@@ -2265,10 +2273,9 @@ posix_disk_space_check(xlator_t *this)
17b94a
                "statvfs failed on %s", subvol_path);
17b94a
         goto out;
17b94a
     }
17b94a
-    totsz = (buf.f_blocks * buf.f_bsize);
17b94a
     freesz = (buf.f_bfree * buf.f_bsize);
17b94a
 
17b94a
-    if (freesz <= ((totsz * percent) / 100)) {
17b94a
+    if (freesz <= size) {
17b94a
         priv->disk_space_full = 1;
17b94a
     } else {
17b94a
         priv->disk_space_full = 0;
17b94a
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
17b94a
index 2c19ce1..7ca4d26 100644
17b94a
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
17b94a
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
17b94a
@@ -720,7 +720,7 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
17b94a
        thread after every 5 sec sleep to working correctly storage.reserve
17b94a
        option behaviour
17b94a
     */
17b94a
-    if (priv->disk_reserve)
17b94a
+    if (priv->disk_reserve_size || priv->disk_reserve_percent)
17b94a
         posix_disk_space_check(this);
17b94a
 
17b94a
     DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, ret, ret, out);
17b94a
@@ -2331,8 +2331,12 @@ posix_statfs(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
17b94a
         goto out;
17b94a
     }
17b94a
 
17b94a
-    percent = priv->disk_reserve;
17b94a
-    reserved_blocks = (buf.f_blocks * percent) / 100;
17b94a
+    if (priv->disk_reserve_size) {
17b94a
+        reserved_blocks = priv->disk_reserve_size / buf.f_bsize;
17b94a
+    } else {
17b94a
+        percent = priv->disk_reserve_percent;
17b94a
+        reserved_blocks = (buf.f_blocks * percent) / 100;
17b94a
+    }
17b94a
 
17b94a
     if (buf.f_bfree > reserved_blocks) {
17b94a
         buf.f_bfree = (buf.f_bfree - reserved_blocks);
17b94a
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
17b94a
index 1da4d01..4364b96 100644
17b94a
--- a/xlators/storage/posix/src/posix.h
17b94a
+++ b/xlators/storage/posix/src/posix.h
17b94a
@@ -225,7 +225,8 @@ struct posix_private {
17b94a
     pthread_t health_check;
17b94a
     gf_boolean_t health_check_active;
17b94a
 
17b94a
-    uint32_t disk_reserve;
17b94a
+    uint32_t disk_reserve_percent;
17b94a
+    uint64_t disk_reserve_size;
17b94a
     uint32_t disk_space_full;
17b94a
     pthread_t disk_space_check;
17b94a
     gf_boolean_t disk_space_check_active;
17b94a
-- 
17b94a
1.8.3.1
17b94a