Skip to contentSkip to conversation

nvmet: per-host namespaces masking

PATCH In review Hannes Reinecke 3 participants 5 messages Feb 5, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet: per-host namespaces masking

Implement per-host namespaces masking, allowing to expose different namespaces to different hosts connecting to the same subsystem.
The original method of adding host nqns to the subsystem takes precedence over the per-host namespace masking.

---
 drivers/nvme/target/admin-cmd.c | 16 +++++++
 drivers/nvme/target/configfs.c  | 82 +++++++++++++++++++++++++++++++++
 drivers/nvme/target/core.c      | 21 ++++++++-
 drivers/nvme/target/nvmet.h     |  4 ++
 4 files changed, 122 insertions(+), 1 deletion(-)
drivers/nvme/target/admin-cmd.c +16 -0
index 613a4d8feac1..d63fd33200db 100644
@@ -489,6 +489,13 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)
 		goto done;
 	}
 
+	if (!nvmet_ns_host_allowed(req->ns, ctrl->hostnqn)) {
+		status = NVME_SC_INVALID_NS;
+		nvmet_put_namespace(req->ns);
+		req->ns = NULL;
+		goto out;
+	}
+
 	nvmet_ns_revalidate(req->ns);
 
 	/*
@@ -566,6 +573,8 @@ static void nvmet_execute_identify_nslist(struct nvmet_req *req)
 	}
 
 	xa_for_each(&ctrl->subsys->namespaces, idx, ns) {
+		if (!nvmet_ns_host_allowed(ns, ctrl->hostnqn))
+			continue;
 		if (ns->nsid <= min_nsid)
 			continue;
 		list[i++] = cpu_to_le32(ns->nsid);
@@ -613,6 +622,13 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)
 		status = NVME_SC_INVALID_NS | NVME_SC_DNR;
 		goto out;
 	}
+	if (!nvmet_ns_host_allowed(req->ns, req->sq->ctrl->hostnqn)) {
+		nvmet_put_namespace(req->ns);
+		req->ns = NULL;
+		req->error_loc = offsetof(struct nvme_identify, nsid);
+		status = NVME_SC_INVALID_NS | NVME_SC_DNR;
+		goto out;
+	}
 
 	if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) {
 		status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
drivers/nvme/target/configfs.c +82 -0

94 lines not on this page · raw message

drivers/nvme/target/core.c +20 -1
index 8ce4d59cc9e7..d62a49f8a6e0 100644
@@ -673,6 +673,7 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
 
 	ns->nsid = nsid;
 	ns->subsys = subsys;
+	INIT_LIST_HEAD(&ns->hosts);
 
 	down_write(&nvmet_ana_sem);
 	ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
@@ -1225,9 +1226,24 @@ u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
 	return 0;
 }
 
+bool nvmet_ns_host_allowed(struct nvmet_ns *ns, const char *hostnqn)
+{
+	struct nvmet_host_link *p;
+
+	lockdep_assert_held(&nvmet_config_sem);
+
+	list_for_each_entry(p, &ns->hosts, entry) {
+		if (!strcmp(nvmet_host_name(p->host), hostnqn))
+			return true;
+	}
+	return false;
+}
+
 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
 {
 	struct nvmet_host_link *p;
+	struct nvmet_ns *ns;
+	unsigned long idx;
 
 	lockdep_assert_held(&nvmet_config_sem);
 
@@ -1241,7 +1257,10 @@ bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
 		if (!strcmp(nvmet_host_name(p->host), hostnqn))
 			return true;
 	}
-
+	xa_for_each(&subsys->namespaces, idx, ns) {
+		if (nvmet_ns_host_allowed(ns, hostnqn))
+			return true;
+	}
 	return false;
 }
 
drivers/nvme/target/nvmet.h +4 -0
index 8776dd1a0490..7d2e5814cfa3 100644
@@ -70,6 +70,9 @@ struct nvmet_ns {
 	struct nvmet_subsys	*subsys;
 	const char		*device_path;
 
+	struct list_head	hosts;
+
+	struct config_group	allowed_hosts_group;
 	struct config_group	device_group;
 	struct config_group	group;
 
@@ -518,6 +521,7 @@ extern u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
 extern u64 nvmet_ana_chgcnt;
 extern struct rw_semaphore nvmet_ana_sem;
 
+bool nvmet_ns_host_allowed(struct nvmet_ns *ns, const char *hostnqn);
 bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn);
 
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#1 lore raw
Quoted 5 lines · Hannes Reinecke

Implement per-host namespaces masking, allowing to expose different namespaces to different hosts connecting to the same subsystem.
The original method of adding host nqns to the subsystem takes precedence over the per-host namespace masking.

Why do you want to expose a single subsystem to multiple hosts? This creates all kinds of covert channels better avoided.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#2 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Hannes Reinecke

Implement per-host namespaces masking, allowing to expose different namespaces to different hosts connecting to the same subsystem.
The original method of adding host nqns to the subsystem takes precedence over the per-host namespace masking.

Why do you want to expose a single subsystem to multiple hosts? This creates all kinds of covert channels better avoided.

But the alternative is a subsystem per host.
Which leads to a proliferation of subsystems on the target. And I wasn't quite keen on that.

Cheers,

Hannes

Signature · 9 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#3 lore raw
Quoted 8 lines · Hannes Reinecke
Quoted 7 lines · Christoph Hellwig
Quoted 5 lines · Hannes Reinecke

Implement per-host namespaces masking, allowing to expose different namespaces to different hosts connecting to the same subsystem.
The original method of adding host nqns to the subsystem takes precedence over the per-host namespace masking.

Quoted 5 lines
Quoted 3 lines

Why do you want to expose a single subsystem to multiple hosts? This creates all kinds of covert channels better avoided.

But the alternative is a subsystem per host.
Which leads to a proliferation of subsystems on the target.

Yes, that was the choice we had in mind when writing the code. The subsystems are very cheap as they don't require any resources except for some fairly small memory allocations.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer
#4 lore raw
In reply to Hannes Reinecke
Quoted 6 lines

Implement per-host namespaces masking, allowing to expose different namespaces to different hosts connecting to the same subsystem.
The original method of adding host nqns to the subsystem takes precedence over the per-host namespace masking.

What is preventing a host to go ahead and issue an I/O command to a ns that exists in the subsystem but is not exposed to it in the nslist?

I don't think this can be even considered without a protection against it. And given that you don't want to take the global semaphore on each and every I/O, I'd say its a bit more involving than what you have here...

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme