blob: e7933115087ab2fab461ee4b10c2012398eb73e0 [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 Bellinger3ccd6e82015-09-13 02:30:46 -0700621 struct se_dev_entry *dest_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;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700643 /*
644 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
645 * the se_dev_entry->pr_ref will have been already obtained by
646 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
647 *
648 * Otherwise, locate se_dev_entry now and obtain a reference until
649 * registration completes in __core_scsi3_add_registration().
650 */
651 if (dest_deve) {
652 pr_reg->pr_reg_deve = dest_deve;
653 } else {
654 rcu_read_lock();
655 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
656 if (!pr_reg->pr_reg_deve) {
657 rcu_read_unlock();
658 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
659 nacl->initiatorname, mapped_lun);
660 kmem_cache_free(t10_pr_reg_cache, pr_reg);
661 return NULL;
662 }
663 kref_get(&pr_reg->pr_reg_deve->pr_kref);
664 rcu_read_unlock();
665 }
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000666 pr_reg->pr_res_mapped_lun = mapped_lun;
667 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700668 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800669 pr_reg->pr_res_key = sa_res_key;
670 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
671 pr_reg->pr_reg_aptpl = aptpl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800672 /*
673 * If an ISID value for this SCSI Initiator Port exists,
674 * save it to the registration now.
675 */
676 if (isid != NULL) {
677 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
678 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
679 pr_reg->isid_present_at_reg = 1;
680 }
681
682 return pr_reg;
683}
684
685static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
686static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
687
688/*
689 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
690 * modes.
691 */
692static struct t10_pr_registration *__core_scsi3_alloc_registration(
693 struct se_device *dev,
694 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000695 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800696 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200697 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800698 unsigned char *isid,
699 u64 sa_res_key,
700 int all_tg_pt,
701 int aptpl)
702{
703 struct se_dev_entry *deve_tmp;
704 struct se_node_acl *nacl_tmp;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000705 struct se_lun_acl *lacl_tmp;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700706 struct se_lun *lun_tmp, *next, *dest_lun;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200707 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
709 int ret;
710 /*
711 * Create a registration for the I_T Nexus upon which the
712 * PROUT REGISTER was received.
713 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000714 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
715 isid, sa_res_key, all_tg_pt,
716 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700717 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800718 return NULL;
719 /*
720 * Return pointer to pr_reg for ALL_TG_PT=0
721 */
Andy Grover6708bb22011-06-08 10:36:43 -0700722 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800723 return pr_reg;
724 /*
725 * Create list of matching SCSI Initiator Port registrations
726 * for ALL_TG_PT=1
727 */
728 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700729 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700730 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
731 continue;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800732 spin_unlock(&dev->se_port_lock);
733
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700734 spin_lock(&lun_tmp->lun_deve_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700735 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800736 /*
737 * This pointer will be NULL for demo mode MappedLUNs
Hannes Reinecke125d0112013-11-19 09:07:46 +0100738 * that have not been make explicit via a ConfigFS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800739 * MappedLUN group for the SCSI Initiator Node ACL.
740 */
Andy Grover6708bb22011-06-08 10:36:43 -0700741 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742 continue;
743
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000744 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700745 lockdep_is_held(&lun_tmp->lun_deve_lock));
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000746 nacl_tmp = lacl_tmp->se_lun_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800747 /*
748 * Skip the matching struct se_node_acl that is allocated
749 * above..
750 */
751 if (nacl == nacl_tmp)
752 continue;
753 /*
754 * Only perform PR registrations for target ports on
755 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
756 * arrived.
757 */
758 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
759 continue;
760 /*
761 * Look for a matching Initiator Node ACL in ASCII format
762 */
763 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
764 continue;
765
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700766 kref_get(&deve_tmp->pr_kref);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700767 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800768 /*
769 * Grab a configfs group dependency that is released
770 * for the exception path at label out: below, or upon
771 * completion of adding ALL_TG_PT=1 registrations in
772 * __core_scsi3_add_registration()
773 */
774 ret = core_scsi3_lunacl_depend_item(deve_tmp);
775 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700776 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800777 "_item() failed\n");
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700778 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700779 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800780 goto out;
781 }
782 /*
783 * Located a matching SCSI Initiator Port on a different
784 * port, allocate the pr_reg_atp and attach it to the
785 * pr_reg->pr_reg_atp_list that will be processed once
786 * the original *pr_reg is processed in
787 * __core_scsi3_add_registration()
788 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700789 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000790 atomic_read(&deve_tmp->pr_kref.refcount) != 0);
791
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800792 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700793 nacl_tmp, dest_lun, deve_tmp,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000794 deve_tmp->mapped_lun, NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800795 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700796 if (!pr_reg_atp) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700797 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800798 core_scsi3_lunacl_undepend_item(deve_tmp);
799 goto out;
800 }
801
802 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
803 &pr_reg->pr_reg_atp_list);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700804 spin_lock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805 }
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700806 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807
808 spin_lock(&dev->se_port_lock);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700809 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810 }
811 spin_unlock(&dev->se_port_lock);
812
813 return pr_reg;
814out:
815 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
816 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
817 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
818 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
819 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
820 }
821 kmem_cache_free(t10_pr_reg_cache, pr_reg);
822 return NULL;
823}
824
825int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000826 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800827 u64 sa_res_key,
828 unsigned char *i_port,
829 unsigned char *isid,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200830 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 unsigned char *t_port,
832 u16 tpgt,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200833 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834 int res_holder,
835 int all_tg_pt,
836 u8 type)
837{
838 struct t10_pr_registration *pr_reg;
839
Andy Grover6708bb22011-06-08 10:36:43 -0700840 if (!i_port || !t_port || !sa_res_key) {
841 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000842 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 }
844
845 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700846 if (!pr_reg) {
847 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000848 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800850
851 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
852 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
853 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
854 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
855 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
856 atomic_set(&pr_reg->pr_res_holders, 0);
857 pr_reg->pr_reg_nacl = NULL;
858 pr_reg->pr_reg_deve = NULL;
859 pr_reg->pr_res_mapped_lun = mapped_lun;
860 pr_reg->pr_aptpl_target_lun = target_lun;
861 pr_reg->pr_res_key = sa_res_key;
862 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
863 pr_reg->pr_reg_aptpl = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800864 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
865 pr_reg->pr_res_type = type;
866 /*
867 * If an ISID value had been saved in APTPL metadata for this
868 * SCSI Initiator Port, restore it now.
869 */
870 if (isid != NULL) {
871 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
872 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
873 pr_reg->isid_present_at_reg = 1;
874 }
875 /*
876 * Copy the i_port and t_port information from caller.
877 */
878 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
879 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
880 pr_reg->pr_reg_tpgt = tpgt;
881 /*
882 * Set pr_res_holder from caller, the pr_reg who is the reservation
883 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
884 * the Initiator Node LUN ACL from the fabric module is created for
885 * this registration.
886 */
887 pr_reg->pr_res_holder = res_holder;
888
889 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700890 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800891 " metadata\n", (res_holder) ? "+reservation" : "");
892 return 0;
893}
894
895static void core_scsi3_aptpl_reserve(
896 struct se_device *dev,
897 struct se_portal_group *tpg,
898 struct se_node_acl *node_acl,
899 struct t10_pr_registration *pr_reg)
900{
901 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902
903 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700904 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800905
906 spin_lock(&dev->dev_reservation_lock);
907 dev->dev_pr_res_holder = pr_reg;
908 spin_unlock(&dev->dev_reservation_lock);
909
Andy Grover6708bb22011-06-08 10:36:43 -0700910 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000912 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
914 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700915 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000916 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700917 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800918}
919
920static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
Andy Grover33ce6a82013-05-16 10:40:54 -0700921 struct t10_pr_registration *, enum register_type, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800922
923static int __core_scsi3_check_aptpl_registration(
924 struct se_device *dev,
925 struct se_portal_group *tpg,
926 struct se_lun *lun,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200927 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800928 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200929 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800930{
931 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400932 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800933 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
934 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
935 u16 tpgt;
936
937 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
938 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
939 /*
940 * Copy Initiator Port information from struct se_node_acl
941 */
942 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
943 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000944 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
945 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800946 /*
947 * Look for the matching registrations+reservation from those
948 * created from APTPL metadata. Note that multiple registrations
949 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
950 * TransportIDs.
951 */
952 spin_lock(&pr_tmpl->aptpl_reg_lock);
953 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
954 pr_reg_aptpl_list) {
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000955
Andy Grover6708bb22011-06-08 10:36:43 -0700956 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700957 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958 !(strcmp(pr_reg->pr_tport, t_port)) &&
959 (pr_reg->pr_reg_tpgt == tpgt) &&
960 (pr_reg->pr_aptpl_target_lun == target_lun)) {
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700961 /*
962 * Obtain the ->pr_reg_deve pointer + reference, that
963 * is released by __core_scsi3_add_registration() below.
964 */
965 rcu_read_lock();
966 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
967 if (!pr_reg->pr_reg_deve) {
968 pr_err("Unable to locate PR APTPL %s mapped_lun:"
969 " %llu\n", nacl->initiatorname, mapped_lun);
970 rcu_read_unlock();
971 continue;
972 }
973 kref_get(&pr_reg->pr_reg_deve->pr_kref);
974 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800975
976 pr_reg->pr_reg_nacl = nacl;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700977 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800978 list_del(&pr_reg->pr_reg_aptpl_list);
979 spin_unlock(&pr_tmpl->aptpl_reg_lock);
980 /*
981 * At this point all of the pointers in *pr_reg will
982 * be setup, so go ahead and add the registration.
983 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800984 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
985 /*
986 * If this registration is the reservation holder,
987 * make that happen now..
988 */
989 if (pr_reg->pr_res_holder)
990 core_scsi3_aptpl_reserve(dev, tpg,
991 nacl, pr_reg);
992 /*
993 * Reenable pr_aptpl_active to accept new metadata
994 * updates once the SCSI device is active again..
995 */
996 spin_lock(&pr_tmpl->aptpl_reg_lock);
997 pr_tmpl->pr_aptpl_active = 1;
998 }
999 }
1000 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1001
1002 return 0;
1003}
1004
1005int core_scsi3_check_aptpl_registration(
1006 struct se_device *dev,
1007 struct se_portal_group *tpg,
1008 struct se_lun *lun,
Nicholas Bellingere2480562014-10-04 04:23:15 +00001009 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +02001010 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001011{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001012 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001013 return 0;
1014
1015 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001016 lun->unpacked_lun, nacl,
1017 mapped_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001018}
1019
1020static void __core_scsi3_dump_registration(
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001021 const struct target_core_fabric_ops *tfo,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001022 struct se_device *dev,
1023 struct se_node_acl *nacl,
1024 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001025 enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026{
1027 struct se_portal_group *se_tpg = nacl->se_tpg;
1028 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029
1030 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001031 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032
Andy Grover6708bb22011-06-08 10:36:43 -07001033 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Andy Grover33ce6a82013-05-16 10:40:54 -07001034 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
1035 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001037 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001038 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001039 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1040 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -07001041 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001042 " Port(s)\n", tfo->get_fabric_name(),
1043 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001044 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001045 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001046 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1047 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1048 pr_reg->pr_reg_aptpl);
1049}
1050
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001051static void __core_scsi3_add_registration(
1052 struct se_device *dev,
1053 struct se_node_acl *nacl,
1054 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001055 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001056 int register_move)
1057{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001058 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001060 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001061 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001062
1063 /*
1064 * Increment PRgeneration counter for struct se_device upon a successful
1065 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1066 *
1067 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1068 * action, the struct se_device->dev_reservation_lock will already be held,
1069 * so we do not call core_scsi3_pr_generation() which grabs the lock
1070 * for the REGISTER.
1071 */
1072 pr_reg->pr_res_generation = (register_move) ?
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001073 dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001074 core_scsi3_pr_generation(dev);
1075
1076 spin_lock(&pr_tmpl->registration_lock);
1077 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078
1079 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1080 spin_unlock(&pr_tmpl->registration_lock);
1081 /*
1082 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1083 */
Andy Grover6708bb22011-06-08 10:36:43 -07001084 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001085 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086 /*
1087 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1088 * allocated in __core_scsi3_alloc_registration()
1089 */
1090 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1091 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001092 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1093
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001094 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1095
1096 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1097
1098 spin_lock(&pr_tmpl->registration_lock);
1099 list_add_tail(&pr_reg_tmp->pr_reg_list,
1100 &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001101
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001102 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1103 register_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001104 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001105 /*
1106 * Drop configfs group dependency reference and deve->pr_kref
1107 * obtained from __core_scsi3_alloc_registration() code.
1108 */
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001109 rcu_read_lock();
1110 deve = pr_reg_tmp->pr_reg_deve;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001111 if (deve) {
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001112 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001113 core_scsi3_lunacl_undepend_item(deve);
1114 pr_reg_tmp->pr_reg_deve = NULL;
1115 }
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001116 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117 }
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001118out:
1119 /*
1120 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1121 */
1122 rcu_read_lock();
1123 deve = pr_reg->pr_reg_deve;
1124 if (deve) {
1125 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1126 kref_put(&deve->pr_kref, target_pr_kref_release);
1127 pr_reg->pr_reg_deve = NULL;
1128 }
1129 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130}
1131
1132static int core_scsi3_alloc_registration(
1133 struct se_device *dev,
1134 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001135 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +02001137 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138 unsigned char *isid,
1139 u64 sa_res_key,
1140 int all_tg_pt,
1141 int aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07001142 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 int register_move)
1144{
1145 struct t10_pr_registration *pr_reg;
1146
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001147 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1148 isid, sa_res_key, all_tg_pt,
1149 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001150 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001151 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152
1153 __core_scsi3_add_registration(dev, nacl, pr_reg,
1154 register_type, register_move);
1155 return 0;
1156}
1157
1158static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1159 struct se_device *dev,
1160 struct se_node_acl *nacl,
1161 unsigned char *isid)
1162{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001163 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1165 struct se_portal_group *tpg;
1166
1167 spin_lock(&pr_tmpl->registration_lock);
1168 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1169 &pr_tmpl->registration_list, pr_reg_list) {
1170 /*
1171 * First look for a matching struct se_node_acl
1172 */
1173 if (pr_reg->pr_reg_nacl != nacl)
1174 continue;
1175
1176 tpg = pr_reg->pr_reg_nacl->se_tpg;
1177 /*
1178 * If this registration does NOT contain a fabric provided
1179 * ISID, then we have found a match.
1180 */
Andy Grover6708bb22011-06-08 10:36:43 -07001181 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182 /*
1183 * Determine if this SCSI device server requires that
1184 * SCSI Intiatior TransportID w/ ISIDs is enforced
1185 * for fabric modules (iSCSI) requiring them.
1186 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001187 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001188 if (dev->dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189 continue;
1190 }
Joern Engel33940d02014-09-16 16:23:12 -04001191 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 spin_unlock(&pr_tmpl->registration_lock);
1193 return pr_reg;
1194 }
1195 /*
1196 * If the *pr_reg contains a fabric defined ISID for multi-value
1197 * SCSI Initiator Port TransportIDs, then we expect a valid
1198 * matching ISID to be provided by the local SCSI Initiator Port.
1199 */
Andy Grover6708bb22011-06-08 10:36:43 -07001200 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 continue;
1202 if (strcmp(isid, pr_reg->pr_reg_isid))
1203 continue;
1204
Joern Engel33940d02014-09-16 16:23:12 -04001205 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206 spin_unlock(&pr_tmpl->registration_lock);
1207 return pr_reg;
1208 }
1209 spin_unlock(&pr_tmpl->registration_lock);
1210
1211 return NULL;
1212}
1213
1214static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1215 struct se_device *dev,
1216 struct se_node_acl *nacl,
1217 struct se_session *sess)
1218{
1219 struct se_portal_group *tpg = nacl->se_tpg;
1220 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1221
Andy Grovere3d6f902011-07-19 08:55:10 +00001222 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001224 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225 PR_REG_ISID_LEN);
1226 isid_ptr = &buf[0];
1227 }
1228
1229 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1230}
1231
1232static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1233{
Joern Engel33940d02014-09-16 16:23:12 -04001234 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001235}
1236
Hannes Reinecke125d0112013-11-19 09:07:46 +01001237static int core_scsi3_check_implicit_release(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001238 struct se_device *dev,
1239 struct t10_pr_registration *pr_reg)
1240{
1241 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1242 struct t10_pr_registration *pr_res_holder;
1243 int ret = 0;
1244
1245 spin_lock(&dev->dev_reservation_lock);
1246 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001247 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248 spin_unlock(&dev->dev_reservation_lock);
1249 return ret;
1250 }
1251 if (pr_res_holder == pr_reg) {
1252 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001253 * Perform an implicit RELEASE if the registration that
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254 * is being released is holding the reservation.
1255 *
1256 * From spc4r17, section 5.7.11.1:
1257 *
1258 * e) If the I_T nexus is the persistent reservation holder
1259 * and the persistent reservation is not an all registrants
1260 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1261 * service action or REGISTER AND IGNORE EXISTING KEY
1262 * service action with the SERVICE ACTION RESERVATION KEY
1263 * field set to zero (see 5.7.11.3).
1264 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001265 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001266 ret = 1;
1267 /*
1268 * For 'All Registrants' reservation types, all existing
1269 * registrations are still processed as reservation holders
1270 * in core_scsi3_pr_seq_non_holder() after the initial
Hannes Reinecke125d0112013-11-19 09:07:46 +01001271 * reservation holder is implicitly released here.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001272 */
1273 } else if (pr_reg->pr_reg_all_tg_pt &&
1274 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1275 pr_reg->pr_reg_nacl->initiatorname)) &&
1276 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001277 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278 " UNREGISTER while existing reservation with matching"
1279 " key 0x%016Lx is present from another SCSI Initiator"
1280 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001281 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282 }
1283 spin_unlock(&dev->dev_reservation_lock);
1284
1285 return ret;
1286}
1287
1288/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001289 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290 */
1291static void __core_scsi3_free_registration(
1292 struct se_device *dev,
1293 struct t10_pr_registration *pr_reg,
1294 struct list_head *preempt_and_abort_list,
1295 int dec_holders)
Bart Van Asschecb0df4d2015-04-10 14:49:02 +02001296 __releases(&pr_tmpl->registration_lock)
1297 __acquires(&pr_tmpl->registration_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001299 const struct target_core_fabric_ops *tfo =
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001301 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001302 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1303 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305
1306 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001307 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001308
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001309 if (!list_empty(&pr_reg->pr_reg_list))
1310 list_del(&pr_reg->pr_reg_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001311 /*
1312 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1313 * so call core_scsi3_put_pr_reg() to decrement our reference.
1314 */
1315 if (dec_holders)
1316 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001317
1318 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319 /*
1320 * Wait until all reference from any other I_T nexuses for this
1321 * *pr_reg have been released. Because list_del() is called above,
1322 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1323 * count back to zero, and we release *pr_reg.
1324 */
1325 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001326 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327 tfo->get_fabric_name());
1328 cpu_relax();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001329 }
1330
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001331 rcu_read_lock();
1332 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1333 if (deve)
1334 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1335 rcu_read_unlock();
1336
1337 spin_lock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001338 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 " Node: %s%s\n", tfo->get_fabric_name(),
1340 pr_reg->pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001341 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001342 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001343 " Port(s)\n", tfo->get_fabric_name(),
1344 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001345 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001346 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1348 pr_reg->pr_res_generation);
1349
Andy Grover6708bb22011-06-08 10:36:43 -07001350 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351 pr_reg->pr_reg_deve = NULL;
1352 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1354 return;
1355 }
1356 /*
1357 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1358 * are released once the ABORT_TASK_SET has completed..
1359 */
1360 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1361}
1362
1363void core_scsi3_free_pr_reg_from_nacl(
1364 struct se_device *dev,
1365 struct se_node_acl *nacl)
1366{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001367 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001368 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001369 bool free_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370 /*
1371 * If the passed se_node_acl matches the reservation holder,
1372 * release the reservation.
1373 */
1374 spin_lock(&dev->dev_reservation_lock);
1375 pr_res_holder = dev->dev_pr_res_holder;
1376 if ((pr_res_holder != NULL) &&
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001377 (pr_res_holder->pr_reg_nacl == nacl)) {
1378 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1379 free_reg = true;
1380 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001381 spin_unlock(&dev->dev_reservation_lock);
1382 /*
1383 * Release any registration associated with the struct se_node_acl.
1384 */
1385 spin_lock(&pr_tmpl->registration_lock);
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001386 if (pr_res_holder && free_reg)
1387 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1388
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1390 &pr_tmpl->registration_list, pr_reg_list) {
1391
1392 if (pr_reg->pr_reg_nacl != nacl)
1393 continue;
1394
1395 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1396 }
1397 spin_unlock(&pr_tmpl->registration_lock);
1398}
1399
1400void core_scsi3_free_all_registrations(
1401 struct se_device *dev)
1402{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001403 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001404 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1405
1406 spin_lock(&dev->dev_reservation_lock);
1407 pr_res_holder = dev->dev_pr_res_holder;
1408 if (pr_res_holder != NULL) {
1409 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1410 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001411 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 }
1413 spin_unlock(&dev->dev_reservation_lock);
1414
1415 spin_lock(&pr_tmpl->registration_lock);
1416 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1417 &pr_tmpl->registration_list, pr_reg_list) {
1418
1419 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1420 }
1421 spin_unlock(&pr_tmpl->registration_lock);
1422
1423 spin_lock(&pr_tmpl->aptpl_reg_lock);
1424 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1425 pr_reg_aptpl_list) {
1426 list_del(&pr_reg->pr_reg_aptpl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001427 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1428 }
1429 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1430}
1431
1432static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1433{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001434 return target_depend_item(&tpg->tpg_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435}
1436
1437static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1438{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001439 target_undepend_item(&tpg->tpg_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001440 atomic_dec_mb(&tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441}
1442
1443static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1444{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001445 if (nacl->dynamic_node_acl)
1446 return 0;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001447 return target_depend_item(&nacl->acl_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001448}
1449
1450static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1451{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001452 if (!nacl->dynamic_node_acl)
1453 target_undepend_item(&nacl->acl_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001454 atomic_dec_mb(&nacl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001455}
1456
1457static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1458{
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001459 struct se_lun_acl *lun_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 struct se_node_acl *nacl;
1461 struct se_portal_group *tpg;
1462 /*
1463 * For nacl->dynamic_node_acl=1
1464 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001465 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1466 atomic_read(&se_deve->pr_kref.refcount) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001467 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001468 return 0;
1469
1470 nacl = lun_acl->se_lun_nacl;
1471 tpg = nacl->se_tpg;
1472
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001473 return target_depend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001474}
1475
1476static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1477{
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001478 struct se_lun_acl *lun_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 struct se_node_acl *nacl;
1480 struct se_portal_group *tpg;
1481 /*
1482 * For nacl->dynamic_node_acl=1
1483 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001484 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1485 atomic_read(&se_deve->pr_kref.refcount) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001486 if (!lun_acl) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001487 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001488 return;
1489 }
1490 nacl = lun_acl->se_lun_nacl;
1491 tpg = nacl->se_tpg;
1492
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001493 target_undepend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001494 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001495}
1496
Christoph Hellwigde103c92012-11-06 12:24:09 -08001497static sense_reason_t
1498core_scsi3_decode_spec_i_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001499 struct se_cmd *cmd,
1500 struct se_portal_group *tpg,
1501 unsigned char *l_isid,
1502 u64 sa_res_key,
1503 int all_tg_pt,
1504 int aptpl)
1505{
Andy Grover5951146d2011-07-19 10:26:37 +00001506 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001507 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001508 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 struct se_node_acl *dest_node_acl = NULL;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001510 struct se_dev_entry *dest_se_deve = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001511 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1512 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001513 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001514 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001515 unsigned char *buf, *ptr, proto_ident;
Sagi Grimberg9b353cc2015-06-29 13:07:06 +03001516 const unsigned char *i_str = NULL;
Julia Lawall13ba5642014-11-30 19:14:12 +01001517 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08001518 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001519 u32 tpdl, tid_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520 u32 dest_rtpi = 0;
1521
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522 /*
1523 * Allocate a struct pr_transport_id_holder and setup the
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001524 * local_node_acl pointer and add to struct list_head tid_dest_list
1525 * for add registration processing in the loop of tid_dest_list below.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001526 */
1527 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001528 if (!tidh_new) {
1529 pr_err("Unable to allocate tidh_new\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001530 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531 }
1532 INIT_LIST_HEAD(&tidh_new->dest_list);
1533 tidh_new->dest_tpg = tpg;
1534 tidh_new->dest_node_acl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535
Andy Grover5951146d2011-07-19 10:26:37 +00001536 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001537 se_sess->se_node_acl, cmd->se_lun,
1538 NULL, cmd->orig_fe_lun, l_isid,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001539 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001540 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001541 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001542 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001543 }
1544 tidh_new->dest_pr_reg = local_pr_reg;
1545 /*
1546 * The local I_T nexus does not hold any configfs dependances,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001547 * so we set tidh_new->dest_se_deve to NULL to prevent the
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001548 * configfs_undepend_item() calls in the tid_dest_list loops below.
1549 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001550 tidh_new->dest_se_deve = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001551 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001552
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001553 if (cmd->data_length < 28) {
1554 pr_warn("SPC-PR: Received PR OUT parameter list"
1555 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001556 ret = TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001557 goto out;
1558 }
1559
Andy Grover49493142012-01-16 16:57:08 -08001560 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001561 if (!buf) {
1562 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1563 goto out;
1564 }
1565
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001566 /*
1567 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1568 * first extract TransportID Parameter Data Length, and make sure
1569 * the value matches up to the SCSI expected data transfer length.
1570 */
1571 tpdl = (buf[24] & 0xff) << 24;
1572 tpdl |= (buf[25] & 0xff) << 16;
1573 tpdl |= (buf[26] & 0xff) << 8;
1574 tpdl |= buf[27] & 0xff;
1575
1576 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001577 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578 " does not equal CDB data_length: %u\n", tpdl,
1579 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001580 ret = TCM_INVALID_PARAMETER_LIST;
1581 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 }
1583 /*
1584 * Start processing the received transport IDs using the
1585 * receiving I_T Nexus portal's fabric dependent methods to
1586 * obtain the SCSI Initiator Port/Device Identifiers.
1587 */
1588 ptr = &buf[28];
1589
1590 while (tpdl > 0) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001591 struct se_lun *dest_lun, *tmp_lun;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001592
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001593 proto_ident = (ptr[0] & 0x0f);
1594 dest_tpg = NULL;
1595
1596 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001597 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1598 tmp_tpg = tmp_lun->lun_tpg;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001599
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 /*
1601 * Look for the matching proto_ident provided by
1602 * the received TransportID
1603 */
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02001604 if (tmp_tpg->proto_id != proto_ident)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001605 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001606 dest_rtpi = tmp_lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001607
Christoph Hellwig2650d712015-05-01 17:47:58 +02001608 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1609 (const char *)ptr, &tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001610 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001611 continue;
1612
Joern Engel33940d02014-09-16 16:23:12 -04001613 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001614 spin_unlock(&dev->se_port_lock);
1615
Christoph Hellwigde103c92012-11-06 12:24:09 -08001616 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001617 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001618 " for tmp_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04001619 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001620 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1621 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001622 }
1623 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001624 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001625 * from the decoded fabric module specific TransportID
1626 * at *i_str.
1627 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001628 mutex_lock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629 dest_node_acl = __core_tpg_get_initiator_node_acl(
1630 tmp_tpg, i_str);
Joern Engel33940d02014-09-16 16:23:12 -04001631 if (dest_node_acl)
1632 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001633 mutex_unlock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001634
Andy Grover6708bb22011-06-08 10:36:43 -07001635 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001636 core_scsi3_tpg_undepend_item(tmp_tpg);
1637 spin_lock(&dev->se_port_lock);
1638 continue;
1639 }
1640
Christoph Hellwigde103c92012-11-06 12:24:09 -08001641 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001642 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001643 " for dest_node_acl->acl_group\n");
Joern Engel33940d02014-09-16 16:23:12 -04001644 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001645 core_scsi3_tpg_undepend_item(tmp_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001646 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1647 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001648 }
1649
1650 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001651 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001652 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001653 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001654 dest_node_acl->initiatorname, dest_rtpi);
1655
1656 spin_lock(&dev->se_port_lock);
1657 break;
1658 }
1659 spin_unlock(&dev->se_port_lock);
1660
Andy Grover6708bb22011-06-08 10:36:43 -07001661 if (!dest_tpg) {
1662 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663 " dest_tpg\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001664 ret = TCM_INVALID_PARAMETER_LIST;
1665 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001666 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001667
Andy Grover6708bb22011-06-08 10:36:43 -07001668 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001669 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001670 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001671 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001672
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001674 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001675 " %u for Transport ID: %s\n", tid_len, ptr);
1676 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1677 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001678 ret = TCM_INVALID_PARAMETER_LIST;
1679 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001680 }
1681 /*
1682 * Locate the desintation struct se_dev_entry pointer for matching
1683 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1684 * Target Port.
1685 */
1686 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1687 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001688 if (!dest_se_deve) {
1689 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001691 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001692 dest_rtpi);
1693
1694 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1695 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001696 ret = TCM_INVALID_PARAMETER_LIST;
1697 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001698 }
1699
Christoph Hellwigde103c92012-11-06 12:24:09 -08001700 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001701 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001702 " failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001703 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001704 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1705 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001706 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1707 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001708 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001709
Andy Grover6708bb22011-06-08 10:36:43 -07001710 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001711 " dest_se_deve mapped_lun: %llu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001712 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001713 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001714
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001715 /*
1716 * Skip any TransportIDs that already have a registration for
1717 * this target port.
1718 */
1719 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1720 iport_ptr);
1721 if (pr_reg_e) {
1722 core_scsi3_put_pr_reg(pr_reg_e);
1723 core_scsi3_lunacl_undepend_item(dest_se_deve);
1724 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1725 core_scsi3_tpg_undepend_item(dest_tpg);
1726 ptr += tid_len;
1727 tpdl -= tid_len;
1728 tid_len = 0;
1729 continue;
1730 }
1731 /*
1732 * Allocate a struct pr_transport_id_holder and setup
1733 * the dest_node_acl and dest_se_deve pointers for the
1734 * loop below.
1735 */
1736 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1737 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001738 if (!tidh_new) {
1739 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001740 core_scsi3_lunacl_undepend_item(dest_se_deve);
1741 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1742 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001743 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1744 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745 }
1746 INIT_LIST_HEAD(&tidh_new->dest_list);
1747 tidh_new->dest_tpg = dest_tpg;
1748 tidh_new->dest_node_acl = dest_node_acl;
1749 tidh_new->dest_se_deve = dest_se_deve;
1750
1751 /*
1752 * Allocate, but do NOT add the registration for the
1753 * TransportID referenced SCSI Initiator port. This
1754 * done because of the following from spc4r17 in section
1755 * 6.14.3 wrt SPEC_I_PT:
1756 *
1757 * "If a registration fails for any initiator port (e.g., if th
1758 * logical unit does not have enough resources available to
1759 * hold the registration information), no registrations shall be
1760 * made, and the command shall be terminated with
1761 * CHECK CONDITION status."
1762 *
1763 * That means we call __core_scsi3_alloc_registration() here,
1764 * and then call __core_scsi3_add_registration() in the
1765 * 2nd loop which will never fail.
1766 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001767 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
1768 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
1769
Andy Grover5951146d2011-07-19 10:26:37 +00001770 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001771 dest_node_acl, dest_lun, dest_se_deve,
1772 dest_se_deve->mapped_lun, iport_ptr,
1773 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001774 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001775 core_scsi3_lunacl_undepend_item(dest_se_deve);
1776 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1777 core_scsi3_tpg_undepend_item(dest_tpg);
1778 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001779 ret = TCM_INVALID_PARAMETER_LIST;
1780 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001781 }
1782 tidh_new->dest_pr_reg = dest_pr_reg;
1783 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1784
1785 ptr += tid_len;
1786 tpdl -= tid_len;
1787 tid_len = 0;
1788
1789 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001790
Andy Grover49493142012-01-16 16:57:08 -08001791 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001792
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001793 /*
1794 * Go ahead and create a registrations from tid_dest_list for the
1795 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1796 * and dest_se_deve.
1797 *
1798 * The SA Reservation Key from the PROUT is set for the
1799 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1800 * means that the TransportID Initiator port will be
1801 * registered on all of the target ports in the SCSI target device
1802 * ALL_TG_PT=0 means the registration will only be for the
1803 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1804 * was received.
1805 */
1806 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1807 dest_tpg = tidh->dest_tpg;
1808 dest_node_acl = tidh->dest_node_acl;
1809 dest_se_deve = tidh->dest_se_deve;
1810 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001811
1812 list_del(&tidh->dest_list);
1813 kfree(tidh);
1814
1815 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001816 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001817
Andy Grover5951146d2011-07-19 10:26:37 +00001818 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001819 dest_pr_reg, 0, 0);
1820
Andy Grover6708bb22011-06-08 10:36:43 -07001821 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001822 " registered Transport ID for Node: %s%s Mapped LUN:"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001823 " %llu\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001824 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1825 dest_se_deve->mapped_lun : 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001826
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001827 if (!dest_se_deve) {
1828 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1829 target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830 continue;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001831 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001832 core_scsi3_lunacl_undepend_item(dest_se_deve);
1833 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1834 core_scsi3_tpg_undepend_item(dest_tpg);
1835 }
1836
1837 return 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001838out_unmap:
Andy Grover49493142012-01-16 16:57:08 -08001839 transport_kunmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001840out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001841 /*
1842 * For the failure case, release everything from tid_dest_list
1843 * including *dest_pr_reg and the configfs dependances..
1844 */
1845 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1846 dest_tpg = tidh->dest_tpg;
1847 dest_node_acl = tidh->dest_node_acl;
1848 dest_se_deve = tidh->dest_se_deve;
1849 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001850
1851 list_del(&tidh->dest_list);
1852 kfree(tidh);
1853 /*
1854 * Release any extra ALL_TG_PT=1 registrations for
1855 * the SPEC_I_PT=1 case.
1856 */
1857 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1858 &dest_pr_reg->pr_reg_atp_list,
1859 pr_reg_atp_mem_list) {
1860 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1861 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1862 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1863 }
1864
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1866
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001867 if (!dest_se_deve) {
1868 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1869 target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001870 continue;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001871 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001872 core_scsi3_lunacl_undepend_item(dest_se_deve);
1873 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1874 core_scsi3_tpg_undepend_item(dest_tpg);
1875 }
1876 return ret;
1877}
1878
Andy Grover3c8a6222013-05-16 10:40:58 -07001879static int core_scsi3_update_aptpl_buf(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001880 struct se_device *dev,
1881 unsigned char *buf,
Andy Grover0607dec2013-05-16 10:40:57 -07001882 u32 pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001883{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001884 struct se_portal_group *tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001885 struct t10_pr_registration *pr_reg;
1886 unsigned char tmp[512], isid_buf[32];
1887 ssize_t len = 0;
1888 int reg_count = 0;
Andy Grover3c8a6222013-05-16 10:40:58 -07001889 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001890
Andy Grover3c8a6222013-05-16 10:40:58 -07001891 spin_lock(&dev->dev_reservation_lock);
1892 spin_lock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001893 /*
1894 * Walk the registration list..
1895 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001896 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001897 pr_reg_list) {
1898
1899 tmp[0] = '\0';
1900 isid_buf[0] = '\0';
1901 tpg = pr_reg->pr_reg_nacl->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001902 /*
1903 * Write out any ISID value to APTPL metadata that was included
1904 * in the original registration.
1905 */
1906 if (pr_reg->isid_present_at_reg)
1907 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1908 pr_reg->pr_reg_isid);
1909 /*
1910 * Include special metadata if the pr_reg matches the
1911 * reservation holder.
1912 */
1913 if (dev->dev_pr_res_holder == pr_reg) {
1914 snprintf(tmp, 512, "PR_REG_START: %d"
1915 "\ninitiator_fabric=%s\n"
1916 "initiator_node=%s\n%s"
1917 "sa_res_key=%llu\n"
1918 "res_holder=1\nres_type=%02x\n"
1919 "res_scope=%02x\nres_all_tg_pt=%d\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001920 "mapped_lun=%llu\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001921 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001922 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1923 pr_reg->pr_res_key, pr_reg->pr_res_type,
1924 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1925 pr_reg->pr_res_mapped_lun);
1926 } else {
1927 snprintf(tmp, 512, "PR_REG_START: %d\n"
1928 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1929 "sa_res_key=%llu\nres_holder=0\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001930 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001931 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1933 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1934 pr_reg->pr_res_mapped_lun);
1935 }
1936
Dan Carpenter60d645a2011-06-15 10:03:05 -07001937 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001938 pr_err("Unable to update renaming APTPL metadata,"
1939 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001940 ret = -EMSGSIZE;
1941 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001942 }
1943 len += sprintf(buf+len, "%s", tmp);
1944
1945 /*
1946 * Include information about the associated SCSI target port.
1947 */
1948 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001949 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001950 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1951 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1952 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001953 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1954 reg_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001955
Dan Carpenter60d645a2011-06-15 10:03:05 -07001956 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001957 pr_err("Unable to update renaming APTPL metadata,"
1958 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001959 ret = -EMSGSIZE;
1960 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961 }
1962 len += sprintf(buf+len, "%s", tmp);
1963 reg_count++;
1964 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965
Andy Grover6708bb22011-06-08 10:36:43 -07001966 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967 len += sprintf(buf+len, "No Registrations or Reservations");
1968
Andy Grover3c8a6222013-05-16 10:40:58 -07001969out:
1970 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001971 spin_unlock(&dev->dev_reservation_lock);
1972
1973 return ret;
1974}
1975
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001976static int __core_scsi3_write_aptpl_to_file(
1977 struct se_device *dev,
Andy Grover4dee96f2013-05-16 10:41:00 -07001978 unsigned char *buf)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001979{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001980 struct t10_wwn *wwn = &dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001981 struct file *file;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001982 int flags = O_RDWR | O_CREAT | O_TRUNC;
1983 char path[512];
Andy Grover4dee96f2013-05-16 10:41:00 -07001984 u32 pr_aptpl_buf_len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001985 int ret;
1986
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001987 memset(path, 0, 512);
1988
Dan Carpenter60d645a2011-06-15 10:03:05 -07001989 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07001990 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001992 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001993 }
1994
1995 snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1996 file = filp_open(path, flags, 0600);
Al Viro0e9b10a2013-02-23 15:22:43 -05001997 if (IS_ERR(file)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001998 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001999 " failed\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05002000 return PTR_ERR(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002001 }
2002
Andy Grover4dee96f2013-05-16 10:41:00 -07002003 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002004
Al Viro0e9b10a2013-02-23 15:22:43 -05002005 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002006
Al Viro0e9b10a2013-02-23 15:22:43 -05002007 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -07002008 pr_debug("Error writing APTPL metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05002009 fput(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002010
Gera Kazakovf730f912013-09-09 15:47:06 -07002011 return (ret < 0) ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002012}
2013
Andy Grover459f2132013-05-16 10:41:02 -07002014/*
2015 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2016 * write out the updated metadata to struct file for this SCSI device.
2017 */
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002018static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002019{
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002020 unsigned char *buf;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002021 int rc, len = PR_APTPL_BUF_LEN;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002022
Andy Grover459f2132013-05-16 10:41:02 -07002023 if (!aptpl) {
2024 char *null_buf = "No Registrations or Reservations\n";
2025
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002026 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
Andy Grover459f2132013-05-16 10:41:02 -07002027 dev->t10_pr.pr_aptpl_active = 0;
2028 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
Andy Grover459f2132013-05-16 10:41:02 -07002029
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002030 if (rc)
2031 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover459f2132013-05-16 10:41:02 -07002032
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002033 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002034 }
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002035retry:
2036 buf = vzalloc(len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002037 if (!buf)
2038 return TCM_OUT_OF_RESOURCES;
2039
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002040 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002041 if (rc < 0) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002042 vfree(buf);
2043 len *= 2;
2044 goto retry;
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002045 }
2046
2047 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2048 if (rc != 0) {
2049 pr_err("SPC-3 PR: Could not update APTPL\n");
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002050 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002051 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2052 }
2053 dev->t10_pr.pr_aptpl_active = 1;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002054 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002055 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2056 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057}
2058
Christoph Hellwigde103c92012-11-06 12:24:09 -08002059static sense_reason_t
2060core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
Andy Groverbc118fe2013-05-16 10:41:04 -07002061 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002062{
Andy Grovere3d6f902011-07-19 08:55:10 +00002063 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002064 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002065 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002066 struct se_portal_group *se_tpg;
Andy Groverbc118fe2013-05-16 10:41:04 -07002067 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002068 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002069 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
Hannes Reineckea0d50f62012-12-17 09:53:34 +01002070 sense_reason_t ret = TCM_NO_SENSE;
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002071 int pr_holder = 0, type;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002072
Andy Grover6708bb22011-06-08 10:36:43 -07002073 if (!se_sess || !se_lun) {
2074 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002075 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002076 }
2077 se_tpg = se_sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002078
Andy Grovere3d6f902011-07-19 08:55:10 +00002079 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002080 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002081 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002082 PR_REG_ISID_LEN);
2083 isid_ptr = &isid_buf[0];
2084 }
2085 /*
2086 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2087 */
Andy Groverbc118fe2013-05-16 10:41:04 -07002088 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2089 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002090 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002091 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002092 " for SA REGISTER, returning CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002093 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002094 }
2095 /*
2096 * Do nothing but return GOOD status.
2097 */
Andy Grover6708bb22011-06-08 10:36:43 -07002098 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002099 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002100
Andy Grover6708bb22011-06-08 10:36:43 -07002101 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002102 /*
2103 * Perform the Service Action REGISTER on the Initiator
2104 * Port Endpoint that the PRO was received from on the
2105 * Logical Unit of the SCSI device server.
2106 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002107 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00002108 se_sess->se_node_acl, cmd->se_lun,
2109 NULL, cmd->orig_fe_lun, isid_ptr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002110 sa_res_key, all_tg_pt, aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07002111 register_type, 0)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002112 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002113 " struct t10_pr_registration\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002114 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002115 }
2116 } else {
2117 /*
2118 * Register both the Initiator port that received
2119 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2120 * TransportID from Parameter list and loop through
2121 * fabric dependent parameter list while calling
2122 * logic from of core_scsi3_alloc_registration() for
2123 * each TransportID provided SCSI Initiator Port/Device
2124 */
2125 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2126 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2127 if (ret != 0)
2128 return ret;
2129 }
Andy Grover459f2132013-05-16 10:41:02 -07002130 return core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002131 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002132
Andy Groverbc118fe2013-05-16 10:41:04 -07002133 /* ok, existing registration */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002134
Andy Groverbc118fe2013-05-16 10:41:04 -07002135 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2136 pr_err("SPC-3 PR REGISTER: Received"
2137 " res_key: 0x%016Lx does not match"
2138 " existing SA REGISTER res_key:"
2139 " 0x%016Lx\n", res_key,
2140 pr_reg->pr_res_key);
2141 ret = TCM_RESERVATION_CONFLICT;
2142 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002143 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002144
2145 if (spec_i_pt) {
Andy Grover1f070cc2013-05-16 10:40:56 -07002146 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2147 " set on a registered nexus\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002148 ret = TCM_INVALID_PARAMETER_LIST;
Andy Groverbc118fe2013-05-16 10:41:04 -07002149 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002150 }
2151
2152 /*
2153 * An existing ALL_TG_PT=1 registration being released
2154 * must also set ALL_TG_PT=1 in the incoming PROUT.
2155 */
Andy Grover1f070cc2013-05-16 10:40:56 -07002156 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2157 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
Christoph Hellwigde103c92012-11-06 12:24:09 -08002158 " registration exists, but ALL_TG_PT=1 bit not"
2159 " present in received PROUT\n");
2160 ret = TCM_INVALID_CDB_FIELD;
Andy Groverbc118fe2013-05-16 10:41:04 -07002161 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002162 }
2163
2164 /*
Andy Grover51d9c412013-05-16 10:41:03 -07002165 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
Christoph Hellwigde103c92012-11-06 12:24:09 -08002166 */
Andy Grover51d9c412013-05-16 10:41:03 -07002167 if (sa_res_key) {
2168 /*
2169 * Increment PRgeneration counter for struct se_device"
2170 * upon a successful REGISTER, see spc4r17 section 6.3.2
2171 * READ_KEYS service action.
2172 */
2173 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2174 pr_reg->pr_res_key = sa_res_key;
2175 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2176 " Key for %s to: 0x%016Lx PRgeneration:"
2177 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2178 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2179 pr_reg->pr_reg_nacl->initiatorname,
2180 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2181
2182 } else {
2183 /*
2184 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2185 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002186 type = pr_reg->pr_res_type;
2187 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2188 pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002189 if (pr_holder < 0) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002190 ret = TCM_RESERVATION_CONFLICT;
Andy Groverbc118fe2013-05-16 10:41:04 -07002191 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002192 }
2193
2194 spin_lock(&pr_tmpl->registration_lock);
2195 /*
2196 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2197 * and matching pr_res_key.
2198 */
2199 if (pr_reg->pr_reg_all_tg_pt) {
2200 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2201 &pr_tmpl->registration_list,
2202 pr_reg_list) {
2203
2204 if (!pr_reg_p->pr_reg_all_tg_pt)
2205 continue;
2206 if (pr_reg_p->pr_res_key != res_key)
2207 continue;
2208 if (pr_reg == pr_reg_p)
2209 continue;
2210 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2211 pr_reg_p->pr_reg_nacl->initiatorname))
2212 continue;
2213
2214 __core_scsi3_free_registration(dev,
2215 pr_reg_p, NULL, 0);
2216 }
2217 }
2218
2219 /*
2220 * Release the calling I_T Nexus registration now..
2221 */
2222 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002223 pr_reg = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002224
2225 /*
2226 * From spc4r17, section 5.7.11.3 Unregistering
2227 *
2228 * If the persistent reservation is a registrants only
2229 * type, the device server shall establish a unit
2230 * attention condition for the initiator port associated
2231 * with every registered I_T nexus except for the I_T
2232 * nexus on which the PERSISTENT RESERVE OUT command was
2233 * received, with the additional sense code set to
2234 * RESERVATIONS RELEASED.
2235 */
2236 if (pr_holder &&
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002237 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2238 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002239 list_for_each_entry(pr_reg_p,
2240 &pr_tmpl->registration_list,
2241 pr_reg_list) {
2242
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002243 target_ua_allocate_lun(
Christoph Hellwigde103c92012-11-06 12:24:09 -08002244 pr_reg_p->pr_reg_nacl,
2245 pr_reg_p->pr_res_mapped_lun,
2246 0x2A,
2247 ASCQ_2AH_RESERVATIONS_RELEASED);
2248 }
2249 }
Andy Grover51d9c412013-05-16 10:41:03 -07002250
Christoph Hellwigde103c92012-11-06 12:24:09 -08002251 spin_unlock(&pr_tmpl->registration_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002252 }
2253
Andy Grover459f2132013-05-16 10:41:02 -07002254 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002255
Andy Groverbc118fe2013-05-16 10:41:04 -07002256out:
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002257 if (pr_reg)
2258 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002259 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002260}
2261
2262unsigned char *core_scsi3_pr_dump_type(int type)
2263{
2264 switch (type) {
2265 case PR_TYPE_WRITE_EXCLUSIVE:
2266 return "Write Exclusive Access";
2267 case PR_TYPE_EXCLUSIVE_ACCESS:
2268 return "Exclusive Access";
2269 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2270 return "Write Exclusive Access, Registrants Only";
2271 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2272 return "Exclusive Access, Registrants Only";
2273 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2274 return "Write Exclusive Access, All Registrants";
2275 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2276 return "Exclusive Access, All Registrants";
2277 default:
2278 break;
2279 }
2280
2281 return "Unknown SPC-3 PR Type";
2282}
2283
Christoph Hellwigde103c92012-11-06 12:24:09 -08002284static sense_reason_t
2285core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002286{
Christoph Hellwigde103c92012-11-06 12:24:09 -08002287 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002288 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002289 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002290 struct t10_pr_registration *pr_reg, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002291 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002292 char i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08002293 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002294
2295 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2296
Andy Grover6708bb22011-06-08 10:36:43 -07002297 if (!se_sess || !se_lun) {
2298 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002299 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002301 /*
2302 * Locate the existing *pr_reg via struct se_node_acl pointers
2303 */
Andy Grover5951146d2011-07-19 10:26:37 +00002304 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002305 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002306 if (!pr_reg) {
2307 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002308 " PR_REGISTERED *pr_reg for RESERVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002309 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002310 }
2311 /*
2312 * From spc4r17 Section 5.7.9: Reserving:
2313 *
2314 * An application client creates a persistent reservation by issuing
2315 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2316 * a registered I_T nexus with the following parameters:
2317 * a) RESERVATION KEY set to the value of the reservation key that is
2318 * registered with the logical unit for the I_T nexus; and
2319 */
2320 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002321 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322 " does not match existing SA REGISTER res_key:"
2323 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002324 ret = TCM_RESERVATION_CONFLICT;
2325 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002326 }
2327 /*
2328 * From spc4r17 Section 5.7.9: Reserving:
2329 *
2330 * From above:
2331 * b) TYPE field and SCOPE field set to the persistent reservation
2332 * being created.
2333 *
2334 * Only one persistent reservation is allowed at a time per logical unit
2335 * and that persistent reservation has a scope of LU_SCOPE.
2336 */
2337 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002338 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002339 ret = TCM_INVALID_PARAMETER_LIST;
2340 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002341 }
2342 /*
2343 * See if we have an existing PR reservation holder pointer at
2344 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2345 * *pr_res_holder.
2346 */
2347 spin_lock(&dev->dev_reservation_lock);
2348 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002349 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002350 /*
2351 * From spc4r17 Section 5.7.9: Reserving:
2352 *
2353 * If the device server receives a PERSISTENT RESERVE OUT
2354 * command from an I_T nexus other than a persistent reservation
2355 * holder (see 5.7.10) that attempts to create a persistent
2356 * reservation when a persistent reservation already exists for
2357 * the logical unit, then the command shall be completed with
2358 * RESERVATION CONFLICT status.
2359 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002360 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002361 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002362 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002363 " [%s]: %s while reservation already held by"
2364 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002365 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002366 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002367 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002368 pr_res_holder->pr_reg_nacl->initiatorname);
2369
2370 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002371 ret = TCM_RESERVATION_CONFLICT;
2372 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002373 }
2374 /*
2375 * From spc4r17 Section 5.7.9: Reserving:
2376 *
2377 * If a persistent reservation holder attempts to modify the
2378 * type or scope of an existing persistent reservation, the
2379 * command shall be completed with RESERVATION CONFLICT status.
2380 */
2381 if ((pr_res_holder->pr_res_type != type) ||
2382 (pr_res_holder->pr_res_scope != scope)) {
2383 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002384 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002385 " [%s]: %s trying to change TYPE and/or SCOPE,"
2386 " while reservation already held by [%s]: %s,"
2387 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002388 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002389 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002390 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002391 pr_res_holder->pr_reg_nacl->initiatorname);
2392
2393 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002394 ret = TCM_RESERVATION_CONFLICT;
2395 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002396 }
2397 /*
2398 * From spc4r17 Section 5.7.9: Reserving:
2399 *
2400 * If the device server receives a PERSISTENT RESERVE OUT
2401 * command with RESERVE service action where the TYPE field and
2402 * the SCOPE field contain the same values as the existing type
2403 * and scope from a persistent reservation holder, it shall not
2404 * make any change to the existing persistent reservation and
2405 * shall completethe command with GOOD status.
2406 */
2407 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002408 ret = 0;
2409 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002410 }
2411 /*
2412 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2413 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2414 */
2415 pr_reg->pr_res_scope = scope;
2416 pr_reg->pr_res_type = type;
2417 pr_reg->pr_res_holder = 1;
2418 dev->dev_pr_res_holder = pr_reg;
Andy Groverd2843c12013-05-16 10:40:55 -07002419 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002420
Andy Grover6708bb22011-06-08 10:36:43 -07002421 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002422 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002423 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002424 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002425 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002426 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002427 se_sess->se_node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002428 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429 spin_unlock(&dev->dev_reservation_lock);
2430
Andy Grover459f2132013-05-16 10:41:02 -07002431 if (pr_tmpl->pr_aptpl_active)
2432 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002433
Christoph Hellwigde103c92012-11-06 12:24:09 -08002434 ret = 0;
2435out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002437 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002438}
2439
Christoph Hellwigde103c92012-11-06 12:24:09 -08002440static sense_reason_t
2441core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2442 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002444 switch (type) {
2445 case PR_TYPE_WRITE_EXCLUSIVE:
2446 case PR_TYPE_EXCLUSIVE_ACCESS:
2447 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2448 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2449 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2450 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08002451 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002452 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002453 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002454 " 0x%02x\n", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002455 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002456 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002457}
2458
2459/*
2460 * Called with struct se_device->dev_reservation_lock held.
2461 */
2462static void __core_scsi3_complete_pro_release(
2463 struct se_device *dev,
2464 struct se_node_acl *se_nacl,
2465 struct t10_pr_registration *pr_reg,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002466 int explicit,
2467 int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002468{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002469 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002471 int pr_res_type = 0, pr_res_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002472
2473 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002474 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002475 /*
2476 * Go ahead and release the current PR reservation holder.
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002477 * If an All Registrants reservation is currently active and
2478 * a unregister operation is requested, replace the current
2479 * dev_pr_res_holder with another active registration.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002480 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002481 if (dev->dev_pr_res_holder) {
2482 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2483 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2484 dev->dev_pr_res_holder->pr_res_type = 0;
2485 dev->dev_pr_res_holder->pr_res_scope = 0;
2486 dev->dev_pr_res_holder->pr_res_holder = 0;
2487 dev->dev_pr_res_holder = NULL;
2488 }
2489 if (!unreg)
2490 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002491
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002492 spin_lock(&dev->t10_pr.registration_lock);
2493 list_del_init(&pr_reg->pr_reg_list);
2494 /*
2495 * If the I_T nexus is a reservation holder, the persistent reservation
2496 * is of an all registrants type, and the I_T nexus is the last remaining
2497 * registered I_T nexus, then the device server shall also release the
2498 * persistent reservation.
2499 */
2500 if (!list_empty(&dev->t10_pr.registration_list) &&
2501 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2502 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2503 dev->dev_pr_res_holder =
2504 list_entry(dev->t10_pr.registration_list.next,
2505 struct t10_pr_registration, pr_reg_list);
2506 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2507 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2508 dev->dev_pr_res_holder->pr_res_holder = 1;
2509 }
2510 spin_unlock(&dev->t10_pr.registration_lock);
2511out:
2512 if (!dev->dev_pr_res_holder) {
2513 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2514 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2515 tfo->get_fabric_name(), (explicit) ? "explicit" :
2516 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2517 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2518 }
Andy Grover6708bb22011-06-08 10:36:43 -07002519 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002520 tfo->get_fabric_name(), se_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002521 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002522 /*
2523 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2524 */
2525 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2526}
2527
Christoph Hellwigde103c92012-11-06 12:24:09 -08002528static sense_reason_t
2529core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2530 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002531{
2532 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002533 struct se_session *se_sess = cmd->se_sess;
2534 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002535 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002536 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002537 sense_reason_t ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538
Andy Grover6708bb22011-06-08 10:36:43 -07002539 if (!se_sess || !se_lun) {
2540 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002541 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002542 }
2543 /*
2544 * Locate the existing *pr_reg via struct se_node_acl pointers
2545 */
2546 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002547 if (!pr_reg) {
2548 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002549 " PR_REGISTERED *pr_reg for RELEASE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002550 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551 }
2552 /*
2553 * From spc4r17 Section 5.7.11.2 Releasing:
2554 *
2555 * If there is no persistent reservation or in response to a persistent
2556 * reservation release request from a registered I_T nexus that is not a
2557 * persistent reservation holder (see 5.7.10), the device server shall
2558 * do the following:
2559 *
2560 * a) Not release the persistent reservation, if any;
2561 * b) Not remove any registrations; and
2562 * c) Complete the command with GOOD status.
2563 */
2564 spin_lock(&dev->dev_reservation_lock);
2565 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002566 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 /*
2568 * No persistent reservation, return GOOD status.
2569 */
2570 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerbb7a8c82012-11-06 15:46:25 -08002571 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002572 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002574 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002575 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 * Release request from a registered I_T nexus that is not a
2577 * persistent reservation holder. return GOOD status.
2578 */
2579 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002580 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002581 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002582
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002583 /*
2584 * From spc4r17 Section 5.7.11.2 Releasing:
2585 *
2586 * Only the persistent reservation holder (see 5.7.10) is allowed to
2587 * release a persistent reservation.
2588 *
2589 * An application client releases the persistent reservation by issuing
2590 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2591 * an I_T nexus that is a persistent reservation holder with the
2592 * following parameters:
2593 *
2594 * a) RESERVATION KEY field set to the value of the reservation key
2595 * that is registered with the logical unit for the I_T nexus;
2596 */
2597 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002598 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002599 " does not match existing SA REGISTER res_key:"
2600 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2601 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002602 ret = TCM_RESERVATION_CONFLICT;
2603 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002604 }
2605 /*
2606 * From spc4r17 Section 5.7.11.2 Releasing and above:
2607 *
2608 * b) TYPE field and SCOPE field set to match the persistent
2609 * reservation being released.
2610 */
2611 if ((pr_res_holder->pr_res_type != type) ||
2612 (pr_res_holder->pr_res_scope != scope)) {
2613 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002614 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002615 " reservation from [%s]: %s with different TYPE "
2616 "and/or SCOPE while reservation already held by"
2617 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002618 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002619 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002620 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002621 pr_res_holder->pr_reg_nacl->initiatorname);
2622
2623 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002624 ret = TCM_RESERVATION_CONFLICT;
2625 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626 }
2627 /*
2628 * In response to a persistent reservation release request from the
2629 * persistent reservation holder the device server shall perform a
2630 * release by doing the following as an uninterrupted series of actions:
2631 * a) Release the persistent reservation;
2632 * b) Not remove any registration(s);
2633 * c) If the released persistent reservation is a registrants only type
2634 * or all registrants type persistent reservation,
2635 * the device server shall establish a unit attention condition for
2636 * the initiator port associated with every regis-
2637 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2638 * RESERVE OUT command with RELEASE service action was received,
2639 * with the additional sense code set to RESERVATIONS RELEASED; and
2640 * d) If the persistent reservation is of any other type, the device
2641 * server shall not establish a unit attention condition.
2642 */
2643 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002644 pr_reg, 1, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002645
2646 spin_unlock(&dev->dev_reservation_lock);
2647
2648 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2649 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2650 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2651 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2652 /*
2653 * If no UNIT ATTENTION conditions will be established for
2654 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2655 * go ahead and check for APTPL=1 update+write below
2656 */
2657 goto write_aptpl;
2658 }
2659
2660 spin_lock(&pr_tmpl->registration_lock);
2661 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2662 pr_reg_list) {
2663 /*
2664 * Do not establish a UNIT ATTENTION condition
2665 * for the calling I_T Nexus
2666 */
2667 if (pr_reg_p == pr_reg)
2668 continue;
2669
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002670 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002671 pr_reg_p->pr_res_mapped_lun,
2672 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2673 }
2674 spin_unlock(&pr_tmpl->registration_lock);
2675
2676write_aptpl:
Andy Grover459f2132013-05-16 10:41:02 -07002677 if (pr_tmpl->pr_aptpl_active)
2678 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2679
Christoph Hellwigde103c92012-11-06 12:24:09 -08002680out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002682 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002683}
2684
Christoph Hellwigde103c92012-11-06 12:24:09 -08002685static sense_reason_t
2686core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687{
2688 struct se_device *dev = cmd->se_dev;
2689 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002690 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002691 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002692 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002693 u64 pr_res_mapped_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694 int calling_it_nexus = 0;
2695 /*
2696 * Locate the existing *pr_reg via struct se_node_acl pointers
2697 */
Andy Grover5951146d2011-07-19 10:26:37 +00002698 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002699 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002700 if (!pr_reg_n) {
2701 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002702 " PR_REGISTERED *pr_reg for CLEAR\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002703 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002704 }
2705 /*
2706 * From spc4r17 section 5.7.11.6, Clearing:
2707 *
2708 * Any application client may release the persistent reservation and
2709 * remove all registrations from a device server by issuing a
2710 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2711 * registered I_T nexus with the following parameter:
2712 *
2713 * a) RESERVATION KEY field set to the value of the reservation key
2714 * that is registered with the logical unit for the I_T nexus.
2715 */
2716 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002717 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718 " res_key: 0x%016Lx does not match"
2719 " existing SA REGISTER res_key:"
2720 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2721 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002722 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002723 }
2724 /*
2725 * a) Release the persistent reservation, if any;
2726 */
2727 spin_lock(&dev->dev_reservation_lock);
2728 pr_res_holder = dev->dev_pr_res_holder;
2729 if (pr_res_holder) {
2730 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2731 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002732 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002733 }
2734 spin_unlock(&dev->dev_reservation_lock);
2735 /*
2736 * b) Remove all registration(s) (see spc4r17 5.7.7);
2737 */
2738 spin_lock(&pr_tmpl->registration_lock);
2739 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2740 &pr_tmpl->registration_list, pr_reg_list) {
2741
2742 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2743 pr_reg_nacl = pr_reg->pr_reg_nacl;
2744 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2745 __core_scsi3_free_registration(dev, pr_reg, NULL,
2746 calling_it_nexus);
2747 /*
2748 * e) Establish a unit attention condition for the initiator
2749 * port associated with every registered I_T nexus other
2750 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2751 * command with CLEAR service action was received, with the
2752 * additional sense code set to RESERVATIONS PREEMPTED.
2753 */
Andy Grover6708bb22011-06-08 10:36:43 -07002754 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002755 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2757 }
2758 spin_unlock(&pr_tmpl->registration_lock);
2759
Andy Grover6708bb22011-06-08 10:36:43 -07002760 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002761 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002762
Andy Grover459f2132013-05-16 10:41:02 -07002763 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002764
2765 core_scsi3_pr_generation(dev);
2766 return 0;
2767}
2768
2769/*
2770 * Called with struct se_device->dev_reservation_lock held.
2771 */
2772static void __core_scsi3_complete_pro_preempt(
2773 struct se_device *dev,
2774 struct t10_pr_registration *pr_reg,
2775 struct list_head *preempt_and_abort_list,
2776 int type,
2777 int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07002778 enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002779{
2780 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002781 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002782 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002783
2784 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002785 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002786 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002787 * Do an implicit RELEASE of the existing reservation.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002788 */
2789 if (dev->dev_pr_res_holder)
2790 __core_scsi3_complete_pro_release(dev, nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002791 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792
2793 dev->dev_pr_res_holder = pr_reg;
2794 pr_reg->pr_res_holder = 1;
2795 pr_reg->pr_res_type = type;
2796 pr_reg->pr_res_scope = scope;
2797
Andy Grover6708bb22011-06-08 10:36:43 -07002798 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002800 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 core_scsi3_pr_dump_type(type),
2802 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002803 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002804 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Andy Groverd2843c12013-05-16 10:40:55 -07002805 nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002806 /*
2807 * For PREEMPT_AND_ABORT, add the preempting reservation's
2808 * struct t10_pr_registration to the list that will be compared
2809 * against received CDBs..
2810 */
2811 if (preempt_and_abort_list)
2812 list_add_tail(&pr_reg->pr_reg_abort_list,
2813 preempt_and_abort_list);
2814}
2815
2816static void core_scsi3_release_preempt_and_abort(
2817 struct list_head *preempt_and_abort_list,
2818 struct t10_pr_registration *pr_reg_holder)
2819{
2820 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2821
2822 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2823 pr_reg_abort_list) {
2824
2825 list_del(&pr_reg->pr_reg_abort_list);
2826 if (pr_reg_holder == pr_reg)
2827 continue;
2828 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002829 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002830 continue;
2831 }
2832
2833 pr_reg->pr_reg_deve = NULL;
2834 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2836 }
2837}
2838
Christoph Hellwigde103c92012-11-06 12:24:09 -08002839static sense_reason_t
2840core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07002841 u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842{
Andy Grover5951146d2011-07-19 10:26:37 +00002843 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002845 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08002846 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002847 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002848 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002849 u64 pr_res_mapped_lun = 0;
Steven Allenb6932ee2014-10-24 15:18:26 -07002850 int all_reg = 0, calling_it_nexus = 0;
2851 bool sa_res_key_unmatched = sa_res_key != 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002852 int prh_type = 0, prh_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853
Christoph Hellwigde103c92012-11-06 12:24:09 -08002854 if (!se_sess)
2855 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002856
Andy Grover5951146d2011-07-19 10:26:37 +00002857 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002858 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002859 if (!pr_reg_n) {
2860 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002862 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002863 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864 }
2865 if (pr_reg_n->pr_res_key != res_key) {
2866 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002867 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002868 }
2869 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002870 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002871 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002872 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002874
2875 spin_lock(&dev->dev_reservation_lock);
2876 pr_res_holder = dev->dev_pr_res_holder;
2877 if (pr_res_holder &&
2878 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2879 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2880 all_reg = 1;
2881
Andy Grover6708bb22011-06-08 10:36:43 -07002882 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002883 spin_unlock(&dev->dev_reservation_lock);
2884 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002885 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002886 }
2887 /*
2888 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2889 *
2890 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2891 * persistent reservation holder or there is no persistent reservation
2892 * holder (i.e., there is no persistent reservation), then the device
2893 * server shall perform a preempt by doing the following in an
2894 * uninterrupted series of actions. (See below..)
2895 */
Andy Grover6708bb22011-06-08 10:36:43 -07002896 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002897 /*
2898 * No existing or SA Reservation Key matching reservations..
2899 *
2900 * PROUT SA PREEMPT with All Registrant type reservations are
2901 * allowed to be processed without a matching SA Reservation Key
2902 */
2903 spin_lock(&pr_tmpl->registration_lock);
2904 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2905 &pr_tmpl->registration_list, pr_reg_list) {
2906 /*
2907 * Removing of registrations in non all registrants
2908 * type reservations without a matching SA reservation
2909 * key.
2910 *
2911 * a) Remove the registrations for all I_T nexuses
2912 * specified by the SERVICE ACTION RESERVATION KEY
2913 * field;
2914 * b) Ignore the contents of the SCOPE and TYPE fields;
2915 * c) Process tasks as defined in 5.7.1; and
2916 * d) Establish a unit attention condition for the
2917 * initiator port associated with every I_T nexus
2918 * that lost its registration other than the I_T
2919 * nexus on which the PERSISTENT RESERVE OUT command
2920 * was received, with the additional sense code set
2921 * to REGISTRATIONS PREEMPTED.
2922 */
Andy Grover6708bb22011-06-08 10:36:43 -07002923 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002924 if (pr_reg->pr_res_key != sa_res_key)
2925 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002926 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002927
2928 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2929 pr_reg_nacl = pr_reg->pr_reg_nacl;
2930 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2931 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002932 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002933 NULL, calling_it_nexus);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002934 } else {
2935 /*
2936 * Case for any existing all registrants type
2937 * reservation, follow logic in spc4r17 section
2938 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2939 *
2940 * For a ZERO SA Reservation key, release
Hannes Reinecke125d0112013-11-19 09:07:46 +01002941 * all other registrations and do an implicit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002942 * release of active persistent reservation.
2943 *
2944 * For a non-ZERO SA Reservation key, only
2945 * release the matching reservation key from
2946 * registrations.
2947 */
2948 if ((sa_res_key) &&
2949 (pr_reg->pr_res_key != sa_res_key))
2950 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002951 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002952
2953 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2954 if (calling_it_nexus)
2955 continue;
2956
2957 pr_reg_nacl = pr_reg->pr_reg_nacl;
2958 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2959 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002960 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002961 NULL, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962 }
Andy Grover6708bb22011-06-08 10:36:43 -07002963 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002964 target_ua_allocate_lun(pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002965 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08002966 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002967 }
2968 spin_unlock(&pr_tmpl->registration_lock);
2969 /*
2970 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2971 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2972 * RESERVATION KEY field to a value that does not match any
2973 * registered reservation key, then the device server shall
2974 * complete the command with RESERVATION CONFLICT status.
2975 */
Steven Allenb6932ee2014-10-24 15:18:26 -07002976 if (sa_res_key_unmatched) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002977 spin_unlock(&dev->dev_reservation_lock);
2978 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002979 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 }
2981 /*
2982 * For an existing all registrants type reservation
2983 * with a zero SA rservation key, preempt the existing
2984 * reservation with the new PR type and scope.
2985 */
2986 if (pr_res_holder && all_reg && !(sa_res_key)) {
2987 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07002988 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2989 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002990
Andy Grover33ce6a82013-05-16 10:40:54 -07002991 if (preempt_type == PREEMPT_AND_ABORT)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002992 core_scsi3_release_preempt_and_abort(
2993 &preempt_and_abort_list, pr_reg_n);
2994 }
2995 spin_unlock(&dev->dev_reservation_lock);
2996
Andy Grover459f2132013-05-16 10:41:02 -07002997 if (pr_tmpl->pr_aptpl_active)
2998 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002999
3000 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003001 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003002 return 0;
3003 }
3004 /*
3005 * The PREEMPTing SA reservation key matches that of the
3006 * existing persistent reservation, first, we check if
3007 * we are preempting our own reservation.
3008 * From spc4r17, section 5.7.11.4.3 Preempting
3009 * persistent reservations and registration handling
3010 *
3011 * If an all registrants persistent reservation is not
3012 * present, it is not an error for the persistent
3013 * reservation holder to preempt itself (i.e., a
3014 * PERSISTENT RESERVE OUT with a PREEMPT service action
3015 * or a PREEMPT AND ABORT service action with the
3016 * SERVICE ACTION RESERVATION KEY value equal to the
3017 * persistent reservation holder's reservation key that
3018 * is received from the persistent reservation holder).
3019 * In that case, the device server shall establish the
3020 * new persistent reservation and maintain the
3021 * registration.
3022 */
3023 prh_type = pr_res_holder->pr_res_type;
3024 prh_scope = pr_res_holder->pr_res_scope;
3025 /*
3026 * If the SERVICE ACTION RESERVATION KEY field identifies a
3027 * persistent reservation holder (see 5.7.10), the device
3028 * server shall perform a preempt by doing the following as
3029 * an uninterrupted series of actions:
3030 *
3031 * a) Release the persistent reservation for the holder
3032 * identified by the SERVICE ACTION RESERVATION KEY field;
3033 */
3034 if (pr_reg_n != pr_res_holder)
3035 __core_scsi3_complete_pro_release(dev,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08003036 pr_res_holder->pr_reg_nacl,
3037 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003038 /*
3039 * b) Remove the registrations for all I_T nexuses identified
3040 * by the SERVICE ACTION RESERVATION KEY field, except the
3041 * I_T nexus that is being used for the PERSISTENT RESERVE
3042 * OUT command. If an all registrants persistent reservation
3043 * is present and the SERVICE ACTION RESERVATION KEY field
3044 * is set to zero, then all registrations shall be removed
3045 * except for that of the I_T nexus that is being used for
3046 * the PERSISTENT RESERVE OUT command;
3047 */
3048 spin_lock(&pr_tmpl->registration_lock);
3049 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3050 &pr_tmpl->registration_list, pr_reg_list) {
3051
3052 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3053 if (calling_it_nexus)
3054 continue;
3055
3056 if (pr_reg->pr_res_key != sa_res_key)
3057 continue;
3058
3059 pr_reg_nacl = pr_reg->pr_reg_nacl;
3060 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3061 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07003062 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 calling_it_nexus);
3064 /*
3065 * e) Establish a unit attention condition for the initiator
3066 * port associated with every I_T nexus that lost its
3067 * persistent reservation and/or registration, with the
3068 * additional sense code set to REGISTRATIONS PREEMPTED;
3069 */
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003070 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003071 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003072 }
3073 spin_unlock(&pr_tmpl->registration_lock);
3074 /*
3075 * c) Establish a persistent reservation for the preempting
3076 * I_T nexus using the contents of the SCOPE and TYPE fields;
3077 */
3078 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07003079 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3080 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003081 /*
3082 * d) Process tasks as defined in 5.7.1;
3083 * e) See above..
3084 * f) If the type or scope has changed, then for every I_T nexus
3085 * whose reservation key was not removed, except for the I_T
3086 * nexus on which the PERSISTENT RESERVE OUT command was
3087 * received, the device server shall establish a unit
3088 * attention condition for the initiator port associated with
3089 * that I_T nexus, with the additional sense code set to
3090 * RESERVATIONS RELEASED. If the type or scope have not
3091 * changed, then no unit attention condition(s) shall be
3092 * established for this reason.
3093 */
3094 if ((prh_type != type) || (prh_scope != scope)) {
3095 spin_lock(&pr_tmpl->registration_lock);
3096 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3097 &pr_tmpl->registration_list, pr_reg_list) {
3098
3099 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3100 if (calling_it_nexus)
3101 continue;
3102
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003103 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003104 pr_reg->pr_res_mapped_lun, 0x2A,
3105 ASCQ_2AH_RESERVATIONS_RELEASED);
3106 }
3107 spin_unlock(&pr_tmpl->registration_lock);
3108 }
3109 spin_unlock(&dev->dev_reservation_lock);
3110 /*
3111 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3112 * All received CDBs for the matching existing reservation and
3113 * registrations undergo ABORT_TASK logic.
3114 *
3115 * From there, core_scsi3_release_preempt_and_abort() will
3116 * release every registration in the list (which have already
3117 * been removed from the primary pr_reg list), except the
3118 * new persistent reservation holder, the calling Initiator Port.
3119 */
Andy Grover33ce6a82013-05-16 10:40:54 -07003120 if (preempt_type == PREEMPT_AND_ABORT) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3122 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3123 pr_reg_n);
3124 }
3125
Andy Grover459f2132013-05-16 10:41:02 -07003126 if (pr_tmpl->pr_aptpl_active)
3127 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128
3129 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003130 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003131 return 0;
3132}
3133
Christoph Hellwigde103c92012-11-06 12:24:09 -08003134static sense_reason_t
3135core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003136 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003137{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003138 switch (type) {
3139 case PR_TYPE_WRITE_EXCLUSIVE:
3140 case PR_TYPE_EXCLUSIVE_ACCESS:
3141 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3142 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3143 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3144 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08003145 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07003146 sa_res_key, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003148 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Andy Grover33ce6a82013-05-16 10:40:54 -07003149 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003150 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003152}
3153
3154
Christoph Hellwigde103c92012-11-06 12:24:09 -08003155static sense_reason_t
3156core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3157 u64 sa_res_key, int aptpl, int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003158{
Andy Grovere3d6f902011-07-19 08:55:10 +00003159 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003160 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003161 struct se_dev_entry *dest_se_deve = NULL;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003162 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003163 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003165 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003167 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003168 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003169 const unsigned char *initiator_str;
Julia Lawall13ba5642014-11-30 19:14:12 +01003170 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003171 u32 tid_len, tmp_tid_len;
Andy Groverd2843c12013-05-16 10:40:55 -07003172 int new_reg = 0, type, scope, matching_iname;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003173 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 unsigned short rtpi;
3175 unsigned char proto_ident;
3176
Andy Grover6708bb22011-06-08 10:36:43 -07003177 if (!se_sess || !se_lun) {
3178 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003179 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003181
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003182 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3183 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003184 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003185 /*
3186 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3187 * Register behaviors for a REGISTER AND MOVE service action
3188 *
3189 * Locate the existing *pr_reg via struct se_node_acl pointers
3190 */
Andy Grover5951146d2011-07-19 10:26:37 +00003191 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003192 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003193 if (!pr_reg) {
3194 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003195 " *pr_reg for REGISTER_AND_MOVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003196 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003197 }
3198 /*
3199 * The provided reservation key much match the existing reservation key
3200 * provided during this initiator's I_T nexus registration.
3201 */
3202 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003203 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003204 " res_key: 0x%016Lx does not match existing SA REGISTER"
3205 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003206 ret = TCM_RESERVATION_CONFLICT;
3207 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003208 }
3209 /*
3210 * The service active reservation key needs to be non zero
3211 */
Andy Grover6708bb22011-06-08 10:36:43 -07003212 if (!sa_res_key) {
3213 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 " sa_res_key\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003215 ret = TCM_INVALID_PARAMETER_LIST;
3216 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003217 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003218
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003219 /*
3220 * Determine the Relative Target Port Identifier where the reservation
3221 * will be moved to for the TransportID containing SCSI initiator WWN
3222 * information.
3223 */
Andy Grover49493142012-01-16 16:57:08 -08003224 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003225 if (!buf) {
3226 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3227 goto out_put_pr_reg;
3228 }
3229
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003230 rtpi = (buf[18] & 0xff) << 8;
3231 rtpi |= buf[19] & 0xff;
3232 tid_len = (buf[20] & 0xff) << 24;
3233 tid_len |= (buf[21] & 0xff) << 16;
3234 tid_len |= (buf[22] & 0xff) << 8;
3235 tid_len |= buf[23] & 0xff;
Andy Grover49493142012-01-16 16:57:08 -08003236 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003237 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003238
3239 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003240 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003241 " does not equal CDB data_length: %u\n", tid_len,
3242 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003243 ret = TCM_INVALID_PARAMETER_LIST;
3244 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003245 }
3246
3247 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003248 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3249 if (tmp_lun->lun_rtpi != rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003250 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003251 dest_se_tpg = tmp_lun->lun_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003252 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003253 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003254 continue;
3255
Joern Engel33940d02014-09-16 16:23:12 -04003256 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003257 spin_unlock(&dev->se_port_lock);
3258
Christoph Hellwigde103c92012-11-06 12:24:09 -08003259 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003260 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003261 " for dest_se_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04003262 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003263 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3264 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003265 }
3266
3267 spin_lock(&dev->se_port_lock);
3268 break;
3269 }
3270 spin_unlock(&dev->se_port_lock);
3271
Andy Grover6708bb22011-06-08 10:36:43 -07003272 if (!dest_se_tpg || !dest_tf_ops) {
3273 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003274 " fabric ops from Relative Target Port Identifier:"
3275 " %hu\n", rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003276 ret = TCM_INVALID_PARAMETER_LIST;
3277 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003278 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003279
Andy Grover49493142012-01-16 16:57:08 -08003280 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003281 if (!buf) {
3282 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3283 goto out_put_pr_reg;
3284 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003285 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003286
Andy Grover6708bb22011-06-08 10:36:43 -07003287 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003288 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003289
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003290 if (proto_ident != dest_se_tpg->proto_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07003291 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003292 " proto_ident: 0x%02x does not match ident: 0x%02x"
3293 " from fabric: %s\n", proto_ident,
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003294 dest_se_tpg->proto_id,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 dest_tf_ops->get_fabric_name());
Christoph Hellwigde103c92012-11-06 12:24:09 -08003296 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003297 goto out;
3298 }
Christoph Hellwig2650d712015-05-01 17:47:58 +02003299 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003300 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003301 if (!initiator_str) {
3302 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003303 " initiator_str from Transport ID\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003304 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003305 goto out;
3306 }
3307
Andy Grover49493142012-01-16 16:57:08 -08003308 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003309 buf = NULL;
3310
Andy Grover6708bb22011-06-08 10:36:43 -07003311 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003312 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3313 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3314 iport_ptr : "");
3315 /*
3316 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3317 * action specifies a TransportID that is the same as the initiator port
3318 * of the I_T nexus for the command received, then the command shall
3319 * be terminated with CHECK CONDITION status, with the sense key set to
3320 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3321 * IN PARAMETER LIST.
3322 */
3323 pr_reg_nacl = pr_reg->pr_reg_nacl;
3324 matching_iname = (!strcmp(initiator_str,
3325 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003326 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003327 goto after_iport_check;
3328
Andy Grover6708bb22011-06-08 10:36:43 -07003329 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3330 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003331 " matches: %s on received I_T Nexus\n", initiator_str,
3332 pr_reg_nacl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003333 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003334 goto out;
3335 }
Andy Grover6708bb22011-06-08 10:36:43 -07003336 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3337 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003338 " matches: %s %s on received I_T Nexus\n",
3339 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3340 pr_reg->pr_reg_isid);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003341 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003342 goto out;
3343 }
3344after_iport_check:
3345 /*
3346 * Locate the destination struct se_node_acl from the received Transport ID
3347 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003348 mutex_lock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003349 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3350 initiator_str);
Joern Engel33940d02014-09-16 16:23:12 -04003351 if (dest_node_acl)
3352 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003353 mutex_unlock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003354
Andy Grover6708bb22011-06-08 10:36:43 -07003355 if (!dest_node_acl) {
3356 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003357 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3358 initiator_str);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003359 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003360 goto out;
3361 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003362
3363 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003364 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003365 " dest_node_acl\n");
Joern Engel33940d02014-09-16 16:23:12 -04003366 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003367 dest_node_acl = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003368 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003369 goto out;
3370 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003371
Andy Grover6708bb22011-06-08 10:36:43 -07003372 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003373 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3374 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003375
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003376 /*
3377 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3378 * PORT IDENTIFIER.
3379 */
3380 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003381 if (!dest_se_deve) {
3382 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003383 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003384 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003385 goto out;
3386 }
3387
Christoph Hellwigde103c92012-11-06 12:24:09 -08003388 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003389 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07003390 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003391 dest_se_deve = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003392 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003393 goto out;
3394 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003395
Andy Grover6708bb22011-06-08 10:36:43 -07003396 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Hannes Reineckef2d30682015-06-10 08:41:22 +02003397 " ACL for dest_se_deve->mapped_lun: %llu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003398 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3399 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003400
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003401 /*
3402 * A persistent reservation needs to already existing in order to
3403 * successfully complete the REGISTER_AND_MOVE service action..
3404 */
3405 spin_lock(&dev->dev_reservation_lock);
3406 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003407 if (!pr_res_holder) {
3408 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003409 " currently held\n");
3410 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003411 ret = TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003412 goto out;
3413 }
3414 /*
3415 * The received on I_T Nexus must be the reservation holder.
3416 *
3417 * From spc4r17 section 5.7.8 Table 50 --
3418 * Register behaviors for a REGISTER AND MOVE service action
3419 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02003420 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003421 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422 " Nexus is not reservation holder\n");
3423 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003424 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003425 goto out;
3426 }
3427 /*
3428 * From spc4r17 section 5.7.8: registering and moving reservation
3429 *
3430 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3431 * action is received and the established persistent reservation is a
3432 * Write Exclusive - All Registrants type or Exclusive Access -
3433 * All Registrants type reservation, then the command shall be completed
3434 * with RESERVATION CONFLICT status.
3435 */
3436 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3437 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003438 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003439 " reservation for type: %s\n",
3440 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3441 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003442 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003443 goto out;
3444 }
3445 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3446 /*
3447 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3448 */
3449 type = pr_res_holder->pr_res_type;
3450 scope = pr_res_holder->pr_res_type;
3451 /*
3452 * c) Associate the reservation key specified in the SERVICE ACTION
3453 * RESERVATION KEY field with the I_T nexus specified as the
3454 * destination of the register and move, where:
3455 * A) The I_T nexus is specified by the TransportID and the
3456 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3457 * B) Regardless of the TransportID format used, the association for
3458 * the initiator port is based on either the initiator port name
3459 * (see 3.1.71) on SCSI transport protocols where port names are
3460 * required or the initiator port identifier (see 3.1.70) on SCSI
3461 * transport protocols where port names are not required;
3462 * d) Register the reservation key specified in the SERVICE ACTION
3463 * RESERVATION KEY field;
3464 * e) Retain the reservation key specified in the SERVICE ACTION
3465 * RESERVATION KEY field and associated information;
3466 *
3467 * Also, It is not an error for a REGISTER AND MOVE service action to
3468 * register an I_T nexus that is already registered with the same
3469 * reservation key or a different reservation key.
3470 */
3471 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3472 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003473 if (!dest_pr_reg) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003474 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
3475 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
3476
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003477 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003478 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3479 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3480 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08003481 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003482 goto out;
3483 }
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003484 spin_lock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003485 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3486 iport_ptr);
3487 new_reg = 1;
3488 }
3489 /*
3490 * f) Release the persistent reservation for the persistent reservation
3491 * holder (i.e., the I_T nexus on which the
3492 */
3493 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08003494 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003495 /*
3496 * g) Move the persistent reservation to the specified I_T nexus using
3497 * the same scope and type as the persistent reservation released in
3498 * item f); and
3499 */
3500 dev->dev_pr_res_holder = dest_pr_reg;
3501 dest_pr_reg->pr_res_holder = 1;
3502 dest_pr_reg->pr_res_type = type;
3503 pr_reg->pr_res_scope = scope;
Andy Groverd2843c12013-05-16 10:40:55 -07003504 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003505 /*
3506 * Increment PRGeneration for existing registrations..
3507 */
Andy Grover6708bb22011-06-08 10:36:43 -07003508 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003509 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3510 spin_unlock(&dev->dev_reservation_lock);
3511
Andy Grover6708bb22011-06-08 10:36:43 -07003512 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003513 " created new reservation holder TYPE: %s on object RTPI:"
3514 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3515 core_scsi3_pr_dump_type(type), rtpi,
3516 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003517 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003518 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3519 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07003520 i_buf, dest_tf_ops->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003521 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3522 iport_ptr : "");
3523 /*
3524 * It is now safe to release configfs group dependencies for destination
3525 * of Transport ID Initiator Device/Port Identifier
3526 */
3527 core_scsi3_lunacl_undepend_item(dest_se_deve);
3528 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3529 core_scsi3_tpg_undepend_item(dest_se_tpg);
3530 /*
3531 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3532 * nexus on which PERSISTENT RESERVE OUT command was received.
3533 */
3534 if (unreg) {
3535 spin_lock(&pr_tmpl->registration_lock);
3536 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3537 spin_unlock(&pr_tmpl->registration_lock);
3538 } else
3539 core_scsi3_put_pr_reg(pr_reg);
3540
Andy Grover459f2132013-05-16 10:41:02 -07003541 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003542
Andy Grover49493142012-01-16 16:57:08 -08003543 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003544
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003545 core_scsi3_put_pr_reg(dest_pr_reg);
3546 return 0;
3547out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003548 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003549 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003550 if (dest_se_deve)
3551 core_scsi3_lunacl_undepend_item(dest_se_deve);
3552 if (dest_node_acl)
3553 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3554 core_scsi3_tpg_undepend_item(dest_se_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003555
3556out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003557 core_scsi3_put_pr_reg(pr_reg);
3558 return ret;
3559}
3560
3561static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3562{
3563 unsigned int __v1, __v2;
3564
3565 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3566 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3567
3568 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3569}
3570
3571/*
3572 * See spc4r17 section 6.14 Table 170
3573 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003574sense_reason_t
3575target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003576{
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003577 struct se_device *dev = cmd->se_dev;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003578 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003579 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003580 u64 res_key, sa_res_key;
3581 int sa, scope, type, aptpl;
3582 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003583 sense_reason_t ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003584
3585 /*
3586 * Following spc2r20 5.5.1 Reservations overview:
3587 *
3588 * If a logical unit has been reserved by any RESERVE command and is
3589 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3590 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3591 * initiator or service action and shall terminate with a RESERVATION
3592 * CONFLICT status.
3593 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003594 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003595 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3596 " SPC-2 reservation is held, returning"
3597 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003598 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003599 }
3600
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003601 /*
3602 * FIXME: A NULL struct se_session pointer means an this is not coming from
3603 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3604 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003605 if (!cmd->se_sess)
3606 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003607
3608 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003609 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003610 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003611 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003612 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003613
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003614 /*
3615 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3616 */
3617 sa = (cdb[1] & 0x1f);
3618 scope = (cdb[2] & 0xf0);
3619 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003620
Andy Grover49493142012-01-16 16:57:08 -08003621 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003622 if (!buf)
3623 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3624
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003625 /*
3626 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3627 */
3628 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3629 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3630 /*
3631 * REGISTER_AND_MOVE uses a different SA parameter list containing
3632 * SCSI TransportIDs.
3633 */
3634 if (sa != PRO_REGISTER_AND_MOVE) {
3635 spec_i_pt = (buf[20] & 0x08);
3636 all_tg_pt = (buf[20] & 0x04);
3637 aptpl = (buf[20] & 0x01);
3638 } else {
3639 aptpl = (buf[17] & 0x01);
3640 unreg = (buf[17] & 0x02);
3641 }
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003642 /*
3643 * If the backend device has been configured to force APTPL metadata
3644 * write-out, go ahead and propigate aptpl=1 down now.
3645 */
3646 if (dev->dev_attrib.force_pr_aptpl)
3647 aptpl = 1;
3648
Andy Grover49493142012-01-16 16:57:08 -08003649 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003650 buf = NULL;
3651
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003652 /*
3653 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3654 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003655 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3656 return TCM_INVALID_PARAMETER_LIST;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003657
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003658 /*
3659 * From spc4r17 section 6.14:
3660 *
3661 * If the SPEC_I_PT bit is set to zero, the service action is not
3662 * REGISTER AND MOVE, and the parameter list length is not 24, then
3663 * the command shall be terminated with CHECK CONDITION status, with
3664 * the sense key set to ILLEGAL REQUEST, and the additional sense
3665 * code set to PARAMETER LIST LENGTH ERROR.
3666 */
Andy Grover6708bb22011-06-08 10:36:43 -07003667 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003668 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003669 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003670 " list length: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003671 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003672 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003673
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003674 /*
3675 * (core_scsi3_emulate_pro_* function parameters
3676 * are defined by spc4r17 Table 174:
3677 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3678 */
3679 switch (sa) {
3680 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003681 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003682 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003683 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003684 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003685 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3686 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003687 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003688 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3689 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003690 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003691 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3692 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003693 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003694 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003695 res_key, sa_res_key, PREEMPT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003696 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003697 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003698 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003699 res_key, sa_res_key, PREEMPT_AND_ABORT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003700 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003701 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003702 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003703 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003704 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003705 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003706 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003707 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003708 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003709 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003710 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003711 " action: 0x%02x\n", cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003712 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003713 }
3714
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003715 if (!ret)
3716 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003717 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003718}
3719
3720/*
3721 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3722 *
3723 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3724 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003725static sense_reason_t
3726core_scsi3_pri_read_keys(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003727{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003728 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003729 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003730 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003731 u32 add_len = 0, off = 8;
3732
3733 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003734 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003735 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003736 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003737 }
3738
Andy Grover49493142012-01-16 16:57:08 -08003739 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003740 if (!buf)
3741 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3742
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003743 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3744 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3745 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3746 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003747
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003748 spin_lock(&dev->t10_pr.registration_lock);
3749 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003750 pr_reg_list) {
3751 /*
3752 * Check for overflow of 8byte PRI READ_KEYS payload and
3753 * next reservation key list descriptor.
3754 */
3755 if ((add_len + 8) > (cmd->data_length - 8))
3756 break;
3757
3758 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3759 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3760 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3761 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3762 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3763 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3764 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3765 buf[off++] = (pr_reg->pr_res_key & 0xff);
3766
3767 add_len += 8;
3768 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003769 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003770
3771 buf[4] = ((add_len >> 24) & 0xff);
3772 buf[5] = ((add_len >> 16) & 0xff);
3773 buf[6] = ((add_len >> 8) & 0xff);
3774 buf[7] = (add_len & 0xff);
3775
Andy Grover49493142012-01-16 16:57:08 -08003776 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003777
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003778 return 0;
3779}
3780
3781/*
3782 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3783 *
3784 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3785 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003786static sense_reason_t
3787core_scsi3_pri_read_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003788{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003789 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003790 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003791 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003792 u64 pr_res_key;
3793 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3794
3795 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003796 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003797 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003798 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003799 }
3800
Andy Grover49493142012-01-16 16:57:08 -08003801 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003802 if (!buf)
3803 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3804
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003805 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3806 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3807 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3808 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003809
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003810 spin_lock(&dev->dev_reservation_lock);
3811 pr_reg = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07003812 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003813 /*
3814 * Set the hardcoded Additional Length
3815 */
3816 buf[4] = ((add_len >> 24) & 0xff);
3817 buf[5] = ((add_len >> 16) & 0xff);
3818 buf[6] = ((add_len >> 8) & 0xff);
3819 buf[7] = (add_len & 0xff);
3820
Andy Grover05d1c7c2011-07-20 19:13:28 +00003821 if (cmd->data_length < 22)
3822 goto err;
3823
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003824 /*
3825 * Set the Reservation key.
3826 *
3827 * From spc4r17, section 5.7.10:
3828 * A persistent reservation holder has its reservation key
3829 * returned in the parameter data from a PERSISTENT
3830 * RESERVE IN command with READ RESERVATION service action as
3831 * follows:
3832 * a) For a persistent reservation of the type Write Exclusive
3833 * - All Registrants or Exclusive Access ­ All Regitrants,
3834 * the reservation key shall be set to zero; or
3835 * b) For all other persistent reservation types, the
3836 * reservation key shall be set to the registered
3837 * reservation key for the I_T nexus that holds the
3838 * persistent reservation.
3839 */
3840 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3841 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3842 pr_res_key = 0;
3843 else
3844 pr_res_key = pr_reg->pr_res_key;
3845
3846 buf[8] = ((pr_res_key >> 56) & 0xff);
3847 buf[9] = ((pr_res_key >> 48) & 0xff);
3848 buf[10] = ((pr_res_key >> 40) & 0xff);
3849 buf[11] = ((pr_res_key >> 32) & 0xff);
3850 buf[12] = ((pr_res_key >> 24) & 0xff);
3851 buf[13] = ((pr_res_key >> 16) & 0xff);
3852 buf[14] = ((pr_res_key >> 8) & 0xff);
3853 buf[15] = (pr_res_key & 0xff);
3854 /*
3855 * Set the SCOPE and TYPE
3856 */
3857 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3858 (pr_reg->pr_res_type & 0x0f);
3859 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003860
3861err:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003862 spin_unlock(&dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08003863 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003864
3865 return 0;
3866}
3867
3868/*
3869 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3870 *
3871 * See spc4r17 section 6.13.4 Table 165
3872 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003873static sense_reason_t
3874core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003875{
Andy Grover5951146d2011-07-19 10:26:37 +00003876 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003877 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003878 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003879 u16 add_len = 8; /* Hardcoded to 8. */
3880
3881 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07003882 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003883 " %u too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003884 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003885 }
3886
Andy Grover49493142012-01-16 16:57:08 -08003887 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003888 if (!buf)
3889 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003890
Joern Engelc4352852014-09-02 17:50:01 -04003891 buf[0] = ((add_len >> 8) & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003892 buf[1] = (add_len & 0xff);
3893 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3894 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3895 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3896 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3897 /*
3898 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3899 * set the TMV: Task Mask Valid bit.
3900 */
3901 buf[3] |= 0x80;
3902 /*
3903 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3904 */
3905 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3906 /*
3907 * PTPL_A: Persistence across Target Power Loss Active bit
3908 */
3909 if (pr_tmpl->pr_aptpl_active)
3910 buf[3] |= 0x01;
3911 /*
3912 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3913 */
3914 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3915 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3916 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3917 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3918 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3919 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3920
Andy Grover49493142012-01-16 16:57:08 -08003921 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003922
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003923 return 0;
3924}
3925
3926/*
3927 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3928 *
3929 * See spc4r17 section 6.13.5 Table 168 and 169
3930 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003931static sense_reason_t
3932core_scsi3_pri_read_full_status(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003933{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003934 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003935 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003936 struct se_portal_group *se_tpg;
3937 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003938 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003939 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003940 u32 add_desc_len = 0, add_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003941 u32 off = 8; /* off into first Full Status descriptor */
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003942 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003943 int exp_desc_len, desc_len;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003944 bool all_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003945
3946 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003947 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003948 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003949 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003950 }
3951
Andy Grover49493142012-01-16 16:57:08 -08003952 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003953 if (!buf)
3954 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003955
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003956 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3957 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3958 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3959 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003960
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003961 spin_lock(&dev->dev_reservation_lock);
3962 if (dev->dev_pr_res_holder) {
3963 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3964
3965 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3966 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3967 all_reg = true;
3968 pr_res_type = pr_holder->pr_res_type;
3969 pr_res_scope = pr_holder->pr_res_scope;
3970 }
3971 }
3972 spin_unlock(&dev->dev_reservation_lock);
3973
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003974 spin_lock(&pr_tmpl->registration_lock);
3975 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3976 &pr_tmpl->registration_list, pr_reg_list) {
3977
3978 se_nacl = pr_reg->pr_reg_nacl;
3979 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3980 add_desc_len = 0;
3981
Joern Engel33940d02014-09-16 16:23:12 -04003982 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003983 spin_unlock(&pr_tmpl->registration_lock);
3984 /*
3985 * Determine expected length of $FABRIC_MOD specific
3986 * TransportID full status descriptor..
3987 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02003988 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3989 &format_code);
3990 if (exp_desc_len < 0 ||
3991 exp_desc_len + add_len > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003992 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003993 " out of buffer: %d\n", cmd->data_length);
3994 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04003995 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003996 break;
3997 }
3998 /*
3999 * Set RESERVATION KEY
4000 */
4001 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4002 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4003 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4004 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4005 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4006 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4007 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4008 buf[off++] = (pr_reg->pr_res_key & 0xff);
4009 off += 4; /* Skip Over Reserved area */
4010
4011 /*
4012 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4013 */
4014 if (pr_reg->pr_reg_all_tg_pt)
4015 buf[off] = 0x02;
4016 /*
4017 * The struct se_lun pointer will be present for the
4018 * reservation holder for PR_HOLDER bit.
4019 *
4020 * Also, if this registration is the reservation
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08004021 * holder or there is an All Registrants reservation
4022 * active, fill in SCOPE and TYPE in the next byte.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004023 */
4024 if (pr_reg->pr_res_holder) {
4025 buf[off++] |= 0x01;
4026 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4027 (pr_reg->pr_res_type & 0x0f);
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08004028 } else if (all_reg) {
4029 buf[off++] |= 0x01;
4030 buf[off++] = (pr_res_scope & 0xf0) |
4031 (pr_res_type & 0x0f);
4032 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004033 off += 2;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08004034 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004035
4036 off += 4; /* Skip over reserved area */
4037 /*
4038 * From spc4r17 6.3.15:
4039 *
4040 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4041 * IDENTIFIER field contains the relative port identifier (see
4042 * 3.1.120) of the target port that is part of the I_T nexus
4043 * described by this full status descriptor. If the ALL_TG_PT
4044 * bit is set to one, the contents of the RELATIVE TARGET PORT
4045 * IDENTIFIER field are not defined by this standard.
4046 */
Andy Grover6708bb22011-06-08 10:36:43 -07004047 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00004048 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004049
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00004050 buf[off++] = ((sep_rtpi >> 8) & 0xff);
4051 buf[off++] = (sep_rtpi & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004052 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09004053 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004054
Christoph Hellwig2650d712015-05-01 17:47:58 +02004055 buf[off+4] = se_tpg->proto_id;
4056
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004057 /*
Christoph Hellwig2650d712015-05-01 17:47:58 +02004058 * Now, have the $FABRIC_MOD fill in the transport ID.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004059 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02004060 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4061 &format_code, &buf[off+4]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004062
4063 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04004064 atomic_dec_mb(&pr_reg->pr_res_holders);
Christoph Hellwig2650d712015-05-01 17:47:58 +02004065
4066 if (desc_len < 0)
4067 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004068 /*
4069 * Set the ADDITIONAL DESCRIPTOR LENGTH
4070 */
4071 buf[off++] = ((desc_len >> 24) & 0xff);
4072 buf[off++] = ((desc_len >> 16) & 0xff);
4073 buf[off++] = ((desc_len >> 8) & 0xff);
4074 buf[off++] = (desc_len & 0xff);
4075 /*
4076 * Size of full desctipor header minus TransportID
4077 * containing $FABRIC_MOD specific) initiator device/port
4078 * WWN information.
4079 *
4080 * See spc4r17 Section 6.13.5 Table 169
4081 */
4082 add_desc_len = (24 + desc_len);
4083
4084 off += desc_len;
4085 add_len += add_desc_len;
4086 }
4087 spin_unlock(&pr_tmpl->registration_lock);
4088 /*
4089 * Set ADDITIONAL_LENGTH
4090 */
4091 buf[4] = ((add_len >> 24) & 0xff);
4092 buf[5] = ((add_len >> 16) & 0xff);
4093 buf[6] = ((add_len >> 8) & 0xff);
4094 buf[7] = (add_len & 0xff);
4095
Andy Grover49493142012-01-16 16:57:08 -08004096 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004097
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004098 return 0;
4099}
4100
Christoph Hellwigde103c92012-11-06 12:24:09 -08004101sense_reason_t
4102target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004103{
Christoph Hellwigde103c92012-11-06 12:24:09 -08004104 sense_reason_t ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004105
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004106 /*
4107 * Following spc2r20 5.5.1 Reservations overview:
4108 *
4109 * If a logical unit has been reserved by any RESERVE command and is
4110 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4111 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4112 * initiator or service action and shall terminate with a RESERVATION
4113 * CONFLICT status.
4114 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004115 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004116 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4117 " SPC-2 reservation is held, returning"
4118 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08004119 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004120 }
4121
4122 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004123 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004124 ret = core_scsi3_pri_read_keys(cmd);
4125 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004126 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004127 ret = core_scsi3_pri_read_reservation(cmd);
4128 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004129 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004130 ret = core_scsi3_pri_report_capabilities(cmd);
4131 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004132 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004133 ret = core_scsi3_pri_read_full_status(cmd);
4134 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004135 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004136 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004137 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08004138 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004139 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004140
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004141 if (!ret)
4142 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004143 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004144}
4145
Christoph Hellwigde103c92012-11-06 12:24:09 -08004146sense_reason_t
4147target_check_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004148{
Christoph Hellwigd977f432012-10-10 17:37:15 -04004149 struct se_device *dev = cmd->se_dev;
Christoph Hellwigde103c92012-11-06 12:24:09 -08004150 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004151
Christoph Hellwigd977f432012-10-10 17:37:15 -04004152 if (!cmd->se_sess)
4153 return 0;
4154 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4155 return 0;
Andy Grovera3541702015-05-19 14:44:41 -07004156 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04004157 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004158
Christoph Hellwigd977f432012-10-10 17:37:15 -04004159 spin_lock(&dev->dev_reservation_lock);
4160 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4161 ret = target_scsi2_reservation_check(cmd);
4162 else
4163 ret = target_scsi3_pr_reservation_check(cmd);
4164 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004165
Christoph Hellwigd977f432012-10-10 17:37:15 -04004166 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004167}