21ab4e
From b0c1f0ac02190dc2fbacd174ceab554e639fcda5 Mon Sep 17 00:00:00 2001
21ab4e
From: Ravishankar N <ravishankar@redhat.com>
21ab4e
Date: Mon, 29 May 2017 21:38:14 +0530
21ab4e
Subject: [PATCH 486/486] posix: use the correct op_errno
21ab4e
21ab4e
Backport of : https://review.gluster.org/17414
21ab4e
21ab4e
Problem:
21ab4e
If readdir/fstat was performed on a directory that was removed,
21ab4e
posix_fd_ctx_get() fails with ENOENT but we incorrectly use the ret
21ab4e
value (-1 in this case) as op_errno, logging "Operation not permitted"
21ab4e
messages in the brick logs. Also in case of fstat, the -1 op_errno was
21ab4e
also propagated to the client via stack unwind, causing the message to
21ab4e
appear in protocol/client logs as well.
21ab4e
21ab4e
Fix:
21ab4e
Use the right op_errno in readdir, fstat and writev. Also, if
21ab4e
posix_fd_ctx_get() failed with ENOENT, convert it into EBADF because
21ab4e
ENOENT is not a valid error for an fd operation.
21ab4e
21ab4e
Change-Id: I69d34dc5dbb76db38ad432968bbd4e5726aeb6d7
21ab4e
BUG: 1454689
21ab4e
Signed-off-by: Ravishankar N <ravishankar@redhat.com>
21ab4e
Reviewed-on: https://code.engineering.redhat.com/gerrit/108107
21ab4e
---
21ab4e
 xlators/storage/posix/src/posix-helpers.c | 18 +++++++++++++-----
21ab4e
 xlators/storage/posix/src/posix.c         |  4 +---
21ab4e
 2 files changed, 14 insertions(+), 8 deletions(-)
21ab4e
21ab4e
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
21ab4e
index 30f09ac..411d0ba 100644
21ab4e
--- a/xlators/storage/posix/src/posix-helpers.c
21ab4e
+++ b/xlators/storage/posix/src/posix-helpers.c
21ab4e
@@ -1697,7 +1697,13 @@ __posix_fd_ctx_get (fd_t *fd, xlator_t *this, struct posix_fd **pfd_p,
21ab4e
         if (fd->inode->ia_type == IA_IFDIR) {
21ab4e
                 dir = sys_opendir (real_path);
21ab4e
                 if (!dir) {
21ab4e
-                        op_errno = errno;
21ab4e
+                        (errno == ENOENT) ? (op_errno = EBADF) :
21ab4e
+                                            (op_errno = errno);
21ab4e
+                        gf_msg (this->name, GF_LOG_ERROR, errno,
21ab4e
+                                P_MSG_READ_FAILED,
21ab4e
+                                "Failed to get anonymous fd for "
21ab4e
+                                "real_path: %s. Returning %s.", real_path,
21ab4e
+                                strerror(op_errno));
21ab4e
                         GF_FREE (pfd);
21ab4e
                         pfd = NULL;
21ab4e
                         goto out;
21ab4e
@@ -1718,11 +1724,13 @@ __posix_fd_ctx_get (fd_t *fd, xlator_t *this, struct posix_fd **pfd_p,
21ab4e
                         _fd = open (unlink_path, fd->flags);
21ab4e
                 }
21ab4e
                 if (_fd == -1) {
21ab4e
-                        op_errno = errno;
21ab4e
-                        gf_msg (this->name, GF_LOG_ERROR, op_errno,
21ab4e
+                        (errno == ENOENT) ? (op_errno = EBADF) :
21ab4e
+                                            (op_errno = errno);
21ab4e
+                        gf_msg (this->name, GF_LOG_ERROR, errno,
21ab4e
                                 P_MSG_READ_FAILED,
21ab4e
-                                "Failed to get anonymous "
21ab4e
-                                "real_path: %s _fd = %d", real_path, _fd);
21ab4e
+                                "Failed to get anonymous fd for "
21ab4e
+                                "real_path: %s. Returning %s.", real_path,
21ab4e
+                                strerror(op_errno));
21ab4e
                         GF_FREE (pfd);
21ab4e
                         pfd = NULL;
21ab4e
                         goto out;
21ab4e
diff --git a/xlators/storage/posix/src/posix.c b/xlators/storage/posix/src/posix.c
21ab4e
index ecc45f0..7bd0dc5 100644
21ab4e
--- a/xlators/storage/posix/src/posix.c
21ab4e
+++ b/xlators/storage/posix/src/posix.c
21ab4e
@@ -3385,7 +3385,6 @@ posix_writev (call_frame_t *frame, xlator_t *this, fd_t *fd,
21ab4e
         if (ret < 0) {
21ab4e
                 gf_msg (this->name, GF_LOG_WARNING, ret, P_MSG_PFD_NULL,
21ab4e
                         "pfd is NULL from fd=%p", fd);
21ab4e
-                op_errno = -ret;
21ab4e
                 goto out;
21ab4e
         }
21ab4e
 
21ab4e
@@ -5916,7 +5915,6 @@ posix_fstat (call_frame_t *frame, xlator_t *this,
21ab4e
         if (ret < 0) {
21ab4e
                 gf_msg (this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
21ab4e
                         "pfd is NULL, fd=%p", fd);
21ab4e
-                op_errno = -ret;
21ab4e
                 goto out;
21ab4e
         }
21ab4e
 
21ab4e
@@ -6348,7 +6346,7 @@ posix_do_readdir (call_frame_t *frame, xlator_t *this,
21ab4e
 
21ab4e
         ret = posix_fd_ctx_get (fd, this, &pfd, &op_errno);
21ab4e
         if (ret < 0) {
21ab4e
-                gf_msg (this->name, GF_LOG_WARNING, -ret, P_MSG_PFD_NULL,
21ab4e
+                gf_msg (this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
21ab4e
                         "pfd is NULL, fd=%p", fd);
21ab4e
                 goto out;
21ab4e
         }
21ab4e
-- 
21ab4e
1.8.3.1
21ab4e