190130
From 50318713486e79d9258cf22e656caff402256dde Mon Sep 17 00:00:00 2001
190130
From: Mohit Agrawal <moagrawal@redhat.com>
190130
Date: Sun, 20 Oct 2019 22:01:01 +0530
190130
Subject: [PATCH 389/449] posix: Avoid diskpace error in case of overwriting
190130
 the data
190130
190130
Problem: Sometime fops like posix_writev, posix_fallocate, posix_zerofile
190130
         failed and throw error ENOSPC if storage.reserve threshold limit
190130
         has reached even fops is overwriting the data
190130
190130
Solution: Retry the fops in case of overwrite if diskspace check
190130
          is failed
190130
190130
> Credits: kinsu <vpolakis@gmail.com>
190130
> Change-Id: I987d73bcf47ed1bb27878df40c39751296e95fe8
190130
> Updates: #745
190130
> Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
190130
> (Cherry pick from commit ca3e5905ac02fb9c373ac3de10b44f061d04cd6f)
190130
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/23572/)
190130
190130
Change-Id: I987d73bcf47ed1bb27878df40c39751296e95fe8
190130
BUG: 1787331
190130
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
190130
Reviewed-on: https://code.engineering.redhat.com/gerrit/202307
190130
Tested-by: RHGS Build Bot <nigelb@redhat.com>
190130
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
190130
---
190130
 tests/bugs/posix/bug-1651445.t                 |   1 +
190130
 xlators/storage/posix/src/posix-entry-ops.c    |   1 -
190130
 xlators/storage/posix/src/posix-inode-fd-ops.c | 141 ++++++++++++++++++++++---
190130
 3 files changed, 126 insertions(+), 17 deletions(-)
190130
190130
diff --git a/tests/bugs/posix/bug-1651445.t b/tests/bugs/posix/bug-1651445.t
190130
index 5248d47..4d08b69 100644
190130
--- a/tests/bugs/posix/bug-1651445.t
190130
+++ b/tests/bugs/posix/bug-1651445.t
190130
@@ -33,6 +33,7 @@ sleep 5
190130
 # setup_lvm create lvm partition of 150M and 40M are reserve so after
190130
 # consuming more than 110M next dd should fail
190130
 TEST ! dd if=/dev/zero of=$M0/c bs=5M count=1
190130
+TEST dd if=/dev/urandom of=$M0/a  bs=1022 count=1  oflag=seek_bytes,sync seek=102 conv=notrunc
190130
 
190130
 rm -rf $M0/*
190130
 
190130
diff --git a/xlators/storage/posix/src/posix-entry-ops.c b/xlators/storage/posix/src/posix-entry-ops.c
190130
index 283b305..bea0bbf 100644
190130
--- a/xlators/storage/posix/src/posix-entry-ops.c
190130
+++ b/xlators/storage/posix/src/posix-entry-ops.c
190130
@@ -1634,7 +1634,6 @@ posix_rename(call_frame_t *frame, xlator_t *this, loc_t *oldloc, loc_t *newloc,
190130
 
190130
     priv = this->private;
190130
     VALIDATE_OR_GOTO(priv, out);
190130
-    DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
190130
 
190130
     SET_FS_ID(frame->root->uid, frame->root->gid);
190130
     MAKE_ENTRY_HANDLE(real_oldpath, par_oldpath, this, oldloc, NULL);
190130
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
190130
index a2a518f..bcce06e 100644
190130
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
190130
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
190130
@@ -692,6 +692,10 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
190130
     gf_boolean_t locked = _gf_false;
190130
     posix_inode_ctx_t *ctx = NULL;
190130
     struct posix_private *priv = NULL;
190130
+    gf_boolean_t check_space_error = _gf_false;
190130
+    struct stat statbuf = {
190130
+        0,
190130
+    };
190130
 
190130
     DECLARE_OLD_FS_ID_VAR;
190130
 
190130
@@ -711,7 +715,10 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
190130
     if (priv->disk_reserve)
190130
         posix_disk_space_check(this);
190130
 
190130
-    DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, ret, ret, out);
190130
+    DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, ret, ret, unlock);
190130
+
190130
+overwrite:
190130
+    check_space_error = _gf_true;
190130
 
190130
     ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
190130
     if (ret < 0) {
190130
@@ -735,7 +742,7 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
190130
         ret = -errno;
190130
         gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
190130
                "fallocate (fstat) failed on fd=%p", fd);
190130
-        goto out;
190130
+        goto unlock;
190130
     }
190130
 
190130
     if (xdata) {
190130
@@ -745,7 +752,7 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
190130
             gf_msg(this->name, GF_LOG_ERROR, 0, 0,
190130
                    "file state check failed, fd %p", fd);
190130
             ret = -EIO;
190130
-            goto out;
190130
+            goto unlock;
190130
         }
190130
     }
190130
 
190130
@@ -756,7 +763,7 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
190130
                "fallocate failed on %s offset: %jd, "
190130
                "len:%zu, flags: %d",
190130
                uuid_utoa(fd->inode->gfid), offset, len, flags);
190130
-        goto out;
190130
+        goto unlock;
190130
     }
190130
 
190130
     ret = posix_fdstat(this, fd->inode, pfd->fd, statpost);
190130
@@ -764,16 +771,47 @@ posix_do_fallocate(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags,
190130
         ret = -errno;
190130
         gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_FSTAT_FAILED,
190130
                "fallocate (fstat) failed on fd=%p", fd);
190130
-        goto out;
190130
+        goto unlock;
190130
     }
190130
 
190130
     posix_set_ctime(frame, this, NULL, pfd->fd, fd->inode, statpost);
190130
 
190130
-out:
190130
+unlock:
190130
     if (locked) {
190130
         pthread_mutex_unlock(&ctx->write_atomic_lock);
190130
         locked = _gf_false;
190130
     }
190130
+
190130
+    if (op_errno == ENOSPC && priv->disk_space_full && !check_space_error) {
190130
+#ifdef FALLOC_FL_KEEP_SIZE
190130
+        if (flags & FALLOC_FL_KEEP_SIZE) {
190130
+            goto overwrite;
190130
+        }
190130
+#endif
190130
+        ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
190130
+        if (ret < 0) {
190130
+            gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
190130
+                   "pfd is NULL from fd=%p", fd);
190130
+            goto out;
190130
+        }
190130
+
190130
+        if (sys_fstat(pfd->fd, &statbuf) < 0) {
190130
+            gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_FILE_OP_FAILED,
190130
+                   "%d", pfd->fd);
190130
+            goto out;
190130
+        }
190130
+
190130
+        if (offset + len <= statbuf.st_size) {
190130
+            gf_msg_debug(this->name, 0,
190130
+                         "io vector size will not"
190130
+                         " change disk size so allow overwrite for"
190130
+                         " fd %d",
190130
+                         pfd->fd);
190130
+            goto overwrite;
190130
+        }
190130
+    }
190130
+
190130
+out:
190130
     SET_TO_OLD_FS_ID();
190130
     if (ret == ENOSPC)
190130
         ret = -ENOSPC;
190130
@@ -1083,25 +1121,57 @@ posix_zerofill(call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset,
190130
     int op_ret = -1;
190130
     int op_errno = EINVAL;
190130
     dict_t *rsp_xdata = NULL;
190130
+    gf_boolean_t check_space_error = _gf_false;
190130
+    struct posix_fd *pfd = NULL;
190130
+    struct stat statbuf = {
190130
+        0,
190130
+    };
190130
 
190130
-    VALIDATE_OR_GOTO(frame, out);
190130
-    VALIDATE_OR_GOTO(this, out);
190130
+    VALIDATE_OR_GOTO(frame, unwind);
190130
+    VALIDATE_OR_GOTO(this, unwind);
190130
 
190130
     priv = this->private;
190130
     DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
190130
 
190130
+overwrite:
190130
+    check_space_error = _gf_true;
190130
     ret = posix_do_zerofill(frame, this, fd, offset, len, &statpre, &statpost,
190130
                             xdata, &rsp_xdata);
190130
     if (ret < 0) {
190130
         op_ret = -1;
190130
         op_errno = -ret;
190130
-        goto out;
190130
+        goto unwind;
190130
     }
190130
 
190130
     STACK_UNWIND_STRICT(zerofill, frame, 0, 0, &statpre, &statpost, rsp_xdata);
190130
     return 0;
190130
 
190130
 out:
190130
+    if (op_errno == ENOSPC && priv->disk_space_full && !check_space_error) {
190130
+        ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
190130
+        if (ret < 0) {
190130
+            gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
190130
+                   "pfd is NULL from fd=%p", fd);
190130
+            goto out;
190130
+        }
190130
+
190130
+        if (sys_fstat(pfd->fd, &statbuf) < 0) {
190130
+            gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_FILE_OP_FAILED,
190130
+                   "%d", pfd->fd);
190130
+            goto out;
190130
+        }
190130
+
190130
+        if (offset + len <= statbuf.st_size) {
190130
+            gf_msg_debug(this->name, 0,
190130
+                         "io vector size will not"
190130
+                         " change disk size so allow overwrite for"
190130
+                         " fd %d",
190130
+                         pfd->fd);
190130
+            goto overwrite;
190130
+        }
190130
+    }
190130
+
190130
+unwind:
190130
     STACK_UNWIND_STRICT(zerofill, frame, op_ret, op_errno, NULL, NULL,
190130
                         rsp_xdata);
190130
     return 0;
190130
@@ -1857,19 +1927,28 @@ posix_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
190130
     gf_boolean_t write_append = _gf_false;
190130
     gf_boolean_t update_atomic = _gf_false;
190130
     posix_inode_ctx_t *ctx = NULL;
190130
+    gf_boolean_t check_space_error = _gf_false;
190130
+    struct stat statbuf = {
190130
+        0,
190130
+    };
190130
+    int totlen = 0;
190130
+    int idx = 0;
190130
 
190130
-    VALIDATE_OR_GOTO(frame, out);
190130
-    VALIDATE_OR_GOTO(this, out);
190130
-    VALIDATE_OR_GOTO(fd, out);
190130
-    VALIDATE_OR_GOTO(fd->inode, out);
190130
-    VALIDATE_OR_GOTO(vector, out);
190130
-    VALIDATE_OR_GOTO(this->private, out);
190130
+    VALIDATE_OR_GOTO(frame, unwind);
190130
+    VALIDATE_OR_GOTO(this, unwind);
190130
+    VALIDATE_OR_GOTO(fd, unwind);
190130
+    VALIDATE_OR_GOTO(fd->inode, unwind);
190130
+    VALIDATE_OR_GOTO(vector, unwind);
190130
+    VALIDATE_OR_GOTO(this->private, unwind);
190130
 
190130
     priv = this->private;
190130
 
190130
-    VALIDATE_OR_GOTO(priv, out);
190130
+    VALIDATE_OR_GOTO(priv, unwind);
190130
     DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
190130
 
190130
+overwrite:
190130
+
190130
+    check_space_error = _gf_true;
190130
     if ((fd->inode->ia_type == IA_IFBLK) || (fd->inode->ia_type == IA_IFCHR)) {
190130
         gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_INVALID_ARGUMENT,
190130
                "writev received on a block/char file (%s)",
190130
@@ -2011,6 +2090,36 @@ out:
190130
         locked = _gf_false;
190130
     }
190130
 
190130
+    if (op_errno == ENOSPC && priv->disk_space_full && !check_space_error) {
190130
+        ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
190130
+        if (ret < 0) {
190130
+            gf_msg(this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
190130
+                   "pfd is NULL from fd=%p", fd);
190130
+            goto unwind;
190130
+        }
190130
+
190130
+        if (sys_fstat(pfd->fd, &statbuf) < 0) {
190130
+            gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_FILE_OP_FAILED,
190130
+                   "%d", pfd->fd);
190130
+            goto unwind;
190130
+        }
190130
+
190130
+        for (idx = 0; idx < count; idx++) {
190130
+            totlen = vector[idx].iov_len;
190130
+        }
190130
+
190130
+        if ((offset + totlen <= statbuf.st_size) &&
190130
+            !(statbuf.st_blocks * statbuf.st_blksize < statbuf.st_size)) {
190130
+            gf_msg_debug(this->name, 0,
190130
+                         "io vector size will not"
190130
+                         " change disk size so allow overwrite for"
190130
+                         " fd %d",
190130
+                         pfd->fd);
190130
+            goto overwrite;
190130
+        }
190130
+    }
190130
+
190130
+unwind:
190130
     STACK_UNWIND_STRICT(writev, frame, op_ret, op_errno, &preop, &postop,
190130
                         rsp_xdata);
190130
 
190130
-- 
190130
1.8.3.1
190130