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