1 /*******************************************************************************
2 * Filename: target_core_pr.c
4 * This file contains SPC-3 compliant persistent reservations and
5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
7 * (c) Copyright 2009-2012 RisingTide Systems LLC.
9 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/list.h>
30 #include <linux/file.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_pr.h"
42 #include "target_core_ua.h"
45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
47 struct pr_transport_id_holder
{
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
;
56 int core_pr_dump_initiator_port(
57 struct t10_pr_registration
*pr_reg
,
61 if (!pr_reg
->isid_present_at_reg
)
64 snprintf(buf
, size
, ",i,0x%s", &pr_reg
->pr_reg_isid
[0]);
68 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
69 struct t10_pr_registration
*, int);
72 target_scsi2_reservation_check(struct se_cmd
*cmd
)
74 struct se_device
*dev
= cmd
->se_dev
;
75 struct se_session
*sess
= cmd
->se_sess
;
77 switch (cmd
->t_task_cdb
[0]) {
86 if (!dev
->dev_reserved_node_acl
|| !sess
)
89 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
90 return TCM_RESERVATION_CONFLICT
;
92 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
93 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
94 return TCM_RESERVATION_CONFLICT
;
100 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
101 struct se_node_acl
*, struct se_session
*);
102 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
104 static int target_check_scsi2_reservation_conflict(struct se_cmd
*cmd
)
106 struct se_session
*se_sess
= cmd
->se_sess
;
107 struct se_device
*dev
= cmd
->se_dev
;
108 struct t10_pr_registration
*pr_reg
;
109 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
112 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
116 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
119 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
120 * status, but no reservation shall be established and the
121 * persistent reservation shall not be changed, if the command
122 * is received from a) and b) below.
124 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
125 * status, but the persistent reservation shall not be released,
126 * if the command is received from a) and b)
128 * a) An I_T nexus that is a persistent reservation holder; or
129 * b) An I_T nexus that is registered if a registrants only or
130 * all registrants type persistent reservation is present.
132 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
133 * RELEASE(6) command, or RELEASE(10) command shall be processed
134 * as defined in SPC-2.
136 if (pr_reg
->pr_res_holder
) {
137 core_scsi3_put_pr_reg(pr_reg
);
140 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
141 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
142 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
143 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
144 core_scsi3_put_pr_reg(pr_reg
);
147 core_scsi3_put_pr_reg(pr_reg
);
151 * Following spc2r20 5.5.1 Reservations overview:
153 * If a logical unit has executed a PERSISTENT RESERVE OUT
154 * command with the REGISTER or the REGISTER AND IGNORE
155 * EXISTING KEY service action and is still registered by any
156 * initiator, all RESERVE commands and all RELEASE commands
157 * regardless of initiator shall conflict and shall terminate
158 * with a RESERVATION CONFLICT status.
160 spin_lock(&pr_tmpl
->registration_lock
);
161 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
162 spin_unlock(&pr_tmpl
->registration_lock
);
166 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
167 " while active SPC-3 registrations exist,"
168 " returning RESERVATION_CONFLICT\n");
176 target_scsi2_reservation_release(struct se_cmd
*cmd
)
178 struct se_device
*dev
= cmd
->se_dev
;
179 struct se_session
*sess
= cmd
->se_sess
;
180 struct se_portal_group
*tpg
;
183 if (!sess
|| !sess
->se_tpg
)
185 rc
= target_check_scsi2_reservation_conflict(cmd
);
189 return TCM_RESERVATION_CONFLICT
;
191 spin_lock(&dev
->dev_reservation_lock
);
192 if (!dev
->dev_reserved_node_acl
|| !sess
)
195 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
198 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
201 dev
->dev_reserved_node_acl
= NULL
;
202 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS
;
203 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
204 dev
->dev_res_bin_isid
= 0;
205 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS_WITH_ISID
;
208 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
209 " MAPPED LUN: %u for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
210 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
211 sess
->se_node_acl
->initiatorname
);
214 spin_unlock(&dev
->dev_reservation_lock
);
216 target_complete_cmd(cmd
, GOOD
);
221 target_scsi2_reservation_reserve(struct se_cmd
*cmd
)
223 struct se_device
*dev
= cmd
->se_dev
;
224 struct se_session
*sess
= cmd
->se_sess
;
225 struct se_portal_group
*tpg
;
226 sense_reason_t ret
= 0;
229 if ((cmd
->t_task_cdb
[1] & 0x01) &&
230 (cmd
->t_task_cdb
[1] & 0x02)) {
231 pr_err("LongIO and Obselete Bits set, returning"
232 " ILLEGAL_REQUEST\n");
233 return TCM_UNSUPPORTED_SCSI_OPCODE
;
236 * This is currently the case for target_core_mod passthrough struct se_cmd
239 if (!sess
|| !sess
->se_tpg
)
241 rc
= target_check_scsi2_reservation_conflict(cmd
);
246 return TCM_RESERVATION_CONFLICT
;
249 spin_lock(&dev
->dev_reservation_lock
);
250 if (dev
->dev_reserved_node_acl
&&
251 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
252 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
253 tpg
->se_tpg_tfo
->get_fabric_name());
254 pr_err("Original reserver LUN: %u %s\n",
255 cmd
->se_lun
->unpacked_lun
,
256 dev
->dev_reserved_node_acl
->initiatorname
);
257 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
258 " from %s \n", cmd
->se_lun
->unpacked_lun
,
259 cmd
->se_deve
->mapped_lun
,
260 sess
->se_node_acl
->initiatorname
);
261 ret
= TCM_RESERVATION_CONFLICT
;
265 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
266 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS
;
267 if (sess
->sess_bin_isid
!= 0) {
268 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
269 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS_WITH_ISID
;
271 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
272 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
273 cmd
->se_lun
->unpacked_lun
, cmd
->se_deve
->mapped_lun
,
274 sess
->se_node_acl
->initiatorname
);
277 spin_unlock(&dev
->dev_reservation_lock
);
280 target_complete_cmd(cmd
, GOOD
);
286 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
288 * This function is called by those initiator ports who are *NOT*
289 * the active PR reservation holder when a reservation is present.
291 static int core_scsi3_pr_seq_non_holder(
295 unsigned char *cdb
= cmd
->t_task_cdb
;
296 struct se_dev_entry
*se_deve
;
297 struct se_session
*se_sess
= cmd
->se_sess
;
298 int other_cdb
= 0, ignore_reg
;
299 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
300 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
301 int we
= 0; /* Write Exclusive */
302 int legacy
= 0; /* Act like a legacy device and return
303 * RESERVATION CONFLICT on some CDBs */
305 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
307 * Determine if the registration should be ignored due to
308 * non-matching ISIDs in target_scsi3_pr_reservation_check().
310 ignore_reg
= (pr_reg_type
& 0x80000000);
312 pr_reg_type
&= ~0x80000000;
314 switch (pr_reg_type
) {
315 case PR_TYPE_WRITE_EXCLUSIVE
:
317 case PR_TYPE_EXCLUSIVE_ACCESS
:
319 * Some commands are only allowed for the persistent reservation
322 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
323 registered_nexus
= 1;
325 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
327 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
329 * Some commands are only allowed for registered I_T Nexuses.
332 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
333 registered_nexus
= 1;
335 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
337 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
339 * Each registered I_T Nexus is a reservation holder.
342 if ((se_deve
->def_pr_registered
) && !(ignore_reg
))
343 registered_nexus
= 1;
349 * Referenced from spc4r17 table 45 for *NON* PR holder access
352 case SECURITY_PROTOCOL_IN
:
353 if (registered_nexus
)
361 case RECEIVE_DIAGNOSTIC
:
366 if (registered_nexus
) {
370 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
372 case PERSISTENT_RESERVE_OUT
:
374 * This follows PERSISTENT_RESERVE_OUT service actions that
375 * are allowed in the presence of various reservations.
376 * See spc4r17, table 46
378 switch (cdb
[1] & 0x1f) {
381 case PRO_PREEMPT_AND_ABORT
:
382 ret
= (registered_nexus
) ? 0 : 1;
385 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
388 case PRO_REGISTER_AND_MOVE
:
393 ret
= (registered_nexus
) ? 0 : 1;
396 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
397 " action: 0x%02x\n", cdb
[1] & 0x1f);
403 /* Handled by CRH=1 in target_scsi2_reservation_release() */
408 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
411 case TEST_UNIT_READY
:
412 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
415 switch (cdb
[1] & 0x1f) {
416 case MI_MANAGEMENT_PROTOCOL_IN
:
417 if (registered_nexus
) {
421 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
423 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
424 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
429 if (registered_nexus
) {
433 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
435 case MI_REPORT_ALIASES
:
436 case MI_REPORT_IDENTIFYING_INFORMATION
:
437 case MI_REPORT_PRIORITY
:
438 case MI_REPORT_TARGET_PGS
:
439 case MI_REPORT_TIMESTAMP
:
440 ret
= 0; /* Allowed */
443 pr_err("Unknown MI Service Action: 0x%02x\n",
448 case ACCESS_CONTROL_IN
:
449 case ACCESS_CONTROL_OUT
:
452 case READ_MEDIA_SERIAL_NUMBER
:
455 case PERSISTENT_RESERVE_IN
:
456 ret
= 0; /*/ Allowed CDBs */
463 * Case where the CDB is explicitly allowed in the above switch
466 if (!ret
&& !other_cdb
) {
467 pr_debug("Allowing explict CDB: 0x%02x for %s"
468 " reservation holder\n", cdb
[0],
469 core_scsi3_pr_dump_type(pr_reg_type
));
474 * Check if write exclusive initiator ports *NOT* holding the
475 * WRITE_EXCLUSIVE_* reservation.
477 if (we
&& !registered_nexus
) {
478 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
480 * Conflict for write exclusive
482 pr_debug("%s Conflict for unregistered nexus"
483 " %s CDB: 0x%02x to %s reservation\n",
484 transport_dump_cmd_direction(cmd
),
485 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
486 core_scsi3_pr_dump_type(pr_reg_type
));
490 * Allow non WRITE CDBs for all Write Exclusive
491 * PR TYPEs to pass for registered and
492 * non-registered_nexuxes NOT holding the reservation.
494 * We only make noise for the unregisterd nexuses,
495 * as we expect registered non-reservation holding
496 * nexuses to issue CDBs.
499 if (!registered_nexus
) {
500 pr_debug("Allowing implict CDB: 0x%02x"
501 " for %s reservation on unregistered"
503 core_scsi3_pr_dump_type(pr_reg_type
));
508 } else if ((reg_only
) || (all_reg
)) {
509 if (registered_nexus
) {
511 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
512 * allow commands from registered nexuses.
515 pr_debug("Allowing implict CDB: 0x%02x for %s"
516 " reservation\n", cdb
[0],
517 core_scsi3_pr_dump_type(pr_reg_type
));
522 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
523 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
524 (registered_nexus
) ? "" : "un",
525 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
526 core_scsi3_pr_dump_type(pr_reg_type
));
528 return 1; /* Conflict by default */
531 static sense_reason_t
532 target_scsi3_pr_reservation_check(struct se_cmd
*cmd
)
534 struct se_device
*dev
= cmd
->se_dev
;
535 struct se_session
*sess
= cmd
->se_sess
;
538 if (!dev
->dev_pr_res_holder
)
541 pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
542 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
543 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
)
544 goto check_nonholder
;
546 if (dev
->dev_pr_res_holder
->isid_present_at_reg
) {
547 if (dev
->dev_pr_res_holder
->pr_reg_bin_isid
!=
548 sess
->sess_bin_isid
) {
549 pr_reg_type
|= 0x80000000;
550 goto check_nonholder
;
557 if (core_scsi3_pr_seq_non_holder(cmd
, pr_reg_type
))
558 return TCM_RESERVATION_CONFLICT
;
562 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
567 * PRGeneration field shall contain the value of a 32-bit wrapping
568 * counter mainted by the device server.
570 * Note that this is done regardless of Active Persist across
571 * Target PowerLoss (APTPL)
573 * See spc4r17 section 6.3.12 READ_KEYS service action
575 spin_lock(&dev
->dev_reservation_lock
);
576 prg
= dev
->t10_pr
.pr_generation
++;
577 spin_unlock(&dev
->dev_reservation_lock
);
582 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
583 struct se_device
*dev
,
584 struct se_node_acl
*nacl
,
585 struct se_dev_entry
*deve
,
591 struct t10_pr_registration
*pr_reg
;
593 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
595 pr_err("Unable to allocate struct t10_pr_registration\n");
599 pr_reg
->pr_aptpl_buf
= kzalloc(dev
->t10_pr
.pr_aptpl_buf_len
,
601 if (!pr_reg
->pr_aptpl_buf
) {
602 pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
603 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
607 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
608 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
609 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
610 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
611 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
612 atomic_set(&pr_reg
->pr_res_holders
, 0);
613 pr_reg
->pr_reg_nacl
= nacl
;
614 pr_reg
->pr_reg_deve
= deve
;
615 pr_reg
->pr_res_mapped_lun
= deve
->mapped_lun
;
616 pr_reg
->pr_aptpl_target_lun
= deve
->se_lun
->unpacked_lun
;
617 pr_reg
->pr_res_key
= sa_res_key
;
618 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
619 pr_reg
->pr_reg_aptpl
= aptpl
;
620 pr_reg
->pr_reg_tg_pt_lun
= deve
->se_lun
;
622 * If an ISID value for this SCSI Initiator Port exists,
623 * save it to the registration now.
626 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
627 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
628 pr_reg
->isid_present_at_reg
= 1;
634 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
635 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
638 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
641 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
642 struct se_device
*dev
,
643 struct se_node_acl
*nacl
,
644 struct se_dev_entry
*deve
,
650 struct se_dev_entry
*deve_tmp
;
651 struct se_node_acl
*nacl_tmp
;
652 struct se_port
*port
, *port_tmp
;
653 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
654 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
657 * Create a registration for the I_T Nexus upon which the
658 * PROUT REGISTER was received.
660 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, deve
, isid
,
661 sa_res_key
, all_tg_pt
, aptpl
);
665 * Return pointer to pr_reg for ALL_TG_PT=0
670 * Create list of matching SCSI Initiator Port registrations
673 spin_lock(&dev
->se_port_lock
);
674 list_for_each_entry_safe(port
, port_tmp
, &dev
->dev_sep_list
, sep_list
) {
675 atomic_inc(&port
->sep_tg_pt_ref_cnt
);
676 smp_mb__after_atomic_inc();
677 spin_unlock(&dev
->se_port_lock
);
679 spin_lock_bh(&port
->sep_alua_lock
);
680 list_for_each_entry(deve_tmp
, &port
->sep_alua_list
,
683 * This pointer will be NULL for demo mode MappedLUNs
684 * that have not been make explict via a ConfigFS
685 * MappedLUN group for the SCSI Initiator Node ACL.
687 if (!deve_tmp
->se_lun_acl
)
690 nacl_tmp
= deve_tmp
->se_lun_acl
->se_lun_nacl
;
692 * Skip the matching struct se_node_acl that is allocated
695 if (nacl
== nacl_tmp
)
698 * Only perform PR registrations for target ports on
699 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
702 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
705 * Look for a matching Initiator Node ACL in ASCII format
707 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
710 atomic_inc(&deve_tmp
->pr_ref_count
);
711 smp_mb__after_atomic_inc();
712 spin_unlock_bh(&port
->sep_alua_lock
);
714 * Grab a configfs group dependency that is released
715 * for the exception path at label out: below, or upon
716 * completion of adding ALL_TG_PT=1 registrations in
717 * __core_scsi3_add_registration()
719 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
721 pr_err("core_scsi3_lunacl_depend"
723 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
724 smp_mb__after_atomic_dec();
725 atomic_dec(&deve_tmp
->pr_ref_count
);
726 smp_mb__after_atomic_dec();
730 * Located a matching SCSI Initiator Port on a different
731 * port, allocate the pr_reg_atp and attach it to the
732 * pr_reg->pr_reg_atp_list that will be processed once
733 * the original *pr_reg is processed in
734 * __core_scsi3_add_registration()
736 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
737 nacl_tmp
, deve_tmp
, NULL
,
738 sa_res_key
, all_tg_pt
, aptpl
);
740 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
741 smp_mb__after_atomic_dec();
742 atomic_dec(&deve_tmp
->pr_ref_count
);
743 smp_mb__after_atomic_dec();
744 core_scsi3_lunacl_undepend_item(deve_tmp
);
748 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
749 &pr_reg
->pr_reg_atp_list
);
750 spin_lock_bh(&port
->sep_alua_lock
);
752 spin_unlock_bh(&port
->sep_alua_lock
);
754 spin_lock(&dev
->se_port_lock
);
755 atomic_dec(&port
->sep_tg_pt_ref_cnt
);
756 smp_mb__after_atomic_dec();
758 spin_unlock(&dev
->se_port_lock
);
762 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
763 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
764 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
765 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
766 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
768 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
772 int core_scsi3_alloc_aptpl_registration(
773 struct t10_reservation
*pr_tmpl
,
775 unsigned char *i_port
,
778 unsigned char *t_port
,
785 struct t10_pr_registration
*pr_reg
;
787 if (!i_port
|| !t_port
|| !sa_res_key
) {
788 pr_err("Illegal parameters for APTPL registration\n");
792 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
794 pr_err("Unable to allocate struct t10_pr_registration\n");
797 pr_reg
->pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
, GFP_KERNEL
);
799 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
800 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
801 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
802 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
803 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
804 atomic_set(&pr_reg
->pr_res_holders
, 0);
805 pr_reg
->pr_reg_nacl
= NULL
;
806 pr_reg
->pr_reg_deve
= NULL
;
807 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
808 pr_reg
->pr_aptpl_target_lun
= target_lun
;
809 pr_reg
->pr_res_key
= sa_res_key
;
810 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
811 pr_reg
->pr_reg_aptpl
= 1;
812 pr_reg
->pr_reg_tg_pt_lun
= NULL
;
813 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
814 pr_reg
->pr_res_type
= type
;
816 * If an ISID value had been saved in APTPL metadata for this
817 * SCSI Initiator Port, restore it now.
820 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
821 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
822 pr_reg
->isid_present_at_reg
= 1;
825 * Copy the i_port and t_port information from caller.
827 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
828 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
829 pr_reg
->pr_reg_tpgt
= tpgt
;
831 * Set pr_res_holder from caller, the pr_reg who is the reservation
832 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
833 * the Initiator Node LUN ACL from the fabric module is created for
836 pr_reg
->pr_res_holder
= res_holder
;
838 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
839 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
840 " metadata\n", (res_holder
) ? "+reservation" : "");
844 static void core_scsi3_aptpl_reserve(
845 struct se_device
*dev
,
846 struct se_portal_group
*tpg
,
847 struct se_node_acl
*node_acl
,
848 struct t10_pr_registration
*pr_reg
)
850 char i_buf
[PR_REG_ISID_ID_LEN
];
853 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
854 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
857 spin_lock(&dev
->dev_reservation_lock
);
858 dev
->dev_pr_res_holder
= pr_reg
;
859 spin_unlock(&dev
->dev_reservation_lock
);
861 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
862 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
863 tpg
->se_tpg_tfo
->get_fabric_name(),
864 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
865 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
866 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
867 tpg
->se_tpg_tfo
->get_fabric_name(), node_acl
->initiatorname
,
868 (prf_isid
) ? &i_buf
[0] : "");
871 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
872 struct t10_pr_registration
*, int, int);
874 static int __core_scsi3_check_aptpl_registration(
875 struct se_device
*dev
,
876 struct se_portal_group
*tpg
,
879 struct se_node_acl
*nacl
,
880 struct se_dev_entry
*deve
)
882 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
883 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
884 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
885 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
888 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
889 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
891 * Copy Initiator Port information from struct se_node_acl
893 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
894 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
895 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
896 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
898 * Look for the matching registrations+reservation from those
899 * created from APTPL metadata. Note that multiple registrations
900 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
903 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
904 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
906 if (!strcmp(pr_reg
->pr_iport
, i_port
) &&
907 (pr_reg
->pr_res_mapped_lun
== deve
->mapped_lun
) &&
908 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
909 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
910 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
912 pr_reg
->pr_reg_nacl
= nacl
;
913 pr_reg
->pr_reg_deve
= deve
;
914 pr_reg
->pr_reg_tg_pt_lun
= lun
;
916 list_del(&pr_reg
->pr_reg_aptpl_list
);
917 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
919 * At this point all of the pointers in *pr_reg will
920 * be setup, so go ahead and add the registration.
923 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
925 * If this registration is the reservation holder,
926 * make that happen now..
928 if (pr_reg
->pr_res_holder
)
929 core_scsi3_aptpl_reserve(dev
, tpg
,
932 * Reenable pr_aptpl_active to accept new metadata
933 * updates once the SCSI device is active again..
935 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
936 pr_tmpl
->pr_aptpl_active
= 1;
939 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
944 int core_scsi3_check_aptpl_registration(
945 struct se_device
*dev
,
946 struct se_portal_group
*tpg
,
948 struct se_lun_acl
*lun_acl
)
950 struct se_node_acl
*nacl
= lun_acl
->se_lun_nacl
;
951 struct se_dev_entry
*deve
= nacl
->device_list
[lun_acl
->mapped_lun
];
953 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
956 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
957 lun
->unpacked_lun
, nacl
, deve
);
960 static void __core_scsi3_dump_registration(
961 struct target_core_fabric_ops
*tfo
,
962 struct se_device
*dev
,
963 struct se_node_acl
*nacl
,
964 struct t10_pr_registration
*pr_reg
,
967 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
968 char i_buf
[PR_REG_ISID_ID_LEN
];
971 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
972 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
975 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
976 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== 2) ?
977 "_AND_MOVE" : (register_type
== 1) ?
978 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
979 (prf_isid
) ? i_buf
: "");
980 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
981 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
982 tfo
->tpg_get_tag(se_tpg
));
983 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
984 " Port(s)\n", tfo
->get_fabric_name(),
985 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
986 dev
->transport
->name
);
987 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
988 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
989 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
990 pr_reg
->pr_reg_aptpl
);
994 * this function can be called with struct se_device->dev_reservation_lock
995 * when register_move = 1
997 static void __core_scsi3_add_registration(
998 struct se_device
*dev
,
999 struct se_node_acl
*nacl
,
1000 struct t10_pr_registration
*pr_reg
,
1004 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1005 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1006 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1009 * Increment PRgeneration counter for struct se_device upon a successful
1010 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1012 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1013 * action, the struct se_device->dev_reservation_lock will already be held,
1014 * so we do not call core_scsi3_pr_generation() which grabs the lock
1017 pr_reg
->pr_res_generation
= (register_move
) ?
1018 dev
->t10_pr
.pr_generation
++ :
1019 core_scsi3_pr_generation(dev
);
1021 spin_lock(&pr_tmpl
->registration_lock
);
1022 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1023 pr_reg
->pr_reg_deve
->def_pr_registered
= 1;
1025 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1026 spin_unlock(&pr_tmpl
->registration_lock
);
1028 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1030 if (!pr_reg
->pr_reg_all_tg_pt
|| register_move
)
1033 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1034 * allocated in __core_scsi3_alloc_registration()
1036 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1037 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1038 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1040 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1042 spin_lock(&pr_tmpl
->registration_lock
);
1043 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1044 &pr_tmpl
->registration_list
);
1045 pr_reg_tmp
->pr_reg_deve
->def_pr_registered
= 1;
1047 __core_scsi3_dump_registration(tfo
, dev
,
1048 pr_reg_tmp
->pr_reg_nacl
, pr_reg_tmp
,
1050 spin_unlock(&pr_tmpl
->registration_lock
);
1052 * Drop configfs group dependency reference from
1053 * __core_scsi3_alloc_registration()
1055 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1059 static int core_scsi3_alloc_registration(
1060 struct se_device
*dev
,
1061 struct se_node_acl
*nacl
,
1062 struct se_dev_entry
*deve
,
1063 unsigned char *isid
,
1070 struct t10_pr_registration
*pr_reg
;
1072 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, deve
, isid
,
1073 sa_res_key
, all_tg_pt
, aptpl
);
1077 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1078 register_type
, register_move
);
1082 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1083 struct se_device
*dev
,
1084 struct se_node_acl
*nacl
,
1085 unsigned char *isid
)
1087 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1088 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1089 struct se_portal_group
*tpg
;
1091 spin_lock(&pr_tmpl
->registration_lock
);
1092 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1093 &pr_tmpl
->registration_list
, pr_reg_list
) {
1095 * First look for a matching struct se_node_acl
1097 if (pr_reg
->pr_reg_nacl
!= nacl
)
1100 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1102 * If this registration does NOT contain a fabric provided
1103 * ISID, then we have found a match.
1105 if (!pr_reg
->isid_present_at_reg
) {
1107 * Determine if this SCSI device server requires that
1108 * SCSI Intiatior TransportID w/ ISIDs is enforced
1109 * for fabric modules (iSCSI) requiring them.
1111 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1112 if (dev
->dev_attrib
.enforce_pr_isids
)
1115 atomic_inc(&pr_reg
->pr_res_holders
);
1116 smp_mb__after_atomic_inc();
1117 spin_unlock(&pr_tmpl
->registration_lock
);
1121 * If the *pr_reg contains a fabric defined ISID for multi-value
1122 * SCSI Initiator Port TransportIDs, then we expect a valid
1123 * matching ISID to be provided by the local SCSI Initiator Port.
1127 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1130 atomic_inc(&pr_reg
->pr_res_holders
);
1131 smp_mb__after_atomic_inc();
1132 spin_unlock(&pr_tmpl
->registration_lock
);
1135 spin_unlock(&pr_tmpl
->registration_lock
);
1140 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1141 struct se_device
*dev
,
1142 struct se_node_acl
*nacl
,
1143 struct se_session
*sess
)
1145 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1146 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1148 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1149 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1150 tpg
->se_tpg_tfo
->sess_get_initiator_sid(sess
, &buf
[0],
1155 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1158 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1160 atomic_dec(&pr_reg
->pr_res_holders
);
1161 smp_mb__after_atomic_dec();
1164 static int core_scsi3_check_implict_release(
1165 struct se_device
*dev
,
1166 struct t10_pr_registration
*pr_reg
)
1168 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1169 struct t10_pr_registration
*pr_res_holder
;
1172 spin_lock(&dev
->dev_reservation_lock
);
1173 pr_res_holder
= dev
->dev_pr_res_holder
;
1174 if (!pr_res_holder
) {
1175 spin_unlock(&dev
->dev_reservation_lock
);
1178 if (pr_res_holder
== pr_reg
) {
1180 * Perform an implict RELEASE if the registration that
1181 * is being released is holding the reservation.
1183 * From spc4r17, section 5.7.11.1:
1185 * e) If the I_T nexus is the persistent reservation holder
1186 * and the persistent reservation is not an all registrants
1187 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1188 * service action or REGISTER AND IGNORE EXISTING KEY
1189 * service action with the SERVICE ACTION RESERVATION KEY
1190 * field set to zero (see 5.7.11.3).
1192 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0);
1195 * For 'All Registrants' reservation types, all existing
1196 * registrations are still processed as reservation holders
1197 * in core_scsi3_pr_seq_non_holder() after the initial
1198 * reservation holder is implictly released here.
1200 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1201 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1202 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1203 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1204 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1205 " UNREGISTER while existing reservation with matching"
1206 " key 0x%016Lx is present from another SCSI Initiator"
1207 " Port\n", pr_reg
->pr_res_key
);
1210 spin_unlock(&dev
->dev_reservation_lock
);
1216 * Called with struct t10_reservation->registration_lock held.
1218 static void __core_scsi3_free_registration(
1219 struct se_device
*dev
,
1220 struct t10_pr_registration
*pr_reg
,
1221 struct list_head
*preempt_and_abort_list
,
1224 struct target_core_fabric_ops
*tfo
=
1225 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1226 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1227 char i_buf
[PR_REG_ISID_ID_LEN
];
1230 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1231 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
1232 PR_REG_ISID_ID_LEN
);
1234 pr_reg
->pr_reg_deve
->def_pr_registered
= 0;
1235 pr_reg
->pr_reg_deve
->pr_res_key
= 0;
1236 list_del(&pr_reg
->pr_reg_list
);
1238 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1239 * so call core_scsi3_put_pr_reg() to decrement our reference.
1242 core_scsi3_put_pr_reg(pr_reg
);
1244 * Wait until all reference from any other I_T nexuses for this
1245 * *pr_reg have been released. Because list_del() is called above,
1246 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1247 * count back to zero, and we release *pr_reg.
1249 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1250 spin_unlock(&pr_tmpl
->registration_lock
);
1251 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1252 tfo
->get_fabric_name());
1254 spin_lock(&pr_tmpl
->registration_lock
);
1257 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1258 " Node: %s%s\n", tfo
->get_fabric_name(),
1259 pr_reg
->pr_reg_nacl
->initiatorname
,
1260 (prf_isid
) ? &i_buf
[0] : "");
1261 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1262 " Port(s)\n", tfo
->get_fabric_name(),
1263 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1264 dev
->transport
->name
);
1265 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1266 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1267 pr_reg
->pr_res_generation
);
1269 if (!preempt_and_abort_list
) {
1270 pr_reg
->pr_reg_deve
= NULL
;
1271 pr_reg
->pr_reg_nacl
= NULL
;
1272 kfree(pr_reg
->pr_aptpl_buf
);
1273 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1277 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1278 * are released once the ABORT_TASK_SET has completed..
1280 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1283 void core_scsi3_free_pr_reg_from_nacl(
1284 struct se_device
*dev
,
1285 struct se_node_acl
*nacl
)
1287 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1288 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1290 * If the passed se_node_acl matches the reservation holder,
1291 * release the reservation.
1293 spin_lock(&dev
->dev_reservation_lock
);
1294 pr_res_holder
= dev
->dev_pr_res_holder
;
1295 if ((pr_res_holder
!= NULL
) &&
1296 (pr_res_holder
->pr_reg_nacl
== nacl
))
1297 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0);
1298 spin_unlock(&dev
->dev_reservation_lock
);
1300 * Release any registration associated with the struct se_node_acl.
1302 spin_lock(&pr_tmpl
->registration_lock
);
1303 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1304 &pr_tmpl
->registration_list
, pr_reg_list
) {
1306 if (pr_reg
->pr_reg_nacl
!= nacl
)
1309 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1311 spin_unlock(&pr_tmpl
->registration_lock
);
1314 void core_scsi3_free_all_registrations(
1315 struct se_device
*dev
)
1317 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1318 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1320 spin_lock(&dev
->dev_reservation_lock
);
1321 pr_res_holder
= dev
->dev_pr_res_holder
;
1322 if (pr_res_holder
!= NULL
) {
1323 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1324 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1327 spin_unlock(&dev
->dev_reservation_lock
);
1329 spin_lock(&pr_tmpl
->registration_lock
);
1330 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1331 &pr_tmpl
->registration_list
, pr_reg_list
) {
1333 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1335 spin_unlock(&pr_tmpl
->registration_lock
);
1337 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1338 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1339 pr_reg_aptpl_list
) {
1340 list_del(&pr_reg
->pr_reg_aptpl_list
);
1341 kfree(pr_reg
->pr_aptpl_buf
);
1342 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1344 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1347 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1349 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1350 &tpg
->tpg_group
.cg_item
);
1353 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1355 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1356 &tpg
->tpg_group
.cg_item
);
1358 atomic_dec(&tpg
->tpg_pr_ref_count
);
1359 smp_mb__after_atomic_dec();
1362 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1364 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1366 if (nacl
->dynamic_node_acl
)
1369 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1370 &nacl
->acl_group
.cg_item
);
1373 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1375 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1377 if (nacl
->dynamic_node_acl
) {
1378 atomic_dec(&nacl
->acl_pr_ref_count
);
1379 smp_mb__after_atomic_dec();
1383 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1384 &nacl
->acl_group
.cg_item
);
1386 atomic_dec(&nacl
->acl_pr_ref_count
);
1387 smp_mb__after_atomic_dec();
1390 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1392 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1393 struct se_node_acl
*nacl
;
1394 struct se_portal_group
*tpg
;
1396 * For nacl->dynamic_node_acl=1
1401 nacl
= lun_acl
->se_lun_nacl
;
1404 return configfs_depend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1405 &lun_acl
->se_lun_group
.cg_item
);
1408 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1410 struct se_lun_acl
*lun_acl
= se_deve
->se_lun_acl
;
1411 struct se_node_acl
*nacl
;
1412 struct se_portal_group
*tpg
;
1414 * For nacl->dynamic_node_acl=1
1417 atomic_dec(&se_deve
->pr_ref_count
);
1418 smp_mb__after_atomic_dec();
1421 nacl
= lun_acl
->se_lun_nacl
;
1424 configfs_undepend_item(tpg
->se_tpg_tfo
->tf_subsys
,
1425 &lun_acl
->se_lun_group
.cg_item
);
1427 atomic_dec(&se_deve
->pr_ref_count
);
1428 smp_mb__after_atomic_dec();
1431 static sense_reason_t
1432 core_scsi3_decode_spec_i_port(
1434 struct se_portal_group
*tpg
,
1435 unsigned char *l_isid
,
1440 struct se_device
*dev
= cmd
->se_dev
;
1441 struct se_port
*tmp_port
;
1442 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1443 struct se_session
*se_sess
= cmd
->se_sess
;
1444 struct se_node_acl
*dest_node_acl
= NULL
;
1445 struct se_dev_entry
*dest_se_deve
= NULL
, *local_se_deve
;
1446 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1447 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1448 LIST_HEAD(tid_dest_list
);
1449 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1450 struct target_core_fabric_ops
*tmp_tf_ops
;
1452 unsigned char *ptr
, *i_str
= NULL
, proto_ident
, tmp_proto_ident
;
1453 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
1455 u32 tpdl
, tid_len
= 0;
1456 int dest_local_nexus
, prf_isid
;
1459 memset(dest_iport
, 0, 64);
1461 local_se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
1463 * Allocate a struct pr_transport_id_holder and setup the
1464 * local_node_acl and local_se_deve pointers and add to
1465 * struct list_head tid_dest_list for add registration
1466 * processing in the loop of tid_dest_list below.
1468 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1470 pr_err("Unable to allocate tidh_new\n");
1471 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1473 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1474 tidh_new
->dest_tpg
= tpg
;
1475 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1476 tidh_new
->dest_se_deve
= local_se_deve
;
1478 local_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1479 se_sess
->se_node_acl
, local_se_deve
, l_isid
,
1480 sa_res_key
, all_tg_pt
, aptpl
);
1481 if (!local_pr_reg
) {
1483 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1485 tidh_new
->dest_pr_reg
= local_pr_reg
;
1487 * The local I_T nexus does not hold any configfs dependances,
1488 * so we set tid_h->dest_local_nexus=1 to prevent the
1489 * configfs_undepend_item() calls in the tid_dest_list loops below.
1491 tidh_new
->dest_local_nexus
= 1;
1492 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1494 if (cmd
->data_length
< 28) {
1495 pr_warn("SPC-PR: Received PR OUT parameter list"
1496 " length too small: %u\n", cmd
->data_length
);
1497 ret
= TCM_INVALID_PARAMETER_LIST
;
1501 buf
= transport_kmap_data_sg(cmd
);
1503 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1508 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1509 * first extract TransportID Parameter Data Length, and make sure
1510 * the value matches up to the SCSI expected data transfer length.
1512 tpdl
= (buf
[24] & 0xff) << 24;
1513 tpdl
|= (buf
[25] & 0xff) << 16;
1514 tpdl
|= (buf
[26] & 0xff) << 8;
1515 tpdl
|= buf
[27] & 0xff;
1517 if ((tpdl
+ 28) != cmd
->data_length
) {
1518 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1519 " does not equal CDB data_length: %u\n", tpdl
,
1521 ret
= TCM_INVALID_PARAMETER_LIST
;
1525 * Start processing the received transport IDs using the
1526 * receiving I_T Nexus portal's fabric dependent methods to
1527 * obtain the SCSI Initiator Port/Device Identifiers.
1532 proto_ident
= (ptr
[0] & 0x0f);
1535 spin_lock(&dev
->se_port_lock
);
1536 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1537 tmp_tpg
= tmp_port
->sep_tpg
;
1540 tmp_tf_ops
= tmp_tpg
->se_tpg_tfo
;
1543 if (!tmp_tf_ops
->get_fabric_proto_ident
||
1544 !tmp_tf_ops
->tpg_parse_pr_out_transport_id
)
1547 * Look for the matching proto_ident provided by
1548 * the received TransportID
1550 tmp_proto_ident
= tmp_tf_ops
->get_fabric_proto_ident(tmp_tpg
);
1551 if (tmp_proto_ident
!= proto_ident
)
1553 dest_rtpi
= tmp_port
->sep_rtpi
;
1555 i_str
= tmp_tf_ops
->tpg_parse_pr_out_transport_id(
1556 tmp_tpg
, (const char *)ptr
, &tid_len
,
1561 atomic_inc(&tmp_tpg
->tpg_pr_ref_count
);
1562 smp_mb__after_atomic_inc();
1563 spin_unlock(&dev
->se_port_lock
);
1565 if (core_scsi3_tpg_depend_item(tmp_tpg
)) {
1566 pr_err(" core_scsi3_tpg_depend_item()"
1568 atomic_dec(&tmp_tpg
->tpg_pr_ref_count
);
1569 smp_mb__after_atomic_dec();
1570 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1574 * Locate the destination initiator ACL to be registered
1575 * from the decoded fabric module specific TransportID
1578 spin_lock_irq(&tmp_tpg
->acl_node_lock
);
1579 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1581 if (dest_node_acl
) {
1582 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
1583 smp_mb__after_atomic_inc();
1585 spin_unlock_irq(&tmp_tpg
->acl_node_lock
);
1587 if (!dest_node_acl
) {
1588 core_scsi3_tpg_undepend_item(tmp_tpg
);
1589 spin_lock(&dev
->se_port_lock
);
1593 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
1594 pr_err("configfs_depend_item() failed"
1595 " for dest_node_acl->acl_group\n");
1596 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
1597 smp_mb__after_atomic_dec();
1598 core_scsi3_tpg_undepend_item(tmp_tpg
);
1599 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1604 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1605 " %s Port RTPI: %hu\n",
1606 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1607 dest_node_acl
->initiatorname
, dest_rtpi
);
1609 spin_lock(&dev
->se_port_lock
);
1612 spin_unlock(&dev
->se_port_lock
);
1615 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1617 ret
= TCM_INVALID_PARAMETER_LIST
;
1621 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1622 " tid_len: %d for %s + %s\n",
1623 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1624 tpdl
, tid_len
, i_str
, iport_ptr
);
1626 if (tid_len
> tpdl
) {
1627 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1628 " %u for Transport ID: %s\n", tid_len
, ptr
);
1629 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1630 core_scsi3_tpg_undepend_item(dest_tpg
);
1631 ret
= TCM_INVALID_PARAMETER_LIST
;
1635 * Locate the desintation struct se_dev_entry pointer for matching
1636 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1639 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1641 if (!dest_se_deve
) {
1642 pr_err("Unable to locate %s dest_se_deve"
1643 " from destination RTPI: %hu\n",
1644 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1647 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1648 core_scsi3_tpg_undepend_item(dest_tpg
);
1649 ret
= TCM_INVALID_PARAMETER_LIST
;
1653 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
1654 pr_err("core_scsi3_lunacl_depend_item()"
1656 atomic_dec(&dest_se_deve
->pr_ref_count
);
1657 smp_mb__after_atomic_dec();
1658 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1659 core_scsi3_tpg_undepend_item(dest_tpg
);
1660 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1664 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1665 " dest_se_deve mapped_lun: %u\n",
1666 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1667 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1670 * Skip any TransportIDs that already have a registration for
1673 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1676 core_scsi3_put_pr_reg(pr_reg_e
);
1677 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1678 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1679 core_scsi3_tpg_undepend_item(dest_tpg
);
1686 * Allocate a struct pr_transport_id_holder and setup
1687 * the dest_node_acl and dest_se_deve pointers for the
1690 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1693 pr_err("Unable to allocate tidh_new\n");
1694 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1695 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1696 core_scsi3_tpg_undepend_item(dest_tpg
);
1697 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1700 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1701 tidh_new
->dest_tpg
= dest_tpg
;
1702 tidh_new
->dest_node_acl
= dest_node_acl
;
1703 tidh_new
->dest_se_deve
= dest_se_deve
;
1706 * Allocate, but do NOT add the registration for the
1707 * TransportID referenced SCSI Initiator port. This
1708 * done because of the following from spc4r17 in section
1709 * 6.14.3 wrt SPEC_I_PT:
1711 * "If a registration fails for any initiator port (e.g., if th
1712 * logical unit does not have enough resources available to
1713 * hold the registration information), no registrations shall be
1714 * made, and the command shall be terminated with
1715 * CHECK CONDITION status."
1717 * That means we call __core_scsi3_alloc_registration() here,
1718 * and then call __core_scsi3_add_registration() in the
1719 * 2nd loop which will never fail.
1721 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1722 dest_node_acl
, dest_se_deve
, iport_ptr
,
1723 sa_res_key
, all_tg_pt
, aptpl
);
1725 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1726 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1727 core_scsi3_tpg_undepend_item(dest_tpg
);
1729 ret
= TCM_INVALID_PARAMETER_LIST
;
1732 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1733 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1741 transport_kunmap_data_sg(cmd
);
1744 * Go ahead and create a registrations from tid_dest_list for the
1745 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1748 * The SA Reservation Key from the PROUT is set for the
1749 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1750 * means that the TransportID Initiator port will be
1751 * registered on all of the target ports in the SCSI target device
1752 * ALL_TG_PT=0 means the registration will only be for the
1753 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1756 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1757 dest_tpg
= tidh
->dest_tpg
;
1758 dest_node_acl
= tidh
->dest_node_acl
;
1759 dest_se_deve
= tidh
->dest_se_deve
;
1760 dest_pr_reg
= tidh
->dest_pr_reg
;
1761 dest_local_nexus
= tidh
->dest_local_nexus
;
1763 list_del(&tidh
->dest_list
);
1766 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1767 prf_isid
= core_pr_dump_initiator_port(dest_pr_reg
, &i_buf
[0],
1768 PR_REG_ISID_ID_LEN
);
1770 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1773 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1774 " registered Transport ID for Node: %s%s Mapped LUN:"
1775 " %u\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1776 dest_node_acl
->initiatorname
, (prf_isid
) ?
1777 &i_buf
[0] : "", dest_se_deve
->mapped_lun
);
1779 if (dest_local_nexus
)
1782 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1783 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1784 core_scsi3_tpg_undepend_item(dest_tpg
);
1789 transport_kunmap_data_sg(cmd
);
1792 * For the failure case, release everything from tid_dest_list
1793 * including *dest_pr_reg and the configfs dependances..
1795 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1796 dest_tpg
= tidh
->dest_tpg
;
1797 dest_node_acl
= tidh
->dest_node_acl
;
1798 dest_se_deve
= tidh
->dest_se_deve
;
1799 dest_pr_reg
= tidh
->dest_pr_reg
;
1800 dest_local_nexus
= tidh
->dest_local_nexus
;
1802 list_del(&tidh
->dest_list
);
1805 * Release any extra ALL_TG_PT=1 registrations for
1806 * the SPEC_I_PT=1 case.
1808 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1809 &dest_pr_reg
->pr_reg_atp_list
,
1810 pr_reg_atp_mem_list
) {
1811 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1812 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1813 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1816 kfree(dest_pr_reg
->pr_aptpl_buf
);
1817 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1819 if (dest_local_nexus
)
1822 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1823 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1824 core_scsi3_tpg_undepend_item(dest_tpg
);
1830 * Called with struct se_device->dev_reservation_lock held
1832 static int __core_scsi3_update_aptpl_buf(
1833 struct se_device
*dev
,
1835 u32 pr_aptpl_buf_len
,
1836 int clear_aptpl_metadata
)
1839 struct se_portal_group
*tpg
;
1840 struct t10_pr_registration
*pr_reg
;
1841 unsigned char tmp
[512], isid_buf
[32];
1845 memset(buf
, 0, pr_aptpl_buf_len
);
1847 * Called to clear metadata once APTPL has been deactivated.
1849 if (clear_aptpl_metadata
) {
1850 snprintf(buf
, pr_aptpl_buf_len
,
1851 "No Registrations or Reservations\n");
1855 * Walk the registration list..
1857 spin_lock(&dev
->t10_pr
.registration_lock
);
1858 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
1863 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1864 lun
= pr_reg
->pr_reg_tg_pt_lun
;
1866 * Write out any ISID value to APTPL metadata that was included
1867 * in the original registration.
1869 if (pr_reg
->isid_present_at_reg
)
1870 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1871 pr_reg
->pr_reg_isid
);
1873 * Include special metadata if the pr_reg matches the
1874 * reservation holder.
1876 if (dev
->dev_pr_res_holder
== pr_reg
) {
1877 snprintf(tmp
, 512, "PR_REG_START: %d"
1878 "\ninitiator_fabric=%s\n"
1879 "initiator_node=%s\n%s"
1881 "res_holder=1\nres_type=%02x\n"
1882 "res_scope=%02x\nres_all_tg_pt=%d\n"
1883 "mapped_lun=%u\n", reg_count
,
1884 tpg
->se_tpg_tfo
->get_fabric_name(),
1885 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1886 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1887 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1888 pr_reg
->pr_res_mapped_lun
);
1890 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1891 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1892 "sa_res_key=%llu\nres_holder=0\n"
1893 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1894 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1895 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1896 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1897 pr_reg
->pr_res_mapped_lun
);
1900 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1901 pr_err("Unable to update renaming"
1902 " APTPL metadata\n");
1903 spin_unlock(&dev
->t10_pr
.registration_lock
);
1906 len
+= sprintf(buf
+len
, "%s", tmp
);
1909 * Include information about the associated SCSI target port.
1911 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1912 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1913 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1914 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1915 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1916 lun
->lun_sep
->sep_rtpi
, lun
->unpacked_lun
, reg_count
);
1918 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1919 pr_err("Unable to update renaming"
1920 " APTPL metadata\n");
1921 spin_unlock(&dev
->t10_pr
.registration_lock
);
1924 len
+= sprintf(buf
+len
, "%s", tmp
);
1927 spin_unlock(&dev
->t10_pr
.registration_lock
);
1930 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1935 static int core_scsi3_update_aptpl_buf(
1936 struct se_device
*dev
,
1938 u32 pr_aptpl_buf_len
,
1939 int clear_aptpl_metadata
)
1943 spin_lock(&dev
->dev_reservation_lock
);
1944 ret
= __core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
1945 clear_aptpl_metadata
);
1946 spin_unlock(&dev
->dev_reservation_lock
);
1952 * Called with struct se_device->aptpl_file_mutex held
1954 static int __core_scsi3_write_aptpl_to_file(
1955 struct se_device
*dev
,
1957 u32 pr_aptpl_buf_len
)
1959 struct t10_wwn
*wwn
= &dev
->t10_wwn
;
1961 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1965 memset(path
, 0, 512);
1967 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
1968 pr_err("WWN value for struct se_device does not fit"
1969 " into path buffer\n");
1973 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
1974 file
= filp_open(path
, flags
, 0600);
1976 pr_err("filp_open(%s) for APTPL metadata"
1978 return PTR_ERR(file
);
1981 if (!pr_aptpl_buf_len
)
1982 pr_aptpl_buf_len
= (strlen(&buf
[0]) + 1); /* Add extra for NULL */
1984 ret
= kernel_write(file
, buf
, pr_aptpl_buf_len
, 0);
1987 pr_debug("Error writing APTPL metadata file: %s\n", path
);
1990 return ret
? -EIO
: 0;
1994 core_scsi3_update_and_write_aptpl(struct se_device
*dev
, unsigned char *in_buf
,
1995 u32 in_pr_aptpl_buf_len
)
1997 unsigned char null_buf
[64], *buf
;
1998 u32 pr_aptpl_buf_len
;
1999 int clear_aptpl_metadata
= 0;
2003 * Can be called with a NULL pointer from PROUT service action CLEAR
2006 memset(null_buf
, 0, 64);
2009 * This will clear the APTPL metadata to:
2010 * "No Registrations or Reservations" status
2012 pr_aptpl_buf_len
= 64;
2013 clear_aptpl_metadata
= 1;
2016 pr_aptpl_buf_len
= in_pr_aptpl_buf_len
;
2019 ret
= core_scsi3_update_aptpl_buf(dev
, buf
, pr_aptpl_buf_len
,
2020 clear_aptpl_metadata
);
2025 * __core_scsi3_write_aptpl_to_file() will call strlen()
2026 * on the passed buf to determine pr_aptpl_buf_len.
2028 return __core_scsi3_write_aptpl_to_file(dev
, buf
, 0);
2031 static sense_reason_t
2032 core_scsi3_emulate_pro_register(struct se_cmd
*cmd
, u64 res_key
, u64 sa_res_key
,
2033 int aptpl
, int all_tg_pt
, int spec_i_pt
, int ignore_key
)
2035 struct se_session
*se_sess
= cmd
->se_sess
;
2036 struct se_device
*dev
= cmd
->se_dev
;
2037 struct se_dev_entry
*se_deve
;
2038 struct se_lun
*se_lun
= cmd
->se_lun
;
2039 struct se_portal_group
*se_tpg
;
2040 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
, *pr_reg_e
;
2041 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2042 /* Used for APTPL metadata w/ UNREGISTER */
2043 unsigned char *pr_aptpl_buf
= NULL
;
2044 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2045 sense_reason_t ret
= TCM_NO_SENSE
;
2046 int pr_holder
= 0, type
;
2048 if (!se_sess
|| !se_lun
) {
2049 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2050 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2052 se_tpg
= se_sess
->se_tpg
;
2053 se_deve
= se_sess
->se_node_acl
->device_list
[cmd
->orig_fe_lun
];
2055 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2056 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2057 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2059 isid_ptr
= &isid_buf
[0];
2062 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2064 pr_reg_e
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2067 pr_warn("SPC-3 PR: Reservation Key non-zero"
2068 " for SA REGISTER, returning CONFLICT\n");
2069 return TCM_RESERVATION_CONFLICT
;
2072 * Do nothing but return GOOD status.
2079 * Perform the Service Action REGISTER on the Initiator
2080 * Port Endpoint that the PRO was received from on the
2081 * Logical Unit of the SCSI device server.
2083 if (core_scsi3_alloc_registration(cmd
->se_dev
,
2084 se_sess
->se_node_acl
, se_deve
, isid_ptr
,
2085 sa_res_key
, all_tg_pt
, aptpl
,
2087 pr_err("Unable to allocate"
2088 " struct t10_pr_registration\n");
2089 return TCM_INVALID_PARAMETER_LIST
;
2093 * Register both the Initiator port that received
2094 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2095 * TransportID from Parameter list and loop through
2096 * fabric dependent parameter list while calling
2097 * logic from of core_scsi3_alloc_registration() for
2098 * each TransportID provided SCSI Initiator Port/Device
2100 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2101 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2106 * Nothing left to do for the APTPL=0 case.
2109 pr_tmpl
->pr_aptpl_active
= 0;
2110 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2111 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2116 * Locate the newly allocated local I_T Nexus *pr_reg, and
2117 * update the APTPL metadata information using its
2118 * preallocated *pr_reg->pr_aptpl_buf.
2120 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2121 se_sess
->se_node_acl
, se_sess
);
2123 if (core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2124 &pr_reg
->pr_aptpl_buf
[0],
2125 pr_tmpl
->pr_aptpl_buf_len
)) {
2126 pr_tmpl
->pr_aptpl_active
= 1;
2127 pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2130 goto out_put_pr_reg
;
2134 * Locate the existing *pr_reg via struct se_node_acl pointers
2137 type
= pr_reg
->pr_res_type
;
2140 if (res_key
!= pr_reg
->pr_res_key
) {
2141 pr_err("SPC-3 PR REGISTER: Received"
2142 " res_key: 0x%016Lx does not match"
2143 " existing SA REGISTER res_key:"
2144 " 0x%016Lx\n", res_key
,
2145 pr_reg
->pr_res_key
);
2146 ret
= TCM_RESERVATION_CONFLICT
;
2147 goto out_put_pr_reg
;
2152 pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2153 " set while sa_res_key=0\n");
2154 ret
= TCM_INVALID_PARAMETER_LIST
;
2155 goto out_put_pr_reg
;
2159 * An existing ALL_TG_PT=1 registration being released
2160 * must also set ALL_TG_PT=1 in the incoming PROUT.
2162 if (pr_reg
->pr_reg_all_tg_pt
&& !(all_tg_pt
)) {
2163 pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2164 " registration exists, but ALL_TG_PT=1 bit not"
2165 " present in received PROUT\n");
2166 ret
= TCM_INVALID_CDB_FIELD
;
2167 goto out_put_pr_reg
;
2171 * Allocate APTPL metadata buffer used for UNREGISTER ops
2174 pr_aptpl_buf
= kzalloc(pr_tmpl
->pr_aptpl_buf_len
,
2176 if (!pr_aptpl_buf
) {
2177 pr_err("Unable to allocate"
2179 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2180 goto out_put_pr_reg
;
2185 * sa_res_key=0 Unregister Reservation Key for registered I_T
2186 * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2190 pr_holder
= core_scsi3_check_implict_release(
2191 cmd
->se_dev
, pr_reg
);
2192 if (pr_holder
< 0) {
2193 kfree(pr_aptpl_buf
);
2194 ret
= TCM_RESERVATION_CONFLICT
;
2195 goto out_put_pr_reg
;
2198 spin_lock(&pr_tmpl
->registration_lock
);
2200 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2201 * and matching pr_res_key.
2203 if (pr_reg
->pr_reg_all_tg_pt
) {
2204 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2205 &pr_tmpl
->registration_list
,
2208 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2210 if (pr_reg_p
->pr_res_key
!= res_key
)
2212 if (pr_reg
== pr_reg_p
)
2214 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2215 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2218 __core_scsi3_free_registration(dev
,
2224 * Release the calling I_T Nexus registration now..
2226 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
, NULL
, 1);
2229 * From spc4r17, section 5.7.11.3 Unregistering
2231 * If the persistent reservation is a registrants only
2232 * type, the device server shall establish a unit
2233 * attention condition for the initiator port associated
2234 * with every registered I_T nexus except for the I_T
2235 * nexus on which the PERSISTENT RESERVE OUT command was
2236 * received, with the additional sense code set to
2237 * RESERVATIONS RELEASED.
2240 (type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
||
2241 type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
)) {
2242 list_for_each_entry(pr_reg_p
,
2243 &pr_tmpl
->registration_list
,
2246 core_scsi3_ua_allocate(
2247 pr_reg_p
->pr_reg_nacl
,
2248 pr_reg_p
->pr_res_mapped_lun
,
2250 ASCQ_2AH_RESERVATIONS_RELEASED
);
2253 spin_unlock(&pr_tmpl
->registration_lock
);
2256 pr_tmpl
->pr_aptpl_active
= 0;
2257 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2258 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2259 " for UNREGISTER\n");
2263 if (!core_scsi3_update_and_write_aptpl(dev
, &pr_aptpl_buf
[0],
2264 pr_tmpl
->pr_aptpl_buf_len
)) {
2265 pr_tmpl
->pr_aptpl_active
= 1;
2266 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2267 " for UNREGISTER\n");
2270 goto out_free_aptpl_buf
;
2274 * Increment PRgeneration counter for struct se_device"
2275 * upon a successful REGISTER, see spc4r17 section 6.3.2
2276 * READ_KEYS service action.
2278 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(cmd
->se_dev
);
2279 pr_reg
->pr_res_key
= sa_res_key
;
2280 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2281 " Key for %s to: 0x%016Lx PRgeneration:"
2282 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2283 (ignore_key
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2284 pr_reg
->pr_reg_nacl
->initiatorname
,
2285 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2288 pr_tmpl
->pr_aptpl_active
= 0;
2289 core_scsi3_update_and_write_aptpl(dev
, NULL
, 0);
2290 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2293 goto out_put_pr_reg
;
2296 if (!core_scsi3_update_and_write_aptpl(dev
, &pr_aptpl_buf
[0],
2297 pr_tmpl
->pr_aptpl_buf_len
)) {
2298 pr_tmpl
->pr_aptpl_active
= 1;
2299 pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2304 kfree(pr_aptpl_buf
);
2307 core_scsi3_put_pr_reg(pr_reg
);
2311 unsigned char *core_scsi3_pr_dump_type(int type
)
2314 case PR_TYPE_WRITE_EXCLUSIVE
:
2315 return "Write Exclusive Access";
2316 case PR_TYPE_EXCLUSIVE_ACCESS
:
2317 return "Exclusive Access";
2318 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2319 return "Write Exclusive Access, Registrants Only";
2320 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2321 return "Exclusive Access, Registrants Only";
2322 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2323 return "Write Exclusive Access, All Registrants";
2324 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2325 return "Exclusive Access, All Registrants";
2330 return "Unknown SPC-3 PR Type";
2333 static sense_reason_t
2334 core_scsi3_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
)
2336 struct se_device
*dev
= cmd
->se_dev
;
2337 struct se_session
*se_sess
= cmd
->se_sess
;
2338 struct se_lun
*se_lun
= cmd
->se_lun
;
2339 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2340 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2341 char i_buf
[PR_REG_ISID_ID_LEN
];
2345 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2347 if (!se_sess
|| !se_lun
) {
2348 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2349 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2352 * Locate the existing *pr_reg via struct se_node_acl pointers
2354 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2357 pr_err("SPC-3 PR: Unable to locate"
2358 " PR_REGISTERED *pr_reg for RESERVE\n");
2359 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2362 * From spc4r17 Section 5.7.9: Reserving:
2364 * An application client creates a persistent reservation by issuing
2365 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2366 * a registered I_T nexus with the following parameters:
2367 * a) RESERVATION KEY set to the value of the reservation key that is
2368 * registered with the logical unit for the I_T nexus; and
2370 if (res_key
!= pr_reg
->pr_res_key
) {
2371 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2372 " does not match existing SA REGISTER res_key:"
2373 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2374 ret
= TCM_RESERVATION_CONFLICT
;
2375 goto out_put_pr_reg
;
2378 * From spc4r17 Section 5.7.9: Reserving:
2381 * b) TYPE field and SCOPE field set to the persistent reservation
2384 * Only one persistent reservation is allowed at a time per logical unit
2385 * and that persistent reservation has a scope of LU_SCOPE.
2387 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2388 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2389 ret
= TCM_INVALID_PARAMETER_LIST
;
2390 goto out_put_pr_reg
;
2393 * See if we have an existing PR reservation holder pointer at
2394 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2397 spin_lock(&dev
->dev_reservation_lock
);
2398 pr_res_holder
= dev
->dev_pr_res_holder
;
2399 if (pr_res_holder
) {
2401 * From spc4r17 Section 5.7.9: Reserving:
2403 * If the device server receives a PERSISTENT RESERVE OUT
2404 * command from an I_T nexus other than a persistent reservation
2405 * holder (see 5.7.10) that attempts to create a persistent
2406 * reservation when a persistent reservation already exists for
2407 * the logical unit, then the command shall be completed with
2408 * RESERVATION CONFLICT status.
2410 if (pr_res_holder
!= pr_reg
) {
2411 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2412 pr_err("SPC-3 PR: Attempted RESERVE from"
2413 " [%s]: %s while reservation already held by"
2414 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2415 cmd
->se_tfo
->get_fabric_name(),
2416 se_sess
->se_node_acl
->initiatorname
,
2417 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2418 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2420 spin_unlock(&dev
->dev_reservation_lock
);
2421 ret
= TCM_RESERVATION_CONFLICT
;
2422 goto out_put_pr_reg
;
2425 * From spc4r17 Section 5.7.9: Reserving:
2427 * If a persistent reservation holder attempts to modify the
2428 * type or scope of an existing persistent reservation, the
2429 * command shall be completed with RESERVATION CONFLICT status.
2431 if ((pr_res_holder
->pr_res_type
!= type
) ||
2432 (pr_res_holder
->pr_res_scope
!= scope
)) {
2433 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2434 pr_err("SPC-3 PR: Attempted RESERVE from"
2435 " [%s]: %s trying to change TYPE and/or SCOPE,"
2436 " while reservation already held by [%s]: %s,"
2437 " returning RESERVATION_CONFLICT\n",
2438 cmd
->se_tfo
->get_fabric_name(),
2439 se_sess
->se_node_acl
->initiatorname
,
2440 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2441 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2443 spin_unlock(&dev
->dev_reservation_lock
);
2444 ret
= TCM_RESERVATION_CONFLICT
;
2445 goto out_put_pr_reg
;
2448 * From spc4r17 Section 5.7.9: Reserving:
2450 * If the device server receives a PERSISTENT RESERVE OUT
2451 * command with RESERVE service action where the TYPE field and
2452 * the SCOPE field contain the same values as the existing type
2453 * and scope from a persistent reservation holder, it shall not
2454 * make any change to the existing persistent reservation and
2455 * shall completethe command with GOOD status.
2457 spin_unlock(&dev
->dev_reservation_lock
);
2459 goto out_put_pr_reg
;
2462 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2463 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2465 pr_reg
->pr_res_scope
= scope
;
2466 pr_reg
->pr_res_type
= type
;
2467 pr_reg
->pr_res_holder
= 1;
2468 dev
->dev_pr_res_holder
= pr_reg
;
2469 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2470 PR_REG_ISID_ID_LEN
);
2472 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2473 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2474 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2475 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2476 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2477 cmd
->se_tfo
->get_fabric_name(),
2478 se_sess
->se_node_acl
->initiatorname
,
2479 (prf_isid
) ? &i_buf
[0] : "");
2480 spin_unlock(&dev
->dev_reservation_lock
);
2482 if (pr_tmpl
->pr_aptpl_active
) {
2483 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2484 &pr_reg
->pr_aptpl_buf
[0],
2485 pr_tmpl
->pr_aptpl_buf_len
)) {
2486 pr_debug("SPC-3 PR: Updated APTPL metadata"
2493 core_scsi3_put_pr_reg(pr_reg
);
2497 static sense_reason_t
2498 core_scsi3_emulate_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
,
2502 case PR_TYPE_WRITE_EXCLUSIVE
:
2503 case PR_TYPE_EXCLUSIVE_ACCESS
:
2504 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2505 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2506 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2507 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2508 return core_scsi3_pro_reserve(cmd
, type
, scope
, res_key
);
2510 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2512 return TCM_INVALID_CDB_FIELD
;
2517 * Called with struct se_device->dev_reservation_lock held.
2519 static void __core_scsi3_complete_pro_release(
2520 struct se_device
*dev
,
2521 struct se_node_acl
*se_nacl
,
2522 struct t10_pr_registration
*pr_reg
,
2525 struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2526 char i_buf
[PR_REG_ISID_ID_LEN
];
2529 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2530 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2531 PR_REG_ISID_ID_LEN
);
2533 * Go ahead and release the current PR reservation holder.
2535 dev
->dev_pr_res_holder
= NULL
;
2537 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2538 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2539 tfo
->get_fabric_name(), (explict
) ? "explict" : "implict",
2540 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
2541 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2542 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2543 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2544 (prf_isid
) ? &i_buf
[0] : "");
2546 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2548 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2551 static sense_reason_t
2552 core_scsi3_emulate_pro_release(struct se_cmd
*cmd
, int type
, int scope
,
2555 struct se_device
*dev
= cmd
->se_dev
;
2556 struct se_session
*se_sess
= cmd
->se_sess
;
2557 struct se_lun
*se_lun
= cmd
->se_lun
;
2558 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2559 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2561 sense_reason_t ret
= 0;
2563 if (!se_sess
|| !se_lun
) {
2564 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2565 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2568 * Locate the existing *pr_reg via struct se_node_acl pointers
2570 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2572 pr_err("SPC-3 PR: Unable to locate"
2573 " PR_REGISTERED *pr_reg for RELEASE\n");
2574 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2577 * From spc4r17 Section 5.7.11.2 Releasing:
2579 * If there is no persistent reservation or in response to a persistent
2580 * reservation release request from a registered I_T nexus that is not a
2581 * persistent reservation holder (see 5.7.10), the device server shall
2584 * a) Not release the persistent reservation, if any;
2585 * b) Not remove any registrations; and
2586 * c) Complete the command with GOOD status.
2588 spin_lock(&dev
->dev_reservation_lock
);
2589 pr_res_holder
= dev
->dev_pr_res_holder
;
2590 if (!pr_res_holder
) {
2592 * No persistent reservation, return GOOD status.
2594 spin_unlock(&dev
->dev_reservation_lock
);
2595 goto out_put_pr_reg
;
2597 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2598 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
2601 if ((all_reg
== 0) && (pr_res_holder
!= pr_reg
)) {
2603 * Non 'All Registrants' PR Type cases..
2604 * Release request from a registered I_T nexus that is not a
2605 * persistent reservation holder. return GOOD status.
2607 spin_unlock(&dev
->dev_reservation_lock
);
2608 goto out_put_pr_reg
;
2612 * From spc4r17 Section 5.7.11.2 Releasing:
2614 * Only the persistent reservation holder (see 5.7.10) is allowed to
2615 * release a persistent reservation.
2617 * An application client releases the persistent reservation by issuing
2618 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2619 * an I_T nexus that is a persistent reservation holder with the
2620 * following parameters:
2622 * a) RESERVATION KEY field set to the value of the reservation key
2623 * that is registered with the logical unit for the I_T nexus;
2625 if (res_key
!= pr_reg
->pr_res_key
) {
2626 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2627 " does not match existing SA REGISTER res_key:"
2628 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2629 spin_unlock(&dev
->dev_reservation_lock
);
2630 ret
= TCM_RESERVATION_CONFLICT
;
2631 goto out_put_pr_reg
;
2634 * From spc4r17 Section 5.7.11.2 Releasing and above:
2636 * b) TYPE field and SCOPE field set to match the persistent
2637 * reservation being released.
2639 if ((pr_res_holder
->pr_res_type
!= type
) ||
2640 (pr_res_holder
->pr_res_scope
!= scope
)) {
2641 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2642 pr_err("SPC-3 PR RELEASE: Attempted to release"
2643 " reservation from [%s]: %s with different TYPE "
2644 "and/or SCOPE while reservation already held by"
2645 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2646 cmd
->se_tfo
->get_fabric_name(),
2647 se_sess
->se_node_acl
->initiatorname
,
2648 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2649 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2651 spin_unlock(&dev
->dev_reservation_lock
);
2652 ret
= TCM_RESERVATION_CONFLICT
;
2653 goto out_put_pr_reg
;
2656 * In response to a persistent reservation release request from the
2657 * persistent reservation holder the device server shall perform a
2658 * release by doing the following as an uninterrupted series of actions:
2659 * a) Release the persistent reservation;
2660 * b) Not remove any registration(s);
2661 * c) If the released persistent reservation is a registrants only type
2662 * or all registrants type persistent reservation,
2663 * the device server shall establish a unit attention condition for
2664 * the initiator port associated with every regis-
2665 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2666 * RESERVE OUT command with RELEASE service action was received,
2667 * with the additional sense code set to RESERVATIONS RELEASED; and
2668 * d) If the persistent reservation is of any other type, the device
2669 * server shall not establish a unit attention condition.
2671 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2674 spin_unlock(&dev
->dev_reservation_lock
);
2676 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2677 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2678 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2679 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2681 * If no UNIT ATTENTION conditions will be established for
2682 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2683 * go ahead and check for APTPL=1 update+write below
2688 spin_lock(&pr_tmpl
->registration_lock
);
2689 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2692 * Do not establish a UNIT ATTENTION condition
2693 * for the calling I_T Nexus
2695 if (pr_reg_p
== pr_reg
)
2698 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2699 pr_reg_p
->pr_res_mapped_lun
,
2700 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2702 spin_unlock(&pr_tmpl
->registration_lock
);
2705 if (pr_tmpl
->pr_aptpl_active
) {
2706 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
2707 &pr_reg
->pr_aptpl_buf
[0], pr_tmpl
->pr_aptpl_buf_len
)) {
2708 pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2712 core_scsi3_put_pr_reg(pr_reg
);
2716 static sense_reason_t
2717 core_scsi3_emulate_pro_clear(struct se_cmd
*cmd
, u64 res_key
)
2719 struct se_device
*dev
= cmd
->se_dev
;
2720 struct se_node_acl
*pr_reg_nacl
;
2721 struct se_session
*se_sess
= cmd
->se_sess
;
2722 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2723 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2724 u32 pr_res_mapped_lun
= 0;
2725 int calling_it_nexus
= 0;
2727 * Locate the existing *pr_reg via struct se_node_acl pointers
2729 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2730 se_sess
->se_node_acl
, se_sess
);
2732 pr_err("SPC-3 PR: Unable to locate"
2733 " PR_REGISTERED *pr_reg for CLEAR\n");
2734 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2737 * From spc4r17 section 5.7.11.6, Clearing:
2739 * Any application client may release the persistent reservation and
2740 * remove all registrations from a device server by issuing a
2741 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2742 * registered I_T nexus with the following parameter:
2744 * a) RESERVATION KEY field set to the value of the reservation key
2745 * that is registered with the logical unit for the I_T nexus.
2747 if (res_key
!= pr_reg_n
->pr_res_key
) {
2748 pr_err("SPC-3 PR REGISTER: Received"
2749 " res_key: 0x%016Lx does not match"
2750 " existing SA REGISTER res_key:"
2751 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2752 core_scsi3_put_pr_reg(pr_reg_n
);
2753 return TCM_RESERVATION_CONFLICT
;
2756 * a) Release the persistent reservation, if any;
2758 spin_lock(&dev
->dev_reservation_lock
);
2759 pr_res_holder
= dev
->dev_pr_res_holder
;
2760 if (pr_res_holder
) {
2761 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2762 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2765 spin_unlock(&dev
->dev_reservation_lock
);
2767 * b) Remove all registration(s) (see spc4r17 5.7.7);
2769 spin_lock(&pr_tmpl
->registration_lock
);
2770 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2771 &pr_tmpl
->registration_list
, pr_reg_list
) {
2773 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2774 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2775 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2776 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2779 * e) Establish a unit attention condition for the initiator
2780 * port associated with every registered I_T nexus other
2781 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2782 * command with CLEAR service action was received, with the
2783 * additional sense code set to RESERVATIONS PREEMPTED.
2785 if (!calling_it_nexus
)
2786 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2787 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2789 spin_unlock(&pr_tmpl
->registration_lock
);
2791 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2792 cmd
->se_tfo
->get_fabric_name());
2794 if (pr_tmpl
->pr_aptpl_active
) {
2795 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
2796 pr_debug("SPC-3 PR: Updated APTPL metadata"
2800 core_scsi3_pr_generation(dev
);
2805 * Called with struct se_device->dev_reservation_lock held.
2807 static void __core_scsi3_complete_pro_preempt(
2808 struct se_device
*dev
,
2809 struct t10_pr_registration
*pr_reg
,
2810 struct list_head
*preempt_and_abort_list
,
2815 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2816 struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2817 char i_buf
[PR_REG_ISID_ID_LEN
];
2820 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2821 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
2822 PR_REG_ISID_ID_LEN
);
2824 * Do an implict RELEASE of the existing reservation.
2826 if (dev
->dev_pr_res_holder
)
2827 __core_scsi3_complete_pro_release(dev
, nacl
,
2828 dev
->dev_pr_res_holder
, 0);
2830 dev
->dev_pr_res_holder
= pr_reg
;
2831 pr_reg
->pr_res_holder
= 1;
2832 pr_reg
->pr_res_type
= type
;
2833 pr_reg
->pr_res_scope
= scope
;
2835 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2836 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2837 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2838 core_scsi3_pr_dump_type(type
),
2839 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2840 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2841 tfo
->get_fabric_name(), (abort
) ? "_AND_ABORT" : "",
2842 nacl
->initiatorname
, (prf_isid
) ? &i_buf
[0] : "");
2844 * For PREEMPT_AND_ABORT, add the preempting reservation's
2845 * struct t10_pr_registration to the list that will be compared
2846 * against received CDBs..
2848 if (preempt_and_abort_list
)
2849 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2850 preempt_and_abort_list
);
2853 static void core_scsi3_release_preempt_and_abort(
2854 struct list_head
*preempt_and_abort_list
,
2855 struct t10_pr_registration
*pr_reg_holder
)
2857 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2859 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2860 pr_reg_abort_list
) {
2862 list_del(&pr_reg
->pr_reg_abort_list
);
2863 if (pr_reg_holder
== pr_reg
)
2865 if (pr_reg
->pr_res_holder
) {
2866 pr_warn("pr_reg->pr_res_holder still set\n");
2870 pr_reg
->pr_reg_deve
= NULL
;
2871 pr_reg
->pr_reg_nacl
= NULL
;
2872 kfree(pr_reg
->pr_aptpl_buf
);
2873 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2877 static sense_reason_t
2878 core_scsi3_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
,
2879 u64 sa_res_key
, int abort
)
2881 struct se_device
*dev
= cmd
->se_dev
;
2882 struct se_node_acl
*pr_reg_nacl
;
2883 struct se_session
*se_sess
= cmd
->se_sess
;
2884 LIST_HEAD(preempt_and_abort_list
);
2885 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2886 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2887 u32 pr_res_mapped_lun
= 0;
2888 int all_reg
= 0, calling_it_nexus
= 0, released_regs
= 0;
2889 int prh_type
= 0, prh_scope
= 0;
2892 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2894 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2897 pr_err("SPC-3 PR: Unable to locate"
2898 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2899 (abort
) ? "_AND_ABORT" : "");
2900 return TCM_RESERVATION_CONFLICT
;
2902 if (pr_reg_n
->pr_res_key
!= res_key
) {
2903 core_scsi3_put_pr_reg(pr_reg_n
);
2904 return TCM_RESERVATION_CONFLICT
;
2906 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2907 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2908 core_scsi3_put_pr_reg(pr_reg_n
);
2909 return TCM_INVALID_PARAMETER_LIST
;
2912 spin_lock(&dev
->dev_reservation_lock
);
2913 pr_res_holder
= dev
->dev_pr_res_holder
;
2914 if (pr_res_holder
&&
2915 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2916 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
2919 if (!all_reg
&& !sa_res_key
) {
2920 spin_unlock(&dev
->dev_reservation_lock
);
2921 core_scsi3_put_pr_reg(pr_reg_n
);
2922 return TCM_INVALID_PARAMETER_LIST
;
2925 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2927 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2928 * persistent reservation holder or there is no persistent reservation
2929 * holder (i.e., there is no persistent reservation), then the device
2930 * server shall perform a preempt by doing the following in an
2931 * uninterrupted series of actions. (See below..)
2933 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
2935 * No existing or SA Reservation Key matching reservations..
2937 * PROUT SA PREEMPT with All Registrant type reservations are
2938 * allowed to be processed without a matching SA Reservation Key
2940 spin_lock(&pr_tmpl
->registration_lock
);
2941 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2942 &pr_tmpl
->registration_list
, pr_reg_list
) {
2944 * Removing of registrations in non all registrants
2945 * type reservations without a matching SA reservation
2948 * a) Remove the registrations for all I_T nexuses
2949 * specified by the SERVICE ACTION RESERVATION KEY
2951 * b) Ignore the contents of the SCOPE and TYPE fields;
2952 * c) Process tasks as defined in 5.7.1; and
2953 * d) Establish a unit attention condition for the
2954 * initiator port associated with every I_T nexus
2955 * that lost its registration other than the I_T
2956 * nexus on which the PERSISTENT RESERVE OUT command
2957 * was received, with the additional sense code set
2958 * to REGISTRATIONS PREEMPTED.
2961 if (pr_reg
->pr_res_key
!= sa_res_key
)
2964 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2965 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2966 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2967 __core_scsi3_free_registration(dev
, pr_reg
,
2968 (abort
) ? &preempt_and_abort_list
:
2969 NULL
, calling_it_nexus
);
2973 * Case for any existing all registrants type
2974 * reservation, follow logic in spc4r17 section
2975 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2977 * For a ZERO SA Reservation key, release
2978 * all other registrations and do an implict
2979 * release of active persistent reservation.
2981 * For a non-ZERO SA Reservation key, only
2982 * release the matching reservation key from
2986 (pr_reg
->pr_res_key
!= sa_res_key
))
2989 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2990 if (calling_it_nexus
)
2993 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2994 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2995 __core_scsi3_free_registration(dev
, pr_reg
,
2996 (abort
) ? &preempt_and_abort_list
:
3000 if (!calling_it_nexus
)
3001 core_scsi3_ua_allocate(pr_reg_nacl
,
3002 pr_res_mapped_lun
, 0x2A,
3003 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3005 spin_unlock(&pr_tmpl
->registration_lock
);
3007 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3008 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3009 * RESERVATION KEY field to a value that does not match any
3010 * registered reservation key, then the device server shall
3011 * complete the command with RESERVATION CONFLICT status.
3013 if (!released_regs
) {
3014 spin_unlock(&dev
->dev_reservation_lock
);
3015 core_scsi3_put_pr_reg(pr_reg_n
);
3016 return TCM_RESERVATION_CONFLICT
;
3019 * For an existing all registrants type reservation
3020 * with a zero SA rservation key, preempt the existing
3021 * reservation with the new PR type and scope.
3023 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
3024 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3025 (abort
) ? &preempt_and_abort_list
: NULL
,
3026 type
, scope
, abort
);
3029 core_scsi3_release_preempt_and_abort(
3030 &preempt_and_abort_list
, pr_reg_n
);
3032 spin_unlock(&dev
->dev_reservation_lock
);
3034 if (pr_tmpl
->pr_aptpl_active
) {
3035 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3036 &pr_reg_n
->pr_aptpl_buf
[0],
3037 pr_tmpl
->pr_aptpl_buf_len
)) {
3038 pr_debug("SPC-3 PR: Updated APTPL"
3039 " metadata for PREEMPT%s\n", (abort
) ?
3044 core_scsi3_put_pr_reg(pr_reg_n
);
3045 core_scsi3_pr_generation(cmd
->se_dev
);
3049 * The PREEMPTing SA reservation key matches that of the
3050 * existing persistent reservation, first, we check if
3051 * we are preempting our own reservation.
3052 * From spc4r17, section 5.7.11.4.3 Preempting
3053 * persistent reservations and registration handling
3055 * If an all registrants persistent reservation is not
3056 * present, it is not an error for the persistent
3057 * reservation holder to preempt itself (i.e., a
3058 * PERSISTENT RESERVE OUT with a PREEMPT service action
3059 * or a PREEMPT AND ABORT service action with the
3060 * SERVICE ACTION RESERVATION KEY value equal to the
3061 * persistent reservation holder's reservation key that
3062 * is received from the persistent reservation holder).
3063 * In that case, the device server shall establish the
3064 * new persistent reservation and maintain the
3067 prh_type
= pr_res_holder
->pr_res_type
;
3068 prh_scope
= pr_res_holder
->pr_res_scope
;
3070 * If the SERVICE ACTION RESERVATION KEY field identifies a
3071 * persistent reservation holder (see 5.7.10), the device
3072 * server shall perform a preempt by doing the following as
3073 * an uninterrupted series of actions:
3075 * a) Release the persistent reservation for the holder
3076 * identified by the SERVICE ACTION RESERVATION KEY field;
3078 if (pr_reg_n
!= pr_res_holder
)
3079 __core_scsi3_complete_pro_release(dev
,
3080 pr_res_holder
->pr_reg_nacl
,
3081 dev
->dev_pr_res_holder
, 0);
3083 * b) Remove the registrations for all I_T nexuses identified
3084 * by the SERVICE ACTION RESERVATION KEY field, except the
3085 * I_T nexus that is being used for the PERSISTENT RESERVE
3086 * OUT command. If an all registrants persistent reservation
3087 * is present and the SERVICE ACTION RESERVATION KEY field
3088 * is set to zero, then all registrations shall be removed
3089 * except for that of the I_T nexus that is being used for
3090 * the PERSISTENT RESERVE OUT command;
3092 spin_lock(&pr_tmpl
->registration_lock
);
3093 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3094 &pr_tmpl
->registration_list
, pr_reg_list
) {
3096 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3097 if (calling_it_nexus
)
3100 if (pr_reg
->pr_res_key
!= sa_res_key
)
3103 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3104 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3105 __core_scsi3_free_registration(dev
, pr_reg
,
3106 (abort
) ? &preempt_and_abort_list
: NULL
,
3109 * e) Establish a unit attention condition for the initiator
3110 * port associated with every I_T nexus that lost its
3111 * persistent reservation and/or registration, with the
3112 * additional sense code set to REGISTRATIONS PREEMPTED;
3114 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3115 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3117 spin_unlock(&pr_tmpl
->registration_lock
);
3119 * c) Establish a persistent reservation for the preempting
3120 * I_T nexus using the contents of the SCOPE and TYPE fields;
3122 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3123 (abort
) ? &preempt_and_abort_list
: NULL
,
3124 type
, scope
, abort
);
3126 * d) Process tasks as defined in 5.7.1;
3128 * f) If the type or scope has changed, then for every I_T nexus
3129 * whose reservation key was not removed, except for the I_T
3130 * nexus on which the PERSISTENT RESERVE OUT command was
3131 * received, the device server shall establish a unit
3132 * attention condition for the initiator port associated with
3133 * that I_T nexus, with the additional sense code set to
3134 * RESERVATIONS RELEASED. If the type or scope have not
3135 * changed, then no unit attention condition(s) shall be
3136 * established for this reason.
3138 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3139 spin_lock(&pr_tmpl
->registration_lock
);
3140 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3141 &pr_tmpl
->registration_list
, pr_reg_list
) {
3143 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3144 if (calling_it_nexus
)
3147 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3148 pr_reg
->pr_res_mapped_lun
, 0x2A,
3149 ASCQ_2AH_RESERVATIONS_RELEASED
);
3151 spin_unlock(&pr_tmpl
->registration_lock
);
3153 spin_unlock(&dev
->dev_reservation_lock
);
3155 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3156 * All received CDBs for the matching existing reservation and
3157 * registrations undergo ABORT_TASK logic.
3159 * From there, core_scsi3_release_preempt_and_abort() will
3160 * release every registration in the list (which have already
3161 * been removed from the primary pr_reg list), except the
3162 * new persistent reservation holder, the calling Initiator Port.
3165 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3166 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3170 if (pr_tmpl
->pr_aptpl_active
) {
3171 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3172 &pr_reg_n
->pr_aptpl_buf
[0],
3173 pr_tmpl
->pr_aptpl_buf_len
)) {
3174 pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3175 "%s\n", abort
? "_AND_ABORT" : "");
3179 core_scsi3_put_pr_reg(pr_reg_n
);
3180 core_scsi3_pr_generation(cmd
->se_dev
);
3184 static sense_reason_t
3185 core_scsi3_emulate_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
,
3186 u64 res_key
, u64 sa_res_key
, int abort
)
3189 case PR_TYPE_WRITE_EXCLUSIVE
:
3190 case PR_TYPE_EXCLUSIVE_ACCESS
:
3191 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3192 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3193 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3194 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3195 return core_scsi3_pro_preempt(cmd
, type
, scope
, res_key
,
3198 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3199 " Type: 0x%02x\n", (abort
) ? "_AND_ABORT" : "", type
);
3200 return TCM_INVALID_CDB_FIELD
;
3205 static sense_reason_t
3206 core_scsi3_emulate_pro_register_and_move(struct se_cmd
*cmd
, u64 res_key
,
3207 u64 sa_res_key
, int aptpl
, int unreg
)
3209 struct se_session
*se_sess
= cmd
->se_sess
;
3210 struct se_device
*dev
= cmd
->se_dev
;
3211 struct se_dev_entry
*dest_se_deve
= NULL
;
3212 struct se_lun
*se_lun
= cmd
->se_lun
;
3213 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3214 struct se_port
*se_port
;
3215 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3216 struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3217 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3218 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3220 unsigned char *initiator_str
;
3221 char *iport_ptr
= NULL
, dest_iport
[64], i_buf
[PR_REG_ISID_ID_LEN
];
3222 u32 tid_len
, tmp_tid_len
;
3223 int new_reg
= 0, type
, scope
, matching_iname
, prf_isid
;
3225 unsigned short rtpi
;
3226 unsigned char proto_ident
;
3228 if (!se_sess
|| !se_lun
) {
3229 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3230 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3233 memset(dest_iport
, 0, 64);
3234 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3235 se_tpg
= se_sess
->se_tpg
;
3236 tf_ops
= se_tpg
->se_tpg_tfo
;
3238 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3239 * Register behaviors for a REGISTER AND MOVE service action
3241 * Locate the existing *pr_reg via struct se_node_acl pointers
3243 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3246 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3247 " *pr_reg for REGISTER_AND_MOVE\n");
3248 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3251 * The provided reservation key much match the existing reservation key
3252 * provided during this initiator's I_T nexus registration.
3254 if (res_key
!= pr_reg
->pr_res_key
) {
3255 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3256 " res_key: 0x%016Lx does not match existing SA REGISTER"
3257 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3258 ret
= TCM_RESERVATION_CONFLICT
;
3259 goto out_put_pr_reg
;
3262 * The service active reservation key needs to be non zero
3265 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3267 ret
= TCM_INVALID_PARAMETER_LIST
;
3268 goto out_put_pr_reg
;
3272 * Determine the Relative Target Port Identifier where the reservation
3273 * will be moved to for the TransportID containing SCSI initiator WWN
3276 buf
= transport_kmap_data_sg(cmd
);
3278 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3279 goto out_put_pr_reg
;
3282 rtpi
= (buf
[18] & 0xff) << 8;
3283 rtpi
|= buf
[19] & 0xff;
3284 tid_len
= (buf
[20] & 0xff) << 24;
3285 tid_len
|= (buf
[21] & 0xff) << 16;
3286 tid_len
|= (buf
[22] & 0xff) << 8;
3287 tid_len
|= buf
[23] & 0xff;
3288 transport_kunmap_data_sg(cmd
);
3291 if ((tid_len
+ 24) != cmd
->data_length
) {
3292 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3293 " does not equal CDB data_length: %u\n", tid_len
,
3295 ret
= TCM_INVALID_PARAMETER_LIST
;
3296 goto out_put_pr_reg
;
3299 spin_lock(&dev
->se_port_lock
);
3300 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3301 if (se_port
->sep_rtpi
!= rtpi
)
3303 dest_se_tpg
= se_port
->sep_tpg
;
3306 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3310 atomic_inc(&dest_se_tpg
->tpg_pr_ref_count
);
3311 smp_mb__after_atomic_inc();
3312 spin_unlock(&dev
->se_port_lock
);
3314 if (core_scsi3_tpg_depend_item(dest_se_tpg
)) {
3315 pr_err("core_scsi3_tpg_depend_item() failed"
3316 " for dest_se_tpg\n");
3317 atomic_dec(&dest_se_tpg
->tpg_pr_ref_count
);
3318 smp_mb__after_atomic_dec();
3319 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3320 goto out_put_pr_reg
;
3323 spin_lock(&dev
->se_port_lock
);
3326 spin_unlock(&dev
->se_port_lock
);
3328 if (!dest_se_tpg
|| !dest_tf_ops
) {
3329 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3330 " fabric ops from Relative Target Port Identifier:"
3332 ret
= TCM_INVALID_PARAMETER_LIST
;
3333 goto out_put_pr_reg
;
3336 buf
= transport_kmap_data_sg(cmd
);
3338 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3339 goto out_put_pr_reg
;
3341 proto_ident
= (buf
[24] & 0x0f);
3343 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3344 " 0x%02x\n", proto_ident
);
3346 if (proto_ident
!= dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
)) {
3347 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3348 " proto_ident: 0x%02x does not match ident: 0x%02x"
3349 " from fabric: %s\n", proto_ident
,
3350 dest_tf_ops
->get_fabric_proto_ident(dest_se_tpg
),
3351 dest_tf_ops
->get_fabric_name());
3352 ret
= TCM_INVALID_PARAMETER_LIST
;
3355 if (dest_tf_ops
->tpg_parse_pr_out_transport_id
== NULL
) {
3356 pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3357 " containg a valid tpg_parse_pr_out_transport_id"
3358 " function pointer\n");
3359 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3362 initiator_str
= dest_tf_ops
->tpg_parse_pr_out_transport_id(dest_se_tpg
,
3363 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3364 if (!initiator_str
) {
3365 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3366 " initiator_str from Transport ID\n");
3367 ret
= TCM_INVALID_PARAMETER_LIST
;
3371 transport_kunmap_data_sg(cmd
);
3374 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3375 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3376 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3379 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3380 * action specifies a TransportID that is the same as the initiator port
3381 * of the I_T nexus for the command received, then the command shall
3382 * be terminated with CHECK CONDITION status, with the sense key set to
3383 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3384 * IN PARAMETER LIST.
3386 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3387 matching_iname
= (!strcmp(initiator_str
,
3388 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3389 if (!matching_iname
)
3390 goto after_iport_check
;
3392 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3393 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3394 " matches: %s on received I_T Nexus\n", initiator_str
,
3395 pr_reg_nacl
->initiatorname
);
3396 ret
= TCM_INVALID_PARAMETER_LIST
;
3399 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3400 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3401 " matches: %s %s on received I_T Nexus\n",
3402 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3403 pr_reg
->pr_reg_isid
);
3404 ret
= TCM_INVALID_PARAMETER_LIST
;
3409 * Locate the destination struct se_node_acl from the received Transport ID
3411 spin_lock_irq(&dest_se_tpg
->acl_node_lock
);
3412 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3414 if (dest_node_acl
) {
3415 atomic_inc(&dest_node_acl
->acl_pr_ref_count
);
3416 smp_mb__after_atomic_inc();
3418 spin_unlock_irq(&dest_se_tpg
->acl_node_lock
);
3420 if (!dest_node_acl
) {
3421 pr_err("Unable to locate %s dest_node_acl for"
3422 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3424 ret
= TCM_INVALID_PARAMETER_LIST
;
3428 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
3429 pr_err("core_scsi3_nodeacl_depend_item() for"
3430 " dest_node_acl\n");
3431 atomic_dec(&dest_node_acl
->acl_pr_ref_count
);
3432 smp_mb__after_atomic_dec();
3433 dest_node_acl
= NULL
;
3434 ret
= TCM_INVALID_PARAMETER_LIST
;
3438 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3439 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3440 dest_node_acl
->initiatorname
);
3443 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3446 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3447 if (!dest_se_deve
) {
3448 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3449 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3450 ret
= TCM_INVALID_PARAMETER_LIST
;
3454 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
3455 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3456 atomic_dec(&dest_se_deve
->pr_ref_count
);
3457 smp_mb__after_atomic_dec();
3458 dest_se_deve
= NULL
;
3459 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3463 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3464 " ACL for dest_se_deve->mapped_lun: %u\n",
3465 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3466 dest_se_deve
->mapped_lun
);
3469 * A persistent reservation needs to already existing in order to
3470 * successfully complete the REGISTER_AND_MOVE service action..
3472 spin_lock(&dev
->dev_reservation_lock
);
3473 pr_res_holder
= dev
->dev_pr_res_holder
;
3474 if (!pr_res_holder
) {
3475 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3476 " currently held\n");
3477 spin_unlock(&dev
->dev_reservation_lock
);
3478 ret
= TCM_INVALID_CDB_FIELD
;
3482 * The received on I_T Nexus must be the reservation holder.
3484 * From spc4r17 section 5.7.8 Table 50 --
3485 * Register behaviors for a REGISTER AND MOVE service action
3487 if (pr_res_holder
!= pr_reg
) {
3488 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3489 " Nexus is not reservation holder\n");
3490 spin_unlock(&dev
->dev_reservation_lock
);
3491 ret
= TCM_RESERVATION_CONFLICT
;
3495 * From spc4r17 section 5.7.8: registering and moving reservation
3497 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3498 * action is received and the established persistent reservation is a
3499 * Write Exclusive - All Registrants type or Exclusive Access -
3500 * All Registrants type reservation, then the command shall be completed
3501 * with RESERVATION CONFLICT status.
3503 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3504 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3505 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3506 " reservation for type: %s\n",
3507 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3508 spin_unlock(&dev
->dev_reservation_lock
);
3509 ret
= TCM_RESERVATION_CONFLICT
;
3512 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3514 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3516 type
= pr_res_holder
->pr_res_type
;
3517 scope
= pr_res_holder
->pr_res_type
;
3519 * c) Associate the reservation key specified in the SERVICE ACTION
3520 * RESERVATION KEY field with the I_T nexus specified as the
3521 * destination of the register and move, where:
3522 * A) The I_T nexus is specified by the TransportID and the
3523 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3524 * B) Regardless of the TransportID format used, the association for
3525 * the initiator port is based on either the initiator port name
3526 * (see 3.1.71) on SCSI transport protocols where port names are
3527 * required or the initiator port identifier (see 3.1.70) on SCSI
3528 * transport protocols where port names are not required;
3529 * d) Register the reservation key specified in the SERVICE ACTION
3530 * RESERVATION KEY field;
3531 * e) Retain the reservation key specified in the SERVICE ACTION
3532 * RESERVATION KEY field and associated information;
3534 * Also, It is not an error for a REGISTER AND MOVE service action to
3535 * register an I_T nexus that is already registered with the same
3536 * reservation key or a different reservation key.
3538 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3541 if (core_scsi3_alloc_registration(cmd
->se_dev
,
3542 dest_node_acl
, dest_se_deve
, iport_ptr
,
3543 sa_res_key
, 0, aptpl
, 2, 1)) {
3544 spin_unlock(&dev
->dev_reservation_lock
);
3545 ret
= TCM_INVALID_PARAMETER_LIST
;
3548 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3553 * f) Release the persistent reservation for the persistent reservation
3554 * holder (i.e., the I_T nexus on which the
3556 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3557 dev
->dev_pr_res_holder
, 0);
3559 * g) Move the persistent reservation to the specified I_T nexus using
3560 * the same scope and type as the persistent reservation released in
3563 dev
->dev_pr_res_holder
= dest_pr_reg
;
3564 dest_pr_reg
->pr_res_holder
= 1;
3565 dest_pr_reg
->pr_res_type
= type
;
3566 pr_reg
->pr_res_scope
= scope
;
3567 prf_isid
= core_pr_dump_initiator_port(pr_reg
, &i_buf
[0],
3568 PR_REG_ISID_ID_LEN
);
3570 * Increment PRGeneration for existing registrations..
3573 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3574 spin_unlock(&dev
->dev_reservation_lock
);
3576 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3577 " created new reservation holder TYPE: %s on object RTPI:"
3578 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3579 core_scsi3_pr_dump_type(type
), rtpi
,
3580 dest_pr_reg
->pr_res_generation
);
3581 pr_debug("SPC-3 PR Successfully moved reservation from"
3582 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3583 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3584 (prf_isid
) ? &i_buf
[0] : "", dest_tf_ops
->get_fabric_name(),
3585 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3588 * It is now safe to release configfs group dependencies for destination
3589 * of Transport ID Initiator Device/Port Identifier
3591 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3592 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3593 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3595 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3596 * nexus on which PERSISTENT RESERVE OUT command was received.
3599 spin_lock(&pr_tmpl
->registration_lock
);
3600 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3601 spin_unlock(&pr_tmpl
->registration_lock
);
3603 core_scsi3_put_pr_reg(pr_reg
);
3606 * Clear the APTPL metadata if APTPL has been disabled, otherwise
3607 * write out the updated metadata to struct file for this SCSI device.
3610 pr_tmpl
->pr_aptpl_active
= 0;
3611 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, NULL
, 0);
3612 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3613 " REGISTER_AND_MOVE\n");
3615 pr_tmpl
->pr_aptpl_active
= 1;
3616 if (!core_scsi3_update_and_write_aptpl(cmd
->se_dev
,
3617 &dest_pr_reg
->pr_aptpl_buf
[0],
3618 pr_tmpl
->pr_aptpl_buf_len
)) {
3619 pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3620 " REGISTER_AND_MOVE\n");
3624 transport_kunmap_data_sg(cmd
);
3626 core_scsi3_put_pr_reg(dest_pr_reg
);
3630 transport_kunmap_data_sg(cmd
);
3632 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3634 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3635 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3638 core_scsi3_put_pr_reg(pr_reg
);
3642 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3644 unsigned int __v1
, __v2
;
3646 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3647 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3649 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3653 * See spc4r17 section 6.14 Table 170
3656 target_scsi3_emulate_pr_out(struct se_cmd
*cmd
)
3658 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3660 u64 res_key
, sa_res_key
;
3661 int sa
, scope
, type
, aptpl
;
3662 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3666 * Following spc2r20 5.5.1 Reservations overview:
3668 * If a logical unit has been reserved by any RESERVE command and is
3669 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3670 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3671 * initiator or service action and shall terminate with a RESERVATION
3674 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
3675 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3676 " SPC-2 reservation is held, returning"
3677 " RESERVATION_CONFLICT\n");
3678 return TCM_RESERVATION_CONFLICT
;
3682 * FIXME: A NULL struct se_session pointer means an this is not coming from
3683 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3686 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3688 if (cmd
->data_length
< 24) {
3689 pr_warn("SPC-PR: Received PR OUT parameter list"
3690 " length too small: %u\n", cmd
->data_length
);
3691 return TCM_INVALID_PARAMETER_LIST
;
3695 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3697 sa
= (cdb
[1] & 0x1f);
3698 scope
= (cdb
[2] & 0xf0);
3699 type
= (cdb
[2] & 0x0f);
3701 buf
= transport_kmap_data_sg(cmd
);
3703 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3706 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3708 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3709 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3711 * REGISTER_AND_MOVE uses a different SA parameter list containing
3712 * SCSI TransportIDs.
3714 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3715 spec_i_pt
= (buf
[20] & 0x08);
3716 all_tg_pt
= (buf
[20] & 0x04);
3717 aptpl
= (buf
[20] & 0x01);
3719 aptpl
= (buf
[17] & 0x01);
3720 unreg
= (buf
[17] & 0x02);
3722 transport_kunmap_data_sg(cmd
);
3726 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3728 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
))
3729 return TCM_INVALID_PARAMETER_LIST
;
3732 * From spc4r17 section 6.14:
3734 * If the SPEC_I_PT bit is set to zero, the service action is not
3735 * REGISTER AND MOVE, and the parameter list length is not 24, then
3736 * the command shall be terminated with CHECK CONDITION status, with
3737 * the sense key set to ILLEGAL REQUEST, and the additional sense
3738 * code set to PARAMETER LIST LENGTH ERROR.
3740 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3741 (cmd
->data_length
!= 24)) {
3742 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3743 " list length: %u\n", cmd
->data_length
);
3744 return TCM_INVALID_PARAMETER_LIST
;
3748 * (core_scsi3_emulate_pro_* function parameters
3749 * are defined by spc4r17 Table 174:
3750 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3754 ret
= core_scsi3_emulate_pro_register(cmd
,
3755 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 0);
3758 ret
= core_scsi3_emulate_pro_reserve(cmd
, type
, scope
, res_key
);
3761 ret
= core_scsi3_emulate_pro_release(cmd
, type
, scope
, res_key
);
3764 ret
= core_scsi3_emulate_pro_clear(cmd
, res_key
);
3767 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3768 res_key
, sa_res_key
, 0);
3770 case PRO_PREEMPT_AND_ABORT
:
3771 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3772 res_key
, sa_res_key
, 1);
3774 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3775 ret
= core_scsi3_emulate_pro_register(cmd
,
3776 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, 1);
3778 case PRO_REGISTER_AND_MOVE
:
3779 ret
= core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3780 sa_res_key
, aptpl
, unreg
);
3783 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3784 " action: 0x%02x\n", cdb
[1] & 0x1f);
3785 return TCM_INVALID_CDB_FIELD
;
3789 target_complete_cmd(cmd
, GOOD
);
3794 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3796 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3798 static sense_reason_t
3799 core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3801 struct se_device
*dev
= cmd
->se_dev
;
3802 struct t10_pr_registration
*pr_reg
;
3804 u32 add_len
= 0, off
= 8;
3806 if (cmd
->data_length
< 8) {
3807 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3808 " too small\n", cmd
->data_length
);
3809 return TCM_INVALID_CDB_FIELD
;
3812 buf
= transport_kmap_data_sg(cmd
);
3814 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3816 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3817 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3818 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3819 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3821 spin_lock(&dev
->t10_pr
.registration_lock
);
3822 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
3825 * Check for overflow of 8byte PRI READ_KEYS payload and
3826 * next reservation key list descriptor.
3828 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3831 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3832 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3833 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3834 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3835 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3836 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3837 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3838 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3842 spin_unlock(&dev
->t10_pr
.registration_lock
);
3844 buf
[4] = ((add_len
>> 24) & 0xff);
3845 buf
[5] = ((add_len
>> 16) & 0xff);
3846 buf
[6] = ((add_len
>> 8) & 0xff);
3847 buf
[7] = (add_len
& 0xff);
3849 transport_kunmap_data_sg(cmd
);
3855 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3857 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3859 static sense_reason_t
3860 core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
3862 struct se_device
*dev
= cmd
->se_dev
;
3863 struct t10_pr_registration
*pr_reg
;
3866 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
3868 if (cmd
->data_length
< 8) {
3869 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3870 " too small\n", cmd
->data_length
);
3871 return TCM_INVALID_CDB_FIELD
;
3874 buf
= transport_kmap_data_sg(cmd
);
3876 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3878 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3879 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3880 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3881 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3883 spin_lock(&dev
->dev_reservation_lock
);
3884 pr_reg
= dev
->dev_pr_res_holder
;
3887 * Set the hardcoded Additional Length
3889 buf
[4] = ((add_len
>> 24) & 0xff);
3890 buf
[5] = ((add_len
>> 16) & 0xff);
3891 buf
[6] = ((add_len
>> 8) & 0xff);
3892 buf
[7] = (add_len
& 0xff);
3894 if (cmd
->data_length
< 22)
3898 * Set the Reservation key.
3900 * From spc4r17, section 5.7.10:
3901 * A persistent reservation holder has its reservation key
3902 * returned in the parameter data from a PERSISTENT
3903 * RESERVE IN command with READ RESERVATION service action as
3905 * a) For a persistent reservation of the type Write Exclusive
3906 * - All Registrants or Exclusive Access  All Regitrants,
3907 * the reservation key shall be set to zero; or
3908 * b) For all other persistent reservation types, the
3909 * reservation key shall be set to the registered
3910 * reservation key for the I_T nexus that holds the
3911 * persistent reservation.
3913 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3914 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
3917 pr_res_key
= pr_reg
->pr_res_key
;
3919 buf
[8] = ((pr_res_key
>> 56) & 0xff);
3920 buf
[9] = ((pr_res_key
>> 48) & 0xff);
3921 buf
[10] = ((pr_res_key
>> 40) & 0xff);
3922 buf
[11] = ((pr_res_key
>> 32) & 0xff);
3923 buf
[12] = ((pr_res_key
>> 24) & 0xff);
3924 buf
[13] = ((pr_res_key
>> 16) & 0xff);
3925 buf
[14] = ((pr_res_key
>> 8) & 0xff);
3926 buf
[15] = (pr_res_key
& 0xff);
3928 * Set the SCOPE and TYPE
3930 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
3931 (pr_reg
->pr_res_type
& 0x0f);
3935 spin_unlock(&dev
->dev_reservation_lock
);
3936 transport_kunmap_data_sg(cmd
);
3942 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3944 * See spc4r17 section 6.13.4 Table 165
3946 static sense_reason_t
3947 core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
3949 struct se_device
*dev
= cmd
->se_dev
;
3950 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3952 u16 add_len
= 8; /* Hardcoded to 8. */
3954 if (cmd
->data_length
< 6) {
3955 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3956 " %u too small\n", cmd
->data_length
);
3957 return TCM_INVALID_CDB_FIELD
;
3960 buf
= transport_kmap_data_sg(cmd
);
3962 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3964 buf
[0] = ((add_len
<< 8) & 0xff);
3965 buf
[1] = (add_len
& 0xff);
3966 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3967 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3968 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3969 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3971 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3972 * set the TMV: Task Mask Valid bit.
3976 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3978 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3980 * PTPL_A: Persistence across Target Power Loss Active bit
3982 if (pr_tmpl
->pr_aptpl_active
)
3985 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3987 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3988 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3989 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3990 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3991 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3992 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3994 transport_kunmap_data_sg(cmd
);
4000 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4002 * See spc4r17 section 6.13.5 Table 168 and 169
4004 static sense_reason_t
4005 core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
4007 struct se_device
*dev
= cmd
->se_dev
;
4008 struct se_node_acl
*se_nacl
;
4009 struct se_portal_group
*se_tpg
;
4010 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
4011 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
4013 u32 add_desc_len
= 0, add_len
= 0, desc_len
, exp_desc_len
;
4014 u32 off
= 8; /* off into first Full Status descriptor */
4015 int format_code
= 0;
4017 if (cmd
->data_length
< 8) {
4018 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4019 " too small\n", cmd
->data_length
);
4020 return TCM_INVALID_CDB_FIELD
;
4023 buf
= transport_kmap_data_sg(cmd
);
4025 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
4027 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
4028 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
4029 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
4030 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
4032 spin_lock(&pr_tmpl
->registration_lock
);
4033 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
4034 &pr_tmpl
->registration_list
, pr_reg_list
) {
4036 se_nacl
= pr_reg
->pr_reg_nacl
;
4037 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
4040 atomic_inc(&pr_reg
->pr_res_holders
);
4041 smp_mb__after_atomic_inc();
4042 spin_unlock(&pr_tmpl
->registration_lock
);
4044 * Determine expected length of $FABRIC_MOD specific
4045 * TransportID full status descriptor..
4047 exp_desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id_len(
4048 se_tpg
, se_nacl
, pr_reg
, &format_code
);
4050 if ((exp_desc_len
+ add_len
) > cmd
->data_length
) {
4051 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4052 " out of buffer: %d\n", cmd
->data_length
);
4053 spin_lock(&pr_tmpl
->registration_lock
);
4054 atomic_dec(&pr_reg
->pr_res_holders
);
4055 smp_mb__after_atomic_dec();
4059 * Set RESERVATION KEY
4061 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
4062 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
4063 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
4064 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
4065 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
4066 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
4067 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
4068 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
4069 off
+= 4; /* Skip Over Reserved area */
4072 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4074 if (pr_reg
->pr_reg_all_tg_pt
)
4077 * The struct se_lun pointer will be present for the
4078 * reservation holder for PR_HOLDER bit.
4080 * Also, if this registration is the reservation
4081 * holder, fill in SCOPE and TYPE in the next byte.
4083 if (pr_reg
->pr_res_holder
) {
4085 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
4086 (pr_reg
->pr_res_type
& 0x0f);
4090 off
+= 4; /* Skip over reserved area */
4092 * From spc4r17 6.3.15:
4094 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4095 * IDENTIFIER field contains the relative port identifier (see
4096 * 3.1.120) of the target port that is part of the I_T nexus
4097 * described by this full status descriptor. If the ALL_TG_PT
4098 * bit is set to one, the contents of the RELATIVE TARGET PORT
4099 * IDENTIFIER field are not defined by this standard.
4101 if (!pr_reg
->pr_reg_all_tg_pt
) {
4102 struct se_port
*port
= pr_reg
->pr_reg_tg_pt_lun
->lun_sep
;
4104 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
4105 buf
[off
++] = (port
->sep_rtpi
& 0xff);
4107 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4110 * Now, have the $FABRIC_MOD fill in the protocol identifier
4112 desc_len
= se_tpg
->se_tpg_tfo
->tpg_get_pr_transport_id(se_tpg
,
4113 se_nacl
, pr_reg
, &format_code
, &buf
[off
+4]);
4115 spin_lock(&pr_tmpl
->registration_lock
);
4116 atomic_dec(&pr_reg
->pr_res_holders
);
4117 smp_mb__after_atomic_dec();
4119 * Set the ADDITIONAL DESCRIPTOR LENGTH
4121 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4122 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4123 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4124 buf
[off
++] = (desc_len
& 0xff);
4126 * Size of full desctipor header minus TransportID
4127 * containing $FABRIC_MOD specific) initiator device/port
4130 * See spc4r17 Section 6.13.5 Table 169
4132 add_desc_len
= (24 + desc_len
);
4135 add_len
+= add_desc_len
;
4137 spin_unlock(&pr_tmpl
->registration_lock
);
4139 * Set ADDITIONAL_LENGTH
4141 buf
[4] = ((add_len
>> 24) & 0xff);
4142 buf
[5] = ((add_len
>> 16) & 0xff);
4143 buf
[6] = ((add_len
>> 8) & 0xff);
4144 buf
[7] = (add_len
& 0xff);
4146 transport_kunmap_data_sg(cmd
);
4152 target_scsi3_emulate_pr_in(struct se_cmd
*cmd
)
4157 * Following spc2r20 5.5.1 Reservations overview:
4159 * If a logical unit has been reserved by any RESERVE command and is
4160 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4161 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4162 * initiator or service action and shall terminate with a RESERVATION
4165 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
4166 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4167 " SPC-2 reservation is held, returning"
4168 " RESERVATION_CONFLICT\n");
4169 return TCM_RESERVATION_CONFLICT
;
4172 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4174 ret
= core_scsi3_pri_read_keys(cmd
);
4176 case PRI_READ_RESERVATION
:
4177 ret
= core_scsi3_pri_read_reservation(cmd
);
4179 case PRI_REPORT_CAPABILITIES
:
4180 ret
= core_scsi3_pri_report_capabilities(cmd
);
4182 case PRI_READ_FULL_STATUS
:
4183 ret
= core_scsi3_pri_read_full_status(cmd
);
4186 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4187 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4188 return TCM_INVALID_CDB_FIELD
;
4192 target_complete_cmd(cmd
, GOOD
);
4197 target_check_reservation(struct se_cmd
*cmd
)
4199 struct se_device
*dev
= cmd
->se_dev
;
4204 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
4206 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
4209 spin_lock(&dev
->dev_reservation_lock
);
4210 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
4211 ret
= target_scsi2_reservation_check(cmd
);
4213 ret
= target_scsi3_pr_reservation_check(cmd
);
4214 spin_unlock(&dev
->dev_reservation_lock
);