Commit b3a0a73e authored by Damien Le Moal's avatar Damien Le Moal Committed by Jens Axboe

block: null_blk: Cleanup device creation and deletion

Introduce the null_create_dev() and null_destroy_dev() helper functions
to respectivel create nullb devices on modprobe and destroy them on
rmmod. The null_destroy_dev() helper avoids duplicated code in the
null_init() and null_exit() functions for deleting devices.
Signed-off-by: default avatarDamien Le Moal <damien.lemoal@opensource.wdc.com>
Reviewed-by: default avatarChaitanya Kulkarni <kch@nvidia.com>
Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/20220420005718.3780004-3-damien.lemoal@opensource.wdc.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 525323d2
...@@ -2086,12 +2086,37 @@ static int null_add_dev(struct nullb_device *dev) ...@@ -2086,12 +2086,37 @@ static int null_add_dev(struct nullb_device *dev)
return rv; return rv;
} }
static int null_create_dev(void)
{
struct nullb_device *dev;
int ret;
dev = null_alloc_dev();
if (!dev)
return -ENOMEM;
ret = null_add_dev(dev);
if (ret) {
null_free_dev(dev);
return ret;
}
return 0;
}
static void null_destroy_dev(struct nullb *nullb)
{
struct nullb_device *dev = nullb->dev;
null_del_dev(nullb);
null_free_dev(dev);
}
static int __init null_init(void) static int __init null_init(void)
{ {
int ret = 0; int ret = 0;
unsigned int i; unsigned int i;
struct nullb *nullb; struct nullb *nullb;
struct nullb_device *dev;
if (g_bs > PAGE_SIZE) { if (g_bs > PAGE_SIZE) {
pr_warn("invalid block size\n"); pr_warn("invalid block size\n");
...@@ -2149,17 +2174,10 @@ static int __init null_init(void) ...@@ -2149,17 +2174,10 @@ static int __init null_init(void)
} }
for (i = 0; i < nr_devices; i++) { for (i = 0; i < nr_devices; i++) {
dev = null_alloc_dev(); ret = null_create_dev();
if (!dev) { if (ret)
ret = -ENOMEM;
goto err_dev;
}
ret = null_add_dev(dev);
if (ret) {
null_free_dev(dev);
goto err_dev; goto err_dev;
} }
}
pr_info("module loaded\n"); pr_info("module loaded\n");
return 0; return 0;
...@@ -2167,9 +2185,7 @@ static int __init null_init(void) ...@@ -2167,9 +2185,7 @@ static int __init null_init(void)
err_dev: err_dev:
while (!list_empty(&nullb_list)) { while (!list_empty(&nullb_list)) {
nullb = list_entry(nullb_list.next, struct nullb, list); nullb = list_entry(nullb_list.next, struct nullb, list);
dev = nullb->dev; null_destroy_dev(nullb);
null_del_dev(nullb);
null_free_dev(dev);
} }
unregister_blkdev(null_major, "nullb"); unregister_blkdev(null_major, "nullb");
err_conf: err_conf:
...@@ -2190,12 +2206,8 @@ static void __exit null_exit(void) ...@@ -2190,12 +2206,8 @@ static void __exit null_exit(void)
mutex_lock(&lock); mutex_lock(&lock);
while (!list_empty(&nullb_list)) { while (!list_empty(&nullb_list)) {
struct nullb_device *dev;
nullb = list_entry(nullb_list.next, struct nullb, list); nullb = list_entry(nullb_list.next, struct nullb, list);
dev = nullb->dev; null_destroy_dev(nullb);
null_del_dev(nullb);
null_free_dev(dev);
} }
mutex_unlock(&lock); mutex_unlock(&lock);
......
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