Blob Blame History Raw
From b8ed821ba05f3d06ebcd302cb595a06fbe6a1d73 Mon Sep 17 00:00:00 2001
From: David Disseldorp <ddiss@suse.de>
Date: Wed, 26 Aug 2015 21:54:07 +0200
Subject: [PATCH] target/rbd: add pr_release support
Patch-mainline: Not yet, SES2 clustered LIO/RBD
References: fate#318836

Add a tcm_rbd_pr_ops handler for PR RELEASE requests.
This change also ensures that deregistration with reservation results in
an implicit RELEASE.

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

--- a/drivers/target/target_core_rbd.c
+++ b/drivers/target/target_core_rbd.c
@@ -844,6 +844,15 @@ tcm_rbd_pr_info_rsv_set(struct tcm_rbd_p
 	return 0;
 }
 
+static void
+tcm_rbd_pr_info_rsv_clear(struct tcm_rbd_pr_info *pr_info)
+{
+	kfree(pr_info->rsv);
+	pr_info->rsv = NULL;
+
+	dout("pr_info rsv cleared\n");
+}
+
 static int
 tcm_rbd_pr_info_append_reg(struct tcm_rbd_pr_info *pr_info, char *nexus,
 			   u64 key)
@@ -882,13 +891,24 @@ static int
 tcm_rbd_pr_info_unregister_reg(struct tcm_rbd_pr_info *pr_info,
 			       struct tcm_rbd_pr_reg *reg)
 {
-	/*
-	 * TODO
-	 * If the I_T nexus is a reservation holder, the persistent reservation
-	 * is of an all registrants type, and the I_T nexus is the last
-	 * remaining registered I_T nexus, then the device server shall also
-	 * release the persistent reservation.
-	 */
+	struct tcm_rbd_pr_rsv *rsv;
+	bool all_reg = false;
+
+	rsv = pr_info->rsv;
+	if (rsv && tcm_rbd_is_rsv_holder(rsv, reg, &all_reg)) {
+		/*
+		 * If the persistent reservation holder is more than one I_T
+		 * nexus, the reservation shall not be released until the
+		 * registrations for all persistent reservation holder I_T
+		 * nexuses are removed.
+		 */
+		if (!all_reg || (pr_info->num_regs == 1)) {
+			pr_warn("implicitly releasing PR of type %d on "
+				"unregister from %s\n",
+				rsv->type, reg->it_nexus);
+			tcm_rbd_pr_info_rsv_clear(pr_info);
+		}
+	}
 
 	tcm_rbd_pr_info_clear_reg(pr_info, reg);
 
@@ -1846,11 +1866,131 @@ err_info_free:
 	return ret;
 }
 
+static sense_reason_t
+tcm_rbd_execute_pr_release(struct se_cmd *cmd, int type, u64 key)
+{
+	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_reg *reg;
+	struct tcm_rbd_pr_reg *existing_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;
+	}
+
+	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;
+	rc = tcm_rbd_pr_info_get(tcm_rbd_dev, &pr_info, &pr_xattr,
+				 &pr_xattr_len);
+	if (rc < 0) {
+		/* existing registration required for release */
+		pr_err("failed to obtain PR info\n");
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+	}
+
+	if (!pr_info->rsv) {
+		/* no persistent reservation, return GOOD status */
+		goto done;
+	}
+
+	/* 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 RELEASE\n");
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+	}
+
+	if (!tcm_rbd_is_rsv_holder(pr_info->rsv, existing_reg, NULL)) {
+		/* registered but not a persistent reservation holder */
+		goto done;
+	}
+
+	if (key != existing_reg->key) {
+		pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
+			" does not match existing SA REGISTER res_key:"
+			" 0x%016Lx\n", key, existing_reg->key);
+		ret = TCM_RESERVATION_CONFLICT;
+		goto err_info_free;
+	}
+
+	if (pr_info->rsv->type != type) {
+		pr_err("SPC-3 PR: Attempted RELEASE from %s with different "
+		       "TYPE, returning RESERVATION_CONFLICT\n",
+		       existing_reg->it_nexus);
+		ret = TCM_RESERVATION_CONFLICT;
+		goto err_info_free;
+	}
+
+	/* release the persistent reservation */
+	tcm_rbd_pr_info_rsv_clear(pr_info);
+
+	/*
+	 * TODO:
+	 * c) If the released persistent reservation is a registrants only type
+	 * or all registrants type persistent reservation,
+	 *    the device server shall establish a unit attention condition for
+	 *    the initiator port associated with every regis-
+	 *    tered I_T nexus other than I_T nexus on which the PERSISTENT
+	 *    RESERVE OUT command with RELEASE service action was received,
+	 *    with the additional sense code set to RESERVATIONS RELEASED
+	 */
+
+	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;
+	}
+
+done:
+	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_register		= tcm_rbd_execute_pr_register,
 	.pr_reserve		= tcm_rbd_execute_pr_reserve,
+	.pr_release		= tcm_rbd_execute_pr_release,
 };
 
 static const struct target_backend_ops tcm_rbd_ops = {