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