Skip to content

Commit 95409e2

Browse files
Hannes Reineckekeithbusch
authored andcommitted
nvmet: implement unique discovery NQN
Unique discovery NQNs allow to differentiate between discovery services from (typically physically separate) NVMe-oF subsystems. This is required for establishing secured connections as otherwise the credentials won't be unique and the integrity of the connection cannot be guaranteed. This patch adds a configfs attribute 'discovery_nqn' in the 'nvmet' configfs directory to specify the unique discovery NQN. Signed-off-by: Hannes Reinecke <hare@kernel.org> Reviewed-by: Sagi Grimberg <sagi@grimberg.me> Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 0551ec9 commit 95409e2

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

drivers/nvme/target/configfs.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,11 @@ static struct config_group *nvmet_subsys_make(struct config_group *group,
16131613
return ERR_PTR(-EINVAL);
16141614
}
16151615

1616+
if (sysfs_streq(name, nvmet_disc_subsys->subsysnqn)) {
1617+
pr_err("can't create subsystem using unique discovery NQN\n");
1618+
return ERR_PTR(-EINVAL);
1619+
}
1620+
16161621
subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
16171622
if (IS_ERR(subsys))
16181623
return ERR_CAST(subsys);
@@ -2159,7 +2164,49 @@ static const struct config_item_type nvmet_hosts_type = {
21592164

21602165
static struct config_group nvmet_hosts_group;
21612166

2167+
static ssize_t nvmet_root_discovery_nqn_show(struct config_item *item,
2168+
char *page)
2169+
{
2170+
return snprintf(page, PAGE_SIZE, "%s\n", nvmet_disc_subsys->subsysnqn);
2171+
}
2172+
2173+
static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
2174+
const char *page, size_t count)
2175+
{
2176+
struct list_head *entry;
2177+
size_t len;
2178+
2179+
len = strcspn(page, "\n");
2180+
if (!len || len > NVMF_NQN_FIELD_LEN - 1)
2181+
return -EINVAL;
2182+
2183+
down_write(&nvmet_config_sem);
2184+
list_for_each(entry, &nvmet_subsystems_group.cg_children) {
2185+
struct config_item *item =
2186+
container_of(entry, struct config_item, ci_entry);
2187+
2188+
if (!strncmp(config_item_name(item), page, len)) {
2189+
pr_err("duplicate NQN %s\n", config_item_name(item));
2190+
up_write(&nvmet_config_sem);
2191+
return -EINVAL;
2192+
}
2193+
}
2194+
memset(nvmet_disc_subsys->subsysnqn, 0, NVMF_NQN_FIELD_LEN);
2195+
memcpy(nvmet_disc_subsys->subsysnqn, page, len);
2196+
up_write(&nvmet_config_sem);
2197+
2198+
return len;
2199+
}
2200+
2201+
CONFIGFS_ATTR(nvmet_root_, discovery_nqn);
2202+
2203+
static struct configfs_attribute *nvmet_root_attrs[] = {
2204+
&nvmet_root_attr_discovery_nqn,
2205+
NULL,
2206+
};
2207+
21622208
static const struct config_item_type nvmet_root_type = {
2209+
.ct_attrs = nvmet_root_attrs,
21632210
.ct_owner = THIS_MODULE,
21642211
};
21652212

drivers/nvme/target/core.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,13 @@ static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
15411541
}
15421542

15431543
down_read(&nvmet_config_sem);
1544+
if (!strncmp(nvmet_disc_subsys->subsysnqn, subsysnqn,
1545+
NVMF_NQN_SIZE)) {
1546+
if (kref_get_unless_zero(&nvmet_disc_subsys->ref)) {
1547+
up_read(&nvmet_config_sem);
1548+
return nvmet_disc_subsys;
1549+
}
1550+
}
15441551
list_for_each_entry(p, &port->subsystems, entry) {
15451552
if (!strncmp(p->subsys->subsysnqn, subsysnqn,
15461553
NVMF_NQN_SIZE)) {

0 commit comments

Comments
 (0)