f338ef
From 55eb2e7642e3428eaa1b2d833c0daa1d34b98324 Mon Sep 17 00:00:00 2001
f338ef
From: Kotresh HR <khiremat@redhat.com>
f338ef
Date: Thu, 8 Aug 2019 10:05:12 +0530
f338ef
Subject: [PATCH 283/284] ctime: Fix ctime issue with utime family of syscalls
f338ef
f338ef
When atime|mtime is updated via utime family of syscalls,
f338ef
ctime is not updated. This patch fixes the same.
f338ef
f338ef
Backport of:
f338ef
 > Patch: https://review.gluster.org/23177
f338ef
 > Change-Id: I7f86d8f8a1e06a332c3449b5bbdbf128c9690f25
f338ef
 > fixes: bz#1738786
f338ef
 > Signed-off-by: Kotresh HR <khiremat@redhat.com>
f338ef
f338ef
Change-Id: I7f86d8f8a1e06a332c3449b5bbdbf128c9690f25
f338ef
BUG: 1743627
f338ef
Signed-off-by: Kotresh HR <khiremat@redhat.com>
f338ef
Reviewed-on: https://code.engineering.redhat.com/gerrit/179184
f338ef
Tested-by: RHGS Build Bot <nigelb@redhat.com>
f338ef
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
f338ef
---
f338ef
 xlators/features/utime/src/utime-gen-fops-c.py | 13 +++-
f338ef
 xlators/storage/posix/src/posix-inode-fd-ops.c |  8 +--
f338ef
 xlators/storage/posix/src/posix-metadata.c     | 96 ++++++++++++++------------
f338ef
 xlators/storage/posix/src/posix-metadata.h     |  3 +-
f338ef
 4 files changed, 68 insertions(+), 52 deletions(-)
f338ef
f338ef
diff --git a/xlators/features/utime/src/utime-gen-fops-c.py b/xlators/features/utime/src/utime-gen-fops-c.py
f338ef
index a8637ff..8730a51 100755
f338ef
--- a/xlators/features/utime/src/utime-gen-fops-c.py
f338ef
+++ b/xlators/features/utime/src/utime-gen-fops-c.py
f338ef
@@ -82,7 +82,18 @@ gf_utime_@NAME@ (call_frame_t *frame, xlator_t *this,
f338ef
              @LONG_ARGS@)
f338ef
 {
f338ef
         gl_timespec_get(&frame->root->ctime);
f338ef
-        frame->root->flags |= MDATA_CTIME;
f338ef
+
f338ef
+        if (!valid) {
f338ef
+                frame->root->flags |= MDATA_CTIME;
f338ef
+        }
f338ef
+
f338ef
+        if (valid & (GF_SET_ATTR_UID | GF_SET_ATTR_GID)) {
f338ef
+                frame->root->flags |= MDATA_CTIME;
f338ef
+        }
f338ef
+
f338ef
+        if (valid & GF_SET_ATTR_MODE) {
f338ef
+                frame->root->flags |= MDATA_CTIME;
f338ef
+        }
f338ef
 
f338ef
         STACK_WIND (frame, gf_utime_@NAME@_cbk, FIRST_CHILD(this),
f338ef
                     FIRST_CHILD(this)->fops->@NAME@, @SHORT_ARGS@);
f338ef
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
f338ef
index d22bbc2..e0ea85b 100644
f338ef
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
f338ef
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
f338ef
@@ -425,8 +425,8 @@ posix_setattr(call_frame_t *frame, xlator_t *this, loc_t *loc,
f338ef
                    real_path);
f338ef
             goto out;
f338ef
         }
f338ef
-        posix_update_utime_in_mdata(this, real_path, -1, loc->inode, stbuf,
f338ef
-                                    valid);
f338ef
+        posix_update_utime_in_mdata(this, real_path, -1, loc->inode,
f338ef
+                                    &frame->root->ctime, stbuf, valid);
f338ef
     }
f338ef
 
f338ef
     if (valid & GF_SET_ATTR_CTIME && !priv->ctime) {
f338ef
@@ -652,8 +652,8 @@ posix_fsetattr(call_frame_t *frame, xlator_t *this, fd_t *fd,
f338ef
                    fd);
f338ef
             goto out;
f338ef
         }
f338ef
-        posix_update_utime_in_mdata(this, NULL, pfd->fd, fd->inode, stbuf,
f338ef
-                                    valid);
f338ef
+        posix_update_utime_in_mdata(this, NULL, pfd->fd, fd->inode,
f338ef
+                                    &frame->root->ctime, stbuf, valid);
f338ef
     }
f338ef
 
f338ef
     if (!valid) {
f338ef
diff --git a/xlators/storage/posix/src/posix-metadata.c b/xlators/storage/posix/src/posix-metadata.c
f338ef
index 5cbdc98..532daa2 100644
f338ef
--- a/xlators/storage/posix/src/posix-metadata.c
f338ef
+++ b/xlators/storage/posix/src/posix-metadata.c
f338ef
@@ -432,8 +432,10 @@ out:
f338ef
  */
f338ef
 static int
f338ef
 posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
f338ef
-                      inode_t *inode, struct timespec *time, struct iatt *stbuf,
f338ef
-                      posix_mdata_flag_t *flag, gf_boolean_t update_utime)
f338ef
+                      inode_t *inode, struct timespec *time,
f338ef
+                      struct timespec *u_atime, struct timespec *u_mtime,
f338ef
+                      struct iatt *stbuf, posix_mdata_flag_t *flag,
f338ef
+                      gf_boolean_t update_utime)
f338ef
 {
f338ef
     posix_mdata_t *mdata = NULL;
f338ef
     int ret = -1;
f338ef
@@ -443,6 +445,10 @@ posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
f338ef
     GF_VALIDATE_OR_GOTO(this->name, inode, out);
f338ef
     GF_VALIDATE_OR_GOTO(this->name, time, out);
f338ef
 
f338ef
+    if (update_utime && (!u_atime || !u_mtime)) {
f338ef
+        goto out;
f338ef
+    }
f338ef
+
f338ef
     LOCK(&inode->lock);
f338ef
     {
f338ef
         ret = __inode_ctx_get1(inode, this, (uint64_t *)&mdata);
f338ef
@@ -506,32 +512,30 @@ posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
f338ef
             }
f338ef
         }
f338ef
 
f338ef
-        /* Earlier, mdata was updated only if the existing time is less
f338ef
-         * than the time to be updated. This would fail the scenarios
f338ef
-         * where mtime can be set to any time using the syscall. Hence
f338ef
-         * just updating without comparison. But the ctime is not
f338ef
-         * allowed to changed to older date.
f338ef
-         */
f338ef
-
f338ef
-        if (flag->ctime && posix_compare_timespec(time, &mdata->ctime) > 0) {
f338ef
-            mdata->ctime = *time;
f338ef
-        }
f338ef
-
f338ef
         /* In distributed systems, there could be races with fops
f338ef
          * updating mtime/atime which could result in different
f338ef
          * mtime/atime for same file. So this makes sure, only the
f338ef
          * highest time is retained. If the mtime/atime update comes
f338ef
          * from the explicit utime syscall, it is allowed to set to
f338ef
-         * previous time
f338ef
+         * previous or future time but the ctime is always set to
f338ef
+         * current time.
f338ef
          */
f338ef
         if (update_utime) {
f338ef
+            if (flag->ctime &&
f338ef
+                posix_compare_timespec(time, &mdata->ctime) > 0) {
f338ef
+                mdata->ctime = *time;
f338ef
+            }
f338ef
             if (flag->mtime) {
f338ef
-                mdata->mtime = *time;
f338ef
+                mdata->mtime = *u_mtime;
f338ef
             }
f338ef
             if (flag->atime) {
f338ef
-                mdata->atime = *time;
f338ef
+                mdata->atime = *u_atime;
f338ef
             }
f338ef
         } else {
f338ef
+            if (flag->ctime &&
f338ef
+                posix_compare_timespec(time, &mdata->ctime) > 0) {
f338ef
+                mdata->ctime = *time;
f338ef
+            }
f338ef
             if (flag->mtime &&
f338ef
                 posix_compare_timespec(time, &mdata->mtime) > 0) {
f338ef
                 mdata->mtime = *time;
f338ef
@@ -584,15 +588,22 @@ out:
f338ef
  */
f338ef
 void
f338ef
 posix_update_utime_in_mdata(xlator_t *this, const char *real_path, int fd,
f338ef
-                            inode_t *inode, struct iatt *stbuf, int valid)
f338ef
+                            inode_t *inode, struct timespec *ctime,
f338ef
+                            struct iatt *stbuf, int valid)
f338ef
 {
f338ef
     int32_t ret = 0;
f338ef
 #if defined(HAVE_UTIMENSAT)
f338ef
-    struct timespec tv = {
f338ef
+    struct timespec tv_atime = {
f338ef
+        0,
f338ef
+    };
f338ef
+    struct timespec tv_mtime = {
f338ef
         0,
f338ef
     };
f338ef
 #else
f338ef
-    struct timeval tv = {
f338ef
+    struct timeval tv_atime = {
f338ef
+        0,
f338ef
+    };
f338ef
+    struct timeval tv_mtime = {
f338ef
         0,
f338ef
     };
f338ef
 #endif
f338ef
@@ -611,35 +622,28 @@ posix_update_utime_in_mdata(xlator_t *this, const char *real_path, int fd,
f338ef
      */
f338ef
     if (inode && priv->ctime) {
f338ef
         if ((valid & GF_SET_ATTR_ATIME) == GF_SET_ATTR_ATIME) {
f338ef
-            tv.tv_sec = stbuf->ia_atime;
f338ef
-            SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv, stbuf->ia_atime_nsec);
f338ef
+            tv_atime.tv_sec = stbuf->ia_atime;
f338ef
+            SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv_atime, stbuf->ia_atime_nsec);
f338ef
 
f338ef
-            flag.ctime = 0;
f338ef
-            flag.mtime = 0;
f338ef
+            flag.ctime = 1;
f338ef
             flag.atime = 1;
f338ef
-            ret = posix_set_mdata_xattr(this, real_path, -1, inode, &tv, NULL,
f338ef
-                                        &flag, _gf_true);
f338ef
-            if (ret) {
f338ef
-                gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
f338ef
-                       "posix set mdata atime failed on file:"
f338ef
-                       " %s gfid:%s",
f338ef
-                       real_path, uuid_utoa(inode->gfid));
f338ef
-            }
f338ef
         }
f338ef
 
f338ef
         if ((valid & GF_SET_ATTR_MTIME) == GF_SET_ATTR_MTIME) {
f338ef
-            tv.tv_sec = stbuf->ia_mtime;
f338ef
-            SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv, stbuf->ia_mtime_nsec);
f338ef
+            tv_mtime.tv_sec = stbuf->ia_mtime;
f338ef
+            SET_TIMESPEC_NSEC_OR_TIMEVAL_USEC(tv_mtime, stbuf->ia_mtime_nsec);
f338ef
 
f338ef
-            flag.ctime = 0;
f338ef
+            flag.ctime = 1;
f338ef
             flag.mtime = 1;
f338ef
-            flag.atime = 0;
f338ef
+        }
f338ef
 
f338ef
-            ret = posix_set_mdata_xattr(this, real_path, -1, inode, &tv, NULL,
f338ef
-                                        &flag, _gf_true);
f338ef
+        if (flag.mtime || flag.atime) {
f338ef
+            ret = posix_set_mdata_xattr(this, real_path, -1, inode, ctime,
f338ef
+                                        &tv_atime, &tv_mtime, NULL, &flag,
f338ef
+                                        _gf_true);
f338ef
             if (ret) {
f338ef
                 gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
f338ef
-                       "posix set mdata mtime failed on file:"
f338ef
+                       "posix set mdata atime failed on file:"
f338ef
                        " %s gfid:%s",
f338ef
                        real_path, uuid_utoa(inode->gfid));
f338ef
             }
f338ef
@@ -702,8 +706,8 @@ posix_set_ctime(call_frame_t *frame, xlator_t *this, const char *real_path,
f338ef
             goto out;
f338ef
         }
f338ef
         ret = posix_set_mdata_xattr(this, real_path, fd, inode,
f338ef
-                                    &frame->root->ctime, stbuf, &flag,
f338ef
-                                    _gf_false);
f338ef
+                                    &frame->root->ctime, NULL, NULL, stbuf,
f338ef
+                                    &flag, _gf_false);
f338ef
         if (ret) {
f338ef
             gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
f338ef
                    "posix set mdata failed on file: %s gfid:%s", real_path,
f338ef
@@ -733,8 +737,8 @@ posix_set_parent_ctime(call_frame_t *frame, xlator_t *this,
f338ef
             goto out;
f338ef
         }
f338ef
         ret = posix_set_mdata_xattr(this, real_path, fd, inode,
f338ef
-                                    &frame->root->ctime, stbuf, &flag,
f338ef
-                                    _gf_false);
f338ef
+                                    &frame->root->ctime, NULL, NULL, stbuf,
f338ef
+                                    &flag, _gf_false);
f338ef
         if (ret) {
f338ef
             gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
f338ef
                    "posix set mdata failed on file: %s gfid:%s", real_path,
f338ef
@@ -792,8 +796,8 @@ posix_set_ctime_cfr(call_frame_t *frame, xlator_t *this,
f338ef
             flag_dup.atime = 0;
f338ef
 
f338ef
         ret = posix_set_mdata_xattr(this, real_path_out, fd_out, inode_out,
f338ef
-                                    &frame->root->ctime, stbuf_out, &flag_dup,
f338ef
-                                    _gf_false);
f338ef
+                                    &frame->root->ctime, NULL, NULL, stbuf_out,
f338ef
+                                    &flag_dup, _gf_false);
f338ef
         if (ret) {
f338ef
             gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
f338ef
                    "posix set mdata failed on file: %s gfid:%s", real_path_out,
f338ef
@@ -811,8 +815,8 @@ posix_set_ctime_cfr(call_frame_t *frame, xlator_t *this,
f338ef
         flag_dup.ctime = 0;
f338ef
 
f338ef
         ret = posix_set_mdata_xattr(this, real_path_in, fd_out, inode_out,
f338ef
-                                    &frame->root->ctime, stbuf_out, &flag_dup,
f338ef
-                                    _gf_false);
f338ef
+                                    &frame->root->ctime, NULL, NULL, stbuf_out,
f338ef
+                                    &flag_dup, _gf_false);
f338ef
         if (ret) {
f338ef
             gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
f338ef
                    "posix set mdata failed on file: %s gfid:%s", real_path_in,
f338ef
diff --git a/xlators/storage/posix/src/posix-metadata.h b/xlators/storage/posix/src/posix-metadata.h
f338ef
index dc25e59..c176699 100644
f338ef
--- a/xlators/storage/posix/src/posix-metadata.h
f338ef
+++ b/xlators/storage/posix/src/posix-metadata.h
f338ef
@@ -40,7 +40,8 @@ __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
f338ef
                         inode_t *inode, struct iatt *stbuf);
f338ef
 void
f338ef
 posix_update_utime_in_mdata(xlator_t *this, const char *real_path, int fd,
f338ef
-                            inode_t *inode, struct iatt *stbuf, int valid);
f338ef
+                            inode_t *inode, struct timespec *ctime,
f338ef
+                            struct iatt *stbuf, int valid);
f338ef
 void
f338ef
 posix_set_ctime(call_frame_t *frame, xlator_t *this, const char *real_path,
f338ef
                 int fd, inode_t *inode, struct iatt *stbuf);
f338ef
-- 
f338ef
1.8.3.1
f338ef