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