[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH] nbd: no need to check all the connections one by one if all are dead



From: Xiubo Li <xiubli@redhat.com>

If all connections are dead the live_connections should be already
set to 0. And set the nsock->fallback to -1 again if all connections
are dead.

Signed-off-by: Xiubo Li <xiubli@redhat.com>
---
 drivers/block/nbd.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 98be6ca..6da42aa 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -783,13 +783,16 @@ static int find_fallback(struct nbd_device *nbd, int index)
 		return new_index;
 	}
 
+	if (atomic_read(&config->live_connections) <= 0) {
+		dev_err_ratelimited(disk_to_dev(nbd->disk),
+				    "Dead connection, failed to find a fallback\n");
+		goto out;
+	}
+
 	if (fallback >= 0 && fallback < config->num_connections &&
-	    !config->socks[fallback]->dead)
+		!config->socks[fallback]->dead) {
 		return fallback;
-
-	if (nsock->fallback_index < 0 ||
-	    nsock->fallback_index >= config->num_connections ||
-	    config->socks[nsock->fallback_index]->dead) {
+	} else {
 		int i;
 		for (i = 0; i < config->num_connections; i++) {
 			if (i == index)
@@ -799,14 +802,10 @@ static int find_fallback(struct nbd_device *nbd, int index)
 				break;
 			}
 		}
-		nsock->fallback_index = new_index;
-		if (new_index < 0) {
-			dev_err_ratelimited(disk_to_dev(nbd->disk),
-					    "Dead connection, failed to find a fallback\n");
-			return new_index;
-		}
 	}
-	new_index = nsock->fallback_index;
+
+out:
+	nsock->fallback_index = new_index;
 	return new_index;
 }
 
-- 
1.8.3.1


Reply to: