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