14f8ab
From 584ee2dbb8158ee3d3c3f055f1b06ff3d9177192 Mon Sep 17 00:00:00 2001
14f8ab
From: Kotresh HR <khiremat@redhat.com>
14f8ab
Date: Thu, 13 Jun 2019 16:23:21 +0530
14f8ab
Subject: [PATCH 196/221] posix/ctime: Fix ctime upgrade issue
14f8ab
14f8ab
Problem:
14f8ab
On a EC volume, during upgrade from the older version where
14f8ab
ctime feature is not enabled(or not present) to the newer
14f8ab
version where the ctime feature is available (enabled default),
14f8ab
the self heal hangs and doesn't complete.
14f8ab
14f8ab
Cause:
14f8ab
The ctime feature has both client side code (utime) and
14f8ab
server side code (posix). The feature is driven from client.
14f8ab
Only if the client side sets the time in the frame, should
14f8ab
the server side sets the time attributes in xattr. But posix
14f8ab
setattr/fseattr was not doing that. When one of the server
14f8ab
nodes is updated, since ctime is enabled by default, it
14f8ab
starts setting xattr on setattr/fseattr on the updated node/brick.
14f8ab
14f8ab
On a EC volume the first two updated nodes(bricks) are not a
14f8ab
problem because there are 4 other bricks with consistent data.
14f8ab
However once the third brick is updated, the new attribute(mdata xattr)
14f8ab
will cause an inconsistency on metadata on 3 bricks, which
14f8ab
prevents the file to be repaired.
14f8ab
14f8ab
Fix:
14f8ab
Don't create mdata xattr with utimes/utimensat system call.
14f8ab
Only update if already present.
14f8ab
14f8ab
Backport of:
14f8ab
 > Patch: https://review.gluster.org/22858
14f8ab
 > Change-Id: Ieacedecb8a738bb437283ef3e0f042fd49dc4c8c
14f8ab
 > fixes: bz#1720201
14f8ab
 > Signed-off-by: Kotresh HR <khiremat@redhat.com>
14f8ab
14f8ab
Change-Id: Ieacedecb8a738bb437283ef3e0f042fd49dc4c8c
14f8ab
BUG: 1713664
14f8ab
Signed-off-by: Kotresh HR <khiremat@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/174238
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
14f8ab
---
14f8ab
 tests/basic/afr/split-brain-healing.t      |  36 ++++---
14f8ab
 tests/utils/get-mdata-xattr.c              | 152 +++++++++++++++++++++++++++++
14f8ab
 tests/volume.rc                            |  30 ++++++
14f8ab
 xlators/storage/posix/src/posix-metadata.c |  21 ++++
14f8ab
 4 files changed, 223 insertions(+), 16 deletions(-)
14f8ab
 create mode 100644 tests/utils/get-mdata-xattr.c
14f8ab
14f8ab
diff --git a/tests/basic/afr/split-brain-healing.t b/tests/basic/afr/split-brain-healing.t
14f8ab
index c80f900..78553e6 100644
14f8ab
--- a/tests/basic/afr/split-brain-healing.t
14f8ab
+++ b/tests/basic/afr/split-brain-healing.t
14f8ab
@@ -20,11 +20,14 @@ function get_replicate_subvol_number {
14f8ab
 cleanup;
14f8ab
 
14f8ab
 AREQUAL_PATH=$(dirname $0)/../../utils
14f8ab
+GET_MDATA_PATH=$(dirname $0)/../../utils
14f8ab
 CFLAGS=""
14f8ab
 test "`uname -s`" != "Linux" && {
14f8ab
     CFLAGS="$CFLAGS -lintl";
14f8ab
 }
14f8ab
 build_tester $AREQUAL_PATH/arequal-checksum.c $CFLAGS
14f8ab
+build_tester $GET_MDATA_PATH/get-mdata-xattr.c
14f8ab
+
14f8ab
 TEST glusterd
14f8ab
 TEST pidof glusterd
14f8ab
 TEST $CLI volume create $V0 replica 2 $H0:$B0/${V0}{1,2,3,4}
14f8ab
@@ -152,13 +155,13 @@ EXPECT $SMALLER_FILE_SIZE stat -c %s file4
14f8ab
 subvolume=$(get_replicate_subvol_number file5)
14f8ab
 if [ $subvolume == 0 ]
14f8ab
 then
14f8ab
-        mtime1=$(stat -c %Y $B0/${V0}1/file5)
14f8ab
-        mtime2=$(stat -c %Y $B0/${V0}2/file5)
14f8ab
+        mtime1=$(get_mtime $B0/${V0}1/file5)
14f8ab
+        mtime2=$(get_mtime $B0/${V0}2/file5)
14f8ab
         LATEST_MTIME=$(($mtime1 > $mtime2 ? $mtime1:$mtime2))
14f8ab
 elif [ $subvolume == 1 ]
14f8ab
 then
14f8ab
-        mtime1=$(stat -c %Y $B0/${V0}3/file5)
14f8ab
-        mtime2=$(stat -c %Y $B0/${V0}4/file5)
14f8ab
+        mtime1=$(get_mtime $B0/${V0}3/file5)
14f8ab
+        mtime2=$(get_mtime $B0/${V0}4/file5)
14f8ab
         LATEST_MTIME=$(($mtime1 > $mtime2 ? $mtime1:$mtime2))
14f8ab
 fi
14f8ab
 $CLI volume heal $V0 split-brain latest-mtime /file5
14f8ab
@@ -166,12 +169,12 @@ EXPECT "0" echo $?
14f8ab
 
14f8ab
 if [ $subvolume == 0 ]
14f8ab
 then
14f8ab
-        mtime1_after_heal=$(stat -c %Y $B0/${V0}1/file5)
14f8ab
-        mtime2_after_heal=$(stat -c %Y $B0/${V0}2/file5)
14f8ab
+        mtime1_after_heal=$(get_mtime $B0/${V0}1/file5)
14f8ab
+        mtime2_after_heal=$(get_mtime $B0/${V0}2/file5)
14f8ab
 elif [ $subvolume == 1 ]
14f8ab
 then
14f8ab
-        mtime1_after_heal=$(stat -c %Y $B0/${V0}3/file5)
14f8ab
-        mtime2_after_heal=$(stat -c %Y $B0/${V0}4/file5)
14f8ab
+        mtime1_after_heal=$(get_mtime $B0/${V0}3/file5)
14f8ab
+        mtime2_after_heal=$(get_mtime $B0/${V0}4/file5)
14f8ab
 fi
14f8ab
 
14f8ab
 #TODO: To below comparisons on full sub-second resolution
14f8ab
@@ -188,14 +191,14 @@ subvolume=$(get_replicate_subvol_number file6)
14f8ab
 if [ $subvolume == 0 ]
14f8ab
 then
14f8ab
         GFID=$(gf_get_gfid_xattr $B0/${V0}1/file6)
14f8ab
-        mtime1=$(stat -c %Y $B0/${V0}1/file6)
14f8ab
-        mtime2=$(stat -c %Y $B0/${V0}2/file6)
14f8ab
+        mtime1=$(get_mtime $B0/${V0}1/file6)
14f8ab
+        mtime2=$(get_mtime $B0/${V0}2/file6)
14f8ab
         LATEST_MTIME=$(($mtime1 > $mtime2 ? $mtime1:$mtime2))
14f8ab
 elif [ $subvolume == 1 ]
14f8ab
 then
14f8ab
         GFID=$(gf_get_gfid_xattr $B0/${V0}3/file6)
14f8ab
-        mtime1=$(stat -c %Y $B0/${V0}3/file6)
14f8ab
-        mtime2=$(stat -c %Y $B0/${V0}4/file6)
14f8ab
+        mtime1=$(get_mtime $B0/${V0}3/file6)
14f8ab
+        mtime2=$(get_mtime $B0/${V0}4/file6)
14f8ab
         LATEST_MTIME=$(($mtime1 > $mtime2 ? $mtime1:$mtime2))
14f8ab
 fi
14f8ab
 GFIDSTR="gfid:$(gf_gfid_xattr_to_str $GFID)"
14f8ab
@@ -204,12 +207,12 @@ EXPECT "0" echo $?
14f8ab
 
14f8ab
 if [ $subvolume == 0 ]
14f8ab
 then
14f8ab
-        mtime1_after_heal=$(stat -c %Y $B0/${V0}1/file6)
14f8ab
-        mtime2_after_heal=$(stat -c %Y $B0/${V0}2/file6)
14f8ab
+        mtime1_after_heal=$(get_mtime $B0/${V0}1/file6)
14f8ab
+        mtime2_after_heal=$(get_mtime $B0/${V0}2/file6)
14f8ab
 elif [ $subvolume == 1 ]
14f8ab
 then
14f8ab
-        mtime1_after_heal=$(stat -c %Y $B0/${V0}3/file6)
14f8ab
-        mtime2_after_heal=$(stat -c %Y $B0/${V0}4/file6)
14f8ab
+        mtime1_after_heal=$(get_mtime $B0/${V0}3/file6)
14f8ab
+        mtime2_after_heal=$(get_mtime $B0/${V0}4/file6)
14f8ab
 fi
14f8ab
 
14f8ab
 #TODO: To below comparisons on full sub-second resolution
14f8ab
@@ -253,4 +256,5 @@ EXPECT "1" echo $?
14f8ab
 
14f8ab
 cd -
14f8ab
 TEST rm $AREQUAL_PATH/arequal-checksum
14f8ab
+TEST rm $GET_MDATA_PATH/get-mdata-xattr
14f8ab
 cleanup
14f8ab
diff --git a/tests/utils/get-mdata-xattr.c b/tests/utils/get-mdata-xattr.c
14f8ab
new file mode 100644
14f8ab
index 0000000..e9f5471
14f8ab
--- /dev/null
14f8ab
+++ b/tests/utils/get-mdata-xattr.c
14f8ab
@@ -0,0 +1,152 @@
14f8ab
+/*
14f8ab
+   Copyright (c) 2019 Red Hat, Inc. <http://www.redhat.com>
14f8ab
+   This file is part of GlusterFS.
14f8ab
+
14f8ab
+   This file is licensed to you under your choice of the GNU Lesser
14f8ab
+   General Public License, version 3 or any later version (LGPLv3 or
14f8ab
+   later), or the GNU General Public License, version 2 (GPLv2), in all
14f8ab
+   cases as published by the Free Software Foundation.
14f8ab
+*/
14f8ab
+
14f8ab
+#include <stdlib.h>
14f8ab
+#include <endian.h>
14f8ab
+#include <stdio.h>
14f8ab
+#include <time.h>
14f8ab
+#include <string.h>
14f8ab
+#include <inttypes.h>
14f8ab
+#include <sys/types.h>
14f8ab
+#include <sys/xattr.h>
14f8ab
+#include <errno.h>
14f8ab
+
14f8ab
+typedef struct gf_timespec_disk {
14f8ab
+    uint64_t tv_sec;
14f8ab
+    uint64_t tv_nsec;
14f8ab
+} gf_timespec_disk_t;
14f8ab
+
14f8ab
+/* posix_mdata_t on disk structure */
14f8ab
+typedef struct __attribute__((__packed__)) posix_mdata_disk {
14f8ab
+    /* version of structure, bumped up if any new member is added */
14f8ab
+    uint8_t version;
14f8ab
+    /* flags indicates valid fields in the structure */
14f8ab
+    uint64_t flags;
14f8ab
+    gf_timespec_disk_t ctime;
14f8ab
+    gf_timespec_disk_t mtime;
14f8ab
+    gf_timespec_disk_t atime;
14f8ab
+} posix_mdata_disk_t;
14f8ab
+
14f8ab
+/* In memory representation posix metadata xattr */
14f8ab
+typedef struct {
14f8ab
+    /* version of structure, bumped up if any new member is added */
14f8ab
+    uint8_t version;
14f8ab
+    /* flags indicates valid fields in the structure */
14f8ab
+    uint64_t flags;
14f8ab
+    struct timespec ctime;
14f8ab
+    struct timespec mtime;
14f8ab
+    struct timespec atime;
14f8ab
+} posix_mdata_t;
14f8ab
+
14f8ab
+#define GF_XATTR_MDATA_KEY "trusted.glusterfs.mdata"
14f8ab
+
14f8ab
+/* posix_mdata_from_disk converts posix_mdata_disk_t into host byte order
14f8ab
+ */
14f8ab
+static inline void
14f8ab
+posix_mdata_from_disk(posix_mdata_t *out, posix_mdata_disk_t *in)
14f8ab
+{
14f8ab
+    out->version = in->version;
14f8ab
+    out->flags = be64toh(in->flags);
14f8ab
+
14f8ab
+    out->ctime.tv_sec = be64toh(in->ctime.tv_sec);
14f8ab
+    out->ctime.tv_nsec = be64toh(in->ctime.tv_nsec);
14f8ab
+
14f8ab
+    out->mtime.tv_sec = be64toh(in->mtime.tv_sec);
14f8ab
+    out->mtime.tv_nsec = be64toh(in->mtime.tv_nsec);
14f8ab
+
14f8ab
+    out->atime.tv_sec = be64toh(in->atime.tv_sec);
14f8ab
+    out->atime.tv_nsec = be64toh(in->atime.tv_nsec);
14f8ab
+}
14f8ab
+
14f8ab
+/* posix_fetch_mdata_xattr fetches the posix_mdata_t from disk */
14f8ab
+static int
14f8ab
+posix_fetch_mdata_xattr(const char *real_path, posix_mdata_t *metadata)
14f8ab
+{
14f8ab
+    size_t size = -1;
14f8ab
+    char *value = NULL;
14f8ab
+    char gfid_str[64] = {0};
14f8ab
+
14f8ab
+    char *key = GF_XATTR_MDATA_KEY;
14f8ab
+
14f8ab
+    if (!metadata || !real_path) {
14f8ab
+        goto err;
14f8ab
+    }
14f8ab
+
14f8ab
+    /* Get size */
14f8ab
+    size = lgetxattr(real_path, key, NULL, 0);
14f8ab
+    if (size == -1) {
14f8ab
+        goto err;
14f8ab
+    }
14f8ab
+
14f8ab
+    value = calloc(size + 1, sizeof(char));
14f8ab
+    if (!value) {
14f8ab
+        goto err;
14f8ab
+    }
14f8ab
+
14f8ab
+    /* Get xattr value */
14f8ab
+    size = lgetxattr(real_path, key, value, size);
14f8ab
+    if (size == -1) {
14f8ab
+        goto err;
14f8ab
+    }
14f8ab
+    posix_mdata_from_disk(metadata, (posix_mdata_disk_t *)value);
14f8ab
+
14f8ab
+out:
14f8ab
+    if (value)
14f8ab
+        free(value);
14f8ab
+    return 0;
14f8ab
+err:
14f8ab
+    if (value)
14f8ab
+        free(value);
14f8ab
+    return -1;
14f8ab
+}
14f8ab
+
14f8ab
+int
14f8ab
+main(int argc, char *argv[])
14f8ab
+{
14f8ab
+    posix_mdata_t metadata;
14f8ab
+    uint64_t result;
14f8ab
+
14f8ab
+    if (argc != 3) {
14f8ab
+        /*
14f8ab
+        Usage: get_mdata_xattr -c|-m|-a <file-name>
14f8ab
+                       where -c --> ctime
14f8ab
+                             -m --> mtime
14f8ab
+                             -a --> atime
14f8ab
+        */
14f8ab
+        printf("-1");
14f8ab
+        goto err;
14f8ab
+    }
14f8ab
+
14f8ab
+    if (posix_fetch_mdata_xattr(argv[2], &metadata)) {
14f8ab
+        printf("-1");
14f8ab
+        goto err;
14f8ab
+    }
14f8ab
+
14f8ab
+    switch (argv[1][1]) {
14f8ab
+        case 'c':
14f8ab
+            result = metadata.ctime.tv_sec;
14f8ab
+            break;
14f8ab
+        case 'm':
14f8ab
+            result = metadata.mtime.tv_sec;
14f8ab
+            break;
14f8ab
+        case 'a':
14f8ab
+            result = metadata.atime.tv_sec;
14f8ab
+            break;
14f8ab
+        default:
14f8ab
+            printf("-1");
14f8ab
+            goto err;
14f8ab
+    }
14f8ab
+    printf("%" PRIu64, result);
14f8ab
+    fflush(stdout);
14f8ab
+    return 0;
14f8ab
+err:
14f8ab
+    fflush(stdout);
14f8ab
+    return -1;
14f8ab
+}
14f8ab
diff --git a/tests/volume.rc b/tests/volume.rc
14f8ab
index bb400cc..6a78c37 100644
14f8ab
--- a/tests/volume.rc
14f8ab
+++ b/tests/volume.rc
14f8ab
@@ -927,3 +927,33 @@ function number_healer_threads_shd {
14f8ab
    local pid=$(get_shd_mux_pid $1)
14f8ab
    pstack $pid | grep $2 | wc -l
14f8ab
 }
14f8ab
+
14f8ab
+function get_mtime {
14f8ab
+    local time=$(get-mdata-xattr -m $1)
14f8ab
+    if [ $time == "-1" ];
14f8ab
+    then
14f8ab
+        echo $(stat -c %Y $1)
14f8ab
+    else
14f8ab
+        echo $time
14f8ab
+    fi
14f8ab
+}
14f8ab
+
14f8ab
+function get_ctime {
14f8ab
+    local time=$(get-mdata-xattr -c $1)
14f8ab
+    if [ $time == "-1" ];
14f8ab
+    then
14f8ab
+        echo $(stat -c %Z $2)
14f8ab
+    else
14f8ab
+        echo $time
14f8ab
+    fi
14f8ab
+}
14f8ab
+
14f8ab
+function get_atime {
14f8ab
+    local time=$(get-mdata-xattr -a $1)
14f8ab
+    if [ $time == "-1" ];
14f8ab
+    then
14f8ab
+        echo $(stat -c %X $1)
14f8ab
+    else
14f8ab
+        echo $time
14f8ab
+    fi
14f8ab
+}
14f8ab
diff --git a/xlators/storage/posix/src/posix-metadata.c b/xlators/storage/posix/src/posix-metadata.c
14f8ab
index e96f222..5a5e6cd 100644
14f8ab
--- a/xlators/storage/posix/src/posix-metadata.c
14f8ab
+++ b/xlators/storage/posix/src/posix-metadata.c
14f8ab
@@ -416,6 +416,22 @@ posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
14f8ab
                  * still fine as the times would get eventually
14f8ab
                  * accurate.
14f8ab
                  */
14f8ab
+
14f8ab
+                /* Don't create xattr with utimes/utimensat, only update if
14f8ab
+                 * present. This otherwise causes issues during inservice
14f8ab
+                 * upgrade. It causes inconsistent xattr values with in replica
14f8ab
+                 * set. The scenario happens during upgrade where clients are
14f8ab
+                 * older versions (without the ctime feature) and the server is
14f8ab
+                 * upgraded to the new version (with the ctime feature which
14f8ab
+                 * is enabled by default).
14f8ab
+                 */
14f8ab
+
14f8ab
+                if (update_utime) {
14f8ab
+                    UNLOCK(&inode->lock);
14f8ab
+                    GF_FREE(mdata);
14f8ab
+                    return 0;
14f8ab
+                }
14f8ab
+
14f8ab
                 mdata->version = 1;
14f8ab
                 mdata->flags = 0;
14f8ab
                 mdata->ctime.tv_sec = time->tv_sec;
14f8ab
@@ -527,6 +543,11 @@ posix_update_utime_in_mdata(xlator_t *this, const char *real_path, int fd,
14f8ab
 
14f8ab
     priv = this->private;
14f8ab
 
14f8ab
+    /* NOTE:
14f8ab
+     * This routine (utimes) is intentionally allowed for all internal and
14f8ab
+     * external clients even if ctime is not set. This is because AFR and
14f8ab
+     * WORM uses time attributes for it's internal operations
14f8ab
+     */
14f8ab
     if (inode && priv->ctime) {
14f8ab
         if ((valid & GF_SET_ATTR_ATIME) == GF_SET_ATTR_ATIME) {
14f8ab
             tv.tv_sec = stbuf->ia_atime;
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab