Blob Blame History Raw
From 4b3c4e992c6ab397b2e56f832b972d57d857583e Mon Sep 17 00:00:00 2001
From: David Disseldorp <ddiss@suse.de>
Date: Fri, 28 Aug 2015 11:09:09 +0200
Subject: [PATCH] target/rbd: add pr_preempt support
Patch-mainline: Not yet, SES2 clustered LIO/RBD
References: fate#318836

Add a tcm_rbd_pr_ops handler for PR PREEMPT requests, that can be used
to remove existing registrations, and/or replace reservations.

Signed-off-by: David Disseldorp <ddiss@suse.de>
---
 drivers/target/target_core_rbd.c |  225 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 225 insertions(+)

--- a/drivers/target/target_core_rbd.c
+++ b/drivers/target/target_core_rbd.c
@@ -2168,6 +2168,230 @@ err_info_free:
 	return ret;
 }
 
+static int
+tcm_rbd_pr_info_rm_regs_key(struct tcm_rbd_pr_info *pr_info,
+			    struct tcm_rbd_pr_reg *existing_reg,
+			    u64 new_key)
+{
+	struct tcm_rbd_pr_reg *reg;
+	struct tcm_rbd_pr_reg *reg_n;
+	bool found = false;
+
+	if (new_key == 0) {
+		dout("removing all non-nexus regs\n");
+	}
+
+	list_for_each_entry_safe(reg, reg_n, &pr_info->regs, regs_node) {
+		if (reg == existing_reg)
+			continue;
+
+		if (new_key && (reg->key != new_key))
+			continue;
+
+		tcm_rbd_pr_info_clear_reg(pr_info, reg);
+		found = true;
+
+		/* TODO flag UA if different IT nexus */
+	}
+
+	if (!found) {
+		return -ENOENT;
+	}
+
+	return 0;
+}
+
+/*
+ * Preempt logic is pretty complex. This implementation attempts to resemble
+ * SPC4r37 Figure 9 — Device server interpretation of PREEMPT service action.
+ */
+static sense_reason_t
+tcm_rbd_execute_pr_preempt(struct se_cmd *cmd, u64 old_key, u64 new_key,
+			   int type, bool abort)
+{
+	struct se_device *dev = cmd->se_dev;
+	struct tcm_rbd_dev *tcm_rbd_dev = TCM_RBD_DEV(dev);
+	char nexus_buf[TCM_RBD_PR_IT_NEXUS_MAXLEN];
+	struct tcm_rbd_pr_info *pr_info;
+	struct tcm_rbd_pr_rsv *rsv;
+	struct tcm_rbd_pr_reg *reg;
+	struct tcm_rbd_pr_reg *existing_reg;
+	bool all_reg;
+	char *pr_xattr;
+	int pr_xattr_len;
+	int rc;
+	sense_reason_t ret;
+	int retries = 0;
+
+	if (!cmd->se_sess || !cmd->se_lun) {
+		pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+	}
+
+	if (abort) {
+		pr_err("PR PREEMPT AND ABORT not supported by RBD backend\n");
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+	}
+
+	rc = tcm_rbd_gen_it_nexus(cmd->se_sess, nexus_buf,
+				  ARRAY_SIZE(nexus_buf));
+	if (rc < 0)
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+retry:
+	pr_info = NULL;
+	pr_xattr = NULL;
+	pr_xattr_len = 0;
+	all_reg = false;
+	rc = tcm_rbd_pr_info_get(tcm_rbd_dev, &pr_info, &pr_xattr,
+				 &pr_xattr_len);
+	if (rc == -ENODATA) {
+		pr_err("SPC-3 PR: no registrations for PREEMPT\n");
+		return TCM_RESERVATION_CONFLICT;
+	} else if (rc < 0) {
+		pr_err("failed to obtain PR info\n");
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+	}
+
+	/* check for an existing registration */
+	existing_reg = NULL;
+	list_for_each_entry(reg, &pr_info->regs, regs_node) {
+		if (!strncmp(reg->it_nexus, nexus_buf, ARRAY_SIZE(nexus_buf))) {
+			dout("found existing PR reg for %s\n", nexus_buf);
+			existing_reg = reg;
+			break;
+		}
+	}
+
+	if (!existing_reg) {
+		pr_err("SPC-3 PR: Unable to locate registration for PREEMPT\n");
+		ret = TCM_RESERVATION_CONFLICT;
+		goto err_info_free;
+	}
+
+	if (old_key != existing_reg->key) {
+		pr_err("SPC-3 PR PREEMPT: Received res_key: 0x%016Lx"
+			" does not match existing SA REGISTER res_key:"
+			" 0x%016Lx\n", old_key, existing_reg->key);
+		ret = TCM_RESERVATION_CONFLICT;
+		goto err_info_free;
+	}
+
+	if (!pr_info->rsv) {
+		/* no reservation, remove regs indicated by new_key */
+		if (new_key == 0) {
+			ret = TCM_INVALID_PARAMETER_LIST;
+			goto err_info_free;
+		}
+		rc = tcm_rbd_pr_info_rm_regs_key(pr_info, existing_reg,
+						 new_key);
+		if (rc == -ENOENT) {
+			ret = TCM_RESERVATION_CONFLICT;
+			goto err_info_free;
+		} else if (rc < 0) {
+			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+			goto err_info_free;
+		}
+		goto commit;
+	}
+
+
+	rsv = pr_info->rsv;
+	if ((rsv->type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG)
+			|| (rsv->type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
+		all_reg = true;
+	}
+
+	if (all_reg) {
+		/* if key is zero, then remove all non-nexus regs */
+		rc = tcm_rbd_pr_info_rm_regs_key(pr_info, existing_reg,
+						 new_key);
+		if (rc == -ENOENT) {
+			ret = TCM_RESERVATION_CONFLICT;
+			goto err_info_free;
+		} else if (rc < 0) {
+			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+			goto err_info_free;
+		}
+
+		if (new_key == 0) {
+			tcm_rbd_pr_info_rsv_clear(pr_info);
+			rsv = NULL;
+			rc = tcm_rbd_pr_info_rsv_set(pr_info, existing_reg->key,
+						     existing_reg->it_nexus, type);
+			if (rc < 0) {
+				pr_err("failed to set PR info reservation\n");
+				ret = TCM_OUT_OF_RESOURCES;
+				goto err_info_free;
+			}
+		}
+		goto commit;
+	}
+
+	if (rsv->key != new_key) {
+		if (new_key == 0) {
+			ret = TCM_INVALID_PARAMETER_LIST;
+			goto err_info_free;
+		}
+		rc = tcm_rbd_pr_info_rm_regs_key(pr_info, existing_reg,
+						 new_key);
+		if (rc == -ENOENT) {
+			ret = TCM_RESERVATION_CONFLICT;
+			goto err_info_free;
+		} else if (rc < 0) {
+			ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+			goto err_info_free;
+		}
+		goto commit;
+	}
+
+	rc = tcm_rbd_pr_info_rm_regs_key(pr_info, existing_reg, new_key);
+	if (rc == -ENOENT) {
+		ret = TCM_RESERVATION_CONFLICT;
+		goto err_info_free;
+	} else if (rc < 0) {
+		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+		goto err_info_free;
+	}
+	tcm_rbd_pr_info_rsv_clear(pr_info);
+	rsv = NULL;
+	rc = tcm_rbd_pr_info_rsv_set(pr_info, existing_reg->key,
+				     existing_reg->it_nexus, type);
+	if (rc < 0) {
+		pr_err("failed to set PR info reservation\n");
+		ret = TCM_OUT_OF_RESOURCES;
+		goto err_info_free;
+	}
+commit:
+	/* PR generation must be incremented on successful PREEMPT */
+	pr_info->gen++;
+
+	rc = tcm_rbd_pr_info_replace(tcm_rbd_dev, pr_xattr, pr_xattr_len,
+				     pr_info);
+	if (rc == -ECANCELED) {
+		pr_warn("atomic PR info update failed due to parallel "
+			"change, expected(%d) %s. Retrying...\n",
+			pr_xattr_len, pr_xattr);
+		retries++;
+		if (retries <= TCM_RBD_PR_REG_MAX_RETRIES) {
+			tcm_rbd_pr_info_free(pr_info);
+			kfree(pr_xattr);
+			goto retry;
+		}
+	}
+	if (rc < 0) {
+		pr_err("atomic PR info update failed: %d\n", rc);
+		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+		goto err_info_free;
+	}
+
+	ret = TCM_NO_SENSE;
+err_info_free:
+	tcm_rbd_pr_info_free(pr_info);
+	kfree(pr_xattr);
+	return ret;
+}
+
 static struct target_pr_ops tcm_rbd_pr_ops = {
 	.pr_read_keys		= tcm_rbd_execute_pr_read_keys,
 	.pr_read_reservation	= tcm_rbd_execute_pr_read_reservation,
@@ -2176,6 +2400,7 @@ static struct target_pr_ops tcm_rbd_pr_o
 	.pr_reserve		= tcm_rbd_execute_pr_reserve,
 	.pr_release		= tcm_rbd_execute_pr_release,
 	.pr_clear		= tcm_rbd_execute_pr_clear,
+	.pr_preempt		= tcm_rbd_execute_pr_preempt,
 };
 
 static const struct target_backend_ops tcm_rbd_ops = {