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