blob: 5ab7100de17eb5694b162403ef43585c4bbceaf1 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_pr.c
3 *
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
6 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07007 * (c) Copyright 2009-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08008 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 ******************************************************************************/
26
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080027#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/list.h>
David S. Miller5538d292015-05-28 11:35:41 -070030#include <linux/vmalloc.h>
Al Viro0e9b10a2013-02-23 15:22:43 -050031#include <linux/file.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020032#include <scsi/scsi_proto.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080033#include <asm/unaligned.h>
34
35#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_backend.h>
37#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080038
Christoph Hellwige26d99a2011-11-14 12:30:30 -050039#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040#include "target_core_pr.h"
41#include "target_core_ua.h"
42
43/*
44 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
45 */
46struct pr_transport_id_holder {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047 struct t10_pr_registration *dest_pr_reg;
48 struct se_portal_group *dest_tpg;
49 struct se_node_acl *dest_node_acl;
50 struct se_dev_entry *dest_se_deve;
51 struct list_head dest_list;
52};
53
Andy Groverd2843c12013-05-16 10:40:55 -070054void core_pr_dump_initiator_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055 struct t10_pr_registration *pr_reg,
56 char *buf,
57 u32 size)
58{
Andy Grover6708bb22011-06-08 10:36:43 -070059 if (!pr_reg->isid_present_at_reg)
Andy Groverd2843c12013-05-16 10:40:55 -070060 buf[0] = '\0';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061
Andy Groverd2843c12013-05-16 10:40:55 -070062 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063}
64
Andy Grover33ce6a82013-05-16 10:40:54 -070065enum register_type {
66 REGISTER,
67 REGISTER_AND_IGNORE_EXISTING_KEY,
68 REGISTER_AND_MOVE,
69};
70
71enum preempt_type {
72 PREEMPT,
73 PREEMPT_AND_ABORT,
74};
75
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080076static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -080077 struct t10_pr_registration *, int, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +020079static int is_reservation_holder(
80 struct t10_pr_registration *pr_res_holder,
81 struct t10_pr_registration *pr_reg)
82{
83 int pr_res_type;
84
85 if (pr_res_holder) {
86 pr_res_type = pr_res_holder->pr_res_type;
87
88 return pr_res_holder == pr_reg ||
89 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
90 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
91 }
92 return 0;
93}
94
Christoph Hellwigde103c92012-11-06 12:24:09 -080095static sense_reason_t
96target_scsi2_reservation_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080097{
Christoph Hellwigd977f432012-10-10 17:37:15 -040098 struct se_device *dev = cmd->se_dev;
99 struct se_session *sess = cmd->se_sess;
100
101 switch (cmd->t_task_cdb[0]) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 case INQUIRY:
103 case RELEASE:
104 case RELEASE_10:
105 return 0;
106 default:
Christoph Hellwigd977f432012-10-10 17:37:15 -0400107 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800108 }
109
Christoph Hellwigd977f432012-10-10 17:37:15 -0400110 if (!dev->dev_reserved_node_acl || !sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 return 0;
112
Christoph Hellwigd977f432012-10-10 17:37:15 -0400113 if (dev->dev_reserved_node_acl != sess->se_node_acl)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800114 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800115
Christoph Hellwigd977f432012-10-10 17:37:15 -0400116 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
117 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800118 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400119 }
120
121 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122}
123
Christoph Hellwigeacac002011-11-03 17:50:40 -0400124static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
125 struct se_node_acl *, struct se_session *);
126static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
127
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700128static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
Christoph Hellwigeacac002011-11-03 17:50:40 -0400129{
130 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400131 struct se_device *dev = cmd->se_dev;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400132 struct t10_pr_registration *pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400133 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400134 int conflict = 0;
135
Christoph Hellwigeacac002011-11-03 17:50:40 -0400136 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
137 se_sess);
138 if (pr_reg) {
139 /*
140 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
141 * behavior
142 *
143 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
144 * status, but no reservation shall be established and the
145 * persistent reservation shall not be changed, if the command
146 * is received from a) and b) below.
147 *
148 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
149 * status, but the persistent reservation shall not be released,
150 * if the command is received from a) and b)
151 *
152 * a) An I_T nexus that is a persistent reservation holder; or
153 * b) An I_T nexus that is registered if a registrants only or
154 * all registrants type persistent reservation is present.
155 *
156 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
157 * RELEASE(6) command, or RELEASE(10) command shall be processed
158 * as defined in SPC-2.
159 */
160 if (pr_reg->pr_res_holder) {
161 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700162 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400163 }
164 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
165 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
166 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
167 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
168 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700169 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400170 }
171 core_scsi3_put_pr_reg(pr_reg);
172 conflict = 1;
173 } else {
174 /*
175 * Following spc2r20 5.5.1 Reservations overview:
176 *
177 * If a logical unit has executed a PERSISTENT RESERVE OUT
178 * command with the REGISTER or the REGISTER AND IGNORE
179 * EXISTING KEY service action and is still registered by any
180 * initiator, all RESERVE commands and all RELEASE commands
181 * regardless of initiator shall conflict and shall terminate
182 * with a RESERVATION CONFLICT status.
183 */
184 spin_lock(&pr_tmpl->registration_lock);
185 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
186 spin_unlock(&pr_tmpl->registration_lock);
187 }
188
189 if (conflict) {
190 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
191 " while active SPC-3 registrations exist,"
192 " returning RESERVATION_CONFLICT\n");
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700193 return -EBUSY;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400194 }
195
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700196 return 0;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400197}
198
Christoph Hellwigde103c92012-11-06 12:24:09 -0800199sense_reason_t
200target_scsi2_reservation_release(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800201{
202 struct se_device *dev = cmd->se_dev;
203 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800204 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800205 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206
Wei Yongjun609234e2012-09-07 14:56:28 +0800207 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400208 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700209 rc = target_check_scsi2_reservation_conflict(cmd);
210 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400211 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800212 if (rc < 0)
213 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214
215 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400216 if (!dev->dev_reserved_node_acl || !sess)
217 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800218
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400219 if (dev->dev_reserved_node_acl != sess->se_node_acl)
220 goto out_unlock;
221
Bernhard Kohledc318d2012-05-13 23:39:37 +0200222 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
223 goto out_unlock;
224
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225 dev->dev_reserved_node_acl = NULL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400226 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
227 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228 dev->dev_res_bin_isid = 0;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400229 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800230 }
Wei Yongjun609234e2012-09-07 14:56:28 +0800231 tpg = sess->se_tpg;
Hannes Reineckef2d30682015-06-10 08:41:22 +0200232 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
233 " MAPPED LUN: %llu for %s\n",
234 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700235 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800236 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800237
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400238out_unlock:
239 spin_unlock(&dev->dev_reservation_lock);
240out:
Christoph Hellwigde103c92012-11-06 12:24:09 -0800241 target_complete_cmd(cmd, GOOD);
242 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243}
244
Christoph Hellwigde103c92012-11-06 12:24:09 -0800245sense_reason_t
246target_scsi2_reservation_reserve(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800247{
248 struct se_device *dev = cmd->se_dev;
249 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800250 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800251 sense_reason_t ret = 0;
252 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800253
Andy Grovera1d8b492011-05-02 17:12:10 -0700254 if ((cmd->t_task_cdb[1] & 0x01) &&
255 (cmd->t_task_cdb[1] & 0x02)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700256 pr_err("LongIO and Obselete Bits set, returning"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800257 " ILLEGAL_REQUEST\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800258 return TCM_UNSUPPORTED_SCSI_OPCODE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 }
260 /*
261 * This is currently the case for target_core_mod passthrough struct se_cmd
262 * ops
263 */
Wei Yongjun609234e2012-09-07 14:56:28 +0800264 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400265 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700266 rc = target_check_scsi2_reservation_conflict(cmd);
267 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400268 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800269
Christoph Hellwigde103c92012-11-06 12:24:09 -0800270 if (rc < 0)
271 return TCM_RESERVATION_CONFLICT;
272
Wei Yongjun609234e2012-09-07 14:56:28 +0800273 tpg = sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800274 spin_lock(&dev->dev_reservation_lock);
275 if (dev->dev_reserved_node_acl &&
276 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700277 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000278 tpg->se_tpg_tfo->get_fabric_name());
Hannes Reineckef2d30682015-06-10 08:41:22 +0200279 pr_err("Original reserver LUN: %llu %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000280 cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281 dev->dev_reserved_node_acl->initiatorname);
Hannes Reineckef2d30682015-06-10 08:41:22 +0200282 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
Andy Grovere3d6f902011-07-19 08:55:10 +0000283 " from %s \n", cmd->se_lun->unpacked_lun,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700284 cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800285 sess->se_node_acl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800286 ret = TCM_RESERVATION_CONFLICT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400287 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800288 }
289
290 dev->dev_reserved_node_acl = sess->se_node_acl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400291 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800292 if (sess->sess_bin_isid != 0) {
293 dev->dev_res_bin_isid = sess->sess_bin_isid;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400294 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 }
Hannes Reineckef2d30682015-06-10 08:41:22 +0200296 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
Andy Grovere3d6f902011-07-19 08:55:10 +0000297 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700298 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800300
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400301out_unlock:
302 spin_unlock(&dev->dev_reservation_lock);
303out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400304 if (!ret)
305 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400306 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800307}
308
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800309
310/*
311 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
312 *
313 * This function is called by those initiator ports who are *NOT*
314 * the active PR reservation holder when a reservation is present.
315 */
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200316static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
317 bool isid_mismatch)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400319 unsigned char *cdb = cmd->t_task_cdb;
Andy Grovere3d6f902011-07-19 08:55:10 +0000320 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700321 struct se_node_acl *nacl = se_sess->se_node_acl;
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200322 int other_cdb = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800323 int registered_nexus = 0, ret = 1; /* Conflict by default */
324 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
325 int we = 0; /* Write Exclusive */
326 int legacy = 0; /* Act like a legacy device and return
327 * RESERVATION CONFLICT on some CDBs */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200329 if (isid_mismatch) {
330 registered_nexus = 0;
331 } else {
332 struct se_dev_entry *se_deve;
333
334 rcu_read_lock();
335 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
336 if (se_deve)
337 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
338 &se_deve->deve_flags);
339 rcu_read_unlock();
340 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800341
342 switch (pr_reg_type) {
343 case PR_TYPE_WRITE_EXCLUSIVE:
344 we = 1;
345 case PR_TYPE_EXCLUSIVE_ACCESS:
346 /*
347 * Some commands are only allowed for the persistent reservation
348 * holder.
349 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 break;
351 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
352 we = 1;
353 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
354 /*
355 * Some commands are only allowed for registered I_T Nexuses.
356 */
357 reg_only = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358 break;
359 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
360 we = 1;
361 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
362 /*
363 * Each registered I_T Nexus is a reservation holder.
364 */
365 all_reg = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 break;
367 default:
Andy Grovere3d6f902011-07-19 08:55:10 +0000368 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800369 }
370 /*
371 * Referenced from spc4r17 table 45 for *NON* PR holder access
372 */
373 switch (cdb[0]) {
374 case SECURITY_PROTOCOL_IN:
375 if (registered_nexus)
376 return 0;
377 ret = (we) ? 0 : 1;
378 break;
379 case MODE_SENSE:
380 case MODE_SENSE_10:
381 case READ_ATTRIBUTE:
382 case READ_BUFFER:
383 case RECEIVE_DIAGNOSTIC:
384 if (legacy) {
385 ret = 1;
386 break;
387 }
388 if (registered_nexus) {
389 ret = 0;
390 break;
391 }
392 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
393 break;
394 case PERSISTENT_RESERVE_OUT:
395 /*
396 * This follows PERSISTENT_RESERVE_OUT service actions that
397 * are allowed in the presence of various reservations.
398 * See spc4r17, table 46
399 */
400 switch (cdb[1] & 0x1f) {
401 case PRO_CLEAR:
402 case PRO_PREEMPT:
403 case PRO_PREEMPT_AND_ABORT:
404 ret = (registered_nexus) ? 0 : 1;
405 break;
406 case PRO_REGISTER:
407 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
408 ret = 0;
409 break;
410 case PRO_REGISTER_AND_MOVE:
411 case PRO_RESERVE:
412 ret = 1;
413 break;
414 case PRO_RELEASE:
415 ret = (registered_nexus) ? 0 : 1;
416 break;
417 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700418 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419 " action: 0x%02x\n", cdb[1] & 0x1f);
Andy Grovere3d6f902011-07-19 08:55:10 +0000420 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800421 }
422 break;
423 case RELEASE:
424 case RELEASE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400425 /* Handled by CRH=1 in target_scsi2_reservation_release() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 ret = 0;
427 break;
428 case RESERVE:
429 case RESERVE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400430 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 ret = 0;
432 break;
433 case TEST_UNIT_READY:
434 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
435 break;
436 case MAINTENANCE_IN:
437 switch (cdb[1] & 0x1f) {
438 case MI_MANAGEMENT_PROTOCOL_IN:
439 if (registered_nexus) {
440 ret = 0;
441 break;
442 }
443 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
444 break;
445 case MI_REPORT_SUPPORTED_OPERATION_CODES:
446 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
447 if (legacy) {
448 ret = 1;
449 break;
450 }
451 if (registered_nexus) {
452 ret = 0;
453 break;
454 }
455 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
456 break;
457 case MI_REPORT_ALIASES:
458 case MI_REPORT_IDENTIFYING_INFORMATION:
459 case MI_REPORT_PRIORITY:
460 case MI_REPORT_TARGET_PGS:
461 case MI_REPORT_TIMESTAMP:
462 ret = 0; /* Allowed */
463 break;
464 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700465 pr_err("Unknown MI Service Action: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466 (cdb[1] & 0x1f));
Andy Grovere3d6f902011-07-19 08:55:10 +0000467 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800468 }
469 break;
470 case ACCESS_CONTROL_IN:
471 case ACCESS_CONTROL_OUT:
472 case INQUIRY:
473 case LOG_SENSE:
Hannes Reinecke85686f62014-11-17 14:25:20 +0100474 case SERVICE_ACTION_IN_12:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 case REPORT_LUNS:
476 case REQUEST_SENSE:
Marco Sanvido68169662012-01-03 17:12:58 -0800477 case PERSISTENT_RESERVE_IN:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 ret = 0; /*/ Allowed CDBs */
479 break;
480 default:
481 other_cdb = 1;
482 break;
483 }
484 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300485 * Case where the CDB is explicitly allowed in the above switch
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800486 * statement.
487 */
Andy Grover6708bb22011-06-08 10:36:43 -0700488 if (!ret && !other_cdb) {
Hannes Reinecke125d0112013-11-19 09:07:46 +0100489 pr_debug("Allowing explicit CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800490 " reservation holder\n", cdb[0],
491 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700492
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493 return ret;
494 }
495 /*
496 * Check if write exclusive initiator ports *NOT* holding the
497 * WRITE_EXCLUSIVE_* reservation.
498 */
Andy Groveree1b1b92012-07-12 17:34:54 -0700499 if (we && !registered_nexus) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 if (cmd->data_direction == DMA_TO_DEVICE) {
501 /*
502 * Conflict for write exclusive
503 */
Andy Grover6708bb22011-06-08 10:36:43 -0700504 pr_debug("%s Conflict for unregistered nexus"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505 " %s CDB: 0x%02x to %s reservation\n",
506 transport_dump_cmd_direction(cmd),
507 se_sess->se_node_acl->initiatorname, cdb[0],
508 core_scsi3_pr_dump_type(pr_reg_type));
509 return 1;
510 } else {
511 /*
512 * Allow non WRITE CDBs for all Write Exclusive
513 * PR TYPEs to pass for registered and
514 * non-registered_nexuxes NOT holding the reservation.
515 *
516 * We only make noise for the unregisterd nexuses,
517 * as we expect registered non-reservation holding
518 * nexuses to issue CDBs.
519 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700520
Andy Grover6708bb22011-06-08 10:36:43 -0700521 if (!registered_nexus) {
Hannes Reinecke125d0112013-11-19 09:07:46 +0100522 pr_debug("Allowing implicit CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 " for %s reservation on unregistered"
524 " nexus\n", cdb[0],
525 core_scsi3_pr_dump_type(pr_reg_type));
526 }
Andy Grover8b1e1242012-04-03 15:51:12 -0700527
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800528 return 0;
529 }
530 } else if ((reg_only) || (all_reg)) {
531 if (registered_nexus) {
532 /*
533 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
534 * allow commands from registered nexuses.
535 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700536
Hannes Reinecke125d0112013-11-19 09:07:46 +0100537 pr_debug("Allowing implicit CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 " reservation\n", cdb[0],
539 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700540
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541 return 0;
542 }
Lee Duncan1ecc7582015-01-05 10:49:44 -0800543 } else if (we && registered_nexus) {
544 /*
545 * Reads are allowed for Write Exclusive locks
546 * from all registrants.
547 */
548 if (cmd->data_direction == DMA_FROM_DEVICE) {
549 pr_debug("Allowing READ CDB: 0x%02x for %s"
550 " reservation\n", cdb[0],
551 core_scsi3_pr_dump_type(pr_reg_type));
552
553 return 0;
554 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555 }
Andy Grover6708bb22011-06-08 10:36:43 -0700556 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 " for %s reservation\n", transport_dump_cmd_direction(cmd),
558 (registered_nexus) ? "" : "un",
559 se_sess->se_node_acl->initiatorname, cdb[0],
560 core_scsi3_pr_dump_type(pr_reg_type));
561
562 return 1; /* Conflict by default */
563}
564
Christoph Hellwigde103c92012-11-06 12:24:09 -0800565static sense_reason_t
566target_scsi3_pr_reservation_check(struct se_cmd *cmd)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400567{
568 struct se_device *dev = cmd->se_dev;
569 struct se_session *sess = cmd->se_sess;
570 u32 pr_reg_type;
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200571 bool isid_mismatch = false;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400572
573 if (!dev->dev_pr_res_holder)
574 return 0;
575
576 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
577 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
578 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
579 goto check_nonholder;
580
581 if (dev->dev_pr_res_holder->isid_present_at_reg) {
582 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
583 sess->sess_bin_isid) {
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200584 isid_mismatch = true;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400585 goto check_nonholder;
586 }
587 }
588
589 return 0;
590
591check_nonholder:
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200592 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
Christoph Hellwigde103c92012-11-06 12:24:09 -0800593 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400594 return 0;
595}
596
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597static u32 core_scsi3_pr_generation(struct se_device *dev)
598{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599 u32 prg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400600
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601 /*
602 * PRGeneration field shall contain the value of a 32-bit wrapping
603 * counter mainted by the device server.
604 *
605 * Note that this is done regardless of Active Persist across
606 * Target PowerLoss (APTPL)
607 *
608 * See spc4r17 section 6.3.12 READ_KEYS service action
609 */
610 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400611 prg = dev->t10_pr.pr_generation++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 spin_unlock(&dev->dev_reservation_lock);
613
614 return prg;
615}
616
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
618 struct se_device *dev,
619 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000620 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200622 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800623 unsigned char *isid,
624 u64 sa_res_key,
625 int all_tg_pt,
626 int aptpl)
627{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800628 struct t10_pr_registration *pr_reg;
629
630 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700631 if (!pr_reg) {
632 pr_err("Unable to allocate struct t10_pr_registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633 return NULL;
634 }
635
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800636 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
637 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
638 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
639 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
640 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
641 atomic_set(&pr_reg->pr_res_holders, 0);
642 pr_reg->pr_reg_nacl = nacl;
643 pr_reg->pr_reg_deve = deve;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000644 pr_reg->pr_res_mapped_lun = mapped_lun;
645 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700646 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800647 pr_reg->pr_res_key = sa_res_key;
648 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
649 pr_reg->pr_reg_aptpl = aptpl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800650 /*
651 * If an ISID value for this SCSI Initiator Port exists,
652 * save it to the registration now.
653 */
654 if (isid != NULL) {
655 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
656 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
657 pr_reg->isid_present_at_reg = 1;
658 }
659
660 return pr_reg;
661}
662
663static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
664static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
665
666/*
667 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
668 * modes.
669 */
670static struct t10_pr_registration *__core_scsi3_alloc_registration(
671 struct se_device *dev,
672 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000673 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800674 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200675 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800676 unsigned char *isid,
677 u64 sa_res_key,
678 int all_tg_pt,
679 int aptpl)
680{
681 struct se_dev_entry *deve_tmp;
682 struct se_node_acl *nacl_tmp;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000683 struct se_lun_acl *lacl_tmp;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700684 struct se_lun *lun_tmp, *next, *dest_lun;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200685 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800686 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
687 int ret;
688 /*
689 * Create a registration for the I_T Nexus upon which the
690 * PROUT REGISTER was received.
691 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000692 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
693 isid, sa_res_key, all_tg_pt,
694 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700695 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800696 return NULL;
697 /*
698 * Return pointer to pr_reg for ALL_TG_PT=0
699 */
Andy Grover6708bb22011-06-08 10:36:43 -0700700 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800701 return pr_reg;
702 /*
703 * Create list of matching SCSI Initiator Port registrations
704 * for ALL_TG_PT=1
705 */
706 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700707 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700708 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
709 continue;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710 spin_unlock(&dev->se_port_lock);
711
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700712 spin_lock(&lun_tmp->lun_deve_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700713 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800714 /*
715 * This pointer will be NULL for demo mode MappedLUNs
Hannes Reinecke125d0112013-11-19 09:07:46 +0100716 * that have not been make explicit via a ConfigFS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717 * MappedLUN group for the SCSI Initiator Node ACL.
718 */
Andy Grover6708bb22011-06-08 10:36:43 -0700719 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800720 continue;
721
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000722 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700723 lockdep_is_held(&lun_tmp->lun_deve_lock));
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000724 nacl_tmp = lacl_tmp->se_lun_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800725 /*
726 * Skip the matching struct se_node_acl that is allocated
727 * above..
728 */
729 if (nacl == nacl_tmp)
730 continue;
731 /*
732 * Only perform PR registrations for target ports on
733 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
734 * arrived.
735 */
736 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
737 continue;
738 /*
739 * Look for a matching Initiator Node ACL in ASCII format
740 */
741 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
742 continue;
743
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700744 kref_get(&deve_tmp->pr_kref);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700745 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800746 /*
747 * Grab a configfs group dependency that is released
748 * for the exception path at label out: below, or upon
749 * completion of adding ALL_TG_PT=1 registrations in
750 * __core_scsi3_add_registration()
751 */
752 ret = core_scsi3_lunacl_depend_item(deve_tmp);
753 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700754 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755 "_item() failed\n");
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700756 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700757 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800758 goto out;
759 }
760 /*
761 * Located a matching SCSI Initiator Port on a different
762 * port, allocate the pr_reg_atp and attach it to the
763 * pr_reg->pr_reg_atp_list that will be processed once
764 * the original *pr_reg is processed in
765 * __core_scsi3_add_registration()
766 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700767 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000768 atomic_read(&deve_tmp->pr_kref.refcount) != 0);
769
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800770 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700771 nacl_tmp, dest_lun, deve_tmp,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000772 deve_tmp->mapped_lun, NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800773 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700774 if (!pr_reg_atp) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700775 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776 core_scsi3_lunacl_undepend_item(deve_tmp);
777 goto out;
778 }
779
780 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
781 &pr_reg->pr_reg_atp_list);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700782 spin_lock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800783 }
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700784 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800785
786 spin_lock(&dev->se_port_lock);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700787 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800788 }
789 spin_unlock(&dev->se_port_lock);
790
791 return pr_reg;
792out:
793 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
794 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
795 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
796 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
797 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
798 }
799 kmem_cache_free(t10_pr_reg_cache, pr_reg);
800 return NULL;
801}
802
803int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000804 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805 u64 sa_res_key,
806 unsigned char *i_port,
807 unsigned char *isid,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200808 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800809 unsigned char *t_port,
810 u16 tpgt,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200811 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800812 int res_holder,
813 int all_tg_pt,
814 u8 type)
815{
816 struct t10_pr_registration *pr_reg;
817
Andy Grover6708bb22011-06-08 10:36:43 -0700818 if (!i_port || !t_port || !sa_res_key) {
819 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000820 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821 }
822
823 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700824 if (!pr_reg) {
825 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000826 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800827 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800828
829 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
830 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
831 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
832 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
833 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
834 atomic_set(&pr_reg->pr_res_holders, 0);
835 pr_reg->pr_reg_nacl = NULL;
836 pr_reg->pr_reg_deve = NULL;
837 pr_reg->pr_res_mapped_lun = mapped_lun;
838 pr_reg->pr_aptpl_target_lun = target_lun;
839 pr_reg->pr_res_key = sa_res_key;
840 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
841 pr_reg->pr_reg_aptpl = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800842 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
843 pr_reg->pr_res_type = type;
844 /*
845 * If an ISID value had been saved in APTPL metadata for this
846 * SCSI Initiator Port, restore it now.
847 */
848 if (isid != NULL) {
849 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
850 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
851 pr_reg->isid_present_at_reg = 1;
852 }
853 /*
854 * Copy the i_port and t_port information from caller.
855 */
856 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
857 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
858 pr_reg->pr_reg_tpgt = tpgt;
859 /*
860 * Set pr_res_holder from caller, the pr_reg who is the reservation
861 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
862 * the Initiator Node LUN ACL from the fabric module is created for
863 * this registration.
864 */
865 pr_reg->pr_res_holder = res_holder;
866
867 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700868 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800869 " metadata\n", (res_holder) ? "+reservation" : "");
870 return 0;
871}
872
873static void core_scsi3_aptpl_reserve(
874 struct se_device *dev,
875 struct se_portal_group *tpg,
876 struct se_node_acl *node_acl,
877 struct t10_pr_registration *pr_reg)
878{
879 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800880
881 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700882 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800883
884 spin_lock(&dev->dev_reservation_lock);
885 dev->dev_pr_res_holder = pr_reg;
886 spin_unlock(&dev->dev_reservation_lock);
887
Andy Grover6708bb22011-06-08 10:36:43 -0700888 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000890 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800891 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
892 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700893 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000894 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700895 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800896}
897
898static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
Andy Grover33ce6a82013-05-16 10:40:54 -0700899 struct t10_pr_registration *, enum register_type, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800900
901static int __core_scsi3_check_aptpl_registration(
902 struct se_device *dev,
903 struct se_portal_group *tpg,
904 struct se_lun *lun,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200905 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800906 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200907 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908{
909 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400910 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
912 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
913 u16 tpgt;
914
915 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
916 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
917 /*
918 * Copy Initiator Port information from struct se_node_acl
919 */
920 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
921 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000922 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
923 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924 /*
925 * Look for the matching registrations+reservation from those
926 * created from APTPL metadata. Note that multiple registrations
927 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
928 * TransportIDs.
929 */
930 spin_lock(&pr_tmpl->aptpl_reg_lock);
931 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
932 pr_reg_aptpl_list) {
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000933
Andy Grover6708bb22011-06-08 10:36:43 -0700934 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700935 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800936 !(strcmp(pr_reg->pr_tport, t_port)) &&
937 (pr_reg->pr_reg_tpgt == tpgt) &&
938 (pr_reg->pr_aptpl_target_lun == target_lun)) {
939
940 pr_reg->pr_reg_nacl = nacl;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700941 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800942
943 list_del(&pr_reg->pr_reg_aptpl_list);
944 spin_unlock(&pr_tmpl->aptpl_reg_lock);
945 /*
946 * At this point all of the pointers in *pr_reg will
947 * be setup, so go ahead and add the registration.
948 */
949
950 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
951 /*
952 * If this registration is the reservation holder,
953 * make that happen now..
954 */
955 if (pr_reg->pr_res_holder)
956 core_scsi3_aptpl_reserve(dev, tpg,
957 nacl, pr_reg);
958 /*
959 * Reenable pr_aptpl_active to accept new metadata
960 * updates once the SCSI device is active again..
961 */
962 spin_lock(&pr_tmpl->aptpl_reg_lock);
963 pr_tmpl->pr_aptpl_active = 1;
964 }
965 }
966 spin_unlock(&pr_tmpl->aptpl_reg_lock);
967
968 return 0;
969}
970
971int core_scsi3_check_aptpl_registration(
972 struct se_device *dev,
973 struct se_portal_group *tpg,
974 struct se_lun *lun,
Nicholas Bellingere2480562014-10-04 04:23:15 +0000975 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200976 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800977{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400978 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800979 return 0;
980
981 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700982 lun->unpacked_lun, nacl,
983 mapped_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800984}
985
986static void __core_scsi3_dump_registration(
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200987 const struct target_core_fabric_ops *tfo,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800988 struct se_device *dev,
989 struct se_node_acl *nacl,
990 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -0700991 enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800992{
993 struct se_portal_group *se_tpg = nacl->se_tpg;
994 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800995
996 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700997 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800998
Andy Grover6708bb22011-06-08 10:36:43 -0700999 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Andy Grover33ce6a82013-05-16 10:40:54 -07001000 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
1001 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001002 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001003 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001004 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001005 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1006 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -07001007 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001008 " Port(s)\n", tfo->get_fabric_name(),
1009 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001010 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001011 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001012 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1013 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1014 pr_reg->pr_reg_aptpl);
1015}
1016
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001017static void __core_scsi3_add_registration(
1018 struct se_device *dev,
1019 struct se_node_acl *nacl,
1020 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001021 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001022 int register_move)
1023{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001024 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001025 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001026 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001027 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001028
1029 /*
1030 * Increment PRgeneration counter for struct se_device upon a successful
1031 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1032 *
1033 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1034 * action, the struct se_device->dev_reservation_lock will already be held,
1035 * so we do not call core_scsi3_pr_generation() which grabs the lock
1036 * for the REGISTER.
1037 */
1038 pr_reg->pr_res_generation = (register_move) ?
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001039 dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040 core_scsi3_pr_generation(dev);
1041
1042 spin_lock(&pr_tmpl->registration_lock);
1043 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001044
1045 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1046 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001047
1048 rcu_read_lock();
1049 deve = pr_reg->pr_reg_deve;
1050 if (deve)
1051 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1052 rcu_read_unlock();
1053
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001054 /*
1055 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1056 */
Andy Grover6708bb22011-06-08 10:36:43 -07001057 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001058 return;
1059 /*
1060 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1061 * allocated in __core_scsi3_alloc_registration()
1062 */
1063 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1064 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001065 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1066
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001067 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1068
1069 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1070
1071 spin_lock(&pr_tmpl->registration_lock);
1072 list_add_tail(&pr_reg_tmp->pr_reg_list,
1073 &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001074
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001075 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1076 register_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001077 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001078
1079 rcu_read_lock();
1080 deve = pr_reg_tmp->pr_reg_deve;
1081 if (deve)
1082 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1083 rcu_read_unlock();
1084
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085 /*
1086 * Drop configfs group dependency reference from
1087 * __core_scsi3_alloc_registration()
1088 */
1089 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1090 }
1091}
1092
1093static int core_scsi3_alloc_registration(
1094 struct se_device *dev,
1095 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001096 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001097 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +02001098 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 unsigned char *isid,
1100 u64 sa_res_key,
1101 int all_tg_pt,
1102 int aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07001103 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001104 int register_move)
1105{
1106 struct t10_pr_registration *pr_reg;
1107
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001108 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1109 isid, sa_res_key, all_tg_pt,
1110 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001111 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001112 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001113
1114 __core_scsi3_add_registration(dev, nacl, pr_reg,
1115 register_type, register_move);
1116 return 0;
1117}
1118
1119static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1120 struct se_device *dev,
1121 struct se_node_acl *nacl,
1122 unsigned char *isid)
1123{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001124 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001125 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1126 struct se_portal_group *tpg;
1127
1128 spin_lock(&pr_tmpl->registration_lock);
1129 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1130 &pr_tmpl->registration_list, pr_reg_list) {
1131 /*
1132 * First look for a matching struct se_node_acl
1133 */
1134 if (pr_reg->pr_reg_nacl != nacl)
1135 continue;
1136
1137 tpg = pr_reg->pr_reg_nacl->se_tpg;
1138 /*
1139 * If this registration does NOT contain a fabric provided
1140 * ISID, then we have found a match.
1141 */
Andy Grover6708bb22011-06-08 10:36:43 -07001142 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 /*
1144 * Determine if this SCSI device server requires that
1145 * SCSI Intiatior TransportID w/ ISIDs is enforced
1146 * for fabric modules (iSCSI) requiring them.
1147 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001148 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001149 if (dev->dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001150 continue;
1151 }
Joern Engel33940d02014-09-16 16:23:12 -04001152 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 spin_unlock(&pr_tmpl->registration_lock);
1154 return pr_reg;
1155 }
1156 /*
1157 * If the *pr_reg contains a fabric defined ISID for multi-value
1158 * SCSI Initiator Port TransportIDs, then we expect a valid
1159 * matching ISID to be provided by the local SCSI Initiator Port.
1160 */
Andy Grover6708bb22011-06-08 10:36:43 -07001161 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162 continue;
1163 if (strcmp(isid, pr_reg->pr_reg_isid))
1164 continue;
1165
Joern Engel33940d02014-09-16 16:23:12 -04001166 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167 spin_unlock(&pr_tmpl->registration_lock);
1168 return pr_reg;
1169 }
1170 spin_unlock(&pr_tmpl->registration_lock);
1171
1172 return NULL;
1173}
1174
1175static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1176 struct se_device *dev,
1177 struct se_node_acl *nacl,
1178 struct se_session *sess)
1179{
1180 struct se_portal_group *tpg = nacl->se_tpg;
1181 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1182
Andy Grovere3d6f902011-07-19 08:55:10 +00001183 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001185 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001186 PR_REG_ISID_LEN);
1187 isid_ptr = &buf[0];
1188 }
1189
1190 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1191}
1192
1193static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1194{
Joern Engel33940d02014-09-16 16:23:12 -04001195 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001196}
1197
Hannes Reinecke125d0112013-11-19 09:07:46 +01001198static int core_scsi3_check_implicit_release(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199 struct se_device *dev,
1200 struct t10_pr_registration *pr_reg)
1201{
1202 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1203 struct t10_pr_registration *pr_res_holder;
1204 int ret = 0;
1205
1206 spin_lock(&dev->dev_reservation_lock);
1207 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001208 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209 spin_unlock(&dev->dev_reservation_lock);
1210 return ret;
1211 }
1212 if (pr_res_holder == pr_reg) {
1213 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001214 * Perform an implicit RELEASE if the registration that
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001215 * is being released is holding the reservation.
1216 *
1217 * From spc4r17, section 5.7.11.1:
1218 *
1219 * e) If the I_T nexus is the persistent reservation holder
1220 * and the persistent reservation is not an all registrants
1221 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1222 * service action or REGISTER AND IGNORE EXISTING KEY
1223 * service action with the SERVICE ACTION RESERVATION KEY
1224 * field set to zero (see 5.7.11.3).
1225 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001226 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 ret = 1;
1228 /*
1229 * For 'All Registrants' reservation types, all existing
1230 * registrations are still processed as reservation holders
1231 * in core_scsi3_pr_seq_non_holder() after the initial
Hannes Reinecke125d0112013-11-19 09:07:46 +01001232 * reservation holder is implicitly released here.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233 */
1234 } else if (pr_reg->pr_reg_all_tg_pt &&
1235 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1236 pr_reg->pr_reg_nacl->initiatorname)) &&
1237 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001238 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001239 " UNREGISTER while existing reservation with matching"
1240 " key 0x%016Lx is present from another SCSI Initiator"
1241 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001242 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001243 }
1244 spin_unlock(&dev->dev_reservation_lock);
1245
1246 return ret;
1247}
1248
1249/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001250 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251 */
1252static void __core_scsi3_free_registration(
1253 struct se_device *dev,
1254 struct t10_pr_registration *pr_reg,
1255 struct list_head *preempt_and_abort_list,
1256 int dec_holders)
Bart Van Asschecb0df4d2015-04-10 14:49:02 +02001257 __releases(&pr_tmpl->registration_lock)
1258 __acquires(&pr_tmpl->registration_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001260 const struct target_core_fabric_ops *tfo =
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001262 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001263 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1264 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001265 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266
1267 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001268 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001270 if (!list_empty(&pr_reg->pr_reg_list))
1271 list_del(&pr_reg->pr_reg_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001272 /*
1273 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1274 * so call core_scsi3_put_pr_reg() to decrement our reference.
1275 */
1276 if (dec_holders)
1277 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001278
1279 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 /*
1281 * Wait until all reference from any other I_T nexuses for this
1282 * *pr_reg have been released. Because list_del() is called above,
1283 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1284 * count back to zero, and we release *pr_reg.
1285 */
1286 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001287 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001288 tfo->get_fabric_name());
1289 cpu_relax();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290 }
1291
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001292 rcu_read_lock();
1293 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1294 if (deve)
1295 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1296 rcu_read_unlock();
1297
1298 spin_lock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001299 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 " Node: %s%s\n", tfo->get_fabric_name(),
1301 pr_reg->pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001302 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001303 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 " Port(s)\n", tfo->get_fabric_name(),
1305 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001306 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001307 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001308 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1309 pr_reg->pr_res_generation);
1310
Andy Grover6708bb22011-06-08 10:36:43 -07001311 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 pr_reg->pr_reg_deve = NULL;
1313 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1315 return;
1316 }
1317 /*
1318 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1319 * are released once the ABORT_TASK_SET has completed..
1320 */
1321 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1322}
1323
1324void core_scsi3_free_pr_reg_from_nacl(
1325 struct se_device *dev,
1326 struct se_node_acl *nacl)
1327{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001328 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001329 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001330 bool free_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331 /*
1332 * If the passed se_node_acl matches the reservation holder,
1333 * release the reservation.
1334 */
1335 spin_lock(&dev->dev_reservation_lock);
1336 pr_res_holder = dev->dev_pr_res_holder;
1337 if ((pr_res_holder != NULL) &&
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001338 (pr_res_holder->pr_reg_nacl == nacl)) {
1339 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1340 free_reg = true;
1341 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001342 spin_unlock(&dev->dev_reservation_lock);
1343 /*
1344 * Release any registration associated with the struct se_node_acl.
1345 */
1346 spin_lock(&pr_tmpl->registration_lock);
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001347 if (pr_res_holder && free_reg)
1348 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1349
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001350 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1351 &pr_tmpl->registration_list, pr_reg_list) {
1352
1353 if (pr_reg->pr_reg_nacl != nacl)
1354 continue;
1355
1356 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1357 }
1358 spin_unlock(&pr_tmpl->registration_lock);
1359}
1360
1361void core_scsi3_free_all_registrations(
1362 struct se_device *dev)
1363{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001364 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001365 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1366
1367 spin_lock(&dev->dev_reservation_lock);
1368 pr_res_holder = dev->dev_pr_res_holder;
1369 if (pr_res_holder != NULL) {
1370 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1371 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001372 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001373 }
1374 spin_unlock(&dev->dev_reservation_lock);
1375
1376 spin_lock(&pr_tmpl->registration_lock);
1377 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1378 &pr_tmpl->registration_list, pr_reg_list) {
1379
1380 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1381 }
1382 spin_unlock(&pr_tmpl->registration_lock);
1383
1384 spin_lock(&pr_tmpl->aptpl_reg_lock);
1385 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1386 pr_reg_aptpl_list) {
1387 list_del(&pr_reg->pr_reg_aptpl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001388 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1389 }
1390 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1391}
1392
1393static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1394{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001395 return target_depend_item(&tpg->tpg_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001396}
1397
1398static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1399{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001400 target_undepend_item(&tpg->tpg_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001401 atomic_dec_mb(&tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001402}
1403
1404static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1405{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001406 if (nacl->dynamic_node_acl)
1407 return 0;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001408 return target_depend_item(&nacl->acl_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001409}
1410
1411static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1412{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001413 if (!nacl->dynamic_node_acl)
1414 target_undepend_item(&nacl->acl_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001415 atomic_dec_mb(&nacl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001416}
1417
1418static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1419{
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001420 struct se_lun_acl *lun_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001421 struct se_node_acl *nacl;
1422 struct se_portal_group *tpg;
1423 /*
1424 * For nacl->dynamic_node_acl=1
1425 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001426 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1427 atomic_read(&se_deve->pr_kref.refcount) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001428 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429 return 0;
1430
1431 nacl = lun_acl->se_lun_nacl;
1432 tpg = nacl->se_tpg;
1433
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001434 return target_depend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435}
1436
1437static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1438{
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001439 struct se_lun_acl *lun_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440 struct se_node_acl *nacl;
1441 struct se_portal_group *tpg;
1442 /*
1443 * For nacl->dynamic_node_acl=1
1444 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001445 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1446 atomic_read(&se_deve->pr_kref.refcount) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001447 if (!lun_acl) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001448 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001449 return;
1450 }
1451 nacl = lun_acl->se_lun_nacl;
1452 tpg = nacl->se_tpg;
1453
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001454 target_undepend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001455 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001456}
1457
Christoph Hellwigde103c92012-11-06 12:24:09 -08001458static sense_reason_t
1459core_scsi3_decode_spec_i_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 struct se_cmd *cmd,
1461 struct se_portal_group *tpg,
1462 unsigned char *l_isid,
1463 u64 sa_res_key,
1464 int all_tg_pt,
1465 int aptpl)
1466{
Andy Grover5951146d2011-07-19 10:26:37 +00001467 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001468 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001469 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001470 struct se_node_acl *dest_node_acl = NULL;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001471 struct se_dev_entry *dest_se_deve = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1473 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001474 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001476 unsigned char *buf, *ptr, proto_ident;
Sagi Grimberg9b353cc2015-06-29 13:07:06 +03001477 const unsigned char *i_str = NULL;
Julia Lawall13ba5642014-11-30 19:14:12 +01001478 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08001479 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001480 u32 tpdl, tid_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001481 u32 dest_rtpi = 0;
1482
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483 /*
1484 * Allocate a struct pr_transport_id_holder and setup the
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001485 * local_node_acl pointer and add to struct list_head tid_dest_list
1486 * for add registration processing in the loop of tid_dest_list below.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 */
1488 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001489 if (!tidh_new) {
1490 pr_err("Unable to allocate tidh_new\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001491 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492 }
1493 INIT_LIST_HEAD(&tidh_new->dest_list);
1494 tidh_new->dest_tpg = tpg;
1495 tidh_new->dest_node_acl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496
Andy Grover5951146d2011-07-19 10:26:37 +00001497 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001498 se_sess->se_node_acl, cmd->se_lun,
1499 NULL, cmd->orig_fe_lun, l_isid,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001501 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001503 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504 }
1505 tidh_new->dest_pr_reg = local_pr_reg;
1506 /*
1507 * The local I_T nexus does not hold any configfs dependances,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001508 * so we set tidh_new->dest_se_deve to NULL to prevent the
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 * configfs_undepend_item() calls in the tid_dest_list loops below.
1510 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001511 tidh_new->dest_se_deve = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001512 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001513
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001514 if (cmd->data_length < 28) {
1515 pr_warn("SPC-PR: Received PR OUT parameter list"
1516 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001517 ret = TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001518 goto out;
1519 }
1520
Andy Grover49493142012-01-16 16:57:08 -08001521 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001522 if (!buf) {
1523 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1524 goto out;
1525 }
1526
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527 /*
1528 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1529 * first extract TransportID Parameter Data Length, and make sure
1530 * the value matches up to the SCSI expected data transfer length.
1531 */
1532 tpdl = (buf[24] & 0xff) << 24;
1533 tpdl |= (buf[25] & 0xff) << 16;
1534 tpdl |= (buf[26] & 0xff) << 8;
1535 tpdl |= buf[27] & 0xff;
1536
1537 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001538 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001539 " does not equal CDB data_length: %u\n", tpdl,
1540 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001541 ret = TCM_INVALID_PARAMETER_LIST;
1542 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001543 }
1544 /*
1545 * Start processing the received transport IDs using the
1546 * receiving I_T Nexus portal's fabric dependent methods to
1547 * obtain the SCSI Initiator Port/Device Identifiers.
1548 */
1549 ptr = &buf[28];
1550
1551 while (tpdl > 0) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001552 struct se_lun *dest_lun, *tmp_lun;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001553
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001554 proto_ident = (ptr[0] & 0x0f);
1555 dest_tpg = NULL;
1556
1557 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001558 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1559 tmp_tpg = tmp_lun->lun_tpg;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001560
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561 /*
1562 * Look for the matching proto_ident provided by
1563 * the received TransportID
1564 */
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02001565 if (tmp_tpg->proto_id != proto_ident)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001566 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001567 dest_rtpi = tmp_lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568
Christoph Hellwig2650d712015-05-01 17:47:58 +02001569 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1570 (const char *)ptr, &tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001571 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 continue;
1573
Joern Engel33940d02014-09-16 16:23:12 -04001574 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001575 spin_unlock(&dev->se_port_lock);
1576
Christoph Hellwigde103c92012-11-06 12:24:09 -08001577 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001578 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 " for tmp_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04001580 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001581 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1582 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583 }
1584 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001585 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001586 * from the decoded fabric module specific TransportID
1587 * at *i_str.
1588 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001589 mutex_lock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 dest_node_acl = __core_tpg_get_initiator_node_acl(
1591 tmp_tpg, i_str);
Joern Engel33940d02014-09-16 16:23:12 -04001592 if (dest_node_acl)
1593 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001594 mutex_unlock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001595
Andy Grover6708bb22011-06-08 10:36:43 -07001596 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001597 core_scsi3_tpg_undepend_item(tmp_tpg);
1598 spin_lock(&dev->se_port_lock);
1599 continue;
1600 }
1601
Christoph Hellwigde103c92012-11-06 12:24:09 -08001602 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001603 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001604 " for dest_node_acl->acl_group\n");
Joern Engel33940d02014-09-16 16:23:12 -04001605 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001606 core_scsi3_tpg_undepend_item(tmp_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001607 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1608 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001609 }
1610
1611 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001612 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001613 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001614 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001615 dest_node_acl->initiatorname, dest_rtpi);
1616
1617 spin_lock(&dev->se_port_lock);
1618 break;
1619 }
1620 spin_unlock(&dev->se_port_lock);
1621
Andy Grover6708bb22011-06-08 10:36:43 -07001622 if (!dest_tpg) {
1623 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001624 " dest_tpg\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001625 ret = TCM_INVALID_PARAMETER_LIST;
1626 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001627 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001628
Andy Grover6708bb22011-06-08 10:36:43 -07001629 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001630 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001631 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001632 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001633
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001634 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001635 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001636 " %u for Transport ID: %s\n", tid_len, ptr);
1637 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1638 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001639 ret = TCM_INVALID_PARAMETER_LIST;
1640 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001641 }
1642 /*
1643 * Locate the desintation struct se_dev_entry pointer for matching
1644 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1645 * Target Port.
1646 */
1647 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1648 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001649 if (!dest_se_deve) {
1650 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001651 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001652 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001653 dest_rtpi);
1654
1655 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1656 core_scsi3_tpg_undepend_item(dest_tpg);
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 }
1660
Christoph Hellwigde103c92012-11-06 12:24:09 -08001661 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001662 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663 " failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001664 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001665 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1666 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001667 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1668 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001669 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001670
Andy Grover6708bb22011-06-08 10:36:43 -07001671 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001672 " dest_se_deve mapped_lun: %llu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001673 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001674 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001675
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001676 /*
1677 * Skip any TransportIDs that already have a registration for
1678 * this target port.
1679 */
1680 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1681 iport_ptr);
1682 if (pr_reg_e) {
1683 core_scsi3_put_pr_reg(pr_reg_e);
1684 core_scsi3_lunacl_undepend_item(dest_se_deve);
1685 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1686 core_scsi3_tpg_undepend_item(dest_tpg);
1687 ptr += tid_len;
1688 tpdl -= tid_len;
1689 tid_len = 0;
1690 continue;
1691 }
1692 /*
1693 * Allocate a struct pr_transport_id_holder and setup
1694 * the dest_node_acl and dest_se_deve pointers for the
1695 * loop below.
1696 */
1697 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1698 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001699 if (!tidh_new) {
1700 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701 core_scsi3_lunacl_undepend_item(dest_se_deve);
1702 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1703 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001704 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1705 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706 }
1707 INIT_LIST_HEAD(&tidh_new->dest_list);
1708 tidh_new->dest_tpg = dest_tpg;
1709 tidh_new->dest_node_acl = dest_node_acl;
1710 tidh_new->dest_se_deve = dest_se_deve;
1711
1712 /*
1713 * Allocate, but do NOT add the registration for the
1714 * TransportID referenced SCSI Initiator port. This
1715 * done because of the following from spc4r17 in section
1716 * 6.14.3 wrt SPEC_I_PT:
1717 *
1718 * "If a registration fails for any initiator port (e.g., if th
1719 * logical unit does not have enough resources available to
1720 * hold the registration information), no registrations shall be
1721 * made, and the command shall be terminated with
1722 * CHECK CONDITION status."
1723 *
1724 * That means we call __core_scsi3_alloc_registration() here,
1725 * and then call __core_scsi3_add_registration() in the
1726 * 2nd loop which will never fail.
1727 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001728 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
1729 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
1730
Andy Grover5951146d2011-07-19 10:26:37 +00001731 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001732 dest_node_acl, dest_lun, dest_se_deve,
1733 dest_se_deve->mapped_lun, iport_ptr,
1734 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001735 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001736 core_scsi3_lunacl_undepend_item(dest_se_deve);
1737 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1738 core_scsi3_tpg_undepend_item(dest_tpg);
1739 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001740 ret = TCM_INVALID_PARAMETER_LIST;
1741 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742 }
1743 tidh_new->dest_pr_reg = dest_pr_reg;
1744 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1745
1746 ptr += tid_len;
1747 tpdl -= tid_len;
1748 tid_len = 0;
1749
1750 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001751
Andy Grover49493142012-01-16 16:57:08 -08001752 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001753
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754 /*
1755 * Go ahead and create a registrations from tid_dest_list for the
1756 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1757 * and dest_se_deve.
1758 *
1759 * The SA Reservation Key from the PROUT is set for the
1760 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1761 * means that the TransportID Initiator port will be
1762 * registered on all of the target ports in the SCSI target device
1763 * ALL_TG_PT=0 means the registration will only be for the
1764 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1765 * was received.
1766 */
1767 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1768 dest_tpg = tidh->dest_tpg;
1769 dest_node_acl = tidh->dest_node_acl;
1770 dest_se_deve = tidh->dest_se_deve;
1771 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772
1773 list_del(&tidh->dest_list);
1774 kfree(tidh);
1775
1776 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001777 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001778
Andy Grover5951146d2011-07-19 10:26:37 +00001779 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001780 dest_pr_reg, 0, 0);
1781
Andy Grover6708bb22011-06-08 10:36:43 -07001782 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001783 " registered Transport ID for Node: %s%s Mapped LUN:"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001784 " %llu\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001785 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1786 dest_se_deve->mapped_lun : 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001787
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001788 if (!dest_se_deve)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001789 continue;
1790
1791 core_scsi3_lunacl_undepend_item(dest_se_deve);
1792 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1793 core_scsi3_tpg_undepend_item(dest_tpg);
1794 }
1795
1796 return 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001797out_unmap:
Andy Grover49493142012-01-16 16:57:08 -08001798 transport_kunmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001799out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001800 /*
1801 * For the failure case, release everything from tid_dest_list
1802 * including *dest_pr_reg and the configfs dependances..
1803 */
1804 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1805 dest_tpg = tidh->dest_tpg;
1806 dest_node_acl = tidh->dest_node_acl;
1807 dest_se_deve = tidh->dest_se_deve;
1808 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001809
1810 list_del(&tidh->dest_list);
1811 kfree(tidh);
1812 /*
1813 * Release any extra ALL_TG_PT=1 registrations for
1814 * the SPEC_I_PT=1 case.
1815 */
1816 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1817 &dest_pr_reg->pr_reg_atp_list,
1818 pr_reg_atp_mem_list) {
1819 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1820 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1821 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1822 }
1823
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001824 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1825
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001826 if (!dest_se_deve)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001827 continue;
1828
1829 core_scsi3_lunacl_undepend_item(dest_se_deve);
1830 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1831 core_scsi3_tpg_undepend_item(dest_tpg);
1832 }
1833 return ret;
1834}
1835
Andy Grover3c8a6222013-05-16 10:40:58 -07001836static int core_scsi3_update_aptpl_buf(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837 struct se_device *dev,
1838 unsigned char *buf,
Andy Grover0607dec2013-05-16 10:40:57 -07001839 u32 pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001840{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001841 struct se_portal_group *tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842 struct t10_pr_registration *pr_reg;
1843 unsigned char tmp[512], isid_buf[32];
1844 ssize_t len = 0;
1845 int reg_count = 0;
Andy Grover3c8a6222013-05-16 10:40:58 -07001846 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847
Andy Grover3c8a6222013-05-16 10:40:58 -07001848 spin_lock(&dev->dev_reservation_lock);
1849 spin_lock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001850 /*
1851 * Walk the registration list..
1852 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001853 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854 pr_reg_list) {
1855
1856 tmp[0] = '\0';
1857 isid_buf[0] = '\0';
1858 tpg = pr_reg->pr_reg_nacl->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001859 /*
1860 * Write out any ISID value to APTPL metadata that was included
1861 * in the original registration.
1862 */
1863 if (pr_reg->isid_present_at_reg)
1864 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1865 pr_reg->pr_reg_isid);
1866 /*
1867 * Include special metadata if the pr_reg matches the
1868 * reservation holder.
1869 */
1870 if (dev->dev_pr_res_holder == pr_reg) {
1871 snprintf(tmp, 512, "PR_REG_START: %d"
1872 "\ninitiator_fabric=%s\n"
1873 "initiator_node=%s\n%s"
1874 "sa_res_key=%llu\n"
1875 "res_holder=1\nres_type=%02x\n"
1876 "res_scope=%02x\nres_all_tg_pt=%d\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001877 "mapped_lun=%llu\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001878 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001879 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1880 pr_reg->pr_res_key, pr_reg->pr_res_type,
1881 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1882 pr_reg->pr_res_mapped_lun);
1883 } else {
1884 snprintf(tmp, 512, "PR_REG_START: %d\n"
1885 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1886 "sa_res_key=%llu\nres_holder=0\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001887 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001888 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001889 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1890 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1891 pr_reg->pr_res_mapped_lun);
1892 }
1893
Dan Carpenter60d645a2011-06-15 10:03:05 -07001894 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001895 pr_err("Unable to update renaming APTPL metadata,"
1896 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001897 ret = -EMSGSIZE;
1898 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899 }
1900 len += sprintf(buf+len, "%s", tmp);
1901
1902 /*
1903 * Include information about the associated SCSI target port.
1904 */
1905 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001906 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001907 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1908 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1909 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001910 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1911 reg_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001912
Dan Carpenter60d645a2011-06-15 10:03:05 -07001913 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001914 pr_err("Unable to update renaming APTPL metadata,"
1915 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001916 ret = -EMSGSIZE;
1917 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918 }
1919 len += sprintf(buf+len, "%s", tmp);
1920 reg_count++;
1921 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922
Andy Grover6708bb22011-06-08 10:36:43 -07001923 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924 len += sprintf(buf+len, "No Registrations or Reservations");
1925
Andy Grover3c8a6222013-05-16 10:40:58 -07001926out:
1927 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001928 spin_unlock(&dev->dev_reservation_lock);
1929
1930 return ret;
1931}
1932
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933static int __core_scsi3_write_aptpl_to_file(
1934 struct se_device *dev,
Andy Grover4dee96f2013-05-16 10:41:00 -07001935 unsigned char *buf)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001937 struct t10_wwn *wwn = &dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001938 struct file *file;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939 int flags = O_RDWR | O_CREAT | O_TRUNC;
1940 char path[512];
Andy Grover4dee96f2013-05-16 10:41:00 -07001941 u32 pr_aptpl_buf_len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 int ret;
1943
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001944 memset(path, 0, 512);
1945
Dan Carpenter60d645a2011-06-15 10:03:05 -07001946 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07001947 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001949 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001950 }
1951
1952 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1953 file = filp_open(path, flags, 0600);
Al Viro0e9b10a2013-02-23 15:22:43 -05001954 if (IS_ERR(file)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001955 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 " failed\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001957 return PTR_ERR(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 }
1959
Andy Grover4dee96f2013-05-16 10:41:00 -07001960 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961
Al Viro0e9b10a2013-02-23 15:22:43 -05001962 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963
Al Viro0e9b10a2013-02-23 15:22:43 -05001964 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -07001965 pr_debug("Error writing APTPL metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001966 fput(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967
Gera Kazakovf730f912013-09-09 15:47:06 -07001968 return (ret < 0) ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969}
1970
Andy Grover459f2132013-05-16 10:41:02 -07001971/*
1972 * Clear the APTPL metadata if APTPL has been disabled, otherwise
1973 * write out the updated metadata to struct file for this SCSI device.
1974 */
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001975static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001976{
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001977 unsigned char *buf;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001978 int rc, len = PR_APTPL_BUF_LEN;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001979
Andy Grover459f2132013-05-16 10:41:02 -07001980 if (!aptpl) {
1981 char *null_buf = "No Registrations or Reservations\n";
1982
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001983 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
Andy Grover459f2132013-05-16 10:41:02 -07001984 dev->t10_pr.pr_aptpl_active = 0;
1985 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
Andy Grover459f2132013-05-16 10:41:02 -07001986
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001987 if (rc)
1988 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover459f2132013-05-16 10:41:02 -07001989
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001990 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991 }
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001992retry:
1993 buf = vzalloc(len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001994 if (!buf)
1995 return TCM_OUT_OF_RESOURCES;
1996
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001997 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07001998 if (rc < 0) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001999 vfree(buf);
2000 len *= 2;
2001 goto retry;
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002002 }
2003
2004 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2005 if (rc != 0) {
2006 pr_err("SPC-3 PR: Could not update APTPL\n");
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002007 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002008 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2009 }
2010 dev->t10_pr.pr_aptpl_active = 1;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002011 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002012 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2013 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014}
2015
Christoph Hellwigde103c92012-11-06 12:24:09 -08002016static sense_reason_t
2017core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
Andy Groverbc118fe2013-05-16 10:41:04 -07002018 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002019{
Andy Grovere3d6f902011-07-19 08:55:10 +00002020 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002021 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002022 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023 struct se_portal_group *se_tpg;
Andy Groverbc118fe2013-05-16 10:41:04 -07002024 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002025 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002026 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
Hannes Reineckea0d50f62012-12-17 09:53:34 +01002027 sense_reason_t ret = TCM_NO_SENSE;
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002028 int pr_holder = 0, type;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002029
Andy Grover6708bb22011-06-08 10:36:43 -07002030 if (!se_sess || !se_lun) {
2031 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002032 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002033 }
2034 se_tpg = se_sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035
Andy Grovere3d6f902011-07-19 08:55:10 +00002036 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002037 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002038 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002039 PR_REG_ISID_LEN);
2040 isid_ptr = &isid_buf[0];
2041 }
2042 /*
2043 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2044 */
Andy Groverbc118fe2013-05-16 10:41:04 -07002045 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2046 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002047 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002048 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049 " for SA REGISTER, returning CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002050 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002051 }
2052 /*
2053 * Do nothing but return GOOD status.
2054 */
Andy Grover6708bb22011-06-08 10:36:43 -07002055 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002056 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057
Andy Grover6708bb22011-06-08 10:36:43 -07002058 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002059 /*
2060 * Perform the Service Action REGISTER on the Initiator
2061 * Port Endpoint that the PRO was received from on the
2062 * Logical Unit of the SCSI device server.
2063 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002064 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00002065 se_sess->se_node_acl, cmd->se_lun,
2066 NULL, cmd->orig_fe_lun, isid_ptr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002067 sa_res_key, all_tg_pt, aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07002068 register_type, 0)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002069 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002070 " struct t10_pr_registration\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002071 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002072 }
2073 } else {
2074 /*
2075 * Register both the Initiator port that received
2076 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2077 * TransportID from Parameter list and loop through
2078 * fabric dependent parameter list while calling
2079 * logic from of core_scsi3_alloc_registration() for
2080 * each TransportID provided SCSI Initiator Port/Device
2081 */
2082 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2083 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2084 if (ret != 0)
2085 return ret;
2086 }
Andy Grover459f2132013-05-16 10:41:02 -07002087 return core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002088 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002089
Andy Groverbc118fe2013-05-16 10:41:04 -07002090 /* ok, existing registration */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002091
Andy Groverbc118fe2013-05-16 10:41:04 -07002092 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2093 pr_err("SPC-3 PR REGISTER: Received"
2094 " res_key: 0x%016Lx does not match"
2095 " existing SA REGISTER res_key:"
2096 " 0x%016Lx\n", res_key,
2097 pr_reg->pr_res_key);
2098 ret = TCM_RESERVATION_CONFLICT;
2099 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002100 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002101
2102 if (spec_i_pt) {
Andy Grover1f070cc2013-05-16 10:40:56 -07002103 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2104 " set on a registered nexus\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002105 ret = TCM_INVALID_PARAMETER_LIST;
Andy Groverbc118fe2013-05-16 10:41:04 -07002106 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002107 }
2108
2109 /*
2110 * An existing ALL_TG_PT=1 registration being released
2111 * must also set ALL_TG_PT=1 in the incoming PROUT.
2112 */
Andy Grover1f070cc2013-05-16 10:40:56 -07002113 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2114 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
Christoph Hellwigde103c92012-11-06 12:24:09 -08002115 " registration exists, but ALL_TG_PT=1 bit not"
2116 " present in received PROUT\n");
2117 ret = TCM_INVALID_CDB_FIELD;
Andy Groverbc118fe2013-05-16 10:41:04 -07002118 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002119 }
2120
2121 /*
Andy Grover51d9c412013-05-16 10:41:03 -07002122 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
Christoph Hellwigde103c92012-11-06 12:24:09 -08002123 */
Andy Grover51d9c412013-05-16 10:41:03 -07002124 if (sa_res_key) {
2125 /*
2126 * Increment PRgeneration counter for struct se_device"
2127 * upon a successful REGISTER, see spc4r17 section 6.3.2
2128 * READ_KEYS service action.
2129 */
2130 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2131 pr_reg->pr_res_key = sa_res_key;
2132 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2133 " Key for %s to: 0x%016Lx PRgeneration:"
2134 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2135 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2136 pr_reg->pr_reg_nacl->initiatorname,
2137 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2138
2139 } else {
2140 /*
2141 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2142 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002143 type = pr_reg->pr_res_type;
2144 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2145 pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002146 if (pr_holder < 0) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002147 ret = TCM_RESERVATION_CONFLICT;
Andy Groverbc118fe2013-05-16 10:41:04 -07002148 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002149 }
2150
2151 spin_lock(&pr_tmpl->registration_lock);
2152 /*
2153 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2154 * and matching pr_res_key.
2155 */
2156 if (pr_reg->pr_reg_all_tg_pt) {
2157 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2158 &pr_tmpl->registration_list,
2159 pr_reg_list) {
2160
2161 if (!pr_reg_p->pr_reg_all_tg_pt)
2162 continue;
2163 if (pr_reg_p->pr_res_key != res_key)
2164 continue;
2165 if (pr_reg == pr_reg_p)
2166 continue;
2167 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2168 pr_reg_p->pr_reg_nacl->initiatorname))
2169 continue;
2170
2171 __core_scsi3_free_registration(dev,
2172 pr_reg_p, NULL, 0);
2173 }
2174 }
2175
2176 /*
2177 * Release the calling I_T Nexus registration now..
2178 */
2179 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002180 pr_reg = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002181
2182 /*
2183 * From spc4r17, section 5.7.11.3 Unregistering
2184 *
2185 * If the persistent reservation is a registrants only
2186 * type, the device server shall establish a unit
2187 * attention condition for the initiator port associated
2188 * with every registered I_T nexus except for the I_T
2189 * nexus on which the PERSISTENT RESERVE OUT command was
2190 * received, with the additional sense code set to
2191 * RESERVATIONS RELEASED.
2192 */
2193 if (pr_holder &&
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002194 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2195 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002196 list_for_each_entry(pr_reg_p,
2197 &pr_tmpl->registration_list,
2198 pr_reg_list) {
2199
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002200 target_ua_allocate_lun(
Christoph Hellwigde103c92012-11-06 12:24:09 -08002201 pr_reg_p->pr_reg_nacl,
2202 pr_reg_p->pr_res_mapped_lun,
2203 0x2A,
2204 ASCQ_2AH_RESERVATIONS_RELEASED);
2205 }
2206 }
Andy Grover51d9c412013-05-16 10:41:03 -07002207
Christoph Hellwigde103c92012-11-06 12:24:09 -08002208 spin_unlock(&pr_tmpl->registration_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002209 }
2210
Andy Grover459f2132013-05-16 10:41:02 -07002211 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002212
Andy Groverbc118fe2013-05-16 10:41:04 -07002213out:
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002214 if (pr_reg)
2215 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002216 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002217}
2218
2219unsigned char *core_scsi3_pr_dump_type(int type)
2220{
2221 switch (type) {
2222 case PR_TYPE_WRITE_EXCLUSIVE:
2223 return "Write Exclusive Access";
2224 case PR_TYPE_EXCLUSIVE_ACCESS:
2225 return "Exclusive Access";
2226 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2227 return "Write Exclusive Access, Registrants Only";
2228 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2229 return "Exclusive Access, Registrants Only";
2230 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2231 return "Write Exclusive Access, All Registrants";
2232 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2233 return "Exclusive Access, All Registrants";
2234 default:
2235 break;
2236 }
2237
2238 return "Unknown SPC-3 PR Type";
2239}
2240
Christoph Hellwigde103c92012-11-06 12:24:09 -08002241static sense_reason_t
2242core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002243{
Christoph Hellwigde103c92012-11-06 12:24:09 -08002244 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002245 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002246 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002247 struct t10_pr_registration *pr_reg, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002248 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002249 char i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08002250 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002251
2252 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2253
Andy Grover6708bb22011-06-08 10:36:43 -07002254 if (!se_sess || !se_lun) {
2255 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002256 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002257 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002258 /*
2259 * Locate the existing *pr_reg via struct se_node_acl pointers
2260 */
Andy Grover5951146d2011-07-19 10:26:37 +00002261 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002262 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002263 if (!pr_reg) {
2264 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002265 " PR_REGISTERED *pr_reg for RESERVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002266 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002267 }
2268 /*
2269 * From spc4r17 Section 5.7.9: Reserving:
2270 *
2271 * An application client creates a persistent reservation by issuing
2272 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2273 * a registered I_T nexus with the following parameters:
2274 * a) RESERVATION KEY set to the value of the reservation key that is
2275 * registered with the logical unit for the I_T nexus; and
2276 */
2277 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002278 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002279 " does not match existing SA REGISTER res_key:"
2280 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002281 ret = TCM_RESERVATION_CONFLICT;
2282 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002283 }
2284 /*
2285 * From spc4r17 Section 5.7.9: Reserving:
2286 *
2287 * From above:
2288 * b) TYPE field and SCOPE field set to the persistent reservation
2289 * being created.
2290 *
2291 * Only one persistent reservation is allowed at a time per logical unit
2292 * and that persistent reservation has a scope of LU_SCOPE.
2293 */
2294 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002295 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002296 ret = TCM_INVALID_PARAMETER_LIST;
2297 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298 }
2299 /*
2300 * See if we have an existing PR reservation holder pointer at
2301 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2302 * *pr_res_holder.
2303 */
2304 spin_lock(&dev->dev_reservation_lock);
2305 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002306 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002307 /*
2308 * From spc4r17 Section 5.7.9: Reserving:
2309 *
2310 * If the device server receives a PERSISTENT RESERVE OUT
2311 * command from an I_T nexus other than a persistent reservation
2312 * holder (see 5.7.10) that attempts to create a persistent
2313 * reservation when a persistent reservation already exists for
2314 * the logical unit, then the command shall be completed with
2315 * RESERVATION CONFLICT status.
2316 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002317 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002318 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002319 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002320 " [%s]: %s while reservation already held by"
2321 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002322 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002323 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002324 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002325 pr_res_holder->pr_reg_nacl->initiatorname);
2326
2327 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002328 ret = TCM_RESERVATION_CONFLICT;
2329 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002330 }
2331 /*
2332 * From spc4r17 Section 5.7.9: Reserving:
2333 *
2334 * If a persistent reservation holder attempts to modify the
2335 * type or scope of an existing persistent reservation, the
2336 * command shall be completed with RESERVATION CONFLICT status.
2337 */
2338 if ((pr_res_holder->pr_res_type != type) ||
2339 (pr_res_holder->pr_res_scope != scope)) {
2340 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002341 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002342 " [%s]: %s trying to change TYPE and/or SCOPE,"
2343 " while reservation already held by [%s]: %s,"
2344 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002345 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002346 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002347 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002348 pr_res_holder->pr_reg_nacl->initiatorname);
2349
2350 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002351 ret = TCM_RESERVATION_CONFLICT;
2352 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002353 }
2354 /*
2355 * From spc4r17 Section 5.7.9: Reserving:
2356 *
2357 * If the device server receives a PERSISTENT RESERVE OUT
2358 * command with RESERVE service action where the TYPE field and
2359 * the SCOPE field contain the same values as the existing type
2360 * and scope from a persistent reservation holder, it shall not
2361 * make any change to the existing persistent reservation and
2362 * shall completethe command with GOOD status.
2363 */
2364 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002365 ret = 0;
2366 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002367 }
2368 /*
2369 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2370 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2371 */
2372 pr_reg->pr_res_scope = scope;
2373 pr_reg->pr_res_type = type;
2374 pr_reg->pr_res_holder = 1;
2375 dev->dev_pr_res_holder = pr_reg;
Andy Groverd2843c12013-05-16 10:40:55 -07002376 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002377
Andy Grover6708bb22011-06-08 10:36:43 -07002378 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002380 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002382 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002383 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002384 se_sess->se_node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002385 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002386 spin_unlock(&dev->dev_reservation_lock);
2387
Andy Grover459f2132013-05-16 10:41:02 -07002388 if (pr_tmpl->pr_aptpl_active)
2389 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002390
Christoph Hellwigde103c92012-11-06 12:24:09 -08002391 ret = 0;
2392out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002393 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002394 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002395}
2396
Christoph Hellwigde103c92012-11-06 12:24:09 -08002397static sense_reason_t
2398core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2399 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002400{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002401 switch (type) {
2402 case PR_TYPE_WRITE_EXCLUSIVE:
2403 case PR_TYPE_EXCLUSIVE_ACCESS:
2404 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2405 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2406 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2407 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08002408 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002409 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002410 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002411 " 0x%02x\n", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002412 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002413 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002414}
2415
2416/*
2417 * Called with struct se_device->dev_reservation_lock held.
2418 */
2419static void __core_scsi3_complete_pro_release(
2420 struct se_device *dev,
2421 struct se_node_acl *se_nacl,
2422 struct t10_pr_registration *pr_reg,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002423 int explicit,
2424 int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002425{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002426 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002427 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002428 int pr_res_type = 0, pr_res_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429
2430 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002431 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002432 /*
2433 * Go ahead and release the current PR reservation holder.
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002434 * If an All Registrants reservation is currently active and
2435 * a unregister operation is requested, replace the current
2436 * dev_pr_res_holder with another active registration.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002437 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002438 if (dev->dev_pr_res_holder) {
2439 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2440 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2441 dev->dev_pr_res_holder->pr_res_type = 0;
2442 dev->dev_pr_res_holder->pr_res_scope = 0;
2443 dev->dev_pr_res_holder->pr_res_holder = 0;
2444 dev->dev_pr_res_holder = NULL;
2445 }
2446 if (!unreg)
2447 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002449 spin_lock(&dev->t10_pr.registration_lock);
2450 list_del_init(&pr_reg->pr_reg_list);
2451 /*
2452 * If the I_T nexus is a reservation holder, the persistent reservation
2453 * is of an all registrants type, and the I_T nexus is the last remaining
2454 * registered I_T nexus, then the device server shall also release the
2455 * persistent reservation.
2456 */
2457 if (!list_empty(&dev->t10_pr.registration_list) &&
2458 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2459 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2460 dev->dev_pr_res_holder =
2461 list_entry(dev->t10_pr.registration_list.next,
2462 struct t10_pr_registration, pr_reg_list);
2463 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2464 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2465 dev->dev_pr_res_holder->pr_res_holder = 1;
2466 }
2467 spin_unlock(&dev->t10_pr.registration_lock);
2468out:
2469 if (!dev->dev_pr_res_holder) {
2470 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2471 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2472 tfo->get_fabric_name(), (explicit) ? "explicit" :
2473 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2474 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2475 }
Andy Grover6708bb22011-06-08 10:36:43 -07002476 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002477 tfo->get_fabric_name(), se_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002478 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002479 /*
2480 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2481 */
2482 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2483}
2484
Christoph Hellwigde103c92012-11-06 12:24:09 -08002485static sense_reason_t
2486core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2487 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002488{
2489 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002490 struct se_session *se_sess = cmd->se_sess;
2491 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002492 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002493 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002494 sense_reason_t ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495
Andy Grover6708bb22011-06-08 10:36:43 -07002496 if (!se_sess || !se_lun) {
2497 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002498 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002499 }
2500 /*
2501 * Locate the existing *pr_reg via struct se_node_acl pointers
2502 */
2503 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002504 if (!pr_reg) {
2505 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002506 " PR_REGISTERED *pr_reg for RELEASE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002507 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002508 }
2509 /*
2510 * From spc4r17 Section 5.7.11.2 Releasing:
2511 *
2512 * If there is no persistent reservation or in response to a persistent
2513 * reservation release request from a registered I_T nexus that is not a
2514 * persistent reservation holder (see 5.7.10), the device server shall
2515 * do the following:
2516 *
2517 * a) Not release the persistent reservation, if any;
2518 * b) Not remove any registrations; and
2519 * c) Complete the command with GOOD status.
2520 */
2521 spin_lock(&dev->dev_reservation_lock);
2522 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002523 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002524 /*
2525 * No persistent reservation, return GOOD status.
2526 */
2527 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerbb7a8c82012-11-06 15:46:25 -08002528 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002529 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002530
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002531 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002532 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002533 * Release request from a registered I_T nexus that is not a
2534 * persistent reservation holder. return GOOD status.
2535 */
2536 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002537 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002539
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 /*
2541 * From spc4r17 Section 5.7.11.2 Releasing:
2542 *
2543 * Only the persistent reservation holder (see 5.7.10) is allowed to
2544 * release a persistent reservation.
2545 *
2546 * An application client releases the persistent reservation by issuing
2547 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2548 * an I_T nexus that is a persistent reservation holder with the
2549 * following parameters:
2550 *
2551 * a) RESERVATION KEY field set to the value of the reservation key
2552 * that is registered with the logical unit for the I_T nexus;
2553 */
2554 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002555 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002556 " does not match existing SA REGISTER res_key:"
2557 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2558 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002559 ret = TCM_RESERVATION_CONFLICT;
2560 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002561 }
2562 /*
2563 * From spc4r17 Section 5.7.11.2 Releasing and above:
2564 *
2565 * b) TYPE field and SCOPE field set to match the persistent
2566 * reservation being released.
2567 */
2568 if ((pr_res_holder->pr_res_type != type) ||
2569 (pr_res_holder->pr_res_scope != scope)) {
2570 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002571 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002572 " reservation from [%s]: %s with different TYPE "
2573 "and/or SCOPE while reservation already held by"
2574 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002575 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002577 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578 pr_res_holder->pr_reg_nacl->initiatorname);
2579
2580 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002581 ret = TCM_RESERVATION_CONFLICT;
2582 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002583 }
2584 /*
2585 * In response to a persistent reservation release request from the
2586 * persistent reservation holder the device server shall perform a
2587 * release by doing the following as an uninterrupted series of actions:
2588 * a) Release the persistent reservation;
2589 * b) Not remove any registration(s);
2590 * c) If the released persistent reservation is a registrants only type
2591 * or all registrants type persistent reservation,
2592 * the device server shall establish a unit attention condition for
2593 * the initiator port associated with every regis-
2594 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2595 * RESERVE OUT command with RELEASE service action was received,
2596 * with the additional sense code set to RESERVATIONS RELEASED; and
2597 * d) If the persistent reservation is of any other type, the device
2598 * server shall not establish a unit attention condition.
2599 */
2600 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002601 pr_reg, 1, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002602
2603 spin_unlock(&dev->dev_reservation_lock);
2604
2605 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2606 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2607 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2608 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2609 /*
2610 * If no UNIT ATTENTION conditions will be established for
2611 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2612 * go ahead and check for APTPL=1 update+write below
2613 */
2614 goto write_aptpl;
2615 }
2616
2617 spin_lock(&pr_tmpl->registration_lock);
2618 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2619 pr_reg_list) {
2620 /*
2621 * Do not establish a UNIT ATTENTION condition
2622 * for the calling I_T Nexus
2623 */
2624 if (pr_reg_p == pr_reg)
2625 continue;
2626
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002627 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002628 pr_reg_p->pr_res_mapped_lun,
2629 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2630 }
2631 spin_unlock(&pr_tmpl->registration_lock);
2632
2633write_aptpl:
Andy Grover459f2132013-05-16 10:41:02 -07002634 if (pr_tmpl->pr_aptpl_active)
2635 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2636
Christoph Hellwigde103c92012-11-06 12:24:09 -08002637out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002638 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002639 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640}
2641
Christoph Hellwigde103c92012-11-06 12:24:09 -08002642static sense_reason_t
2643core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002644{
2645 struct se_device *dev = cmd->se_dev;
2646 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002647 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002648 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002649 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002650 u64 pr_res_mapped_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002651 int calling_it_nexus = 0;
2652 /*
2653 * Locate the existing *pr_reg via struct se_node_acl pointers
2654 */
Andy Grover5951146d2011-07-19 10:26:37 +00002655 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002656 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002657 if (!pr_reg_n) {
2658 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002659 " PR_REGISTERED *pr_reg for CLEAR\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002660 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002661 }
2662 /*
2663 * From spc4r17 section 5.7.11.6, Clearing:
2664 *
2665 * Any application client may release the persistent reservation and
2666 * remove all registrations from a device server by issuing a
2667 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2668 * registered I_T nexus with the following parameter:
2669 *
2670 * a) RESERVATION KEY field set to the value of the reservation key
2671 * that is registered with the logical unit for the I_T nexus.
2672 */
2673 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002674 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002675 " res_key: 0x%016Lx does not match"
2676 " existing SA REGISTER res_key:"
2677 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2678 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002679 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002680 }
2681 /*
2682 * a) Release the persistent reservation, if any;
2683 */
2684 spin_lock(&dev->dev_reservation_lock);
2685 pr_res_holder = dev->dev_pr_res_holder;
2686 if (pr_res_holder) {
2687 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2688 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002689 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002690 }
2691 spin_unlock(&dev->dev_reservation_lock);
2692 /*
2693 * b) Remove all registration(s) (see spc4r17 5.7.7);
2694 */
2695 spin_lock(&pr_tmpl->registration_lock);
2696 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2697 &pr_tmpl->registration_list, pr_reg_list) {
2698
2699 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2700 pr_reg_nacl = pr_reg->pr_reg_nacl;
2701 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2702 __core_scsi3_free_registration(dev, pr_reg, NULL,
2703 calling_it_nexus);
2704 /*
2705 * e) Establish a unit attention condition for the initiator
2706 * port associated with every registered I_T nexus other
2707 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2708 * command with CLEAR service action was received, with the
2709 * additional sense code set to RESERVATIONS PREEMPTED.
2710 */
Andy Grover6708bb22011-06-08 10:36:43 -07002711 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002712 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002713 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2714 }
2715 spin_unlock(&pr_tmpl->registration_lock);
2716
Andy Grover6708bb22011-06-08 10:36:43 -07002717 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002718 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002719
Andy Grover459f2132013-05-16 10:41:02 -07002720 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002721
2722 core_scsi3_pr_generation(dev);
2723 return 0;
2724}
2725
2726/*
2727 * Called with struct se_device->dev_reservation_lock held.
2728 */
2729static void __core_scsi3_complete_pro_preempt(
2730 struct se_device *dev,
2731 struct t10_pr_registration *pr_reg,
2732 struct list_head *preempt_and_abort_list,
2733 int type,
2734 int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07002735 enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002736{
2737 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002738 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002739 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002740
2741 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002742 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002743 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002744 * Do an implicit RELEASE of the existing reservation.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002745 */
2746 if (dev->dev_pr_res_holder)
2747 __core_scsi3_complete_pro_release(dev, nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002748 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002749
2750 dev->dev_pr_res_holder = pr_reg;
2751 pr_reg->pr_res_holder = 1;
2752 pr_reg->pr_res_type = type;
2753 pr_reg->pr_res_scope = scope;
2754
Andy Grover6708bb22011-06-08 10:36:43 -07002755 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002757 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758 core_scsi3_pr_dump_type(type),
2759 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002760 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002761 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Andy Groverd2843c12013-05-16 10:40:55 -07002762 nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002763 /*
2764 * For PREEMPT_AND_ABORT, add the preempting reservation's
2765 * struct t10_pr_registration to the list that will be compared
2766 * against received CDBs..
2767 */
2768 if (preempt_and_abort_list)
2769 list_add_tail(&pr_reg->pr_reg_abort_list,
2770 preempt_and_abort_list);
2771}
2772
2773static void core_scsi3_release_preempt_and_abort(
2774 struct list_head *preempt_and_abort_list,
2775 struct t10_pr_registration *pr_reg_holder)
2776{
2777 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2778
2779 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2780 pr_reg_abort_list) {
2781
2782 list_del(&pr_reg->pr_reg_abort_list);
2783 if (pr_reg_holder == pr_reg)
2784 continue;
2785 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002786 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002787 continue;
2788 }
2789
2790 pr_reg->pr_reg_deve = NULL;
2791 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2793 }
2794}
2795
Christoph Hellwigde103c92012-11-06 12:24:09 -08002796static sense_reason_t
2797core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07002798 u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799{
Andy Grover5951146d2011-07-19 10:26:37 +00002800 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002802 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08002803 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002805 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002806 u64 pr_res_mapped_lun = 0;
Steven Allenb6932ee2014-10-24 15:18:26 -07002807 int all_reg = 0, calling_it_nexus = 0;
2808 bool sa_res_key_unmatched = sa_res_key != 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002809 int prh_type = 0, prh_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810
Christoph Hellwigde103c92012-11-06 12:24:09 -08002811 if (!se_sess)
2812 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002813
Andy Grover5951146d2011-07-19 10:26:37 +00002814 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002815 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002816 if (!pr_reg_n) {
2817 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002819 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002820 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002821 }
2822 if (pr_reg_n->pr_res_key != res_key) {
2823 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002824 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825 }
2826 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002827 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002828 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002829 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002830 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002831
2832 spin_lock(&dev->dev_reservation_lock);
2833 pr_res_holder = dev->dev_pr_res_holder;
2834 if (pr_res_holder &&
2835 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2836 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2837 all_reg = 1;
2838
Andy Grover6708bb22011-06-08 10:36:43 -07002839 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840 spin_unlock(&dev->dev_reservation_lock);
2841 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002842 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843 }
2844 /*
2845 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2846 *
2847 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2848 * persistent reservation holder or there is no persistent reservation
2849 * holder (i.e., there is no persistent reservation), then the device
2850 * server shall perform a preempt by doing the following in an
2851 * uninterrupted series of actions. (See below..)
2852 */
Andy Grover6708bb22011-06-08 10:36:43 -07002853 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002854 /*
2855 * No existing or SA Reservation Key matching reservations..
2856 *
2857 * PROUT SA PREEMPT with All Registrant type reservations are
2858 * allowed to be processed without a matching SA Reservation Key
2859 */
2860 spin_lock(&pr_tmpl->registration_lock);
2861 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2862 &pr_tmpl->registration_list, pr_reg_list) {
2863 /*
2864 * Removing of registrations in non all registrants
2865 * type reservations without a matching SA reservation
2866 * key.
2867 *
2868 * a) Remove the registrations for all I_T nexuses
2869 * specified by the SERVICE ACTION RESERVATION KEY
2870 * field;
2871 * b) Ignore the contents of the SCOPE and TYPE fields;
2872 * c) Process tasks as defined in 5.7.1; and
2873 * d) Establish a unit attention condition for the
2874 * initiator port associated with every I_T nexus
2875 * that lost its registration other than the I_T
2876 * nexus on which the PERSISTENT RESERVE OUT command
2877 * was received, with the additional sense code set
2878 * to REGISTRATIONS PREEMPTED.
2879 */
Andy Grover6708bb22011-06-08 10:36:43 -07002880 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881 if (pr_reg->pr_res_key != sa_res_key)
2882 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002883 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002884
2885 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2886 pr_reg_nacl = pr_reg->pr_reg_nacl;
2887 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2888 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002889 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002890 NULL, calling_it_nexus);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002891 } else {
2892 /*
2893 * Case for any existing all registrants type
2894 * reservation, follow logic in spc4r17 section
2895 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2896 *
2897 * For a ZERO SA Reservation key, release
Hannes Reinecke125d0112013-11-19 09:07:46 +01002898 * all other registrations and do an implicit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002899 * release of active persistent reservation.
2900 *
2901 * For a non-ZERO SA Reservation key, only
2902 * release the matching reservation key from
2903 * registrations.
2904 */
2905 if ((sa_res_key) &&
2906 (pr_reg->pr_res_key != sa_res_key))
2907 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002908 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002909
2910 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2911 if (calling_it_nexus)
2912 continue;
2913
2914 pr_reg_nacl = pr_reg->pr_reg_nacl;
2915 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2916 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002917 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918 NULL, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919 }
Andy Grover6708bb22011-06-08 10:36:43 -07002920 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002921 target_ua_allocate_lun(pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08002923 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002924 }
2925 spin_unlock(&pr_tmpl->registration_lock);
2926 /*
2927 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2928 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2929 * RESERVATION KEY field to a value that does not match any
2930 * registered reservation key, then the device server shall
2931 * complete the command with RESERVATION CONFLICT status.
2932 */
Steven Allenb6932ee2014-10-24 15:18:26 -07002933 if (sa_res_key_unmatched) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002934 spin_unlock(&dev->dev_reservation_lock);
2935 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002936 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002937 }
2938 /*
2939 * For an existing all registrants type reservation
2940 * with a zero SA rservation key, preempt the existing
2941 * reservation with the new PR type and scope.
2942 */
2943 if (pr_res_holder && all_reg && !(sa_res_key)) {
2944 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07002945 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2946 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002947
Andy Grover33ce6a82013-05-16 10:40:54 -07002948 if (preempt_type == PREEMPT_AND_ABORT)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002949 core_scsi3_release_preempt_and_abort(
2950 &preempt_and_abort_list, pr_reg_n);
2951 }
2952 spin_unlock(&dev->dev_reservation_lock);
2953
Andy Grover459f2132013-05-16 10:41:02 -07002954 if (pr_tmpl->pr_aptpl_active)
2955 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002956
2957 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00002958 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002959 return 0;
2960 }
2961 /*
2962 * The PREEMPTing SA reservation key matches that of the
2963 * existing persistent reservation, first, we check if
2964 * we are preempting our own reservation.
2965 * From spc4r17, section 5.7.11.4.3 Preempting
2966 * persistent reservations and registration handling
2967 *
2968 * If an all registrants persistent reservation is not
2969 * present, it is not an error for the persistent
2970 * reservation holder to preempt itself (i.e., a
2971 * PERSISTENT RESERVE OUT with a PREEMPT service action
2972 * or a PREEMPT AND ABORT service action with the
2973 * SERVICE ACTION RESERVATION KEY value equal to the
2974 * persistent reservation holder's reservation key that
2975 * is received from the persistent reservation holder).
2976 * In that case, the device server shall establish the
2977 * new persistent reservation and maintain the
2978 * registration.
2979 */
2980 prh_type = pr_res_holder->pr_res_type;
2981 prh_scope = pr_res_holder->pr_res_scope;
2982 /*
2983 * If the SERVICE ACTION RESERVATION KEY field identifies a
2984 * persistent reservation holder (see 5.7.10), the device
2985 * server shall perform a preempt by doing the following as
2986 * an uninterrupted series of actions:
2987 *
2988 * a) Release the persistent reservation for the holder
2989 * identified by the SERVICE ACTION RESERVATION KEY field;
2990 */
2991 if (pr_reg_n != pr_res_holder)
2992 __core_scsi3_complete_pro_release(dev,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002993 pr_res_holder->pr_reg_nacl,
2994 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002995 /*
2996 * b) Remove the registrations for all I_T nexuses identified
2997 * by the SERVICE ACTION RESERVATION KEY field, except the
2998 * I_T nexus that is being used for the PERSISTENT RESERVE
2999 * OUT command. If an all registrants persistent reservation
3000 * is present and the SERVICE ACTION RESERVATION KEY field
3001 * is set to zero, then all registrations shall be removed
3002 * except for that of the I_T nexus that is being used for
3003 * the PERSISTENT RESERVE OUT command;
3004 */
3005 spin_lock(&pr_tmpl->registration_lock);
3006 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3007 &pr_tmpl->registration_list, pr_reg_list) {
3008
3009 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3010 if (calling_it_nexus)
3011 continue;
3012
3013 if (pr_reg->pr_res_key != sa_res_key)
3014 continue;
3015
3016 pr_reg_nacl = pr_reg->pr_reg_nacl;
3017 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3018 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07003019 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003020 calling_it_nexus);
3021 /*
3022 * e) Establish a unit attention condition for the initiator
3023 * port associated with every I_T nexus that lost its
3024 * persistent reservation and/or registration, with the
3025 * additional sense code set to REGISTRATIONS PREEMPTED;
3026 */
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003027 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003028 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003029 }
3030 spin_unlock(&pr_tmpl->registration_lock);
3031 /*
3032 * c) Establish a persistent reservation for the preempting
3033 * I_T nexus using the contents of the SCOPE and TYPE fields;
3034 */
3035 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07003036 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3037 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003038 /*
3039 * d) Process tasks as defined in 5.7.1;
3040 * e) See above..
3041 * f) If the type or scope has changed, then for every I_T nexus
3042 * whose reservation key was not removed, except for the I_T
3043 * nexus on which the PERSISTENT RESERVE OUT command was
3044 * received, the device server shall establish a unit
3045 * attention condition for the initiator port associated with
3046 * that I_T nexus, with the additional sense code set to
3047 * RESERVATIONS RELEASED. If the type or scope have not
3048 * changed, then no unit attention condition(s) shall be
3049 * established for this reason.
3050 */
3051 if ((prh_type != type) || (prh_scope != scope)) {
3052 spin_lock(&pr_tmpl->registration_lock);
3053 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3054 &pr_tmpl->registration_list, pr_reg_list) {
3055
3056 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3057 if (calling_it_nexus)
3058 continue;
3059
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003060 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003061 pr_reg->pr_res_mapped_lun, 0x2A,
3062 ASCQ_2AH_RESERVATIONS_RELEASED);
3063 }
3064 spin_unlock(&pr_tmpl->registration_lock);
3065 }
3066 spin_unlock(&dev->dev_reservation_lock);
3067 /*
3068 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3069 * All received CDBs for the matching existing reservation and
3070 * registrations undergo ABORT_TASK logic.
3071 *
3072 * From there, core_scsi3_release_preempt_and_abort() will
3073 * release every registration in the list (which have already
3074 * been removed from the primary pr_reg list), except the
3075 * new persistent reservation holder, the calling Initiator Port.
3076 */
Andy Grover33ce6a82013-05-16 10:40:54 -07003077 if (preempt_type == PREEMPT_AND_ABORT) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3079 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3080 pr_reg_n);
3081 }
3082
Andy Grover459f2132013-05-16 10:41:02 -07003083 if (pr_tmpl->pr_aptpl_active)
3084 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003085
3086 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003087 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003088 return 0;
3089}
3090
Christoph Hellwigde103c92012-11-06 12:24:09 -08003091static sense_reason_t
3092core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003093 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003094{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095 switch (type) {
3096 case PR_TYPE_WRITE_EXCLUSIVE:
3097 case PR_TYPE_EXCLUSIVE_ACCESS:
3098 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3099 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3100 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3101 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08003102 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07003103 sa_res_key, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003104 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003105 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Andy Grover33ce6a82013-05-16 10:40:54 -07003106 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003107 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003108 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003109}
3110
3111
Christoph Hellwigde103c92012-11-06 12:24:09 -08003112static sense_reason_t
3113core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3114 u64 sa_res_key, int aptpl, int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115{
Andy Grovere3d6f902011-07-19 08:55:10 +00003116 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003117 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003118 struct se_dev_entry *dest_se_deve = NULL;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003119 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003120 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003122 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003124 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003125 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003126 const unsigned char *initiator_str;
Julia Lawall13ba5642014-11-30 19:14:12 +01003127 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 u32 tid_len, tmp_tid_len;
Andy Groverd2843c12013-05-16 10:40:55 -07003129 int new_reg = 0, type, scope, matching_iname;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003130 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003131 unsigned short rtpi;
3132 unsigned char proto_ident;
3133
Andy Grover6708bb22011-06-08 10:36:43 -07003134 if (!se_sess || !se_lun) {
3135 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003136 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003137 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003138
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3140 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003141 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142 /*
3143 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3144 * Register behaviors for a REGISTER AND MOVE service action
3145 *
3146 * Locate the existing *pr_reg via struct se_node_acl pointers
3147 */
Andy Grover5951146d2011-07-19 10:26:37 +00003148 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003149 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003150 if (!pr_reg) {
3151 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003152 " *pr_reg for REGISTER_AND_MOVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003153 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 }
3155 /*
3156 * The provided reservation key much match the existing reservation key
3157 * provided during this initiator's I_T nexus registration.
3158 */
3159 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003160 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161 " res_key: 0x%016Lx does not match existing SA REGISTER"
3162 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003163 ret = TCM_RESERVATION_CONFLICT;
3164 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003165 }
3166 /*
3167 * The service active reservation key needs to be non zero
3168 */
Andy Grover6708bb22011-06-08 10:36:43 -07003169 if (!sa_res_key) {
3170 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003171 " sa_res_key\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003172 ret = TCM_INVALID_PARAMETER_LIST;
3173 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003175
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 /*
3177 * Determine the Relative Target Port Identifier where the reservation
3178 * will be moved to for the TransportID containing SCSI initiator WWN
3179 * information.
3180 */
Andy Grover49493142012-01-16 16:57:08 -08003181 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003182 if (!buf) {
3183 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3184 goto out_put_pr_reg;
3185 }
3186
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 rtpi = (buf[18] & 0xff) << 8;
3188 rtpi |= buf[19] & 0xff;
3189 tid_len = (buf[20] & 0xff) << 24;
3190 tid_len |= (buf[21] & 0xff) << 16;
3191 tid_len |= (buf[22] & 0xff) << 8;
3192 tid_len |= buf[23] & 0xff;
Andy Grover49493142012-01-16 16:57:08 -08003193 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003194 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003195
3196 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003197 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 " does not equal CDB data_length: %u\n", tid_len,
3199 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003200 ret = TCM_INVALID_PARAMETER_LIST;
3201 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003202 }
3203
3204 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003205 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3206 if (tmp_lun->lun_rtpi != rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003207 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003208 dest_se_tpg = tmp_lun->lun_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003209 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003210 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003211 continue;
3212
Joern Engel33940d02014-09-16 16:23:12 -04003213 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 spin_unlock(&dev->se_port_lock);
3215
Christoph Hellwigde103c92012-11-06 12:24:09 -08003216 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003217 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003218 " for dest_se_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04003219 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003220 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3221 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003222 }
3223
3224 spin_lock(&dev->se_port_lock);
3225 break;
3226 }
3227 spin_unlock(&dev->se_port_lock);
3228
Andy Grover6708bb22011-06-08 10:36:43 -07003229 if (!dest_se_tpg || !dest_tf_ops) {
3230 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003231 " fabric ops from Relative Target Port Identifier:"
3232 " %hu\n", rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003233 ret = TCM_INVALID_PARAMETER_LIST;
3234 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003235 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003236
Andy Grover49493142012-01-16 16:57:08 -08003237 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003238 if (!buf) {
3239 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3240 goto out_put_pr_reg;
3241 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003242 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003243
Andy Grover6708bb22011-06-08 10:36:43 -07003244 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003245 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003246
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003247 if (proto_ident != dest_se_tpg->proto_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07003248 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003249 " proto_ident: 0x%02x does not match ident: 0x%02x"
3250 " from fabric: %s\n", proto_ident,
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003251 dest_se_tpg->proto_id,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003252 dest_tf_ops->get_fabric_name());
Christoph Hellwigde103c92012-11-06 12:24:09 -08003253 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003254 goto out;
3255 }
Christoph Hellwig2650d712015-05-01 17:47:58 +02003256 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003257 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003258 if (!initiator_str) {
3259 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003260 " initiator_str from Transport ID\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003261 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003262 goto out;
3263 }
3264
Andy Grover49493142012-01-16 16:57:08 -08003265 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003266 buf = NULL;
3267
Andy Grover6708bb22011-06-08 10:36:43 -07003268 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3270 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3271 iport_ptr : "");
3272 /*
3273 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3274 * action specifies a TransportID that is the same as the initiator port
3275 * of the I_T nexus for the command received, then the command shall
3276 * be terminated with CHECK CONDITION status, with the sense key set to
3277 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3278 * IN PARAMETER LIST.
3279 */
3280 pr_reg_nacl = pr_reg->pr_reg_nacl;
3281 matching_iname = (!strcmp(initiator_str,
3282 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003283 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003284 goto after_iport_check;
3285
Andy Grover6708bb22011-06-08 10:36:43 -07003286 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3287 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003288 " matches: %s on received I_T Nexus\n", initiator_str,
3289 pr_reg_nacl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003290 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003291 goto out;
3292 }
Andy Grover6708bb22011-06-08 10:36:43 -07003293 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3294 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 " matches: %s %s on received I_T Nexus\n",
3296 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3297 pr_reg->pr_reg_isid);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003298 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003299 goto out;
3300 }
3301after_iport_check:
3302 /*
3303 * Locate the destination struct se_node_acl from the received Transport ID
3304 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003305 mutex_lock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3307 initiator_str);
Joern Engel33940d02014-09-16 16:23:12 -04003308 if (dest_node_acl)
3309 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003310 mutex_unlock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311
Andy Grover6708bb22011-06-08 10:36:43 -07003312 if (!dest_node_acl) {
3313 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003314 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3315 initiator_str);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003316 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003317 goto out;
3318 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003319
3320 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003321 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003322 " dest_node_acl\n");
Joern Engel33940d02014-09-16 16:23:12 -04003323 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003324 dest_node_acl = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003325 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326 goto out;
3327 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003328
Andy Grover6708bb22011-06-08 10:36:43 -07003329 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003330 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3331 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003332
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003333 /*
3334 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3335 * PORT IDENTIFIER.
3336 */
3337 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003338 if (!dest_se_deve) {
3339 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003340 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003341 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003342 goto out;
3343 }
3344
Christoph Hellwigde103c92012-11-06 12:24:09 -08003345 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003346 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07003347 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003348 dest_se_deve = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003349 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003350 goto out;
3351 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003352
Andy Grover6708bb22011-06-08 10:36:43 -07003353 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Hannes Reineckef2d30682015-06-10 08:41:22 +02003354 " ACL for dest_se_deve->mapped_lun: %llu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003355 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3356 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003357
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003358 /*
3359 * A persistent reservation needs to already existing in order to
3360 * successfully complete the REGISTER_AND_MOVE service action..
3361 */
3362 spin_lock(&dev->dev_reservation_lock);
3363 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003364 if (!pr_res_holder) {
3365 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003366 " currently held\n");
3367 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003368 ret = TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003369 goto out;
3370 }
3371 /*
3372 * The received on I_T Nexus must be the reservation holder.
3373 *
3374 * From spc4r17 section 5.7.8 Table 50 --
3375 * Register behaviors for a REGISTER AND MOVE service action
3376 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02003377 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003378 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003379 " Nexus is not reservation holder\n");
3380 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003381 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003382 goto out;
3383 }
3384 /*
3385 * From spc4r17 section 5.7.8: registering and moving reservation
3386 *
3387 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3388 * action is received and the established persistent reservation is a
3389 * Write Exclusive - All Registrants type or Exclusive Access -
3390 * All Registrants type reservation, then the command shall be completed
3391 * with RESERVATION CONFLICT status.
3392 */
3393 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3394 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003395 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003396 " reservation for type: %s\n",
3397 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3398 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003399 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003400 goto out;
3401 }
3402 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3403 /*
3404 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3405 */
3406 type = pr_res_holder->pr_res_type;
3407 scope = pr_res_holder->pr_res_type;
3408 /*
3409 * c) Associate the reservation key specified in the SERVICE ACTION
3410 * RESERVATION KEY field with the I_T nexus specified as the
3411 * destination of the register and move, where:
3412 * A) The I_T nexus is specified by the TransportID and the
3413 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3414 * B) Regardless of the TransportID format used, the association for
3415 * the initiator port is based on either the initiator port name
3416 * (see 3.1.71) on SCSI transport protocols where port names are
3417 * required or the initiator port identifier (see 3.1.70) on SCSI
3418 * transport protocols where port names are not required;
3419 * d) Register the reservation key specified in the SERVICE ACTION
3420 * RESERVATION KEY field;
3421 * e) Retain the reservation key specified in the SERVICE ACTION
3422 * RESERVATION KEY field and associated information;
3423 *
3424 * Also, It is not an error for a REGISTER AND MOVE service action to
3425 * register an I_T nexus that is already registered with the same
3426 * reservation key or a different reservation key.
3427 */
3428 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3429 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003430 if (!dest_pr_reg) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003431 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
3432 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
3433
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003434 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003435 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3436 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3437 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08003438 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003439 goto out;
3440 }
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003441 spin_lock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003442 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3443 iport_ptr);
3444 new_reg = 1;
3445 }
3446 /*
3447 * f) Release the persistent reservation for the persistent reservation
3448 * holder (i.e., the I_T nexus on which the
3449 */
3450 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08003451 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003452 /*
3453 * g) Move the persistent reservation to the specified I_T nexus using
3454 * the same scope and type as the persistent reservation released in
3455 * item f); and
3456 */
3457 dev->dev_pr_res_holder = dest_pr_reg;
3458 dest_pr_reg->pr_res_holder = 1;
3459 dest_pr_reg->pr_res_type = type;
3460 pr_reg->pr_res_scope = scope;
Andy Groverd2843c12013-05-16 10:40:55 -07003461 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003462 /*
3463 * Increment PRGeneration for existing registrations..
3464 */
Andy Grover6708bb22011-06-08 10:36:43 -07003465 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003466 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3467 spin_unlock(&dev->dev_reservation_lock);
3468
Andy Grover6708bb22011-06-08 10:36:43 -07003469 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003470 " created new reservation holder TYPE: %s on object RTPI:"
3471 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3472 core_scsi3_pr_dump_type(type), rtpi,
3473 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003474 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003475 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3476 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07003477 i_buf, dest_tf_ops->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003478 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3479 iport_ptr : "");
3480 /*
3481 * It is now safe to release configfs group dependencies for destination
3482 * of Transport ID Initiator Device/Port Identifier
3483 */
3484 core_scsi3_lunacl_undepend_item(dest_se_deve);
3485 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3486 core_scsi3_tpg_undepend_item(dest_se_tpg);
3487 /*
3488 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3489 * nexus on which PERSISTENT RESERVE OUT command was received.
3490 */
3491 if (unreg) {
3492 spin_lock(&pr_tmpl->registration_lock);
3493 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3494 spin_unlock(&pr_tmpl->registration_lock);
3495 } else
3496 core_scsi3_put_pr_reg(pr_reg);
3497
Andy Grover459f2132013-05-16 10:41:02 -07003498 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003499
Andy Grover49493142012-01-16 16:57:08 -08003500 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003501
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003502 core_scsi3_put_pr_reg(dest_pr_reg);
3503 return 0;
3504out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003505 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003506 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003507 if (dest_se_deve)
3508 core_scsi3_lunacl_undepend_item(dest_se_deve);
3509 if (dest_node_acl)
3510 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3511 core_scsi3_tpg_undepend_item(dest_se_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003512
3513out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003514 core_scsi3_put_pr_reg(pr_reg);
3515 return ret;
3516}
3517
3518static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3519{
3520 unsigned int __v1, __v2;
3521
3522 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3523 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3524
3525 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3526}
3527
3528/*
3529 * See spc4r17 section 6.14 Table 170
3530 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003531sense_reason_t
3532target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003533{
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003534 struct se_device *dev = cmd->se_dev;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003535 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003536 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003537 u64 res_key, sa_res_key;
3538 int sa, scope, type, aptpl;
3539 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003540 sense_reason_t ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003541
3542 /*
3543 * Following spc2r20 5.5.1 Reservations overview:
3544 *
3545 * If a logical unit has been reserved by any RESERVE command and is
3546 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3547 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3548 * initiator or service action and shall terminate with a RESERVATION
3549 * CONFLICT status.
3550 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003551 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003552 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3553 " SPC-2 reservation is held, returning"
3554 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003555 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003556 }
3557
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003558 /*
3559 * FIXME: A NULL struct se_session pointer means an this is not coming from
3560 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3561 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003562 if (!cmd->se_sess)
3563 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003564
3565 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003566 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003567 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003568 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003569 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003570
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003571 /*
3572 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3573 */
3574 sa = (cdb[1] & 0x1f);
3575 scope = (cdb[2] & 0xf0);
3576 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003577
Andy Grover49493142012-01-16 16:57:08 -08003578 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003579 if (!buf)
3580 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3581
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003582 /*
3583 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3584 */
3585 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3586 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3587 /*
3588 * REGISTER_AND_MOVE uses a different SA parameter list containing
3589 * SCSI TransportIDs.
3590 */
3591 if (sa != PRO_REGISTER_AND_MOVE) {
3592 spec_i_pt = (buf[20] & 0x08);
3593 all_tg_pt = (buf[20] & 0x04);
3594 aptpl = (buf[20] & 0x01);
3595 } else {
3596 aptpl = (buf[17] & 0x01);
3597 unreg = (buf[17] & 0x02);
3598 }
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003599 /*
3600 * If the backend device has been configured to force APTPL metadata
3601 * write-out, go ahead and propigate aptpl=1 down now.
3602 */
3603 if (dev->dev_attrib.force_pr_aptpl)
3604 aptpl = 1;
3605
Andy Grover49493142012-01-16 16:57:08 -08003606 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003607 buf = NULL;
3608
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003609 /*
3610 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3611 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003612 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3613 return TCM_INVALID_PARAMETER_LIST;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003614
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003615 /*
3616 * From spc4r17 section 6.14:
3617 *
3618 * If the SPEC_I_PT bit is set to zero, the service action is not
3619 * REGISTER AND MOVE, and the parameter list length is not 24, then
3620 * the command shall be terminated with CHECK CONDITION status, with
3621 * the sense key set to ILLEGAL REQUEST, and the additional sense
3622 * code set to PARAMETER LIST LENGTH ERROR.
3623 */
Andy Grover6708bb22011-06-08 10:36:43 -07003624 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003625 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003626 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003627 " list length: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003628 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003629 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003630
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003631 /*
3632 * (core_scsi3_emulate_pro_* function parameters
3633 * are defined by spc4r17 Table 174:
3634 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3635 */
3636 switch (sa) {
3637 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003638 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003639 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003640 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003641 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003642 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3643 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003644 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003645 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3646 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003647 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003648 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3649 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003650 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003651 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003652 res_key, sa_res_key, PREEMPT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003653 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003654 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003655 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003656 res_key, sa_res_key, PREEMPT_AND_ABORT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003657 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003658 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003659 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003660 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003661 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003662 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003663 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003664 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003665 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003666 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003667 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003668 " action: 0x%02x\n", cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003669 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003670 }
3671
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003672 if (!ret)
3673 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003674 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003675}
3676
3677/*
3678 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3679 *
3680 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3681 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003682static sense_reason_t
3683core_scsi3_pri_read_keys(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003684{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003685 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003686 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003687 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003688 u32 add_len = 0, off = 8;
3689
3690 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003691 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003692 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003693 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003694 }
3695
Andy Grover49493142012-01-16 16:57:08 -08003696 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003697 if (!buf)
3698 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3699
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003700 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3701 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3702 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3703 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003704
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003705 spin_lock(&dev->t10_pr.registration_lock);
3706 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003707 pr_reg_list) {
3708 /*
3709 * Check for overflow of 8byte PRI READ_KEYS payload and
3710 * next reservation key list descriptor.
3711 */
3712 if ((add_len + 8) > (cmd->data_length - 8))
3713 break;
3714
3715 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3716 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3717 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3718 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3719 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3720 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3721 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3722 buf[off++] = (pr_reg->pr_res_key & 0xff);
3723
3724 add_len += 8;
3725 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003726 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003727
3728 buf[4] = ((add_len >> 24) & 0xff);
3729 buf[5] = ((add_len >> 16) & 0xff);
3730 buf[6] = ((add_len >> 8) & 0xff);
3731 buf[7] = (add_len & 0xff);
3732
Andy Grover49493142012-01-16 16:57:08 -08003733 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003734
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003735 return 0;
3736}
3737
3738/*
3739 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3740 *
3741 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3742 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003743static sense_reason_t
3744core_scsi3_pri_read_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003745{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003746 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003747 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003748 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003749 u64 pr_res_key;
3750 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3751
3752 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003753 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003754 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003755 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003756 }
3757
Andy Grover49493142012-01-16 16:57:08 -08003758 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003759 if (!buf)
3760 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3761
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003762 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3763 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3764 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3765 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003766
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003767 spin_lock(&dev->dev_reservation_lock);
3768 pr_reg = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07003769 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003770 /*
3771 * Set the hardcoded Additional Length
3772 */
3773 buf[4] = ((add_len >> 24) & 0xff);
3774 buf[5] = ((add_len >> 16) & 0xff);
3775 buf[6] = ((add_len >> 8) & 0xff);
3776 buf[7] = (add_len & 0xff);
3777
Andy Grover05d1c7c2011-07-20 19:13:28 +00003778 if (cmd->data_length < 22)
3779 goto err;
3780
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003781 /*
3782 * Set the Reservation key.
3783 *
3784 * From spc4r17, section 5.7.10:
3785 * A persistent reservation holder has its reservation key
3786 * returned in the parameter data from a PERSISTENT
3787 * RESERVE IN command with READ RESERVATION service action as
3788 * follows:
3789 * a) For a persistent reservation of the type Write Exclusive
3790 * - All Registrants or Exclusive Access ­ All Regitrants,
3791 * the reservation key shall be set to zero; or
3792 * b) For all other persistent reservation types, the
3793 * reservation key shall be set to the registered
3794 * reservation key for the I_T nexus that holds the
3795 * persistent reservation.
3796 */
3797 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3798 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3799 pr_res_key = 0;
3800 else
3801 pr_res_key = pr_reg->pr_res_key;
3802
3803 buf[8] = ((pr_res_key >> 56) & 0xff);
3804 buf[9] = ((pr_res_key >> 48) & 0xff);
3805 buf[10] = ((pr_res_key >> 40) & 0xff);
3806 buf[11] = ((pr_res_key >> 32) & 0xff);
3807 buf[12] = ((pr_res_key >> 24) & 0xff);
3808 buf[13] = ((pr_res_key >> 16) & 0xff);
3809 buf[14] = ((pr_res_key >> 8) & 0xff);
3810 buf[15] = (pr_res_key & 0xff);
3811 /*
3812 * Set the SCOPE and TYPE
3813 */
3814 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3815 (pr_reg->pr_res_type & 0x0f);
3816 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003817
3818err:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003819 spin_unlock(&dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08003820 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003821
3822 return 0;
3823}
3824
3825/*
3826 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3827 *
3828 * See spc4r17 section 6.13.4 Table 165
3829 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003830static sense_reason_t
3831core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003832{
Andy Grover5951146d2011-07-19 10:26:37 +00003833 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003834 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003835 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003836 u16 add_len = 8; /* Hardcoded to 8. */
3837
3838 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07003839 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003840 " %u too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003841 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003842 }
3843
Andy Grover49493142012-01-16 16:57:08 -08003844 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003845 if (!buf)
3846 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003847
Joern Engelc4352852014-09-02 17:50:01 -04003848 buf[0] = ((add_len >> 8) & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003849 buf[1] = (add_len & 0xff);
3850 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3851 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3852 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3853 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3854 /*
3855 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3856 * set the TMV: Task Mask Valid bit.
3857 */
3858 buf[3] |= 0x80;
3859 /*
3860 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3861 */
3862 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3863 /*
3864 * PTPL_A: Persistence across Target Power Loss Active bit
3865 */
3866 if (pr_tmpl->pr_aptpl_active)
3867 buf[3] |= 0x01;
3868 /*
3869 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3870 */
3871 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3872 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3873 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3874 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3875 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3876 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3877
Andy Grover49493142012-01-16 16:57:08 -08003878 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003879
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003880 return 0;
3881}
3882
3883/*
3884 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3885 *
3886 * See spc4r17 section 6.13.5 Table 168 and 169
3887 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003888static sense_reason_t
3889core_scsi3_pri_read_full_status(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003890{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003891 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003892 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003893 struct se_portal_group *se_tpg;
3894 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003895 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003896 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003897 u32 add_desc_len = 0, add_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003898 u32 off = 8; /* off into first Full Status descriptor */
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003899 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003900 int exp_desc_len, desc_len;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003901 bool all_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003902
3903 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003904 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003905 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003906 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003907 }
3908
Andy Grover49493142012-01-16 16:57:08 -08003909 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003910 if (!buf)
3911 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003912
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003913 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3914 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3915 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3916 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003917
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003918 spin_lock(&dev->dev_reservation_lock);
3919 if (dev->dev_pr_res_holder) {
3920 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3921
3922 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3923 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3924 all_reg = true;
3925 pr_res_type = pr_holder->pr_res_type;
3926 pr_res_scope = pr_holder->pr_res_scope;
3927 }
3928 }
3929 spin_unlock(&dev->dev_reservation_lock);
3930
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003931 spin_lock(&pr_tmpl->registration_lock);
3932 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3933 &pr_tmpl->registration_list, pr_reg_list) {
3934
3935 se_nacl = pr_reg->pr_reg_nacl;
3936 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3937 add_desc_len = 0;
3938
Joern Engel33940d02014-09-16 16:23:12 -04003939 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003940 spin_unlock(&pr_tmpl->registration_lock);
3941 /*
3942 * Determine expected length of $FABRIC_MOD specific
3943 * TransportID full status descriptor..
3944 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02003945 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3946 &format_code);
3947 if (exp_desc_len < 0 ||
3948 exp_desc_len + add_len > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003949 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003950 " out of buffer: %d\n", cmd->data_length);
3951 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04003952 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003953 break;
3954 }
3955 /*
3956 * Set RESERVATION KEY
3957 */
3958 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3959 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3960 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3961 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3962 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3963 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3964 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3965 buf[off++] = (pr_reg->pr_res_key & 0xff);
3966 off += 4; /* Skip Over Reserved area */
3967
3968 /*
3969 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3970 */
3971 if (pr_reg->pr_reg_all_tg_pt)
3972 buf[off] = 0x02;
3973 /*
3974 * The struct se_lun pointer will be present for the
3975 * reservation holder for PR_HOLDER bit.
3976 *
3977 * Also, if this registration is the reservation
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003978 * holder or there is an All Registrants reservation
3979 * active, fill in SCOPE and TYPE in the next byte.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003980 */
3981 if (pr_reg->pr_res_holder) {
3982 buf[off++] |= 0x01;
3983 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3984 (pr_reg->pr_res_type & 0x0f);
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003985 } else if (all_reg) {
3986 buf[off++] |= 0x01;
3987 buf[off++] = (pr_res_scope & 0xf0) |
3988 (pr_res_type & 0x0f);
3989 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003990 off += 2;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003991 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003992
3993 off += 4; /* Skip over reserved area */
3994 /*
3995 * From spc4r17 6.3.15:
3996 *
3997 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3998 * IDENTIFIER field contains the relative port identifier (see
3999 * 3.1.120) of the target port that is part of the I_T nexus
4000 * described by this full status descriptor. If the ALL_TG_PT
4001 * bit is set to one, the contents of the RELATIVE TARGET PORT
4002 * IDENTIFIER field are not defined by this standard.
4003 */
Andy Grover6708bb22011-06-08 10:36:43 -07004004 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00004005 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004006
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00004007 buf[off++] = ((sep_rtpi >> 8) & 0xff);
4008 buf[off++] = (sep_rtpi & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004009 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09004010 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004011
Christoph Hellwig2650d712015-05-01 17:47:58 +02004012 buf[off+4] = se_tpg->proto_id;
4013
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004014 /*
Christoph Hellwig2650d712015-05-01 17:47:58 +02004015 * Now, have the $FABRIC_MOD fill in the transport ID.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004016 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02004017 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4018 &format_code, &buf[off+4]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004019
4020 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04004021 atomic_dec_mb(&pr_reg->pr_res_holders);
Christoph Hellwig2650d712015-05-01 17:47:58 +02004022
4023 if (desc_len < 0)
4024 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004025 /*
4026 * Set the ADDITIONAL DESCRIPTOR LENGTH
4027 */
4028 buf[off++] = ((desc_len >> 24) & 0xff);
4029 buf[off++] = ((desc_len >> 16) & 0xff);
4030 buf[off++] = ((desc_len >> 8) & 0xff);
4031 buf[off++] = (desc_len & 0xff);
4032 /*
4033 * Size of full desctipor header minus TransportID
4034 * containing $FABRIC_MOD specific) initiator device/port
4035 * WWN information.
4036 *
4037 * See spc4r17 Section 6.13.5 Table 169
4038 */
4039 add_desc_len = (24 + desc_len);
4040
4041 off += desc_len;
4042 add_len += add_desc_len;
4043 }
4044 spin_unlock(&pr_tmpl->registration_lock);
4045 /*
4046 * Set ADDITIONAL_LENGTH
4047 */
4048 buf[4] = ((add_len >> 24) & 0xff);
4049 buf[5] = ((add_len >> 16) & 0xff);
4050 buf[6] = ((add_len >> 8) & 0xff);
4051 buf[7] = (add_len & 0xff);
4052
Andy Grover49493142012-01-16 16:57:08 -08004053 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004054
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004055 return 0;
4056}
4057
Christoph Hellwigde103c92012-11-06 12:24:09 -08004058sense_reason_t
4059target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004060{
Christoph Hellwigde103c92012-11-06 12:24:09 -08004061 sense_reason_t ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004062
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004063 /*
4064 * Following spc2r20 5.5.1 Reservations overview:
4065 *
4066 * If a logical unit has been reserved by any RESERVE command and is
4067 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4068 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4069 * initiator or service action and shall terminate with a RESERVATION
4070 * CONFLICT status.
4071 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004072 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004073 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4074 " SPC-2 reservation is held, returning"
4075 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08004076 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004077 }
4078
4079 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004080 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004081 ret = core_scsi3_pri_read_keys(cmd);
4082 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004083 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004084 ret = core_scsi3_pri_read_reservation(cmd);
4085 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004086 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004087 ret = core_scsi3_pri_report_capabilities(cmd);
4088 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004089 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004090 ret = core_scsi3_pri_read_full_status(cmd);
4091 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004092 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004093 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004094 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08004095 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004096 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004097
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004098 if (!ret)
4099 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004100 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004101}
4102
Christoph Hellwigde103c92012-11-06 12:24:09 -08004103sense_reason_t
4104target_check_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004105{
Christoph Hellwigd977f432012-10-10 17:37:15 -04004106 struct se_device *dev = cmd->se_dev;
Christoph Hellwigde103c92012-11-06 12:24:09 -08004107 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004108
Christoph Hellwigd977f432012-10-10 17:37:15 -04004109 if (!cmd->se_sess)
4110 return 0;
4111 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4112 return 0;
Andy Grovera3541702015-05-19 14:44:41 -07004113 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04004114 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004115
Christoph Hellwigd977f432012-10-10 17:37:15 -04004116 spin_lock(&dev->dev_reservation_lock);
4117 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4118 ret = target_scsi2_reservation_check(cmd);
4119 else
4120 ret = target_scsi3_pr_reservation_check(cmd);
4121 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004122
Christoph Hellwigd977f432012-10-10 17:37:15 -04004123 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004124}