Commit 3f74e064 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe

nbd: refactor device removal

Share common code for the synchronous and workqueue based device removal,
and remove the pointless use of refcount_dec_and_mutex_lock.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20210811124428.2368491-3-hch@lst.deReviewed-by: default avatarJosef Bacik <josef@toxicpanda.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 68c9417b
...@@ -259,48 +259,37 @@ static void nbd_notify_destroy_completion(struct nbd_device *nbd) ...@@ -259,48 +259,37 @@ static void nbd_notify_destroy_completion(struct nbd_device *nbd)
complete(nbd->destroy_complete); complete(nbd->destroy_complete);
} }
static void nbd_dev_remove_work(struct work_struct *work) static void nbd_dev_remove(struct nbd_device *nbd)
{ {
struct nbd_device *nbd =
container_of(work, struct nbd_device, remove_work);
nbd_del_disk(nbd); nbd_del_disk(nbd);
mutex_lock(&nbd_index_mutex);
/* /*
* Remove from idr after del_gendisk() completes, * Remove from idr after del_gendisk() completes, so if the same ID is
* so if the same id is reused, the following * reused, the following add_disk() will succeed.
* add_disk() will succeed.
*/ */
mutex_lock(&nbd_index_mutex);
idr_remove(&nbd_index_idr, nbd->index); idr_remove(&nbd_index_idr, nbd->index);
nbd_notify_destroy_completion(nbd); nbd_notify_destroy_completion(nbd);
mutex_unlock(&nbd_index_mutex); mutex_unlock(&nbd_index_mutex);
kfree(nbd); kfree(nbd);
} }
static void nbd_dev_remove(struct nbd_device *nbd) static void nbd_dev_remove_work(struct work_struct *work)
{ {
/* Call del_gendisk() asynchrounously to prevent deadlock */ nbd_dev_remove(container_of(work, struct nbd_device, remove_work));
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags)) {
queue_work(nbd_del_wq, &nbd->remove_work);
return;
}
nbd_del_disk(nbd);
idr_remove(&nbd_index_idr, nbd->index);
nbd_notify_destroy_completion(nbd);
kfree(nbd);
} }
static void nbd_put(struct nbd_device *nbd) static void nbd_put(struct nbd_device *nbd)
{ {
if (refcount_dec_and_mutex_lock(&nbd->refs, if (!refcount_dec_and_test(&nbd->refs))
&nbd_index_mutex)) { return;
/* Call del_gendisk() asynchrounously to prevent deadlock */
if (test_bit(NBD_DESTROY_ON_DISCONNECT, &nbd->flags))
queue_work(nbd_del_wq, &nbd->remove_work);
else
nbd_dev_remove(nbd); nbd_dev_remove(nbd);
mutex_unlock(&nbd_index_mutex);
}
} }
static int nbd_disconnected(struct nbd_config *config) static int nbd_disconnected(struct nbd_config *config)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment