Commit 075790eb authored by Keith Busch's avatar Keith Busch Committed by Jens Axboe

NVMe: Use IDA for namespace disk naming

A namespace may be detached from a controller, but a user may be holding
a reference to it. Attaching a new namespace with the same NSID will create
duplicate names when using the NSID to name the disk.

This patch uses an IDA that is released only when the last reference is
released instead of using the namespace ID.
Signed-off-by: default avatarKeith Busch <keith.busch@intel.com>
Reviewed-by: default avatarSagi Grimberg <sagig@mellanox.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarJohannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: default avatarJens Axboe <axboe@fb.com>
parent b00a726a
...@@ -55,8 +55,9 @@ static void nvme_free_ns(struct kref *kref) ...@@ -55,8 +55,9 @@ static void nvme_free_ns(struct kref *kref)
ns->disk->private_data = NULL; ns->disk->private_data = NULL;
spin_unlock(&dev_list_lock); spin_unlock(&dev_list_lock);
nvme_put_ctrl(ns->ctrl);
put_disk(ns->disk); put_disk(ns->disk);
ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
nvme_put_ctrl(ns->ctrl);
kfree(ns); kfree(ns);
} }
...@@ -1118,9 +1119,13 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) ...@@ -1118,9 +1119,13 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
if (!ns) if (!ns)
return; return;
ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
if (ns->instance < 0)
goto out_free_ns;
ns->queue = blk_mq_init_queue(ctrl->tagset); ns->queue = blk_mq_init_queue(ctrl->tagset);
if (IS_ERR(ns->queue)) if (IS_ERR(ns->queue))
goto out_free_ns; goto out_release_instance;
queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue); queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
ns->queue->queuedata = ns; ns->queue->queuedata = ns;
ns->ctrl = ctrl; ns->ctrl = ctrl;
...@@ -1153,7 +1158,7 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) ...@@ -1153,7 +1158,7 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
disk->queue = ns->queue; disk->queue = ns->queue;
disk->driverfs_dev = ctrl->device; disk->driverfs_dev = ctrl->device;
disk->flags = GENHD_FL_EXT_DEVT; disk->flags = GENHD_FL_EXT_DEVT;
sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, nsid); sprintf(disk->disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
if (nvme_revalidate_disk(ns->disk)) if (nvme_revalidate_disk(ns->disk))
goto out_free_disk; goto out_free_disk;
...@@ -1173,6 +1178,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid) ...@@ -1173,6 +1178,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
kfree(disk); kfree(disk);
out_free_queue: out_free_queue:
blk_cleanup_queue(ns->queue); blk_cleanup_queue(ns->queue);
out_release_instance:
ida_simple_remove(&ctrl->ns_ida, ns->instance);
out_free_ns: out_free_ns:
kfree(ns); kfree(ns);
} }
...@@ -1350,6 +1357,7 @@ static void nvme_free_ctrl(struct kref *kref) ...@@ -1350,6 +1357,7 @@ static void nvme_free_ctrl(struct kref *kref)
put_device(ctrl->device); put_device(ctrl->device);
nvme_release_instance(ctrl); nvme_release_instance(ctrl);
ida_destroy(&ctrl->ns_ida);
ctrl->ops->free_ctrl(ctrl); ctrl->ops->free_ctrl(ctrl);
} }
...@@ -1390,6 +1398,7 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev, ...@@ -1390,6 +1398,7 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
} }
get_device(ctrl->device); get_device(ctrl->device);
dev_set_drvdata(ctrl->device, ctrl); dev_set_drvdata(ctrl->device, ctrl);
ida_init(&ctrl->ns_ida);
spin_lock(&dev_list_lock); spin_lock(&dev_list_lock);
list_add_tail(&ctrl->node, &nvme_ctrl_list); list_add_tail(&ctrl->node, &nvme_ctrl_list);
......
...@@ -72,6 +72,7 @@ struct nvme_ctrl { ...@@ -72,6 +72,7 @@ struct nvme_ctrl {
struct mutex namespaces_mutex; struct mutex namespaces_mutex;
struct device *device; /* char device */ struct device *device; /* char device */
struct list_head node; struct list_head node;
struct ida ns_ida;
char name[12]; char name[12];
char serial[20]; char serial[20];
...@@ -102,6 +103,7 @@ struct nvme_ns { ...@@ -102,6 +103,7 @@ struct nvme_ns {
struct request_queue *queue; struct request_queue *queue;
struct gendisk *disk; struct gendisk *disk;
struct kref kref; struct kref kref;
int instance;
u8 eui[8]; u8 eui[8];
u8 uuid[16]; u8 uuid[16];
......
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