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