Skip to contentSkip to conversation

Add support for reservations to NVMeoF target

PATCH New Omri Mann 3 participants 4 messages Aug 23, 2017 – Aug 30, 2017

Subsystems (2)
lore
Share & tools
lore

Omri Mann

author
#0 lore raw

PATCH 1/1 Add support for reservations to NVMeoF target

---
 drivers/nvme/target/admin-cmd.c |  42 +++++
 drivers/nvme/target/core.c      |  24 +++
 drivers/nvme/target/io-cmd.c    | 365 ++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/target/nvmet.h     |  29 ++++
 4 files changed, 460 insertions(+)
drivers/nvme/target/admin-cmd.c +42 -0
index a53bb66..9695af7 100644
@@ -156,6 +156,30 @@ 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 < 16) {
+			status = NVME_SC_INTERNAL;
+			goto err;
+		}
+		mutex_lock(&req->sq->ctrl->lock);
+		req->sq->ctrl->rsrv_log_async_sent = false;
+		if (!list_empty(&req->sq->ctrl->rsrv_log)) {
+			struct nvmet_rsrv_log *p;
+
+			list_for_each_entry(p, &req->sq->ctrl->rsrv_log, link) {
+				if (++((u8 *)buf)[9] == 255)
+					break;
+			}
+			p = list_first_entry(&req->sq->ctrl->rsrv_log,
+					     struct nvmet_rsrv_log, link);
+			++req->sq->ctrl->rsrv_log_counter;
+			*(__le64 *)buf =
+				cpu_to_le64(&req->sq->ctrl->rsrv_log_counter);
+			((u8 *)buf)[8] = p->log_type;
+			*(__le32 *)(buf+12) = cpu_to_le32(p->nsid);
+		}
+		mutex_unlock(&req->sq->ctrl->lock);
+		break;
 	default:
 		BUG();
 	}
@@ -461,6 +485,15 @@ static void nvmet_execute_set_features(struct nvmet_req *req)
 		req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
 		nvmet_set_result(req, req->sq->ctrl->kato);
 		break;
+	case NVME_FEAT_HOST_ID:
+		status = nvmet_copy_from_sgl(req, 0, &req->sq->ctrl->host_id,
+						8);
+		break;
+	case NVME_FEAT_RESV_MASK:
+		req->sq->ctrl->rsrv_mask = req->cmd->common.cdw10[1];
+		break;
+	case NVME_FEAT_RESV_PERSIST:
+		break;
 	default:
 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
 		break;
@@ -509,6 +542,14 @@ static void nvmet_execute_get_features(struct nvmet_req *req)
 	case NVME_FEAT_KATO:
 		nvmet_set_result(req, req->sq->ctrl->kato * 1000);
 		break;
+	case NVME_FEAT_HOST_ID:
+		status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->host_id, 8);
+		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;
@@ -563,6 +604,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 f4b02bb..3cd0546 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);
+	rwlock_init(&ns->rsrv_lock);
 
 	ns->nsid = nsid;
 	ns->subsys = subsys;
@@ -846,6 +869,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/io-cmd.c +365 -0

402 lines not on this page · raw message

drivers/nvme/target/nvmet.h +29 -0
index e3b244c..9ddaac4 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;
+
+	rwlock_t		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)
@@ -132,6 +139,12 @@ struct nvmet_ctrl {
 
 	char			subsysnqn[NVMF_NQN_FIELD_LEN];
 	char			hostnqn[NVMF_NQN_FIELD_LEN];
+
+	u64			host_id;
+	struct list_head	rsrv_log;
+	u64			rsrv_log_counter;
+	u32			rsrv_mask;
+	bool			rsrv_log_async_sent;
 };
 
 struct nvmet_subsys {
@@ -229,6 +242,18 @@ struct nvmet_req {
 	struct nvmet_fabrics_ops *ops;
 };
 
+struct nvmet_rsrv {
+	struct list_head	link;
+	struct nvmet_ctrl	*ctrl;
+	u64			rkey;
+};
+
+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);
@@ -330,4 +355,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 */
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw

PATCH 1/1 Add support for reservations to NVMeoF target

Hi Omri,

First, please follow the format of patch submission as described
in: Documentation/process/submitting-patches.rst

Second, I would love to see a generic reservations lib so nvmet and LIO can share the same abstraction (with proper callouts in place). But I don't think its a gating issue. Adding Mike to the thread.

Third, persistent reservations should survive power loss, which means they need to be logged in a file at least.

As for the implementation itself, any reason to choose a rw_lock instead of a rcu protection scheme? Up until now we got the IO path to be lock free and I'm not very enthusiast starting now.

Quoted 19 lines
drivers/nvme/target/admin-cmd.c +11 -0
index a53bb66..9695af7 100644
@@ -156,6 +156,30 @@ 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 < 16) {
+			status = NVME_SC_INTERNAL;
+			goto err;
+		}
+		mutex_lock(&req->sq->ctrl->lock);
+		req->sq->ctrl->rsrv_log_async_sent = false;
+		if (!list_empty(&req->sq->ctrl->rsrv_log)) {
+			struct nvmet_rsrv_log *p;
+
+			list_for_each_entry(p, &req->sq->ctrl->rsrv_log, link) {

drivers/nvme/target/admin-cmd.c static void nvmet_execute_get_log_page(struct nvmet_req *req) · line 179

Quoted 10 lines
drivers/nvme/target/admin-cmd.c +10 -0
+				if (++((u8 *)buf)[9] == 255)
+					break;
+			}
+			p = list_first_entry(&req->sq->ctrl->rsrv_log,
+					     struct nvmet_rsrv_log, link);
+			++req->sq->ctrl->rsrv_log_counter;
+			*(__le64 *)buf =
+				cpu_to_le64(&req->sq->ctrl->rsrv_log_counter);
+			((u8 *)buf)[8] = p->log_type;
+			*(__le32 *)(buf+12) = cpu_to_le32(p->nsid);

I suggest that buf should be casted to a generic reservation log page event (defined in include/linux/nvme.h) instead of this buffer manipulation. And please move the handling to a dedicated routine.

Quoted 7 lines
drivers/nvme/target/admin-cmd.c +3 -0
+		}
+		mutex_unlock(&req->sq->ctrl->lock);
+		break;
  	default:
  		BUG();
  	}

drivers/nvme/target/admin-cmd.c static void nvmet_execute_set_features(struct nvmet_req *req) · line 491

Quoted 7 lines
drivers/nvme/target/admin-cmd.c +4 -0
@@ -461,6 +485,15 @@ static void nvmet_execute_set_features(struct nvmet_req *req)
  		req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
  		nvmet_set_result(req, req->sq->ctrl->kato);
  		break;
+	case NVME_FEAT_HOST_ID:
+		status = nvmet_copy_from_sgl(req, 0, &req->sq->ctrl->host_id,
+						8);
+		break;

Can you split the host_id part to a dedicated patch?

Quoted 10 lines
drivers/nvme/target/admin-cmd.c +10 -0
+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;

drivers/nvme/target/admin-cmd.c static void nvmet_execute_set_features(struct nvmet_req *req)

Quoted 10 lines
drivers/nvme/target/admin-cmd.c +10 -0
+	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);
+	}
+}

It would be good if you directly fill a reservation log page structure itself here.

Quoted 100 lines
drivers/nvme/target/admin-cmd.c +4 -0
+
  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);
+	rwlock_init(&ns->rsrv_lock);
 
  	ns->nsid = nsid;
  	ns->subsys = subsys;
@@ -846,6 +869,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/io-cmd.c +57 -0
index 3b4d47a..4eb4182 100644
@@ -16,6 +16,8 @@
  #include <linux/module.h>
  #include "nvmet.h"
 
+#define IEKEY	0x8	/* Ignore Existing Key */
+
  static void nvmet_bio_done(struct bio *bio)
  {
  	struct nvmet_req *req = bio->bi_private;
@@ -40,6 +42,57 @@ static void nvmet_inline_bio_init(struct nvmet_req *req)
  	bio_init(bio, req->inline_bvec, NVMET_MAX_INLINE_BIOVEC);
  }
 
+static inline bool nvmet_ctrl_same_host(struct nvmet_ctrl *ctrl1,
+					struct nvmet_ctrl *ctrl2)
+{
+	if (ctrl1 == NULL || ctrl2 == NULL)
+		return false;
+	if (ctrl1 == ctrl2)
+		return true;
+	if (ctrl1->host_id != 0 && ctrl1->host_id == ctrl2->host_id)
+		return true;
+	return false;
+}
+
+static struct nvmet_rsrv *nvmet_find_reservation(struct nvmet_req *req)
+{
+	struct nvmet_rsrv *p;
+
+	list_for_each_entry(p, &req->ns->rsrv_list, link) {
+		if (nvmet_ctrl_same_host(p->ctrl, req->sq->ctrl))
+			return p;
+	}
+	return NULL;
+}
+
+static bool nvmet_check_reservation(struct nvmet_req *req)
+{
+	bool success = true;
+
+	read_lock(&req->ns->rsrv_lock);
+	switch (req->ns->rsrv_type) {
+	case PR_WRITE_EXCLUSIVE:
+		if (req->cmd->rw.opcode == nvme_cmd_read)
+			goto out;
+	case PR_EXCLUSIVE_ACCESS:
+		success = nvmet_ctrl_same_host(req->ns->rsrv_ctrl,
+						req->sq->ctrl);
+		goto out;
+	case PR_WRITE_EXCLUSIVE_REG_ONLY:
+	case PR_WRITE_EXCLUSIVE_ALL_REGS:
+		if (req->cmd->rw.opcode == nvme_cmd_read)
+			goto out;
+	case PR_EXCLUSIVE_ACCESS_REG_ONLY:
+	case PR_EXCLUSIVE_ACCESS_ALL_REGS:
+		success = (nvmet_find_reservation(req) != NULL);
+	default:
+		goto out;
+	}
+out:
+	read_unlock(&req->ns->rsrv_lock);
+	return success;
+}
+
  static void nvmet_execute_rw(struct nvmet_req *req)
  {
  	int sg_cnt = req->sg_cnt;
@@ -196,6 +249,296 @@ static void nvmet_execute_write_zeroes(struct nvmet_req *req)
  	}
  }
 
+static void nvmet_execute_resv_register(struct nvmet_req *req)
+{
+	struct nvmet_rsrv *p;
+	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);

drivers/nvme/target/io-cmd.c static void nvmet_execute_write_zeroes(struct nvmet_req *req) · line 265

Quoted 10 lines
drivers/nvme/target/io-cmd.c +9 -0
+	u16 status = NVME_SC_SUCCESS;
+	struct { u64 crkey, nrkey; } keys;
+
+	write_lock(&req->ns->rsrv_lock);
+	status = nvmet_copy_from_sgl(req, 0, &keys, sizeof(keys));
+	if (status)
+		goto out;
+	p = nvmet_find_reservation(req);
 
+	switch (cdw10 & 0x7) {

You should add a macro to extract the reservation action directly from nvme_command.

drivers/nvme/target/io-cmd.c static void nvmet_execute_write_zeroes(struct nvmet_req *req) · line 265

Quoted 1 line
drivers/nvme/target/io-cmd.c +1 -0
+	case 0:	/* Register */

I think that meaningful enumeration is better then comments.

drivers/nvme/target/io-cmd.c static void nvmet_execute_write_zeroes(struct nvmet_req *req) · line 268

Quoted 2 lines
drivers/nvme/target/io-cmd.c +2 -0
+		if (p == NULL) {
+			p = kzalloc(sizeof(*p), GFP_KERNEL);

Allocation under write_lock?

Quoted 9 lines
drivers/nvme/target/io-cmd.c +9 -0
+
+static void nvmet_execute_resv_report(struct nvmet_req *req)
+{
+	union {
+		struct {
+			__le32	gen;
+			u8	rtype;
+			__le16	regctls;
+			u16	rsvd2;

drivers/nvme/target/io-cmd.c static void nvmet_execute_write_zeroes(struct nvmet_req *req) · line 327

Quoted 10 lines
drivers/nvme/target/io-cmd.c +10 -0
+			u8	ptpls;
+			u8	rsvd14[14];
+		} __packed hdr;
+		struct {
+			__le16	cntlid;
+			u8	rcsts;
+			u8	rsvd5[5];
+			__le64	hostid;
+			__le64	rkey;
+		} __packed cntrl;

These are generic nvme structures, so they should live in nvme protocol header.

#2 lore raw

PATCH 1/1 Add support for reservations to NVMeoF target

Quoted 5 lines · Sagi Grimberg

Hi Omri,

First, please follow the format of patch submission as described
in: Documentation/process/submitting-patches.rst

Quoted 7 lines

Second, I would love to see a generic reservations lib so nvmet and LIO can share the same abstraction (with proper callouts in place). But I don't think its a gating issue. Adding Mike to the thread.

Third, persistent reservations should survive power loss, which means they need to be logged in a file at least.

Strictly speaking they only need to be persistent depending on the CPTPL field in the Reservation Register command, but implementing that is mandatory, so yes we'll need it. And I really want it to actually do atomic updates, unlike the LIO code which could corrupt its database fairly easily.

Bart (added to Cc) did some nice work on a switch and even a DLM implementation for SCST, which I'd really like to get into mainline:

https://github.com/bvanassche/scst/blob/master/scst/src/scst_pres.c
https://github.com/bvanassche/scst/blob/master/scst/src/scst_dlm.c

and I'd love to get some of that code (or at least the ideas) into mainline - both for NVMe and possibly also for the existing SCSI target.

Quoted 3 lines

As for the implementation itself, any reason to choose a rw_lock instead of a rcu protection scheme? Up until now we got the IO path to be lock free and I'm not very enthusiast starting now.

Agreed.

Quoted 7 lines

drivers/nvme/target/io-cmd.c line 18

Quoted 7 lines

index 3b4d47a..4eb4182 100644

drivers/nvme/target/io-cmd.c +1 -0
@@ -16,6 +16,8 @@
  #include <linux/module.h>
  #include "nvmet.h"
+#define IEKEY	0x8	/* Ignore Existing Key */

Needs to got to include/linux/nvme.h

Quoted 13 lines
Quoted 13 lines
drivers/nvme/target/io-cmd.c +13 -0
+static void nvmet_execute_resv_report(struct nvmet_req *req)
+{
+	union {
+		struct {
+			__le32	gen;
+			u8	rtype;
+			__le16	regctls;
+			u16	rsvd2;
+			u8	ptpls;
+			u8	rsvd14[14];
+		} __packed hdr;
+		struct {
+			__le16	cntlid;
Quoted 8 lines

drivers/nvme/target/io-cmd.c static void nvmet_execute_write_zeroes(struct nvmet_req *req) · line 327

Quoted 5 lines
drivers/nvme/target/io-cmd.c +5 -0
+			u8	rcsts;
+			u8	rsvd5[5];
+			__le64	hostid;
+			__le64	rkey;
+		} __packed cntrl;

These are generic nvme structures, so they should live in nvme protocol header.

Agreed. Also support for the reservation report command in nvme-cli
would be really nice for debugging.

author
#3 lore raw

PATCH 1/1 Add support for reservations to NVMeoF target

I think nvme-cli already supports the reservation related commands
including resv-report
in version 1.1

Quoted 71 lines · Christoph Hellwig
Quoted 5 lines · Sagi Grimberg

Hi Omri,

First, please follow the format of patch submission as described
in: Documentation/process/submitting-patches.rst

Quoted 7 lines

Second, I would love to see a generic reservations lib so nvmet and LIO can share the same abstraction (with proper callouts in place). But I don't think its a gating issue. Adding Mike to the thread.

Third, persistent reservations should survive power loss, which means they need to be logged in a file at least.

Strictly speaking they only need to be persistent depending on the CPTPL field in the Reservation Register command, but implementing that is mandatory, so yes we'll need it. And I really want it to actually do atomic updates, unlike the LIO code which could corrupt its database fairly easily.

Bart (added to Cc) did some nice work on a switch and even a DLM implementation for SCST, which I'd really like to get into mainline:

https://github.com/bvanassche/scst/blob/master/scst/src/scst_pres.c
https://github.com/bvanassche/scst/blob/master/scst/src/scst_dlm.c

and I'd love to get some of that code (or at least the ideas) into mainline - both for NVMe and possibly also for the existing SCSI target.

Quoted 3 lines

As for the implementation itself, any reason to choose a rw_lock instead of a rcu protection scheme? Up until now we got the IO path to be lock free and I'm not very enthusiast starting now.

Agreed.

Quoted 7 lines

drivers/nvme/target/io-cmd.c line 18

Quoted 7 lines

index 3b4d47a..4eb4182 100644

drivers/nvme/target/io-cmd.c +1 -0
@@ -16,6 +16,8 @@
  #include <linux/module.h>
  #include "nvmet.h"
+#define IEKEY      0x8     /* Ignore Existing Key */

Needs to got to include/linux/nvme.h

Quoted 13 lines
Quoted 13 lines
drivers/nvme/target/io-cmd.c +13 -0
+static void nvmet_execute_resv_report(struct nvmet_req *req)
+{
+   union {
+           struct {
+                   __le32  gen;
+                   u8      rtype;
+                   __le16  regctls;
+                   u16     rsvd2;
+                   u8      ptpls;
+                   u8      rsvd14[14];
+           } __packed hdr;
+           struct {
+                   __le16  cntlid;
Quoted 8 lines

drivers/nvme/target/io-cmd.c static void nvmet_execute_write_zeroes(struct nvmet_req *req) · line 327

Quoted 5 lines
drivers/nvme/target/io-cmd.c +5 -0
+                   u8      rcsts;
+                   u8      rsvd5[5];
+                   __le64  hostid;
+                   __le64  rkey;
+           } __packed cntrl;

These are generic nvme structures, so they should live in nvme protocol header.

Agreed. Also support for the reservation report command in nvme-cli
would be really nice for debugging.