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