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 v5 6/8] nbd: factor out a nbd_genl_foreach_sock
Date: Thu, 30 Jul 2026 16:20:44 +0800 [thread overview]
Message-ID: <20260730082046.3459239-7-yangerkun@huawei.com> (raw)
In-Reply-To: <20260730082046.3459239-1-yangerkun@huawei.com>
The NBD_ATTR_SOCKETS walk is duplicated in nbd_genl_connect (add sockets)
and nbd_genl_reconfigure (reconnect). Factor out a single helper that
walks the list and calls a callback per fd; with a NULL callback it is a
pure counter, used by a later patch to learn nr_hw_queues before the
device exists. Returns the number of fds walked (>= 0) or a negative
errno; a callback >0 stops early as success (reconnect's -ENOSPC).
Signed-off-by: Yang Erkun <yangerkun@huawei.com>
---
drivers/block/nbd.c | 137 +++++++++++++++++++++++---------------------
1 file changed, 73 insertions(+), 64 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 3b7363b11d0b..34b84fc1c61f 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -2108,6 +2108,58 @@ static int nbd_genl_size_set(struct genl_info *info, struct nbd_device *nbd)
return 0;
}
+/*
+ * Walk the NBD_ATTR_SOCKETS nested list can call @cb for each socket fd.
+ *
+ * Return the number of fds walked, or a negative errno.
+ */
+static int nbd_genl_foreach_sock(struct genl_info *info,
+ int (*cb)(struct nbd_device *nbd, int fd),
+ struct nbd_device *nbd)
+{
+ struct nlattr *attr;
+ int rem, count = 0;
+
+ if (!info->attrs[NBD_ATTR_SOCKETS])
+ return 0;
+
+ nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS], rem) {
+ struct nlattr *socks[NBD_SOCK_MAX + 1];
+ int ret;
+
+ if (nla_type(attr) != NBD_SOCK_ITEM) {
+ pr_err("socks must be embedded in a SOCK_ITEM attr\n");
+ return -EINVAL;
+ }
+
+ if (nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
+ attr,
+ nbd_sock_policy,
+ info->extack)) {
+ pr_err("error processing sock list\n");
+ return -EINVAL;
+ }
+
+ if (!socks[NBD_SOCK_FD])
+ continue;
+
+ count++;
+ if (cb) {
+ ret = cb(nbd, (int)nla_get_u32(socks[NBD_SOCK_FD]));
+ if (ret > 0)
+ return count;
+ if (ret < 0)
+ return ret;
+ }
+ }
+ return count;
+}
+
+static int nbd_genl_connect_sock_cb(struct nbd_device *nbd, int fd)
+{
+ return nbd_add_socket(nbd, fd, true);
+}
+
static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
{
struct nbd_device *nbd;
@@ -2227,36 +2279,9 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
}
}
- if (info->attrs[NBD_ATTR_SOCKETS]) {
- struct nlattr *attr;
- int rem, fd;
-
- nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
- rem) {
- struct nlattr *socks[NBD_SOCK_MAX+1];
-
- if (nla_type(attr) != NBD_SOCK_ITEM) {
- pr_err("socks must be embedded in a SOCK_ITEM attr\n");
- ret = -EINVAL;
- goto out;
- }
- ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
- attr,
- nbd_sock_policy,
- info->extack);
- if (ret != 0) {
- pr_err("error processing sock list\n");
- ret = -EINVAL;
- goto out;
- }
- if (!socks[NBD_SOCK_FD])
- continue;
- fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
- ret = nbd_add_socket(nbd, fd, true);
- if (ret)
- goto out;
- }
- }
+ ret = nbd_genl_foreach_sock(info, nbd_genl_connect_sock_cb, nbd);
+ if (ret < 0)
+ goto out;
if (info->attrs[NBD_ATTR_BACKEND_IDENTIFIER]) {
nbd->backend = nla_strdup(info->attrs[NBD_ATTR_BACKEND_IDENTIFIER],
@@ -2345,6 +2370,20 @@ static int nbd_genl_disconnect(struct sk_buff *skb, struct genl_info *info)
return 0;
}
+static int nbd_genl_reconnect_sock_cb(struct nbd_device *nbd, int fd)
+{
+ int ret = nbd_reconnect_socket(nbd, fd);
+
+ if (!ret) {
+ dev_info(nbd_to_dev(nbd), "reconnected socket\n");
+ return 0;
+ }
+
+ if (ret == -ENOSPC)
+ return 1;
+ return ret;
+}
+
static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
{
struct nbd_device *nbd = NULL;
@@ -2441,40 +2480,10 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info)
}
}
- if (info->attrs[NBD_ATTR_SOCKETS]) {
- struct nlattr *attr;
- int rem, fd;
-
- nla_for_each_nested(attr, info->attrs[NBD_ATTR_SOCKETS],
- rem) {
- struct nlattr *socks[NBD_SOCK_MAX+1];
-
- if (nla_type(attr) != NBD_SOCK_ITEM) {
- pr_err("socks must be embedded in a SOCK_ITEM attr\n");
- ret = -EINVAL;
- goto out;
- }
- ret = nla_parse_nested_deprecated(socks, NBD_SOCK_MAX,
- attr,
- nbd_sock_policy,
- info->extack);
- if (ret != 0) {
- pr_err("error processing sock list\n");
- ret = -EINVAL;
- goto out;
- }
- if (!socks[NBD_SOCK_FD])
- continue;
- fd = (int)nla_get_u32(socks[NBD_SOCK_FD]);
- ret = nbd_reconnect_socket(nbd, fd);
- if (ret) {
- if (ret == -ENOSPC)
- ret = 0;
- goto out;
- }
- dev_info(nbd_to_dev(nbd), "reconnected socket\n");
- }
- }
+ ret = nbd_genl_foreach_sock(info, nbd_genl_reconnect_sock_cb, nbd);
+ /* foreach_sock returns a positive count on success; doit must return 0 */
+ if (ret >= 0)
+ ret = 0;
out:
mutex_unlock(&nbd->config_lock);
nbd_config_put(nbd);
--
2.52.0
next prev parent reply other threads:[~2026-07-30 8:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-30 8:20 [PATCH v5 0/8] nbd: eliminate queue freeze/unfreeze overhead in connection setup Yang Erkun
2026-07-30 8:20 ` [PATCH v5 1/8] nbd: simplify find_fallback() by removing redundant logic Yang Erkun
2026-07-30 8:20 ` [PATCH v5 2/8] nbd: disallow NBD_SET_SOCK on an active device Yang Erkun
2026-07-30 8:20 ` [PATCH v5 3/8] nbd: reset write cache on disconnect Yang Erkun
2026-07-30 8:20 ` [PATCH v5 4/8] nbd: remove queue freeze in nbd_add_socket Yang Erkun
2026-07-30 8:20 ` [PATCH v5 5/8] nbd: skip queue freeze when setting size at device startup Yang Erkun
2026-07-30 8:20 ` Yang Erkun [this message]
2026-07-30 8:20 ` [PATCH v5 7/8] nbd: remove queue freeze for newly created nbd from netlink path Yang Erkun
2026-07-30 8:20 ` [PATCH v5 8/8] 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=20260730082046.3459239-7-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