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