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