Linux block layer
 help / color / mirror / Atom feed
From: Yang Erkun <yangerkun@huawei.com>
To: josef@toxicpanda.com, axboe@kernel.dk, hch@lst.de, yukuai@kernel.org
Cc: yi.zhang@huawei.com, chengzhihao1@huawei.com,
	echo.chenlin@huawei.com, leo.lilong@huaweicloud.com,
	wangkefeng.wang@huawei.com, huawei.libin@huawei.com,
	leijitang@huawei.com, linux-block@vger.kernel.org,
	nbd@other.debian.org
Subject: [PATCH v4 1/7] nbd: simplify find_fallback() by removing redundant logic
Date: Mon, 27 Jul 2026 15:47:12 +0800	[thread overview]
Message-ID: <20260727074718.414868-2-yangerkun@huawei.com> (raw)
In-Reply-To: <20260727074718.414868-1-yangerkun@huawei.com>

From: Long Li <leo.lilong@huawei.com>

Remove the intermediate new_index variable and return -1 directly.
The second conditional checking nsock->fallback_index validity is the
logical inverse of the first, so drop it and let execution fall through
naturally. Consolidate the two identical dev_err_ratelimited() + return
paths into a single no_fallback label to reduce duplication.

Signed-off-by: Long Li <leo.lilong@huawei.com>
Reviewed-by: Yu Kuai <yukuai@fygo.io>
---
 drivers/block/nbd.c | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 8f10762e90ef..b1a5acd57426 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1061,40 +1061,31 @@ static int find_fallback(struct nbd_device *nbd, int index)
 	int new_index = -1;
 	struct nbd_sock *nsock = config->socks[index];
 	int fallback = nsock->fallback_index;
+	int i;
 
 	if (test_bit(NBD_RT_DISCONNECTED, &config->runtime_flags))
 		return new_index;
 
-	if (config->num_connections <= 1) {
-		dev_err_ratelimited(disk_to_dev(nbd->disk),
-				    "Dead connection, failed to find a fallback\n");
-		return new_index;
-	}
+	if (config->num_connections <= 1)
+		goto no_fallback;
 
 	if (fallback >= 0 && fallback < config->num_connections &&
 	    !config->socks[fallback]->dead)
 		return fallback;
 
-	if (nsock->fallback_index < 0 ||
-	    nsock->fallback_index >= config->num_connections ||
-	    config->socks[nsock->fallback_index]->dead) {
-		int i;
-		for (i = 0; i < config->num_connections; i++) {
-			if (i == index)
-				continue;
-			if (!config->socks[i]->dead) {
-				new_index = i;
-				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;
+	for (i = 0; i < config->num_connections; i++) {
+		if (i != index && !config->socks[i]->dead) {
+			new_index = i;
+			break;
 		}
 	}
-	new_index = nsock->fallback_index;
+	nsock->fallback_index = new_index;
+	if (new_index >= 0)
+		return new_index;
+
+no_fallback:
+	dev_err_ratelimited(disk_to_dev(nbd->disk),
+			    "Dead connection, failed to find a fallback\n");
 	return new_index;
 }
 
-- 
2.52.0


  reply	other threads:[~2026-07-27  7:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-27  7:47 [PATCH v4 0/7] nbd: eliminate queue freeze/unfreeze overhead in connection setup Yang Erkun
2026-07-27  7:47 ` Yang Erkun [this message]
2026-07-27  7:47 ` [PATCH v4 2/7] nbd: disallow NBD_SET_SOCK on an active device Yang Erkun
2026-07-27  7:47 ` [PATCH v4 3/7] nbd: remove queue freeze in nbd_add_socket Yang Erkun
2026-07-27  7:47 ` [PATCH v4 4/7] nbd: skip queue freeze when setting size at device startup Yang Erkun
2026-07-27  7:47 ` [PATCH v4 5/7] nbd: factor out a nbd_genl_foreach_sock Yang Erkun
2026-07-27  7:47 ` [PATCH v4 6/7] nbd: remove queue freeze for newly created nbd from netlink path Yang Erkun
2026-07-27  7:47 ` [PATCH v4 7/7] nbd: add nr_hw_queues module parameter for pre-created devices Yang Erkun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260727074718.414868-2-yangerkun@huawei.com \
    --to=yangerkun@huawei.com \
    --cc=axboe@kernel.dk \
    --cc=chengzhihao1@huawei.com \
    --cc=echo.chenlin@huawei.com \
    --cc=hch@lst.de \
    --cc=huawei.libin@huawei.com \
    --cc=josef@toxicpanda.com \
    --cc=leijitang@huawei.com \
    --cc=leo.lilong@huaweicloud.com \
    --cc=linux-block@vger.kernel.org \
    --cc=nbd@other.debian.org \
    --cc=wangkefeng.wang@huawei.com \
    --cc=yi.zhang@huawei.com \
    --cc=yukuai@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox