summaryrefslogtreecommitdiff
path: root/drivers/block
diff options
context:
space:
mode:
authorChristoph Hellwig <hch@lst.de>2021-08-11 14:44:27 +0200
committerJens Axboe <axboe@kernel.dk>2021-08-13 14:17:39 -0600
commit6177b56c96ff3b5e23d47f6b6c8630f31145da93 (patch)
tree37aa161a899ba695984ca13a3fc8d4ec0cc8382e /drivers/block
parent7bdc00cf7e369b3be17f26e5643da28de98d9d6d (diff)
nbd: refactor device search and allocation in nbd_genl_connect
Use idr_for_each_entry instead of the awkward callback to find an existing device for the index == -1 case, and de-duplicate the device allocation if no existing device was found. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20210811124428.2368491-6-hch@lst.de Reviewed-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'drivers/block')
-rw-r--r--drivers/block/nbd.c45
1 files changed, 14 insertions, 31 deletions
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index a81b95c66dbf..8c0e334bdfbf 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1767,18 +1767,6 @@ out:
return ERR_PTR(err);
}
-static int find_free_cb(int id, void *ptr, void *data)
-{
- struct nbd_device *nbd = ptr;
- struct nbd_device **found = data;
-
- if (!refcount_read(&nbd->config_refs)) {
- *found = nbd;
- return 1;
- }
- return 0;
-}
-
/* Netlink interface. */
static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
[NBD_ATTR_INDEX] = { .type = NLA_U32 },
@@ -1848,31 +1836,26 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
again:
mutex_lock(&nbd_index_mutex);
if (index == -1) {
- ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
- if (ret == 0) {
- nbd = nbd_dev_add(-1);
- if (IS_ERR(nbd)) {
- mutex_unlock(&nbd_index_mutex);
- printk(KERN_ERR "nbd: failed to add new device\n");
- return PTR_ERR(nbd);
+ struct nbd_device *tmp;
+ int id;
+
+ idr_for_each_entry(&nbd_index_idr, tmp, id) {
+ if (!refcount_read(&tmp->config_refs)) {
+ nbd = tmp;
+ break;
}
}
} else {
nbd = idr_find(&nbd_index_idr, index);
- if (!nbd) {
- nbd = nbd_dev_add(index);
- if (IS_ERR(nbd)) {
- mutex_unlock(&nbd_index_mutex);
- printk(KERN_ERR "nbd: failed to add new device\n");
- return PTR_ERR(nbd);
- }
- }
}
+
if (!nbd) {
- printk(KERN_ERR "nbd: couldn't find device at index %d\n",
- index);
- mutex_unlock(&nbd_index_mutex);
- return -EINVAL;
+ nbd = nbd_dev_add(index);
+ if (IS_ERR(nbd)) {
+ mutex_unlock(&nbd_index_mutex);
+ pr_err("nbd: failed to add new device\n");
+ return PTR_ERR(nbd);
+ }
}
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&