summaryrefslogtreecommitdiffstats
path: root/drivers/block
diff options
context:
space:
mode:
authorChristoph Hellwig <hch@lst.de>2021-08-11 14:44:24 +0200
committerJens Axboe <axboe@kernel.dk>2021-08-13 14:17:32 -0600
commit3f74e0645c52a08f640380c9c46f9a3a172b9389 (patch)
treef1dfdd32e0f83a749aa7bfe2d0a90c323ffa2af0 /drivers/block
parent68c9417b193d0d174b0ada013602272177e61303 (diff)
downloadlinux-3f74e0645c52a08f640380c9c46f9a3a172b9389.tar.bz2
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: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20210811124428.2368491-3-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.c37
1 files changed, 13 insertions, 24 deletions
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index deefb2cda9bb..a9883fbed924 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -259,48 +259,37 @@ static void nbd_notify_destroy_completion(struct nbd_device *nbd)
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);
- mutex_lock(&nbd_index_mutex);
/*
- * Remove from idr after del_gendisk() completes,
- * so if the same id is reused, the following
- * add_disk() will succeed.
+ * Remove from idr after del_gendisk() completes, so if the same ID is
+ * reused, the following add_disk() will succeed.
*/
+ mutex_lock(&nbd_index_mutex);
idr_remove(&nbd_index_idr, nbd->index);
-
nbd_notify_destroy_completion(nbd);
mutex_unlock(&nbd_index_mutex);
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 */
- 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);
+ nbd_dev_remove(container_of(work, struct nbd_device, remove_work));
}
static void nbd_put(struct nbd_device *nbd)
{
- if (refcount_dec_and_mutex_lock(&nbd->refs,
- &nbd_index_mutex)) {
+ if (!refcount_dec_and_test(&nbd->refs))
+ 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);
- mutex_unlock(&nbd_index_mutex);
- }
}
static int nbd_disconnected(struct nbd_config *config)