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