Merge tag 'nolibc-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau...
[linux-2.6-block.git] / drivers / target / target_core_pr.c
CommitLineData
c66ac9db
NB
1/*******************************************************************************
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 *
4c76251e 7 * (c) Copyright 2009-2013 Datera, Inc.
c66ac9db
NB
8 *
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
c66ac9db
NB
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/list.h>
5538d294 30#include <linux/vmalloc.h>
0e9b10a9 31#include <linux/file.h>
8dcf07be
BVA
32#include <linux/fcntl.h>
33#include <linux/fs.h>
ba929992 34#include <scsi/scsi_proto.h>
c66ac9db
NB
35#include <asm/unaligned.h>
36
37#include <target/target_core_base.h>
c4795fb2
CH
38#include <target/target_core_backend.h>
39#include <target/target_core_fabric.h>
c66ac9db 40
e26d99ae 41#include "target_core_internal.h"
c66ac9db
NB
42#include "target_core_pr.h"
43#include "target_core_ua.h"
44
45/*
46 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 */
48struct pr_transport_id_holder {
c66ac9db
NB
49 struct t10_pr_registration *dest_pr_reg;
50 struct se_portal_group *dest_tpg;
51 struct se_node_acl *dest_node_acl;
52 struct se_dev_entry *dest_se_deve;
53 struct list_head dest_list;
54};
55
d2843c17 56void core_pr_dump_initiator_port(
c66ac9db
NB
57 struct t10_pr_registration *pr_reg,
58 char *buf,
59 u32 size)
60{
24528f08 61 if (!pr_reg->isid_present_at_reg) {
d2843c17 62 buf[0] = '\0';
24528f08 63 return;
64 }
c66ac9db 65
d2843c17 66 snprintf(buf, size, ",i,0x%s", pr_reg->pr_reg_isid);
c66ac9db
NB
67}
68
33ce6a87
AG
69enum register_type {
70 REGISTER,
71 REGISTER_AND_IGNORE_EXISTING_KEY,
72 REGISTER_AND_MOVE,
73};
74
75enum preempt_type {
76 PREEMPT,
77 PREEMPT_AND_ABORT,
78};
79
c66ac9db 80static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
6c3c9baa 81 struct t10_pr_registration *, int, int);
c66ac9db 82
e673dc92
IT
83static int is_reservation_holder(
84 struct t10_pr_registration *pr_res_holder,
85 struct t10_pr_registration *pr_reg)
86{
87 int pr_res_type;
88
89 if (pr_res_holder) {
90 pr_res_type = pr_res_holder->pr_res_type;
91
92 return pr_res_holder == pr_reg ||
93 pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
94 pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG;
95 }
96 return 0;
97}
98
de103c93
CH
99static sense_reason_t
100target_scsi2_reservation_check(struct se_cmd *cmd)
c66ac9db 101{
d977f437
CH
102 struct se_device *dev = cmd->se_dev;
103 struct se_session *sess = cmd->se_sess;
104
105 switch (cmd->t_task_cdb[0]) {
c66ac9db
NB
106 case INQUIRY:
107 case RELEASE:
108 case RELEASE_10:
109 return 0;
110 default:
d977f437 111 break;
c66ac9db
NB
112 }
113
fae43461 114 if (!dev->reservation_holder || !sess)
c66ac9db
NB
115 return 0;
116
fae43461 117 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
de103c93 118 return TCM_RESERVATION_CONFLICT;
d977f437
CH
119
120 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
121 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
de103c93 122 return TCM_RESERVATION_CONFLICT;
c66ac9db 123 }
c66ac9db 124
d977f437 125 return 0;
c66ac9db
NB
126}
127
eacac00c
CH
128static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
129 struct se_node_acl *, struct se_session *);
130static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
131
087a03b3 132static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
eacac00c
CH
133{
134 struct se_session *se_sess = cmd->se_sess;
0fd97ccf 135 struct se_device *dev = cmd->se_dev;
eacac00c 136 struct t10_pr_registration *pr_reg;
0fd97ccf 137 struct t10_reservation *pr_tmpl = &dev->t10_pr;
eacac00c
CH
138 int conflict = 0;
139
eacac00c
CH
140 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
141 se_sess);
142 if (pr_reg) {
143 /*
144 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
145 * behavior
146 *
147 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
148 * status, but no reservation shall be established and the
149 * persistent reservation shall not be changed, if the command
150 * is received from a) and b) below.
151 *
152 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
153 * status, but the persistent reservation shall not be released,
154 * if the command is received from a) and b)
155 *
156 * a) An I_T nexus that is a persistent reservation holder; or
157 * b) An I_T nexus that is registered if a registrants only or
158 * all registrants type persistent reservation is present.
159 *
160 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
161 * RELEASE(6) command, or RELEASE(10) command shall be processed
162 * as defined in SPC-2.
163 */
164 if (pr_reg->pr_res_holder) {
165 core_scsi3_put_pr_reg(pr_reg);
087a03b3 166 return 1;
eacac00c
CH
167 }
168 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
169 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
170 (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
171 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
172 core_scsi3_put_pr_reg(pr_reg);
087a03b3 173 return 1;
eacac00c
CH
174 }
175 core_scsi3_put_pr_reg(pr_reg);
176 conflict = 1;
177 } else {
178 /*
179 * Following spc2r20 5.5.1 Reservations overview:
180 *
181 * If a logical unit has executed a PERSISTENT RESERVE OUT
182 * command with the REGISTER or the REGISTER AND IGNORE
183 * EXISTING KEY service action and is still registered by any
184 * initiator, all RESERVE commands and all RELEASE commands
185 * regardless of initiator shall conflict and shall terminate
186 * with a RESERVATION CONFLICT status.
187 */
188 spin_lock(&pr_tmpl->registration_lock);
189 conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
190 spin_unlock(&pr_tmpl->registration_lock);
191 }
192
193 if (conflict) {
194 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
195 " while active SPC-3 registrations exist,"
196 " returning RESERVATION_CONFLICT\n");
087a03b3 197 return -EBUSY;
eacac00c
CH
198 }
199
087a03b3 200 return 0;
eacac00c
CH
201}
202
fae43461
BVA
203void target_release_reservation(struct se_device *dev)
204{
205 dev->reservation_holder = NULL;
206 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
207 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
208 dev->dev_res_bin_isid = 0;
209 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
210 }
211}
212
de103c93
CH
213sense_reason_t
214target_scsi2_reservation_release(struct se_cmd *cmd)
c66ac9db
NB
215{
216 struct se_device *dev = cmd->se_dev;
217 struct se_session *sess = cmd->se_sess;
609234e3 218 struct se_portal_group *tpg;
de103c93 219 int rc;
c66ac9db 220
609234e3 221 if (!sess || !sess->se_tpg)
d29a5b6a 222 goto out;
087a03b3
NB
223 rc = target_check_scsi2_reservation_conflict(cmd);
224 if (rc == 1)
d29a5b6a 225 goto out;
de103c93
CH
226 if (rc < 0)
227 return TCM_RESERVATION_CONFLICT;
c66ac9db
NB
228
229 spin_lock(&dev->dev_reservation_lock);
fae43461 230 if (!dev->reservation_holder || !sess)
d29a5b6a
CH
231 goto out_unlock;
232
fae43461 233 if (dev->reservation_holder->se_node_acl != sess->se_node_acl)
d29a5b6a 234 goto out_unlock;
c66ac9db 235
edc318d9
BK
236 if (dev->dev_res_bin_isid != sess->sess_bin_isid)
237 goto out_unlock;
238
fae43461 239 target_release_reservation(dev);
609234e3 240 tpg = sess->se_tpg;
f2d30680
HR
241 pr_debug("SCSI-2 Released reservation for %s LUN: %llu ->"
242 " MAPPED LUN: %llu for %s\n",
30c7ca93 243 tpg->se_tpg_tfo->fabric_name,
29a05dee 244 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
c66ac9db 245 sess->se_node_acl->initiatorname);
c66ac9db 246
d29a5b6a
CH
247out_unlock:
248 spin_unlock(&dev->dev_reservation_lock);
249out:
de103c93
CH
250 target_complete_cmd(cmd, GOOD);
251 return 0;
c66ac9db
NB
252}
253
de103c93
CH
254sense_reason_t
255target_scsi2_reservation_reserve(struct se_cmd *cmd)
c66ac9db
NB
256{
257 struct se_device *dev = cmd->se_dev;
258 struct se_session *sess = cmd->se_sess;
609234e3 259 struct se_portal_group *tpg;
de103c93
CH
260 sense_reason_t ret = 0;
261 int rc;
c66ac9db 262
a1d8b49a
AG
263 if ((cmd->t_task_cdb[1] & 0x01) &&
264 (cmd->t_task_cdb[1] & 0x02)) {
a3960ced 265 pr_err("LongIO and Obsolete Bits set, returning ILLEGAL_REQUEST\n");
de103c93 266 return TCM_UNSUPPORTED_SCSI_OPCODE;
c66ac9db
NB
267 }
268 /*
269 * This is currently the case for target_core_mod passthrough struct se_cmd
270 * ops
271 */
609234e3 272 if (!sess || !sess->se_tpg)
d29a5b6a 273 goto out;
087a03b3
NB
274 rc = target_check_scsi2_reservation_conflict(cmd);
275 if (rc == 1)
d29a5b6a 276 goto out;
c66ac9db 277
de103c93
CH
278 if (rc < 0)
279 return TCM_RESERVATION_CONFLICT;
280
609234e3 281 tpg = sess->se_tpg;
c66ac9db 282 spin_lock(&dev->dev_reservation_lock);
fae43461
BVA
283 if (dev->reservation_holder &&
284 dev->reservation_holder->se_node_acl != sess->se_node_acl) {
6708bb27 285 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
30c7ca93 286 tpg->se_tpg_tfo->fabric_name);
f2d30680 287 pr_err("Original reserver LUN: %llu %s\n",
e3d6f909 288 cmd->se_lun->unpacked_lun,
fae43461 289 dev->reservation_holder->se_node_acl->initiatorname);
f2d30680 290 pr_err("Current attempt - LUN: %llu -> MAPPED LUN: %llu"
e3d6f909 291 " from %s \n", cmd->se_lun->unpacked_lun,
29a05dee 292 cmd->orig_fe_lun,
c66ac9db 293 sess->se_node_acl->initiatorname);
de103c93 294 ret = TCM_RESERVATION_CONFLICT;
d29a5b6a 295 goto out_unlock;
c66ac9db
NB
296 }
297
fae43461 298 dev->reservation_holder = sess;
0fd97ccf 299 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
c66ac9db
NB
300 if (sess->sess_bin_isid != 0) {
301 dev->dev_res_bin_isid = sess->sess_bin_isid;
0fd97ccf 302 dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
c66ac9db 303 }
f2d30680 304 pr_debug("SCSI-2 Reserved %s LUN: %llu -> MAPPED LUN: %llu"
30c7ca93 305 " for %s\n", tpg->se_tpg_tfo->fabric_name,
29a05dee 306 cmd->se_lun->unpacked_lun, cmd->orig_fe_lun,
c66ac9db 307 sess->se_node_acl->initiatorname);
c66ac9db 308
d29a5b6a
CH
309out_unlock:
310 spin_unlock(&dev->dev_reservation_lock);
311out:
6bb35e00
CH
312 if (!ret)
313 target_complete_cmd(cmd, GOOD);
d29a5b6a 314 return ret;
c66ac9db
NB
315}
316
c66ac9db
NB
317
318/*
319 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
320 *
321 * This function is called by those initiator ports who are *NOT*
322 * the active PR reservation holder when a reservation is present.
323 */
9fcb57f3
CH
324static int core_scsi3_pr_seq_non_holder(struct se_cmd *cmd, u32 pr_reg_type,
325 bool isid_mismatch)
c66ac9db 326{
d977f437 327 unsigned char *cdb = cmd->t_task_cdb;
e3d6f909 328 struct se_session *se_sess = cmd->se_sess;
29a05dee 329 struct se_node_acl *nacl = se_sess->se_node_acl;
9fcb57f3 330 int other_cdb = 0;
c66ac9db
NB
331 int registered_nexus = 0, ret = 1; /* Conflict by default */
332 int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
333 int we = 0; /* Write Exclusive */
334 int legacy = 0; /* Act like a legacy device and return
335 * RESERVATION CONFLICT on some CDBs */
c66ac9db 336
9fcb57f3
CH
337 if (isid_mismatch) {
338 registered_nexus = 0;
339 } else {
340 struct se_dev_entry *se_deve;
341
342 rcu_read_lock();
343 se_deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
344 if (se_deve)
345 registered_nexus = test_bit(DEF_PR_REG_ACTIVE,
346 &se_deve->deve_flags);
347 rcu_read_unlock();
348 }
c66ac9db
NB
349
350 switch (pr_reg_type) {
351 case PR_TYPE_WRITE_EXCLUSIVE:
352 we = 1;
353 case PR_TYPE_EXCLUSIVE_ACCESS:
354 /*
355 * Some commands are only allowed for the persistent reservation
356 * holder.
357 */
c66ac9db
NB
358 break;
359 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
360 we = 1;
d7e595dd 361 /* fall through */
c66ac9db
NB
362 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
363 /*
364 * Some commands are only allowed for registered I_T Nexuses.
365 */
366 reg_only = 1;
c66ac9db
NB
367 break;
368 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
369 we = 1;
d7e595dd 370 /* fall through */
c66ac9db
NB
371 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
372 /*
373 * Each registered I_T Nexus is a reservation holder.
374 */
375 all_reg = 1;
c66ac9db
NB
376 break;
377 default:
e3d6f909 378 return -EINVAL;
c66ac9db
NB
379 }
380 /*
381 * Referenced from spc4r17 table 45 for *NON* PR holder access
382 */
383 switch (cdb[0]) {
384 case SECURITY_PROTOCOL_IN:
385 if (registered_nexus)
386 return 0;
387 ret = (we) ? 0 : 1;
388 break;
389 case MODE_SENSE:
390 case MODE_SENSE_10:
391 case READ_ATTRIBUTE:
392 case READ_BUFFER:
393 case RECEIVE_DIAGNOSTIC:
394 if (legacy) {
395 ret = 1;
396 break;
397 }
398 if (registered_nexus) {
399 ret = 0;
400 break;
401 }
402 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
403 break;
404 case PERSISTENT_RESERVE_OUT:
405 /*
406 * This follows PERSISTENT_RESERVE_OUT service actions that
407 * are allowed in the presence of various reservations.
408 * See spc4r17, table 46
409 */
410 switch (cdb[1] & 0x1f) {
411 case PRO_CLEAR:
412 case PRO_PREEMPT:
413 case PRO_PREEMPT_AND_ABORT:
414 ret = (registered_nexus) ? 0 : 1;
415 break;
416 case PRO_REGISTER:
417 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
418 ret = 0;
419 break;
420 case PRO_REGISTER_AND_MOVE:
421 case PRO_RESERVE:
422 ret = 1;
423 break;
424 case PRO_RELEASE:
425 ret = (registered_nexus) ? 0 : 1;
426 break;
427 default:
6708bb27 428 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
c66ac9db 429 " action: 0x%02x\n", cdb[1] & 0x1f);
e3d6f909 430 return -EINVAL;
c66ac9db
NB
431 }
432 break;
433 case RELEASE:
434 case RELEASE_10:
eacac00c 435 /* Handled by CRH=1 in target_scsi2_reservation_release() */
c66ac9db
NB
436 ret = 0;
437 break;
438 case RESERVE:
439 case RESERVE_10:
eacac00c 440 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
c66ac9db
NB
441 ret = 0;
442 break;
443 case TEST_UNIT_READY:
444 ret = (legacy) ? 1 : 0; /* Conflict for legacy */
445 break;
446 case MAINTENANCE_IN:
447 switch (cdb[1] & 0x1f) {
448 case MI_MANAGEMENT_PROTOCOL_IN:
449 if (registered_nexus) {
450 ret = 0;
451 break;
452 }
453 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
454 break;
455 case MI_REPORT_SUPPORTED_OPERATION_CODES:
456 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
457 if (legacy) {
458 ret = 1;
459 break;
460 }
461 if (registered_nexus) {
462 ret = 0;
463 break;
464 }
465 ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
466 break;
467 case MI_REPORT_ALIASES:
468 case MI_REPORT_IDENTIFYING_INFORMATION:
469 case MI_REPORT_PRIORITY:
470 case MI_REPORT_TARGET_PGS:
471 case MI_REPORT_TIMESTAMP:
472 ret = 0; /* Allowed */
473 break;
474 default:
6708bb27 475 pr_err("Unknown MI Service Action: 0x%02x\n",
c66ac9db 476 (cdb[1] & 0x1f));
e3d6f909 477 return -EINVAL;
c66ac9db
NB
478 }
479 break;
480 case ACCESS_CONTROL_IN:
481 case ACCESS_CONTROL_OUT:
482 case INQUIRY:
483 case LOG_SENSE:
85686f69 484 case SERVICE_ACTION_IN_12:
c66ac9db
NB
485 case REPORT_LUNS:
486 case REQUEST_SENSE:
6816966a 487 case PERSISTENT_RESERVE_IN:
c66ac9db
NB
488 ret = 0; /*/ Allowed CDBs */
489 break;
490 default:
491 other_cdb = 1;
492 break;
493 }
494 /*
25985edc 495 * Case where the CDB is explicitly allowed in the above switch
c66ac9db
NB
496 * statement.
497 */
6708bb27 498 if (!ret && !other_cdb) {
125d0119 499 pr_debug("Allowing explicit CDB: 0x%02x for %s"
c66ac9db
NB
500 " reservation holder\n", cdb[0],
501 core_scsi3_pr_dump_type(pr_reg_type));
8b1e1244 502
c66ac9db
NB
503 return ret;
504 }
505 /*
506 * Check if write exclusive initiator ports *NOT* holding the
507 * WRITE_EXCLUSIVE_* reservation.
508 */
ee1b1b9c 509 if (we && !registered_nexus) {
c66ac9db
NB
510 if (cmd->data_direction == DMA_TO_DEVICE) {
511 /*
512 * Conflict for write exclusive
513 */
6708bb27 514 pr_debug("%s Conflict for unregistered nexus"
c66ac9db
NB
515 " %s CDB: 0x%02x to %s reservation\n",
516 transport_dump_cmd_direction(cmd),
517 se_sess->se_node_acl->initiatorname, cdb[0],
518 core_scsi3_pr_dump_type(pr_reg_type));
519 return 1;
520 } else {
521 /*
522 * Allow non WRITE CDBs for all Write Exclusive
523 * PR TYPEs to pass for registered and
524 * non-registered_nexuxes NOT holding the reservation.
525 *
526 * We only make noise for the unregisterd nexuses,
527 * as we expect registered non-reservation holding
528 * nexuses to issue CDBs.
529 */
8b1e1244 530
6708bb27 531 if (!registered_nexus) {
125d0119 532 pr_debug("Allowing implicit CDB: 0x%02x"
c66ac9db
NB
533 " for %s reservation on unregistered"
534 " nexus\n", cdb[0],
535 core_scsi3_pr_dump_type(pr_reg_type));
536 }
8b1e1244 537
c66ac9db
NB
538 return 0;
539 }
540 } else if ((reg_only) || (all_reg)) {
541 if (registered_nexus) {
542 /*
543 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
544 * allow commands from registered nexuses.
545 */
8b1e1244 546
125d0119 547 pr_debug("Allowing implicit CDB: 0x%02x for %s"
c66ac9db
NB
548 " reservation\n", cdb[0],
549 core_scsi3_pr_dump_type(pr_reg_type));
8b1e1244 550
c66ac9db
NB
551 return 0;
552 }
1ecc7586
LD
553 } else if (we && registered_nexus) {
554 /*
555 * Reads are allowed for Write Exclusive locks
556 * from all registrants.
557 */
558 if (cmd->data_direction == DMA_FROM_DEVICE) {
559 pr_debug("Allowing READ CDB: 0x%02x for %s"
560 " reservation\n", cdb[0],
561 core_scsi3_pr_dump_type(pr_reg_type));
562
563 return 0;
564 }
c66ac9db 565 }
6708bb27 566 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
c66ac9db
NB
567 " for %s reservation\n", transport_dump_cmd_direction(cmd),
568 (registered_nexus) ? "" : "un",
569 se_sess->se_node_acl->initiatorname, cdb[0],
570 core_scsi3_pr_dump_type(pr_reg_type));
571
572 return 1; /* Conflict by default */
573}
574
de103c93
CH
575static sense_reason_t
576target_scsi3_pr_reservation_check(struct se_cmd *cmd)
d977f437
CH
577{
578 struct se_device *dev = cmd->se_dev;
579 struct se_session *sess = cmd->se_sess;
580 u32 pr_reg_type;
9fcb57f3 581 bool isid_mismatch = false;
d977f437
CH
582
583 if (!dev->dev_pr_res_holder)
584 return 0;
585
586 pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
587 cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
588 if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
589 goto check_nonholder;
590
591 if (dev->dev_pr_res_holder->isid_present_at_reg) {
592 if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
593 sess->sess_bin_isid) {
9fcb57f3 594 isid_mismatch = true;
d977f437
CH
595 goto check_nonholder;
596 }
597 }
598
599 return 0;
600
601check_nonholder:
9fcb57f3 602 if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type, isid_mismatch))
de103c93 603 return TCM_RESERVATION_CONFLICT;
d977f437
CH
604 return 0;
605}
606
c66ac9db
NB
607static u32 core_scsi3_pr_generation(struct se_device *dev)
608{
c66ac9db 609 u32 prg;
0fd97ccf 610
c66ac9db
NB
611 /*
612 * PRGeneration field shall contain the value of a 32-bit wrapping
613 * counter mainted by the device server.
614 *
615 * Note that this is done regardless of Active Persist across
616 * Target PowerLoss (APTPL)
617 *
618 * See spc4r17 section 6.3.12 READ_KEYS service action
619 */
620 spin_lock(&dev->dev_reservation_lock);
0fd97ccf 621 prg = dev->t10_pr.pr_generation++;
c66ac9db
NB
622 spin_unlock(&dev->dev_reservation_lock);
623
624 return prg;
625}
626
c66ac9db
NB
627static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
628 struct se_device *dev,
629 struct se_node_acl *nacl,
79dc9c9e 630 struct se_lun *lun,
3ccd6e83 631 struct se_dev_entry *dest_deve,
f2d30680 632 u64 mapped_lun,
c66ac9db
NB
633 unsigned char *isid,
634 u64 sa_res_key,
635 int all_tg_pt,
636 int aptpl)
637{
c66ac9db
NB
638 struct t10_pr_registration *pr_reg;
639
640 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
6708bb27
AG
641 if (!pr_reg) {
642 pr_err("Unable to allocate struct t10_pr_registration\n");
c66ac9db
NB
643 return NULL;
644 }
645
c66ac9db
NB
646 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
647 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
648 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
649 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
650 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
651 atomic_set(&pr_reg->pr_res_holders, 0);
652 pr_reg->pr_reg_nacl = nacl;
3ccd6e83
NB
653 /*
654 * For destination registrations for ALL_TG_PT=1 and SPEC_I_PT=1,
655 * the se_dev_entry->pr_ref will have been already obtained by
656 * core_get_se_deve_from_rtpi() or __core_scsi3_alloc_registration().
657 *
658 * Otherwise, locate se_dev_entry now and obtain a reference until
659 * registration completes in __core_scsi3_add_registration().
660 */
661 if (dest_deve) {
662 pr_reg->pr_reg_deve = dest_deve;
663 } else {
664 rcu_read_lock();
665 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
666 if (!pr_reg->pr_reg_deve) {
667 rcu_read_unlock();
668 pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
669 nacl->initiatorname, mapped_lun);
670 kmem_cache_free(t10_pr_reg_cache, pr_reg);
671 return NULL;
672 }
673 kref_get(&pr_reg->pr_reg_deve->pr_kref);
674 rcu_read_unlock();
675 }
79dc9c9e
NB
676 pr_reg->pr_res_mapped_lun = mapped_lun;
677 pr_reg->pr_aptpl_target_lun = lun->unpacked_lun;
adf653f9 678 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
c66ac9db
NB
679 pr_reg->pr_res_key = sa_res_key;
680 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
681 pr_reg->pr_reg_aptpl = aptpl;
c66ac9db
NB
682 /*
683 * If an ISID value for this SCSI Initiator Port exists,
684 * save it to the registration now.
685 */
686 if (isid != NULL) {
687 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
688 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
689 pr_reg->isid_present_at_reg = 1;
690 }
691
692 return pr_reg;
693}
694
695static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
696static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
697
698/*
699 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
700 * modes.
701 */
702static struct t10_pr_registration *__core_scsi3_alloc_registration(
703 struct se_device *dev,
704 struct se_node_acl *nacl,
79dc9c9e 705 struct se_lun *lun,
c66ac9db 706 struct se_dev_entry *deve,
f2d30680 707 u64 mapped_lun,
c66ac9db
NB
708 unsigned char *isid,
709 u64 sa_res_key,
710 int all_tg_pt,
711 int aptpl)
712{
713 struct se_dev_entry *deve_tmp;
714 struct se_node_acl *nacl_tmp;
79dc9c9e 715 struct se_lun_acl *lacl_tmp;
adf653f9 716 struct se_lun *lun_tmp, *next, *dest_lun;
9ac8928e 717 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
c66ac9db
NB
718 struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
719 int ret;
720 /*
721 * Create a registration for the I_T Nexus upon which the
722 * PROUT REGISTER was received.
723 */
79dc9c9e
NB
724 pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, lun, deve, mapped_lun,
725 isid, sa_res_key, all_tg_pt,
726 aptpl);
6708bb27 727 if (!pr_reg)
c66ac9db
NB
728 return NULL;
729 /*
730 * Return pointer to pr_reg for ALL_TG_PT=0
731 */
6708bb27 732 if (!all_tg_pt)
c66ac9db
NB
733 return pr_reg;
734 /*
735 * Create list of matching SCSI Initiator Port registrations
736 * for ALL_TG_PT=1
737 */
738 spin_lock(&dev->se_port_lock);
adf653f9 739 list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
9e37d042
NB
740 if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
741 continue;
c66ac9db
NB
742 spin_unlock(&dev->se_port_lock);
743
1adff1b3 744 spin_lock(&lun_tmp->lun_deve_lock);
adf653f9 745 list_for_each_entry(deve_tmp, &lun_tmp->lun_deve_list, lun_link) {
c66ac9db
NB
746 /*
747 * This pointer will be NULL for demo mode MappedLUNs
125d0119 748 * that have not been make explicit via a ConfigFS
c66ac9db
NB
749 * MappedLUN group for the SCSI Initiator Node ACL.
750 */
6708bb27 751 if (!deve_tmp->se_lun_acl)
c66ac9db
NB
752 continue;
753
79dc9c9e 754 lacl_tmp = rcu_dereference_check(deve_tmp->se_lun_acl,
adf653f9 755 lockdep_is_held(&lun_tmp->lun_deve_lock));
79dc9c9e 756 nacl_tmp = lacl_tmp->se_lun_nacl;
c66ac9db
NB
757 /*
758 * Skip the matching struct se_node_acl that is allocated
759 * above..
760 */
761 if (nacl == nacl_tmp)
762 continue;
763 /*
764 * Only perform PR registrations for target ports on
765 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
766 * arrived.
767 */
768 if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
769 continue;
770 /*
771 * Look for a matching Initiator Node ACL in ASCII format
772 */
773 if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
774 continue;
775
29a05dee 776 kref_get(&deve_tmp->pr_kref);
1adff1b3 777 spin_unlock(&lun_tmp->lun_deve_lock);
c66ac9db
NB
778 /*
779 * Grab a configfs group dependency that is released
780 * for the exception path at label out: below, or upon
781 * completion of adding ALL_TG_PT=1 registrations in
782 * __core_scsi3_add_registration()
783 */
784 ret = core_scsi3_lunacl_depend_item(deve_tmp);
785 if (ret < 0) {
6708bb27 786 pr_err("core_scsi3_lunacl_depend"
c66ac9db 787 "_item() failed\n");
9e37d042 788 percpu_ref_put(&lun_tmp->lun_ref);
29a05dee 789 kref_put(&deve_tmp->pr_kref, target_pr_kref_release);
c66ac9db
NB
790 goto out;
791 }
792 /*
793 * Located a matching SCSI Initiator Port on a different
794 * port, allocate the pr_reg_atp and attach it to the
795 * pr_reg->pr_reg_atp_list that will be processed once
796 * the original *pr_reg is processed in
797 * __core_scsi3_add_registration()
798 */
adf653f9 799 dest_lun = rcu_dereference_check(deve_tmp->se_lun,
2c935bc5 800 kref_read(&deve_tmp->pr_kref) != 0);
79dc9c9e 801
c66ac9db 802 pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
adf653f9 803 nacl_tmp, dest_lun, deve_tmp,
79dc9c9e 804 deve_tmp->mapped_lun, NULL,
c66ac9db 805 sa_res_key, all_tg_pt, aptpl);
6708bb27 806 if (!pr_reg_atp) {
9e37d042 807 percpu_ref_put(&lun_tmp->lun_ref);
c66ac9db
NB
808 core_scsi3_lunacl_undepend_item(deve_tmp);
809 goto out;
810 }
811
812 list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
813 &pr_reg->pr_reg_atp_list);
1adff1b3 814 spin_lock(&lun_tmp->lun_deve_lock);
c66ac9db 815 }
1adff1b3 816 spin_unlock(&lun_tmp->lun_deve_lock);
c66ac9db
NB
817
818 spin_lock(&dev->se_port_lock);
9e37d042 819 percpu_ref_put(&lun_tmp->lun_ref);
c66ac9db
NB
820 }
821 spin_unlock(&dev->se_port_lock);
822
823 return pr_reg;
824out:
825 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
826 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
827 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
828 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
829 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
830 }
831 kmem_cache_free(t10_pr_reg_cache, pr_reg);
832 return NULL;
833}
834
835int core_scsi3_alloc_aptpl_registration(
e3d6f909 836 struct t10_reservation *pr_tmpl,
c66ac9db
NB
837 u64 sa_res_key,
838 unsigned char *i_port,
839 unsigned char *isid,
f2d30680 840 u64 mapped_lun,
c66ac9db
NB
841 unsigned char *t_port,
842 u16 tpgt,
f2d30680 843 u64 target_lun,
c66ac9db
NB
844 int res_holder,
845 int all_tg_pt,
846 u8 type)
847{
848 struct t10_pr_registration *pr_reg;
849
6708bb27
AG
850 if (!i_port || !t_port || !sa_res_key) {
851 pr_err("Illegal parameters for APTPL registration\n");
e3d6f909 852 return -EINVAL;
c66ac9db
NB
853 }
854
855 pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
6708bb27
AG
856 if (!pr_reg) {
857 pr_err("Unable to allocate struct t10_pr_registration\n");
e3d6f909 858 return -ENOMEM;
c66ac9db 859 }
c66ac9db
NB
860
861 INIT_LIST_HEAD(&pr_reg->pr_reg_list);
862 INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
863 INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
864 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
865 INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
866 atomic_set(&pr_reg->pr_res_holders, 0);
867 pr_reg->pr_reg_nacl = NULL;
868 pr_reg->pr_reg_deve = NULL;
869 pr_reg->pr_res_mapped_lun = mapped_lun;
870 pr_reg->pr_aptpl_target_lun = target_lun;
871 pr_reg->pr_res_key = sa_res_key;
872 pr_reg->pr_reg_all_tg_pt = all_tg_pt;
873 pr_reg->pr_reg_aptpl = 1;
c66ac9db
NB
874 pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
875 pr_reg->pr_res_type = type;
876 /*
877 * If an ISID value had been saved in APTPL metadata for this
878 * SCSI Initiator Port, restore it now.
879 */
880 if (isid != NULL) {
881 pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
882 snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
883 pr_reg->isid_present_at_reg = 1;
884 }
885 /*
886 * Copy the i_port and t_port information from caller.
887 */
888 snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
889 snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
890 pr_reg->pr_reg_tpgt = tpgt;
891 /*
892 * Set pr_res_holder from caller, the pr_reg who is the reservation
893 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
894 * the Initiator Node LUN ACL from the fabric module is created for
895 * this registration.
896 */
897 pr_reg->pr_res_holder = res_holder;
898
899 list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
6708bb27 900 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
c66ac9db
NB
901 " metadata\n", (res_holder) ? "+reservation" : "");
902 return 0;
903}
904
905static void core_scsi3_aptpl_reserve(
906 struct se_device *dev,
907 struct se_portal_group *tpg,
908 struct se_node_acl *node_acl,
909 struct t10_pr_registration *pr_reg)
910{
911 char i_buf[PR_REG_ISID_ID_LEN];
c66ac9db
NB
912
913 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
d2843c17 914 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db
NB
915
916 spin_lock(&dev->dev_reservation_lock);
917 dev->dev_pr_res_holder = pr_reg;
918 spin_unlock(&dev->dev_reservation_lock);
919
6708bb27 920 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
c66ac9db 921 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
30c7ca93 922 tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
923 core_scsi3_pr_dump_type(pr_reg->pr_res_type),
924 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
6708bb27 925 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
30c7ca93 926 tpg->se_tpg_tfo->fabric_name, node_acl->initiatorname,
d2843c17 927 i_buf);
c66ac9db
NB
928}
929
930static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
33ce6a87 931 struct t10_pr_registration *, enum register_type, int);
c66ac9db
NB
932
933static int __core_scsi3_check_aptpl_registration(
934 struct se_device *dev,
935 struct se_portal_group *tpg,
936 struct se_lun *lun,
f2d30680 937 u64 target_lun,
c66ac9db 938 struct se_node_acl *nacl,
f2d30680 939 u64 mapped_lun)
c66ac9db
NB
940{
941 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
0fd97ccf 942 struct t10_reservation *pr_tmpl = &dev->t10_pr;
c66ac9db
NB
943 unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
944 unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
945 u16 tpgt;
946
947 memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
948 memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
949 /*
950 * Copy Initiator Port information from struct se_node_acl
951 */
952 snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
953 snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
e3d6f909
AG
954 tpg->se_tpg_tfo->tpg_get_wwn(tpg));
955 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
c66ac9db
NB
956 /*
957 * Look for the matching registrations+reservation from those
958 * created from APTPL metadata. Note that multiple registrations
959 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
960 * TransportIDs.
961 */
962 spin_lock(&pr_tmpl->aptpl_reg_lock);
963 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
964 pr_reg_aptpl_list) {
92404e60 965
6708bb27 966 if (!strcmp(pr_reg->pr_iport, i_port) &&
29a05dee 967 (pr_reg->pr_res_mapped_lun == mapped_lun) &&
c66ac9db
NB
968 !(strcmp(pr_reg->pr_tport, t_port)) &&
969 (pr_reg->pr_reg_tpgt == tpgt) &&
970 (pr_reg->pr_aptpl_target_lun == target_lun)) {
3ccd6e83
NB
971 /*
972 * Obtain the ->pr_reg_deve pointer + reference, that
973 * is released by __core_scsi3_add_registration() below.
974 */
975 rcu_read_lock();
976 pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
977 if (!pr_reg->pr_reg_deve) {
978 pr_err("Unable to locate PR APTPL %s mapped_lun:"
979 " %llu\n", nacl->initiatorname, mapped_lun);
980 rcu_read_unlock();
981 continue;
982 }
983 kref_get(&pr_reg->pr_reg_deve->pr_kref);
984 rcu_read_unlock();
c66ac9db
NB
985
986 pr_reg->pr_reg_nacl = nacl;
adf653f9 987 pr_reg->tg_pt_sep_rtpi = lun->lun_rtpi;
c66ac9db
NB
988 list_del(&pr_reg->pr_reg_aptpl_list);
989 spin_unlock(&pr_tmpl->aptpl_reg_lock);
990 /*
991 * At this point all of the pointers in *pr_reg will
992 * be setup, so go ahead and add the registration.
993 */
c66ac9db
NB
994 __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
995 /*
996 * If this registration is the reservation holder,
997 * make that happen now..
998 */
999 if (pr_reg->pr_res_holder)
1000 core_scsi3_aptpl_reserve(dev, tpg,
1001 nacl, pr_reg);
1002 /*
1003 * Reenable pr_aptpl_active to accept new metadata
1004 * updates once the SCSI device is active again..
1005 */
1006 spin_lock(&pr_tmpl->aptpl_reg_lock);
1007 pr_tmpl->pr_aptpl_active = 1;
1008 }
1009 }
1010 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1011
1012 return 0;
1013}
1014
1015int core_scsi3_check_aptpl_registration(
1016 struct se_device *dev,
1017 struct se_portal_group *tpg,
1018 struct se_lun *lun,
e2480563 1019 struct se_node_acl *nacl,
f2d30680 1020 u64 mapped_lun)
c66ac9db 1021{
d977f437 1022 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
c66ac9db
NB
1023 return 0;
1024
1025 return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
29a05dee
NB
1026 lun->unpacked_lun, nacl,
1027 mapped_lun);
c66ac9db
NB
1028}
1029
1030static void __core_scsi3_dump_registration(
9ac8928e 1031 const struct target_core_fabric_ops *tfo,
c66ac9db
NB
1032 struct se_device *dev,
1033 struct se_node_acl *nacl,
1034 struct t10_pr_registration *pr_reg,
33ce6a87 1035 enum register_type register_type)
c66ac9db
NB
1036{
1037 struct se_portal_group *se_tpg = nacl->se_tpg;
1038 char i_buf[PR_REG_ISID_ID_LEN];
c66ac9db
NB
1039
1040 memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
d2843c17 1041 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db 1042
6708bb27 1043 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
30c7ca93 1044 " Node: %s%s\n", tfo->fabric_name, (register_type == REGISTER_AND_MOVE) ?
33ce6a87 1045 "_AND_MOVE" : (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ?
c66ac9db 1046 "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
d2843c17 1047 i_buf);
6708bb27 1048 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
30c7ca93 1049 tfo->fabric_name, tfo->tpg_get_wwn(se_tpg),
c66ac9db 1050 tfo->tpg_get_tag(se_tpg));
6708bb27 1051 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
30c7ca93 1052 " Port(s)\n", tfo->fabric_name,
c66ac9db 1053 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
e3d6f909 1054 dev->transport->name);
6708bb27 1055 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
30c7ca93 1056 " 0x%08x APTPL: %d\n", tfo->fabric_name,
c66ac9db
NB
1057 pr_reg->pr_res_key, pr_reg->pr_res_generation,
1058 pr_reg->pr_reg_aptpl);
1059}
1060
c66ac9db
NB
1061static void __core_scsi3_add_registration(
1062 struct se_device *dev,
1063 struct se_node_acl *nacl,
1064 struct t10_pr_registration *pr_reg,
33ce6a87 1065 enum register_type register_type,
c66ac9db
NB
1066 int register_move)
1067{
9ac8928e 1068 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
c66ac9db 1069 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
0fd97ccf 1070 struct t10_reservation *pr_tmpl = &dev->t10_pr;
80bfdfa9 1071 struct se_dev_entry *deve;
c66ac9db
NB
1072
1073 /*
1074 * Increment PRgeneration counter for struct se_device upon a successful
1075 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1076 *
1077 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1078 * action, the struct se_device->dev_reservation_lock will already be held,
1079 * so we do not call core_scsi3_pr_generation() which grabs the lock
1080 * for the REGISTER.
1081 */
1082 pr_reg->pr_res_generation = (register_move) ?
0fd97ccf 1083 dev->t10_pr.pr_generation++ :
c66ac9db
NB
1084 core_scsi3_pr_generation(dev);
1085
1086 spin_lock(&pr_tmpl->registration_lock);
1087 list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
c66ac9db
NB
1088
1089 __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1090 spin_unlock(&pr_tmpl->registration_lock);
1091 /*
1092 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1093 */
6708bb27 1094 if (!pr_reg->pr_reg_all_tg_pt || register_move)
3ccd6e83 1095 goto out;
c66ac9db
NB
1096 /*
1097 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1098 * allocated in __core_scsi3_alloc_registration()
1099 */
1100 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1101 &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
80bfdfa9
NB
1102 struct se_node_acl *nacl_tmp = pr_reg_tmp->pr_reg_nacl;
1103
c66ac9db
NB
1104 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1105
1106 pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1107
1108 spin_lock(&pr_tmpl->registration_lock);
1109 list_add_tail(&pr_reg_tmp->pr_reg_list,
1110 &pr_tmpl->registration_list);
c66ac9db 1111
80bfdfa9
NB
1112 __core_scsi3_dump_registration(tfo, dev, nacl_tmp, pr_reg_tmp,
1113 register_type);
c66ac9db 1114 spin_unlock(&pr_tmpl->registration_lock);
3ccd6e83
NB
1115 /*
1116 * Drop configfs group dependency reference and deve->pr_kref
1117 * obtained from __core_scsi3_alloc_registration() code.
1118 */
80bfdfa9
NB
1119 rcu_read_lock();
1120 deve = pr_reg_tmp->pr_reg_deve;
3ccd6e83 1121 if (deve) {
80bfdfa9 1122 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
3ccd6e83
NB
1123 core_scsi3_lunacl_undepend_item(deve);
1124 pr_reg_tmp->pr_reg_deve = NULL;
1125 }
80bfdfa9 1126 rcu_read_unlock();
c66ac9db 1127 }
3ccd6e83
NB
1128out:
1129 /*
1130 * Drop deve->pr_kref obtained in __core_scsi3_do_alloc_registration()
1131 */
1132 rcu_read_lock();
1133 deve = pr_reg->pr_reg_deve;
1134 if (deve) {
1135 set_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1136 kref_put(&deve->pr_kref, target_pr_kref_release);
1137 pr_reg->pr_reg_deve = NULL;
1138 }
1139 rcu_read_unlock();
c66ac9db
NB
1140}
1141
1142static int core_scsi3_alloc_registration(
1143 struct se_device *dev,
1144 struct se_node_acl *nacl,
79dc9c9e 1145 struct se_lun *lun,
c66ac9db 1146 struct se_dev_entry *deve,
f2d30680 1147 u64 mapped_lun,
c66ac9db
NB
1148 unsigned char *isid,
1149 u64 sa_res_key,
1150 int all_tg_pt,
1151 int aptpl,
33ce6a87 1152 enum register_type register_type,
c66ac9db
NB
1153 int register_move)
1154{
1155 struct t10_pr_registration *pr_reg;
1156
79dc9c9e
NB
1157 pr_reg = __core_scsi3_alloc_registration(dev, nacl, lun, deve, mapped_lun,
1158 isid, sa_res_key, all_tg_pt,
1159 aptpl);
6708bb27 1160 if (!pr_reg)
e3d6f909 1161 return -EPERM;
c66ac9db
NB
1162
1163 __core_scsi3_add_registration(dev, nacl, pr_reg,
1164 register_type, register_move);
1165 return 0;
1166}
1167
1168static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1169 struct se_device *dev,
1170 struct se_node_acl *nacl,
1171 unsigned char *isid)
1172{
0fd97ccf 1173 struct t10_reservation *pr_tmpl = &dev->t10_pr;
c66ac9db
NB
1174 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1175 struct se_portal_group *tpg;
1176
1177 spin_lock(&pr_tmpl->registration_lock);
1178 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1179 &pr_tmpl->registration_list, pr_reg_list) {
1180 /*
1181 * First look for a matching struct se_node_acl
1182 */
1183 if (pr_reg->pr_reg_nacl != nacl)
1184 continue;
1185
1186 tpg = pr_reg->pr_reg_nacl->se_tpg;
1187 /*
1188 * If this registration does NOT contain a fabric provided
1189 * ISID, then we have found a match.
1190 */
6708bb27 1191 if (!pr_reg->isid_present_at_reg) {
c66ac9db
NB
1192 /*
1193 * Determine if this SCSI device server requires that
1194 * SCSI Intiatior TransportID w/ ISIDs is enforced
1195 * for fabric modules (iSCSI) requiring them.
1196 */
e3d6f909 1197 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
0fd97ccf 1198 if (dev->dev_attrib.enforce_pr_isids)
c66ac9db
NB
1199 continue;
1200 }
33940d09 1201 atomic_inc_mb(&pr_reg->pr_res_holders);
c66ac9db
NB
1202 spin_unlock(&pr_tmpl->registration_lock);
1203 return pr_reg;
1204 }
1205 /*
1206 * If the *pr_reg contains a fabric defined ISID for multi-value
1207 * SCSI Initiator Port TransportIDs, then we expect a valid
1208 * matching ISID to be provided by the local SCSI Initiator Port.
1209 */
6708bb27 1210 if (!isid)
c66ac9db
NB
1211 continue;
1212 if (strcmp(isid, pr_reg->pr_reg_isid))
1213 continue;
1214
33940d09 1215 atomic_inc_mb(&pr_reg->pr_res_holders);
c66ac9db
NB
1216 spin_unlock(&pr_tmpl->registration_lock);
1217 return pr_reg;
1218 }
1219 spin_unlock(&pr_tmpl->registration_lock);
1220
1221 return NULL;
1222}
1223
1224static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1225 struct se_device *dev,
1226 struct se_node_acl *nacl,
1227 struct se_session *sess)
1228{
1229 struct se_portal_group *tpg = nacl->se_tpg;
1230 unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1231
e3d6f909 1232 if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
c66ac9db 1233 memset(&buf[0], 0, PR_REG_ISID_LEN);
e3d6f909 1234 tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
c66ac9db
NB
1235 PR_REG_ISID_LEN);
1236 isid_ptr = &buf[0];
1237 }
1238
1239 return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1240}
1241
1242static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1243{
33940d09 1244 atomic_dec_mb(&pr_reg->pr_res_holders);
c66ac9db
NB
1245}
1246
125d0119 1247static int core_scsi3_check_implicit_release(
c66ac9db
NB
1248 struct se_device *dev,
1249 struct t10_pr_registration *pr_reg)
1250{
1251 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1252 struct t10_pr_registration *pr_res_holder;
1253 int ret = 0;
1254
1255 spin_lock(&dev->dev_reservation_lock);
1256 pr_res_holder = dev->dev_pr_res_holder;
6708bb27 1257 if (!pr_res_holder) {
c66ac9db
NB
1258 spin_unlock(&dev->dev_reservation_lock);
1259 return ret;
1260 }
1261 if (pr_res_holder == pr_reg) {
1262 /*
125d0119 1263 * Perform an implicit RELEASE if the registration that
c66ac9db
NB
1264 * is being released is holding the reservation.
1265 *
1266 * From spc4r17, section 5.7.11.1:
1267 *
1268 * e) If the I_T nexus is the persistent reservation holder
1269 * and the persistent reservation is not an all registrants
1270 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1271 * service action or REGISTER AND IGNORE EXISTING KEY
1272 * service action with the SERVICE ACTION RESERVATION KEY
1273 * field set to zero (see 5.7.11.3).
1274 */
6c3c9baa 1275 __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0, 1);
c66ac9db
NB
1276 ret = 1;
1277 /*
1278 * For 'All Registrants' reservation types, all existing
1279 * registrations are still processed as reservation holders
1280 * in core_scsi3_pr_seq_non_holder() after the initial
125d0119 1281 * reservation holder is implicitly released here.
c66ac9db
NB
1282 */
1283 } else if (pr_reg->pr_reg_all_tg_pt &&
1284 (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1285 pr_reg->pr_reg_nacl->initiatorname)) &&
1286 (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
6708bb27 1287 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
c66ac9db
NB
1288 " UNREGISTER while existing reservation with matching"
1289 " key 0x%016Lx is present from another SCSI Initiator"
1290 " Port\n", pr_reg->pr_res_key);
e3d6f909 1291 ret = -EPERM;
c66ac9db
NB
1292 }
1293 spin_unlock(&dev->dev_reservation_lock);
1294
1295 return ret;
1296}
1297
c66ac9db
NB
1298static void __core_scsi3_free_registration(
1299 struct se_device *dev,
1300 struct t10_pr_registration *pr_reg,
1301 struct list_head *preempt_and_abort_list,
1302 int dec_holders)
cb0df4d3
BVA
1303 __releases(&pr_tmpl->registration_lock)
1304 __acquires(&pr_tmpl->registration_lock)
c66ac9db 1305{
9ac8928e 1306 const struct target_core_fabric_ops *tfo =
c66ac9db 1307 pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
0fd97ccf 1308 struct t10_reservation *pr_tmpl = &dev->t10_pr;
80bfdfa9
NB
1309 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1310 struct se_dev_entry *deve;
c66ac9db 1311 char i_buf[PR_REG_ISID_ID_LEN];
c66ac9db 1312
618baaf7
BVA
1313 lockdep_assert_held(&pr_tmpl->registration_lock);
1314
c66ac9db 1315 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
d2843c17 1316 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db 1317
6c3c9baa
NB
1318 if (!list_empty(&pr_reg->pr_reg_list))
1319 list_del(&pr_reg->pr_reg_list);
c66ac9db
NB
1320 /*
1321 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1322 * so call core_scsi3_put_pr_reg() to decrement our reference.
1323 */
1324 if (dec_holders)
1325 core_scsi3_put_pr_reg(pr_reg);
80bfdfa9
NB
1326
1327 spin_unlock(&pr_tmpl->registration_lock);
c66ac9db
NB
1328 /*
1329 * Wait until all reference from any other I_T nexuses for this
1330 * *pr_reg have been released. Because list_del() is called above,
1331 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1332 * count back to zero, and we release *pr_reg.
1333 */
1334 while (atomic_read(&pr_reg->pr_res_holders) != 0) {
6708bb27 1335 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
30c7ca93 1336 tfo->fabric_name);
c66ac9db 1337 cpu_relax();
c66ac9db
NB
1338 }
1339
80bfdfa9
NB
1340 rcu_read_lock();
1341 deve = target_nacl_find_deve(nacl, pr_reg->pr_res_mapped_lun);
1342 if (deve)
1343 clear_bit(DEF_PR_REG_ACTIVE, &deve->deve_flags);
1344 rcu_read_unlock();
1345
1346 spin_lock(&pr_tmpl->registration_lock);
6708bb27 1347 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
30c7ca93 1348 " Node: %s%s\n", tfo->fabric_name,
c66ac9db 1349 pr_reg->pr_reg_nacl->initiatorname,
d2843c17 1350 i_buf);
6708bb27 1351 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
30c7ca93 1352 " Port(s)\n", tfo->fabric_name,
c66ac9db 1353 (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
e3d6f909 1354 dev->transport->name);
6708bb27 1355 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
30c7ca93 1356 " 0x%08x\n", tfo->fabric_name, pr_reg->pr_res_key,
c66ac9db
NB
1357 pr_reg->pr_res_generation);
1358
6708bb27 1359 if (!preempt_and_abort_list) {
c66ac9db
NB
1360 pr_reg->pr_reg_deve = NULL;
1361 pr_reg->pr_reg_nacl = NULL;
c66ac9db
NB
1362 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1363 return;
1364 }
1365 /*
1366 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1367 * are released once the ABORT_TASK_SET has completed..
1368 */
1369 list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1370}
1371
1372void core_scsi3_free_pr_reg_from_nacl(
1373 struct se_device *dev,
1374 struct se_node_acl *nacl)
1375{
0fd97ccf 1376 struct t10_reservation *pr_tmpl = &dev->t10_pr;
c66ac9db 1377 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
6c3c9baa 1378 bool free_reg = false;
c66ac9db
NB
1379 /*
1380 * If the passed se_node_acl matches the reservation holder,
1381 * release the reservation.
1382 */
1383 spin_lock(&dev->dev_reservation_lock);
1384 pr_res_holder = dev->dev_pr_res_holder;
1385 if ((pr_res_holder != NULL) &&
6c3c9baa
NB
1386 (pr_res_holder->pr_reg_nacl == nacl)) {
1387 __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0, 1);
1388 free_reg = true;
1389 }
c66ac9db
NB
1390 spin_unlock(&dev->dev_reservation_lock);
1391 /*
1392 * Release any registration associated with the struct se_node_acl.
1393 */
1394 spin_lock(&pr_tmpl->registration_lock);
6c3c9baa
NB
1395 if (pr_res_holder && free_reg)
1396 __core_scsi3_free_registration(dev, pr_res_holder, NULL, 0);
1397
c66ac9db
NB
1398 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1399 &pr_tmpl->registration_list, pr_reg_list) {
1400
1401 if (pr_reg->pr_reg_nacl != nacl)
1402 continue;
1403
1404 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1405 }
1406 spin_unlock(&pr_tmpl->registration_lock);
1407}
1408
1409void core_scsi3_free_all_registrations(
1410 struct se_device *dev)
1411{
0fd97ccf 1412 struct t10_reservation *pr_tmpl = &dev->t10_pr;
c66ac9db
NB
1413 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1414
1415 spin_lock(&dev->dev_reservation_lock);
1416 pr_res_holder = dev->dev_pr_res_holder;
1417 if (pr_res_holder != NULL) {
1418 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1419 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
6c3c9baa 1420 pr_res_holder, 0, 0);
c66ac9db
NB
1421 }
1422 spin_unlock(&dev->dev_reservation_lock);
1423
1424 spin_lock(&pr_tmpl->registration_lock);
1425 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1426 &pr_tmpl->registration_list, pr_reg_list) {
1427
1428 __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1429 }
1430 spin_unlock(&pr_tmpl->registration_lock);
1431
1432 spin_lock(&pr_tmpl->aptpl_reg_lock);
1433 list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1434 pr_reg_aptpl_list) {
1435 list_del(&pr_reg->pr_reg_aptpl_list);
c66ac9db
NB
1436 kmem_cache_free(t10_pr_reg_cache, pr_reg);
1437 }
1438 spin_unlock(&pr_tmpl->aptpl_reg_lock);
1439}
1440
1441static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1442{
d588cf8f 1443 return target_depend_item(&tpg->tpg_group.cg_item);
c66ac9db
NB
1444}
1445
1446static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1447{
d588cf8f 1448 target_undepend_item(&tpg->tpg_group.cg_item);
33940d09 1449 atomic_dec_mb(&tpg->tpg_pr_ref_count);
c66ac9db
NB
1450}
1451
1452static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1453{
c66ac9db
NB
1454 if (nacl->dynamic_node_acl)
1455 return 0;
d588cf8f 1456 return target_depend_item(&nacl->acl_group.cg_item);
c66ac9db
NB
1457}
1458
1459static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1460{
d588cf8f
CH
1461 if (!nacl->dynamic_node_acl)
1462 target_undepend_item(&nacl->acl_group.cg_item);
33940d09 1463 atomic_dec_mb(&nacl->acl_pr_ref_count);
c66ac9db
NB
1464}
1465
1466static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1467{
79dc9c9e 1468 struct se_lun_acl *lun_acl;
ac75d8be 1469
c66ac9db
NB
1470 /*
1471 * For nacl->dynamic_node_acl=1
1472 */
79dc9c9e 1473 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
2c935bc5 1474 kref_read(&se_deve->pr_kref) != 0);
6708bb27 1475 if (!lun_acl)
c66ac9db
NB
1476 return 0;
1477
d588cf8f 1478 return target_depend_item(&lun_acl->se_lun_group.cg_item);
c66ac9db
NB
1479}
1480
1481static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1482{
29a05dee 1483 struct se_lun_acl *lun_acl;
ac75d8be 1484
c66ac9db
NB
1485 /*
1486 * For nacl->dynamic_node_acl=1
1487 */
79dc9c9e 1488 lun_acl = rcu_dereference_check(se_deve->se_lun_acl,
2c935bc5 1489 kref_read(&se_deve->pr_kref) != 0);
6708bb27 1490 if (!lun_acl) {
29a05dee 1491 kref_put(&se_deve->pr_kref, target_pr_kref_release);
c66ac9db
NB
1492 return;
1493 }
c66ac9db 1494
d588cf8f 1495 target_undepend_item(&lun_acl->se_lun_group.cg_item);
29a05dee 1496 kref_put(&se_deve->pr_kref, target_pr_kref_release);
c66ac9db
NB
1497}
1498
de103c93
CH
1499static sense_reason_t
1500core_scsi3_decode_spec_i_port(
c66ac9db
NB
1501 struct se_cmd *cmd,
1502 struct se_portal_group *tpg,
1503 unsigned char *l_isid,
1504 u64 sa_res_key,
1505 int all_tg_pt,
1506 int aptpl)
1507{
5951146d 1508 struct se_device *dev = cmd->se_dev;
c66ac9db 1509 struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
e3d6f909 1510 struct se_session *se_sess = cmd->se_sess;
c66ac9db 1511 struct se_node_acl *dest_node_acl = NULL;
79dc9c9e 1512 struct se_dev_entry *dest_se_deve = NULL;
c66ac9db
NB
1513 struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1514 struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
d0f474e5 1515 LIST_HEAD(tid_dest_list);
c66ac9db 1516 struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
2650d71e 1517 unsigned char *buf, *ptr, proto_ident;
9b353cc8 1518 const unsigned char *i_str = NULL;
13ba564c 1519 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
de103c93 1520 sense_reason_t ret;
c66ac9db 1521 u32 tpdl, tid_len = 0;
c66ac9db
NB
1522 u32 dest_rtpi = 0;
1523
c66ac9db
NB
1524 /*
1525 * Allocate a struct pr_transport_id_holder and setup the
79dc9c9e
NB
1526 * local_node_acl pointer and add to struct list_head tid_dest_list
1527 * for add registration processing in the loop of tid_dest_list below.
c66ac9db
NB
1528 */
1529 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
6708bb27
AG
1530 if (!tidh_new) {
1531 pr_err("Unable to allocate tidh_new\n");
79dd6f2f 1532 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
c66ac9db
NB
1533 }
1534 INIT_LIST_HEAD(&tidh_new->dest_list);
1535 tidh_new->dest_tpg = tpg;
1536 tidh_new->dest_node_acl = se_sess->se_node_acl;
c66ac9db 1537
5951146d 1538 local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
79dc9c9e
NB
1539 se_sess->se_node_acl, cmd->se_lun,
1540 NULL, cmd->orig_fe_lun, l_isid,
c66ac9db 1541 sa_res_key, all_tg_pt, aptpl);
6708bb27 1542 if (!local_pr_reg) {
c66ac9db 1543 kfree(tidh_new);
79dd6f2f 1544 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
c66ac9db
NB
1545 }
1546 tidh_new->dest_pr_reg = local_pr_reg;
1547 /*
1548 * The local I_T nexus does not hold any configfs dependances,
79dc9c9e 1549 * so we set tidh_new->dest_se_deve to NULL to prevent the
c66ac9db
NB
1550 * configfs_undepend_item() calls in the tid_dest_list loops below.
1551 */
79dc9c9e 1552 tidh_new->dest_se_deve = NULL;
c66ac9db 1553 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
05d1c7c0 1554
0d7f1299
PB
1555 if (cmd->data_length < 28) {
1556 pr_warn("SPC-PR: Received PR OUT parameter list"
1557 " length too small: %u\n", cmd->data_length);
de103c93 1558 ret = TCM_INVALID_PARAMETER_LIST;
0d7f1299
PB
1559 goto out;
1560 }
1561
4949314c 1562 buf = transport_kmap_data_sg(cmd);
de103c93 1563 if (!buf) {
79dd6f2f 1564 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
de103c93
CH
1565 goto out;
1566 }
1567
c66ac9db
NB
1568 /*
1569 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1570 * first extract TransportID Parameter Data Length, and make sure
1571 * the value matches up to the SCSI expected data transfer length.
1572 */
a85d667e 1573 tpdl = get_unaligned_be32(&buf[24]);
c66ac9db
NB
1574
1575 if ((tpdl + 28) != cmd->data_length) {
6708bb27 1576 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
c66ac9db
NB
1577 " does not equal CDB data_length: %u\n", tpdl,
1578 cmd->data_length);
de103c93
CH
1579 ret = TCM_INVALID_PARAMETER_LIST;
1580 goto out_unmap;
c66ac9db
NB
1581 }
1582 /*
1583 * Start processing the received transport IDs using the
1584 * receiving I_T Nexus portal's fabric dependent methods to
1585 * obtain the SCSI Initiator Port/Device Identifiers.
1586 */
1587 ptr = &buf[28];
1588
1589 while (tpdl > 0) {
adf653f9 1590 struct se_lun *dest_lun, *tmp_lun;
79dc9c9e 1591
c66ac9db
NB
1592 proto_ident = (ptr[0] & 0x0f);
1593 dest_tpg = NULL;
1594
1595 spin_lock(&dev->se_port_lock);
adf653f9
CH
1596 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
1597 tmp_tpg = tmp_lun->lun_tpg;
2650d71e 1598
c66ac9db
NB
1599 /*
1600 * Look for the matching proto_ident provided by
1601 * the received TransportID
1602 */
2aeeafae 1603 if (tmp_tpg->proto_id != proto_ident)
c66ac9db 1604 continue;
adf653f9 1605 dest_rtpi = tmp_lun->lun_rtpi;
c66ac9db 1606
2650d71e 1607 i_str = target_parse_pr_out_transport_id(tmp_tpg,
094bb5d7 1608 ptr, &tid_len, &iport_ptr);
6708bb27 1609 if (!i_str)
c66ac9db
NB
1610 continue;
1611
33940d09 1612 atomic_inc_mb(&tmp_tpg->tpg_pr_ref_count);
c66ac9db
NB
1613 spin_unlock(&dev->se_port_lock);
1614
de103c93 1615 if (core_scsi3_tpg_depend_item(tmp_tpg)) {
6708bb27 1616 pr_err(" core_scsi3_tpg_depend_item()"
c66ac9db 1617 " for tmp_tpg\n");
33940d09 1618 atomic_dec_mb(&tmp_tpg->tpg_pr_ref_count);
de103c93
CH
1619 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1620 goto out_unmap;
c66ac9db
NB
1621 }
1622 /*
35d1efe8 1623 * Locate the destination initiator ACL to be registered
c66ac9db
NB
1624 * from the decoded fabric module specific TransportID
1625 * at *i_str.
1626 */
403edd78 1627 mutex_lock(&tmp_tpg->acl_node_mutex);
c66ac9db
NB
1628 dest_node_acl = __core_tpg_get_initiator_node_acl(
1629 tmp_tpg, i_str);
33940d09
JE
1630 if (dest_node_acl)
1631 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
403edd78 1632 mutex_unlock(&tmp_tpg->acl_node_mutex);
c66ac9db 1633
6708bb27 1634 if (!dest_node_acl) {
c66ac9db
NB
1635 core_scsi3_tpg_undepend_item(tmp_tpg);
1636 spin_lock(&dev->se_port_lock);
1637 continue;
1638 }
1639
de103c93 1640 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
6708bb27 1641 pr_err("configfs_depend_item() failed"
c66ac9db 1642 " for dest_node_acl->acl_group\n");
33940d09 1643 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
c66ac9db 1644 core_scsi3_tpg_undepend_item(tmp_tpg);
de103c93
CH
1645 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1646 goto out_unmap;
c66ac9db
NB
1647 }
1648
1649 dest_tpg = tmp_tpg;
6708bb27 1650 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
c66ac9db 1651 " %s Port RTPI: %hu\n",
30c7ca93 1652 dest_tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
1653 dest_node_acl->initiatorname, dest_rtpi);
1654
1655 spin_lock(&dev->se_port_lock);
1656 break;
1657 }
1658 spin_unlock(&dev->se_port_lock);
1659
6708bb27
AG
1660 if (!dest_tpg) {
1661 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
c66ac9db 1662 " dest_tpg\n");
de103c93
CH
1663 ret = TCM_INVALID_PARAMETER_LIST;
1664 goto out_unmap;
c66ac9db 1665 }
8b1e1244 1666
6708bb27 1667 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
c66ac9db 1668 " tid_len: %d for %s + %s\n",
30c7ca93 1669 dest_tpg->se_tpg_tfo->fabric_name, cmd->data_length,
c66ac9db 1670 tpdl, tid_len, i_str, iport_ptr);
8b1e1244 1671
c66ac9db 1672 if (tid_len > tpdl) {
6708bb27 1673 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
c66ac9db
NB
1674 " %u for Transport ID: %s\n", tid_len, ptr);
1675 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1676 core_scsi3_tpg_undepend_item(dest_tpg);
de103c93
CH
1677 ret = TCM_INVALID_PARAMETER_LIST;
1678 goto out_unmap;
c66ac9db
NB
1679 }
1680 /*
1681 * Locate the desintation struct se_dev_entry pointer for matching
1682 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1683 * Target Port.
1684 */
1685 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1686 dest_rtpi);
6708bb27
AG
1687 if (!dest_se_deve) {
1688 pr_err("Unable to locate %s dest_se_deve"
c66ac9db 1689 " from destination RTPI: %hu\n",
30c7ca93 1690 dest_tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
1691 dest_rtpi);
1692
1693 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1694 core_scsi3_tpg_undepend_item(dest_tpg);
de103c93
CH
1695 ret = TCM_INVALID_PARAMETER_LIST;
1696 goto out_unmap;
c66ac9db
NB
1697 }
1698
de103c93 1699 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
6708bb27 1700 pr_err("core_scsi3_lunacl_depend_item()"
c66ac9db 1701 " failed\n");
29a05dee 1702 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
c66ac9db
NB
1703 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1704 core_scsi3_tpg_undepend_item(dest_tpg);
de103c93
CH
1705 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1706 goto out_unmap;
c66ac9db 1707 }
8b1e1244 1708
6708bb27 1709 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
f2d30680 1710 " dest_se_deve mapped_lun: %llu\n",
30c7ca93 1711 dest_tpg->se_tpg_tfo->fabric_name,
c66ac9db 1712 dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
8b1e1244 1713
c66ac9db
NB
1714 /*
1715 * Skip any TransportIDs that already have a registration for
1716 * this target port.
1717 */
1718 pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1719 iport_ptr);
1720 if (pr_reg_e) {
1721 core_scsi3_put_pr_reg(pr_reg_e);
1722 core_scsi3_lunacl_undepend_item(dest_se_deve);
1723 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1724 core_scsi3_tpg_undepend_item(dest_tpg);
1725 ptr += tid_len;
1726 tpdl -= tid_len;
1727 tid_len = 0;
1728 continue;
1729 }
1730 /*
1731 * Allocate a struct pr_transport_id_holder and setup
1732 * the dest_node_acl and dest_se_deve pointers for the
1733 * loop below.
1734 */
1735 tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1736 GFP_KERNEL);
6708bb27
AG
1737 if (!tidh_new) {
1738 pr_err("Unable to allocate tidh_new\n");
c66ac9db
NB
1739 core_scsi3_lunacl_undepend_item(dest_se_deve);
1740 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1741 core_scsi3_tpg_undepend_item(dest_tpg);
de103c93
CH
1742 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1743 goto out_unmap;
c66ac9db
NB
1744 }
1745 INIT_LIST_HEAD(&tidh_new->dest_list);
1746 tidh_new->dest_tpg = dest_tpg;
1747 tidh_new->dest_node_acl = dest_node_acl;
1748 tidh_new->dest_se_deve = dest_se_deve;
1749
1750 /*
1751 * Allocate, but do NOT add the registration for the
1752 * TransportID referenced SCSI Initiator port. This
1753 * done because of the following from spc4r17 in section
1754 * 6.14.3 wrt SPEC_I_PT:
1755 *
1756 * "If a registration fails for any initiator port (e.g., if th
1757 * logical unit does not have enough resources available to
1758 * hold the registration information), no registrations shall be
1759 * made, and the command shall be terminated with
1760 * CHECK CONDITION status."
1761 *
1762 * That means we call __core_scsi3_alloc_registration() here,
1763 * and then call __core_scsi3_add_registration() in the
1764 * 2nd loop which will never fail.
1765 */
79dc9c9e 1766 dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
2c935bc5 1767 kref_read(&dest_se_deve->pr_kref) != 0);
79dc9c9e 1768
5951146d 1769 dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
79dc9c9e
NB
1770 dest_node_acl, dest_lun, dest_se_deve,
1771 dest_se_deve->mapped_lun, iport_ptr,
1772 sa_res_key, all_tg_pt, aptpl);
6708bb27 1773 if (!dest_pr_reg) {
c66ac9db
NB
1774 core_scsi3_lunacl_undepend_item(dest_se_deve);
1775 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1776 core_scsi3_tpg_undepend_item(dest_tpg);
1777 kfree(tidh_new);
79dd6f2f 1778 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
de103c93 1779 goto out_unmap;
c66ac9db
NB
1780 }
1781 tidh_new->dest_pr_reg = dest_pr_reg;
1782 list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1783
1784 ptr += tid_len;
1785 tpdl -= tid_len;
1786 tid_len = 0;
1787
1788 }
05d1c7c0 1789
4949314c 1790 transport_kunmap_data_sg(cmd);
05d1c7c0 1791
c66ac9db
NB
1792 /*
1793 * Go ahead and create a registrations from tid_dest_list for the
1794 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1795 * and dest_se_deve.
1796 *
1797 * The SA Reservation Key from the PROUT is set for the
1798 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1799 * means that the TransportID Initiator port will be
1800 * registered on all of the target ports in the SCSI target device
1801 * ALL_TG_PT=0 means the registration will only be for the
1802 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1803 * was received.
1804 */
1805 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1806 dest_tpg = tidh->dest_tpg;
1807 dest_node_acl = tidh->dest_node_acl;
1808 dest_se_deve = tidh->dest_se_deve;
1809 dest_pr_reg = tidh->dest_pr_reg;
c66ac9db
NB
1810
1811 list_del(&tidh->dest_list);
1812 kfree(tidh);
1813
1814 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
d2843c17 1815 core_pr_dump_initiator_port(dest_pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db 1816
5951146d 1817 __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
c66ac9db
NB
1818 dest_pr_reg, 0, 0);
1819
6708bb27 1820 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
c66ac9db 1821 " registered Transport ID for Node: %s%s Mapped LUN:"
30c7ca93 1822 " %llu\n", dest_tpg->se_tpg_tfo->fabric_name,
79dc9c9e
NB
1823 dest_node_acl->initiatorname, i_buf, (dest_se_deve) ?
1824 dest_se_deve->mapped_lun : 0);
c66ac9db 1825
3ccd6e83
NB
1826 if (!dest_se_deve) {
1827 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1828 target_pr_kref_release);
c66ac9db 1829 continue;
3ccd6e83 1830 }
c66ac9db
NB
1831 core_scsi3_lunacl_undepend_item(dest_se_deve);
1832 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1833 core_scsi3_tpg_undepend_item(dest_tpg);
1834 }
1835
1836 return 0;
de103c93 1837out_unmap:
4949314c 1838 transport_kunmap_data_sg(cmd);
de103c93 1839out:
c66ac9db
NB
1840 /*
1841 * For the failure case, release everything from tid_dest_list
1842 * including *dest_pr_reg and the configfs dependances..
1843 */
1844 list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1845 dest_tpg = tidh->dest_tpg;
1846 dest_node_acl = tidh->dest_node_acl;
1847 dest_se_deve = tidh->dest_se_deve;
1848 dest_pr_reg = tidh->dest_pr_reg;
c66ac9db
NB
1849
1850 list_del(&tidh->dest_list);
1851 kfree(tidh);
1852 /*
1853 * Release any extra ALL_TG_PT=1 registrations for
1854 * the SPEC_I_PT=1 case.
1855 */
1856 list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1857 &dest_pr_reg->pr_reg_atp_list,
1858 pr_reg_atp_mem_list) {
1859 list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1860 core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1861 kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1862 }
1863
c66ac9db
NB
1864 kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1865
3ccd6e83
NB
1866 if (!dest_se_deve) {
1867 kref_put(&local_pr_reg->pr_reg_deve->pr_kref,
1868 target_pr_kref_release);
c66ac9db 1869 continue;
3ccd6e83 1870 }
c66ac9db
NB
1871 core_scsi3_lunacl_undepend_item(dest_se_deve);
1872 core_scsi3_nodeacl_undepend_item(dest_node_acl);
1873 core_scsi3_tpg_undepend_item(dest_tpg);
1874 }
1875 return ret;
1876}
1877
3c8a6228 1878static int core_scsi3_update_aptpl_buf(
c66ac9db
NB
1879 struct se_device *dev,
1880 unsigned char *buf,
0607decd 1881 u32 pr_aptpl_buf_len)
c66ac9db 1882{
c66ac9db 1883 struct se_portal_group *tpg;
c66ac9db
NB
1884 struct t10_pr_registration *pr_reg;
1885 unsigned char tmp[512], isid_buf[32];
1886 ssize_t len = 0;
1887 int reg_count = 0;
3c8a6228 1888 int ret = 0;
c66ac9db 1889
3c8a6228
AG
1890 spin_lock(&dev->dev_reservation_lock);
1891 spin_lock(&dev->t10_pr.registration_lock);
c66ac9db
NB
1892 /*
1893 * Walk the registration list..
1894 */
0fd97ccf 1895 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
c66ac9db
NB
1896 pr_reg_list) {
1897
1898 tmp[0] = '\0';
1899 isid_buf[0] = '\0';
1900 tpg = pr_reg->pr_reg_nacl->se_tpg;
c66ac9db
NB
1901 /*
1902 * Write out any ISID value to APTPL metadata that was included
1903 * in the original registration.
1904 */
1905 if (pr_reg->isid_present_at_reg)
1906 snprintf(isid_buf, 32, "initiator_sid=%s\n",
1907 pr_reg->pr_reg_isid);
1908 /*
1909 * Include special metadata if the pr_reg matches the
1910 * reservation holder.
1911 */
1912 if (dev->dev_pr_res_holder == pr_reg) {
1913 snprintf(tmp, 512, "PR_REG_START: %d"
1914 "\ninitiator_fabric=%s\n"
1915 "initiator_node=%s\n%s"
1916 "sa_res_key=%llu\n"
1917 "res_holder=1\nres_type=%02x\n"
1918 "res_scope=%02x\nres_all_tg_pt=%d\n"
f2d30680 1919 "mapped_lun=%llu\n", reg_count,
30c7ca93 1920 tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
1921 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1922 pr_reg->pr_res_key, pr_reg->pr_res_type,
1923 pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1924 pr_reg->pr_res_mapped_lun);
1925 } else {
1926 snprintf(tmp, 512, "PR_REG_START: %d\n"
1927 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1928 "sa_res_key=%llu\nres_holder=0\n"
f2d30680 1929 "res_all_tg_pt=%d\nmapped_lun=%llu\n",
30c7ca93 1930 reg_count, tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
1931 pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1932 pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1933 pr_reg->pr_res_mapped_lun);
1934 }
1935
60d645a4 1936 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
f161d4b4
NB
1937 pr_err("Unable to update renaming APTPL metadata,"
1938 " reallocating larger buffer\n");
3c8a6228
AG
1939 ret = -EMSGSIZE;
1940 goto out;
c66ac9db
NB
1941 }
1942 len += sprintf(buf+len, "%s", tmp);
1943
1944 /*
1945 * Include information about the associated SCSI target port.
1946 */
1947 snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
f2d30680 1948 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%llu\nPR_REG_END:"
30c7ca93 1949 " %d\n", tpg->se_tpg_tfo->fabric_name,
e3d6f909
AG
1950 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1951 tpg->se_tpg_tfo->tpg_get_tag(tpg),
79dc9c9e
NB
1952 pr_reg->tg_pt_sep_rtpi, pr_reg->pr_aptpl_target_lun,
1953 reg_count);
c66ac9db 1954
60d645a4 1955 if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
f161d4b4
NB
1956 pr_err("Unable to update renaming APTPL metadata,"
1957 " reallocating larger buffer\n");
3c8a6228
AG
1958 ret = -EMSGSIZE;
1959 goto out;
c66ac9db
NB
1960 }
1961 len += sprintf(buf+len, "%s", tmp);
1962 reg_count++;
1963 }
c66ac9db 1964
6708bb27 1965 if (!reg_count)
c66ac9db
NB
1966 len += sprintf(buf+len, "No Registrations or Reservations");
1967
3c8a6228
AG
1968out:
1969 spin_unlock(&dev->t10_pr.registration_lock);
c66ac9db
NB
1970 spin_unlock(&dev->dev_reservation_lock);
1971
1972 return ret;
1973}
1974
c66ac9db
NB
1975static int __core_scsi3_write_aptpl_to_file(
1976 struct se_device *dev,
4dee96fb 1977 unsigned char *buf)
c66ac9db 1978{
0fd97ccf 1979 struct t10_wwn *wwn = &dev->t10_wwn;
c66ac9db 1980 struct file *file;
c66ac9db 1981 int flags = O_RDWR | O_CREAT | O_TRUNC;
bdc79f0e 1982 char *path;
4dee96fb 1983 u32 pr_aptpl_buf_len;
c66ac9db 1984 int ret;
e13ec939 1985 loff_t pos = 0;
c66ac9db 1986
bdc79f0e
DD
1987 path = kasprintf(GFP_KERNEL, "%s/pr/aptpl_%s", db_root,
1988 &wwn->unit_serial[0]);
1989 if (!path)
1990 return -ENOMEM;
c66ac9db 1991
c66ac9db 1992 file = filp_open(path, flags, 0600);
0e9b10a9 1993 if (IS_ERR(file)) {
6708bb27 1994 pr_err("filp_open(%s) for APTPL metadata"
c66ac9db 1995 " failed\n", path);
bdc79f0e 1996 kfree(path);
0e9b10a9 1997 return PTR_ERR(file);
c66ac9db
NB
1998 }
1999
4dee96fb 2000 pr_aptpl_buf_len = (strlen(buf) + 1); /* Add extra for NULL */
c66ac9db 2001
e13ec939 2002 ret = kernel_write(file, buf, pr_aptpl_buf_len, &pos);
c66ac9db 2003
0e9b10a9 2004 if (ret < 0)
6708bb27 2005 pr_debug("Error writing APTPL metadata file: %s\n", path);
0e9b10a9 2006 fput(file);
bdc79f0e 2007 kfree(path);
c66ac9db 2008
f730f915 2009 return (ret < 0) ? -EIO : 0;
c66ac9db
NB
2010}
2011
459f213b
AG
2012/*
2013 * Clear the APTPL metadata if APTPL has been disabled, otherwise
2014 * write out the updated metadata to struct file for this SCSI device.
2015 */
8a391857 2016static sense_reason_t core_scsi3_update_and_write_aptpl(struct se_device *dev, bool aptpl)
c66ac9db 2017{
8a391857 2018 unsigned char *buf;
f161d4b4 2019 int rc, len = PR_APTPL_BUF_LEN;
de103c93 2020
459f213b
AG
2021 if (!aptpl) {
2022 char *null_buf = "No Registrations or Reservations\n";
2023
8a391857 2024 rc = __core_scsi3_write_aptpl_to_file(dev, null_buf);
459f213b
AG
2025 dev->t10_pr.pr_aptpl_active = 0;
2026 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
459f213b 2027
8a391857
NB
2028 if (rc)
2029 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
459f213b 2030
8a391857
NB
2031 return 0;
2032 }
f161d4b4
NB
2033retry:
2034 buf = vzalloc(len);
8a391857
NB
2035 if (!buf)
2036 return TCM_OUT_OF_RESOURCES;
2037
f161d4b4 2038 rc = core_scsi3_update_aptpl_buf(dev, buf, len);
8a391857 2039 if (rc < 0) {
f161d4b4
NB
2040 vfree(buf);
2041 len *= 2;
2042 goto retry;
c66ac9db
NB
2043 }
2044
8a391857
NB
2045 rc = __core_scsi3_write_aptpl_to_file(dev, buf);
2046 if (rc != 0) {
2047 pr_err("SPC-3 PR: Could not update APTPL\n");
f161d4b4 2048 vfree(buf);
8a391857
NB
2049 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2050 }
2051 dev->t10_pr.pr_aptpl_active = 1;
f161d4b4 2052 vfree(buf);
8a391857
NB
2053 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2054 return 0;
c66ac9db
NB
2055}
2056
de103c93
CH
2057static sense_reason_t
2058core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
bc118fe4 2059 bool aptpl, bool all_tg_pt, bool spec_i_pt, enum register_type register_type)
c66ac9db 2060{
e3d6f909 2061 struct se_session *se_sess = cmd->se_sess;
5951146d 2062 struct se_device *dev = cmd->se_dev;
e3d6f909 2063 struct se_lun *se_lun = cmd->se_lun;
c66ac9db 2064 struct se_portal_group *se_tpg;
bc118fe4 2065 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp;
0fd97ccf 2066 struct t10_reservation *pr_tmpl = &dev->t10_pr;
c66ac9db 2067 unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
a0d50f62 2068 sense_reason_t ret = TCM_NO_SENSE;
fc09149d 2069 int pr_holder = 0, type;
c66ac9db 2070
6708bb27
AG
2071 if (!se_sess || !se_lun) {
2072 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
de103c93 2073 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
2074 }
2075 se_tpg = se_sess->se_tpg;
c66ac9db 2076
e3d6f909 2077 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
c66ac9db 2078 memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
e3d6f909 2079 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
c66ac9db
NB
2080 PR_REG_ISID_LEN);
2081 isid_ptr = &isid_buf[0];
2082 }
2083 /*
2084 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2085 */
bc118fe4
AG
2086 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2087 if (!pr_reg) {
c66ac9db 2088 if (res_key) {
6708bb27 2089 pr_warn("SPC-3 PR: Reservation Key non-zero"
c66ac9db 2090 " for SA REGISTER, returning CONFLICT\n");
de103c93 2091 return TCM_RESERVATION_CONFLICT;
c66ac9db
NB
2092 }
2093 /*
2094 * Do nothing but return GOOD status.
2095 */
6708bb27 2096 if (!sa_res_key)
03e98c9e 2097 return 0;
c66ac9db 2098
6708bb27 2099 if (!spec_i_pt) {
c66ac9db
NB
2100 /*
2101 * Perform the Service Action REGISTER on the Initiator
2102 * Port Endpoint that the PRO was received from on the
2103 * Logical Unit of the SCSI device server.
2104 */
de103c93 2105 if (core_scsi3_alloc_registration(cmd->se_dev,
79dc9c9e
NB
2106 se_sess->se_node_acl, cmd->se_lun,
2107 NULL, cmd->orig_fe_lun, isid_ptr,
c66ac9db 2108 sa_res_key, all_tg_pt, aptpl,
33ce6a87 2109 register_type, 0)) {
6708bb27 2110 pr_err("Unable to allocate"
c66ac9db 2111 " struct t10_pr_registration\n");
79dd6f2f 2112 return TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
c66ac9db
NB
2113 }
2114 } else {
2115 /*
2116 * Register both the Initiator port that received
2117 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2118 * TransportID from Parameter list and loop through
2119 * fabric dependent parameter list while calling
2120 * logic from of core_scsi3_alloc_registration() for
2121 * each TransportID provided SCSI Initiator Port/Device
2122 */
2123 ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2124 isid_ptr, sa_res_key, all_tg_pt, aptpl);
2125 if (ret != 0)
2126 return ret;
2127 }
459f213b 2128 return core_scsi3_update_and_write_aptpl(dev, aptpl);
de103c93
CH
2129 }
2130
bc118fe4
AG
2131 /* ok, existing registration */
2132
2133 if ((register_type == REGISTER) && (res_key != pr_reg->pr_res_key)) {
2134 pr_err("SPC-3 PR REGISTER: Received"
2135 " res_key: 0x%016Lx does not match"
2136 " existing SA REGISTER res_key:"
2137 " 0x%016Lx\n", res_key,
2138 pr_reg->pr_res_key);
2139 ret = TCM_RESERVATION_CONFLICT;
2140 goto out;
de103c93
CH
2141 }
2142
2143 if (spec_i_pt) {
1f070cc2
AG
2144 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2145 " set on a registered nexus\n");
de103c93 2146 ret = TCM_INVALID_PARAMETER_LIST;
bc118fe4 2147 goto out;
de103c93
CH
2148 }
2149
2150 /*
2151 * An existing ALL_TG_PT=1 registration being released
2152 * must also set ALL_TG_PT=1 in the incoming PROUT.
2153 */
1f070cc2
AG
2154 if (pr_reg->pr_reg_all_tg_pt && !all_tg_pt) {
2155 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
de103c93
CH
2156 " registration exists, but ALL_TG_PT=1 bit not"
2157 " present in received PROUT\n");
2158 ret = TCM_INVALID_CDB_FIELD;
bc118fe4 2159 goto out;
de103c93
CH
2160 }
2161
de103c93 2162 /*
51d9c41d 2163 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
de103c93 2164 */
51d9c41d
AG
2165 if (sa_res_key) {
2166 /*
2167 * Increment PRgeneration counter for struct se_device"
2168 * upon a successful REGISTER, see spc4r17 section 6.3.2
2169 * READ_KEYS service action.
2170 */
2171 pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2172 pr_reg->pr_res_key = sa_res_key;
2173 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2174 " Key for %s to: 0x%016Lx PRgeneration:"
30c7ca93 2175 " 0x%08x\n", cmd->se_tfo->fabric_name,
51d9c41d
AG
2176 (register_type == REGISTER_AND_IGNORE_EXISTING_KEY) ? "_AND_IGNORE_EXISTING_KEY" : "",
2177 pr_reg->pr_reg_nacl->initiatorname,
2178 pr_reg->pr_res_key, pr_reg->pr_res_generation);
2179
2180 } else {
2181 /*
2182 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2183 */
6c3c9baa
NB
2184 type = pr_reg->pr_res_type;
2185 pr_holder = core_scsi3_check_implicit_release(cmd->se_dev,
2186 pr_reg);
de103c93 2187 if (pr_holder < 0) {
de103c93 2188 ret = TCM_RESERVATION_CONFLICT;
bc118fe4 2189 goto out;
c66ac9db 2190 }
de103c93
CH
2191
2192 spin_lock(&pr_tmpl->registration_lock);
c66ac9db 2193 /*
de103c93
CH
2194 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2195 * and matching pr_res_key.
c66ac9db 2196 */
de103c93
CH
2197 if (pr_reg->pr_reg_all_tg_pt) {
2198 list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2199 &pr_tmpl->registration_list,
2200 pr_reg_list) {
2201
2202 if (!pr_reg_p->pr_reg_all_tg_pt)
2203 continue;
2204 if (pr_reg_p->pr_res_key != res_key)
2205 continue;
2206 if (pr_reg == pr_reg_p)
2207 continue;
2208 if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2209 pr_reg_p->pr_reg_nacl->initiatorname))
2210 continue;
2211
2212 __core_scsi3_free_registration(dev,
2213 pr_reg_p, NULL, 0);
c66ac9db
NB
2214 }
2215 }
de103c93 2216
c66ac9db 2217 /*
de103c93 2218 * Release the calling I_T Nexus registration now..
c66ac9db 2219 */
de103c93 2220 __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
fc09149d 2221 pr_reg = NULL;
c66ac9db 2222
de103c93
CH
2223 /*
2224 * From spc4r17, section 5.7.11.3 Unregistering
2225 *
2226 * If the persistent reservation is a registrants only
2227 * type, the device server shall establish a unit
2228 * attention condition for the initiator port associated
2229 * with every registered I_T nexus except for the I_T
2230 * nexus on which the PERSISTENT RESERVE OUT command was
2231 * received, with the additional sense code set to
2232 * RESERVATIONS RELEASED.
2233 */
2234 if (pr_holder &&
fc09149d
NB
2235 (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2236 type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
de103c93
CH
2237 list_for_each_entry(pr_reg_p,
2238 &pr_tmpl->registration_list,
2239 pr_reg_list) {
2240
c51c8e7b 2241 target_ua_allocate_lun(
de103c93
CH
2242 pr_reg_p->pr_reg_nacl,
2243 pr_reg_p->pr_res_mapped_lun,
2244 0x2A,
2245 ASCQ_2AH_RESERVATIONS_RELEASED);
c66ac9db 2246 }
de103c93 2247 }
c66ac9db 2248
51d9c41d 2249 spin_unlock(&pr_tmpl->registration_lock);
de103c93 2250 }
c66ac9db 2251
459f213b 2252 ret = core_scsi3_update_and_write_aptpl(dev, aptpl);
de103c93 2253
bc118fe4 2254out:
fc09149d
NB
2255 if (pr_reg)
2256 core_scsi3_put_pr_reg(pr_reg);
de103c93 2257 return ret;
c66ac9db
NB
2258}
2259
2260unsigned char *core_scsi3_pr_dump_type(int type)
2261{
2262 switch (type) {
2263 case PR_TYPE_WRITE_EXCLUSIVE:
2264 return "Write Exclusive Access";
2265 case PR_TYPE_EXCLUSIVE_ACCESS:
2266 return "Exclusive Access";
2267 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2268 return "Write Exclusive Access, Registrants Only";
2269 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2270 return "Exclusive Access, Registrants Only";
2271 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2272 return "Write Exclusive Access, All Registrants";
2273 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2274 return "Exclusive Access, All Registrants";
2275 default:
2276 break;
2277 }
2278
2279 return "Unknown SPC-3 PR Type";
2280}
2281
de103c93
CH
2282static sense_reason_t
2283core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
c66ac9db 2284{
de103c93 2285 struct se_device *dev = cmd->se_dev;
e3d6f909 2286 struct se_session *se_sess = cmd->se_sess;
e3d6f909 2287 struct se_lun *se_lun = cmd->se_lun;
c66ac9db 2288 struct t10_pr_registration *pr_reg, *pr_res_holder;
0fd97ccf 2289 struct t10_reservation *pr_tmpl = &dev->t10_pr;
c66ac9db 2290 char i_buf[PR_REG_ISID_ID_LEN];
de103c93 2291 sense_reason_t ret;
c66ac9db
NB
2292
2293 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2294
6708bb27
AG
2295 if (!se_sess || !se_lun) {
2296 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
de103c93 2297 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db 2298 }
c66ac9db
NB
2299 /*
2300 * Locate the existing *pr_reg via struct se_node_acl pointers
2301 */
5951146d 2302 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
c66ac9db 2303 se_sess);
6708bb27
AG
2304 if (!pr_reg) {
2305 pr_err("SPC-3 PR: Unable to locate"
c66ac9db 2306 " PR_REGISTERED *pr_reg for RESERVE\n");
de103c93 2307 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
2308 }
2309 /*
2310 * From spc4r17 Section 5.7.9: Reserving:
2311 *
2312 * An application client creates a persistent reservation by issuing
2313 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2314 * a registered I_T nexus with the following parameters:
2315 * a) RESERVATION KEY set to the value of the reservation key that is
2316 * registered with the logical unit for the I_T nexus; and
2317 */
2318 if (res_key != pr_reg->pr_res_key) {
6708bb27 2319 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
c66ac9db
NB
2320 " does not match existing SA REGISTER res_key:"
2321 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
de103c93
CH
2322 ret = TCM_RESERVATION_CONFLICT;
2323 goto out_put_pr_reg;
c66ac9db
NB
2324 }
2325 /*
2326 * From spc4r17 Section 5.7.9: Reserving:
2327 *
2328 * From above:
2329 * b) TYPE field and SCOPE field set to the persistent reservation
2330 * being created.
2331 *
2332 * Only one persistent reservation is allowed at a time per logical unit
2333 * and that persistent reservation has a scope of LU_SCOPE.
2334 */
2335 if (scope != PR_SCOPE_LU_SCOPE) {
6708bb27 2336 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
de103c93
CH
2337 ret = TCM_INVALID_PARAMETER_LIST;
2338 goto out_put_pr_reg;
c66ac9db
NB
2339 }
2340 /*
2341 * See if we have an existing PR reservation holder pointer at
2342 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2343 * *pr_res_holder.
2344 */
2345 spin_lock(&dev->dev_reservation_lock);
2346 pr_res_holder = dev->dev_pr_res_holder;
ee1b1b9c 2347 if (pr_res_holder) {
c66ac9db
NB
2348 /*
2349 * From spc4r17 Section 5.7.9: Reserving:
2350 *
2351 * If the device server receives a PERSISTENT RESERVE OUT
2352 * command from an I_T nexus other than a persistent reservation
2353 * holder (see 5.7.10) that attempts to create a persistent
2354 * reservation when a persistent reservation already exists for
2355 * the logical unit, then the command shall be completed with
2356 * RESERVATION CONFLICT status.
2357 */
e673dc92 2358 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
c66ac9db 2359 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
6708bb27 2360 pr_err("SPC-3 PR: Attempted RESERVE from"
c66ac9db
NB
2361 " [%s]: %s while reservation already held by"
2362 " [%s]: %s, returning RESERVATION_CONFLICT\n",
30c7ca93 2363 cmd->se_tfo->fabric_name,
c66ac9db 2364 se_sess->se_node_acl->initiatorname,
30c7ca93 2365 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
2366 pr_res_holder->pr_reg_nacl->initiatorname);
2367
2368 spin_unlock(&dev->dev_reservation_lock);
de103c93
CH
2369 ret = TCM_RESERVATION_CONFLICT;
2370 goto out_put_pr_reg;
c66ac9db
NB
2371 }
2372 /*
2373 * From spc4r17 Section 5.7.9: Reserving:
2374 *
2375 * If a persistent reservation holder attempts to modify the
2376 * type or scope of an existing persistent reservation, the
2377 * command shall be completed with RESERVATION CONFLICT status.
2378 */
2379 if ((pr_res_holder->pr_res_type != type) ||
2380 (pr_res_holder->pr_res_scope != scope)) {
2381 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
6708bb27 2382 pr_err("SPC-3 PR: Attempted RESERVE from"
c66ac9db
NB
2383 " [%s]: %s trying to change TYPE and/or SCOPE,"
2384 " while reservation already held by [%s]: %s,"
2385 " returning RESERVATION_CONFLICT\n",
30c7ca93 2386 cmd->se_tfo->fabric_name,
c66ac9db 2387 se_sess->se_node_acl->initiatorname,
30c7ca93 2388 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
2389 pr_res_holder->pr_reg_nacl->initiatorname);
2390
2391 spin_unlock(&dev->dev_reservation_lock);
de103c93
CH
2392 ret = TCM_RESERVATION_CONFLICT;
2393 goto out_put_pr_reg;
c66ac9db
NB
2394 }
2395 /*
2396 * From spc4r17 Section 5.7.9: Reserving:
2397 *
2398 * If the device server receives a PERSISTENT RESERVE OUT
2399 * command with RESERVE service action where the TYPE field and
2400 * the SCOPE field contain the same values as the existing type
2401 * and scope from a persistent reservation holder, it shall not
2402 * make any change to the existing persistent reservation and
2403 * shall completethe command with GOOD status.
2404 */
2405 spin_unlock(&dev->dev_reservation_lock);
de103c93
CH
2406 ret = 0;
2407 goto out_put_pr_reg;
c66ac9db
NB
2408 }
2409 /*
2410 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2411 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2412 */
2413 pr_reg->pr_res_scope = scope;
2414 pr_reg->pr_res_type = type;
2415 pr_reg->pr_res_holder = 1;
2416 dev->dev_pr_res_holder = pr_reg;
d2843c17 2417 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db 2418
6708bb27 2419 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
c66ac9db 2420 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
30c7ca93 2421 cmd->se_tfo->fabric_name, core_scsi3_pr_dump_type(type),
c66ac9db 2422 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
6708bb27 2423 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
30c7ca93 2424 cmd->se_tfo->fabric_name,
c66ac9db 2425 se_sess->se_node_acl->initiatorname,
d2843c17 2426 i_buf);
c66ac9db
NB
2427 spin_unlock(&dev->dev_reservation_lock);
2428
459f213b
AG
2429 if (pr_tmpl->pr_aptpl_active)
2430 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
c66ac9db 2431
de103c93
CH
2432 ret = 0;
2433out_put_pr_reg:
c66ac9db 2434 core_scsi3_put_pr_reg(pr_reg);
de103c93 2435 return ret;
c66ac9db
NB
2436}
2437
de103c93
CH
2438static sense_reason_t
2439core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2440 u64 res_key)
c66ac9db 2441{
c66ac9db
NB
2442 switch (type) {
2443 case PR_TYPE_WRITE_EXCLUSIVE:
2444 case PR_TYPE_EXCLUSIVE_ACCESS:
2445 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2446 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2447 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2448 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
de103c93 2449 return core_scsi3_pro_reserve(cmd, type, scope, res_key);
c66ac9db 2450 default:
6708bb27 2451 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
c66ac9db 2452 " 0x%02x\n", type);
de103c93 2453 return TCM_INVALID_CDB_FIELD;
c66ac9db 2454 }
c66ac9db
NB
2455}
2456
c66ac9db
NB
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,
6c3c9baa
NB
2461 int explicit,
2462 int unreg)
c66ac9db 2463{
9ac8928e 2464 const struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
c66ac9db 2465 char i_buf[PR_REG_ISID_ID_LEN];
6c3c9baa 2466 int pr_res_type = 0, pr_res_scope = 0;
c66ac9db 2467
618baaf7
BVA
2468 lockdep_assert_held(&dev->dev_reservation_lock);
2469
c66ac9db 2470 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
d2843c17 2471 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db
NB
2472 /*
2473 * Go ahead and release the current PR reservation holder.
6c3c9baa
NB
2474 * If an All Registrants reservation is currently active and
2475 * a unregister operation is requested, replace the current
2476 * dev_pr_res_holder with another active registration.
2477 */
2478 if (dev->dev_pr_res_holder) {
2479 pr_res_type = dev->dev_pr_res_holder->pr_res_type;
2480 pr_res_scope = dev->dev_pr_res_holder->pr_res_scope;
2481 dev->dev_pr_res_holder->pr_res_type = 0;
2482 dev->dev_pr_res_holder->pr_res_scope = 0;
2483 dev->dev_pr_res_holder->pr_res_holder = 0;
2484 dev->dev_pr_res_holder = NULL;
2485 }
2486 if (!unreg)
2487 goto out;
c66ac9db 2488
6c3c9baa
NB
2489 spin_lock(&dev->t10_pr.registration_lock);
2490 list_del_init(&pr_reg->pr_reg_list);
2491 /*
2492 * If the I_T nexus is a reservation holder, the persistent reservation
2493 * is of an all registrants type, and the I_T nexus is the last remaining
2494 * registered I_T nexus, then the device server shall also release the
2495 * persistent reservation.
2496 */
2497 if (!list_empty(&dev->t10_pr.registration_list) &&
2498 ((pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2499 (pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))) {
2500 dev->dev_pr_res_holder =
2501 list_entry(dev->t10_pr.registration_list.next,
2502 struct t10_pr_registration, pr_reg_list);
2503 dev->dev_pr_res_holder->pr_res_type = pr_res_type;
2504 dev->dev_pr_res_holder->pr_res_scope = pr_res_scope;
2505 dev->dev_pr_res_holder->pr_res_holder = 1;
2506 }
2507 spin_unlock(&dev->t10_pr.registration_lock);
2508out:
2509 if (!dev->dev_pr_res_holder) {
2510 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2511 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
30c7ca93 2512 tfo->fabric_name, (explicit) ? "explicit" :
6c3c9baa
NB
2513 "implicit", core_scsi3_pr_dump_type(pr_res_type),
2514 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2515 }
6708bb27 2516 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
30c7ca93 2517 tfo->fabric_name, se_nacl->initiatorname,
d2843c17 2518 i_buf);
c66ac9db
NB
2519 /*
2520 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2521 */
2522 pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2523}
2524
de103c93
CH
2525static sense_reason_t
2526core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2527 u64 res_key)
c66ac9db
NB
2528{
2529 struct se_device *dev = cmd->se_dev;
e3d6f909
AG
2530 struct se_session *se_sess = cmd->se_sess;
2531 struct se_lun *se_lun = cmd->se_lun;
c66ac9db 2532 struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
0fd97ccf 2533 struct t10_reservation *pr_tmpl = &dev->t10_pr;
de103c93 2534 sense_reason_t ret = 0;
c66ac9db 2535
6708bb27
AG
2536 if (!se_sess || !se_lun) {
2537 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
de103c93 2538 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
2539 }
2540 /*
2541 * Locate the existing *pr_reg via struct se_node_acl pointers
2542 */
2543 pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
6708bb27
AG
2544 if (!pr_reg) {
2545 pr_err("SPC-3 PR: Unable to locate"
c66ac9db 2546 " PR_REGISTERED *pr_reg for RELEASE\n");
de103c93 2547 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
2548 }
2549 /*
2550 * From spc4r17 Section 5.7.11.2 Releasing:
2551 *
2552 * If there is no persistent reservation or in response to a persistent
2553 * reservation release request from a registered I_T nexus that is not a
2554 * persistent reservation holder (see 5.7.10), the device server shall
2555 * do the following:
2556 *
2557 * a) Not release the persistent reservation, if any;
2558 * b) Not remove any registrations; and
2559 * c) Complete the command with GOOD status.
2560 */
2561 spin_lock(&dev->dev_reservation_lock);
2562 pr_res_holder = dev->dev_pr_res_holder;
6708bb27 2563 if (!pr_res_holder) {
c66ac9db
NB
2564 /*
2565 * No persistent reservation, return GOOD status.
2566 */
2567 spin_unlock(&dev->dev_reservation_lock);
bb7a8c8e 2568 goto out_put_pr_reg;
c66ac9db 2569 }
c66ac9db 2570
e673dc92 2571 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
c66ac9db 2572 /*
c66ac9db
NB
2573 * Release request from a registered I_T nexus that is not a
2574 * persistent reservation holder. return GOOD status.
2575 */
2576 spin_unlock(&dev->dev_reservation_lock);
de103c93 2577 goto out_put_pr_reg;
c66ac9db 2578 }
de103c93 2579
c66ac9db
NB
2580 /*
2581 * From spc4r17 Section 5.7.11.2 Releasing:
2582 *
2583 * Only the persistent reservation holder (see 5.7.10) is allowed to
2584 * release a persistent reservation.
2585 *
2586 * An application client releases the persistent reservation by issuing
2587 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2588 * an I_T nexus that is a persistent reservation holder with the
2589 * following parameters:
2590 *
2591 * a) RESERVATION KEY field set to the value of the reservation key
2592 * that is registered with the logical unit for the I_T nexus;
2593 */
2594 if (res_key != pr_reg->pr_res_key) {
6708bb27 2595 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
c66ac9db
NB
2596 " does not match existing SA REGISTER res_key:"
2597 " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2598 spin_unlock(&dev->dev_reservation_lock);
de103c93
CH
2599 ret = TCM_RESERVATION_CONFLICT;
2600 goto out_put_pr_reg;
c66ac9db
NB
2601 }
2602 /*
2603 * From spc4r17 Section 5.7.11.2 Releasing and above:
2604 *
2605 * b) TYPE field and SCOPE field set to match the persistent
2606 * reservation being released.
2607 */
2608 if ((pr_res_holder->pr_res_type != type) ||
2609 (pr_res_holder->pr_res_scope != scope)) {
2610 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
6708bb27 2611 pr_err("SPC-3 PR RELEASE: Attempted to release"
c66ac9db
NB
2612 " reservation from [%s]: %s with different TYPE "
2613 "and/or SCOPE while reservation already held by"
2614 " [%s]: %s, returning RESERVATION_CONFLICT\n",
30c7ca93 2615 cmd->se_tfo->fabric_name,
c66ac9db 2616 se_sess->se_node_acl->initiatorname,
30c7ca93 2617 pr_res_nacl->se_tpg->se_tpg_tfo->fabric_name,
c66ac9db
NB
2618 pr_res_holder->pr_reg_nacl->initiatorname);
2619
2620 spin_unlock(&dev->dev_reservation_lock);
de103c93
CH
2621 ret = TCM_RESERVATION_CONFLICT;
2622 goto out_put_pr_reg;
c66ac9db
NB
2623 }
2624 /*
2625 * In response to a persistent reservation release request from the
2626 * persistent reservation holder the device server shall perform a
2627 * release by doing the following as an uninterrupted series of actions:
2628 * a) Release the persistent reservation;
2629 * b) Not remove any registration(s);
2630 * c) If the released persistent reservation is a registrants only type
2631 * or all registrants type persistent reservation,
2632 * the device server shall establish a unit attention condition for
2633 * the initiator port associated with every regis-
2634 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2635 * RESERVE OUT command with RELEASE service action was received,
2636 * with the additional sense code set to RESERVATIONS RELEASED; and
2637 * d) If the persistent reservation is of any other type, the device
2638 * server shall not establish a unit attention condition.
2639 */
2640 __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
6c3c9baa 2641 pr_reg, 1, 0);
c66ac9db
NB
2642
2643 spin_unlock(&dev->dev_reservation_lock);
2644
2645 if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2646 (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2647 (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2648 (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2649 /*
2650 * If no UNIT ATTENTION conditions will be established for
2651 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2652 * go ahead and check for APTPL=1 update+write below
2653 */
2654 goto write_aptpl;
2655 }
2656
2657 spin_lock(&pr_tmpl->registration_lock);
2658 list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2659 pr_reg_list) {
2660 /*
2661 * Do not establish a UNIT ATTENTION condition
2662 * for the calling I_T Nexus
2663 */
2664 if (pr_reg_p == pr_reg)
2665 continue;
2666
c51c8e7b 2667 target_ua_allocate_lun(pr_reg_p->pr_reg_nacl,
c66ac9db
NB
2668 pr_reg_p->pr_res_mapped_lun,
2669 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2670 }
2671 spin_unlock(&pr_tmpl->registration_lock);
2672
2673write_aptpl:
459f213b
AG
2674 if (pr_tmpl->pr_aptpl_active)
2675 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
2676
de103c93 2677out_put_pr_reg:
c66ac9db 2678 core_scsi3_put_pr_reg(pr_reg);
de103c93 2679 return ret;
c66ac9db
NB
2680}
2681
de103c93
CH
2682static sense_reason_t
2683core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
c66ac9db
NB
2684{
2685 struct se_device *dev = cmd->se_dev;
2686 struct se_node_acl *pr_reg_nacl;
e3d6f909 2687 struct se_session *se_sess = cmd->se_sess;
0fd97ccf 2688 struct t10_reservation *pr_tmpl = &dev->t10_pr;
c66ac9db 2689 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
f2d30680 2690 u64 pr_res_mapped_lun = 0;
c66ac9db
NB
2691 int calling_it_nexus = 0;
2692 /*
2693 * Locate the existing *pr_reg via struct se_node_acl pointers
2694 */
5951146d 2695 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
c66ac9db 2696 se_sess->se_node_acl, se_sess);
6708bb27
AG
2697 if (!pr_reg_n) {
2698 pr_err("SPC-3 PR: Unable to locate"
c66ac9db 2699 " PR_REGISTERED *pr_reg for CLEAR\n");
de103c93 2700 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
2701 }
2702 /*
2703 * From spc4r17 section 5.7.11.6, Clearing:
2704 *
2705 * Any application client may release the persistent reservation and
2706 * remove all registrations from a device server by issuing a
2707 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2708 * registered I_T nexus with the following parameter:
2709 *
2710 * a) RESERVATION KEY field set to the value of the reservation key
2711 * that is registered with the logical unit for the I_T nexus.
2712 */
2713 if (res_key != pr_reg_n->pr_res_key) {
6708bb27 2714 pr_err("SPC-3 PR REGISTER: Received"
c66ac9db
NB
2715 " res_key: 0x%016Lx does not match"
2716 " existing SA REGISTER res_key:"
2717 " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2718 core_scsi3_put_pr_reg(pr_reg_n);
de103c93 2719 return TCM_RESERVATION_CONFLICT;
c66ac9db
NB
2720 }
2721 /*
2722 * a) Release the persistent reservation, if any;
2723 */
2724 spin_lock(&dev->dev_reservation_lock);
2725 pr_res_holder = dev->dev_pr_res_holder;
2726 if (pr_res_holder) {
2727 struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2728 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
6c3c9baa 2729 pr_res_holder, 0, 0);
c66ac9db
NB
2730 }
2731 spin_unlock(&dev->dev_reservation_lock);
2732 /*
2733 * b) Remove all registration(s) (see spc4r17 5.7.7);
2734 */
2735 spin_lock(&pr_tmpl->registration_lock);
2736 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2737 &pr_tmpl->registration_list, pr_reg_list) {
2738
2739 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2740 pr_reg_nacl = pr_reg->pr_reg_nacl;
2741 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2742 __core_scsi3_free_registration(dev, pr_reg, NULL,
2743 calling_it_nexus);
2744 /*
2745 * e) Establish a unit attention condition for the initiator
2746 * port associated with every registered I_T nexus other
2747 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2748 * command with CLEAR service action was received, with the
2749 * additional sense code set to RESERVATIONS PREEMPTED.
2750 */
6708bb27 2751 if (!calling_it_nexus)
c51c8e7b 2752 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun,
c66ac9db
NB
2753 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2754 }
2755 spin_unlock(&pr_tmpl->registration_lock);
2756
6708bb27 2757 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
30c7ca93 2758 cmd->se_tfo->fabric_name);
c66ac9db 2759
459f213b 2760 core_scsi3_update_and_write_aptpl(cmd->se_dev, false);
c66ac9db
NB
2761
2762 core_scsi3_pr_generation(dev);
2763 return 0;
2764}
2765
c66ac9db
NB
2766static void __core_scsi3_complete_pro_preempt(
2767 struct se_device *dev,
2768 struct t10_pr_registration *pr_reg,
2769 struct list_head *preempt_and_abort_list,
2770 int type,
2771 int scope,
33ce6a87 2772 enum preempt_type preempt_type)
c66ac9db
NB
2773{
2774 struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
9ac8928e 2775 const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
c66ac9db 2776 char i_buf[PR_REG_ISID_ID_LEN];
c66ac9db 2777
618baaf7
BVA
2778 lockdep_assert_held(&dev->dev_reservation_lock);
2779
c66ac9db 2780 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
d2843c17 2781 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db 2782 /*
125d0119 2783 * Do an implicit RELEASE of the existing reservation.
c66ac9db
NB
2784 */
2785 if (dev->dev_pr_res_holder)
2786 __core_scsi3_complete_pro_release(dev, nacl,
6c3c9baa 2787 dev->dev_pr_res_holder, 0, 0);
c66ac9db
NB
2788
2789 dev->dev_pr_res_holder = pr_reg;
2790 pr_reg->pr_res_holder = 1;
2791 pr_reg->pr_res_type = type;
2792 pr_reg->pr_res_scope = scope;
2793
6708bb27 2794 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
c66ac9db 2795 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
30c7ca93 2796 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
c66ac9db
NB
2797 core_scsi3_pr_dump_type(type),
2798 (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
6708bb27 2799 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
30c7ca93 2800 tfo->fabric_name, (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "",
d2843c17 2801 nacl->initiatorname, i_buf);
c66ac9db
NB
2802 /*
2803 * For PREEMPT_AND_ABORT, add the preempting reservation's
2804 * struct t10_pr_registration to the list that will be compared
2805 * against received CDBs..
2806 */
2807 if (preempt_and_abort_list)
2808 list_add_tail(&pr_reg->pr_reg_abort_list,
2809 preempt_and_abort_list);
2810}
2811
2812static void core_scsi3_release_preempt_and_abort(
2813 struct list_head *preempt_and_abort_list,
2814 struct t10_pr_registration *pr_reg_holder)
2815{
2816 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2817
2818 list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2819 pr_reg_abort_list) {
2820
2821 list_del(&pr_reg->pr_reg_abort_list);
2822 if (pr_reg_holder == pr_reg)
2823 continue;
2824 if (pr_reg->pr_res_holder) {
6708bb27 2825 pr_warn("pr_reg->pr_res_holder still set\n");
c66ac9db
NB
2826 continue;
2827 }
2828
2829 pr_reg->pr_reg_deve = NULL;
2830 pr_reg->pr_reg_nacl = NULL;
c66ac9db
NB
2831 kmem_cache_free(t10_pr_reg_cache, pr_reg);
2832 }
2833}
2834
de103c93
CH
2835static sense_reason_t
2836core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
33ce6a87 2837 u64 sa_res_key, enum preempt_type preempt_type)
c66ac9db 2838{
5951146d 2839 struct se_device *dev = cmd->se_dev;
c66ac9db 2840 struct se_node_acl *pr_reg_nacl;
e3d6f909 2841 struct se_session *se_sess = cmd->se_sess;
d0f474e5 2842 LIST_HEAD(preempt_and_abort_list);
c66ac9db 2843 struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
0fd97ccf 2844 struct t10_reservation *pr_tmpl = &dev->t10_pr;
f2d30680 2845 u64 pr_res_mapped_lun = 0;
b6932ee3
SA
2846 int all_reg = 0, calling_it_nexus = 0;
2847 bool sa_res_key_unmatched = sa_res_key != 0;
de103c93 2848 int prh_type = 0, prh_scope = 0;
c66ac9db 2849
de103c93
CH
2850 if (!se_sess)
2851 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db 2852
5951146d 2853 pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
c66ac9db 2854 se_sess);
6708bb27
AG
2855 if (!pr_reg_n) {
2856 pr_err("SPC-3 PR: Unable to locate"
c66ac9db 2857 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
33ce6a87 2858 (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "");
de103c93 2859 return TCM_RESERVATION_CONFLICT;
c66ac9db
NB
2860 }
2861 if (pr_reg_n->pr_res_key != res_key) {
2862 core_scsi3_put_pr_reg(pr_reg_n);
de103c93 2863 return TCM_RESERVATION_CONFLICT;
c66ac9db
NB
2864 }
2865 if (scope != PR_SCOPE_LU_SCOPE) {
6708bb27 2866 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
c66ac9db 2867 core_scsi3_put_pr_reg(pr_reg_n);
de103c93 2868 return TCM_INVALID_PARAMETER_LIST;
c66ac9db 2869 }
c66ac9db
NB
2870
2871 spin_lock(&dev->dev_reservation_lock);
2872 pr_res_holder = dev->dev_pr_res_holder;
2873 if (pr_res_holder &&
2874 ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2875 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2876 all_reg = 1;
2877
6708bb27 2878 if (!all_reg && !sa_res_key) {
c66ac9db
NB
2879 spin_unlock(&dev->dev_reservation_lock);
2880 core_scsi3_put_pr_reg(pr_reg_n);
de103c93 2881 return TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
2882 }
2883 /*
2884 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2885 *
2886 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2887 * persistent reservation holder or there is no persistent reservation
2888 * holder (i.e., there is no persistent reservation), then the device
2889 * server shall perform a preempt by doing the following in an
2890 * uninterrupted series of actions. (See below..)
2891 */
6708bb27 2892 if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
c66ac9db
NB
2893 /*
2894 * No existing or SA Reservation Key matching reservations..
2895 *
2896 * PROUT SA PREEMPT with All Registrant type reservations are
2897 * allowed to be processed without a matching SA Reservation Key
2898 */
2899 spin_lock(&pr_tmpl->registration_lock);
2900 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2901 &pr_tmpl->registration_list, pr_reg_list) {
2902 /*
2903 * Removing of registrations in non all registrants
2904 * type reservations without a matching SA reservation
2905 * key.
2906 *
2907 * a) Remove the registrations for all I_T nexuses
2908 * specified by the SERVICE ACTION RESERVATION KEY
2909 * field;
2910 * b) Ignore the contents of the SCOPE and TYPE fields;
2911 * c) Process tasks as defined in 5.7.1; and
2912 * d) Establish a unit attention condition for the
2913 * initiator port associated with every I_T nexus
2914 * that lost its registration other than the I_T
2915 * nexus on which the PERSISTENT RESERVE OUT command
2916 * was received, with the additional sense code set
2917 * to REGISTRATIONS PREEMPTED.
2918 */
6708bb27 2919 if (!all_reg) {
c66ac9db
NB
2920 if (pr_reg->pr_res_key != sa_res_key)
2921 continue;
b6932ee3 2922 sa_res_key_unmatched = false;
c66ac9db
NB
2923
2924 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2925 pr_reg_nacl = pr_reg->pr_reg_nacl;
2926 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2927 __core_scsi3_free_registration(dev, pr_reg,
33ce6a87 2928 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
c66ac9db 2929 NULL, calling_it_nexus);
c66ac9db
NB
2930 } else {
2931 /*
2932 * Case for any existing all registrants type
2933 * reservation, follow logic in spc4r17 section
2934 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2935 *
2936 * For a ZERO SA Reservation key, release
125d0119 2937 * all other registrations and do an implicit
c66ac9db
NB
2938 * release of active persistent reservation.
2939 *
2940 * For a non-ZERO SA Reservation key, only
2941 * release the matching reservation key from
2942 * registrations.
2943 */
2944 if ((sa_res_key) &&
2945 (pr_reg->pr_res_key != sa_res_key))
2946 continue;
b6932ee3 2947 sa_res_key_unmatched = false;
c66ac9db
NB
2948
2949 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2950 if (calling_it_nexus)
2951 continue;
2952
2953 pr_reg_nacl = pr_reg->pr_reg_nacl;
2954 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2955 __core_scsi3_free_registration(dev, pr_reg,
33ce6a87 2956 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list :
c66ac9db 2957 NULL, 0);
c66ac9db 2958 }
6708bb27 2959 if (!calling_it_nexus)
c51c8e7b 2960 target_ua_allocate_lun(pr_reg_nacl,
c66ac9db 2961 pr_res_mapped_lun, 0x2A,
9e08e34e 2962 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
c66ac9db
NB
2963 }
2964 spin_unlock(&pr_tmpl->registration_lock);
2965 /*
2966 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2967 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2968 * RESERVATION KEY field to a value that does not match any
2969 * registered reservation key, then the device server shall
2970 * complete the command with RESERVATION CONFLICT status.
2971 */
b6932ee3 2972 if (sa_res_key_unmatched) {
c66ac9db
NB
2973 spin_unlock(&dev->dev_reservation_lock);
2974 core_scsi3_put_pr_reg(pr_reg_n);
de103c93 2975 return TCM_RESERVATION_CONFLICT;
c66ac9db
NB
2976 }
2977 /*
2978 * For an existing all registrants type reservation
2979 * with a zero SA rservation key, preempt the existing
2980 * reservation with the new PR type and scope.
2981 */
2982 if (pr_res_holder && all_reg && !(sa_res_key)) {
2983 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
33ce6a87
AG
2984 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
2985 type, scope, preempt_type);
c66ac9db 2986
33ce6a87 2987 if (preempt_type == PREEMPT_AND_ABORT)
c66ac9db
NB
2988 core_scsi3_release_preempt_and_abort(
2989 &preempt_and_abort_list, pr_reg_n);
2990 }
2991 spin_unlock(&dev->dev_reservation_lock);
2992
459f213b
AG
2993 if (pr_tmpl->pr_aptpl_active)
2994 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
c66ac9db
NB
2995
2996 core_scsi3_put_pr_reg(pr_reg_n);
5951146d 2997 core_scsi3_pr_generation(cmd->se_dev);
c66ac9db
NB
2998 return 0;
2999 }
3000 /*
3001 * The PREEMPTing SA reservation key matches that of the
3002 * existing persistent reservation, first, we check if
3003 * we are preempting our own reservation.
3004 * From spc4r17, section 5.7.11.4.3 Preempting
3005 * persistent reservations and registration handling
3006 *
3007 * If an all registrants persistent reservation is not
3008 * present, it is not an error for the persistent
3009 * reservation holder to preempt itself (i.e., a
3010 * PERSISTENT RESERVE OUT with a PREEMPT service action
3011 * or a PREEMPT AND ABORT service action with the
3012 * SERVICE ACTION RESERVATION KEY value equal to the
3013 * persistent reservation holder's reservation key that
3014 * is received from the persistent reservation holder).
3015 * In that case, the device server shall establish the
3016 * new persistent reservation and maintain the
3017 * registration.
3018 */
3019 prh_type = pr_res_holder->pr_res_type;
3020 prh_scope = pr_res_holder->pr_res_scope;
3021 /*
3022 * If the SERVICE ACTION RESERVATION KEY field identifies a
3023 * persistent reservation holder (see 5.7.10), the device
3024 * server shall perform a preempt by doing the following as
3025 * an uninterrupted series of actions:
3026 *
3027 * a) Release the persistent reservation for the holder
3028 * identified by the SERVICE ACTION RESERVATION KEY field;
3029 */
3030 if (pr_reg_n != pr_res_holder)
3031 __core_scsi3_complete_pro_release(dev,
6c3c9baa
NB
3032 pr_res_holder->pr_reg_nacl,
3033 dev->dev_pr_res_holder, 0, 0);
c66ac9db
NB
3034 /*
3035 * b) Remove the registrations for all I_T nexuses identified
3036 * by the SERVICE ACTION RESERVATION KEY field, except the
3037 * I_T nexus that is being used for the PERSISTENT RESERVE
3038 * OUT command. If an all registrants persistent reservation
3039 * is present and the SERVICE ACTION RESERVATION KEY field
3040 * is set to zero, then all registrations shall be removed
3041 * except for that of the I_T nexus that is being used for
3042 * the PERSISTENT RESERVE OUT command;
3043 */
3044 spin_lock(&pr_tmpl->registration_lock);
3045 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3046 &pr_tmpl->registration_list, pr_reg_list) {
3047
3048 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3049 if (calling_it_nexus)
3050 continue;
3051
3052 if (pr_reg->pr_res_key != sa_res_key)
3053 continue;
3054
3055 pr_reg_nacl = pr_reg->pr_reg_nacl;
3056 pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3057 __core_scsi3_free_registration(dev, pr_reg,
33ce6a87 3058 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
c66ac9db
NB
3059 calling_it_nexus);
3060 /*
3061 * e) Establish a unit attention condition for the initiator
3062 * port associated with every I_T nexus that lost its
3063 * persistent reservation and/or registration, with the
3064 * additional sense code set to REGISTRATIONS PREEMPTED;
3065 */
c51c8e7b 3066 target_ua_allocate_lun(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
9e08e34e 3067 ASCQ_2AH_REGISTRATIONS_PREEMPTED);
c66ac9db
NB
3068 }
3069 spin_unlock(&pr_tmpl->registration_lock);
3070 /*
3071 * c) Establish a persistent reservation for the preempting
3072 * I_T nexus using the contents of the SCOPE and TYPE fields;
3073 */
3074 __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
33ce6a87
AG
3075 (preempt_type == PREEMPT_AND_ABORT) ? &preempt_and_abort_list : NULL,
3076 type, scope, preempt_type);
c66ac9db
NB
3077 /*
3078 * d) Process tasks as defined in 5.7.1;
3079 * e) See above..
3080 * f) If the type or scope has changed, then for every I_T nexus
3081 * whose reservation key was not removed, except for the I_T
3082 * nexus on which the PERSISTENT RESERVE OUT command was
3083 * received, the device server shall establish a unit
3084 * attention condition for the initiator port associated with
3085 * that I_T nexus, with the additional sense code set to
3086 * RESERVATIONS RELEASED. If the type or scope have not
3087 * changed, then no unit attention condition(s) shall be
3088 * established for this reason.
3089 */
3090 if ((prh_type != type) || (prh_scope != scope)) {
3091 spin_lock(&pr_tmpl->registration_lock);
3092 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3093 &pr_tmpl->registration_list, pr_reg_list) {
3094
3095 calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3096 if (calling_it_nexus)
3097 continue;
3098
c51c8e7b 3099 target_ua_allocate_lun(pr_reg->pr_reg_nacl,
c66ac9db
NB
3100 pr_reg->pr_res_mapped_lun, 0x2A,
3101 ASCQ_2AH_RESERVATIONS_RELEASED);
3102 }
3103 spin_unlock(&pr_tmpl->registration_lock);
3104 }
3105 spin_unlock(&dev->dev_reservation_lock);
3106 /*
3107 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3108 * All received CDBs for the matching existing reservation and
3109 * registrations undergo ABORT_TASK logic.
3110 *
3111 * From there, core_scsi3_release_preempt_and_abort() will
3112 * release every registration in the list (which have already
3113 * been removed from the primary pr_reg list), except the
3114 * new persistent reservation holder, the calling Initiator Port.
3115 */
33ce6a87 3116 if (preempt_type == PREEMPT_AND_ABORT) {
c66ac9db
NB
3117 core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3118 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3119 pr_reg_n);
3120 }
3121
459f213b
AG
3122 if (pr_tmpl->pr_aptpl_active)
3123 core_scsi3_update_and_write_aptpl(cmd->se_dev, true);
c66ac9db
NB
3124
3125 core_scsi3_put_pr_reg(pr_reg_n);
5951146d 3126 core_scsi3_pr_generation(cmd->se_dev);
c66ac9db
NB
3127 return 0;
3128}
3129
de103c93
CH
3130static sense_reason_t
3131core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
33ce6a87 3132 u64 res_key, u64 sa_res_key, enum preempt_type preempt_type)
c66ac9db 3133{
c66ac9db
NB
3134 switch (type) {
3135 case PR_TYPE_WRITE_EXCLUSIVE:
3136 case PR_TYPE_EXCLUSIVE_ACCESS:
3137 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3138 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3139 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3140 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
de103c93 3141 return core_scsi3_pro_preempt(cmd, type, scope, res_key,
33ce6a87 3142 sa_res_key, preempt_type);
c66ac9db 3143 default:
6708bb27 3144 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
33ce6a87 3145 " Type: 0x%02x\n", (preempt_type == PREEMPT_AND_ABORT) ? "_AND_ABORT" : "", type);
de103c93 3146 return TCM_INVALID_CDB_FIELD;
c66ac9db 3147 }
c66ac9db
NB
3148}
3149
3150
de103c93
CH
3151static sense_reason_t
3152core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3153 u64 sa_res_key, int aptpl, int unreg)
c66ac9db 3154{
e3d6f909 3155 struct se_session *se_sess = cmd->se_sess;
5951146d 3156 struct se_device *dev = cmd->se_dev;
28168905 3157 struct se_dev_entry *dest_se_deve = NULL;
adf653f9 3158 struct se_lun *se_lun = cmd->se_lun, *tmp_lun;
c66ac9db 3159 struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
c66ac9db 3160 struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
9ac8928e 3161 const struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
c66ac9db 3162 struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
0fd97ccf 3163 struct t10_reservation *pr_tmpl = &dev->t10_pr;
05d1c7c0 3164 unsigned char *buf;
2650d71e 3165 const unsigned char *initiator_str;
13ba564c 3166 char *iport_ptr = NULL, i_buf[PR_REG_ISID_ID_LEN];
c66ac9db 3167 u32 tid_len, tmp_tid_len;
d2843c17 3168 int new_reg = 0, type, scope, matching_iname;
de103c93 3169 sense_reason_t ret;
c66ac9db
NB
3170 unsigned short rtpi;
3171 unsigned char proto_ident;
3172
6708bb27
AG
3173 if (!se_sess || !se_lun) {
3174 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
de103c93 3175 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db 3176 }
de103c93 3177
c66ac9db
NB
3178 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3179 se_tpg = se_sess->se_tpg;
e3d6f909 3180 tf_ops = se_tpg->se_tpg_tfo;
c66ac9db
NB
3181 /*
3182 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3183 * Register behaviors for a REGISTER AND MOVE service action
3184 *
3185 * Locate the existing *pr_reg via struct se_node_acl pointers
3186 */
5951146d 3187 pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
c66ac9db 3188 se_sess);
6708bb27
AG
3189 if (!pr_reg) {
3190 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
c66ac9db 3191 " *pr_reg for REGISTER_AND_MOVE\n");
de103c93 3192 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
3193 }
3194 /*
3195 * The provided reservation key much match the existing reservation key
3196 * provided during this initiator's I_T nexus registration.
3197 */
3198 if (res_key != pr_reg->pr_res_key) {
6708bb27 3199 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
c66ac9db
NB
3200 " res_key: 0x%016Lx does not match existing SA REGISTER"
3201 " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
de103c93
CH
3202 ret = TCM_RESERVATION_CONFLICT;
3203 goto out_put_pr_reg;
c66ac9db
NB
3204 }
3205 /*
3206 * The service active reservation key needs to be non zero
3207 */
6708bb27
AG
3208 if (!sa_res_key) {
3209 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
c66ac9db 3210 " sa_res_key\n");
de103c93
CH
3211 ret = TCM_INVALID_PARAMETER_LIST;
3212 goto out_put_pr_reg;
c66ac9db 3213 }
05d1c7c0 3214
c66ac9db
NB
3215 /*
3216 * Determine the Relative Target Port Identifier where the reservation
3217 * will be moved to for the TransportID containing SCSI initiator WWN
3218 * information.
3219 */
4949314c 3220 buf = transport_kmap_data_sg(cmd);
de103c93 3221 if (!buf) {
79dd6f2f 3222 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
de103c93
CH
3223 goto out_put_pr_reg;
3224 }
3225
a85d667e
BVA
3226 rtpi = get_unaligned_be16(&buf[18]);
3227 tid_len = get_unaligned_be32(&buf[20]);
4949314c 3228 transport_kunmap_data_sg(cmd);
05d1c7c0 3229 buf = NULL;
c66ac9db
NB
3230
3231 if ((tid_len + 24) != cmd->data_length) {
6708bb27 3232 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
c66ac9db
NB
3233 " does not equal CDB data_length: %u\n", tid_len,
3234 cmd->data_length);
de103c93
CH
3235 ret = TCM_INVALID_PARAMETER_LIST;
3236 goto out_put_pr_reg;
c66ac9db
NB
3237 }
3238
3239 spin_lock(&dev->se_port_lock);
adf653f9
CH
3240 list_for_each_entry(tmp_lun, &dev->dev_sep_list, lun_dev_link) {
3241 if (tmp_lun->lun_rtpi != rtpi)
c66ac9db 3242 continue;
adf653f9 3243 dest_se_tpg = tmp_lun->lun_tpg;
e3d6f909 3244 dest_tf_ops = dest_se_tpg->se_tpg_tfo;
6708bb27 3245 if (!dest_tf_ops)
c66ac9db
NB
3246 continue;
3247
33940d09 3248 atomic_inc_mb(&dest_se_tpg->tpg_pr_ref_count);
c66ac9db
NB
3249 spin_unlock(&dev->se_port_lock);
3250
de103c93 3251 if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
6708bb27 3252 pr_err("core_scsi3_tpg_depend_item() failed"
c66ac9db 3253 " for dest_se_tpg\n");
33940d09 3254 atomic_dec_mb(&dest_se_tpg->tpg_pr_ref_count);
de103c93
CH
3255 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3256 goto out_put_pr_reg;
c66ac9db
NB
3257 }
3258
3259 spin_lock(&dev->se_port_lock);
3260 break;
3261 }
3262 spin_unlock(&dev->se_port_lock);
3263
6708bb27
AG
3264 if (!dest_se_tpg || !dest_tf_ops) {
3265 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
c66ac9db
NB
3266 " fabric ops from Relative Target Port Identifier:"
3267 " %hu\n", rtpi);
de103c93
CH
3268 ret = TCM_INVALID_PARAMETER_LIST;
3269 goto out_put_pr_reg;
c66ac9db 3270 }
05d1c7c0 3271
4949314c 3272 buf = transport_kmap_data_sg(cmd);
de103c93 3273 if (!buf) {
79dd6f2f 3274 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
de103c93
CH
3275 goto out_put_pr_reg;
3276 }
c66ac9db 3277 proto_ident = (buf[24] & 0x0f);
8b1e1244 3278
6708bb27 3279 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
c66ac9db 3280 " 0x%02x\n", proto_ident);
8b1e1244 3281
2aeeafae 3282 if (proto_ident != dest_se_tpg->proto_id) {
6708bb27 3283 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
c66ac9db
NB
3284 " proto_ident: 0x%02x does not match ident: 0x%02x"
3285 " from fabric: %s\n", proto_ident,
2aeeafae 3286 dest_se_tpg->proto_id,
30c7ca93 3287 dest_tf_ops->fabric_name);
de103c93 3288 ret = TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
3289 goto out;
3290 }
2650d71e 3291 initiator_str = target_parse_pr_out_transport_id(dest_se_tpg,
094bb5d7 3292 &buf[24], &tmp_tid_len, &iport_ptr);
6708bb27
AG
3293 if (!initiator_str) {
3294 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
c66ac9db 3295 " initiator_str from Transport ID\n");
de103c93 3296 ret = TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
3297 goto out;
3298 }
3299
4949314c 3300 transport_kunmap_data_sg(cmd);
05d1c7c0
AG
3301 buf = NULL;
3302
6708bb27 3303 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
30c7ca93 3304 " %s\n", dest_tf_ops->fabric_name, (iport_ptr != NULL) ?
c66ac9db
NB
3305 "port" : "device", initiator_str, (iport_ptr != NULL) ?
3306 iport_ptr : "");
3307 /*
3308 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3309 * action specifies a TransportID that is the same as the initiator port
3310 * of the I_T nexus for the command received, then the command shall
3311 * be terminated with CHECK CONDITION status, with the sense key set to
3312 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3313 * IN PARAMETER LIST.
3314 */
3315 pr_reg_nacl = pr_reg->pr_reg_nacl;
3316 matching_iname = (!strcmp(initiator_str,
3317 pr_reg_nacl->initiatorname)) ? 1 : 0;
6708bb27 3318 if (!matching_iname)
c66ac9db
NB
3319 goto after_iport_check;
3320
6708bb27
AG
3321 if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3322 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
c66ac9db
NB
3323 " matches: %s on received I_T Nexus\n", initiator_str,
3324 pr_reg_nacl->initiatorname);
de103c93 3325 ret = TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
3326 goto out;
3327 }
6708bb27
AG
3328 if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3329 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
c66ac9db
NB
3330 " matches: %s %s on received I_T Nexus\n",
3331 initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3332 pr_reg->pr_reg_isid);
de103c93 3333 ret = TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
3334 goto out;
3335 }
3336after_iport_check:
3337 /*
3338 * Locate the destination struct se_node_acl from the received Transport ID
3339 */
403edd78 3340 mutex_lock(&dest_se_tpg->acl_node_mutex);
c66ac9db
NB
3341 dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3342 initiator_str);
33940d09
JE
3343 if (dest_node_acl)
3344 atomic_inc_mb(&dest_node_acl->acl_pr_ref_count);
403edd78 3345 mutex_unlock(&dest_se_tpg->acl_node_mutex);
c66ac9db 3346
6708bb27
AG
3347 if (!dest_node_acl) {
3348 pr_err("Unable to locate %s dest_node_acl for"
30c7ca93 3349 " TransportID%s\n", dest_tf_ops->fabric_name,
c66ac9db 3350 initiator_str);
de103c93 3351 ret = TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
3352 goto out;
3353 }
de103c93
CH
3354
3355 if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
6708bb27 3356 pr_err("core_scsi3_nodeacl_depend_item() for"
c66ac9db 3357 " dest_node_acl\n");
33940d09 3358 atomic_dec_mb(&dest_node_acl->acl_pr_ref_count);
c66ac9db 3359 dest_node_acl = NULL;
de103c93 3360 ret = TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
3361 goto out;
3362 }
8b1e1244 3363
6708bb27 3364 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
30c7ca93 3365 " %s from TransportID\n", dest_tf_ops->fabric_name,
c66ac9db 3366 dest_node_acl->initiatorname);
8b1e1244 3367
c66ac9db
NB
3368 /*
3369 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3370 * PORT IDENTIFIER.
3371 */
3372 dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
6708bb27
AG
3373 if (!dest_se_deve) {
3374 pr_err("Unable to locate %s dest_se_deve from RTPI:"
30c7ca93 3375 " %hu\n", dest_tf_ops->fabric_name, rtpi);
de103c93 3376 ret = TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
3377 goto out;
3378 }
3379
de103c93 3380 if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
6708bb27 3381 pr_err("core_scsi3_lunacl_depend_item() failed\n");
29a05dee 3382 kref_put(&dest_se_deve->pr_kref, target_pr_kref_release);
c66ac9db 3383 dest_se_deve = NULL;
de103c93 3384 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
3385 goto out;
3386 }
8b1e1244 3387
6708bb27 3388 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
f2d30680 3389 " ACL for dest_se_deve->mapped_lun: %llu\n",
30c7ca93 3390 dest_tf_ops->fabric_name, dest_node_acl->initiatorname,
c66ac9db 3391 dest_se_deve->mapped_lun);
8b1e1244 3392
c66ac9db
NB
3393 /*
3394 * A persistent reservation needs to already existing in order to
3395 * successfully complete the REGISTER_AND_MOVE service action..
3396 */
3397 spin_lock(&dev->dev_reservation_lock);
3398 pr_res_holder = dev->dev_pr_res_holder;
6708bb27
AG
3399 if (!pr_res_holder) {
3400 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
c66ac9db
NB
3401 " currently held\n");
3402 spin_unlock(&dev->dev_reservation_lock);
de103c93 3403 ret = TCM_INVALID_CDB_FIELD;
c66ac9db
NB
3404 goto out;
3405 }
3406 /*
3407 * The received on I_T Nexus must be the reservation holder.
3408 *
3409 * From spc4r17 section 5.7.8 Table 50 --
3410 * Register behaviors for a REGISTER AND MOVE service action
3411 */
e673dc92 3412 if (!is_reservation_holder(pr_res_holder, pr_reg)) {
6708bb27 3413 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
c66ac9db
NB
3414 " Nexus is not reservation holder\n");
3415 spin_unlock(&dev->dev_reservation_lock);
de103c93 3416 ret = TCM_RESERVATION_CONFLICT;
c66ac9db
NB
3417 goto out;
3418 }
3419 /*
3420 * From spc4r17 section 5.7.8: registering and moving reservation
3421 *
3422 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3423 * action is received and the established persistent reservation is a
3424 * Write Exclusive - All Registrants type or Exclusive Access -
3425 * All Registrants type reservation, then the command shall be completed
3426 * with RESERVATION CONFLICT status.
3427 */
3428 if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3429 (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
6708bb27 3430 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
c66ac9db
NB
3431 " reservation for type: %s\n",
3432 core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3433 spin_unlock(&dev->dev_reservation_lock);
de103c93 3434 ret = TCM_RESERVATION_CONFLICT;
c66ac9db
NB
3435 goto out;
3436 }
3437 pr_res_nacl = pr_res_holder->pr_reg_nacl;
3438 /*
3439 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3440 */
3441 type = pr_res_holder->pr_res_type;
3442 scope = pr_res_holder->pr_res_type;
3443 /*
3444 * c) Associate the reservation key specified in the SERVICE ACTION
3445 * RESERVATION KEY field with the I_T nexus specified as the
3446 * destination of the register and move, where:
3447 * A) The I_T nexus is specified by the TransportID and the
3448 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3449 * B) Regardless of the TransportID format used, the association for
3450 * the initiator port is based on either the initiator port name
3451 * (see 3.1.71) on SCSI transport protocols where port names are
3452 * required or the initiator port identifier (see 3.1.70) on SCSI
3453 * transport protocols where port names are not required;
3454 * d) Register the reservation key specified in the SERVICE ACTION
3455 * RESERVATION KEY field;
3456 * e) Retain the reservation key specified in the SERVICE ACTION
3457 * RESERVATION KEY field and associated information;
3458 *
3459 * Also, It is not an error for a REGISTER AND MOVE service action to
3460 * register an I_T nexus that is already registered with the same
3461 * reservation key or a different reservation key.
3462 */
3463 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3464 iport_ptr);
6708bb27 3465 if (!dest_pr_reg) {
79dc9c9e 3466 struct se_lun *dest_lun = rcu_dereference_check(dest_se_deve->se_lun,
2c935bc5 3467 kref_read(&dest_se_deve->pr_kref) != 0);
79dc9c9e 3468
80bfdfa9 3469 spin_unlock(&dev->dev_reservation_lock);
79dc9c9e
NB
3470 if (core_scsi3_alloc_registration(cmd->se_dev, dest_node_acl,
3471 dest_lun, dest_se_deve, dest_se_deve->mapped_lun,
3472 iport_ptr, sa_res_key, 0, aptpl, 2, 1)) {
79dd6f2f 3473 ret = TCM_INSUFFICIENT_REGISTRATION_RESOURCES;
c66ac9db
NB
3474 goto out;
3475 }
80bfdfa9 3476 spin_lock(&dev->dev_reservation_lock);
c66ac9db
NB
3477 dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3478 iport_ptr);
3479 new_reg = 1;
3480 }
3481 /*
3482 * f) Release the persistent reservation for the persistent reservation
3483 * holder (i.e., the I_T nexus on which the
3484 */
3485 __core_scsi3_complete_pro_release(dev, pr_res_nacl,
6c3c9baa 3486 dev->dev_pr_res_holder, 0, 0);
c66ac9db
NB
3487 /*
3488 * g) Move the persistent reservation to the specified I_T nexus using
3489 * the same scope and type as the persistent reservation released in
3490 * item f); and
3491 */
3492 dev->dev_pr_res_holder = dest_pr_reg;
3493 dest_pr_reg->pr_res_holder = 1;
3494 dest_pr_reg->pr_res_type = type;
3495 pr_reg->pr_res_scope = scope;
d2843c17 3496 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
c66ac9db
NB
3497 /*
3498 * Increment PRGeneration for existing registrations..
3499 */
6708bb27 3500 if (!new_reg)
c66ac9db
NB
3501 dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3502 spin_unlock(&dev->dev_reservation_lock);
3503
6708bb27 3504 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
c66ac9db 3505 " created new reservation holder TYPE: %s on object RTPI:"
30c7ca93 3506 " %hu PRGeneration: 0x%08x\n", dest_tf_ops->fabric_name,
c66ac9db
NB
3507 core_scsi3_pr_dump_type(type), rtpi,
3508 dest_pr_reg->pr_res_generation);
6708bb27 3509 pr_debug("SPC-3 PR Successfully moved reservation from"
c66ac9db 3510 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
30c7ca93
DD
3511 tf_ops->fabric_name, pr_reg_nacl->initiatorname,
3512 i_buf, dest_tf_ops->fabric_name,
c66ac9db
NB
3513 dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3514 iport_ptr : "");
3515 /*
3516 * It is now safe to release configfs group dependencies for destination
3517 * of Transport ID Initiator Device/Port Identifier
3518 */
3519 core_scsi3_lunacl_undepend_item(dest_se_deve);
3520 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3521 core_scsi3_tpg_undepend_item(dest_se_tpg);
3522 /*
3523 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3524 * nexus on which PERSISTENT RESERVE OUT command was received.
3525 */
3526 if (unreg) {
3527 spin_lock(&pr_tmpl->registration_lock);
3528 __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3529 spin_unlock(&pr_tmpl->registration_lock);
3530 } else
3531 core_scsi3_put_pr_reg(pr_reg);
3532
459f213b 3533 core_scsi3_update_and_write_aptpl(cmd->se_dev, aptpl);
c66ac9db
NB
3534
3535 core_scsi3_put_pr_reg(dest_pr_reg);
3536 return 0;
3537out:
05d1c7c0 3538 if (buf)
4949314c 3539 transport_kunmap_data_sg(cmd);
c66ac9db
NB
3540 if (dest_se_deve)
3541 core_scsi3_lunacl_undepend_item(dest_se_deve);
3542 if (dest_node_acl)
3543 core_scsi3_nodeacl_undepend_item(dest_node_acl);
3544 core_scsi3_tpg_undepend_item(dest_se_tpg);
de103c93
CH
3545
3546out_put_pr_reg:
c66ac9db
NB
3547 core_scsi3_put_pr_reg(pr_reg);
3548 return ret;
3549}
3550
c66ac9db
NB
3551/*
3552 * See spc4r17 section 6.14 Table 170
3553 */
de103c93
CH
3554sense_reason_t
3555target_scsi3_emulate_pr_out(struct se_cmd *cmd)
c66ac9db 3556{
92404e60 3557 struct se_device *dev = cmd->se_dev;
617c0e06 3558 unsigned char *cdb = &cmd->t_task_cdb[0];
05d1c7c0 3559 unsigned char *buf;
c66ac9db
NB
3560 u64 res_key, sa_res_key;
3561 int sa, scope, type, aptpl;
3562 int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
de103c93 3563 sense_reason_t ret;
617c0e06
CH
3564
3565 /*
3566 * Following spc2r20 5.5.1 Reservations overview:
3567 *
3568 * If a logical unit has been reserved by any RESERVE command and is
3569 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3570 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3571 * initiator or service action and shall terminate with a RESERVATION
3572 * CONFLICT status.
3573 */
0fd97ccf 3574 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
617c0e06
CH
3575 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3576 " SPC-2 reservation is held, returning"
3577 " RESERVATION_CONFLICT\n");
de103c93 3578 return TCM_RESERVATION_CONFLICT;
617c0e06
CH
3579 }
3580
c66ac9db
NB
3581 /*
3582 * FIXME: A NULL struct se_session pointer means an this is not coming from
3583 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3584 */
de103c93
CH
3585 if (!cmd->se_sess)
3586 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
3587
3588 if (cmd->data_length < 24) {
6708bb27 3589 pr_warn("SPC-PR: Received PR OUT parameter list"
c66ac9db 3590 " length too small: %u\n", cmd->data_length);
7ee00317 3591 return TCM_PARAMETER_LIST_LENGTH_ERROR;
c66ac9db 3592 }
de103c93 3593
c66ac9db
NB
3594 /*
3595 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3596 */
3597 sa = (cdb[1] & 0x1f);
3598 scope = (cdb[2] & 0xf0);
3599 type = (cdb[2] & 0x0f);
05d1c7c0 3600
4949314c 3601 buf = transport_kmap_data_sg(cmd);
de103c93
CH
3602 if (!buf)
3603 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3604
c66ac9db
NB
3605 /*
3606 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3607 */
a85d667e
BVA
3608 res_key = get_unaligned_be64(&buf[0]);
3609 sa_res_key = get_unaligned_be64(&buf[8]);
c66ac9db
NB
3610 /*
3611 * REGISTER_AND_MOVE uses a different SA parameter list containing
3612 * SCSI TransportIDs.
3613 */
3614 if (sa != PRO_REGISTER_AND_MOVE) {
3615 spec_i_pt = (buf[20] & 0x08);
3616 all_tg_pt = (buf[20] & 0x04);
3617 aptpl = (buf[20] & 0x01);
3618 } else {
3619 aptpl = (buf[17] & 0x01);
3620 unreg = (buf[17] & 0x02);
3621 }
92404e60
NB
3622 /*
3623 * If the backend device has been configured to force APTPL metadata
3624 * write-out, go ahead and propigate aptpl=1 down now.
3625 */
3626 if (dev->dev_attrib.force_pr_aptpl)
3627 aptpl = 1;
3628
4949314c 3629 transport_kunmap_data_sg(cmd);
05d1c7c0
AG
3630 buf = NULL;
3631
c66ac9db
NB
3632 /*
3633 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3634 */
7ee00317 3635 if (spec_i_pt && (sa != PRO_REGISTER))
de103c93 3636 return TCM_INVALID_PARAMETER_LIST;
d29a5b6a 3637
c66ac9db
NB
3638 /*
3639 * From spc4r17 section 6.14:
3640 *
3641 * If the SPEC_I_PT bit is set to zero, the service action is not
3642 * REGISTER AND MOVE, and the parameter list length is not 24, then
3643 * the command shall be terminated with CHECK CONDITION status, with
3644 * the sense key set to ILLEGAL REQUEST, and the additional sense
3645 * code set to PARAMETER LIST LENGTH ERROR.
3646 */
7ee00317 3647 if (!spec_i_pt && (sa != PRO_REGISTER_AND_MOVE) &&
c66ac9db 3648 (cmd->data_length != 24)) {
6708bb27 3649 pr_warn("SPC-PR: Received PR OUT illegal parameter"
c66ac9db 3650 " list length: %u\n", cmd->data_length);
7ee00317 3651 return TCM_PARAMETER_LIST_LENGTH_ERROR;
c66ac9db 3652 }
de103c93 3653
c66ac9db
NB
3654 /*
3655 * (core_scsi3_emulate_pro_* function parameters
3656 * are defined by spc4r17 Table 174:
3657 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3658 */
3659 switch (sa) {
3660 case PRO_REGISTER:
d29a5b6a 3661 ret = core_scsi3_emulate_pro_register(cmd,
33ce6a87 3662 res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER);
d29a5b6a 3663 break;
c66ac9db 3664 case PRO_RESERVE:
d29a5b6a
CH
3665 ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3666 break;
c66ac9db 3667 case PRO_RELEASE:
d29a5b6a
CH
3668 ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3669 break;
c66ac9db 3670 case PRO_CLEAR:
d29a5b6a
CH
3671 ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3672 break;
c66ac9db 3673 case PRO_PREEMPT:
d29a5b6a 3674 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
33ce6a87 3675 res_key, sa_res_key, PREEMPT);
d29a5b6a 3676 break;
c66ac9db 3677 case PRO_PREEMPT_AND_ABORT:
d29a5b6a 3678 ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
33ce6a87 3679 res_key, sa_res_key, PREEMPT_AND_ABORT);
d29a5b6a 3680 break;
c66ac9db 3681 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
d29a5b6a 3682 ret = core_scsi3_emulate_pro_register(cmd,
33ce6a87 3683 0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, REGISTER_AND_IGNORE_EXISTING_KEY);
d29a5b6a 3684 break;
c66ac9db 3685 case PRO_REGISTER_AND_MOVE:
d29a5b6a 3686 ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
c66ac9db 3687 sa_res_key, aptpl, unreg);
d29a5b6a 3688 break;
c66ac9db 3689 default:
6708bb27 3690 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
7ee00317 3691 " action: 0x%02x\n", sa);
de103c93 3692 return TCM_INVALID_CDB_FIELD;
c66ac9db
NB
3693 }
3694
6bb35e00
CH
3695 if (!ret)
3696 target_complete_cmd(cmd, GOOD);
d29a5b6a 3697 return ret;
c66ac9db
NB
3698}
3699
3700/*
3701 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3702 *
3703 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3704 */
de103c93
CH
3705static sense_reason_t
3706core_scsi3_pri_read_keys(struct se_cmd *cmd)
c66ac9db 3707{
0fd97ccf 3708 struct se_device *dev = cmd->se_dev;
c66ac9db 3709 struct t10_pr_registration *pr_reg;
05d1c7c0 3710 unsigned char *buf;
c66ac9db
NB
3711 u32 add_len = 0, off = 8;
3712
3713 if (cmd->data_length < 8) {
6708bb27 3714 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
c66ac9db 3715 " too small\n", cmd->data_length);
de103c93 3716 return TCM_INVALID_CDB_FIELD;
c66ac9db
NB
3717 }
3718
4949314c 3719 buf = transport_kmap_data_sg(cmd);
de103c93
CH
3720 if (!buf)
3721 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3722
a85d667e 3723 put_unaligned_be32(dev->t10_pr.pr_generation, buf);
c66ac9db 3724
0fd97ccf
CH
3725 spin_lock(&dev->t10_pr.registration_lock);
3726 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
c66ac9db
NB
3727 pr_reg_list) {
3728 /*
3729 * Check for overflow of 8byte PRI READ_KEYS payload and
3730 * next reservation key list descriptor.
3731 */
63ce3c38
DD
3732 if (off + 8 <= cmd->data_length) {
3733 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3734 off += 8;
3735 }
3736 /*
3737 * SPC5r17: 6.16.2 READ KEYS service action
3738 * The ADDITIONAL LENGTH field indicates the number of bytes in
3739 * the Reservation key list. The contents of the ADDITIONAL
3740 * LENGTH field are not altered based on the allocation length
3741 */
c66ac9db
NB
3742 add_len += 8;
3743 }
0fd97ccf 3744 spin_unlock(&dev->t10_pr.registration_lock);
c66ac9db 3745
a85d667e 3746 put_unaligned_be32(add_len, &buf[4]);
c66ac9db 3747
4949314c 3748 transport_kunmap_data_sg(cmd);
05d1c7c0 3749
c66ac9db
NB
3750 return 0;
3751}
3752
3753/*
3754 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3755 *
3756 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3757 */
de103c93
CH
3758static sense_reason_t
3759core_scsi3_pri_read_reservation(struct se_cmd *cmd)
c66ac9db 3760{
0fd97ccf 3761 struct se_device *dev = cmd->se_dev;
c66ac9db 3762 struct t10_pr_registration *pr_reg;
05d1c7c0 3763 unsigned char *buf;
c66ac9db
NB
3764 u64 pr_res_key;
3765 u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3766
3767 if (cmd->data_length < 8) {
6708bb27 3768 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
c66ac9db 3769 " too small\n", cmd->data_length);
de103c93 3770 return TCM_INVALID_CDB_FIELD;
c66ac9db
NB
3771 }
3772
4949314c 3773 buf = transport_kmap_data_sg(cmd);
de103c93
CH
3774 if (!buf)
3775 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3776
a85d667e 3777 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
c66ac9db 3778
0fd97ccf
CH
3779 spin_lock(&dev->dev_reservation_lock);
3780 pr_reg = dev->dev_pr_res_holder;
ee1b1b9c 3781 if (pr_reg) {
c66ac9db
NB
3782 /*
3783 * Set the hardcoded Additional Length
3784 */
a85d667e 3785 put_unaligned_be32(add_len, &buf[4]);
c66ac9db 3786
05d1c7c0
AG
3787 if (cmd->data_length < 22)
3788 goto err;
3789
c66ac9db
NB
3790 /*
3791 * Set the Reservation key.
3792 *
3793 * From spc4r17, section 5.7.10:
3794 * A persistent reservation holder has its reservation key
3795 * returned in the parameter data from a PERSISTENT
3796 * RESERVE IN command with READ RESERVATION service action as
3797 * follows:
3798 * a) For a persistent reservation of the type Write Exclusive
3799 * - All Registrants or Exclusive Access ­ All Regitrants,
3800 * the reservation key shall be set to zero; or
3801 * b) For all other persistent reservation types, the
3802 * reservation key shall be set to the registered
3803 * reservation key for the I_T nexus that holds the
3804 * persistent reservation.
3805 */
3806 if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3807 (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3808 pr_res_key = 0;
3809 else
3810 pr_res_key = pr_reg->pr_res_key;
3811
a85d667e 3812 put_unaligned_be64(pr_res_key, &buf[8]);
c66ac9db
NB
3813 /*
3814 * Set the SCOPE and TYPE
3815 */
3816 buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3817 (pr_reg->pr_res_type & 0x0f);
3818 }
05d1c7c0
AG
3819
3820err:
0fd97ccf 3821 spin_unlock(&dev->dev_reservation_lock);
4949314c 3822 transport_kunmap_data_sg(cmd);
c66ac9db
NB
3823
3824 return 0;
3825}
3826
3827/*
3828 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3829 *
3830 * See spc4r17 section 6.13.4 Table 165
3831 */
de103c93
CH
3832static sense_reason_t
3833core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
c66ac9db 3834{
5951146d 3835 struct se_device *dev = cmd->se_dev;
0fd97ccf 3836 struct t10_reservation *pr_tmpl = &dev->t10_pr;
05d1c7c0 3837 unsigned char *buf;
c66ac9db
NB
3838 u16 add_len = 8; /* Hardcoded to 8. */
3839
3840 if (cmd->data_length < 6) {
6708bb27 3841 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
c66ac9db 3842 " %u too small\n", cmd->data_length);
de103c93 3843 return TCM_INVALID_CDB_FIELD;
c66ac9db
NB
3844 }
3845
4949314c 3846 buf = transport_kmap_data_sg(cmd);
de103c93
CH
3847 if (!buf)
3848 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
05d1c7c0 3849
a85d667e 3850 put_unaligned_be16(add_len, &buf[0]);
c66ac9db
NB
3851 buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3852 buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3853 buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3854 buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3855 /*
3856 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3857 * set the TMV: Task Mask Valid bit.
3858 */
3859 buf[3] |= 0x80;
3860 /*
3861 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3862 */
3863 buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3864 /*
3865 * PTPL_A: Persistence across Target Power Loss Active bit
3866 */
3867 if (pr_tmpl->pr_aptpl_active)
3868 buf[3] |= 0x01;
3869 /*
3870 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3871 */
3872 buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3873 buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3874 buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3875 buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3876 buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3877 buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3878
4949314c 3879 transport_kunmap_data_sg(cmd);
05d1c7c0 3880
c66ac9db
NB
3881 return 0;
3882}
3883
3884/*
3885 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3886 *
3887 * See spc4r17 section 6.13.5 Table 168 and 169
3888 */
de103c93
CH
3889static sense_reason_t
3890core_scsi3_pri_read_full_status(struct se_cmd *cmd)
c66ac9db 3891{
0fd97ccf 3892 struct se_device *dev = cmd->se_dev;
c66ac9db 3893 struct se_node_acl *se_nacl;
c66ac9db
NB
3894 struct se_portal_group *se_tpg;
3895 struct t10_pr_registration *pr_reg, *pr_reg_tmp;
0fd97ccf 3896 struct t10_reservation *pr_tmpl = &dev->t10_pr;
05d1c7c0 3897 unsigned char *buf;
2650d71e 3898 u32 add_desc_len = 0, add_len = 0;
c66ac9db 3899 u32 off = 8; /* off into first Full Status descriptor */
d16ca7c5 3900 int format_code = 0, pr_res_type = 0, pr_res_scope = 0;
2650d71e 3901 int exp_desc_len, desc_len;
d16ca7c5 3902 bool all_reg = false;
c66ac9db
NB
3903
3904 if (cmd->data_length < 8) {
6708bb27 3905 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
c66ac9db 3906 " too small\n", cmd->data_length);
de103c93 3907 return TCM_INVALID_CDB_FIELD;
c66ac9db
NB
3908 }
3909
4949314c 3910 buf = transport_kmap_data_sg(cmd);
de103c93
CH
3911 if (!buf)
3912 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
05d1c7c0 3913
a85d667e 3914 put_unaligned_be32(dev->t10_pr.pr_generation, &buf[0]);
c66ac9db 3915
d16ca7c5
NB
3916 spin_lock(&dev->dev_reservation_lock);
3917 if (dev->dev_pr_res_holder) {
3918 struct t10_pr_registration *pr_holder = dev->dev_pr_res_holder;
3919
3920 if (pr_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG ||
3921 pr_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG) {
3922 all_reg = true;
3923 pr_res_type = pr_holder->pr_res_type;
3924 pr_res_scope = pr_holder->pr_res_scope;
3925 }
3926 }
3927 spin_unlock(&dev->dev_reservation_lock);
3928
c66ac9db
NB
3929 spin_lock(&pr_tmpl->registration_lock);
3930 list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3931 &pr_tmpl->registration_list, pr_reg_list) {
3932
3933 se_nacl = pr_reg->pr_reg_nacl;
3934 se_tpg = pr_reg->pr_reg_nacl->se_tpg;
3935 add_desc_len = 0;
3936
33940d09 3937 atomic_inc_mb(&pr_reg->pr_res_holders);
c66ac9db
NB
3938 spin_unlock(&pr_tmpl->registration_lock);
3939 /*
3940 * Determine expected length of $FABRIC_MOD specific
3941 * TransportID full status descriptor..
3942 */
2650d71e
CH
3943 exp_desc_len = target_get_pr_transport_id_len(se_nacl, pr_reg,
3944 &format_code);
3945 if (exp_desc_len < 0 ||
3946 exp_desc_len + add_len > cmd->data_length) {
6708bb27 3947 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
c66ac9db
NB
3948 " out of buffer: %d\n", cmd->data_length);
3949 spin_lock(&pr_tmpl->registration_lock);
33940d09 3950 atomic_dec_mb(&pr_reg->pr_res_holders);
c66ac9db
NB
3951 break;
3952 }
3953 /*
3954 * Set RESERVATION KEY
3955 */
a85d667e
BVA
3956 put_unaligned_be64(pr_reg->pr_res_key, &buf[off]);
3957 off += 8;
c66ac9db
NB
3958 off += 4; /* Skip Over Reserved area */
3959
3960 /*
3961 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3962 */
3963 if (pr_reg->pr_reg_all_tg_pt)
3964 buf[off] = 0x02;
3965 /*
3966 * The struct se_lun pointer will be present for the
3967 * reservation holder for PR_HOLDER bit.
3968 *
3969 * Also, if this registration is the reservation
d16ca7c5
NB
3970 * holder or there is an All Registrants reservation
3971 * active, fill in SCOPE and TYPE in the next byte.
c66ac9db
NB
3972 */
3973 if (pr_reg->pr_res_holder) {
3974 buf[off++] |= 0x01;
3975 buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
3976 (pr_reg->pr_res_type & 0x0f);
d16ca7c5
NB
3977 } else if (all_reg) {
3978 buf[off++] |= 0x01;
3979 buf[off++] = (pr_res_scope & 0xf0) |
3980 (pr_res_type & 0x0f);
3981 } else {
c66ac9db 3982 off += 2;
d16ca7c5 3983 }
c66ac9db
NB
3984
3985 off += 4; /* Skip over reserved area */
3986 /*
3987 * From spc4r17 6.3.15:
3988 *
3989 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
3990 * IDENTIFIER field contains the relative port identifier (see
3991 * 3.1.120) of the target port that is part of the I_T nexus
3992 * described by this full status descriptor. If the ALL_TG_PT
3993 * bit is set to one, the contents of the RELATIVE TARGET PORT
3994 * IDENTIFIER field are not defined by this standard.
3995 */
6708bb27 3996 if (!pr_reg->pr_reg_all_tg_pt) {
79dc9c9e 3997 u16 sep_rtpi = pr_reg->tg_pt_sep_rtpi;
c66ac9db 3998
a85d667e
BVA
3999 put_unaligned_be16(sep_rtpi, &buf[off]);
4000 off += 2;
c66ac9db 4001 } else
35d1efe8 4002 off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
c66ac9db 4003
2650d71e
CH
4004 buf[off+4] = se_tpg->proto_id;
4005
c66ac9db 4006 /*
2650d71e 4007 * Now, have the $FABRIC_MOD fill in the transport ID.
c66ac9db 4008 */
2650d71e
CH
4009 desc_len = target_get_pr_transport_id(se_nacl, pr_reg,
4010 &format_code, &buf[off+4]);
c66ac9db
NB
4011
4012 spin_lock(&pr_tmpl->registration_lock);
33940d09 4013 atomic_dec_mb(&pr_reg->pr_res_holders);
2650d71e
CH
4014
4015 if (desc_len < 0)
4016 break;
c66ac9db
NB
4017 /*
4018 * Set the ADDITIONAL DESCRIPTOR LENGTH
4019 */
a85d667e 4020 put_unaligned_be32(desc_len, &buf[off]);
c58a252b 4021 off += 4;
c66ac9db
NB
4022 /*
4023 * Size of full desctipor header minus TransportID
4024 * containing $FABRIC_MOD specific) initiator device/port
4025 * WWN information.
4026 *
4027 * See spc4r17 Section 6.13.5 Table 169
4028 */
4029 add_desc_len = (24 + desc_len);
4030
4031 off += desc_len;
4032 add_len += add_desc_len;
4033 }
4034 spin_unlock(&pr_tmpl->registration_lock);
4035 /*
4036 * Set ADDITIONAL_LENGTH
4037 */
a85d667e 4038 put_unaligned_be32(add_len, &buf[4]);
c66ac9db 4039
4949314c 4040 transport_kunmap_data_sg(cmd);
05d1c7c0 4041
c66ac9db
NB
4042 return 0;
4043}
4044
de103c93
CH
4045sense_reason_t
4046target_scsi3_emulate_pr_in(struct se_cmd *cmd)
c66ac9db 4047{
de103c93 4048 sense_reason_t ret;
e76a35d6 4049
c66ac9db
NB
4050 /*
4051 * Following spc2r20 5.5.1 Reservations overview:
4052 *
4053 * If a logical unit has been reserved by any RESERVE command and is
4054 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4055 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4056 * initiator or service action and shall terminate with a RESERVATION
4057 * CONFLICT status.
4058 */
0fd97ccf 4059 if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
6708bb27 4060 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
c66ac9db
NB
4061 " SPC-2 reservation is held, returning"
4062 " RESERVATION_CONFLICT\n");
de103c93 4063 return TCM_RESERVATION_CONFLICT;
c66ac9db
NB
4064 }
4065
617c0e06
CH
4066 switch (cmd->t_task_cdb[1] & 0x1f) {
4067 case PRI_READ_KEYS:
d29a5b6a
CH
4068 ret = core_scsi3_pri_read_keys(cmd);
4069 break;
617c0e06 4070 case PRI_READ_RESERVATION:
d29a5b6a
CH
4071 ret = core_scsi3_pri_read_reservation(cmd);
4072 break;
617c0e06 4073 case PRI_REPORT_CAPABILITIES:
d29a5b6a
CH
4074 ret = core_scsi3_pri_report_capabilities(cmd);
4075 break;
617c0e06 4076 case PRI_READ_FULL_STATUS:
d29a5b6a
CH
4077 ret = core_scsi3_pri_read_full_status(cmd);
4078 break;
617c0e06
CH
4079 default:
4080 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4081 " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
de103c93 4082 return TCM_INVALID_CDB_FIELD;
d29a5b6a
CH
4083 }
4084
6bb35e00
CH
4085 if (!ret)
4086 target_complete_cmd(cmd, GOOD);
d29a5b6a 4087 return ret;
c66ac9db
NB
4088}
4089
de103c93
CH
4090sense_reason_t
4091target_check_reservation(struct se_cmd *cmd)
c66ac9db 4092{
d977f437 4093 struct se_device *dev = cmd->se_dev;
de103c93 4094 sense_reason_t ret;
c66ac9db 4095
d977f437
CH
4096 if (!cmd->se_sess)
4097 return 0;
4098 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4099 return 0;
b49d6f78
DD
4100 if (!dev->dev_attrib.emulate_pr)
4101 return 0;
4ec5bf0e 4102 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
d977f437 4103 return 0;
c66ac9db 4104
d977f437
CH
4105 spin_lock(&dev->dev_reservation_lock);
4106 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4107 ret = target_scsi2_reservation_check(cmd);
4108 else
4109 ret = target_scsi3_pr_reservation_check(cmd);
4110 spin_unlock(&dev->dev_reservation_lock);
0fd97ccf 4111
d977f437 4112 return ret;
c66ac9db 4113}