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