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