blob: 5f79ea05f9b815ce9ced1f7a6c670325cebf6c18 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002/*******************************************************************************
3 * Filename: target_core_pr.c
4 *
5 * This file contains SPC-3 compliant persistent reservations and
6 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07008 * (c) Copyright 2009-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08009 *
10 * Nicholas A. Bellinger <nab@kernel.org>
11 *
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080012 ******************************************************************************/
13
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080014#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/list.h>
David S. Miller5538d292015-05-28 11:35:41 -070017#include <linux/vmalloc.h>
Al Viro0e9b10a2013-02-23 15:22:43 -050018#include <linux/file.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080019#include <linux/fcntl.h>
20#include <linux/fs.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020021#include <scsi/scsi_proto.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080022#include <asm/unaligned.h>
23
24#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050025#include <target/target_core_backend.h>
26#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080027
Christoph Hellwige26d99a2011-11-14 12:30:30 -050028#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080029#include "target_core_pr.h"
30#include "target_core_ua.h"
31
32/*
33 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
34 */
35struct pr_transport_id_holder {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036 struct t10_pr_registration *dest_pr_reg;
37 struct se_portal_group *dest_tpg;
38 struct se_node_acl *dest_node_acl;
39 struct se_dev_entry *dest_se_deve;
40 struct list_head dest_list;
41};
42
Andy Groverd2843c12013-05-16 10:40:55 -070043void core_pr_dump_initiator_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080044 struct t10_pr_registration *pr_reg,
45 char *buf,
46 u32 size)
47{
tangwenji24528f02017-08-24 19:59:37 +080048 if (!pr_reg->isid_present_at_reg) {
Andy Groverd2843c12013-05-16 10:40:55 -070049 buf[0] = '\0';
tangwenji24528f02017-08-24 19:59:37 +080050 return;
51 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080052
Andy Groverd2843c12013-05-16 10:40:55 -070053 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054}
55
Andy Grover33ce6a82013-05-16 10:40:54 -070056enum register_type {
57 REGISTER,
58 REGISTER_AND_IGNORE_EXISTING_KEY,
59 REGISTER_AND_MOVE,
60};
61
62enum preempt_type {
63 PREEMPT,
64 PREEMPT_AND_ABORT,
65};
66
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080067static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -080068 struct t10_pr_registration *, int, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080069
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +020070static int is_reservation_holder(
71 struct t10_pr_registration *pr_res_holder,
72 struct t10_pr_registration *pr_reg)
73{
74 int pr_res_type;
75
76 if (pr_res_holder) {
77 pr_res_type = pr_res_holder->pr_res_type;
78
79 return pr_res_holder == pr_reg ||
80 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
81 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
82 }
83 return 0;
84}
85
Christoph Hellwigde103c92012-11-06 12:24:09 -080086static sense_reason_t
87target_scsi2_reservation_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088{
Christoph Hellwigd977f432012-10-10 17:37:15 -040089 struct se_device *dev = cmd->se_dev;
90 struct se_session *sess = cmd->se_sess;
91
92 switch (cmd->t_task_cdb[0]) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080093 case INQUIRY:
94 case RELEASE:
95 case RELEASE_10:
96 return 0;
97 default:
Christoph Hellwigd977f432012-10-10 17:37:15 -040098 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099 }
100
Bart Van Asschefae43462019-04-02 12:58:06 -0700101 if (!dev->reservation_holder || !sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 return 0;
103
Bart Van Asschefae43462019-04-02 12:58:06 -0700104 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800105 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800106
Christoph Hellwigd977f432012-10-10 17:37:15 -0400107 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
108 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800109 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400110 }
111
112 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113}
114
Christoph Hellwigeacac002011-11-03 17:50:40 -0400115static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
116 struct se_node_acl *, struct se_session *);
117static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
118
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700119static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
Christoph Hellwigeacac002011-11-03 17:50:40 -0400120{
121 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400122 struct se_device *dev = cmd->se_dev;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400123 struct t10_pr_registration *pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400124 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400125 int conflict = 0;
126
Christoph Hellwigeacac002011-11-03 17:50:40 -0400127 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
128 se_sess);
129 if (pr_reg) {
130 /*
131 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
132 * behavior
133 *
134 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
135 * status, but no reservation shall be established and the
136 * persistent reservation shall not be changed, if the command
137 * is received from a) and b) below.
138 *
139 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
140 * status, but the persistent reservation shall not be released,
141 * if the command is received from a) and b)
142 *
143 * a) An I_T nexus that is a persistent reservation holder; or
144 * b) An I_T nexus that is registered if a registrants only or
145 * all registrants type persistent reservation is present.
146 *
147 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
148 * RELEASE(6) command, or RELEASE(10) command shall be processed
149 * as defined in SPC-2.
150 */
151 if (pr_reg->pr_res_holder) {
152 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700153 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400154 }
155 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
156 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
157 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
158 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
159 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700160 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400161 }
162 core_scsi3_put_pr_reg(pr_reg);
163 conflict = 1;
164 } else {
165 /*
166 * Following spc2r20 5.5.1 Reservations overview:
167 *
168 * If a logical unit has executed a PERSISTENT RESERVE OUT
169 * command with the REGISTER or the REGISTER AND IGNORE
170 * EXISTING KEY service action and is still registered by any
171 * initiator, all RESERVE commands and all RELEASE commands
172 * regardless of initiator shall conflict and shall terminate
173 * with a RESERVATION CONFLICT status.
174 */
175 spin_lock(&pr_tmpl->registration_lock);
176 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
177 spin_unlock(&pr_tmpl->registration_lock);
178 }
179
180 if (conflict) {
181 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
182 " while active SPC-3 registrations exist,"
183 " returning RESERVATION_CONFLICT\n");
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700184 return -EBUSY;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400185 }
186
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700187 return 0;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400188}
189
Bart Van Asschefae43462019-04-02 12:58:06 -0700190void target_release_reservation(struct se_device *dev)
191{
192 dev->reservation_holder = NULL;
193 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
194 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
195 dev->dev_res_bin_isid = 0;
196 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
197 }
198}
199
Christoph Hellwigde103c92012-11-06 12:24:09 -0800200sense_reason_t
201target_scsi2_reservation_release(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202{
203 struct se_device *dev = cmd->se_dev;
204 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800205 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800206 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207
Wei Yongjun609234e2012-09-07 14:56:28 +0800208 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400209 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700210 rc = target_check_scsi2_reservation_conflict(cmd);
211 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400212 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800213 if (rc < 0)
214 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800215
216 spin_lock(&dev->dev_reservation_lock);
Bart Van Asschefae43462019-04-02 12:58:06 -0700217 if (!dev->reservation_holder || !sess)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400218 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800219
Bart Van Asschefae43462019-04-02 12:58:06 -0700220 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400221 goto out_unlock;
222
Bernhard Kohledc318d2012-05-13 23:39:37 +0200223 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
224 goto out_unlock;
225
Bart Van Asschefae43462019-04-02 12:58:06 -0700226 target_release_reservation(dev);
Wei Yongjun609234e2012-09-07 14:56:28 +0800227 tpg = sess->se_tpg;
Hannes Reineckef2d30682015-06-10 08:41:22 +0200228 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
229 " MAPPED LUN: %llu for %s\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +0100230 tpg->se_tpg_tfo->fabric_name,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700231 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800233
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400234out_unlock:
235 spin_unlock(&dev->dev_reservation_lock);
236out:
Christoph Hellwigde103c92012-11-06 12:24:09 -0800237 target_complete_cmd(cmd, GOOD);
238 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239}
240
Christoph Hellwigde103c92012-11-06 12:24:09 -0800241sense_reason_t
242target_scsi2_reservation_reserve(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243{
244 struct se_device *dev = cmd->se_dev;
245 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800246 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800247 sense_reason_t ret = 0;
248 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249
Andy Grovera1d8b492011-05-02 17:12:10 -0700250 if ((cmd->t_task_cdb[1] & 0x01) &&
251 (cmd->t_task_cdb[1] & 0x02)) {
Colin Ian Kinga3960ce2016-11-07 16:31:21 +0000252 pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800253 return TCM_UNSUPPORTED_SCSI_OPCODE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800254 }
255 /*
256 * This is currently the case for target_core_mod passthrough struct se_cmd
257 * ops
258 */
Wei Yongjun609234e2012-09-07 14:56:28 +0800259 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400260 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700261 rc = target_check_scsi2_reservation_conflict(cmd);
262 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400263 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800264
Christoph Hellwigde103c92012-11-06 12:24:09 -0800265 if (rc < 0)
266 return TCM_RESERVATION_CONFLICT;
267
Wei Yongjun609234e2012-09-07 14:56:28 +0800268 tpg = sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800269 spin_lock(&dev->dev_reservation_lock);
Bart Van Asschefae43462019-04-02 12:58:06 -0700270 if (dev->reservation_holder &&
271 dev->reservation_holder->se_node_acl != sess->se_node_acl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700272 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +0100273 tpg->se_tpg_tfo->fabric_name);
Hannes Reineckef2d30682015-06-10 08:41:22 +0200274 pr_err("Original reserver LUN: %llu %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000275 cmd->se_lun->unpacked_lun,
Bart Van Asschefae43462019-04-02 12:58:06 -0700276 dev->reservation_holder->se_node_acl->initiatorname);
Hannes Reineckef2d30682015-06-10 08:41:22 +0200277 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
Andy Grovere3d6f902011-07-19 08:55:10 +0000278 " from %s \n", cmd->se_lun->unpacked_lun,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700279 cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 sess->se_node_acl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800281 ret = TCM_RESERVATION_CONFLICT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400282 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800283 }
284
Bart Van Asschefae43462019-04-02 12:58:06 -0700285 dev->reservation_holder = sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400286 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 if (sess->sess_bin_isid != 0) {
288 dev->dev_res_bin_isid = sess->sess_bin_isid;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400289 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290 }
Hannes Reineckef2d30682015-06-10 08:41:22 +0200291 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
David Disseldorp30c7ca92018-11-23 18:36:12 +0100292 " for %s\n", tpg->se_tpg_tfo->fabric_name,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700293 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800294 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400296out_unlock:
297 spin_unlock(&dev->dev_reservation_lock);
298out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400299 if (!ret)
300 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400301 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302}
303
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304
305/*
306 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
307 *
308 * This function is called by those initiator ports who are *NOT*
309 * the active PR reservation holder when a reservation is present.
310 */
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200311static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
312 bool isid_mismatch)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400314 unsigned char *cdb = cmd->t_task_cdb;
Andy Grovere3d6f902011-07-19 08:55:10 +0000315 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700316 struct se_node_acl *nacl = se_sess->se_node_acl;
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200317 int other_cdb = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 int registered_nexus = 0, ret = 1; /* Conflict by default */
319 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
320 int we = 0; /* Write Exclusive */
321 int legacy = 0; /* Act like a legacy device and return
322 * RESERVATION CONFLICT on some CDBs */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200324 if (isid_mismatch) {
325 registered_nexus = 0;
326 } else {
327 struct se_dev_entry *se_deve;
328
329 rcu_read_lock();
330 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
331 if (se_deve)
332 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
333 &se_deve->deve_flags);
334 rcu_read_unlock();
335 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800336
337 switch (pr_reg_type) {
338 case PR_TYPE_WRITE_EXCLUSIVE:
339 we = 1;
340 case PR_TYPE_EXCLUSIVE_ACCESS:
341 /*
342 * Some commands are only allowed for the persistent reservation
343 * holder.
344 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800345 break;
346 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
347 we = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500348 fallthrough;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800349 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
350 /*
351 * Some commands are only allowed for registered I_T Nexuses.
352 */
353 reg_only = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800354 break;
355 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
356 we = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500357 fallthrough;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
359 /*
360 * Each registered I_T Nexus is a reservation holder.
361 */
362 all_reg = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800363 break;
364 default:
Andy Grovere3d6f902011-07-19 08:55:10 +0000365 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 }
367 /*
368 * Referenced from spc4r17 table 45 for *NON* PR holder access
369 */
370 switch (cdb[0]) {
371 case SECURITY_PROTOCOL_IN:
372 if (registered_nexus)
373 return 0;
374 ret = (we) ? 0 : 1;
375 break;
376 case MODE_SENSE:
377 case MODE_SENSE_10:
378 case READ_ATTRIBUTE:
379 case READ_BUFFER:
380 case RECEIVE_DIAGNOSTIC:
381 if (legacy) {
382 ret = 1;
383 break;
384 }
385 if (registered_nexus) {
386 ret = 0;
387 break;
388 }
389 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
390 break;
391 case PERSISTENT_RESERVE_OUT:
392 /*
393 * This follows PERSISTENT_RESERVE_OUT service actions that
394 * are allowed in the presence of various reservations.
395 * See spc4r17, table 46
396 */
397 switch (cdb[1] & 0x1f) {
398 case PRO_CLEAR:
399 case PRO_PREEMPT:
400 case PRO_PREEMPT_AND_ABORT:
401 ret = (registered_nexus) ? 0 : 1;
402 break;
403 case PRO_REGISTER:
404 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
405 ret = 0;
406 break;
407 case PRO_REGISTER_AND_MOVE:
408 case PRO_RESERVE:
409 ret = 1;
410 break;
411 case PRO_RELEASE:
412 ret = (registered_nexus) ? 0 : 1;
413 break;
414 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700415 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416 " action: 0x%02x\n", cdb[1] & 0x1f);
Andy Grovere3d6f902011-07-19 08:55:10 +0000417 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418 }
419 break;
420 case RELEASE:
421 case RELEASE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400422 /* Handled by CRH=1 in target_scsi2_reservation_release() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800423 ret = 0;
424 break;
425 case RESERVE:
426 case RESERVE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400427 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800428 ret = 0;
429 break;
430 case TEST_UNIT_READY:
431 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
432 break;
433 case MAINTENANCE_IN:
434 switch (cdb[1] & 0x1f) {
435 case MI_MANAGEMENT_PROTOCOL_IN:
436 if (registered_nexus) {
437 ret = 0;
438 break;
439 }
440 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
441 break;
442 case MI_REPORT_SUPPORTED_OPERATION_CODES:
443 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
444 if (legacy) {
445 ret = 1;
446 break;
447 }
448 if (registered_nexus) {
449 ret = 0;
450 break;
451 }
452 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
453 break;
454 case MI_REPORT_ALIASES:
455 case MI_REPORT_IDENTIFYING_INFORMATION:
456 case MI_REPORT_PRIORITY:
457 case MI_REPORT_TARGET_PGS:
458 case MI_REPORT_TIMESTAMP:
459 ret = 0; /* Allowed */
460 break;
461 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700462 pr_err("Unknown MI Service Action: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800463 (cdb[1] & 0x1f));
Andy Grovere3d6f902011-07-19 08:55:10 +0000464 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465 }
466 break;
467 case ACCESS_CONTROL_IN:
468 case ACCESS_CONTROL_OUT:
469 case INQUIRY:
470 case LOG_SENSE:
Hannes Reinecke85686f62014-11-17 14:25:20 +0100471 case SERVICE_ACTION_IN_12:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472 case REPORT_LUNS:
473 case REQUEST_SENSE:
Marco Sanvido68169662012-01-03 17:12:58 -0800474 case PERSISTENT_RESERVE_IN:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 ret = 0; /*/ Allowed CDBs */
476 break;
477 default:
478 other_cdb = 1;
479 break;
480 }
481 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300482 * Case where the CDB is explicitly allowed in the above switch
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483 * statement.
484 */
Andy Grover6708bb22011-06-08 10:36:43 -0700485 if (!ret && !other_cdb) {
Hannes Reinecke125d0112013-11-19 09:07:46 +0100486 pr_debug("Allowing explicit CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800487 " reservation holder\n", cdb[0],
488 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700489
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800490 return ret;
491 }
492 /*
493 * Check if write exclusive initiator ports *NOT* holding the
494 * WRITE_EXCLUSIVE_* reservation.
495 */
Andy Groveree1b1b92012-07-12 17:34:54 -0700496 if (we && !registered_nexus) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800497 if (cmd->data_direction == DMA_TO_DEVICE) {
498 /*
499 * Conflict for write exclusive
500 */
Andy Grover6708bb22011-06-08 10:36:43 -0700501 pr_debug("%s Conflict for unregistered nexus"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502 " %s CDB: 0x%02x to %s reservation\n",
503 transport_dump_cmd_direction(cmd),
504 se_sess->se_node_acl->initiatorname, cdb[0],
505 core_scsi3_pr_dump_type(pr_reg_type));
506 return 1;
507 } else {
508 /*
509 * Allow non WRITE CDBs for all Write Exclusive
510 * PR TYPEs to pass for registered and
511 * non-registered_nexuxes NOT holding the reservation.
512 *
513 * We only make noise for the unregisterd nexuses,
514 * as we expect registered non-reservation holding
515 * nexuses to issue CDBs.
516 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700517
Andy Grover6708bb22011-06-08 10:36:43 -0700518 if (!registered_nexus) {
Hannes Reinecke125d0112013-11-19 09:07:46 +0100519 pr_debug("Allowing implicit CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800520 " for %s reservation on unregistered"
521 " nexus\n", cdb[0],
522 core_scsi3_pr_dump_type(pr_reg_type));
523 }
Andy Grover8b1e1242012-04-03 15:51:12 -0700524
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 return 0;
526 }
527 } else if ((reg_only) || (all_reg)) {
528 if (registered_nexus) {
529 /*
530 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
531 * allow commands from registered nexuses.
532 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700533
Hannes Reinecke125d0112013-11-19 09:07:46 +0100534 pr_debug("Allowing implicit CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 " reservation\n", cdb[0],
536 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700537
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 return 0;
539 }
Lee Duncan1ecc7582015-01-05 10:49:44 -0800540 } else if (we && registered_nexus) {
541 /*
542 * Reads are allowed for Write Exclusive locks
543 * from all registrants.
544 */
545 if (cmd->data_direction == DMA_FROM_DEVICE) {
546 pr_debug("Allowing READ CDB: 0x%02x for %s"
547 " reservation\n", cdb[0],
548 core_scsi3_pr_dump_type(pr_reg_type));
549
550 return 0;
551 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552 }
Andy Grover6708bb22011-06-08 10:36:43 -0700553 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 " for %s reservation\n", transport_dump_cmd_direction(cmd),
555 (registered_nexus) ? "" : "un",
556 se_sess->se_node_acl->initiatorname, cdb[0],
557 core_scsi3_pr_dump_type(pr_reg_type));
558
559 return 1; /* Conflict by default */
560}
561
Christoph Hellwigde103c92012-11-06 12:24:09 -0800562static sense_reason_t
563target_scsi3_pr_reservation_check(struct se_cmd *cmd)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400564{
565 struct se_device *dev = cmd->se_dev;
566 struct se_session *sess = cmd->se_sess;
567 u32 pr_reg_type;
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200568 bool isid_mismatch = false;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400569
570 if (!dev->dev_pr_res_holder)
571 return 0;
572
573 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
574 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
575 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
576 goto check_nonholder;
577
578 if (dev->dev_pr_res_holder->isid_present_at_reg) {
579 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
580 sess->sess_bin_isid) {
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200581 isid_mismatch = true;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400582 goto check_nonholder;
583 }
584 }
585
586 return 0;
587
588check_nonholder:
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200589 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
Christoph Hellwigde103c92012-11-06 12:24:09 -0800590 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400591 return 0;
592}
593
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800594static u32 core_scsi3_pr_generation(struct se_device *dev)
595{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800596 u32 prg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400597
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598 /*
599 * PRGeneration field shall contain the value of a 32-bit wrapping
600 * counter mainted by the device server.
601 *
602 * Note that this is done regardless of Active Persist across
603 * Target PowerLoss (APTPL)
604 *
605 * See spc4r17 section 6.3.12 READ_KEYS service action
606 */
607 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400608 prg = dev->t10_pr.pr_generation++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609 spin_unlock(&dev->dev_reservation_lock);
610
611 return prg;
612}
613
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
615 struct se_device *dev,
616 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000617 struct se_lun *lun,
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700618 struct se_dev_entry *dest_deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200619 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620 unsigned char *isid,
621 u64 sa_res_key,
622 int all_tg_pt,
623 int aptpl)
624{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625 struct t10_pr_registration *pr_reg;
626
627 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700628 if (!pr_reg) {
629 pr_err("Unable to allocate struct t10_pr_registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630 return NULL;
631 }
632
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
634 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
635 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
636 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
637 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
638 atomic_set(&pr_reg->pr_res_holders, 0);
639 pr_reg->pr_reg_nacl = nacl;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700640 /*
641 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
642 * the se_dev_entry->pr_ref will have been already obtained by
643 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
644 *
645 * Otherwise, locate se_dev_entry now and obtain a reference until
646 * registration completes in __core_scsi3_add_registration().
647 */
648 if (dest_deve) {
649 pr_reg->pr_reg_deve = dest_deve;
650 } else {
651 rcu_read_lock();
652 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
653 if (!pr_reg->pr_reg_deve) {
654 rcu_read_unlock();
655 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
656 nacl->initiatorname, mapped_lun);
657 kmem_cache_free(t10_pr_reg_cache, pr_reg);
658 return NULL;
659 }
660 kref_get(&pr_reg->pr_reg_deve->pr_kref);
661 rcu_read_unlock();
662 }
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000663 pr_reg->pr_res_mapped_lun = mapped_lun;
664 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700665 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666 pr_reg->pr_res_key = sa_res_key;
667 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
668 pr_reg->pr_reg_aptpl = aptpl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800669 /*
670 * If an ISID value for this SCSI Initiator Port exists,
671 * save it to the registration now.
672 */
673 if (isid != NULL) {
674 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
675 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
676 pr_reg->isid_present_at_reg = 1;
677 }
678
679 return pr_reg;
680}
681
682static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
683static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
684
685/*
686 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
687 * modes.
688 */
689static struct t10_pr_registration *__core_scsi3_alloc_registration(
690 struct se_device *dev,
691 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000692 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200694 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 unsigned char *isid,
696 u64 sa_res_key,
697 int all_tg_pt,
698 int aptpl)
699{
700 struct se_dev_entry *deve_tmp;
701 struct se_node_acl *nacl_tmp;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000702 struct se_lun_acl *lacl_tmp;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700703 struct se_lun *lun_tmp, *next, *dest_lun;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200704 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800705 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
706 int ret;
707 /*
708 * Create a registration for the I_T Nexus upon which the
709 * PROUT REGISTER was received.
710 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000711 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
712 isid, sa_res_key, all_tg_pt,
713 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700714 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715 return NULL;
716 /*
717 * Return pointer to pr_reg for ALL_TG_PT=0
718 */
Andy Grover6708bb22011-06-08 10:36:43 -0700719 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800720 return pr_reg;
721 /*
722 * Create list of matching SCSI Initiator Port registrations
723 * for ALL_TG_PT=1
724 */
725 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700726 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700727 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
728 continue;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800729 spin_unlock(&dev->se_port_lock);
730
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700731 spin_lock(&lun_tmp->lun_deve_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700732 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733 /*
734 * This pointer will be NULL for demo mode MappedLUNs
Hannes Reinecke125d0112013-11-19 09:07:46 +0100735 * that have not been make explicit via a ConfigFS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800736 * MappedLUN group for the SCSI Initiator Node ACL.
737 */
Andy Grover6708bb22011-06-08 10:36:43 -0700738 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800739 continue;
740
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000741 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700742 lockdep_is_held(&lun_tmp->lun_deve_lock));
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000743 nacl_tmp = lacl_tmp->se_lun_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800744 /*
745 * Skip the matching struct se_node_acl that is allocated
746 * above..
747 */
748 if (nacl == nacl_tmp)
749 continue;
750 /*
751 * Only perform PR registrations for target ports on
752 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
753 * arrived.
754 */
755 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
756 continue;
757 /*
758 * Look for a matching Initiator Node ACL in ASCII format
759 */
760 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
761 continue;
762
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700763 kref_get(&deve_tmp->pr_kref);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700764 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800765 /*
766 * Grab a configfs group dependency that is released
767 * for the exception path at label out: below, or upon
768 * completion of adding ALL_TG_PT=1 registrations in
769 * __core_scsi3_add_registration()
770 */
771 ret = core_scsi3_lunacl_depend_item(deve_tmp);
772 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700773 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800774 "_item() failed\n");
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700775 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700776 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800777 goto out;
778 }
779 /*
780 * Located a matching SCSI Initiator Port on a different
781 * port, allocate the pr_reg_atp and attach it to the
782 * pr_reg->pr_reg_atp_list that will be processed once
783 * the original *pr_reg is processed in
784 * __core_scsi3_add_registration()
785 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700786 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100787 kref_read(&deve_tmp->pr_kref) != 0);
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000788
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800789 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700790 nacl_tmp, dest_lun, deve_tmp,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000791 deve_tmp->mapped_lun, NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800792 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700793 if (!pr_reg_atp) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700794 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800795 core_scsi3_lunacl_undepend_item(deve_tmp);
796 goto out;
797 }
798
799 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
800 &pr_reg->pr_reg_atp_list);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700801 spin_lock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802 }
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700803 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804
805 spin_lock(&dev->se_port_lock);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700806 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807 }
808 spin_unlock(&dev->se_port_lock);
809
810 return pr_reg;
811out:
812 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
813 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
814 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
815 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
816 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
817 }
818 kmem_cache_free(t10_pr_reg_cache, pr_reg);
819 return NULL;
820}
821
822int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000823 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800824 u64 sa_res_key,
825 unsigned char *i_port,
826 unsigned char *isid,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200827 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800828 unsigned char *t_port,
829 u16 tpgt,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200830 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 int res_holder,
832 int all_tg_pt,
833 u8 type)
834{
835 struct t10_pr_registration *pr_reg;
836
Andy Grover6708bb22011-06-08 10:36:43 -0700837 if (!i_port || !t_port || !sa_res_key) {
838 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000839 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800840 }
841
842 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700843 if (!pr_reg) {
844 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000845 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800847
848 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
849 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
850 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
851 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
852 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
853 atomic_set(&pr_reg->pr_res_holders, 0);
854 pr_reg->pr_reg_nacl = NULL;
855 pr_reg->pr_reg_deve = NULL;
856 pr_reg->pr_res_mapped_lun = mapped_lun;
857 pr_reg->pr_aptpl_target_lun = target_lun;
858 pr_reg->pr_res_key = sa_res_key;
859 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
860 pr_reg->pr_reg_aptpl = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800861 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
862 pr_reg->pr_res_type = type;
863 /*
864 * If an ISID value had been saved in APTPL metadata for this
865 * SCSI Initiator Port, restore it now.
866 */
867 if (isid != NULL) {
868 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
869 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
870 pr_reg->isid_present_at_reg = 1;
871 }
872 /*
873 * Copy the i_port and t_port information from caller.
874 */
875 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
876 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
877 pr_reg->pr_reg_tpgt = tpgt;
878 /*
879 * Set pr_res_holder from caller, the pr_reg who is the reservation
880 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
881 * the Initiator Node LUN ACL from the fabric module is created for
882 * this registration.
883 */
884 pr_reg->pr_res_holder = res_holder;
885
886 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700887 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800888 " metadata\n", (res_holder) ? "+reservation" : "");
889 return 0;
890}
891
892static void core_scsi3_aptpl_reserve(
893 struct se_device *dev,
894 struct se_portal_group *tpg,
895 struct se_node_acl *node_acl,
896 struct t10_pr_registration *pr_reg)
897{
898 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800899
900 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700901 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902
903 spin_lock(&dev->dev_reservation_lock);
904 dev->dev_pr_res_holder = pr_reg;
905 spin_unlock(&dev->dev_reservation_lock);
906
Andy Grover6708bb22011-06-08 10:36:43 -0700907 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +0100909 tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800910 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
911 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700912 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +0100913 tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700914 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800915}
916
917static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
Andy Grover33ce6a82013-05-16 10:40:54 -0700918 struct t10_pr_registration *, enum register_type, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800919
920static int __core_scsi3_check_aptpl_registration(
921 struct se_device *dev,
922 struct se_portal_group *tpg,
923 struct se_lun *lun,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200924 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800925 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200926 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800927{
928 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400929 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800930 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
931 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
932 u16 tpgt;
933
934 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
935 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
936 /*
937 * Copy Initiator Port information from struct se_node_acl
938 */
939 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
940 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000941 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
942 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800943 /*
944 * Look for the matching registrations+reservation from those
945 * created from APTPL metadata. Note that multiple registrations
946 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
947 * TransportIDs.
948 */
949 spin_lock(&pr_tmpl->aptpl_reg_lock);
950 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
951 pr_reg_aptpl_list) {
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000952
Andy Grover6708bb22011-06-08 10:36:43 -0700953 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700954 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800955 !(strcmp(pr_reg->pr_tport, t_port)) &&
956 (pr_reg->pr_reg_tpgt == tpgt) &&
957 (pr_reg->pr_aptpl_target_lun == target_lun)) {
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700958 /*
959 * Obtain the ->pr_reg_deve pointer + reference, that
960 * is released by __core_scsi3_add_registration() below.
961 */
962 rcu_read_lock();
963 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
964 if (!pr_reg->pr_reg_deve) {
965 pr_err("Unable to locate PR APTPL %s mapped_lun:"
966 " %llu\n", nacl->initiatorname, mapped_lun);
967 rcu_read_unlock();
968 continue;
969 }
970 kref_get(&pr_reg->pr_reg_deve->pr_kref);
971 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800972
973 pr_reg->pr_reg_nacl = nacl;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700974 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800975 list_del(&pr_reg->pr_reg_aptpl_list);
976 spin_unlock(&pr_tmpl->aptpl_reg_lock);
977 /*
978 * At this point all of the pointers in *pr_reg will
979 * be setup, so go ahead and add the registration.
980 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800981 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
982 /*
983 * If this registration is the reservation holder,
984 * make that happen now..
985 */
986 if (pr_reg->pr_res_holder)
987 core_scsi3_aptpl_reserve(dev, tpg,
988 nacl, pr_reg);
989 /*
990 * Reenable pr_aptpl_active to accept new metadata
991 * updates once the SCSI device is active again..
992 */
993 spin_lock(&pr_tmpl->aptpl_reg_lock);
994 pr_tmpl->pr_aptpl_active = 1;
995 }
996 }
997 spin_unlock(&pr_tmpl->aptpl_reg_lock);
998
999 return 0;
1000}
1001
1002int core_scsi3_check_aptpl_registration(
1003 struct se_device *dev,
1004 struct se_portal_group *tpg,
1005 struct se_lun *lun,
Nicholas Bellingere2480562014-10-04 04:23:15 +00001006 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +02001007 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001008{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001009 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001010 return 0;
1011
1012 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001013 lun->unpacked_lun, nacl,
1014 mapped_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001015}
1016
1017static void __core_scsi3_dump_registration(
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001018 const struct target_core_fabric_ops *tfo,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001019 struct se_device *dev,
1020 struct se_node_acl *nacl,
1021 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001022 enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001023{
1024 struct se_portal_group *se_tpg = nacl->se_tpg;
1025 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026
1027 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001028 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029
Andy Grover6708bb22011-06-08 10:36:43 -07001030 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001031 " Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
Andy Grover33ce6a82013-05-16 10:40:54 -07001032 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001033 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001034 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001035 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01001036 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -07001038 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001039 " Port(s)\n", tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001041 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001042 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001043 " 0x%08x APTPL: %d\n", tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001044 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1045 pr_reg->pr_reg_aptpl);
1046}
1047
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048static void __core_scsi3_add_registration(
1049 struct se_device *dev,
1050 struct se_node_acl *nacl,
1051 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001052 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001053 int register_move)
1054{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001055 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001056 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001057 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001058 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059
1060 /*
1061 * Increment PRgeneration counter for struct se_device upon a successful
1062 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1063 *
1064 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1065 * action, the struct se_device->dev_reservation_lock will already be held,
1066 * so we do not call core_scsi3_pr_generation() which grabs the lock
1067 * for the REGISTER.
1068 */
1069 pr_reg->pr_res_generation = (register_move) ?
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001070 dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001071 core_scsi3_pr_generation(dev);
1072
1073 spin_lock(&pr_tmpl->registration_lock);
1074 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001075
1076 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1077 spin_unlock(&pr_tmpl->registration_lock);
1078 /*
1079 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1080 */
Andy Grover6708bb22011-06-08 10:36:43 -07001081 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001082 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001083 /*
1084 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1085 * allocated in __core_scsi3_alloc_registration()
1086 */
1087 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1088 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001089 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1090
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1092
1093 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1094
1095 spin_lock(&pr_tmpl->registration_lock);
1096 list_add_tail(&pr_reg_tmp->pr_reg_list,
1097 &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001099 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1100 register_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001102 /*
1103 * Drop configfs group dependency reference and deve->pr_kref
1104 * obtained from __core_scsi3_alloc_registration() code.
1105 */
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001106 rcu_read_lock();
1107 deve = pr_reg_tmp->pr_reg_deve;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001108 if (deve) {
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001109 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001110 core_scsi3_lunacl_undepend_item(deve);
1111 pr_reg_tmp->pr_reg_deve = NULL;
1112 }
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001113 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001114 }
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001115out:
1116 /*
1117 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1118 */
1119 rcu_read_lock();
1120 deve = pr_reg->pr_reg_deve;
1121 if (deve) {
1122 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1123 kref_put(&deve->pr_kref, target_pr_kref_release);
1124 pr_reg->pr_reg_deve = NULL;
1125 }
1126 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001127}
1128
1129static int core_scsi3_alloc_registration(
1130 struct se_device *dev,
1131 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001132 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001133 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +02001134 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001135 unsigned char *isid,
1136 u64 sa_res_key,
1137 int all_tg_pt,
1138 int aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07001139 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140 int register_move)
1141{
1142 struct t10_pr_registration *pr_reg;
1143
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001144 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1145 isid, sa_res_key, all_tg_pt,
1146 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001147 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001148 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149
1150 __core_scsi3_add_registration(dev, nacl, pr_reg,
1151 register_type, register_move);
1152 return 0;
1153}
1154
1155static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1156 struct se_device *dev,
1157 struct se_node_acl *nacl,
1158 unsigned char *isid)
1159{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001160 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001161 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162
1163 spin_lock(&pr_tmpl->registration_lock);
1164 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1165 &pr_tmpl->registration_list, pr_reg_list) {
1166 /*
1167 * First look for a matching struct se_node_acl
1168 */
1169 if (pr_reg->pr_reg_nacl != nacl)
1170 continue;
1171
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172 /*
1173 * If this registration does NOT contain a fabric provided
1174 * ISID, then we have found a match.
1175 */
Andy Grover6708bb22011-06-08 10:36:43 -07001176 if (!pr_reg->isid_present_at_reg) {
Joern Engel33940d02014-09-16 16:23:12 -04001177 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001178 spin_unlock(&pr_tmpl->registration_lock);
1179 return pr_reg;
1180 }
1181 /*
1182 * If the *pr_reg contains a fabric defined ISID for multi-value
1183 * SCSI Initiator Port TransportIDs, then we expect a valid
1184 * matching ISID to be provided by the local SCSI Initiator Port.
1185 */
Andy Grover6708bb22011-06-08 10:36:43 -07001186 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001187 continue;
1188 if (strcmp(isid, pr_reg->pr_reg_isid))
1189 continue;
1190
Joern Engel33940d02014-09-16 16:23:12 -04001191 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 spin_unlock(&pr_tmpl->registration_lock);
1193 return pr_reg;
1194 }
1195 spin_unlock(&pr_tmpl->registration_lock);
1196
1197 return NULL;
1198}
1199
1200static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1201 struct se_device *dev,
1202 struct se_node_acl *nacl,
1203 struct se_session *sess)
1204{
1205 struct se_portal_group *tpg = nacl->se_tpg;
1206 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1207
Andy Grovere3d6f902011-07-19 08:55:10 +00001208 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001210 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001211 PR_REG_ISID_LEN);
1212 isid_ptr = &buf[0];
1213 }
1214
1215 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1216}
1217
1218static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1219{
Joern Engel33940d02014-09-16 16:23:12 -04001220 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001221}
1222
Hannes Reinecke125d0112013-11-19 09:07:46 +01001223static int core_scsi3_check_implicit_release(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224 struct se_device *dev,
1225 struct t10_pr_registration *pr_reg)
1226{
1227 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1228 struct t10_pr_registration *pr_res_holder;
1229 int ret = 0;
1230
1231 spin_lock(&dev->dev_reservation_lock);
1232 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001233 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001234 spin_unlock(&dev->dev_reservation_lock);
1235 return ret;
1236 }
1237 if (pr_res_holder == pr_reg) {
1238 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001239 * Perform an implicit RELEASE if the registration that
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001240 * is being released is holding the reservation.
1241 *
1242 * From spc4r17, section 5.7.11.1:
1243 *
1244 * e) If the I_T nexus is the persistent reservation holder
1245 * and the persistent reservation is not an all registrants
1246 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1247 * service action or REGISTER AND IGNORE EXISTING KEY
1248 * service action with the SERVICE ACTION RESERVATION KEY
1249 * field set to zero (see 5.7.11.3).
1250 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001251 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 ret = 1;
1253 /*
1254 * For 'All Registrants' reservation types, all existing
1255 * registrations are still processed as reservation holders
1256 * in core_scsi3_pr_seq_non_holder() after the initial
Hannes Reinecke125d0112013-11-19 09:07:46 +01001257 * reservation holder is implicitly released here.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001258 */
1259 } else if (pr_reg->pr_reg_all_tg_pt &&
1260 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1261 pr_reg->pr_reg_nacl->initiatorname)) &&
1262 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001263 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001264 " UNREGISTER while existing reservation with matching"
1265 " key 0x%016Lx is present from another SCSI Initiator"
1266 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001267 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268 }
1269 spin_unlock(&dev->dev_reservation_lock);
1270
1271 return ret;
1272}
1273
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274static void __core_scsi3_free_registration(
1275 struct se_device *dev,
1276 struct t10_pr_registration *pr_reg,
1277 struct list_head *preempt_and_abort_list,
1278 int dec_holders)
Bart Van Asschecb0df4d2015-04-10 14:49:02 +02001279 __releases(&pr_tmpl->registration_lock)
1280 __acquires(&pr_tmpl->registration_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001281{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001282 const struct target_core_fabric_ops *tfo =
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001283 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001284 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001285 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1286 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001288
Bart Van Assche618baaf2019-01-25 10:34:53 -08001289 lockdep_assert_held(&pr_tmpl->registration_lock);
1290
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001292 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001293
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001294 if (!list_empty(&pr_reg->pr_reg_list))
1295 list_del(&pr_reg->pr_reg_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296 /*
1297 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1298 * so call core_scsi3_put_pr_reg() to decrement our reference.
1299 */
1300 if (dec_holders)
1301 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001302
1303 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 /*
1305 * Wait until all reference from any other I_T nexuses for this
1306 * *pr_reg have been released. Because list_del() is called above,
1307 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1308 * count back to zero, and we release *pr_reg.
1309 */
1310 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001311 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01001312 tfo->fabric_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313 cpu_relax();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314 }
1315
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001316 rcu_read_lock();
1317 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1318 if (deve)
1319 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1320 rcu_read_unlock();
1321
1322 spin_lock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001323 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001324 " Node: %s%s\n", tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001325 pr_reg->pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001326 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001327 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001328 " Port(s)\n", tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001329 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001330 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001331 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001332 " 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001333 pr_reg->pr_res_generation);
1334
Andy Grover6708bb22011-06-08 10:36:43 -07001335 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001336 pr_reg->pr_reg_deve = NULL;
1337 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001338 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1339 return;
1340 }
1341 /*
1342 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1343 * are released once the ABORT_TASK_SET has completed..
1344 */
1345 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1346}
1347
1348void core_scsi3_free_pr_reg_from_nacl(
1349 struct se_device *dev,
1350 struct se_node_acl *nacl)
1351{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001352 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001354 bool free_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355 /*
1356 * If the passed se_node_acl matches the reservation holder,
1357 * release the reservation.
1358 */
1359 spin_lock(&dev->dev_reservation_lock);
1360 pr_res_holder = dev->dev_pr_res_holder;
1361 if ((pr_res_holder != NULL) &&
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001362 (pr_res_holder->pr_reg_nacl == nacl)) {
1363 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1364 free_reg = true;
1365 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366 spin_unlock(&dev->dev_reservation_lock);
1367 /*
1368 * Release any registration associated with the struct se_node_acl.
1369 */
1370 spin_lock(&pr_tmpl->registration_lock);
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001371 if (pr_res_holder && free_reg)
1372 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1373
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001374 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1375 &pr_tmpl->registration_list, pr_reg_list) {
1376
1377 if (pr_reg->pr_reg_nacl != nacl)
1378 continue;
1379
1380 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1381 }
1382 spin_unlock(&pr_tmpl->registration_lock);
1383}
1384
1385void core_scsi3_free_all_registrations(
1386 struct se_device *dev)
1387{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001388 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1390
1391 spin_lock(&dev->dev_reservation_lock);
1392 pr_res_holder = dev->dev_pr_res_holder;
1393 if (pr_res_holder != NULL) {
1394 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1395 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001396 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001397 }
1398 spin_unlock(&dev->dev_reservation_lock);
1399
1400 spin_lock(&pr_tmpl->registration_lock);
1401 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1402 &pr_tmpl->registration_list, pr_reg_list) {
1403
1404 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1405 }
1406 spin_unlock(&pr_tmpl->registration_lock);
1407
1408 spin_lock(&pr_tmpl->aptpl_reg_lock);
1409 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1410 pr_reg_aptpl_list) {
1411 list_del(&pr_reg->pr_reg_aptpl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1413 }
1414 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1415}
1416
1417static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1418{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001419 return target_depend_item(&tpg->tpg_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001420}
1421
1422static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1423{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001424 target_undepend_item(&tpg->tpg_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001425 atomic_dec_mb(&tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426}
1427
1428static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1429{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001430 if (nacl->dynamic_node_acl)
1431 return 0;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001432 return target_depend_item(&nacl->acl_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001433}
1434
1435static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1436{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001437 if (!nacl->dynamic_node_acl)
1438 target_undepend_item(&nacl->acl_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001439 atomic_dec_mb(&nacl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440}
1441
1442static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1443{
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001444 struct se_lun_acl *lun_acl;
Bart Van Asscheac75d8b2015-10-22 15:52:01 -07001445
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446 /*
1447 * For nacl->dynamic_node_acl=1
1448 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001449 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001450 kref_read(&se_deve->pr_kref) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001451 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452 return 0;
1453
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001454 return target_depend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001455}
1456
1457static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1458{
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001459 struct se_lun_acl *lun_acl;
Bart Van Asscheac75d8b2015-10-22 15:52:01 -07001460
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001461 /*
1462 * For nacl->dynamic_node_acl=1
1463 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001464 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001465 kref_read(&se_deve->pr_kref) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001466 if (!lun_acl) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001467 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001468 return;
1469 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001470
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001471 target_undepend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001472 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001473}
1474
Christoph Hellwigde103c92012-11-06 12:24:09 -08001475static sense_reason_t
1476core_scsi3_decode_spec_i_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 struct se_cmd *cmd,
1478 struct se_portal_group *tpg,
1479 unsigned char *l_isid,
1480 u64 sa_res_key,
1481 int all_tg_pt,
1482 int aptpl)
1483{
Andy Grover5951146d2011-07-19 10:26:37 +00001484 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001486 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 struct se_node_acl *dest_node_acl = NULL;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001488 struct se_dev_entry *dest_se_deve = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001489 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1490 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001491 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001493 unsigned char *buf, *ptr, proto_ident;
Sagi Grimberg9b353cc2015-06-29 13:07:06 +03001494 const unsigned char *i_str = NULL;
Julia Lawall13ba5642014-11-30 19:14:12 +01001495 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08001496 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001497 u32 tpdl, tid_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001498 u32 dest_rtpi = 0;
1499
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 /*
1501 * Allocate a struct pr_transport_id_holder and setup the
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001502 * local_node_acl pointer and add to struct list_head tid_dest_list
1503 * for add registration processing in the loop of tid_dest_list below.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504 */
1505 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001506 if (!tidh_new) {
1507 pr_err("Unable to allocate tidh_new\n");
tangwenji79dd6f22017-08-21 20:55:41 +08001508 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 }
1510 INIT_LIST_HEAD(&tidh_new->dest_list);
1511 tidh_new->dest_tpg = tpg;
1512 tidh_new->dest_node_acl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001513
Andy Grover5951146d2011-07-19 10:26:37 +00001514 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001515 se_sess->se_node_acl, cmd->se_lun,
1516 NULL, cmd->orig_fe_lun, l_isid,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001517 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001518 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001519 kfree(tidh_new);
tangwenji79dd6f22017-08-21 20:55:41 +08001520 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001521 }
Mike Christief32ba612020-07-01 20:43:19 -05001522
1523 if (core_scsi3_lunacl_depend_item(local_pr_reg->pr_reg_deve)) {
1524 kfree(tidh_new);
1525 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1526 target_pr_kref_release);
1527 kmem_cache_free(t10_pr_reg_cache, local_pr_reg);
1528 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
1529 }
1530
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531 tidh_new->dest_pr_reg = local_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001533
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001534 if (cmd->data_length < 28) {
1535 pr_warn("SPC-PR: Received PR OUT parameter list"
1536 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001537 ret = TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001538 goto out;
1539 }
1540
Andy Grover49493142012-01-16 16:57:08 -08001541 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001542 if (!buf) {
tangwenji79dd6f22017-08-21 20:55:41 +08001543 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001544 goto out;
1545 }
1546
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001547 /*
1548 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1549 * first extract TransportID Parameter Data Length, and make sure
1550 * the value matches up to the SCSI expected data transfer length.
1551 */
Bart Van Asschea85d6672017-05-23 16:48:27 -07001552 tpdl = get_unaligned_be32(&buf[24]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001553
1554 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001555 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001556 " does not equal CDB data_length: %u\n", tpdl,
1557 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001558 ret = TCM_INVALID_PARAMETER_LIST;
1559 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001560 }
1561 /*
1562 * Start processing the received transport IDs using the
1563 * receiving I_T Nexus portal's fabric dependent methods to
1564 * obtain the SCSI Initiator Port/Device Identifiers.
1565 */
1566 ptr = &buf[28];
1567
1568 while (tpdl > 0) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001569 struct se_lun *dest_lun, *tmp_lun;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001570
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001571 proto_ident = (ptr[0] & 0x0f);
1572 dest_tpg = NULL;
1573
1574 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001575 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1576 tmp_tpg = tmp_lun->lun_tpg;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001577
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578 /*
1579 * Look for the matching proto_ident provided by
1580 * the received TransportID
1581 */
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02001582 if (tmp_tpg->proto_id != proto_ident)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001584 dest_rtpi = tmp_lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001585
Mike Christie63c9ffe2020-07-01 20:43:17 -05001586 iport_ptr = NULL;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001587 i_str = target_parse_pr_out_transport_id(tmp_tpg,
Rasmus Villemoes094bb5d2017-11-21 01:12:43 +01001588 ptr, &tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001589 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 continue;
Mike Christie63c9ffe2020-07-01 20:43:17 -05001591 /*
1592 * Determine if this SCSI device server requires that
1593 * SCSI Intiatior TransportID w/ ISIDs is enforced
1594 * for fabric modules (iSCSI) requiring them.
1595 */
1596 if (tpg->se_tpg_tfo->sess_get_initiator_sid &&
1597 dev->dev_attrib.enforce_pr_isids &&
1598 !iport_ptr) {
1599 pr_warn("SPC-PR: enforce_pr_isids is set but a isid has not been sent in the SPEC_I_PT data for %s.",
1600 i_str);
1601 ret = TCM_INVALID_PARAMETER_LIST;
1602 spin_unlock(&dev->se_port_lock);
1603 goto out_unmap;
1604 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001605
Joern Engel33940d02014-09-16 16:23:12 -04001606 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001607 spin_unlock(&dev->se_port_lock);
1608
Christoph Hellwigde103c92012-11-06 12:24:09 -08001609 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001610 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001611 " for tmp_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04001612 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001613 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1614 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001615 }
1616 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001617 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001618 * from the decoded fabric module specific TransportID
1619 * at *i_str.
1620 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001621 mutex_lock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001622 dest_node_acl = __core_tpg_get_initiator_node_acl(
1623 tmp_tpg, i_str);
Joern Engel33940d02014-09-16 16:23:12 -04001624 if (dest_node_acl)
1625 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001626 mutex_unlock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001627
Andy Grover6708bb22011-06-08 10:36:43 -07001628 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629 core_scsi3_tpg_undepend_item(tmp_tpg);
1630 spin_lock(&dev->se_port_lock);
1631 continue;
1632 }
1633
Christoph Hellwigde103c92012-11-06 12:24:09 -08001634 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001635 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001636 " for dest_node_acl->acl_group\n");
Joern Engel33940d02014-09-16 16:23:12 -04001637 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638 core_scsi3_tpg_undepend_item(tmp_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001639 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1640 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001641 }
1642
1643 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001644 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001645 " %s Port RTPI: %hu\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01001646 dest_tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001647 dest_node_acl->initiatorname, dest_rtpi);
1648
1649 spin_lock(&dev->se_port_lock);
1650 break;
1651 }
1652 spin_unlock(&dev->se_port_lock);
1653
Andy Grover6708bb22011-06-08 10:36:43 -07001654 if (!dest_tpg) {
1655 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001656 " dest_tpg\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001657 ret = TCM_INVALID_PARAMETER_LIST;
1658 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001659 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001660
Andy Grover6708bb22011-06-08 10:36:43 -07001661 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001662 " tid_len: %d for %s + %s\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01001663 dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001664 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001665
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001666 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001667 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 " %u for Transport ID: %s\n", tid_len, ptr);
1669 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1670 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001671 ret = TCM_INVALID_PARAMETER_LIST;
1672 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673 }
1674 /*
1675 * Locate the desintation struct se_dev_entry pointer for matching
1676 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1677 * Target Port.
1678 */
1679 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1680 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001681 if (!dest_se_deve) {
1682 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 " from destination RTPI: %hu\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01001684 dest_tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001685 dest_rtpi);
1686
1687 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1688 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001689 ret = TCM_INVALID_PARAMETER_LIST;
1690 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001691 }
1692
Christoph Hellwigde103c92012-11-06 12:24:09 -08001693 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001694 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001695 " failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001696 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1698 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001699 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1700 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001702
Andy Grover6708bb22011-06-08 10:36:43 -07001703 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001704 " dest_se_deve mapped_lun: %llu\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01001705 dest_tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001707
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001708 /*
1709 * Skip any TransportIDs that already have a registration for
1710 * this target port.
1711 */
1712 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1713 iport_ptr);
1714 if (pr_reg_e) {
1715 core_scsi3_put_pr_reg(pr_reg_e);
1716 core_scsi3_lunacl_undepend_item(dest_se_deve);
1717 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1718 core_scsi3_tpg_undepend_item(dest_tpg);
1719 ptr += tid_len;
1720 tpdl -= tid_len;
1721 tid_len = 0;
1722 continue;
1723 }
1724 /*
1725 * Allocate a struct pr_transport_id_holder and setup
1726 * the dest_node_acl and dest_se_deve pointers for the
1727 * loop below.
1728 */
1729 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1730 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001731 if (!tidh_new) {
1732 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001733 core_scsi3_lunacl_undepend_item(dest_se_deve);
1734 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1735 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001736 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1737 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001738 }
1739 INIT_LIST_HEAD(&tidh_new->dest_list);
1740 tidh_new->dest_tpg = dest_tpg;
1741 tidh_new->dest_node_acl = dest_node_acl;
1742 tidh_new->dest_se_deve = dest_se_deve;
1743
1744 /*
1745 * Allocate, but do NOT add the registration for the
1746 * TransportID referenced SCSI Initiator port. This
1747 * done because of the following from spc4r17 in section
1748 * 6.14.3 wrt SPEC_I_PT:
1749 *
1750 * "If a registration fails for any initiator port (e.g., if th
1751 * logical unit does not have enough resources available to
1752 * hold the registration information), no registrations shall be
1753 * made, and the command shall be terminated with
1754 * CHECK CONDITION status."
1755 *
1756 * That means we call __core_scsi3_alloc_registration() here,
1757 * and then call __core_scsi3_add_registration() in the
1758 * 2nd loop which will never fail.
1759 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001760 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001761 kref_read(&dest_se_deve->pr_kref) != 0);
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001762
Andy Grover5951146d2011-07-19 10:26:37 +00001763 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001764 dest_node_acl, dest_lun, dest_se_deve,
1765 dest_se_deve->mapped_lun, iport_ptr,
1766 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001767 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001768 core_scsi3_lunacl_undepend_item(dest_se_deve);
1769 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1770 core_scsi3_tpg_undepend_item(dest_tpg);
1771 kfree(tidh_new);
tangwenji79dd6f22017-08-21 20:55:41 +08001772 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001773 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001774 }
1775 tidh_new->dest_pr_reg = dest_pr_reg;
1776 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1777
1778 ptr += tid_len;
1779 tpdl -= tid_len;
1780 tid_len = 0;
1781
1782 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001783
Andy Grover49493142012-01-16 16:57:08 -08001784 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001785
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001786 /*
1787 * Go ahead and create a registrations from tid_dest_list for the
1788 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1789 * and dest_se_deve.
1790 *
1791 * The SA Reservation Key from the PROUT is set for the
1792 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1793 * means that the TransportID Initiator port will be
1794 * registered on all of the target ports in the SCSI target device
1795 * ALL_TG_PT=0 means the registration will only be for the
1796 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1797 * was received.
1798 */
1799 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1800 dest_tpg = tidh->dest_tpg;
1801 dest_node_acl = tidh->dest_node_acl;
1802 dest_se_deve = tidh->dest_se_deve;
1803 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001804
1805 list_del(&tidh->dest_list);
1806 kfree(tidh);
1807
1808 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001809 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001810
Andy Grover5951146d2011-07-19 10:26:37 +00001811 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812 dest_pr_reg, 0, 0);
1813
Andy Grover6708bb22011-06-08 10:36:43 -07001814 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001815 " registered Transport ID for Node: %s%s Mapped LUN:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001816 " %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001817 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1818 dest_se_deve->mapped_lun : 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001819
Mike Christief32ba612020-07-01 20:43:19 -05001820 if (dest_pr_reg == local_pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821 continue;
Mike Christief32ba612020-07-01 20:43:19 -05001822
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001823 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1824 core_scsi3_tpg_undepend_item(dest_tpg);
1825 }
1826
1827 return 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001828out_unmap:
Andy Grover49493142012-01-16 16:57:08 -08001829 transport_kunmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001830out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001831 /*
1832 * For the failure case, release everything from tid_dest_list
1833 * including *dest_pr_reg and the configfs dependances..
1834 */
1835 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
Mike Christief32ba612020-07-01 20:43:19 -05001836 bool is_local = false;
1837
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001838 dest_tpg = tidh->dest_tpg;
1839 dest_node_acl = tidh->dest_node_acl;
1840 dest_se_deve = tidh->dest_se_deve;
1841 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842
Mike Christief32ba612020-07-01 20:43:19 -05001843 if (dest_pr_reg == local_pr_reg)
1844 is_local = true;
1845
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001846 list_del(&tidh->dest_list);
1847 kfree(tidh);
1848 /*
1849 * Release any extra ALL_TG_PT=1 registrations for
1850 * the SPEC_I_PT=1 case.
1851 */
1852 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1853 &dest_pr_reg->pr_reg_atp_list,
1854 pr_reg_atp_mem_list) {
1855 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1856 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1857 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1858 }
1859
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001860 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001861 core_scsi3_lunacl_undepend_item(dest_se_deve);
Mike Christief32ba612020-07-01 20:43:19 -05001862
1863 if (is_local)
1864 continue;
1865
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1867 core_scsi3_tpg_undepend_item(dest_tpg);
1868 }
1869 return ret;
1870}
1871
Andy Grover3c8a6222013-05-16 10:40:58 -07001872static int core_scsi3_update_aptpl_buf(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001873 struct se_device *dev,
1874 unsigned char *buf,
Andy Grover0607dec2013-05-16 10:40:57 -07001875 u32 pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001876{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001877 struct se_portal_group *tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001878 struct t10_pr_registration *pr_reg;
1879 unsigned char tmp[512], isid_buf[32];
1880 ssize_t len = 0;
1881 int reg_count = 0;
Andy Grover3c8a6222013-05-16 10:40:58 -07001882 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001883
Andy Grover3c8a6222013-05-16 10:40:58 -07001884 spin_lock(&dev->dev_reservation_lock);
1885 spin_lock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001886 /*
1887 * Walk the registration list..
1888 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001889 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001890 pr_reg_list) {
1891
1892 tmp[0] = '\0';
1893 isid_buf[0] = '\0';
1894 tpg = pr_reg->pr_reg_nacl->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895 /*
1896 * Write out any ISID value to APTPL metadata that was included
1897 * in the original registration.
1898 */
1899 if (pr_reg->isid_present_at_reg)
1900 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1901 pr_reg->pr_reg_isid);
1902 /*
1903 * Include special metadata if the pr_reg matches the
1904 * reservation holder.
1905 */
1906 if (dev->dev_pr_res_holder == pr_reg) {
1907 snprintf(tmp, 512, "PR_REG_START: %d"
1908 "\ninitiator_fabric=%s\n"
1909 "initiator_node=%s\n%s"
1910 "sa_res_key=%llu\n"
1911 "res_holder=1\nres_type=%02x\n"
1912 "res_scope=%02x\nres_all_tg_pt=%d\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001913 "mapped_lun=%llu\n", reg_count,
David Disseldorp30c7ca92018-11-23 18:36:12 +01001914 tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001915 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1916 pr_reg->pr_res_key, pr_reg->pr_res_type,
1917 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1918 pr_reg->pr_res_mapped_lun);
1919 } else {
1920 snprintf(tmp, 512, "PR_REG_START: %d\n"
1921 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1922 "sa_res_key=%llu\nres_holder=0\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001923 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01001924 reg_count, tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1926 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1927 pr_reg->pr_res_mapped_lun);
1928 }
1929
Dan Carpenter60d645a2011-06-15 10:03:05 -07001930 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001931 pr_err("Unable to update renaming APTPL metadata,"
1932 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001933 ret = -EMSGSIZE;
1934 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001935 }
1936 len += sprintf(buf+len, "%s", tmp);
1937
1938 /*
1939 * Include information about the associated SCSI target port.
1940 */
1941 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001942 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01001943 " %d\n", tpg->se_tpg_tfo->fabric_name,
Andy Grovere3d6f902011-07-19 08:55:10 +00001944 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1945 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001946 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1947 reg_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948
Dan Carpenter60d645a2011-06-15 10:03:05 -07001949 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001950 pr_err("Unable to update renaming APTPL metadata,"
1951 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001952 ret = -EMSGSIZE;
1953 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954 }
1955 len += sprintf(buf+len, "%s", tmp);
1956 reg_count++;
1957 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958
Andy Grover6708bb22011-06-08 10:36:43 -07001959 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960 len += sprintf(buf+len, "No Registrations or Reservations");
1961
Andy Grover3c8a6222013-05-16 10:40:58 -07001962out:
1963 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001964 spin_unlock(&dev->dev_reservation_lock);
1965
1966 return ret;
1967}
1968
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969static int __core_scsi3_write_aptpl_to_file(
1970 struct se_device *dev,
Andy Grover4dee96f2013-05-16 10:41:00 -07001971 unsigned char *buf)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001972{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001973 struct t10_wwn *wwn = &dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001974 struct file *file;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001975 int flags = O_RDWR | O_CREAT | O_TRUNC;
David Disseldorpbdc79f02017-10-19 01:39:19 +02001976 char *path;
Andy Grover4dee96f2013-05-16 10:41:00 -07001977 u32 pr_aptpl_buf_len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001978 int ret;
Christoph Hellwige13ec932017-09-01 17:39:14 +02001979 loff_t pos = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001980
David Disseldorpbdc79f02017-10-19 01:39:19 +02001981 path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
1982 &wwn->unit_serial[0]);
1983 if (!path)
1984 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001985
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001986 file = filp_open(path, flags, 0600);
Al Viro0e9b10a2013-02-23 15:22:43 -05001987 if (IS_ERR(file)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001988 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001989 " failed\n", path);
David Disseldorpbdc79f02017-10-19 01:39:19 +02001990 kfree(path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001991 return PTR_ERR(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001992 }
1993
Andy Grover4dee96f2013-05-16 10:41:00 -07001994 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001995
Christoph Hellwige13ec932017-09-01 17:39:14 +02001996 ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001997
Al Viro0e9b10a2013-02-23 15:22:43 -05001998 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -07001999 pr_debug("Error writing APTPL metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05002000 fput(file);
David Disseldorpbdc79f02017-10-19 01:39:19 +02002001 kfree(path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002
Gera Kazakovf730f912013-09-09 15:47:06 -07002003 return (ret < 0) ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002004}
2005
Andy Grover459f2132013-05-16 10:41:02 -07002006/*
2007 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2008 * write out the updated metadata to struct file for this SCSI device.
2009 */
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002010static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002011{
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002012 unsigned char *buf;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002013 int rc, len = PR_APTPL_BUF_LEN;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002014
Andy Grover459f2132013-05-16 10:41:02 -07002015 if (!aptpl) {
2016 char *null_buf = "No Registrations or Reservations\n";
2017
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002018 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
Andy Grover459f2132013-05-16 10:41:02 -07002019 dev->t10_pr.pr_aptpl_active = 0;
2020 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
Andy Grover459f2132013-05-16 10:41:02 -07002021
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002022 if (rc)
2023 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover459f2132013-05-16 10:41:02 -07002024
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002025 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002026 }
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002027retry:
2028 buf = vzalloc(len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002029 if (!buf)
2030 return TCM_OUT_OF_RESOURCES;
2031
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002032 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002033 if (rc < 0) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002034 vfree(buf);
2035 len *= 2;
2036 goto retry;
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002037 }
2038
2039 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2040 if (rc != 0) {
2041 pr_err("SPC-3 PR: Could not update APTPL\n");
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002042 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002043 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2044 }
2045 dev->t10_pr.pr_aptpl_active = 1;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002046 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002047 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2048 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049}
2050
Christoph Hellwigde103c92012-11-06 12:24:09 -08002051static sense_reason_t
2052core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
Andy Groverbc118fe2013-05-16 10:41:04 -07002053 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002054{
Andy Grovere3d6f902011-07-19 08:55:10 +00002055 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002056 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002057 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002058 struct se_portal_group *se_tpg;
Andy Groverbc118fe2013-05-16 10:41:04 -07002059 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002060 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002061 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
Hannes Reineckea0d50f62012-12-17 09:53:34 +01002062 sense_reason_t ret = TCM_NO_SENSE;
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002063 int pr_holder = 0, type;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064
Andy Grover6708bb22011-06-08 10:36:43 -07002065 if (!se_sess || !se_lun) {
2066 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002067 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002068 }
2069 se_tpg = se_sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002070
Andy Grovere3d6f902011-07-19 08:55:10 +00002071 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002072 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002073 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002074 PR_REG_ISID_LEN);
2075 isid_ptr = &isid_buf[0];
2076 }
2077 /*
2078 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2079 */
Andy Groverbc118fe2013-05-16 10:41:04 -07002080 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2081 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002082 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002083 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002084 " for SA REGISTER, returning CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002085 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002086 }
2087 /*
2088 * Do nothing but return GOOD status.
2089 */
Andy Grover6708bb22011-06-08 10:36:43 -07002090 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002091 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002092
Andy Grover6708bb22011-06-08 10:36:43 -07002093 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002094 /*
2095 * Perform the Service Action REGISTER on the Initiator
2096 * Port Endpoint that the PRO was received from on the
2097 * Logical Unit of the SCSI device server.
2098 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002099 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00002100 se_sess->se_node_acl, cmd->se_lun,
2101 NULL, cmd->orig_fe_lun, isid_ptr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002102 sa_res_key, all_tg_pt, aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07002103 register_type, 0)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002104 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105 " struct t10_pr_registration\n");
tangwenji79dd6f22017-08-21 20:55:41 +08002106 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002107 }
2108 } else {
2109 /*
2110 * Register both the Initiator port that received
2111 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2112 * TransportID from Parameter list and loop through
2113 * fabric dependent parameter list while calling
2114 * logic from of core_scsi3_alloc_registration() for
2115 * each TransportID provided SCSI Initiator Port/Device
2116 */
2117 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2118 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2119 if (ret != 0)
2120 return ret;
2121 }
Andy Grover459f2132013-05-16 10:41:02 -07002122 return core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002123 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124
Andy Groverbc118fe2013-05-16 10:41:04 -07002125 /* ok, existing registration */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002126
Andy Groverbc118fe2013-05-16 10:41:04 -07002127 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2128 pr_err("SPC-3 PR REGISTER: Received"
2129 " res_key: 0x%016Lx does not match"
2130 " existing SA REGISTER res_key:"
2131 " 0x%016Lx\n", res_key,
2132 pr_reg->pr_res_key);
2133 ret = TCM_RESERVATION_CONFLICT;
2134 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002135 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002136
2137 if (spec_i_pt) {
Andy Grover1f070cc2013-05-16 10:40:56 -07002138 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2139 " set on a registered nexus\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002140 ret = TCM_INVALID_PARAMETER_LIST;
Andy Groverbc118fe2013-05-16 10:41:04 -07002141 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002142 }
2143
2144 /*
2145 * An existing ALL_TG_PT=1 registration being released
2146 * must also set ALL_TG_PT=1 in the incoming PROUT.
2147 */
Andy Grover1f070cc2013-05-16 10:40:56 -07002148 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2149 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
Christoph Hellwigde103c92012-11-06 12:24:09 -08002150 " registration exists, but ALL_TG_PT=1 bit not"
2151 " present in received PROUT\n");
2152 ret = TCM_INVALID_CDB_FIELD;
Andy Groverbc118fe2013-05-16 10:41:04 -07002153 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002154 }
2155
2156 /*
Andy Grover51d9c412013-05-16 10:41:03 -07002157 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
Christoph Hellwigde103c92012-11-06 12:24:09 -08002158 */
Andy Grover51d9c412013-05-16 10:41:03 -07002159 if (sa_res_key) {
2160 /*
2161 * Increment PRgeneration counter for struct se_device"
2162 * upon a successful REGISTER, see spc4r17 section 6.3.2
2163 * READ_KEYS service action.
2164 */
2165 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2166 pr_reg->pr_res_key = sa_res_key;
2167 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2168 " Key for %s to: 0x%016Lx PRgeneration:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01002169 " 0x%08x\n", cmd->se_tfo->fabric_name,
Andy Grover51d9c412013-05-16 10:41:03 -07002170 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2171 pr_reg->pr_reg_nacl->initiatorname,
2172 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2173
2174 } else {
2175 /*
2176 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2177 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002178 type = pr_reg->pr_res_type;
2179 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2180 pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002181 if (pr_holder < 0) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002182 ret = TCM_RESERVATION_CONFLICT;
Andy Groverbc118fe2013-05-16 10:41:04 -07002183 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002184 }
2185
2186 spin_lock(&pr_tmpl->registration_lock);
2187 /*
2188 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2189 * and matching pr_res_key.
2190 */
2191 if (pr_reg->pr_reg_all_tg_pt) {
2192 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2193 &pr_tmpl->registration_list,
2194 pr_reg_list) {
2195
2196 if (!pr_reg_p->pr_reg_all_tg_pt)
2197 continue;
2198 if (pr_reg_p->pr_res_key != res_key)
2199 continue;
2200 if (pr_reg == pr_reg_p)
2201 continue;
2202 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2203 pr_reg_p->pr_reg_nacl->initiatorname))
2204 continue;
2205
2206 __core_scsi3_free_registration(dev,
2207 pr_reg_p, NULL, 0);
2208 }
2209 }
2210
2211 /*
2212 * Release the calling I_T Nexus registration now..
2213 */
2214 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002215 pr_reg = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002216
2217 /*
2218 * From spc4r17, section 5.7.11.3 Unregistering
2219 *
2220 * If the persistent reservation is a registrants only
2221 * type, the device server shall establish a unit
2222 * attention condition for the initiator port associated
2223 * with every registered I_T nexus except for the I_T
2224 * nexus on which the PERSISTENT RESERVE OUT command was
2225 * received, with the additional sense code set to
2226 * RESERVATIONS RELEASED.
2227 */
2228 if (pr_holder &&
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002229 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2230 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002231 list_for_each_entry(pr_reg_p,
2232 &pr_tmpl->registration_list,
2233 pr_reg_list) {
2234
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002235 target_ua_allocate_lun(
Christoph Hellwigde103c92012-11-06 12:24:09 -08002236 pr_reg_p->pr_reg_nacl,
2237 pr_reg_p->pr_res_mapped_lun,
2238 0x2A,
2239 ASCQ_2AH_RESERVATIONS_RELEASED);
2240 }
2241 }
Andy Grover51d9c412013-05-16 10:41:03 -07002242
Christoph Hellwigde103c92012-11-06 12:24:09 -08002243 spin_unlock(&pr_tmpl->registration_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002244 }
2245
Andy Grover459f2132013-05-16 10:41:02 -07002246 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002247
Andy Groverbc118fe2013-05-16 10:41:04 -07002248out:
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002249 if (pr_reg)
2250 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002251 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002252}
2253
2254unsigned char *core_scsi3_pr_dump_type(int type)
2255{
2256 switch (type) {
2257 case PR_TYPE_WRITE_EXCLUSIVE:
2258 return "Write Exclusive Access";
2259 case PR_TYPE_EXCLUSIVE_ACCESS:
2260 return "Exclusive Access";
2261 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2262 return "Write Exclusive Access, Registrants Only";
2263 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2264 return "Exclusive Access, Registrants Only";
2265 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2266 return "Write Exclusive Access, All Registrants";
2267 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2268 return "Exclusive Access, All Registrants";
2269 default:
2270 break;
2271 }
2272
2273 return "Unknown SPC-3 PR Type";
2274}
2275
Christoph Hellwigde103c92012-11-06 12:24:09 -08002276static sense_reason_t
2277core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002278{
Christoph Hellwigde103c92012-11-06 12:24:09 -08002279 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002280 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002281 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002282 struct t10_pr_registration *pr_reg, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002283 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002284 char i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08002285 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002286
2287 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2288
Andy Grover6708bb22011-06-08 10:36:43 -07002289 if (!se_sess || !se_lun) {
2290 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002291 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002292 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002293 /*
2294 * Locate the existing *pr_reg via struct se_node_acl pointers
2295 */
Andy Grover5951146d2011-07-19 10:26:37 +00002296 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002297 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002298 if (!pr_reg) {
2299 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 " PR_REGISTERED *pr_reg for RESERVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002301 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302 }
2303 /*
2304 * From spc4r17 Section 5.7.9: Reserving:
2305 *
2306 * An application client creates a persistent reservation by issuing
2307 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2308 * a registered I_T nexus with the following parameters:
2309 * a) RESERVATION KEY set to the value of the reservation key that is
2310 * registered with the logical unit for the I_T nexus; and
2311 */
2312 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002313 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002314 " does not match existing SA REGISTER res_key:"
2315 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002316 ret = TCM_RESERVATION_CONFLICT;
2317 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002318 }
2319 /*
2320 * From spc4r17 Section 5.7.9: Reserving:
2321 *
2322 * From above:
2323 * b) TYPE field and SCOPE field set to the persistent reservation
2324 * being created.
2325 *
2326 * Only one persistent reservation is allowed at a time per logical unit
2327 * and that persistent reservation has a scope of LU_SCOPE.
2328 */
2329 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002330 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002331 ret = TCM_INVALID_PARAMETER_LIST;
2332 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002333 }
2334 /*
2335 * See if we have an existing PR reservation holder pointer at
2336 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2337 * *pr_res_holder.
2338 */
2339 spin_lock(&dev->dev_reservation_lock);
2340 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002341 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002342 /*
2343 * From spc4r17 Section 5.7.9: Reserving:
2344 *
2345 * If the device server receives a PERSISTENT RESERVE OUT
2346 * command from an I_T nexus other than a persistent reservation
2347 * holder (see 5.7.10) that attempts to create a persistent
2348 * reservation when a persistent reservation already exists for
2349 * the logical unit, then the command shall be completed with
2350 * RESERVATION CONFLICT status.
2351 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002352 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002353 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002354 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002355 " [%s]: %s while reservation already held by"
2356 " [%s]: %s, returning RESERVATION_CONFLICT\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002357 cmd->se_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002358 se_sess->se_node_acl->initiatorname,
David Disseldorp30c7ca92018-11-23 18:36:12 +01002359 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002360 pr_res_holder->pr_reg_nacl->initiatorname);
2361
2362 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002363 ret = TCM_RESERVATION_CONFLICT;
2364 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002365 }
2366 /*
2367 * From spc4r17 Section 5.7.9: Reserving:
2368 *
2369 * If a persistent reservation holder attempts to modify the
2370 * type or scope of an existing persistent reservation, the
2371 * command shall be completed with RESERVATION CONFLICT status.
2372 */
2373 if ((pr_res_holder->pr_res_type != type) ||
2374 (pr_res_holder->pr_res_scope != scope)) {
2375 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002376 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002377 " [%s]: %s trying to change TYPE and/or SCOPE,"
2378 " while reservation already held by [%s]: %s,"
2379 " returning RESERVATION_CONFLICT\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002380 cmd->se_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381 se_sess->se_node_acl->initiatorname,
David Disseldorp30c7ca92018-11-23 18:36:12 +01002382 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002383 pr_res_holder->pr_reg_nacl->initiatorname);
2384
2385 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002386 ret = TCM_RESERVATION_CONFLICT;
2387 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002388 }
2389 /*
2390 * From spc4r17 Section 5.7.9: Reserving:
2391 *
2392 * If the device server receives a PERSISTENT RESERVE OUT
2393 * command with RESERVE service action where the TYPE field and
2394 * the SCOPE field contain the same values as the existing type
2395 * and scope from a persistent reservation holder, it shall not
2396 * make any change to the existing persistent reservation and
2397 * shall completethe command with GOOD status.
2398 */
2399 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002400 ret = 0;
2401 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002402 }
2403 /*
2404 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2405 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2406 */
2407 pr_reg->pr_res_scope = scope;
2408 pr_reg->pr_res_type = type;
2409 pr_reg->pr_res_holder = 1;
2410 dev->dev_pr_res_holder = pr_reg;
Andy Groverd2843c12013-05-16 10:40:55 -07002411 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002412
Andy Grover6708bb22011-06-08 10:36:43 -07002413 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002414 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002415 cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002416 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002417 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002418 cmd->se_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 se_sess->se_node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002420 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002421 spin_unlock(&dev->dev_reservation_lock);
2422
Andy Grover459f2132013-05-16 10:41:02 -07002423 if (pr_tmpl->pr_aptpl_active)
2424 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002425
Christoph Hellwigde103c92012-11-06 12:24:09 -08002426 ret = 0;
2427out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002428 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002429 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002430}
2431
Christoph Hellwigde103c92012-11-06 12:24:09 -08002432static sense_reason_t
2433core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2434 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 switch (type) {
2437 case PR_TYPE_WRITE_EXCLUSIVE:
2438 case PR_TYPE_EXCLUSIVE_ACCESS:
2439 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2440 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2441 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2442 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08002443 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002444 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002445 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002446 " 0x%02x\n", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002447 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449}
2450
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002451static void __core_scsi3_complete_pro_release(
2452 struct se_device *dev,
2453 struct se_node_acl *se_nacl,
2454 struct t10_pr_registration *pr_reg,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002455 int explicit,
2456 int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002457{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002458 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002459 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002460 int pr_res_type = 0, pr_res_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002461
Bart Van Assche618baaf2019-01-25 10:34:53 -08002462 lockdep_assert_held(&dev->dev_reservation_lock);
2463
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002464 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002465 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002466 /*
2467 * Go ahead and release the current PR reservation holder.
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002468 * If an All Registrants reservation is currently active and
2469 * a unregister operation is requested, replace the current
2470 * dev_pr_res_holder with another active registration.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002471 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002472 if (dev->dev_pr_res_holder) {
2473 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2474 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2475 dev->dev_pr_res_holder->pr_res_type = 0;
2476 dev->dev_pr_res_holder->pr_res_scope = 0;
2477 dev->dev_pr_res_holder->pr_res_holder = 0;
2478 dev->dev_pr_res_holder = NULL;
2479 }
2480 if (!unreg)
2481 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002482
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002483 spin_lock(&dev->t10_pr.registration_lock);
2484 list_del_init(&pr_reg->pr_reg_list);
2485 /*
2486 * If the I_T nexus is a reservation holder, the persistent reservation
2487 * is of an all registrants type, and the I_T nexus is the last remaining
2488 * registered I_T nexus, then the device server shall also release the
2489 * persistent reservation.
2490 */
2491 if (!list_empty(&dev->t10_pr.registration_list) &&
2492 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2493 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2494 dev->dev_pr_res_holder =
2495 list_entry(dev->t10_pr.registration_list.next,
2496 struct t10_pr_registration, pr_reg_list);
2497 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2498 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2499 dev->dev_pr_res_holder->pr_res_holder = 1;
2500 }
2501 spin_unlock(&dev->t10_pr.registration_lock);
2502out:
2503 if (!dev->dev_pr_res_holder) {
2504 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2505 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002506 tfo->fabric_name, (explicit) ? "explicit" :
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002507 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2508 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2509 }
Andy Grover6708bb22011-06-08 10:36:43 -07002510 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002511 tfo->fabric_name, se_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002512 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002513 /*
2514 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2515 */
2516 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2517}
2518
Christoph Hellwigde103c92012-11-06 12:24:09 -08002519static sense_reason_t
2520core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2521 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002522{
2523 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002524 struct se_session *se_sess = cmd->se_sess;
2525 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002526 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002527 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002528 sense_reason_t ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002529
Andy Grover6708bb22011-06-08 10:36:43 -07002530 if (!se_sess || !se_lun) {
2531 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002532 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002533 }
2534 /*
2535 * Locate the existing *pr_reg via struct se_node_acl pointers
2536 */
2537 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002538 if (!pr_reg) {
2539 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 " PR_REGISTERED *pr_reg for RELEASE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002541 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542 }
2543 /*
2544 * From spc4r17 Section 5.7.11.2 Releasing:
2545 *
2546 * If there is no persistent reservation or in response to a persistent
2547 * reservation release request from a registered I_T nexus that is not a
2548 * persistent reservation holder (see 5.7.10), the device server shall
2549 * do the following:
2550 *
2551 * a) Not release the persistent reservation, if any;
2552 * b) Not remove any registrations; and
2553 * c) Complete the command with GOOD status.
2554 */
2555 spin_lock(&dev->dev_reservation_lock);
2556 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002557 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002558 /*
2559 * No persistent reservation, return GOOD status.
2560 */
2561 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerbb7a8c82012-11-06 15:46:25 -08002562 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002563 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002564
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002565 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 * Release request from a registered I_T nexus that is not a
2568 * persistent reservation holder. return GOOD status.
2569 */
2570 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002571 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002572 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002573
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002574 /*
2575 * From spc4r17 Section 5.7.11.2 Releasing:
2576 *
2577 * Only the persistent reservation holder (see 5.7.10) is allowed to
2578 * release a persistent reservation.
2579 *
2580 * An application client releases the persistent reservation by issuing
2581 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2582 * an I_T nexus that is a persistent reservation holder with the
2583 * following parameters:
2584 *
2585 * a) RESERVATION KEY field set to the value of the reservation key
2586 * that is registered with the logical unit for the I_T nexus;
2587 */
2588 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002589 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002590 " does not match existing SA REGISTER res_key:"
2591 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2592 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002593 ret = TCM_RESERVATION_CONFLICT;
2594 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002595 }
2596 /*
2597 * From spc4r17 Section 5.7.11.2 Releasing and above:
2598 *
2599 * b) TYPE field and SCOPE field set to match the persistent
2600 * reservation being released.
2601 */
2602 if ((pr_res_holder->pr_res_type != type) ||
2603 (pr_res_holder->pr_res_scope != scope)) {
2604 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002605 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002606 " reservation from [%s]: %s with different TYPE "
2607 "and/or SCOPE while reservation already held by"
2608 " [%s]: %s, returning RESERVATION_CONFLICT\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002609 cmd->se_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 se_sess->se_node_acl->initiatorname,
David Disseldorp30c7ca92018-11-23 18:36:12 +01002611 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002612 pr_res_holder->pr_reg_nacl->initiatorname);
2613
2614 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002615 ret = TCM_RESERVATION_CONFLICT;
2616 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002617 }
2618 /*
2619 * In response to a persistent reservation release request from the
2620 * persistent reservation holder the device server shall perform a
2621 * release by doing the following as an uninterrupted series of actions:
2622 * a) Release the persistent reservation;
2623 * b) Not remove any registration(s);
2624 * c) If the released persistent reservation is a registrants only type
2625 * or all registrants type persistent reservation,
2626 * the device server shall establish a unit attention condition for
2627 * the initiator port associated with every regis-
2628 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2629 * RESERVE OUT command with RELEASE service action was received,
2630 * with the additional sense code set to RESERVATIONS RELEASED; and
2631 * d) If the persistent reservation is of any other type, the device
2632 * server shall not establish a unit attention condition.
2633 */
2634 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002635 pr_reg, 1, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002636
2637 spin_unlock(&dev->dev_reservation_lock);
2638
2639 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2640 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2641 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2642 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2643 /*
2644 * If no UNIT ATTENTION conditions will be established for
2645 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2646 * go ahead and check for APTPL=1 update+write below
2647 */
2648 goto write_aptpl;
2649 }
2650
2651 spin_lock(&pr_tmpl->registration_lock);
2652 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2653 pr_reg_list) {
2654 /*
2655 * Do not establish a UNIT ATTENTION condition
2656 * for the calling I_T Nexus
2657 */
2658 if (pr_reg_p == pr_reg)
2659 continue;
2660
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002661 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002662 pr_reg_p->pr_res_mapped_lun,
2663 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2664 }
2665 spin_unlock(&pr_tmpl->registration_lock);
2666
2667write_aptpl:
Andy Grover459f2132013-05-16 10:41:02 -07002668 if (pr_tmpl->pr_aptpl_active)
2669 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2670
Christoph Hellwigde103c92012-11-06 12:24:09 -08002671out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002673 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002674}
2675
Christoph Hellwigde103c92012-11-06 12:24:09 -08002676static sense_reason_t
2677core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678{
2679 struct se_device *dev = cmd->se_dev;
2680 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002681 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002682 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002683 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002684 u64 pr_res_mapped_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002685 int calling_it_nexus = 0;
2686 /*
2687 * Locate the existing *pr_reg via struct se_node_acl pointers
2688 */
Andy Grover5951146d2011-07-19 10:26:37 +00002689 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002690 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002691 if (!pr_reg_n) {
2692 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002693 " PR_REGISTERED *pr_reg for CLEAR\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002694 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002695 }
2696 /*
2697 * From spc4r17 section 5.7.11.6, Clearing:
2698 *
2699 * Any application client may release the persistent reservation and
2700 * remove all registrations from a device server by issuing a
2701 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2702 * registered I_T nexus with the following parameter:
2703 *
2704 * a) RESERVATION KEY field set to the value of the reservation key
2705 * that is registered with the logical unit for the I_T nexus.
2706 */
2707 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002708 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002709 " res_key: 0x%016Lx does not match"
2710 " existing SA REGISTER res_key:"
2711 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2712 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002713 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002714 }
2715 /*
2716 * a) Release the persistent reservation, if any;
2717 */
2718 spin_lock(&dev->dev_reservation_lock);
2719 pr_res_holder = dev->dev_pr_res_holder;
2720 if (pr_res_holder) {
2721 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2722 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002723 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724 }
2725 spin_unlock(&dev->dev_reservation_lock);
2726 /*
2727 * b) Remove all registration(s) (see spc4r17 5.7.7);
2728 */
2729 spin_lock(&pr_tmpl->registration_lock);
2730 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2731 &pr_tmpl->registration_list, pr_reg_list) {
2732
2733 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2734 pr_reg_nacl = pr_reg->pr_reg_nacl;
2735 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2736 __core_scsi3_free_registration(dev, pr_reg, NULL,
2737 calling_it_nexus);
2738 /*
2739 * e) Establish a unit attention condition for the initiator
2740 * port associated with every registered I_T nexus other
2741 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2742 * command with CLEAR service action was received, with the
2743 * additional sense code set to RESERVATIONS PREEMPTED.
2744 */
Andy Grover6708bb22011-06-08 10:36:43 -07002745 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002746 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002747 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2748 }
2749 spin_unlock(&pr_tmpl->registration_lock);
2750
Andy Grover6708bb22011-06-08 10:36:43 -07002751 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002752 cmd->se_tfo->fabric_name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002753
Andy Grover459f2132013-05-16 10:41:02 -07002754 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755
2756 core_scsi3_pr_generation(dev);
2757 return 0;
2758}
2759
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002760static void __core_scsi3_complete_pro_preempt(
2761 struct se_device *dev,
2762 struct t10_pr_registration *pr_reg,
2763 struct list_head *preempt_and_abort_list,
2764 int type,
2765 int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07002766 enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767{
2768 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002769 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002770 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002771
Bart Van Assche618baaf2019-01-25 10:34:53 -08002772 lockdep_assert_held(&dev->dev_reservation_lock);
2773
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002774 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002775 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002777 * Do an implicit RELEASE of the existing reservation.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778 */
2779 if (dev->dev_pr_res_holder)
2780 __core_scsi3_complete_pro_release(dev, nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002781 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002782
2783 dev->dev_pr_res_holder = pr_reg;
2784 pr_reg->pr_res_holder = 1;
2785 pr_reg->pr_res_type = type;
2786 pr_reg->pr_res_scope = scope;
2787
Andy Grover6708bb22011-06-08 10:36:43 -07002788 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002790 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002791 core_scsi3_pr_dump_type(type),
2792 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002793 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01002794 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Andy Groverd2843c12013-05-16 10:40:55 -07002795 nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002796 /*
2797 * For PREEMPT_AND_ABORT, add the preempting reservation's
2798 * struct t10_pr_registration to the list that will be compared
2799 * against received CDBs..
2800 */
2801 if (preempt_and_abort_list)
2802 list_add_tail(&pr_reg->pr_reg_abort_list,
2803 preempt_and_abort_list);
2804}
2805
2806static void core_scsi3_release_preempt_and_abort(
2807 struct list_head *preempt_and_abort_list,
2808 struct t10_pr_registration *pr_reg_holder)
2809{
2810 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2811
2812 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2813 pr_reg_abort_list) {
2814
2815 list_del(&pr_reg->pr_reg_abort_list);
2816 if (pr_reg_holder == pr_reg)
2817 continue;
2818 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002819 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002820 continue;
2821 }
2822
2823 pr_reg->pr_reg_deve = NULL;
2824 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2826 }
2827}
2828
Christoph Hellwigde103c92012-11-06 12:24:09 -08002829static sense_reason_t
2830core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07002831 u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002832{
Andy Grover5951146d2011-07-19 10:26:37 +00002833 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002834 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002835 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08002836 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002837 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002838 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002839 u64 pr_res_mapped_lun = 0;
Steven Allenb6932ee2014-10-24 15:18:26 -07002840 int all_reg = 0, calling_it_nexus = 0;
2841 bool sa_res_key_unmatched = sa_res_key != 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002842 int prh_type = 0, prh_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843
Christoph Hellwigde103c92012-11-06 12:24:09 -08002844 if (!se_sess)
2845 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846
Andy Grover5951146d2011-07-19 10:26:37 +00002847 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002849 if (!pr_reg_n) {
2850 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002852 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002853 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854 }
2855 if (pr_reg_n->pr_res_key != res_key) {
2856 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002857 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002858 }
2859 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002860 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002862 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002863 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864
2865 spin_lock(&dev->dev_reservation_lock);
2866 pr_res_holder = dev->dev_pr_res_holder;
2867 if (pr_res_holder &&
2868 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2869 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2870 all_reg = 1;
2871
Andy Grover6708bb22011-06-08 10:36:43 -07002872 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 spin_unlock(&dev->dev_reservation_lock);
2874 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002875 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002876 }
2877 /*
2878 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2879 *
2880 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2881 * persistent reservation holder or there is no persistent reservation
2882 * holder (i.e., there is no persistent reservation), then the device
2883 * server shall perform a preempt by doing the following in an
2884 * uninterrupted series of actions. (See below..)
2885 */
Andy Grover6708bb22011-06-08 10:36:43 -07002886 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 /*
2888 * No existing or SA Reservation Key matching reservations..
2889 *
2890 * PROUT SA PREEMPT with All Registrant type reservations are
2891 * allowed to be processed without a matching SA Reservation Key
2892 */
2893 spin_lock(&pr_tmpl->registration_lock);
2894 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2895 &pr_tmpl->registration_list, pr_reg_list) {
2896 /*
2897 * Removing of registrations in non all registrants
2898 * type reservations without a matching SA reservation
2899 * key.
2900 *
2901 * a) Remove the registrations for all I_T nexuses
2902 * specified by the SERVICE ACTION RESERVATION KEY
2903 * field;
2904 * b) Ignore the contents of the SCOPE and TYPE fields;
2905 * c) Process tasks as defined in 5.7.1; and
2906 * d) Establish a unit attention condition for the
2907 * initiator port associated with every I_T nexus
2908 * that lost its registration other than the I_T
2909 * nexus on which the PERSISTENT RESERVE OUT command
2910 * was received, with the additional sense code set
2911 * to REGISTRATIONS PREEMPTED.
2912 */
Andy Grover6708bb22011-06-08 10:36:43 -07002913 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 if (pr_reg->pr_res_key != sa_res_key)
2915 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002916 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002917
2918 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2919 pr_reg_nacl = pr_reg->pr_reg_nacl;
2920 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2921 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002922 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923 NULL, calling_it_nexus);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002924 } else {
2925 /*
2926 * Case for any existing all registrants type
2927 * reservation, follow logic in spc4r17 section
2928 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2929 *
2930 * For a ZERO SA Reservation key, release
Hannes Reinecke125d0112013-11-19 09:07:46 +01002931 * all other registrations and do an implicit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002932 * release of active persistent reservation.
2933 *
2934 * For a non-ZERO SA Reservation key, only
2935 * release the matching reservation key from
2936 * registrations.
2937 */
2938 if ((sa_res_key) &&
2939 (pr_reg->pr_res_key != sa_res_key))
2940 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002941 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002942
2943 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2944 if (calling_it_nexus)
2945 continue;
2946
2947 pr_reg_nacl = pr_reg->pr_reg_nacl;
2948 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2949 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002950 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002951 NULL, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002952 }
Andy Grover6708bb22011-06-08 10:36:43 -07002953 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002954 target_ua_allocate_lun(pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002955 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08002956 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957 }
2958 spin_unlock(&pr_tmpl->registration_lock);
2959 /*
2960 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2961 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2962 * RESERVATION KEY field to a value that does not match any
2963 * registered reservation key, then the device server shall
2964 * complete the command with RESERVATION CONFLICT status.
2965 */
Steven Allenb6932ee2014-10-24 15:18:26 -07002966 if (sa_res_key_unmatched) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002967 spin_unlock(&dev->dev_reservation_lock);
2968 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002969 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002970 }
2971 /*
2972 * For an existing all registrants type reservation
2973 * with a zero SA rservation key, preempt the existing
2974 * reservation with the new PR type and scope.
2975 */
2976 if (pr_res_holder && all_reg && !(sa_res_key)) {
2977 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07002978 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2979 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980
Andy Grover33ce6a82013-05-16 10:40:54 -07002981 if (preempt_type == PREEMPT_AND_ABORT)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 core_scsi3_release_preempt_and_abort(
2983 &preempt_and_abort_list, pr_reg_n);
2984 }
2985 spin_unlock(&dev->dev_reservation_lock);
2986
Andy Grover459f2132013-05-16 10:41:02 -07002987 if (pr_tmpl->pr_aptpl_active)
2988 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002989
2990 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00002991 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 return 0;
2993 }
2994 /*
2995 * The PREEMPTing SA reservation key matches that of the
2996 * existing persistent reservation, first, we check if
2997 * we are preempting our own reservation.
2998 * From spc4r17, section 5.7.11.4.3 Preempting
2999 * persistent reservations and registration handling
3000 *
3001 * If an all registrants persistent reservation is not
3002 * present, it is not an error for the persistent
3003 * reservation holder to preempt itself (i.e., a
3004 * PERSISTENT RESERVE OUT with a PREEMPT service action
3005 * or a PREEMPT AND ABORT service action with the
3006 * SERVICE ACTION RESERVATION KEY value equal to the
3007 * persistent reservation holder's reservation key that
3008 * is received from the persistent reservation holder).
3009 * In that case, the device server shall establish the
3010 * new persistent reservation and maintain the
3011 * registration.
3012 */
3013 prh_type = pr_res_holder->pr_res_type;
3014 prh_scope = pr_res_holder->pr_res_scope;
3015 /*
3016 * If the SERVICE ACTION RESERVATION KEY field identifies a
3017 * persistent reservation holder (see 5.7.10), the device
3018 * server shall perform a preempt by doing the following as
3019 * an uninterrupted series of actions:
3020 *
3021 * a) Release the persistent reservation for the holder
3022 * identified by the SERVICE ACTION RESERVATION KEY field;
3023 */
3024 if (pr_reg_n != pr_res_holder)
3025 __core_scsi3_complete_pro_release(dev,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08003026 pr_res_holder->pr_reg_nacl,
3027 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003028 /*
3029 * b) Remove the registrations for all I_T nexuses identified
3030 * by the SERVICE ACTION RESERVATION KEY field, except the
3031 * I_T nexus that is being used for the PERSISTENT RESERVE
3032 * OUT command. If an all registrants persistent reservation
3033 * is present and the SERVICE ACTION RESERVATION KEY field
3034 * is set to zero, then all registrations shall be removed
3035 * except for that of the I_T nexus that is being used for
3036 * the PERSISTENT RESERVE OUT command;
3037 */
3038 spin_lock(&pr_tmpl->registration_lock);
3039 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3040 &pr_tmpl->registration_list, pr_reg_list) {
3041
3042 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3043 if (calling_it_nexus)
3044 continue;
3045
3046 if (pr_reg->pr_res_key != sa_res_key)
3047 continue;
3048
3049 pr_reg_nacl = pr_reg->pr_reg_nacl;
3050 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3051 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07003052 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003053 calling_it_nexus);
3054 /*
3055 * e) Establish a unit attention condition for the initiator
3056 * port associated with every I_T nexus that lost its
3057 * persistent reservation and/or registration, with the
3058 * additional sense code set to REGISTRATIONS PREEMPTED;
3059 */
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003060 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003061 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003062 }
3063 spin_unlock(&pr_tmpl->registration_lock);
3064 /*
3065 * c) Establish a persistent reservation for the preempting
3066 * I_T nexus using the contents of the SCOPE and TYPE fields;
3067 */
3068 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07003069 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3070 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003071 /*
3072 * d) Process tasks as defined in 5.7.1;
3073 * e) See above..
3074 * f) If the type or scope has changed, then for every I_T nexus
3075 * whose reservation key was not removed, except for the I_T
3076 * nexus on which the PERSISTENT RESERVE OUT command was
3077 * received, the device server shall establish a unit
3078 * attention condition for the initiator port associated with
3079 * that I_T nexus, with the additional sense code set to
3080 * RESERVATIONS RELEASED. If the type or scope have not
3081 * changed, then no unit attention condition(s) shall be
3082 * established for this reason.
3083 */
3084 if ((prh_type != type) || (prh_scope != scope)) {
3085 spin_lock(&pr_tmpl->registration_lock);
3086 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3087 &pr_tmpl->registration_list, pr_reg_list) {
3088
3089 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3090 if (calling_it_nexus)
3091 continue;
3092
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003093 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003094 pr_reg->pr_res_mapped_lun, 0x2A,
3095 ASCQ_2AH_RESERVATIONS_RELEASED);
3096 }
3097 spin_unlock(&pr_tmpl->registration_lock);
3098 }
3099 spin_unlock(&dev->dev_reservation_lock);
3100 /*
3101 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3102 * All received CDBs for the matching existing reservation and
3103 * registrations undergo ABORT_TASK logic.
3104 *
3105 * From there, core_scsi3_release_preempt_and_abort() will
3106 * release every registration in the list (which have already
3107 * been removed from the primary pr_reg list), except the
3108 * new persistent reservation holder, the calling Initiator Port.
3109 */
Andy Grover33ce6a82013-05-16 10:40:54 -07003110 if (preempt_type == PREEMPT_AND_ABORT) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003111 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3112 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3113 pr_reg_n);
3114 }
3115
Andy Grover459f2132013-05-16 10:41:02 -07003116 if (pr_tmpl->pr_aptpl_active)
3117 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003118
3119 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003120 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121 return 0;
3122}
3123
Christoph Hellwigde103c92012-11-06 12:24:09 -08003124static sense_reason_t
3125core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003126 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003127{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 switch (type) {
3129 case PR_TYPE_WRITE_EXCLUSIVE:
3130 case PR_TYPE_EXCLUSIVE_ACCESS:
3131 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3132 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3133 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3134 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08003135 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07003136 sa_res_key, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003137 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003138 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Andy Grover33ce6a82013-05-16 10:40:54 -07003139 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003140 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003141 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142}
3143
3144
Christoph Hellwigde103c92012-11-06 12:24:09 -08003145static sense_reason_t
3146core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3147 u64 sa_res_key, int aptpl, int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003148{
Andy Grovere3d6f902011-07-19 08:55:10 +00003149 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003150 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003151 struct se_dev_entry *dest_se_deve = NULL;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003152 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003153 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003155 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003156 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003157 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003158 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003159 const unsigned char *initiator_str;
Julia Lawall13ba5642014-11-30 19:14:12 +01003160 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161 u32 tid_len, tmp_tid_len;
Andy Groverd2843c12013-05-16 10:40:55 -07003162 int new_reg = 0, type, scope, matching_iname;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003163 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 unsigned short rtpi;
3165 unsigned char proto_ident;
3166
Andy Grover6708bb22011-06-08 10:36:43 -07003167 if (!se_sess || !se_lun) {
3168 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003169 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003170 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003171
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003172 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3173 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003174 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175 /*
3176 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3177 * Register behaviors for a REGISTER AND MOVE service action
3178 *
3179 * Locate the existing *pr_reg via struct se_node_acl pointers
3180 */
Andy Grover5951146d2011-07-19 10:26:37 +00003181 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003182 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003183 if (!pr_reg) {
3184 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 " *pr_reg for REGISTER_AND_MOVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003186 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 }
3188 /*
3189 * The provided reservation key much match the existing reservation key
3190 * provided during this initiator's I_T nexus registration.
3191 */
3192 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003193 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003194 " res_key: 0x%016Lx does not match existing SA REGISTER"
3195 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003196 ret = TCM_RESERVATION_CONFLICT;
3197 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 }
3199 /*
3200 * The service active reservation key needs to be non zero
3201 */
Andy Grover6708bb22011-06-08 10:36:43 -07003202 if (!sa_res_key) {
3203 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003204 " sa_res_key\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003205 ret = TCM_INVALID_PARAMETER_LIST;
3206 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003207 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003208
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003209 /*
3210 * Determine the Relative Target Port Identifier where the reservation
3211 * will be moved to for the TransportID containing SCSI initiator WWN
3212 * information.
3213 */
Andy Grover49493142012-01-16 16:57:08 -08003214 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003215 if (!buf) {
tangwenji79dd6f22017-08-21 20:55:41 +08003216 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003217 goto out_put_pr_reg;
3218 }
3219
Bart Van Asschea85d6672017-05-23 16:48:27 -07003220 rtpi = get_unaligned_be16(&buf[18]);
3221 tid_len = get_unaligned_be32(&buf[20]);
Andy Grover49493142012-01-16 16:57:08 -08003222 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003223 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003224
3225 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003226 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003227 " does not equal CDB data_length: %u\n", tid_len,
3228 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003229 ret = TCM_INVALID_PARAMETER_LIST;
3230 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003231 }
3232
3233 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003234 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3235 if (tmp_lun->lun_rtpi != rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003236 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003237 dest_se_tpg = tmp_lun->lun_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003238 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003239 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003240 continue;
3241
Joern Engel33940d02014-09-16 16:23:12 -04003242 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003243 spin_unlock(&dev->se_port_lock);
3244
Christoph Hellwigde103c92012-11-06 12:24:09 -08003245 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003246 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003247 " for dest_se_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04003248 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003249 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3250 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003251 }
3252
3253 spin_lock(&dev->se_port_lock);
3254 break;
3255 }
3256 spin_unlock(&dev->se_port_lock);
3257
Andy Grover6708bb22011-06-08 10:36:43 -07003258 if (!dest_se_tpg || !dest_tf_ops) {
3259 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003260 " fabric ops from Relative Target Port Identifier:"
3261 " %hu\n", rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003262 ret = TCM_INVALID_PARAMETER_LIST;
3263 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003264 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003265
Andy Grover49493142012-01-16 16:57:08 -08003266 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003267 if (!buf) {
tangwenji79dd6f22017-08-21 20:55:41 +08003268 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003269 goto out_put_pr_reg;
3270 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003271 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003272
Andy Grover6708bb22011-06-08 10:36:43 -07003273 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003274 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003275
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003276 if (proto_ident != dest_se_tpg->proto_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07003277 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003278 " proto_ident: 0x%02x does not match ident: 0x%02x"
3279 " from fabric: %s\n", proto_ident,
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003280 dest_se_tpg->proto_id,
David Disseldorp30c7ca92018-11-23 18:36:12 +01003281 dest_tf_ops->fabric_name);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003282 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003283 goto out;
3284 }
Christoph Hellwig2650d712015-05-01 17:47:58 +02003285 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
Rasmus Villemoes094bb5d2017-11-21 01:12:43 +01003286 &buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003287 if (!initiator_str) {
3288 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003289 " initiator_str from Transport ID\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003290 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003291 goto out;
3292 }
3293
Andy Grover49493142012-01-16 16:57:08 -08003294 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003295 buf = NULL;
3296
Andy Grover6708bb22011-06-08 10:36:43 -07003297 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
David Disseldorp30c7ca92018-11-23 18:36:12 +01003298 " %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003299 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3300 iport_ptr : "");
3301 /*
3302 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3303 * action specifies a TransportID that is the same as the initiator port
3304 * of the I_T nexus for the command received, then the command shall
3305 * be terminated with CHECK CONDITION status, with the sense key set to
3306 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3307 * IN PARAMETER LIST.
3308 */
3309 pr_reg_nacl = pr_reg->pr_reg_nacl;
3310 matching_iname = (!strcmp(initiator_str,
3311 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003312 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003313 goto after_iport_check;
3314
Andy Grover6708bb22011-06-08 10:36:43 -07003315 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3316 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003317 " matches: %s on received I_T Nexus\n", initiator_str,
3318 pr_reg_nacl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003319 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003320 goto out;
3321 }
Andy Grover6708bb22011-06-08 10:36:43 -07003322 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3323 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003324 " matches: %s %s on received I_T Nexus\n",
3325 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3326 pr_reg->pr_reg_isid);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003327 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003328 goto out;
3329 }
3330after_iport_check:
3331 /*
3332 * Locate the destination struct se_node_acl from the received Transport ID
3333 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003334 mutex_lock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003335 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3336 initiator_str);
Joern Engel33940d02014-09-16 16:23:12 -04003337 if (dest_node_acl)
3338 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003339 mutex_unlock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003340
Andy Grover6708bb22011-06-08 10:36:43 -07003341 if (!dest_node_acl) {
3342 pr_err("Unable to locate %s dest_node_acl for"
David Disseldorp30c7ca92018-11-23 18:36:12 +01003343 " TransportID%s\n", dest_tf_ops->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003344 initiator_str);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003345 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003346 goto out;
3347 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003348
3349 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003350 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003351 " dest_node_acl\n");
Joern Engel33940d02014-09-16 16:23:12 -04003352 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003353 dest_node_acl = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003354 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003355 goto out;
3356 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003357
Andy Grover6708bb22011-06-08 10:36:43 -07003358 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01003359 " %s from TransportID\n", dest_tf_ops->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003360 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003361
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003362 /*
3363 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3364 * PORT IDENTIFIER.
3365 */
3366 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003367 if (!dest_se_deve) {
3368 pr_err("Unable to locate %s dest_se_deve from RTPI:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01003369 " %hu\n", dest_tf_ops->fabric_name, rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003370 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003371 goto out;
3372 }
3373
Christoph Hellwigde103c92012-11-06 12:24:09 -08003374 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003375 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07003376 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003377 dest_se_deve = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003378 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003379 goto out;
3380 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003381
Andy Grover6708bb22011-06-08 10:36:43 -07003382 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Hannes Reineckef2d30682015-06-10 08:41:22 +02003383 " ACL for dest_se_deve->mapped_lun: %llu\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01003384 dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003385 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003386
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003387 /*
3388 * A persistent reservation needs to already existing in order to
3389 * successfully complete the REGISTER_AND_MOVE service action..
3390 */
3391 spin_lock(&dev->dev_reservation_lock);
3392 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003393 if (!pr_res_holder) {
3394 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003395 " currently held\n");
3396 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003397 ret = TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003398 goto out;
3399 }
3400 /*
3401 * The received on I_T Nexus must be the reservation holder.
3402 *
3403 * From spc4r17 section 5.7.8 Table 50 --
3404 * Register behaviors for a REGISTER AND MOVE service action
3405 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02003406 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003407 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003408 " Nexus is not reservation holder\n");
3409 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003410 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003411 goto out;
3412 }
3413 /*
3414 * From spc4r17 section 5.7.8: registering and moving reservation
3415 *
3416 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3417 * action is received and the established persistent reservation is a
3418 * Write Exclusive - All Registrants type or Exclusive Access -
3419 * All Registrants type reservation, then the command shall be completed
3420 * with RESERVATION CONFLICT status.
3421 */
3422 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3423 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003424 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003425 " reservation for type: %s\n",
3426 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3427 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003428 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003429 goto out;
3430 }
3431 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3432 /*
3433 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3434 */
3435 type = pr_res_holder->pr_res_type;
3436 scope = pr_res_holder->pr_res_type;
3437 /*
3438 * c) Associate the reservation key specified in the SERVICE ACTION
3439 * RESERVATION KEY field with the I_T nexus specified as the
3440 * destination of the register and move, where:
3441 * A) The I_T nexus is specified by the TransportID and the
3442 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3443 * B) Regardless of the TransportID format used, the association for
3444 * the initiator port is based on either the initiator port name
3445 * (see 3.1.71) on SCSI transport protocols where port names are
3446 * required or the initiator port identifier (see 3.1.70) on SCSI
3447 * transport protocols where port names are not required;
3448 * d) Register the reservation key specified in the SERVICE ACTION
3449 * RESERVATION KEY field;
3450 * e) Retain the reservation key specified in the SERVICE ACTION
3451 * RESERVATION KEY field and associated information;
3452 *
3453 * Also, It is not an error for a REGISTER AND MOVE service action to
3454 * register an I_T nexus that is already registered with the same
3455 * reservation key or a different reservation key.
3456 */
3457 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3458 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003459 if (!dest_pr_reg) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003460 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003461 kref_read(&dest_se_deve->pr_kref) != 0);
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003462
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003463 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003464 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3465 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3466 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
tangwenji79dd6f22017-08-21 20:55:41 +08003467 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003468 goto out;
3469 }
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003470 spin_lock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003471 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3472 iport_ptr);
3473 new_reg = 1;
3474 }
3475 /*
3476 * f) Release the persistent reservation for the persistent reservation
3477 * holder (i.e., the I_T nexus on which the
3478 */
3479 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08003480 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003481 /*
3482 * g) Move the persistent reservation to the specified I_T nexus using
3483 * the same scope and type as the persistent reservation released in
3484 * item f); and
3485 */
3486 dev->dev_pr_res_holder = dest_pr_reg;
3487 dest_pr_reg->pr_res_holder = 1;
3488 dest_pr_reg->pr_res_type = type;
3489 pr_reg->pr_res_scope = scope;
Andy Groverd2843c12013-05-16 10:40:55 -07003490 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003491 /*
3492 * Increment PRGeneration for existing registrations..
3493 */
Andy Grover6708bb22011-06-08 10:36:43 -07003494 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003495 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3496 spin_unlock(&dev->dev_reservation_lock);
3497
Andy Grover6708bb22011-06-08 10:36:43 -07003498 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003499 " created new reservation holder TYPE: %s on object RTPI:"
David Disseldorp30c7ca92018-11-23 18:36:12 +01003500 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003501 core_scsi3_pr_dump_type(type), rtpi,
3502 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003503 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003504 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
David Disseldorp30c7ca92018-11-23 18:36:12 +01003505 tf_ops->fabric_name, pr_reg_nacl->initiatorname,
3506 i_buf, dest_tf_ops->fabric_name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003507 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3508 iport_ptr : "");
3509 /*
3510 * It is now safe to release configfs group dependencies for destination
3511 * of Transport ID Initiator Device/Port Identifier
3512 */
3513 core_scsi3_lunacl_undepend_item(dest_se_deve);
3514 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3515 core_scsi3_tpg_undepend_item(dest_se_tpg);
3516 /*
3517 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3518 * nexus on which PERSISTENT RESERVE OUT command was received.
3519 */
3520 if (unreg) {
3521 spin_lock(&pr_tmpl->registration_lock);
3522 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3523 spin_unlock(&pr_tmpl->registration_lock);
3524 } else
3525 core_scsi3_put_pr_reg(pr_reg);
3526
Andy Grover459f2132013-05-16 10:41:02 -07003527 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003528
3529 core_scsi3_put_pr_reg(dest_pr_reg);
3530 return 0;
3531out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003532 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003533 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003534 if (dest_se_deve)
3535 core_scsi3_lunacl_undepend_item(dest_se_deve);
3536 if (dest_node_acl)
3537 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3538 core_scsi3_tpg_undepend_item(dest_se_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003539
3540out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003541 core_scsi3_put_pr_reg(pr_reg);
3542 return ret;
3543}
3544
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003545/*
3546 * See spc4r17 section 6.14 Table 170
3547 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003548sense_reason_t
3549target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003550{
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003551 struct se_device *dev = cmd->se_dev;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003552 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003553 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003554 u64 res_key, sa_res_key;
3555 int sa, scope, type, aptpl;
3556 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003557 sense_reason_t ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003558
3559 /*
3560 * Following spc2r20 5.5.1 Reservations overview:
3561 *
3562 * If a logical unit has been reserved by any RESERVE command and is
3563 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3564 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3565 * initiator or service action and shall terminate with a RESERVATION
3566 * CONFLICT status.
3567 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003568 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003569 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3570 " SPC-2 reservation is held, returning"
3571 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003572 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003573 }
3574
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003575 /*
3576 * FIXME: A NULL struct se_session pointer means an this is not coming from
3577 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3578 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003579 if (!cmd->se_sess)
3580 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003581
3582 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003583 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003584 " length too small: %u\n", cmd->data_length);
Tang Wenji7ee00312017-07-08 11:28:25 +08003585 return TCM_PARAMETER_LIST_LENGTH_ERROR;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003586 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003587
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003588 /*
3589 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3590 */
3591 sa = (cdb[1] & 0x1f);
3592 scope = (cdb[2] & 0xf0);
3593 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003594
Andy Grover49493142012-01-16 16:57:08 -08003595 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003596 if (!buf)
3597 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3598
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003599 /*
3600 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3601 */
Bart Van Asschea85d6672017-05-23 16:48:27 -07003602 res_key = get_unaligned_be64(&buf[0]);
3603 sa_res_key = get_unaligned_be64(&buf[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003604 /*
3605 * REGISTER_AND_MOVE uses a different SA parameter list containing
3606 * SCSI TransportIDs.
3607 */
3608 if (sa != PRO_REGISTER_AND_MOVE) {
3609 spec_i_pt = (buf[20] & 0x08);
3610 all_tg_pt = (buf[20] & 0x04);
3611 aptpl = (buf[20] & 0x01);
3612 } else {
3613 aptpl = (buf[17] & 0x01);
3614 unreg = (buf[17] & 0x02);
3615 }
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003616 /*
3617 * If the backend device has been configured to force APTPL metadata
3618 * write-out, go ahead and propigate aptpl=1 down now.
3619 */
3620 if (dev->dev_attrib.force_pr_aptpl)
3621 aptpl = 1;
3622
Andy Grover49493142012-01-16 16:57:08 -08003623 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003624 buf = NULL;
3625
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003626 /*
3627 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3628 */
Tang Wenji7ee00312017-07-08 11:28:25 +08003629 if (spec_i_pt && (sa != PRO_REGISTER))
Christoph Hellwigde103c92012-11-06 12:24:09 -08003630 return TCM_INVALID_PARAMETER_LIST;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003631
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003632 /*
3633 * From spc4r17 section 6.14:
3634 *
3635 * If the SPEC_I_PT bit is set to zero, the service action is not
3636 * REGISTER AND MOVE, and the parameter list length is not 24, then
3637 * the command shall be terminated with CHECK CONDITION status, with
3638 * the sense key set to ILLEGAL REQUEST, and the additional sense
3639 * code set to PARAMETER LIST LENGTH ERROR.
3640 */
Tang Wenji7ee00312017-07-08 11:28:25 +08003641 if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003642 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003643 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003644 " list length: %u\n", cmd->data_length);
Tang Wenji7ee00312017-07-08 11:28:25 +08003645 return TCM_PARAMETER_LIST_LENGTH_ERROR;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003646 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003647
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003648 /*
3649 * (core_scsi3_emulate_pro_* function parameters
3650 * are defined by spc4r17 Table 174:
3651 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3652 */
3653 switch (sa) {
3654 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003655 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003656 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003657 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003658 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003659 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3660 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003661 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003662 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3663 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003664 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003665 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3666 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003667 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003668 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003669 res_key, sa_res_key, PREEMPT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003670 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003671 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003672 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003673 res_key, sa_res_key, PREEMPT_AND_ABORT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003674 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003675 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003676 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003677 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003678 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003679 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003680 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003681 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003682 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003683 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003684 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Tang Wenji7ee00312017-07-08 11:28:25 +08003685 " action: 0x%02x\n", sa);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003686 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003687 }
3688
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003689 if (!ret)
3690 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003691 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003692}
3693
3694/*
3695 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3696 *
3697 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3698 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003699static sense_reason_t
3700core_scsi3_pri_read_keys(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003701{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003702 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003703 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003704 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003705 u32 add_len = 0, off = 8;
3706
3707 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003708 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003709 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003710 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003711 }
3712
Andy Grover49493142012-01-16 16:57:08 -08003713 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003714 if (!buf)
3715 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3716
Bart Van Asschea85d6672017-05-23 16:48:27 -07003717 put_unaligned_be32(dev->t10_pr.pr_generation, buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003718
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003719 spin_lock(&dev->t10_pr.registration_lock);
3720 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003721 pr_reg_list) {
3722 /*
3723 * Check for overflow of 8byte PRI READ_KEYS payload and
3724 * next reservation key list descriptor.
3725 */
David Disseldorp63ce3c32018-06-19 17:58:24 +02003726 if (off + 8 <= cmd->data_length) {
3727 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3728 off += 8;
3729 }
3730 /*
3731 * SPC5r17: 6.16.2 READ KEYS service action
3732 * The ADDITIONAL LENGTH field indicates the number of bytes in
3733 * the Reservation key list. The contents of the ADDITIONAL
3734 * LENGTH field are not altered based on the allocation length
3735 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003736 add_len += 8;
3737 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003738 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003739
Bart Van Asschea85d6672017-05-23 16:48:27 -07003740 put_unaligned_be32(add_len, &buf[4]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003741
Andy Grover49493142012-01-16 16:57:08 -08003742 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003743
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003744 return 0;
3745}
3746
3747/*
3748 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3749 *
3750 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3751 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003752static sense_reason_t
3753core_scsi3_pri_read_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003754{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003755 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003756 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003757 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003758 u64 pr_res_key;
3759 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3760
3761 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003762 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003763 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003764 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003765 }
3766
Andy Grover49493142012-01-16 16:57:08 -08003767 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003768 if (!buf)
3769 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3770
Bart Van Asschea85d6672017-05-23 16:48:27 -07003771 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003772
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003773 spin_lock(&dev->dev_reservation_lock);
3774 pr_reg = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07003775 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003776 /*
3777 * Set the hardcoded Additional Length
3778 */
Bart Van Asschea85d6672017-05-23 16:48:27 -07003779 put_unaligned_be32(add_len, &buf[4]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003780
Andy Grover05d1c7c2011-07-20 19:13:28 +00003781 if (cmd->data_length < 22)
3782 goto err;
3783
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003784 /*
3785 * Set the Reservation key.
3786 *
3787 * From spc4r17, section 5.7.10:
3788 * A persistent reservation holder has its reservation key
3789 * returned in the parameter data from a PERSISTENT
3790 * RESERVE IN command with READ RESERVATION service action as
3791 * follows:
3792 * a) For a persistent reservation of the type Write Exclusive
3793 * - All Registrants or Exclusive Access ­ All Regitrants,
3794 * the reservation key shall be set to zero; or
3795 * b) For all other persistent reservation types, the
3796 * reservation key shall be set to the registered
3797 * reservation key for the I_T nexus that holds the
3798 * persistent reservation.
3799 */
3800 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3801 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3802 pr_res_key = 0;
3803 else
3804 pr_res_key = pr_reg->pr_res_key;
3805
Bart Van Asschea85d6672017-05-23 16:48:27 -07003806 put_unaligned_be64(pr_res_key, &buf[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003807 /*
3808 * Set the SCOPE and TYPE
3809 */
3810 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3811 (pr_reg->pr_res_type & 0x0f);
3812 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003813
3814err:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003815 spin_unlock(&dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08003816 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003817
3818 return 0;
3819}
3820
3821/*
3822 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3823 *
3824 * See spc4r17 section 6.13.4 Table 165
3825 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003826static sense_reason_t
3827core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003828{
Andy Grover5951146d2011-07-19 10:26:37 +00003829 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003830 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003831 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003832 u16 add_len = 8; /* Hardcoded to 8. */
3833
3834 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07003835 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003836 " %u too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003837 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003838 }
3839
Andy Grover49493142012-01-16 16:57:08 -08003840 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003841 if (!buf)
3842 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003843
Bart Van Asschea85d6672017-05-23 16:48:27 -07003844 put_unaligned_be16(add_len, &buf[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003845 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3846 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3847 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3848 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3849 /*
3850 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3851 * set the TMV: Task Mask Valid bit.
3852 */
3853 buf[3] |= 0x80;
3854 /*
3855 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3856 */
3857 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3858 /*
3859 * PTPL_A: Persistence across Target Power Loss Active bit
3860 */
3861 if (pr_tmpl->pr_aptpl_active)
3862 buf[3] |= 0x01;
3863 /*
3864 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3865 */
3866 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3867 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3868 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3869 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3870 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3871 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3872
Andy Grover49493142012-01-16 16:57:08 -08003873 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003874
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003875 return 0;
3876}
3877
3878/*
3879 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3880 *
3881 * See spc4r17 section 6.13.5 Table 168 and 169
3882 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003883static sense_reason_t
3884core_scsi3_pri_read_full_status(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003885{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003886 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003887 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003888 struct se_portal_group *se_tpg;
3889 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003890 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003891 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003892 u32 add_desc_len = 0, add_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003893 u32 off = 8; /* off into first Full Status descriptor */
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003894 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003895 int exp_desc_len, desc_len;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003896 bool all_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003897
3898 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003899 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003900 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003901 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003902 }
3903
Andy Grover49493142012-01-16 16:57:08 -08003904 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003905 if (!buf)
3906 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003907
Bart Van Asschea85d6672017-05-23 16:48:27 -07003908 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003909
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003910 spin_lock(&dev->dev_reservation_lock);
3911 if (dev->dev_pr_res_holder) {
3912 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3913
3914 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3915 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3916 all_reg = true;
3917 pr_res_type = pr_holder->pr_res_type;
3918 pr_res_scope = pr_holder->pr_res_scope;
3919 }
3920 }
3921 spin_unlock(&dev->dev_reservation_lock);
3922
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003923 spin_lock(&pr_tmpl->registration_lock);
3924 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3925 &pr_tmpl->registration_list, pr_reg_list) {
3926
3927 se_nacl = pr_reg->pr_reg_nacl;
3928 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3929 add_desc_len = 0;
3930
Joern Engel33940d02014-09-16 16:23:12 -04003931 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003932 spin_unlock(&pr_tmpl->registration_lock);
3933 /*
3934 * Determine expected length of $FABRIC_MOD specific
3935 * TransportID full status descriptor..
3936 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02003937 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3938 &format_code);
3939 if (exp_desc_len < 0 ||
3940 exp_desc_len + add_len > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003941 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003942 " out of buffer: %d\n", cmd->data_length);
3943 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04003944 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003945 break;
3946 }
3947 /*
3948 * Set RESERVATION KEY
3949 */
Bart Van Asschea85d6672017-05-23 16:48:27 -07003950 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3951 off += 8;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003952 off += 4; /* Skip Over Reserved area */
3953
3954 /*
3955 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3956 */
3957 if (pr_reg->pr_reg_all_tg_pt)
3958 buf[off] = 0x02;
3959 /*
3960 * The struct se_lun pointer will be present for the
3961 * reservation holder for PR_HOLDER bit.
3962 *
3963 * Also, if this registration is the reservation
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003964 * holder or there is an All Registrants reservation
3965 * active, fill in SCOPE and TYPE in the next byte.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003966 */
3967 if (pr_reg->pr_res_holder) {
3968 buf[off++] |= 0x01;
3969 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3970 (pr_reg->pr_res_type & 0x0f);
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003971 } else if (all_reg) {
3972 buf[off++] |= 0x01;
3973 buf[off++] = (pr_res_scope & 0xf0) |
3974 (pr_res_type & 0x0f);
3975 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003976 off += 2;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003977 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003978
3979 off += 4; /* Skip over reserved area */
3980 /*
3981 * From spc4r17 6.3.15:
3982 *
3983 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3984 * IDENTIFIER field contains the relative port identifier (see
3985 * 3.1.120) of the target port that is part of the I_T nexus
3986 * described by this full status descriptor. If the ALL_TG_PT
3987 * bit is set to one, the contents of the RELATIVE TARGET PORT
3988 * IDENTIFIER field are not defined by this standard.
3989 */
Andy Grover6708bb22011-06-08 10:36:43 -07003990 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003991 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003992
Bart Van Asschea85d6672017-05-23 16:48:27 -07003993 put_unaligned_be16(sep_rtpi, &buf[off]);
3994 off += 2;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003995 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09003996 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003997
Christoph Hellwig2650d712015-05-01 17:47:58 +02003998 buf[off+4] = se_tpg->proto_id;
3999
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004000 /*
Christoph Hellwig2650d712015-05-01 17:47:58 +02004001 * Now, have the $FABRIC_MOD fill in the transport ID.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004002 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02004003 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4004 &format_code, &buf[off+4]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004005
4006 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04004007 atomic_dec_mb(&pr_reg->pr_res_holders);
Christoph Hellwig2650d712015-05-01 17:47:58 +02004008
4009 if (desc_len < 0)
4010 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004011 /*
4012 * Set the ADDITIONAL DESCRIPTOR LENGTH
4013 */
Bart Van Asschea85d6672017-05-23 16:48:27 -07004014 put_unaligned_be32(desc_len, &buf[off]);
tangwenjic58a2522017-08-17 19:51:54 +08004015 off += 4;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004016 /*
4017 * Size of full desctipor header minus TransportID
4018 * containing $FABRIC_MOD specific) initiator device/port
4019 * WWN information.
4020 *
4021 * See spc4r17 Section 6.13.5 Table 169
4022 */
4023 add_desc_len = (24 + desc_len);
4024
4025 off += desc_len;
4026 add_len += add_desc_len;
4027 }
4028 spin_unlock(&pr_tmpl->registration_lock);
4029 /*
4030 * Set ADDITIONAL_LENGTH
4031 */
Bart Van Asschea85d6672017-05-23 16:48:27 -07004032 put_unaligned_be32(add_len, &buf[4]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004033
Andy Grover49493142012-01-16 16:57:08 -08004034 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004035
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004036 return 0;
4037}
4038
Christoph Hellwigde103c92012-11-06 12:24:09 -08004039sense_reason_t
4040target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004041{
Christoph Hellwigde103c92012-11-06 12:24:09 -08004042 sense_reason_t ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004043
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004044 /*
4045 * Following spc2r20 5.5.1 Reservations overview:
4046 *
4047 * If a logical unit has been reserved by any RESERVE command and is
4048 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4049 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4050 * initiator or service action and shall terminate with a RESERVATION
4051 * CONFLICT status.
4052 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004053 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004054 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4055 " SPC-2 reservation is held, returning"
4056 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08004057 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004058 }
4059
4060 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004061 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004062 ret = core_scsi3_pri_read_keys(cmd);
4063 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004064 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004065 ret = core_scsi3_pri_read_reservation(cmd);
4066 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004067 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004068 ret = core_scsi3_pri_report_capabilities(cmd);
4069 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004070 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004071 ret = core_scsi3_pri_read_full_status(cmd);
4072 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004073 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004074 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004075 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08004076 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004077 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004078
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004079 if (!ret)
4080 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004081 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004082}
4083
Christoph Hellwigde103c92012-11-06 12:24:09 -08004084sense_reason_t
4085target_check_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004086{
Christoph Hellwigd977f432012-10-10 17:37:15 -04004087 struct se_device *dev = cmd->se_dev;
Christoph Hellwigde103c92012-11-06 12:24:09 -08004088 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004089
Christoph Hellwigd977f432012-10-10 17:37:15 -04004090 if (!cmd->se_sess)
4091 return 0;
4092 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4093 return 0;
David Disseldorpb49d6f72018-11-07 14:11:07 +01004094 if (!dev->dev_attrib.emulate_pr)
4095 return 0;
Bodo Stroesser69088a02020-04-27 17:08:22 +02004096 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
Christoph Hellwigd977f432012-10-10 17:37:15 -04004097 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004098
Christoph Hellwigd977f432012-10-10 17:37:15 -04004099 spin_lock(&dev->dev_reservation_lock);
4100 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4101 ret = target_scsi2_reservation_check(cmd);
4102 else
4103 ret = target_scsi3_pr_reservation_check(cmd);
4104 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004105
Christoph Hellwigd977f432012-10-10 17:37:15 -04004106 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004107}