f338ef
From 546f412c155dd5aca2b3cd4202f80c9977b215dc Mon Sep 17 00:00:00 2001
f338ef
From: Pranith Kumar K <pkarampu@redhat.com>
f338ef
Date: Wed, 4 Sep 2019 12:06:34 +0530
f338ef
Subject: [PATCH 287/297] cluster/ec: Fail fsync/flush for files on update
f338ef
 size/version failure
f338ef
f338ef
Problem:
f338ef
If update size/version is not successful on the file, updates on the
f338ef
same stripe could lead to data corruptions if the earlier un-aligned
f338ef
write is not successful on all the bricks. Application won't have
f338ef
any knowledge of this because update size/version happens in the
f338ef
background.
f338ef
f338ef
Fix:
f338ef
Fail fsync/flush on fds that are opened before update-size-version
f338ef
went bad.
f338ef
f338ef
Upstream-patch: https://review.gluster.org/c/glusterfs/+/23355
f338ef
fixes: bz#1745107
f338ef
Change-Id: I9d323eddcda703bd27d55f340c4079d76e06e492
f338ef
Signed-off-by: Pranith Kumar K <pkarampu@redhat.com>
f338ef
Reviewed-on: https://code.engineering.redhat.com/gerrit/180672
f338ef
Tested-by: RHGS Build Bot <nigelb@redhat.com>
f338ef
Reviewed-by: Ashish Pandey <aspandey@redhat.com>
f338ef
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
f338ef
---
f338ef
 tests/basic/ec/ec-badfd.c            | 124 +++++++++++++++++++++++++++++++++++
f338ef
 tests/basic/ec/ec-badfd.t            |  26 ++++++++
f338ef
 xlators/cluster/ec/src/ec-common.c   |  23 +++++++
f338ef
 xlators/cluster/ec/src/ec-generic.c  |  47 +++++++++++++
f338ef
 xlators/cluster/ec/src/ec-helpers.c  |   7 ++
f338ef
 xlators/cluster/ec/src/ec-messages.h |   2 +-
f338ef
 xlators/cluster/ec/src/ec-types.h    |   2 +
f338ef
 7 files changed, 230 insertions(+), 1 deletion(-)
f338ef
 create mode 100644 tests/basic/ec/ec-badfd.c
f338ef
 create mode 100755 tests/basic/ec/ec-badfd.t
f338ef
f338ef
diff --git a/tests/basic/ec/ec-badfd.c b/tests/basic/ec/ec-badfd.c
f338ef
new file mode 100644
f338ef
index 0000000..8be23c1
f338ef
--- /dev/null
f338ef
+++ b/tests/basic/ec/ec-badfd.c
f338ef
@@ -0,0 +1,124 @@
f338ef
+#include <stdio.h>
f338ef
+#include <fcntl.h>
f338ef
+#include <unistd.h>
f338ef
+#include <time.h>
f338ef
+#include <limits.h>
f338ef
+#include <string.h>
f338ef
+#include <stdlib.h>
f338ef
+#include <errno.h>
f338ef
+#include <glusterfs/api/glfs.h>
f338ef
+#include <glusterfs/api/glfs-handles.h>
f338ef
+
f338ef
+int
f338ef
+fill_iov(struct iovec *iov, char fillchar, int count)
f338ef
+{
f338ef
+    int ret = -1;
f338ef
+
f338ef
+    iov->iov_base = malloc(count + 1);
f338ef
+    if (iov->iov_base == NULL) {
f338ef
+        return ret;
f338ef
+    } else {
f338ef
+        iov->iov_len = count;
f338ef
+        ret = 0;
f338ef
+    }
f338ef
+    memset(iov->iov_base, fillchar, count);
f338ef
+    memset(iov->iov_base + count, '\0', 1);
f338ef
+
f338ef
+    return ret;
f338ef
+}
f338ef
+
f338ef
+int
f338ef
+write_sync(glfs_t *fs, glfs_fd_t *glfd, int char_count)
f338ef
+{
f338ef
+    ssize_t ret = -1;
f338ef
+    int flags = O_RDWR;
f338ef
+    struct iovec iov = {0};
f338ef
+
f338ef
+    ret = fill_iov(&iov, 'a', char_count);
f338ef
+    if (ret) {
f338ef
+        fprintf(stderr, "failed to create iov");
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
+    ret = glfs_pwritev(glfd, &iov, 1, 0, flags);
f338ef
+out:
f338ef
+    if (ret < 0) {
f338ef
+        fprintf(stderr, "glfs_pwritev failed, %d", errno);
f338ef
+    }
f338ef
+    return ret;
f338ef
+}
f338ef
+
f338ef
+int
f338ef
+main(int argc, char *argv[])
f338ef
+{
f338ef
+    glfs_t *fs = NULL;
f338ef
+    glfs_fd_t *fd = NULL;
f338ef
+    int ret = 1;
f338ef
+    char volume_cmd[4096] = {0};
f338ef
+
f338ef
+    if (argc != 4) {
f338ef
+        fprintf(stderr, "Syntax: %s <host> <volname> <file>\n", argv[0]);
f338ef
+        return 1;
f338ef
+    }
f338ef
+
f338ef
+    fs = glfs_new(argv[2]);
f338ef
+    if (!fs) {
f338ef
+        fprintf(stderr, "glfs_new: returned NULL\n");
f338ef
+        return 1;
f338ef
+    }
f338ef
+
f338ef
+    ret = glfs_set_volfile_server(fs, "tcp", argv[1], 24007);
f338ef
+    if (ret != 0) {
f338ef
+        fprintf(stderr, "glfs_set_volfile_server: returned %d\n", ret);
f338ef
+        goto out;
f338ef
+    }
f338ef
+    ret = glfs_set_logging(fs, "/tmp/ec-badfd.log", 7);
f338ef
+    if (ret != 0) {
f338ef
+        fprintf(stderr, "glfs_set_logging: returned %d\n", ret);
f338ef
+        goto out;
f338ef
+    }
f338ef
+    ret = glfs_init(fs);
f338ef
+    if (ret != 0) {
f338ef
+        fprintf(stderr, "glfs_init: returned %d\n", ret);
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
+    fd = glfs_open(fs, argv[3], O_RDWR);
f338ef
+    if (fd == NULL) {
f338ef
+        fprintf(stderr, "glfs_open: returned NULL\n");
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
+    ret = write_sync(fs, fd, 16);
f338ef
+    if (ret < 0) {
f338ef
+        fprintf(stderr, "write_sync failed\n");
f338ef
+    }
f338ef
+
f338ef
+    snprintf(volume_cmd, sizeof(volume_cmd),
f338ef
+             "gluster --mode=script volume stop %s", argv[2]);
f338ef
+    /*Stop the volume so that update-size-version fails*/
f338ef
+    system(volume_cmd);
f338ef
+    sleep(8); /* 3 seconds more than eager-lock-timeout*/
f338ef
+    snprintf(volume_cmd, sizeof(volume_cmd),
f338ef
+             "gluster --mode=script volume start %s", argv[2]);
f338ef
+    system(volume_cmd);
f338ef
+    sleep(8); /*wait for bricks to come up*/
f338ef
+    ret = glfs_fsync(fd, NULL, NULL);
f338ef
+    if (ret == 0) {
f338ef
+        fprintf(stderr, "fsync succeeded on a BADFD\n");
f338ef
+        exit(1);
f338ef
+    }
f338ef
+
f338ef
+    ret = glfs_close(fd);
f338ef
+    if (ret == 0) {
f338ef
+        fprintf(stderr, "flush succeeded on a BADFD\n");
f338ef
+        exit(1);
f338ef
+    }
f338ef
+    ret = 0;
f338ef
+
f338ef
+out:
f338ef
+    unlink("/tmp/ec-badfd.log");
f338ef
+    glfs_fini(fs);
f338ef
+
f338ef
+    return ret;
f338ef
+}
f338ef
diff --git a/tests/basic/ec/ec-badfd.t b/tests/basic/ec/ec-badfd.t
f338ef
new file mode 100755
f338ef
index 0000000..56feb47
f338ef
--- /dev/null
f338ef
+++ b/tests/basic/ec/ec-badfd.t
f338ef
@@ -0,0 +1,26 @@
f338ef
+#!/bin/bash
f338ef
+
f338ef
+. $(dirname $0)/../../include.rc
f338ef
+. $(dirname $0)/../../volume.rc
f338ef
+
f338ef
+cleanup;
f338ef
+
f338ef
+TEST glusterd
f338ef
+TEST pidof glusterd
f338ef
+
f338ef
+TEST $CLI volume create $V0 disperse 6 redundancy 2 $H0:$B0/${V0}{1..6}
f338ef
+TEST $CLI volume set $V0 performance.write-behind off
f338ef
+TEST $CLI volume set $V0 disperse.eager-lock-timeout 5
f338ef
+
f338ef
+TEST $CLI volume start $V0
f338ef
+EXPECT 'Started' volinfo_field $V0 'Status'
f338ef
+
f338ef
+TEST $GFS -s $H0 --volfile-id $V0 $M0
f338ef
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "6" ec_child_up_count $V0 0
f338ef
+TEST touch $M0/file
f338ef
+
f338ef
+TEST build_tester $(dirname $0)/ec-badfd.c -lgfapi -Wall -O2
f338ef
+TEST $(dirname $0)/ec-badfd $H0 $V0 /file
f338ef
+cleanup_tester $(dirname ${0})/ec-badfd
f338ef
+
f338ef
+cleanup;
f338ef
diff --git a/xlators/cluster/ec/src/ec-common.c b/xlators/cluster/ec/src/ec-common.c
f338ef
index 5fb4610..92d4e5d 100644
f338ef
--- a/xlators/cluster/ec/src/ec-common.c
f338ef
+++ b/xlators/cluster/ec/src/ec-common.c
f338ef
@@ -2255,6 +2255,23 @@ ec_unlock_lock(ec_lock_link_t *link)
f338ef
     }
f338ef
 }
f338ef
 
f338ef
+void
f338ef
+ec_inode_bad_inc(inode_t *inode, xlator_t *xl)
f338ef
+{
f338ef
+    ec_inode_t *ctx = NULL;
f338ef
+
f338ef
+    LOCK(&inode->lock);
f338ef
+    {
f338ef
+        ctx = __ec_inode_get(inode, xl);
f338ef
+        if (ctx == NULL) {
f338ef
+            goto unlock;
f338ef
+        }
f338ef
+        ctx->bad_version++;
f338ef
+    }
f338ef
+unlock:
f338ef
+    UNLOCK(&inode->lock);
f338ef
+}
f338ef
+
f338ef
 int32_t
f338ef
 ec_update_size_version_done(call_frame_t *frame, void *cookie, xlator_t *this,
f338ef
                             int32_t op_ret, int32_t op_errno, dict_t *xattr,
f338ef
@@ -2270,6 +2287,12 @@ ec_update_size_version_done(call_frame_t *frame, void *cookie, xlator_t *this,
f338ef
     ctx = lock->ctx;
f338ef
 
f338ef
     if (op_ret < 0) {
f338ef
+        if (link->lock->fd == NULL) {
f338ef
+            ec_inode_bad_inc(link->lock->loc.inode, this);
f338ef
+        } else {
f338ef
+            ec_inode_bad_inc(link->lock->fd->inode, this);
f338ef
+        }
f338ef
+
f338ef
         gf_msg(fop->xl->name, fop_log_level(fop->id, op_errno), op_errno,
f338ef
                EC_MSG_SIZE_VERS_UPDATE_FAIL,
f338ef
                "Failed to update version and size. %s", ec_msg_str(fop));
f338ef
diff --git a/xlators/cluster/ec/src/ec-generic.c b/xlators/cluster/ec/src/ec-generic.c
f338ef
index acc16b5..b019050 100644
f338ef
--- a/xlators/cluster/ec/src/ec-generic.c
f338ef
+++ b/xlators/cluster/ec/src/ec-generic.c
f338ef
@@ -150,6 +150,37 @@ ec_manager_flush(ec_fop_data_t *fop, int32_t state)
f338ef
     }
f338ef
 }
f338ef
 
f338ef
+static int32_t
f338ef
+ec_validate_fd(fd_t *fd, xlator_t *xl)
f338ef
+{
f338ef
+    uint64_t iversion = 0;
f338ef
+    uint64_t fversion = 0;
f338ef
+    ec_inode_t *inode_ctx = NULL;
f338ef
+    ec_fd_t *fd_ctx = NULL;
f338ef
+
f338ef
+    LOCK(&fd->lock);
f338ef
+    {
f338ef
+        fd_ctx = __ec_fd_get(fd, xl);
f338ef
+        if (fd_ctx) {
f338ef
+            fversion = fd_ctx->bad_version;
f338ef
+        }
f338ef
+    }
f338ef
+    UNLOCK(&fd->lock);
f338ef
+
f338ef
+    LOCK(&fd->inode->lock);
f338ef
+    {
f338ef
+        inode_ctx = __ec_inode_get(fd->inode, xl);
f338ef
+        if (inode_ctx) {
f338ef
+            iversion = inode_ctx->bad_version;
f338ef
+        }
f338ef
+    }
f338ef
+    UNLOCK(&fd->inode->lock);
f338ef
+    if (fversion < iversion) {
f338ef
+        return EBADF;
f338ef
+    }
f338ef
+    return 0;
f338ef
+}
f338ef
+
f338ef
 void
f338ef
 ec_flush(call_frame_t *frame, xlator_t *this, uintptr_t target,
f338ef
          uint32_t fop_flags, fop_flush_cbk_t func, void *data, fd_t *fd,
f338ef
@@ -165,6 +196,14 @@ ec_flush(call_frame_t *frame, xlator_t *this, uintptr_t target,
f338ef
     GF_VALIDATE_OR_GOTO(this->name, frame, out);
f338ef
     GF_VALIDATE_OR_GOTO(this->name, this->private, out);
f338ef
 
f338ef
+    error = ec_validate_fd(fd, this);
f338ef
+    if (error) {
f338ef
+        gf_msg(this->name, GF_LOG_ERROR, EBADF, EC_MSG_FD_BAD,
f338ef
+               "Failing %s on %s", gf_fop_list[GF_FOP_FLUSH],
f338ef
+               fd->inode ? uuid_utoa(fd->inode->gfid) : "");
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
     fop = ec_fop_data_allocate(frame, this, GF_FOP_FLUSH, 0, target, fop_flags,
f338ef
                                ec_wind_flush, ec_manager_flush, callback, data);
f338ef
     if (fop == NULL) {
f338ef
@@ -381,6 +420,14 @@ ec_fsync(call_frame_t *frame, xlator_t *this, uintptr_t target,
f338ef
     GF_VALIDATE_OR_GOTO(this->name, frame, out);
f338ef
     GF_VALIDATE_OR_GOTO(this->name, this->private, out);
f338ef
 
f338ef
+    error = ec_validate_fd(fd, this);
f338ef
+    if (error) {
f338ef
+        gf_msg(this->name, GF_LOG_ERROR, EBADF, EC_MSG_FD_BAD,
f338ef
+               "Failing %s on %s", gf_fop_list[GF_FOP_FSYNC],
f338ef
+               fd->inode ? uuid_utoa(fd->inode->gfid) : "");
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
     fop = ec_fop_data_allocate(frame, this, GF_FOP_FSYNC, 0, target, fop_flags,
f338ef
                                ec_wind_fsync, ec_manager_fsync, callback, data);
f338ef
     if (fop == NULL) {
f338ef
diff --git a/xlators/cluster/ec/src/ec-helpers.c b/xlators/cluster/ec/src/ec-helpers.c
f338ef
index 43f6e3b..baac001 100644
f338ef
--- a/xlators/cluster/ec/src/ec-helpers.c
f338ef
+++ b/xlators/cluster/ec/src/ec-helpers.c
f338ef
@@ -753,6 +753,7 @@ __ec_fd_get(fd_t *fd, xlator_t *xl)
f338ef
 {
f338ef
     int i = 0;
f338ef
     ec_fd_t *ctx = NULL;
f338ef
+    ec_inode_t *ictx = NULL;
f338ef
     uint64_t value = 0;
f338ef
     ec_t *ec = xl->private;
f338ef
 
f338ef
@@ -775,6 +776,12 @@ __ec_fd_get(fd_t *fd, xlator_t *xl)
f338ef
                 GF_FREE(ctx);
f338ef
                 return NULL;
f338ef
             }
f338ef
+            /* Only refering bad-version so no need for lock
f338ef
+             * */
f338ef
+            ictx = __ec_inode_get(fd->inode, xl);
f338ef
+            if (ictx) {
f338ef
+                ctx->bad_version = ictx->bad_version;
f338ef
+            }
f338ef
         }
f338ef
     } else {
f338ef
         ctx = (ec_fd_t *)(uintptr_t)value;
f338ef
diff --git a/xlators/cluster/ec/src/ec-messages.h b/xlators/cluster/ec/src/ec-messages.h
f338ef
index 7c28808..be86b37 100644
f338ef
--- a/xlators/cluster/ec/src/ec-messages.h
f338ef
+++ b/xlators/cluster/ec/src/ec-messages.h
f338ef
@@ -55,6 +55,6 @@ GLFS_MSGID(EC, EC_MSG_INVALID_CONFIG, EC_MSG_HEAL_FAIL,
f338ef
            EC_MSG_CONFIG_XATTR_INVALID, EC_MSG_EXTENSION, EC_MSG_EXTENSION_NONE,
f338ef
            EC_MSG_EXTENSION_UNKNOWN, EC_MSG_EXTENSION_UNSUPPORTED,
f338ef
            EC_MSG_EXTENSION_FAILED, EC_MSG_NO_GF, EC_MSG_MATRIX_FAILED,
f338ef
-           EC_MSG_DYN_CREATE_FAILED, EC_MSG_DYN_CODEGEN_FAILED);
f338ef
+           EC_MSG_DYN_CREATE_FAILED, EC_MSG_DYN_CODEGEN_FAILED, EC_MSG_FD_BAD);
f338ef
 
f338ef
 #endif /* !_EC_MESSAGES_H_ */
f338ef
diff --git a/xlators/cluster/ec/src/ec-types.h b/xlators/cluster/ec/src/ec-types.h
f338ef
index 1c295c0..f27f2ec 100644
f338ef
--- a/xlators/cluster/ec/src/ec-types.h
f338ef
+++ b/xlators/cluster/ec/src/ec-types.h
f338ef
@@ -150,6 +150,7 @@ struct _ec_fd {
f338ef
     loc_t loc;
f338ef
     uintptr_t open;
f338ef
     int32_t flags;
f338ef
+    uint64_t bad_version;
f338ef
     ec_fd_status_t fd_status[0];
f338ef
 };
f338ef
 
f338ef
@@ -180,6 +181,7 @@ struct _ec_inode {
f338ef
     uint64_t dirty[2];
f338ef
     struct list_head heal;
f338ef
     ec_stripe_list_t stripe_cache;
f338ef
+    uint64_t bad_version;
f338ef
 };
f338ef
 
f338ef
 typedef int32_t (*fop_heal_cbk_t)(call_frame_t *, void *, xlator_t *, int32_t,
f338ef
-- 
f338ef
1.8.3.1
f338ef