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