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>
This commit is contained in:
parent
7bdc00cf7e
commit
6177b56c96
1 changed files with 14 additions and 31 deletions
|
@ -1767,18 +1767,6 @@ out:
|
||||||
return ERR_PTR(err);
|
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. */
|
/* Netlink interface. */
|
||||||
static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
|
static const struct nla_policy nbd_attr_policy[NBD_ATTR_MAX + 1] = {
|
||||||
[NBD_ATTR_INDEX] = { .type = NLA_U32 },
|
[NBD_ATTR_INDEX] = { .type = NLA_U32 },
|
||||||
|
@ -1848,31 +1836,26 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
|
||||||
again:
|
again:
|
||||||
mutex_lock(&nbd_index_mutex);
|
mutex_lock(&nbd_index_mutex);
|
||||||
if (index == -1) {
|
if (index == -1) {
|
||||||
ret = idr_for_each(&nbd_index_idr, &find_free_cb, &nbd);
|
struct nbd_device *tmp;
|
||||||
if (ret == 0) {
|
int id;
|
||||||
nbd = nbd_dev_add(-1);
|
|
||||||
if (IS_ERR(nbd)) {
|
idr_for_each_entry(&nbd_index_idr, tmp, id) {
|
||||||
mutex_unlock(&nbd_index_mutex);
|
if (!refcount_read(&tmp->config_refs)) {
|
||||||
printk(KERN_ERR "nbd: failed to add new device\n");
|
nbd = tmp;
|
||||||
return PTR_ERR(nbd);
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
nbd = idr_find(&nbd_index_idr, index);
|
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) {
|
if (!nbd) {
|
||||||
printk(KERN_ERR "nbd: couldn't find device at index %d\n",
|
nbd = nbd_dev_add(index);
|
||||||
index);
|
if (IS_ERR(nbd)) {
|
||||||
mutex_unlock(&nbd_index_mutex);
|
mutex_unlock(&nbd_index_mutex);
|
||||||
return -EINVAL;
|
pr_err("nbd: failed to add new device\n");
|
||||||
|
return PTR_ERR(nbd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
|
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags) &&
|
||||||
|
|
Loading…
Add table
Reference in a new issue