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