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