17b94a
From 211d0f7dbb4991b2191925973222ebc79f010e84 Mon Sep 17 00:00:00 2001
17b94a
From: Xavi Hernandez <xhernandez@redhat.com>
17b94a
Date: Sun, 8 Mar 2020 18:36:45 +0100
17b94a
Subject: [PATCH 521/526] open-behind: fix missing fd reference
17b94a
17b94a
Open behind was not keeping any reference on fd's pending to be
17b94a
opened. This makes it possible that a concurrent close and en entry
17b94a
fop (unlink, rename, ...) caused destruction of the fd while it
17b94a
was still being used.
17b94a
17b94a
Upstream patch:
17b94a
> Upstream patch link: https://review.gluster.org/c/glusterfs/+/24204
17b94a
> Change-Id: Ie9e992902cf2cd7be4af1f8b4e57af9bd6afd8e9
17b94a
> Fixes: bz#1810934
17b94a
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
17b94a
17b94a
Change-Id: Ie9e992902cf2cd7be4af1f8b4e57af9bd6afd8e9
17b94a
BUG: 1830713
17b94a
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
17b94a
Reviewed-on: https://code.engineering.redhat.com/gerrit/224485
17b94a
Tested-by: RHGS Build Bot <nigelb@redhat.com>
17b94a
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
17b94a
---
17b94a
 xlators/performance/open-behind/src/open-behind.c | 27 ++++++++++++++---------
17b94a
 1 file changed, 16 insertions(+), 11 deletions(-)
17b94a
17b94a
diff --git a/xlators/performance/open-behind/src/open-behind.c b/xlators/performance/open-behind/src/open-behind.c
17b94a
index 3ee3c40..dd2f2fd 100644
17b94a
--- a/xlators/performance/open-behind/src/open-behind.c
17b94a
+++ b/xlators/performance/open-behind/src/open-behind.c
17b94a
@@ -206,8 +206,13 @@ ob_fd_free(ob_fd_t *ob_fd)
17b94a
     if (ob_fd->xdata)
17b94a
         dict_unref(ob_fd->xdata);
17b94a
 
17b94a
-    if (ob_fd->open_frame)
17b94a
+    if (ob_fd->open_frame) {
17b94a
+        /* If we sill have a frame it means that background open has never
17b94a
+         * been triggered. We need to release the pending reference. */
17b94a
+        fd_unref(ob_fd->fd);
17b94a
+
17b94a
         STACK_DESTROY(ob_fd->open_frame->root);
17b94a
+    }
17b94a
 
17b94a
     GF_FREE(ob_fd);
17b94a
 }
17b94a
@@ -297,6 +302,7 @@ ob_wake_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int op_ret,
17b94a
             call_resume(stub);
17b94a
     }
17b94a
 
17b94a
+    /* The background open is completed. We can release the 'fd' reference. */
17b94a
     fd_unref(fd);
17b94a
 
17b94a
     STACK_DESTROY(frame->root);
17b94a
@@ -331,7 +337,9 @@ ob_fd_wake(xlator_t *this, fd_t *fd, ob_fd_t *ob_fd)
17b94a
     }
17b94a
 
17b94a
     if (frame) {
17b94a
-        frame->local = fd_ref(fd);
17b94a
+        /* We don't need to take a reference here. We already have a reference
17b94a
+         * while the open is pending. */
17b94a
+        frame->local = fd;
17b94a
 
17b94a
         STACK_WIND(frame, ob_wake_cbk, FIRST_CHILD(this),
17b94a
                    FIRST_CHILD(this)->fops->open, &ob_fd->loc, ob_fd->flags, fd,
17b94a
@@ -345,15 +353,12 @@ void
17b94a
 ob_inode_wake(xlator_t *this, struct list_head *ob_fds)
17b94a
 {
17b94a
     ob_fd_t *ob_fd = NULL, *tmp = NULL;
17b94a
-    fd_t *fd = NULL;
17b94a
 
17b94a
     if (!list_empty(ob_fds)) {
17b94a
         list_for_each_entry_safe(ob_fd, tmp, ob_fds, ob_fds_on_inode)
17b94a
         {
17b94a
             ob_fd_wake(this, ob_fd->fd, ob_fd);
17b94a
-            fd = ob_fd->fd;
17b94a
             ob_fd_free(ob_fd);
17b94a
-            fd_unref(fd);
17b94a
         }
17b94a
     }
17b94a
 }
17b94a
@@ -365,7 +370,7 @@ ob_fd_copy(ob_fd_t *src, ob_fd_t *dst)
17b94a
     if (!src || !dst)
17b94a
         goto out;
17b94a
 
17b94a
-    dst->fd = __fd_ref(src->fd);
17b94a
+    dst->fd = src->fd;
17b94a
     dst->loc.inode = inode_ref(src->loc.inode);
17b94a
     gf_uuid_copy(dst->loc.gfid, src->loc.gfid);
17b94a
     dst->flags = src->flags;
17b94a
@@ -509,7 +514,6 @@ ob_open_behind(call_frame_t *frame, xlator_t *this, loc_t *loc, int flags,
17b94a
 
17b94a
     ob_fd->ob_inode = ob_inode;
17b94a
 
17b94a
-    /* don't do fd_ref, it'll cause leaks */
17b94a
     ob_fd->fd = fd;
17b94a
 
17b94a
     ob_fd->open_frame = copy_frame(frame);
17b94a
@@ -539,15 +543,16 @@ ob_open_behind(call_frame_t *frame, xlator_t *this, loc_t *loc, int flags,
17b94a
     }
17b94a
     UNLOCK(&fd->inode->lock);
17b94a
 
17b94a
-    if (!open_in_progress && !unlinked) {
17b94a
-        fd_ref(fd);
17b94a
+    /* We take a reference while the background open is pending or being
17b94a
+     * processed. If we finally wind the request in the foreground, then
17b94a
+     * ob_fd_free() will take care of this additional reference. */
17b94a
+    fd_ref(fd);
17b94a
 
17b94a
+    if (!open_in_progress && !unlinked) {
17b94a
         STACK_UNWIND_STRICT(open, frame, 0, 0, fd, xdata);
17b94a
 
17b94a
         if (!conf->lazy_open)
17b94a
             ob_fd_wake(this, fd, NULL);
17b94a
-
17b94a
-        fd_unref(fd);
17b94a
     } else {
17b94a
         ob_fd_free(ob_fd);
17b94a
         STACK_WIND(frame, default_open_cbk, FIRST_CHILD(this),
17b94a
-- 
17b94a
1.8.3.1
17b94a