Skip to contentSkip to conversation

nvmet: Add support for reservations.

PATCH New Omri Mann 2 participants 4 messages Sep 4, 2017 – Sep 7, 2017

Subsystems (2)
lore
Share & tools
lore

Omri Mann

author
#0 lore raw

PATCH 1/1 nvmet: Add support for reservations.

Signed-off-by Omri Mann <omri at excelero.com>
---
 drivers/nvme/target/admin-cmd.c   |  48 +++++
 drivers/nvme/target/core.c        |  24 +++
 drivers/nvme/target/fabrics-cmd.c |   2 +-
 drivers/nvme/target/io-cmd.c      | 414 ++++++++++++++++++++++++++++++++++++++
 drivers/nvme/target/nvmet.h       |  29 +++
 include/linux/nvme.h              |  30 ++-
 6 files changed, 545 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +48 -0
index 3e1a255..fb88dd5 100644
@@ -107,6 +107,35 @@ static u16 nvmet_get_smart_log(struct nvmet_req *req,
 	return status;
 }
 
+static u16 nvmet_get_rsrv_log(struct nvmet_req *req,
+		struct nvme_reservation_log *rlog)
+{
+	u16 status = NVME_SC_SUCCESS;
+	int remaining;
+	struct nvmet_ctrl *ctrl = req->sq->ctrl;
+	struct nvmet_rsrv_log *p;
+
+	mutex_lock(&ctrl->lock);
+	ctrl->rsrv_log_async_sent = false;
+	if (!list_empty(&ctrl->rsrv_log)) {
+		remaining = 0;
+		list_for_each_entry(p, &ctrl->rsrv_log, link) {
+			if (++remaining == 256)
+				break;
+		}
+		rlog->num_avail = remaining - 1;
+		p = list_first_entry(&ctrl->rsrv_log,
+					struct nvmet_rsrv_log, link);
+		++ctrl->rsrv_log_counter;
+		rlog->count = cpu_to_le64(ctrl->rsrv_log_counter);
+		rlog->type = p->log_type;
+		rlog->nsid = cpu_to_le32(p->nsid);
+	}
+	mutex_unlock(&ctrl->lock);
+
+	return status;
+}
+
 static void nvmet_execute_get_log_page(struct nvmet_req *req)
 {
 	struct nvme_smart_log *smart_log;
@@ -156,6 +185,13 @@ static void nvmet_execute_get_log_page(struct nvmet_req *req)
 		 * still claim to fully implement this mandatory log page.
 		 */
 		break;
+	case NVME_LOG_RESERVATION:
+		if (data_len != sizeof(struct nvme_reservation_log)) {
+			status = NVME_SC_INTERNAL;
+			goto err;
+		}
+		status = nvmet_get_rsrv_log(req, buf);
+		break;
 	default:
 		BUG();
 	}
@@ -456,6 +492,12 @@ static void nvmet_execute_set_features(struct nvmet_req *req)
 	case NVME_FEAT_HOST_ID:
 		status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
 		break;
+	case NVME_FEAT_RESV_MASK:
+		req->sq->ctrl->rsrv_mask =
+			le32_to_cpu(req->cmd->common.cdw10[1]);
+		break;
+	case NVME_FEAT_RESV_PERSIST:
+		break;
 	default:
 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
 		break;
@@ -511,6 +553,11 @@ static void nvmet_execute_get_features(struct nvmet_req *req)
 			status = nvmet_copy_to_sgl(req, 0,
 			req->sq->ctrl->hostid, sizeof(req->sq->ctrl->hostid));
 		break;
+	case NVME_FEAT_RESV_MASK:
+		nvmet_set_result(req, req->sq->ctrl->rsrv_mask);
+		break;
+	case NVME_FEAT_RESV_PERSIST:
+		break;
 	default:
 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
 		break;
@@ -565,6 +612,7 @@ u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
 		case NVME_LOG_ERROR:
 		case NVME_LOG_SMART:
 		case NVME_LOG_FW_SLOT:
+		case NVME_LOG_RESERVATION:
 			req->execute = nvmet_execute_get_log_page;
 			return 0;
 		}
drivers/nvme/target/core.c +24 -0
index 7c23eaf..3904d7e 100644
@@ -126,6 +126,27 @@ static void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
 	schedule_work(&ctrl->async_event_work);
 }
 
+void add_reservation_log_page(struct nvmet_ctrl *ctrl,
+		struct nvmet_ns *ns, int type)
+{
+	struct nvmet_rsrv_log *p;
+
+	if (ctrl->rsrv_mask & (1 << type))
+		return;
+	p = kzalloc(sizeof(*p), GFP_KERNEL);
+	if (p == NULL)
+		return;
+	p->nsid = ns->nsid;
+	p->log_type = type;
+	mutex_lock(&ctrl->lock);
+	list_add_tail(&p->link, &ctrl->rsrv_log);
+	mutex_unlock(&ctrl->lock);
+	if (!ctrl->rsrv_log_async_sent) {
+		ctrl->rsrv_log_async_sent = true;
+		nvmet_add_async_event(ctrl, 6, 0, NVME_LOG_RESERVATION);
+	}
+}
+
 int nvmet_register_transport(struct nvmet_fabrics_ops *ops)
 {
 	int ret = 0;
@@ -377,6 +398,8 @@ struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
 
 	INIT_LIST_HEAD(&ns->dev_link);
 	init_completion(&ns->disable_done);
+	INIT_LIST_HEAD(&ns->rsrv_list);
+	mutex_init(&ns->rsrv_lock);
 
 	ns->nsid = nsid;
 	ns->subsys = subsys;
@@ -847,6 +870,7 @@ static void nvmet_ctrl_free(struct kref *ref)
 	nvmet_stop_keep_alive_timer(ctrl);
 
 	mutex_lock(&subsys->lock);
+	nvmet_rsrv_remove_ctrl(ctrl);
 	list_del(&ctrl->subsys_entry);
 	mutex_unlock(&subsys->lock);
 
drivers/nvme/target/fabrics-cmd.c +1 -1
index 40c8682..a13624f 100644
@@ -154,7 +154,7 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 				  le32_to_cpu(c->kato), &ctrl);
 	if (status)
 		goto out;
-	memcpy(ctrl->hostid, d->hostid, sizeof(ctrl->hostid));
+	memcpy(ctrl->hostid, &d->hostid, sizeof(ctrl->hostid));
 
 	status = nvmet_install_queue(ctrl, req);
 	if (status) {
drivers/nvme/target/io-cmd.c +414 -0

447 lines not on this page · raw message

drivers/nvme/target/nvmet.h +29 -0
index 918c557..bcf3eec 100644
@@ -26,6 +26,7 @@
 #include <linux/configfs.h>
 #include <linux/rcupdate.h>
 #include <linux/blkdev.h>
+#include <linux/pr.h>
 
 #define NVMET_ASYNC_EVENTS		4
 #define NVMET_ERROR_LOG_SLOTS		128
@@ -57,6 +58,12 @@ struct nvmet_ns {
 	struct config_group	group;
 
 	struct completion	disable_done;
+
+	struct mutex		rsrv_lock;
+	struct list_head	rsrv_list;
+	struct nvmet_ctrl	*rsrv_ctrl;
+	enum pr_type		rsrv_type;
+	u32			rsrv_gen;
 };
 
 static inline struct nvmet_ns *to_nvmet_ns(struct config_item *item)
@@ -134,6 +141,11 @@ struct nvmet_ctrl {
 
 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
 	char			hostnqn[NVMF_NQN_FIELD_LEN];
+
+	struct list_head	rsrv_log;
+	u64			rsrv_log_counter;
+	u32			rsrv_mask;
+	bool			rsrv_log_async_sent;
 };
 
 struct nvmet_subsys {
@@ -231,6 +243,19 @@ struct nvmet_req {
 	struct nvmet_fabrics_ops *ops;
 };
 
+struct nvmet_rsrv {
+	struct list_head	link;
+	struct nvmet_ctrl	*ctrl;
+	u64			rkey;
+	struct rcu_head		rcu_head;
+};
+
+struct nvmet_rsrv_log {
+	struct list_head	link;
+	u32			nsid;
+	u8			log_type;
+};
+
 static inline void nvmet_set_status(struct nvmet_req *req, u16 status)
 {
 	req->rsp->status = cpu_to_le16(status << 1);
@@ -332,4 +357,8 @@ u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf,
 bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
 		const char *hostnqn);
 
+void add_reservation_log_page(struct nvmet_ctrl *ctrl, struct nvmet_ns *ns,
+		int type);
+void nvmet_rsrv_remove_ctrl(struct nvmet_ctrl *ctrl);
+
 #endif /* _NVMET_H */
include/linux/nvme.h +29 -1
index 05560c2..bcf9184 100644
@@ -393,6 +393,15 @@ struct nvme_fw_slot_info_log {
 	__u8			rsvd64[448];
 };
 
+struct nvme_reservation_log {
+	__le64			count;
+	__u8			type;
+	__u8			num_avail;
+	__u16			rsvd2;
+	__le32			nsid;
+	__u8			rsvd48[48];
+};
+
 enum {
 	NVME_SMART_CRIT_SPARE		= 1 << 0,
 	NVME_SMART_CRIT_TEMPERATURE	= 1 << 1,
@@ -432,7 +441,7 @@ struct nvme_reservation_status {
 	__u8	regctl[2];
 	__u8	resv5[2];
 	__u8	ptpls;
-	__u8	resv10[13];
+	__u8	resv10[14];
 	struct {
 		__le16	cntlid;
 		__u8	rcsts;
@@ -442,6 +451,24 @@ struct nvme_reservation_status {
 	} regctl_ds[];
 };
 
+struct nvme_reservation_status_ext {
+	__le32	gen;
+	__u8	rtype;
+	__u8	regctl[2];
+	__u8	resv5[2];
+	__u8	ptpls;
+	__u8	resv10[14];
+	__u8	resv24[40];
+	struct {
+		__le16	cntlid;
+		__u8	rcsts;
+		__u8	resv3[5];
+		__le64	rkey;
+		__u8	hostid[16];
+		__u8	resv32[32];
+	} regctl_eds[];
+};
+
 enum nvme_async_event_type {
 	NVME_AER_TYPE_ERROR	= 0,
 	NVME_AER_TYPE_SMART	= 1,
@@ -709,6 +736,7 @@ enum {
 	NVME_FWACT_REPL		= (0 << 3),
 	NVME_FWACT_REPL_ACTV	= (1 << 3),
 	NVME_FWACT_ACTV		= (2 << 3),
+	NVME_IEKEY		= (1 << 3),
 };
 
 struct nvme_identify {
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 nvmet: Add support for reservations.

Hi Omri,

a bit more information (a.k.a. a commit message) would be helpful to review your patch. Also I do not see how you intent to make persistant reservation survive across reboots.

Thanks,
Johannes

Signature · 6 lines
Johannes Thumshirn                                          Storage
jthumshirn at suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
author
#2 lore raw

PATCH 1/1 nvmet: Add support for reservations.

A list of registrants, protected by RCU, is kept for each namespace and a reference for the reservation type and holder.
Each I/O operation is checked for reservation conflict.

For every change in the reservation status that warrants it, a log page entry is generated for each controller that should receive it, and an Asynchronous event is sent to all relevant controllers that have not been sent one since the last time they have read the reservation log page.

Reservation status structures definitions were copied from nvme-cli.

This patch does not include support for persistency across power failures.

Quoted 16 lines · Johannes Thumshirn

Hi Omri,

a bit more information (a.k.a. a commit message) would be helpful to review your patch. Also I do not see how you intent to make persistant reservation survive across reboots.

Thanks,
Johannes

--
Johannes Thumshirn Storage
jthumshirn at suse.de +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

#3 lore raw

PATCH 1/1 nvmet: Add support for reservations.

Quoted 4 lines · Omri Mann

A list of registrants, protected by RCU, is kept for each namespace and a reference for the reservation type and holder.
Each I/O operation is checked for reservation conflict.

Quoted 8 lines

For every change in the reservation status that warrants it, a log page entry is generated for each controller that should receive it, and an Asynchronous event is sent to all relevant controllers that have not been sent one since the last time they have read the reservation log page.

Reservation status structures definitions were copied from nvme-cli.

This patch does not include support for persistency across power failures.

This is something I don't relly like. How about adding it to configfs and signal userspace via a uevent or sth. like that so we can call nvmetcli store (or something along that lines).

Byte,
Johannes

Signature · 6 lines
Johannes Thumshirn                                          Storage
jthumshirn at suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850