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