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