233933
From fc0903de1f7565e06db9d41e6dfd62221a745d24 Mon Sep 17 00:00:00 2001
233933
From: Kotresh HR <khiremat@redhat.com>
233933
Date: Mon, 24 Jun 2019 13:06:49 +0530
233933
Subject: [PATCH 260/261] ctime: Set mdata xattr on legacy files
233933
233933
Problem:
233933
The files which were created before ctime enabled would not
233933
have "trusted.glusterfs.mdata"(stores time attributes) xattr.
233933
Upon fops which modifies either ctime or mtime, the xattr
233933
gets created with latest ctime, mtime and atime, which is
233933
incorrect. It should update only the corresponding time
233933
attribute and rest from backend
233933
233933
Solution:
233933
Creating xattr with values from brick is not possible as
233933
each brick of replica set would have different times.
233933
So create the xattr upon successful lookup if the xattr
233933
is not created
233933
233933
Note To Reviewers:
233933
The time attributes used to set xattr is got from successful
233933
lookup. Instead of sending the whole iatt over the wire via
233933
setxattr, a structure called mdata_iatt is sent. The mdata_iatt
233933
contains only time attributes.
233933
233933
Backport of
233933
 > Patch: https://review.gluster.org/22936
233933
 > Change-Id: I5e535631ddef04195361ae0364336410a2895dd4
233933
 > fixes: bz#1593542
233933
233933
Change-Id: I5e535631ddef04195361ae0364336410a2895dd4
233933
BUG: 1715422
233933
Signed-off-by: Kotresh HR <khiremat@redhat.com>
233933
Reviewed-on: https://code.engineering.redhat.com/gerrit/176725
233933
Tested-by: RHGS Build Bot <nigelb@redhat.com>
233933
Reviewed-by: Amar Tumballi Suryanarayan <amarts@redhat.com>
233933
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
233933
---
233933
 libglusterfs/src/dict.c                        |  59 ++++++++++
233933
 libglusterfs/src/glusterfs/dict.h              |   5 +
233933
 libglusterfs/src/glusterfs/glusterfs.h         |   3 +
233933
 libglusterfs/src/glusterfs/iatt.h              |  20 ++++
233933
 libglusterfs/src/libglusterfs.sym              |   3 +
233933
 rpc/xdr/src/glusterfs-fops.x                   |   1 +
233933
 rpc/xdr/src/glusterfs3.h                       |  59 ++++++++++
233933
 rpc/xdr/src/glusterfs4-xdr.x                   |  12 ++
233933
 rpc/xdr/src/libgfxdr.sym                       |   3 +-
233933
 tests/basic/ctime/ctime-mdata-legacy-files.t   |  83 +++++++++++++
233933
 xlators/features/utime/src/utime-messages.h    |   3 +-
233933
 xlators/features/utime/src/utime.c             | 154 ++++++++++++++++++++++---
233933
 xlators/storage/posix/src/posix-inode-fd-ops.c |  17 +++
233933
 xlators/storage/posix/src/posix-messages.h     |   3 +-
233933
 xlators/storage/posix/src/posix-metadata.c     | 103 ++++++++++-------
233933
 xlators/storage/posix/src/posix-metadata.h     |   4 +
233933
 16 files changed, 475 insertions(+), 57 deletions(-)
233933
 create mode 100644 tests/basic/ctime/ctime-mdata-legacy-files.t
233933
233933
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
233933
index 6917df9..d8cdda4 100644
233933
--- a/libglusterfs/src/dict.c
233933
+++ b/libglusterfs/src/dict.c
233933
@@ -124,6 +124,7 @@ int32_t
233933
 is_data_equal(data_t *one, data_t *two)
233933
 {
233933
     struct iatt *iatt1, *iatt2;
233933
+    struct mdata_iatt *mdata_iatt1, *mdata_iatt2;
233933
 
233933
     if (!one || !two || !one->data || !two->data) {
233933
         gf_msg_callingfn("dict", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG,
233933
@@ -188,6 +189,24 @@ is_data_equal(data_t *one, data_t *two)
233933
         */
233933
         return 1;
233933
     }
233933
+    if (one->data_type == GF_DATA_TYPE_MDATA) {
233933
+        if ((one->len < sizeof(struct mdata_iatt)) ||
233933
+            (two->len < sizeof(struct mdata_iatt))) {
233933
+            return 0;
233933
+        }
233933
+        mdata_iatt1 = (struct mdata_iatt *)one->data;
233933
+        mdata_iatt2 = (struct mdata_iatt *)two->data;
233933
+
233933
+        if (mdata_iatt1->ia_atime != mdata_iatt2->ia_atime ||
233933
+            mdata_iatt1->ia_mtime != mdata_iatt2->ia_mtime ||
233933
+            mdata_iatt1->ia_ctime != mdata_iatt2->ia_ctime ||
233933
+            mdata_iatt1->ia_atime_nsec != mdata_iatt2->ia_atime_nsec ||
233933
+            mdata_iatt1->ia_mtime_nsec != mdata_iatt2->ia_mtime_nsec ||
233933
+            mdata_iatt1->ia_ctime_nsec != mdata_iatt2->ia_ctime_nsec) {
233933
+            return 0;
233933
+        }
233933
+        return 1;
233933
+    }
233933
 
233933
     if (one->len != two->len)
233933
         return 0;
233933
@@ -1078,6 +1097,7 @@ static char *data_type_name[GF_DATA_TYPE_MAX] = {
233933
     [GF_DATA_TYPE_PTR] = "pointer",
233933
     [GF_DATA_TYPE_GFUUID] = "gf-uuid",
233933
     [GF_DATA_TYPE_IATT] = "iatt",
233933
+    [GF_DATA_TYPE_MDATA] = "mdata",
233933
 };
233933
 
233933
 int64_t
233933
@@ -2666,6 +2686,45 @@ err:
233933
 }
233933
 
233933
 int
233933
+dict_set_mdata(dict_t *this, char *key, struct mdata_iatt *mdata,
233933
+               bool is_static)
233933
+{
233933
+    return dict_set_bin_common(this, key, mdata, sizeof(struct mdata_iatt),
233933
+                               is_static, GF_DATA_TYPE_MDATA);
233933
+}
233933
+
233933
+int
233933
+dict_get_mdata(dict_t *this, char *key, struct mdata_iatt *mdata)
233933
+{
233933
+    data_t *data = NULL;
233933
+    int ret = -EINVAL;
233933
+
233933
+    if (!this || !key || !mdata) {
233933
+        goto err;
233933
+    }
233933
+    ret = dict_get_with_ref(this, key, &data);
233933
+    if (ret < 0) {
233933
+        goto err;
233933
+    }
233933
+
233933
+    VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_MDATA, key, -EINVAL);
233933
+    if (data->len < sizeof(struct mdata_iatt)) {
233933
+        gf_msg("glusterfs", GF_LOG_ERROR, ENOBUFS, LG_MSG_UNDERSIZED_BUF,
233933
+               "data value for '%s' is smaller than expected", key);
233933
+        ret = -ENOBUFS;
233933
+        goto err;
233933
+    }
233933
+
233933
+    memcpy(mdata, data->data, min(data->len, sizeof(struct mdata_iatt)));
233933
+
233933
+err:
233933
+    if (data)
233933
+        data_unref(data);
233933
+
233933
+    return ret;
233933
+}
233933
+
233933
+int
233933
 dict_set_iatt(dict_t *this, char *key, struct iatt *iatt, bool is_static)
233933
 {
233933
     return dict_set_bin_common(this, key, iatt, sizeof(struct iatt), is_static,
233933
diff --git a/libglusterfs/src/glusterfs/dict.h b/libglusterfs/src/glusterfs/dict.h
233933
index 022f564..8239c7a 100644
233933
--- a/libglusterfs/src/glusterfs/dict.h
233933
+++ b/libglusterfs/src/glusterfs/dict.h
233933
@@ -392,6 +392,11 @@ GF_MUST_CHECK int
233933
 dict_set_iatt(dict_t *this, char *key, struct iatt *iatt, bool is_static);
233933
 GF_MUST_CHECK int
233933
 dict_get_iatt(dict_t *this, char *key, struct iatt *iatt);
233933
+GF_MUST_CHECK int
233933
+dict_set_mdata(dict_t *this, char *key, struct mdata_iatt *mdata,
233933
+               bool is_static);
233933
+GF_MUST_CHECK int
233933
+dict_get_mdata(dict_t *this, char *key, struct mdata_iatt *mdata);
233933
 
233933
 void
233933
 dict_dump_to_statedump(dict_t *dict, char *dict_name, char *domain);
233933
diff --git a/libglusterfs/src/glusterfs/glusterfs.h b/libglusterfs/src/glusterfs/glusterfs.h
233933
index 2cedf1a..79c93ae 100644
233933
--- a/libglusterfs/src/glusterfs/glusterfs.h
233933
+++ b/libglusterfs/src/glusterfs/glusterfs.h
233933
@@ -229,6 +229,9 @@ enum gf_internal_fop_indicator {
233933
 #define VIRTUAL_QUOTA_XATTR_CLEANUP_KEY "glusterfs.quota-xattr-cleanup"
233933
 #define QUOTA_READ_ONLY_KEY "trusted.glusterfs.quota.read-only"
233933
 
233933
+/* ctime related */
233933
+#define CTIME_MDATA_XDATA_KEY "set-ctime-mdata"
233933
+
233933
 /* afr related */
233933
 #define AFR_XATTR_PREFIX "trusted.afr"
233933
 
233933
diff --git a/libglusterfs/src/glusterfs/iatt.h b/libglusterfs/src/glusterfs/iatt.h
233933
index bee7a0a..f03d68b 100644
233933
--- a/libglusterfs/src/glusterfs/iatt.h
233933
+++ b/libglusterfs/src/glusterfs/iatt.h
233933
@@ -92,6 +92,15 @@ struct old_iatt {
233933
     uint32_t ia_ctime_nsec;
233933
 };
233933
 
233933
+struct mdata_iatt {
233933
+    int64_t ia_atime; /* last access time */
233933
+    int64_t ia_mtime; /* last modification time */
233933
+    int64_t ia_ctime; /* last status change time */
233933
+    uint32_t ia_atime_nsec;
233933
+    uint32_t ia_mtime_nsec;
233933
+    uint32_t ia_ctime_nsec;
233933
+};
233933
+
233933
 /* 64-bit mask for valid members in struct iatt. */
233933
 #define IATT_TYPE 0x0000000000000001U
233933
 #define IATT_MODE 0x0000000000000002U
233933
@@ -313,6 +322,17 @@ st_mode_from_ia(ia_prot_t prot, ia_type_t type)
233933
     return st_mode;
233933
 }
233933
 
233933
+static inline void
233933
+iatt_to_mdata(struct mdata_iatt *mdata, struct iatt *iatt)
233933
+{
233933
+    mdata->ia_atime = iatt->ia_atime;
233933
+    mdata->ia_atime_nsec = iatt->ia_atime_nsec;
233933
+    mdata->ia_mtime = iatt->ia_mtime;
233933
+    mdata->ia_mtime_nsec = iatt->ia_mtime_nsec;
233933
+    mdata->ia_ctime = iatt->ia_ctime;
233933
+    mdata->ia_ctime_nsec = iatt->ia_ctime_nsec;
233933
+}
233933
+
233933
 static inline int
233933
 iatt_from_stat(struct iatt *iatt, struct stat *stat)
233933
 {
233933
diff --git a/libglusterfs/src/libglusterfs.sym b/libglusterfs/src/libglusterfs.sym
233933
index 4dca7de..b161380 100644
233933
--- a/libglusterfs/src/libglusterfs.sym
233933
+++ b/libglusterfs/src/libglusterfs.sym
233933
@@ -380,6 +380,7 @@ dict_get_bin
233933
 dict_get_double
233933
 dict_get_gfuuid
233933
 dict_get_iatt
233933
+dict_get_mdata
233933
 dict_get_int16
233933
 dict_get_int32
233933
 dict_get_int32n
233933
@@ -417,6 +418,7 @@ dict_set_dynstrn
233933
 dict_set_dynstr_with_alloc
233933
 dict_set_gfuuid
233933
 dict_set_iatt
233933
+dict_set_mdata
233933
 dict_set_int16
233933
 dict_set_int32
233933
 dict_set_int32n
233933
@@ -509,6 +511,7 @@ fop_lease_stub
233933
 fop_link_stub
233933
 fop_lk_stub
233933
 fop_log_level
233933
+fop_lookup_cbk_stub
233933
 fop_lookup_stub
233933
 fop_mkdir_stub
233933
 fop_mknod_stub
233933
diff --git a/rpc/xdr/src/glusterfs-fops.x b/rpc/xdr/src/glusterfs-fops.x
233933
index bacf0773..651f8de 100644
233933
--- a/rpc/xdr/src/glusterfs-fops.x
233933
+++ b/rpc/xdr/src/glusterfs-fops.x
233933
@@ -245,5 +245,6 @@ enum gf_dict_data_type_t {
233933
         GF_DATA_TYPE_PTR,
233933
         GF_DATA_TYPE_GFUUID,
233933
         GF_DATA_TYPE_IATT,
233933
+        GF_DATA_TYPE_MDATA,
233933
         GF_DATA_TYPE_MAX
233933
 };
233933
diff --git a/rpc/xdr/src/glusterfs3.h b/rpc/xdr/src/glusterfs3.h
233933
index 5521f4d..86b3a4c 100644
233933
--- a/rpc/xdr/src/glusterfs3.h
233933
+++ b/rpc/xdr/src/glusterfs3.h
233933
@@ -585,6 +585,34 @@ out:
233933
 }
233933
 
233933
 static inline void
233933
+gfx_mdata_iatt_to_mdata_iatt(struct gfx_mdata_iatt *gf_mdata_iatt,
233933
+                             struct mdata_iatt *mdata_iatt)
233933
+{
233933
+    if (!mdata_iatt || !gf_mdata_iatt)
233933
+        return;
233933
+    mdata_iatt->ia_atime = gf_mdata_iatt->ia_atime;
233933
+    mdata_iatt->ia_atime_nsec = gf_mdata_iatt->ia_atime_nsec;
233933
+    mdata_iatt->ia_mtime = gf_mdata_iatt->ia_mtime;
233933
+    mdata_iatt->ia_mtime_nsec = gf_mdata_iatt->ia_mtime_nsec;
233933
+    mdata_iatt->ia_ctime = gf_mdata_iatt->ia_ctime;
233933
+    mdata_iatt->ia_ctime_nsec = gf_mdata_iatt->ia_ctime_nsec;
233933
+}
233933
+
233933
+static inline void
233933
+gfx_mdata_iatt_from_mdata_iatt(struct gfx_mdata_iatt *gf_mdata_iatt,
233933
+                               struct mdata_iatt *mdata_iatt)
233933
+{
233933
+    if (!mdata_iatt || !gf_mdata_iatt)
233933
+        return;
233933
+    gf_mdata_iatt->ia_atime = mdata_iatt->ia_atime;
233933
+    gf_mdata_iatt->ia_atime_nsec = mdata_iatt->ia_atime_nsec;
233933
+    gf_mdata_iatt->ia_mtime = mdata_iatt->ia_mtime;
233933
+    gf_mdata_iatt->ia_mtime_nsec = mdata_iatt->ia_mtime_nsec;
233933
+    gf_mdata_iatt->ia_ctime = mdata_iatt->ia_ctime;
233933
+    gf_mdata_iatt->ia_ctime_nsec = mdata_iatt->ia_ctime_nsec;
233933
+}
233933
+
233933
+static inline void
233933
 gfx_stat_to_iattx(struct gfx_iattx *gf_stat, struct iatt *iatt)
233933
 {
233933
     if (!iatt || !gf_stat)
233933
@@ -721,6 +749,12 @@ dict_to_xdr(dict_t *this, gfx_dict *dict)
233933
                 gfx_stat_from_iattx(&xpair->value.gfx_value_u.iatt,
233933
                                     (struct iatt *)dpair->value->data);
233933
                 break;
233933
+            case GF_DATA_TYPE_MDATA:
233933
+                index++;
233933
+                gfx_mdata_iatt_from_mdata_iatt(
233933
+                    &xpair->value.gfx_value_u.mdata_iatt,
233933
+                    (struct mdata_iatt *)dpair->value->data);
233933
+                break;
233933
             case GF_DATA_TYPE_GFUUID:
233933
                 index++;
233933
                 memcpy(&xpair->value.gfx_value_u.uuid, dpair->value->data,
233933
@@ -787,6 +821,7 @@ xdr_to_dict(gfx_dict *dict, dict_t **to)
233933
     dict_t *this = NULL;
233933
     unsigned char *uuid = NULL;
233933
     struct iatt *iatt = NULL;
233933
+    struct mdata_iatt *mdata_iatt = NULL;
233933
 
233933
     if (!to || !dict)
233933
         goto out;
233933
@@ -854,6 +889,30 @@ xdr_to_dict(gfx_dict *dict, dict_t **to)
233933
                 gfx_stat_to_iattx(&xpair->value.gfx_value_u.iatt, iatt);
233933
                 ret = dict_set_iatt(this, key, iatt, false);
233933
                 break;
233933
+            case GF_DATA_TYPE_MDATA:
233933
+                mdata_iatt = GF_CALLOC(1, sizeof(struct mdata_iatt),
233933
+                                       gf_common_mt_char);
233933
+                if (!mdata_iatt) {
233933
+                    errno = ENOMEM;
233933
+                    gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM, LG_MSG_NO_MEMORY,
233933
+                           "failed to allocate memory. key: %s", key);
233933
+                    ret = -1;
233933
+                    goto out;
233933
+                }
233933
+                gfx_mdata_iatt_to_mdata_iatt(
233933
+                    &xpair->value.gfx_value_u.mdata_iatt, mdata_iatt);
233933
+                ret = dict_set_mdata(this, key, mdata_iatt, false);
233933
+                if (ret != 0) {
233933
+                    GF_FREE(mdata_iatt);
233933
+                    gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM,
233933
+                           LG_MSG_DICT_SET_FAILED,
233933
+                           "failed to set the key (%s)"
233933
+                           " into dict",
233933
+                           key);
233933
+                    ret = -1;
233933
+                    goto out;
233933
+                }
233933
+                break;
233933
             case GF_DATA_TYPE_PTR:
233933
             case GF_DATA_TYPE_STR_OLD:
233933
                 value = GF_MALLOC(xpair->value.gfx_value_u.other.other_len + 1,
233933
diff --git a/rpc/xdr/src/glusterfs4-xdr.x b/rpc/xdr/src/glusterfs4-xdr.x
233933
index bec0872..6f92b70 100644
233933
--- a/rpc/xdr/src/glusterfs4-xdr.x
233933
+++ b/rpc/xdr/src/glusterfs4-xdr.x
233933
@@ -46,6 +46,16 @@ struct gfx_iattx {
233933
         unsigned int     mode;          /* type of file and rwx mode */
233933
 };
233933
 
233933
+struct gfx_mdata_iatt {
233933
+        hyper      ia_atime;      /* last access time */
233933
+        hyper      ia_mtime;      /* last modification time */
233933
+        hyper      ia_ctime;      /* last status change time */
233933
+
233933
+        unsigned int     ia_atime_nsec;
233933
+        unsigned int     ia_mtime_nsec;
233933
+        unsigned int     ia_ctime_nsec;
233933
+};
233933
+
233933
 union gfx_value switch (gf_dict_data_type_t type) {
233933
         case GF_DATA_TYPE_INT:
233933
                 hyper value_int;
233933
@@ -62,6 +72,8 @@ union gfx_value switch (gf_dict_data_type_t type) {
233933
         case GF_DATA_TYPE_PTR:
233933
         case GF_DATA_TYPE_STR_OLD:
233933
                 opaque other<>;
233933
+        case GF_DATA_TYPE_MDATA:
233933
+                gfx_mdata_iatt mdata_iatt;
233933
 };
233933
 
233933
 /* AUTH */
233933
diff --git a/rpc/xdr/src/libgfxdr.sym b/rpc/xdr/src/libgfxdr.sym
233933
index 22cdf30..dd4ac85 100644
233933
--- a/rpc/xdr/src/libgfxdr.sym
233933
+++ b/rpc/xdr/src/libgfxdr.sym
233933
@@ -251,6 +251,7 @@ xdr_to_write3args
233933
 xdr_vector_round_up
233933
 xdr_gfx_read_rsp
233933
 xdr_gfx_iattx
233933
+xdr_gfx_mdata_iatt
233933
 xdr_gfx_value
233933
 xdr_gfx_dict_pair
233933
 xdr_gfx_dict
233933
@@ -344,4 +345,4 @@ xdr_compound_req_v2
233933
 xdr_gfx_compound_req
233933
 xdr_compound_rsp_v2
233933
 xdr_gfx_compound_rsp
233933
-xdr_gfx_copy_file_range_req
233933
\ No newline at end of file
233933
+xdr_gfx_copy_file_range_req
233933
diff --git a/tests/basic/ctime/ctime-mdata-legacy-files.t b/tests/basic/ctime/ctime-mdata-legacy-files.t
233933
new file mode 100644
233933
index 0000000..2e782d5
233933
--- /dev/null
233933
+++ b/tests/basic/ctime/ctime-mdata-legacy-files.t
233933
@@ -0,0 +1,83 @@
233933
+#!/bin/bash
233933
+. $(dirname $0)/../../include.rc
233933
+. $(dirname $0)/../../volume.rc
233933
+. $(dirname $0)/../../afr.rc
233933
+cleanup;
233933
+
233933
+###############################################################################
233933
+#Replica volume
233933
+
233933
+TEST glusterd
233933
+TEST pidof glusterd
233933
+TEST $CLI volume create $V0 replica 3 $H0:$B0/${V0}{0,1,2}
233933
+TEST $CLI volume set $V0 performance.stat-prefetch off
233933
+TEST $CLI volume start $V0
233933
+
233933
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 $M0;
233933
+
233933
+#Disable ctime and create file, file doesn't contain "trusted.glusterfs.mdata" xattr
233933
+TEST $CLI volume set $V0 ctime off
233933
+
233933
+TEST "mkdir $M0/DIR"
233933
+TEST "echo hello_world > $M0/DIR/FILE"
233933
+
233933
+#Verify absence of xattr
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}0/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}0/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}1/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}1/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}2/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}2/DIR/FILE"
233933
+
233933
+#Enable ctime
233933
+TEST $CLI volume set $V0 ctime on
233933
+sleep 3
233933
+TEST stat $M0/DIR/FILE
233933
+
233933
+#Verify presence "trusted.glusterfs.mdata" xattr on backend
233933
+#The lookup above should have created xattr
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}0/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}0/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}1/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}1/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}2/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V0}2/DIR/FILE"
233933
+
233933
+###############################################################################
233933
+#Disperse Volume
233933
+
233933
+TEST $CLI volume create $V1 disperse 3 redundancy 1  $H0:$B0/${V1}{0,1,2}
233933
+TEST $CLI volume set $V1 performance.stat-prefetch off
233933
+TEST $CLI volume start $V1
233933
+
233933
+TEST glusterfs --volfile-id=$V1 --volfile-server=$H0 --entry-timeout=0 $M1;
233933
+
233933
+#Disable ctime and create file, file doesn't contain "trusted.glusterfs.mdata" xattr
233933
+TEST $CLI volume set $V1 ctime off
233933
+TEST "mkdir $M1/DIR"
233933
+TEST "echo hello_world > $M1/DIR/FILE"
233933
+
233933
+#Verify absence of xattr
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}0/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}0/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}1/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}1/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}2/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT "" check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}2/DIR/FILE"
233933
+
233933
+#Enable ctime
233933
+TEST $CLI volume set $V1 ctime on
233933
+sleep 3
233933
+TEST stat $M1/DIR/FILE
233933
+
233933
+#Verify presence "trusted.glusterfs.mdata" xattr on backend
233933
+#The lookup above should have created xattr
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}0/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}0/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}1/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}1/DIR/FILE"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}2/DIR"
233933
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 'trusted.glusterfs.mdata' check_for_xattr 'trusted.glusterfs.mdata' "$B0/${V1}2/DIR/FILE"
233933
+
233933
+cleanup;
233933
+###############################################################################
233933
diff --git a/xlators/features/utime/src/utime-messages.h b/xlators/features/utime/src/utime-messages.h
233933
index bac18ab..bd40265 100644
233933
--- a/xlators/features/utime/src/utime-messages.h
233933
+++ b/xlators/features/utime/src/utime-messages.h
233933
@@ -23,6 +23,7 @@
233933
  * glfs-message-id.h.
233933
  */
233933
 
233933
-GLFS_MSGID(UTIME, UTIME_MSG_NO_MEMORY);
233933
+GLFS_MSGID(UTIME, UTIME_MSG_NO_MEMORY, UTIME_MSG_SET_MDATA_FAILED,
233933
+           UTIME_MSG_DICT_SET_FAILED);
233933
 
233933
 #endif /* __UTIME_MESSAGES_H__ */
233933
diff --git a/xlators/features/utime/src/utime.c b/xlators/features/utime/src/utime.c
233933
index 877c751..2a986e7 100644
233933
--- a/xlators/features/utime/src/utime.c
233933
+++ b/xlators/features/utime/src/utime.c
233933
@@ -9,8 +9,10 @@
233933
 */
233933
 
233933
 #include "utime.h"
233933
+#include "utime-helpers.h"
233933
 #include "utime-messages.h"
233933
 #include "utime-mem-types.h"
233933
+#include <glusterfs/call-stub.h>
233933
 
233933
 int32_t
233933
 gf_utime_invalidate(xlator_t *this, inode_t *inode)
233933
@@ -133,6 +135,124 @@ mem_acct_init(xlator_t *this)
233933
 }
233933
 
233933
 int32_t
233933
+gf_utime_set_mdata_setxattr_cbk(call_frame_t *frame, void *cookie,
233933
+                                xlator_t *this, int op_ret, int op_errno,
233933
+                                dict_t *xdata)
233933
+{
233933
+    /* Don't fail lookup if mdata setxattr fails */
233933
+    if (op_ret) {
233933
+        gf_msg(this->name, GF_LOG_ERROR, op_errno, UTIME_MSG_SET_MDATA_FAILED,
233933
+               "dict set of key for set-ctime-mdata failed");
233933
+    }
233933
+    call_resume(frame->local);
233933
+    return 0;
233933
+}
233933
+
233933
+int32_t
233933
+gf_utime_set_mdata_lookup_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
233933
+                              int32_t op_ret, int32_t op_errno, inode_t *inode,
233933
+                              struct iatt *stbuf, dict_t *xdata,
233933
+                              struct iatt *postparent)
233933
+{
233933
+    dict_t *dict = NULL;
233933
+    struct mdata_iatt *mdata = NULL;
233933
+    int ret = 0;
233933
+    loc_t loc = {
233933
+        0,
233933
+    };
233933
+
233933
+    if (!op_ret && dict_get(xdata, GF_XATTR_MDATA_KEY) == NULL) {
233933
+        dict = dict_new();
233933
+        if (!dict) {
233933
+            op_errno = ENOMEM;
233933
+            goto err;
233933
+        }
233933
+        mdata = GF_MALLOC(sizeof(struct mdata_iatt), gf_common_mt_char);
233933
+        if (mdata == NULL) {
233933
+            op_errno = ENOMEM;
233933
+            goto err;
233933
+        }
233933
+        iatt_to_mdata(mdata, stbuf);
233933
+        ret = dict_set_mdata(dict, CTIME_MDATA_XDATA_KEY, mdata, _gf_false);
233933
+        if (ret < 0) {
233933
+            gf_msg(this->name, GF_LOG_WARNING, ENOMEM, UTIME_MSG_NO_MEMORY,
233933
+                   "dict set of key for set-ctime-mdata failed");
233933
+            goto err;
233933
+        }
233933
+        frame->local = fop_lookup_cbk_stub(frame, default_lookup_cbk, op_ret,
233933
+                                           op_errno, inode, stbuf, xdata,
233933
+                                           postparent);
233933
+        if (!frame->local) {
233933
+            gf_msg(this->name, GF_LOG_WARNING, ENOMEM, UTIME_MSG_NO_MEMORY,
233933
+                   "lookup_cbk stub allocation failed");
233933
+            goto stub_err;
233933
+        }
233933
+
233933
+        loc.inode = inode_ref(inode);
233933
+        gf_uuid_copy(loc.gfid, stbuf->ia_gfid);
233933
+        STACK_WIND(frame, gf_utime_set_mdata_setxattr_cbk, FIRST_CHILD(this),
233933
+                   FIRST_CHILD(this)->fops->setxattr, &loc, dict, 0, NULL);
233933
+
233933
+        dict_unref(dict);
233933
+        inode_unref(loc.inode);
233933
+        return 0;
233933
+    }
233933
+
233933
+    STACK_UNWIND_STRICT(lookup, frame, op_ret, op_errno, inode, stbuf, xdata,
233933
+                        postparent);
233933
+    return 0;
233933
+
233933
+err:
233933
+    if (mdata) {
233933
+        GF_FREE(mdata);
233933
+    }
233933
+stub_err:
233933
+    if (dict) {
233933
+        dict_unref(dict);
233933
+    }
233933
+    STACK_UNWIND_STRICT(lookup, frame, -1, op_errno, NULL, NULL, NULL, NULL);
233933
+    return 0;
233933
+}
233933
+
233933
+int
233933
+gf_utime_lookup(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
233933
+{
233933
+    int op_errno = -1;
233933
+    int ret = -1;
233933
+
233933
+    VALIDATE_OR_GOTO(frame, err);
233933
+    VALIDATE_OR_GOTO(this, err);
233933
+    VALIDATE_OR_GOTO(loc, err);
233933
+    VALIDATE_OR_GOTO(loc->inode, err);
233933
+
233933
+    xdata = xdata ? dict_ref(xdata) : dict_new();
233933
+    if (!xdata) {
233933
+        op_errno = ENOMEM;
233933
+        goto err;
233933
+    }
233933
+
233933
+    ret = dict_set_int8(xdata, GF_XATTR_MDATA_KEY, 1);
233933
+    if (ret < 0) {
233933
+        gf_msg(this->name, GF_LOG_WARNING, -ret, UTIME_MSG_DICT_SET_FAILED,
233933
+               "%s: Unable to set dict value for %s", loc->path,
233933
+               GF_XATTR_MDATA_KEY);
233933
+        op_errno = -ret;
233933
+        goto free_dict;
233933
+    }
233933
+
233933
+    STACK_WIND(frame, gf_utime_set_mdata_lookup_cbk, FIRST_CHILD(this),
233933
+               FIRST_CHILD(this)->fops->lookup, loc, xdata);
233933
+    dict_unref(xdata);
233933
+    return 0;
233933
+
233933
+free_dict:
233933
+    dict_unref(xdata);
233933
+err:
233933
+    STACK_UNWIND_STRICT(lookup, frame, -1, op_errno, NULL, NULL, NULL, NULL);
233933
+    return 0;
233933
+}
233933
+
233933
+int32_t
233933
 init(xlator_t *this)
233933
 {
233933
     utime_priv_t *utime = NULL;
233933
@@ -182,19 +302,27 @@ notify(xlator_t *this, int event, void *data, ...)
233933
 }
233933
 
233933
 struct xlator_fops fops = {
233933
-    /* TODO: Need to go through other fops and
233933
-     *       check if they modify time attributes
233933
-     */
233933
-    .rename = gf_utime_rename,       .mknod = gf_utime_mknod,
233933
-    .readv = gf_utime_readv,         .fremovexattr = gf_utime_fremovexattr,
233933
-    .open = gf_utime_open,           .create = gf_utime_create,
233933
-    .mkdir = gf_utime_mkdir,         .writev = gf_utime_writev,
233933
-    .rmdir = gf_utime_rmdir,         .fallocate = gf_utime_fallocate,
233933
-    .truncate = gf_utime_truncate,   .symlink = gf_utime_symlink,
233933
-    .zerofill = gf_utime_zerofill,   .link = gf_utime_link,
233933
-    .ftruncate = gf_utime_ftruncate, .unlink = gf_utime_unlink,
233933
-    .setattr = gf_utime_setattr,     .fsetattr = gf_utime_fsetattr,
233933
-    .opendir = gf_utime_opendir,     .removexattr = gf_utime_removexattr,
233933
+    .rename = gf_utime_rename,
233933
+    .mknod = gf_utime_mknod,
233933
+    .readv = gf_utime_readv,
233933
+    .fremovexattr = gf_utime_fremovexattr,
233933
+    .open = gf_utime_open,
233933
+    .create = gf_utime_create,
233933
+    .mkdir = gf_utime_mkdir,
233933
+    .writev = gf_utime_writev,
233933
+    .rmdir = gf_utime_rmdir,
233933
+    .fallocate = gf_utime_fallocate,
233933
+    .truncate = gf_utime_truncate,
233933
+    .symlink = gf_utime_symlink,
233933
+    .zerofill = gf_utime_zerofill,
233933
+    .link = gf_utime_link,
233933
+    .ftruncate = gf_utime_ftruncate,
233933
+    .unlink = gf_utime_unlink,
233933
+    .setattr = gf_utime_setattr,
233933
+    .fsetattr = gf_utime_fsetattr,
233933
+    .opendir = gf_utime_opendir,
233933
+    .removexattr = gf_utime_removexattr,
233933
+    .lookup = gf_utime_lookup,
233933
 };
233933
 struct xlator_cbks cbks = {
233933
     .invalidate = gf_utime_invalidate,
233933
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
233933
index ea3b69c..d22bbc2 100644
233933
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
233933
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
233933
@@ -2625,6 +2625,9 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
233933
     gf_cs_obj_state state = -1;
233933
     int i = 0;
233933
     int len;
233933
+    struct mdata_iatt mdata_iatt = {
233933
+        0,
233933
+    };
233933
 
233933
     DECLARE_OLD_FS_ID_VAR;
233933
     SET_FS_ID(frame->root->uid, frame->root->gid);
233933
@@ -2638,6 +2641,20 @@ posix_setxattr(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict,
233933
     priv = this->private;
233933
     DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_ret, op_errno, out);
233933
 
233933
+    ret = dict_get_mdata(dict, CTIME_MDATA_XDATA_KEY, &mdata_iatt);
233933
+    if (ret == 0) {
233933
+        /* This is initiated by lookup when ctime feature is enabled to create
233933
+         * "trusted.glusterfs.mdata" xattr if not present. These are the files
233933
+         * which were created when ctime feature is disabled.
233933
+         */
233933
+        ret = posix_set_mdata_xattr_legacy_files(this, loc->inode, &mdata_iatt,
233933
+                                                 &op_errno);
233933
+        if (ret != 0) {
233933
+            op_ret = -1;
233933
+        }
233933
+        goto out;
233933
+    }
233933
+
233933
     MAKE_INODE_HANDLE(real_path, this, loc, NULL);
233933
     if (!real_path) {
233933
         op_ret = -1;
233933
diff --git a/xlators/storage/posix/src/posix-messages.h b/xlators/storage/posix/src/posix-messages.h
233933
index 3229275..15e23ff 100644
233933
--- a/xlators/storage/posix/src/posix-messages.h
233933
+++ b/xlators/storage/posix/src/posix-messages.h
233933
@@ -68,6 +68,7 @@ GLFS_MSGID(POSIX, P_MSG_XATTR_FAILED, P_MSG_NULL_GFID, P_MSG_FCNTL_FAILED,
233933
            P_MSG_FALLOCATE_FAILED, P_MSG_STOREMDATA_FAILED,
233933
            P_MSG_FETCHMDATA_FAILED, P_MSG_GETMDATA_FAILED,
233933
            P_MSG_SETMDATA_FAILED, P_MSG_FRESHFILE, P_MSG_MUTEX_FAILED,
233933
-           P_MSG_COPY_FILE_RANGE_FAILED, P_MSG_TIMER_DELETE_FAILED);
233933
+           P_MSG_COPY_FILE_RANGE_FAILED, P_MSG_TIMER_DELETE_FAILED,
233933
+           P_MSG_NOMEM);
233933
 
233933
 #endif /* !_GLUSTERD_MESSAGES_H_ */
233933
diff --git a/xlators/storage/posix/src/posix-metadata.c b/xlators/storage/posix/src/posix-metadata.c
233933
index 5a5e6cd..647c0bb 100644
233933
--- a/xlators/storage/posix/src/posix-metadata.c
233933
+++ b/xlators/storage/posix/src/posix-metadata.c
233933
@@ -245,6 +245,10 @@ __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
233933
     if (ret == -1 || !mdata) {
233933
         mdata = GF_CALLOC(1, sizeof(posix_mdata_t), gf_posix_mt_mdata_attr);
233933
         if (!mdata) {
233933
+            gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_NOMEM,
233933
+                   "Could not allocate mdata. file: %s: gfid: %s",
233933
+                   real_path ? real_path : "null",
233933
+                   inode ? uuid_utoa(inode->gfid) : "null");
233933
             ret = -1;
233933
             goto out;
233933
         }
233933
@@ -262,18 +266,8 @@ __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
233933
             }
233933
         } else {
233933
             /* Failed to get mdata from disk, xattr missing.
233933
-             * This happens on two cases.
233933
-             * 1. File is created before ctime is enabled.
233933
-             * 2. On new file creation.
233933
-             *
233933
-             * Do nothing, just return success. It is as
233933
-             * good as ctime feature is not enabled for this
233933
-             * file. For files created before ctime is enabled,
233933
-             * time attributes gets updated into ctime structure
233933
-             * once the metadata modification fop happens and
233933
-             * time attributes become consistent eventually.
233933
-             * For new files, it would obviously get updated
233933
-             * before the fop completion.
233933
+             * This happens when the file is created before
233933
+             * ctime is enabled.
233933
              */
233933
             if (stbuf && op_errno != ENOENT) {
233933
                 ret = 0;
233933
@@ -345,6 +339,54 @@ posix_compare_timespec(struct timespec *first, struct timespec *second)
233933
         return first->tv_sec - second->tv_sec;
233933
 }
233933
 
233933
+int
233933
+posix_set_mdata_xattr_legacy_files(xlator_t *this, inode_t *inode,
233933
+                                   struct mdata_iatt *mdata_iatt, int *op_errno)
233933
+{
233933
+    posix_mdata_t *mdata = NULL;
233933
+    int ret = 0;
233933
+
233933
+    GF_VALIDATE_OR_GOTO("posix", this, out);
233933
+    GF_VALIDATE_OR_GOTO(this->name, inode, out);
233933
+
233933
+    LOCK(&inode->lock);
233933
+    {
233933
+        mdata = GF_CALLOC(1, sizeof(posix_mdata_t), gf_posix_mt_mdata_attr);
233933
+        if (!mdata) {
233933
+            gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_NOMEM,
233933
+                   "Could not allocate mdata. gfid: %s",
233933
+                   uuid_utoa(inode->gfid));
233933
+            ret = -1;
233933
+            *op_errno = ENOMEM;
233933
+            goto unlock;
233933
+        }
233933
+
233933
+        mdata->version = 1;
233933
+        mdata->flags = 0;
233933
+        mdata->ctime.tv_sec = mdata_iatt->ia_ctime;
233933
+        mdata->ctime.tv_nsec = mdata_iatt->ia_ctime_nsec;
233933
+        mdata->atime.tv_sec = mdata_iatt->ia_atime;
233933
+        mdata->atime.tv_nsec = mdata_iatt->ia_atime_nsec;
233933
+        mdata->mtime.tv_sec = mdata_iatt->ia_mtime;
233933
+        mdata->mtime.tv_nsec = mdata_iatt->ia_mtime_nsec;
233933
+
233933
+        __inode_ctx_set1(inode, this, (uint64_t *)&mdata);
233933
+
233933
+        ret = posix_store_mdata_xattr(this, NULL, -1, inode, mdata);
233933
+        if (ret) {
233933
+            gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_STOREMDATA_FAILED,
233933
+                   "gfid: %s key:%s ", uuid_utoa(inode->gfid),
233933
+                   GF_XATTR_MDATA_KEY);
233933
+            *op_errno = errno;
233933
+            goto unlock;
233933
+        }
233933
+    }
233933
+unlock:
233933
+    UNLOCK(&inode->lock);
233933
+out:
233933
+    return ret;
233933
+}
233933
+
233933
 /* posix_set_mdata_xattr updates the posix_mdata_t based on the flag
233933
  * in inode context and stores it on disk
233933
  */
233933
@@ -372,6 +414,9 @@ posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
233933
              */
233933
             mdata = GF_CALLOC(1, sizeof(posix_mdata_t), gf_posix_mt_mdata_attr);
233933
             if (!mdata) {
233933
+                gf_msg(this->name, GF_LOG_ERROR, ENOMEM, P_MSG_NOMEM,
233933
+                       "Could not allocate mdata. file: %s: gfid: %s",
233933
+                       real_path ? real_path : "null", uuid_utoa(inode->gfid));
233933
                 ret = -1;
233933
                 goto unlock;
233933
             }
233933
@@ -386,35 +431,11 @@ posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
233933
                 __inode_ctx_set1(inode, this, (uint64_t *)&mdata);
233933
             } else {
233933
                 /*
233933
-                 * This is the first time creating the time
233933
-                 * attr. This happens when you activate this
233933
-                 * feature, and the legacy file will not have
233933
-                 * any xattr set.
233933
-                 *
233933
-                 * New files will create extended attributes.
233933
-                 */
233933
-
233933
-                /*
233933
-                 * TODO: This is wrong approach, because before
233933
-                 * creating fresh xattr, we should consult
233933
-                 * to all replica and/or distribution set.
233933
-                 *
233933
-                 * We should contact the time management
233933
-                 * xlators, and ask them to create an xattr.
233933
-                 */
233933
-                /* We should not be relying on backend file's
233933
-                 * time attributes to load the initial ctime
233933
-                 * time attribute structure. This is incorrect
233933
-                 * as each replica set would have witnessed the
233933
-                 * file creation at different times.
233933
-                 *
233933
-                 * For new file creation, ctime, atime and mtime
233933
-                 * should be same, hence initiate the ctime
233933
-                 * structure with the time from the frame. But
233933
-                 * for the files which were created before ctime
233933
-                 * feature is enabled, this is not accurate but
233933
-                 * still fine as the times would get eventually
233933
-                 * accurate.
233933
+                 * This is the first time creating the time attr. This happens
233933
+                 * when you activate this feature. On this code path, only new
233933
+                 * files will create mdata xattr. The legacy files (files
233933
+                 * created before ctime enabled) will not have any xattr set.
233933
+                 * The xattr on legacy file will be set via lookup.
233933
                  */
233933
 
233933
                 /* Don't create xattr with utimes/utimensat, only update if
233933
diff --git a/xlators/storage/posix/src/posix-metadata.h b/xlators/storage/posix/src/posix-metadata.h
233933
index 3416148..dc25e59 100644
233933
--- a/xlators/storage/posix/src/posix-metadata.h
233933
+++ b/xlators/storage/posix/src/posix-metadata.h
233933
@@ -53,5 +53,9 @@ posix_set_ctime_cfr(call_frame_t *frame, xlator_t *this,
233933
                     const char *real_path_in, int fd_in, inode_t *inode_in,
233933
                     struct iatt *stbuf_in, const char *read_path_put,
233933
                     int fd_out, inode_t *inode_out, struct iatt *stbuf_out);
233933
+int
233933
+posix_set_mdata_xattr_legacy_files(xlator_t *this, inode_t *inode,
233933
+                                   struct mdata_iatt *mdata_iatt,
233933
+                                   int *op_errno);
233933
 
233933
 #endif /* _POSIX_METADATA_H */
233933
-- 
233933
1.8.3.1
233933