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