74b1de
From 2f07d12f902e371d8cb8c76007d558e3a727b56a Mon Sep 17 00:00:00 2001
74b1de
From: Kotresh HR <khiremat@redhat.com>
74b1de
Date: Tue, 9 Apr 2019 18:23:05 +0530
74b1de
Subject: [PATCH 122/124] posix/ctime: Fix stat(time attributes) inconsistency
74b1de
 during readdirp
74b1de
74b1de
Problem:
74b1de
   Creation of tar file on gluster volume throws warning
74b1de
'file changed as we read it'
74b1de
74b1de
Cause:
74b1de
   During readdirp, for few of the files whose inode is not
74b1de
present, time attributes were served from backend. This caused
74b1de
the ctime of few files to be different between before readdir
74b1de
and after readdir by tar.
74b1de
74b1de
Solution:
74b1de
  If ctime feature is enabled and inode is not present, don't
74b1de
serve the time attributes from backend file, serve it from xattr.
74b1de
74b1de
Backport of:
74b1de
 > Patch: https://review.gluster.org/22540
74b1de
 > fixes: bz#1698078
74b1de
 > Change-Id: I427ef865f97399475faf5aa6ca495f7e317603ae
74b1de
 > Signed-off-by: Kotresh HR <khiremat@redhat.com>
74b1de
74b1de
BUG: 1699709
74b1de
Change-Id: I427ef865f97399475faf5aa6ca495f7e317603ae
74b1de
Signed-off-by: Kotresh HR <khiremat@redhat.com>
74b1de
Reviewed-on: https://code.engineering.redhat.com/gerrit/168687
74b1de
Tested-by: RHGS Build Bot <nigelb@redhat.com>
74b1de
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
74b1de
---
74b1de
 tests/basic/ctime/ctime-readdir.c          | 29 +++++++++++++++++
74b1de
 tests/basic/ctime/ctime-readdir.t          | 50 ++++++++++++++++++++++++++++++
74b1de
 xlators/storage/posix/src/posix-helpers.c  | 29 +++++++++++------
74b1de
 xlators/storage/posix/src/posix-metadata.c | 41 ++++++++++++++----------
74b1de
 4 files changed, 123 insertions(+), 26 deletions(-)
74b1de
 create mode 100644 tests/basic/ctime/ctime-readdir.c
74b1de
 create mode 100644 tests/basic/ctime/ctime-readdir.t
74b1de
74b1de
diff --git a/tests/basic/ctime/ctime-readdir.c b/tests/basic/ctime/ctime-readdir.c
74b1de
new file mode 100644
74b1de
index 0000000..8760db2
74b1de
--- /dev/null
74b1de
+++ b/tests/basic/ctime/ctime-readdir.c
74b1de
@@ -0,0 +1,29 @@
74b1de
+#include <stdio.h>
74b1de
+#include <dirent.h>
74b1de
+#include <string.h>
74b1de
+#include <assert.h>
74b1de
+
74b1de
+int
74b1de
+main(int argc, char **argv)
74b1de
+{
74b1de
+    DIR *dir = NULL;
74b1de
+    struct dirent *entry = NULL;
74b1de
+    int ret = 0;
74b1de
+    char *path = NULL;
74b1de
+
74b1de
+    assert(argc == 2);
74b1de
+    path = argv[1];
74b1de
+
74b1de
+    dir = opendir(path);
74b1de
+    if (!dir) {
74b1de
+        printf("opendir(%s) failed.\n", path);
74b1de
+        return -1;
74b1de
+    }
74b1de
+
74b1de
+    while ((entry = readdir(dir)) != NULL) {
74b1de
+    }
74b1de
+    if (dir)
74b1de
+        closedir(dir);
74b1de
+
74b1de
+    return ret;
74b1de
+}
74b1de
diff --git a/tests/basic/ctime/ctime-readdir.t b/tests/basic/ctime/ctime-readdir.t
74b1de
new file mode 100644
74b1de
index 0000000..4564fc1
74b1de
--- /dev/null
74b1de
+++ b/tests/basic/ctime/ctime-readdir.t
74b1de
@@ -0,0 +1,50 @@
74b1de
+#!/bin/bash
74b1de
+
74b1de
+. $(dirname $0)/../../include.rc
74b1de
+. $(dirname $0)/../../volume.rc
74b1de
+
74b1de
+cleanup;
74b1de
+
74b1de
+TEST glusterd
74b1de
+
74b1de
+TEST $CLI volume create $V0 replica 3 ${H0}:$B0/brick{1,2,3};
74b1de
+TEST $CLI volume set $V0 performance.stat-prefetch on
74b1de
+TEST $CLI volume set $V0 performance.readdir-ahead off
74b1de
+TEST $CLI volume start $V0;
74b1de
+
74b1de
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 $M0;
74b1de
+
74b1de
+TEST mkdir $M0/dir0
74b1de
+TEST "echo hello_world > $M0/dir0/FILE"
74b1de
+
74b1de
+ctime1=$(stat -c %Z $M0/dir0/FILE)
74b1de
+echo "Mount change time: $ctime1"
74b1de
+
74b1de
+sleep 2
74b1de
+
74b1de
+#Write to back end directly to modify ctime of backend file
74b1de
+TEST "echo write_from_backend >> $B0/brick1/dir0/FILE"
74b1de
+TEST "echo write_from_backend >> $B0/brick2/dir0/FILE"
74b1de
+TEST "echo write_from_backend >> $B0/brick3/dir0/FILE"
74b1de
+echo "Backend change time"
74b1de
+echo "brick1: $(stat -c %Z $B0/brick1/dir0/FILE)"
74b1de
+echo "brick2: $(stat -c %Z $B0/brick2/dir0/FILE)"
74b1de
+echo "brick3: $(stat -c %Z $B0/brick3/dir0/FILE)"
74b1de
+
74b1de
+#Stop and start to hit the case of no inode for readdir
74b1de
+TEST umount $M0
74b1de
+TEST $CLI volume stop $V0
74b1de
+TEST $CLI volume start $V0
74b1de
+TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 --entry-timeout=0 $M0;
74b1de
+
74b1de
+TEST build_tester $(dirname $0)/ctime-readdir.c
74b1de
+
74b1de
+#Do readdir
74b1de
+TEST ./$(dirname $0)/ctime-readdir $M0/dir0
74b1de
+
74b1de
+EXPECT "$ctime1" stat -c %Z $M0/dir0/FILE
74b1de
+echo "Mount change time after readdir $(stat -c %Z $M0/dir0/FILE)"
74b1de
+
74b1de
+cleanup_tester $(dirname $0)/ctime-readdir
74b1de
+
74b1de
+cleanup;
74b1de
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
74b1de
index 193afc5..37e33a9 100644
74b1de
--- a/xlators/storage/posix/src/posix-helpers.c
74b1de
+++ b/xlators/storage/posix/src/posix-helpers.c
74b1de
@@ -832,17 +832,26 @@ posix_pstat(xlator_t *this, inode_t *inode, uuid_t gfid, const char *path,
74b1de
 
74b1de
     iatt_from_stat(&stbuf, &lstatbuf);
74b1de
 
74b1de
-    if (inode && priv->ctime) {
74b1de
-        if (!inode_locked) {
74b1de
-            ret = posix_get_mdata_xattr(this, path, -1, inode, &stbuf);
74b1de
+    if (priv->ctime) {
74b1de
+        if (inode) {
74b1de
+            if (!inode_locked) {
74b1de
+                ret = posix_get_mdata_xattr(this, path, -1, inode, &stbuf);
74b1de
+            } else {
74b1de
+                ret = __posix_get_mdata_xattr(this, path, -1, inode, &stbuf);
74b1de
+            }
74b1de
+            if (ret) {
74b1de
+                gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
74b1de
+                       "posix get mdata failed on gfid: %s",
74b1de
+                       uuid_utoa(inode->gfid));
74b1de
+                goto out;
74b1de
+            }
74b1de
         } else {
74b1de
-            ret = __posix_get_mdata_xattr(this, path, -1, inode, &stbuf);
74b1de
-        }
74b1de
-        if (ret) {
74b1de
-            gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
74b1de
-                   "posix get mdata failed on gfid: %s",
74b1de
-                   uuid_utoa(inode->gfid));
74b1de
-            goto out;
74b1de
+            ret = __posix_get_mdata_xattr(this, path, -1, NULL, &stbuf);
74b1de
+            if (ret) {
74b1de
+                gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED,
74b1de
+                       "posix get mdata failed on path: %s", path);
74b1de
+                goto out;
74b1de
+            }
74b1de
         }
74b1de
     }
74b1de
 
74b1de
diff --git a/xlators/storage/posix/src/posix-metadata.c b/xlators/storage/posix/src/posix-metadata.c
74b1de
index 0ea9099..7ff5225 100644
74b1de
--- a/xlators/storage/posix/src/posix-metadata.c
74b1de
+++ b/xlators/storage/posix/src/posix-metadata.c
74b1de
@@ -79,6 +79,7 @@ posix_fetch_mdata_xattr(xlator_t *this, const char *real_path_arg, int _fd,
74b1de
         fd_based_fop = _gf_true;
74b1de
     }
74b1de
     if (!(fd_based_fop || real_path_arg)) {
74b1de
+        GF_VALIDATE_OR_GOTO(this->name, inode, out);
74b1de
         MAKE_HANDLE_PATH(real_path, this, inode->gfid, NULL);
74b1de
         if (!real_path) {
74b1de
             uuid_utoa_r(inode->gfid, gfid_str);
74b1de
@@ -114,14 +115,14 @@ posix_fetch_mdata_xattr(xlator_t *this, const char *real_path_arg, int _fd,
74b1de
                          key,
74b1de
                          real_path ? real_path
74b1de
                                    : (real_path_arg ? real_path_arg : "null"),
74b1de
-                         uuid_utoa(inode->gfid));
74b1de
+                         inode ? uuid_utoa(inode->gfid) : "null");
74b1de
         } else {
74b1de
             gf_msg(this->name, GF_LOG_DEBUG, *op_errno, P_MSG_XATTR_FAILED,
74b1de
                    "getxattr failed"
74b1de
                    " on %s gfid: %s key: %s ",
74b1de
                    real_path ? real_path
74b1de
                              : (real_path_arg ? real_path_arg : "null"),
74b1de
-                   uuid_utoa(inode->gfid), key);
74b1de
+                   inode ? uuid_utoa(inode->gfid) : "null", key);
74b1de
         }
74b1de
         op_ret = -1;
74b1de
         goto out;
74b1de
@@ -148,7 +149,7 @@ posix_fetch_mdata_xattr(xlator_t *this, const char *real_path_arg, int _fd,
74b1de
                "getxattr failed on "
74b1de
                " on %s gfid: %s key: %s ",
74b1de
                real_path ? real_path : (real_path_arg ? real_path_arg : "null"),
74b1de
-               uuid_utoa(inode->gfid), key);
74b1de
+               inode ? uuid_utoa(inode->gfid) : "null", key);
74b1de
         goto out;
74b1de
     }
74b1de
 
74b1de
@@ -233,9 +234,14 @@ __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
74b1de
     int ret = -1;
74b1de
     int op_errno = 0;
74b1de
 
74b1de
-    GF_VALIDATE_OR_GOTO(this->name, inode, out);
74b1de
+    /* Handle readdirp: inode might be null, time attributes should be served
74b1de
+     * from xattr not from backend's file attributes */
74b1de
+    if (inode) {
74b1de
+        ret = __inode_ctx_get1(inode, this, (uint64_t *)&mdata);
74b1de
+    } else {
74b1de
+        ret = -1;
74b1de
+    }
74b1de
 
74b1de
-    ret = __inode_ctx_get1(inode, this, (uint64_t *)&mdata);
74b1de
     if (ret == -1 || !mdata) {
74b1de
         mdata = GF_CALLOC(1, sizeof(posix_mdata_t), gf_posix_mt_mdata_attr);
74b1de
         if (!mdata) {
74b1de
@@ -251,7 +257,9 @@ __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
74b1de
              * is hit when in-memory status is lost due to brick
74b1de
              * down scenario
74b1de
              */
74b1de
-            __inode_ctx_set1(inode, this, (uint64_t *)&mdata);
74b1de
+            if (inode) {
74b1de
+                __inode_ctx_set1(inode, this, (uint64_t *)&mdata);
74b1de
+            }
74b1de
         } else {
74b1de
             /* Failed to get mdata from disk, xattr missing.
74b1de
              * This happens on two cases.
74b1de
@@ -278,7 +286,8 @@ __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
74b1de
                  */
74b1de
                 gf_msg(this->name, GF_LOG_WARNING, op_errno,
74b1de
                        P_MSG_FETCHMDATA_FAILED, "file: %s: gfid: %s key:%s ",
74b1de
-                       real_path ? real_path : "null", uuid_utoa(inode->gfid),
74b1de
+                       real_path ? real_path : "null",
74b1de
+                       inode ? uuid_utoa(inode->gfid) : "null",
74b1de
                        GF_XATTR_MDATA_KEY);
74b1de
                 GF_FREE(mdata);
74b1de
                 ret = 0;
74b1de
@@ -297,6 +306,10 @@ __posix_get_mdata_xattr(xlator_t *this, const char *real_path, int _fd,
74b1de
         stbuf->ia_atime = mdata->atime.tv_sec;
74b1de
         stbuf->ia_atime_nsec = mdata->atime.tv_nsec;
74b1de
     }
74b1de
+    /* Not set in inode context, hence free mdata */
74b1de
+    if (!inode) {
74b1de
+        GF_FREE(mdata);
74b1de
+    }
74b1de
 
74b1de
 out:
74b1de
     return ret;
74b1de
@@ -416,6 +429,11 @@ posix_set_mdata_xattr(xlator_t *this, const char *real_path, int fd,
74b1de
             }
74b1de
         }
74b1de
 
74b1de
+        if ((flag->ctime == 0) && (flag->mtime == 0) && (flag->atime == 0)) {
74b1de
+            ret = 0;
74b1de
+            goto unlock;
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
@@ -486,7 +504,6 @@ out:
74b1de
         stbuf->ia_atime_nsec = mdata->atime.tv_nsec;
74b1de
     }
74b1de
 
74b1de
-
74b1de
     return ret;
74b1de
 }
74b1de
 
74b1de
@@ -604,10 +621,6 @@ posix_set_ctime(call_frame_t *frame, xlator_t *this, const char *real_path,
74b1de
 
74b1de
     if (priv->ctime) {
74b1de
         (void)posix_get_mdata_flag(frame->root->flags, &flag;;
74b1de
-        if ((flag.ctime == 0) && (flag.mtime == 0) && (flag.atime == 0)) {
74b1de
-            goto out;
74b1de
-        }
74b1de
-
74b1de
         if (frame->root->ctime.tv_sec == 0) {
74b1de
             gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_SETMDATA_FAILED,
74b1de
                    "posix set mdata failed, No ctime : %s gfid:%s", real_path,
74b1de
@@ -643,9 +656,6 @@ posix_set_parent_ctime(call_frame_t *frame, xlator_t *this,
74b1de
 
74b1de
     if (inode && priv->ctime) {
74b1de
         (void)posix_get_parent_mdata_flag(frame->root->flags, &flag;;
74b1de
-        if ((flag.ctime == 0) && (flag.mtime == 0) && (flag.atime == 0)) {
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
@@ -655,7 +665,6 @@ posix_set_parent_ctime(call_frame_t *frame, xlator_t *this,
74b1de
                    uuid_utoa(inode->gfid));
74b1de
         }
74b1de
     }
74b1de
-out:
74b1de
     return;
74b1de
 }
74b1de
 
74b1de
-- 
74b1de
1.8.3.1
74b1de