blob: df20921c233c18454dd86e68bb04809ee627eeff [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{
tangwenji998201f2017-08-24 19:59:37 +080059 if (!pr_reg->isid_present_at_reg) {
Andy Groverd2843c12013-05-16 10:40:55 -070060 buf[0] = '\0';
tangwenji998201f2017-08-24 19:59:37 +080061 return;
62 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063
Andy Groverd2843c12013-05-16 10:40:55 -070064 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080065}
66
Andy Grover33ce6a82013-05-16 10:40:54 -070067enum register_type {
68 REGISTER,
69 REGISTER_AND_IGNORE_EXISTING_KEY,
70 REGISTER_AND_MOVE,
71};
72
73enum preempt_type {
74 PREEMPT,
75 PREEMPT_AND_ABORT,
76};
77
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -080079 struct t10_pr_registration *, int, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +020081static int is_reservation_holder(
82 struct t10_pr_registration *pr_res_holder,
83 struct t10_pr_registration *pr_reg)
84{
85 int pr_res_type;
86
87 if (pr_res_holder) {
88 pr_res_type = pr_res_holder->pr_res_type;
89
90 return pr_res_holder == pr_reg ||
91 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
92 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
93 }
94 return 0;
95}
96
Christoph Hellwigde103c92012-11-06 12:24:09 -080097static sense_reason_t
98target_scsi2_reservation_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400100 struct se_device *dev = cmd->se_dev;
101 struct se_session *sess = cmd->se_sess;
102
103 switch (cmd->t_task_cdb[0]) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 case INQUIRY:
105 case RELEASE:
106 case RELEASE_10:
107 return 0;
108 default:
Christoph Hellwigd977f432012-10-10 17:37:15 -0400109 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800110 }
111
Christoph Hellwigd977f432012-10-10 17:37:15 -0400112 if (!dev->dev_reserved_node_acl || !sess)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113 return 0;
114
Christoph Hellwigd977f432012-10-10 17:37:15 -0400115 if (dev->dev_reserved_node_acl != sess->se_node_acl)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800116 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800117
Christoph Hellwigd977f432012-10-10 17:37:15 -0400118 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
119 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
Christoph Hellwigde103c92012-11-06 12:24:09 -0800120 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400121 }
122
123 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800124}
125
Christoph Hellwigeacac002011-11-03 17:50:40 -0400126static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
127 struct se_node_acl *, struct se_session *);
128static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
129
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700130static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
Christoph Hellwigeacac002011-11-03 17:50:40 -0400131{
132 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400133 struct se_device *dev = cmd->se_dev;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400134 struct t10_pr_registration *pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400135 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400136 int conflict = 0;
137
Christoph Hellwigeacac002011-11-03 17:50:40 -0400138 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
139 se_sess);
140 if (pr_reg) {
141 /*
142 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
143 * behavior
144 *
145 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
146 * status, but no reservation shall be established and the
147 * persistent reservation shall not be changed, if the command
148 * is received from a) and b) below.
149 *
150 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
151 * status, but the persistent reservation shall not be released,
152 * if the command is received from a) and b)
153 *
154 * a) An I_T nexus that is a persistent reservation holder; or
155 * b) An I_T nexus that is registered if a registrants only or
156 * all registrants type persistent reservation is present.
157 *
158 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
159 * RELEASE(6) command, or RELEASE(10) command shall be processed
160 * as defined in SPC-2.
161 */
162 if (pr_reg->pr_res_holder) {
163 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700164 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400165 }
166 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
167 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
168 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
169 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
170 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700171 return 1;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400172 }
173 core_scsi3_put_pr_reg(pr_reg);
174 conflict = 1;
175 } else {
176 /*
177 * Following spc2r20 5.5.1 Reservations overview:
178 *
179 * If a logical unit has executed a PERSISTENT RESERVE OUT
180 * command with the REGISTER or the REGISTER AND IGNORE
181 * EXISTING KEY service action and is still registered by any
182 * initiator, all RESERVE commands and all RELEASE commands
183 * regardless of initiator shall conflict and shall terminate
184 * with a RESERVATION CONFLICT status.
185 */
186 spin_lock(&pr_tmpl->registration_lock);
187 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
188 spin_unlock(&pr_tmpl->registration_lock);
189 }
190
191 if (conflict) {
192 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
193 " while active SPC-3 registrations exist,"
194 " returning RESERVATION_CONFLICT\n");
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700195 return -EBUSY;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400196 }
197
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700198 return 0;
Christoph Hellwigeacac002011-11-03 17:50:40 -0400199}
200
Christoph Hellwigde103c92012-11-06 12:24:09 -0800201sense_reason_t
202target_scsi2_reservation_release(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800203{
204 struct se_device *dev = cmd->se_dev;
205 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800206 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800207 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208
Wei Yongjun609234e2012-09-07 14:56:28 +0800209 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400210 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700211 rc = target_check_scsi2_reservation_conflict(cmd);
212 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400213 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800214 if (rc < 0)
215 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800216
217 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400218 if (!dev->dev_reserved_node_acl || !sess)
219 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400221 if (dev->dev_reserved_node_acl != sess->se_node_acl)
222 goto out_unlock;
223
Bernhard Kohledc318d2012-05-13 23:39:37 +0200224 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
225 goto out_unlock;
226
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800227 dev->dev_reserved_node_acl = NULL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400228 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
229 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800230 dev->dev_res_bin_isid = 0;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400231 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232 }
Wei Yongjun609234e2012-09-07 14:56:28 +0800233 tpg = sess->se_tpg;
Hannes Reineckef2d30682015-06-10 08:41:22 +0200234 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
235 " MAPPED LUN: %llu for %s\n",
236 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700237 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400240out_unlock:
241 spin_unlock(&dev->dev_reservation_lock);
242out:
Christoph Hellwigde103c92012-11-06 12:24:09 -0800243 target_complete_cmd(cmd, GOOD);
244 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800245}
246
Christoph Hellwigde103c92012-11-06 12:24:09 -0800247sense_reason_t
248target_scsi2_reservation_reserve(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249{
250 struct se_device *dev = cmd->se_dev;
251 struct se_session *sess = cmd->se_sess;
Wei Yongjun609234e2012-09-07 14:56:28 +0800252 struct se_portal_group *tpg;
Christoph Hellwigde103c92012-11-06 12:24:09 -0800253 sense_reason_t ret = 0;
254 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800255
Andy Grovera1d8b492011-05-02 17:12:10 -0700256 if ((cmd->t_task_cdb[1] & 0x01) &&
257 (cmd->t_task_cdb[1] & 0x02)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700258 pr_err("LongIO and Obselete Bits set, returning"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 " ILLEGAL_REQUEST\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800260 return TCM_UNSUPPORTED_SCSI_OPCODE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800261 }
262 /*
263 * This is currently the case for target_core_mod passthrough struct se_cmd
264 * ops
265 */
Wei Yongjun609234e2012-09-07 14:56:28 +0800266 if (!sess || !sess->se_tpg)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400267 goto out;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -0700268 rc = target_check_scsi2_reservation_conflict(cmd);
269 if (rc == 1)
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400270 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800271
Christoph Hellwigde103c92012-11-06 12:24:09 -0800272 if (rc < 0)
273 return TCM_RESERVATION_CONFLICT;
274
Wei Yongjun609234e2012-09-07 14:56:28 +0800275 tpg = sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276 spin_lock(&dev->dev_reservation_lock);
277 if (dev->dev_reserved_node_acl &&
278 (dev->dev_reserved_node_acl != sess->se_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700279 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000280 tpg->se_tpg_tfo->get_fabric_name());
Hannes Reineckef2d30682015-06-10 08:41:22 +0200281 pr_err("Original reserver LUN: %llu %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000282 cmd->se_lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800283 dev->dev_reserved_node_acl->initiatorname);
Hannes Reineckef2d30682015-06-10 08:41:22 +0200284 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
Andy Grovere3d6f902011-07-19 08:55:10 +0000285 " from %s \n", cmd->se_lun->unpacked_lun,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700286 cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 sess->se_node_acl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800288 ret = TCM_RESERVATION_CONFLICT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400289 goto out_unlock;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290 }
291
292 dev->dev_reserved_node_acl = sess->se_node_acl;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400293 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800294 if (sess->sess_bin_isid != 0) {
295 dev->dev_res_bin_isid = sess->sess_bin_isid;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400296 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800297 }
Hannes Reineckef2d30682015-06-10 08:41:22 +0200298 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
Andy Grovere3d6f902011-07-19 08:55:10 +0000299 " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700300 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 sess->se_node_acl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400303out_unlock:
304 spin_unlock(&dev->dev_reservation_lock);
305out:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400306 if (!ret)
307 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400308 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800309}
310
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311
312/*
313 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
314 *
315 * This function is called by those initiator ports who are *NOT*
316 * the active PR reservation holder when a reservation is present.
317 */
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200318static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
319 bool isid_mismatch)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800320{
Christoph Hellwigd977f432012-10-10 17:37:15 -0400321 unsigned char *cdb = cmd->t_task_cdb;
Andy Grovere3d6f902011-07-19 08:55:10 +0000322 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700323 struct se_node_acl *nacl = se_sess->se_node_acl;
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200324 int other_cdb = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325 int registered_nexus = 0, ret = 1; /* Conflict by default */
326 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
327 int we = 0; /* Write Exclusive */
328 int legacy = 0; /* Act like a legacy device and return
329 * RESERVATION CONFLICT on some CDBs */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200331 if (isid_mismatch) {
332 registered_nexus = 0;
333 } else {
334 struct se_dev_entry *se_deve;
335
336 rcu_read_lock();
337 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
338 if (se_deve)
339 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
340 &se_deve->deve_flags);
341 rcu_read_unlock();
342 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800343
344 switch (pr_reg_type) {
345 case PR_TYPE_WRITE_EXCLUSIVE:
346 we = 1;
347 case PR_TYPE_EXCLUSIVE_ACCESS:
348 /*
349 * Some commands are only allowed for the persistent reservation
350 * holder.
351 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352 break;
353 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
354 we = 1;
355 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
356 /*
357 * Some commands are only allowed for registered I_T Nexuses.
358 */
359 reg_only = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 break;
361 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
362 we = 1;
363 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
364 /*
365 * Each registered I_T Nexus is a reservation holder.
366 */
367 all_reg = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800368 break;
369 default:
Andy Grovere3d6f902011-07-19 08:55:10 +0000370 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 }
372 /*
373 * Referenced from spc4r17 table 45 for *NON* PR holder access
374 */
375 switch (cdb[0]) {
376 case SECURITY_PROTOCOL_IN:
377 if (registered_nexus)
378 return 0;
379 ret = (we) ? 0 : 1;
380 break;
381 case MODE_SENSE:
382 case MODE_SENSE_10:
383 case READ_ATTRIBUTE:
384 case READ_BUFFER:
385 case RECEIVE_DIAGNOSTIC:
386 if (legacy) {
387 ret = 1;
388 break;
389 }
390 if (registered_nexus) {
391 ret = 0;
392 break;
393 }
394 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
395 break;
396 case PERSISTENT_RESERVE_OUT:
397 /*
398 * This follows PERSISTENT_RESERVE_OUT service actions that
399 * are allowed in the presence of various reservations.
400 * See spc4r17, table 46
401 */
402 switch (cdb[1] & 0x1f) {
403 case PRO_CLEAR:
404 case PRO_PREEMPT:
405 case PRO_PREEMPT_AND_ABORT:
406 ret = (registered_nexus) ? 0 : 1;
407 break;
408 case PRO_REGISTER:
409 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
410 ret = 0;
411 break;
412 case PRO_REGISTER_AND_MOVE:
413 case PRO_RESERVE:
414 ret = 1;
415 break;
416 case PRO_RELEASE:
417 ret = (registered_nexus) ? 0 : 1;
418 break;
419 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700420 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800421 " action: 0x%02x\n", cdb[1] & 0x1f);
Andy Grovere3d6f902011-07-19 08:55:10 +0000422 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800423 }
424 break;
425 case RELEASE:
426 case RELEASE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400427 /* Handled by CRH=1 in target_scsi2_reservation_release() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800428 ret = 0;
429 break;
430 case RESERVE:
431 case RESERVE_10:
Christoph Hellwigeacac002011-11-03 17:50:40 -0400432 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800433 ret = 0;
434 break;
435 case TEST_UNIT_READY:
436 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
437 break;
438 case MAINTENANCE_IN:
439 switch (cdb[1] & 0x1f) {
440 case MI_MANAGEMENT_PROTOCOL_IN:
441 if (registered_nexus) {
442 ret = 0;
443 break;
444 }
445 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
446 break;
447 case MI_REPORT_SUPPORTED_OPERATION_CODES:
448 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
449 if (legacy) {
450 ret = 1;
451 break;
452 }
453 if (registered_nexus) {
454 ret = 0;
455 break;
456 }
457 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
458 break;
459 case MI_REPORT_ALIASES:
460 case MI_REPORT_IDENTIFYING_INFORMATION:
461 case MI_REPORT_PRIORITY:
462 case MI_REPORT_TARGET_PGS:
463 case MI_REPORT_TIMESTAMP:
464 ret = 0; /* Allowed */
465 break;
466 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700467 pr_err("Unknown MI Service Action: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800468 (cdb[1] & 0x1f));
Andy Grovere3d6f902011-07-19 08:55:10 +0000469 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470 }
471 break;
472 case ACCESS_CONTROL_IN:
473 case ACCESS_CONTROL_OUT:
474 case INQUIRY:
475 case LOG_SENSE:
Hannes Reinecke85686f62014-11-17 14:25:20 +0100476 case SERVICE_ACTION_IN_12:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477 case REPORT_LUNS:
478 case REQUEST_SENSE:
Marco Sanvido68169662012-01-03 17:12:58 -0800479 case PERSISTENT_RESERVE_IN:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800480 ret = 0; /*/ Allowed CDBs */
481 break;
482 default:
483 other_cdb = 1;
484 break;
485 }
486 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300487 * Case where the CDB is explicitly allowed in the above switch
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800488 * statement.
489 */
Andy Grover6708bb22011-06-08 10:36:43 -0700490 if (!ret && !other_cdb) {
Hannes Reinecke125d0112013-11-19 09:07:46 +0100491 pr_debug("Allowing explicit CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492 " reservation holder\n", cdb[0],
493 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700494
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800495 return ret;
496 }
497 /*
498 * Check if write exclusive initiator ports *NOT* holding the
499 * WRITE_EXCLUSIVE_* reservation.
500 */
Andy Groveree1b1b92012-07-12 17:34:54 -0700501 if (we && !registered_nexus) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502 if (cmd->data_direction == DMA_TO_DEVICE) {
503 /*
504 * Conflict for write exclusive
505 */
Andy Grover6708bb22011-06-08 10:36:43 -0700506 pr_debug("%s Conflict for unregistered nexus"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800507 " %s CDB: 0x%02x to %s reservation\n",
508 transport_dump_cmd_direction(cmd),
509 se_sess->se_node_acl->initiatorname, cdb[0],
510 core_scsi3_pr_dump_type(pr_reg_type));
511 return 1;
512 } else {
513 /*
514 * Allow non WRITE CDBs for all Write Exclusive
515 * PR TYPEs to pass for registered and
516 * non-registered_nexuxes NOT holding the reservation.
517 *
518 * We only make noise for the unregisterd nexuses,
519 * as we expect registered non-reservation holding
520 * nexuses to issue CDBs.
521 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700522
Andy Grover6708bb22011-06-08 10:36:43 -0700523 if (!registered_nexus) {
Hannes Reinecke125d0112013-11-19 09:07:46 +0100524 pr_debug("Allowing implicit CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 " for %s reservation on unregistered"
526 " nexus\n", cdb[0],
527 core_scsi3_pr_dump_type(pr_reg_type));
528 }
Andy Grover8b1e1242012-04-03 15:51:12 -0700529
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530 return 0;
531 }
532 } else if ((reg_only) || (all_reg)) {
533 if (registered_nexus) {
534 /*
535 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
536 * allow commands from registered nexuses.
537 */
Andy Grover8b1e1242012-04-03 15:51:12 -0700538
Hannes Reinecke125d0112013-11-19 09:07:46 +0100539 pr_debug("Allowing implicit CDB: 0x%02x for %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540 " reservation\n", cdb[0],
541 core_scsi3_pr_dump_type(pr_reg_type));
Andy Grover8b1e1242012-04-03 15:51:12 -0700542
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800543 return 0;
544 }
Lee Duncan1ecc7582015-01-05 10:49:44 -0800545 } else if (we && registered_nexus) {
546 /*
547 * Reads are allowed for Write Exclusive locks
548 * from all registrants.
549 */
550 if (cmd->data_direction == DMA_FROM_DEVICE) {
551 pr_debug("Allowing READ CDB: 0x%02x for %s"
552 " reservation\n", cdb[0],
553 core_scsi3_pr_dump_type(pr_reg_type));
554
555 return 0;
556 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 }
Andy Grover6708bb22011-06-08 10:36:43 -0700558 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800559 " for %s reservation\n", transport_dump_cmd_direction(cmd),
560 (registered_nexus) ? "" : "un",
561 se_sess->se_node_acl->initiatorname, cdb[0],
562 core_scsi3_pr_dump_type(pr_reg_type));
563
564 return 1; /* Conflict by default */
565}
566
Christoph Hellwigde103c92012-11-06 12:24:09 -0800567static sense_reason_t
568target_scsi3_pr_reservation_check(struct se_cmd *cmd)
Christoph Hellwigd977f432012-10-10 17:37:15 -0400569{
570 struct se_device *dev = cmd->se_dev;
571 struct se_session *sess = cmd->se_sess;
572 u32 pr_reg_type;
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200573 bool isid_mismatch = false;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400574
575 if (!dev->dev_pr_res_holder)
576 return 0;
577
578 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
579 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
580 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
581 goto check_nonholder;
582
583 if (dev->dev_pr_res_holder->isid_present_at_reg) {
584 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
585 sess->sess_bin_isid) {
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200586 isid_mismatch = true;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400587 goto check_nonholder;
588 }
589 }
590
591 return 0;
592
593check_nonholder:
Christoph Hellwig9fcb57f2015-03-29 09:39:03 +0200594 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
Christoph Hellwigde103c92012-11-06 12:24:09 -0800595 return TCM_RESERVATION_CONFLICT;
Christoph Hellwigd977f432012-10-10 17:37:15 -0400596 return 0;
597}
598
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599static u32 core_scsi3_pr_generation(struct se_device *dev)
600{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601 u32 prg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400602
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800603 /*
604 * PRGeneration field shall contain the value of a 32-bit wrapping
605 * counter mainted by the device server.
606 *
607 * Note that this is done regardless of Active Persist across
608 * Target PowerLoss (APTPL)
609 *
610 * See spc4r17 section 6.3.12 READ_KEYS service action
611 */
612 spin_lock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400613 prg = dev->t10_pr.pr_generation++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614 spin_unlock(&dev->dev_reservation_lock);
615
616 return prg;
617}
618
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800619static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
620 struct se_device *dev,
621 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000622 struct se_lun *lun,
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700623 struct se_dev_entry *dest_deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200624 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625 unsigned char *isid,
626 u64 sa_res_key,
627 int all_tg_pt,
628 int aptpl)
629{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630 struct t10_pr_registration *pr_reg;
631
632 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
Andy Grover6708bb22011-06-08 10:36:43 -0700633 if (!pr_reg) {
634 pr_err("Unable to allocate struct t10_pr_registration\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800635 return NULL;
636 }
637
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
639 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
640 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
641 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
642 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
643 atomic_set(&pr_reg->pr_res_holders, 0);
644 pr_reg->pr_reg_nacl = nacl;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700645 /*
646 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
647 * the se_dev_entry->pr_ref will have been already obtained by
648 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
649 *
650 * Otherwise, locate se_dev_entry now and obtain a reference until
651 * registration completes in __core_scsi3_add_registration().
652 */
653 if (dest_deve) {
654 pr_reg->pr_reg_deve = dest_deve;
655 } else {
656 rcu_read_lock();
657 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
658 if (!pr_reg->pr_reg_deve) {
659 rcu_read_unlock();
660 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
661 nacl->initiatorname, mapped_lun);
662 kmem_cache_free(t10_pr_reg_cache, pr_reg);
663 return NULL;
664 }
665 kref_get(&pr_reg->pr_reg_deve->pr_kref);
666 rcu_read_unlock();
667 }
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000668 pr_reg->pr_res_mapped_lun = mapped_lun;
669 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700670 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800671 pr_reg->pr_res_key = sa_res_key;
672 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
673 pr_reg->pr_reg_aptpl = aptpl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800674 /*
675 * If an ISID value for this SCSI Initiator Port exists,
676 * save it to the registration now.
677 */
678 if (isid != NULL) {
679 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
680 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
681 pr_reg->isid_present_at_reg = 1;
682 }
683
684 return pr_reg;
685}
686
687static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
688static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
689
690/*
691 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
692 * modes.
693 */
694static struct t10_pr_registration *__core_scsi3_alloc_registration(
695 struct se_device *dev,
696 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000697 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800698 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200699 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800700 unsigned char *isid,
701 u64 sa_res_key,
702 int all_tg_pt,
703 int aptpl)
704{
705 struct se_dev_entry *deve_tmp;
706 struct se_node_acl *nacl_tmp;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000707 struct se_lun_acl *lacl_tmp;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700708 struct se_lun *lun_tmp, *next, *dest_lun;
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200709 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
711 int ret;
712 /*
713 * Create a registration for the I_T Nexus upon which the
714 * PROUT REGISTER was received.
715 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000716 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
717 isid, sa_res_key, all_tg_pt,
718 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700719 if (!pr_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800720 return NULL;
721 /*
722 * Return pointer to pr_reg for ALL_TG_PT=0
723 */
Andy Grover6708bb22011-06-08 10:36:43 -0700724 if (!all_tg_pt)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800725 return pr_reg;
726 /*
727 * Create list of matching SCSI Initiator Port registrations
728 * for ALL_TG_PT=1
729 */
730 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700731 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700732 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
733 continue;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800734 spin_unlock(&dev->se_port_lock);
735
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700736 spin_lock(&lun_tmp->lun_deve_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700737 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800738 /*
739 * This pointer will be NULL for demo mode MappedLUNs
Hannes Reinecke125d0112013-11-19 09:07:46 +0100740 * that have not been make explicit via a ConfigFS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800741 * MappedLUN group for the SCSI Initiator Node ACL.
742 */
Andy Grover6708bb22011-06-08 10:36:43 -0700743 if (!deve_tmp->se_lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800744 continue;
745
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000746 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700747 lockdep_is_held(&lun_tmp->lun_deve_lock));
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000748 nacl_tmp = lacl_tmp->se_lun_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800749 /*
750 * Skip the matching struct se_node_acl that is allocated
751 * above..
752 */
753 if (nacl == nacl_tmp)
754 continue;
755 /*
756 * Only perform PR registrations for target ports on
757 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
758 * arrived.
759 */
760 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
761 continue;
762 /*
763 * Look for a matching Initiator Node ACL in ASCII format
764 */
765 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
766 continue;
767
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700768 kref_get(&deve_tmp->pr_kref);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700769 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800770 /*
771 * Grab a configfs group dependency that is released
772 * for the exception path at label out: below, or upon
773 * completion of adding ALL_TG_PT=1 registrations in
774 * __core_scsi3_add_registration()
775 */
776 ret = core_scsi3_lunacl_depend_item(deve_tmp);
777 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700778 pr_err("core_scsi3_lunacl_depend"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800779 "_item() failed\n");
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700780 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700781 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800782 goto out;
783 }
784 /*
785 * Located a matching SCSI Initiator Port on a different
786 * port, allocate the pr_reg_atp and attach it to the
787 * pr_reg->pr_reg_atp_list that will be processed once
788 * the original *pr_reg is processed in
789 * __core_scsi3_add_registration()
790 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700791 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000792 atomic_read(&deve_tmp->pr_kref.refcount) != 0);
793
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800794 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700795 nacl_tmp, dest_lun, deve_tmp,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +0000796 deve_tmp->mapped_lun, NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800797 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -0700798 if (!pr_reg_atp) {
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700799 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800800 core_scsi3_lunacl_undepend_item(deve_tmp);
801 goto out;
802 }
803
804 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
805 &pr_reg->pr_reg_atp_list);
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700806 spin_lock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807 }
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700808 spin_unlock(&lun_tmp->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800809
810 spin_lock(&dev->se_port_lock);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700811 percpu_ref_put(&lun_tmp->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800812 }
813 spin_unlock(&dev->se_port_lock);
814
815 return pr_reg;
816out:
817 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
818 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
819 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
820 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
821 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
822 }
823 kmem_cache_free(t10_pr_reg_cache, pr_reg);
824 return NULL;
825}
826
827int core_scsi3_alloc_aptpl_registration(
Andy Grovere3d6f902011-07-19 08:55:10 +0000828 struct t10_reservation *pr_tmpl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800829 u64 sa_res_key,
830 unsigned char *i_port,
831 unsigned char *isid,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200832 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800833 unsigned char *t_port,
834 u16 tpgt,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200835 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836 int res_holder,
837 int all_tg_pt,
838 u8 type)
839{
840 struct t10_pr_registration *pr_reg;
841
Andy Grover6708bb22011-06-08 10:36:43 -0700842 if (!i_port || !t_port || !sa_res_key) {
843 pr_err("Illegal parameters for APTPL registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000844 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800845 }
846
847 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700848 if (!pr_reg) {
849 pr_err("Unable to allocate struct t10_pr_registration\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000850 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800851 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800852
853 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
854 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
855 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
856 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
857 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
858 atomic_set(&pr_reg->pr_res_holders, 0);
859 pr_reg->pr_reg_nacl = NULL;
860 pr_reg->pr_reg_deve = NULL;
861 pr_reg->pr_res_mapped_lun = mapped_lun;
862 pr_reg->pr_aptpl_target_lun = target_lun;
863 pr_reg->pr_res_key = sa_res_key;
864 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
865 pr_reg->pr_reg_aptpl = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800866 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
867 pr_reg->pr_res_type = type;
868 /*
869 * If an ISID value had been saved in APTPL metadata for this
870 * SCSI Initiator Port, restore it now.
871 */
872 if (isid != NULL) {
873 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
874 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
875 pr_reg->isid_present_at_reg = 1;
876 }
877 /*
878 * Copy the i_port and t_port information from caller.
879 */
880 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
881 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
882 pr_reg->pr_reg_tpgt = tpgt;
883 /*
884 * Set pr_res_holder from caller, the pr_reg who is the reservation
885 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
886 * the Initiator Node LUN ACL from the fabric module is created for
887 * this registration.
888 */
889 pr_reg->pr_res_holder = res_holder;
890
891 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700892 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800893 " metadata\n", (res_holder) ? "+reservation" : "");
894 return 0;
895}
896
897static void core_scsi3_aptpl_reserve(
898 struct se_device *dev,
899 struct se_portal_group *tpg,
900 struct se_node_acl *node_acl,
901 struct t10_pr_registration *pr_reg)
902{
903 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800904
905 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -0700906 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800907
908 spin_lock(&dev->dev_reservation_lock);
909 dev->dev_pr_res_holder = pr_reg;
910 spin_unlock(&dev->dev_reservation_lock);
911
Andy Grover6708bb22011-06-08 10:36:43 -0700912 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000914 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800915 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
916 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -0700917 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000918 tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -0700919 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800920}
921
922static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
Andy Grover33ce6a82013-05-16 10:40:54 -0700923 struct t10_pr_registration *, enum register_type, int);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924
925static int __core_scsi3_check_aptpl_registration(
926 struct se_device *dev,
927 struct se_portal_group *tpg,
928 struct se_lun *lun,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200929 u64 target_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800930 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +0200931 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800932{
933 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400934 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800935 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
936 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
937 u16 tpgt;
938
939 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
940 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
941 /*
942 * Copy Initiator Port information from struct se_node_acl
943 */
944 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
945 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +0000946 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
947 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800948 /*
949 * Look for the matching registrations+reservation from those
950 * created from APTPL metadata. Note that multiple registrations
951 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
952 * TransportIDs.
953 */
954 spin_lock(&pr_tmpl->aptpl_reg_lock);
955 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
956 pr_reg_aptpl_list) {
Nicholas Bellinger92404e62014-10-04 01:06:08 +0000957
Andy Grover6708bb22011-06-08 10:36:43 -0700958 if (!strcmp(pr_reg->pr_iport, i_port) &&
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700959 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800960 !(strcmp(pr_reg->pr_tport, t_port)) &&
961 (pr_reg->pr_reg_tpgt == tpgt) &&
962 (pr_reg->pr_aptpl_target_lun == target_lun)) {
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -0700963 /*
964 * Obtain the ->pr_reg_deve pointer + reference, that
965 * is released by __core_scsi3_add_registration() below.
966 */
967 rcu_read_lock();
968 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
969 if (!pr_reg->pr_reg_deve) {
970 pr_err("Unable to locate PR APTPL %s mapped_lun:"
971 " %llu\n", nacl->initiatorname, mapped_lun);
972 rcu_read_unlock();
973 continue;
974 }
975 kref_get(&pr_reg->pr_reg_deve->pr_kref);
976 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800977
978 pr_reg->pr_reg_nacl = nacl;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700979 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800980 list_del(&pr_reg->pr_reg_aptpl_list);
981 spin_unlock(&pr_tmpl->aptpl_reg_lock);
982 /*
983 * At this point all of the pointers in *pr_reg will
984 * be setup, so go ahead and add the registration.
985 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800986 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
987 /*
988 * If this registration is the reservation holder,
989 * make that happen now..
990 */
991 if (pr_reg->pr_res_holder)
992 core_scsi3_aptpl_reserve(dev, tpg,
993 nacl, pr_reg);
994 /*
995 * Reenable pr_aptpl_active to accept new metadata
996 * updates once the SCSI device is active again..
997 */
998 spin_lock(&pr_tmpl->aptpl_reg_lock);
999 pr_tmpl->pr_aptpl_active = 1;
1000 }
1001 }
1002 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1003
1004 return 0;
1005}
1006
1007int core_scsi3_check_aptpl_registration(
1008 struct se_device *dev,
1009 struct se_portal_group *tpg,
1010 struct se_lun *lun,
Nicholas Bellingere2480562014-10-04 04:23:15 +00001011 struct se_node_acl *nacl,
Hannes Reineckef2d30682015-06-10 08:41:22 +02001012 u64 mapped_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001013{
Christoph Hellwigd977f432012-10-10 17:37:15 -04001014 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001015 return 0;
1016
1017 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001018 lun->unpacked_lun, nacl,
1019 mapped_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020}
1021
1022static void __core_scsi3_dump_registration(
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001023 const struct target_core_fabric_ops *tfo,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024 struct se_device *dev,
1025 struct se_node_acl *nacl,
1026 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001027 enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001028{
1029 struct se_portal_group *se_tpg = nacl->se_tpg;
1030 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001031
1032 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001033 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001034
Andy Grover6708bb22011-06-08 10:36:43 -07001035 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
Andy Grover33ce6a82013-05-16 10:40:54 -07001036 " Node: %s%s\n", tfo->get_fabric_name(), (register_type == REGISTER_AND_MOVE) ?
1037 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001038 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001039 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001040 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
1042 tfo->tpg_get_tag(se_tpg));
Andy Grover6708bb22011-06-08 10:36:43 -07001043 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001044 " Port(s)\n", tfo->get_fabric_name(),
1045 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001046 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001047 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048 " 0x%08x APTPL: %d\n", tfo->get_fabric_name(),
1049 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1050 pr_reg->pr_reg_aptpl);
1051}
1052
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001053static void __core_scsi3_add_registration(
1054 struct se_device *dev,
1055 struct se_node_acl *nacl,
1056 struct t10_pr_registration *pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07001057 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001058 int register_move)
1059{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001060 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001061 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001062 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001063 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001064
1065 /*
1066 * Increment PRgeneration counter for struct se_device upon a successful
1067 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1068 *
1069 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1070 * action, the struct se_device->dev_reservation_lock will already be held,
1071 * so we do not call core_scsi3_pr_generation() which grabs the lock
1072 * for the REGISTER.
1073 */
1074 pr_reg->pr_res_generation = (register_move) ?
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001075 dev->t10_pr.pr_generation++ :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001076 core_scsi3_pr_generation(dev);
1077
1078 spin_lock(&pr_tmpl->registration_lock);
1079 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001080
1081 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1082 spin_unlock(&pr_tmpl->registration_lock);
1083 /*
1084 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1085 */
Andy Grover6708bb22011-06-08 10:36:43 -07001086 if (!pr_reg->pr_reg_all_tg_pt || register_move)
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001087 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001088 /*
1089 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1090 * allocated in __core_scsi3_alloc_registration()
1091 */
1092 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1093 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001094 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1095
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001096 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1097
1098 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1099
1100 spin_lock(&pr_tmpl->registration_lock);
1101 list_add_tail(&pr_reg_tmp->pr_reg_list,
1102 &pr_tmpl->registration_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001103
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001104 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1105 register_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001106 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001107 /*
1108 * Drop configfs group dependency reference and deve->pr_kref
1109 * obtained from __core_scsi3_alloc_registration() code.
1110 */
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001111 rcu_read_lock();
1112 deve = pr_reg_tmp->pr_reg_deve;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001113 if (deve) {
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001114 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001115 core_scsi3_lunacl_undepend_item(deve);
1116 pr_reg_tmp->pr_reg_deve = NULL;
1117 }
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001118 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001119 }
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001120out:
1121 /*
1122 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1123 */
1124 rcu_read_lock();
1125 deve = pr_reg->pr_reg_deve;
1126 if (deve) {
1127 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1128 kref_put(&deve->pr_kref, target_pr_kref_release);
1129 pr_reg->pr_reg_deve = NULL;
1130 }
1131 rcu_read_unlock();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132}
1133
1134static int core_scsi3_alloc_registration(
1135 struct se_device *dev,
1136 struct se_node_acl *nacl,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001137 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138 struct se_dev_entry *deve,
Hannes Reineckef2d30682015-06-10 08:41:22 +02001139 u64 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140 unsigned char *isid,
1141 u64 sa_res_key,
1142 int all_tg_pt,
1143 int aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07001144 enum register_type register_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 int register_move)
1146{
1147 struct t10_pr_registration *pr_reg;
1148
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001149 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1150 isid, sa_res_key, all_tg_pt,
1151 aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001152 if (!pr_reg)
Andy Grovere3d6f902011-07-19 08:55:10 +00001153 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154
1155 __core_scsi3_add_registration(dev, nacl, pr_reg,
1156 register_type, register_move);
1157 return 0;
1158}
1159
1160static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1161 struct se_device *dev,
1162 struct se_node_acl *nacl,
1163 unsigned char *isid)
1164{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001165 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1167 struct se_portal_group *tpg;
1168
1169 spin_lock(&pr_tmpl->registration_lock);
1170 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1171 &pr_tmpl->registration_list, pr_reg_list) {
1172 /*
1173 * First look for a matching struct se_node_acl
1174 */
1175 if (pr_reg->pr_reg_nacl != nacl)
1176 continue;
1177
1178 tpg = pr_reg->pr_reg_nacl->se_tpg;
1179 /*
1180 * If this registration does NOT contain a fabric provided
1181 * ISID, then we have found a match.
1182 */
Andy Grover6708bb22011-06-08 10:36:43 -07001183 if (!pr_reg->isid_present_at_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001184 /*
1185 * Determine if this SCSI device server requires that
1186 * SCSI Intiatior TransportID w/ ISIDs is enforced
1187 * for fabric modules (iSCSI) requiring them.
1188 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001189 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001190 if (dev->dev_attrib.enforce_pr_isids)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 continue;
1192 }
Joern Engel33940d02014-09-16 16:23:12 -04001193 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001194 spin_unlock(&pr_tmpl->registration_lock);
1195 return pr_reg;
1196 }
1197 /*
1198 * If the *pr_reg contains a fabric defined ISID for multi-value
1199 * SCSI Initiator Port TransportIDs, then we expect a valid
1200 * matching ISID to be provided by the local SCSI Initiator Port.
1201 */
Andy Grover6708bb22011-06-08 10:36:43 -07001202 if (!isid)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203 continue;
1204 if (strcmp(isid, pr_reg->pr_reg_isid))
1205 continue;
1206
Joern Engel33940d02014-09-16 16:23:12 -04001207 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001208 spin_unlock(&pr_tmpl->registration_lock);
1209 return pr_reg;
1210 }
1211 spin_unlock(&pr_tmpl->registration_lock);
1212
1213 return NULL;
1214}
1215
1216static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1217 struct se_device *dev,
1218 struct se_node_acl *nacl,
1219 struct se_session *sess)
1220{
1221 struct se_portal_group *tpg = nacl->se_tpg;
1222 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1223
Andy Grovere3d6f902011-07-19 08:55:10 +00001224 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001225 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001226 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 PR_REG_ISID_LEN);
1228 isid_ptr = &buf[0];
1229 }
1230
1231 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1232}
1233
1234static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1235{
Joern Engel33940d02014-09-16 16:23:12 -04001236 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001237}
1238
Hannes Reinecke125d0112013-11-19 09:07:46 +01001239static int core_scsi3_check_implicit_release(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001240 struct se_device *dev,
1241 struct t10_pr_registration *pr_reg)
1242{
1243 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1244 struct t10_pr_registration *pr_res_holder;
1245 int ret = 0;
1246
1247 spin_lock(&dev->dev_reservation_lock);
1248 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07001249 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250 spin_unlock(&dev->dev_reservation_lock);
1251 return ret;
1252 }
1253 if (pr_res_holder == pr_reg) {
1254 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001255 * Perform an implicit RELEASE if the registration that
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256 * is being released is holding the reservation.
1257 *
1258 * From spc4r17, section 5.7.11.1:
1259 *
1260 * e) If the I_T nexus is the persistent reservation holder
1261 * and the persistent reservation is not an all registrants
1262 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1263 * service action or REGISTER AND IGNORE EXISTING KEY
1264 * service action with the SERVICE ACTION RESERVATION KEY
1265 * field set to zero (see 5.7.11.3).
1266 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001267 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268 ret = 1;
1269 /*
1270 * For 'All Registrants' reservation types, all existing
1271 * registrations are still processed as reservation holders
1272 * in core_scsi3_pr_seq_non_holder() after the initial
Hannes Reinecke125d0112013-11-19 09:07:46 +01001273 * reservation holder is implicitly released here.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 */
1275 } else if (pr_reg->pr_reg_all_tg_pt &&
1276 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1277 pr_reg->pr_reg_nacl->initiatorname)) &&
1278 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001279 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280 " UNREGISTER while existing reservation with matching"
1281 " key 0x%016Lx is present from another SCSI Initiator"
1282 " Port\n", pr_reg->pr_res_key);
Andy Grovere3d6f902011-07-19 08:55:10 +00001283 ret = -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001284 }
1285 spin_unlock(&dev->dev_reservation_lock);
1286
1287 return ret;
1288}
1289
1290/*
Andy Grovere3d6f902011-07-19 08:55:10 +00001291 * Called with struct t10_reservation->registration_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 */
1293static void __core_scsi3_free_registration(
1294 struct se_device *dev,
1295 struct t10_pr_registration *pr_reg,
1296 struct list_head *preempt_and_abort_list,
1297 int dec_holders)
Bart Van Asschecb0df4d2015-04-10 14:49:02 +02001298 __releases(&pr_tmpl->registration_lock)
1299 __acquires(&pr_tmpl->registration_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001301 const struct target_core_fabric_ops *tfo =
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001303 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001304 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1305 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001306 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307
1308 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001309 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001311 if (!list_empty(&pr_reg->pr_reg_list))
1312 list_del(&pr_reg->pr_reg_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313 /*
1314 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1315 * so call core_scsi3_put_pr_reg() to decrement our reference.
1316 */
1317 if (dec_holders)
1318 core_scsi3_put_pr_reg(pr_reg);
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001319
1320 spin_unlock(&pr_tmpl->registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321 /*
1322 * Wait until all reference from any other I_T nexuses for this
1323 * *pr_reg have been released. Because list_del() is called above,
1324 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1325 * count back to zero, and we release *pr_reg.
1326 */
1327 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001328 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001329 tfo->get_fabric_name());
1330 cpu_relax();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001331 }
1332
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07001333 rcu_read_lock();
1334 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1335 if (deve)
1336 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1337 rcu_read_unlock();
1338
1339 spin_lock(&pr_tmpl->registration_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001340 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341 " Node: %s%s\n", tfo->get_fabric_name(),
1342 pr_reg->pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07001343 i_buf);
Andy Grover6708bb22011-06-08 10:36:43 -07001344 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001345 " Port(s)\n", tfo->get_fabric_name(),
1346 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
Andy Grovere3d6f902011-07-19 08:55:10 +00001347 dev->transport->name);
Andy Grover6708bb22011-06-08 10:36:43 -07001348 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1350 pr_reg->pr_res_generation);
1351
Andy Grover6708bb22011-06-08 10:36:43 -07001352 if (!preempt_and_abort_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 pr_reg->pr_reg_deve = NULL;
1354 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1356 return;
1357 }
1358 /*
1359 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1360 * are released once the ABORT_TASK_SET has completed..
1361 */
1362 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1363}
1364
1365void core_scsi3_free_pr_reg_from_nacl(
1366 struct se_device *dev,
1367 struct se_node_acl *nacl)
1368{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001369 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001371 bool free_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001372 /*
1373 * If the passed se_node_acl matches the reservation holder,
1374 * release the reservation.
1375 */
1376 spin_lock(&dev->dev_reservation_lock);
1377 pr_res_holder = dev->dev_pr_res_holder;
1378 if ((pr_res_holder != NULL) &&
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001379 (pr_res_holder->pr_reg_nacl == nacl)) {
1380 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1381 free_reg = true;
1382 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001383 spin_unlock(&dev->dev_reservation_lock);
1384 /*
1385 * Release any registration associated with the struct se_node_acl.
1386 */
1387 spin_lock(&pr_tmpl->registration_lock);
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001388 if (pr_res_holder && free_reg)
1389 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1390
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001391 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1392 &pr_tmpl->registration_list, pr_reg_list) {
1393
1394 if (pr_reg->pr_reg_nacl != nacl)
1395 continue;
1396
1397 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1398 }
1399 spin_unlock(&pr_tmpl->registration_lock);
1400}
1401
1402void core_scsi3_free_all_registrations(
1403 struct se_device *dev)
1404{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001405 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001406 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1407
1408 spin_lock(&dev->dev_reservation_lock);
1409 pr_res_holder = dev->dev_pr_res_holder;
1410 if (pr_res_holder != NULL) {
1411 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1412 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08001413 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001414 }
1415 spin_unlock(&dev->dev_reservation_lock);
1416
1417 spin_lock(&pr_tmpl->registration_lock);
1418 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1419 &pr_tmpl->registration_list, pr_reg_list) {
1420
1421 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1422 }
1423 spin_unlock(&pr_tmpl->registration_lock);
1424
1425 spin_lock(&pr_tmpl->aptpl_reg_lock);
1426 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1427 pr_reg_aptpl_list) {
1428 list_del(&pr_reg->pr_reg_aptpl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1430 }
1431 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1432}
1433
1434static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1435{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001436 return target_depend_item(&tpg->tpg_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001437}
1438
1439static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1440{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001441 target_undepend_item(&tpg->tpg_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001442 atomic_dec_mb(&tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443}
1444
1445static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1446{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001447 if (nacl->dynamic_node_acl)
1448 return 0;
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001449 return target_depend_item(&nacl->acl_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001450}
1451
1452static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1453{
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001454 if (!nacl->dynamic_node_acl)
1455 target_undepend_item(&nacl->acl_group.cg_item);
Joern Engel33940d02014-09-16 16:23:12 -04001456 atomic_dec_mb(&nacl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001457}
1458
1459static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1460{
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001461 struct se_lun_acl *lun_acl;
Bart Van Asscheac75d8b2015-10-22 15:52:01 -07001462
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001463 /*
1464 * For nacl->dynamic_node_acl=1
1465 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001466 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1467 atomic_read(&se_deve->pr_kref.refcount) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001468 if (!lun_acl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469 return 0;
1470
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001471 return target_depend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472}
1473
1474static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1475{
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001476 struct se_lun_acl *lun_acl;
Bart Van Asscheac75d8b2015-10-22 15:52:01 -07001477
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001478 /*
1479 * For nacl->dynamic_node_acl=1
1480 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001481 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
1482 atomic_read(&se_deve->pr_kref.refcount) != 0);
Andy Grover6708bb22011-06-08 10:36:43 -07001483 if (!lun_acl) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001484 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 return;
1486 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487
Christoph Hellwigd588cf82015-05-03 08:50:52 +02001488 target_undepend_item(&lun_acl->se_lun_group.cg_item);
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001489 kref_put(&se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001490}
1491
Christoph Hellwigde103c92012-11-06 12:24:09 -08001492static sense_reason_t
1493core_scsi3_decode_spec_i_port(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494 struct se_cmd *cmd,
1495 struct se_portal_group *tpg,
1496 unsigned char *l_isid,
1497 u64 sa_res_key,
1498 int all_tg_pt,
1499 int aptpl)
1500{
Andy Grover5951146d2011-07-19 10:26:37 +00001501 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00001503 struct se_session *se_sess = cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504 struct se_node_acl *dest_node_acl = NULL;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001505 struct se_dev_entry *dest_se_deve = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001506 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1507 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
Roland Dreierd0f474e2012-01-12 10:41:18 -08001508 LIST_HEAD(tid_dest_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001510 unsigned char *buf, *ptr, proto_ident;
Sagi Grimberg9b353cc2015-06-29 13:07:06 +03001511 const unsigned char *i_str = NULL;
Julia Lawall13ba5642014-11-30 19:14:12 +01001512 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08001513 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001514 u32 tpdl, tid_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001515 u32 dest_rtpi = 0;
1516
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001517 /*
1518 * Allocate a struct pr_transport_id_holder and setup the
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001519 * local_node_acl pointer and add to struct list_head tid_dest_list
1520 * for add registration processing in the loop of tid_dest_list below.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001521 */
1522 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001523 if (!tidh_new) {
1524 pr_err("Unable to allocate tidh_new\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001525 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001526 }
1527 INIT_LIST_HEAD(&tidh_new->dest_list);
1528 tidh_new->dest_tpg = tpg;
1529 tidh_new->dest_node_acl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530
Andy Grover5951146d2011-07-19 10:26:37 +00001531 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001532 se_sess->se_node_acl, cmd->se_lun,
1533 NULL, cmd->orig_fe_lun, l_isid,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001534 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001535 if (!local_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001536 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001537 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 }
1539 tidh_new->dest_pr_reg = local_pr_reg;
1540 /*
1541 * The local I_T nexus does not hold any configfs dependances,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001542 * so we set tidh_new->dest_se_deve to NULL to prevent the
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001543 * configfs_undepend_item() calls in the tid_dest_list loops below.
1544 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001545 tidh_new->dest_se_deve = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001546 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001547
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001548 if (cmd->data_length < 28) {
1549 pr_warn("SPC-PR: Received PR OUT parameter list"
1550 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001551 ret = TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +02001552 goto out;
1553 }
1554
Andy Grover49493142012-01-16 16:57:08 -08001555 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001556 if (!buf) {
1557 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1558 goto out;
1559 }
1560
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561 /*
1562 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1563 * first extract TransportID Parameter Data Length, and make sure
1564 * the value matches up to the SCSI expected data transfer length.
1565 */
1566 tpdl = (buf[24] & 0xff) << 24;
1567 tpdl |= (buf[25] & 0xff) << 16;
1568 tpdl |= (buf[26] & 0xff) << 8;
1569 tpdl |= buf[27] & 0xff;
1570
1571 if ((tpdl + 28) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07001572 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573 " does not equal CDB data_length: %u\n", tpdl,
1574 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001575 ret = TCM_INVALID_PARAMETER_LIST;
1576 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001577 }
1578 /*
1579 * Start processing the received transport IDs using the
1580 * receiving I_T Nexus portal's fabric dependent methods to
1581 * obtain the SCSI Initiator Port/Device Identifiers.
1582 */
1583 ptr = &buf[28];
1584
1585 while (tpdl > 0) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001586 struct se_lun *dest_lun, *tmp_lun;
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001587
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588 proto_ident = (ptr[0] & 0x0f);
1589 dest_tpg = NULL;
1590
1591 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001592 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1593 tmp_tpg = tmp_lun->lun_tpg;
Christoph Hellwig2650d712015-05-01 17:47:58 +02001594
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001595 /*
1596 * Look for the matching proto_ident provided by
1597 * the received TransportID
1598 */
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02001599 if (tmp_tpg->proto_id != proto_ident)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001601 dest_rtpi = tmp_lun->lun_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001602
Christoph Hellwig2650d712015-05-01 17:47:58 +02001603 i_str = target_parse_pr_out_transport_id(tmp_tpg,
1604 (const char *)ptr, &tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07001605 if (!i_str)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001606 continue;
1607
Joern Engel33940d02014-09-16 16:23:12 -04001608 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001609 spin_unlock(&dev->se_port_lock);
1610
Christoph Hellwigde103c92012-11-06 12:24:09 -08001611 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001612 pr_err(" core_scsi3_tpg_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001613 " for tmp_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04001614 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001615 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1616 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001617 }
1618 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +09001619 * Locate the destination initiator ACL to be registered
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001620 * from the decoded fabric module specific TransportID
1621 * at *i_str.
1622 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001623 mutex_lock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001624 dest_node_acl = __core_tpg_get_initiator_node_acl(
1625 tmp_tpg, i_str);
Joern Engel33940d02014-09-16 16:23:12 -04001626 if (dest_node_acl)
1627 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00001628 mutex_unlock(&tmp_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629
Andy Grover6708bb22011-06-08 10:36:43 -07001630 if (!dest_node_acl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001631 core_scsi3_tpg_undepend_item(tmp_tpg);
1632 spin_lock(&dev->se_port_lock);
1633 continue;
1634 }
1635
Christoph Hellwigde103c92012-11-06 12:24:09 -08001636 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001637 pr_err("configfs_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638 " for dest_node_acl->acl_group\n");
Joern Engel33940d02014-09-16 16:23:12 -04001639 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001640 core_scsi3_tpg_undepend_item(tmp_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001641 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1642 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001643 }
1644
1645 dest_tpg = tmp_tpg;
Andy Grover6708bb22011-06-08 10:36:43 -07001646 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001647 " %s Port RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001648 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001649 dest_node_acl->initiatorname, dest_rtpi);
1650
1651 spin_lock(&dev->se_port_lock);
1652 break;
1653 }
1654 spin_unlock(&dev->se_port_lock);
1655
Andy Grover6708bb22011-06-08 10:36:43 -07001656 if (!dest_tpg) {
1657 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001658 " dest_tpg\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08001659 ret = TCM_INVALID_PARAMETER_LIST;
1660 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001661 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001662
Andy Grover6708bb22011-06-08 10:36:43 -07001663 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001664 " tid_len: %d for %s + %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001665 dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001666 tpdl, tid_len, i_str, iport_ptr);
Andy Grover8b1e1242012-04-03 15:51:12 -07001667
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001668 if (tid_len > tpdl) {
Andy Grover6708bb22011-06-08 10:36:43 -07001669 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670 " %u for Transport ID: %s\n", tid_len, ptr);
1671 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1672 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001673 ret = TCM_INVALID_PARAMETER_LIST;
1674 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001675 }
1676 /*
1677 * Locate the desintation struct se_dev_entry pointer for matching
1678 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1679 * Target Port.
1680 */
1681 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1682 dest_rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07001683 if (!dest_se_deve) {
1684 pr_err("Unable to locate %s dest_se_deve"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001685 " from destination RTPI: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001686 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687 dest_rtpi);
1688
1689 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1690 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001691 ret = TCM_INVALID_PARAMETER_LIST;
1692 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001693 }
1694
Christoph Hellwigde103c92012-11-06 12:24:09 -08001695 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001696 pr_err("core_scsi3_lunacl_depend_item()"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001697 " failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07001698 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001699 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1700 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001701 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1702 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001703 }
Andy Grover8b1e1242012-04-03 15:51:12 -07001704
Andy Grover6708bb22011-06-08 10:36:43 -07001705 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001706 " dest_se_deve mapped_lun: %llu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001707 dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001708 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07001709
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001710 /*
1711 * Skip any TransportIDs that already have a registration for
1712 * this target port.
1713 */
1714 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1715 iport_ptr);
1716 if (pr_reg_e) {
1717 core_scsi3_put_pr_reg(pr_reg_e);
1718 core_scsi3_lunacl_undepend_item(dest_se_deve);
1719 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1720 core_scsi3_tpg_undepend_item(dest_tpg);
1721 ptr += tid_len;
1722 tpdl -= tid_len;
1723 tid_len = 0;
1724 continue;
1725 }
1726 /*
1727 * Allocate a struct pr_transport_id_holder and setup
1728 * the dest_node_acl and dest_se_deve pointers for the
1729 * loop below.
1730 */
1731 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1732 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001733 if (!tidh_new) {
1734 pr_err("Unable to allocate tidh_new\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001735 core_scsi3_lunacl_undepend_item(dest_se_deve);
1736 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1737 core_scsi3_tpg_undepend_item(dest_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001738 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1739 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001740 }
1741 INIT_LIST_HEAD(&tidh_new->dest_list);
1742 tidh_new->dest_tpg = dest_tpg;
1743 tidh_new->dest_node_acl = dest_node_acl;
1744 tidh_new->dest_se_deve = dest_se_deve;
1745
1746 /*
1747 * Allocate, but do NOT add the registration for the
1748 * TransportID referenced SCSI Initiator port. This
1749 * done because of the following from spc4r17 in section
1750 * 6.14.3 wrt SPEC_I_PT:
1751 *
1752 * "If a registration fails for any initiator port (e.g., if th
1753 * logical unit does not have enough resources available to
1754 * hold the registration information), no registrations shall be
1755 * made, and the command shall be terminated with
1756 * CHECK CONDITION status."
1757 *
1758 * That means we call __core_scsi3_alloc_registration() here,
1759 * and then call __core_scsi3_add_registration() in the
1760 * 2nd loop which will never fail.
1761 */
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001762 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
1763 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
1764
Andy Grover5951146d2011-07-19 10:26:37 +00001765 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001766 dest_node_acl, dest_lun, dest_se_deve,
1767 dest_se_deve->mapped_lun, iport_ptr,
1768 sa_res_key, all_tg_pt, aptpl);
Andy Grover6708bb22011-06-08 10:36:43 -07001769 if (!dest_pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001770 core_scsi3_lunacl_undepend_item(dest_se_deve);
1771 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1772 core_scsi3_tpg_undepend_item(dest_tpg);
1773 kfree(tidh_new);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001774 ret = TCM_INVALID_PARAMETER_LIST;
1775 goto out_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001776 }
1777 tidh_new->dest_pr_reg = dest_pr_reg;
1778 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1779
1780 ptr += tid_len;
1781 tpdl -= tid_len;
1782 tid_len = 0;
1783
1784 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00001785
Andy Grover49493142012-01-16 16:57:08 -08001786 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00001787
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001788 /*
1789 * Go ahead and create a registrations from tid_dest_list for the
1790 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1791 * and dest_se_deve.
1792 *
1793 * The SA Reservation Key from the PROUT is set for the
1794 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1795 * means that the TransportID Initiator port will be
1796 * registered on all of the target ports in the SCSI target device
1797 * ALL_TG_PT=0 means the registration will only be for the
1798 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1799 * was received.
1800 */
1801 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1802 dest_tpg = tidh->dest_tpg;
1803 dest_node_acl = tidh->dest_node_acl;
1804 dest_se_deve = tidh->dest_se_deve;
1805 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806
1807 list_del(&tidh->dest_list);
1808 kfree(tidh);
1809
1810 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07001811 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812
Andy Grover5951146d2011-07-19 10:26:37 +00001813 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001814 dest_pr_reg, 0, 0);
1815
Andy Grover6708bb22011-06-08 10:36:43 -07001816 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001817 " registered Transport ID for Node: %s%s Mapped LUN:"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001818 " %llu\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001819 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1820 dest_se_deve->mapped_lun : 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001821
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001822 if (!dest_se_deve) {
1823 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1824 target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001825 continue;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001826 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001827 core_scsi3_lunacl_undepend_item(dest_se_deve);
1828 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1829 core_scsi3_tpg_undepend_item(dest_tpg);
1830 }
1831
1832 return 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001833out_unmap:
Andy Grover49493142012-01-16 16:57:08 -08001834 transport_kunmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001835out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001836 /*
1837 * For the failure case, release everything from tid_dest_list
1838 * including *dest_pr_reg and the configfs dependances..
1839 */
1840 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1841 dest_tpg = tidh->dest_tpg;
1842 dest_node_acl = tidh->dest_node_acl;
1843 dest_se_deve = tidh->dest_se_deve;
1844 dest_pr_reg = tidh->dest_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001845
1846 list_del(&tidh->dest_list);
1847 kfree(tidh);
1848 /*
1849 * Release any extra ALL_TG_PT=1 registrations for
1850 * the SPEC_I_PT=1 case.
1851 */
1852 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1853 &dest_pr_reg->pr_reg_atp_list,
1854 pr_reg_atp_mem_list) {
1855 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1856 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1857 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1858 }
1859
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001860 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1861
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001862 if (!dest_se_deve) {
1863 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1864 target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865 continue;
Nicholas Bellinger3ccd6e82015-09-13 02:30:46 -07001866 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001867 core_scsi3_lunacl_undepend_item(dest_se_deve);
1868 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1869 core_scsi3_tpg_undepend_item(dest_tpg);
1870 }
1871 return ret;
1872}
1873
Andy Grover3c8a6222013-05-16 10:40:58 -07001874static int core_scsi3_update_aptpl_buf(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001875 struct se_device *dev,
1876 unsigned char *buf,
Andy Grover0607dec2013-05-16 10:40:57 -07001877 u32 pr_aptpl_buf_len)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001878{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001879 struct se_portal_group *tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001880 struct t10_pr_registration *pr_reg;
1881 unsigned char tmp[512], isid_buf[32];
1882 ssize_t len = 0;
1883 int reg_count = 0;
Andy Grover3c8a6222013-05-16 10:40:58 -07001884 int ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001885
Andy Grover3c8a6222013-05-16 10:40:58 -07001886 spin_lock(&dev->dev_reservation_lock);
1887 spin_lock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001888 /*
1889 * Walk the registration list..
1890 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001891 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001892 pr_reg_list) {
1893
1894 tmp[0] = '\0';
1895 isid_buf[0] = '\0';
1896 tpg = pr_reg->pr_reg_nacl->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001897 /*
1898 * Write out any ISID value to APTPL metadata that was included
1899 * in the original registration.
1900 */
1901 if (pr_reg->isid_present_at_reg)
1902 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1903 pr_reg->pr_reg_isid);
1904 /*
1905 * Include special metadata if the pr_reg matches the
1906 * reservation holder.
1907 */
1908 if (dev->dev_pr_res_holder == pr_reg) {
1909 snprintf(tmp, 512, "PR_REG_START: %d"
1910 "\ninitiator_fabric=%s\n"
1911 "initiator_node=%s\n%s"
1912 "sa_res_key=%llu\n"
1913 "res_holder=1\nres_type=%02x\n"
1914 "res_scope=%02x\nres_all_tg_pt=%d\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001915 "mapped_lun=%llu\n", reg_count,
Andy Grovere3d6f902011-07-19 08:55:10 +00001916 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001917 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1918 pr_reg->pr_res_key, pr_reg->pr_res_type,
1919 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1920 pr_reg->pr_res_mapped_lun);
1921 } else {
1922 snprintf(tmp, 512, "PR_REG_START: %d\n"
1923 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1924 "sa_res_key=%llu\nres_holder=0\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001925 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001926 reg_count, tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1928 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1929 pr_reg->pr_res_mapped_lun);
1930 }
1931
Dan Carpenter60d645a2011-06-15 10:03:05 -07001932 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001933 pr_err("Unable to update renaming APTPL metadata,"
1934 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001935 ret = -EMSGSIZE;
1936 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001937 }
1938 len += sprintf(buf+len, "%s", tmp);
1939
1940 /*
1941 * Include information about the associated SCSI target port.
1942 */
1943 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
Hannes Reineckef2d30682015-06-10 08:41:22 +02001944 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
Andy Grovere3d6f902011-07-19 08:55:10 +00001945 " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1946 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1947 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00001948 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1949 reg_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001950
Dan Carpenter60d645a2011-06-15 10:03:05 -07001951 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08001952 pr_err("Unable to update renaming APTPL metadata,"
1953 " reallocating larger buffer\n");
Andy Grover3c8a6222013-05-16 10:40:58 -07001954 ret = -EMSGSIZE;
1955 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 }
1957 len += sprintf(buf+len, "%s", tmp);
1958 reg_count++;
1959 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960
Andy Grover6708bb22011-06-08 10:36:43 -07001961 if (!reg_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962 len += sprintf(buf+len, "No Registrations or Reservations");
1963
Andy Grover3c8a6222013-05-16 10:40:58 -07001964out:
1965 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001966 spin_unlock(&dev->dev_reservation_lock);
1967
1968 return ret;
1969}
1970
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001971static int __core_scsi3_write_aptpl_to_file(
1972 struct se_device *dev,
Andy Grover4dee96f2013-05-16 10:41:00 -07001973 unsigned char *buf)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001974{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001975 struct t10_wwn *wwn = &dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001976 struct file *file;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001977 int flags = O_RDWR | O_CREAT | O_TRUNC;
1978 char path[512];
Andy Grover4dee96f2013-05-16 10:41:00 -07001979 u32 pr_aptpl_buf_len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001980 int ret;
1981
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001982 memset(path, 0, 512);
1983
Dan Carpenter60d645a2011-06-15 10:03:05 -07001984 if (strlen(&wwn->unit_serial[0]) >= 512) {
Andy Grover6708bb22011-06-08 10:36:43 -07001985 pr_err("WWN value for struct se_device does not fit"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001986 " into path buffer\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001987 return -EMSGSIZE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988 }
1989
Lee Duncanfdddf932016-04-14 18:18:51 -07001990 snprintf(path, 512, "%s/pr/aptpl_%s", db_root, &wwn->unit_serial[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991 file = filp_open(path, flags, 0600);
Al Viro0e9b10a2013-02-23 15:22:43 -05001992 if (IS_ERR(file)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001993 pr_err("filp_open(%s) for APTPL metadata"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001994 " failed\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05001995 return PTR_ERR(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996 }
1997
Andy Grover4dee96f2013-05-16 10:41:00 -07001998 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001999
Al Viro0e9b10a2013-02-23 15:22:43 -05002000 ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002001
Al Viro0e9b10a2013-02-23 15:22:43 -05002002 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -07002003 pr_debug("Error writing APTPL metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -05002004 fput(file);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002005
Gera Kazakovf730f912013-09-09 15:47:06 -07002006 return (ret < 0) ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007}
2008
Andy Grover459f2132013-05-16 10:41:02 -07002009/*
2010 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2011 * write out the updated metadata to struct file for this SCSI device.
2012 */
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002013static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014{
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002015 unsigned char *buf;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002016 int rc, len = PR_APTPL_BUF_LEN;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002017
Andy Grover459f2132013-05-16 10:41:02 -07002018 if (!aptpl) {
2019 char *null_buf = "No Registrations or Reservations\n";
2020
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002021 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
Andy Grover459f2132013-05-16 10:41:02 -07002022 dev->t10_pr.pr_aptpl_active = 0;
2023 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
Andy Grover459f2132013-05-16 10:41:02 -07002024
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002025 if (rc)
2026 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover459f2132013-05-16 10:41:02 -07002027
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002028 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002029 }
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002030retry:
2031 buf = vzalloc(len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002032 if (!buf)
2033 return TCM_OUT_OF_RESOURCES;
2034
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002035 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002036 if (rc < 0) {
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002037 vfree(buf);
2038 len *= 2;
2039 goto retry;
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002040 }
2041
2042 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2043 if (rc != 0) {
2044 pr_err("SPC-3 PR: Could not update APTPL\n");
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002045 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002046 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2047 }
2048 dev->t10_pr.pr_aptpl_active = 1;
Nicholas Bellingerf161d4b2015-02-11 18:34:40 -08002049 vfree(buf);
Nicholas Bellinger8a391852013-06-24 22:18:40 -07002050 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2051 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002052}
2053
Christoph Hellwigde103c92012-11-06 12:24:09 -08002054static sense_reason_t
2055core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
Andy Groverbc118fe2013-05-16 10:41:04 -07002056 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057{
Andy Grovere3d6f902011-07-19 08:55:10 +00002058 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00002059 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002060 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002061 struct se_portal_group *se_tpg;
Andy Groverbc118fe2013-05-16 10:41:04 -07002062 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002063 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
Hannes Reineckea0d50f62012-12-17 09:53:34 +01002065 sense_reason_t ret = TCM_NO_SENSE;
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002066 int pr_holder = 0, type;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002067
Andy Grover6708bb22011-06-08 10:36:43 -07002068 if (!se_sess || !se_lun) {
2069 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002070 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002071 }
2072 se_tpg = se_sess->se_tpg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002073
Andy Grovere3d6f902011-07-19 08:55:10 +00002074 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002075 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +00002076 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002077 PR_REG_ISID_LEN);
2078 isid_ptr = &isid_buf[0];
2079 }
2080 /*
2081 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2082 */
Andy Groverbc118fe2013-05-16 10:41:04 -07002083 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2084 if (!pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002085 if (res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002086 pr_warn("SPC-3 PR: Reservation Key non-zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002087 " for SA REGISTER, returning CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002088 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002089 }
2090 /*
2091 * Do nothing but return GOOD status.
2092 */
Andy Grover6708bb22011-06-08 10:36:43 -07002093 if (!sa_res_key)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002094 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002095
Andy Grover6708bb22011-06-08 10:36:43 -07002096 if (!spec_i_pt) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002097 /*
2098 * Perform the Service Action REGISTER on the Initiator
2099 * Port Endpoint that the PRO was received from on the
2100 * Logical Unit of the SCSI device server.
2101 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002102 if (core_scsi3_alloc_registration(cmd->se_dev,
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00002103 se_sess->se_node_acl, cmd->se_lun,
2104 NULL, cmd->orig_fe_lun, isid_ptr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105 sa_res_key, all_tg_pt, aptpl,
Andy Grover33ce6a82013-05-16 10:40:54 -07002106 register_type, 0)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002107 pr_err("Unable to allocate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002108 " struct t10_pr_registration\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002109 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002110 }
2111 } else {
2112 /*
2113 * Register both the Initiator port that received
2114 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2115 * TransportID from Parameter list and loop through
2116 * fabric dependent parameter list while calling
2117 * logic from of core_scsi3_alloc_registration() for
2118 * each TransportID provided SCSI Initiator Port/Device
2119 */
2120 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2121 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2122 if (ret != 0)
2123 return ret;
2124 }
Andy Grover459f2132013-05-16 10:41:02 -07002125 return core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002126 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002127
Andy Groverbc118fe2013-05-16 10:41:04 -07002128 /* ok, existing registration */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002129
Andy Groverbc118fe2013-05-16 10:41:04 -07002130 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2131 pr_err("SPC-3 PR REGISTER: Received"
2132 " res_key: 0x%016Lx does not match"
2133 " existing SA REGISTER res_key:"
2134 " 0x%016Lx\n", res_key,
2135 pr_reg->pr_res_key);
2136 ret = TCM_RESERVATION_CONFLICT;
2137 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002138 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002139
2140 if (spec_i_pt) {
Andy Grover1f070cc2013-05-16 10:40:56 -07002141 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2142 " set on a registered nexus\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002143 ret = TCM_INVALID_PARAMETER_LIST;
Andy Groverbc118fe2013-05-16 10:41:04 -07002144 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002145 }
2146
2147 /*
2148 * An existing ALL_TG_PT=1 registration being released
2149 * must also set ALL_TG_PT=1 in the incoming PROUT.
2150 */
Andy Grover1f070cc2013-05-16 10:40:56 -07002151 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2152 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
Christoph Hellwigde103c92012-11-06 12:24:09 -08002153 " registration exists, but ALL_TG_PT=1 bit not"
2154 " present in received PROUT\n");
2155 ret = TCM_INVALID_CDB_FIELD;
Andy Groverbc118fe2013-05-16 10:41:04 -07002156 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002157 }
2158
2159 /*
Andy Grover51d9c412013-05-16 10:41:03 -07002160 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
Christoph Hellwigde103c92012-11-06 12:24:09 -08002161 */
Andy Grover51d9c412013-05-16 10:41:03 -07002162 if (sa_res_key) {
2163 /*
2164 * Increment PRgeneration counter for struct se_device"
2165 * upon a successful REGISTER, see spc4r17 section 6.3.2
2166 * READ_KEYS service action.
2167 */
2168 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2169 pr_reg->pr_res_key = sa_res_key;
2170 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2171 " Key for %s to: 0x%016Lx PRgeneration:"
2172 " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2173 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2174 pr_reg->pr_reg_nacl->initiatorname,
2175 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2176
2177 } else {
2178 /*
2179 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2180 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002181 type = pr_reg->pr_res_type;
2182 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2183 pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002184 if (pr_holder < 0) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002185 ret = TCM_RESERVATION_CONFLICT;
Andy Groverbc118fe2013-05-16 10:41:04 -07002186 goto out;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002187 }
2188
2189 spin_lock(&pr_tmpl->registration_lock);
2190 /*
2191 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2192 * and matching pr_res_key.
2193 */
2194 if (pr_reg->pr_reg_all_tg_pt) {
2195 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2196 &pr_tmpl->registration_list,
2197 pr_reg_list) {
2198
2199 if (!pr_reg_p->pr_reg_all_tg_pt)
2200 continue;
2201 if (pr_reg_p->pr_res_key != res_key)
2202 continue;
2203 if (pr_reg == pr_reg_p)
2204 continue;
2205 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2206 pr_reg_p->pr_reg_nacl->initiatorname))
2207 continue;
2208
2209 __core_scsi3_free_registration(dev,
2210 pr_reg_p, NULL, 0);
2211 }
2212 }
2213
2214 /*
2215 * Release the calling I_T Nexus registration now..
2216 */
2217 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002218 pr_reg = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002219
2220 /*
2221 * From spc4r17, section 5.7.11.3 Unregistering
2222 *
2223 * If the persistent reservation is a registrants only
2224 * type, the device server shall establish a unit
2225 * attention condition for the initiator port associated
2226 * with every registered I_T nexus except for the I_T
2227 * nexus on which the PERSISTENT RESERVE OUT command was
2228 * received, with the additional sense code set to
2229 * RESERVATIONS RELEASED.
2230 */
2231 if (pr_holder &&
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002232 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2233 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08002234 list_for_each_entry(pr_reg_p,
2235 &pr_tmpl->registration_list,
2236 pr_reg_list) {
2237
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002238 target_ua_allocate_lun(
Christoph Hellwigde103c92012-11-06 12:24:09 -08002239 pr_reg_p->pr_reg_nacl,
2240 pr_reg_p->pr_res_mapped_lun,
2241 0x2A,
2242 ASCQ_2AH_RESERVATIONS_RELEASED);
2243 }
2244 }
Andy Grover51d9c412013-05-16 10:41:03 -07002245
Christoph Hellwigde103c92012-11-06 12:24:09 -08002246 spin_unlock(&pr_tmpl->registration_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002247 }
2248
Andy Grover459f2132013-05-16 10:41:02 -07002249 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002250
Andy Groverbc118fe2013-05-16 10:41:04 -07002251out:
Nicholas Bellingerfc091492014-01-30 13:08:49 -08002252 if (pr_reg)
2253 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002254 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255}
2256
2257unsigned char *core_scsi3_pr_dump_type(int type)
2258{
2259 switch (type) {
2260 case PR_TYPE_WRITE_EXCLUSIVE:
2261 return "Write Exclusive Access";
2262 case PR_TYPE_EXCLUSIVE_ACCESS:
2263 return "Exclusive Access";
2264 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2265 return "Write Exclusive Access, Registrants Only";
2266 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2267 return "Exclusive Access, Registrants Only";
2268 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2269 return "Write Exclusive Access, All Registrants";
2270 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2271 return "Exclusive Access, All Registrants";
2272 default:
2273 break;
2274 }
2275
2276 return "Unknown SPC-3 PR Type";
2277}
2278
Christoph Hellwigde103c92012-11-06 12:24:09 -08002279static sense_reason_t
2280core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281{
Christoph Hellwigde103c92012-11-06 12:24:09 -08002282 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002283 struct se_session *se_sess = cmd->se_sess;
Andy Grovere3d6f902011-07-19 08:55:10 +00002284 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002285 struct t10_pr_registration *pr_reg, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002286 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002287 char i_buf[PR_REG_ISID_ID_LEN];
Christoph Hellwigde103c92012-11-06 12:24:09 -08002288 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002289
2290 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2291
Andy Grover6708bb22011-06-08 10:36:43 -07002292 if (!se_sess || !se_lun) {
2293 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002294 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002296 /*
2297 * Locate the existing *pr_reg via struct se_node_acl pointers
2298 */
Andy Grover5951146d2011-07-19 10:26:37 +00002299 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002301 if (!pr_reg) {
2302 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002303 " PR_REGISTERED *pr_reg for RESERVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002304 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002305 }
2306 /*
2307 * From spc4r17 Section 5.7.9: Reserving:
2308 *
2309 * An application client creates a persistent reservation by issuing
2310 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2311 * a registered I_T nexus with the following parameters:
2312 * a) RESERVATION KEY set to the value of the reservation key that is
2313 * registered with the logical unit for the I_T nexus; and
2314 */
2315 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002316 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002317 " does not match existing SA REGISTER res_key:"
2318 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002319 ret = TCM_RESERVATION_CONFLICT;
2320 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002321 }
2322 /*
2323 * From spc4r17 Section 5.7.9: Reserving:
2324 *
2325 * From above:
2326 * b) TYPE field and SCOPE field set to the persistent reservation
2327 * being created.
2328 *
2329 * Only one persistent reservation is allowed at a time per logical unit
2330 * and that persistent reservation has a scope of LU_SCOPE.
2331 */
2332 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002333 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002334 ret = TCM_INVALID_PARAMETER_LIST;
2335 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002336 }
2337 /*
2338 * See if we have an existing PR reservation holder pointer at
2339 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2340 * *pr_res_holder.
2341 */
2342 spin_lock(&dev->dev_reservation_lock);
2343 pr_res_holder = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07002344 if (pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002345 /*
2346 * From spc4r17 Section 5.7.9: Reserving:
2347 *
2348 * If the device server receives a PERSISTENT RESERVE OUT
2349 * command from an I_T nexus other than a persistent reservation
2350 * holder (see 5.7.10) that attempts to create a persistent
2351 * reservation when a persistent reservation already exists for
2352 * the logical unit, then the command shall be completed with
2353 * RESERVATION CONFLICT status.
2354 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002355 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002356 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002357 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002358 " [%s]: %s while reservation already held by"
2359 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002360 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002361 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002362 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002363 pr_res_holder->pr_reg_nacl->initiatorname);
2364
2365 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002366 ret = TCM_RESERVATION_CONFLICT;
2367 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002368 }
2369 /*
2370 * From spc4r17 Section 5.7.9: Reserving:
2371 *
2372 * If a persistent reservation holder attempts to modify the
2373 * type or scope of an existing persistent reservation, the
2374 * command shall be completed with RESERVATION CONFLICT status.
2375 */
2376 if ((pr_res_holder->pr_res_type != type) ||
2377 (pr_res_holder->pr_res_scope != scope)) {
2378 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002379 pr_err("SPC-3 PR: Attempted RESERVE from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002380 " [%s]: %s trying to change TYPE and/or SCOPE,"
2381 " while reservation already held by [%s]: %s,"
2382 " returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002383 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002384 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002385 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002386 pr_res_holder->pr_reg_nacl->initiatorname);
2387
2388 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002389 ret = TCM_RESERVATION_CONFLICT;
2390 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002391 }
2392 /*
2393 * From spc4r17 Section 5.7.9: Reserving:
2394 *
2395 * If the device server receives a PERSISTENT RESERVE OUT
2396 * command with RESERVE service action where the TYPE field and
2397 * the SCOPE field contain the same values as the existing type
2398 * and scope from a persistent reservation holder, it shall not
2399 * make any change to the existing persistent reservation and
2400 * shall completethe command with GOOD status.
2401 */
2402 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002403 ret = 0;
2404 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405 }
2406 /*
2407 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2408 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2409 */
2410 pr_reg->pr_res_scope = scope;
2411 pr_reg->pr_res_type = type;
2412 pr_reg->pr_res_holder = 1;
2413 dev->dev_pr_res_holder = pr_reg;
Andy Groverd2843c12013-05-16 10:40:55 -07002414 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002415
Andy Grover6708bb22011-06-08 10:36:43 -07002416 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002417 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002418 cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002420 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002421 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002422 se_sess->se_node_acl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002423 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002424 spin_unlock(&dev->dev_reservation_lock);
2425
Andy Grover459f2132013-05-16 10:41:02 -07002426 if (pr_tmpl->pr_aptpl_active)
2427 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002428
Christoph Hellwigde103c92012-11-06 12:24:09 -08002429 ret = 0;
2430out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002431 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002432 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002433}
2434
Christoph Hellwigde103c92012-11-06 12:24:09 -08002435static sense_reason_t
2436core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2437 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002438{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002439 switch (type) {
2440 case PR_TYPE_WRITE_EXCLUSIVE:
2441 case PR_TYPE_EXCLUSIVE_ACCESS:
2442 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2443 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2444 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2445 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08002446 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002447 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002448 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449 " 0x%02x\n", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002450 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002451 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002452}
2453
2454/*
2455 * Called with struct se_device->dev_reservation_lock held.
2456 */
2457static void __core_scsi3_complete_pro_release(
2458 struct se_device *dev,
2459 struct se_node_acl *se_nacl,
2460 struct t10_pr_registration *pr_reg,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002461 int explicit,
2462 int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002463{
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002464 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002465 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002466 int pr_res_type = 0, pr_res_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002467
2468 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002469 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470 /*
2471 * Go ahead and release the current PR reservation holder.
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002472 * If an All Registrants reservation is currently active and
2473 * a unregister operation is requested, replace the current
2474 * dev_pr_res_holder with another active registration.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002475 */
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002476 if (dev->dev_pr_res_holder) {
2477 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2478 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2479 dev->dev_pr_res_holder->pr_res_type = 0;
2480 dev->dev_pr_res_holder->pr_res_scope = 0;
2481 dev->dev_pr_res_holder->pr_res_holder = 0;
2482 dev->dev_pr_res_holder = NULL;
2483 }
2484 if (!unreg)
2485 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002486
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002487 spin_lock(&dev->t10_pr.registration_lock);
2488 list_del_init(&pr_reg->pr_reg_list);
2489 /*
2490 * If the I_T nexus is a reservation holder, the persistent reservation
2491 * is of an all registrants type, and the I_T nexus is the last remaining
2492 * registered I_T nexus, then the device server shall also release the
2493 * persistent reservation.
2494 */
2495 if (!list_empty(&dev->t10_pr.registration_list) &&
2496 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2497 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2498 dev->dev_pr_res_holder =
2499 list_entry(dev->t10_pr.registration_list.next,
2500 struct t10_pr_registration, pr_reg_list);
2501 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2502 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2503 dev->dev_pr_res_holder->pr_res_holder = 1;
2504 }
2505 spin_unlock(&dev->t10_pr.registration_lock);
2506out:
2507 if (!dev->dev_pr_res_holder) {
2508 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2509 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2510 tfo->get_fabric_name(), (explicit) ? "explicit" :
2511 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2512 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2513 }
Andy Grover6708bb22011-06-08 10:36:43 -07002514 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002515 tfo->get_fabric_name(), se_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07002516 i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002517 /*
2518 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2519 */
2520 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2521}
2522
Christoph Hellwigde103c92012-11-06 12:24:09 -08002523static sense_reason_t
2524core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2525 u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002526{
2527 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00002528 struct se_session *se_sess = cmd->se_sess;
2529 struct se_lun *se_lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002530 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002531 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002532 sense_reason_t ret = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002533
Andy Grover6708bb22011-06-08 10:36:43 -07002534 if (!se_sess || !se_lun) {
2535 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002536 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002537 }
2538 /*
2539 * Locate the existing *pr_reg via struct se_node_acl pointers
2540 */
2541 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002542 if (!pr_reg) {
2543 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 " PR_REGISTERED *pr_reg for RELEASE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002545 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002546 }
2547 /*
2548 * From spc4r17 Section 5.7.11.2 Releasing:
2549 *
2550 * If there is no persistent reservation or in response to a persistent
2551 * reservation release request from a registered I_T nexus that is not a
2552 * persistent reservation holder (see 5.7.10), the device server shall
2553 * do the following:
2554 *
2555 * a) Not release the persistent reservation, if any;
2556 * b) Not remove any registrations; and
2557 * c) Complete the command with GOOD status.
2558 */
2559 spin_lock(&dev->dev_reservation_lock);
2560 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07002561 if (!pr_res_holder) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002562 /*
2563 * No persistent reservation, return GOOD status.
2564 */
2565 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellingerbb7a8c82012-11-06 15:46:25 -08002566 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002568
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02002569 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002570 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002571 * Release request from a registered I_T nexus that is not a
2572 * persistent reservation holder. return GOOD status.
2573 */
2574 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002575 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08002577
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578 /*
2579 * From spc4r17 Section 5.7.11.2 Releasing:
2580 *
2581 * Only the persistent reservation holder (see 5.7.10) is allowed to
2582 * release a persistent reservation.
2583 *
2584 * An application client releases the persistent reservation by issuing
2585 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2586 * an I_T nexus that is a persistent reservation holder with the
2587 * following parameters:
2588 *
2589 * a) RESERVATION KEY field set to the value of the reservation key
2590 * that is registered with the logical unit for the I_T nexus;
2591 */
2592 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002593 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002594 " does not match existing SA REGISTER res_key:"
2595 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2596 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002597 ret = TCM_RESERVATION_CONFLICT;
2598 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002599 }
2600 /*
2601 * From spc4r17 Section 5.7.11.2 Releasing and above:
2602 *
2603 * b) TYPE field and SCOPE field set to match the persistent
2604 * reservation being released.
2605 */
2606 if ((pr_res_holder->pr_res_type != type) ||
2607 (pr_res_holder->pr_res_scope != scope)) {
2608 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07002609 pr_err("SPC-3 PR RELEASE: Attempted to release"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 " reservation from [%s]: %s with different TYPE "
2611 "and/or SCOPE while reservation already held by"
2612 " [%s]: %s, returning RESERVATION_CONFLICT\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002613 cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002614 se_sess->se_node_acl->initiatorname,
Andy Grovere3d6f902011-07-19 08:55:10 +00002615 pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002616 pr_res_holder->pr_reg_nacl->initiatorname);
2617
2618 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002619 ret = TCM_RESERVATION_CONFLICT;
2620 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002621 }
2622 /*
2623 * In response to a persistent reservation release request from the
2624 * persistent reservation holder the device server shall perform a
2625 * release by doing the following as an uninterrupted series of actions:
2626 * a) Release the persistent reservation;
2627 * b) Not remove any registration(s);
2628 * c) If the released persistent reservation is a registrants only type
2629 * or all registrants type persistent reservation,
2630 * the device server shall establish a unit attention condition for
2631 * the initiator port associated with every regis-
2632 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2633 * RESERVE OUT command with RELEASE service action was received,
2634 * with the additional sense code set to RESERVATIONS RELEASED; and
2635 * d) If the persistent reservation is of any other type, the device
2636 * server shall not establish a unit attention condition.
2637 */
2638 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002639 pr_reg, 1, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640
2641 spin_unlock(&dev->dev_reservation_lock);
2642
2643 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2644 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2645 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2646 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2647 /*
2648 * If no UNIT ATTENTION conditions will be established for
2649 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2650 * go ahead and check for APTPL=1 update+write below
2651 */
2652 goto write_aptpl;
2653 }
2654
2655 spin_lock(&pr_tmpl->registration_lock);
2656 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2657 pr_reg_list) {
2658 /*
2659 * Do not establish a UNIT ATTENTION condition
2660 * for the calling I_T Nexus
2661 */
2662 if (pr_reg_p == pr_reg)
2663 continue;
2664
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002665 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666 pr_reg_p->pr_res_mapped_lun,
2667 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2668 }
2669 spin_unlock(&pr_tmpl->registration_lock);
2670
2671write_aptpl:
Andy Grover459f2132013-05-16 10:41:02 -07002672 if (pr_tmpl->pr_aptpl_active)
2673 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2674
Christoph Hellwigde103c92012-11-06 12:24:09 -08002675out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676 core_scsi3_put_pr_reg(pr_reg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002677 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678}
2679
Christoph Hellwigde103c92012-11-06 12:24:09 -08002680static sense_reason_t
2681core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002682{
2683 struct se_device *dev = cmd->se_dev;
2684 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002685 struct se_session *se_sess = cmd->se_sess;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002686 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002688 u64 pr_res_mapped_lun = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002689 int calling_it_nexus = 0;
2690 /*
2691 * Locate the existing *pr_reg via struct se_node_acl pointers
2692 */
Andy Grover5951146d2011-07-19 10:26:37 +00002693 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002694 se_sess->se_node_acl, se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002695 if (!pr_reg_n) {
2696 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 " PR_REGISTERED *pr_reg for CLEAR\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002698 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002699 }
2700 /*
2701 * From spc4r17 section 5.7.11.6, Clearing:
2702 *
2703 * Any application client may release the persistent reservation and
2704 * remove all registrations from a device server by issuing a
2705 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2706 * registered I_T nexus with the following parameter:
2707 *
2708 * a) RESERVATION KEY field set to the value of the reservation key
2709 * that is registered with the logical unit for the I_T nexus.
2710 */
2711 if (res_key != pr_reg_n->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07002712 pr_err("SPC-3 PR REGISTER: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002713 " res_key: 0x%016Lx does not match"
2714 " existing SA REGISTER res_key:"
2715 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2716 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002717 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718 }
2719 /*
2720 * a) Release the persistent reservation, if any;
2721 */
2722 spin_lock(&dev->dev_reservation_lock);
2723 pr_res_holder = dev->dev_pr_res_holder;
2724 if (pr_res_holder) {
2725 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2726 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002727 pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002728 }
2729 spin_unlock(&dev->dev_reservation_lock);
2730 /*
2731 * b) Remove all registration(s) (see spc4r17 5.7.7);
2732 */
2733 spin_lock(&pr_tmpl->registration_lock);
2734 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2735 &pr_tmpl->registration_list, pr_reg_list) {
2736
2737 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2738 pr_reg_nacl = pr_reg->pr_reg_nacl;
2739 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2740 __core_scsi3_free_registration(dev, pr_reg, NULL,
2741 calling_it_nexus);
2742 /*
2743 * e) Establish a unit attention condition for the initiator
2744 * port associated with every registered I_T nexus other
2745 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2746 * command with CLEAR service action was received, with the
2747 * additional sense code set to RESERVATIONS PREEMPTED.
2748 */
Andy Grover6708bb22011-06-08 10:36:43 -07002749 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002750 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002751 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2752 }
2753 spin_unlock(&pr_tmpl->registration_lock);
2754
Andy Grover6708bb22011-06-08 10:36:43 -07002755 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002756 cmd->se_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002757
Andy Grover459f2132013-05-16 10:41:02 -07002758 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002759
2760 core_scsi3_pr_generation(dev);
2761 return 0;
2762}
2763
2764/*
2765 * Called with struct se_device->dev_reservation_lock held.
2766 */
2767static void __core_scsi3_complete_pro_preempt(
2768 struct se_device *dev,
2769 struct t10_pr_registration *pr_reg,
2770 struct list_head *preempt_and_abort_list,
2771 int type,
2772 int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07002773 enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002774{
2775 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002776 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002777 char i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778
2779 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
Andy Groverd2843c12013-05-16 10:40:55 -07002780 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002781 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01002782 * Do an implicit RELEASE of the existing reservation.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002783 */
2784 if (dev->dev_pr_res_holder)
2785 __core_scsi3_complete_pro_release(dev, nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08002786 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002787
2788 dev->dev_pr_res_holder = pr_reg;
2789 pr_reg->pr_res_holder = 1;
2790 pr_reg->pr_res_type = type;
2791 pr_reg->pr_res_scope = scope;
2792
Andy Grover6708bb22011-06-08 10:36:43 -07002793 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002794 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002795 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002796 core_scsi3_pr_dump_type(type),
2797 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
Andy Grover6708bb22011-06-08 10:36:43 -07002798 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002799 tfo->get_fabric_name(), (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
Andy Groverd2843c12013-05-16 10:40:55 -07002800 nacl->initiatorname, i_buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 /*
2802 * For PREEMPT_AND_ABORT, add the preempting reservation's
2803 * struct t10_pr_registration to the list that will be compared
2804 * against received CDBs..
2805 */
2806 if (preempt_and_abort_list)
2807 list_add_tail(&pr_reg->pr_reg_abort_list,
2808 preempt_and_abort_list);
2809}
2810
2811static void core_scsi3_release_preempt_and_abort(
2812 struct list_head *preempt_and_abort_list,
2813 struct t10_pr_registration *pr_reg_holder)
2814{
2815 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2816
2817 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2818 pr_reg_abort_list) {
2819
2820 list_del(&pr_reg->pr_reg_abort_list);
2821 if (pr_reg_holder == pr_reg)
2822 continue;
2823 if (pr_reg->pr_res_holder) {
Andy Grover6708bb22011-06-08 10:36:43 -07002824 pr_warn("pr_reg->pr_res_holder still set\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002825 continue;
2826 }
2827
2828 pr_reg->pr_reg_deve = NULL;
2829 pr_reg->pr_reg_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002830 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2831 }
2832}
2833
Christoph Hellwigde103c92012-11-06 12:24:09 -08002834static sense_reason_t
2835core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07002836 u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002837{
Andy Grover5951146d2011-07-19 10:26:37 +00002838 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002839 struct se_node_acl *pr_reg_nacl;
Andy Grovere3d6f902011-07-19 08:55:10 +00002840 struct se_session *se_sess = cmd->se_sess;
Roland Dreierd0f474e2012-01-12 10:41:18 -08002841 LIST_HEAD(preempt_and_abort_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002843 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Hannes Reineckef2d30682015-06-10 08:41:22 +02002844 u64 pr_res_mapped_lun = 0;
Steven Allenb6932ee2014-10-24 15:18:26 -07002845 int all_reg = 0, calling_it_nexus = 0;
2846 bool sa_res_key_unmatched = sa_res_key != 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08002847 int prh_type = 0, prh_scope = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848
Christoph Hellwigde103c92012-11-06 12:24:09 -08002849 if (!se_sess)
2850 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851
Andy Grover5951146d2011-07-19 10:26:37 +00002852 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07002854 if (!pr_reg_n) {
2855 pr_err("SPC-3 PR: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002856 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
Andy Grover33ce6a82013-05-16 10:40:54 -07002857 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
Christoph Hellwigde103c92012-11-06 12:24:09 -08002858 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002859 }
2860 if (pr_reg_n->pr_res_key != res_key) {
2861 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002862 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002863 }
2864 if (scope != PR_SCOPE_LU_SCOPE) {
Andy Grover6708bb22011-06-08 10:36:43 -07002865 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002866 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002867 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002868 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869
2870 spin_lock(&dev->dev_reservation_lock);
2871 pr_res_holder = dev->dev_pr_res_holder;
2872 if (pr_res_holder &&
2873 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2874 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2875 all_reg = 1;
2876
Andy Grover6708bb22011-06-08 10:36:43 -07002877 if (!all_reg && !sa_res_key) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002878 spin_unlock(&dev->dev_reservation_lock);
2879 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002880 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881 }
2882 /*
2883 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2884 *
2885 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2886 * persistent reservation holder or there is no persistent reservation
2887 * holder (i.e., there is no persistent reservation), then the device
2888 * server shall perform a preempt by doing the following in an
2889 * uninterrupted series of actions. (See below..)
2890 */
Andy Grover6708bb22011-06-08 10:36:43 -07002891 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002892 /*
2893 * No existing or SA Reservation Key matching reservations..
2894 *
2895 * PROUT SA PREEMPT with All Registrant type reservations are
2896 * allowed to be processed without a matching SA Reservation Key
2897 */
2898 spin_lock(&pr_tmpl->registration_lock);
2899 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2900 &pr_tmpl->registration_list, pr_reg_list) {
2901 /*
2902 * Removing of registrations in non all registrants
2903 * type reservations without a matching SA reservation
2904 * key.
2905 *
2906 * a) Remove the registrations for all I_T nexuses
2907 * specified by the SERVICE ACTION RESERVATION KEY
2908 * field;
2909 * b) Ignore the contents of the SCOPE and TYPE fields;
2910 * c) Process tasks as defined in 5.7.1; and
2911 * d) Establish a unit attention condition for the
2912 * initiator port associated with every I_T nexus
2913 * that lost its registration other than the I_T
2914 * nexus on which the PERSISTENT RESERVE OUT command
2915 * was received, with the additional sense code set
2916 * to REGISTRATIONS PREEMPTED.
2917 */
Andy Grover6708bb22011-06-08 10:36:43 -07002918 if (!all_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919 if (pr_reg->pr_res_key != sa_res_key)
2920 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002921 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922
2923 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2924 pr_reg_nacl = pr_reg->pr_reg_nacl;
2925 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2926 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002927 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002928 NULL, calling_it_nexus);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002929 } else {
2930 /*
2931 * Case for any existing all registrants type
2932 * reservation, follow logic in spc4r17 section
2933 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2934 *
2935 * For a ZERO SA Reservation key, release
Hannes Reinecke125d0112013-11-19 09:07:46 +01002936 * all other registrations and do an implicit
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002937 * release of active persistent reservation.
2938 *
2939 * For a non-ZERO SA Reservation key, only
2940 * release the matching reservation key from
2941 * registrations.
2942 */
2943 if ((sa_res_key) &&
2944 (pr_reg->pr_res_key != sa_res_key))
2945 continue;
Steven Allenb6932ee2014-10-24 15:18:26 -07002946 sa_res_key_unmatched = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002947
2948 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2949 if (calling_it_nexus)
2950 continue;
2951
2952 pr_reg_nacl = pr_reg->pr_reg_nacl;
2953 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2954 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07002955 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002956 NULL, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957 }
Andy Grover6708bb22011-06-08 10:36:43 -07002958 if (!calling_it_nexus)
Hannes Reineckec51c8e72015-06-11 10:01:26 +02002959 target_ua_allocate_lun(pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002960 pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08002961 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002962 }
2963 spin_unlock(&pr_tmpl->registration_lock);
2964 /*
2965 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2966 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2967 * RESERVATION KEY field to a value that does not match any
2968 * registered reservation key, then the device server shall
2969 * complete the command with RESERVATION CONFLICT status.
2970 */
Steven Allenb6932ee2014-10-24 15:18:26 -07002971 if (sa_res_key_unmatched) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972 spin_unlock(&dev->dev_reservation_lock);
2973 core_scsi3_put_pr_reg(pr_reg_n);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002974 return TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002975 }
2976 /*
2977 * For an existing all registrants type reservation
2978 * with a zero SA rservation key, preempt the existing
2979 * reservation with the new PR type and scope.
2980 */
2981 if (pr_res_holder && all_reg && !(sa_res_key)) {
2982 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07002983 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2984 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002985
Andy Grover33ce6a82013-05-16 10:40:54 -07002986 if (preempt_type == PREEMPT_AND_ABORT)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002987 core_scsi3_release_preempt_and_abort(
2988 &preempt_and_abort_list, pr_reg_n);
2989 }
2990 spin_unlock(&dev->dev_reservation_lock);
2991
Andy Grover459f2132013-05-16 10:41:02 -07002992 if (pr_tmpl->pr_aptpl_active)
2993 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002994
2995 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00002996 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002997 return 0;
2998 }
2999 /*
3000 * The PREEMPTing SA reservation key matches that of the
3001 * existing persistent reservation, first, we check if
3002 * we are preempting our own reservation.
3003 * From spc4r17, section 5.7.11.4.3 Preempting
3004 * persistent reservations and registration handling
3005 *
3006 * If an all registrants persistent reservation is not
3007 * present, it is not an error for the persistent
3008 * reservation holder to preempt itself (i.e., a
3009 * PERSISTENT RESERVE OUT with a PREEMPT service action
3010 * or a PREEMPT AND ABORT service action with the
3011 * SERVICE ACTION RESERVATION KEY value equal to the
3012 * persistent reservation holder's reservation key that
3013 * is received from the persistent reservation holder).
3014 * In that case, the device server shall establish the
3015 * new persistent reservation and maintain the
3016 * registration.
3017 */
3018 prh_type = pr_res_holder->pr_res_type;
3019 prh_scope = pr_res_holder->pr_res_scope;
3020 /*
3021 * If the SERVICE ACTION RESERVATION KEY field identifies a
3022 * persistent reservation holder (see 5.7.10), the device
3023 * server shall perform a preempt by doing the following as
3024 * an uninterrupted series of actions:
3025 *
3026 * a) Release the persistent reservation for the holder
3027 * identified by the SERVICE ACTION RESERVATION KEY field;
3028 */
3029 if (pr_reg_n != pr_res_holder)
3030 __core_scsi3_complete_pro_release(dev,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08003031 pr_res_holder->pr_reg_nacl,
3032 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003033 /*
3034 * b) Remove the registrations for all I_T nexuses identified
3035 * by the SERVICE ACTION RESERVATION KEY field, except the
3036 * I_T nexus that is being used for the PERSISTENT RESERVE
3037 * OUT command. If an all registrants persistent reservation
3038 * is present and the SERVICE ACTION RESERVATION KEY field
3039 * is set to zero, then all registrations shall be removed
3040 * except for that of the I_T nexus that is being used for
3041 * the PERSISTENT RESERVE OUT command;
3042 */
3043 spin_lock(&pr_tmpl->registration_lock);
3044 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3045 &pr_tmpl->registration_list, pr_reg_list) {
3046
3047 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3048 if (calling_it_nexus)
3049 continue;
3050
3051 if (pr_reg->pr_res_key != sa_res_key)
3052 continue;
3053
3054 pr_reg_nacl = pr_reg->pr_reg_nacl;
3055 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3056 __core_scsi3_free_registration(dev, pr_reg,
Andy Grover33ce6a82013-05-16 10:40:54 -07003057 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003058 calling_it_nexus);
3059 /*
3060 * e) Establish a unit attention condition for the initiator
3061 * port associated with every I_T nexus that lost its
3062 * persistent reservation and/or registration, with the
3063 * additional sense code set to REGISTRATIONS PREEMPTED;
3064 */
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003065 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
Marco Sanvido9e08e342012-01-03 17:12:57 -08003066 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003067 }
3068 spin_unlock(&pr_tmpl->registration_lock);
3069 /*
3070 * c) Establish a persistent reservation for the preempting
3071 * I_T nexus using the contents of the SCOPE and TYPE fields;
3072 */
3073 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
Andy Grover33ce6a82013-05-16 10:40:54 -07003074 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3075 type, scope, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003076 /*
3077 * d) Process tasks as defined in 5.7.1;
3078 * e) See above..
3079 * f) If the type or scope has changed, then for every I_T nexus
3080 * whose reservation key was not removed, except for the I_T
3081 * nexus on which the PERSISTENT RESERVE OUT command was
3082 * received, the device server shall establish a unit
3083 * attention condition for the initiator port associated with
3084 * that I_T nexus, with the additional sense code set to
3085 * RESERVATIONS RELEASED. If the type or scope have not
3086 * changed, then no unit attention condition(s) shall be
3087 * established for this reason.
3088 */
3089 if ((prh_type != type) || (prh_scope != scope)) {
3090 spin_lock(&pr_tmpl->registration_lock);
3091 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3092 &pr_tmpl->registration_list, pr_reg_list) {
3093
3094 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3095 if (calling_it_nexus)
3096 continue;
3097
Hannes Reineckec51c8e72015-06-11 10:01:26 +02003098 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003099 pr_reg->pr_res_mapped_lun, 0x2A,
3100 ASCQ_2AH_RESERVATIONS_RELEASED);
3101 }
3102 spin_unlock(&pr_tmpl->registration_lock);
3103 }
3104 spin_unlock(&dev->dev_reservation_lock);
3105 /*
3106 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3107 * All received CDBs for the matching existing reservation and
3108 * registrations undergo ABORT_TASK logic.
3109 *
3110 * From there, core_scsi3_release_preempt_and_abort() will
3111 * release every registration in the list (which have already
3112 * been removed from the primary pr_reg list), except the
3113 * new persistent reservation holder, the calling Initiator Port.
3114 */
Andy Grover33ce6a82013-05-16 10:40:54 -07003115 if (preempt_type == PREEMPT_AND_ABORT) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003116 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3117 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3118 pr_reg_n);
3119 }
3120
Andy Grover459f2132013-05-16 10:41:02 -07003121 if (pr_tmpl->pr_aptpl_active)
3122 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123
3124 core_scsi3_put_pr_reg(pr_reg_n);
Andy Grover5951146d2011-07-19 10:26:37 +00003125 core_scsi3_pr_generation(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003126 return 0;
3127}
3128
Christoph Hellwigde103c92012-11-06 12:24:09 -08003129static sense_reason_t
3130core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003131 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003132{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003133 switch (type) {
3134 case PR_TYPE_WRITE_EXCLUSIVE:
3135 case PR_TYPE_EXCLUSIVE_ACCESS:
3136 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3137 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3138 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3139 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
Christoph Hellwigde103c92012-11-06 12:24:09 -08003140 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
Andy Grover33ce6a82013-05-16 10:40:54 -07003141 sa_res_key, preempt_type);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003142 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003143 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
Andy Grover33ce6a82013-05-16 10:40:54 -07003144 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003145 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003146 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147}
3148
3149
Christoph Hellwigde103c92012-11-06 12:24:09 -08003150static sense_reason_t
3151core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3152 u64 sa_res_key, int aptpl, int unreg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003153{
Andy Grovere3d6f902011-07-19 08:55:10 +00003154 struct se_session *se_sess = cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +00003155 struct se_device *dev = cmd->se_dev;
Jörn Engel28168902012-03-15 15:06:58 -04003156 struct se_dev_entry *dest_se_deve = NULL;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003157 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003158 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
Christoph Hellwig9ac89282015-04-08 20:01:35 +02003160 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003162 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003163 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003164 const unsigned char *initiator_str;
Julia Lawall13ba5642014-11-30 19:14:12 +01003165 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003166 u32 tid_len, tmp_tid_len;
Andy Groverd2843c12013-05-16 10:40:55 -07003167 int new_reg = 0, type, scope, matching_iname;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003168 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003169 unsigned short rtpi;
3170 unsigned char proto_ident;
3171
Andy Grover6708bb22011-06-08 10:36:43 -07003172 if (!se_sess || !se_lun) {
3173 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003174 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003176
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003177 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3178 se_tpg = se_sess->se_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003179 tf_ops = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180 /*
3181 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3182 * Register behaviors for a REGISTER AND MOVE service action
3183 *
3184 * Locate the existing *pr_reg via struct se_node_acl pointers
3185 */
Andy Grover5951146d2011-07-19 10:26:37 +00003186 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187 se_sess);
Andy Grover6708bb22011-06-08 10:36:43 -07003188 if (!pr_reg) {
3189 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003190 " *pr_reg for REGISTER_AND_MOVE\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003191 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003192 }
3193 /*
3194 * The provided reservation key much match the existing reservation key
3195 * provided during this initiator's I_T nexus registration.
3196 */
3197 if (res_key != pr_reg->pr_res_key) {
Andy Grover6708bb22011-06-08 10:36:43 -07003198 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003199 " res_key: 0x%016Lx does not match existing SA REGISTER"
3200 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003201 ret = TCM_RESERVATION_CONFLICT;
3202 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003203 }
3204 /*
3205 * The service active reservation key needs to be non zero
3206 */
Andy Grover6708bb22011-06-08 10:36:43 -07003207 if (!sa_res_key) {
3208 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003209 " sa_res_key\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003210 ret = TCM_INVALID_PARAMETER_LIST;
3211 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003212 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003213
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 /*
3215 * Determine the Relative Target Port Identifier where the reservation
3216 * will be moved to for the TransportID containing SCSI initiator WWN
3217 * information.
3218 */
Andy Grover49493142012-01-16 16:57:08 -08003219 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003220 if (!buf) {
3221 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3222 goto out_put_pr_reg;
3223 }
3224
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003225 rtpi = (buf[18] & 0xff) << 8;
3226 rtpi |= buf[19] & 0xff;
3227 tid_len = (buf[20] & 0xff) << 24;
3228 tid_len |= (buf[21] & 0xff) << 16;
3229 tid_len |= (buf[22] & 0xff) << 8;
3230 tid_len |= buf[23] & 0xff;
Andy Grover49493142012-01-16 16:57:08 -08003231 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003232 buf = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003233
3234 if ((tid_len + 24) != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003235 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003236 " does not equal CDB data_length: %u\n", tid_len,
3237 cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003238 ret = TCM_INVALID_PARAMETER_LIST;
3239 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003240 }
3241
3242 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003243 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3244 if (tmp_lun->lun_rtpi != rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003245 continue;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07003246 dest_se_tpg = tmp_lun->lun_tpg;
Andy Grovere3d6f902011-07-19 08:55:10 +00003247 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
Andy Grover6708bb22011-06-08 10:36:43 -07003248 if (!dest_tf_ops)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003249 continue;
3250
Joern Engel33940d02014-09-16 16:23:12 -04003251 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003252 spin_unlock(&dev->se_port_lock);
3253
Christoph Hellwigde103c92012-11-06 12:24:09 -08003254 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003255 pr_err("core_scsi3_tpg_depend_item() failed"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003256 " for dest_se_tpg\n");
Joern Engel33940d02014-09-16 16:23:12 -04003257 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003258 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3259 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003260 }
3261
3262 spin_lock(&dev->se_port_lock);
3263 break;
3264 }
3265 spin_unlock(&dev->se_port_lock);
3266
Andy Grover6708bb22011-06-08 10:36:43 -07003267 if (!dest_se_tpg || !dest_tf_ops) {
3268 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003269 " fabric ops from Relative Target Port Identifier:"
3270 " %hu\n", rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003271 ret = TCM_INVALID_PARAMETER_LIST;
3272 goto out_put_pr_reg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003273 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003274
Andy Grover49493142012-01-16 16:57:08 -08003275 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003276 if (!buf) {
3277 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3278 goto out_put_pr_reg;
3279 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003280 proto_ident = (buf[24] & 0x0f);
Andy Grover8b1e1242012-04-03 15:51:12 -07003281
Andy Grover6708bb22011-06-08 10:36:43 -07003282 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003283 " 0x%02x\n", proto_ident);
Andy Grover8b1e1242012-04-03 15:51:12 -07003284
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003285 if (proto_ident != dest_se_tpg->proto_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07003286 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003287 " proto_ident: 0x%02x does not match ident: 0x%02x"
3288 " from fabric: %s\n", proto_ident,
Christoph Hellwig2aeeafa2015-05-01 17:47:57 +02003289 dest_se_tpg->proto_id,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003290 dest_tf_ops->get_fabric_name());
Christoph Hellwigde103c92012-11-06 12:24:09 -08003291 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003292 goto out;
3293 }
Christoph Hellwig2650d712015-05-01 17:47:58 +02003294 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003296 if (!initiator_str) {
3297 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003298 " initiator_str from Transport ID\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003299 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003300 goto out;
3301 }
3302
Andy Grover49493142012-01-16 16:57:08 -08003303 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003304 buf = NULL;
3305
Andy Grover6708bb22011-06-08 10:36:43 -07003306 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003307 " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3308 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3309 iport_ptr : "");
3310 /*
3311 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3312 * action specifies a TransportID that is the same as the initiator port
3313 * of the I_T nexus for the command received, then the command shall
3314 * be terminated with CHECK CONDITION status, with the sense key set to
3315 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3316 * IN PARAMETER LIST.
3317 */
3318 pr_reg_nacl = pr_reg->pr_reg_nacl;
3319 matching_iname = (!strcmp(initiator_str,
3320 pr_reg_nacl->initiatorname)) ? 1 : 0;
Andy Grover6708bb22011-06-08 10:36:43 -07003321 if (!matching_iname)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003322 goto after_iport_check;
3323
Andy Grover6708bb22011-06-08 10:36:43 -07003324 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3325 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326 " matches: %s on received I_T Nexus\n", initiator_str,
3327 pr_reg_nacl->initiatorname);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003328 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003329 goto out;
3330 }
Andy Grover6708bb22011-06-08 10:36:43 -07003331 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3332 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003333 " matches: %s %s on received I_T Nexus\n",
3334 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3335 pr_reg->pr_reg_isid);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003336 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003337 goto out;
3338 }
3339after_iport_check:
3340 /*
3341 * Locate the destination struct se_node_acl from the received Transport ID
3342 */
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003343 mutex_lock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003344 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3345 initiator_str);
Joern Engel33940d02014-09-16 16:23:12 -04003346 if (dest_node_acl)
3347 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellinger403edd72015-03-08 22:33:47 +00003348 mutex_unlock(&dest_se_tpg->acl_node_mutex);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003349
Andy Grover6708bb22011-06-08 10:36:43 -07003350 if (!dest_node_acl) {
3351 pr_err("Unable to locate %s dest_node_acl for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003352 " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3353 initiator_str);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003354 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003355 goto out;
3356 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003357
3358 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003359 pr_err("core_scsi3_nodeacl_depend_item() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003360 " dest_node_acl\n");
Joern Engel33940d02014-09-16 16:23:12 -04003361 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003362 dest_node_acl = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003363 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003364 goto out;
3365 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003366
Andy Grover6708bb22011-06-08 10:36:43 -07003367 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003368 " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3369 dest_node_acl->initiatorname);
Andy Grover8b1e1242012-04-03 15:51:12 -07003370
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003371 /*
3372 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3373 * PORT IDENTIFIER.
3374 */
3375 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
Andy Grover6708bb22011-06-08 10:36:43 -07003376 if (!dest_se_deve) {
3377 pr_err("Unable to locate %s dest_se_deve from RTPI:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003378 " %hu\n", dest_tf_ops->get_fabric_name(), rtpi);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003379 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003380 goto out;
3381 }
3382
Christoph Hellwigde103c92012-11-06 12:24:09 -08003383 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003384 pr_err("core_scsi3_lunacl_depend_item() failed\n");
Nicholas Bellinger29a05de2015-03-22 20:42:19 -07003385 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003386 dest_se_deve = NULL;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003387 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003388 goto out;
3389 }
Andy Grover8b1e1242012-04-03 15:51:12 -07003390
Andy Grover6708bb22011-06-08 10:36:43 -07003391 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
Hannes Reineckef2d30682015-06-10 08:41:22 +02003392 " ACL for dest_se_deve->mapped_lun: %llu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003393 dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3394 dest_se_deve->mapped_lun);
Andy Grover8b1e1242012-04-03 15:51:12 -07003395
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003396 /*
3397 * A persistent reservation needs to already existing in order to
3398 * successfully complete the REGISTER_AND_MOVE service action..
3399 */
3400 spin_lock(&dev->dev_reservation_lock);
3401 pr_res_holder = dev->dev_pr_res_holder;
Andy Grover6708bb22011-06-08 10:36:43 -07003402 if (!pr_res_holder) {
3403 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003404 " currently held\n");
3405 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003406 ret = TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003407 goto out;
3408 }
3409 /*
3410 * The received on I_T Nexus must be the reservation holder.
3411 *
3412 * From spc4r17 section 5.7.8 Table 50 --
3413 * Register behaviors for a REGISTER AND MOVE service action
3414 */
Ilias Tsitsimpise673dc92015-03-26 17:12:42 +02003415 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003416 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003417 " Nexus is not reservation holder\n");
3418 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003419 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003420 goto out;
3421 }
3422 /*
3423 * From spc4r17 section 5.7.8: registering and moving reservation
3424 *
3425 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3426 * action is received and the established persistent reservation is a
3427 * Write Exclusive - All Registrants type or Exclusive Access -
3428 * All Registrants type reservation, then the command shall be completed
3429 * with RESERVATION CONFLICT status.
3430 */
3431 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3432 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003433 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003434 " reservation for type: %s\n",
3435 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3436 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003437 ret = TCM_RESERVATION_CONFLICT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003438 goto out;
3439 }
3440 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3441 /*
3442 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3443 */
3444 type = pr_res_holder->pr_res_type;
3445 scope = pr_res_holder->pr_res_type;
3446 /*
3447 * c) Associate the reservation key specified in the SERVICE ACTION
3448 * RESERVATION KEY field with the I_T nexus specified as the
3449 * destination of the register and move, where:
3450 * A) The I_T nexus is specified by the TransportID and the
3451 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3452 * B) Regardless of the TransportID format used, the association for
3453 * the initiator port is based on either the initiator port name
3454 * (see 3.1.71) on SCSI transport protocols where port names are
3455 * required or the initiator port identifier (see 3.1.70) on SCSI
3456 * transport protocols where port names are not required;
3457 * d) Register the reservation key specified in the SERVICE ACTION
3458 * RESERVATION KEY field;
3459 * e) Retain the reservation key specified in the SERVICE ACTION
3460 * RESERVATION KEY field and associated information;
3461 *
3462 * Also, It is not an error for a REGISTER AND MOVE service action to
3463 * register an I_T nexus that is already registered with the same
3464 * reservation key or a different reservation key.
3465 */
3466 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3467 iport_ptr);
Andy Grover6708bb22011-06-08 10:36:43 -07003468 if (!dest_pr_reg) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003469 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
3470 atomic_read(&dest_se_deve->pr_kref.refcount) != 0);
3471
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003472 spin_unlock(&dev->dev_reservation_lock);
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00003473 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3474 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3475 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08003476 ret = TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003477 goto out;
3478 }
Nicholas Bellinger80bfdfa2015-03-25 01:02:57 -07003479 spin_lock(&dev->dev_reservation_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003480 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3481 iport_ptr);
3482 new_reg = 1;
3483 }
3484 /*
3485 * f) Release the persistent reservation for the persistent reservation
3486 * holder (i.e., the I_T nexus on which the
3487 */
3488 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
Nicholas Bellinger6c3c9ba2014-12-15 11:50:26 -08003489 dev->dev_pr_res_holder, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003490 /*
3491 * g) Move the persistent reservation to the specified I_T nexus using
3492 * the same scope and type as the persistent reservation released in
3493 * item f); and
3494 */
3495 dev->dev_pr_res_holder = dest_pr_reg;
3496 dest_pr_reg->pr_res_holder = 1;
3497 dest_pr_reg->pr_res_type = type;
3498 pr_reg->pr_res_scope = scope;
Andy Groverd2843c12013-05-16 10:40:55 -07003499 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003500 /*
3501 * Increment PRGeneration for existing registrations..
3502 */
Andy Grover6708bb22011-06-08 10:36:43 -07003503 if (!new_reg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003504 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3505 spin_unlock(&dev->dev_reservation_lock);
3506
Andy Grover6708bb22011-06-08 10:36:43 -07003507 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003508 " created new reservation holder TYPE: %s on object RTPI:"
3509 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3510 core_scsi3_pr_dump_type(type), rtpi,
3511 dest_pr_reg->pr_res_generation);
Andy Grover6708bb22011-06-08 10:36:43 -07003512 pr_debug("SPC-3 PR Successfully moved reservation from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003513 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3514 tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
Andy Groverd2843c12013-05-16 10:40:55 -07003515 i_buf, dest_tf_ops->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003516 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3517 iport_ptr : "");
3518 /*
3519 * It is now safe to release configfs group dependencies for destination
3520 * of Transport ID Initiator Device/Port Identifier
3521 */
3522 core_scsi3_lunacl_undepend_item(dest_se_deve);
3523 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3524 core_scsi3_tpg_undepend_item(dest_se_tpg);
3525 /*
3526 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3527 * nexus on which PERSISTENT RESERVE OUT command was received.
3528 */
3529 if (unreg) {
3530 spin_lock(&pr_tmpl->registration_lock);
3531 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3532 spin_unlock(&pr_tmpl->registration_lock);
3533 } else
3534 core_scsi3_put_pr_reg(pr_reg);
3535
Andy Grover459f2132013-05-16 10:41:02 -07003536 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003537
Andy Grover49493142012-01-16 16:57:08 -08003538 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003539
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003540 core_scsi3_put_pr_reg(dest_pr_reg);
3541 return 0;
3542out:
Andy Grover05d1c7c2011-07-20 19:13:28 +00003543 if (buf)
Andy Grover49493142012-01-16 16:57:08 -08003544 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003545 if (dest_se_deve)
3546 core_scsi3_lunacl_undepend_item(dest_se_deve);
3547 if (dest_node_acl)
3548 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3549 core_scsi3_tpg_undepend_item(dest_se_tpg);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003550
3551out_put_pr_reg:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003552 core_scsi3_put_pr_reg(pr_reg);
3553 return ret;
3554}
3555
3556static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3557{
3558 unsigned int __v1, __v2;
3559
3560 __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3561 __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3562
3563 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3564}
3565
3566/*
3567 * See spc4r17 section 6.14 Table 170
3568 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003569sense_reason_t
3570target_scsi3_emulate_pr_out(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003571{
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003572 struct se_device *dev = cmd->se_dev;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003573 unsigned char *cdb = &cmd->t_task_cdb[0];
Andy Grover05d1c7c2011-07-20 19:13:28 +00003574 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003575 u64 res_key, sa_res_key;
3576 int sa, scope, type, aptpl;
3577 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
Christoph Hellwigde103c92012-11-06 12:24:09 -08003578 sense_reason_t ret;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003579
3580 /*
3581 * Following spc2r20 5.5.1 Reservations overview:
3582 *
3583 * If a logical unit has been reserved by any RESERVE command and is
3584 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3585 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3586 * initiator or service action and shall terminate with a RESERVATION
3587 * CONFLICT status.
3588 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003589 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003590 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3591 " SPC-2 reservation is held, returning"
3592 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08003593 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04003594 }
3595
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003596 /*
3597 * FIXME: A NULL struct se_session pointer means an this is not coming from
3598 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3599 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003600 if (!cmd->se_sess)
3601 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003602
3603 if (cmd->data_length < 24) {
Andy Grover6708bb22011-06-08 10:36:43 -07003604 pr_warn("SPC-PR: Received PR OUT parameter list"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003605 " length too small: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003606 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003607 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003608
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003609 /*
3610 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3611 */
3612 sa = (cdb[1] & 0x1f);
3613 scope = (cdb[2] & 0xf0);
3614 type = (cdb[2] & 0x0f);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003615
Andy Grover49493142012-01-16 16:57:08 -08003616 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003617 if (!buf)
3618 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3619
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003620 /*
3621 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3622 */
3623 res_key = core_scsi3_extract_reservation_key(&buf[0]);
3624 sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3625 /*
3626 * REGISTER_AND_MOVE uses a different SA parameter list containing
3627 * SCSI TransportIDs.
3628 */
3629 if (sa != PRO_REGISTER_AND_MOVE) {
3630 spec_i_pt = (buf[20] & 0x08);
3631 all_tg_pt = (buf[20] & 0x04);
3632 aptpl = (buf[20] & 0x01);
3633 } else {
3634 aptpl = (buf[17] & 0x01);
3635 unreg = (buf[17] & 0x02);
3636 }
Nicholas Bellinger92404e62014-10-04 01:06:08 +00003637 /*
3638 * If the backend device has been configured to force APTPL metadata
3639 * write-out, go ahead and propigate aptpl=1 down now.
3640 */
3641 if (dev->dev_attrib.force_pr_aptpl)
3642 aptpl = 1;
3643
Andy Grover49493142012-01-16 16:57:08 -08003644 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003645 buf = NULL;
3646
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003647 /*
3648 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3649 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003650 if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3651 return TCM_INVALID_PARAMETER_LIST;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003652
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003653 /*
3654 * From spc4r17 section 6.14:
3655 *
3656 * If the SPEC_I_PT bit is set to zero, the service action is not
3657 * REGISTER AND MOVE, and the parameter list length is not 24, then
3658 * the command shall be terminated with CHECK CONDITION status, with
3659 * the sense key set to ILLEGAL REQUEST, and the additional sense
3660 * code set to PARAMETER LIST LENGTH ERROR.
3661 */
Andy Grover6708bb22011-06-08 10:36:43 -07003662 if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003663 (cmd->data_length != 24)) {
Andy Grover6708bb22011-06-08 10:36:43 -07003664 pr_warn("SPC-PR: Received PR OUT illegal parameter"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003665 " list length: %u\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003666 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003667 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08003668
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003669 /*
3670 * (core_scsi3_emulate_pro_* function parameters
3671 * are defined by spc4r17 Table 174:
3672 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3673 */
3674 switch (sa) {
3675 case PRO_REGISTER:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003676 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003677 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003678 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003679 case PRO_RESERVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003680 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3681 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003682 case PRO_RELEASE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003683 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3684 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003685 case PRO_CLEAR:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003686 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3687 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003688 case PRO_PREEMPT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003689 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003690 res_key, sa_res_key, PREEMPT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003691 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003692 case PRO_PREEMPT_AND_ABORT:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003693 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
Andy Grover33ce6a82013-05-16 10:40:54 -07003694 res_key, sa_res_key, PREEMPT_AND_ABORT);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003695 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003696 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003697 ret = core_scsi3_emulate_pro_register(cmd,
Andy Grover33ce6a82013-05-16 10:40:54 -07003698 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003699 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003700 case PRO_REGISTER_AND_MOVE:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003701 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003702 sa_res_key, aptpl, unreg);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003703 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003704 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003705 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003706 " action: 0x%02x\n", cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003707 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003708 }
3709
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003710 if (!ret)
3711 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04003712 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003713}
3714
3715/*
3716 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3717 *
3718 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3719 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003720static sense_reason_t
3721core_scsi3_pri_read_keys(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003722{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003723 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003724 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003725 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003726 u32 add_len = 0, off = 8;
3727
3728 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003729 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003730 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003731 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003732 }
3733
Andy Grover49493142012-01-16 16:57:08 -08003734 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003735 if (!buf)
3736 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3737
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003738 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3739 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3740 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3741 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003742
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003743 spin_lock(&dev->t10_pr.registration_lock);
3744 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003745 pr_reg_list) {
3746 /*
3747 * Check for overflow of 8byte PRI READ_KEYS payload and
3748 * next reservation key list descriptor.
3749 */
3750 if ((add_len + 8) > (cmd->data_length - 8))
3751 break;
3752
3753 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3754 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3755 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3756 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3757 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3758 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3759 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3760 buf[off++] = (pr_reg->pr_res_key & 0xff);
3761
3762 add_len += 8;
3763 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003764 spin_unlock(&dev->t10_pr.registration_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003765
3766 buf[4] = ((add_len >> 24) & 0xff);
3767 buf[5] = ((add_len >> 16) & 0xff);
3768 buf[6] = ((add_len >> 8) & 0xff);
3769 buf[7] = (add_len & 0xff);
3770
Andy Grover49493142012-01-16 16:57:08 -08003771 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003772
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003773 return 0;
3774}
3775
3776/*
3777 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3778 *
3779 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3780 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003781static sense_reason_t
3782core_scsi3_pri_read_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003783{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003784 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003785 struct t10_pr_registration *pr_reg;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003786 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003787 u64 pr_res_key;
3788 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3789
3790 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003791 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003792 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003793 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003794 }
3795
Andy Grover49493142012-01-16 16:57:08 -08003796 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003797 if (!buf)
3798 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3799
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003800 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3801 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3802 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3803 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003804
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003805 spin_lock(&dev->dev_reservation_lock);
3806 pr_reg = dev->dev_pr_res_holder;
Andy Groveree1b1b92012-07-12 17:34:54 -07003807 if (pr_reg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003808 /*
3809 * Set the hardcoded Additional Length
3810 */
3811 buf[4] = ((add_len >> 24) & 0xff);
3812 buf[5] = ((add_len >> 16) & 0xff);
3813 buf[6] = ((add_len >> 8) & 0xff);
3814 buf[7] = (add_len & 0xff);
3815
Andy Grover05d1c7c2011-07-20 19:13:28 +00003816 if (cmd->data_length < 22)
3817 goto err;
3818
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003819 /*
3820 * Set the Reservation key.
3821 *
3822 * From spc4r17, section 5.7.10:
3823 * A persistent reservation holder has its reservation key
3824 * returned in the parameter data from a PERSISTENT
3825 * RESERVE IN command with READ RESERVATION service action as
3826 * follows:
3827 * a) For a persistent reservation of the type Write Exclusive
3828 * - All Registrants or Exclusive Access ­ All Regitrants,
3829 * the reservation key shall be set to zero; or
3830 * b) For all other persistent reservation types, the
3831 * reservation key shall be set to the registered
3832 * reservation key for the I_T nexus that holds the
3833 * persistent reservation.
3834 */
3835 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3836 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3837 pr_res_key = 0;
3838 else
3839 pr_res_key = pr_reg->pr_res_key;
3840
3841 buf[8] = ((pr_res_key >> 56) & 0xff);
3842 buf[9] = ((pr_res_key >> 48) & 0xff);
3843 buf[10] = ((pr_res_key >> 40) & 0xff);
3844 buf[11] = ((pr_res_key >> 32) & 0xff);
3845 buf[12] = ((pr_res_key >> 24) & 0xff);
3846 buf[13] = ((pr_res_key >> 16) & 0xff);
3847 buf[14] = ((pr_res_key >> 8) & 0xff);
3848 buf[15] = (pr_res_key & 0xff);
3849 /*
3850 * Set the SCOPE and TYPE
3851 */
3852 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3853 (pr_reg->pr_res_type & 0x0f);
3854 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00003855
3856err:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003857 spin_unlock(&dev->dev_reservation_lock);
Andy Grover49493142012-01-16 16:57:08 -08003858 transport_kunmap_data_sg(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003859
3860 return 0;
3861}
3862
3863/*
3864 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3865 *
3866 * See spc4r17 section 6.13.4 Table 165
3867 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003868static sense_reason_t
3869core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003870{
Andy Grover5951146d2011-07-19 10:26:37 +00003871 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003872 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003873 unsigned char *buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003874 u16 add_len = 8; /* Hardcoded to 8. */
3875
3876 if (cmd->data_length < 6) {
Andy Grover6708bb22011-06-08 10:36:43 -07003877 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003878 " %u too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003879 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003880 }
3881
Andy Grover49493142012-01-16 16:57:08 -08003882 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003883 if (!buf)
3884 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003885
Joern Engelc4352852014-09-02 17:50:01 -04003886 buf[0] = ((add_len >> 8) & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003887 buf[1] = (add_len & 0xff);
3888 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3889 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3890 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3891 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3892 /*
3893 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3894 * set the TMV: Task Mask Valid bit.
3895 */
3896 buf[3] |= 0x80;
3897 /*
3898 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3899 */
3900 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3901 /*
3902 * PTPL_A: Persistence across Target Power Loss Active bit
3903 */
3904 if (pr_tmpl->pr_aptpl_active)
3905 buf[3] |= 0x01;
3906 /*
3907 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3908 */
3909 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3910 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3911 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3912 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3913 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3914 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3915
Andy Grover49493142012-01-16 16:57:08 -08003916 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003917
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003918 return 0;
3919}
3920
3921/*
3922 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3923 *
3924 * See spc4r17 section 6.13.5 Table 168 and 169
3925 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08003926static sense_reason_t
3927core_scsi3_pri_read_full_status(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003928{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003929 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003930 struct se_node_acl *se_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003931 struct se_portal_group *se_tpg;
3932 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003933 struct t10_reservation *pr_tmpl = &dev->t10_pr;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003934 unsigned char *buf;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003935 u32 add_desc_len = 0, add_len = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003936 u32 off = 8; /* off into first Full Status descriptor */
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003937 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
Christoph Hellwig2650d712015-05-01 17:47:58 +02003938 int exp_desc_len, desc_len;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003939 bool all_reg = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003940
3941 if (cmd->data_length < 8) {
Andy Grover6708bb22011-06-08 10:36:43 -07003942 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003943 " too small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003944 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003945 }
3946
Andy Grover49493142012-01-16 16:57:08 -08003947 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08003948 if (!buf)
3949 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003950
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04003951 buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3952 buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3953 buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3954 buf[3] = (dev->t10_pr.pr_generation & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003955
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08003956 spin_lock(&dev->dev_reservation_lock);
3957 if (dev->dev_pr_res_holder) {
3958 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3959
3960 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3961 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3962 all_reg = true;
3963 pr_res_type = pr_holder->pr_res_type;
3964 pr_res_scope = pr_holder->pr_res_scope;
3965 }
3966 }
3967 spin_unlock(&dev->dev_reservation_lock);
3968
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003969 spin_lock(&pr_tmpl->registration_lock);
3970 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3971 &pr_tmpl->registration_list, pr_reg_list) {
3972
3973 se_nacl = pr_reg->pr_reg_nacl;
3974 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3975 add_desc_len = 0;
3976
Joern Engel33940d02014-09-16 16:23:12 -04003977 atomic_inc_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003978 spin_unlock(&pr_tmpl->registration_lock);
3979 /*
3980 * Determine expected length of $FABRIC_MOD specific
3981 * TransportID full status descriptor..
3982 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02003983 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3984 &format_code);
3985 if (exp_desc_len < 0 ||
3986 exp_desc_len + add_len > cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003987 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003988 " out of buffer: %d\n", cmd->data_length);
3989 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04003990 atomic_dec_mb(&pr_reg->pr_res_holders);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003991 break;
3992 }
3993 /*
3994 * Set RESERVATION KEY
3995 */
3996 buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3997 buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3998 buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3999 buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4000 buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4001 buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4002 buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4003 buf[off++] = (pr_reg->pr_res_key & 0xff);
4004 off += 4; /* Skip Over Reserved area */
4005
4006 /*
4007 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4008 */
4009 if (pr_reg->pr_reg_all_tg_pt)
4010 buf[off] = 0x02;
4011 /*
4012 * The struct se_lun pointer will be present for the
4013 * reservation holder for PR_HOLDER bit.
4014 *
4015 * Also, if this registration is the reservation
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08004016 * holder or there is an All Registrants reservation
4017 * active, fill in SCOPE and TYPE in the next byte.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004018 */
4019 if (pr_reg->pr_res_holder) {
4020 buf[off++] |= 0x01;
4021 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4022 (pr_reg->pr_res_type & 0x0f);
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08004023 } else if (all_reg) {
4024 buf[off++] |= 0x01;
4025 buf[off++] = (pr_res_scope & 0xf0) |
4026 (pr_res_type & 0x0f);
4027 } else {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004028 off += 2;
Nicholas Bellingerd16ca7c2014-12-14 01:47:19 -08004029 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004030
4031 off += 4; /* Skip over reserved area */
4032 /*
4033 * From spc4r17 6.3.15:
4034 *
4035 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4036 * IDENTIFIER field contains the relative port identifier (see
4037 * 3.1.120) of the target port that is part of the I_T nexus
4038 * described by this full status descriptor. If the ALL_TG_PT
4039 * bit is set to one, the contents of the RELATIVE TARGET PORT
4040 * IDENTIFIER field are not defined by this standard.
4041 */
Andy Grover6708bb22011-06-08 10:36:43 -07004042 if (!pr_reg->pr_reg_all_tg_pt) {
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00004043 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004044
Nicholas Bellinger79dc9c92015-03-27 04:51:03 +00004045 buf[off++] = ((sep_rtpi >> 8) & 0xff);
4046 buf[off++] = (sep_rtpi & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004047 } else
Masanari Iida35d1efe2012-08-16 22:43:13 +09004048 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004049
Christoph Hellwig2650d712015-05-01 17:47:58 +02004050 buf[off+4] = se_tpg->proto_id;
4051
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004052 /*
Christoph Hellwig2650d712015-05-01 17:47:58 +02004053 * Now, have the $FABRIC_MOD fill in the transport ID.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004054 */
Christoph Hellwig2650d712015-05-01 17:47:58 +02004055 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4056 &format_code, &buf[off+4]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004057
4058 spin_lock(&pr_tmpl->registration_lock);
Joern Engel33940d02014-09-16 16:23:12 -04004059 atomic_dec_mb(&pr_reg->pr_res_holders);
Christoph Hellwig2650d712015-05-01 17:47:58 +02004060
4061 if (desc_len < 0)
4062 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004063 /*
4064 * Set the ADDITIONAL DESCRIPTOR LENGTH
4065 */
4066 buf[off++] = ((desc_len >> 24) & 0xff);
4067 buf[off++] = ((desc_len >> 16) & 0xff);
4068 buf[off++] = ((desc_len >> 8) & 0xff);
4069 buf[off++] = (desc_len & 0xff);
4070 /*
4071 * Size of full desctipor header minus TransportID
4072 * containing $FABRIC_MOD specific) initiator device/port
4073 * WWN information.
4074 *
4075 * See spc4r17 Section 6.13.5 Table 169
4076 */
4077 add_desc_len = (24 + desc_len);
4078
4079 off += desc_len;
4080 add_len += add_desc_len;
4081 }
4082 spin_unlock(&pr_tmpl->registration_lock);
4083 /*
4084 * Set ADDITIONAL_LENGTH
4085 */
4086 buf[4] = ((add_len >> 24) & 0xff);
4087 buf[5] = ((add_len >> 16) & 0xff);
4088 buf[6] = ((add_len >> 8) & 0xff);
4089 buf[7] = (add_len & 0xff);
4090
Andy Grover49493142012-01-16 16:57:08 -08004091 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +00004092
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004093 return 0;
4094}
4095
Christoph Hellwigde103c92012-11-06 12:24:09 -08004096sense_reason_t
4097target_scsi3_emulate_pr_in(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004098{
Christoph Hellwigde103c92012-11-06 12:24:09 -08004099 sense_reason_t ret;
Christoph Hellwige76a35d2011-11-03 17:50:42 -04004100
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004101 /*
4102 * Following spc2r20 5.5.1 Reservations overview:
4103 *
4104 * If a logical unit has been reserved by any RESERVE command and is
4105 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4106 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4107 * initiator or service action and shall terminate with a RESERVATION
4108 * CONFLICT status.
4109 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004110 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004111 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4112 " SPC-2 reservation is held, returning"
4113 " RESERVATION_CONFLICT\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -08004114 return TCM_RESERVATION_CONFLICT;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004115 }
4116
4117 switch (cmd->t_task_cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004118 case PRI_READ_KEYS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004119 ret = core_scsi3_pri_read_keys(cmd);
4120 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004121 case PRI_READ_RESERVATION:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004122 ret = core_scsi3_pri_read_reservation(cmd);
4123 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004124 case PRI_REPORT_CAPABILITIES:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004125 ret = core_scsi3_pri_report_capabilities(cmd);
4126 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004127 case PRI_READ_FULL_STATUS:
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004128 ret = core_scsi3_pri_read_full_status(cmd);
4129 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004130 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004131 pr_err("Unknown PERSISTENT_RESERVE_IN service"
Christoph Hellwig617c0e02011-11-03 17:50:41 -04004132 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
Christoph Hellwigde103c92012-11-06 12:24:09 -08004133 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004134 }
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004135
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04004136 if (!ret)
4137 target_complete_cmd(cmd, GOOD);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04004138 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004139}
4140
Christoph Hellwigde103c92012-11-06 12:24:09 -08004141sense_reason_t
4142target_check_reservation(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004143{
Christoph Hellwigd977f432012-10-10 17:37:15 -04004144 struct se_device *dev = cmd->se_dev;
Christoph Hellwigde103c92012-11-06 12:24:09 -08004145 sense_reason_t ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004146
Christoph Hellwigd977f432012-10-10 17:37:15 -04004147 if (!cmd->se_sess)
4148 return 0;
4149 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4150 return 0;
Andy Grovera3541702015-05-19 14:44:41 -07004151 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigd977f432012-10-10 17:37:15 -04004152 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004153
Christoph Hellwigd977f432012-10-10 17:37:15 -04004154 spin_lock(&dev->dev_reservation_lock);
4155 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4156 ret = target_scsi2_reservation_check(cmd);
4157 else
4158 ret = target_scsi3_pr_reservation_check(cmd);
4159 spin_unlock(&dev->dev_reservation_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04004160
Christoph Hellwigd977f432012-10-10 17:37:15 -04004161 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004162}