Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

block: null_blk: Improve device creation with configfs

Currently, the directory name used to create a nullb device through
sysfs is not used as the device name, potentially causing headaches for
users if devices are already created through the modprobe operation
withe the nr_device module parameter not set to 0. E.g. a user can do
"mkdir /sys/kernel/config/nullb/nullb0" to create a nullb device even
though /dev/nullb0 was already created by modprobe. In this case, the
configfs nullb device will be named nullb1, causing confusion for the
user.

Simplify this by using the configfs directory name as the nullb device
name, always, unless another nullb device is already using the same
name. E.g. if modprobe created nullb0, then:

$ mkdir /sys/kernel/config/nullb/nullb0
mkdir: cannot create directory '/sys/kernel/config/nullb/nullb0': File
exists

will be reported to the user.

To implement this, the function null_find_dev_by_name() is added to
check for the existence of a nullb device with the name used for a new
configfs device directory. nullb_group_make_item() uses this new
function to check if the directory name can be used as the disk name.
Finally, null_add_dev() is modified to use the device config item name
as the disk name for a new nullb device created using configfs.
The naming of devices created though modprobe remains unchanged.

Of note is that it is possible for a user to create through configfs a
nullb device with the same name as an existing device. E.g.

$ mkdir /sys/kernel/config/nullb/null

will successfully create the nullb device named "null" but this block
device will however not appear under /dev/ since /dev/null already
exists.

Suggested-by: Joseph Bacik <josef@toxicpanda.com>
Signed-off-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/20220420005718.3780004-5-damien.lemoal@opensource.wdc.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Damien Le Moal and committed by
Jens Axboe
49c3b926 db060f54

+27 -1
+27 -1
drivers/block/null_blk/main.c
··· 235 235 static void null_free_dev(struct nullb_device *dev); 236 236 static void null_del_dev(struct nullb *nullb); 237 237 static int null_add_dev(struct nullb_device *dev); 238 + static struct nullb *null_find_dev_by_name(const char *name); 238 239 static void null_free_device_storage(struct nullb_device *dev, bool is_cache); 239 240 240 241 static inline struct nullb_device *to_nullb_device(struct config_item *item) ··· 563 562 config_item *nullb_group_make_item(struct config_group *group, const char *name) 564 563 { 565 564 struct nullb_device *dev; 565 + 566 + if (null_find_dev_by_name(name)) 567 + return ERR_PTR(-EEXIST); 566 568 567 569 dev = null_alloc_dev(); 568 570 if (!dev) ··· 2066 2062 2067 2063 null_config_discard(nullb); 2068 2064 2069 - sprintf(nullb->disk_name, "nullb%d", nullb->index); 2065 + if (config_item_name(&dev->item)) { 2066 + /* Use configfs dir name as the device name */ 2067 + snprintf(nullb->disk_name, sizeof(nullb->disk_name), 2068 + "%s", config_item_name(&dev->item)); 2069 + } else { 2070 + sprintf(nullb->disk_name, "nullb%d", nullb->index); 2071 + } 2070 2072 2071 2073 rv = null_gendisk_register(nullb); 2072 2074 if (rv) ··· 2099 2089 dev->nullb = NULL; 2100 2090 out: 2101 2091 return rv; 2092 + } 2093 + 2094 + static struct nullb *null_find_dev_by_name(const char *name) 2095 + { 2096 + struct nullb *nullb = NULL, *nb; 2097 + 2098 + mutex_lock(&lock); 2099 + list_for_each_entry(nb, &nullb_list, list) { 2100 + if (strcmp(nb->disk_name, name) == 0) { 2101 + nullb = nb; 2102 + break; 2103 + } 2104 + } 2105 + mutex_unlock(&lock); 2106 + 2107 + return nullb; 2102 2108 } 2103 2109 2104 2110 static int null_create_dev(void)