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