d1681e
From 3b1c45188c7260ae3dda4bcedd7cb81566f1f2ea Mon Sep 17 00:00:00 2001
d1681e
From: Mohit Agrawal <moagrawa@redhat.com>
d1681e
Date: Sat, 15 Jul 2017 17:55:14 +0530
d1681e
Subject: [PATCH 092/128] posix: Needs to reserve disk space to prevent the
d1681e
 brick from getting full
d1681e
d1681e
Problem: Currently there is no option available at posix xlator to save the
d1681e
         disk from getting full
d1681e
d1681e
Solution: Introduce a new option storage.reserve at posix xlator to
d1681e
          configure disk threshold.posix xlator spawn a thread to update the
d1681e
          disk space status in posix private structure and same flag is checked
d1681e
          by every posix fop before start operation.If flag value is 1 then
d1681e
          it sets op_errno to ENOSPC and goto out from the fop.
d1681e
d1681e
> BUG: 1471366
d1681e
> Change-Id: I98287cd409860f4c754fc69a332e0521bfb1b67e
d1681e
> Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
d1681e
> Reviewed-on: https://review.gluster.org/17780
d1681e
> Smoke: Gluster Build System <jenkins@build.gluster.org>
d1681e
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
d1681e
> Reviewed-by: Amar Tumballi <amarts@redhat.com>
d1681e
> Reviewed-by: Jeff Darcy <jeff@pl.atyp.us>
d1681e
d1681e
BUG: 1464350
d1681e
Change-Id: I98287cd409860f4c754fc69a332e0521bfb1b67e
d1681e
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
d1681e
Reviewed-on: https://code.engineering.redhat.com/gerrit/124629
d1681e
Tested-by: RHGS Build Bot <nigelb@redhat.com>
d1681e
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
d1681e
---
d1681e
 xlators/mgmt/glusterd/src/glusterd-volume-set.c |   4 +
d1681e
 xlators/storage/posix/src/posix-aio.c           |   1 +
d1681e
 xlators/storage/posix/src/posix-helpers.c       | 115 ++++++++++++++++++++++++
d1681e
 xlators/storage/posix/src/posix-messages.h      |  11 ++-
d1681e
 xlators/storage/posix/src/posix.c               |  80 +++++++++++++++--
d1681e
 xlators/storage/posix/src/posix.h               |  19 ++++
d1681e
 6 files changed, 222 insertions(+), 8 deletions(-)
d1681e
d1681e
diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-set.c b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
d1681e
index b15a5af..a57eb9e 100644
d1681e
--- a/xlators/mgmt/glusterd/src/glusterd-volume-set.c
d1681e
+++ b/xlators/mgmt/glusterd/src/glusterd-volume-set.c
d1681e
@@ -2805,6 +2805,10 @@ struct volopt_map_entry glusterd_volopt_map[] = {
d1681e
           .voltype     = "storage/posix",
d1681e
           .op_version  = GD_OP_VERSION_3_12_0,
d1681e
         },
d1681e
+        { .key         = "storage.reserve",
d1681e
+          .voltype     = "storage/posix",
d1681e
+          .op_version  = GD_OP_VERSION_3_13_0,
d1681e
+        },
d1681e
         { .key         = "storage.bd-aio",
d1681e
           .voltype     = "storage/bd",
d1681e
           .op_version  = GD_OP_VERSION_RHS_3_0
d1681e
diff --git a/xlators/storage/posix/src/posix-aio.c b/xlators/storage/posix/src/posix-aio.c
d1681e
index b5ac1b9..2adafeb 100644
d1681e
--- a/xlators/storage/posix/src/posix-aio.c
d1681e
+++ b/xlators/storage/posix/src/posix-aio.c
d1681e
@@ -330,6 +330,7 @@ posix_aio_writev (call_frame_t *frame, xlator_t *this, fd_t *fd,
d1681e
         VALIDATE_OR_GOTO (fd, err);
d1681e
 
d1681e
         priv = this->private;
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_errno, op_errno, err);
d1681e
 
d1681e
         ret = posix_fd_ctx_get (fd, this, &pfd, &op_errno);
d1681e
         if (ret < 0) {
d1681e
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
d1681e
index f97c90b..826441f 100644
d1681e
--- a/xlators/storage/posix/src/posix-helpers.c
d1681e
+++ b/xlators/storage/posix/src/posix-helpers.c
d1681e
@@ -1977,6 +1977,121 @@ unlock:
d1681e
         UNLOCK (&priv->lock);
d1681e
 }
d1681e
 
d1681e
+void
d1681e
+posix_disk_space_check (xlator_t *this)
d1681e
+{
d1681e
+        struct  posix_private *priv     = NULL;
d1681e
+        char    *subvol_path            = NULL;
d1681e
+        int     op_ret                  = 0;
d1681e
+        int     percent                 = 0;
d1681e
+        struct statvfs buf              = {0};
d1681e
+        uint64_t totsz                  = 0;
d1681e
+        uint64_t freesz                 = 0;
d1681e
+
d1681e
+        GF_VALIDATE_OR_GOTO (this->name, this, out);
d1681e
+        priv = this->private;
d1681e
+        GF_VALIDATE_OR_GOTO ("posix-helpers", priv, out);
d1681e
+
d1681e
+        subvol_path = priv->base_path;
d1681e
+        percent = priv->disk_threshhold;
d1681e
+
d1681e
+        op_ret = sys_statvfs (subvol_path, &buf;;
d1681e
+
d1681e
+        if (op_ret == -1) {
d1681e
+                gf_msg (this->name, GF_LOG_ERROR, errno, P_MSG_STATVFS_FAILED,
d1681e
+                        "statvfs failed on %s", subvol_path);
d1681e
+                goto out;
d1681e
+        }
d1681e
+        totsz = (buf.f_blocks * buf.f_bsize);
d1681e
+        freesz = (buf.f_bfree * buf.f_bsize);
d1681e
+
d1681e
+        if (freesz <= ((totsz * percent) / 100)) {
d1681e
+                priv->disk_space_full = 1;
d1681e
+        } else {
d1681e
+                priv->disk_space_full = 0;
d1681e
+        }
d1681e
+out:
d1681e
+        return;
d1681e
+}
d1681e
+
d1681e
+
d1681e
+static void *
d1681e
+posix_disk_space_check_thread_proc (void *data)
d1681e
+{
d1681e
+        xlator_t             *this               = NULL;
d1681e
+        struct posix_private *priv               = NULL;
d1681e
+        uint32_t              interval           = 0;
d1681e
+        int                   ret                = -1;
d1681e
+
d1681e
+        this = data;
d1681e
+        priv = this->private;
d1681e
+
d1681e
+        interval = 5;
d1681e
+        gf_msg_debug (this->name, 0, "disk-space thread started, "
d1681e
+                      "interval = %d seconds", interval);
d1681e
+        while (1) {
d1681e
+                /* aborting sleep() is a request to exit this thread, sleep()
d1681e
+                 * will normally not return when cancelled */
d1681e
+                ret = sleep (interval);
d1681e
+                if (ret > 0)
d1681e
+                        break;
d1681e
+                /* prevent thread errors while doing the health-check(s) */
d1681e
+                pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);
d1681e
+
d1681e
+                /* Do the disk-check.*/
d1681e
+                posix_disk_space_check (this);
d1681e
+                if (!priv->disk_space_check_active)
d1681e
+                        goto out;
d1681e
+                pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
d1681e
+        }
d1681e
+
d1681e
+out:
d1681e
+        gf_msg_debug (this->name, 0, "disk space check thread exiting");
d1681e
+        LOCK (&priv->lock);
d1681e
+        {
d1681e
+                priv->disk_space_check_active = _gf_false;
d1681e
+        }
d1681e
+        UNLOCK (&priv->lock);
d1681e
+
d1681e
+
d1681e
+        return NULL;
d1681e
+}
d1681e
+
d1681e
+void
d1681e
+posix_spawn_disk_space_check_thread (xlator_t *xl)
d1681e
+{
d1681e
+        struct posix_private *priv               = NULL;
d1681e
+        int                   ret                = -1;
d1681e
+
d1681e
+        priv = xl->private;
d1681e
+
d1681e
+        LOCK (&priv->lock);
d1681e
+        {
d1681e
+                /* cancel the running thread  */
d1681e
+                if (priv->disk_space_check_active == _gf_true) {
d1681e
+                        pthread_cancel (priv->disk_space_check);
d1681e
+                        priv->disk_space_check_active = _gf_false;
d1681e
+                }
d1681e
+
d1681e
+                ret = gf_thread_create (&priv->disk_space_check, NULL,
d1681e
+                                        posix_disk_space_check_thread_proc,
d1681e
+                                        xl, "posix_reserve");
d1681e
+                if (ret < 0) {
d1681e
+                        priv->disk_space_check_active = _gf_false;
d1681e
+                        gf_msg (xl->name, GF_LOG_ERROR, errno,
d1681e
+                                P_MSG_DISK_SPACE_CHECK_FAILED,
d1681e
+                                "unable to setup disk space check thread");
d1681e
+                        goto unlock;
d1681e
+                }
d1681e
+
d1681e
+                /* run the thread detached, resources will be freed on exit */
d1681e
+                pthread_detach (priv->disk_space_check);
d1681e
+                priv->disk_space_check_active = _gf_true;
d1681e
+        }
d1681e
+unlock:
d1681e
+        UNLOCK (&priv->lock);
d1681e
+}
d1681e
+
d1681e
 int
d1681e
 posix_fsyncer_pick (xlator_t *this, struct list_head *head)
d1681e
 {
d1681e
diff --git a/xlators/storage/posix/src/posix-messages.h b/xlators/storage/posix/src/posix-messages.h
d1681e
index ee06d6f..20cf1f0 100644
d1681e
--- a/xlators/storage/posix/src/posix-messages.h
d1681e
+++ b/xlators/storage/posix/src/posix-messages.h
d1681e
@@ -45,7 +45,7 @@
d1681e
  */
d1681e
 
d1681e
 #define POSIX_COMP_BASE         GLFS_MSGID_COMP_POSIX
d1681e
-#define GLFS_NUM_MESSAGES       110
d1681e
+#define GLFS_NUM_MESSAGES       111
d1681e
 #define GLFS_MSGID_END          (POSIX_COMP_BASE + GLFS_NUM_MESSAGES + 1)
d1681e
 /* Messaged with message IDs */
d1681e
 #define glfs_msg_start_x POSIX_COMP_BASE, "Invalid: Start of messages"
d1681e
@@ -955,6 +955,15 @@
d1681e
  */
d1681e
 
d1681e
 
d1681e
+#define P_MSG_DISK_SPACE_CHECK_FAILED             (POSIX_COMP_BASE + 112)
d1681e
+
d1681e
+/*!
d1681e
+ * @messageid
d1681e
+ * @diagnosis
d1681e
+ * @recommendedaction
d1681e
+ *
d1681e
+ */
d1681e
+
d1681e
 /*------------*/
d1681e
 #define glfs_msg_end_x GLFS_MSGID_END, "Invalid: End of messages"
d1681e
 
d1681e
diff --git a/xlators/storage/posix/src/posix.c b/xlators/storage/posix/src/posix.c
d1681e
index d858878..1cb0fef 100644
d1681e
--- a/xlators/storage/posix/src/posix.c
d1681e
+++ b/xlators/storage/posix/src/posix.c
d1681e
@@ -766,6 +766,7 @@ posix_do_fallocate (call_frame_t *frame, xlator_t *this, fd_t *fd,
d1681e
         struct posix_fd    *pfd    = NULL;
d1681e
         gf_boolean_t        locked = _gf_false;
d1681e
         posix_inode_ctx_t  *ctx    = NULL;
d1681e
+        struct  posix_private *priv = NULL;
d1681e
 
d1681e
         DECLARE_OLD_FS_ID_VAR;
d1681e
 
d1681e
@@ -775,6 +776,9 @@ posix_do_fallocate (call_frame_t *frame, xlator_t *this, fd_t *fd,
d1681e
         VALIDATE_OR_GOTO (this, out);
d1681e
         VALIDATE_OR_GOTO (fd, out);
d1681e
 
d1681e
+        priv = this->private;
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, ret, ret, out);
d1681e
+
d1681e
         ret = posix_fd_ctx_get (fd, this, &pfd, &op_errno);
d1681e
         if (ret < 0) {
d1681e
                 gf_msg_debug (this->name, 0, "pfd is NULL from fd=%p", fd);
d1681e
@@ -1073,20 +1077,27 @@ posix_zerofill(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
d1681e
         int32_t ret                      =  0;
d1681e
         struct  iatt statpre             = {0,};
d1681e
         struct  iatt statpost            = {0,};
d1681e
+        struct  posix_private *priv      = NULL;
d1681e
+        int     op_ret                   = -1;
d1681e
+        int     op_errno                 = -1;
d1681e
+
d1681e
+        VALIDATE_OR_GOTO (frame, out);
d1681e
+        VALIDATE_OR_GOTO (this, out);
d1681e
+
d1681e
+        priv = this->private;
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         ret = posix_do_zerofill (frame, this, fd, offset, len,
d1681e
                                  &statpre, &statpost, xdata);
d1681e
-        if (ret < 0) {
d1681e
-                goto err;
d1681e
-        }
d1681e
+        if (ret < 0)
d1681e
+                goto out;
d1681e
 
d1681e
         STACK_UNWIND_STRICT(zerofill, frame, 0, 0, &statpre, &statpost, NULL);
d1681e
         return 0;
d1681e
 
d1681e
-err:
d1681e
-        STACK_UNWIND_STRICT(zerofill, frame, -1, -ret, NULL, NULL, NULL);
d1681e
+out:
d1681e
+        STACK_UNWIND_STRICT(zerofill, frame, op_ret, op_errno, NULL, NULL, NULL);
d1681e
         return 0;
d1681e
-
d1681e
 }
d1681e
 
d1681e
 static int32_t
d1681e
@@ -1354,6 +1365,7 @@ posix_mknod (call_frame_t *frame, xlator_t *this,
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
         GFID_NULL_CHECK_AND_GOTO (frame, this, loc, xdata, op_ret, op_errno,
d1681e
                                   out);
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         MAKE_ENTRY_HANDLE (real_path, par_path, this, loc, NULL);
d1681e
 
d1681e
@@ -1574,6 +1586,7 @@ posix_mkdir (call_frame_t *frame, xlator_t *this,
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
         GFID_NULL_CHECK_AND_GOTO (frame, this, loc, xdata, op_ret, op_errno,
d1681e
                                   out);
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         MAKE_ENTRY_HANDLE (real_path, par_path, this, loc, NULL);
d1681e
         if (!real_path || !par_path) {
d1681e
@@ -2400,6 +2413,7 @@ posix_symlink (call_frame_t *frame, xlator_t *this,
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
         GFID_NULL_CHECK_AND_GOTO (frame, this, loc, xdata, op_ret, op_errno,
d1681e
                                   out);
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         MAKE_ENTRY_HANDLE (real_path, par_path, this, loc, &stbuf);
d1681e
 
d1681e
@@ -2558,6 +2572,7 @@ posix_rename (call_frame_t *frame, xlator_t *this,
d1681e
 
d1681e
         priv = this->private;
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         SET_FS_ID (frame->root->uid, frame->root->gid);
d1681e
         MAKE_ENTRY_HANDLE (real_oldpath, par_oldpath, this, oldloc, NULL);
d1681e
@@ -2840,6 +2855,7 @@ posix_link (call_frame_t *frame, xlator_t *this,
d1681e
 
d1681e
         priv = this->private;
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         SET_FS_ID (frame->root->uid, frame->root->gid);
d1681e
         MAKE_INODE_HANDLE (real_oldpath, this, oldloc, &stbuf);
d1681e
@@ -3049,6 +3065,7 @@ posix_create (call_frame_t *frame, xlator_t *this,
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
         GFID_NULL_CHECK_AND_GOTO (frame, this, loc, xdata, op_ret, op_errno,
d1681e
                                   out);
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         MAKE_ENTRY_HANDLE (real_path, par_path, this, loc, &stbuf);
d1681e
 
d1681e
@@ -3236,6 +3253,9 @@ posix_open (call_frame_t *frame, xlator_t *this,
d1681e
         priv = this->private;
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
 
d1681e
+        if (flags & O_CREAT)
d1681e
+                DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
+
d1681e
         MAKE_INODE_HANDLE (real_path, this, loc, &stbuf);
d1681e
         if (!real_path) {
d1681e
                 op_ret = -1;
d1681e
@@ -3559,6 +3579,7 @@ posix_writev (call_frame_t *frame, xlator_t *this, fd_t *fd,
d1681e
         priv = this->private;
d1681e
 
d1681e
         VALIDATE_OR_GOTO (priv, out);
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
 
d1681e
         ret = posix_fd_ctx_get (fd, this, &pfd, &op_errno);
d1681e
         if (ret < 0) {
d1681e
@@ -3698,6 +3719,7 @@ posix_statfs (call_frame_t *frame, xlator_t *this,
d1681e
         struct statvfs         buf       = {0, };
d1681e
         struct posix_private * priv      = NULL;
d1681e
         int                    shared_by = 1;
d1681e
+        int                    percent   = 0;
d1681e
 
d1681e
         VALIDATE_OR_GOTO (frame, out);
d1681e
         VALIDATE_OR_GOTO (this, out);
d1681e
@@ -3722,6 +3744,9 @@ posix_statfs (call_frame_t *frame, xlator_t *this,
d1681e
                 goto out;
d1681e
         }
d1681e
 
d1681e
+        percent = priv->disk_threshhold;
d1681e
+        buf.f_bfree = (buf.f_bfree - ((buf.f_blocks * percent) / 100));
d1681e
+
d1681e
         shared_by = priv->shared_brick_count;
d1681e
         if (shared_by > 1) {
d1681e
                 buf.f_blocks /= shared_by;
d1681e
@@ -3879,6 +3904,7 @@ posix_fsync (call_frame_t *frame, xlator_t *this,
d1681e
 #endif
d1681e
 
d1681e
 	priv = this->private;
d1681e
+
d1681e
 	if (priv->batch_fsync_mode && xdata && dict_get (xdata, "batch-fsync")) {
d1681e
 		posix_batch_fsync (frame, this, fd, datasync, xdata);
d1681e
 		return 0;
d1681e
@@ -3983,6 +4009,7 @@ posix_setxattr (call_frame_t *frame, xlator_t *this,
d1681e
         ssize_t       acl_size                = 0;
d1681e
         dict_t       *xattr                   = NULL;
d1681e
         posix_xattr_filler_t filler = {0,};
d1681e
+        struct  posix_private *priv           = NULL;
d1681e
 
d1681e
         DECLARE_OLD_FS_ID_VAR;
d1681e
         SET_FS_ID (frame->root->uid, frame->root->gid);
d1681e
@@ -3992,6 +4019,9 @@ posix_setxattr (call_frame_t *frame, xlator_t *this,
d1681e
         VALIDATE_OR_GOTO (loc, out);
d1681e
         VALIDATE_OR_GOTO (dict, out);
d1681e
 
d1681e
+        priv = this->private;
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
+
d1681e
         MAKE_INODE_HANDLE (real_path, this, loc, NULL);
d1681e
         if (!real_path) {
d1681e
                 op_ret = -1;
d1681e
@@ -5346,6 +5376,7 @@ posix_fsetxattr (call_frame_t *frame, xlator_t *this,
d1681e
         struct  iatt       stbuf          = {0,};
d1681e
         dict_t            *xattr          = NULL;
d1681e
         posix_xattr_filler_t filler       = {0,};
d1681e
+        struct  posix_private *priv       = NULL;
d1681e
 
d1681e
         DECLARE_OLD_FS_ID_VAR;
d1681e
         SET_FS_ID (frame->root->uid, frame->root->gid);
d1681e
@@ -5355,6 +5386,9 @@ posix_fsetxattr (call_frame_t *frame, xlator_t *this,
d1681e
         VALIDATE_OR_GOTO (fd, out);
d1681e
         VALIDATE_OR_GOTO (dict, out);
d1681e
 
d1681e
+        priv = this->private;
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
+
d1681e
         ret = posix_fd_ctx_get (fd, this, &pfd, &op_errno);
d1681e
         if (ret < 0) {
d1681e
                 gf_msg (this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
d1681e
@@ -6018,11 +6052,17 @@ do_xattrop (call_frame_t *frame, xlator_t *this, loc_t *loc, fd_t *fd,
d1681e
         dict_t               *xattr_rsp = NULL;
d1681e
         dict_t               *xdata_rsp = NULL;
d1681e
         struct iatt           stbuf = {0};
d1681e
+        struct  posix_private *priv     = NULL;
d1681e
+
d1681e
 
d1681e
         VALIDATE_OR_GOTO (frame, out);
d1681e
         VALIDATE_OR_GOTO (xattr, out);
d1681e
         VALIDATE_OR_GOTO (this, out);
d1681e
 
d1681e
+        priv = this->private;
d1681e
+        DISK_SPACE_CHECK_AND_GOTO (frame, priv, op_ret, op_errno, out);
d1681e
+
d1681e
+
d1681e
         if (fd) {
d1681e
                 op_ret = posix_fd_ctx_get (fd, this, &pfd, &op_errno);
d1681e
                 if (op_ret < 0) {
d1681e
@@ -6120,7 +6160,6 @@ posix_fxattrop (call_frame_t *frame, xlator_t *this,
d1681e
         return 0;
d1681e
 }
d1681e
 
d1681e
-
d1681e
 int
d1681e
 posix_access (call_frame_t *frame, xlator_t *this,
d1681e
               loc_t *loc, int32_t mask, dict_t *xdata)
d1681e
@@ -6944,6 +6983,11 @@ notify (xlator_t *this,
d1681e
                         pthread_cancel (priv->health_check);
d1681e
                         priv->health_check = 0;
d1681e
                 }
d1681e
+                if (priv->disk_space_check) {
d1681e
+                        priv->disk_space_check_active = _gf_false;
d1681e
+                        pthread_cancel (priv->disk_space_check);
d1681e
+                        priv->disk_space_check = 0;
d1681e
+                }
d1681e
                 if (priv->janitor) {
d1681e
                         (void) gf_thread_cleanup_xint (priv->janitor);
d1681e
                         priv->janitor = 0;
d1681e
@@ -7140,6 +7184,11 @@ reconfigure (xlator_t *this, dict_t *options)
d1681e
                         " fallback to <hostname>:<export>");
d1681e
         }
d1681e
 
d1681e
+        GF_OPTION_RECONF ("reserve", priv->disk_threshhold,
d1681e
+                          options, uint32, out);
d1681e
+        if (priv->disk_threshhold)
d1681e
+                posix_spawn_disk_space_check_thread (this);
d1681e
+
d1681e
         GF_OPTION_RECONF ("health-check-interval", priv->health_check_interval,
d1681e
                           options, uint32, out);
d1681e
         posix_spawn_health_check_thread (this);
d1681e
@@ -7738,6 +7787,13 @@ init (xlator_t *this)
d1681e
                                 " fallback to <hostname>:<export>");
d1681e
         }
d1681e
 
d1681e
+        _private->disk_space_check_active = _gf_false;
d1681e
+        _private->disk_space_full          = 0;
d1681e
+        GF_OPTION_INIT ("reserve",
d1681e
+                        _private->disk_threshhold, uint32, out);
d1681e
+        if (_private->disk_threshhold)
d1681e
+                posix_spawn_disk_space_check_thread (this);
d1681e
+
d1681e
         _private->health_check_active = _gf_false;
d1681e
         GF_OPTION_INIT ("health-check-interval",
d1681e
                         _private->health_check_interval, uint32, out);
d1681e
@@ -7940,6 +7996,16 @@ struct volume_options options[] = {
d1681e
           .description = "Interval in seconds for a filesystem health check, "
d1681e
                          "set to 0 to disable"
d1681e
         },
d1681e
+        {
d1681e
+          .key = {"reserve"},
d1681e
+          .type = GF_OPTION_TYPE_INT,
d1681e
+          .min = 0,
d1681e
+          .default_value = "1",
d1681e
+          .validate = GF_OPT_VALIDATE_MIN,
d1681e
+          .description = "Value in percentage in integer form required "
d1681e
+           "to set reserve disk, "
d1681e
+           "set to 0 to disable"
d1681e
+        },
d1681e
 	{ .key = {"batch-fsync-mode"},
d1681e
 	  .type = GF_OPTION_TYPE_STR,
d1681e
 	  .default_value = "reverse-fsync",
d1681e
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
d1681e
index c2dcfda..21c7d36 100644
d1681e
--- a/xlators/storage/posix/src/posix.h
d1681e
+++ b/xlators/storage/posix/src/posix.h
d1681e
@@ -63,6 +63,18 @@
d1681e
 #define GF_UNLINK_TRUE 0x0000000000000001
d1681e
 #define GF_UNLINK_FALSE 0x0000000000000000
d1681e
 
d1681e
+#define DISK_SPACE_CHECK_AND_GOTO(frame, priv, op_ret, op_errno, out)  do {   \
d1681e
+               if (frame->root->pid >= 0 && priv->disk_space_full) {          \
d1681e
+                        op_ret = -1;                                          \
d1681e
+                        op_errno = ENOSPC;                                    \
d1681e
+                        gf_msg_debug ("posix", ENOSPC,                        \
d1681e
+                                      "disk space utilization reached limits" \
d1681e
+                                      " for path %s ",  priv->base_path);     \
d1681e
+                        goto out;                                             \
d1681e
+               }                                                              \
d1681e
+        } while (0)
d1681e
+
d1681e
+
d1681e
 /**
d1681e
  * posix_fd - internal structure common to file and directory fd's
d1681e
  */
d1681e
@@ -197,6 +209,11 @@ struct posix_private {
d1681e
         pthread_t       health_check;
d1681e
         gf_boolean_t    health_check_active;
d1681e
 
d1681e
+        uint32_t        disk_threshhold;
d1681e
+        uint32_t        disk_space_full;
d1681e
+        pthread_t       disk_space_check;
d1681e
+        gf_boolean_t    disk_space_check_active;
d1681e
+
d1681e
 #ifdef GF_DARWIN_HOST_OS
d1681e
         enum {
d1681e
                 XATTR_NONE = 0,
d1681e
@@ -304,6 +321,8 @@ __posix_fd_set_odirect (fd_t *fd, struct posix_fd *pfd, int opflags,
d1681e
 			off_t offset, size_t size);
d1681e
 void posix_spawn_health_check_thread (xlator_t *this);
d1681e
 
d1681e
+void posix_spawn_disk_space_check_thread (xlator_t *this);
d1681e
+
d1681e
 void *posix_fsyncer (void *);
d1681e
 int
d1681e
 posix_get_ancestry (xlator_t *this, inode_t *leaf_inode,
d1681e
-- 
d1681e
1.8.3.1
d1681e