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