f338ef
From ad233c1b3abdfe2bdfd1eacc83b5f84b7afa6b46 Mon Sep 17 00:00:00 2001
f338ef
From: N Balachandran <nbalacha@redhat.com>
f338ef
Date: Tue, 1 Oct 2019 17:37:15 +0530
f338ef
Subject: [PATCH 304/304] cluster/dht: Correct fd processing loop
f338ef
f338ef
The fd processing loops in the
f338ef
dht_migration_complete_check_task and the
f338ef
dht_rebalance_inprogress_task functions were unsafe
f338ef
and could cause an open to be sent on an already freed
f338ef
fd. This has been fixed.
f338ef
f338ef
> Change-Id: I0a3c7d2fba314089e03dfd704f9dceb134749540
f338ef
> Fixes: bz#1757399
f338ef
> Signed-off-by: N Balachandran <nbalacha@redhat.com>
f338ef
> (Cherry picked from commit 9b15867070b0cc241ab165886292ecffc3bc0aed)
f338ef
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/23506/)
f338ef
f338ef
Change-Id: I0a3c7d2fba314089e03dfd704f9dceb134749540
f338ef
BUG: 1756325
f338ef
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
f338ef
Reviewed-on: https://code.engineering.redhat.com/gerrit/182826
f338ef
Tested-by: RHGS Build Bot <nigelb@redhat.com>
f338ef
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
f338ef
---
f338ef
 xlators/cluster/dht/src/dht-helper.c | 84 ++++++++++++++++++++++++++----------
f338ef
 1 file changed, 62 insertions(+), 22 deletions(-)
f338ef
f338ef
diff --git a/xlators/cluster/dht/src/dht-helper.c b/xlators/cluster/dht/src/dht-helper.c
f338ef
index 4c57e0d..1e9fee0 100644
f338ef
--- a/xlators/cluster/dht/src/dht-helper.c
f338ef
+++ b/xlators/cluster/dht/src/dht-helper.c
f338ef
@@ -1261,6 +1261,7 @@ dht_migration_complete_check_task(void *data)
f338ef
     fd_t *tmp = NULL;
f338ef
     uint64_t tmp_miginfo = 0;
f338ef
     dht_migrate_info_t *miginfo = NULL;
f338ef
+    gf_boolean_t skip_open = _gf_false;
f338ef
     int open_failed = 0;
f338ef
 
f338ef
     this = THIS;
f338ef
@@ -1399,24 +1400,34 @@ dht_migration_complete_check_task(void *data)
f338ef
      * the loop will cause the destruction of the fd. So we need to
f338ef
      * iterate the list safely because iter_fd cannot be trusted.
f338ef
      */
f338ef
-    list_for_each_entry_safe(iter_fd, tmp, &inode->fd_list, inode_list)
f338ef
-    {
f338ef
-        if (fd_is_anonymous(iter_fd))
f338ef
-            continue;
f338ef
-
f338ef
-        if (dht_fd_open_on_dst(this, iter_fd, dst_node))
f338ef
-            continue;
f338ef
-
f338ef
+    iter_fd = list_entry((&inode->fd_list)->next, typeof(*iter_fd), inode_list);
f338ef
+    while (&iter_fd->inode_list != (&inode->fd_list)) {
f338ef
+        if (fd_is_anonymous(iter_fd) ||
f338ef
+            (dht_fd_open_on_dst(this, iter_fd, dst_node))) {
f338ef
+            if (!tmp) {
f338ef
+                iter_fd = list_entry(iter_fd->inode_list.next, typeof(*iter_fd),
f338ef
+                                     inode_list);
f338ef
+                continue;
f338ef
+            }
f338ef
+            skip_open = _gf_true;
f338ef
+        }
f338ef
         /* We need to release the inode->lock before calling
f338ef
          * syncop_open() to avoid possible deadlocks. However this
f338ef
          * can cause the iter_fd to be released by other threads.
f338ef
          * To avoid this, we take a reference before releasing the
f338ef
          * lock.
f338ef
          */
f338ef
-        __fd_ref(iter_fd);
f338ef
+        fd_ref(iter_fd);
f338ef
 
f338ef
         UNLOCK(&inode->lock);
f338ef
 
f338ef
+        if (tmp) {
f338ef
+            fd_unref(tmp);
f338ef
+            tmp = NULL;
f338ef
+        }
f338ef
+        if (skip_open)
f338ef
+            goto next;
f338ef
+
f338ef
         /* flags for open are stripped down to allow following the
f338ef
          * new location of the file, otherwise we can get EEXIST or
f338ef
          * truncate the file again as rebalance is moving the data */
f338ef
@@ -1438,9 +1449,11 @@ dht_migration_complete_check_task(void *data)
f338ef
             dht_fd_ctx_set(this, iter_fd, dst_node);
f338ef
         }
f338ef
 
f338ef
-        fd_unref(iter_fd);
f338ef
-
f338ef
+    next:
f338ef
         LOCK(&inode->lock);
f338ef
+        skip_open = _gf_false;
f338ef
+        tmp = iter_fd;
f338ef
+        iter_fd = list_entry(tmp->inode_list.next, typeof(*tmp), inode_list);
f338ef
     }
f338ef
 
f338ef
     SYNCTASK_SETID(frame->root->uid, frame->root->gid);
f338ef
@@ -1453,6 +1466,10 @@ dht_migration_complete_check_task(void *data)
f338ef
 
f338ef
 unlock:
f338ef
     UNLOCK(&inode->lock);
f338ef
+    if (tmp) {
f338ef
+        fd_unref(tmp);
f338ef
+        tmp = NULL;
f338ef
+    }
f338ef
 
f338ef
 out:
f338ef
     if (dict) {
f338ef
@@ -1534,6 +1551,7 @@ dht_rebalance_inprogress_task(void *data)
f338ef
     int open_failed = 0;
f338ef
     uint64_t tmp_miginfo = 0;
f338ef
     dht_migrate_info_t *miginfo = NULL;
f338ef
+    gf_boolean_t skip_open = _gf_false;
f338ef
 
f338ef
     this = THIS;
f338ef
     frame = data;
f338ef
@@ -1654,24 +1672,40 @@ dht_rebalance_inprogress_task(void *data)
f338ef
      * the loop will cause the destruction of the fd. So we need to
f338ef
      * iterate the list safely because iter_fd cannot be trusted.
f338ef
      */
f338ef
-    list_for_each_entry_safe(iter_fd, tmp, &inode->fd_list, inode_list)
f338ef
-    {
f338ef
-        if (fd_is_anonymous(iter_fd))
f338ef
-            continue;
f338ef
-
f338ef
-        if (dht_fd_open_on_dst(this, iter_fd, dst_node))
f338ef
-            continue;
f338ef
-
f338ef
+    iter_fd = list_entry((&inode->fd_list)->next, typeof(*iter_fd), inode_list);
f338ef
+    while (&iter_fd->inode_list != (&inode->fd_list)) {
f338ef
         /* We need to release the inode->lock before calling
f338ef
          * syncop_open() to avoid possible deadlocks. However this
f338ef
          * can cause the iter_fd to be released by other threads.
f338ef
          * To avoid this, we take a reference before releasing the
f338ef
          * lock.
f338ef
          */
f338ef
-        __fd_ref(iter_fd);
f338ef
 
f338ef
+        if (fd_is_anonymous(iter_fd) ||
f338ef
+            (dht_fd_open_on_dst(this, iter_fd, dst_node))) {
f338ef
+            if (!tmp) {
f338ef
+                iter_fd = list_entry(iter_fd->inode_list.next, typeof(*iter_fd),
f338ef
+                                     inode_list);
f338ef
+                continue;
f338ef
+            }
f338ef
+            skip_open = _gf_true;
f338ef
+        }
f338ef
+
f338ef
+        /* Yes, this is ugly but there isn't a cleaner way to do this
f338ef
+         * the fd_ref is an atomic increment so not too bad. We want to
f338ef
+         * reduce the number of inode locks and unlocks.
f338ef
+         */
f338ef
+
f338ef
+        fd_ref(iter_fd);
f338ef
         UNLOCK(&inode->lock);
f338ef
 
f338ef
+        if (tmp) {
f338ef
+            fd_unref(tmp);
f338ef
+            tmp = NULL;
f338ef
+        }
f338ef
+        if (skip_open)
f338ef
+            goto next;
f338ef
+
f338ef
         /* flags for open are stripped down to allow following the
f338ef
          * new location of the file, otherwise we can get EEXIST or
f338ef
          * truncate the file again as rebalance is moving the data */
f338ef
@@ -1692,9 +1726,11 @@ dht_rebalance_inprogress_task(void *data)
f338ef
             dht_fd_ctx_set(this, iter_fd, dst_node);
f338ef
         }
f338ef
 
f338ef
-        fd_unref(iter_fd);
f338ef
-
f338ef
+    next:
f338ef
         LOCK(&inode->lock);
f338ef
+        skip_open = _gf_false;
f338ef
+        tmp = iter_fd;
f338ef
+        iter_fd = list_entry(tmp->inode_list.next, typeof(*tmp), inode_list);
f338ef
     }
f338ef
 
f338ef
     SYNCTASK_SETID(frame->root->uid, frame->root->gid);
f338ef
@@ -1702,6 +1738,10 @@ dht_rebalance_inprogress_task(void *data)
f338ef
 unlock:
f338ef
     UNLOCK(&inode->lock);
f338ef
 
f338ef
+    if (tmp) {
f338ef
+        fd_unref(tmp);
f338ef
+        tmp = NULL;
f338ef
+    }
f338ef
     if (open_failed) {
f338ef
         ret = -1;
f338ef
         goto out;
f338ef
-- 
f338ef
1.8.3.1
f338ef