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-2013 Datera, Inc.
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>
39 #include "target_core_internal.h"
40 #include "target_core_pr.h"
41 #include "target_core_ua.h"
44 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
46 struct pr_transport_id_holder
{
47 struct t10_pr_registration
*dest_pr_reg
;
48 struct se_portal_group
*dest_tpg
;
49 struct se_node_acl
*dest_node_acl
;
50 struct se_dev_entry
*dest_se_deve
;
51 struct list_head dest_list
;
54 void core_pr_dump_initiator_port(
55 struct t10_pr_registration
*pr_reg
,
59 if (!pr_reg
->isid_present_at_reg
)
62 snprintf(buf
, size
, ",i,0x%s", pr_reg
->pr_reg_isid
);
67 REGISTER_AND_IGNORE_EXISTING_KEY
,
76 static void __core_scsi3_complete_pro_release(struct se_device
*, struct se_node_acl
*,
77 struct t10_pr_registration
*, int, int);
79 static int is_reservation_holder(
80 struct t10_pr_registration
*pr_res_holder
,
81 struct t10_pr_registration
*pr_reg
)
86 pr_res_type
= pr_res_holder
->pr_res_type
;
88 return pr_res_holder
== pr_reg
||
89 pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
||
90 pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
;
96 target_scsi2_reservation_check(struct se_cmd
*cmd
)
98 struct se_device
*dev
= cmd
->se_dev
;
99 struct se_session
*sess
= cmd
->se_sess
;
101 switch (cmd
->t_task_cdb
[0]) {
110 if (!dev
->dev_reserved_node_acl
|| !sess
)
113 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
114 return TCM_RESERVATION_CONFLICT
;
116 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
117 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
118 return TCM_RESERVATION_CONFLICT
;
124 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(struct se_device
*,
125 struct se_node_acl
*, struct se_session
*);
126 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*);
128 static int target_check_scsi2_reservation_conflict(struct se_cmd
*cmd
)
130 struct se_session
*se_sess
= cmd
->se_sess
;
131 struct se_device
*dev
= cmd
->se_dev
;
132 struct t10_pr_registration
*pr_reg
;
133 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
136 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
140 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
143 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
144 * status, but no reservation shall be established and the
145 * persistent reservation shall not be changed, if the command
146 * is received from a) and b) below.
148 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
149 * status, but the persistent reservation shall not be released,
150 * if the command is received from a) and b)
152 * a) An I_T nexus that is a persistent reservation holder; or
153 * b) An I_T nexus that is registered if a registrants only or
154 * all registrants type persistent reservation is present.
156 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
157 * RELEASE(6) command, or RELEASE(10) command shall be processed
158 * as defined in SPC-2.
160 if (pr_reg
->pr_res_holder
) {
161 core_scsi3_put_pr_reg(pr_reg
);
164 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) ||
165 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) ||
166 (pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
167 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
168 core_scsi3_put_pr_reg(pr_reg
);
171 core_scsi3_put_pr_reg(pr_reg
);
175 * Following spc2r20 5.5.1 Reservations overview:
177 * If a logical unit has executed a PERSISTENT RESERVE OUT
178 * command with the REGISTER or the REGISTER AND IGNORE
179 * EXISTING KEY service action and is still registered by any
180 * initiator, all RESERVE commands and all RELEASE commands
181 * regardless of initiator shall conflict and shall terminate
182 * with a RESERVATION CONFLICT status.
184 spin_lock(&pr_tmpl
->registration_lock
);
185 conflict
= (list_empty(&pr_tmpl
->registration_list
)) ? 0 : 1;
186 spin_unlock(&pr_tmpl
->registration_lock
);
190 pr_err("Received legacy SPC-2 RESERVE/RELEASE"
191 " while active SPC-3 registrations exist,"
192 " returning RESERVATION_CONFLICT\n");
200 target_scsi2_reservation_release(struct se_cmd
*cmd
)
202 struct se_device
*dev
= cmd
->se_dev
;
203 struct se_session
*sess
= cmd
->se_sess
;
204 struct se_portal_group
*tpg
;
207 if (!sess
|| !sess
->se_tpg
)
209 rc
= target_check_scsi2_reservation_conflict(cmd
);
213 return TCM_RESERVATION_CONFLICT
;
215 spin_lock(&dev
->dev_reservation_lock
);
216 if (!dev
->dev_reserved_node_acl
|| !sess
)
219 if (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)
222 if (dev
->dev_res_bin_isid
!= sess
->sess_bin_isid
)
225 dev
->dev_reserved_node_acl
= NULL
;
226 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS
;
227 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS_WITH_ISID
) {
228 dev
->dev_res_bin_isid
= 0;
229 dev
->dev_reservation_flags
&= ~DRF_SPC2_RESERVATIONS_WITH_ISID
;
232 pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
233 " MAPPED LUN: %u for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
234 cmd
->se_lun
->unpacked_lun
, cmd
->orig_fe_lun
,
235 sess
->se_node_acl
->initiatorname
);
238 spin_unlock(&dev
->dev_reservation_lock
);
240 target_complete_cmd(cmd
, GOOD
);
245 target_scsi2_reservation_reserve(struct se_cmd
*cmd
)
247 struct se_device
*dev
= cmd
->se_dev
;
248 struct se_session
*sess
= cmd
->se_sess
;
249 struct se_portal_group
*tpg
;
250 sense_reason_t ret
= 0;
253 if ((cmd
->t_task_cdb
[1] & 0x01) &&
254 (cmd
->t_task_cdb
[1] & 0x02)) {
255 pr_err("LongIO and Obselete Bits set, returning"
256 " ILLEGAL_REQUEST\n");
257 return TCM_UNSUPPORTED_SCSI_OPCODE
;
260 * This is currently the case for target_core_mod passthrough struct se_cmd
263 if (!sess
|| !sess
->se_tpg
)
265 rc
= target_check_scsi2_reservation_conflict(cmd
);
270 return TCM_RESERVATION_CONFLICT
;
273 spin_lock(&dev
->dev_reservation_lock
);
274 if (dev
->dev_reserved_node_acl
&&
275 (dev
->dev_reserved_node_acl
!= sess
->se_node_acl
)) {
276 pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
277 tpg
->se_tpg_tfo
->get_fabric_name());
278 pr_err("Original reserver LUN: %u %s\n",
279 cmd
->se_lun
->unpacked_lun
,
280 dev
->dev_reserved_node_acl
->initiatorname
);
281 pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
282 " from %s \n", cmd
->se_lun
->unpacked_lun
,
284 sess
->se_node_acl
->initiatorname
);
285 ret
= TCM_RESERVATION_CONFLICT
;
289 dev
->dev_reserved_node_acl
= sess
->se_node_acl
;
290 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS
;
291 if (sess
->sess_bin_isid
!= 0) {
292 dev
->dev_res_bin_isid
= sess
->sess_bin_isid
;
293 dev
->dev_reservation_flags
|= DRF_SPC2_RESERVATIONS_WITH_ISID
;
295 pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
296 " for %s\n", tpg
->se_tpg_tfo
->get_fabric_name(),
297 cmd
->se_lun
->unpacked_lun
, cmd
->orig_fe_lun
,
298 sess
->se_node_acl
->initiatorname
);
301 spin_unlock(&dev
->dev_reservation_lock
);
304 target_complete_cmd(cmd
, GOOD
);
310 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
312 * This function is called by those initiator ports who are *NOT*
313 * the active PR reservation holder when a reservation is present.
315 static int core_scsi3_pr_seq_non_holder(
319 unsigned char *cdb
= cmd
->t_task_cdb
;
320 struct se_dev_entry
*se_deve
;
321 struct se_session
*se_sess
= cmd
->se_sess
;
322 struct se_node_acl
*nacl
= se_sess
->se_node_acl
;
323 int other_cdb
= 0, ignore_reg
;
324 int registered_nexus
= 0, ret
= 1; /* Conflict by default */
325 int all_reg
= 0, reg_only
= 0; /* ALL_REG, REG_ONLY */
326 int we
= 0; /* Write Exclusive */
327 int legacy
= 0; /* Act like a legacy device and return
328 * RESERVATION CONFLICT on some CDBs */
329 bool registered
= false;
332 se_deve
= target_nacl_find_deve(nacl
, cmd
->orig_fe_lun
);
334 registered
= test_bit(DEF_PR_REG_ACTIVE
, &se_deve
->deve_flags
);
337 * Determine if the registration should be ignored due to
338 * non-matching ISIDs in target_scsi3_pr_reservation_check().
340 ignore_reg
= (pr_reg_type
& 0x80000000);
342 pr_reg_type
&= ~0x80000000;
344 switch (pr_reg_type
) {
345 case PR_TYPE_WRITE_EXCLUSIVE
:
347 case PR_TYPE_EXCLUSIVE_ACCESS
:
349 * Some commands are only allowed for the persistent reservation
352 if ((registered
) && !(ignore_reg
))
353 registered_nexus
= 1;
355 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
357 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
359 * Some commands are only allowed for registered I_T Nexuses.
362 if ((registered
) && !(ignore_reg
))
363 registered_nexus
= 1;
365 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
367 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
369 * Each registered I_T Nexus is a reservation holder.
372 if ((registered
) && !(ignore_reg
))
373 registered_nexus
= 1;
379 * Referenced from spc4r17 table 45 for *NON* PR holder access
382 case SECURITY_PROTOCOL_IN
:
383 if (registered_nexus
)
391 case RECEIVE_DIAGNOSTIC
:
396 if (registered_nexus
) {
400 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
402 case PERSISTENT_RESERVE_OUT
:
404 * This follows PERSISTENT_RESERVE_OUT service actions that
405 * are allowed in the presence of various reservations.
406 * See spc4r17, table 46
408 switch (cdb
[1] & 0x1f) {
411 case PRO_PREEMPT_AND_ABORT
:
412 ret
= (registered_nexus
) ? 0 : 1;
415 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
418 case PRO_REGISTER_AND_MOVE
:
423 ret
= (registered_nexus
) ? 0 : 1;
426 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
427 " action: 0x%02x\n", cdb
[1] & 0x1f);
433 /* Handled by CRH=1 in target_scsi2_reservation_release() */
438 /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
441 case TEST_UNIT_READY
:
442 ret
= (legacy
) ? 1 : 0; /* Conflict for legacy */
445 switch (cdb
[1] & 0x1f) {
446 case MI_MANAGEMENT_PROTOCOL_IN
:
447 if (registered_nexus
) {
451 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
453 case MI_REPORT_SUPPORTED_OPERATION_CODES
:
454 case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS
:
459 if (registered_nexus
) {
463 ret
= (we
) ? 0 : 1; /* Allowed Write Exclusive */
465 case MI_REPORT_ALIASES
:
466 case MI_REPORT_IDENTIFYING_INFORMATION
:
467 case MI_REPORT_PRIORITY
:
468 case MI_REPORT_TARGET_PGS
:
469 case MI_REPORT_TIMESTAMP
:
470 ret
= 0; /* Allowed */
473 pr_err("Unknown MI Service Action: 0x%02x\n",
478 case ACCESS_CONTROL_IN
:
479 case ACCESS_CONTROL_OUT
:
482 case SERVICE_ACTION_IN_12
:
485 case PERSISTENT_RESERVE_IN
:
486 ret
= 0; /*/ Allowed CDBs */
493 * Case where the CDB is explicitly allowed in the above switch
496 if (!ret
&& !other_cdb
) {
497 pr_debug("Allowing explicit CDB: 0x%02x for %s"
498 " reservation holder\n", cdb
[0],
499 core_scsi3_pr_dump_type(pr_reg_type
));
504 * Check if write exclusive initiator ports *NOT* holding the
505 * WRITE_EXCLUSIVE_* reservation.
507 if (we
&& !registered_nexus
) {
508 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
510 * Conflict for write exclusive
512 pr_debug("%s Conflict for unregistered nexus"
513 " %s CDB: 0x%02x to %s reservation\n",
514 transport_dump_cmd_direction(cmd
),
515 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
516 core_scsi3_pr_dump_type(pr_reg_type
));
520 * Allow non WRITE CDBs for all Write Exclusive
521 * PR TYPEs to pass for registered and
522 * non-registered_nexuxes NOT holding the reservation.
524 * We only make noise for the unregisterd nexuses,
525 * as we expect registered non-reservation holding
526 * nexuses to issue CDBs.
529 if (!registered_nexus
) {
530 pr_debug("Allowing implicit CDB: 0x%02x"
531 " for %s reservation on unregistered"
533 core_scsi3_pr_dump_type(pr_reg_type
));
538 } else if ((reg_only
) || (all_reg
)) {
539 if (registered_nexus
) {
541 * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
542 * allow commands from registered nexuses.
545 pr_debug("Allowing implicit CDB: 0x%02x for %s"
546 " reservation\n", cdb
[0],
547 core_scsi3_pr_dump_type(pr_reg_type
));
551 } else if (we
&& registered_nexus
) {
553 * Reads are allowed for Write Exclusive locks
554 * from all registrants.
556 if (cmd
->data_direction
== DMA_FROM_DEVICE
) {
557 pr_debug("Allowing READ CDB: 0x%02x for %s"
558 " reservation\n", cdb
[0],
559 core_scsi3_pr_dump_type(pr_reg_type
));
564 pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
565 " for %s reservation\n", transport_dump_cmd_direction(cmd
),
566 (registered_nexus
) ? "" : "un",
567 se_sess
->se_node_acl
->initiatorname
, cdb
[0],
568 core_scsi3_pr_dump_type(pr_reg_type
));
570 return 1; /* Conflict by default */
573 static sense_reason_t
574 target_scsi3_pr_reservation_check(struct se_cmd
*cmd
)
576 struct se_device
*dev
= cmd
->se_dev
;
577 struct se_session
*sess
= cmd
->se_sess
;
580 if (!dev
->dev_pr_res_holder
)
583 pr_reg_type
= dev
->dev_pr_res_holder
->pr_res_type
;
584 cmd
->pr_res_key
= dev
->dev_pr_res_holder
->pr_res_key
;
585 if (dev
->dev_pr_res_holder
->pr_reg_nacl
!= sess
->se_node_acl
)
586 goto check_nonholder
;
588 if (dev
->dev_pr_res_holder
->isid_present_at_reg
) {
589 if (dev
->dev_pr_res_holder
->pr_reg_bin_isid
!=
590 sess
->sess_bin_isid
) {
591 pr_reg_type
|= 0x80000000;
592 goto check_nonholder
;
599 if (core_scsi3_pr_seq_non_holder(cmd
, pr_reg_type
))
600 return TCM_RESERVATION_CONFLICT
;
604 static u32
core_scsi3_pr_generation(struct se_device
*dev
)
609 * PRGeneration field shall contain the value of a 32-bit wrapping
610 * counter mainted by the device server.
612 * Note that this is done regardless of Active Persist across
613 * Target PowerLoss (APTPL)
615 * See spc4r17 section 6.3.12 READ_KEYS service action
617 spin_lock(&dev
->dev_reservation_lock
);
618 prg
= dev
->t10_pr
.pr_generation
++;
619 spin_unlock(&dev
->dev_reservation_lock
);
624 static struct t10_pr_registration
*__core_scsi3_do_alloc_registration(
625 struct se_device
*dev
,
626 struct se_node_acl
*nacl
,
628 struct se_dev_entry
*deve
,
635 struct t10_pr_registration
*pr_reg
;
637 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_ATOMIC
);
639 pr_err("Unable to allocate struct t10_pr_registration\n");
643 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
644 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
645 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
646 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
647 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
648 atomic_set(&pr_reg
->pr_res_holders
, 0);
649 pr_reg
->pr_reg_nacl
= nacl
;
650 pr_reg
->pr_reg_deve
= deve
;
651 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
652 pr_reg
->pr_aptpl_target_lun
= lun
->unpacked_lun
;
653 pr_reg
->tg_pt_sep_rtpi
= lun
->lun_sep
->sep_rtpi
;
654 pr_reg
->pr_res_key
= sa_res_key
;
655 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
656 pr_reg
->pr_reg_aptpl
= aptpl
;
658 * If an ISID value for this SCSI Initiator Port exists,
659 * save it to the registration now.
662 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
663 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
664 pr_reg
->isid_present_at_reg
= 1;
670 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*);
671 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*);
674 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
677 static struct t10_pr_registration
*__core_scsi3_alloc_registration(
678 struct se_device
*dev
,
679 struct se_node_acl
*nacl
,
681 struct se_dev_entry
*deve
,
688 struct se_dev_entry
*deve_tmp
;
689 struct se_node_acl
*nacl_tmp
;
690 struct se_lun_acl
*lacl_tmp
;
691 struct se_lun
*lun_tmp
;
692 struct se_port
*port
, *port_tmp
;
693 const struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
694 struct t10_pr_registration
*pr_reg
, *pr_reg_atp
, *pr_reg_tmp
, *pr_reg_tmp_safe
;
697 * Create a registration for the I_T Nexus upon which the
698 * PROUT REGISTER was received.
700 pr_reg
= __core_scsi3_do_alloc_registration(dev
, nacl
, lun
, deve
, mapped_lun
,
701 isid
, sa_res_key
, all_tg_pt
,
706 * Return pointer to pr_reg for ALL_TG_PT=0
711 * Create list of matching SCSI Initiator Port registrations
714 spin_lock(&dev
->se_port_lock
);
715 list_for_each_entry_safe(port
, port_tmp
, &dev
->dev_sep_list
, sep_list
) {
716 atomic_inc_mb(&port
->sep_tg_pt_ref_cnt
);
717 spin_unlock(&dev
->se_port_lock
);
719 spin_lock_bh(&port
->sep_alua_lock
);
720 list_for_each_entry(deve_tmp
, &port
->sep_alua_list
,
723 * This pointer will be NULL for demo mode MappedLUNs
724 * that have not been make explicit via a ConfigFS
725 * MappedLUN group for the SCSI Initiator Node ACL.
727 if (!deve_tmp
->se_lun_acl
)
730 lacl_tmp
= rcu_dereference_check(deve_tmp
->se_lun_acl
,
731 lockdep_is_held(&port
->sep_alua_lock
));
732 nacl_tmp
= lacl_tmp
->se_lun_nacl
;
734 * Skip the matching struct se_node_acl that is allocated
737 if (nacl
== nacl_tmp
)
740 * Only perform PR registrations for target ports on
741 * the same fabric module as the REGISTER w/ ALL_TG_PT=1
744 if (tfo
!= nacl_tmp
->se_tpg
->se_tpg_tfo
)
747 * Look for a matching Initiator Node ACL in ASCII format
749 if (strcmp(nacl
->initiatorname
, nacl_tmp
->initiatorname
))
752 kref_get(&deve_tmp
->pr_kref
);
753 spin_unlock_bh(&port
->sep_alua_lock
);
755 * Grab a configfs group dependency that is released
756 * for the exception path at label out: below, or upon
757 * completion of adding ALL_TG_PT=1 registrations in
758 * __core_scsi3_add_registration()
760 ret
= core_scsi3_lunacl_depend_item(deve_tmp
);
762 pr_err("core_scsi3_lunacl_depend"
764 atomic_dec_mb(&port
->sep_tg_pt_ref_cnt
);
765 kref_put(&deve_tmp
->pr_kref
, target_pr_kref_release
);
769 * Located a matching SCSI Initiator Port on a different
770 * port, allocate the pr_reg_atp and attach it to the
771 * pr_reg->pr_reg_atp_list that will be processed once
772 * the original *pr_reg is processed in
773 * __core_scsi3_add_registration()
775 lun_tmp
= rcu_dereference_check(deve_tmp
->se_lun
,
776 atomic_read(&deve_tmp
->pr_kref
.refcount
) != 0);
778 pr_reg_atp
= __core_scsi3_do_alloc_registration(dev
,
779 nacl_tmp
, lun_tmp
, deve_tmp
,
780 deve_tmp
->mapped_lun
, NULL
,
781 sa_res_key
, all_tg_pt
, aptpl
);
783 atomic_dec_mb(&port
->sep_tg_pt_ref_cnt
);
784 core_scsi3_lunacl_undepend_item(deve_tmp
);
788 list_add_tail(&pr_reg_atp
->pr_reg_atp_mem_list
,
789 &pr_reg
->pr_reg_atp_list
);
790 spin_lock_bh(&port
->sep_alua_lock
);
792 spin_unlock_bh(&port
->sep_alua_lock
);
794 spin_lock(&dev
->se_port_lock
);
795 atomic_dec_mb(&port
->sep_tg_pt_ref_cnt
);
797 spin_unlock(&dev
->se_port_lock
);
801 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
802 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
803 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
804 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
805 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
807 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
811 int core_scsi3_alloc_aptpl_registration(
812 struct t10_reservation
*pr_tmpl
,
814 unsigned char *i_port
,
817 unsigned char *t_port
,
824 struct t10_pr_registration
*pr_reg
;
826 if (!i_port
|| !t_port
|| !sa_res_key
) {
827 pr_err("Illegal parameters for APTPL registration\n");
831 pr_reg
= kmem_cache_zalloc(t10_pr_reg_cache
, GFP_KERNEL
);
833 pr_err("Unable to allocate struct t10_pr_registration\n");
837 INIT_LIST_HEAD(&pr_reg
->pr_reg_list
);
838 INIT_LIST_HEAD(&pr_reg
->pr_reg_abort_list
);
839 INIT_LIST_HEAD(&pr_reg
->pr_reg_aptpl_list
);
840 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_list
);
841 INIT_LIST_HEAD(&pr_reg
->pr_reg_atp_mem_list
);
842 atomic_set(&pr_reg
->pr_res_holders
, 0);
843 pr_reg
->pr_reg_nacl
= NULL
;
844 pr_reg
->pr_reg_deve
= NULL
;
845 pr_reg
->pr_res_mapped_lun
= mapped_lun
;
846 pr_reg
->pr_aptpl_target_lun
= target_lun
;
847 pr_reg
->pr_res_key
= sa_res_key
;
848 pr_reg
->pr_reg_all_tg_pt
= all_tg_pt
;
849 pr_reg
->pr_reg_aptpl
= 1;
850 pr_reg
->pr_res_scope
= 0; /* Always LUN_SCOPE */
851 pr_reg
->pr_res_type
= type
;
853 * If an ISID value had been saved in APTPL metadata for this
854 * SCSI Initiator Port, restore it now.
857 pr_reg
->pr_reg_bin_isid
= get_unaligned_be64(isid
);
858 snprintf(pr_reg
->pr_reg_isid
, PR_REG_ISID_LEN
, "%s", isid
);
859 pr_reg
->isid_present_at_reg
= 1;
862 * Copy the i_port and t_port information from caller.
864 snprintf(pr_reg
->pr_iport
, PR_APTPL_MAX_IPORT_LEN
, "%s", i_port
);
865 snprintf(pr_reg
->pr_tport
, PR_APTPL_MAX_TPORT_LEN
, "%s", t_port
);
866 pr_reg
->pr_reg_tpgt
= tpgt
;
868 * Set pr_res_holder from caller, the pr_reg who is the reservation
869 * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
870 * the Initiator Node LUN ACL from the fabric module is created for
873 pr_reg
->pr_res_holder
= res_holder
;
875 list_add_tail(&pr_reg
->pr_reg_aptpl_list
, &pr_tmpl
->aptpl_reg_list
);
876 pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
877 " metadata\n", (res_holder
) ? "+reservation" : "");
881 static void core_scsi3_aptpl_reserve(
882 struct se_device
*dev
,
883 struct se_portal_group
*tpg
,
884 struct se_node_acl
*node_acl
,
885 struct t10_pr_registration
*pr_reg
)
887 char i_buf
[PR_REG_ISID_ID_LEN
];
889 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
890 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
892 spin_lock(&dev
->dev_reservation_lock
);
893 dev
->dev_pr_res_holder
= pr_reg
;
894 spin_unlock(&dev
->dev_reservation_lock
);
896 pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
897 " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
898 tpg
->se_tpg_tfo
->get_fabric_name(),
899 core_scsi3_pr_dump_type(pr_reg
->pr_res_type
),
900 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
901 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
902 tpg
->se_tpg_tfo
->get_fabric_name(), node_acl
->initiatorname
,
906 static void __core_scsi3_add_registration(struct se_device
*, struct se_node_acl
*,
907 struct t10_pr_registration
*, enum register_type
, int);
909 static int __core_scsi3_check_aptpl_registration(
910 struct se_device
*dev
,
911 struct se_portal_group
*tpg
,
914 struct se_node_acl
*nacl
,
917 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
918 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
919 unsigned char i_port
[PR_APTPL_MAX_IPORT_LEN
];
920 unsigned char t_port
[PR_APTPL_MAX_TPORT_LEN
];
923 memset(i_port
, 0, PR_APTPL_MAX_IPORT_LEN
);
924 memset(t_port
, 0, PR_APTPL_MAX_TPORT_LEN
);
926 * Copy Initiator Port information from struct se_node_acl
928 snprintf(i_port
, PR_APTPL_MAX_IPORT_LEN
, "%s", nacl
->initiatorname
);
929 snprintf(t_port
, PR_APTPL_MAX_TPORT_LEN
, "%s",
930 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
931 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
933 * Look for the matching registrations+reservation from those
934 * created from APTPL metadata. Note that multiple registrations
935 * may exist for fabrics that use ISIDs in their SCSI Initiator Port
938 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
939 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
942 if (!strcmp(pr_reg
->pr_iport
, i_port
) &&
943 (pr_reg
->pr_res_mapped_lun
== mapped_lun
) &&
944 !(strcmp(pr_reg
->pr_tport
, t_port
)) &&
945 (pr_reg
->pr_reg_tpgt
== tpgt
) &&
946 (pr_reg
->pr_aptpl_target_lun
== target_lun
)) {
948 pr_reg
->pr_reg_nacl
= nacl
;
949 pr_reg
->tg_pt_sep_rtpi
= lun
->lun_sep
->sep_rtpi
;
951 list_del(&pr_reg
->pr_reg_aptpl_list
);
952 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
954 * At this point all of the pointers in *pr_reg will
955 * be setup, so go ahead and add the registration.
958 __core_scsi3_add_registration(dev
, nacl
, pr_reg
, 0, 0);
960 * If this registration is the reservation holder,
961 * make that happen now..
963 if (pr_reg
->pr_res_holder
)
964 core_scsi3_aptpl_reserve(dev
, tpg
,
967 * Reenable pr_aptpl_active to accept new metadata
968 * updates once the SCSI device is active again..
970 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
971 pr_tmpl
->pr_aptpl_active
= 1;
974 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
979 int core_scsi3_check_aptpl_registration(
980 struct se_device
*dev
,
981 struct se_portal_group
*tpg
,
983 struct se_node_acl
*nacl
,
986 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
989 return __core_scsi3_check_aptpl_registration(dev
, tpg
, lun
,
990 lun
->unpacked_lun
, nacl
,
994 static void __core_scsi3_dump_registration(
995 const struct target_core_fabric_ops
*tfo
,
996 struct se_device
*dev
,
997 struct se_node_acl
*nacl
,
998 struct t10_pr_registration
*pr_reg
,
999 enum register_type register_type
)
1001 struct se_portal_group
*se_tpg
= nacl
->se_tpg
;
1002 char i_buf
[PR_REG_ISID_ID_LEN
];
1004 memset(&i_buf
[0], 0, PR_REG_ISID_ID_LEN
);
1005 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1007 pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
1008 " Node: %s%s\n", tfo
->get_fabric_name(), (register_type
== REGISTER_AND_MOVE
) ?
1009 "_AND_MOVE" : (register_type
== REGISTER_AND_IGNORE_EXISTING_KEY
) ?
1010 "_AND_IGNORE_EXISTING_KEY" : "", nacl
->initiatorname
,
1012 pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
1013 tfo
->get_fabric_name(), tfo
->tpg_get_wwn(se_tpg
),
1014 tfo
->tpg_get_tag(se_tpg
));
1015 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1016 " Port(s)\n", tfo
->get_fabric_name(),
1017 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1018 dev
->transport
->name
);
1019 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1020 " 0x%08x APTPL: %d\n", tfo
->get_fabric_name(),
1021 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
,
1022 pr_reg
->pr_reg_aptpl
);
1025 static void __core_scsi3_add_registration(
1026 struct se_device
*dev
,
1027 struct se_node_acl
*nacl
,
1028 struct t10_pr_registration
*pr_reg
,
1029 enum register_type register_type
,
1032 const struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
1033 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1034 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1035 struct se_dev_entry
*deve
;
1038 * Increment PRgeneration counter for struct se_device upon a successful
1039 * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1041 * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1042 * action, the struct se_device->dev_reservation_lock will already be held,
1043 * so we do not call core_scsi3_pr_generation() which grabs the lock
1046 pr_reg
->pr_res_generation
= (register_move
) ?
1047 dev
->t10_pr
.pr_generation
++ :
1048 core_scsi3_pr_generation(dev
);
1050 spin_lock(&pr_tmpl
->registration_lock
);
1051 list_add_tail(&pr_reg
->pr_reg_list
, &pr_tmpl
->registration_list
);
1053 __core_scsi3_dump_registration(tfo
, dev
, nacl
, pr_reg
, register_type
);
1054 spin_unlock(&pr_tmpl
->registration_lock
);
1057 deve
= pr_reg
->pr_reg_deve
;
1059 set_bit(DEF_PR_REG_ACTIVE
, &deve
->deve_flags
);
1063 * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1065 if (!pr_reg
->pr_reg_all_tg_pt
|| register_move
)
1068 * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1069 * allocated in __core_scsi3_alloc_registration()
1071 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1072 &pr_reg
->pr_reg_atp_list
, pr_reg_atp_mem_list
) {
1073 struct se_node_acl
*nacl_tmp
= pr_reg_tmp
->pr_reg_nacl
;
1075 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1077 pr_reg_tmp
->pr_res_generation
= core_scsi3_pr_generation(dev
);
1079 spin_lock(&pr_tmpl
->registration_lock
);
1080 list_add_tail(&pr_reg_tmp
->pr_reg_list
,
1081 &pr_tmpl
->registration_list
);
1083 __core_scsi3_dump_registration(tfo
, dev
, nacl_tmp
, pr_reg_tmp
,
1085 spin_unlock(&pr_tmpl
->registration_lock
);
1088 deve
= pr_reg_tmp
->pr_reg_deve
;
1090 set_bit(DEF_PR_REG_ACTIVE
, &deve
->deve_flags
);
1094 * Drop configfs group dependency reference from
1095 * __core_scsi3_alloc_registration()
1097 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1101 static int core_scsi3_alloc_registration(
1102 struct se_device
*dev
,
1103 struct se_node_acl
*nacl
,
1105 struct se_dev_entry
*deve
,
1107 unsigned char *isid
,
1111 enum register_type register_type
,
1114 struct t10_pr_registration
*pr_reg
;
1116 pr_reg
= __core_scsi3_alloc_registration(dev
, nacl
, lun
, deve
, mapped_lun
,
1117 isid
, sa_res_key
, all_tg_pt
,
1122 __core_scsi3_add_registration(dev
, nacl
, pr_reg
,
1123 register_type
, register_move
);
1127 static struct t10_pr_registration
*__core_scsi3_locate_pr_reg(
1128 struct se_device
*dev
,
1129 struct se_node_acl
*nacl
,
1130 unsigned char *isid
)
1132 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1133 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
1134 struct se_portal_group
*tpg
;
1136 spin_lock(&pr_tmpl
->registration_lock
);
1137 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1138 &pr_tmpl
->registration_list
, pr_reg_list
) {
1140 * First look for a matching struct se_node_acl
1142 if (pr_reg
->pr_reg_nacl
!= nacl
)
1145 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1147 * If this registration does NOT contain a fabric provided
1148 * ISID, then we have found a match.
1150 if (!pr_reg
->isid_present_at_reg
) {
1152 * Determine if this SCSI device server requires that
1153 * SCSI Intiatior TransportID w/ ISIDs is enforced
1154 * for fabric modules (iSCSI) requiring them.
1156 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1157 if (dev
->dev_attrib
.enforce_pr_isids
)
1160 atomic_inc_mb(&pr_reg
->pr_res_holders
);
1161 spin_unlock(&pr_tmpl
->registration_lock
);
1165 * If the *pr_reg contains a fabric defined ISID for multi-value
1166 * SCSI Initiator Port TransportIDs, then we expect a valid
1167 * matching ISID to be provided by the local SCSI Initiator Port.
1171 if (strcmp(isid
, pr_reg
->pr_reg_isid
))
1174 atomic_inc_mb(&pr_reg
->pr_res_holders
);
1175 spin_unlock(&pr_tmpl
->registration_lock
);
1178 spin_unlock(&pr_tmpl
->registration_lock
);
1183 static struct t10_pr_registration
*core_scsi3_locate_pr_reg(
1184 struct se_device
*dev
,
1185 struct se_node_acl
*nacl
,
1186 struct se_session
*sess
)
1188 struct se_portal_group
*tpg
= nacl
->se_tpg
;
1189 unsigned char buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
1191 if (tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
1192 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
1193 tpg
->se_tpg_tfo
->sess_get_initiator_sid(sess
, &buf
[0],
1198 return __core_scsi3_locate_pr_reg(dev
, nacl
, isid_ptr
);
1201 static void core_scsi3_put_pr_reg(struct t10_pr_registration
*pr_reg
)
1203 atomic_dec_mb(&pr_reg
->pr_res_holders
);
1206 static int core_scsi3_check_implicit_release(
1207 struct se_device
*dev
,
1208 struct t10_pr_registration
*pr_reg
)
1210 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1211 struct t10_pr_registration
*pr_res_holder
;
1214 spin_lock(&dev
->dev_reservation_lock
);
1215 pr_res_holder
= dev
->dev_pr_res_holder
;
1216 if (!pr_res_holder
) {
1217 spin_unlock(&dev
->dev_reservation_lock
);
1220 if (pr_res_holder
== pr_reg
) {
1222 * Perform an implicit RELEASE if the registration that
1223 * is being released is holding the reservation.
1225 * From spc4r17, section 5.7.11.1:
1227 * e) If the I_T nexus is the persistent reservation holder
1228 * and the persistent reservation is not an all registrants
1229 * type, then a PERSISTENT RESERVE OUT command with REGISTER
1230 * service action or REGISTER AND IGNORE EXISTING KEY
1231 * service action with the SERVICE ACTION RESERVATION KEY
1232 * field set to zero (see 5.7.11.3).
1234 __core_scsi3_complete_pro_release(dev
, nacl
, pr_reg
, 0, 1);
1237 * For 'All Registrants' reservation types, all existing
1238 * registrations are still processed as reservation holders
1239 * in core_scsi3_pr_seq_non_holder() after the initial
1240 * reservation holder is implicitly released here.
1242 } else if (pr_reg
->pr_reg_all_tg_pt
&&
1243 (!strcmp(pr_res_holder
->pr_reg_nacl
->initiatorname
,
1244 pr_reg
->pr_reg_nacl
->initiatorname
)) &&
1245 (pr_res_holder
->pr_res_key
== pr_reg
->pr_res_key
)) {
1246 pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1247 " UNREGISTER while existing reservation with matching"
1248 " key 0x%016Lx is present from another SCSI Initiator"
1249 " Port\n", pr_reg
->pr_res_key
);
1252 spin_unlock(&dev
->dev_reservation_lock
);
1258 * Called with struct t10_reservation->registration_lock held.
1260 static void __core_scsi3_free_registration(
1261 struct se_device
*dev
,
1262 struct t10_pr_registration
*pr_reg
,
1263 struct list_head
*preempt_and_abort_list
,
1265 __releases(&pr_tmpl
->registration_lock
)
1266 __acquires(&pr_tmpl
->registration_lock
)
1268 const struct target_core_fabric_ops
*tfo
=
1269 pr_reg
->pr_reg_nacl
->se_tpg
->se_tpg_tfo
;
1270 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1271 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
1272 struct se_dev_entry
*deve
;
1273 char i_buf
[PR_REG_ISID_ID_LEN
];
1275 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1276 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1278 if (!list_empty(&pr_reg
->pr_reg_list
))
1279 list_del(&pr_reg
->pr_reg_list
);
1281 * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1282 * so call core_scsi3_put_pr_reg() to decrement our reference.
1285 core_scsi3_put_pr_reg(pr_reg
);
1287 spin_unlock(&pr_tmpl
->registration_lock
);
1289 * Wait until all reference from any other I_T nexuses for this
1290 * *pr_reg have been released. Because list_del() is called above,
1291 * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1292 * count back to zero, and we release *pr_reg.
1294 while (atomic_read(&pr_reg
->pr_res_holders
) != 0) {
1295 pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1296 tfo
->get_fabric_name());
1301 deve
= target_nacl_find_deve(nacl
, pr_reg
->pr_res_mapped_lun
);
1303 clear_bit(DEF_PR_REG_ACTIVE
, &deve
->deve_flags
);
1306 spin_lock(&pr_tmpl
->registration_lock
);
1307 pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1308 " Node: %s%s\n", tfo
->get_fabric_name(),
1309 pr_reg
->pr_reg_nacl
->initiatorname
,
1311 pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1312 " Port(s)\n", tfo
->get_fabric_name(),
1313 (pr_reg
->pr_reg_all_tg_pt
) ? "ALL" : "SINGLE",
1314 dev
->transport
->name
);
1315 pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1316 " 0x%08x\n", tfo
->get_fabric_name(), pr_reg
->pr_res_key
,
1317 pr_reg
->pr_res_generation
);
1319 if (!preempt_and_abort_list
) {
1320 pr_reg
->pr_reg_deve
= NULL
;
1321 pr_reg
->pr_reg_nacl
= NULL
;
1322 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1326 * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1327 * are released once the ABORT_TASK_SET has completed..
1329 list_add_tail(&pr_reg
->pr_reg_abort_list
, preempt_and_abort_list
);
1332 void core_scsi3_free_pr_reg_from_nacl(
1333 struct se_device
*dev
,
1334 struct se_node_acl
*nacl
)
1336 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1337 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1338 bool free_reg
= false;
1340 * If the passed se_node_acl matches the reservation holder,
1341 * release the reservation.
1343 spin_lock(&dev
->dev_reservation_lock
);
1344 pr_res_holder
= dev
->dev_pr_res_holder
;
1345 if ((pr_res_holder
!= NULL
) &&
1346 (pr_res_holder
->pr_reg_nacl
== nacl
)) {
1347 __core_scsi3_complete_pro_release(dev
, nacl
, pr_res_holder
, 0, 1);
1350 spin_unlock(&dev
->dev_reservation_lock
);
1352 * Release any registration associated with the struct se_node_acl.
1354 spin_lock(&pr_tmpl
->registration_lock
);
1355 if (pr_res_holder
&& free_reg
)
1356 __core_scsi3_free_registration(dev
, pr_res_holder
, NULL
, 0);
1358 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1359 &pr_tmpl
->registration_list
, pr_reg_list
) {
1361 if (pr_reg
->pr_reg_nacl
!= nacl
)
1364 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1366 spin_unlock(&pr_tmpl
->registration_lock
);
1369 void core_scsi3_free_all_registrations(
1370 struct se_device
*dev
)
1372 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
1373 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_res_holder
;
1375 spin_lock(&dev
->dev_reservation_lock
);
1376 pr_res_holder
= dev
->dev_pr_res_holder
;
1377 if (pr_res_holder
!= NULL
) {
1378 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
1379 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
1380 pr_res_holder
, 0, 0);
1382 spin_unlock(&dev
->dev_reservation_lock
);
1384 spin_lock(&pr_tmpl
->registration_lock
);
1385 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
1386 &pr_tmpl
->registration_list
, pr_reg_list
) {
1388 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 0);
1390 spin_unlock(&pr_tmpl
->registration_lock
);
1392 spin_lock(&pr_tmpl
->aptpl_reg_lock
);
1393 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, &pr_tmpl
->aptpl_reg_list
,
1394 pr_reg_aptpl_list
) {
1395 list_del(&pr_reg
->pr_reg_aptpl_list
);
1396 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
1398 spin_unlock(&pr_tmpl
->aptpl_reg_lock
);
1401 static int core_scsi3_tpg_depend_item(struct se_portal_group
*tpg
)
1403 return target_depend_item(&tpg
->tpg_group
.cg_item
);
1406 static void core_scsi3_tpg_undepend_item(struct se_portal_group
*tpg
)
1408 target_undepend_item(&tpg
->tpg_group
.cg_item
);
1409 atomic_dec_mb(&tpg
->tpg_pr_ref_count
);
1412 static int core_scsi3_nodeacl_depend_item(struct se_node_acl
*nacl
)
1414 if (nacl
->dynamic_node_acl
)
1416 return target_depend_item(&nacl
->acl_group
.cg_item
);
1419 static void core_scsi3_nodeacl_undepend_item(struct se_node_acl
*nacl
)
1421 if (!nacl
->dynamic_node_acl
)
1422 target_undepend_item(&nacl
->acl_group
.cg_item
);
1423 atomic_dec_mb(&nacl
->acl_pr_ref_count
);
1426 static int core_scsi3_lunacl_depend_item(struct se_dev_entry
*se_deve
)
1428 struct se_lun_acl
*lun_acl
;
1429 struct se_node_acl
*nacl
;
1430 struct se_portal_group
*tpg
;
1432 * For nacl->dynamic_node_acl=1
1434 lun_acl
= rcu_dereference_check(se_deve
->se_lun_acl
,
1435 atomic_read(&se_deve
->pr_kref
.refcount
) != 0);
1439 nacl
= lun_acl
->se_lun_nacl
;
1442 return target_depend_item(&lun_acl
->se_lun_group
.cg_item
);
1445 static void core_scsi3_lunacl_undepend_item(struct se_dev_entry
*se_deve
)
1447 struct se_lun_acl
*lun_acl
;
1448 struct se_node_acl
*nacl
;
1449 struct se_portal_group
*tpg
;
1451 * For nacl->dynamic_node_acl=1
1453 lun_acl
= rcu_dereference_check(se_deve
->se_lun_acl
,
1454 atomic_read(&se_deve
->pr_kref
.refcount
) != 0);
1456 kref_put(&se_deve
->pr_kref
, target_pr_kref_release
);
1459 nacl
= lun_acl
->se_lun_nacl
;
1462 target_undepend_item(&lun_acl
->se_lun_group
.cg_item
);
1463 kref_put(&se_deve
->pr_kref
, target_pr_kref_release
);
1466 static sense_reason_t
1467 core_scsi3_decode_spec_i_port(
1469 struct se_portal_group
*tpg
,
1470 unsigned char *l_isid
,
1475 struct se_device
*dev
= cmd
->se_dev
;
1476 struct se_port
*tmp_port
;
1477 struct se_portal_group
*dest_tpg
= NULL
, *tmp_tpg
;
1478 struct se_session
*se_sess
= cmd
->se_sess
;
1479 struct se_node_acl
*dest_node_acl
= NULL
;
1480 struct se_dev_entry
*dest_se_deve
= NULL
;
1481 struct t10_pr_registration
*dest_pr_reg
, *local_pr_reg
, *pr_reg_e
;
1482 struct t10_pr_registration
*pr_reg_tmp
, *pr_reg_tmp_safe
;
1483 LIST_HEAD(tid_dest_list
);
1484 struct pr_transport_id_holder
*tidh_new
, *tidh
, *tidh_tmp
;
1485 unsigned char *buf
, *ptr
, proto_ident
;
1486 const unsigned char *i_str
;
1487 char *iport_ptr
= NULL
, i_buf
[PR_REG_ISID_ID_LEN
];
1489 u32 tpdl
, tid_len
= 0;
1493 * Allocate a struct pr_transport_id_holder and setup the
1494 * local_node_acl pointer and add to struct list_head tid_dest_list
1495 * for add registration processing in the loop of tid_dest_list below.
1497 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
), GFP_KERNEL
);
1499 pr_err("Unable to allocate tidh_new\n");
1500 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1502 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1503 tidh_new
->dest_tpg
= tpg
;
1504 tidh_new
->dest_node_acl
= se_sess
->se_node_acl
;
1506 local_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1507 se_sess
->se_node_acl
, cmd
->se_lun
,
1508 NULL
, cmd
->orig_fe_lun
, l_isid
,
1509 sa_res_key
, all_tg_pt
, aptpl
);
1510 if (!local_pr_reg
) {
1512 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1514 tidh_new
->dest_pr_reg
= local_pr_reg
;
1516 * The local I_T nexus does not hold any configfs dependances,
1517 * so we set tidh_new->dest_se_deve to NULL to prevent the
1518 * configfs_undepend_item() calls in the tid_dest_list loops below.
1520 tidh_new
->dest_se_deve
= NULL
;
1521 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1523 if (cmd
->data_length
< 28) {
1524 pr_warn("SPC-PR: Received PR OUT parameter list"
1525 " length too small: %u\n", cmd
->data_length
);
1526 ret
= TCM_INVALID_PARAMETER_LIST
;
1530 buf
= transport_kmap_data_sg(cmd
);
1532 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1537 * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1538 * first extract TransportID Parameter Data Length, and make sure
1539 * the value matches up to the SCSI expected data transfer length.
1541 tpdl
= (buf
[24] & 0xff) << 24;
1542 tpdl
|= (buf
[25] & 0xff) << 16;
1543 tpdl
|= (buf
[26] & 0xff) << 8;
1544 tpdl
|= buf
[27] & 0xff;
1546 if ((tpdl
+ 28) != cmd
->data_length
) {
1547 pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1548 " does not equal CDB data_length: %u\n", tpdl
,
1550 ret
= TCM_INVALID_PARAMETER_LIST
;
1554 * Start processing the received transport IDs using the
1555 * receiving I_T Nexus portal's fabric dependent methods to
1556 * obtain the SCSI Initiator Port/Device Identifiers.
1561 struct se_lun
*dest_lun
;
1563 proto_ident
= (ptr
[0] & 0x0f);
1566 spin_lock(&dev
->se_port_lock
);
1567 list_for_each_entry(tmp_port
, &dev
->dev_sep_list
, sep_list
) {
1568 tmp_tpg
= tmp_port
->sep_tpg
;
1573 * Look for the matching proto_ident provided by
1574 * the received TransportID
1576 if (tmp_tpg
->proto_id
!= proto_ident
)
1578 dest_rtpi
= tmp_port
->sep_rtpi
;
1580 i_str
= target_parse_pr_out_transport_id(tmp_tpg
,
1581 (const char *)ptr
, &tid_len
, &iport_ptr
);
1585 atomic_inc_mb(&tmp_tpg
->tpg_pr_ref_count
);
1586 spin_unlock(&dev
->se_port_lock
);
1588 if (core_scsi3_tpg_depend_item(tmp_tpg
)) {
1589 pr_err(" core_scsi3_tpg_depend_item()"
1591 atomic_dec_mb(&tmp_tpg
->tpg_pr_ref_count
);
1592 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1596 * Locate the destination initiator ACL to be registered
1597 * from the decoded fabric module specific TransportID
1600 spin_lock_irq(&tmp_tpg
->acl_node_lock
);
1601 dest_node_acl
= __core_tpg_get_initiator_node_acl(
1604 atomic_inc_mb(&dest_node_acl
->acl_pr_ref_count
);
1605 spin_unlock_irq(&tmp_tpg
->acl_node_lock
);
1607 if (!dest_node_acl
) {
1608 core_scsi3_tpg_undepend_item(tmp_tpg
);
1609 spin_lock(&dev
->se_port_lock
);
1613 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
1614 pr_err("configfs_depend_item() failed"
1615 " for dest_node_acl->acl_group\n");
1616 atomic_dec_mb(&dest_node_acl
->acl_pr_ref_count
);
1617 core_scsi3_tpg_undepend_item(tmp_tpg
);
1618 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1623 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1624 " %s Port RTPI: %hu\n",
1625 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1626 dest_node_acl
->initiatorname
, dest_rtpi
);
1628 spin_lock(&dev
->se_port_lock
);
1631 spin_unlock(&dev
->se_port_lock
);
1634 pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1636 ret
= TCM_INVALID_PARAMETER_LIST
;
1640 pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1641 " tid_len: %d for %s + %s\n",
1642 dest_tpg
->se_tpg_tfo
->get_fabric_name(), cmd
->data_length
,
1643 tpdl
, tid_len
, i_str
, iport_ptr
);
1645 if (tid_len
> tpdl
) {
1646 pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1647 " %u for Transport ID: %s\n", tid_len
, ptr
);
1648 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1649 core_scsi3_tpg_undepend_item(dest_tpg
);
1650 ret
= TCM_INVALID_PARAMETER_LIST
;
1654 * Locate the desintation struct se_dev_entry pointer for matching
1655 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1658 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
,
1660 if (!dest_se_deve
) {
1661 pr_err("Unable to locate %s dest_se_deve"
1662 " from destination RTPI: %hu\n",
1663 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1666 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1667 core_scsi3_tpg_undepend_item(dest_tpg
);
1668 ret
= TCM_INVALID_PARAMETER_LIST
;
1672 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
1673 pr_err("core_scsi3_lunacl_depend_item()"
1675 kref_put(&dest_se_deve
->pr_kref
, target_pr_kref_release
);
1676 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1677 core_scsi3_tpg_undepend_item(dest_tpg
);
1678 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1682 pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1683 " dest_se_deve mapped_lun: %u\n",
1684 dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1685 dest_node_acl
->initiatorname
, dest_se_deve
->mapped_lun
);
1688 * Skip any TransportIDs that already have a registration for
1691 pr_reg_e
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
1694 core_scsi3_put_pr_reg(pr_reg_e
);
1695 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1696 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1697 core_scsi3_tpg_undepend_item(dest_tpg
);
1704 * Allocate a struct pr_transport_id_holder and setup
1705 * the dest_node_acl and dest_se_deve pointers for the
1708 tidh_new
= kzalloc(sizeof(struct pr_transport_id_holder
),
1711 pr_err("Unable to allocate tidh_new\n");
1712 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1713 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1714 core_scsi3_tpg_undepend_item(dest_tpg
);
1715 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1718 INIT_LIST_HEAD(&tidh_new
->dest_list
);
1719 tidh_new
->dest_tpg
= dest_tpg
;
1720 tidh_new
->dest_node_acl
= dest_node_acl
;
1721 tidh_new
->dest_se_deve
= dest_se_deve
;
1724 * Allocate, but do NOT add the registration for the
1725 * TransportID referenced SCSI Initiator port. This
1726 * done because of the following from spc4r17 in section
1727 * 6.14.3 wrt SPEC_I_PT:
1729 * "If a registration fails for any initiator port (e.g., if th
1730 * logical unit does not have enough resources available to
1731 * hold the registration information), no registrations shall be
1732 * made, and the command shall be terminated with
1733 * CHECK CONDITION status."
1735 * That means we call __core_scsi3_alloc_registration() here,
1736 * and then call __core_scsi3_add_registration() in the
1737 * 2nd loop which will never fail.
1739 dest_lun
= rcu_dereference_check(dest_se_deve
->se_lun
,
1740 atomic_read(&dest_se_deve
->pr_kref
.refcount
) != 0);
1742 dest_pr_reg
= __core_scsi3_alloc_registration(cmd
->se_dev
,
1743 dest_node_acl
, dest_lun
, dest_se_deve
,
1744 dest_se_deve
->mapped_lun
, iport_ptr
,
1745 sa_res_key
, all_tg_pt
, aptpl
);
1747 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1748 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1749 core_scsi3_tpg_undepend_item(dest_tpg
);
1751 ret
= TCM_INVALID_PARAMETER_LIST
;
1754 tidh_new
->dest_pr_reg
= dest_pr_reg
;
1755 list_add_tail(&tidh_new
->dest_list
, &tid_dest_list
);
1763 transport_kunmap_data_sg(cmd
);
1766 * Go ahead and create a registrations from tid_dest_list for the
1767 * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1770 * The SA Reservation Key from the PROUT is set for the
1771 * registration, and ALL_TG_PT is also passed. ALL_TG_PT=1
1772 * means that the TransportID Initiator port will be
1773 * registered on all of the target ports in the SCSI target device
1774 * ALL_TG_PT=0 means the registration will only be for the
1775 * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1778 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1779 dest_tpg
= tidh
->dest_tpg
;
1780 dest_node_acl
= tidh
->dest_node_acl
;
1781 dest_se_deve
= tidh
->dest_se_deve
;
1782 dest_pr_reg
= tidh
->dest_pr_reg
;
1784 list_del(&tidh
->dest_list
);
1787 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
1788 core_pr_dump_initiator_port(dest_pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
1790 __core_scsi3_add_registration(cmd
->se_dev
, dest_node_acl
,
1793 pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1794 " registered Transport ID for Node: %s%s Mapped LUN:"
1795 " %u\n", dest_tpg
->se_tpg_tfo
->get_fabric_name(),
1796 dest_node_acl
->initiatorname
, i_buf
, (dest_se_deve
) ?
1797 dest_se_deve
->mapped_lun
: 0);
1802 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1803 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1804 core_scsi3_tpg_undepend_item(dest_tpg
);
1809 transport_kunmap_data_sg(cmd
);
1812 * For the failure case, release everything from tid_dest_list
1813 * including *dest_pr_reg and the configfs dependances..
1815 list_for_each_entry_safe(tidh
, tidh_tmp
, &tid_dest_list
, dest_list
) {
1816 dest_tpg
= tidh
->dest_tpg
;
1817 dest_node_acl
= tidh
->dest_node_acl
;
1818 dest_se_deve
= tidh
->dest_se_deve
;
1819 dest_pr_reg
= tidh
->dest_pr_reg
;
1821 list_del(&tidh
->dest_list
);
1824 * Release any extra ALL_TG_PT=1 registrations for
1825 * the SPEC_I_PT=1 case.
1827 list_for_each_entry_safe(pr_reg_tmp
, pr_reg_tmp_safe
,
1828 &dest_pr_reg
->pr_reg_atp_list
,
1829 pr_reg_atp_mem_list
) {
1830 list_del(&pr_reg_tmp
->pr_reg_atp_mem_list
);
1831 core_scsi3_lunacl_undepend_item(pr_reg_tmp
->pr_reg_deve
);
1832 kmem_cache_free(t10_pr_reg_cache
, pr_reg_tmp
);
1835 kmem_cache_free(t10_pr_reg_cache
, dest_pr_reg
);
1840 core_scsi3_lunacl_undepend_item(dest_se_deve
);
1841 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
1842 core_scsi3_tpg_undepend_item(dest_tpg
);
1847 static int core_scsi3_update_aptpl_buf(
1848 struct se_device
*dev
,
1850 u32 pr_aptpl_buf_len
)
1852 struct se_portal_group
*tpg
;
1853 struct t10_pr_registration
*pr_reg
;
1854 unsigned char tmp
[512], isid_buf
[32];
1859 spin_lock(&dev
->dev_reservation_lock
);
1860 spin_lock(&dev
->t10_pr
.registration_lock
);
1862 * Walk the registration list..
1864 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
1869 tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
1871 * Write out any ISID value to APTPL metadata that was included
1872 * in the original registration.
1874 if (pr_reg
->isid_present_at_reg
)
1875 snprintf(isid_buf
, 32, "initiator_sid=%s\n",
1876 pr_reg
->pr_reg_isid
);
1878 * Include special metadata if the pr_reg matches the
1879 * reservation holder.
1881 if (dev
->dev_pr_res_holder
== pr_reg
) {
1882 snprintf(tmp
, 512, "PR_REG_START: %d"
1883 "\ninitiator_fabric=%s\n"
1884 "initiator_node=%s\n%s"
1886 "res_holder=1\nres_type=%02x\n"
1887 "res_scope=%02x\nres_all_tg_pt=%d\n"
1888 "mapped_lun=%u\n", reg_count
,
1889 tpg
->se_tpg_tfo
->get_fabric_name(),
1890 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1891 pr_reg
->pr_res_key
, pr_reg
->pr_res_type
,
1892 pr_reg
->pr_res_scope
, pr_reg
->pr_reg_all_tg_pt
,
1893 pr_reg
->pr_res_mapped_lun
);
1895 snprintf(tmp
, 512, "PR_REG_START: %d\n"
1896 "initiator_fabric=%s\ninitiator_node=%s\n%s"
1897 "sa_res_key=%llu\nres_holder=0\n"
1898 "res_all_tg_pt=%d\nmapped_lun=%u\n",
1899 reg_count
, tpg
->se_tpg_tfo
->get_fabric_name(),
1900 pr_reg
->pr_reg_nacl
->initiatorname
, isid_buf
,
1901 pr_reg
->pr_res_key
, pr_reg
->pr_reg_all_tg_pt
,
1902 pr_reg
->pr_res_mapped_lun
);
1905 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1906 pr_err("Unable to update renaming APTPL metadata,"
1907 " reallocating larger buffer\n");
1911 len
+= sprintf(buf
+len
, "%s", tmp
);
1914 * Include information about the associated SCSI target port.
1916 snprintf(tmp
, 512, "target_fabric=%s\ntarget_node=%s\n"
1917 "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1918 " %d\n", tpg
->se_tpg_tfo
->get_fabric_name(),
1919 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
),
1920 tpg
->se_tpg_tfo
->tpg_get_tag(tpg
),
1921 pr_reg
->tg_pt_sep_rtpi
, pr_reg
->pr_aptpl_target_lun
,
1924 if ((len
+ strlen(tmp
) >= pr_aptpl_buf_len
)) {
1925 pr_err("Unable to update renaming APTPL metadata,"
1926 " reallocating larger buffer\n");
1930 len
+= sprintf(buf
+len
, "%s", tmp
);
1935 len
+= sprintf(buf
+len
, "No Registrations or Reservations");
1938 spin_unlock(&dev
->t10_pr
.registration_lock
);
1939 spin_unlock(&dev
->dev_reservation_lock
);
1944 static int __core_scsi3_write_aptpl_to_file(
1945 struct se_device
*dev
,
1948 struct t10_wwn
*wwn
= &dev
->t10_wwn
;
1950 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1952 u32 pr_aptpl_buf_len
;
1955 memset(path
, 0, 512);
1957 if (strlen(&wwn
->unit_serial
[0]) >= 512) {
1958 pr_err("WWN value for struct se_device does not fit"
1959 " into path buffer\n");
1963 snprintf(path
, 512, "/var/target/pr/aptpl_%s", &wwn
->unit_serial
[0]);
1964 file
= filp_open(path
, flags
, 0600);
1966 pr_err("filp_open(%s) for APTPL metadata"
1968 return PTR_ERR(file
);
1971 pr_aptpl_buf_len
= (strlen(buf
) + 1); /* Add extra for NULL */
1973 ret
= kernel_write(file
, buf
, pr_aptpl_buf_len
, 0);
1976 pr_debug("Error writing APTPL metadata file: %s\n", path
);
1979 return (ret
< 0) ? -EIO
: 0;
1983 * Clear the APTPL metadata if APTPL has been disabled, otherwise
1984 * write out the updated metadata to struct file for this SCSI device.
1986 static sense_reason_t
core_scsi3_update_and_write_aptpl(struct se_device
*dev
, bool aptpl
)
1989 int rc
, len
= PR_APTPL_BUF_LEN
;
1992 char *null_buf
= "No Registrations or Reservations\n";
1994 rc
= __core_scsi3_write_aptpl_to_file(dev
, null_buf
);
1995 dev
->t10_pr
.pr_aptpl_active
= 0;
1996 pr_debug("SPC-3 PR: Set APTPL Bit Deactivated\n");
1999 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2006 return TCM_OUT_OF_RESOURCES
;
2008 rc
= core_scsi3_update_aptpl_buf(dev
, buf
, len
);
2015 rc
= __core_scsi3_write_aptpl_to_file(dev
, buf
);
2017 pr_err("SPC-3 PR: Could not update APTPL\n");
2019 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2021 dev
->t10_pr
.pr_aptpl_active
= 1;
2023 pr_debug("SPC-3 PR: Set APTPL Bit Activated\n");
2027 static sense_reason_t
2028 core_scsi3_emulate_pro_register(struct se_cmd
*cmd
, u64 res_key
, u64 sa_res_key
,
2029 bool aptpl
, bool all_tg_pt
, bool spec_i_pt
, enum register_type register_type
)
2031 struct se_session
*se_sess
= cmd
->se_sess
;
2032 struct se_device
*dev
= cmd
->se_dev
;
2033 struct se_lun
*se_lun
= cmd
->se_lun
;
2034 struct se_portal_group
*se_tpg
;
2035 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_reg_tmp
;
2036 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2037 unsigned char isid_buf
[PR_REG_ISID_LEN
], *isid_ptr
= NULL
;
2038 sense_reason_t ret
= TCM_NO_SENSE
;
2039 int pr_holder
= 0, type
;
2041 if (!se_sess
|| !se_lun
) {
2042 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2043 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2045 se_tpg
= se_sess
->se_tpg
;
2047 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
) {
2048 memset(&isid_buf
[0], 0, PR_REG_ISID_LEN
);
2049 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
, &isid_buf
[0],
2051 isid_ptr
= &isid_buf
[0];
2054 * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2056 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2059 pr_warn("SPC-3 PR: Reservation Key non-zero"
2060 " for SA REGISTER, returning CONFLICT\n");
2061 return TCM_RESERVATION_CONFLICT
;
2064 * Do nothing but return GOOD status.
2071 * Perform the Service Action REGISTER on the Initiator
2072 * Port Endpoint that the PRO was received from on the
2073 * Logical Unit of the SCSI device server.
2075 if (core_scsi3_alloc_registration(cmd
->se_dev
,
2076 se_sess
->se_node_acl
, cmd
->se_lun
,
2077 NULL
, cmd
->orig_fe_lun
, isid_ptr
,
2078 sa_res_key
, all_tg_pt
, aptpl
,
2079 register_type
, 0)) {
2080 pr_err("Unable to allocate"
2081 " struct t10_pr_registration\n");
2082 return TCM_INVALID_PARAMETER_LIST
;
2086 * Register both the Initiator port that received
2087 * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2088 * TransportID from Parameter list and loop through
2089 * fabric dependent parameter list while calling
2090 * logic from of core_scsi3_alloc_registration() for
2091 * each TransportID provided SCSI Initiator Port/Device
2093 ret
= core_scsi3_decode_spec_i_port(cmd
, se_tpg
,
2094 isid_ptr
, sa_res_key
, all_tg_pt
, aptpl
);
2098 return core_scsi3_update_and_write_aptpl(dev
, aptpl
);
2101 /* ok, existing registration */
2103 if ((register_type
== REGISTER
) && (res_key
!= pr_reg
->pr_res_key
)) {
2104 pr_err("SPC-3 PR REGISTER: Received"
2105 " res_key: 0x%016Lx does not match"
2106 " existing SA REGISTER res_key:"
2107 " 0x%016Lx\n", res_key
,
2108 pr_reg
->pr_res_key
);
2109 ret
= TCM_RESERVATION_CONFLICT
;
2114 pr_err("SPC-3 PR REGISTER: SPEC_I_PT"
2115 " set on a registered nexus\n");
2116 ret
= TCM_INVALID_PARAMETER_LIST
;
2121 * An existing ALL_TG_PT=1 registration being released
2122 * must also set ALL_TG_PT=1 in the incoming PROUT.
2124 if (pr_reg
->pr_reg_all_tg_pt
&& !all_tg_pt
) {
2125 pr_err("SPC-3 PR REGISTER: ALL_TG_PT=1"
2126 " registration exists, but ALL_TG_PT=1 bit not"
2127 " present in received PROUT\n");
2128 ret
= TCM_INVALID_CDB_FIELD
;
2133 * sa_res_key=1 Change Reservation Key for registered I_T Nexus.
2137 * Increment PRgeneration counter for struct se_device"
2138 * upon a successful REGISTER, see spc4r17 section 6.3.2
2139 * READ_KEYS service action.
2141 pr_reg
->pr_res_generation
= core_scsi3_pr_generation(cmd
->se_dev
);
2142 pr_reg
->pr_res_key
= sa_res_key
;
2143 pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2144 " Key for %s to: 0x%016Lx PRgeneration:"
2145 " 0x%08x\n", cmd
->se_tfo
->get_fabric_name(),
2146 (register_type
== REGISTER_AND_IGNORE_EXISTING_KEY
) ? "_AND_IGNORE_EXISTING_KEY" : "",
2147 pr_reg
->pr_reg_nacl
->initiatorname
,
2148 pr_reg
->pr_res_key
, pr_reg
->pr_res_generation
);
2152 * sa_res_key=0 Unregister Reservation Key for registered I_T Nexus.
2154 type
= pr_reg
->pr_res_type
;
2155 pr_holder
= core_scsi3_check_implicit_release(cmd
->se_dev
,
2157 if (pr_holder
< 0) {
2158 ret
= TCM_RESERVATION_CONFLICT
;
2162 spin_lock(&pr_tmpl
->registration_lock
);
2164 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2165 * and matching pr_res_key.
2167 if (pr_reg
->pr_reg_all_tg_pt
) {
2168 list_for_each_entry_safe(pr_reg_p
, pr_reg_tmp
,
2169 &pr_tmpl
->registration_list
,
2172 if (!pr_reg_p
->pr_reg_all_tg_pt
)
2174 if (pr_reg_p
->pr_res_key
!= res_key
)
2176 if (pr_reg
== pr_reg_p
)
2178 if (strcmp(pr_reg
->pr_reg_nacl
->initiatorname
,
2179 pr_reg_p
->pr_reg_nacl
->initiatorname
))
2182 __core_scsi3_free_registration(dev
,
2188 * Release the calling I_T Nexus registration now..
2190 __core_scsi3_free_registration(cmd
->se_dev
, pr_reg
, NULL
, 1);
2194 * From spc4r17, section 5.7.11.3 Unregistering
2196 * If the persistent reservation is a registrants only
2197 * type, the device server shall establish a unit
2198 * attention condition for the initiator port associated
2199 * with every registered I_T nexus except for the I_T
2200 * nexus on which the PERSISTENT RESERVE OUT command was
2201 * received, with the additional sense code set to
2202 * RESERVATIONS RELEASED.
2205 (type
== PR_TYPE_WRITE_EXCLUSIVE_REGONLY
||
2206 type
== PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
)) {
2207 list_for_each_entry(pr_reg_p
,
2208 &pr_tmpl
->registration_list
,
2211 core_scsi3_ua_allocate(
2212 pr_reg_p
->pr_reg_nacl
,
2213 pr_reg_p
->pr_res_mapped_lun
,
2215 ASCQ_2AH_RESERVATIONS_RELEASED
);
2219 spin_unlock(&pr_tmpl
->registration_lock
);
2222 ret
= core_scsi3_update_and_write_aptpl(dev
, aptpl
);
2226 core_scsi3_put_pr_reg(pr_reg
);
2230 unsigned char *core_scsi3_pr_dump_type(int type
)
2233 case PR_TYPE_WRITE_EXCLUSIVE
:
2234 return "Write Exclusive Access";
2235 case PR_TYPE_EXCLUSIVE_ACCESS
:
2236 return "Exclusive Access";
2237 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2238 return "Write Exclusive Access, Registrants Only";
2239 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2240 return "Exclusive Access, Registrants Only";
2241 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2242 return "Write Exclusive Access, All Registrants";
2243 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2244 return "Exclusive Access, All Registrants";
2249 return "Unknown SPC-3 PR Type";
2252 static sense_reason_t
2253 core_scsi3_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
)
2255 struct se_device
*dev
= cmd
->se_dev
;
2256 struct se_session
*se_sess
= cmd
->se_sess
;
2257 struct se_lun
*se_lun
= cmd
->se_lun
;
2258 struct t10_pr_registration
*pr_reg
, *pr_res_holder
;
2259 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2260 char i_buf
[PR_REG_ISID_ID_LEN
];
2263 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2265 if (!se_sess
|| !se_lun
) {
2266 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2267 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2270 * Locate the existing *pr_reg via struct se_node_acl pointers
2272 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2275 pr_err("SPC-3 PR: Unable to locate"
2276 " PR_REGISTERED *pr_reg for RESERVE\n");
2277 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2280 * From spc4r17 Section 5.7.9: Reserving:
2282 * An application client creates a persistent reservation by issuing
2283 * a PERSISTENT RESERVE OUT command with RESERVE service action through
2284 * a registered I_T nexus with the following parameters:
2285 * a) RESERVATION KEY set to the value of the reservation key that is
2286 * registered with the logical unit for the I_T nexus; and
2288 if (res_key
!= pr_reg
->pr_res_key
) {
2289 pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2290 " does not match existing SA REGISTER res_key:"
2291 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2292 ret
= TCM_RESERVATION_CONFLICT
;
2293 goto out_put_pr_reg
;
2296 * From spc4r17 Section 5.7.9: Reserving:
2299 * b) TYPE field and SCOPE field set to the persistent reservation
2302 * Only one persistent reservation is allowed at a time per logical unit
2303 * and that persistent reservation has a scope of LU_SCOPE.
2305 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2306 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2307 ret
= TCM_INVALID_PARAMETER_LIST
;
2308 goto out_put_pr_reg
;
2311 * See if we have an existing PR reservation holder pointer at
2312 * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2315 spin_lock(&dev
->dev_reservation_lock
);
2316 pr_res_holder
= dev
->dev_pr_res_holder
;
2317 if (pr_res_holder
) {
2319 * From spc4r17 Section 5.7.9: Reserving:
2321 * If the device server receives a PERSISTENT RESERVE OUT
2322 * command from an I_T nexus other than a persistent reservation
2323 * holder (see 5.7.10) that attempts to create a persistent
2324 * reservation when a persistent reservation already exists for
2325 * the logical unit, then the command shall be completed with
2326 * RESERVATION CONFLICT status.
2328 if (!is_reservation_holder(pr_res_holder
, pr_reg
)) {
2329 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2330 pr_err("SPC-3 PR: Attempted RESERVE from"
2331 " [%s]: %s while reservation already held by"
2332 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2333 cmd
->se_tfo
->get_fabric_name(),
2334 se_sess
->se_node_acl
->initiatorname
,
2335 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2336 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2338 spin_unlock(&dev
->dev_reservation_lock
);
2339 ret
= TCM_RESERVATION_CONFLICT
;
2340 goto out_put_pr_reg
;
2343 * From spc4r17 Section 5.7.9: Reserving:
2345 * If a persistent reservation holder attempts to modify the
2346 * type or scope of an existing persistent reservation, the
2347 * command shall be completed with RESERVATION CONFLICT status.
2349 if ((pr_res_holder
->pr_res_type
!= type
) ||
2350 (pr_res_holder
->pr_res_scope
!= scope
)) {
2351 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2352 pr_err("SPC-3 PR: Attempted RESERVE from"
2353 " [%s]: %s trying to change TYPE and/or SCOPE,"
2354 " while reservation already held by [%s]: %s,"
2355 " returning RESERVATION_CONFLICT\n",
2356 cmd
->se_tfo
->get_fabric_name(),
2357 se_sess
->se_node_acl
->initiatorname
,
2358 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2359 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2361 spin_unlock(&dev
->dev_reservation_lock
);
2362 ret
= TCM_RESERVATION_CONFLICT
;
2363 goto out_put_pr_reg
;
2366 * From spc4r17 Section 5.7.9: Reserving:
2368 * If the device server receives a PERSISTENT RESERVE OUT
2369 * command with RESERVE service action where the TYPE field and
2370 * the SCOPE field contain the same values as the existing type
2371 * and scope from a persistent reservation holder, it shall not
2372 * make any change to the existing persistent reservation and
2373 * shall completethe command with GOOD status.
2375 spin_unlock(&dev
->dev_reservation_lock
);
2377 goto out_put_pr_reg
;
2380 * Otherwise, our *pr_reg becomes the PR reservation holder for said
2381 * TYPE/SCOPE. Also set the received scope and type in *pr_reg.
2383 pr_reg
->pr_res_scope
= scope
;
2384 pr_reg
->pr_res_type
= type
;
2385 pr_reg
->pr_res_holder
= 1;
2386 dev
->dev_pr_res_holder
= pr_reg
;
2387 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2389 pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2390 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2391 cmd
->se_tfo
->get_fabric_name(), core_scsi3_pr_dump_type(type
),
2392 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2393 pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2394 cmd
->se_tfo
->get_fabric_name(),
2395 se_sess
->se_node_acl
->initiatorname
,
2397 spin_unlock(&dev
->dev_reservation_lock
);
2399 if (pr_tmpl
->pr_aptpl_active
)
2400 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
2404 core_scsi3_put_pr_reg(pr_reg
);
2408 static sense_reason_t
2409 core_scsi3_emulate_pro_reserve(struct se_cmd
*cmd
, int type
, int scope
,
2413 case PR_TYPE_WRITE_EXCLUSIVE
:
2414 case PR_TYPE_EXCLUSIVE_ACCESS
:
2415 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
2416 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
2417 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
2418 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
2419 return core_scsi3_pro_reserve(cmd
, type
, scope
, res_key
);
2421 pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2423 return TCM_INVALID_CDB_FIELD
;
2428 * Called with struct se_device->dev_reservation_lock held.
2430 static void __core_scsi3_complete_pro_release(
2431 struct se_device
*dev
,
2432 struct se_node_acl
*se_nacl
,
2433 struct t10_pr_registration
*pr_reg
,
2437 const struct target_core_fabric_ops
*tfo
= se_nacl
->se_tpg
->se_tpg_tfo
;
2438 char i_buf
[PR_REG_ISID_ID_LEN
];
2439 int pr_res_type
= 0, pr_res_scope
= 0;
2441 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2442 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2444 * Go ahead and release the current PR reservation holder.
2445 * If an All Registrants reservation is currently active and
2446 * a unregister operation is requested, replace the current
2447 * dev_pr_res_holder with another active registration.
2449 if (dev
->dev_pr_res_holder
) {
2450 pr_res_type
= dev
->dev_pr_res_holder
->pr_res_type
;
2451 pr_res_scope
= dev
->dev_pr_res_holder
->pr_res_scope
;
2452 dev
->dev_pr_res_holder
->pr_res_type
= 0;
2453 dev
->dev_pr_res_holder
->pr_res_scope
= 0;
2454 dev
->dev_pr_res_holder
->pr_res_holder
= 0;
2455 dev
->dev_pr_res_holder
= NULL
;
2460 spin_lock(&dev
->t10_pr
.registration_lock
);
2461 list_del_init(&pr_reg
->pr_reg_list
);
2463 * If the I_T nexus is a reservation holder, the persistent reservation
2464 * is of an all registrants type, and the I_T nexus is the last remaining
2465 * registered I_T nexus, then the device server shall also release the
2466 * persistent reservation.
2468 if (!list_empty(&dev
->t10_pr
.registration_list
) &&
2469 ((pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2470 (pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))) {
2471 dev
->dev_pr_res_holder
=
2472 list_entry(dev
->t10_pr
.registration_list
.next
,
2473 struct t10_pr_registration
, pr_reg_list
);
2474 dev
->dev_pr_res_holder
->pr_res_type
= pr_res_type
;
2475 dev
->dev_pr_res_holder
->pr_res_scope
= pr_res_scope
;
2476 dev
->dev_pr_res_holder
->pr_res_holder
= 1;
2478 spin_unlock(&dev
->t10_pr
.registration_lock
);
2480 if (!dev
->dev_pr_res_holder
) {
2481 pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2482 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2483 tfo
->get_fabric_name(), (explicit) ? "explicit" :
2484 "implicit", core_scsi3_pr_dump_type(pr_res_type
),
2485 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2487 pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2488 tfo
->get_fabric_name(), se_nacl
->initiatorname
,
2491 * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2493 pr_reg
->pr_res_holder
= pr_reg
->pr_res_type
= pr_reg
->pr_res_scope
= 0;
2496 static sense_reason_t
2497 core_scsi3_emulate_pro_release(struct se_cmd
*cmd
, int type
, int scope
,
2500 struct se_device
*dev
= cmd
->se_dev
;
2501 struct se_session
*se_sess
= cmd
->se_sess
;
2502 struct se_lun
*se_lun
= cmd
->se_lun
;
2503 struct t10_pr_registration
*pr_reg
, *pr_reg_p
, *pr_res_holder
;
2504 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2505 sense_reason_t ret
= 0;
2507 if (!se_sess
|| !se_lun
) {
2508 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2509 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2512 * Locate the existing *pr_reg via struct se_node_acl pointers
2514 pr_reg
= core_scsi3_locate_pr_reg(dev
, se_sess
->se_node_acl
, se_sess
);
2516 pr_err("SPC-3 PR: Unable to locate"
2517 " PR_REGISTERED *pr_reg for RELEASE\n");
2518 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2521 * From spc4r17 Section 5.7.11.2 Releasing:
2523 * If there is no persistent reservation or in response to a persistent
2524 * reservation release request from a registered I_T nexus that is not a
2525 * persistent reservation holder (see 5.7.10), the device server shall
2528 * a) Not release the persistent reservation, if any;
2529 * b) Not remove any registrations; and
2530 * c) Complete the command with GOOD status.
2532 spin_lock(&dev
->dev_reservation_lock
);
2533 pr_res_holder
= dev
->dev_pr_res_holder
;
2534 if (!pr_res_holder
) {
2536 * No persistent reservation, return GOOD status.
2538 spin_unlock(&dev
->dev_reservation_lock
);
2539 goto out_put_pr_reg
;
2542 if (!is_reservation_holder(pr_res_holder
, pr_reg
)) {
2544 * Release request from a registered I_T nexus that is not a
2545 * persistent reservation holder. return GOOD status.
2547 spin_unlock(&dev
->dev_reservation_lock
);
2548 goto out_put_pr_reg
;
2552 * From spc4r17 Section 5.7.11.2 Releasing:
2554 * Only the persistent reservation holder (see 5.7.10) is allowed to
2555 * release a persistent reservation.
2557 * An application client releases the persistent reservation by issuing
2558 * a PERSISTENT RESERVE OUT command with RELEASE service action through
2559 * an I_T nexus that is a persistent reservation holder with the
2560 * following parameters:
2562 * a) RESERVATION KEY field set to the value of the reservation key
2563 * that is registered with the logical unit for the I_T nexus;
2565 if (res_key
!= pr_reg
->pr_res_key
) {
2566 pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2567 " does not match existing SA REGISTER res_key:"
2568 " 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
2569 spin_unlock(&dev
->dev_reservation_lock
);
2570 ret
= TCM_RESERVATION_CONFLICT
;
2571 goto out_put_pr_reg
;
2574 * From spc4r17 Section 5.7.11.2 Releasing and above:
2576 * b) TYPE field and SCOPE field set to match the persistent
2577 * reservation being released.
2579 if ((pr_res_holder
->pr_res_type
!= type
) ||
2580 (pr_res_holder
->pr_res_scope
!= scope
)) {
2581 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2582 pr_err("SPC-3 PR RELEASE: Attempted to release"
2583 " reservation from [%s]: %s with different TYPE "
2584 "and/or SCOPE while reservation already held by"
2585 " [%s]: %s, returning RESERVATION_CONFLICT\n",
2586 cmd
->se_tfo
->get_fabric_name(),
2587 se_sess
->se_node_acl
->initiatorname
,
2588 pr_res_nacl
->se_tpg
->se_tpg_tfo
->get_fabric_name(),
2589 pr_res_holder
->pr_reg_nacl
->initiatorname
);
2591 spin_unlock(&dev
->dev_reservation_lock
);
2592 ret
= TCM_RESERVATION_CONFLICT
;
2593 goto out_put_pr_reg
;
2596 * In response to a persistent reservation release request from the
2597 * persistent reservation holder the device server shall perform a
2598 * release by doing the following as an uninterrupted series of actions:
2599 * a) Release the persistent reservation;
2600 * b) Not remove any registration(s);
2601 * c) If the released persistent reservation is a registrants only type
2602 * or all registrants type persistent reservation,
2603 * the device server shall establish a unit attention condition for
2604 * the initiator port associated with every regis-
2605 * tered I_T nexus other than I_T nexus on which the PERSISTENT
2606 * RESERVE OUT command with RELEASE service action was received,
2607 * with the additional sense code set to RESERVATIONS RELEASED; and
2608 * d) If the persistent reservation is of any other type, the device
2609 * server shall not establish a unit attention condition.
2611 __core_scsi3_complete_pro_release(dev
, se_sess
->se_node_acl
,
2614 spin_unlock(&dev
->dev_reservation_lock
);
2616 if ((type
!= PR_TYPE_WRITE_EXCLUSIVE_REGONLY
) &&
2617 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
) &&
2618 (type
!= PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) &&
2619 (type
!= PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
2621 * If no UNIT ATTENTION conditions will be established for
2622 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2623 * go ahead and check for APTPL=1 update+write below
2628 spin_lock(&pr_tmpl
->registration_lock
);
2629 list_for_each_entry(pr_reg_p
, &pr_tmpl
->registration_list
,
2632 * Do not establish a UNIT ATTENTION condition
2633 * for the calling I_T Nexus
2635 if (pr_reg_p
== pr_reg
)
2638 core_scsi3_ua_allocate(pr_reg_p
->pr_reg_nacl
,
2639 pr_reg_p
->pr_res_mapped_lun
,
2640 0x2A, ASCQ_2AH_RESERVATIONS_RELEASED
);
2642 spin_unlock(&pr_tmpl
->registration_lock
);
2645 if (pr_tmpl
->pr_aptpl_active
)
2646 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
2649 core_scsi3_put_pr_reg(pr_reg
);
2653 static sense_reason_t
2654 core_scsi3_emulate_pro_clear(struct se_cmd
*cmd
, u64 res_key
)
2656 struct se_device
*dev
= cmd
->se_dev
;
2657 struct se_node_acl
*pr_reg_nacl
;
2658 struct se_session
*se_sess
= cmd
->se_sess
;
2659 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2660 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2661 u32 pr_res_mapped_lun
= 0;
2662 int calling_it_nexus
= 0;
2664 * Locate the existing *pr_reg via struct se_node_acl pointers
2666 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
,
2667 se_sess
->se_node_acl
, se_sess
);
2669 pr_err("SPC-3 PR: Unable to locate"
2670 " PR_REGISTERED *pr_reg for CLEAR\n");
2671 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2674 * From spc4r17 section 5.7.11.6, Clearing:
2676 * Any application client may release the persistent reservation and
2677 * remove all registrations from a device server by issuing a
2678 * PERSISTENT RESERVE OUT command with CLEAR service action through a
2679 * registered I_T nexus with the following parameter:
2681 * a) RESERVATION KEY field set to the value of the reservation key
2682 * that is registered with the logical unit for the I_T nexus.
2684 if (res_key
!= pr_reg_n
->pr_res_key
) {
2685 pr_err("SPC-3 PR REGISTER: Received"
2686 " res_key: 0x%016Lx does not match"
2687 " existing SA REGISTER res_key:"
2688 " 0x%016Lx\n", res_key
, pr_reg_n
->pr_res_key
);
2689 core_scsi3_put_pr_reg(pr_reg_n
);
2690 return TCM_RESERVATION_CONFLICT
;
2693 * a) Release the persistent reservation, if any;
2695 spin_lock(&dev
->dev_reservation_lock
);
2696 pr_res_holder
= dev
->dev_pr_res_holder
;
2697 if (pr_res_holder
) {
2698 struct se_node_acl
*pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
2699 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
2700 pr_res_holder
, 0, 0);
2702 spin_unlock(&dev
->dev_reservation_lock
);
2704 * b) Remove all registration(s) (see spc4r17 5.7.7);
2706 spin_lock(&pr_tmpl
->registration_lock
);
2707 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2708 &pr_tmpl
->registration_list
, pr_reg_list
) {
2710 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2711 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2712 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2713 __core_scsi3_free_registration(dev
, pr_reg
, NULL
,
2716 * e) Establish a unit attention condition for the initiator
2717 * port associated with every registered I_T nexus other
2718 * than the I_T nexus on which the PERSISTENT RESERVE OUT
2719 * command with CLEAR service action was received, with the
2720 * additional sense code set to RESERVATIONS PREEMPTED.
2722 if (!calling_it_nexus
)
2723 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
,
2724 0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED
);
2726 spin_unlock(&pr_tmpl
->registration_lock
);
2728 pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2729 cmd
->se_tfo
->get_fabric_name());
2731 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, false);
2733 core_scsi3_pr_generation(dev
);
2738 * Called with struct se_device->dev_reservation_lock held.
2740 static void __core_scsi3_complete_pro_preempt(
2741 struct se_device
*dev
,
2742 struct t10_pr_registration
*pr_reg
,
2743 struct list_head
*preempt_and_abort_list
,
2746 enum preempt_type preempt_type
)
2748 struct se_node_acl
*nacl
= pr_reg
->pr_reg_nacl
;
2749 const struct target_core_fabric_ops
*tfo
= nacl
->se_tpg
->se_tpg_tfo
;
2750 char i_buf
[PR_REG_ISID_ID_LEN
];
2752 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
2753 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
2755 * Do an implicit RELEASE of the existing reservation.
2757 if (dev
->dev_pr_res_holder
)
2758 __core_scsi3_complete_pro_release(dev
, nacl
,
2759 dev
->dev_pr_res_holder
, 0, 0);
2761 dev
->dev_pr_res_holder
= pr_reg
;
2762 pr_reg
->pr_res_holder
= 1;
2763 pr_reg
->pr_res_type
= type
;
2764 pr_reg
->pr_res_scope
= scope
;
2766 pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2767 " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2768 tfo
->get_fabric_name(), (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "",
2769 core_scsi3_pr_dump_type(type
),
2770 (pr_reg
->pr_reg_all_tg_pt
) ? 1 : 0);
2771 pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2772 tfo
->get_fabric_name(), (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "",
2773 nacl
->initiatorname
, i_buf
);
2775 * For PREEMPT_AND_ABORT, add the preempting reservation's
2776 * struct t10_pr_registration to the list that will be compared
2777 * against received CDBs..
2779 if (preempt_and_abort_list
)
2780 list_add_tail(&pr_reg
->pr_reg_abort_list
,
2781 preempt_and_abort_list
);
2784 static void core_scsi3_release_preempt_and_abort(
2785 struct list_head
*preempt_and_abort_list
,
2786 struct t10_pr_registration
*pr_reg_holder
)
2788 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
2790 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
, preempt_and_abort_list
,
2791 pr_reg_abort_list
) {
2793 list_del(&pr_reg
->pr_reg_abort_list
);
2794 if (pr_reg_holder
== pr_reg
)
2796 if (pr_reg
->pr_res_holder
) {
2797 pr_warn("pr_reg->pr_res_holder still set\n");
2801 pr_reg
->pr_reg_deve
= NULL
;
2802 pr_reg
->pr_reg_nacl
= NULL
;
2803 kmem_cache_free(t10_pr_reg_cache
, pr_reg
);
2807 static sense_reason_t
2808 core_scsi3_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
, u64 res_key
,
2809 u64 sa_res_key
, enum preempt_type preempt_type
)
2811 struct se_device
*dev
= cmd
->se_dev
;
2812 struct se_node_acl
*pr_reg_nacl
;
2813 struct se_session
*se_sess
= cmd
->se_sess
;
2814 LIST_HEAD(preempt_and_abort_list
);
2815 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
, *pr_reg_n
, *pr_res_holder
;
2816 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
2817 u32 pr_res_mapped_lun
= 0;
2818 int all_reg
= 0, calling_it_nexus
= 0;
2819 bool sa_res_key_unmatched
= sa_res_key
!= 0;
2820 int prh_type
= 0, prh_scope
= 0;
2823 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2825 pr_reg_n
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
2828 pr_err("SPC-3 PR: Unable to locate"
2829 " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2830 (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "");
2831 return TCM_RESERVATION_CONFLICT
;
2833 if (pr_reg_n
->pr_res_key
!= res_key
) {
2834 core_scsi3_put_pr_reg(pr_reg_n
);
2835 return TCM_RESERVATION_CONFLICT
;
2837 if (scope
!= PR_SCOPE_LU_SCOPE
) {
2838 pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope
);
2839 core_scsi3_put_pr_reg(pr_reg_n
);
2840 return TCM_INVALID_PARAMETER_LIST
;
2843 spin_lock(&dev
->dev_reservation_lock
);
2844 pr_res_holder
= dev
->dev_pr_res_holder
;
2845 if (pr_res_holder
&&
2846 ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
2847 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)))
2850 if (!all_reg
&& !sa_res_key
) {
2851 spin_unlock(&dev
->dev_reservation_lock
);
2852 core_scsi3_put_pr_reg(pr_reg_n
);
2853 return TCM_INVALID_PARAMETER_LIST
;
2856 * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2858 * If the SERVICE ACTION RESERVATION KEY field does not identify a
2859 * persistent reservation holder or there is no persistent reservation
2860 * holder (i.e., there is no persistent reservation), then the device
2861 * server shall perform a preempt by doing the following in an
2862 * uninterrupted series of actions. (See below..)
2864 if (!pr_res_holder
|| (pr_res_holder
->pr_res_key
!= sa_res_key
)) {
2866 * No existing or SA Reservation Key matching reservations..
2868 * PROUT SA PREEMPT with All Registrant type reservations are
2869 * allowed to be processed without a matching SA Reservation Key
2871 spin_lock(&pr_tmpl
->registration_lock
);
2872 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
2873 &pr_tmpl
->registration_list
, pr_reg_list
) {
2875 * Removing of registrations in non all registrants
2876 * type reservations without a matching SA reservation
2879 * a) Remove the registrations for all I_T nexuses
2880 * specified by the SERVICE ACTION RESERVATION KEY
2882 * b) Ignore the contents of the SCOPE and TYPE fields;
2883 * c) Process tasks as defined in 5.7.1; and
2884 * d) Establish a unit attention condition for the
2885 * initiator port associated with every I_T nexus
2886 * that lost its registration other than the I_T
2887 * nexus on which the PERSISTENT RESERVE OUT command
2888 * was received, with the additional sense code set
2889 * to REGISTRATIONS PREEMPTED.
2892 if (pr_reg
->pr_res_key
!= sa_res_key
)
2894 sa_res_key_unmatched
= false;
2896 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2897 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2898 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2899 __core_scsi3_free_registration(dev
, pr_reg
,
2900 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
:
2901 NULL
, calling_it_nexus
);
2904 * Case for any existing all registrants type
2905 * reservation, follow logic in spc4r17 section
2906 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2908 * For a ZERO SA Reservation key, release
2909 * all other registrations and do an implicit
2910 * release of active persistent reservation.
2912 * For a non-ZERO SA Reservation key, only
2913 * release the matching reservation key from
2917 (pr_reg
->pr_res_key
!= sa_res_key
))
2919 sa_res_key_unmatched
= false;
2921 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
2922 if (calling_it_nexus
)
2925 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
2926 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
2927 __core_scsi3_free_registration(dev
, pr_reg
,
2928 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
:
2931 if (!calling_it_nexus
)
2932 core_scsi3_ua_allocate(pr_reg_nacl
,
2933 pr_res_mapped_lun
, 0x2A,
2934 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
2936 spin_unlock(&pr_tmpl
->registration_lock
);
2938 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
2939 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
2940 * RESERVATION KEY field to a value that does not match any
2941 * registered reservation key, then the device server shall
2942 * complete the command with RESERVATION CONFLICT status.
2944 if (sa_res_key_unmatched
) {
2945 spin_unlock(&dev
->dev_reservation_lock
);
2946 core_scsi3_put_pr_reg(pr_reg_n
);
2947 return TCM_RESERVATION_CONFLICT
;
2950 * For an existing all registrants type reservation
2951 * with a zero SA rservation key, preempt the existing
2952 * reservation with the new PR type and scope.
2954 if (pr_res_holder
&& all_reg
&& !(sa_res_key
)) {
2955 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
2956 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
2957 type
, scope
, preempt_type
);
2959 if (preempt_type
== PREEMPT_AND_ABORT
)
2960 core_scsi3_release_preempt_and_abort(
2961 &preempt_and_abort_list
, pr_reg_n
);
2963 spin_unlock(&dev
->dev_reservation_lock
);
2965 if (pr_tmpl
->pr_aptpl_active
)
2966 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
2968 core_scsi3_put_pr_reg(pr_reg_n
);
2969 core_scsi3_pr_generation(cmd
->se_dev
);
2973 * The PREEMPTing SA reservation key matches that of the
2974 * existing persistent reservation, first, we check if
2975 * we are preempting our own reservation.
2976 * From spc4r17, section 5.7.11.4.3 Preempting
2977 * persistent reservations and registration handling
2979 * If an all registrants persistent reservation is not
2980 * present, it is not an error for the persistent
2981 * reservation holder to preempt itself (i.e., a
2982 * PERSISTENT RESERVE OUT with a PREEMPT service action
2983 * or a PREEMPT AND ABORT service action with the
2984 * SERVICE ACTION RESERVATION KEY value equal to the
2985 * persistent reservation holder's reservation key that
2986 * is received from the persistent reservation holder).
2987 * In that case, the device server shall establish the
2988 * new persistent reservation and maintain the
2991 prh_type
= pr_res_holder
->pr_res_type
;
2992 prh_scope
= pr_res_holder
->pr_res_scope
;
2994 * If the SERVICE ACTION RESERVATION KEY field identifies a
2995 * persistent reservation holder (see 5.7.10), the device
2996 * server shall perform a preempt by doing the following as
2997 * an uninterrupted series of actions:
2999 * a) Release the persistent reservation for the holder
3000 * identified by the SERVICE ACTION RESERVATION KEY field;
3002 if (pr_reg_n
!= pr_res_holder
)
3003 __core_scsi3_complete_pro_release(dev
,
3004 pr_res_holder
->pr_reg_nacl
,
3005 dev
->dev_pr_res_holder
, 0, 0);
3007 * b) Remove the registrations for all I_T nexuses identified
3008 * by the SERVICE ACTION RESERVATION KEY field, except the
3009 * I_T nexus that is being used for the PERSISTENT RESERVE
3010 * OUT command. If an all registrants persistent reservation
3011 * is present and the SERVICE ACTION RESERVATION KEY field
3012 * is set to zero, then all registrations shall be removed
3013 * except for that of the I_T nexus that is being used for
3014 * the PERSISTENT RESERVE OUT command;
3016 spin_lock(&pr_tmpl
->registration_lock
);
3017 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3018 &pr_tmpl
->registration_list
, pr_reg_list
) {
3020 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3021 if (calling_it_nexus
)
3024 if (pr_reg
->pr_res_key
!= sa_res_key
)
3027 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3028 pr_res_mapped_lun
= pr_reg
->pr_res_mapped_lun
;
3029 __core_scsi3_free_registration(dev
, pr_reg
,
3030 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
3033 * e) Establish a unit attention condition for the initiator
3034 * port associated with every I_T nexus that lost its
3035 * persistent reservation and/or registration, with the
3036 * additional sense code set to REGISTRATIONS PREEMPTED;
3038 core_scsi3_ua_allocate(pr_reg_nacl
, pr_res_mapped_lun
, 0x2A,
3039 ASCQ_2AH_REGISTRATIONS_PREEMPTED
);
3041 spin_unlock(&pr_tmpl
->registration_lock
);
3043 * c) Establish a persistent reservation for the preempting
3044 * I_T nexus using the contents of the SCOPE and TYPE fields;
3046 __core_scsi3_complete_pro_preempt(dev
, pr_reg_n
,
3047 (preempt_type
== PREEMPT_AND_ABORT
) ? &preempt_and_abort_list
: NULL
,
3048 type
, scope
, preempt_type
);
3050 * d) Process tasks as defined in 5.7.1;
3052 * f) If the type or scope has changed, then for every I_T nexus
3053 * whose reservation key was not removed, except for the I_T
3054 * nexus on which the PERSISTENT RESERVE OUT command was
3055 * received, the device server shall establish a unit
3056 * attention condition for the initiator port associated with
3057 * that I_T nexus, with the additional sense code set to
3058 * RESERVATIONS RELEASED. If the type or scope have not
3059 * changed, then no unit attention condition(s) shall be
3060 * established for this reason.
3062 if ((prh_type
!= type
) || (prh_scope
!= scope
)) {
3063 spin_lock(&pr_tmpl
->registration_lock
);
3064 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3065 &pr_tmpl
->registration_list
, pr_reg_list
) {
3067 calling_it_nexus
= (pr_reg_n
== pr_reg
) ? 1 : 0;
3068 if (calling_it_nexus
)
3071 core_scsi3_ua_allocate(pr_reg
->pr_reg_nacl
,
3072 pr_reg
->pr_res_mapped_lun
, 0x2A,
3073 ASCQ_2AH_RESERVATIONS_RELEASED
);
3075 spin_unlock(&pr_tmpl
->registration_lock
);
3077 spin_unlock(&dev
->dev_reservation_lock
);
3079 * Call LUN_RESET logic upon list of struct t10_pr_registration,
3080 * All received CDBs for the matching existing reservation and
3081 * registrations undergo ABORT_TASK logic.
3083 * From there, core_scsi3_release_preempt_and_abort() will
3084 * release every registration in the list (which have already
3085 * been removed from the primary pr_reg list), except the
3086 * new persistent reservation holder, the calling Initiator Port.
3088 if (preempt_type
== PREEMPT_AND_ABORT
) {
3089 core_tmr_lun_reset(dev
, NULL
, &preempt_and_abort_list
, cmd
);
3090 core_scsi3_release_preempt_and_abort(&preempt_and_abort_list
,
3094 if (pr_tmpl
->pr_aptpl_active
)
3095 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, true);
3097 core_scsi3_put_pr_reg(pr_reg_n
);
3098 core_scsi3_pr_generation(cmd
->se_dev
);
3102 static sense_reason_t
3103 core_scsi3_emulate_pro_preempt(struct se_cmd
*cmd
, int type
, int scope
,
3104 u64 res_key
, u64 sa_res_key
, enum preempt_type preempt_type
)
3107 case PR_TYPE_WRITE_EXCLUSIVE
:
3108 case PR_TYPE_EXCLUSIVE_ACCESS
:
3109 case PR_TYPE_WRITE_EXCLUSIVE_REGONLY
:
3110 case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY
:
3111 case PR_TYPE_WRITE_EXCLUSIVE_ALLREG
:
3112 case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
:
3113 return core_scsi3_pro_preempt(cmd
, type
, scope
, res_key
,
3114 sa_res_key
, preempt_type
);
3116 pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3117 " Type: 0x%02x\n", (preempt_type
== PREEMPT_AND_ABORT
) ? "_AND_ABORT" : "", type
);
3118 return TCM_INVALID_CDB_FIELD
;
3123 static sense_reason_t
3124 core_scsi3_emulate_pro_register_and_move(struct se_cmd
*cmd
, u64 res_key
,
3125 u64 sa_res_key
, int aptpl
, int unreg
)
3127 struct se_session
*se_sess
= cmd
->se_sess
;
3128 struct se_device
*dev
= cmd
->se_dev
;
3129 struct se_dev_entry
*dest_se_deve
= NULL
;
3130 struct se_lun
*se_lun
= cmd
->se_lun
;
3131 struct se_node_acl
*pr_res_nacl
, *pr_reg_nacl
, *dest_node_acl
= NULL
;
3132 struct se_port
*se_port
;
3133 struct se_portal_group
*se_tpg
, *dest_se_tpg
= NULL
;
3134 const struct target_core_fabric_ops
*dest_tf_ops
= NULL
, *tf_ops
;
3135 struct t10_pr_registration
*pr_reg
, *pr_res_holder
, *dest_pr_reg
;
3136 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3138 const unsigned char *initiator_str
;
3139 char *iport_ptr
= NULL
, i_buf
[PR_REG_ISID_ID_LEN
];
3140 u32 tid_len
, tmp_tid_len
;
3141 int new_reg
= 0, type
, scope
, matching_iname
;
3143 unsigned short rtpi
;
3144 unsigned char proto_ident
;
3146 if (!se_sess
|| !se_lun
) {
3147 pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3148 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3151 memset(i_buf
, 0, PR_REG_ISID_ID_LEN
);
3152 se_tpg
= se_sess
->se_tpg
;
3153 tf_ops
= se_tpg
->se_tpg_tfo
;
3155 * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3156 * Register behaviors for a REGISTER AND MOVE service action
3158 * Locate the existing *pr_reg via struct se_node_acl pointers
3160 pr_reg
= core_scsi3_locate_pr_reg(cmd
->se_dev
, se_sess
->se_node_acl
,
3163 pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3164 " *pr_reg for REGISTER_AND_MOVE\n");
3165 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3168 * The provided reservation key much match the existing reservation key
3169 * provided during this initiator's I_T nexus registration.
3171 if (res_key
!= pr_reg
->pr_res_key
) {
3172 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3173 " res_key: 0x%016Lx does not match existing SA REGISTER"
3174 " res_key: 0x%016Lx\n", res_key
, pr_reg
->pr_res_key
);
3175 ret
= TCM_RESERVATION_CONFLICT
;
3176 goto out_put_pr_reg
;
3179 * The service active reservation key needs to be non zero
3182 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3184 ret
= TCM_INVALID_PARAMETER_LIST
;
3185 goto out_put_pr_reg
;
3189 * Determine the Relative Target Port Identifier where the reservation
3190 * will be moved to for the TransportID containing SCSI initiator WWN
3193 buf
= transport_kmap_data_sg(cmd
);
3195 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3196 goto out_put_pr_reg
;
3199 rtpi
= (buf
[18] & 0xff) << 8;
3200 rtpi
|= buf
[19] & 0xff;
3201 tid_len
= (buf
[20] & 0xff) << 24;
3202 tid_len
|= (buf
[21] & 0xff) << 16;
3203 tid_len
|= (buf
[22] & 0xff) << 8;
3204 tid_len
|= buf
[23] & 0xff;
3205 transport_kunmap_data_sg(cmd
);
3208 if ((tid_len
+ 24) != cmd
->data_length
) {
3209 pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3210 " does not equal CDB data_length: %u\n", tid_len
,
3212 ret
= TCM_INVALID_PARAMETER_LIST
;
3213 goto out_put_pr_reg
;
3216 spin_lock(&dev
->se_port_lock
);
3217 list_for_each_entry(se_port
, &dev
->dev_sep_list
, sep_list
) {
3218 if (se_port
->sep_rtpi
!= rtpi
)
3220 dest_se_tpg
= se_port
->sep_tpg
;
3223 dest_tf_ops
= dest_se_tpg
->se_tpg_tfo
;
3227 atomic_inc_mb(&dest_se_tpg
->tpg_pr_ref_count
);
3228 spin_unlock(&dev
->se_port_lock
);
3230 if (core_scsi3_tpg_depend_item(dest_se_tpg
)) {
3231 pr_err("core_scsi3_tpg_depend_item() failed"
3232 " for dest_se_tpg\n");
3233 atomic_dec_mb(&dest_se_tpg
->tpg_pr_ref_count
);
3234 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3235 goto out_put_pr_reg
;
3238 spin_lock(&dev
->se_port_lock
);
3241 spin_unlock(&dev
->se_port_lock
);
3243 if (!dest_se_tpg
|| !dest_tf_ops
) {
3244 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3245 " fabric ops from Relative Target Port Identifier:"
3247 ret
= TCM_INVALID_PARAMETER_LIST
;
3248 goto out_put_pr_reg
;
3251 buf
= transport_kmap_data_sg(cmd
);
3253 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3254 goto out_put_pr_reg
;
3256 proto_ident
= (buf
[24] & 0x0f);
3258 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3259 " 0x%02x\n", proto_ident
);
3261 if (proto_ident
!= dest_se_tpg
->proto_id
) {
3262 pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3263 " proto_ident: 0x%02x does not match ident: 0x%02x"
3264 " from fabric: %s\n", proto_ident
,
3265 dest_se_tpg
->proto_id
,
3266 dest_tf_ops
->get_fabric_name());
3267 ret
= TCM_INVALID_PARAMETER_LIST
;
3270 initiator_str
= target_parse_pr_out_transport_id(dest_se_tpg
,
3271 (const char *)&buf
[24], &tmp_tid_len
, &iport_ptr
);
3272 if (!initiator_str
) {
3273 pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3274 " initiator_str from Transport ID\n");
3275 ret
= TCM_INVALID_PARAMETER_LIST
;
3279 transport_kunmap_data_sg(cmd
);
3282 pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3283 " %s\n", dest_tf_ops
->get_fabric_name(), (iport_ptr
!= NULL
) ?
3284 "port" : "device", initiator_str
, (iport_ptr
!= NULL
) ?
3287 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3288 * action specifies a TransportID that is the same as the initiator port
3289 * of the I_T nexus for the command received, then the command shall
3290 * be terminated with CHECK CONDITION status, with the sense key set to
3291 * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3292 * IN PARAMETER LIST.
3294 pr_reg_nacl
= pr_reg
->pr_reg_nacl
;
3295 matching_iname
= (!strcmp(initiator_str
,
3296 pr_reg_nacl
->initiatorname
)) ? 1 : 0;
3297 if (!matching_iname
)
3298 goto after_iport_check
;
3300 if (!iport_ptr
|| !pr_reg
->isid_present_at_reg
) {
3301 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3302 " matches: %s on received I_T Nexus\n", initiator_str
,
3303 pr_reg_nacl
->initiatorname
);
3304 ret
= TCM_INVALID_PARAMETER_LIST
;
3307 if (!strcmp(iport_ptr
, pr_reg
->pr_reg_isid
)) {
3308 pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3309 " matches: %s %s on received I_T Nexus\n",
3310 initiator_str
, iport_ptr
, pr_reg_nacl
->initiatorname
,
3311 pr_reg
->pr_reg_isid
);
3312 ret
= TCM_INVALID_PARAMETER_LIST
;
3317 * Locate the destination struct se_node_acl from the received Transport ID
3319 spin_lock_irq(&dest_se_tpg
->acl_node_lock
);
3320 dest_node_acl
= __core_tpg_get_initiator_node_acl(dest_se_tpg
,
3323 atomic_inc_mb(&dest_node_acl
->acl_pr_ref_count
);
3324 spin_unlock_irq(&dest_se_tpg
->acl_node_lock
);
3326 if (!dest_node_acl
) {
3327 pr_err("Unable to locate %s dest_node_acl for"
3328 " TransportID%s\n", dest_tf_ops
->get_fabric_name(),
3330 ret
= TCM_INVALID_PARAMETER_LIST
;
3334 if (core_scsi3_nodeacl_depend_item(dest_node_acl
)) {
3335 pr_err("core_scsi3_nodeacl_depend_item() for"
3336 " dest_node_acl\n");
3337 atomic_dec_mb(&dest_node_acl
->acl_pr_ref_count
);
3338 dest_node_acl
= NULL
;
3339 ret
= TCM_INVALID_PARAMETER_LIST
;
3343 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3344 " %s from TransportID\n", dest_tf_ops
->get_fabric_name(),
3345 dest_node_acl
->initiatorname
);
3348 * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3351 dest_se_deve
= core_get_se_deve_from_rtpi(dest_node_acl
, rtpi
);
3352 if (!dest_se_deve
) {
3353 pr_err("Unable to locate %s dest_se_deve from RTPI:"
3354 " %hu\n", dest_tf_ops
->get_fabric_name(), rtpi
);
3355 ret
= TCM_INVALID_PARAMETER_LIST
;
3359 if (core_scsi3_lunacl_depend_item(dest_se_deve
)) {
3360 pr_err("core_scsi3_lunacl_depend_item() failed\n");
3361 kref_put(&dest_se_deve
->pr_kref
, target_pr_kref_release
);
3362 dest_se_deve
= NULL
;
3363 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3367 pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3368 " ACL for dest_se_deve->mapped_lun: %u\n",
3369 dest_tf_ops
->get_fabric_name(), dest_node_acl
->initiatorname
,
3370 dest_se_deve
->mapped_lun
);
3373 * A persistent reservation needs to already existing in order to
3374 * successfully complete the REGISTER_AND_MOVE service action..
3376 spin_lock(&dev
->dev_reservation_lock
);
3377 pr_res_holder
= dev
->dev_pr_res_holder
;
3378 if (!pr_res_holder
) {
3379 pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3380 " currently held\n");
3381 spin_unlock(&dev
->dev_reservation_lock
);
3382 ret
= TCM_INVALID_CDB_FIELD
;
3386 * The received on I_T Nexus must be the reservation holder.
3388 * From spc4r17 section 5.7.8 Table 50 --
3389 * Register behaviors for a REGISTER AND MOVE service action
3391 if (!is_reservation_holder(pr_res_holder
, pr_reg
)) {
3392 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3393 " Nexus is not reservation holder\n");
3394 spin_unlock(&dev
->dev_reservation_lock
);
3395 ret
= TCM_RESERVATION_CONFLICT
;
3399 * From spc4r17 section 5.7.8: registering and moving reservation
3401 * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3402 * action is received and the established persistent reservation is a
3403 * Write Exclusive - All Registrants type or Exclusive Access -
3404 * All Registrants type reservation, then the command shall be completed
3405 * with RESERVATION CONFLICT status.
3407 if ((pr_res_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3408 (pr_res_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
)) {
3409 pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3410 " reservation for type: %s\n",
3411 core_scsi3_pr_dump_type(pr_res_holder
->pr_res_type
));
3412 spin_unlock(&dev
->dev_reservation_lock
);
3413 ret
= TCM_RESERVATION_CONFLICT
;
3416 pr_res_nacl
= pr_res_holder
->pr_reg_nacl
;
3418 * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3420 type
= pr_res_holder
->pr_res_type
;
3421 scope
= pr_res_holder
->pr_res_type
;
3423 * c) Associate the reservation key specified in the SERVICE ACTION
3424 * RESERVATION KEY field with the I_T nexus specified as the
3425 * destination of the register and move, where:
3426 * A) The I_T nexus is specified by the TransportID and the
3427 * RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3428 * B) Regardless of the TransportID format used, the association for
3429 * the initiator port is based on either the initiator port name
3430 * (see 3.1.71) on SCSI transport protocols where port names are
3431 * required or the initiator port identifier (see 3.1.70) on SCSI
3432 * transport protocols where port names are not required;
3433 * d) Register the reservation key specified in the SERVICE ACTION
3434 * RESERVATION KEY field;
3435 * e) Retain the reservation key specified in the SERVICE ACTION
3436 * RESERVATION KEY field and associated information;
3438 * Also, It is not an error for a REGISTER AND MOVE service action to
3439 * register an I_T nexus that is already registered with the same
3440 * reservation key or a different reservation key.
3442 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3445 struct se_lun
*dest_lun
= rcu_dereference_check(dest_se_deve
->se_lun
,
3446 atomic_read(&dest_se_deve
->pr_kref
.refcount
) != 0);
3448 spin_unlock(&dev
->dev_reservation_lock
);
3449 if (core_scsi3_alloc_registration(cmd
->se_dev
, dest_node_acl
,
3450 dest_lun
, dest_se_deve
, dest_se_deve
->mapped_lun
,
3451 iport_ptr
, sa_res_key
, 0, aptpl
, 2, 1)) {
3452 ret
= TCM_INVALID_PARAMETER_LIST
;
3455 spin_lock(&dev
->dev_reservation_lock
);
3456 dest_pr_reg
= __core_scsi3_locate_pr_reg(dev
, dest_node_acl
,
3461 * f) Release the persistent reservation for the persistent reservation
3462 * holder (i.e., the I_T nexus on which the
3464 __core_scsi3_complete_pro_release(dev
, pr_res_nacl
,
3465 dev
->dev_pr_res_holder
, 0, 0);
3467 * g) Move the persistent reservation to the specified I_T nexus using
3468 * the same scope and type as the persistent reservation released in
3471 dev
->dev_pr_res_holder
= dest_pr_reg
;
3472 dest_pr_reg
->pr_res_holder
= 1;
3473 dest_pr_reg
->pr_res_type
= type
;
3474 pr_reg
->pr_res_scope
= scope
;
3475 core_pr_dump_initiator_port(pr_reg
, i_buf
, PR_REG_ISID_ID_LEN
);
3477 * Increment PRGeneration for existing registrations..
3480 dest_pr_reg
->pr_res_generation
= pr_tmpl
->pr_generation
++;
3481 spin_unlock(&dev
->dev_reservation_lock
);
3483 pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3484 " created new reservation holder TYPE: %s on object RTPI:"
3485 " %hu PRGeneration: 0x%08x\n", dest_tf_ops
->get_fabric_name(),
3486 core_scsi3_pr_dump_type(type
), rtpi
,
3487 dest_pr_reg
->pr_res_generation
);
3488 pr_debug("SPC-3 PR Successfully moved reservation from"
3489 " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3490 tf_ops
->get_fabric_name(), pr_reg_nacl
->initiatorname
,
3491 i_buf
, dest_tf_ops
->get_fabric_name(),
3492 dest_node_acl
->initiatorname
, (iport_ptr
!= NULL
) ?
3495 * It is now safe to release configfs group dependencies for destination
3496 * of Transport ID Initiator Device/Port Identifier
3498 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3499 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3500 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3502 * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3503 * nexus on which PERSISTENT RESERVE OUT command was received.
3506 spin_lock(&pr_tmpl
->registration_lock
);
3507 __core_scsi3_free_registration(dev
, pr_reg
, NULL
, 1);
3508 spin_unlock(&pr_tmpl
->registration_lock
);
3510 core_scsi3_put_pr_reg(pr_reg
);
3512 core_scsi3_update_and_write_aptpl(cmd
->se_dev
, aptpl
);
3514 transport_kunmap_data_sg(cmd
);
3516 core_scsi3_put_pr_reg(dest_pr_reg
);
3520 transport_kunmap_data_sg(cmd
);
3522 core_scsi3_lunacl_undepend_item(dest_se_deve
);
3524 core_scsi3_nodeacl_undepend_item(dest_node_acl
);
3525 core_scsi3_tpg_undepend_item(dest_se_tpg
);
3528 core_scsi3_put_pr_reg(pr_reg
);
3532 static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb
)
3534 unsigned int __v1
, __v2
;
3536 __v1
= (cdb
[0] << 24) | (cdb
[1] << 16) | (cdb
[2] << 8) | cdb
[3];
3537 __v2
= (cdb
[4] << 24) | (cdb
[5] << 16) | (cdb
[6] << 8) | cdb
[7];
3539 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
3543 * See spc4r17 section 6.14 Table 170
3546 target_scsi3_emulate_pr_out(struct se_cmd
*cmd
)
3548 struct se_device
*dev
= cmd
->se_dev
;
3549 unsigned char *cdb
= &cmd
->t_task_cdb
[0];
3551 u64 res_key
, sa_res_key
;
3552 int sa
, scope
, type
, aptpl
;
3553 int spec_i_pt
= 0, all_tg_pt
= 0, unreg
= 0;
3557 * Following spc2r20 5.5.1 Reservations overview:
3559 * If a logical unit has been reserved by any RESERVE command and is
3560 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3561 * PERSISTENT RESERVE OUT commands shall conflict regardless of
3562 * initiator or service action and shall terminate with a RESERVATION
3565 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
3566 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3567 " SPC-2 reservation is held, returning"
3568 " RESERVATION_CONFLICT\n");
3569 return TCM_RESERVATION_CONFLICT
;
3573 * FIXME: A NULL struct se_session pointer means an this is not coming from
3574 * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3577 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3579 if (cmd
->data_length
< 24) {
3580 pr_warn("SPC-PR: Received PR OUT parameter list"
3581 " length too small: %u\n", cmd
->data_length
);
3582 return TCM_INVALID_PARAMETER_LIST
;
3586 * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3588 sa
= (cdb
[1] & 0x1f);
3589 scope
= (cdb
[2] & 0xf0);
3590 type
= (cdb
[2] & 0x0f);
3592 buf
= transport_kmap_data_sg(cmd
);
3594 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3597 * From PERSISTENT_RESERVE_OUT parameter list (payload)
3599 res_key
= core_scsi3_extract_reservation_key(&buf
[0]);
3600 sa_res_key
= core_scsi3_extract_reservation_key(&buf
[8]);
3602 * REGISTER_AND_MOVE uses a different SA parameter list containing
3603 * SCSI TransportIDs.
3605 if (sa
!= PRO_REGISTER_AND_MOVE
) {
3606 spec_i_pt
= (buf
[20] & 0x08);
3607 all_tg_pt
= (buf
[20] & 0x04);
3608 aptpl
= (buf
[20] & 0x01);
3610 aptpl
= (buf
[17] & 0x01);
3611 unreg
= (buf
[17] & 0x02);
3614 * If the backend device has been configured to force APTPL metadata
3615 * write-out, go ahead and propigate aptpl=1 down now.
3617 if (dev
->dev_attrib
.force_pr_aptpl
)
3620 transport_kunmap_data_sg(cmd
);
3624 * SPEC_I_PT=1 is only valid for Service action: REGISTER
3626 if (spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER
))
3627 return TCM_INVALID_PARAMETER_LIST
;
3630 * From spc4r17 section 6.14:
3632 * If the SPEC_I_PT bit is set to zero, the service action is not
3633 * REGISTER AND MOVE, and the parameter list length is not 24, then
3634 * the command shall be terminated with CHECK CONDITION status, with
3635 * the sense key set to ILLEGAL REQUEST, and the additional sense
3636 * code set to PARAMETER LIST LENGTH ERROR.
3638 if (!spec_i_pt
&& ((cdb
[1] & 0x1f) != PRO_REGISTER_AND_MOVE
) &&
3639 (cmd
->data_length
!= 24)) {
3640 pr_warn("SPC-PR: Received PR OUT illegal parameter"
3641 " list length: %u\n", cmd
->data_length
);
3642 return TCM_INVALID_PARAMETER_LIST
;
3646 * (core_scsi3_emulate_pro_* function parameters
3647 * are defined by spc4r17 Table 174:
3648 * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3652 ret
= core_scsi3_emulate_pro_register(cmd
,
3653 res_key
, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, REGISTER
);
3656 ret
= core_scsi3_emulate_pro_reserve(cmd
, type
, scope
, res_key
);
3659 ret
= core_scsi3_emulate_pro_release(cmd
, type
, scope
, res_key
);
3662 ret
= core_scsi3_emulate_pro_clear(cmd
, res_key
);
3665 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3666 res_key
, sa_res_key
, PREEMPT
);
3668 case PRO_PREEMPT_AND_ABORT
:
3669 ret
= core_scsi3_emulate_pro_preempt(cmd
, type
, scope
,
3670 res_key
, sa_res_key
, PREEMPT_AND_ABORT
);
3672 case PRO_REGISTER_AND_IGNORE_EXISTING_KEY
:
3673 ret
= core_scsi3_emulate_pro_register(cmd
,
3674 0, sa_res_key
, aptpl
, all_tg_pt
, spec_i_pt
, REGISTER_AND_IGNORE_EXISTING_KEY
);
3676 case PRO_REGISTER_AND_MOVE
:
3677 ret
= core_scsi3_emulate_pro_register_and_move(cmd
, res_key
,
3678 sa_res_key
, aptpl
, unreg
);
3681 pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3682 " action: 0x%02x\n", cdb
[1] & 0x1f);
3683 return TCM_INVALID_CDB_FIELD
;
3687 target_complete_cmd(cmd
, GOOD
);
3692 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3694 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3696 static sense_reason_t
3697 core_scsi3_pri_read_keys(struct se_cmd
*cmd
)
3699 struct se_device
*dev
= cmd
->se_dev
;
3700 struct t10_pr_registration
*pr_reg
;
3702 u32 add_len
= 0, off
= 8;
3704 if (cmd
->data_length
< 8) {
3705 pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3706 " too small\n", cmd
->data_length
);
3707 return TCM_INVALID_CDB_FIELD
;
3710 buf
= transport_kmap_data_sg(cmd
);
3712 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3714 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3715 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3716 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3717 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3719 spin_lock(&dev
->t10_pr
.registration_lock
);
3720 list_for_each_entry(pr_reg
, &dev
->t10_pr
.registration_list
,
3723 * Check for overflow of 8byte PRI READ_KEYS payload and
3724 * next reservation key list descriptor.
3726 if ((add_len
+ 8) > (cmd
->data_length
- 8))
3729 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3730 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3731 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3732 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3733 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3734 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3735 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3736 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3740 spin_unlock(&dev
->t10_pr
.registration_lock
);
3742 buf
[4] = ((add_len
>> 24) & 0xff);
3743 buf
[5] = ((add_len
>> 16) & 0xff);
3744 buf
[6] = ((add_len
>> 8) & 0xff);
3745 buf
[7] = (add_len
& 0xff);
3747 transport_kunmap_data_sg(cmd
);
3753 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3755 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3757 static sense_reason_t
3758 core_scsi3_pri_read_reservation(struct se_cmd
*cmd
)
3760 struct se_device
*dev
= cmd
->se_dev
;
3761 struct t10_pr_registration
*pr_reg
;
3764 u32 add_len
= 16; /* Hardcoded to 16 when a reservation is held. */
3766 if (cmd
->data_length
< 8) {
3767 pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3768 " too small\n", cmd
->data_length
);
3769 return TCM_INVALID_CDB_FIELD
;
3772 buf
= transport_kmap_data_sg(cmd
);
3774 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3776 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3777 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3778 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3779 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3781 spin_lock(&dev
->dev_reservation_lock
);
3782 pr_reg
= dev
->dev_pr_res_holder
;
3785 * Set the hardcoded Additional Length
3787 buf
[4] = ((add_len
>> 24) & 0xff);
3788 buf
[5] = ((add_len
>> 16) & 0xff);
3789 buf
[6] = ((add_len
>> 8) & 0xff);
3790 buf
[7] = (add_len
& 0xff);
3792 if (cmd
->data_length
< 22)
3796 * Set the Reservation key.
3798 * From spc4r17, section 5.7.10:
3799 * A persistent reservation holder has its reservation key
3800 * returned in the parameter data from a PERSISTENT
3801 * RESERVE IN command with READ RESERVATION service action as
3803 * a) For a persistent reservation of the type Write Exclusive
3804 * - All Registrants or Exclusive Access  All Regitrants,
3805 * the reservation key shall be set to zero; or
3806 * b) For all other persistent reservation types, the
3807 * reservation key shall be set to the registered
3808 * reservation key for the I_T nexus that holds the
3809 * persistent reservation.
3811 if ((pr_reg
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
) ||
3812 (pr_reg
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
))
3815 pr_res_key
= pr_reg
->pr_res_key
;
3817 buf
[8] = ((pr_res_key
>> 56) & 0xff);
3818 buf
[9] = ((pr_res_key
>> 48) & 0xff);
3819 buf
[10] = ((pr_res_key
>> 40) & 0xff);
3820 buf
[11] = ((pr_res_key
>> 32) & 0xff);
3821 buf
[12] = ((pr_res_key
>> 24) & 0xff);
3822 buf
[13] = ((pr_res_key
>> 16) & 0xff);
3823 buf
[14] = ((pr_res_key
>> 8) & 0xff);
3824 buf
[15] = (pr_res_key
& 0xff);
3826 * Set the SCOPE and TYPE
3828 buf
[21] = (pr_reg
->pr_res_scope
& 0xf0) |
3829 (pr_reg
->pr_res_type
& 0x0f);
3833 spin_unlock(&dev
->dev_reservation_lock
);
3834 transport_kunmap_data_sg(cmd
);
3840 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3842 * See spc4r17 section 6.13.4 Table 165
3844 static sense_reason_t
3845 core_scsi3_pri_report_capabilities(struct se_cmd
*cmd
)
3847 struct se_device
*dev
= cmd
->se_dev
;
3848 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3850 u16 add_len
= 8; /* Hardcoded to 8. */
3852 if (cmd
->data_length
< 6) {
3853 pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3854 " %u too small\n", cmd
->data_length
);
3855 return TCM_INVALID_CDB_FIELD
;
3858 buf
= transport_kmap_data_sg(cmd
);
3860 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3862 buf
[0] = ((add_len
>> 8) & 0xff);
3863 buf
[1] = (add_len
& 0xff);
3864 buf
[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3865 buf
[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3866 buf
[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3867 buf
[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3869 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3870 * set the TMV: Task Mask Valid bit.
3874 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3876 buf
[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3878 * PTPL_A: Persistence across Target Power Loss Active bit
3880 if (pr_tmpl
->pr_aptpl_active
)
3883 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3885 buf
[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3886 buf
[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3887 buf
[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3888 buf
[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3889 buf
[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3890 buf
[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3892 transport_kunmap_data_sg(cmd
);
3898 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
3900 * See spc4r17 section 6.13.5 Table 168 and 169
3902 static sense_reason_t
3903 core_scsi3_pri_read_full_status(struct se_cmd
*cmd
)
3905 struct se_device
*dev
= cmd
->se_dev
;
3906 struct se_node_acl
*se_nacl
;
3907 struct se_portal_group
*se_tpg
;
3908 struct t10_pr_registration
*pr_reg
, *pr_reg_tmp
;
3909 struct t10_reservation
*pr_tmpl
= &dev
->t10_pr
;
3911 u32 add_desc_len
= 0, add_len
= 0;
3912 u32 off
= 8; /* off into first Full Status descriptor */
3913 int format_code
= 0, pr_res_type
= 0, pr_res_scope
= 0;
3914 int exp_desc_len
, desc_len
;
3915 bool all_reg
= false;
3917 if (cmd
->data_length
< 8) {
3918 pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
3919 " too small\n", cmd
->data_length
);
3920 return TCM_INVALID_CDB_FIELD
;
3923 buf
= transport_kmap_data_sg(cmd
);
3925 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3927 buf
[0] = ((dev
->t10_pr
.pr_generation
>> 24) & 0xff);
3928 buf
[1] = ((dev
->t10_pr
.pr_generation
>> 16) & 0xff);
3929 buf
[2] = ((dev
->t10_pr
.pr_generation
>> 8) & 0xff);
3930 buf
[3] = (dev
->t10_pr
.pr_generation
& 0xff);
3932 spin_lock(&dev
->dev_reservation_lock
);
3933 if (dev
->dev_pr_res_holder
) {
3934 struct t10_pr_registration
*pr_holder
= dev
->dev_pr_res_holder
;
3936 if (pr_holder
->pr_res_type
== PR_TYPE_WRITE_EXCLUSIVE_ALLREG
||
3937 pr_holder
->pr_res_type
== PR_TYPE_EXCLUSIVE_ACCESS_ALLREG
) {
3939 pr_res_type
= pr_holder
->pr_res_type
;
3940 pr_res_scope
= pr_holder
->pr_res_scope
;
3943 spin_unlock(&dev
->dev_reservation_lock
);
3945 spin_lock(&pr_tmpl
->registration_lock
);
3946 list_for_each_entry_safe(pr_reg
, pr_reg_tmp
,
3947 &pr_tmpl
->registration_list
, pr_reg_list
) {
3949 se_nacl
= pr_reg
->pr_reg_nacl
;
3950 se_tpg
= pr_reg
->pr_reg_nacl
->se_tpg
;
3953 atomic_inc_mb(&pr_reg
->pr_res_holders
);
3954 spin_unlock(&pr_tmpl
->registration_lock
);
3956 * Determine expected length of $FABRIC_MOD specific
3957 * TransportID full status descriptor..
3959 exp_desc_len
= target_get_pr_transport_id_len(se_nacl
, pr_reg
,
3961 if (exp_desc_len
< 0 ||
3962 exp_desc_len
+ add_len
> cmd
->data_length
) {
3963 pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
3964 " out of buffer: %d\n", cmd
->data_length
);
3965 spin_lock(&pr_tmpl
->registration_lock
);
3966 atomic_dec_mb(&pr_reg
->pr_res_holders
);
3970 * Set RESERVATION KEY
3972 buf
[off
++] = ((pr_reg
->pr_res_key
>> 56) & 0xff);
3973 buf
[off
++] = ((pr_reg
->pr_res_key
>> 48) & 0xff);
3974 buf
[off
++] = ((pr_reg
->pr_res_key
>> 40) & 0xff);
3975 buf
[off
++] = ((pr_reg
->pr_res_key
>> 32) & 0xff);
3976 buf
[off
++] = ((pr_reg
->pr_res_key
>> 24) & 0xff);
3977 buf
[off
++] = ((pr_reg
->pr_res_key
>> 16) & 0xff);
3978 buf
[off
++] = ((pr_reg
->pr_res_key
>> 8) & 0xff);
3979 buf
[off
++] = (pr_reg
->pr_res_key
& 0xff);
3980 off
+= 4; /* Skip Over Reserved area */
3983 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
3985 if (pr_reg
->pr_reg_all_tg_pt
)
3988 * The struct se_lun pointer will be present for the
3989 * reservation holder for PR_HOLDER bit.
3991 * Also, if this registration is the reservation
3992 * holder or there is an All Registrants reservation
3993 * active, fill in SCOPE and TYPE in the next byte.
3995 if (pr_reg
->pr_res_holder
) {
3997 buf
[off
++] = (pr_reg
->pr_res_scope
& 0xf0) |
3998 (pr_reg
->pr_res_type
& 0x0f);
3999 } else if (all_reg
) {
4001 buf
[off
++] = (pr_res_scope
& 0xf0) |
4002 (pr_res_type
& 0x0f);
4007 off
+= 4; /* Skip over reserved area */
4009 * From spc4r17 6.3.15:
4011 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4012 * IDENTIFIER field contains the relative port identifier (see
4013 * 3.1.120) of the target port that is part of the I_T nexus
4014 * described by this full status descriptor. If the ALL_TG_PT
4015 * bit is set to one, the contents of the RELATIVE TARGET PORT
4016 * IDENTIFIER field are not defined by this standard.
4018 if (!pr_reg
->pr_reg_all_tg_pt
) {
4019 u16 sep_rtpi
= pr_reg
->tg_pt_sep_rtpi
;
4021 buf
[off
++] = ((sep_rtpi
>> 8) & 0xff);
4022 buf
[off
++] = (sep_rtpi
& 0xff);
4024 off
+= 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4026 buf
[off
+4] = se_tpg
->proto_id
;
4029 * Now, have the $FABRIC_MOD fill in the transport ID.
4031 desc_len
= target_get_pr_transport_id(se_nacl
, pr_reg
,
4032 &format_code
, &buf
[off
+4]);
4034 spin_lock(&pr_tmpl
->registration_lock
);
4035 atomic_dec_mb(&pr_reg
->pr_res_holders
);
4040 * Set the ADDITIONAL DESCRIPTOR LENGTH
4042 buf
[off
++] = ((desc_len
>> 24) & 0xff);
4043 buf
[off
++] = ((desc_len
>> 16) & 0xff);
4044 buf
[off
++] = ((desc_len
>> 8) & 0xff);
4045 buf
[off
++] = (desc_len
& 0xff);
4047 * Size of full desctipor header minus TransportID
4048 * containing $FABRIC_MOD specific) initiator device/port
4051 * See spc4r17 Section 6.13.5 Table 169
4053 add_desc_len
= (24 + desc_len
);
4056 add_len
+= add_desc_len
;
4058 spin_unlock(&pr_tmpl
->registration_lock
);
4060 * Set ADDITIONAL_LENGTH
4062 buf
[4] = ((add_len
>> 24) & 0xff);
4063 buf
[5] = ((add_len
>> 16) & 0xff);
4064 buf
[6] = ((add_len
>> 8) & 0xff);
4065 buf
[7] = (add_len
& 0xff);
4067 transport_kunmap_data_sg(cmd
);
4073 target_scsi3_emulate_pr_in(struct se_cmd
*cmd
)
4078 * Following spc2r20 5.5.1 Reservations overview:
4080 * If a logical unit has been reserved by any RESERVE command and is
4081 * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4082 * PERSISTENT RESERVE OUT commands shall conflict regardless of
4083 * initiator or service action and shall terminate with a RESERVATION
4086 if (cmd
->se_dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
) {
4087 pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4088 " SPC-2 reservation is held, returning"
4089 " RESERVATION_CONFLICT\n");
4090 return TCM_RESERVATION_CONFLICT
;
4093 switch (cmd
->t_task_cdb
[1] & 0x1f) {
4095 ret
= core_scsi3_pri_read_keys(cmd
);
4097 case PRI_READ_RESERVATION
:
4098 ret
= core_scsi3_pri_read_reservation(cmd
);
4100 case PRI_REPORT_CAPABILITIES
:
4101 ret
= core_scsi3_pri_report_capabilities(cmd
);
4103 case PRI_READ_FULL_STATUS
:
4104 ret
= core_scsi3_pri_read_full_status(cmd
);
4107 pr_err("Unknown PERSISTENT_RESERVE_IN service"
4108 " action: 0x%02x\n", cmd
->t_task_cdb
[1] & 0x1f);
4109 return TCM_INVALID_CDB_FIELD
;
4113 target_complete_cmd(cmd
, GOOD
);
4118 target_check_reservation(struct se_cmd
*cmd
)
4120 struct se_device
*dev
= cmd
->se_dev
;
4125 if (dev
->se_hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
)
4127 if (dev
->transport
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH
)
4130 spin_lock(&dev
->dev_reservation_lock
);
4131 if (dev
->dev_reservation_flags
& DRF_SPC2_RESERVATIONS
)
4132 ret
= target_scsi2_reservation_check(cmd
);
4134 ret
= target_scsi3_pr_reservation_check(cmd
);
4135 spin_unlock(&dev
->dev_reservation_lock
);