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