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