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